]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
c++: DECL_LOCAL_FUNCTION_P -> DECL_LOCAL_DECL_P
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2020 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
48 #include "c-family/known-headers.h"
49
50 \f
51 /* The lexer. */
52
53 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
54 and c-lex.c) and the C++ parser. */
55
56 /* The various kinds of non integral constant we encounter. */
57 enum non_integral_constant {
58 NIC_NONE,
59 /* floating-point literal */
60 NIC_FLOAT,
61 /* %<this%> */
62 NIC_THIS,
63 /* %<__FUNCTION__%> */
64 NIC_FUNC_NAME,
65 /* %<__PRETTY_FUNCTION__%> */
66 NIC_PRETTY_FUNC,
67 /* %<__func__%> */
68 NIC_C99_FUNC,
69 /* "%<va_arg%> */
70 NIC_VA_ARG,
71 /* a cast */
72 NIC_CAST,
73 /* %<typeid%> operator */
74 NIC_TYPEID,
75 /* non-constant compound literals */
76 NIC_NCC,
77 /* a function call */
78 NIC_FUNC_CALL,
79 /* an increment */
80 NIC_INC,
81 /* an decrement */
82 NIC_DEC,
83 /* an array reference */
84 NIC_ARRAY_REF,
85 /* %<->%> */
86 NIC_ARROW,
87 /* %<.%> */
88 NIC_POINT,
89 /* the address of a label */
90 NIC_ADDR_LABEL,
91 /* %<*%> */
92 NIC_STAR,
93 /* %<&%> */
94 NIC_ADDR,
95 /* %<++%> */
96 NIC_PREINCREMENT,
97 /* %<--%> */
98 NIC_PREDECREMENT,
99 /* %<new%> */
100 NIC_NEW,
101 /* %<delete%> */
102 NIC_DEL,
103 /* calls to overloaded operators */
104 NIC_OVERLOADED,
105 /* an assignment */
106 NIC_ASSIGNMENT,
107 /* a comma operator */
108 NIC_COMMA,
109 /* a call to a constructor */
110 NIC_CONSTRUCTOR,
111 /* a transaction expression */
112 NIC_TRANSACTION
113 };
114
115 /* The various kinds of errors about name-lookup failing. */
116 enum name_lookup_error {
117 /* NULL */
118 NLE_NULL,
119 /* is not a type */
120 NLE_TYPE,
121 /* is not a class or namespace */
122 NLE_CXX98,
123 /* is not a class, namespace, or enumeration */
124 NLE_NOT_CXX98
125 };
126
127 /* The various kinds of required token */
128 enum required_token {
129 RT_NONE,
130 RT_SEMICOLON, /* ';' */
131 RT_OPEN_PAREN, /* '(' */
132 RT_CLOSE_BRACE, /* '}' */
133 RT_OPEN_BRACE, /* '{' */
134 RT_CLOSE_SQUARE, /* ']' */
135 RT_OPEN_SQUARE, /* '[' */
136 RT_COMMA, /* ',' */
137 RT_SCOPE, /* '::' */
138 RT_LESS, /* '<' */
139 RT_GREATER, /* '>' */
140 RT_EQ, /* '=' */
141 RT_ELLIPSIS, /* '...' */
142 RT_MULT, /* '*' */
143 RT_COMPL, /* '~' */
144 RT_COLON, /* ':' */
145 RT_COLON_SCOPE, /* ':' or '::' */
146 RT_CLOSE_PAREN, /* ')' */
147 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
148 RT_PRAGMA_EOL, /* end of line */
149 RT_NAME, /* identifier */
150
151 /* The type is CPP_KEYWORD */
152 RT_NEW, /* new */
153 RT_DELETE, /* delete */
154 RT_RETURN, /* return */
155 RT_WHILE, /* while */
156 RT_EXTERN, /* extern */
157 RT_STATIC_ASSERT, /* static_assert */
158 RT_DECLTYPE, /* decltype */
159 RT_OPERATOR, /* operator */
160 RT_CLASS, /* class */
161 RT_TEMPLATE, /* template */
162 RT_NAMESPACE, /* namespace */
163 RT_USING, /* using */
164 RT_ASM, /* asm */
165 RT_TRY, /* try */
166 RT_CATCH, /* catch */
167 RT_THROW, /* throw */
168 RT_AUTO, /* auto */
169 RT_LABEL, /* __label__ */
170 RT_AT_TRY, /* @try */
171 RT_AT_SYNCHRONIZED, /* @synchronized */
172 RT_AT_THROW, /* @throw */
173
174 RT_SELECT, /* selection-statement */
175 RT_ITERATION, /* iteration-statement */
176 RT_JUMP, /* jump-statement */
177 RT_CLASS_KEY, /* class-key */
178 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
179 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
180 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
181 RT_TRANSACTION_CANCEL, /* __transaction_cancel */
182
183 RT_CO_YIELD /* co_yield */
184 };
185
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
188
189 class type_id_in_expr_sentinel
190 {
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
200 };
201
202 /* Prototypes. */
203
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (unsigned, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
246
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
249 static tree cp_parser_late_noexcept_specifier
250 (cp_parser *, tree);
251 static void noexcept_override_late_checks
252 (tree, tree);
253
254 static void cp_parser_initial_pragma
255 (cp_token *);
256
257 static bool cp_parser_omp_declare_reduction_exprs
258 (tree, cp_parser *);
259 static void cp_finalize_oacc_routine
260 (cp_parser *, tree, bool);
261
262 /* Manifest constants. */
263 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
264 #define CP_SAVED_TOKEN_STACK 5
265
266 /* Variables. */
267
268 /* The stream to which debugging output should be written. */
269 static FILE *cp_lexer_debug_stream;
270
271 /* Nonzero if we are parsing an unevaluated operand: an operand to
272 sizeof, typeof, or alignof. */
273 int cp_unevaluated_operand;
274
275 /* Dump up to NUM tokens in BUFFER to FILE starting with token
276 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
277 first token in BUFFER. If NUM is 0, dump all the tokens. If
278 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
279 highlighted by surrounding it in [[ ]]. */
280
281 static void
282 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
283 cp_token *start_token, unsigned num,
284 cp_token *curr_token)
285 {
286 unsigned i, nprinted;
287 cp_token *token;
288 bool do_print;
289
290 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
291
292 if (buffer == NULL)
293 return;
294
295 if (num == 0)
296 num = buffer->length ();
297
298 if (start_token == NULL)
299 start_token = buffer->address ();
300
301 if (start_token > buffer->address ())
302 {
303 cp_lexer_print_token (file, &(*buffer)[0]);
304 fprintf (file, " ... ");
305 }
306
307 do_print = false;
308 nprinted = 0;
309 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
310 {
311 if (token == start_token)
312 do_print = true;
313
314 if (!do_print)
315 continue;
316
317 nprinted++;
318 if (token == curr_token)
319 fprintf (file, "[[");
320
321 cp_lexer_print_token (file, token);
322
323 if (token == curr_token)
324 fprintf (file, "]]");
325
326 switch (token->type)
327 {
328 case CPP_SEMICOLON:
329 case CPP_OPEN_BRACE:
330 case CPP_CLOSE_BRACE:
331 case CPP_EOF:
332 fputc ('\n', file);
333 break;
334
335 default:
336 fputc (' ', file);
337 }
338 }
339
340 if (i == num && i < buffer->length ())
341 {
342 fprintf (file, " ... ");
343 cp_lexer_print_token (file, &buffer->last ());
344 }
345
346 fprintf (file, "\n");
347 }
348
349
350 /* Dump all tokens in BUFFER to stderr. */
351
352 void
353 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
354 {
355 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
356 }
357
358 DEBUG_FUNCTION void
359 debug (vec<cp_token, va_gc> &ref)
360 {
361 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
362 }
363
364 DEBUG_FUNCTION void
365 debug (vec<cp_token, va_gc> *ptr)
366 {
367 if (ptr)
368 debug (*ptr);
369 else
370 fprintf (stderr, "<nil>\n");
371 }
372
373
374 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
375 description for T. */
376
377 static void
378 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
379 {
380 if (t)
381 {
382 fprintf (file, "%s: ", desc);
383 print_node_brief (file, "", t, 0);
384 }
385 }
386
387
388 /* Dump parser context C to FILE. */
389
390 static void
391 cp_debug_print_context (FILE *file, cp_parser_context *c)
392 {
393 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
394 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
395 print_node_brief (file, "", c->object_type, 0);
396 fprintf (file, "}\n");
397 }
398
399
400 /* Print the stack of parsing contexts to FILE starting with FIRST. */
401
402 static void
403 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
404 {
405 unsigned i;
406 cp_parser_context *c;
407
408 fprintf (file, "Parsing context stack:\n");
409 for (i = 0, c = first; c; c = c->next, i++)
410 {
411 fprintf (file, "\t#%u: ", i);
412 cp_debug_print_context (file, c);
413 }
414 }
415
416
417 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
418
419 static void
420 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
421 {
422 if (flag)
423 fprintf (file, "%s: true\n", desc);
424 }
425
426
427 /* Print an unparsed function entry UF to FILE. */
428
429 static void
430 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
431 {
432 unsigned i;
433 cp_default_arg_entry *default_arg_fn;
434 tree fn;
435
436 fprintf (file, "\tFunctions with default args:\n");
437 for (i = 0;
438 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
439 i++)
440 {
441 fprintf (file, "\t\tClass type: ");
442 print_node_brief (file, "", default_arg_fn->class_type, 0);
443 fprintf (file, "\t\tDeclaration: ");
444 print_node_brief (file, "", default_arg_fn->decl, 0);
445 fprintf (file, "\n");
446 }
447
448 fprintf (file, "\n\tFunctions with definitions that require "
449 "post-processing\n\t\t");
450 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
451 {
452 print_node_brief (file, "", fn, 0);
453 fprintf (file, " ");
454 }
455 fprintf (file, "\n");
456
457 fprintf (file, "\n\tNon-static data members with initializers that require "
458 "post-processing\n\t\t");
459 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
460 {
461 print_node_brief (file, "", fn, 0);
462 fprintf (file, " ");
463 }
464 fprintf (file, "\n");
465 }
466
467
468 /* Print the stack of unparsed member functions S to FILE. */
469
470 static void
471 cp_debug_print_unparsed_queues (FILE *file,
472 vec<cp_unparsed_functions_entry, va_gc> *s)
473 {
474 unsigned i;
475 cp_unparsed_functions_entry *uf;
476
477 fprintf (file, "Unparsed functions\n");
478 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
479 {
480 fprintf (file, "#%u:\n", i);
481 cp_debug_print_unparsed_function (file, uf);
482 }
483 }
484
485
486 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
487 the given PARSER. If FILE is NULL, the output is printed on stderr. */
488
489 static void
490 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
491 {
492 cp_token *next_token, *first_token, *start_token;
493
494 if (file == NULL)
495 file = stderr;
496
497 next_token = parser->lexer->next_token;
498 first_token = parser->lexer->buffer->address ();
499 start_token = (next_token > first_token + window_size / 2)
500 ? next_token - window_size / 2
501 : first_token;
502 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
503 next_token);
504 }
505
506
507 /* Dump debugging information for the given PARSER. If FILE is NULL,
508 the output is printed on stderr. */
509
510 void
511 cp_debug_parser (FILE *file, cp_parser *parser)
512 {
513 const size_t window_size = 20;
514 cp_token *token;
515 expanded_location eloc;
516
517 if (file == NULL)
518 file = stderr;
519
520 fprintf (file, "Parser state\n\n");
521 fprintf (file, "Number of tokens: %u\n",
522 vec_safe_length (parser->lexer->buffer));
523 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
524 cp_debug_print_tree_if_set (file, "Object scope",
525 parser->object_scope);
526 cp_debug_print_tree_if_set (file, "Qualifying scope",
527 parser->qualifying_scope);
528 cp_debug_print_context_stack (file, parser->context);
529 cp_debug_print_flag (file, "Allow GNU extensions",
530 parser->allow_gnu_extensions_p);
531 cp_debug_print_flag (file, "'>' token is greater-than",
532 parser->greater_than_is_operator_p);
533 cp_debug_print_flag (file, "Default args allowed in current "
534 "parameter list", parser->default_arg_ok_p);
535 cp_debug_print_flag (file, "Parsing integral constant-expression",
536 parser->integral_constant_expression_p);
537 cp_debug_print_flag (file, "Allow non-constant expression in current "
538 "constant-expression",
539 parser->allow_non_integral_constant_expression_p);
540 cp_debug_print_flag (file, "Seen non-constant expression",
541 parser->non_integral_constant_expression_p);
542 cp_debug_print_flag (file, "Local names forbidden in current context",
543 (parser->local_variables_forbidden_p
544 & LOCAL_VARS_FORBIDDEN));
545 cp_debug_print_flag (file, "'this' forbidden in current context",
546 (parser->local_variables_forbidden_p
547 & THIS_FORBIDDEN));
548 cp_debug_print_flag (file, "In unbraced linkage specification",
549 parser->in_unbraced_linkage_specification_p);
550 cp_debug_print_flag (file, "Parsing a declarator",
551 parser->in_declarator_p);
552 cp_debug_print_flag (file, "In template argument list",
553 parser->in_template_argument_list_p);
554 cp_debug_print_flag (file, "Parsing an iteration statement",
555 parser->in_statement & IN_ITERATION_STMT);
556 cp_debug_print_flag (file, "Parsing a switch statement",
557 parser->in_statement & IN_SWITCH_STMT);
558 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
559 parser->in_statement & IN_OMP_BLOCK);
560 cp_debug_print_flag (file, "Parsing an OpenMP loop",
561 parser->in_statement & IN_OMP_FOR);
562 cp_debug_print_flag (file, "Parsing an if statement",
563 parser->in_statement & IN_IF_STMT);
564 cp_debug_print_flag (file, "Parsing a type-id in an expression "
565 "context", parser->in_type_id_in_expr_p);
566 cp_debug_print_flag (file, "String expressions should be translated "
567 "to execution character set",
568 parser->translate_strings_p);
569 cp_debug_print_flag (file, "Parsing function body outside of a "
570 "local class", parser->in_function_body);
571 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
572 parser->colon_corrects_to_scope_p);
573 cp_debug_print_flag (file, "Colon doesn't start a class definition",
574 parser->colon_doesnt_start_class_def_p);
575 if (parser->type_definition_forbidden_message)
576 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
577 parser->type_definition_forbidden_message,
578 parser->type_definition_forbidden_message_arg
579 ? parser->type_definition_forbidden_message_arg : "<none>");
580 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
581 fprintf (file, "Number of class definitions in progress: %u\n",
582 parser->num_classes_being_defined);
583 fprintf (file, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser->num_template_parameter_lists);
585 cp_debug_parser_tokens (file, parser, window_size);
586 token = parser->lexer->next_token;
587 fprintf (file, "Next token to parse:\n");
588 fprintf (file, "\tToken: ");
589 cp_lexer_print_token (file, token);
590 eloc = expand_location (token->location);
591 fprintf (file, "\n\tFile: %s\n", eloc.file);
592 fprintf (file, "\tLine: %d\n", eloc.line);
593 fprintf (file, "\tColumn: %d\n", eloc.column);
594 }
595
596 DEBUG_FUNCTION void
597 debug (cp_parser &ref)
598 {
599 cp_debug_parser (stderr, &ref);
600 }
601
602 DEBUG_FUNCTION void
603 debug (cp_parser *ptr)
604 {
605 if (ptr)
606 debug (*ptr);
607 else
608 fprintf (stderr, "<nil>\n");
609 }
610
611 /* Allocate memory for a new lexer object and return it. */
612
613 static cp_lexer *
614 cp_lexer_alloc (void)
615 {
616 /* Allocate the memory. */
617 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
618
619 /* Initially we are not debugging. */
620 lexer->debugging_p = false;
621
622 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
623
624 /* Create the buffer. */
625 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
626
627 return lexer;
628 }
629
630 /* Create a new main C++ lexer, the lexer that gets tokens from the
631 preprocessor. */
632
633 static cp_lexer *
634 cp_lexer_new_main (void)
635 {
636 cp_token token;
637
638 /* It's possible that parsing the first pragma will load a PCH file,
639 which is a GC collection point. So we have to do that before
640 allocating any memory. */
641 cp_lexer_get_preprocessor_token (0, &token);
642 cp_parser_initial_pragma (&token);
643 c_common_no_more_pch ();
644
645 cp_lexer *lexer = cp_lexer_alloc ();
646 /* Put the first token in the buffer. */
647 cp_token *tok = lexer->buffer->quick_push (token);
648
649 /* Get the remaining tokens from the preprocessor. */
650 while (tok->type != CPP_EOF)
651 {
652 tok = vec_safe_push (lexer->buffer, cp_token ());
653 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
654 }
655
656 lexer->next_token = lexer->buffer->address ();
657 lexer->last_token = lexer->next_token
658 + lexer->buffer->length ()
659 - 1;
660
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
664
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
667 }
668
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
671
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
674 {
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
678
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681
682 /* Insert an EOF token. */
683 lexer->saved_type = last->type;
684 lexer->saved_keyword = last->keyword;
685 last->type = CPP_EOF;
686 last->keyword = RID_MAX;
687
688 lexer->next_token = first;
689 lexer->last_token = last;
690
691 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
692
693 /* Initially we are not debugging. */
694 lexer->debugging_p = false;
695
696 gcc_assert (!lexer->next_token->purged_p
697 && !lexer->last_token->purged_p);
698 return lexer;
699 }
700
701 /* Frees all resources associated with LEXER. */
702
703 static void
704 cp_lexer_destroy (cp_lexer *lexer)
705 {
706 if (lexer->buffer)
707 vec_free (lexer->buffer);
708 else
709 {
710 /* Restore the token we overwrite with EOF. */
711 lexer->last_token->type = lexer->saved_type;
712 lexer->last_token->keyword = lexer->saved_keyword;
713 }
714 lexer->saved_tokens.release ();
715 ggc_free (lexer);
716 }
717
718 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
719 be used. The point of this flag is to help the compiler to fold away calls
720 to cp_lexer_debugging_p within this source file at compile time, when the
721 lexer is not being debugged. */
722
723 #define LEXER_DEBUGGING_ENABLED_P false
724
725 /* Returns nonzero if debugging information should be output. */
726
727 static inline bool
728 cp_lexer_debugging_p (cp_lexer *lexer)
729 {
730 if (!LEXER_DEBUGGING_ENABLED_P)
731 return false;
732
733 return lexer->debugging_p;
734 }
735
736
737 static inline cp_token_position
738 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
739 {
740 return lexer->next_token - previous_p;
741 }
742
743 static inline cp_token *
744 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
745 {
746 return pos;
747 }
748
749 static inline void
750 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
751 {
752 lexer->next_token = cp_lexer_token_at (lexer, pos);
753 }
754
755 static inline cp_token_position
756 cp_lexer_previous_token_position (cp_lexer *lexer)
757 {
758 return cp_lexer_token_position (lexer, true);
759 }
760
761 static inline cp_token *
762 cp_lexer_previous_token (cp_lexer *lexer)
763 {
764 cp_token_position tp = cp_lexer_previous_token_position (lexer);
765
766 /* Skip past purged tokens. */
767 while (tp->purged_p)
768 {
769 gcc_assert (tp != vec_safe_address (lexer->buffer));
770 tp--;
771 }
772
773 return cp_lexer_token_at (lexer, tp);
774 }
775
776 /* Same as above, but return NULL when the lexer doesn't own the token
777 buffer or if the next_token is at the start of the token
778 vector or if all previous tokens are purged. */
779
780 static cp_token *
781 cp_lexer_safe_previous_token (cp_lexer *lexer)
782 {
783 if (lexer->buffer
784 && lexer->next_token != lexer->buffer->address ())
785 {
786 cp_token_position tp = cp_lexer_previous_token_position (lexer);
787
788 /* Skip past purged tokens. */
789 while (tp->purged_p)
790 {
791 if (tp == lexer->buffer->address ())
792 return NULL;
793 tp--;
794 }
795 return cp_lexer_token_at (lexer, tp);
796 }
797
798 return NULL;
799 }
800
801 /* Overload for make_location, taking the lexer to mean the location of the
802 previous token. */
803
804 static inline location_t
805 make_location (location_t caret, location_t start, cp_lexer *lexer)
806 {
807 cp_token *t = cp_lexer_previous_token (lexer);
808 return make_location (caret, start, t->location);
809 }
810
811 /* nonzero if we are presently saving tokens. */
812
813 static inline int
814 cp_lexer_saving_tokens (const cp_lexer* lexer)
815 {
816 return lexer->saved_tokens.length () != 0;
817 }
818
819 /* Store the next token from the preprocessor in *TOKEN. Return true
820 if we reach EOF. If LEXER is NULL, assume we are handling an
821 initial #pragma pch_preprocess, and thus want the lexer to return
822 processed strings. */
823
824 static void
825 cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
826 {
827 static int is_extern_c = 0;
828
829 /* Get a new token from the preprocessor. */
830 token->type
831 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
832 flags);
833 token->keyword = RID_MAX;
834 token->purged_p = false;
835 token->error_reported = false;
836 token->tree_check_p = false;
837
838 /* On some systems, some header files are surrounded by an
839 implicit extern "C" block. Set a flag in the token if it
840 comes from such a header. */
841 is_extern_c += pending_lang_change;
842 pending_lang_change = 0;
843 token->implicit_extern_c = is_extern_c > 0;
844
845 /* Check to see if this token is a keyword. */
846 if (token->type == CPP_NAME)
847 {
848 if (IDENTIFIER_KEYWORD_P (token->u.value))
849 {
850 /* Mark this token as a keyword. */
851 token->type = CPP_KEYWORD;
852 /* Record which keyword. */
853 token->keyword = C_RID_CODE (token->u.value);
854 }
855 else
856 {
857 if (warn_cxx11_compat
858 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
859 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
860 {
861 /* Warn about the C++0x keyword (but still treat it as
862 an identifier). */
863 warning_at (token->location, OPT_Wc__11_compat,
864 "identifier %qE is a keyword in C++11",
865 token->u.value);
866
867 /* Clear out the C_RID_CODE so we don't warn about this
868 particular identifier-turned-keyword again. */
869 C_SET_RID_CODE (token->u.value, RID_MAX);
870 }
871 if (warn_cxx20_compat
872 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
873 && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
874 {
875 /* Warn about the C++20 keyword (but still treat it as
876 an identifier). */
877 warning_at (token->location, OPT_Wc__20_compat,
878 "identifier %qE is a keyword in C++20",
879 token->u.value);
880
881 /* Clear out the C_RID_CODE so we don't warn about this
882 particular identifier-turned-keyword again. */
883 C_SET_RID_CODE (token->u.value, RID_MAX);
884 }
885
886 token->keyword = RID_MAX;
887 }
888 }
889 else if (token->type == CPP_AT_NAME)
890 {
891 /* This only happens in Objective-C++; it must be a keyword. */
892 token->type = CPP_KEYWORD;
893 switch (C_RID_CODE (token->u.value))
894 {
895 /* Replace 'class' with '@class', 'private' with '@private',
896 etc. This prevents confusion with the C++ keyword
897 'class', and makes the tokens consistent with other
898 Objective-C 'AT' keywords. For example '@class' is
899 reported as RID_AT_CLASS which is consistent with
900 '@synchronized', which is reported as
901 RID_AT_SYNCHRONIZED.
902 */
903 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
904 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
905 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
906 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
907 case RID_THROW: token->keyword = RID_AT_THROW; break;
908 case RID_TRY: token->keyword = RID_AT_TRY; break;
909 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
910 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
911 default: token->keyword = C_RID_CODE (token->u.value);
912 }
913 }
914 }
915
916 /* Update the globals input_location and the input file stack from TOKEN. */
917 static inline void
918 cp_lexer_set_source_position_from_token (cp_token *token)
919 {
920 input_location = token->location;
921 }
922
923 /* Update the globals input_location and the input file stack from LEXER. */
924 static inline void
925 cp_lexer_set_source_position (cp_lexer *lexer)
926 {
927 cp_token *token = cp_lexer_peek_token (lexer);
928 cp_lexer_set_source_position_from_token (token);
929 }
930
931 /* Return a pointer to the next token in the token stream, but do not
932 consume it. */
933
934 static inline cp_token *
935 cp_lexer_peek_token (cp_lexer *lexer)
936 {
937 if (cp_lexer_debugging_p (lexer))
938 {
939 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
940 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
941 putc ('\n', cp_lexer_debug_stream);
942 }
943 return lexer->next_token;
944 }
945
946 /* Return true if the next token has the indicated TYPE. */
947
948 static inline bool
949 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
950 {
951 return cp_lexer_peek_token (lexer)->type == type;
952 }
953
954 /* Return true if the next token does not have the indicated TYPE. */
955
956 static inline bool
957 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
958 {
959 return !cp_lexer_next_token_is (lexer, type);
960 }
961
962 /* Return true if the next token is the indicated KEYWORD. */
963
964 static inline bool
965 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
966 {
967 return cp_lexer_peek_token (lexer)->keyword == keyword;
968 }
969
970 static inline bool
971 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
972 {
973 return cp_lexer_peek_nth_token (lexer, n)->type == type;
974 }
975
976 static inline bool
977 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
978 {
979 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
980 }
981
982 /* Return true if KEYWORD can start a decl-specifier. */
983
984 bool
985 cp_keyword_starts_decl_specifier_p (enum rid keyword)
986 {
987 switch (keyword)
988 {
989 /* auto specifier: storage-class-specifier in C++,
990 simple-type-specifier in C++0x. */
991 case RID_AUTO:
992 /* Storage classes. */
993 case RID_REGISTER:
994 case RID_STATIC:
995 case RID_EXTERN:
996 case RID_MUTABLE:
997 case RID_THREAD:
998 /* Elaborated type specifiers. */
999 case RID_ENUM:
1000 case RID_CLASS:
1001 case RID_STRUCT:
1002 case RID_UNION:
1003 case RID_TYPENAME:
1004 /* Simple type specifiers. */
1005 case RID_CHAR:
1006 case RID_CHAR8:
1007 case RID_CHAR16:
1008 case RID_CHAR32:
1009 case RID_WCHAR:
1010 case RID_BOOL:
1011 case RID_SHORT:
1012 case RID_INT:
1013 case RID_LONG:
1014 case RID_SIGNED:
1015 case RID_UNSIGNED:
1016 case RID_FLOAT:
1017 case RID_DOUBLE:
1018 case RID_VOID:
1019 /* GNU extensions. */
1020 case RID_ATTRIBUTE:
1021 case RID_TYPEOF:
1022 /* C++11 extensions. */
1023 case RID_DECLTYPE:
1024 case RID_UNDERLYING_TYPE:
1025 case RID_CONSTEXPR:
1026 /* C++20 extensions. */
1027 case RID_CONSTINIT:
1028 case RID_CONSTEVAL:
1029 return true;
1030
1031 default:
1032 if (keyword >= RID_FIRST_INT_N
1033 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1034 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1035 return true;
1036 return false;
1037 }
1038 }
1039
1040 /* Return true if the next token is a keyword for a decl-specifier. */
1041
1042 static bool
1043 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1044 {
1045 cp_token *token;
1046
1047 token = cp_lexer_peek_token (lexer);
1048 return cp_keyword_starts_decl_specifier_p (token->keyword);
1049 }
1050
1051 /* Returns TRUE iff the token T begins a decltype type. */
1052
1053 static bool
1054 token_is_decltype (cp_token *t)
1055 {
1056 return (t->keyword == RID_DECLTYPE
1057 || t->type == CPP_DECLTYPE);
1058 }
1059
1060 /* Returns TRUE iff the next token begins a decltype type. */
1061
1062 static bool
1063 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1064 {
1065 cp_token *t = cp_lexer_peek_token (lexer);
1066 return token_is_decltype (t);
1067 }
1068
1069 /* Called when processing a token with tree_check_value; perform or defer the
1070 associated checks and return the value. */
1071
1072 static tree
1073 saved_checks_value (struct tree_check *check_value)
1074 {
1075 /* Perform any access checks that were deferred. */
1076 vec<deferred_access_check, va_gc> *checks;
1077 deferred_access_check *chk;
1078 checks = check_value->checks;
1079 if (checks)
1080 {
1081 int i;
1082 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1083 perform_or_defer_access_check (chk->binfo,
1084 chk->decl,
1085 chk->diag_decl, tf_warning_or_error);
1086 }
1087 /* Return the stored value. */
1088 return check_value->value;
1089 }
1090
1091 /* Return a pointer to the Nth token in the token stream. If N is 1,
1092 then this is precisely equivalent to cp_lexer_peek_token (except
1093 that it is not inline). One would like to disallow that case, but
1094 there is one case (cp_parser_nth_token_starts_template_id) where
1095 the caller passes a variable for N and it might be 1. */
1096
1097 static cp_token *
1098 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1099 {
1100 cp_token *token;
1101
1102 /* N is 1-based, not zero-based. */
1103 gcc_assert (n > 0);
1104
1105 if (cp_lexer_debugging_p (lexer))
1106 fprintf (cp_lexer_debug_stream,
1107 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1108
1109 --n;
1110 token = lexer->next_token;
1111 while (n && token->type != CPP_EOF)
1112 {
1113 ++token;
1114 if (!token->purged_p)
1115 --n;
1116 }
1117
1118 if (cp_lexer_debugging_p (lexer))
1119 {
1120 cp_lexer_print_token (cp_lexer_debug_stream, token);
1121 putc ('\n', cp_lexer_debug_stream);
1122 }
1123
1124 return token;
1125 }
1126
1127 /* Return the next token, and advance the lexer's next_token pointer
1128 to point to the next non-purged token. */
1129
1130 static cp_token *
1131 cp_lexer_consume_token (cp_lexer* lexer)
1132 {
1133 cp_token *token = lexer->next_token;
1134
1135 do
1136 {
1137 gcc_assert (token->type != CPP_EOF);
1138 lexer->next_token++;
1139 }
1140 while (lexer->next_token->purged_p);
1141
1142 cp_lexer_set_source_position_from_token (token);
1143
1144 /* Provide debugging output. */
1145 if (cp_lexer_debugging_p (lexer))
1146 {
1147 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1148 cp_lexer_print_token (cp_lexer_debug_stream, token);
1149 putc ('\n', cp_lexer_debug_stream);
1150 }
1151
1152 return token;
1153 }
1154
1155 /* Permanently remove the next token from the token stream, and
1156 advance the next_token pointer to refer to the next non-purged
1157 token. */
1158
1159 static void
1160 cp_lexer_purge_token (cp_lexer *lexer)
1161 {
1162 cp_token *tok = lexer->next_token;
1163
1164 gcc_assert (tok->type != CPP_EOF);
1165 tok->purged_p = true;
1166 tok->location = UNKNOWN_LOCATION;
1167 tok->u.value = NULL_TREE;
1168 tok->keyword = RID_MAX;
1169
1170 do
1171 tok++;
1172 while (tok->purged_p);
1173 lexer->next_token = tok;
1174 }
1175
1176 /* Permanently remove all tokens after TOK, up to, but not
1177 including, the token that will be returned next by
1178 cp_lexer_peek_token. */
1179
1180 static void
1181 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1182 {
1183 cp_token *peek = lexer->next_token;
1184
1185 gcc_assert (tok < peek);
1186
1187 for (tok++; tok != peek; tok++)
1188 {
1189 tok->purged_p = true;
1190 tok->location = UNKNOWN_LOCATION;
1191 tok->u.value = NULL_TREE;
1192 tok->keyword = RID_MAX;
1193 }
1194 }
1195
1196 /* Begin saving tokens. All tokens consumed after this point will be
1197 preserved. */
1198
1199 static void
1200 cp_lexer_save_tokens (cp_lexer* lexer)
1201 {
1202 /* Provide debugging output. */
1203 if (cp_lexer_debugging_p (lexer))
1204 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1205
1206 lexer->saved_tokens.safe_push (lexer->next_token);
1207 }
1208
1209 /* Commit to the portion of the token stream most recently saved. */
1210
1211 static void
1212 cp_lexer_commit_tokens (cp_lexer* lexer)
1213 {
1214 /* Provide debugging output. */
1215 if (cp_lexer_debugging_p (lexer))
1216 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1217
1218 lexer->saved_tokens.pop ();
1219 }
1220
1221 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1222 to the token stream. Stop saving tokens. */
1223
1224 static void
1225 cp_lexer_rollback_tokens (cp_lexer* lexer)
1226 {
1227 /* Provide debugging output. */
1228 if (cp_lexer_debugging_p (lexer))
1229 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1230
1231 lexer->next_token = lexer->saved_tokens.pop ();
1232 }
1233
1234 /* RAII wrapper around the above functions, with sanity checking. Creating
1235 a variable saves tokens, which are committed when the variable is
1236 destroyed unless they are explicitly rolled back by calling the rollback
1237 member function. */
1238
1239 struct saved_token_sentinel
1240 {
1241 cp_lexer *lexer;
1242 unsigned len;
1243 bool commit;
1244 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1245 {
1246 len = lexer->saved_tokens.length ();
1247 cp_lexer_save_tokens (lexer);
1248 }
1249 void rollback ()
1250 {
1251 cp_lexer_rollback_tokens (lexer);
1252 commit = false;
1253 }
1254 ~saved_token_sentinel()
1255 {
1256 if (commit)
1257 cp_lexer_commit_tokens (lexer);
1258 gcc_assert (lexer->saved_tokens.length () == len);
1259 }
1260 };
1261
1262 /* Print a representation of the TOKEN on the STREAM. */
1263
1264 static void
1265 cp_lexer_print_token (FILE * stream, cp_token *token)
1266 {
1267 /* We don't use cpp_type2name here because the parser defines
1268 a few tokens of its own. */
1269 static const char *const token_names[] = {
1270 /* cpplib-defined token types */
1271 #define OP(e, s) #e,
1272 #define TK(e, s) #e,
1273 TTYPE_TABLE
1274 #undef OP
1275 #undef TK
1276 /* C++ parser token types - see "Manifest constants", above. */
1277 "KEYWORD",
1278 "TEMPLATE_ID",
1279 "NESTED_NAME_SPECIFIER",
1280 };
1281
1282 /* For some tokens, print the associated data. */
1283 switch (token->type)
1284 {
1285 case CPP_KEYWORD:
1286 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1287 For example, `struct' is mapped to an INTEGER_CST. */
1288 if (!identifier_p (token->u.value))
1289 break;
1290 /* fall through */
1291 case CPP_NAME:
1292 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1293 break;
1294
1295 case CPP_STRING:
1296 case CPP_STRING16:
1297 case CPP_STRING32:
1298 case CPP_WSTRING:
1299 case CPP_UTF8STRING:
1300 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1301 break;
1302
1303 case CPP_NUMBER:
1304 print_generic_expr (stream, token->u.value);
1305 break;
1306
1307 default:
1308 /* If we have a name for the token, print it out. Otherwise, we
1309 simply give the numeric code. */
1310 if (token->type < ARRAY_SIZE(token_names))
1311 fputs (token_names[token->type], stream);
1312 else
1313 fprintf (stream, "[%d]", token->type);
1314 break;
1315 }
1316 }
1317
1318 DEBUG_FUNCTION void
1319 debug (cp_token &ref)
1320 {
1321 cp_lexer_print_token (stderr, &ref);
1322 fprintf (stderr, "\n");
1323 }
1324
1325 DEBUG_FUNCTION void
1326 debug (cp_token *ptr)
1327 {
1328 if (ptr)
1329 debug (*ptr);
1330 else
1331 fprintf (stderr, "<nil>\n");
1332 }
1333
1334
1335 /* Start emitting debugging information. */
1336
1337 static void
1338 cp_lexer_start_debugging (cp_lexer* lexer)
1339 {
1340 if (!LEXER_DEBUGGING_ENABLED_P)
1341 fatal_error (input_location,
1342 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1343
1344 lexer->debugging_p = true;
1345 cp_lexer_debug_stream = stderr;
1346 }
1347
1348 /* Stop emitting debugging information. */
1349
1350 static void
1351 cp_lexer_stop_debugging (cp_lexer* lexer)
1352 {
1353 if (!LEXER_DEBUGGING_ENABLED_P)
1354 fatal_error (input_location,
1355 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1356
1357 lexer->debugging_p = false;
1358 cp_lexer_debug_stream = NULL;
1359 }
1360
1361 /* Create a new cp_token_cache, representing a range of tokens. */
1362
1363 static cp_token_cache *
1364 cp_token_cache_new (cp_token *first, cp_token *last)
1365 {
1366 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1367 cache->first = first;
1368 cache->last = last;
1369 return cache;
1370 }
1371
1372 /* Diagnose if #pragma omp declare simd isn't followed immediately
1373 by function declaration or definition. */
1374
1375 static inline void
1376 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1377 {
1378 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1379 {
1380 error ("%<#pragma omp declare %s%> not immediately followed by "
1381 "function declaration or definition",
1382 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1383 parser->omp_declare_simd = NULL;
1384 }
1385 }
1386
1387 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1388 and put that into "omp declare simd" attribute. */
1389
1390 static inline void
1391 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1392 {
1393 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1394 {
1395 if (fndecl == error_mark_node)
1396 {
1397 parser->omp_declare_simd = NULL;
1398 return;
1399 }
1400 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1401 {
1402 cp_ensure_no_omp_declare_simd (parser);
1403 return;
1404 }
1405 }
1406 }
1407
1408 /* Diagnose if #pragma acc routine isn't followed immediately by function
1409 declaration or definition. */
1410
1411 static inline void
1412 cp_ensure_no_oacc_routine (cp_parser *parser)
1413 {
1414 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1415 {
1416 error_at (parser->oacc_routine->loc,
1417 "%<#pragma acc routine%> not immediately followed by "
1418 "function declaration or definition");
1419 parser->oacc_routine = NULL;
1420 }
1421 }
1422 \f
1423 /* Decl-specifiers. */
1424
1425 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1426
1427 static void
1428 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1429 {
1430 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1431 }
1432
1433 /* Declarators. */
1434
1435 /* Nothing other than the parser should be creating declarators;
1436 declarators are a semi-syntactic representation of C++ entities.
1437 Other parts of the front end that need to create entities (like
1438 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1439
1440 static cp_declarator *make_call_declarator
1441 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1442 static cp_declarator *make_array_declarator
1443 (cp_declarator *, tree);
1444 static cp_declarator *make_pointer_declarator
1445 (cp_cv_quals, cp_declarator *, tree);
1446 static cp_declarator *make_reference_declarator
1447 (cp_cv_quals, cp_declarator *, bool, tree);
1448 static cp_declarator *make_ptrmem_declarator
1449 (cp_cv_quals, tree, cp_declarator *, tree);
1450
1451 /* An erroneous declarator. */
1452 static cp_declarator *cp_error_declarator;
1453
1454 /* The obstack on which declarators and related data structures are
1455 allocated. */
1456 static struct obstack declarator_obstack;
1457
1458 /* Alloc BYTES from the declarator memory pool. */
1459
1460 static inline void *
1461 alloc_declarator (size_t bytes)
1462 {
1463 return obstack_alloc (&declarator_obstack, bytes);
1464 }
1465
1466 /* Allocate a declarator of the indicated KIND. Clear fields that are
1467 common to all declarators. */
1468
1469 static cp_declarator *
1470 make_declarator (cp_declarator_kind kind)
1471 {
1472 cp_declarator *declarator;
1473
1474 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1475 declarator->kind = kind;
1476 declarator->parenthesized = UNKNOWN_LOCATION;
1477 declarator->attributes = NULL_TREE;
1478 declarator->std_attributes = NULL_TREE;
1479 declarator->declarator = NULL;
1480 declarator->parameter_pack_p = false;
1481 declarator->id_loc = UNKNOWN_LOCATION;
1482
1483 return declarator;
1484 }
1485
1486 /* Make a declarator for a generalized identifier. If
1487 QUALIFYING_SCOPE is non-NULL, the identifier is
1488 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1489 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1490 is, if any. */
1491
1492 static cp_declarator *
1493 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1494 special_function_kind sfk, location_t id_location)
1495 {
1496 cp_declarator *declarator;
1497
1498 /* It is valid to write:
1499
1500 class C { void f(); };
1501 typedef C D;
1502 void D::f();
1503
1504 The standard is not clear about whether `typedef const C D' is
1505 legal; as of 2002-09-15 the committee is considering that
1506 question. EDG 3.0 allows that syntax. Therefore, we do as
1507 well. */
1508 if (qualifying_scope && TYPE_P (qualifying_scope))
1509 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1510
1511 gcc_assert (identifier_p (unqualified_name)
1512 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1513 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1514
1515 declarator = make_declarator (cdk_id);
1516 declarator->u.id.qualifying_scope = qualifying_scope;
1517 declarator->u.id.unqualified_name = unqualified_name;
1518 declarator->u.id.sfk = sfk;
1519 declarator->id_loc = id_location;
1520
1521 return declarator;
1522 }
1523
1524 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1525 of modifiers such as const or volatile to apply to the pointer
1526 type, represented as identifiers. ATTRIBUTES represent the attributes that
1527 appertain to the pointer or reference. */
1528
1529 cp_declarator *
1530 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1531 tree attributes)
1532 {
1533 cp_declarator *declarator;
1534
1535 declarator = make_declarator (cdk_pointer);
1536 declarator->declarator = target;
1537 declarator->u.pointer.qualifiers = cv_qualifiers;
1538 declarator->u.pointer.class_type = NULL_TREE;
1539 if (target)
1540 {
1541 declarator->id_loc = target->id_loc;
1542 declarator->parameter_pack_p = target->parameter_pack_p;
1543 target->parameter_pack_p = false;
1544 }
1545 else
1546 declarator->parameter_pack_p = false;
1547
1548 declarator->std_attributes = attributes;
1549
1550 return declarator;
1551 }
1552
1553 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1554 represent the attributes that appertain to the pointer or
1555 reference. */
1556
1557 cp_declarator *
1558 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1559 bool rvalue_ref, tree attributes)
1560 {
1561 cp_declarator *declarator;
1562
1563 declarator = make_declarator (cdk_reference);
1564 declarator->declarator = target;
1565 declarator->u.reference.qualifiers = cv_qualifiers;
1566 declarator->u.reference.rvalue_ref = rvalue_ref;
1567 if (target)
1568 {
1569 declarator->id_loc = target->id_loc;
1570 declarator->parameter_pack_p = target->parameter_pack_p;
1571 target->parameter_pack_p = false;
1572 }
1573 else
1574 declarator->parameter_pack_p = false;
1575
1576 declarator->std_attributes = attributes;
1577
1578 return declarator;
1579 }
1580
1581 /* Like make_pointer_declarator -- but for a pointer to a non-static
1582 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1583 appertain to the pointer or reference. */
1584
1585 cp_declarator *
1586 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1587 cp_declarator *pointee,
1588 tree attributes)
1589 {
1590 cp_declarator *declarator;
1591
1592 declarator = make_declarator (cdk_ptrmem);
1593 declarator->declarator = pointee;
1594 declarator->u.pointer.qualifiers = cv_qualifiers;
1595 declarator->u.pointer.class_type = class_type;
1596
1597 if (pointee)
1598 {
1599 declarator->parameter_pack_p = pointee->parameter_pack_p;
1600 pointee->parameter_pack_p = false;
1601 }
1602 else
1603 declarator->parameter_pack_p = false;
1604
1605 declarator->std_attributes = attributes;
1606
1607 return declarator;
1608 }
1609
1610 /* Make a declarator for the function given by TARGET, with the
1611 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1612 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1613 indicates what exceptions can be thrown. */
1614
1615 cp_declarator *
1616 make_call_declarator (cp_declarator *target,
1617 tree parms,
1618 cp_cv_quals cv_qualifiers,
1619 cp_virt_specifiers virt_specifiers,
1620 cp_ref_qualifier ref_qualifier,
1621 tree tx_qualifier,
1622 tree exception_specification,
1623 tree late_return_type,
1624 tree requires_clause)
1625 {
1626 cp_declarator *declarator;
1627
1628 declarator = make_declarator (cdk_function);
1629 declarator->declarator = target;
1630 declarator->u.function.parameters = parms;
1631 declarator->u.function.qualifiers = cv_qualifiers;
1632 declarator->u.function.virt_specifiers = virt_specifiers;
1633 declarator->u.function.ref_qualifier = ref_qualifier;
1634 declarator->u.function.tx_qualifier = tx_qualifier;
1635 declarator->u.function.exception_specification = exception_specification;
1636 declarator->u.function.late_return_type = late_return_type;
1637 declarator->u.function.requires_clause = requires_clause;
1638 if (target)
1639 {
1640 declarator->id_loc = target->id_loc;
1641 declarator->parameter_pack_p = target->parameter_pack_p;
1642 target->parameter_pack_p = false;
1643 }
1644 else
1645 declarator->parameter_pack_p = false;
1646
1647 return declarator;
1648 }
1649
1650 /* Make a declarator for an array of BOUNDS elements, each of which is
1651 defined by ELEMENT. */
1652
1653 cp_declarator *
1654 make_array_declarator (cp_declarator *element, tree bounds)
1655 {
1656 cp_declarator *declarator;
1657
1658 declarator = make_declarator (cdk_array);
1659 declarator->declarator = element;
1660 declarator->u.array.bounds = bounds;
1661 if (element)
1662 {
1663 declarator->id_loc = element->id_loc;
1664 declarator->parameter_pack_p = element->parameter_pack_p;
1665 element->parameter_pack_p = false;
1666 }
1667 else
1668 declarator->parameter_pack_p = false;
1669
1670 return declarator;
1671 }
1672
1673 /* Determine whether the declarator we've seen so far can be a
1674 parameter pack, when followed by an ellipsis. */
1675 static bool
1676 declarator_can_be_parameter_pack (cp_declarator *declarator)
1677 {
1678 if (declarator && declarator->parameter_pack_p)
1679 /* We already saw an ellipsis. */
1680 return false;
1681
1682 /* Search for a declarator name, or any other declarator that goes
1683 after the point where the ellipsis could appear in a parameter
1684 pack. If we find any of these, then this declarator cannot be
1685 made into a parameter pack. */
1686 bool found = false;
1687 while (declarator && !found)
1688 {
1689 switch ((int)declarator->kind)
1690 {
1691 case cdk_id:
1692 case cdk_array:
1693 case cdk_decomp:
1694 found = true;
1695 break;
1696
1697 case cdk_error:
1698 return true;
1699
1700 default:
1701 declarator = declarator->declarator;
1702 break;
1703 }
1704 }
1705
1706 return !found;
1707 }
1708
1709 cp_parameter_declarator *no_parameters;
1710
1711 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1712 DECLARATOR and DEFAULT_ARGUMENT. */
1713
1714 cp_parameter_declarator *
1715 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1716 cp_declarator *declarator,
1717 tree default_argument,
1718 location_t loc,
1719 bool template_parameter_pack_p = false)
1720 {
1721 cp_parameter_declarator *parameter;
1722
1723 parameter = ((cp_parameter_declarator *)
1724 alloc_declarator (sizeof (cp_parameter_declarator)));
1725 parameter->next = NULL;
1726 if (decl_specifiers)
1727 parameter->decl_specifiers = *decl_specifiers;
1728 else
1729 clear_decl_specs (&parameter->decl_specifiers);
1730 parameter->declarator = declarator;
1731 parameter->default_argument = default_argument;
1732 parameter->template_parameter_pack_p = template_parameter_pack_p;
1733 parameter->loc = loc;
1734
1735 return parameter;
1736 }
1737
1738 /* Returns true iff DECLARATOR is a declaration for a function. */
1739
1740 static bool
1741 function_declarator_p (const cp_declarator *declarator)
1742 {
1743 while (declarator)
1744 {
1745 if (declarator->kind == cdk_function
1746 && declarator->declarator->kind == cdk_id)
1747 return true;
1748 if (declarator->kind == cdk_id
1749 || declarator->kind == cdk_decomp
1750 || declarator->kind == cdk_error)
1751 return false;
1752 declarator = declarator->declarator;
1753 }
1754 return false;
1755 }
1756
1757 /* The parser. */
1758
1759 /* Overview
1760 --------
1761
1762 A cp_parser parses the token stream as specified by the C++
1763 grammar. Its job is purely parsing, not semantic analysis. For
1764 example, the parser breaks the token stream into declarators,
1765 expressions, statements, and other similar syntactic constructs.
1766 It does not check that the types of the expressions on either side
1767 of an assignment-statement are compatible, or that a function is
1768 not declared with a parameter of type `void'.
1769
1770 The parser invokes routines elsewhere in the compiler to perform
1771 semantic analysis and to build up the abstract syntax tree for the
1772 code processed.
1773
1774 The parser (and the template instantiation code, which is, in a
1775 way, a close relative of parsing) are the only parts of the
1776 compiler that should be calling push_scope and pop_scope, or
1777 related functions. The parser (and template instantiation code)
1778 keeps track of what scope is presently active; everything else
1779 should simply honor that. (The code that generates static
1780 initializers may also need to set the scope, in order to check
1781 access control correctly when emitting the initializers.)
1782
1783 Methodology
1784 -----------
1785
1786 The parser is of the standard recursive-descent variety. Upcoming
1787 tokens in the token stream are examined in order to determine which
1788 production to use when parsing a non-terminal. Some C++ constructs
1789 require arbitrary look ahead to disambiguate. For example, it is
1790 impossible, in the general case, to tell whether a statement is an
1791 expression or declaration without scanning the entire statement.
1792 Therefore, the parser is capable of "parsing tentatively." When the
1793 parser is not sure what construct comes next, it enters this mode.
1794 Then, while we attempt to parse the construct, the parser queues up
1795 error messages, rather than issuing them immediately, and saves the
1796 tokens it consumes. If the construct is parsed successfully, the
1797 parser "commits", i.e., it issues any queued error messages and
1798 the tokens that were being preserved are permanently discarded.
1799 If, however, the construct is not parsed successfully, the parser
1800 rolls back its state completely so that it can resume parsing using
1801 a different alternative.
1802
1803 Future Improvements
1804 -------------------
1805
1806 The performance of the parser could probably be improved substantially.
1807 We could often eliminate the need to parse tentatively by looking ahead
1808 a little bit. In some places, this approach might not entirely eliminate
1809 the need to parse tentatively, but it might still speed up the average
1810 case. */
1811
1812 /* Flags that are passed to some parsing functions. These values can
1813 be bitwise-ored together. */
1814
1815 enum
1816 {
1817 /* No flags. */
1818 CP_PARSER_FLAGS_NONE = 0x0,
1819 /* The construct is optional. If it is not present, then no error
1820 should be issued. */
1821 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1822 /* When parsing a type-specifier, treat user-defined type-names
1823 as non-type identifiers. */
1824 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1825 /* When parsing a type-specifier, do not try to parse a class-specifier
1826 or enum-specifier. */
1827 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1828 /* When parsing a decl-specifier-seq, only allow type-specifier or
1829 constexpr. */
1830 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1831 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1832 for C++20 consteval. */
1833 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1834 /* When parsing a decl-specifier-seq, allow missing typename. */
1835 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
1836 /* When parsing of the noexcept-specifier should be delayed. */
1837 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
1838 /* When parsing a consteval declarator. */
1839 CP_PARSER_FLAGS_CONSTEVAL = 0x80
1840 };
1841
1842 /* This type is used for parameters and variables which hold
1843 combinations of the above flags. */
1844 typedef int cp_parser_flags;
1845
1846 /* The different kinds of declarators we want to parse. */
1847
1848 enum cp_parser_declarator_kind
1849 {
1850 /* We want an abstract declarator. */
1851 CP_PARSER_DECLARATOR_ABSTRACT,
1852 /* We want a named declarator. */
1853 CP_PARSER_DECLARATOR_NAMED,
1854 /* We don't mind, but the name must be an unqualified-id. */
1855 CP_PARSER_DECLARATOR_EITHER
1856 };
1857
1858 /* The precedence values used to parse binary expressions. The minimum value
1859 of PREC must be 1, because zero is reserved to quickly discriminate
1860 binary operators from other tokens. */
1861
1862 enum cp_parser_prec
1863 {
1864 PREC_NOT_OPERATOR,
1865 PREC_LOGICAL_OR_EXPRESSION,
1866 PREC_LOGICAL_AND_EXPRESSION,
1867 PREC_INCLUSIVE_OR_EXPRESSION,
1868 PREC_EXCLUSIVE_OR_EXPRESSION,
1869 PREC_AND_EXPRESSION,
1870 PREC_EQUALITY_EXPRESSION,
1871 PREC_RELATIONAL_EXPRESSION,
1872 PREC_SPACESHIP_EXPRESSION,
1873 PREC_SHIFT_EXPRESSION,
1874 PREC_ADDITIVE_EXPRESSION,
1875 PREC_MULTIPLICATIVE_EXPRESSION,
1876 PREC_PM_EXPRESSION,
1877 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1878 };
1879
1880 /* A mapping from a token type to a corresponding tree node type, with a
1881 precedence value. */
1882
1883 struct cp_parser_binary_operations_map_node
1884 {
1885 /* The token type. */
1886 enum cpp_ttype token_type;
1887 /* The corresponding tree code. */
1888 enum tree_code tree_type;
1889 /* The precedence of this operator. */
1890 enum cp_parser_prec prec;
1891 };
1892
1893 struct cp_parser_expression_stack_entry
1894 {
1895 /* Left hand side of the binary operation we are currently
1896 parsing. */
1897 cp_expr lhs;
1898 /* Original tree code for left hand side, if it was a binary
1899 expression itself (used for -Wparentheses). */
1900 enum tree_code lhs_type;
1901 /* Tree code for the binary operation we are parsing. */
1902 enum tree_code tree_type;
1903 /* Precedence of the binary operation we are parsing. */
1904 enum cp_parser_prec prec;
1905 /* Location of the binary operation we are parsing. */
1906 location_t loc;
1907 };
1908
1909 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1910 entries because precedence levels on the stack are monotonically
1911 increasing. */
1912 typedef struct cp_parser_expression_stack_entry
1913 cp_parser_expression_stack[NUM_PREC_VALUES];
1914
1915 /* Prototypes. */
1916
1917 /* Constructors and destructors. */
1918
1919 static cp_parser_context *cp_parser_context_new
1920 (cp_parser_context *);
1921
1922 /* Class variables. */
1923
1924 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1925
1926 /* The operator-precedence table used by cp_parser_binary_expression.
1927 Transformed into an associative array (binops_by_token) by
1928 cp_parser_new. */
1929
1930 static const cp_parser_binary_operations_map_node binops[] = {
1931 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1932 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1933
1934 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1935 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1936 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1937
1938 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1939 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1940
1941 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1942 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1943
1944 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
1945
1946 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1947 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1948 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1949 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1950
1951 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1952 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1953
1954 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1955
1956 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1957
1958 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1959
1960 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1961
1962 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1963 };
1964
1965 /* The same as binops, but initialized by cp_parser_new so that
1966 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1967 for speed. */
1968 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1969
1970 /* Constructors and destructors. */
1971
1972 /* Construct a new context. The context below this one on the stack
1973 is given by NEXT. */
1974
1975 static cp_parser_context *
1976 cp_parser_context_new (cp_parser_context* next)
1977 {
1978 cp_parser_context *context;
1979
1980 /* Allocate the storage. */
1981 if (cp_parser_context_free_list != NULL)
1982 {
1983 /* Pull the first entry from the free list. */
1984 context = cp_parser_context_free_list;
1985 cp_parser_context_free_list = context->next;
1986 memset (context, 0, sizeof (*context));
1987 }
1988 else
1989 context = ggc_cleared_alloc<cp_parser_context> ();
1990
1991 /* No errors have occurred yet in this context. */
1992 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1993 /* If this is not the bottommost context, copy information that we
1994 need from the previous context. */
1995 if (next)
1996 {
1997 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1998 expression, then we are parsing one in this context, too. */
1999 context->object_type = next->object_type;
2000 /* Thread the stack. */
2001 context->next = next;
2002 }
2003
2004 return context;
2005 }
2006
2007 /* Managing the unparsed function queues. */
2008
2009 #define unparsed_funs_with_default_args \
2010 parser->unparsed_queues->last ().funs_with_default_args
2011 #define unparsed_funs_with_definitions \
2012 parser->unparsed_queues->last ().funs_with_definitions
2013 #define unparsed_nsdmis \
2014 parser->unparsed_queues->last ().nsdmis
2015 #define unparsed_noexcepts \
2016 parser->unparsed_queues->last ().noexcepts
2017
2018 static void
2019 push_unparsed_function_queues (cp_parser *parser)
2020 {
2021 cp_unparsed_functions_entry e = { NULL, make_tree_vector (), NULL, NULL };
2022 vec_safe_push (parser->unparsed_queues, e);
2023 }
2024
2025 static void
2026 pop_unparsed_function_queues (cp_parser *parser)
2027 {
2028 release_tree_vector (unparsed_funs_with_definitions);
2029 parser->unparsed_queues->pop ();
2030 }
2031
2032 /* Prototypes. */
2033
2034 /* Constructors and destructors. */
2035
2036 static cp_parser *cp_parser_new
2037 (cp_lexer *);
2038
2039 /* Routines to parse various constructs.
2040
2041 Those that return `tree' will return the error_mark_node (rather
2042 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2043 Sometimes, they will return an ordinary node if error-recovery was
2044 attempted, even though a parse error occurred. So, to check
2045 whether or not a parse error occurred, you should always use
2046 cp_parser_error_occurred. If the construct is optional (indicated
2047 either by an `_opt' in the name of the function that does the
2048 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2049 the construct is not present. */
2050
2051 /* Lexical conventions [gram.lex] */
2052
2053 static cp_expr cp_parser_identifier
2054 (cp_parser *);
2055 static cp_expr cp_parser_string_literal
2056 (cp_parser *, bool, bool, bool);
2057 static cp_expr cp_parser_userdef_char_literal
2058 (cp_parser *);
2059 static tree cp_parser_userdef_string_literal
2060 (tree);
2061 static cp_expr cp_parser_userdef_numeric_literal
2062 (cp_parser *);
2063
2064 /* Basic concepts [gram.basic] */
2065
2066 static void cp_parser_translation_unit (cp_parser *);
2067
2068 /* Expressions [gram.expr] */
2069
2070 static cp_expr cp_parser_primary_expression
2071 (cp_parser *, bool, bool, bool, cp_id_kind *);
2072 static cp_expr cp_parser_id_expression
2073 (cp_parser *, bool, bool, bool *, bool, bool);
2074 static cp_expr cp_parser_unqualified_id
2075 (cp_parser *, bool, bool, bool, bool);
2076 static tree cp_parser_nested_name_specifier_opt
2077 (cp_parser *, bool, bool, bool, bool, bool = false);
2078 static tree cp_parser_nested_name_specifier
2079 (cp_parser *, bool, bool, bool, bool);
2080 static tree cp_parser_qualifying_entity
2081 (cp_parser *, bool, bool, bool, bool, bool);
2082 static cp_expr cp_parser_postfix_expression
2083 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2084 static tree cp_parser_postfix_open_square_expression
2085 (cp_parser *, tree, bool, bool);
2086 static tree cp_parser_postfix_dot_deref_expression
2087 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2088 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2089 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2090 bool = false);
2091 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2092 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2093 static void cp_parser_pseudo_destructor_name
2094 (cp_parser *, tree, tree *, tree *);
2095 static cp_expr cp_parser_unary_expression
2096 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2097 static enum tree_code cp_parser_unary_operator
2098 (cp_token *);
2099 static tree cp_parser_has_attribute_expression
2100 (cp_parser *);
2101 static tree cp_parser_new_expression
2102 (cp_parser *);
2103 static vec<tree, va_gc> *cp_parser_new_placement
2104 (cp_parser *);
2105 static tree cp_parser_new_type_id
2106 (cp_parser *, tree *);
2107 static cp_declarator *cp_parser_new_declarator_opt
2108 (cp_parser *);
2109 static cp_declarator *cp_parser_direct_new_declarator
2110 (cp_parser *);
2111 static vec<tree, va_gc> *cp_parser_new_initializer
2112 (cp_parser *);
2113 static tree cp_parser_delete_expression
2114 (cp_parser *);
2115 static cp_expr cp_parser_cast_expression
2116 (cp_parser *, bool, bool, bool, cp_id_kind *);
2117 static cp_expr cp_parser_binary_expression
2118 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2119 static tree cp_parser_question_colon_clause
2120 (cp_parser *, cp_expr);
2121 static cp_expr cp_parser_assignment_expression
2122 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2123 static enum tree_code cp_parser_assignment_operator_opt
2124 (cp_parser *);
2125 static cp_expr cp_parser_expression
2126 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2127 static cp_expr cp_parser_constant_expression
2128 (cp_parser *, bool = false, bool * = NULL, bool = false);
2129 static cp_expr cp_parser_builtin_offsetof
2130 (cp_parser *);
2131 static cp_expr cp_parser_lambda_expression
2132 (cp_parser *);
2133 static void cp_parser_lambda_introducer
2134 (cp_parser *, tree);
2135 static bool cp_parser_lambda_declarator_opt
2136 (cp_parser *, tree);
2137 static void cp_parser_lambda_body
2138 (cp_parser *, tree);
2139
2140 /* Statements [gram.stmt.stmt] */
2141
2142 static void cp_parser_statement
2143 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2144 static void cp_parser_label_for_labeled_statement
2145 (cp_parser *, tree);
2146 static tree cp_parser_expression_statement
2147 (cp_parser *, tree);
2148 static tree cp_parser_compound_statement
2149 (cp_parser *, tree, int, bool);
2150 static void cp_parser_statement_seq_opt
2151 (cp_parser *, tree);
2152 static tree cp_parser_selection_statement
2153 (cp_parser *, bool *, vec<tree> *);
2154 static tree cp_parser_condition
2155 (cp_parser *);
2156 static tree cp_parser_iteration_statement
2157 (cp_parser *, bool *, bool, unsigned short);
2158 static bool cp_parser_init_statement
2159 (cp_parser *, tree *decl);
2160 static tree cp_parser_for
2161 (cp_parser *, bool, unsigned short);
2162 static tree cp_parser_c_for
2163 (cp_parser *, tree, tree, bool, unsigned short);
2164 static tree cp_parser_range_for
2165 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2166 static void do_range_for_auto_deduction
2167 (tree, tree);
2168 static tree cp_parser_perform_range_for_lookup
2169 (tree, tree *, tree *);
2170 static tree cp_parser_range_for_member_function
2171 (tree, tree);
2172 static tree cp_parser_jump_statement
2173 (cp_parser *);
2174 static void cp_parser_declaration_statement
2175 (cp_parser *);
2176
2177 static tree cp_parser_implicitly_scoped_statement
2178 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2179 static void cp_parser_already_scoped_statement
2180 (cp_parser *, bool *, const token_indent_info &);
2181
2182 /* Declarations [gram.dcl.dcl] */
2183
2184 static void cp_parser_declaration_seq_opt
2185 (cp_parser *);
2186 static void cp_parser_declaration
2187 (cp_parser *);
2188 static void cp_parser_toplevel_declaration
2189 (cp_parser *);
2190 static void cp_parser_block_declaration
2191 (cp_parser *, bool);
2192 static void cp_parser_simple_declaration
2193 (cp_parser *, bool, tree *);
2194 static void cp_parser_decl_specifier_seq
2195 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2196 static tree cp_parser_storage_class_specifier_opt
2197 (cp_parser *);
2198 static tree cp_parser_function_specifier_opt
2199 (cp_parser *, cp_decl_specifier_seq *);
2200 static tree cp_parser_type_specifier
2201 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2202 int *, bool *);
2203 static tree cp_parser_simple_type_specifier
2204 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2205 static tree cp_parser_placeholder_type_specifier
2206 (cp_parser *, location_t, tree, bool);
2207 static tree cp_parser_type_name
2208 (cp_parser *, bool);
2209 static tree cp_parser_nonclass_name
2210 (cp_parser* parser);
2211 static tree cp_parser_elaborated_type_specifier
2212 (cp_parser *, bool, bool);
2213 static tree cp_parser_enum_specifier
2214 (cp_parser *);
2215 static void cp_parser_enumerator_list
2216 (cp_parser *, tree);
2217 static void cp_parser_enumerator_definition
2218 (cp_parser *, tree);
2219 static tree cp_parser_namespace_name
2220 (cp_parser *);
2221 static void cp_parser_namespace_definition
2222 (cp_parser *);
2223 static void cp_parser_namespace_body
2224 (cp_parser *);
2225 static tree cp_parser_qualified_namespace_specifier
2226 (cp_parser *);
2227 static void cp_parser_namespace_alias_definition
2228 (cp_parser *);
2229 static bool cp_parser_using_declaration
2230 (cp_parser *, bool);
2231 static void cp_parser_using_directive
2232 (cp_parser *);
2233 static tree cp_parser_alias_declaration
2234 (cp_parser *);
2235 static void cp_parser_asm_definition
2236 (cp_parser *);
2237 static void cp_parser_linkage_specification
2238 (cp_parser *);
2239 static void cp_parser_static_assert
2240 (cp_parser *, bool);
2241 static tree cp_parser_decltype
2242 (cp_parser *);
2243 static tree cp_parser_decomposition_declaration
2244 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2245
2246 /* Declarators [gram.dcl.decl] */
2247
2248 static tree cp_parser_init_declarator
2249 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2250 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2251 location_t *, tree *);
2252 static cp_declarator *cp_parser_declarator
2253 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2254 bool, bool, bool);
2255 static cp_declarator *cp_parser_direct_declarator
2256 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2257 bool);
2258 static enum tree_code cp_parser_ptr_operator
2259 (cp_parser *, tree *, cp_cv_quals *, tree *);
2260 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2261 (cp_parser *);
2262 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2263 (cp_parser *);
2264 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2265 (cp_parser *);
2266 static tree cp_parser_tx_qualifier_opt
2267 (cp_parser *);
2268 static tree cp_parser_late_return_type_opt
2269 (cp_parser *, cp_declarator *, tree &);
2270 static tree cp_parser_declarator_id
2271 (cp_parser *, bool);
2272 static tree cp_parser_type_id
2273 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2274 static tree cp_parser_template_type_arg
2275 (cp_parser *);
2276 static tree cp_parser_trailing_type_id (cp_parser *);
2277 static tree cp_parser_type_id_1
2278 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2279 static void cp_parser_type_specifier_seq
2280 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2281 static tree cp_parser_parameter_declaration_clause
2282 (cp_parser *, cp_parser_flags);
2283 static tree cp_parser_parameter_declaration_list
2284 (cp_parser *, cp_parser_flags);
2285 static cp_parameter_declarator *cp_parser_parameter_declaration
2286 (cp_parser *, cp_parser_flags, bool, bool *);
2287 static tree cp_parser_default_argument
2288 (cp_parser *, bool);
2289 static void cp_parser_function_body
2290 (cp_parser *, bool);
2291 static tree cp_parser_initializer
2292 (cp_parser *, bool *, bool *, bool = false);
2293 static cp_expr cp_parser_initializer_clause
2294 (cp_parser *, bool *);
2295 static cp_expr cp_parser_braced_list
2296 (cp_parser*, bool*);
2297 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2298 (cp_parser *, bool *, bool *);
2299
2300 static void cp_parser_ctor_initializer_opt_and_function_body
2301 (cp_parser *, bool);
2302
2303 static tree cp_parser_late_parsing_omp_declare_simd
2304 (cp_parser *, tree);
2305
2306 static tree cp_parser_late_parsing_oacc_routine
2307 (cp_parser *, tree);
2308
2309 static tree synthesize_implicit_template_parm
2310 (cp_parser *, tree);
2311 static tree finish_fully_implicit_template
2312 (cp_parser *, tree);
2313 static void abort_fully_implicit_template
2314 (cp_parser *);
2315
2316 /* Classes [gram.class] */
2317
2318 static tree cp_parser_class_name
2319 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2320 static tree cp_parser_class_specifier
2321 (cp_parser *);
2322 static tree cp_parser_class_head
2323 (cp_parser *, bool *);
2324 static enum tag_types cp_parser_class_key
2325 (cp_parser *);
2326 static void cp_parser_type_parameter_key
2327 (cp_parser* parser);
2328 static void cp_parser_member_specification_opt
2329 (cp_parser *);
2330 static void cp_parser_member_declaration
2331 (cp_parser *);
2332 static tree cp_parser_pure_specifier
2333 (cp_parser *);
2334 static tree cp_parser_constant_initializer
2335 (cp_parser *);
2336
2337 /* Derived classes [gram.class.derived] */
2338
2339 static tree cp_parser_base_clause
2340 (cp_parser *);
2341 static tree cp_parser_base_specifier
2342 (cp_parser *);
2343
2344 /* Special member functions [gram.special] */
2345
2346 static tree cp_parser_conversion_function_id
2347 (cp_parser *);
2348 static tree cp_parser_conversion_type_id
2349 (cp_parser *);
2350 static cp_declarator *cp_parser_conversion_declarator_opt
2351 (cp_parser *);
2352 static void cp_parser_ctor_initializer_opt
2353 (cp_parser *);
2354 static void cp_parser_mem_initializer_list
2355 (cp_parser *);
2356 static tree cp_parser_mem_initializer
2357 (cp_parser *);
2358 static tree cp_parser_mem_initializer_id
2359 (cp_parser *);
2360
2361 /* Overloading [gram.over] */
2362
2363 static cp_expr cp_parser_operator_function_id
2364 (cp_parser *);
2365 static cp_expr cp_parser_operator
2366 (cp_parser *, location_t);
2367
2368 /* Templates [gram.temp] */
2369
2370 static void cp_parser_template_declaration
2371 (cp_parser *, bool);
2372 static tree cp_parser_template_parameter_list
2373 (cp_parser *);
2374 static tree cp_parser_template_parameter
2375 (cp_parser *, bool *, bool *);
2376 static tree cp_parser_type_parameter
2377 (cp_parser *, bool *);
2378 static tree cp_parser_template_id
2379 (cp_parser *, bool, bool, enum tag_types, bool);
2380 static tree cp_parser_template_id_expr
2381 (cp_parser *, bool, bool, bool);
2382 static tree cp_parser_template_name
2383 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2384 static tree cp_parser_template_argument_list
2385 (cp_parser *);
2386 static tree cp_parser_template_argument
2387 (cp_parser *);
2388 static void cp_parser_explicit_instantiation
2389 (cp_parser *);
2390 static void cp_parser_explicit_specialization
2391 (cp_parser *);
2392
2393 /* Exception handling [gram.except] */
2394
2395 static tree cp_parser_try_block
2396 (cp_parser *);
2397 static void cp_parser_function_try_block
2398 (cp_parser *);
2399 static void cp_parser_handler_seq
2400 (cp_parser *);
2401 static void cp_parser_handler
2402 (cp_parser *);
2403 static tree cp_parser_exception_declaration
2404 (cp_parser *);
2405 static tree cp_parser_throw_expression
2406 (cp_parser *);
2407 static tree cp_parser_exception_specification_opt
2408 (cp_parser *, cp_parser_flags);
2409 static tree cp_parser_type_id_list
2410 (cp_parser *);
2411 static tree cp_parser_noexcept_specification_opt
2412 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2413
2414 /* GNU Extensions */
2415
2416 static tree cp_parser_asm_specification_opt
2417 (cp_parser *);
2418 static tree cp_parser_asm_operand_list
2419 (cp_parser *);
2420 static tree cp_parser_asm_clobber_list
2421 (cp_parser *);
2422 static tree cp_parser_asm_label_list
2423 (cp_parser *);
2424 static bool cp_next_tokens_can_be_attribute_p
2425 (cp_parser *);
2426 static bool cp_next_tokens_can_be_gnu_attribute_p
2427 (cp_parser *);
2428 static bool cp_next_tokens_can_be_std_attribute_p
2429 (cp_parser *);
2430 static bool cp_nth_tokens_can_be_std_attribute_p
2431 (cp_parser *, size_t);
2432 static bool cp_nth_tokens_can_be_gnu_attribute_p
2433 (cp_parser *, size_t);
2434 static bool cp_nth_tokens_can_be_attribute_p
2435 (cp_parser *, size_t);
2436 static tree cp_parser_attributes_opt
2437 (cp_parser *);
2438 static tree cp_parser_gnu_attributes_opt
2439 (cp_parser *);
2440 static tree cp_parser_gnu_attribute_list
2441 (cp_parser *, bool = false);
2442 static tree cp_parser_std_attribute
2443 (cp_parser *, tree);
2444 static tree cp_parser_std_attribute_spec
2445 (cp_parser *);
2446 static tree cp_parser_std_attribute_spec_seq
2447 (cp_parser *);
2448 static size_t cp_parser_skip_attributes_opt
2449 (cp_parser *, size_t);
2450 static bool cp_parser_extension_opt
2451 (cp_parser *, int *);
2452 static void cp_parser_label_declaration
2453 (cp_parser *);
2454
2455 /* Concept Extensions */
2456
2457 static tree cp_parser_concept_definition
2458 (cp_parser *);
2459 static tree cp_parser_constraint_expression
2460 (cp_parser *);
2461 static tree cp_parser_requires_clause_opt
2462 (cp_parser *, bool);
2463 static tree cp_parser_requires_expression
2464 (cp_parser *);
2465 static tree cp_parser_requirement_parameter_list
2466 (cp_parser *);
2467 static tree cp_parser_requirement_body
2468 (cp_parser *);
2469 static tree cp_parser_requirement_seq
2470 (cp_parser *);
2471 static tree cp_parser_requirement
2472 (cp_parser *);
2473 static tree cp_parser_simple_requirement
2474 (cp_parser *);
2475 static tree cp_parser_compound_requirement
2476 (cp_parser *);
2477 static tree cp_parser_type_requirement
2478 (cp_parser *);
2479 static tree cp_parser_nested_requirement
2480 (cp_parser *);
2481
2482 /* Transactional Memory Extensions */
2483
2484 static tree cp_parser_transaction
2485 (cp_parser *, cp_token *);
2486 static tree cp_parser_transaction_expression
2487 (cp_parser *, enum rid);
2488 static void cp_parser_function_transaction
2489 (cp_parser *, enum rid);
2490 static tree cp_parser_transaction_cancel
2491 (cp_parser *);
2492
2493 /* Coroutine extensions. */
2494
2495 static tree cp_parser_yield_expression
2496 (cp_parser *);
2497
2498
2499 enum pragma_context {
2500 pragma_external,
2501 pragma_member,
2502 pragma_objc_icode,
2503 pragma_stmt,
2504 pragma_compound
2505 };
2506 static bool cp_parser_pragma
2507 (cp_parser *, enum pragma_context, bool *);
2508
2509 /* Objective-C++ Productions */
2510
2511 static tree cp_parser_objc_message_receiver
2512 (cp_parser *);
2513 static tree cp_parser_objc_message_args
2514 (cp_parser *);
2515 static tree cp_parser_objc_message_expression
2516 (cp_parser *);
2517 static cp_expr cp_parser_objc_encode_expression
2518 (cp_parser *);
2519 static tree cp_parser_objc_defs_expression
2520 (cp_parser *);
2521 static tree cp_parser_objc_protocol_expression
2522 (cp_parser *);
2523 static tree cp_parser_objc_selector_expression
2524 (cp_parser *);
2525 static cp_expr cp_parser_objc_expression
2526 (cp_parser *);
2527 static bool cp_parser_objc_selector_p
2528 (enum cpp_ttype);
2529 static tree cp_parser_objc_selector
2530 (cp_parser *);
2531 static tree cp_parser_objc_protocol_refs_opt
2532 (cp_parser *);
2533 static void cp_parser_objc_declaration
2534 (cp_parser *, tree);
2535 static tree cp_parser_objc_statement
2536 (cp_parser *);
2537 static bool cp_parser_objc_valid_prefix_attributes
2538 (cp_parser *, tree *);
2539 static void cp_parser_objc_at_property_declaration
2540 (cp_parser *) ;
2541 static void cp_parser_objc_at_synthesize_declaration
2542 (cp_parser *) ;
2543 static void cp_parser_objc_at_dynamic_declaration
2544 (cp_parser *) ;
2545 static tree cp_parser_objc_struct_declaration
2546 (cp_parser *) ;
2547
2548 /* Utility Routines */
2549
2550 static cp_expr cp_parser_lookup_name
2551 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2552 static tree cp_parser_lookup_name_simple
2553 (cp_parser *, tree, location_t);
2554 static tree cp_parser_maybe_treat_template_as_class
2555 (tree, bool);
2556 static bool cp_parser_check_declarator_template_parameters
2557 (cp_parser *, cp_declarator *, location_t);
2558 static bool cp_parser_check_template_parameters
2559 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2560 static cp_expr cp_parser_simple_cast_expression
2561 (cp_parser *);
2562 static tree cp_parser_global_scope_opt
2563 (cp_parser *, bool);
2564 static bool cp_parser_constructor_declarator_p
2565 (cp_parser *, cp_parser_flags, bool);
2566 static tree cp_parser_function_definition_from_specifiers_and_declarator
2567 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2568 static tree cp_parser_function_definition_after_declarator
2569 (cp_parser *, bool);
2570 static bool cp_parser_template_declaration_after_export
2571 (cp_parser *, bool);
2572 static void cp_parser_perform_template_parameter_access_checks
2573 (vec<deferred_access_check, va_gc> *);
2574 static tree cp_parser_single_declaration
2575 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2576 static cp_expr cp_parser_functional_cast
2577 (cp_parser *, tree);
2578 static tree cp_parser_save_member_function_body
2579 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2580 static tree cp_parser_save_nsdmi
2581 (cp_parser *);
2582 static tree cp_parser_enclosed_template_argument_list
2583 (cp_parser *);
2584 static void cp_parser_save_default_args
2585 (cp_parser *, tree);
2586 static void cp_parser_late_parsing_for_member
2587 (cp_parser *, tree);
2588 static tree cp_parser_late_parse_one_default_arg
2589 (cp_parser *, tree, tree, tree);
2590 static void cp_parser_late_parsing_nsdmi
2591 (cp_parser *, tree);
2592 static void cp_parser_late_parsing_default_args
2593 (cp_parser *, tree);
2594 static tree cp_parser_sizeof_operand
2595 (cp_parser *, enum rid);
2596 static cp_expr cp_parser_trait_expr
2597 (cp_parser *, enum rid);
2598 static bool cp_parser_declares_only_class_p
2599 (cp_parser *);
2600 static void cp_parser_set_storage_class
2601 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2602 static void cp_parser_set_decl_spec_type
2603 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2604 static void set_and_check_decl_spec_loc
2605 (cp_decl_specifier_seq *decl_specs,
2606 cp_decl_spec ds, cp_token *);
2607 static bool cp_parser_friend_p
2608 (const cp_decl_specifier_seq *);
2609 static void cp_parser_required_error
2610 (cp_parser *, required_token, bool, location_t);
2611 static cp_token *cp_parser_require
2612 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2613 static cp_token *cp_parser_require_keyword
2614 (cp_parser *, enum rid, required_token);
2615 static bool cp_parser_token_starts_function_definition_p
2616 (cp_token *);
2617 static bool cp_parser_next_token_starts_class_definition_p
2618 (cp_parser *);
2619 static bool cp_parser_next_token_ends_template_argument_p
2620 (cp_parser *);
2621 static bool cp_parser_nth_token_starts_template_argument_list_p
2622 (cp_parser *, size_t);
2623 static enum tag_types cp_parser_token_is_class_key
2624 (cp_token *);
2625 static enum tag_types cp_parser_token_is_type_parameter_key
2626 (cp_token *);
2627 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2628 static void cp_parser_check_class_key
2629 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2630 static void cp_parser_check_access_in_redeclaration
2631 (tree type, location_t location);
2632 static bool cp_parser_optional_template_keyword
2633 (cp_parser *);
2634 static void cp_parser_pre_parsed_nested_name_specifier
2635 (cp_parser *);
2636 static bool cp_parser_cache_group
2637 (cp_parser *, enum cpp_ttype, unsigned);
2638 static tree cp_parser_cache_defarg
2639 (cp_parser *parser, bool nsdmi);
2640 static void cp_parser_parse_tentatively
2641 (cp_parser *);
2642 static void cp_parser_commit_to_tentative_parse
2643 (cp_parser *);
2644 static void cp_parser_commit_to_topmost_tentative_parse
2645 (cp_parser *);
2646 static void cp_parser_abort_tentative_parse
2647 (cp_parser *);
2648 static bool cp_parser_parse_definitely
2649 (cp_parser *);
2650 static inline bool cp_parser_parsing_tentatively
2651 (cp_parser *);
2652 static bool cp_parser_uncommitted_to_tentative_parse_p
2653 (cp_parser *);
2654 static void cp_parser_error
2655 (cp_parser *, const char *);
2656 static void cp_parser_name_lookup_error
2657 (cp_parser *, tree, tree, name_lookup_error, location_t);
2658 static bool cp_parser_simulate_error
2659 (cp_parser *);
2660 static bool cp_parser_check_type_definition
2661 (cp_parser *);
2662 static void cp_parser_check_for_definition_in_return_type
2663 (cp_declarator *, tree, location_t type_location);
2664 static void cp_parser_check_for_invalid_template_id
2665 (cp_parser *, tree, enum tag_types, location_t location);
2666 static bool cp_parser_non_integral_constant_expression
2667 (cp_parser *, non_integral_constant);
2668 static void cp_parser_diagnose_invalid_type_name
2669 (cp_parser *, tree, location_t);
2670 static bool cp_parser_parse_and_diagnose_invalid_type_name
2671 (cp_parser *);
2672 static int cp_parser_skip_to_closing_parenthesis
2673 (cp_parser *, bool, bool, bool);
2674 static void cp_parser_skip_to_end_of_statement
2675 (cp_parser *);
2676 static void cp_parser_consume_semicolon_at_end_of_statement
2677 (cp_parser *);
2678 static void cp_parser_skip_to_end_of_block_or_statement
2679 (cp_parser *);
2680 static bool cp_parser_skip_to_closing_brace
2681 (cp_parser *);
2682 static void cp_parser_skip_to_end_of_template_parameter_list
2683 (cp_parser *);
2684 static void cp_parser_skip_to_pragma_eol
2685 (cp_parser*, cp_token *);
2686 static bool cp_parser_error_occurred
2687 (cp_parser *);
2688 static bool cp_parser_allow_gnu_extensions_p
2689 (cp_parser *);
2690 static bool cp_parser_is_pure_string_literal
2691 (cp_token *);
2692 static bool cp_parser_is_string_literal
2693 (cp_token *);
2694 static bool cp_parser_is_keyword
2695 (cp_token *, enum rid);
2696 static tree cp_parser_make_typename_type
2697 (cp_parser *, tree, location_t location);
2698 static cp_declarator * cp_parser_make_indirect_declarator
2699 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2700 static bool cp_parser_compound_literal_p
2701 (cp_parser *);
2702 static bool cp_parser_array_designator_p
2703 (cp_parser *);
2704 static bool cp_parser_init_statement_p
2705 (cp_parser *);
2706 static bool cp_parser_skip_to_closing_square_bracket
2707 (cp_parser *);
2708 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2709
2710 // -------------------------------------------------------------------------- //
2711 // Unevaluated Operand Guard
2712 //
2713 // Implementation of an RAII helper for unevaluated operand parsing.
2714 cp_unevaluated::cp_unevaluated ()
2715 {
2716 ++cp_unevaluated_operand;
2717 ++c_inhibit_evaluation_warnings;
2718 }
2719
2720 cp_unevaluated::~cp_unevaluated ()
2721 {
2722 --c_inhibit_evaluation_warnings;
2723 --cp_unevaluated_operand;
2724 }
2725
2726 // -------------------------------------------------------------------------- //
2727 // Tentative Parsing
2728
2729 /* Returns nonzero if we are parsing tentatively. */
2730
2731 static inline bool
2732 cp_parser_parsing_tentatively (cp_parser* parser)
2733 {
2734 return parser->context->next != NULL;
2735 }
2736
2737 /* Returns nonzero if TOKEN is a string literal. */
2738
2739 static bool
2740 cp_parser_is_pure_string_literal (cp_token* token)
2741 {
2742 return (token->type == CPP_STRING ||
2743 token->type == CPP_STRING16 ||
2744 token->type == CPP_STRING32 ||
2745 token->type == CPP_WSTRING ||
2746 token->type == CPP_UTF8STRING);
2747 }
2748
2749 /* Returns nonzero if TOKEN is a string literal
2750 of a user-defined string literal. */
2751
2752 static bool
2753 cp_parser_is_string_literal (cp_token* token)
2754 {
2755 return (cp_parser_is_pure_string_literal (token) ||
2756 token->type == CPP_STRING_USERDEF ||
2757 token->type == CPP_STRING16_USERDEF ||
2758 token->type == CPP_STRING32_USERDEF ||
2759 token->type == CPP_WSTRING_USERDEF ||
2760 token->type == CPP_UTF8STRING_USERDEF);
2761 }
2762
2763 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2764
2765 static bool
2766 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2767 {
2768 return token->keyword == keyword;
2769 }
2770
2771 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2772 PRAGMA_NONE. */
2773
2774 static enum pragma_kind
2775 cp_parser_pragma_kind (cp_token *token)
2776 {
2777 if (token->type != CPP_PRAGMA)
2778 return PRAGMA_NONE;
2779 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2780 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2781 }
2782
2783 /* Helper function for cp_parser_error.
2784 Having peeked a token of kind TOK1_KIND that might signify
2785 a conflict marker, peek successor tokens to determine
2786 if we actually do have a conflict marker.
2787 Specifically, we consider a run of 7 '<', '=' or '>' characters
2788 at the start of a line as a conflict marker.
2789 These come through the lexer as three pairs and a single,
2790 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2791 If it returns true, *OUT_LOC is written to with the location/range
2792 of the marker. */
2793
2794 static bool
2795 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2796 location_t *out_loc)
2797 {
2798 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2799 if (token2->type != tok1_kind)
2800 return false;
2801 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2802 if (token3->type != tok1_kind)
2803 return false;
2804 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2805 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2806 return false;
2807
2808 /* It must be at the start of the line. */
2809 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2810 if (LOCATION_COLUMN (start_loc) != 1)
2811 return false;
2812
2813 /* We have a conflict marker. Construct a location of the form:
2814 <<<<<<<
2815 ^~~~~~~
2816 with start == caret, finishing at the end of the marker. */
2817 location_t finish_loc = get_finish (token4->location);
2818 *out_loc = make_location (start_loc, start_loc, finish_loc);
2819
2820 return true;
2821 }
2822
2823 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2824 RT_CLOSE_PAREN. */
2825
2826 static const char *
2827 get_matching_symbol (required_token token_desc)
2828 {
2829 switch (token_desc)
2830 {
2831 default:
2832 gcc_unreachable ();
2833 return "";
2834 case RT_CLOSE_BRACE:
2835 return "{";
2836 case RT_CLOSE_PAREN:
2837 return "(";
2838 }
2839 }
2840
2841 /* Attempt to convert TOKEN_DESC from a required_token to an
2842 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2843
2844 static enum cpp_ttype
2845 get_required_cpp_ttype (required_token token_desc)
2846 {
2847 switch (token_desc)
2848 {
2849 case RT_SEMICOLON:
2850 return CPP_SEMICOLON;
2851 case RT_OPEN_PAREN:
2852 return CPP_OPEN_PAREN;
2853 case RT_CLOSE_BRACE:
2854 return CPP_CLOSE_BRACE;
2855 case RT_OPEN_BRACE:
2856 return CPP_OPEN_BRACE;
2857 case RT_CLOSE_SQUARE:
2858 return CPP_CLOSE_SQUARE;
2859 case RT_OPEN_SQUARE:
2860 return CPP_OPEN_SQUARE;
2861 case RT_COMMA:
2862 return CPP_COMMA;
2863 case RT_COLON:
2864 return CPP_COLON;
2865 case RT_CLOSE_PAREN:
2866 return CPP_CLOSE_PAREN;
2867
2868 default:
2869 /* Use CPP_EOF as a "no completions possible" code. */
2870 return CPP_EOF;
2871 }
2872 }
2873
2874
2875 /* Subroutine of cp_parser_error and cp_parser_required_error.
2876
2877 Issue a diagnostic of the form
2878 FILE:LINE: MESSAGE before TOKEN
2879 where TOKEN is the next token in the input stream. MESSAGE
2880 (specified by the caller) is usually of the form "expected
2881 OTHER-TOKEN".
2882
2883 This bypasses the check for tentative passing, and potentially
2884 adds material needed by cp_parser_required_error.
2885
2886 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2887 suggesting insertion of the missing token.
2888
2889 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2890 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2891 location. */
2892
2893 static void
2894 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2895 required_token missing_token_desc,
2896 location_t matching_location)
2897 {
2898 cp_token *token = cp_lexer_peek_token (parser->lexer);
2899 /* This diagnostic makes more sense if it is tagged to the line
2900 of the token we just peeked at. */
2901 cp_lexer_set_source_position_from_token (token);
2902
2903 if (token->type == CPP_PRAGMA)
2904 {
2905 error_at (token->location,
2906 "%<#pragma%> is not allowed here");
2907 cp_parser_skip_to_pragma_eol (parser, token);
2908 return;
2909 }
2910
2911 /* If this is actually a conflict marker, report it as such. */
2912 if (token->type == CPP_LSHIFT
2913 || token->type == CPP_RSHIFT
2914 || token->type == CPP_EQ_EQ)
2915 {
2916 location_t loc;
2917 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2918 {
2919 error_at (loc, "version control conflict marker in file");
2920 expanded_location token_exploc = expand_location (token->location);
2921 /* Consume tokens until the end of the source line. */
2922 for (;;)
2923 {
2924 cp_lexer_consume_token (parser->lexer);
2925 cp_token *next = cp_lexer_peek_token (parser->lexer);
2926 if (next->type == CPP_EOF)
2927 break;
2928 if (next->location == UNKNOWN_LOCATION
2929 || loc == UNKNOWN_LOCATION)
2930 break;
2931
2932 expanded_location next_exploc = expand_location (next->location);
2933 if (next_exploc.file != token_exploc.file)
2934 break;
2935 if (next_exploc.line != token_exploc.line)
2936 break;
2937 }
2938 return;
2939 }
2940 }
2941
2942 auto_diagnostic_group d;
2943 gcc_rich_location richloc (input_location);
2944
2945 bool added_matching_location = false;
2946
2947 if (missing_token_desc != RT_NONE)
2948 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
2949 {
2950 /* Potentially supply a fix-it hint, suggesting to add the
2951 missing token immediately after the *previous* token.
2952 This may move the primary location within richloc. */
2953 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2954 location_t prev_token_loc = prev_token->location;
2955 maybe_suggest_missing_token_insertion (&richloc, ttype,
2956 prev_token_loc);
2957
2958 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2959 Attempt to consolidate diagnostics by printing it as a
2960 secondary range within the main diagnostic. */
2961 if (matching_location != UNKNOWN_LOCATION)
2962 added_matching_location
2963 = richloc.add_location_if_nearby (matching_location);
2964 }
2965
2966 /* If we were parsing a string-literal and there is an unknown name
2967 token right after, then check to see if that could also have been
2968 a literal string by checking the name against a list of known
2969 standard string literal constants defined in header files. If
2970 there is one, then add that as an hint to the error message. */
2971 name_hint h;
2972 if (token->type == CPP_NAME)
2973 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
2974 if (cp_parser_is_string_literal (prev_token))
2975 {
2976 tree name = token->u.value;
2977 const char *token_name = IDENTIFIER_POINTER (name);
2978 const char *header_hint
2979 = get_cp_stdlib_header_for_string_macro_name (token_name);
2980 if (header_hint != NULL)
2981 h = name_hint (NULL, new suggest_missing_header (token->location,
2982 token_name,
2983 header_hint));
2984 }
2985
2986 /* Actually emit the error. */
2987 c_parse_error (gmsgid,
2988 /* Because c_parser_error does not understand
2989 CPP_KEYWORD, keywords are treated like
2990 identifiers. */
2991 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2992 token->u.value, token->flags, &richloc);
2993
2994 if (missing_token_desc != RT_NONE)
2995 {
2996 /* If we weren't able to consolidate matching_location, then
2997 print it as a secondary diagnostic. */
2998 if (matching_location != UNKNOWN_LOCATION
2999 && !added_matching_location)
3000 inform (matching_location, "to match this %qs",
3001 get_matching_symbol (missing_token_desc));
3002 }
3003 }
3004
3005 /* If not parsing tentatively, issue a diagnostic of the form
3006 FILE:LINE: MESSAGE before TOKEN
3007 where TOKEN is the next token in the input stream. MESSAGE
3008 (specified by the caller) is usually of the form "expected
3009 OTHER-TOKEN". */
3010
3011 static void
3012 cp_parser_error (cp_parser* parser, const char* gmsgid)
3013 {
3014 if (!cp_parser_simulate_error (parser))
3015 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3016 }
3017
3018 /* Issue an error about name-lookup failing. NAME is the
3019 IDENTIFIER_NODE DECL is the result of
3020 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3021 the thing that we hoped to find. */
3022
3023 static void
3024 cp_parser_name_lookup_error (cp_parser* parser,
3025 tree name,
3026 tree decl,
3027 name_lookup_error desired,
3028 location_t location)
3029 {
3030 /* If name lookup completely failed, tell the user that NAME was not
3031 declared. */
3032 if (decl == error_mark_node)
3033 {
3034 if (parser->scope && parser->scope != global_namespace)
3035 error_at (location, "%<%E::%E%> has not been declared",
3036 parser->scope, name);
3037 else if (parser->scope == global_namespace)
3038 error_at (location, "%<::%E%> has not been declared", name);
3039 else if (parser->object_scope
3040 && !CLASS_TYPE_P (parser->object_scope))
3041 error_at (location, "request for member %qE in non-class type %qT",
3042 name, parser->object_scope);
3043 else if (parser->object_scope)
3044 error_at (location, "%<%T::%E%> has not been declared",
3045 parser->object_scope, name);
3046 else
3047 error_at (location, "%qE has not been declared", name);
3048 }
3049 else if (parser->scope && parser->scope != global_namespace)
3050 {
3051 switch (desired)
3052 {
3053 case NLE_TYPE:
3054 error_at (location, "%<%E::%E%> is not a type",
3055 parser->scope, name);
3056 break;
3057 case NLE_CXX98:
3058 error_at (location, "%<%E::%E%> is not a class or namespace",
3059 parser->scope, name);
3060 break;
3061 case NLE_NOT_CXX98:
3062 error_at (location,
3063 "%<%E::%E%> is not a class, namespace, or enumeration",
3064 parser->scope, name);
3065 break;
3066 default:
3067 gcc_unreachable ();
3068
3069 }
3070 }
3071 else if (parser->scope == global_namespace)
3072 {
3073 switch (desired)
3074 {
3075 case NLE_TYPE:
3076 error_at (location, "%<::%E%> is not a type", name);
3077 break;
3078 case NLE_CXX98:
3079 error_at (location, "%<::%E%> is not a class or namespace", name);
3080 break;
3081 case NLE_NOT_CXX98:
3082 error_at (location,
3083 "%<::%E%> is not a class, namespace, or enumeration",
3084 name);
3085 break;
3086 default:
3087 gcc_unreachable ();
3088 }
3089 }
3090 else
3091 {
3092 switch (desired)
3093 {
3094 case NLE_TYPE:
3095 error_at (location, "%qE is not a type", name);
3096 break;
3097 case NLE_CXX98:
3098 error_at (location, "%qE is not a class or namespace", name);
3099 break;
3100 case NLE_NOT_CXX98:
3101 error_at (location,
3102 "%qE is not a class, namespace, or enumeration", name);
3103 break;
3104 default:
3105 gcc_unreachable ();
3106 }
3107 }
3108 }
3109
3110 /* If we are parsing tentatively, remember that an error has occurred
3111 during this tentative parse. Returns true if the error was
3112 simulated; false if a message should be issued by the caller. */
3113
3114 static bool
3115 cp_parser_simulate_error (cp_parser* parser)
3116 {
3117 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3118 {
3119 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3120 return true;
3121 }
3122 return false;
3123 }
3124
3125 /* This function is called when a type is defined. If type
3126 definitions are forbidden at this point, an error message is
3127 issued. */
3128
3129 static bool
3130 cp_parser_check_type_definition (cp_parser* parser)
3131 {
3132 /* If types are forbidden here, issue a message. */
3133 if (parser->type_definition_forbidden_message)
3134 {
3135 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3136 or %qs in the message need to be interpreted. */
3137 error (parser->type_definition_forbidden_message,
3138 parser->type_definition_forbidden_message_arg);
3139 return false;
3140 }
3141 return true;
3142 }
3143
3144 /* This function is called when the DECLARATOR is processed. The TYPE
3145 was a type defined in the decl-specifiers. If it is invalid to
3146 define a type in the decl-specifiers for DECLARATOR, an error is
3147 issued. TYPE_LOCATION is the location of TYPE and is used
3148 for error reporting. */
3149
3150 static void
3151 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3152 tree type, location_t type_location)
3153 {
3154 /* [dcl.fct] forbids type definitions in return types.
3155 Unfortunately, it's not easy to know whether or not we are
3156 processing a return type until after the fact. */
3157 while (declarator
3158 && (declarator->kind == cdk_pointer
3159 || declarator->kind == cdk_reference
3160 || declarator->kind == cdk_ptrmem))
3161 declarator = declarator->declarator;
3162 if (declarator
3163 && declarator->kind == cdk_function)
3164 {
3165 error_at (type_location,
3166 "new types may not be defined in a return type");
3167 inform (type_location,
3168 "(perhaps a semicolon is missing after the definition of %qT)",
3169 type);
3170 }
3171 }
3172
3173 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3174 "<" in any valid C++ program. If the next token is indeed "<",
3175 issue a message warning the user about what appears to be an
3176 invalid attempt to form a template-id. LOCATION is the location
3177 of the type-specifier (TYPE) */
3178
3179 static void
3180 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3181 tree type,
3182 enum tag_types tag_type,
3183 location_t location)
3184 {
3185 cp_token_position start = 0;
3186
3187 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3188 {
3189 if (TREE_CODE (type) == TYPE_DECL)
3190 type = TREE_TYPE (type);
3191 if (TYPE_P (type) && !template_placeholder_p (type))
3192 error_at (location, "%qT is not a template", type);
3193 else if (identifier_p (type))
3194 {
3195 if (tag_type != none_type)
3196 error_at (location, "%qE is not a class template", type);
3197 else
3198 error_at (location, "%qE is not a template", type);
3199 }
3200 else
3201 error_at (location, "invalid template-id");
3202 /* Remember the location of the invalid "<". */
3203 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3204 start = cp_lexer_token_position (parser->lexer, true);
3205 /* Consume the "<". */
3206 cp_lexer_consume_token (parser->lexer);
3207 /* Parse the template arguments. */
3208 cp_parser_enclosed_template_argument_list (parser);
3209 /* Permanently remove the invalid template arguments so that
3210 this error message is not issued again. */
3211 if (start)
3212 cp_lexer_purge_tokens_after (parser->lexer, start);
3213 }
3214 }
3215
3216 /* If parsing an integral constant-expression, issue an error message
3217 about the fact that THING appeared and return true. Otherwise,
3218 return false. In either case, set
3219 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3220
3221 static bool
3222 cp_parser_non_integral_constant_expression (cp_parser *parser,
3223 non_integral_constant thing)
3224 {
3225 parser->non_integral_constant_expression_p = true;
3226 if (parser->integral_constant_expression_p)
3227 {
3228 if (!parser->allow_non_integral_constant_expression_p)
3229 {
3230 const char *msg = NULL;
3231 switch (thing)
3232 {
3233 case NIC_FLOAT:
3234 pedwarn (input_location, OPT_Wpedantic,
3235 "ISO C++ forbids using a floating-point literal "
3236 "in a constant-expression");
3237 return true;
3238 case NIC_CAST:
3239 error ("a cast to a type other than an integral or "
3240 "enumeration type cannot appear in a "
3241 "constant-expression");
3242 return true;
3243 case NIC_TYPEID:
3244 error ("%<typeid%> operator "
3245 "cannot appear in a constant-expression");
3246 return true;
3247 case NIC_NCC:
3248 error ("non-constant compound literals "
3249 "cannot appear in a constant-expression");
3250 return true;
3251 case NIC_FUNC_CALL:
3252 error ("a function call "
3253 "cannot appear in a constant-expression");
3254 return true;
3255 case NIC_INC:
3256 error ("an increment "
3257 "cannot appear in a constant-expression");
3258 return true;
3259 case NIC_DEC:
3260 error ("an decrement "
3261 "cannot appear in a constant-expression");
3262 return true;
3263 case NIC_ARRAY_REF:
3264 error ("an array reference "
3265 "cannot appear in a constant-expression");
3266 return true;
3267 case NIC_ADDR_LABEL:
3268 error ("the address of a label "
3269 "cannot appear in a constant-expression");
3270 return true;
3271 case NIC_OVERLOADED:
3272 error ("calls to overloaded operators "
3273 "cannot appear in a constant-expression");
3274 return true;
3275 case NIC_ASSIGNMENT:
3276 error ("an assignment cannot appear in a constant-expression");
3277 return true;
3278 case NIC_COMMA:
3279 error ("a comma operator "
3280 "cannot appear in a constant-expression");
3281 return true;
3282 case NIC_CONSTRUCTOR:
3283 error ("a call to a constructor "
3284 "cannot appear in a constant-expression");
3285 return true;
3286 case NIC_TRANSACTION:
3287 error ("a transaction expression "
3288 "cannot appear in a constant-expression");
3289 return true;
3290 case NIC_THIS:
3291 msg = "this";
3292 break;
3293 case NIC_FUNC_NAME:
3294 msg = "__FUNCTION__";
3295 break;
3296 case NIC_PRETTY_FUNC:
3297 msg = "__PRETTY_FUNCTION__";
3298 break;
3299 case NIC_C99_FUNC:
3300 msg = "__func__";
3301 break;
3302 case NIC_VA_ARG:
3303 msg = "va_arg";
3304 break;
3305 case NIC_ARROW:
3306 msg = "->";
3307 break;
3308 case NIC_POINT:
3309 msg = ".";
3310 break;
3311 case NIC_STAR:
3312 msg = "*";
3313 break;
3314 case NIC_ADDR:
3315 msg = "&";
3316 break;
3317 case NIC_PREINCREMENT:
3318 msg = "++";
3319 break;
3320 case NIC_PREDECREMENT:
3321 msg = "--";
3322 break;
3323 case NIC_NEW:
3324 msg = "new";
3325 break;
3326 case NIC_DEL:
3327 msg = "delete";
3328 break;
3329 default:
3330 gcc_unreachable ();
3331 }
3332 if (msg)
3333 error ("%qs cannot appear in a constant-expression", msg);
3334 return true;
3335 }
3336 }
3337 return false;
3338 }
3339
3340 /* Emit a diagnostic for an invalid type name. This function commits
3341 to the current active tentative parse, if any. (Otherwise, the
3342 problematic construct might be encountered again later, resulting
3343 in duplicate error messages.) LOCATION is the location of ID. */
3344
3345 static void
3346 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3347 location_t location)
3348 {
3349 tree decl, ambiguous_decls;
3350 cp_parser_commit_to_tentative_parse (parser);
3351 /* Try to lookup the identifier. */
3352 decl = cp_parser_lookup_name (parser, id, none_type,
3353 /*is_template=*/false,
3354 /*is_namespace=*/false,
3355 /*check_dependency=*/true,
3356 &ambiguous_decls, location);
3357 if (ambiguous_decls)
3358 /* If the lookup was ambiguous, an error will already have
3359 been issued. */
3360 return;
3361 /* If the lookup found a template-name, it means that the user forgot
3362 to specify an argument list. Emit a useful error message. */
3363 if (DECL_TYPE_TEMPLATE_P (decl))
3364 {
3365 auto_diagnostic_group d;
3366 error_at (location,
3367 "invalid use of template-name %qE without an argument list",
3368 decl);
3369 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3370 inform (location, "class template argument deduction is only available "
3371 "with %<-std=c++17%> or %<-std=gnu++17%>");
3372 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3373 }
3374 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3375 error_at (location, "invalid use of destructor %qD as a type", id);
3376 else if (TREE_CODE (decl) == TYPE_DECL)
3377 /* Something like 'unsigned A a;' */
3378 error_at (location, "invalid combination of multiple type-specifiers");
3379 else if (!parser->scope)
3380 {
3381 /* Issue an error message. */
3382 auto_diagnostic_group d;
3383 name_hint hint;
3384 if (TREE_CODE (id) == IDENTIFIER_NODE)
3385 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3386 if (const char *suggestion = hint.suggestion ())
3387 {
3388 gcc_rich_location richloc (location);
3389 richloc.add_fixit_replace (suggestion);
3390 error_at (&richloc,
3391 "%qE does not name a type; did you mean %qs?",
3392 id, suggestion);
3393 }
3394 else
3395 error_at (location, "%qE does not name a type", id);
3396 /* If we're in a template class, it's possible that the user was
3397 referring to a type from a base class. For example:
3398
3399 template <typename T> struct A { typedef T X; };
3400 template <typename T> struct B : public A<T> { X x; };
3401
3402 The user should have said "typename A<T>::X". */
3403 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3404 inform (location, "C++11 %<constexpr%> only available with "
3405 "%<-std=c++11%> or %<-std=gnu++11%>");
3406 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3407 inform (location, "C++11 %<noexcept%> only available with "
3408 "%<-std=c++11%> or %<-std=gnu++11%>");
3409 else if (cxx_dialect < cxx11
3410 && TREE_CODE (id) == IDENTIFIER_NODE
3411 && id_equal (id, "thread_local"))
3412 inform (location, "C++11 %<thread_local%> only available with "
3413 "%<-std=c++11%> or %<-std=gnu++11%>");
3414 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3415 inform (location, "C++20 %<constinit%> only available with "
3416 "%<-std=c++20%> or %<-std=gnu++20%>");
3417 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3418 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3419 "%<-fconcepts%>");
3420 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3421 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3422 "%<-fconcepts%>");
3423 else if (processing_template_decl && current_class_type
3424 && TYPE_BINFO (current_class_type))
3425 {
3426 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3427 b; b = TREE_CHAIN (b))
3428 {
3429 tree base_type = BINFO_TYPE (b);
3430 if (CLASS_TYPE_P (base_type)
3431 && dependent_type_p (base_type))
3432 {
3433 /* Go from a particular instantiation of the
3434 template (which will have an empty TYPE_FIELDs),
3435 to the main version. */
3436 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3437 for (tree field = TYPE_FIELDS (base_type);
3438 field; field = DECL_CHAIN (field))
3439 if (TREE_CODE (field) == TYPE_DECL
3440 && DECL_NAME (field) == id)
3441 {
3442 inform (location,
3443 "(perhaps %<typename %T::%E%> was intended)",
3444 BINFO_TYPE (b), id);
3445 goto found;
3446 }
3447 }
3448 }
3449 found:;
3450 }
3451 }
3452 /* Here we diagnose qualified-ids where the scope is actually correct,
3453 but the identifier does not resolve to a valid type name. */
3454 else if (parser->scope != error_mark_node)
3455 {
3456 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3457 {
3458 auto_diagnostic_group d;
3459 name_hint hint;
3460 if (decl == error_mark_node)
3461 hint = suggest_alternative_in_explicit_scope (location, id,
3462 parser->scope);
3463 const char *suggestion = hint.suggestion ();
3464 gcc_rich_location richloc (location_of (id));
3465 if (suggestion)
3466 richloc.add_fixit_replace (suggestion);
3467 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3468 {
3469 if (suggestion)
3470 error_at (&richloc,
3471 "%qE in namespace %qE does not name a template"
3472 " type; did you mean %qs?",
3473 id, parser->scope, suggestion);
3474 else
3475 error_at (&richloc,
3476 "%qE in namespace %qE does not name a template type",
3477 id, parser->scope);
3478 }
3479 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3480 {
3481 if (suggestion)
3482 error_at (&richloc,
3483 "%qE in namespace %qE does not name a template"
3484 " type; did you mean %qs?",
3485 TREE_OPERAND (id, 0), parser->scope, suggestion);
3486 else
3487 error_at (&richloc,
3488 "%qE in namespace %qE does not name a template"
3489 " type",
3490 TREE_OPERAND (id, 0), parser->scope);
3491 }
3492 else
3493 {
3494 if (suggestion)
3495 error_at (&richloc,
3496 "%qE in namespace %qE does not name a type"
3497 "; did you mean %qs?",
3498 id, parser->scope, suggestion);
3499 else
3500 error_at (&richloc,
3501 "%qE in namespace %qE does not name a type",
3502 id, parser->scope);
3503 }
3504 if (DECL_P (decl))
3505 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3506 }
3507 else if (CLASS_TYPE_P (parser->scope)
3508 && constructor_name_p (id, parser->scope))
3509 {
3510 /* A<T>::A<T>() */
3511 auto_diagnostic_group d;
3512 error_at (location, "%<%T::%E%> names the constructor, not"
3513 " the type", parser->scope, id);
3514 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3515 error_at (location, "and %qT has no template constructors",
3516 parser->scope);
3517 }
3518 else if (TYPE_P (parser->scope)
3519 && dependent_scope_p (parser->scope))
3520 {
3521 gcc_rich_location richloc (location);
3522 richloc.add_fixit_insert_before ("typename ");
3523 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3524 error_at (&richloc,
3525 "need %<typename%> before %<%T::%D::%E%> because "
3526 "%<%T::%D%> is a dependent scope",
3527 TYPE_CONTEXT (parser->scope),
3528 TYPENAME_TYPE_FULLNAME (parser->scope),
3529 id,
3530 TYPE_CONTEXT (parser->scope),
3531 TYPENAME_TYPE_FULLNAME (parser->scope));
3532 else
3533 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3534 "%qT is a dependent scope",
3535 parser->scope, id, parser->scope);
3536 }
3537 else if (TYPE_P (parser->scope))
3538 {
3539 auto_diagnostic_group d;
3540 if (!COMPLETE_TYPE_P (parser->scope))
3541 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3542 parser->scope);
3543 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3544 error_at (location_of (id),
3545 "%qE in %q#T does not name a template type",
3546 id, parser->scope);
3547 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3548 error_at (location_of (id),
3549 "%qE in %q#T does not name a template type",
3550 TREE_OPERAND (id, 0), parser->scope);
3551 else
3552 error_at (location_of (id),
3553 "%qE in %q#T does not name a type",
3554 id, parser->scope);
3555 if (DECL_P (decl))
3556 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3557 }
3558 else
3559 gcc_unreachable ();
3560 }
3561 }
3562
3563 /* Check for a common situation where a type-name should be present,
3564 but is not, and issue a sensible error message. Returns true if an
3565 invalid type-name was detected.
3566
3567 The situation handled by this function are variable declarations of the
3568 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3569 Usually, `ID' should name a type, but if we got here it means that it
3570 does not. We try to emit the best possible error message depending on
3571 how exactly the id-expression looks like. */
3572
3573 static bool
3574 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3575 {
3576 tree id;
3577 cp_token *token = cp_lexer_peek_token (parser->lexer);
3578
3579 /* Avoid duplicate error about ambiguous lookup. */
3580 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3581 {
3582 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3583 if (next->type == CPP_NAME && next->error_reported)
3584 goto out;
3585 }
3586
3587 cp_parser_parse_tentatively (parser);
3588 id = cp_parser_id_expression (parser,
3589 /*template_keyword_p=*/false,
3590 /*check_dependency_p=*/true,
3591 /*template_p=*/NULL,
3592 /*declarator_p=*/false,
3593 /*optional_p=*/false);
3594 /* If the next token is a (, this is a function with no explicit return
3595 type, i.e. constructor, destructor or conversion op. */
3596 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3597 || TREE_CODE (id) == TYPE_DECL)
3598 {
3599 cp_parser_abort_tentative_parse (parser);
3600 return false;
3601 }
3602 if (!cp_parser_parse_definitely (parser))
3603 return false;
3604
3605 /* Emit a diagnostic for the invalid type. */
3606 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3607 out:
3608 /* If we aren't in the middle of a declarator (i.e. in a
3609 parameter-declaration-clause), skip to the end of the declaration;
3610 there's no point in trying to process it. */
3611 if (!parser->in_declarator_p)
3612 cp_parser_skip_to_end_of_block_or_statement (parser);
3613 return true;
3614 }
3615
3616 /* Consume tokens up to, and including, the next non-nested closing `)'.
3617 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3618 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3619 found an unnested token of that type. */
3620
3621 static int
3622 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3623 bool recovering,
3624 cpp_ttype or_ttype,
3625 bool consume_paren)
3626 {
3627 unsigned paren_depth = 0;
3628 unsigned brace_depth = 0;
3629 unsigned square_depth = 0;
3630 unsigned condop_depth = 0;
3631
3632 if (recovering && or_ttype == CPP_EOF
3633 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3634 return 0;
3635
3636 while (true)
3637 {
3638 cp_token * token = cp_lexer_peek_token (parser->lexer);
3639
3640 /* Have we found what we're looking for before the closing paren? */
3641 if (token->type == or_ttype && or_ttype != CPP_EOF
3642 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3643 return -1;
3644
3645 switch (token->type)
3646 {
3647 case CPP_PRAGMA_EOL:
3648 if (!parser->lexer->in_pragma)
3649 break;
3650 /* FALLTHRU */
3651 case CPP_EOF:
3652 /* If we've run out of tokens, then there is no closing `)'. */
3653 return 0;
3654
3655 /* This is good for lambda expression capture-lists. */
3656 case CPP_OPEN_SQUARE:
3657 ++square_depth;
3658 break;
3659 case CPP_CLOSE_SQUARE:
3660 if (!square_depth--)
3661 return 0;
3662 break;
3663
3664 case CPP_SEMICOLON:
3665 /* This matches the processing in skip_to_end_of_statement. */
3666 if (!brace_depth)
3667 return 0;
3668 break;
3669
3670 case CPP_OPEN_BRACE:
3671 ++brace_depth;
3672 break;
3673 case CPP_CLOSE_BRACE:
3674 if (!brace_depth--)
3675 return 0;
3676 break;
3677
3678 case CPP_OPEN_PAREN:
3679 if (!brace_depth)
3680 ++paren_depth;
3681 break;
3682
3683 case CPP_CLOSE_PAREN:
3684 if (!brace_depth && !paren_depth--)
3685 {
3686 if (consume_paren)
3687 cp_lexer_consume_token (parser->lexer);
3688 return 1;
3689 }
3690 break;
3691
3692 case CPP_QUERY:
3693 if (!brace_depth && !paren_depth && !square_depth)
3694 ++condop_depth;
3695 break;
3696
3697 case CPP_COLON:
3698 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3699 condop_depth--;
3700 break;
3701
3702 case CPP_PRAGMA:
3703 /* We fell into a pragma. Skip it, and continue. */
3704 cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
3705 continue;
3706
3707 default:
3708 break;
3709 }
3710
3711 /* Consume the token. */
3712 cp_lexer_consume_token (parser->lexer);
3713 }
3714 }
3715
3716 /* Consume tokens up to, and including, the next non-nested closing `)'.
3717 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3718 are doing error recovery. Returns -1 if OR_COMMA is true and we
3719 found an unnested token of that type. */
3720
3721 static int
3722 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3723 bool recovering,
3724 bool or_comma,
3725 bool consume_paren)
3726 {
3727 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3728 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3729 ttype, consume_paren);
3730 }
3731
3732 /* Consume tokens until we reach the end of the current statement.
3733 Normally, that will be just before consuming a `;'. However, if a
3734 non-nested `}' comes first, then we stop before consuming that. */
3735
3736 static void
3737 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3738 {
3739 unsigned nesting_depth = 0;
3740
3741 /* Unwind generic function template scope if necessary. */
3742 if (parser->fully_implicit_function_template_p)
3743 abort_fully_implicit_template (parser);
3744
3745 while (true)
3746 {
3747 cp_token *token = cp_lexer_peek_token (parser->lexer);
3748
3749 switch (token->type)
3750 {
3751 case CPP_PRAGMA_EOL:
3752 if (!parser->lexer->in_pragma)
3753 break;
3754 /* FALLTHRU */
3755 case CPP_EOF:
3756 /* If we've run out of tokens, stop. */
3757 return;
3758
3759 case CPP_SEMICOLON:
3760 /* If the next token is a `;', we have reached the end of the
3761 statement. */
3762 if (!nesting_depth)
3763 return;
3764 break;
3765
3766 case CPP_CLOSE_BRACE:
3767 /* If this is a non-nested '}', stop before consuming it.
3768 That way, when confronted with something like:
3769
3770 { 3 + }
3771
3772 we stop before consuming the closing '}', even though we
3773 have not yet reached a `;'. */
3774 if (nesting_depth == 0)
3775 return;
3776
3777 /* If it is the closing '}' for a block that we have
3778 scanned, stop -- but only after consuming the token.
3779 That way given:
3780
3781 void f g () { ... }
3782 typedef int I;
3783
3784 we will stop after the body of the erroneously declared
3785 function, but before consuming the following `typedef'
3786 declaration. */
3787 if (--nesting_depth == 0)
3788 {
3789 cp_lexer_consume_token (parser->lexer);
3790 return;
3791 }
3792 break;
3793
3794 case CPP_OPEN_BRACE:
3795 ++nesting_depth;
3796 break;
3797
3798 case CPP_PRAGMA:
3799 /* We fell into a pragma. Skip it, and continue or return. */
3800 cp_parser_skip_to_pragma_eol (parser, token);
3801 if (!nesting_depth)
3802 return;
3803 continue;
3804
3805 default:
3806 break;
3807 }
3808
3809 /* Consume the token. */
3810 cp_lexer_consume_token (parser->lexer);
3811 }
3812 }
3813
3814 /* This function is called at the end of a statement or declaration.
3815 If the next token is a semicolon, it is consumed; otherwise, error
3816 recovery is attempted. */
3817
3818 static void
3819 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3820 {
3821 /* Look for the trailing `;'. */
3822 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3823 {
3824 /* If there is additional (erroneous) input, skip to the end of
3825 the statement. */
3826 cp_parser_skip_to_end_of_statement (parser);
3827 /* If the next token is now a `;', consume it. */
3828 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3829 cp_lexer_consume_token (parser->lexer);
3830 }
3831 }
3832
3833 /* Skip tokens until we have consumed an entire block, or until we
3834 have consumed a non-nested `;'. */
3835
3836 static void
3837 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3838 {
3839 int nesting_depth = 0;
3840
3841 /* Unwind generic function template scope if necessary. */
3842 if (parser->fully_implicit_function_template_p)
3843 abort_fully_implicit_template (parser);
3844
3845 while (nesting_depth >= 0)
3846 {
3847 cp_token *token = cp_lexer_peek_token (parser->lexer);
3848
3849 switch (token->type)
3850 {
3851 case CPP_PRAGMA_EOL:
3852 if (!parser->lexer->in_pragma)
3853 break;
3854 /* FALLTHRU */
3855 case CPP_EOF:
3856 /* If we've run out of tokens, stop. */
3857 return;
3858
3859 case CPP_SEMICOLON:
3860 /* Stop if this is an unnested ';'. */
3861 if (!nesting_depth)
3862 nesting_depth = -1;
3863 break;
3864
3865 case CPP_CLOSE_BRACE:
3866 /* Stop if this is an unnested '}', or closes the outermost
3867 nesting level. */
3868 nesting_depth--;
3869 if (nesting_depth < 0)
3870 return;
3871 if (!nesting_depth)
3872 nesting_depth = -1;
3873 break;
3874
3875 case CPP_OPEN_BRACE:
3876 /* Nest. */
3877 nesting_depth++;
3878 break;
3879
3880 case CPP_PRAGMA:
3881 /* Skip it, and continue or return. */
3882 cp_parser_skip_to_pragma_eol (parser, token);
3883 if (!nesting_depth)
3884 return;
3885 continue;
3886
3887 default:
3888 break;
3889 }
3890
3891 /* Consume the token. */
3892 cp_lexer_consume_token (parser->lexer);
3893 }
3894 }
3895
3896 /* Skip tokens until a non-nested closing curly brace is the next
3897 token, or there are no more tokens. Return true in the first case,
3898 false otherwise. */
3899
3900 static bool
3901 cp_parser_skip_to_closing_brace (cp_parser *parser)
3902 {
3903 unsigned nesting_depth = 0;
3904
3905 while (true)
3906 {
3907 cp_token *token = cp_lexer_peek_token (parser->lexer);
3908
3909 switch (token->type)
3910 {
3911 case CPP_PRAGMA_EOL:
3912 if (!parser->lexer->in_pragma)
3913 break;
3914 /* FALLTHRU */
3915 case CPP_EOF:
3916 /* If we've run out of tokens, stop. */
3917 return false;
3918
3919 case CPP_CLOSE_BRACE:
3920 /* If the next token is a non-nested `}', then we have reached
3921 the end of the current block. */
3922 if (nesting_depth-- == 0)
3923 return true;
3924 break;
3925
3926 case CPP_OPEN_BRACE:
3927 /* If it the next token is a `{', then we are entering a new
3928 block. Consume the entire block. */
3929 ++nesting_depth;
3930 break;
3931
3932 default:
3933 break;
3934 }
3935
3936 /* Consume the token. */
3937 cp_lexer_consume_token (parser->lexer);
3938 }
3939 }
3940
3941 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3942 parameter is the PRAGMA token, allowing us to purge the entire pragma
3943 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
3944 forwards (not error recovery). */
3945
3946 static void
3947 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3948 {
3949 cp_token *token;
3950
3951 do
3952 {
3953 /* The preprocessor makes sure that a PRAGMA_EOL token appears
3954 before an EOF token, even when the EOF is on the pragma line.
3955 We should never get here without being inside a deferred
3956 pragma. */
3957 gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
3958 token = cp_lexer_consume_token (parser->lexer);
3959 }
3960 while (token->type != CPP_PRAGMA_EOL);
3961
3962 if (pragma_tok)
3963 {
3964 /* Ensure that the pragma is not parsed again. */
3965 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3966 parser->lexer->in_pragma = false;
3967 }
3968 }
3969
3970 /* Require pragma end of line, resyncing with it as necessary. The
3971 arguments are as for cp_parser_skip_to_pragma_eol. */
3972
3973 static void
3974 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3975 {
3976 parser->lexer->in_pragma = false;
3977 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3978 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3979 }
3980
3981 /* This is a simple wrapper around make_typename_type. When the id is
3982 an unresolved identifier node, we can provide a superior diagnostic
3983 using cp_parser_diagnose_invalid_type_name. */
3984
3985 static tree
3986 cp_parser_make_typename_type (cp_parser *parser, tree id,
3987 location_t id_location)
3988 {
3989 tree result;
3990 if (identifier_p (id))
3991 {
3992 result = make_typename_type (parser->scope, id, typename_type,
3993 /*complain=*/tf_none);
3994 if (result == error_mark_node)
3995 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3996 return result;
3997 }
3998 return make_typename_type (parser->scope, id, typename_type, tf_error);
3999 }
4000
4001 /* This is a wrapper around the
4002 make_{pointer,ptrmem,reference}_declarator functions that decides
4003 which one to call based on the CODE and CLASS_TYPE arguments. The
4004 CODE argument should be one of the values returned by
4005 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4006 appertain to the pointer or reference. */
4007
4008 static cp_declarator *
4009 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4010 cp_cv_quals cv_qualifiers,
4011 cp_declarator *target,
4012 tree attributes)
4013 {
4014 if (code == ERROR_MARK || target == cp_error_declarator)
4015 return cp_error_declarator;
4016
4017 if (code == INDIRECT_REF)
4018 if (class_type == NULL_TREE)
4019 return make_pointer_declarator (cv_qualifiers, target, attributes);
4020 else
4021 return make_ptrmem_declarator (cv_qualifiers, class_type,
4022 target, attributes);
4023 else if (code == ADDR_EXPR && class_type == NULL_TREE)
4024 return make_reference_declarator (cv_qualifiers, target,
4025 false, attributes);
4026 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4027 return make_reference_declarator (cv_qualifiers, target,
4028 true, attributes);
4029 gcc_unreachable ();
4030 }
4031
4032 /* Create a new C++ parser. */
4033
4034 static cp_parser *
4035 cp_parser_new (cp_lexer *lexer)
4036 {
4037 /* Initialize the binops_by_token so that we can get the tree
4038 directly from the token. */
4039 for (unsigned i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
4040 binops_by_token[binops[i].token_type] = binops[i];
4041
4042 cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4043 parser->lexer = lexer;
4044 parser->context = cp_parser_context_new (NULL);
4045
4046 /* For now, we always accept GNU extensions. */
4047 parser->allow_gnu_extensions_p = 1;
4048
4049 /* The `>' token is a greater-than operator, not the end of a
4050 template-id. */
4051 parser->greater_than_is_operator_p = true;
4052
4053 parser->default_arg_ok_p = true;
4054
4055 /* We are not parsing a constant-expression. */
4056 parser->integral_constant_expression_p = false;
4057 parser->allow_non_integral_constant_expression_p = false;
4058 parser->non_integral_constant_expression_p = false;
4059
4060 /* Local variable names are not forbidden. */
4061 parser->local_variables_forbidden_p = 0;
4062
4063 /* We are not processing an `extern "C"' declaration. */
4064 parser->in_unbraced_linkage_specification_p = false;
4065
4066 /* We are not processing a declarator. */
4067 parser->in_declarator_p = false;
4068
4069 /* We are not processing a template-argument-list. */
4070 parser->in_template_argument_list_p = false;
4071
4072 /* We are not in an iteration statement. */
4073 parser->in_statement = 0;
4074
4075 /* We are not in a switch statement. */
4076 parser->in_switch_statement_p = false;
4077
4078 /* We are not parsing a type-id inside an expression. */
4079 parser->in_type_id_in_expr_p = false;
4080
4081 /* String literals should be translated to the execution character set. */
4082 parser->translate_strings_p = true;
4083
4084 /* We are not parsing a function body. */
4085 parser->in_function_body = false;
4086
4087 /* We can correct until told otherwise. */
4088 parser->colon_corrects_to_scope_p = true;
4089
4090 /* The unparsed function queue is empty. */
4091 push_unparsed_function_queues (parser);
4092
4093 /* There are no classes being defined. */
4094 parser->num_classes_being_defined = 0;
4095
4096 /* No template parameters apply. */
4097 parser->num_template_parameter_lists = 0;
4098
4099 /* Special parsing data structures. */
4100 parser->omp_declare_simd = NULL;
4101 parser->oacc_routine = NULL;
4102
4103 /* Not declaring an implicit function template. */
4104 parser->auto_is_implicit_function_template_parm_p = false;
4105 parser->fully_implicit_function_template_p = false;
4106 parser->implicit_template_parms = 0;
4107 parser->implicit_template_scope = 0;
4108
4109 /* Allow constrained-type-specifiers. */
4110 parser->prevent_constrained_type_specifiers = 0;
4111
4112 /* We haven't yet seen an 'extern "C"'. */
4113 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4114
4115 return parser;
4116 }
4117
4118 /* Create a cp_lexer structure which will emit the tokens in CACHE
4119 and push it onto the parser's lexer stack. This is used for delayed
4120 parsing of in-class method bodies and default arguments, and should
4121 not be confused with tentative parsing. */
4122 static void
4123 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4124 {
4125 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4126 lexer->next = parser->lexer;
4127 parser->lexer = lexer;
4128
4129 /* Move the current source position to that of the first token in the
4130 new lexer. */
4131 cp_lexer_set_source_position_from_token (lexer->next_token);
4132 }
4133
4134 /* Pop the top lexer off the parser stack. This is never used for the
4135 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4136 static void
4137 cp_parser_pop_lexer (cp_parser *parser)
4138 {
4139 cp_lexer *lexer = parser->lexer;
4140 parser->lexer = lexer->next;
4141 cp_lexer_destroy (lexer);
4142
4143 /* Put the current source position back where it was before this
4144 lexer was pushed. */
4145 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4146 }
4147
4148 /* Lexical conventions [gram.lex] */
4149
4150 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4151 identifier. */
4152
4153 static cp_expr
4154 cp_parser_identifier (cp_parser* parser)
4155 {
4156 cp_token *token;
4157
4158 /* Look for the identifier. */
4159 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4160 /* Return the value. */
4161 if (token)
4162 return cp_expr (token->u.value, token->location);
4163 else
4164 return error_mark_node;
4165 }
4166
4167 /* Parse a sequence of adjacent string constants. Returns a
4168 TREE_STRING representing the combined, nul-terminated string
4169 constant. If TRANSLATE is true, translate the string to the
4170 execution character set. If WIDE_OK is true, a wide string is
4171 invalid here.
4172
4173 C++98 [lex.string] says that if a narrow string literal token is
4174 adjacent to a wide string literal token, the behavior is undefined.
4175 However, C99 6.4.5p4 says that this results in a wide string literal.
4176 We follow C99 here, for consistency with the C front end.
4177
4178 This code is largely lifted from lex_string() in c-lex.c.
4179
4180 FUTURE: ObjC++ will need to handle @-strings here. */
4181 static cp_expr
4182 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4183 bool lookup_udlit = true)
4184 {
4185 tree value;
4186 size_t count;
4187 struct obstack str_ob;
4188 struct obstack loc_ob;
4189 cpp_string str, istr, *strs;
4190 cp_token *tok;
4191 enum cpp_ttype type, curr_type;
4192 int have_suffix_p = 0;
4193 tree string_tree;
4194 tree suffix_id = NULL_TREE;
4195 bool curr_tok_is_userdef_p = false;
4196
4197 tok = cp_lexer_peek_token (parser->lexer);
4198 if (!cp_parser_is_string_literal (tok))
4199 {
4200 cp_parser_error (parser, "expected string-literal");
4201 return error_mark_node;
4202 }
4203
4204 location_t loc = tok->location;
4205
4206 if (cpp_userdef_string_p (tok->type))
4207 {
4208 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4209 curr_type = cpp_userdef_string_remove_type (tok->type);
4210 curr_tok_is_userdef_p = true;
4211 }
4212 else
4213 {
4214 string_tree = tok->u.value;
4215 curr_type = tok->type;
4216 }
4217 type = curr_type;
4218
4219 /* Try to avoid the overhead of creating and destroying an obstack
4220 for the common case of just one string. */
4221 if (!cp_parser_is_string_literal
4222 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4223 {
4224 cp_lexer_consume_token (parser->lexer);
4225
4226 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4227 str.len = TREE_STRING_LENGTH (string_tree);
4228 count = 1;
4229
4230 if (curr_tok_is_userdef_p)
4231 {
4232 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4233 have_suffix_p = 1;
4234 curr_type = cpp_userdef_string_remove_type (tok->type);
4235 }
4236 else
4237 curr_type = tok->type;
4238
4239 strs = &str;
4240 }
4241 else
4242 {
4243 location_t last_tok_loc = tok->location;
4244 gcc_obstack_init (&str_ob);
4245 gcc_obstack_init (&loc_ob);
4246 count = 0;
4247
4248 do
4249 {
4250 cp_lexer_consume_token (parser->lexer);
4251 count++;
4252 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4253 str.len = TREE_STRING_LENGTH (string_tree);
4254
4255 if (curr_tok_is_userdef_p)
4256 {
4257 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4258 if (have_suffix_p == 0)
4259 {
4260 suffix_id = curr_suffix_id;
4261 have_suffix_p = 1;
4262 }
4263 else if (have_suffix_p == 1
4264 && curr_suffix_id != suffix_id)
4265 {
4266 error ("inconsistent user-defined literal suffixes"
4267 " %qD and %qD in string literal",
4268 suffix_id, curr_suffix_id);
4269 have_suffix_p = -1;
4270 }
4271 curr_type = cpp_userdef_string_remove_type (tok->type);
4272 }
4273 else
4274 curr_type = tok->type;
4275
4276 if (type != curr_type)
4277 {
4278 if (type == CPP_STRING)
4279 type = curr_type;
4280 else if (curr_type != CPP_STRING)
4281 {
4282 rich_location rich_loc (line_table, tok->location);
4283 rich_loc.add_range (last_tok_loc);
4284 error_at (&rich_loc,
4285 "unsupported non-standard concatenation "
4286 "of string literals");
4287 }
4288 }
4289
4290 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4291 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4292
4293 last_tok_loc = tok->location;
4294
4295 tok = cp_lexer_peek_token (parser->lexer);
4296 if (cpp_userdef_string_p (tok->type))
4297 {
4298 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4299 curr_type = cpp_userdef_string_remove_type (tok->type);
4300 curr_tok_is_userdef_p = true;
4301 }
4302 else
4303 {
4304 string_tree = tok->u.value;
4305 curr_type = tok->type;
4306 curr_tok_is_userdef_p = false;
4307 }
4308 }
4309 while (cp_parser_is_string_literal (tok));
4310
4311 /* A string literal built by concatenation has its caret=start at
4312 the start of the initial string, and its finish at the finish of
4313 the final string literal. */
4314 loc = make_location (loc, loc, get_finish (last_tok_loc));
4315
4316 strs = (cpp_string *) obstack_finish (&str_ob);
4317 }
4318
4319 if (type != CPP_STRING && !wide_ok)
4320 {
4321 cp_parser_error (parser, "a wide string is invalid in this context");
4322 type = CPP_STRING;
4323 }
4324
4325 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4326 (parse_in, strs, count, &istr, type))
4327 {
4328 value = build_string (istr.len, (const char *)istr.text);
4329 free (CONST_CAST (unsigned char *, istr.text));
4330 if (count > 1)
4331 {
4332 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4333 gcc_assert (g_string_concat_db);
4334 g_string_concat_db->record_string_concatenation (count, locs);
4335 }
4336
4337 switch (type)
4338 {
4339 default:
4340 case CPP_STRING:
4341 TREE_TYPE (value) = char_array_type_node;
4342 break;
4343 case CPP_UTF8STRING:
4344 if (flag_char8_t)
4345 TREE_TYPE (value) = char8_array_type_node;
4346 else
4347 TREE_TYPE (value) = char_array_type_node;
4348 break;
4349 case CPP_STRING16:
4350 TREE_TYPE (value) = char16_array_type_node;
4351 break;
4352 case CPP_STRING32:
4353 TREE_TYPE (value) = char32_array_type_node;
4354 break;
4355 case CPP_WSTRING:
4356 TREE_TYPE (value) = wchar_array_type_node;
4357 break;
4358 }
4359
4360 value = fix_string_type (value);
4361
4362 if (have_suffix_p)
4363 {
4364 tree literal = build_userdef_literal (suffix_id, value,
4365 OT_NONE, NULL_TREE);
4366 if (lookup_udlit)
4367 value = cp_parser_userdef_string_literal (literal);
4368 else
4369 value = literal;
4370 }
4371 }
4372 else
4373 /* cpp_interpret_string has issued an error. */
4374 value = error_mark_node;
4375
4376 if (count > 1)
4377 {
4378 obstack_free (&str_ob, 0);
4379 obstack_free (&loc_ob, 0);
4380 }
4381
4382 return cp_expr (value, loc);
4383 }
4384
4385 /* Look up a literal operator with the name and the exact arguments. */
4386
4387 static tree
4388 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4389 {
4390 tree decl = lookup_name (name);
4391 if (!decl || !is_overloaded_fn (decl))
4392 return error_mark_node;
4393
4394 for (lkp_iterator iter (decl); iter; ++iter)
4395 {
4396 tree fn = *iter;
4397
4398 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4399 {
4400 unsigned int ix;
4401 bool found = true;
4402
4403 for (ix = 0;
4404 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4405 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4406 {
4407 tree tparm = TREE_VALUE (parmtypes);
4408 tree targ = TREE_TYPE ((*args)[ix]);
4409 bool ptr = TYPE_PTR_P (tparm);
4410 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4411 if ((ptr || arr || !same_type_p (tparm, targ))
4412 && (!ptr || !arr
4413 || !same_type_p (TREE_TYPE (tparm),
4414 TREE_TYPE (targ))))
4415 found = false;
4416 }
4417
4418 if (found
4419 && ix == vec_safe_length (args)
4420 /* May be this should be sufficient_parms_p instead,
4421 depending on how exactly should user-defined literals
4422 work in presence of default arguments on the literal
4423 operator parameters. */
4424 && parmtypes == void_list_node)
4425 return decl;
4426 }
4427 }
4428
4429 return error_mark_node;
4430 }
4431
4432 /* Parse a user-defined char constant. Returns a call to a user-defined
4433 literal operator taking the character as an argument. */
4434
4435 static cp_expr
4436 cp_parser_userdef_char_literal (cp_parser *parser)
4437 {
4438 cp_token *token = cp_lexer_consume_token (parser->lexer);
4439 tree literal = token->u.value;
4440 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4441 tree value = USERDEF_LITERAL_VALUE (literal);
4442 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4443 tree decl, result;
4444
4445 /* Build up a call to the user-defined operator */
4446 /* Lookup the name we got back from the id-expression. */
4447 releasing_vec args;
4448 vec_safe_push (args, value);
4449 decl = lookup_literal_operator (name, args);
4450 if (!decl || decl == error_mark_node)
4451 {
4452 error ("unable to find character literal operator %qD with %qT argument",
4453 name, TREE_TYPE (value));
4454 return error_mark_node;
4455 }
4456 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4457 return result;
4458 }
4459
4460 /* A subroutine of cp_parser_userdef_numeric_literal to
4461 create a char... template parameter pack from a string node. */
4462
4463 static tree
4464 make_char_string_pack (tree value)
4465 {
4466 tree charvec;
4467 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4468 const char *str = TREE_STRING_POINTER (value);
4469 int i, len = TREE_STRING_LENGTH (value) - 1;
4470 tree argvec = make_tree_vec (1);
4471
4472 /* Fill in CHARVEC with all of the parameters. */
4473 charvec = make_tree_vec (len);
4474 for (i = 0; i < len; ++i)
4475 {
4476 unsigned char s[3] = { '\'', str[i], '\'' };
4477 cpp_string in = { 3, s };
4478 cpp_string out = { 0, 0 };
4479 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4480 return NULL_TREE;
4481 gcc_assert (out.len == 2);
4482 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4483 out.text[0]);
4484 }
4485
4486 /* Build the argument packs. */
4487 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4488
4489 TREE_VEC_ELT (argvec, 0) = argpack;
4490
4491 return argvec;
4492 }
4493
4494 /* A subroutine of cp_parser_userdef_numeric_literal to
4495 create a char... template parameter pack from a string node. */
4496
4497 static tree
4498 make_string_pack (tree value)
4499 {
4500 tree charvec;
4501 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4502 const unsigned char *str
4503 = (const unsigned char *) TREE_STRING_POINTER (value);
4504 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4505 int len = TREE_STRING_LENGTH (value) / sz - 1;
4506 tree argvec = make_tree_vec (2);
4507
4508 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4509 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4510
4511 /* First template parm is character type. */
4512 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4513
4514 /* Fill in CHARVEC with all of the parameters. */
4515 charvec = make_tree_vec (len);
4516 for (int i = 0; i < len; ++i)
4517 TREE_VEC_ELT (charvec, i)
4518 = double_int_to_tree (str_char_type_node,
4519 double_int::from_buffer (str + i * sz, sz));
4520
4521 /* Build the argument packs. */
4522 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4523
4524 TREE_VEC_ELT (argvec, 1) = argpack;
4525
4526 return argvec;
4527 }
4528
4529 /* Parse a user-defined numeric constant. returns a call to a user-defined
4530 literal operator. */
4531
4532 static cp_expr
4533 cp_parser_userdef_numeric_literal (cp_parser *parser)
4534 {
4535 cp_token *token = cp_lexer_consume_token (parser->lexer);
4536 tree literal = token->u.value;
4537 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4538 tree value = USERDEF_LITERAL_VALUE (literal);
4539 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4540 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4541 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4542 tree decl, result;
4543
4544 /* Look for a literal operator taking the exact type of numeric argument
4545 as the literal value. */
4546 releasing_vec args;
4547 vec_safe_push (args, value);
4548 decl = lookup_literal_operator (name, args);
4549 if (decl && decl != error_mark_node)
4550 {
4551 result = finish_call_expr (decl, &args, false, true,
4552 tf_warning_or_error);
4553
4554 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4555 {
4556 warning_at (token->location, OPT_Woverflow,
4557 "integer literal exceeds range of %qT type",
4558 long_long_unsigned_type_node);
4559 }
4560 else
4561 {
4562 if (overflow > 0)
4563 warning_at (token->location, OPT_Woverflow,
4564 "floating literal exceeds range of %qT type",
4565 long_double_type_node);
4566 else if (overflow < 0)
4567 warning_at (token->location, OPT_Woverflow,
4568 "floating literal truncated to zero");
4569 }
4570
4571 return result;
4572 }
4573
4574 /* If the numeric argument didn't work, look for a raw literal
4575 operator taking a const char* argument consisting of the number
4576 in string format. */
4577 args->truncate (0);
4578 vec_safe_push (args, num_string);
4579 decl = lookup_literal_operator (name, args);
4580 if (decl && decl != error_mark_node)
4581 {
4582 result = finish_call_expr (decl, &args, false, true,
4583 tf_warning_or_error);
4584 return result;
4585 }
4586
4587 /* If the raw literal didn't work, look for a non-type template
4588 function with parameter pack char.... Call the function with
4589 template parameter characters representing the number. */
4590 args->truncate (0);
4591 decl = lookup_literal_operator (name, args);
4592 if (decl && decl != error_mark_node)
4593 {
4594 tree tmpl_args = make_char_string_pack (num_string);
4595 if (tmpl_args == NULL_TREE)
4596 {
4597 error ("failed to translate literal to execution character set %qT",
4598 num_string);
4599 return error_mark_node;
4600 }
4601 decl = lookup_template_function (decl, tmpl_args);
4602 result = finish_call_expr (decl, &args, false, true,
4603 tf_warning_or_error);
4604 return result;
4605 }
4606
4607 /* In C++14 the standard library defines complex number suffixes that
4608 conflict with GNU extensions. Prefer them if <complex> is #included. */
4609 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4610 bool i14 = (cxx_dialect > cxx11
4611 && (id_equal (suffix_id, "i")
4612 || id_equal (suffix_id, "if")
4613 || id_equal (suffix_id, "il")));
4614 diagnostic_t kind = DK_ERROR;
4615 int opt = 0;
4616
4617 if (i14 && ext)
4618 {
4619 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
4620 LOOK_want::NORMAL, false);
4621 if (cxlit == error_mark_node)
4622 {
4623 /* No <complex>, so pedwarn and use GNU semantics. */
4624 kind = DK_PEDWARN;
4625 opt = OPT_Wpedantic;
4626 }
4627 }
4628
4629 bool complained
4630 = emit_diagnostic (kind, input_location, opt,
4631 "unable to find numeric literal operator %qD", name);
4632
4633 if (!complained)
4634 /* Don't inform either. */;
4635 else if (i14)
4636 {
4637 inform (token->location, "add %<using namespace std::complex_literals%> "
4638 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4639 "suffixes");
4640 if (ext)
4641 inform (token->location, "or use %<j%> instead of %<i%> for the "
4642 "GNU built-in suffix");
4643 }
4644 else if (!ext)
4645 inform (token->location, "use %<-fext-numeric-literals%> "
4646 "to enable more built-in suffixes");
4647
4648 if (kind == DK_ERROR)
4649 value = error_mark_node;
4650 else
4651 {
4652 /* Use the built-in semantics. */
4653 tree type;
4654 if (id_equal (suffix_id, "i"))
4655 {
4656 if (TREE_CODE (value) == INTEGER_CST)
4657 type = integer_type_node;
4658 else
4659 type = double_type_node;
4660 }
4661 else if (id_equal (suffix_id, "if"))
4662 type = float_type_node;
4663 else /* if (id_equal (suffix_id, "il")) */
4664 type = long_double_type_node;
4665
4666 value = build_complex (build_complex_type (type),
4667 fold_convert (type, integer_zero_node),
4668 fold_convert (type, value));
4669 }
4670
4671 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4672 /* Avoid repeated diagnostics. */
4673 token->u.value = value;
4674 return value;
4675 }
4676
4677 /* Parse a user-defined string constant. Returns a call to a user-defined
4678 literal operator taking a character pointer and the length of the string
4679 as arguments. */
4680
4681 static tree
4682 cp_parser_userdef_string_literal (tree literal)
4683 {
4684 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4685 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4686 tree value = USERDEF_LITERAL_VALUE (literal);
4687 int len = TREE_STRING_LENGTH (value)
4688 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4689 tree decl;
4690
4691 /* Build up a call to the user-defined operator. */
4692 /* Lookup the name we got back from the id-expression. */
4693 releasing_vec args;
4694 vec_safe_push (args, value);
4695 vec_safe_push (args, build_int_cst (size_type_node, len));
4696 decl = lookup_literal_operator (name, args);
4697
4698 if (decl && decl != error_mark_node)
4699 return finish_call_expr (decl, &args, false, true,
4700 tf_warning_or_error);
4701
4702 /* Look for a suitable template function, either (C++20) with a single
4703 parameter of class type, or (N3599) with typename parameter CharT and
4704 parameter pack CharT... */
4705 args->truncate (0);
4706 decl = lookup_literal_operator (name, args);
4707 if (decl && decl != error_mark_node)
4708 {
4709 /* Use resolve_nondeduced_context to try to choose one form of template
4710 or the other. */
4711 tree tmpl_args = make_tree_vec (1);
4712 TREE_VEC_ELT (tmpl_args, 0) = value;
4713 decl = lookup_template_function (decl, tmpl_args);
4714 tree res = resolve_nondeduced_context (decl, tf_none);
4715 if (DECL_P (res))
4716 decl = res;
4717 else
4718 {
4719 TREE_OPERAND (decl, 1) = make_string_pack (value);
4720 res = resolve_nondeduced_context (decl, tf_none);
4721 if (DECL_P (res))
4722 decl = res;
4723 }
4724 if (!DECL_P (decl) && cxx_dialect > cxx17)
4725 TREE_OPERAND (decl, 1) = tmpl_args;
4726 return finish_call_expr (decl, &args, false, true,
4727 tf_warning_or_error);
4728 }
4729
4730 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4731 name, TREE_TYPE (value), size_type_node);
4732 return error_mark_node;
4733 }
4734
4735
4736 /* Basic concepts [gram.basic] */
4737
4738 /* Parse a translation-unit.
4739
4740 translation-unit:
4741 declaration-seq [opt] */
4742
4743 static void
4744 cp_parser_translation_unit (cp_parser* parser)
4745 {
4746 gcc_checking_assert (!cp_error_declarator);
4747
4748 /* Create the declarator obstack. */
4749 gcc_obstack_init (&declarator_obstack);
4750 /* Create the error declarator. */
4751 cp_error_declarator = make_declarator (cdk_error);
4752 /* Create the empty parameter list. */
4753 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4754 UNKNOWN_LOCATION);
4755 /* Remember where the base of the declarator obstack lies. */
4756 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4757
4758 push_deferring_access_checks (flag_access_control
4759 ? dk_no_deferred : dk_no_check);
4760
4761 bool implicit_extern_c = false;
4762
4763 /* Parse until EOF. */
4764 for (;;)
4765 {
4766 cp_token *token = cp_lexer_peek_token (parser->lexer);
4767
4768 /* If we're entering or exiting a region that's implicitly
4769 extern "C", modify the lang context appropriately. This is
4770 so horrible. Please die. */
4771 if (implicit_extern_c
4772 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4773 {
4774 implicit_extern_c = !implicit_extern_c;
4775 if (implicit_extern_c)
4776 push_lang_context (lang_name_c);
4777 else
4778 pop_lang_context ();
4779 }
4780
4781 if (token->type == CPP_EOF)
4782 break;
4783
4784 if (token->type == CPP_CLOSE_BRACE)
4785 {
4786 cp_parser_error (parser, "expected declaration");
4787 cp_lexer_consume_token (parser->lexer);
4788 /* If the next token is now a `;', consume it. */
4789 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4790 cp_lexer_consume_token (parser->lexer);
4791 }
4792 else
4793 cp_parser_toplevel_declaration (parser);
4794 }
4795
4796 /* Get rid of the token array; we don't need it any more. */
4797 cp_lexer_destroy (parser->lexer);
4798 parser->lexer = NULL;
4799
4800 /* The EOF should have reset this. */
4801 gcc_checking_assert (!implicit_extern_c);
4802
4803 /* Make sure the declarator obstack was fully cleaned up. */
4804 gcc_assert (obstack_next_free (&declarator_obstack)
4805 == declarator_obstack_base);
4806 }
4807
4808 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4809 decltype context. */
4810
4811 static inline tsubst_flags_t
4812 complain_flags (bool decltype_p)
4813 {
4814 tsubst_flags_t complain = tf_warning_or_error;
4815 if (decltype_p)
4816 complain |= tf_decltype;
4817 return complain;
4818 }
4819
4820 /* We're about to parse a collection of statements. If we're currently
4821 parsing tentatively, set up a firewall so that any nested
4822 cp_parser_commit_to_tentative_parse won't affect the current context. */
4823
4824 static cp_token_position
4825 cp_parser_start_tentative_firewall (cp_parser *parser)
4826 {
4827 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4828 return 0;
4829
4830 cp_parser_parse_tentatively (parser);
4831 cp_parser_commit_to_topmost_tentative_parse (parser);
4832 return cp_lexer_token_position (parser->lexer, false);
4833 }
4834
4835 /* We've finished parsing the collection of statements. Wrap up the
4836 firewall and replace the relevant tokens with the parsed form. */
4837
4838 static void
4839 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4840 tree expr)
4841 {
4842 if (!start)
4843 return;
4844
4845 /* Finish the firewall level. */
4846 cp_parser_parse_definitely (parser);
4847 /* And remember the result of the parse for when we try again. */
4848 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4849 token->type = CPP_PREPARSED_EXPR;
4850 token->u.value = expr;
4851 token->keyword = RID_MAX;
4852 cp_lexer_purge_tokens_after (parser->lexer, start);
4853 }
4854
4855 /* Like the above functions, but let the user modify the tokens. Used by
4856 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4857 later parses, so it makes sense to localize the effects of
4858 cp_parser_commit_to_tentative_parse. */
4859
4860 struct tentative_firewall
4861 {
4862 cp_parser *parser;
4863 bool set;
4864
4865 tentative_firewall (cp_parser *p): parser(p)
4866 {
4867 /* If we're currently parsing tentatively, start a committed level as a
4868 firewall and then an inner tentative parse. */
4869 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4870 {
4871 cp_parser_parse_tentatively (parser);
4872 cp_parser_commit_to_topmost_tentative_parse (parser);
4873 cp_parser_parse_tentatively (parser);
4874 }
4875 }
4876
4877 ~tentative_firewall()
4878 {
4879 if (set)
4880 {
4881 /* Finish the inner tentative parse and the firewall, propagating any
4882 uncommitted error state to the outer tentative parse. */
4883 bool err = cp_parser_error_occurred (parser);
4884 cp_parser_parse_definitely (parser);
4885 cp_parser_parse_definitely (parser);
4886 if (err)
4887 cp_parser_simulate_error (parser);
4888 }
4889 }
4890 };
4891
4892 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4893 This class is for tracking such a matching pair of symbols.
4894 In particular, it tracks the location of the first token,
4895 so that if the second token is missing, we can highlight the
4896 location of the first token when notifying the user about the
4897 problem. */
4898
4899 template <typename traits_t>
4900 class token_pair
4901 {
4902 public:
4903 /* token_pair's ctor. */
4904 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4905
4906 /* If the next token is the opening symbol for this pair, consume it and
4907 return true.
4908 Otherwise, issue an error and return false.
4909 In either case, record the location of the opening token. */
4910
4911 bool require_open (cp_parser *parser)
4912 {
4913 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4914 return cp_parser_require (parser, traits_t::open_token_type,
4915 traits_t::required_token_open);
4916 }
4917
4918 /* Consume the next token from PARSER, recording its location as
4919 that of the opening token within the pair. */
4920
4921 cp_token * consume_open (cp_parser *parser)
4922 {
4923 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4924 gcc_assert (tok->type == traits_t::open_token_type);
4925 m_open_loc = tok->location;
4926 return tok;
4927 }
4928
4929 /* If the next token is the closing symbol for this pair, consume it
4930 and return it.
4931 Otherwise, issue an error, highlighting the location of the
4932 corresponding opening token, and return NULL. */
4933
4934 cp_token *require_close (cp_parser *parser) const
4935 {
4936 return cp_parser_require (parser, traits_t::close_token_type,
4937 traits_t::required_token_close,
4938 m_open_loc);
4939 }
4940
4941 location_t open_location () const { return m_open_loc; }
4942
4943 private:
4944 location_t m_open_loc;
4945 };
4946
4947 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4948
4949 struct matching_paren_traits
4950 {
4951 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4952 static const enum required_token required_token_open = RT_OPEN_PAREN;
4953 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4954 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4955 };
4956
4957 /* "matching_parens" is a token_pair<T> class for tracking matching
4958 pairs of parentheses. */
4959
4960 typedef token_pair<matching_paren_traits> matching_parens;
4961
4962 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4963
4964 struct matching_brace_traits
4965 {
4966 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4967 static const enum required_token required_token_open = RT_OPEN_BRACE;
4968 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4969 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4970 };
4971
4972 /* "matching_braces" is a token_pair<T> class for tracking matching
4973 pairs of braces. */
4974
4975 typedef token_pair<matching_brace_traits> matching_braces;
4976
4977
4978 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4979 enclosing parentheses. */
4980
4981 static cp_expr
4982 cp_parser_statement_expr (cp_parser *parser)
4983 {
4984 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4985
4986 /* Consume the '('. */
4987 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4988 matching_parens parens;
4989 parens.consume_open (parser);
4990 /* Start the statement-expression. */
4991 tree expr = begin_stmt_expr ();
4992 /* Parse the compound-statement. */
4993 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4994 /* Finish up. */
4995 expr = finish_stmt_expr (expr, false);
4996 /* Consume the ')'. */
4997 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4998 if (!parens.require_close (parser))
4999 cp_parser_skip_to_end_of_statement (parser);
5000
5001 cp_parser_end_tentative_firewall (parser, start, expr);
5002 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5003 return cp_expr (expr, combined_loc);
5004 }
5005
5006 /* Expressions [gram.expr] */
5007
5008 /* Parse a fold-operator.
5009
5010 fold-operator:
5011 - * / % ^ & | = < > << >>
5012 = -= *= /= %= ^= &= |= <<= >>=
5013 == != <= >= && || , .* ->*
5014
5015 This returns the tree code corresponding to the matched operator
5016 as an int. When the current token matches a compound assignment
5017 operator, the resulting tree code is the negative value of the
5018 non-assignment operator. */
5019
5020 static int
5021 cp_parser_fold_operator (cp_token *token)
5022 {
5023 switch (token->type)
5024 {
5025 case CPP_PLUS: return PLUS_EXPR;
5026 case CPP_MINUS: return MINUS_EXPR;
5027 case CPP_MULT: return MULT_EXPR;
5028 case CPP_DIV: return TRUNC_DIV_EXPR;
5029 case CPP_MOD: return TRUNC_MOD_EXPR;
5030 case CPP_XOR: return BIT_XOR_EXPR;
5031 case CPP_AND: return BIT_AND_EXPR;
5032 case CPP_OR: return BIT_IOR_EXPR;
5033 case CPP_LSHIFT: return LSHIFT_EXPR;
5034 case CPP_RSHIFT: return RSHIFT_EXPR;
5035
5036 case CPP_EQ: return -NOP_EXPR;
5037 case CPP_PLUS_EQ: return -PLUS_EXPR;
5038 case CPP_MINUS_EQ: return -MINUS_EXPR;
5039 case CPP_MULT_EQ: return -MULT_EXPR;
5040 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5041 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5042 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5043 case CPP_AND_EQ: return -BIT_AND_EXPR;
5044 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5045 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5046 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5047
5048 case CPP_EQ_EQ: return EQ_EXPR;
5049 case CPP_NOT_EQ: return NE_EXPR;
5050 case CPP_LESS: return LT_EXPR;
5051 case CPP_GREATER: return GT_EXPR;
5052 case CPP_LESS_EQ: return LE_EXPR;
5053 case CPP_GREATER_EQ: return GE_EXPR;
5054
5055 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5056 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5057
5058 case CPP_COMMA: return COMPOUND_EXPR;
5059
5060 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5061 case CPP_DEREF_STAR: return MEMBER_REF;
5062
5063 default: return ERROR_MARK;
5064 }
5065 }
5066
5067 /* Returns true if CODE indicates a binary expression, which is not allowed in
5068 the LHS of a fold-expression. More codes will need to be added to use this
5069 function in other contexts. */
5070
5071 static bool
5072 is_binary_op (tree_code code)
5073 {
5074 switch (code)
5075 {
5076 case PLUS_EXPR:
5077 case POINTER_PLUS_EXPR:
5078 case MINUS_EXPR:
5079 case MULT_EXPR:
5080 case TRUNC_DIV_EXPR:
5081 case TRUNC_MOD_EXPR:
5082 case BIT_XOR_EXPR:
5083 case BIT_AND_EXPR:
5084 case BIT_IOR_EXPR:
5085 case LSHIFT_EXPR:
5086 case RSHIFT_EXPR:
5087
5088 case MODOP_EXPR:
5089
5090 case EQ_EXPR:
5091 case NE_EXPR:
5092 case LE_EXPR:
5093 case GE_EXPR:
5094 case LT_EXPR:
5095 case GT_EXPR:
5096
5097 case TRUTH_ANDIF_EXPR:
5098 case TRUTH_ORIF_EXPR:
5099
5100 case COMPOUND_EXPR:
5101
5102 case DOTSTAR_EXPR:
5103 case MEMBER_REF:
5104 return true;
5105
5106 default:
5107 return false;
5108 }
5109 }
5110
5111 /* If the next token is a suitable fold operator, consume it and return as
5112 the function above. */
5113
5114 static int
5115 cp_parser_fold_operator (cp_parser *parser)
5116 {
5117 cp_token* token = cp_lexer_peek_token (parser->lexer);
5118 int code = cp_parser_fold_operator (token);
5119 if (code != ERROR_MARK)
5120 cp_lexer_consume_token (parser->lexer);
5121 return code;
5122 }
5123
5124 /* Parse a fold-expression.
5125
5126 fold-expression:
5127 ( ... folding-operator cast-expression)
5128 ( cast-expression folding-operator ... )
5129 ( cast-expression folding operator ... folding-operator cast-expression)
5130
5131 Note that the '(' and ')' are matched in primary expression. */
5132
5133 static cp_expr
5134 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5135 {
5136 cp_id_kind pidk;
5137
5138 // Left fold.
5139 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5140 {
5141 cp_lexer_consume_token (parser->lexer);
5142 int op = cp_parser_fold_operator (parser);
5143 if (op == ERROR_MARK)
5144 {
5145 cp_parser_error (parser, "expected binary operator");
5146 return error_mark_node;
5147 }
5148
5149 tree expr = cp_parser_cast_expression (parser, false, false,
5150 false, &pidk);
5151 if (expr == error_mark_node)
5152 return error_mark_node;
5153 return finish_left_unary_fold_expr (expr, op);
5154 }
5155
5156 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5157 int op = cp_parser_fold_operator (parser);
5158 if (op == ERROR_MARK)
5159 {
5160 cp_parser_error (parser, "expected binary operator");
5161 return error_mark_node;
5162 }
5163
5164 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5165 {
5166 cp_parser_error (parser, "expected ...");
5167 return error_mark_node;
5168 }
5169 cp_lexer_consume_token (parser->lexer);
5170
5171 /* The operands of a fold-expression are cast-expressions, so binary or
5172 conditional expressions are not allowed. We check this here to avoid
5173 tentative parsing. */
5174 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5175 /* OK, the expression was parenthesized. */;
5176 else if (is_binary_op (TREE_CODE (expr1)))
5177 error_at (location_of (expr1),
5178 "binary expression in operand of fold-expression");
5179 else if (TREE_CODE (expr1) == COND_EXPR
5180 || (REFERENCE_REF_P (expr1)
5181 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5182 error_at (location_of (expr1),
5183 "conditional expression in operand of fold-expression");
5184
5185 // Right fold.
5186 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5187 return finish_right_unary_fold_expr (expr1, op);
5188
5189 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5190 {
5191 cp_parser_error (parser, "mismatched operator in fold-expression");
5192 return error_mark_node;
5193 }
5194 cp_lexer_consume_token (parser->lexer);
5195
5196 // Binary left or right fold.
5197 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5198 if (expr2 == error_mark_node)
5199 return error_mark_node;
5200 return finish_binary_fold_expr (expr1, expr2, op);
5201 }
5202
5203 /* Parse a primary-expression.
5204
5205 primary-expression:
5206 literal
5207 this
5208 ( expression )
5209 id-expression
5210 lambda-expression (C++11)
5211
5212 GNU Extensions:
5213
5214 primary-expression:
5215 ( compound-statement )
5216 __builtin_va_arg ( assignment-expression , type-id )
5217 __builtin_offsetof ( type-id , offsetof-expression )
5218
5219 C++ Extensions:
5220 __has_nothrow_assign ( type-id )
5221 __has_nothrow_constructor ( type-id )
5222 __has_nothrow_copy ( type-id )
5223 __has_trivial_assign ( type-id )
5224 __has_trivial_constructor ( type-id )
5225 __has_trivial_copy ( type-id )
5226 __has_trivial_destructor ( type-id )
5227 __has_virtual_destructor ( type-id )
5228 __is_abstract ( type-id )
5229 __is_base_of ( type-id , type-id )
5230 __is_class ( type-id )
5231 __is_empty ( type-id )
5232 __is_enum ( type-id )
5233 __is_final ( type-id )
5234 __is_literal_type ( type-id )
5235 __is_pod ( type-id )
5236 __is_polymorphic ( type-id )
5237 __is_std_layout ( type-id )
5238 __is_trivial ( type-id )
5239 __is_union ( type-id )
5240
5241 Objective-C++ Extension:
5242
5243 primary-expression:
5244 objc-expression
5245
5246 literal:
5247 __null
5248
5249 ADDRESS_P is true iff this expression was immediately preceded by
5250 "&" and therefore might denote a pointer-to-member. CAST_P is true
5251 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5252 true iff this expression is a template argument.
5253
5254 Returns a representation of the expression. Upon return, *IDK
5255 indicates what kind of id-expression (if any) was present. */
5256
5257 static cp_expr
5258 cp_parser_primary_expression (cp_parser *parser,
5259 bool address_p,
5260 bool cast_p,
5261 bool template_arg_p,
5262 bool decltype_p,
5263 cp_id_kind *idk)
5264 {
5265 cp_token *token = NULL;
5266
5267 /* Assume the primary expression is not an id-expression. */
5268 *idk = CP_ID_KIND_NONE;
5269
5270 /* Peek at the next token. */
5271 token = cp_lexer_peek_token (parser->lexer);
5272 switch ((int) token->type)
5273 {
5274 /* literal:
5275 integer-literal
5276 character-literal
5277 floating-literal
5278 string-literal
5279 boolean-literal
5280 pointer-literal
5281 user-defined-literal */
5282 case CPP_CHAR:
5283 case CPP_CHAR16:
5284 case CPP_CHAR32:
5285 case CPP_WCHAR:
5286 case CPP_UTF8CHAR:
5287 case CPP_NUMBER:
5288 case CPP_PREPARSED_EXPR:
5289 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5290 return cp_parser_userdef_numeric_literal (parser);
5291 token = cp_lexer_consume_token (parser->lexer);
5292 if (TREE_CODE (token->u.value) == FIXED_CST)
5293 {
5294 error_at (token->location,
5295 "fixed-point types not supported in C++");
5296 return error_mark_node;
5297 }
5298 /* Floating-point literals are only allowed in an integral
5299 constant expression if they are cast to an integral or
5300 enumeration type. */
5301 if (TREE_CODE (token->u.value) == REAL_CST
5302 && parser->integral_constant_expression_p
5303 && pedantic)
5304 {
5305 /* CAST_P will be set even in invalid code like "int(2.7 +
5306 ...)". Therefore, we have to check that the next token
5307 is sure to end the cast. */
5308 if (cast_p)
5309 {
5310 cp_token *next_token;
5311
5312 next_token = cp_lexer_peek_token (parser->lexer);
5313 if (/* The comma at the end of an
5314 enumerator-definition. */
5315 next_token->type != CPP_COMMA
5316 /* The curly brace at the end of an enum-specifier. */
5317 && next_token->type != CPP_CLOSE_BRACE
5318 /* The end of a statement. */
5319 && next_token->type != CPP_SEMICOLON
5320 /* The end of the cast-expression. */
5321 && next_token->type != CPP_CLOSE_PAREN
5322 /* The end of an array bound. */
5323 && next_token->type != CPP_CLOSE_SQUARE
5324 /* The closing ">" in a template-argument-list. */
5325 && (next_token->type != CPP_GREATER
5326 || parser->greater_than_is_operator_p)
5327 /* C++0x only: A ">>" treated like two ">" tokens,
5328 in a template-argument-list. */
5329 && (next_token->type != CPP_RSHIFT
5330 || (cxx_dialect == cxx98)
5331 || parser->greater_than_is_operator_p))
5332 cast_p = false;
5333 }
5334
5335 /* If we are within a cast, then the constraint that the
5336 cast is to an integral or enumeration type will be
5337 checked at that point. If we are not within a cast, then
5338 this code is invalid. */
5339 if (!cast_p)
5340 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5341 }
5342 return (cp_expr (token->u.value, token->location)
5343 .maybe_add_location_wrapper ());
5344
5345 case CPP_CHAR_USERDEF:
5346 case CPP_CHAR16_USERDEF:
5347 case CPP_CHAR32_USERDEF:
5348 case CPP_WCHAR_USERDEF:
5349 case CPP_UTF8CHAR_USERDEF:
5350 return cp_parser_userdef_char_literal (parser);
5351
5352 case CPP_STRING:
5353 case CPP_STRING16:
5354 case CPP_STRING32:
5355 case CPP_WSTRING:
5356 case CPP_UTF8STRING:
5357 case CPP_STRING_USERDEF:
5358 case CPP_STRING16_USERDEF:
5359 case CPP_STRING32_USERDEF:
5360 case CPP_WSTRING_USERDEF:
5361 case CPP_UTF8STRING_USERDEF:
5362 /* ??? Should wide strings be allowed when parser->translate_strings_p
5363 is false (i.e. in attributes)? If not, we can kill the third
5364 argument to cp_parser_string_literal. */
5365 return (cp_parser_string_literal (parser,
5366 parser->translate_strings_p,
5367 true)
5368 .maybe_add_location_wrapper ());
5369
5370 case CPP_OPEN_PAREN:
5371 /* If we see `( { ' then we are looking at the beginning of
5372 a GNU statement-expression. */
5373 if (cp_parser_allow_gnu_extensions_p (parser)
5374 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5375 {
5376 /* Statement-expressions are not allowed by the standard. */
5377 pedwarn (token->location, OPT_Wpedantic,
5378 "ISO C++ forbids braced-groups within expressions");
5379
5380 /* And they're not allowed outside of a function-body; you
5381 cannot, for example, write:
5382
5383 int i = ({ int j = 3; j + 1; });
5384
5385 at class or namespace scope. */
5386 if (!parser->in_function_body
5387 || parser->in_template_argument_list_p)
5388 {
5389 error_at (token->location,
5390 "statement-expressions are not allowed outside "
5391 "functions nor in template-argument lists");
5392 cp_parser_skip_to_end_of_block_or_statement (parser);
5393 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5394 cp_lexer_consume_token (parser->lexer);
5395 return error_mark_node;
5396 }
5397 else
5398 return cp_parser_statement_expr (parser);
5399 }
5400 /* Otherwise it's a normal parenthesized expression. */
5401 {
5402 cp_expr expr;
5403 bool saved_greater_than_is_operator_p;
5404
5405 location_t open_paren_loc = token->location;
5406
5407 /* Consume the `('. */
5408 matching_parens parens;
5409 parens.consume_open (parser);
5410 /* Within a parenthesized expression, a `>' token is always
5411 the greater-than operator. */
5412 saved_greater_than_is_operator_p
5413 = parser->greater_than_is_operator_p;
5414 parser->greater_than_is_operator_p = true;
5415
5416 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5417 /* Left fold expression. */
5418 expr = NULL_TREE;
5419 else
5420 /* Parse the parenthesized expression. */
5421 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5422
5423 token = cp_lexer_peek_token (parser->lexer);
5424 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5425 {
5426 expr = cp_parser_fold_expression (parser, expr);
5427 if (expr != error_mark_node
5428 && cxx_dialect < cxx17)
5429 pedwarn (input_location, 0, "fold-expressions only available "
5430 "with %<-std=c++17%> or %<-std=gnu++17%>");
5431 }
5432 else
5433 /* Let the front end know that this expression was
5434 enclosed in parentheses. This matters in case, for
5435 example, the expression is of the form `A::B', since
5436 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5437 not. */
5438 expr = finish_parenthesized_expr (expr);
5439
5440 /* DR 705: Wrapping an unqualified name in parentheses
5441 suppresses arg-dependent lookup. We want to pass back
5442 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5443 (c++/37862), but none of the others. */
5444 if (*idk != CP_ID_KIND_QUALIFIED)
5445 *idk = CP_ID_KIND_NONE;
5446
5447 /* The `>' token might be the end of a template-id or
5448 template-parameter-list now. */
5449 parser->greater_than_is_operator_p
5450 = saved_greater_than_is_operator_p;
5451
5452 /* Consume the `)'. */
5453 token = cp_lexer_peek_token (parser->lexer);
5454 location_t close_paren_loc = token->location;
5455 expr.set_range (open_paren_loc, close_paren_loc);
5456 if (!parens.require_close (parser)
5457 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5458 cp_parser_skip_to_end_of_statement (parser);
5459
5460 return expr;
5461 }
5462
5463 case CPP_OPEN_SQUARE:
5464 {
5465 if (c_dialect_objc ())
5466 {
5467 /* We might have an Objective-C++ message. */
5468 cp_parser_parse_tentatively (parser);
5469 tree msg = cp_parser_objc_message_expression (parser);
5470 /* If that works out, we're done ... */
5471 if (cp_parser_parse_definitely (parser))
5472 return msg;
5473 /* ... else, fall though to see if it's a lambda. */
5474 }
5475 cp_expr lam = cp_parser_lambda_expression (parser);
5476 /* Don't warn about a failed tentative parse. */
5477 if (cp_parser_error_occurred (parser))
5478 return error_mark_node;
5479 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5480 return lam;
5481 }
5482
5483 case CPP_OBJC_STRING:
5484 if (c_dialect_objc ())
5485 /* We have an Objective-C++ string literal. */
5486 return cp_parser_objc_expression (parser);
5487 cp_parser_error (parser, "expected primary-expression");
5488 return error_mark_node;
5489
5490 case CPP_KEYWORD:
5491 switch (token->keyword)
5492 {
5493 /* These two are the boolean literals. */
5494 case RID_TRUE:
5495 cp_lexer_consume_token (parser->lexer);
5496 return cp_expr (boolean_true_node, token->location);
5497 case RID_FALSE:
5498 cp_lexer_consume_token (parser->lexer);
5499 return cp_expr (boolean_false_node, token->location);
5500
5501 /* The `__null' literal. */
5502 case RID_NULL:
5503 cp_lexer_consume_token (parser->lexer);
5504 return cp_expr (null_node, token->location);
5505
5506 /* The `nullptr' literal. */
5507 case RID_NULLPTR:
5508 cp_lexer_consume_token (parser->lexer);
5509 return cp_expr (nullptr_node, token->location);
5510
5511 /* Recognize the `this' keyword. */
5512 case RID_THIS:
5513 cp_lexer_consume_token (parser->lexer);
5514 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5515 {
5516 error_at (token->location,
5517 "%<this%> may not be used in this context");
5518 return error_mark_node;
5519 }
5520 /* Pointers cannot appear in constant-expressions. */
5521 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5522 return error_mark_node;
5523 return cp_expr (finish_this_expr (), token->location);
5524
5525 /* The `operator' keyword can be the beginning of an
5526 id-expression. */
5527 case RID_OPERATOR:
5528 goto id_expression;
5529
5530 case RID_FUNCTION_NAME:
5531 case RID_PRETTY_FUNCTION_NAME:
5532 case RID_C99_FUNCTION_NAME:
5533 {
5534 non_integral_constant name;
5535
5536 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5537 __func__ are the names of variables -- but they are
5538 treated specially. Therefore, they are handled here,
5539 rather than relying on the generic id-expression logic
5540 below. Grammatically, these names are id-expressions.
5541
5542 Consume the token. */
5543 token = cp_lexer_consume_token (parser->lexer);
5544
5545 switch (token->keyword)
5546 {
5547 case RID_FUNCTION_NAME:
5548 name = NIC_FUNC_NAME;
5549 break;
5550 case RID_PRETTY_FUNCTION_NAME:
5551 name = NIC_PRETTY_FUNC;
5552 break;
5553 case RID_C99_FUNCTION_NAME:
5554 name = NIC_C99_FUNC;
5555 break;
5556 default:
5557 gcc_unreachable ();
5558 }
5559
5560 if (cp_parser_non_integral_constant_expression (parser, name))
5561 return error_mark_node;
5562
5563 /* Look up the name. */
5564 return finish_fname (token->u.value);
5565 }
5566
5567 case RID_VA_ARG:
5568 {
5569 tree expression;
5570 tree type;
5571 location_t type_location;
5572 location_t start_loc
5573 = cp_lexer_peek_token (parser->lexer)->location;
5574 /* The `__builtin_va_arg' construct is used to handle
5575 `va_arg'. Consume the `__builtin_va_arg' token. */
5576 cp_lexer_consume_token (parser->lexer);
5577 /* Look for the opening `('. */
5578 matching_parens parens;
5579 parens.require_open (parser);
5580 /* Now, parse the assignment-expression. */
5581 expression = cp_parser_assignment_expression (parser);
5582 /* Look for the `,'. */
5583 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5584 type_location = cp_lexer_peek_token (parser->lexer)->location;
5585 /* Parse the type-id. */
5586 {
5587 type_id_in_expr_sentinel s (parser);
5588 type = cp_parser_type_id (parser);
5589 }
5590 /* Look for the closing `)'. */
5591 location_t finish_loc
5592 = cp_lexer_peek_token (parser->lexer)->location;
5593 parens.require_close (parser);
5594 /* Using `va_arg' in a constant-expression is not
5595 allowed. */
5596 if (cp_parser_non_integral_constant_expression (parser,
5597 NIC_VA_ARG))
5598 return error_mark_node;
5599 /* Construct a location of the form:
5600 __builtin_va_arg (v, int)
5601 ~~~~~~~~~~~~~~~~~~~~~^~~~
5602 with the caret at the type, ranging from the start of the
5603 "__builtin_va_arg" token to the close paren. */
5604 location_t combined_loc
5605 = make_location (type_location, start_loc, finish_loc);
5606 return build_x_va_arg (combined_loc, expression, type);
5607 }
5608
5609 case RID_OFFSETOF:
5610 return cp_parser_builtin_offsetof (parser);
5611
5612 case RID_HAS_NOTHROW_ASSIGN:
5613 case RID_HAS_NOTHROW_CONSTRUCTOR:
5614 case RID_HAS_NOTHROW_COPY:
5615 case RID_HAS_TRIVIAL_ASSIGN:
5616 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5617 case RID_HAS_TRIVIAL_COPY:
5618 case RID_HAS_TRIVIAL_DESTRUCTOR:
5619 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5620 case RID_HAS_VIRTUAL_DESTRUCTOR:
5621 case RID_IS_ABSTRACT:
5622 case RID_IS_AGGREGATE:
5623 case RID_IS_BASE_OF:
5624 case RID_IS_CLASS:
5625 case RID_IS_EMPTY:
5626 case RID_IS_ENUM:
5627 case RID_IS_FINAL:
5628 case RID_IS_LITERAL_TYPE:
5629 case RID_IS_POD:
5630 case RID_IS_POLYMORPHIC:
5631 case RID_IS_SAME_AS:
5632 case RID_IS_STD_LAYOUT:
5633 case RID_IS_TRIVIAL:
5634 case RID_IS_TRIVIALLY_ASSIGNABLE:
5635 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5636 case RID_IS_TRIVIALLY_COPYABLE:
5637 case RID_IS_UNION:
5638 case RID_IS_ASSIGNABLE:
5639 case RID_IS_CONSTRUCTIBLE:
5640 return cp_parser_trait_expr (parser, token->keyword);
5641
5642 // C++ concepts
5643 case RID_REQUIRES:
5644 return cp_parser_requires_expression (parser);
5645
5646 /* Objective-C++ expressions. */
5647 case RID_AT_ENCODE:
5648 case RID_AT_PROTOCOL:
5649 case RID_AT_SELECTOR:
5650 return cp_parser_objc_expression (parser);
5651
5652 case RID_TEMPLATE:
5653 if (parser->in_function_body
5654 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5655 == CPP_LESS))
5656 {
5657 error_at (token->location,
5658 "a template declaration cannot appear at block scope");
5659 cp_parser_skip_to_end_of_block_or_statement (parser);
5660 return error_mark_node;
5661 }
5662 /* FALLTHRU */
5663 default:
5664 cp_parser_error (parser, "expected primary-expression");
5665 return error_mark_node;
5666 }
5667
5668 /* An id-expression can start with either an identifier, a
5669 `::' as the beginning of a qualified-id, or the "operator"
5670 keyword. */
5671 case CPP_NAME:
5672 case CPP_SCOPE:
5673 case CPP_TEMPLATE_ID:
5674 case CPP_NESTED_NAME_SPECIFIER:
5675 {
5676 id_expression:
5677 cp_expr id_expression;
5678 cp_expr decl;
5679 const char *error_msg;
5680 bool template_p;
5681 bool done;
5682 cp_token *id_expr_token;
5683
5684 /* Parse the id-expression. */
5685 id_expression
5686 = cp_parser_id_expression (parser,
5687 /*template_keyword_p=*/false,
5688 /*check_dependency_p=*/true,
5689 &template_p,
5690 /*declarator_p=*/false,
5691 /*optional_p=*/false);
5692 if (id_expression == error_mark_node)
5693 return error_mark_node;
5694 id_expr_token = token;
5695 token = cp_lexer_peek_token (parser->lexer);
5696 done = (token->type != CPP_OPEN_SQUARE
5697 && token->type != CPP_OPEN_PAREN
5698 && token->type != CPP_DOT
5699 && token->type != CPP_DEREF
5700 && token->type != CPP_PLUS_PLUS
5701 && token->type != CPP_MINUS_MINUS);
5702 /* If we have a template-id, then no further lookup is
5703 required. If the template-id was for a template-class, we
5704 will sometimes have a TYPE_DECL at this point. */
5705 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5706 || TREE_CODE (id_expression) == TYPE_DECL)
5707 decl = id_expression;
5708 /* Look up the name. */
5709 else
5710 {
5711 tree ambiguous_decls;
5712
5713 /* If we already know that this lookup is ambiguous, then
5714 we've already issued an error message; there's no reason
5715 to check again. */
5716 if (id_expr_token->type == CPP_NAME
5717 && id_expr_token->error_reported)
5718 {
5719 cp_parser_simulate_error (parser);
5720 return error_mark_node;
5721 }
5722
5723 decl = cp_parser_lookup_name (parser, id_expression,
5724 none_type,
5725 template_p,
5726 /*is_namespace=*/false,
5727 /*check_dependency=*/true,
5728 &ambiguous_decls,
5729 id_expression.get_location ());
5730 /* If the lookup was ambiguous, an error will already have
5731 been issued. */
5732 if (ambiguous_decls)
5733 return error_mark_node;
5734
5735 /* In Objective-C++, we may have an Objective-C 2.0
5736 dot-syntax for classes here. */
5737 if (c_dialect_objc ()
5738 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5739 && TREE_CODE (decl) == TYPE_DECL
5740 && objc_is_class_name (decl))
5741 {
5742 tree component;
5743 cp_lexer_consume_token (parser->lexer);
5744 component = cp_parser_identifier (parser);
5745 if (component == error_mark_node)
5746 return error_mark_node;
5747
5748 tree result = objc_build_class_component_ref (id_expression,
5749 component);
5750 /* Build a location of the form:
5751 expr.component
5752 ~~~~~^~~~~~~~~
5753 with caret at the start of the component name (at
5754 input_location), ranging from the start of the id_expression
5755 to the end of the component name. */
5756 location_t combined_loc
5757 = make_location (input_location, id_expression.get_start (),
5758 get_finish (input_location));
5759 protected_set_expr_location (result, combined_loc);
5760 return result;
5761 }
5762
5763 /* In Objective-C++, an instance variable (ivar) may be preferred
5764 to whatever cp_parser_lookup_name() found.
5765 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5766 rest of c-family, we have to do a little extra work to preserve
5767 any location information in cp_expr "decl". Given that
5768 objc_lookup_ivar is implemented in "c-family" and "objc", we
5769 have a trip through the pure "tree" type, rather than cp_expr.
5770 Naively copying it back to "decl" would implicitly give the
5771 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5772 store an EXPR_LOCATION. Hence we only update "decl" (and
5773 hence its location_t) if we get back a different tree node. */
5774 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5775 id_expression);
5776 if (decl_tree != decl.get_value ())
5777 decl = cp_expr (decl_tree);
5778
5779 /* If name lookup gives us a SCOPE_REF, then the
5780 qualifying scope was dependent. */
5781 if (TREE_CODE (decl) == SCOPE_REF)
5782 {
5783 /* At this point, we do not know if DECL is a valid
5784 integral constant expression. We assume that it is
5785 in fact such an expression, so that code like:
5786
5787 template <int N> struct A {
5788 int a[B<N>::i];
5789 };
5790
5791 is accepted. At template-instantiation time, we
5792 will check that B<N>::i is actually a constant. */
5793 return decl;
5794 }
5795 /* Check to see if DECL is a local variable in a context
5796 where that is forbidden. */
5797 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
5798 && local_variable_p (decl))
5799 {
5800 error_at (id_expression.get_location (),
5801 "local variable %qD may not appear in this context",
5802 decl.get_value ());
5803 return error_mark_node;
5804 }
5805 }
5806
5807 decl = (finish_id_expression
5808 (id_expression, decl, parser->scope,
5809 idk,
5810 parser->integral_constant_expression_p,
5811 parser->allow_non_integral_constant_expression_p,
5812 &parser->non_integral_constant_expression_p,
5813 template_p, done, address_p,
5814 template_arg_p,
5815 &error_msg,
5816 id_expression.get_location ()));
5817 if (error_msg)
5818 cp_parser_error (parser, error_msg);
5819 /* Build a location for an id-expression of the form:
5820 ::ns::id
5821 ~~~~~~^~
5822 or:
5823 id
5824 ^~
5825 i.e. from the start of the first token to the end of the final
5826 token, with the caret at the start of the unqualified-id. */
5827 location_t caret_loc = get_pure_location (id_expression.get_location ());
5828 location_t start_loc = get_start (id_expr_token->location);
5829 location_t finish_loc = get_finish (id_expression.get_location ());
5830 location_t combined_loc
5831 = make_location (caret_loc, start_loc, finish_loc);
5832
5833 decl.set_location (combined_loc);
5834 return decl;
5835 }
5836
5837 /* Anything else is an error. */
5838 default:
5839 cp_parser_error (parser, "expected primary-expression");
5840 return error_mark_node;
5841 }
5842 }
5843
5844 static inline cp_expr
5845 cp_parser_primary_expression (cp_parser *parser,
5846 bool address_p,
5847 bool cast_p,
5848 bool template_arg_p,
5849 cp_id_kind *idk)
5850 {
5851 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5852 /*decltype*/false, idk);
5853 }
5854
5855 /* Parse an id-expression.
5856
5857 id-expression:
5858 unqualified-id
5859 qualified-id
5860
5861 qualified-id:
5862 :: [opt] nested-name-specifier template [opt] unqualified-id
5863 :: identifier
5864 :: operator-function-id
5865 :: template-id
5866
5867 Return a representation of the unqualified portion of the
5868 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5869 a `::' or nested-name-specifier.
5870
5871 Often, if the id-expression was a qualified-id, the caller will
5872 want to make a SCOPE_REF to represent the qualified-id. This
5873 function does not do this in order to avoid wastefully creating
5874 SCOPE_REFs when they are not required.
5875
5876 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5877 `template' keyword.
5878
5879 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5880 uninstantiated templates.
5881
5882 If *TEMPLATE_P is non-NULL, it is set to true iff the
5883 `template' keyword is used to explicitly indicate that the entity
5884 named is a template.
5885
5886 If DECLARATOR_P is true, the id-expression is appearing as part of
5887 a declarator, rather than as part of an expression. */
5888
5889 static cp_expr
5890 cp_parser_id_expression (cp_parser *parser,
5891 bool template_keyword_p,
5892 bool check_dependency_p,
5893 bool *template_p,
5894 bool declarator_p,
5895 bool optional_p)
5896 {
5897 bool global_scope_p;
5898 bool nested_name_specifier_p;
5899
5900 /* Assume the `template' keyword was not used. */
5901 if (template_p)
5902 *template_p = template_keyword_p;
5903
5904 /* Look for the optional `::' operator. */
5905 global_scope_p
5906 = (!template_keyword_p
5907 && (cp_parser_global_scope_opt (parser,
5908 /*current_scope_valid_p=*/false)
5909 != NULL_TREE));
5910
5911 /* Look for the optional nested-name-specifier. */
5912 nested_name_specifier_p
5913 = (cp_parser_nested_name_specifier_opt (parser,
5914 /*typename_keyword_p=*/false,
5915 check_dependency_p,
5916 /*type_p=*/false,
5917 declarator_p,
5918 template_keyword_p)
5919 != NULL_TREE);
5920
5921 /* If there is a nested-name-specifier, then we are looking at
5922 the first qualified-id production. */
5923 if (nested_name_specifier_p)
5924 {
5925 tree saved_scope;
5926 tree saved_object_scope;
5927 tree saved_qualifying_scope;
5928 cp_expr unqualified_id;
5929 bool is_template;
5930
5931 /* See if the next token is the `template' keyword. */
5932 if (!template_p)
5933 template_p = &is_template;
5934 *template_p = cp_parser_optional_template_keyword (parser);
5935 /* Name lookup we do during the processing of the
5936 unqualified-id might obliterate SCOPE. */
5937 saved_scope = parser->scope;
5938 saved_object_scope = parser->object_scope;
5939 saved_qualifying_scope = parser->qualifying_scope;
5940 /* Process the final unqualified-id. */
5941 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5942 check_dependency_p,
5943 declarator_p,
5944 /*optional_p=*/false);
5945 /* Restore the SAVED_SCOPE for our caller. */
5946 parser->scope = saved_scope;
5947 parser->object_scope = saved_object_scope;
5948 parser->qualifying_scope = saved_qualifying_scope;
5949
5950 return unqualified_id;
5951 }
5952 /* Otherwise, if we are in global scope, then we are looking at one
5953 of the other qualified-id productions. */
5954 else if (global_scope_p)
5955 {
5956 cp_token *token;
5957 tree id;
5958
5959 /* Peek at the next token. */
5960 token = cp_lexer_peek_token (parser->lexer);
5961
5962 /* If it's an identifier, and the next token is not a "<", then
5963 we can avoid the template-id case. This is an optimization
5964 for this common case. */
5965 if (token->type == CPP_NAME
5966 && !cp_parser_nth_token_starts_template_argument_list_p
5967 (parser, 2))
5968 return cp_parser_identifier (parser);
5969
5970 cp_parser_parse_tentatively (parser);
5971 /* Try a template-id. */
5972 id = cp_parser_template_id_expr (parser,
5973 /*template_keyword_p=*/false,
5974 /*check_dependency_p=*/true,
5975 declarator_p);
5976 /* If that worked, we're done. */
5977 if (cp_parser_parse_definitely (parser))
5978 return id;
5979
5980 /* Peek at the next token. (Changes in the token buffer may
5981 have invalidated the pointer obtained above.) */
5982 token = cp_lexer_peek_token (parser->lexer);
5983
5984 switch (token->type)
5985 {
5986 case CPP_NAME:
5987 return cp_parser_identifier (parser);
5988
5989 case CPP_KEYWORD:
5990 if (token->keyword == RID_OPERATOR)
5991 return cp_parser_operator_function_id (parser);
5992 /* Fall through. */
5993
5994 default:
5995 cp_parser_error (parser, "expected id-expression");
5996 return error_mark_node;
5997 }
5998 }
5999 else
6000 return cp_parser_unqualified_id (parser, template_keyword_p,
6001 /*check_dependency_p=*/true,
6002 declarator_p,
6003 optional_p);
6004 }
6005
6006 /* Parse an unqualified-id.
6007
6008 unqualified-id:
6009 identifier
6010 operator-function-id
6011 conversion-function-id
6012 ~ class-name
6013 template-id
6014
6015 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6016 keyword, in a construct like `A::template ...'.
6017
6018 Returns a representation of unqualified-id. For the `identifier'
6019 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6020 production a BIT_NOT_EXPR is returned; the operand of the
6021 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6022 other productions, see the documentation accompanying the
6023 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6024 names are looked up in uninstantiated templates. If DECLARATOR_P
6025 is true, the unqualified-id is appearing as part of a declarator,
6026 rather than as part of an expression. */
6027
6028 static cp_expr
6029 cp_parser_unqualified_id (cp_parser* parser,
6030 bool template_keyword_p,
6031 bool check_dependency_p,
6032 bool declarator_p,
6033 bool optional_p)
6034 {
6035 cp_token *token;
6036
6037 /* Peek at the next token. */
6038 token = cp_lexer_peek_token (parser->lexer);
6039
6040 switch ((int) token->type)
6041 {
6042 case CPP_NAME:
6043 {
6044 tree id;
6045
6046 /* We don't know yet whether or not this will be a
6047 template-id. */
6048 cp_parser_parse_tentatively (parser);
6049 /* Try a template-id. */
6050 id = cp_parser_template_id_expr (parser, template_keyword_p,
6051 check_dependency_p,
6052 declarator_p);
6053 /* If it worked, we're done. */
6054 if (cp_parser_parse_definitely (parser))
6055 return id;
6056 /* Otherwise, it's an ordinary identifier. */
6057 return cp_parser_identifier (parser);
6058 }
6059
6060 case CPP_TEMPLATE_ID:
6061 return cp_parser_template_id_expr (parser, template_keyword_p,
6062 check_dependency_p,
6063 declarator_p);
6064
6065 case CPP_COMPL:
6066 {
6067 tree type_decl;
6068 tree qualifying_scope;
6069 tree object_scope;
6070 tree scope;
6071 bool done;
6072 location_t tilde_loc = token->location;
6073
6074 /* Consume the `~' token. */
6075 cp_lexer_consume_token (parser->lexer);
6076 /* Parse the class-name. The standard, as written, seems to
6077 say that:
6078
6079 template <typename T> struct S { ~S (); };
6080 template <typename T> S<T>::~S() {}
6081
6082 is invalid, since `~' must be followed by a class-name, but
6083 `S<T>' is dependent, and so not known to be a class.
6084 That's not right; we need to look in uninstantiated
6085 templates. A further complication arises from:
6086
6087 template <typename T> void f(T t) {
6088 t.T::~T();
6089 }
6090
6091 Here, it is not possible to look up `T' in the scope of `T'
6092 itself. We must look in both the current scope, and the
6093 scope of the containing complete expression.
6094
6095 Yet another issue is:
6096
6097 struct S {
6098 int S;
6099 ~S();
6100 };
6101
6102 S::~S() {}
6103
6104 The standard does not seem to say that the `S' in `~S'
6105 should refer to the type `S' and not the data member
6106 `S::S'. */
6107
6108 /* DR 244 says that we look up the name after the "~" in the
6109 same scope as we looked up the qualifying name. That idea
6110 isn't fully worked out; it's more complicated than that. */
6111 scope = parser->scope;
6112 object_scope = parser->object_scope;
6113 qualifying_scope = parser->qualifying_scope;
6114
6115 /* Check for invalid scopes. */
6116 if (scope == error_mark_node)
6117 {
6118 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6119 cp_lexer_consume_token (parser->lexer);
6120 return error_mark_node;
6121 }
6122 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6123 {
6124 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6125 error_at (token->location,
6126 "scope %qT before %<~%> is not a class-name",
6127 scope);
6128 cp_parser_simulate_error (parser);
6129 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6130 cp_lexer_consume_token (parser->lexer);
6131 return error_mark_node;
6132 }
6133 if (template_keyword_p)
6134 {
6135 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6136 error_at (tilde_loc, "%<template%> keyword not permitted in "
6137 "destructor name");
6138 cp_parser_simulate_error (parser);
6139 return error_mark_node;
6140 }
6141
6142 gcc_assert (!scope || TYPE_P (scope));
6143
6144 token = cp_lexer_peek_token (parser->lexer);
6145
6146 /* Create a location with caret == start at the tilde,
6147 finishing at the end of the peeked token, e.g:
6148 ~token
6149 ^~~~~~. */
6150 location_t loc
6151 = make_location (tilde_loc, tilde_loc, token->location);
6152
6153 /* If the name is of the form "X::~X" it's OK even if X is a
6154 typedef. */
6155
6156 if (scope
6157 && token->type == CPP_NAME
6158 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6159 != CPP_LESS)
6160 && (token->u.value == TYPE_IDENTIFIER (scope)
6161 || (CLASS_TYPE_P (scope)
6162 && constructor_name_p (token->u.value, scope))))
6163 {
6164 cp_lexer_consume_token (parser->lexer);
6165 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6166 }
6167
6168 /* ~auto means the destructor of whatever the object is. */
6169 if (cp_parser_is_keyword (token, RID_AUTO))
6170 {
6171 if (cxx_dialect < cxx14)
6172 pedwarn (loc, 0,
6173 "%<~auto%> only available with "
6174 "%<-std=c++14%> or %<-std=gnu++14%>");
6175 cp_lexer_consume_token (parser->lexer);
6176 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6177 }
6178
6179 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6180 declarator-id of a constructor or destructor. */
6181 if (token->type == CPP_TEMPLATE_ID && cxx_dialect >= cxx20)
6182 {
6183 if (!cp_parser_simulate_error (parser))
6184 error_at (tilde_loc, "template-id not allowed for destructor");
6185 return error_mark_node;
6186 }
6187
6188 /* If there was an explicit qualification (S::~T), first look
6189 in the scope given by the qualification (i.e., S).
6190
6191 Note: in the calls to cp_parser_class_name below we pass
6192 typename_type so that lookup finds the injected-class-name
6193 rather than the constructor. */
6194 done = false;
6195 type_decl = NULL_TREE;
6196 if (scope)
6197 {
6198 cp_parser_parse_tentatively (parser);
6199 type_decl = cp_parser_class_name (parser,
6200 /*typename_keyword_p=*/false,
6201 /*template_keyword_p=*/false,
6202 typename_type,
6203 /*check_dependency=*/false,
6204 /*class_head_p=*/false,
6205 declarator_p);
6206 if (cp_parser_parse_definitely (parser))
6207 done = true;
6208 }
6209 /* In "N::S::~S", look in "N" as well. */
6210 if (!done && scope && qualifying_scope)
6211 {
6212 cp_parser_parse_tentatively (parser);
6213 parser->scope = qualifying_scope;
6214 parser->object_scope = NULL_TREE;
6215 parser->qualifying_scope = NULL_TREE;
6216 type_decl
6217 = cp_parser_class_name (parser,
6218 /*typename_keyword_p=*/false,
6219 /*template_keyword_p=*/false,
6220 typename_type,
6221 /*check_dependency=*/false,
6222 /*class_head_p=*/false,
6223 declarator_p);
6224 if (cp_parser_parse_definitely (parser))
6225 done = true;
6226 }
6227 /* In "p->S::~T", look in the scope given by "*p" as well. */
6228 else if (!done && object_scope)
6229 {
6230 cp_parser_parse_tentatively (parser);
6231 parser->scope = object_scope;
6232 parser->object_scope = NULL_TREE;
6233 parser->qualifying_scope = NULL_TREE;
6234 type_decl
6235 = cp_parser_class_name (parser,
6236 /*typename_keyword_p=*/false,
6237 /*template_keyword_p=*/false,
6238 typename_type,
6239 /*check_dependency=*/false,
6240 /*class_head_p=*/false,
6241 declarator_p);
6242 if (cp_parser_parse_definitely (parser))
6243 done = true;
6244 }
6245 /* Look in the surrounding context. */
6246 if (!done)
6247 {
6248 parser->scope = NULL_TREE;
6249 parser->object_scope = NULL_TREE;
6250 parser->qualifying_scope = NULL_TREE;
6251 if (processing_template_decl)
6252 cp_parser_parse_tentatively (parser);
6253 type_decl
6254 = cp_parser_class_name (parser,
6255 /*typename_keyword_p=*/false,
6256 /*template_keyword_p=*/false,
6257 typename_type,
6258 /*check_dependency=*/false,
6259 /*class_head_p=*/false,
6260 declarator_p);
6261 if (processing_template_decl
6262 && ! cp_parser_parse_definitely (parser))
6263 {
6264 /* We couldn't find a type with this name. If we're parsing
6265 tentatively, fail and try something else. */
6266 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6267 {
6268 cp_parser_simulate_error (parser);
6269 return error_mark_node;
6270 }
6271 /* Otherwise, accept it and check for a match at instantiation
6272 time. */
6273 type_decl = cp_parser_identifier (parser);
6274 if (type_decl != error_mark_node)
6275 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6276 return type_decl;
6277 }
6278 }
6279 /* If an error occurred, assume that the name of the
6280 destructor is the same as the name of the qualifying
6281 class. That allows us to keep parsing after running
6282 into ill-formed destructor names. */
6283 if (type_decl == error_mark_node && scope)
6284 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6285 else if (type_decl == error_mark_node)
6286 return error_mark_node;
6287
6288 /* Check that destructor name and scope match. */
6289 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6290 {
6291 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6292 error_at (loc,
6293 "declaration of %<~%T%> as member of %qT",
6294 type_decl, scope);
6295 cp_parser_simulate_error (parser);
6296 return error_mark_node;
6297 }
6298
6299 /* [class.dtor]
6300
6301 A typedef-name that names a class shall not be used as the
6302 identifier in the declarator for a destructor declaration. */
6303 if (declarator_p
6304 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6305 && !DECL_SELF_REFERENCE_P (type_decl)
6306 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6307 error_at (loc,
6308 "typedef-name %qD used as destructor declarator",
6309 type_decl);
6310
6311 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6312 }
6313
6314 case CPP_KEYWORD:
6315 if (token->keyword == RID_OPERATOR)
6316 {
6317 cp_expr id;
6318
6319 /* This could be a template-id, so we try that first. */
6320 cp_parser_parse_tentatively (parser);
6321 /* Try a template-id. */
6322 id = cp_parser_template_id_expr (parser, template_keyword_p,
6323 /*check_dependency_p=*/true,
6324 declarator_p);
6325 /* If that worked, we're done. */
6326 if (cp_parser_parse_definitely (parser))
6327 return id;
6328 /* We still don't know whether we're looking at an
6329 operator-function-id or a conversion-function-id. */
6330 cp_parser_parse_tentatively (parser);
6331 /* Try an operator-function-id. */
6332 id = cp_parser_operator_function_id (parser);
6333 /* If that didn't work, try a conversion-function-id. */
6334 if (!cp_parser_parse_definitely (parser))
6335 id = cp_parser_conversion_function_id (parser);
6336
6337 return id;
6338 }
6339 /* Fall through. */
6340
6341 default:
6342 if (optional_p)
6343 return NULL_TREE;
6344 cp_parser_error (parser, "expected unqualified-id");
6345 return error_mark_node;
6346 }
6347 }
6348
6349 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6350 be a template-id or the name shall refer to a class template or an
6351 alias template. */
6352
6353 static void
6354 check_template_keyword_in_nested_name_spec (tree name)
6355 {
6356 if (CLASS_TYPE_P (name)
6357 && ((CLASSTYPE_USE_TEMPLATE (name)
6358 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6359 || CLASSTYPE_IS_TEMPLATE (name)))
6360 return;
6361
6362 if (TREE_CODE (name) == TYPENAME_TYPE
6363 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6364 return;
6365 /* Alias templates are also OK. */
6366 else if (alias_template_specialization_p (name, nt_opaque))
6367 return;
6368
6369 permerror (input_location, TYPE_P (name)
6370 ? G_("%qT is not a template")
6371 : G_("%qD is not a template"),
6372 name);
6373 }
6374
6375 /* Parse an (optional) nested-name-specifier.
6376
6377 nested-name-specifier: [C++98]
6378 class-or-namespace-name :: nested-name-specifier [opt]
6379 class-or-namespace-name :: template nested-name-specifier [opt]
6380
6381 nested-name-specifier: [C++0x]
6382 type-name ::
6383 namespace-name ::
6384 nested-name-specifier identifier ::
6385 nested-name-specifier template [opt] simple-template-id ::
6386
6387 PARSER->SCOPE should be set appropriately before this function is
6388 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6389 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6390 in name lookups.
6391
6392 Sets PARSER->SCOPE to the class (TYPE) or namespace
6393 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6394 it unchanged if there is no nested-name-specifier. Returns the new
6395 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6396
6397 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6398 part of a declaration and/or decl-specifier. */
6399
6400 static tree
6401 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6402 bool typename_keyword_p,
6403 bool check_dependency_p,
6404 bool type_p,
6405 bool is_declaration,
6406 bool template_keyword_p /* = false */)
6407 {
6408 bool success = false;
6409 cp_token_position start = 0;
6410 cp_token *token;
6411
6412 /* Remember where the nested-name-specifier starts. */
6413 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6414 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6415 {
6416 start = cp_lexer_token_position (parser->lexer, false);
6417 push_deferring_access_checks (dk_deferred);
6418 }
6419
6420 while (true)
6421 {
6422 tree new_scope;
6423 tree old_scope;
6424 tree saved_qualifying_scope;
6425
6426 /* Spot cases that cannot be the beginning of a
6427 nested-name-specifier. */
6428 token = cp_lexer_peek_token (parser->lexer);
6429
6430 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6431 the already parsed nested-name-specifier. */
6432 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6433 {
6434 /* Grab the nested-name-specifier and continue the loop. */
6435 cp_parser_pre_parsed_nested_name_specifier (parser);
6436 /* If we originally encountered this nested-name-specifier
6437 with IS_DECLARATION set to false, we will not have
6438 resolved TYPENAME_TYPEs, so we must do so here. */
6439 if (is_declaration
6440 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6441 {
6442 new_scope = resolve_typename_type (parser->scope,
6443 /*only_current_p=*/false);
6444 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6445 parser->scope = new_scope;
6446 }
6447 success = true;
6448 continue;
6449 }
6450
6451 /* Spot cases that cannot be the beginning of a
6452 nested-name-specifier. On the second and subsequent times
6453 through the loop, we look for the `template' keyword. */
6454 if (success && token->keyword == RID_TEMPLATE)
6455 ;
6456 /* A template-id can start a nested-name-specifier. */
6457 else if (token->type == CPP_TEMPLATE_ID)
6458 ;
6459 /* DR 743: decltype can be used in a nested-name-specifier. */
6460 else if (token_is_decltype (token))
6461 ;
6462 else
6463 {
6464 /* If the next token is not an identifier, then it is
6465 definitely not a type-name or namespace-name. */
6466 if (token->type != CPP_NAME)
6467 break;
6468 /* If the following token is neither a `<' (to begin a
6469 template-id), nor a `::', then we are not looking at a
6470 nested-name-specifier. */
6471 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6472
6473 if (token->type == CPP_COLON
6474 && parser->colon_corrects_to_scope_p
6475 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6476 {
6477 gcc_rich_location richloc (token->location);
6478 richloc.add_fixit_replace ("::");
6479 error_at (&richloc,
6480 "found %<:%> in nested-name-specifier, "
6481 "expected %<::%>");
6482 token->type = CPP_SCOPE;
6483 }
6484
6485 if (token->type != CPP_SCOPE
6486 && !cp_parser_nth_token_starts_template_argument_list_p
6487 (parser, 2))
6488 break;
6489 }
6490
6491 /* The nested-name-specifier is optional, so we parse
6492 tentatively. */
6493 cp_parser_parse_tentatively (parser);
6494
6495 /* Look for the optional `template' keyword, if this isn't the
6496 first time through the loop. */
6497 if (success)
6498 {
6499 template_keyword_p = cp_parser_optional_template_keyword (parser);
6500 /* DR1710: "In a qualified-id used as the name in
6501 a typename-specifier, elaborated-type-specifier, using-declaration,
6502 or class-or-decltype, an optional keyword template appearing at
6503 the top level is ignored." */
6504 if (!template_keyword_p
6505 && typename_keyword_p
6506 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
6507 template_keyword_p = true;
6508 }
6509
6510 /* Save the old scope since the name lookup we are about to do
6511 might destroy it. */
6512 old_scope = parser->scope;
6513 saved_qualifying_scope = parser->qualifying_scope;
6514 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6515 look up names in "X<T>::I" in order to determine that "Y" is
6516 a template. So, if we have a typename at this point, we make
6517 an effort to look through it. */
6518 if (is_declaration
6519 && !typename_keyword_p
6520 && parser->scope
6521 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6522 parser->scope = resolve_typename_type (parser->scope,
6523 /*only_current_p=*/false);
6524 /* Parse the qualifying entity. */
6525 new_scope
6526 = cp_parser_qualifying_entity (parser,
6527 typename_keyword_p,
6528 template_keyword_p,
6529 check_dependency_p,
6530 type_p,
6531 is_declaration);
6532 /* Look for the `::' token. */
6533 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6534
6535 /* If we found what we wanted, we keep going; otherwise, we're
6536 done. */
6537 if (!cp_parser_parse_definitely (parser))
6538 {
6539 bool error_p = false;
6540
6541 /* Restore the OLD_SCOPE since it was valid before the
6542 failed attempt at finding the last
6543 class-or-namespace-name. */
6544 parser->scope = old_scope;
6545 parser->qualifying_scope = saved_qualifying_scope;
6546
6547 /* If the next token is a decltype, and the one after that is a
6548 `::', then the decltype has failed to resolve to a class or
6549 enumeration type. Give this error even when parsing
6550 tentatively since it can't possibly be valid--and we're going
6551 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6552 won't get another chance.*/
6553 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6554 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6555 == CPP_SCOPE))
6556 {
6557 token = cp_lexer_consume_token (parser->lexer);
6558 tree dtype = token->u.tree_check_value->value;
6559 if (dtype != error_mark_node)
6560 error_at (token->location, "%<decltype%> evaluates to %qT, "
6561 "which is not a class or enumeration type",
6562 dtype);
6563 parser->scope = error_mark_node;
6564 error_p = true;
6565 /* As below. */
6566 success = true;
6567 cp_lexer_consume_token (parser->lexer);
6568 }
6569
6570 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6571 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6572 {
6573 /* If we have a non-type template-id followed by ::, it can't
6574 possibly be valid. */
6575 token = cp_lexer_peek_token (parser->lexer);
6576 tree tid = token->u.tree_check_value->value;
6577 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6578 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6579 {
6580 tree tmpl = NULL_TREE;
6581 if (is_overloaded_fn (tid))
6582 {
6583 tree fns = get_fns (tid);
6584 if (OVL_SINGLE_P (fns))
6585 tmpl = OVL_FIRST (fns);
6586 if (function_concept_p (fns))
6587 error_at (token->location, "concept-id %qD "
6588 "in nested-name-specifier", tid);
6589 else
6590 error_at (token->location, "function template-id "
6591 "%qD in nested-name-specifier", tid);
6592 }
6593 else
6594 {
6595 tmpl = TREE_OPERAND (tid, 0);
6596 if (variable_concept_p (tmpl)
6597 || standard_concept_p (tmpl))
6598 error_at (token->location, "concept-id %qD "
6599 "in nested-name-specifier", tid);
6600 else
6601 {
6602 /* Variable template. */
6603 gcc_assert (variable_template_p (tmpl));
6604 error_at (token->location, "variable template-id "
6605 "%qD in nested-name-specifier", tid);
6606 }
6607 }
6608 if (tmpl)
6609 inform (DECL_SOURCE_LOCATION (tmpl),
6610 "%qD declared here", tmpl);
6611
6612 parser->scope = error_mark_node;
6613 error_p = true;
6614 /* As below. */
6615 success = true;
6616 cp_lexer_consume_token (parser->lexer);
6617 cp_lexer_consume_token (parser->lexer);
6618 }
6619 }
6620
6621 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6622 break;
6623 /* If the next token is an identifier, and the one after
6624 that is a `::', then any valid interpretation would have
6625 found a class-or-namespace-name. */
6626 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6627 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6628 == CPP_SCOPE)
6629 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6630 != CPP_COMPL))
6631 {
6632 token = cp_lexer_consume_token (parser->lexer);
6633 if (!error_p)
6634 {
6635 if (!token->error_reported)
6636 {
6637 tree decl;
6638 tree ambiguous_decls;
6639
6640 decl = cp_parser_lookup_name (parser, token->u.value,
6641 none_type,
6642 /*is_template=*/false,
6643 /*is_namespace=*/false,
6644 /*check_dependency=*/true,
6645 &ambiguous_decls,
6646 token->location);
6647 if (TREE_CODE (decl) == TEMPLATE_DECL)
6648 error_at (token->location,
6649 "%qD used without template arguments",
6650 decl);
6651 else if (ambiguous_decls)
6652 {
6653 // cp_parser_lookup_name has the same diagnostic,
6654 // thus make sure to emit it at most once.
6655 if (cp_parser_uncommitted_to_tentative_parse_p
6656 (parser))
6657 {
6658 error_at (token->location,
6659 "reference to %qD is ambiguous",
6660 token->u.value);
6661 print_candidates (ambiguous_decls);
6662 }
6663 decl = error_mark_node;
6664 }
6665 else
6666 {
6667 if (cxx_dialect != cxx98)
6668 cp_parser_name_lookup_error
6669 (parser, token->u.value, decl, NLE_NOT_CXX98,
6670 token->location);
6671 else
6672 cp_parser_name_lookup_error
6673 (parser, token->u.value, decl, NLE_CXX98,
6674 token->location);
6675 }
6676 }
6677 parser->scope = error_mark_node;
6678 error_p = true;
6679 /* Treat this as a successful nested-name-specifier
6680 due to:
6681
6682 [basic.lookup.qual]
6683
6684 If the name found is not a class-name (clause
6685 _class_) or namespace-name (_namespace.def_), the
6686 program is ill-formed. */
6687 success = true;
6688 }
6689 cp_lexer_consume_token (parser->lexer);
6690 }
6691 break;
6692 }
6693 /* We've found one valid nested-name-specifier. */
6694 success = true;
6695 /* Name lookup always gives us a DECL. */
6696 if (TREE_CODE (new_scope) == TYPE_DECL)
6697 new_scope = TREE_TYPE (new_scope);
6698 /* Uses of "template" must be followed by actual templates. */
6699 if (template_keyword_p)
6700 check_template_keyword_in_nested_name_spec (new_scope);
6701 /* If it is a class scope, try to complete it; we are about to
6702 be looking up names inside the class. */
6703 if (TYPE_P (new_scope)
6704 /* Since checking types for dependency can be expensive,
6705 avoid doing it if the type is already complete. */
6706 && !COMPLETE_TYPE_P (new_scope)
6707 /* Do not try to complete dependent types. */
6708 && !dependent_type_p (new_scope))
6709 {
6710 new_scope = complete_type (new_scope);
6711 /* If it is a typedef to current class, use the current
6712 class instead, as the typedef won't have any names inside
6713 it yet. */
6714 if (!COMPLETE_TYPE_P (new_scope)
6715 && currently_open_class (new_scope))
6716 new_scope = TYPE_MAIN_VARIANT (new_scope);
6717 }
6718 /* Make sure we look in the right scope the next time through
6719 the loop. */
6720 parser->scope = new_scope;
6721 }
6722
6723 /* If parsing tentatively, replace the sequence of tokens that makes
6724 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6725 token. That way, should we re-parse the token stream, we will
6726 not have to repeat the effort required to do the parse, nor will
6727 we issue duplicate error messages. */
6728 if (success && start)
6729 {
6730 cp_token *token;
6731
6732 token = cp_lexer_token_at (parser->lexer, start);
6733 /* Reset the contents of the START token. */
6734 token->type = CPP_NESTED_NAME_SPECIFIER;
6735 /* Retrieve any deferred checks. Do not pop this access checks yet
6736 so the memory will not be reclaimed during token replacing below. */
6737 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6738 token->tree_check_p = true;
6739 token->u.tree_check_value->value = parser->scope;
6740 token->u.tree_check_value->checks = get_deferred_access_checks ();
6741 token->u.tree_check_value->qualifying_scope =
6742 parser->qualifying_scope;
6743 token->keyword = RID_MAX;
6744
6745 /* Purge all subsequent tokens. */
6746 cp_lexer_purge_tokens_after (parser->lexer, start);
6747 }
6748
6749 if (start)
6750 pop_to_parent_deferring_access_checks ();
6751
6752 return success ? parser->scope : NULL_TREE;
6753 }
6754
6755 /* Parse a nested-name-specifier. See
6756 cp_parser_nested_name_specifier_opt for details. This function
6757 behaves identically, except that it will an issue an error if no
6758 nested-name-specifier is present. */
6759
6760 static tree
6761 cp_parser_nested_name_specifier (cp_parser *parser,
6762 bool typename_keyword_p,
6763 bool check_dependency_p,
6764 bool type_p,
6765 bool is_declaration)
6766 {
6767 tree scope;
6768
6769 /* Look for the nested-name-specifier. */
6770 scope = cp_parser_nested_name_specifier_opt (parser,
6771 typename_keyword_p,
6772 check_dependency_p,
6773 type_p,
6774 is_declaration);
6775 /* If it was not present, issue an error message. */
6776 if (!scope)
6777 {
6778 cp_parser_error (parser, "expected nested-name-specifier");
6779 parser->scope = NULL_TREE;
6780 }
6781
6782 return scope;
6783 }
6784
6785 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6786 this is either a class-name or a namespace-name (which corresponds
6787 to the class-or-namespace-name production in the grammar). For
6788 C++0x, it can also be a type-name that refers to an enumeration
6789 type or a simple-template-id.
6790
6791 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6792 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6793 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6794 TYPE_P is TRUE iff the next name should be taken as a class-name,
6795 even the same name is declared to be another entity in the same
6796 scope.
6797
6798 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6799 specified by the class-or-namespace-name. If neither is found the
6800 ERROR_MARK_NODE is returned. */
6801
6802 static tree
6803 cp_parser_qualifying_entity (cp_parser *parser,
6804 bool typename_keyword_p,
6805 bool template_keyword_p,
6806 bool check_dependency_p,
6807 bool type_p,
6808 bool is_declaration)
6809 {
6810 tree saved_scope;
6811 tree saved_qualifying_scope;
6812 tree saved_object_scope;
6813 tree scope;
6814 bool only_class_p;
6815 bool successful_parse_p;
6816
6817 /* DR 743: decltype can appear in a nested-name-specifier. */
6818 if (cp_lexer_next_token_is_decltype (parser->lexer))
6819 {
6820 scope = cp_parser_decltype (parser);
6821 if (TREE_CODE (scope) != ENUMERAL_TYPE
6822 && !MAYBE_CLASS_TYPE_P (scope))
6823 {
6824 cp_parser_simulate_error (parser);
6825 return error_mark_node;
6826 }
6827 if (TYPE_NAME (scope))
6828 scope = TYPE_NAME (scope);
6829 return scope;
6830 }
6831
6832 /* Before we try to parse the class-name, we must save away the
6833 current PARSER->SCOPE since cp_parser_class_name will destroy
6834 it. */
6835 saved_scope = parser->scope;
6836 saved_qualifying_scope = parser->qualifying_scope;
6837 saved_object_scope = parser->object_scope;
6838 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6839 there is no need to look for a namespace-name. */
6840 only_class_p = template_keyword_p
6841 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6842 if (!only_class_p)
6843 cp_parser_parse_tentatively (parser);
6844 scope = cp_parser_class_name (parser,
6845 typename_keyword_p,
6846 template_keyword_p,
6847 type_p ? class_type : none_type,
6848 check_dependency_p,
6849 /*class_head_p=*/false,
6850 is_declaration,
6851 /*enum_ok=*/cxx_dialect > cxx98);
6852 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6853 /* If that didn't work, try for a namespace-name. */
6854 if (!only_class_p && !successful_parse_p)
6855 {
6856 /* Restore the saved scope. */
6857 parser->scope = saved_scope;
6858 parser->qualifying_scope = saved_qualifying_scope;
6859 parser->object_scope = saved_object_scope;
6860 /* If we are not looking at an identifier followed by the scope
6861 resolution operator, then this is not part of a
6862 nested-name-specifier. (Note that this function is only used
6863 to parse the components of a nested-name-specifier.) */
6864 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6865 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6866 return error_mark_node;
6867 scope = cp_parser_namespace_name (parser);
6868 }
6869
6870 return scope;
6871 }
6872
6873 /* Return true if we are looking at a compound-literal, false otherwise. */
6874
6875 static bool
6876 cp_parser_compound_literal_p (cp_parser *parser)
6877 {
6878 cp_lexer_save_tokens (parser->lexer);
6879
6880 /* Skip tokens until the next token is a closing parenthesis.
6881 If we find the closing `)', and the next token is a `{', then
6882 we are looking at a compound-literal. */
6883 bool compound_literal_p
6884 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6885 /*consume_paren=*/true)
6886 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6887
6888 /* Roll back the tokens we skipped. */
6889 cp_lexer_rollback_tokens (parser->lexer);
6890
6891 return compound_literal_p;
6892 }
6893
6894 /* Return true if EXPR is the integer constant zero or a complex constant
6895 of zero, without any folding, but ignoring location wrappers. */
6896
6897 bool
6898 literal_integer_zerop (const_tree expr)
6899 {
6900 return (location_wrapper_p (expr)
6901 && integer_zerop (TREE_OPERAND (expr, 0)));
6902 }
6903
6904 /* Parse a postfix-expression.
6905
6906 postfix-expression:
6907 primary-expression
6908 postfix-expression [ expression ]
6909 postfix-expression ( expression-list [opt] )
6910 simple-type-specifier ( expression-list [opt] )
6911 typename :: [opt] nested-name-specifier identifier
6912 ( expression-list [opt] )
6913 typename :: [opt] nested-name-specifier template [opt] template-id
6914 ( expression-list [opt] )
6915 postfix-expression . template [opt] id-expression
6916 postfix-expression -> template [opt] id-expression
6917 postfix-expression . pseudo-destructor-name
6918 postfix-expression -> pseudo-destructor-name
6919 postfix-expression ++
6920 postfix-expression --
6921 dynamic_cast < type-id > ( expression )
6922 static_cast < type-id > ( expression )
6923 reinterpret_cast < type-id > ( expression )
6924 const_cast < type-id > ( expression )
6925 typeid ( expression )
6926 typeid ( type-id )
6927
6928 GNU Extension:
6929
6930 postfix-expression:
6931 ( type-id ) { initializer-list , [opt] }
6932
6933 This extension is a GNU version of the C99 compound-literal
6934 construct. (The C99 grammar uses `type-name' instead of `type-id',
6935 but they are essentially the same concept.)
6936
6937 If ADDRESS_P is true, the postfix expression is the operand of the
6938 `&' operator. CAST_P is true if this expression is the target of a
6939 cast.
6940
6941 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6942 class member access expressions [expr.ref].
6943
6944 Returns a representation of the expression. */
6945
6946 static cp_expr
6947 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6948 bool member_access_only_p, bool decltype_p,
6949 cp_id_kind * pidk_return)
6950 {
6951 cp_token *token;
6952 location_t loc;
6953 enum rid keyword;
6954 cp_id_kind idk = CP_ID_KIND_NONE;
6955 cp_expr postfix_expression = NULL_TREE;
6956 bool is_member_access = false;
6957
6958 /* Peek at the next token. */
6959 token = cp_lexer_peek_token (parser->lexer);
6960 loc = token->location;
6961 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6962
6963 /* Some of the productions are determined by keywords. */
6964 keyword = token->keyword;
6965 switch (keyword)
6966 {
6967 case RID_DYNCAST:
6968 case RID_STATCAST:
6969 case RID_REINTCAST:
6970 case RID_CONSTCAST:
6971 {
6972 tree type;
6973 cp_expr expression;
6974 const char *saved_message;
6975 bool saved_in_type_id_in_expr_p;
6976
6977 /* All of these can be handled in the same way from the point
6978 of view of parsing. Begin by consuming the token
6979 identifying the cast. */
6980 cp_lexer_consume_token (parser->lexer);
6981
6982 /* New types cannot be defined in the cast. */
6983 saved_message = parser->type_definition_forbidden_message;
6984 parser->type_definition_forbidden_message
6985 = G_("types may not be defined in casts");
6986
6987 /* Look for the opening `<'. */
6988 cp_parser_require (parser, CPP_LESS, RT_LESS);
6989 /* Parse the type to which we are casting. */
6990 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6991 parser->in_type_id_in_expr_p = true;
6992 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
6993 NULL);
6994 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6995 /* Look for the closing `>'. */
6996 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6997 /* Restore the old message. */
6998 parser->type_definition_forbidden_message = saved_message;
6999
7000 bool saved_greater_than_is_operator_p
7001 = parser->greater_than_is_operator_p;
7002 parser->greater_than_is_operator_p = true;
7003
7004 /* And the expression which is being cast. */
7005 matching_parens parens;
7006 parens.require_open (parser);
7007 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7008 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7009 RT_CLOSE_PAREN);
7010 location_t end_loc = close_paren ?
7011 close_paren->location : UNKNOWN_LOCATION;
7012
7013 parser->greater_than_is_operator_p
7014 = saved_greater_than_is_operator_p;
7015
7016 /* Only type conversions to integral or enumeration types
7017 can be used in constant-expressions. */
7018 if (!cast_valid_in_integral_constant_expression_p (type)
7019 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7020 {
7021 postfix_expression = error_mark_node;
7022 break;
7023 }
7024
7025 /* Construct a location e.g. :
7026 reinterpret_cast <int *> (expr)
7027 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7028 ranging from the start of the "*_cast" token to the final closing
7029 paren, with the caret at the start. */
7030 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7031
7032 switch (keyword)
7033 {
7034 case RID_DYNCAST:
7035 postfix_expression
7036 = build_dynamic_cast (cp_cast_loc, type, expression,
7037 tf_warning_or_error);
7038 break;
7039 case RID_STATCAST:
7040 postfix_expression
7041 = build_static_cast (cp_cast_loc, type, expression,
7042 tf_warning_or_error);
7043 break;
7044 case RID_REINTCAST:
7045 postfix_expression
7046 = build_reinterpret_cast (cp_cast_loc, type, expression,
7047 tf_warning_or_error);
7048 break;
7049 case RID_CONSTCAST:
7050 postfix_expression
7051 = build_const_cast (cp_cast_loc, type, expression,
7052 tf_warning_or_error);
7053 break;
7054 default:
7055 gcc_unreachable ();
7056 }
7057 }
7058 break;
7059
7060 case RID_TYPEID:
7061 {
7062 tree type;
7063 const char *saved_message;
7064 bool saved_in_type_id_in_expr_p;
7065
7066 /* Consume the `typeid' token. */
7067 cp_lexer_consume_token (parser->lexer);
7068 /* Look for the `(' token. */
7069 matching_parens parens;
7070 parens.require_open (parser);
7071 /* Types cannot be defined in a `typeid' expression. */
7072 saved_message = parser->type_definition_forbidden_message;
7073 parser->type_definition_forbidden_message
7074 = G_("types may not be defined in a %<typeid%> expression");
7075 /* We can't be sure yet whether we're looking at a type-id or an
7076 expression. */
7077 cp_parser_parse_tentatively (parser);
7078 /* Try a type-id first. */
7079 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7080 parser->in_type_id_in_expr_p = true;
7081 type = cp_parser_type_id (parser);
7082 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7083 /* Look for the `)' token. Otherwise, we can't be sure that
7084 we're not looking at an expression: consider `typeid (int
7085 (3))', for example. */
7086 cp_token *close_paren = parens.require_close (parser);
7087 /* If all went well, simply lookup the type-id. */
7088 if (cp_parser_parse_definitely (parser))
7089 postfix_expression = get_typeid (type, tf_warning_or_error);
7090 /* Otherwise, fall back to the expression variant. */
7091 else
7092 {
7093 tree expression;
7094
7095 /* Look for an expression. */
7096 expression = cp_parser_expression (parser, & idk);
7097 /* Compute its typeid. */
7098 postfix_expression = build_typeid (expression, tf_warning_or_error);
7099 /* Look for the `)' token. */
7100 close_paren = parens.require_close (parser);
7101 }
7102 /* Restore the saved message. */
7103 parser->type_definition_forbidden_message = saved_message;
7104 /* `typeid' may not appear in an integral constant expression. */
7105 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7106 postfix_expression = error_mark_node;
7107
7108 /* Construct a location e.g. :
7109 typeid (expr)
7110 ^~~~~~~~~~~~~
7111 ranging from the start of the "typeid" token to the final closing
7112 paren, with the caret at the start. */
7113 if (close_paren)
7114 {
7115 location_t typeid_loc
7116 = make_location (start_loc, start_loc, close_paren->location);
7117 postfix_expression.set_location (typeid_loc);
7118 postfix_expression.maybe_add_location_wrapper ();
7119 }
7120 }
7121 break;
7122
7123 case RID_TYPENAME:
7124 {
7125 tree type;
7126 /* The syntax permitted here is the same permitted for an
7127 elaborated-type-specifier. */
7128 ++parser->prevent_constrained_type_specifiers;
7129 type = cp_parser_elaborated_type_specifier (parser,
7130 /*is_friend=*/false,
7131 /*is_declaration=*/false);
7132 --parser->prevent_constrained_type_specifiers;
7133 postfix_expression = cp_parser_functional_cast (parser, type);
7134 }
7135 break;
7136
7137 case RID_ADDRESSOF:
7138 case RID_BUILTIN_SHUFFLE:
7139 case RID_BUILTIN_LAUNDER:
7140 {
7141 vec<tree, va_gc> *vec;
7142 unsigned int i;
7143 tree p;
7144
7145 cp_lexer_consume_token (parser->lexer);
7146 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7147 /*cast_p=*/false, /*allow_expansion_p=*/true,
7148 /*non_constant_p=*/NULL);
7149 if (vec == NULL)
7150 {
7151 postfix_expression = error_mark_node;
7152 break;
7153 }
7154
7155 FOR_EACH_VEC_ELT (*vec, i, p)
7156 mark_exp_read (p);
7157
7158 switch (keyword)
7159 {
7160 case RID_ADDRESSOF:
7161 if (vec->length () == 1)
7162 postfix_expression
7163 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7164 else
7165 {
7166 error_at (loc, "wrong number of arguments to "
7167 "%<__builtin_addressof%>");
7168 postfix_expression = error_mark_node;
7169 }
7170 break;
7171
7172 case RID_BUILTIN_LAUNDER:
7173 if (vec->length () == 1)
7174 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7175 tf_warning_or_error);
7176 else
7177 {
7178 error_at (loc, "wrong number of arguments to "
7179 "%<__builtin_launder%>");
7180 postfix_expression = error_mark_node;
7181 }
7182 break;
7183
7184 case RID_BUILTIN_SHUFFLE:
7185 if (vec->length () == 2)
7186 postfix_expression
7187 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7188 (*vec)[1], tf_warning_or_error);
7189 else if (vec->length () == 3)
7190 postfix_expression
7191 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7192 (*vec)[2], tf_warning_or_error);
7193 else
7194 {
7195 error_at (loc, "wrong number of arguments to "
7196 "%<__builtin_shuffle%>");
7197 postfix_expression = error_mark_node;
7198 }
7199 break;
7200
7201 default:
7202 gcc_unreachable ();
7203 }
7204 break;
7205 }
7206
7207 case RID_BUILTIN_CONVERTVECTOR:
7208 {
7209 tree expression;
7210 tree type;
7211 /* Consume the `__builtin_convertvector' token. */
7212 cp_lexer_consume_token (parser->lexer);
7213 /* Look for the opening `('. */
7214 matching_parens parens;
7215 parens.require_open (parser);
7216 /* Now, parse the assignment-expression. */
7217 expression = cp_parser_assignment_expression (parser);
7218 /* Look for the `,'. */
7219 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7220 location_t type_location
7221 = cp_lexer_peek_token (parser->lexer)->location;
7222 /* Parse the type-id. */
7223 {
7224 type_id_in_expr_sentinel s (parser);
7225 type = cp_parser_type_id (parser);
7226 }
7227 /* Look for the closing `)'. */
7228 parens.require_close (parser);
7229 return cp_build_vec_convert (expression, type_location, type,
7230 tf_warning_or_error);
7231 }
7232
7233 default:
7234 {
7235 tree type;
7236
7237 /* If the next thing is a simple-type-specifier, we may be
7238 looking at a functional cast. We could also be looking at
7239 an id-expression. So, we try the functional cast, and if
7240 that doesn't work we fall back to the primary-expression. */
7241 cp_parser_parse_tentatively (parser);
7242 /* Look for the simple-type-specifier. */
7243 ++parser->prevent_constrained_type_specifiers;
7244 type = cp_parser_simple_type_specifier (parser,
7245 /*decl_specs=*/NULL,
7246 CP_PARSER_FLAGS_NONE);
7247 --parser->prevent_constrained_type_specifiers;
7248 /* Parse the cast itself. */
7249 if (!cp_parser_error_occurred (parser))
7250 postfix_expression
7251 = cp_parser_functional_cast (parser, type);
7252 /* If that worked, we're done. */
7253 if (cp_parser_parse_definitely (parser))
7254 break;
7255
7256 /* If the functional-cast didn't work out, try a
7257 compound-literal. */
7258 if (cp_parser_allow_gnu_extensions_p (parser)
7259 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7260 {
7261 cp_expr initializer = NULL_TREE;
7262
7263 cp_parser_parse_tentatively (parser);
7264
7265 matching_parens parens;
7266 parens.consume_open (parser);
7267
7268 /* Avoid calling cp_parser_type_id pointlessly, see comment
7269 in cp_parser_cast_expression about c++/29234. */
7270 if (!cp_parser_compound_literal_p (parser))
7271 cp_parser_simulate_error (parser);
7272 else
7273 {
7274 /* Parse the type. */
7275 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7276 parser->in_type_id_in_expr_p = true;
7277 type = cp_parser_type_id (parser);
7278 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7279 parens.require_close (parser);
7280 }
7281
7282 /* If things aren't going well, there's no need to
7283 keep going. */
7284 if (!cp_parser_error_occurred (parser))
7285 {
7286 bool non_constant_p;
7287 /* Parse the brace-enclosed initializer list. */
7288 initializer = cp_parser_braced_list (parser,
7289 &non_constant_p);
7290 }
7291 /* If that worked, we're definitely looking at a
7292 compound-literal expression. */
7293 if (cp_parser_parse_definitely (parser))
7294 {
7295 /* Warn the user that a compound literal is not
7296 allowed in standard C++. */
7297 pedwarn (input_location, OPT_Wpedantic,
7298 "ISO C++ forbids compound-literals");
7299 /* For simplicity, we disallow compound literals in
7300 constant-expressions. We could
7301 allow compound literals of integer type, whose
7302 initializer was a constant, in constant
7303 expressions. Permitting that usage, as a further
7304 extension, would not change the meaning of any
7305 currently accepted programs. (Of course, as
7306 compound literals are not part of ISO C++, the
7307 standard has nothing to say.) */
7308 if (cp_parser_non_integral_constant_expression (parser,
7309 NIC_NCC))
7310 {
7311 postfix_expression = error_mark_node;
7312 break;
7313 }
7314 /* Form the representation of the compound-literal. */
7315 postfix_expression
7316 = finish_compound_literal (type, initializer,
7317 tf_warning_or_error, fcl_c99);
7318 postfix_expression.set_location (initializer.get_location ());
7319 break;
7320 }
7321 }
7322
7323 /* It must be a primary-expression. */
7324 postfix_expression
7325 = cp_parser_primary_expression (parser, address_p, cast_p,
7326 /*template_arg_p=*/false,
7327 decltype_p,
7328 &idk);
7329 }
7330 break;
7331 }
7332
7333 /* Note that we don't need to worry about calling build_cplus_new on a
7334 class-valued CALL_EXPR in decltype when it isn't the end of the
7335 postfix-expression; unary_complex_lvalue will take care of that for
7336 all these cases. */
7337
7338 /* Keep looping until the postfix-expression is complete. */
7339 while (true)
7340 {
7341 if (idk == CP_ID_KIND_UNQUALIFIED
7342 && identifier_p (postfix_expression)
7343 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7344 /* It is not a Koenig lookup function call. */
7345 postfix_expression
7346 = unqualified_name_lookup_error (postfix_expression);
7347
7348 /* Peek at the next token. */
7349 token = cp_lexer_peek_token (parser->lexer);
7350
7351 switch (token->type)
7352 {
7353 case CPP_OPEN_SQUARE:
7354 if (cp_next_tokens_can_be_std_attribute_p (parser))
7355 {
7356 cp_parser_error (parser,
7357 "two consecutive %<[%> shall "
7358 "only introduce an attribute");
7359 return error_mark_node;
7360 }
7361 postfix_expression
7362 = cp_parser_postfix_open_square_expression (parser,
7363 postfix_expression,
7364 false,
7365 decltype_p);
7366 postfix_expression.set_range (start_loc,
7367 postfix_expression.get_location ());
7368
7369 idk = CP_ID_KIND_NONE;
7370 is_member_access = false;
7371 break;
7372
7373 case CPP_OPEN_PAREN:
7374 /* postfix-expression ( expression-list [opt] ) */
7375 {
7376 bool koenig_p;
7377 bool is_builtin_constant_p;
7378 bool saved_integral_constant_expression_p = false;
7379 bool saved_non_integral_constant_expression_p = false;
7380 tsubst_flags_t complain = complain_flags (decltype_p);
7381 vec<tree, va_gc> *args;
7382 location_t close_paren_loc = UNKNOWN_LOCATION;
7383
7384 is_member_access = false;
7385
7386 tree stripped_expression
7387 = tree_strip_any_location_wrapper (postfix_expression);
7388 is_builtin_constant_p
7389 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7390 if (is_builtin_constant_p)
7391 {
7392 /* The whole point of __builtin_constant_p is to allow
7393 non-constant expressions to appear as arguments. */
7394 saved_integral_constant_expression_p
7395 = parser->integral_constant_expression_p;
7396 saved_non_integral_constant_expression_p
7397 = parser->non_integral_constant_expression_p;
7398 parser->integral_constant_expression_p = false;
7399 }
7400 args = (cp_parser_parenthesized_expression_list
7401 (parser, non_attr,
7402 /*cast_p=*/false, /*allow_expansion_p=*/true,
7403 /*non_constant_p=*/NULL,
7404 /*close_paren_loc=*/&close_paren_loc,
7405 /*wrap_locations_p=*/true));
7406 if (is_builtin_constant_p)
7407 {
7408 parser->integral_constant_expression_p
7409 = saved_integral_constant_expression_p;
7410 parser->non_integral_constant_expression_p
7411 = saved_non_integral_constant_expression_p;
7412 }
7413
7414 if (args == NULL)
7415 {
7416 postfix_expression = error_mark_node;
7417 break;
7418 }
7419
7420 /* Function calls are not permitted in
7421 constant-expressions. */
7422 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7423 && cp_parser_non_integral_constant_expression (parser,
7424 NIC_FUNC_CALL))
7425 {
7426 postfix_expression = error_mark_node;
7427 release_tree_vector (args);
7428 break;
7429 }
7430
7431 koenig_p = false;
7432 if (idk == CP_ID_KIND_UNQUALIFIED
7433 || idk == CP_ID_KIND_TEMPLATE_ID)
7434 {
7435 if (identifier_p (postfix_expression)
7436 /* In C++20, we may need to perform ADL for a template
7437 name. */
7438 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7439 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7440 {
7441 if (!args->is_empty ())
7442 {
7443 koenig_p = true;
7444 if (!any_type_dependent_arguments_p (args))
7445 postfix_expression
7446 = perform_koenig_lookup (postfix_expression, args,
7447 complain);
7448 }
7449 else
7450 postfix_expression
7451 = unqualified_fn_lookup_error (postfix_expression);
7452 }
7453 /* We do not perform argument-dependent lookup if
7454 normal lookup finds a non-function, in accordance
7455 with the expected resolution of DR 218. */
7456 else if (!args->is_empty ()
7457 && is_overloaded_fn (postfix_expression))
7458 {
7459 /* Do not do argument dependent lookup if regular
7460 lookup finds a member function or a block-scope
7461 function declaration. [basic.lookup.argdep]/3 */
7462 bool do_adl_p = true;
7463 tree fns = get_fns (postfix_expression);
7464 for (lkp_iterator iter (fns); iter; ++iter)
7465 {
7466 tree fn = STRIP_TEMPLATE (*iter);
7467 if ((TREE_CODE (fn) == USING_DECL
7468 && DECL_DEPENDENT_P (fn))
7469 || DECL_FUNCTION_MEMBER_P (fn)
7470 || DECL_LOCAL_DECL_P (fn))
7471 {
7472 do_adl_p = false;
7473 break;
7474 }
7475 }
7476
7477 if (do_adl_p)
7478 {
7479 koenig_p = true;
7480 if (!any_type_dependent_arguments_p (args))
7481 postfix_expression
7482 = perform_koenig_lookup (postfix_expression, args,
7483 complain);
7484 }
7485 }
7486 }
7487
7488 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7489 {
7490 tree instance = TREE_OPERAND (postfix_expression, 0);
7491 tree fn = TREE_OPERAND (postfix_expression, 1);
7492
7493 if (processing_template_decl
7494 && (type_dependent_object_expression_p (instance)
7495 || (!BASELINK_P (fn)
7496 && TREE_CODE (fn) != FIELD_DECL)
7497 || type_dependent_expression_p (fn)
7498 || any_type_dependent_arguments_p (args)))
7499 {
7500 maybe_generic_this_capture (instance, fn);
7501 postfix_expression
7502 = build_min_nt_call_vec (postfix_expression, args);
7503 }
7504 else if (BASELINK_P (fn))
7505 {
7506 postfix_expression
7507 = (build_new_method_call
7508 (instance, fn, &args, NULL_TREE,
7509 (idk == CP_ID_KIND_QUALIFIED
7510 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7511 : LOOKUP_NORMAL),
7512 /*fn_p=*/NULL,
7513 complain));
7514 }
7515 else
7516 postfix_expression
7517 = finish_call_expr (postfix_expression, &args,
7518 /*disallow_virtual=*/false,
7519 /*koenig_p=*/false,
7520 complain);
7521 }
7522 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7523 || TREE_CODE (postfix_expression) == MEMBER_REF
7524 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7525 postfix_expression = (build_offset_ref_call_from_tree
7526 (postfix_expression, &args,
7527 complain));
7528 else if (idk == CP_ID_KIND_QUALIFIED)
7529 /* A call to a static class member, or a namespace-scope
7530 function. */
7531 postfix_expression
7532 = finish_call_expr (postfix_expression, &args,
7533 /*disallow_virtual=*/true,
7534 koenig_p,
7535 complain);
7536 else
7537 /* All other function calls. */
7538 postfix_expression
7539 = finish_call_expr (postfix_expression, &args,
7540 /*disallow_virtual=*/false,
7541 koenig_p,
7542 complain);
7543
7544 if (close_paren_loc != UNKNOWN_LOCATION)
7545 {
7546 location_t combined_loc = make_location (token->location,
7547 start_loc,
7548 close_paren_loc);
7549 postfix_expression.set_location (combined_loc);
7550 }
7551
7552 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7553 idk = CP_ID_KIND_NONE;
7554
7555 release_tree_vector (args);
7556 }
7557 break;
7558
7559 case CPP_DOT:
7560 case CPP_DEREF:
7561 /* postfix-expression . template [opt] id-expression
7562 postfix-expression . pseudo-destructor-name
7563 postfix-expression -> template [opt] id-expression
7564 postfix-expression -> pseudo-destructor-name */
7565
7566 /* Consume the `.' or `->' operator. */
7567 cp_lexer_consume_token (parser->lexer);
7568
7569 postfix_expression
7570 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7571 postfix_expression,
7572 false, &idk, loc);
7573
7574 is_member_access = true;
7575 break;
7576
7577 case CPP_PLUS_PLUS:
7578 /* postfix-expression ++ */
7579 /* Consume the `++' token. */
7580 cp_lexer_consume_token (parser->lexer);
7581 /* Generate a representation for the complete expression. */
7582 postfix_expression
7583 = finish_increment_expr (postfix_expression,
7584 POSTINCREMENT_EXPR);
7585 /* Increments may not appear in constant-expressions. */
7586 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7587 postfix_expression = error_mark_node;
7588 idk = CP_ID_KIND_NONE;
7589 is_member_access = false;
7590 break;
7591
7592 case CPP_MINUS_MINUS:
7593 /* postfix-expression -- */
7594 /* Consume the `--' token. */
7595 cp_lexer_consume_token (parser->lexer);
7596 /* Generate a representation for the complete expression. */
7597 postfix_expression
7598 = finish_increment_expr (postfix_expression,
7599 POSTDECREMENT_EXPR);
7600 /* Decrements may not appear in constant-expressions. */
7601 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7602 postfix_expression = error_mark_node;
7603 idk = CP_ID_KIND_NONE;
7604 is_member_access = false;
7605 break;
7606
7607 default:
7608 if (pidk_return != NULL)
7609 * pidk_return = idk;
7610 if (member_access_only_p)
7611 return is_member_access
7612 ? postfix_expression
7613 : cp_expr (error_mark_node);
7614 else
7615 return postfix_expression;
7616 }
7617 }
7618
7619 /* We should never get here. */
7620 gcc_unreachable ();
7621 return error_mark_node;
7622 }
7623
7624 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7625 by cp_parser_builtin_offsetof. We're looking for
7626
7627 postfix-expression [ expression ]
7628 postfix-expression [ braced-init-list ] (C++11)
7629
7630 FOR_OFFSETOF is set if we're being called in that context, which
7631 changes how we deal with integer constant expressions. */
7632
7633 static tree
7634 cp_parser_postfix_open_square_expression (cp_parser *parser,
7635 tree postfix_expression,
7636 bool for_offsetof,
7637 bool decltype_p)
7638 {
7639 tree index = NULL_TREE;
7640 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7641 bool saved_greater_than_is_operator_p;
7642
7643 /* Consume the `[' token. */
7644 cp_lexer_consume_token (parser->lexer);
7645
7646 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7647 parser->greater_than_is_operator_p = true;
7648
7649 /* Parse the index expression. */
7650 /* ??? For offsetof, there is a question of what to allow here. If
7651 offsetof is not being used in an integral constant expression context,
7652 then we *could* get the right answer by computing the value at runtime.
7653 If we are in an integral constant expression context, then we might
7654 could accept any constant expression; hard to say without analysis.
7655 Rather than open the barn door too wide right away, allow only integer
7656 constant expressions here. */
7657 if (for_offsetof)
7658 index = cp_parser_constant_expression (parser);
7659 else
7660 {
7661 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7662 {
7663 bool expr_nonconst_p;
7664 cp_lexer_set_source_position (parser->lexer);
7665 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7666 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7667 }
7668 else
7669 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
7670 /*decltype_p=*/false,
7671 /*warn_comma_p=*/warn_comma_subscript);
7672 }
7673
7674 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7675
7676 /* Look for the closing `]'. */
7677 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7678
7679 /* Build the ARRAY_REF. */
7680 postfix_expression = grok_array_decl (loc, postfix_expression,
7681 index, decltype_p);
7682
7683 /* When not doing offsetof, array references are not permitted in
7684 constant-expressions. */
7685 if (!for_offsetof
7686 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7687 postfix_expression = error_mark_node;
7688
7689 return postfix_expression;
7690 }
7691
7692 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7693 dereference of incomplete type, returns true if error_mark_node should
7694 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7695 and *DEPENDENT_P. */
7696
7697 bool
7698 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7699 bool *dependent_p)
7700 {
7701 /* In a template, be permissive by treating an object expression
7702 of incomplete type as dependent (after a pedwarn). */
7703 diagnostic_t kind = (processing_template_decl
7704 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7705
7706 switch (TREE_CODE (*postfix_expression))
7707 {
7708 case CAST_EXPR:
7709 case REINTERPRET_CAST_EXPR:
7710 case CONST_CAST_EXPR:
7711 case STATIC_CAST_EXPR:
7712 case DYNAMIC_CAST_EXPR:
7713 case IMPLICIT_CONV_EXPR:
7714 case VIEW_CONVERT_EXPR:
7715 case NON_LVALUE_EXPR:
7716 kind = DK_ERROR;
7717 break;
7718 case OVERLOAD:
7719 /* Don't emit any diagnostic for OVERLOADs. */
7720 kind = DK_IGNORED;
7721 break;
7722 default:
7723 /* Avoid clobbering e.g. DECLs. */
7724 if (!EXPR_P (*postfix_expression))
7725 kind = DK_ERROR;
7726 break;
7727 }
7728
7729 if (kind == DK_IGNORED)
7730 return false;
7731
7732 location_t exploc = location_of (*postfix_expression);
7733 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7734 if (!MAYBE_CLASS_TYPE_P (*scope))
7735 return true;
7736 if (kind == DK_ERROR)
7737 *scope = *postfix_expression = error_mark_node;
7738 else if (processing_template_decl)
7739 {
7740 *dependent_p = true;
7741 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7742 }
7743 return false;
7744 }
7745
7746 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7747 by cp_parser_builtin_offsetof. We're looking for
7748
7749 postfix-expression . template [opt] id-expression
7750 postfix-expression . pseudo-destructor-name
7751 postfix-expression -> template [opt] id-expression
7752 postfix-expression -> pseudo-destructor-name
7753
7754 FOR_OFFSETOF is set if we're being called in that context. That sorta
7755 limits what of the above we'll actually accept, but nevermind.
7756 TOKEN_TYPE is the "." or "->" token, which will already have been
7757 removed from the stream. */
7758
7759 static tree
7760 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7761 enum cpp_ttype token_type,
7762 cp_expr postfix_expression,
7763 bool for_offsetof, cp_id_kind *idk,
7764 location_t location)
7765 {
7766 tree name;
7767 bool dependent_p;
7768 bool pseudo_destructor_p;
7769 tree scope = NULL_TREE;
7770 location_t start_loc = postfix_expression.get_start ();
7771
7772 /* If this is a `->' operator, dereference the pointer. */
7773 if (token_type == CPP_DEREF)
7774 postfix_expression = build_x_arrow (location, postfix_expression,
7775 tf_warning_or_error);
7776 /* Check to see whether or not the expression is type-dependent and
7777 not the current instantiation. */
7778 dependent_p = type_dependent_object_expression_p (postfix_expression);
7779 /* The identifier following the `->' or `.' is not qualified. */
7780 parser->scope = NULL_TREE;
7781 parser->qualifying_scope = NULL_TREE;
7782 parser->object_scope = NULL_TREE;
7783 *idk = CP_ID_KIND_NONE;
7784
7785 /* Enter the scope corresponding to the type of the object
7786 given by the POSTFIX_EXPRESSION. */
7787 if (!dependent_p)
7788 {
7789 scope = TREE_TYPE (postfix_expression);
7790 /* According to the standard, no expression should ever have
7791 reference type. Unfortunately, we do not currently match
7792 the standard in this respect in that our internal representation
7793 of an expression may have reference type even when the standard
7794 says it does not. Therefore, we have to manually obtain the
7795 underlying type here. */
7796 scope = non_reference (scope);
7797 /* The type of the POSTFIX_EXPRESSION must be complete. */
7798 /* Unlike the object expression in other contexts, *this is not
7799 required to be of complete type for purposes of class member
7800 access (5.2.5) outside the member function body. */
7801 if (postfix_expression != current_class_ref
7802 && scope != error_mark_node
7803 && !currently_open_class (scope))
7804 {
7805 scope = complete_type (scope);
7806 if (!COMPLETE_TYPE_P (scope)
7807 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7808 &dependent_p))
7809 return error_mark_node;
7810 }
7811
7812 if (!dependent_p)
7813 {
7814 /* Let the name lookup machinery know that we are processing a
7815 class member access expression. */
7816 parser->context->object_type = scope;
7817 /* If something went wrong, we want to be able to discern that case,
7818 as opposed to the case where there was no SCOPE due to the type
7819 of expression being dependent. */
7820 if (!scope)
7821 scope = error_mark_node;
7822 /* If the SCOPE was erroneous, make the various semantic analysis
7823 functions exit quickly -- and without issuing additional error
7824 messages. */
7825 if (scope == error_mark_node)
7826 postfix_expression = error_mark_node;
7827 }
7828 }
7829
7830 if (dependent_p)
7831 {
7832 tree type = TREE_TYPE (postfix_expression);
7833 /* If we don't have a (type-dependent) object of class type, use
7834 typeof to figure out the type of the object. */
7835 if (type == NULL_TREE)
7836 type = finish_typeof (postfix_expression);
7837 parser->context->object_type = type;
7838 }
7839
7840 /* Assume this expression is not a pseudo-destructor access. */
7841 pseudo_destructor_p = false;
7842
7843 /* If the SCOPE is a scalar type, then, if this is a valid program,
7844 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7845 is type dependent, it can be pseudo-destructor-name or something else.
7846 Try to parse it as pseudo-destructor-name first. */
7847 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7848 {
7849 tree s;
7850 tree type;
7851
7852 cp_parser_parse_tentatively (parser);
7853 /* Parse the pseudo-destructor-name. */
7854 s = NULL_TREE;
7855 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7856 &s, &type);
7857 if (dependent_p
7858 && (cp_parser_error_occurred (parser)
7859 || !SCALAR_TYPE_P (type)))
7860 cp_parser_abort_tentative_parse (parser);
7861 else if (cp_parser_parse_definitely (parser))
7862 {
7863 pseudo_destructor_p = true;
7864 postfix_expression
7865 = finish_pseudo_destructor_expr (postfix_expression,
7866 s, type, location);
7867 }
7868 }
7869
7870 if (!pseudo_destructor_p)
7871 {
7872 /* If the SCOPE is not a scalar type, we are looking at an
7873 ordinary class member access expression, rather than a
7874 pseudo-destructor-name. */
7875 bool template_p;
7876 cp_token *token = cp_lexer_peek_token (parser->lexer);
7877 /* Parse the id-expression. */
7878 name = (cp_parser_id_expression
7879 (parser,
7880 cp_parser_optional_template_keyword (parser),
7881 /*check_dependency_p=*/true,
7882 &template_p,
7883 /*declarator_p=*/false,
7884 /*optional_p=*/false));
7885 /* In general, build a SCOPE_REF if the member name is qualified.
7886 However, if the name was not dependent and has already been
7887 resolved; there is no need to build the SCOPE_REF. For example;
7888
7889 struct X { void f(); };
7890 template <typename T> void f(T* t) { t->X::f(); }
7891
7892 Even though "t" is dependent, "X::f" is not and has been resolved
7893 to a BASELINK; there is no need to include scope information. */
7894
7895 /* But we do need to remember that there was an explicit scope for
7896 virtual function calls. */
7897 if (parser->scope)
7898 *idk = CP_ID_KIND_QUALIFIED;
7899
7900 /* If the name is a template-id that names a type, we will get a
7901 TYPE_DECL here. That is invalid code. */
7902 if (TREE_CODE (name) == TYPE_DECL)
7903 {
7904 error_at (token->location, "invalid use of %qD", name);
7905 postfix_expression = error_mark_node;
7906 }
7907 else
7908 {
7909 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7910 {
7911 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7912 {
7913 error_at (token->location, "%<%D::%D%> is not a class member",
7914 parser->scope, name);
7915 postfix_expression = error_mark_node;
7916 }
7917 else
7918 name = build_qualified_name (/*type=*/NULL_TREE,
7919 parser->scope,
7920 name,
7921 template_p);
7922 parser->scope = NULL_TREE;
7923 parser->qualifying_scope = NULL_TREE;
7924 parser->object_scope = NULL_TREE;
7925 }
7926 if (parser->scope && name && BASELINK_P (name))
7927 adjust_result_of_qualified_name_lookup
7928 (name, parser->scope, scope);
7929 postfix_expression
7930 = finish_class_member_access_expr (postfix_expression, name,
7931 template_p,
7932 tf_warning_or_error);
7933 /* Build a location e.g.:
7934 ptr->access_expr
7935 ~~~^~~~~~~~~~~~~
7936 where the caret is at the deref token, ranging from
7937 the start of postfix_expression to the end of the access expr. */
7938 location_t combined_loc
7939 = make_location (input_location, start_loc, parser->lexer);
7940 protected_set_expr_location (postfix_expression, combined_loc);
7941 }
7942 }
7943
7944 /* We no longer need to look up names in the scope of the object on
7945 the left-hand side of the `.' or `->' operator. */
7946 parser->context->object_type = NULL_TREE;
7947
7948 /* Outside of offsetof, these operators may not appear in
7949 constant-expressions. */
7950 if (!for_offsetof
7951 && (cp_parser_non_integral_constant_expression
7952 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7953 postfix_expression = error_mark_node;
7954
7955 return postfix_expression;
7956 }
7957
7958 /* Parse a parenthesized expression-list.
7959
7960 expression-list:
7961 assignment-expression
7962 expression-list, assignment-expression
7963
7964 attribute-list:
7965 expression-list
7966 identifier
7967 identifier, expression-list
7968
7969 CAST_P is true if this expression is the target of a cast.
7970
7971 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7972 argument pack.
7973
7974 WRAP_LOCATIONS_P is true if expressions within this list for which
7975 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7976 their source locations.
7977
7978 Returns a vector of trees. Each element is a representation of an
7979 assignment-expression. NULL is returned if the ( and or ) are
7980 missing. An empty, but allocated, vector is returned on no
7981 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7982 if we are parsing an attribute list for an attribute that wants a
7983 plain identifier argument, normal_attr for an attribute that wants
7984 an expression, or non_attr if we aren't parsing an attribute list. If
7985 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7986 not all of the expressions in the list were constant.
7987 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7988 will be written to with the location of the closing parenthesis. If
7989 an error occurs, it may or may not be written to. */
7990
7991 static vec<tree, va_gc> *
7992 cp_parser_parenthesized_expression_list (cp_parser* parser,
7993 int is_attribute_list,
7994 bool cast_p,
7995 bool allow_expansion_p,
7996 bool *non_constant_p,
7997 location_t *close_paren_loc,
7998 bool wrap_locations_p)
7999 {
8000 vec<tree, va_gc> *expression_list;
8001 bool fold_expr_p = is_attribute_list != non_attr;
8002 tree identifier = NULL_TREE;
8003 bool saved_greater_than_is_operator_p;
8004
8005 /* Assume all the expressions will be constant. */
8006 if (non_constant_p)
8007 *non_constant_p = false;
8008
8009 matching_parens parens;
8010 if (!parens.require_open (parser))
8011 return NULL;
8012
8013 expression_list = make_tree_vector ();
8014
8015 /* Within a parenthesized expression, a `>' token is always
8016 the greater-than operator. */
8017 saved_greater_than_is_operator_p
8018 = parser->greater_than_is_operator_p;
8019 parser->greater_than_is_operator_p = true;
8020
8021 cp_expr expr (NULL_TREE);
8022
8023 /* Consume expressions until there are no more. */
8024 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8025 while (true)
8026 {
8027 /* At the beginning of attribute lists, check to see if the
8028 next token is an identifier. */
8029 if (is_attribute_list == id_attr
8030 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8031 {
8032 cp_token *token;
8033
8034 /* Consume the identifier. */
8035 token = cp_lexer_consume_token (parser->lexer);
8036 /* Save the identifier. */
8037 identifier = token->u.value;
8038 }
8039 else
8040 {
8041 bool expr_non_constant_p;
8042
8043 /* Parse the next assignment-expression. */
8044 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8045 {
8046 /* A braced-init-list. */
8047 cp_lexer_set_source_position (parser->lexer);
8048 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8049 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8050 if (non_constant_p && expr_non_constant_p)
8051 *non_constant_p = true;
8052 }
8053 else if (non_constant_p)
8054 {
8055 expr = (cp_parser_constant_expression
8056 (parser, /*allow_non_constant_p=*/true,
8057 &expr_non_constant_p));
8058 if (expr_non_constant_p)
8059 *non_constant_p = true;
8060 }
8061 else
8062 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
8063 cast_p);
8064
8065 if (fold_expr_p)
8066 expr = instantiate_non_dependent_expr (expr);
8067
8068 /* If we have an ellipsis, then this is an expression
8069 expansion. */
8070 if (allow_expansion_p
8071 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8072 {
8073 /* Consume the `...'. */
8074 cp_lexer_consume_token (parser->lexer);
8075
8076 /* Build the argument pack. */
8077 expr = make_pack_expansion (expr);
8078 }
8079
8080 if (wrap_locations_p)
8081 expr.maybe_add_location_wrapper ();
8082
8083 /* Add it to the list. We add error_mark_node
8084 expressions to the list, so that we can still tell if
8085 the correct form for a parenthesized expression-list
8086 is found. That gives better errors. */
8087 vec_safe_push (expression_list, expr.get_value ());
8088
8089 if (expr == error_mark_node)
8090 goto skip_comma;
8091 }
8092
8093 /* After the first item, attribute lists look the same as
8094 expression lists. */
8095 is_attribute_list = non_attr;
8096
8097 get_comma:;
8098 /* If the next token isn't a `,', then we are done. */
8099 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8100 break;
8101
8102 /* Otherwise, consume the `,' and keep going. */
8103 cp_lexer_consume_token (parser->lexer);
8104 }
8105
8106 if (close_paren_loc)
8107 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8108
8109 if (!parens.require_close (parser))
8110 {
8111 int ending;
8112
8113 skip_comma:;
8114 /* We try and resync to an unnested comma, as that will give the
8115 user better diagnostics. */
8116 ending = cp_parser_skip_to_closing_parenthesis (parser,
8117 /*recovering=*/true,
8118 /*or_comma=*/true,
8119 /*consume_paren=*/true);
8120 if (ending < 0)
8121 goto get_comma;
8122 if (!ending)
8123 {
8124 parser->greater_than_is_operator_p
8125 = saved_greater_than_is_operator_p;
8126 return NULL;
8127 }
8128 }
8129
8130 parser->greater_than_is_operator_p
8131 = saved_greater_than_is_operator_p;
8132
8133 if (identifier)
8134 vec_safe_insert (expression_list, 0, identifier);
8135
8136 return expression_list;
8137 }
8138
8139 /* Parse a pseudo-destructor-name.
8140
8141 pseudo-destructor-name:
8142 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8143 :: [opt] nested-name-specifier template template-id :: ~ type-name
8144 :: [opt] nested-name-specifier [opt] ~ type-name
8145
8146 If either of the first two productions is used, sets *SCOPE to the
8147 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8148 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8149 or ERROR_MARK_NODE if the parse fails. */
8150
8151 static void
8152 cp_parser_pseudo_destructor_name (cp_parser* parser,
8153 tree object,
8154 tree* scope,
8155 tree* type)
8156 {
8157 bool nested_name_specifier_p;
8158
8159 /* Handle ~auto. */
8160 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8161 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8162 && !type_dependent_expression_p (object))
8163 {
8164 if (cxx_dialect < cxx14)
8165 pedwarn (input_location, 0,
8166 "%<~auto%> only available with "
8167 "%<-std=c++14%> or %<-std=gnu++14%>");
8168 cp_lexer_consume_token (parser->lexer);
8169 cp_lexer_consume_token (parser->lexer);
8170 *scope = NULL_TREE;
8171 *type = TREE_TYPE (object);
8172 return;
8173 }
8174
8175 /* Assume that things will not work out. */
8176 *type = error_mark_node;
8177
8178 /* Look for the optional `::' operator. */
8179 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8180 /* Look for the optional nested-name-specifier. */
8181 nested_name_specifier_p
8182 = (cp_parser_nested_name_specifier_opt (parser,
8183 /*typename_keyword_p=*/false,
8184 /*check_dependency_p=*/true,
8185 /*type_p=*/false,
8186 /*is_declaration=*/false)
8187 != NULL_TREE);
8188 /* Now, if we saw a nested-name-specifier, we might be doing the
8189 second production. */
8190 if (nested_name_specifier_p
8191 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8192 {
8193 /* Consume the `template' keyword. */
8194 cp_lexer_consume_token (parser->lexer);
8195 /* Parse the template-id. */
8196 cp_parser_template_id (parser,
8197 /*template_keyword_p=*/true,
8198 /*check_dependency_p=*/false,
8199 class_type,
8200 /*is_declaration=*/true);
8201 /* Look for the `::' token. */
8202 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8203 }
8204 /* If the next token is not a `~', then there might be some
8205 additional qualification. */
8206 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8207 {
8208 /* At this point, we're looking for "type-name :: ~". The type-name
8209 must not be a class-name, since this is a pseudo-destructor. So,
8210 it must be either an enum-name, or a typedef-name -- both of which
8211 are just identifiers. So, we peek ahead to check that the "::"
8212 and "~" tokens are present; if they are not, then we can avoid
8213 calling type_name. */
8214 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8215 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8216 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8217 {
8218 cp_parser_error (parser, "non-scalar type");
8219 return;
8220 }
8221
8222 /* Look for the type-name. */
8223 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8224 if (*scope == error_mark_node)
8225 return;
8226
8227 /* Look for the `::' token. */
8228 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8229 }
8230 else
8231 *scope = NULL_TREE;
8232
8233 /* Look for the `~'. */
8234 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8235
8236 /* Once we see the ~, this has to be a pseudo-destructor. */
8237 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8238 cp_parser_commit_to_topmost_tentative_parse (parser);
8239
8240 /* Look for the type-name again. We are not responsible for
8241 checking that it matches the first type-name. */
8242 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8243 }
8244
8245 /* Parse a unary-expression.
8246
8247 unary-expression:
8248 postfix-expression
8249 ++ cast-expression
8250 -- cast-expression
8251 await-expression
8252 unary-operator cast-expression
8253 sizeof unary-expression
8254 sizeof ( type-id )
8255 alignof ( type-id ) [C++0x]
8256 new-expression
8257 delete-expression
8258
8259 GNU Extensions:
8260
8261 unary-expression:
8262 __extension__ cast-expression
8263 __alignof__ unary-expression
8264 __alignof__ ( type-id )
8265 alignof unary-expression [C++0x]
8266 __real__ cast-expression
8267 __imag__ cast-expression
8268 && identifier
8269 sizeof ( type-id ) { initializer-list , [opt] }
8270 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8271 __alignof__ ( type-id ) { initializer-list , [opt] }
8272
8273 ADDRESS_P is true iff the unary-expression is appearing as the
8274 operand of the `&' operator. CAST_P is true if this expression is
8275 the target of a cast.
8276
8277 Returns a representation of the expression. */
8278
8279 static cp_expr
8280 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8281 bool address_p, bool cast_p, bool decltype_p)
8282 {
8283 cp_token *token;
8284 enum tree_code unary_operator;
8285
8286 /* Peek at the next token. */
8287 token = cp_lexer_peek_token (parser->lexer);
8288 /* Some keywords give away the kind of expression. */
8289 if (token->type == CPP_KEYWORD)
8290 {
8291 enum rid keyword = token->keyword;
8292
8293 switch (keyword)
8294 {
8295 case RID_ALIGNOF:
8296 case RID_SIZEOF:
8297 {
8298 tree operand, ret;
8299 enum tree_code op;
8300 location_t start_loc = token->location;
8301
8302 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8303 bool std_alignof = id_equal (token->u.value, "alignof");
8304
8305 /* Consume the token. */
8306 cp_lexer_consume_token (parser->lexer);
8307 /* Parse the operand. */
8308 operand = cp_parser_sizeof_operand (parser, keyword);
8309
8310 /* Construct a location e.g. :
8311 alignof (expr)
8312 ^~~~~~~~~~~~~~
8313 with start == caret at the start of the "alignof"/"sizeof"
8314 token, with the endpoint at the final closing paren. */
8315 location_t compound_loc
8316 = make_location (start_loc, start_loc, parser->lexer);
8317
8318 if (TYPE_P (operand))
8319 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
8320 std_alignof, true);
8321 else
8322 {
8323 /* ISO C++ defines alignof only with types, not with
8324 expressions. So pedwarn if alignof is used with a non-
8325 type expression. However, __alignof__ is ok. */
8326 if (std_alignof)
8327 pedwarn (token->location, OPT_Wpedantic,
8328 "ISO C++ does not allow %<alignof%> "
8329 "with a non-type");
8330
8331 ret = cxx_sizeof_or_alignof_expr (compound_loc,
8332 operand, op, true);
8333 }
8334 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8335 SIZEOF_EXPR with the original operand. */
8336 if (op == SIZEOF_EXPR && ret != error_mark_node)
8337 {
8338 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8339 {
8340 if (!processing_template_decl && TYPE_P (operand))
8341 {
8342 ret = build_min (SIZEOF_EXPR, size_type_node,
8343 build1 (NOP_EXPR, operand,
8344 error_mark_node));
8345 SIZEOF_EXPR_TYPE_P (ret) = 1;
8346 }
8347 else
8348 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8349 TREE_SIDE_EFFECTS (ret) = 0;
8350 TREE_READONLY (ret) = 1;
8351 SET_EXPR_LOCATION (ret, compound_loc);
8352 }
8353 }
8354
8355 cp_expr ret_expr (ret, compound_loc);
8356 ret_expr = ret_expr.maybe_add_location_wrapper ();
8357 return ret_expr;
8358 }
8359
8360 case RID_BUILTIN_HAS_ATTRIBUTE:
8361 return cp_parser_has_attribute_expression (parser);
8362
8363 case RID_NEW:
8364 return cp_parser_new_expression (parser);
8365
8366 case RID_DELETE:
8367 return cp_parser_delete_expression (parser);
8368
8369 case RID_EXTENSION:
8370 {
8371 /* The saved value of the PEDANTIC flag. */
8372 int saved_pedantic;
8373 tree expr;
8374
8375 /* Save away the PEDANTIC flag. */
8376 cp_parser_extension_opt (parser, &saved_pedantic);
8377 /* Parse the cast-expression. */
8378 expr = cp_parser_simple_cast_expression (parser);
8379 /* Restore the PEDANTIC flag. */
8380 pedantic = saved_pedantic;
8381
8382 return expr;
8383 }
8384
8385 case RID_REALPART:
8386 case RID_IMAGPART:
8387 {
8388 tree expression;
8389
8390 /* Consume the `__real__' or `__imag__' token. */
8391 cp_lexer_consume_token (parser->lexer);
8392 /* Parse the cast-expression. */
8393 expression = cp_parser_simple_cast_expression (parser);
8394 /* Create the complete representation. */
8395 return build_x_unary_op (token->location,
8396 (keyword == RID_REALPART
8397 ? REALPART_EXPR : IMAGPART_EXPR),
8398 expression,
8399 tf_warning_or_error);
8400 }
8401 break;
8402
8403 case RID_TRANSACTION_ATOMIC:
8404 case RID_TRANSACTION_RELAXED:
8405 return cp_parser_transaction_expression (parser, keyword);
8406
8407 case RID_NOEXCEPT:
8408 {
8409 tree expr;
8410 const char *saved_message;
8411 bool saved_integral_constant_expression_p;
8412 bool saved_non_integral_constant_expression_p;
8413 bool saved_greater_than_is_operator_p;
8414
8415 location_t start_loc = token->location;
8416
8417 cp_lexer_consume_token (parser->lexer);
8418 matching_parens parens;
8419 parens.require_open (parser);
8420
8421 saved_message = parser->type_definition_forbidden_message;
8422 parser->type_definition_forbidden_message
8423 = G_("types may not be defined in %<noexcept%> expressions");
8424
8425 saved_integral_constant_expression_p
8426 = parser->integral_constant_expression_p;
8427 saved_non_integral_constant_expression_p
8428 = parser->non_integral_constant_expression_p;
8429 parser->integral_constant_expression_p = false;
8430
8431 saved_greater_than_is_operator_p
8432 = parser->greater_than_is_operator_p;
8433 parser->greater_than_is_operator_p = true;
8434
8435 ++cp_unevaluated_operand;
8436 ++c_inhibit_evaluation_warnings;
8437 ++cp_noexcept_operand;
8438 expr = cp_parser_expression (parser);
8439 --cp_noexcept_operand;
8440 --c_inhibit_evaluation_warnings;
8441 --cp_unevaluated_operand;
8442
8443 parser->greater_than_is_operator_p
8444 = saved_greater_than_is_operator_p;
8445
8446 parser->integral_constant_expression_p
8447 = saved_integral_constant_expression_p;
8448 parser->non_integral_constant_expression_p
8449 = saved_non_integral_constant_expression_p;
8450
8451 parser->type_definition_forbidden_message = saved_message;
8452
8453 parens.require_close (parser);
8454
8455 /* Construct a location of the form:
8456 noexcept (expr)
8457 ^~~~~~~~~~~~~~~
8458 with start == caret, finishing at the close-paren. */
8459 location_t noexcept_loc
8460 = make_location (start_loc, start_loc, parser->lexer);
8461
8462 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8463 noexcept_loc);
8464 }
8465
8466 case RID_CO_AWAIT:
8467 {
8468 tree expr;
8469 location_t kw_loc = token->location;
8470
8471 /* Consume the `co_await' token. */
8472 cp_lexer_consume_token (parser->lexer);
8473 /* Parse its cast-expression. */
8474 expr = cp_parser_simple_cast_expression (parser);
8475 if (expr == error_mark_node)
8476 return error_mark_node;
8477
8478 /* Handle [expr.await]. */
8479 return cp_expr (finish_co_await_expr (kw_loc, expr));
8480 }
8481
8482 default:
8483 break;
8484 }
8485 }
8486
8487 /* Look for the `:: new' and `:: delete', which also signal the
8488 beginning of a new-expression, or delete-expression,
8489 respectively. If the next token is `::', then it might be one of
8490 these. */
8491 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8492 {
8493 enum rid keyword;
8494
8495 /* See if the token after the `::' is one of the keywords in
8496 which we're interested. */
8497 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8498 /* If it's `new', we have a new-expression. */
8499 if (keyword == RID_NEW)
8500 return cp_parser_new_expression (parser);
8501 /* Similarly, for `delete'. */
8502 else if (keyword == RID_DELETE)
8503 return cp_parser_delete_expression (parser);
8504 }
8505
8506 /* Look for a unary operator. */
8507 unary_operator = cp_parser_unary_operator (token);
8508 /* The `++' and `--' operators can be handled similarly, even though
8509 they are not technically unary-operators in the grammar. */
8510 if (unary_operator == ERROR_MARK)
8511 {
8512 if (token->type == CPP_PLUS_PLUS)
8513 unary_operator = PREINCREMENT_EXPR;
8514 else if (token->type == CPP_MINUS_MINUS)
8515 unary_operator = PREDECREMENT_EXPR;
8516 /* Handle the GNU address-of-label extension. */
8517 else if (cp_parser_allow_gnu_extensions_p (parser)
8518 && token->type == CPP_AND_AND)
8519 {
8520 tree identifier;
8521 tree expression;
8522 location_t start_loc = token->location;
8523
8524 /* Consume the '&&' token. */
8525 cp_lexer_consume_token (parser->lexer);
8526 /* Look for the identifier. */
8527 identifier = cp_parser_identifier (parser);
8528 /* Construct a location of the form:
8529 &&label
8530 ^~~~~~~
8531 with caret==start at the "&&", finish at the end of the label. */
8532 location_t combined_loc
8533 = make_location (start_loc, start_loc, parser->lexer);
8534 /* Create an expression representing the address. */
8535 expression = finish_label_address_expr (identifier, combined_loc);
8536 if (cp_parser_non_integral_constant_expression (parser,
8537 NIC_ADDR_LABEL))
8538 expression = error_mark_node;
8539 return expression;
8540 }
8541 }
8542 if (unary_operator != ERROR_MARK)
8543 {
8544 cp_expr cast_expression;
8545 cp_expr expression = error_mark_node;
8546 non_integral_constant non_constant_p = NIC_NONE;
8547 location_t loc = token->location;
8548 tsubst_flags_t complain = complain_flags (decltype_p);
8549
8550 /* Consume the operator token. */
8551 token = cp_lexer_consume_token (parser->lexer);
8552 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8553
8554 /* Parse the cast-expression. */
8555 cast_expression
8556 = cp_parser_cast_expression (parser,
8557 unary_operator == ADDR_EXPR,
8558 /*cast_p=*/false,
8559 /*decltype*/false,
8560 pidk);
8561
8562 /* Make a location:
8563 OP_TOKEN CAST_EXPRESSION
8564 ^~~~~~~~~~~~~~~~~~~~~~~~~
8565 with start==caret at the operator token, and
8566 extending to the end of the cast_expression. */
8567 loc = make_location (loc, loc, cast_expression.get_finish ());
8568
8569 /* Now, build an appropriate representation. */
8570 switch (unary_operator)
8571 {
8572 case INDIRECT_REF:
8573 non_constant_p = NIC_STAR;
8574 expression = build_x_indirect_ref (loc, cast_expression,
8575 RO_UNARY_STAR,
8576 complain);
8577 /* TODO: build_x_indirect_ref does not always honor the
8578 location, so ensure it is set. */
8579 expression.set_location (loc);
8580 break;
8581
8582 case ADDR_EXPR:
8583 non_constant_p = NIC_ADDR;
8584 /* Fall through. */
8585 case BIT_NOT_EXPR:
8586 expression = build_x_unary_op (loc, unary_operator,
8587 cast_expression,
8588 complain);
8589 /* TODO: build_x_unary_op does not always honor the location,
8590 so ensure it is set. */
8591 expression.set_location (loc);
8592 break;
8593
8594 case PREINCREMENT_EXPR:
8595 case PREDECREMENT_EXPR:
8596 non_constant_p = unary_operator == PREINCREMENT_EXPR
8597 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8598 /* Fall through. */
8599 case NEGATE_EXPR:
8600 /* Immediately fold negation of a constant, unless the constant is 0
8601 (since -0 == 0) or it would overflow. */
8602 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8603 {
8604 tree stripped_expr
8605 = tree_strip_any_location_wrapper (cast_expression);
8606 if (CONSTANT_CLASS_P (stripped_expr)
8607 && !integer_zerop (stripped_expr)
8608 && !TREE_OVERFLOW (stripped_expr))
8609 {
8610 tree folded = fold_build1 (unary_operator,
8611 TREE_TYPE (stripped_expr),
8612 stripped_expr);
8613 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8614 {
8615 expression = maybe_wrap_with_location (folded, loc);
8616 break;
8617 }
8618 }
8619 }
8620 /* Fall through. */
8621 case UNARY_PLUS_EXPR:
8622 case TRUTH_NOT_EXPR:
8623 expression = finish_unary_op_expr (loc, unary_operator,
8624 cast_expression, complain);
8625 break;
8626
8627 default:
8628 gcc_unreachable ();
8629 }
8630
8631 if (non_constant_p != NIC_NONE
8632 && cp_parser_non_integral_constant_expression (parser,
8633 non_constant_p))
8634 expression = error_mark_node;
8635
8636 return expression;
8637 }
8638
8639 return cp_parser_postfix_expression (parser, address_p, cast_p,
8640 /*member_access_only_p=*/false,
8641 decltype_p,
8642 pidk);
8643 }
8644
8645 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8646 unary-operator, the corresponding tree code is returned. */
8647
8648 static enum tree_code
8649 cp_parser_unary_operator (cp_token* token)
8650 {
8651 switch (token->type)
8652 {
8653 case CPP_MULT:
8654 return INDIRECT_REF;
8655
8656 case CPP_AND:
8657 return ADDR_EXPR;
8658
8659 case CPP_PLUS:
8660 return UNARY_PLUS_EXPR;
8661
8662 case CPP_MINUS:
8663 return NEGATE_EXPR;
8664
8665 case CPP_NOT:
8666 return TRUTH_NOT_EXPR;
8667
8668 case CPP_COMPL:
8669 return BIT_NOT_EXPR;
8670
8671 default:
8672 return ERROR_MARK;
8673 }
8674 }
8675
8676 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8677 Returns a representation of the expression. */
8678
8679 static tree
8680 cp_parser_has_attribute_expression (cp_parser *parser)
8681 {
8682 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8683
8684 /* Consume the __builtin_has_attribute token. */
8685 cp_lexer_consume_token (parser->lexer);
8686
8687 matching_parens parens;
8688 if (!parens.require_open (parser))
8689 return error_mark_node;
8690
8691 /* Types cannot be defined in a `sizeof' expression. Save away the
8692 old message. */
8693 const char *saved_message = parser->type_definition_forbidden_message;
8694 const char *saved_message_arg
8695 = parser->type_definition_forbidden_message_arg;
8696 parser->type_definition_forbidden_message
8697 = G_("types may not be defined in %qs expressions");
8698 parser->type_definition_forbidden_message_arg
8699 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
8700
8701 /* The restrictions on constant-expressions do not apply inside
8702 sizeof expressions. */
8703 bool saved_integral_constant_expression_p
8704 = parser->integral_constant_expression_p;
8705 bool saved_non_integral_constant_expression_p
8706 = parser->non_integral_constant_expression_p;
8707 parser->integral_constant_expression_p = false;
8708
8709 /* Do not actually evaluate the expression. */
8710 ++cp_unevaluated_operand;
8711 ++c_inhibit_evaluation_warnings;
8712
8713 tree oper = NULL_TREE;
8714
8715 /* We can't be sure yet whether we're looking at a type-id or an
8716 expression. */
8717 cp_parser_parse_tentatively (parser);
8718
8719 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8720 parser->in_type_id_in_expr_p = true;
8721 /* Look for the type-id. */
8722 oper = cp_parser_type_id (parser);
8723 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8724
8725 cp_parser_parse_definitely (parser);
8726
8727 /* If the type-id production did not work out, then we must be
8728 looking at an expression. */
8729 if (!oper || oper == error_mark_node)
8730 oper = cp_parser_assignment_expression (parser);
8731
8732 STRIP_ANY_LOCATION_WRAPPER (oper);
8733
8734 /* Go back to evaluating expressions. */
8735 --cp_unevaluated_operand;
8736 --c_inhibit_evaluation_warnings;
8737
8738 /* And restore the old one. */
8739 parser->type_definition_forbidden_message = saved_message;
8740 parser->type_definition_forbidden_message_arg = saved_message_arg;
8741 parser->integral_constant_expression_p
8742 = saved_integral_constant_expression_p;
8743 parser->non_integral_constant_expression_p
8744 = saved_non_integral_constant_expression_p;
8745
8746 /* Consume the comma if it's there. */
8747 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8748 {
8749 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8750 /*consume_paren=*/true);
8751 return error_mark_node;
8752 }
8753
8754 /* Parse the attribute specification. */
8755 bool ret = false;
8756 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8757 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8758 {
8759 if (oper == error_mark_node)
8760 /* Nothing. */;
8761 else if (type_dependent_expression_p (oper))
8762 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
8763 "not supported yet");
8764 else
8765 {
8766 /* Fold constant expressions used in attributes first. */
8767 cp_check_const_attributes (attr);
8768
8769 /* Finally, see if OPER has been declared with ATTR. */
8770 ret = has_attribute (atloc, oper, attr, default_conversion);
8771 }
8772
8773 parens.require_close (parser);
8774 }
8775 else
8776 {
8777 error_at (atloc, "expected identifier");
8778 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8779 }
8780
8781 /* Construct a location e.g. :
8782 __builtin_has_attribute (oper, attr)
8783 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8784 with start == caret at the start of the built-in token,
8785 and with the endpoint at the final closing paren. */
8786 location_t compound_loc
8787 = make_location (start_loc, start_loc, parser->lexer);
8788
8789 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8790 ret_expr.set_location (compound_loc);
8791 ret_expr = ret_expr.maybe_add_location_wrapper ();
8792 return ret_expr;
8793 }
8794
8795 /* Parse a new-expression.
8796
8797 new-expression:
8798 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8799 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8800
8801 Returns a representation of the expression. */
8802
8803 static tree
8804 cp_parser_new_expression (cp_parser* parser)
8805 {
8806 bool global_scope_p;
8807 vec<tree, va_gc> *placement;
8808 tree type;
8809 vec<tree, va_gc> *initializer;
8810 tree nelts = NULL_TREE;
8811 tree ret;
8812
8813 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8814
8815 /* Look for the optional `::' operator. */
8816 global_scope_p
8817 = (cp_parser_global_scope_opt (parser,
8818 /*current_scope_valid_p=*/false)
8819 != NULL_TREE);
8820 /* Look for the `new' operator. */
8821 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8822 /* There's no easy way to tell a new-placement from the
8823 `( type-id )' construct. */
8824 cp_parser_parse_tentatively (parser);
8825 /* Look for a new-placement. */
8826 placement = cp_parser_new_placement (parser);
8827 /* If that didn't work out, there's no new-placement. */
8828 if (!cp_parser_parse_definitely (parser))
8829 {
8830 if (placement != NULL)
8831 release_tree_vector (placement);
8832 placement = NULL;
8833 }
8834
8835 /* If the next token is a `(', then we have a parenthesized
8836 type-id. */
8837 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8838 {
8839 cp_token *token;
8840 const char *saved_message = parser->type_definition_forbidden_message;
8841
8842 /* Consume the `('. */
8843 matching_parens parens;
8844 parens.consume_open (parser);
8845
8846 /* Parse the type-id. */
8847 parser->type_definition_forbidden_message
8848 = G_("types may not be defined in a new-expression");
8849 {
8850 type_id_in_expr_sentinel s (parser);
8851 type = cp_parser_type_id (parser);
8852 }
8853 parser->type_definition_forbidden_message = saved_message;
8854
8855 /* Look for the closing `)'. */
8856 parens.require_close (parser);
8857 token = cp_lexer_peek_token (parser->lexer);
8858 /* There should not be a direct-new-declarator in this production,
8859 but GCC used to allowed this, so we check and emit a sensible error
8860 message for this case. */
8861 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8862 {
8863 error_at (token->location,
8864 "array bound forbidden after parenthesized type-id");
8865 inform (token->location,
8866 "try removing the parentheses around the type-id");
8867 cp_parser_direct_new_declarator (parser);
8868 }
8869 }
8870 /* Otherwise, there must be a new-type-id. */
8871 else
8872 type = cp_parser_new_type_id (parser, &nelts);
8873
8874 /* If the next token is a `(' or '{', then we have a new-initializer. */
8875 cp_token *token = cp_lexer_peek_token (parser->lexer);
8876 if (token->type == CPP_OPEN_PAREN
8877 || token->type == CPP_OPEN_BRACE)
8878 initializer = cp_parser_new_initializer (parser);
8879 else
8880 initializer = NULL;
8881
8882 /* A new-expression may not appear in an integral constant
8883 expression. */
8884 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8885 ret = error_mark_node;
8886 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8887 of a new-type-id or type-id of a new-expression, the new-expression shall
8888 contain a new-initializer of the form ( assignment-expression )".
8889 Additionally, consistently with the spirit of DR 1467, we want to accept
8890 'new auto { 2 }' too. */
8891 else if ((ret = type_uses_auto (type))
8892 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8893 && (vec_safe_length (initializer) != 1
8894 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8895 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8896 {
8897 error_at (token->location,
8898 "initialization of new-expression for type %<auto%> "
8899 "requires exactly one element");
8900 ret = error_mark_node;
8901 }
8902 else
8903 {
8904 /* Construct a location e.g.:
8905 ptr = new int[100]
8906 ^~~~~~~~~~~~
8907 with caret == start at the start of the "new" token, and the end
8908 at the end of the final token we consumed. */
8909 location_t combined_loc = make_location (start_loc, start_loc,
8910 parser->lexer);
8911 /* Create a representation of the new-expression. */
8912 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
8913 global_scope_p, tf_warning_or_error);
8914 }
8915
8916 if (placement != NULL)
8917 release_tree_vector (placement);
8918 if (initializer != NULL)
8919 release_tree_vector (initializer);
8920
8921 return ret;
8922 }
8923
8924 /* Parse a new-placement.
8925
8926 new-placement:
8927 ( expression-list )
8928
8929 Returns the same representation as for an expression-list. */
8930
8931 static vec<tree, va_gc> *
8932 cp_parser_new_placement (cp_parser* parser)
8933 {
8934 vec<tree, va_gc> *expression_list;
8935
8936 /* Parse the expression-list. */
8937 expression_list = (cp_parser_parenthesized_expression_list
8938 (parser, non_attr, /*cast_p=*/false,
8939 /*allow_expansion_p=*/true,
8940 /*non_constant_p=*/NULL));
8941
8942 if (expression_list && expression_list->is_empty ())
8943 error ("expected expression-list or type-id");
8944
8945 return expression_list;
8946 }
8947
8948 /* Parse a new-type-id.
8949
8950 new-type-id:
8951 type-specifier-seq new-declarator [opt]
8952
8953 Returns the TYPE allocated. If the new-type-id indicates an array
8954 type, *NELTS is set to the number of elements in the last array
8955 bound; the TYPE will not include the last array bound. */
8956
8957 static tree
8958 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8959 {
8960 cp_decl_specifier_seq type_specifier_seq;
8961 cp_declarator *new_declarator;
8962 cp_declarator *declarator;
8963 cp_declarator *outer_declarator;
8964 const char *saved_message;
8965
8966 /* The type-specifier sequence must not contain type definitions.
8967 (It cannot contain declarations of new types either, but if they
8968 are not definitions we will catch that because they are not
8969 complete.) */
8970 saved_message = parser->type_definition_forbidden_message;
8971 parser->type_definition_forbidden_message
8972 = G_("types may not be defined in a new-type-id");
8973 /* Parse the type-specifier-seq. */
8974 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
8975 /*is_declaration=*/false,
8976 /*is_trailing_return=*/false,
8977 &type_specifier_seq);
8978 /* Restore the old message. */
8979 parser->type_definition_forbidden_message = saved_message;
8980
8981 if (type_specifier_seq.type == error_mark_node)
8982 return error_mark_node;
8983
8984 /* Parse the new-declarator. */
8985 new_declarator = cp_parser_new_declarator_opt (parser);
8986
8987 /* Determine the number of elements in the last array dimension, if
8988 any. */
8989 *nelts = NULL_TREE;
8990 /* Skip down to the last array dimension. */
8991 declarator = new_declarator;
8992 outer_declarator = NULL;
8993 while (declarator && (declarator->kind == cdk_pointer
8994 || declarator->kind == cdk_ptrmem))
8995 {
8996 outer_declarator = declarator;
8997 declarator = declarator->declarator;
8998 }
8999 while (declarator
9000 && declarator->kind == cdk_array
9001 && declarator->declarator
9002 && declarator->declarator->kind == cdk_array)
9003 {
9004 outer_declarator = declarator;
9005 declarator = declarator->declarator;
9006 }
9007
9008 if (declarator && declarator->kind == cdk_array)
9009 {
9010 *nelts = declarator->u.array.bounds;
9011 if (*nelts == error_mark_node)
9012 *nelts = integer_one_node;
9013
9014 if (*nelts == NULL_TREE)
9015 /* Leave [] in the declarator. */;
9016 else if (outer_declarator)
9017 outer_declarator->declarator = declarator->declarator;
9018 else
9019 new_declarator = NULL;
9020 }
9021
9022 return groktypename (&type_specifier_seq, new_declarator, false);
9023 }
9024
9025 /* Parse an (optional) new-declarator.
9026
9027 new-declarator:
9028 ptr-operator new-declarator [opt]
9029 direct-new-declarator
9030
9031 Returns the declarator. */
9032
9033 static cp_declarator *
9034 cp_parser_new_declarator_opt (cp_parser* parser)
9035 {
9036 enum tree_code code;
9037 tree type, std_attributes = NULL_TREE;
9038 cp_cv_quals cv_quals;
9039
9040 /* We don't know if there's a ptr-operator next, or not. */
9041 cp_parser_parse_tentatively (parser);
9042 /* Look for a ptr-operator. */
9043 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9044 /* If that worked, look for more new-declarators. */
9045 if (cp_parser_parse_definitely (parser))
9046 {
9047 cp_declarator *declarator;
9048
9049 /* Parse another optional declarator. */
9050 declarator = cp_parser_new_declarator_opt (parser);
9051
9052 declarator = cp_parser_make_indirect_declarator
9053 (code, type, cv_quals, declarator, std_attributes);
9054
9055 return declarator;
9056 }
9057
9058 /* If the next token is a `[', there is a direct-new-declarator. */
9059 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9060 return cp_parser_direct_new_declarator (parser);
9061
9062 return NULL;
9063 }
9064
9065 /* Parse a direct-new-declarator.
9066
9067 direct-new-declarator:
9068 [ expression ]
9069 direct-new-declarator [constant-expression]
9070
9071 */
9072
9073 static cp_declarator *
9074 cp_parser_direct_new_declarator (cp_parser* parser)
9075 {
9076 cp_declarator *declarator = NULL;
9077 bool first_p = true;
9078
9079 while (true)
9080 {
9081 tree expression;
9082 cp_token *token;
9083
9084 /* Look for the opening `['. */
9085 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9086
9087 token = cp_lexer_peek_token (parser->lexer);
9088 if (token->type == CPP_CLOSE_SQUARE && first_p)
9089 expression = NULL_TREE;
9090 else
9091 expression = cp_parser_expression (parser);
9092 /* The standard requires that the expression have integral
9093 type. DR 74 adds enumeration types. We believe that the
9094 real intent is that these expressions be handled like the
9095 expression in a `switch' condition, which also allows
9096 classes with a single conversion to integral or
9097 enumeration type. */
9098 if (expression && !processing_template_decl)
9099 {
9100 expression
9101 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9102 expression,
9103 /*complain=*/true);
9104 if (!expression)
9105 {
9106 error_at (token->location,
9107 "expression in new-declarator must have integral "
9108 "or enumeration type");
9109 expression = error_mark_node;
9110 }
9111 }
9112
9113 /* Look for the closing `]'. */
9114 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9115
9116 /* Add this bound to the declarator. */
9117 declarator = make_array_declarator (declarator, expression);
9118
9119 /* If the next token is not a `[', then there are no more
9120 bounds. */
9121 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9122 break;
9123 first_p = false;
9124 }
9125
9126 return declarator;
9127 }
9128
9129 /* Parse a new-initializer.
9130
9131 new-initializer:
9132 ( expression-list [opt] )
9133 braced-init-list
9134
9135 Returns a representation of the expression-list. */
9136
9137 static vec<tree, va_gc> *
9138 cp_parser_new_initializer (cp_parser* parser)
9139 {
9140 vec<tree, va_gc> *expression_list;
9141
9142 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9143 {
9144 tree t;
9145 bool expr_non_constant_p;
9146 cp_lexer_set_source_position (parser->lexer);
9147 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9148 t = cp_parser_braced_list (parser, &expr_non_constant_p);
9149 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
9150 expression_list = make_tree_vector_single (t);
9151 }
9152 else
9153 expression_list = (cp_parser_parenthesized_expression_list
9154 (parser, non_attr, /*cast_p=*/false,
9155 /*allow_expansion_p=*/true,
9156 /*non_constant_p=*/NULL));
9157
9158 return expression_list;
9159 }
9160
9161 /* Parse a delete-expression.
9162
9163 delete-expression:
9164 :: [opt] delete cast-expression
9165 :: [opt] delete [ ] cast-expression
9166
9167 Returns a representation of the expression. */
9168
9169 static tree
9170 cp_parser_delete_expression (cp_parser* parser)
9171 {
9172 bool global_scope_p;
9173 bool array_p;
9174 tree expression;
9175 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9176
9177 /* Look for the optional `::' operator. */
9178 global_scope_p
9179 = (cp_parser_global_scope_opt (parser,
9180 /*current_scope_valid_p=*/false)
9181 != NULL_TREE);
9182 /* Look for the `delete' keyword. */
9183 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9184 /* See if the array syntax is in use. */
9185 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9186 {
9187 /* Consume the `[' token. */
9188 cp_lexer_consume_token (parser->lexer);
9189 /* Look for the `]' token. */
9190 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9191 /* Remember that this is the `[]' construct. */
9192 array_p = true;
9193 }
9194 else
9195 array_p = false;
9196
9197 /* Parse the cast-expression. */
9198 expression = cp_parser_simple_cast_expression (parser);
9199
9200 /* A delete-expression may not appear in an integral constant
9201 expression. */
9202 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9203 return error_mark_node;
9204
9205 /* Construct a location e.g.:
9206 delete [ ] ptr
9207 ^~~~~~~~~~~~~~
9208 with caret == start at the start of the "delete" token, and
9209 the end at the end of the final token we consumed. */
9210 location_t combined_loc = make_location (start_loc, start_loc,
9211 parser->lexer);
9212 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9213 global_scope_p, tf_warning_or_error);
9214
9215 return expression;
9216 }
9217
9218 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9219 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9220 0 otherwise. */
9221
9222 static int
9223 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9224 {
9225 cp_token *token = cp_lexer_peek_token (parser->lexer);
9226 switch (token->type)
9227 {
9228 case CPP_COMMA:
9229 case CPP_SEMICOLON:
9230 case CPP_QUERY:
9231 case CPP_COLON:
9232 case CPP_CLOSE_SQUARE:
9233 case CPP_CLOSE_PAREN:
9234 case CPP_CLOSE_BRACE:
9235 case CPP_OPEN_BRACE:
9236 case CPP_DOT:
9237 case CPP_DOT_STAR:
9238 case CPP_DEREF:
9239 case CPP_DEREF_STAR:
9240 case CPP_DIV:
9241 case CPP_MOD:
9242 case CPP_LSHIFT:
9243 case CPP_RSHIFT:
9244 case CPP_LESS:
9245 case CPP_GREATER:
9246 case CPP_LESS_EQ:
9247 case CPP_GREATER_EQ:
9248 case CPP_EQ_EQ:
9249 case CPP_NOT_EQ:
9250 case CPP_EQ:
9251 case CPP_MULT_EQ:
9252 case CPP_DIV_EQ:
9253 case CPP_MOD_EQ:
9254 case CPP_PLUS_EQ:
9255 case CPP_MINUS_EQ:
9256 case CPP_RSHIFT_EQ:
9257 case CPP_LSHIFT_EQ:
9258 case CPP_AND_EQ:
9259 case CPP_XOR_EQ:
9260 case CPP_OR_EQ:
9261 case CPP_XOR:
9262 case CPP_OR:
9263 case CPP_OR_OR:
9264 case CPP_EOF:
9265 case CPP_ELLIPSIS:
9266 return 0;
9267
9268 case CPP_OPEN_PAREN:
9269 /* In ((type ()) () the last () isn't a valid cast-expression,
9270 so the whole must be parsed as postfix-expression. */
9271 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9272 != CPP_CLOSE_PAREN;
9273
9274 case CPP_OPEN_SQUARE:
9275 /* '[' may start a primary-expression in obj-c++ and in C++11,
9276 as a lambda-expression, eg, '(void)[]{}'. */
9277 if (cxx_dialect >= cxx11)
9278 return -1;
9279 return c_dialect_objc ();
9280
9281 case CPP_PLUS_PLUS:
9282 case CPP_MINUS_MINUS:
9283 /* '++' and '--' may or may not start a cast-expression:
9284
9285 struct T { void operator++(int); };
9286 void f() { (T())++; }
9287
9288 vs
9289
9290 int a;
9291 (int)++a; */
9292 return -1;
9293
9294 default:
9295 return 1;
9296 }
9297 }
9298
9299 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9300 in the order: const_cast, static_cast, reinterpret_cast.
9301
9302 Don't suggest dynamic_cast.
9303
9304 Return the first legal cast kind found, or NULL otherwise. */
9305
9306 static const char *
9307 get_cast_suggestion (tree dst_type, tree orig_expr)
9308 {
9309 tree trial;
9310
9311 /* Reuse the parser logic by attempting to build the various kinds of
9312 cast, with "complain" disabled.
9313 Identify the first such cast that is valid. */
9314
9315 /* Don't attempt to run such logic within template processing. */
9316 if (processing_template_decl)
9317 return NULL;
9318
9319 /* First try const_cast. */
9320 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
9321 if (trial != error_mark_node)
9322 return "const_cast";
9323
9324 /* If that fails, try static_cast. */
9325 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
9326 if (trial != error_mark_node)
9327 return "static_cast";
9328
9329 /* Finally, try reinterpret_cast. */
9330 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
9331 tf_none);
9332 if (trial != error_mark_node)
9333 return "reinterpret_cast";
9334
9335 /* No such cast possible. */
9336 return NULL;
9337 }
9338
9339 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9340 suggesting how to convert a C-style cast of the form:
9341
9342 (DST_TYPE)ORIG_EXPR
9343
9344 to a C++-style cast.
9345
9346 The primary range of RICHLOC is asssumed to be that of the original
9347 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9348 of the parens in the C-style cast. */
9349
9350 static void
9351 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9352 location_t close_paren_loc, tree orig_expr,
9353 tree dst_type)
9354 {
9355 /* This function is non-trivial, so bail out now if the warning isn't
9356 going to be emitted. */
9357 if (!warn_old_style_cast)
9358 return;
9359
9360 /* Try to find a legal C++ cast, trying them in order:
9361 const_cast, static_cast, reinterpret_cast. */
9362 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9363 if (!cast_suggestion)
9364 return;
9365
9366 /* Replace the open paren with "CAST_SUGGESTION<". */
9367 pretty_printer pp;
9368 pp_printf (&pp, "%s<", cast_suggestion);
9369 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9370
9371 /* Replace the close paren with "> (". */
9372 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9373
9374 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9375 rich_loc->add_fixit_insert_after (")");
9376 }
9377
9378
9379 /* Parse a cast-expression.
9380
9381 cast-expression:
9382 unary-expression
9383 ( type-id ) cast-expression
9384
9385 ADDRESS_P is true iff the unary-expression is appearing as the
9386 operand of the `&' operator. CAST_P is true if this expression is
9387 the target of a cast.
9388
9389 Returns a representation of the expression. */
9390
9391 static cp_expr
9392 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9393 bool decltype_p, cp_id_kind * pidk)
9394 {
9395 /* If it's a `(', then we might be looking at a cast. */
9396 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9397 {
9398 tree type = NULL_TREE;
9399 cp_expr expr (NULL_TREE);
9400 int cast_expression = 0;
9401 const char *saved_message;
9402
9403 /* There's no way to know yet whether or not this is a cast.
9404 For example, `(int (3))' is a unary-expression, while `(int)
9405 3' is a cast. So, we resort to parsing tentatively. */
9406 cp_parser_parse_tentatively (parser);
9407 /* Types may not be defined in a cast. */
9408 saved_message = parser->type_definition_forbidden_message;
9409 parser->type_definition_forbidden_message
9410 = G_("types may not be defined in casts");
9411 /* Consume the `('. */
9412 matching_parens parens;
9413 cp_token *open_paren = parens.consume_open (parser);
9414 location_t open_paren_loc = open_paren->location;
9415 location_t close_paren_loc = UNKNOWN_LOCATION;
9416
9417 /* A very tricky bit is that `(struct S) { 3 }' is a
9418 compound-literal (which we permit in C++ as an extension).
9419 But, that construct is not a cast-expression -- it is a
9420 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9421 is legal; if the compound-literal were a cast-expression,
9422 you'd need an extra set of parentheses.) But, if we parse
9423 the type-id, and it happens to be a class-specifier, then we
9424 will commit to the parse at that point, because we cannot
9425 undo the action that is done when creating a new class. So,
9426 then we cannot back up and do a postfix-expression.
9427
9428 Another tricky case is the following (c++/29234):
9429
9430 struct S { void operator () (); };
9431
9432 void foo ()
9433 {
9434 ( S()() );
9435 }
9436
9437 As a type-id we parse the parenthesized S()() as a function
9438 returning a function, groktypename complains and we cannot
9439 back up in this case either.
9440
9441 Therefore, we scan ahead to the closing `)', and check to see
9442 if the tokens after the `)' can start a cast-expression. Otherwise
9443 we are dealing with an unary-expression, a postfix-expression
9444 or something else.
9445
9446 Yet another tricky case, in C++11, is the following (c++/54891):
9447
9448 (void)[]{};
9449
9450 The issue is that usually, besides the case of lambda-expressions,
9451 the parenthesized type-id cannot be followed by '[', and, eg, we
9452 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9453 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9454 we don't commit, we try a cast-expression, then an unary-expression.
9455
9456 Save tokens so that we can put them back. */
9457 cp_lexer_save_tokens (parser->lexer);
9458
9459 /* We may be looking at a cast-expression. */
9460 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9461 /*consume_paren=*/true))
9462 cast_expression
9463 = cp_parser_tokens_start_cast_expression (parser);
9464
9465 /* Roll back the tokens we skipped. */
9466 cp_lexer_rollback_tokens (parser->lexer);
9467 /* If we aren't looking at a cast-expression, simulate an error so
9468 that the call to cp_parser_error_occurred below returns true. */
9469 if (!cast_expression)
9470 cp_parser_simulate_error (parser);
9471 else
9472 {
9473 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9474 parser->in_type_id_in_expr_p = true;
9475 /* Look for the type-id. */
9476 type = cp_parser_type_id (parser);
9477 /* Look for the closing `)'. */
9478 cp_token *close_paren = parens.require_close (parser);
9479 if (close_paren)
9480 close_paren_loc = close_paren->location;
9481 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9482 }
9483
9484 /* Restore the saved message. */
9485 parser->type_definition_forbidden_message = saved_message;
9486
9487 /* At this point this can only be either a cast or a
9488 parenthesized ctor such as `(T ())' that looks like a cast to
9489 function returning T. */
9490 if (!cp_parser_error_occurred (parser))
9491 {
9492 /* Only commit if the cast-expression doesn't start with
9493 '++', '--', or '[' in C++11. */
9494 if (cast_expression > 0)
9495 cp_parser_commit_to_topmost_tentative_parse (parser);
9496
9497 expr = cp_parser_cast_expression (parser,
9498 /*address_p=*/false,
9499 /*cast_p=*/true,
9500 /*decltype_p=*/false,
9501 pidk);
9502
9503 if (cp_parser_parse_definitely (parser))
9504 {
9505 /* Warn about old-style casts, if so requested. */
9506 if (warn_old_style_cast
9507 && !in_system_header_at (input_location)
9508 && !VOID_TYPE_P (type)
9509 && current_lang_name != lang_name_c)
9510 {
9511 gcc_rich_location rich_loc (input_location);
9512 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9513 expr, type);
9514 warning_at (&rich_loc, OPT_Wold_style_cast,
9515 "use of old-style cast to %q#T", type);
9516 }
9517
9518 /* Only type conversions to integral or enumeration types
9519 can be used in constant-expressions. */
9520 if (!cast_valid_in_integral_constant_expression_p (type)
9521 && cp_parser_non_integral_constant_expression (parser,
9522 NIC_CAST))
9523 return error_mark_node;
9524
9525 /* Perform the cast. */
9526 /* Make a location:
9527 (TYPE) EXPR
9528 ^~~~~~~~~~~
9529 with start==caret at the open paren, extending to the
9530 end of "expr". */
9531 location_t cast_loc = make_location (open_paren_loc,
9532 open_paren_loc,
9533 expr.get_finish ());
9534 expr = build_c_cast (cast_loc, type, expr);
9535 return expr;
9536 }
9537 }
9538 else
9539 cp_parser_abort_tentative_parse (parser);
9540 }
9541
9542 /* If we get here, then it's not a cast, so it must be a
9543 unary-expression. */
9544 return cp_parser_unary_expression (parser, pidk, address_p,
9545 cast_p, decltype_p);
9546 }
9547
9548 /* Parse a binary expression of the general form:
9549
9550 pm-expression:
9551 cast-expression
9552 pm-expression .* cast-expression
9553 pm-expression ->* cast-expression
9554
9555 multiplicative-expression:
9556 pm-expression
9557 multiplicative-expression * pm-expression
9558 multiplicative-expression / pm-expression
9559 multiplicative-expression % pm-expression
9560
9561 additive-expression:
9562 multiplicative-expression
9563 additive-expression + multiplicative-expression
9564 additive-expression - multiplicative-expression
9565
9566 shift-expression:
9567 additive-expression
9568 shift-expression << additive-expression
9569 shift-expression >> additive-expression
9570
9571 relational-expression:
9572 shift-expression
9573 relational-expression < shift-expression
9574 relational-expression > shift-expression
9575 relational-expression <= shift-expression
9576 relational-expression >= shift-expression
9577
9578 GNU Extension:
9579
9580 relational-expression:
9581 relational-expression <? shift-expression
9582 relational-expression >? shift-expression
9583
9584 equality-expression:
9585 relational-expression
9586 equality-expression == relational-expression
9587 equality-expression != relational-expression
9588
9589 and-expression:
9590 equality-expression
9591 and-expression & equality-expression
9592
9593 exclusive-or-expression:
9594 and-expression
9595 exclusive-or-expression ^ and-expression
9596
9597 inclusive-or-expression:
9598 exclusive-or-expression
9599 inclusive-or-expression | exclusive-or-expression
9600
9601 logical-and-expression:
9602 inclusive-or-expression
9603 logical-and-expression && inclusive-or-expression
9604
9605 logical-or-expression:
9606 logical-and-expression
9607 logical-or-expression || logical-and-expression
9608
9609 All these are implemented with a single function like:
9610
9611 binary-expression:
9612 simple-cast-expression
9613 binary-expression <token> binary-expression
9614
9615 CAST_P is true if this expression is the target of a cast.
9616
9617 The binops_by_token map is used to get the tree codes for each <token> type.
9618 binary-expressions are associated according to a precedence table. */
9619
9620 #define TOKEN_PRECEDENCE(token) \
9621 (((token->type == CPP_GREATER \
9622 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9623 && !parser->greater_than_is_operator_p) \
9624 ? PREC_NOT_OPERATOR \
9625 : binops_by_token[token->type].prec)
9626
9627 static cp_expr
9628 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9629 bool no_toplevel_fold_p,
9630 bool decltype_p,
9631 enum cp_parser_prec prec,
9632 cp_id_kind * pidk)
9633 {
9634 cp_parser_expression_stack stack;
9635 cp_parser_expression_stack_entry *sp = &stack[0];
9636 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
9637 cp_parser_expression_stack_entry current;
9638 cp_expr rhs;
9639 cp_token *token;
9640 enum tree_code rhs_type;
9641 enum cp_parser_prec new_prec, lookahead_prec;
9642 tree overload;
9643
9644 /* Parse the first expression. */
9645 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9646 ? TRUTH_NOT_EXPR : ERROR_MARK);
9647 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9648 cast_p, decltype_p, pidk);
9649 current.prec = prec;
9650
9651 if (cp_parser_error_occurred (parser))
9652 return error_mark_node;
9653
9654 for (;;)
9655 {
9656 /* Get an operator token. */
9657 token = cp_lexer_peek_token (parser->lexer);
9658
9659 if (warn_cxx11_compat
9660 && token->type == CPP_RSHIFT
9661 && !parser->greater_than_is_operator_p)
9662 {
9663 if (warning_at (token->location, OPT_Wc__11_compat,
9664 "%<>>%> operator is treated"
9665 " as two right angle brackets in C++11"))
9666 inform (token->location,
9667 "suggest parentheses around %<>>%> expression");
9668 }
9669
9670 new_prec = TOKEN_PRECEDENCE (token);
9671 if (new_prec != PREC_NOT_OPERATOR
9672 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9673 /* This is a fold-expression; handle it later. */
9674 new_prec = PREC_NOT_OPERATOR;
9675
9676 /* Popping an entry off the stack means we completed a subexpression:
9677 - either we found a token which is not an operator (`>' where it is not
9678 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9679 will happen repeatedly;
9680 - or, we found an operator which has lower priority. This is the case
9681 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9682 parsing `3 * 4'. */
9683 if (new_prec <= current.prec)
9684 {
9685 if (sp == stack)
9686 break;
9687 else
9688 goto pop;
9689 }
9690
9691 get_rhs:
9692 current.tree_type = binops_by_token[token->type].tree_type;
9693 current.loc = token->location;
9694
9695 /* We used the operator token. */
9696 cp_lexer_consume_token (parser->lexer);
9697
9698 /* For "false && x" or "true || x", x will never be executed;
9699 disable warnings while evaluating it. */
9700 if ((current.tree_type == TRUTH_ANDIF_EXPR
9701 && cp_fully_fold (current.lhs) == truthvalue_false_node)
9702 || (current.tree_type == TRUTH_ORIF_EXPR
9703 && cp_fully_fold (current.lhs) == truthvalue_true_node))
9704 {
9705 disable_warnings_sp = sp;
9706 ++c_inhibit_evaluation_warnings;
9707 }
9708
9709 /* Extract another operand. It may be the RHS of this expression
9710 or the LHS of a new, higher priority expression. */
9711 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9712 ? TRUTH_NOT_EXPR : ERROR_MARK);
9713 rhs = cp_parser_simple_cast_expression (parser);
9714
9715 /* Get another operator token. Look up its precedence to avoid
9716 building a useless (immediately popped) stack entry for common
9717 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9718 token = cp_lexer_peek_token (parser->lexer);
9719 lookahead_prec = TOKEN_PRECEDENCE (token);
9720 if (lookahead_prec != PREC_NOT_OPERATOR
9721 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9722 lookahead_prec = PREC_NOT_OPERATOR;
9723 if (lookahead_prec > new_prec)
9724 {
9725 /* ... and prepare to parse the RHS of the new, higher priority
9726 expression. Since precedence levels on the stack are
9727 monotonically increasing, we do not have to care about
9728 stack overflows. */
9729 *sp = current;
9730 ++sp;
9731 current.lhs = rhs;
9732 current.lhs_type = rhs_type;
9733 current.prec = new_prec;
9734 new_prec = lookahead_prec;
9735 goto get_rhs;
9736
9737 pop:
9738 lookahead_prec = new_prec;
9739 /* If the stack is not empty, we have parsed into LHS the right side
9740 (`4' in the example above) of an expression we had suspended.
9741 We can use the information on the stack to recover the LHS (`3')
9742 from the stack together with the tree code (`MULT_EXPR'), and
9743 the precedence of the higher level subexpression
9744 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9745 which will be used to actually build the additive expression. */
9746 rhs = current.lhs;
9747 rhs_type = current.lhs_type;
9748 --sp;
9749 current = *sp;
9750 }
9751
9752 /* Undo the disabling of warnings done above. */
9753 if (sp == disable_warnings_sp)
9754 {
9755 disable_warnings_sp = NULL;
9756 --c_inhibit_evaluation_warnings;
9757 }
9758
9759 if (warn_logical_not_paren
9760 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9761 && current.lhs_type == TRUTH_NOT_EXPR
9762 /* Avoid warning for !!x == y. */
9763 && (TREE_CODE (current.lhs) != NE_EXPR
9764 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9765 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9766 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9767 /* Avoid warning for !b == y where b is boolean. */
9768 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9769 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9770 != BOOLEAN_TYPE))))
9771 /* Avoid warning for !!b == y where b is boolean. */
9772 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
9773 || TREE_TYPE (current.lhs) == NULL_TREE
9774 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9775 warn_logical_not_parentheses (current.loc, current.tree_type,
9776 current.lhs, maybe_constant_value (rhs));
9777
9778 overload = NULL;
9779
9780 location_t combined_loc = make_location (current.loc,
9781 current.lhs.get_start (),
9782 rhs.get_finish ());
9783
9784 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9785 ERROR_MARK for everything that is not a binary expression.
9786 This makes warn_about_parentheses miss some warnings that
9787 involve unary operators. For unary expressions we should
9788 pass the correct tree_code unless the unary expression was
9789 surrounded by parentheses.
9790 */
9791 if (no_toplevel_fold_p
9792 && lookahead_prec <= current.prec
9793 && sp == stack)
9794 {
9795 if (current.lhs == error_mark_node || rhs == error_mark_node)
9796 current.lhs = error_mark_node;
9797 else
9798 {
9799 current.lhs.maybe_add_location_wrapper ();
9800 rhs.maybe_add_location_wrapper ();
9801 current.lhs
9802 = build_min (current.tree_type,
9803 TREE_CODE_CLASS (current.tree_type)
9804 == tcc_comparison
9805 ? boolean_type_node : TREE_TYPE (current.lhs),
9806 current.lhs.get_value (), rhs.get_value ());
9807 SET_EXPR_LOCATION (current.lhs, combined_loc);
9808 }
9809 }
9810 else
9811 {
9812 op_location_t op_loc (current.loc, combined_loc);
9813 current.lhs = build_x_binary_op (op_loc, current.tree_type,
9814 current.lhs, current.lhs_type,
9815 rhs, rhs_type, &overload,
9816 complain_flags (decltype_p));
9817 /* TODO: build_x_binary_op doesn't always honor the location. */
9818 current.lhs.set_location (combined_loc);
9819 }
9820 current.lhs_type = current.tree_type;
9821
9822 /* If the binary operator required the use of an overloaded operator,
9823 then this expression cannot be an integral constant-expression.
9824 An overloaded operator can be used even if both operands are
9825 otherwise permissible in an integral constant-expression if at
9826 least one of the operands is of enumeration type. */
9827
9828 if (overload
9829 && cp_parser_non_integral_constant_expression (parser,
9830 NIC_OVERLOADED))
9831 return error_mark_node;
9832 }
9833
9834 return current.lhs;
9835 }
9836
9837 static cp_expr
9838 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9839 bool no_toplevel_fold_p,
9840 enum cp_parser_prec prec,
9841 cp_id_kind * pidk)
9842 {
9843 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9844 /*decltype*/false, prec, pidk);
9845 }
9846
9847 /* Parse the `? expression : assignment-expression' part of a
9848 conditional-expression. The LOGICAL_OR_EXPR is the
9849 logical-or-expression that started the conditional-expression.
9850 Returns a representation of the entire conditional-expression.
9851
9852 This routine is used by cp_parser_assignment_expression.
9853
9854 ? expression : assignment-expression
9855
9856 GNU Extensions:
9857
9858 ? : assignment-expression */
9859
9860 static tree
9861 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9862 {
9863 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9864 cp_expr assignment_expr;
9865 struct cp_token *token;
9866 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9867
9868 /* Consume the `?' token. */
9869 cp_lexer_consume_token (parser->lexer);
9870 token = cp_lexer_peek_token (parser->lexer);
9871 if (cp_parser_allow_gnu_extensions_p (parser)
9872 && token->type == CPP_COLON)
9873 {
9874 pedwarn (token->location, OPT_Wpedantic,
9875 "ISO C++ does not allow %<?:%> with omitted middle operand");
9876 /* Implicit true clause. */
9877 expr = NULL_TREE;
9878 c_inhibit_evaluation_warnings +=
9879 folded_logical_or_expr == truthvalue_true_node;
9880 warn_for_omitted_condop (token->location, logical_or_expr);
9881 }
9882 else
9883 {
9884 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9885 parser->colon_corrects_to_scope_p = false;
9886 /* Parse the expression. */
9887 c_inhibit_evaluation_warnings +=
9888 folded_logical_or_expr == truthvalue_false_node;
9889 expr = cp_parser_expression (parser);
9890 c_inhibit_evaluation_warnings +=
9891 ((folded_logical_or_expr == truthvalue_true_node)
9892 - (folded_logical_or_expr == truthvalue_false_node));
9893 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9894 }
9895
9896 /* The next token should be a `:'. */
9897 cp_parser_require (parser, CPP_COLON, RT_COLON);
9898 /* Parse the assignment-expression. */
9899 assignment_expr = cp_parser_assignment_expression (parser);
9900 c_inhibit_evaluation_warnings -=
9901 folded_logical_or_expr == truthvalue_true_node;
9902
9903 /* Make a location:
9904 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9905 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9906 with the caret at the "?", ranging from the start of
9907 the logical_or_expr to the end of the assignment_expr. */
9908 loc = make_location (loc,
9909 logical_or_expr.get_start (),
9910 assignment_expr.get_finish ());
9911
9912 /* Build the conditional-expression. */
9913 return build_x_conditional_expr (loc, logical_or_expr,
9914 expr,
9915 assignment_expr,
9916 tf_warning_or_error);
9917 }
9918
9919 /* Parse an assignment-expression.
9920
9921 assignment-expression:
9922 conditional-expression
9923 logical-or-expression assignment-operator assignment_expression
9924 throw-expression
9925 yield-expression
9926
9927 CAST_P is true if this expression is the target of a cast.
9928 DECLTYPE_P is true if this expression is the operand of decltype.
9929
9930 Returns a representation for the expression. */
9931
9932 static cp_expr
9933 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9934 bool cast_p, bool decltype_p)
9935 {
9936 cp_expr expr;
9937
9938 /* If the next token is the `throw' keyword, then we're looking at
9939 a throw-expression. */
9940 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9941 expr = cp_parser_throw_expression (parser);
9942 /* If the next token is the `co_yield' keyword, then we're looking at
9943 a yield-expression. */
9944 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
9945 expr = cp_parser_yield_expression (parser);
9946 /* Otherwise, it must be that we are looking at a
9947 logical-or-expression. */
9948 else
9949 {
9950 /* Parse the binary expressions (logical-or-expression). */
9951 expr = cp_parser_binary_expression (parser, cast_p, false,
9952 decltype_p,
9953 PREC_NOT_OPERATOR, pidk);
9954 /* If the next token is a `?' then we're actually looking at a
9955 conditional-expression. */
9956 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9957 return cp_parser_question_colon_clause (parser, expr);
9958 else
9959 {
9960 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9961
9962 /* If it's an assignment-operator, we're using the second
9963 production. */
9964 enum tree_code assignment_operator
9965 = cp_parser_assignment_operator_opt (parser);
9966 if (assignment_operator != ERROR_MARK)
9967 {
9968 bool non_constant_p;
9969
9970 /* Parse the right-hand side of the assignment. */
9971 cp_expr rhs = cp_parser_initializer_clause (parser,
9972 &non_constant_p);
9973
9974 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9975 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9976
9977 /* An assignment may not appear in a
9978 constant-expression. */
9979 if (cp_parser_non_integral_constant_expression (parser,
9980 NIC_ASSIGNMENT))
9981 return error_mark_node;
9982 /* Build the assignment expression. Its default
9983 location:
9984 LHS = RHS
9985 ~~~~^~~~~
9986 is the location of the '=' token as the
9987 caret, ranging from the start of the lhs to the
9988 end of the rhs. */
9989 loc = make_location (loc,
9990 expr.get_start (),
9991 rhs.get_finish ());
9992 expr = build_x_modify_expr (loc, expr,
9993 assignment_operator,
9994 rhs,
9995 complain_flags (decltype_p));
9996 /* TODO: build_x_modify_expr doesn't honor the location,
9997 so we must set it here. */
9998 expr.set_location (loc);
9999 }
10000 }
10001 }
10002
10003 return expr;
10004 }
10005
10006 /* Parse an (optional) assignment-operator.
10007
10008 assignment-operator: one of
10009 = *= /= %= += -= >>= <<= &= ^= |=
10010
10011 GNU Extension:
10012
10013 assignment-operator: one of
10014 <?= >?=
10015
10016 If the next token is an assignment operator, the corresponding tree
10017 code is returned, and the token is consumed. For example, for
10018 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10019 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10020 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10021 operator, ERROR_MARK is returned. */
10022
10023 static enum tree_code
10024 cp_parser_assignment_operator_opt (cp_parser* parser)
10025 {
10026 enum tree_code op;
10027 cp_token *token;
10028
10029 /* Peek at the next token. */
10030 token = cp_lexer_peek_token (parser->lexer);
10031
10032 switch (token->type)
10033 {
10034 case CPP_EQ:
10035 op = NOP_EXPR;
10036 break;
10037
10038 case CPP_MULT_EQ:
10039 op = MULT_EXPR;
10040 break;
10041
10042 case CPP_DIV_EQ:
10043 op = TRUNC_DIV_EXPR;
10044 break;
10045
10046 case CPP_MOD_EQ:
10047 op = TRUNC_MOD_EXPR;
10048 break;
10049
10050 case CPP_PLUS_EQ:
10051 op = PLUS_EXPR;
10052 break;
10053
10054 case CPP_MINUS_EQ:
10055 op = MINUS_EXPR;
10056 break;
10057
10058 case CPP_RSHIFT_EQ:
10059 op = RSHIFT_EXPR;
10060 break;
10061
10062 case CPP_LSHIFT_EQ:
10063 op = LSHIFT_EXPR;
10064 break;
10065
10066 case CPP_AND_EQ:
10067 op = BIT_AND_EXPR;
10068 break;
10069
10070 case CPP_XOR_EQ:
10071 op = BIT_XOR_EXPR;
10072 break;
10073
10074 case CPP_OR_EQ:
10075 op = BIT_IOR_EXPR;
10076 break;
10077
10078 default:
10079 /* Nothing else is an assignment operator. */
10080 op = ERROR_MARK;
10081 }
10082
10083 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10084 if (op != ERROR_MARK
10085 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10086 op = ERROR_MARK;
10087
10088 /* If it was an assignment operator, consume it. */
10089 if (op != ERROR_MARK)
10090 cp_lexer_consume_token (parser->lexer);
10091
10092 return op;
10093 }
10094
10095 /* Parse an expression.
10096
10097 expression:
10098 assignment-expression
10099 expression , assignment-expression
10100
10101 CAST_P is true if this expression is the target of a cast.
10102 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10103 except possibly parenthesized or on the RHS of a comma (N3276).
10104 WARN_COMMA_P is true if a comma should be diagnosed.
10105
10106 Returns a representation of the expression. */
10107
10108 static cp_expr
10109 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10110 bool cast_p, bool decltype_p, bool warn_comma_p)
10111 {
10112 cp_expr expression = NULL_TREE;
10113 location_t loc = UNKNOWN_LOCATION;
10114
10115 while (true)
10116 {
10117 cp_expr assignment_expression;
10118
10119 /* Parse the next assignment-expression. */
10120 assignment_expression
10121 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10122
10123 /* We don't create a temporary for a call that is the immediate operand
10124 of decltype or on the RHS of a comma. But when we see a comma, we
10125 need to create a temporary for a call on the LHS. */
10126 if (decltype_p && !processing_template_decl
10127 && TREE_CODE (assignment_expression) == CALL_EXPR
10128 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10129 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10130 assignment_expression
10131 = build_cplus_new (TREE_TYPE (assignment_expression),
10132 assignment_expression, tf_warning_or_error);
10133
10134 /* If this is the first assignment-expression, we can just
10135 save it away. */
10136 if (!expression)
10137 expression = assignment_expression;
10138 else
10139 {
10140 /* Create a location with caret at the comma, ranging
10141 from the start of the LHS to the end of the RHS. */
10142 loc = make_location (loc,
10143 expression.get_start (),
10144 assignment_expression.get_finish ());
10145 expression = build_x_compound_expr (loc, expression,
10146 assignment_expression,
10147 complain_flags (decltype_p));
10148 expression.set_location (loc);
10149 }
10150 /* If the next token is not a comma, or we're in a fold-expression, then
10151 we are done with the expression. */
10152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10153 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10154 break;
10155 /* Consume the `,'. */
10156 loc = cp_lexer_peek_token (parser->lexer)->location;
10157 if (warn_comma_p)
10158 {
10159 /* [depr.comma.subscript]: A comma expression appearing as
10160 the expr-or-braced-init-list of a subscripting expression
10161 is deprecated. A parenthesized comma expression is not
10162 deprecated. */
10163 warning_at (loc, OPT_Wcomma_subscript,
10164 "top-level comma expression in array subscript "
10165 "is deprecated");
10166 warn_comma_p = false;
10167 }
10168 cp_lexer_consume_token (parser->lexer);
10169 /* A comma operator cannot appear in a constant-expression. */
10170 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10171 expression = error_mark_node;
10172 }
10173
10174 return expression;
10175 }
10176
10177 /* Parse a constant-expression.
10178
10179 constant-expression:
10180 conditional-expression
10181
10182 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10183 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10184 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10185 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
10186 only parse a conditional-expression, otherwise parse an
10187 assignment-expression. See below for rationale. */
10188
10189 static cp_expr
10190 cp_parser_constant_expression (cp_parser* parser,
10191 bool allow_non_constant_p,
10192 bool *non_constant_p,
10193 bool strict_p)
10194 {
10195 bool saved_integral_constant_expression_p;
10196 bool saved_allow_non_integral_constant_expression_p;
10197 bool saved_non_integral_constant_expression_p;
10198 cp_expr expression;
10199
10200 /* It might seem that we could simply parse the
10201 conditional-expression, and then check to see if it were
10202 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10203 one that the compiler can figure out is constant, possibly after
10204 doing some simplifications or optimizations. The standard has a
10205 precise definition of constant-expression, and we must honor
10206 that, even though it is somewhat more restrictive.
10207
10208 For example:
10209
10210 int i[(2, 3)];
10211
10212 is not a legal declaration, because `(2, 3)' is not a
10213 constant-expression. The `,' operator is forbidden in a
10214 constant-expression. However, GCC's constant-folding machinery
10215 will fold this operation to an INTEGER_CST for `3'. */
10216
10217 /* Save the old settings. */
10218 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10219 saved_allow_non_integral_constant_expression_p
10220 = parser->allow_non_integral_constant_expression_p;
10221 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10222 /* We are now parsing a constant-expression. */
10223 parser->integral_constant_expression_p = true;
10224 parser->allow_non_integral_constant_expression_p
10225 = (allow_non_constant_p || cxx_dialect >= cxx11);
10226 parser->non_integral_constant_expression_p = false;
10227 /* Although the grammar says "conditional-expression", when not STRICT_P,
10228 we parse an "assignment-expression", which also permits
10229 "throw-expression" and the use of assignment operators. In the case
10230 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10231 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10232 actually essential that we look for an assignment-expression.
10233 For example, cp_parser_initializer_clauses uses this function to
10234 determine whether a particular assignment-expression is in fact
10235 constant. */
10236 if (strict_p)
10237 {
10238 /* Parse the binary expressions (logical-or-expression). */
10239 expression = cp_parser_binary_expression (parser, false, false, false,
10240 PREC_NOT_OPERATOR, NULL);
10241 /* If the next token is a `?' then we're actually looking at
10242 a conditional-expression; otherwise we're done. */
10243 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10244 expression = cp_parser_question_colon_clause (parser, expression);
10245 }
10246 else
10247 expression = cp_parser_assignment_expression (parser);
10248 /* Restore the old settings. */
10249 parser->integral_constant_expression_p
10250 = saved_integral_constant_expression_p;
10251 parser->allow_non_integral_constant_expression_p
10252 = saved_allow_non_integral_constant_expression_p;
10253 if (cxx_dialect >= cxx11)
10254 {
10255 /* Require an rvalue constant expression here; that's what our
10256 callers expect. Reference constant expressions are handled
10257 separately in e.g. cp_parser_template_argument. */
10258 tree decay = expression;
10259 if (TREE_TYPE (expression)
10260 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10261 decay = build_address (expression);
10262 bool is_const = is_rvalue_constant_expression (decay);
10263 parser->non_integral_constant_expression_p = !is_const;
10264 if (!is_const && !allow_non_constant_p)
10265 require_rvalue_constant_expression (decay);
10266 }
10267 if (allow_non_constant_p)
10268 *non_constant_p = parser->non_integral_constant_expression_p;
10269 parser->non_integral_constant_expression_p
10270 = saved_non_integral_constant_expression_p;
10271
10272 return expression;
10273 }
10274
10275 /* Parse __builtin_offsetof.
10276
10277 offsetof-expression:
10278 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10279
10280 offsetof-member-designator:
10281 id-expression
10282 | offsetof-member-designator "." id-expression
10283 | offsetof-member-designator "[" expression "]"
10284 | offsetof-member-designator "->" id-expression */
10285
10286 static cp_expr
10287 cp_parser_builtin_offsetof (cp_parser *parser)
10288 {
10289 int save_ice_p, save_non_ice_p;
10290 tree type;
10291 cp_expr expr;
10292 cp_id_kind dummy;
10293 cp_token *token;
10294 location_t finish_loc;
10295
10296 /* We're about to accept non-integral-constant things, but will
10297 definitely yield an integral constant expression. Save and
10298 restore these values around our local parsing. */
10299 save_ice_p = parser->integral_constant_expression_p;
10300 save_non_ice_p = parser->non_integral_constant_expression_p;
10301
10302 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10303
10304 /* Consume the "__builtin_offsetof" token. */
10305 cp_lexer_consume_token (parser->lexer);
10306 /* Consume the opening `('. */
10307 matching_parens parens;
10308 parens.require_open (parser);
10309 /* Parse the type-id. */
10310 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10311 {
10312 const char *saved_message = parser->type_definition_forbidden_message;
10313 parser->type_definition_forbidden_message
10314 = G_("types may not be defined within %<__builtin_offsetof%>");
10315 type = cp_parser_type_id (parser);
10316 parser->type_definition_forbidden_message = saved_message;
10317 }
10318 /* Look for the `,'. */
10319 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10320 token = cp_lexer_peek_token (parser->lexer);
10321
10322 /* Build the (type *)null that begins the traditional offsetof macro. */
10323 tree object_ptr
10324 = build_static_cast (input_location, build_pointer_type (type),
10325 null_pointer_node, tf_warning_or_error);
10326
10327 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10328 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10329 true, &dummy, token->location);
10330 while (true)
10331 {
10332 token = cp_lexer_peek_token (parser->lexer);
10333 switch (token->type)
10334 {
10335 case CPP_OPEN_SQUARE:
10336 /* offsetof-member-designator "[" expression "]" */
10337 expr = cp_parser_postfix_open_square_expression (parser, expr,
10338 true, false);
10339 break;
10340
10341 case CPP_DEREF:
10342 /* offsetof-member-designator "->" identifier */
10343 expr = grok_array_decl (token->location, expr,
10344 integer_zero_node, false);
10345 /* FALLTHRU */
10346
10347 case CPP_DOT:
10348 /* offsetof-member-designator "." identifier */
10349 cp_lexer_consume_token (parser->lexer);
10350 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10351 expr, true, &dummy,
10352 token->location);
10353 break;
10354
10355 case CPP_CLOSE_PAREN:
10356 /* Consume the ")" token. */
10357 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10358 cp_lexer_consume_token (parser->lexer);
10359 goto success;
10360
10361 default:
10362 /* Error. We know the following require will fail, but
10363 that gives the proper error message. */
10364 parens.require_close (parser);
10365 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10366 expr = error_mark_node;
10367 goto failure;
10368 }
10369 }
10370
10371 success:
10372 /* Make a location of the form:
10373 __builtin_offsetof (struct s, f)
10374 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10375 with caret at the type-id, ranging from the start of the
10376 "_builtin_offsetof" token to the close paren. */
10377 loc = make_location (loc, start_loc, finish_loc);
10378 /* The result will be an INTEGER_CST, so we need to explicitly
10379 preserve the location. */
10380 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10381
10382 failure:
10383 parser->integral_constant_expression_p = save_ice_p;
10384 parser->non_integral_constant_expression_p = save_non_ice_p;
10385
10386 expr = expr.maybe_add_location_wrapper ();
10387 return expr;
10388 }
10389
10390 /* Parse a trait expression.
10391
10392 Returns a representation of the expression, the underlying type
10393 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10394
10395 static cp_expr
10396 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10397 {
10398 cp_trait_kind kind;
10399 tree type1, type2 = NULL_TREE;
10400 bool binary = false;
10401 bool variadic = false;
10402
10403 switch (keyword)
10404 {
10405 case RID_HAS_NOTHROW_ASSIGN:
10406 kind = CPTK_HAS_NOTHROW_ASSIGN;
10407 break;
10408 case RID_HAS_NOTHROW_CONSTRUCTOR:
10409 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10410 break;
10411 case RID_HAS_NOTHROW_COPY:
10412 kind = CPTK_HAS_NOTHROW_COPY;
10413 break;
10414 case RID_HAS_TRIVIAL_ASSIGN:
10415 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10416 break;
10417 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10418 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10419 break;
10420 case RID_HAS_TRIVIAL_COPY:
10421 kind = CPTK_HAS_TRIVIAL_COPY;
10422 break;
10423 case RID_HAS_TRIVIAL_DESTRUCTOR:
10424 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10425 break;
10426 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10427 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10428 break;
10429 case RID_HAS_VIRTUAL_DESTRUCTOR:
10430 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10431 break;
10432 case RID_IS_ABSTRACT:
10433 kind = CPTK_IS_ABSTRACT;
10434 break;
10435 case RID_IS_AGGREGATE:
10436 kind = CPTK_IS_AGGREGATE;
10437 break;
10438 case RID_IS_BASE_OF:
10439 kind = CPTK_IS_BASE_OF;
10440 binary = true;
10441 break;
10442 case RID_IS_CLASS:
10443 kind = CPTK_IS_CLASS;
10444 break;
10445 case RID_IS_EMPTY:
10446 kind = CPTK_IS_EMPTY;
10447 break;
10448 case RID_IS_ENUM:
10449 kind = CPTK_IS_ENUM;
10450 break;
10451 case RID_IS_FINAL:
10452 kind = CPTK_IS_FINAL;
10453 break;
10454 case RID_IS_LITERAL_TYPE:
10455 kind = CPTK_IS_LITERAL_TYPE;
10456 break;
10457 case RID_IS_POD:
10458 kind = CPTK_IS_POD;
10459 break;
10460 case RID_IS_POLYMORPHIC:
10461 kind = CPTK_IS_POLYMORPHIC;
10462 break;
10463 case RID_IS_SAME_AS:
10464 kind = CPTK_IS_SAME_AS;
10465 binary = true;
10466 break;
10467 case RID_IS_STD_LAYOUT:
10468 kind = CPTK_IS_STD_LAYOUT;
10469 break;
10470 case RID_IS_TRIVIAL:
10471 kind = CPTK_IS_TRIVIAL;
10472 break;
10473 case RID_IS_TRIVIALLY_ASSIGNABLE:
10474 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10475 binary = true;
10476 break;
10477 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10478 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10479 variadic = true;
10480 break;
10481 case RID_IS_TRIVIALLY_COPYABLE:
10482 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10483 break;
10484 case RID_IS_UNION:
10485 kind = CPTK_IS_UNION;
10486 break;
10487 case RID_UNDERLYING_TYPE:
10488 kind = CPTK_UNDERLYING_TYPE;
10489 break;
10490 case RID_BASES:
10491 kind = CPTK_BASES;
10492 break;
10493 case RID_DIRECT_BASES:
10494 kind = CPTK_DIRECT_BASES;
10495 break;
10496 case RID_IS_ASSIGNABLE:
10497 kind = CPTK_IS_ASSIGNABLE;
10498 binary = true;
10499 break;
10500 case RID_IS_CONSTRUCTIBLE:
10501 kind = CPTK_IS_CONSTRUCTIBLE;
10502 variadic = true;
10503 break;
10504 default:
10505 gcc_unreachable ();
10506 }
10507
10508 /* Get location of initial token. */
10509 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10510
10511 /* Consume the token. */
10512 cp_lexer_consume_token (parser->lexer);
10513
10514 matching_parens parens;
10515 parens.require_open (parser);
10516
10517 {
10518 type_id_in_expr_sentinel s (parser);
10519 type1 = cp_parser_type_id (parser);
10520 }
10521
10522 if (type1 == error_mark_node)
10523 return error_mark_node;
10524
10525 if (binary)
10526 {
10527 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10528
10529 {
10530 type_id_in_expr_sentinel s (parser);
10531 type2 = cp_parser_type_id (parser);
10532 }
10533
10534 if (type2 == error_mark_node)
10535 return error_mark_node;
10536 }
10537 else if (variadic)
10538 {
10539 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10540 {
10541 cp_lexer_consume_token (parser->lexer);
10542 tree elt = cp_parser_type_id (parser);
10543 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10544 {
10545 cp_lexer_consume_token (parser->lexer);
10546 elt = make_pack_expansion (elt);
10547 }
10548 if (elt == error_mark_node)
10549 return error_mark_node;
10550 type2 = tree_cons (NULL_TREE, elt, type2);
10551 }
10552 }
10553
10554 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10555 parens.require_close (parser);
10556
10557 /* Construct a location of the form:
10558 __is_trivially_copyable(_Tp)
10559 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10560 with start == caret, finishing at the close-paren. */
10561 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10562
10563 /* Complete the trait expression, which may mean either processing
10564 the trait expr now or saving it for template instantiation. */
10565 switch (kind)
10566 {
10567 case CPTK_UNDERLYING_TYPE:
10568 return cp_expr (finish_underlying_type (type1), trait_loc);
10569 case CPTK_BASES:
10570 return cp_expr (finish_bases (type1, false), trait_loc);
10571 case CPTK_DIRECT_BASES:
10572 return cp_expr (finish_bases (type1, true), trait_loc);
10573 default:
10574 return finish_trait_expr (trait_loc, kind, type1, type2);
10575 }
10576 }
10577
10578 /* Parse a lambda expression.
10579
10580 lambda-expression:
10581 lambda-introducer lambda-declarator [opt] compound-statement
10582
10583 Returns a representation of the expression. */
10584
10585 static cp_expr
10586 cp_parser_lambda_expression (cp_parser* parser)
10587 {
10588 tree lambda_expr = build_lambda_expr ();
10589 tree type;
10590 bool ok = true;
10591 cp_token *token = cp_lexer_peek_token (parser->lexer);
10592 cp_token_position start = 0;
10593
10594 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10595
10596 if (cxx_dialect >= cxx20)
10597 /* C++20 allows lambdas in unevaluated context. */;
10598 else if (cp_unevaluated_operand)
10599 {
10600 if (!token->error_reported)
10601 {
10602 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10603 "lambda-expression in unevaluated context"
10604 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10605 token->error_reported = true;
10606 }
10607 ok = false;
10608 }
10609 else if (parser->in_template_argument_list_p || processing_template_parmlist)
10610 {
10611 if (!token->error_reported)
10612 {
10613 error_at (token->location, "lambda-expression in template-argument"
10614 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10615 token->error_reported = true;
10616 }
10617 ok = false;
10618 }
10619
10620 /* We may be in the middle of deferred access check. Disable
10621 it now. */
10622 push_deferring_access_checks (dk_no_deferred);
10623
10624 cp_parser_lambda_introducer (parser, lambda_expr);
10625 if (cp_parser_error_occurred (parser))
10626 return error_mark_node;
10627
10628 type = begin_lambda_type (lambda_expr);
10629 if (type == error_mark_node)
10630 return error_mark_node;
10631
10632 record_lambda_scope (lambda_expr);
10633
10634 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10635 determine_visibility (TYPE_NAME (type));
10636
10637 /* Now that we've started the type, add the capture fields for any
10638 explicit captures. */
10639 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10640
10641 {
10642 /* Inside the class, surrounding template-parameter-lists do not apply. */
10643 unsigned int saved_num_template_parameter_lists
10644 = parser->num_template_parameter_lists;
10645 unsigned char in_statement = parser->in_statement;
10646 bool in_switch_statement_p = parser->in_switch_statement_p;
10647 bool fully_implicit_function_template_p
10648 = parser->fully_implicit_function_template_p;
10649 tree implicit_template_parms = parser->implicit_template_parms;
10650 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10651 bool auto_is_implicit_function_template_parm_p
10652 = parser->auto_is_implicit_function_template_parm_p;
10653
10654 parser->num_template_parameter_lists = 0;
10655 parser->in_statement = 0;
10656 parser->in_switch_statement_p = false;
10657 parser->fully_implicit_function_template_p = false;
10658 parser->implicit_template_parms = 0;
10659 parser->implicit_template_scope = 0;
10660 parser->auto_is_implicit_function_template_parm_p = false;
10661
10662 /* The body of a lambda in a discarded statement is not discarded. */
10663 bool discarded = in_discarded_stmt;
10664 in_discarded_stmt = 0;
10665
10666 /* By virtue of defining a local class, a lambda expression has access to
10667 the private variables of enclosing classes. */
10668
10669 if (cp_parser_start_tentative_firewall (parser))
10670 start = token;
10671
10672 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10673
10674 if (ok && cp_parser_error_occurred (parser))
10675 ok = false;
10676
10677 if (ok)
10678 {
10679 cp_parser_lambda_body (parser, lambda_expr);
10680 }
10681 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10682 {
10683 if (cp_parser_skip_to_closing_brace (parser))
10684 cp_lexer_consume_token (parser->lexer);
10685 }
10686
10687 /* The capture list was built up in reverse order; fix that now. */
10688 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10689 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10690
10691 if (ok)
10692 maybe_add_lambda_conv_op (type);
10693
10694 finish_struct (type, /*attributes=*/NULL_TREE);
10695
10696 in_discarded_stmt = discarded;
10697
10698 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10699 parser->in_statement = in_statement;
10700 parser->in_switch_statement_p = in_switch_statement_p;
10701 parser->fully_implicit_function_template_p
10702 = fully_implicit_function_template_p;
10703 parser->implicit_template_parms = implicit_template_parms;
10704 parser->implicit_template_scope = implicit_template_scope;
10705 parser->auto_is_implicit_function_template_parm_p
10706 = auto_is_implicit_function_template_parm_p;
10707 }
10708
10709 /* This field is only used during parsing of the lambda. */
10710 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10711
10712 /* This lambda shouldn't have any proxies left at this point. */
10713 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10714 /* And now that we're done, push proxies for an enclosing lambda. */
10715 insert_pending_capture_proxies ();
10716
10717 /* Update the lambda expression to a range. */
10718 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10719 token->location,
10720 parser->lexer);
10721
10722 if (ok)
10723 lambda_expr = build_lambda_object (lambda_expr);
10724 else
10725 lambda_expr = error_mark_node;
10726
10727 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10728
10729 pop_deferring_access_checks ();
10730
10731 return lambda_expr;
10732 }
10733
10734 /* Parse the beginning of a lambda expression.
10735
10736 lambda-introducer:
10737 [ lambda-capture [opt] ]
10738
10739 LAMBDA_EXPR is the current representation of the lambda expression. */
10740
10741 static void
10742 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10743 {
10744 /* Need commas after the first capture. */
10745 bool first = true;
10746
10747 /* Eat the leading `['. */
10748 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10749
10750 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10751 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10752 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
10753 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
10754 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10755 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10756 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10757 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10758
10759 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10760 {
10761 cp_lexer_consume_token (parser->lexer);
10762 first = false;
10763
10764 if (!(at_function_scope_p () || parsing_nsdmi ()))
10765 error ("non-local lambda expression cannot have a capture-default");
10766 }
10767
10768 hash_set<tree, true> ids;
10769 tree first_capture_id = NULL_TREE;
10770 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10771 {
10772 cp_token* capture_token;
10773 tree capture_id;
10774 tree capture_init_expr;
10775 cp_id_kind idk = CP_ID_KIND_NONE;
10776 bool explicit_init_p = false;
10777
10778 enum capture_kind_type
10779 {
10780 BY_COPY,
10781 BY_REFERENCE
10782 };
10783 enum capture_kind_type capture_kind = BY_COPY;
10784
10785 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10786 {
10787 error ("expected end of capture-list");
10788 return;
10789 }
10790
10791 if (first)
10792 first = false;
10793 else
10794 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10795
10796 /* Possibly capture `this'. */
10797 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10798 {
10799 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10800 if (cxx_dialect < cxx20
10801 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10802 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10803 "with by-copy capture default");
10804 cp_lexer_consume_token (parser->lexer);
10805 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10806 pedwarn (input_location, 0,
10807 "already captured %qD in lambda expression",
10808 this_identifier);
10809 else
10810 add_capture (lambda_expr, /*id=*/this_identifier,
10811 /*initializer=*/finish_this_expr (),
10812 /*by_reference_p=*/true, explicit_init_p);
10813 continue;
10814 }
10815
10816 /* Possibly capture `*this'. */
10817 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10818 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10819 {
10820 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10821 if (cxx_dialect < cxx17)
10822 pedwarn (loc, 0, "%<*this%> capture only available with "
10823 "%<-std=c++17%> or %<-std=gnu++17%>");
10824 cp_lexer_consume_token (parser->lexer);
10825 cp_lexer_consume_token (parser->lexer);
10826 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10827 pedwarn (input_location, 0,
10828 "already captured %qD in lambda expression",
10829 this_identifier);
10830 else
10831 add_capture (lambda_expr, /*id=*/this_identifier,
10832 /*initializer=*/finish_this_expr (),
10833 /*by_reference_p=*/false, explicit_init_p);
10834 continue;
10835 }
10836
10837 /* But reject `&this'. */
10838 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10839 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10840 {
10841 error_at (cp_lexer_peek_token (parser->lexer)->location,
10842 "%<this%> cannot be captured by reference");
10843 cp_lexer_consume_token (parser->lexer);
10844 cp_lexer_consume_token (parser->lexer);
10845 continue;
10846 }
10847
10848 /* Remember whether we want to capture as a reference or not. */
10849 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10850 {
10851 capture_kind = BY_REFERENCE;
10852 cp_lexer_consume_token (parser->lexer);
10853 }
10854
10855 bool init_pack_expansion = false;
10856 location_t ellipsis_loc = UNKNOWN_LOCATION;
10857 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10858 {
10859 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
10860 if (cxx_dialect < cxx20)
10861 pedwarn (ellipsis_loc, 0, "pack init-capture only available with "
10862 "%<-std=c++20%> or %<-std=gnu++20%>");
10863 cp_lexer_consume_token (parser->lexer);
10864 init_pack_expansion = true;
10865 }
10866
10867 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
10868 if (init_pack_expansion && capture_kind != BY_REFERENCE
10869 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
10870 {
10871 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
10872 0, "%<&%> should come before %<...%>");
10873 capture_kind = BY_REFERENCE;
10874 cp_lexer_consume_token (parser->lexer);
10875 }
10876
10877 /* Get the identifier. */
10878 capture_token = cp_lexer_peek_token (parser->lexer);
10879 capture_id = cp_parser_identifier (parser);
10880
10881 if (capture_id == error_mark_node)
10882 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10883 delimiters, but I modified this to stop on unnested ']' as well. It
10884 was already changed to stop on unnested '}', so the
10885 "closing_parenthesis" name is no more misleading with my change. */
10886 {
10887 cp_parser_skip_to_closing_parenthesis (parser,
10888 /*recovering=*/true,
10889 /*or_comma=*/true,
10890 /*consume_paren=*/true);
10891 break;
10892 }
10893
10894 /* Find the initializer for this capture. */
10895 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10896 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10897 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10898 {
10899 bool direct, non_constant;
10900 /* An explicit initializer exists. */
10901 if (cxx_dialect < cxx14)
10902 pedwarn (input_location, 0,
10903 "lambda capture initializers "
10904 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10905 capture_init_expr = cp_parser_initializer (parser, &direct,
10906 &non_constant, true);
10907 explicit_init_p = true;
10908 if (capture_init_expr == NULL_TREE)
10909 {
10910 error ("empty initializer for lambda init-capture");
10911 capture_init_expr = error_mark_node;
10912 }
10913 if (init_pack_expansion)
10914 capture_init_expr = make_pack_expansion (capture_init_expr);
10915 }
10916 else
10917 {
10918 const char* error_msg;
10919
10920 /* Turn the identifier into an id-expression. */
10921 capture_init_expr
10922 = cp_parser_lookup_name_simple (parser, capture_id,
10923 capture_token->location);
10924
10925 if (capture_init_expr == error_mark_node)
10926 {
10927 unqualified_name_lookup_error (capture_id);
10928 continue;
10929 }
10930 else if (!VAR_P (capture_init_expr)
10931 && TREE_CODE (capture_init_expr) != PARM_DECL)
10932 {
10933 error_at (capture_token->location,
10934 "capture of non-variable %qE",
10935 capture_init_expr);
10936 if (DECL_P (capture_init_expr))
10937 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10938 "%q#D declared here", capture_init_expr);
10939 continue;
10940 }
10941 if (VAR_P (capture_init_expr)
10942 && decl_storage_duration (capture_init_expr) != dk_auto)
10943 {
10944 if (pedwarn (capture_token->location, 0, "capture of variable "
10945 "%qD with non-automatic storage duration",
10946 capture_init_expr))
10947 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10948 "%q#D declared here", capture_init_expr);
10949 continue;
10950 }
10951
10952 capture_init_expr
10953 = finish_id_expression
10954 (capture_id,
10955 capture_init_expr,
10956 parser->scope,
10957 &idk,
10958 /*integral_constant_expression_p=*/false,
10959 /*allow_non_integral_constant_expression_p=*/false,
10960 /*non_integral_constant_expression_p=*/NULL,
10961 /*template_p=*/false,
10962 /*done=*/true,
10963 /*address_p=*/false,
10964 /*template_arg_p=*/false,
10965 &error_msg,
10966 capture_token->location);
10967
10968 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10969 {
10970 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10971 cp_lexer_consume_token (parser->lexer);
10972 capture_init_expr = make_pack_expansion (capture_init_expr);
10973 if (init_pack_expansion)
10974 {
10975 /* If what follows is an initializer, the second '...' is
10976 invalid. But for cases like [...xs...], the first one
10977 is invalid. */
10978 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10979 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10980 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10981 ellipsis_loc = loc;
10982 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
10983 continue;
10984 }
10985 }
10986 }
10987
10988 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10989 && !explicit_init_p)
10990 {
10991 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10992 && capture_kind == BY_COPY)
10993 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10994 "of %qD redundant with by-copy capture default",
10995 capture_id);
10996 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10997 && capture_kind == BY_REFERENCE)
10998 pedwarn (capture_token->location, 0, "explicit by-reference "
10999 "capture of %qD redundant with by-reference capture "
11000 "default", capture_id);
11001 }
11002
11003 /* Check for duplicates.
11004 Optimize for the zero or one explicit captures cases and only create
11005 the hash_set after adding second capture. */
11006 bool found = false;
11007 if (!ids.is_empty ())
11008 found = ids.add (capture_id);
11009 else if (first_capture_id == NULL_TREE)
11010 first_capture_id = capture_id;
11011 else if (capture_id == first_capture_id)
11012 found = true;
11013 else
11014 {
11015 ids.add (first_capture_id);
11016 ids.add (capture_id);
11017 }
11018 if (found)
11019 pedwarn (input_location, 0,
11020 "already captured %qD in lambda expression", capture_id);
11021 else
11022 add_capture (lambda_expr, capture_id, capture_init_expr,
11023 /*by_reference_p=*/capture_kind == BY_REFERENCE,
11024 explicit_init_p);
11025
11026 /* If there is any qualification still in effect, clear it
11027 now; we will be starting fresh with the next capture. */
11028 parser->scope = NULL_TREE;
11029 parser->qualifying_scope = NULL_TREE;
11030 parser->object_scope = NULL_TREE;
11031 }
11032
11033 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11034 }
11035
11036 /* Parse the (optional) middle of a lambda expression.
11037
11038 lambda-declarator:
11039 < template-parameter-list [opt] >
11040 requires-clause [opt]
11041 ( parameter-declaration-clause [opt] )
11042 attribute-specifier [opt]
11043 decl-specifier-seq [opt]
11044 exception-specification [opt]
11045 lambda-return-type-clause [opt]
11046 requires-clause [opt]
11047
11048 LAMBDA_EXPR is the current representation of the lambda expression. */
11049
11050 static bool
11051 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11052 {
11053 /* 5.1.1.4 of the standard says:
11054 If a lambda-expression does not include a lambda-declarator, it is as if
11055 the lambda-declarator were ().
11056 This means an empty parameter list, no attributes, and no exception
11057 specification. */
11058 tree param_list = void_list_node;
11059 tree std_attrs = NULL_TREE;
11060 tree gnu_attrs = NULL_TREE;
11061 tree exception_spec = NULL_TREE;
11062 tree template_param_list = NULL_TREE;
11063 tree tx_qual = NULL_TREE;
11064 tree return_type = NULL_TREE;
11065 tree trailing_requires_clause = NULL_TREE;
11066 cp_decl_specifier_seq lambda_specs;
11067 clear_decl_specs (&lambda_specs);
11068 /* A lambda op() is const unless explicitly 'mutable'. */
11069 cp_cv_quals quals = TYPE_QUAL_CONST;
11070
11071 /* The template-parameter-list is optional, but must begin with
11072 an opening angle if present. */
11073 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11074 {
11075 if (cxx_dialect < cxx14)
11076 pedwarn (parser->lexer->next_token->location, 0,
11077 "lambda templates are only available with "
11078 "%<-std=c++14%> or %<-std=gnu++14%>");
11079 else if (cxx_dialect < cxx20)
11080 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
11081 "lambda templates are only available with "
11082 "%<-std=c++20%> or %<-std=gnu++20%>");
11083
11084 cp_lexer_consume_token (parser->lexer);
11085
11086 template_param_list = cp_parser_template_parameter_list (parser);
11087 cp_parser_skip_to_end_of_template_parameter_list (parser);
11088
11089 /* We may have a constrained generic lambda; parse the requires-clause
11090 immediately after the template-parameter-list and combine with any
11091 shorthand constraints present. */
11092 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11093 if (flag_concepts)
11094 {
11095 tree reqs = get_shorthand_constraints (current_template_parms);
11096 if (dreqs)
11097 reqs = combine_constraint_expressions (reqs, dreqs);
11098 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11099 }
11100
11101 /* We just processed one more parameter list. */
11102 ++parser->num_template_parameter_lists;
11103 }
11104
11105 /* Committee discussion supports allowing attributes here. */
11106 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11107
11108 /* The parameter-declaration-clause is optional (unless
11109 template-parameter-list was given), but must begin with an
11110 opening parenthesis if present. */
11111 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11112 {
11113 bool is_consteval = false;
11114 /* For C++20, before parsing the parameter list check if there is
11115 a consteval specifier in the corresponding decl-specifier-seq. */
11116 if (cxx_dialect >= cxx20)
11117 {
11118 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1);
11119 cp_lexer_nth_token_is (parser->lexer, n, CPP_KEYWORD); n++)
11120 {
11121 if (cp_lexer_peek_nth_token (parser->lexer, n)->keyword
11122 == RID_CONSTEVAL)
11123 {
11124 is_consteval = true;
11125 break;
11126 }
11127 }
11128 }
11129
11130 matching_parens parens;
11131 parens.consume_open (parser);
11132
11133 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11134
11135 if (is_consteval)
11136 current_binding_level->immediate_fn_ctx_p = true;
11137
11138 /* Parse parameters. */
11139 param_list
11140 = cp_parser_parameter_declaration_clause
11141 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11142
11143 /* Default arguments shall not be specified in the
11144 parameter-declaration-clause of a lambda-declarator. */
11145 if (cxx_dialect < cxx14)
11146 for (tree t = param_list; t; t = TREE_CHAIN (t))
11147 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11148 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
11149 "default argument specified for lambda parameter");
11150
11151 parens.require_close (parser);
11152
11153 /* In the decl-specifier-seq of the lambda-declarator, each
11154 decl-specifier shall either be mutable or constexpr. */
11155 int declares_class_or_enum;
11156 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
11157 && !cp_next_tokens_can_be_gnu_attribute_p (parser))
11158 cp_parser_decl_specifier_seq (parser,
11159 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11160 &lambda_specs, &declares_class_or_enum);
11161 if (lambda_specs.storage_class == sc_mutable)
11162 {
11163 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
11164 quals = TYPE_UNQUALIFIED;
11165 if (lambda_specs.conflicting_specifiers_p)
11166 error_at (lambda_specs.locations[ds_storage_class],
11167 "duplicate %<mutable%>");
11168 }
11169
11170 tx_qual = cp_parser_tx_qualifier_opt (parser);
11171
11172 /* Parse optional exception specification. */
11173 exception_spec
11174 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
11175
11176 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11177
11178 /* Parse optional trailing return type. */
11179 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
11180 {
11181 cp_lexer_consume_token (parser->lexer);
11182 return_type = cp_parser_trailing_type_id (parser);
11183 }
11184
11185 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11186 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
11187
11188 /* Parse optional trailing requires clause. */
11189 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
11190
11191 /* The function parameters must be in scope all the way until after the
11192 trailing-return-type in case of decltype. */
11193 pop_bindings_and_leave_scope ();
11194 }
11195 else if (template_param_list != NULL_TREE) // generate diagnostic
11196 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11197
11198 /* Create the function call operator.
11199
11200 Messing with declarators like this is no uglier than building up the
11201 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11202 other code. */
11203 {
11204 cp_decl_specifier_seq return_type_specs;
11205 cp_declarator* declarator;
11206 tree fco;
11207 void *p;
11208
11209 clear_decl_specs (&return_type_specs);
11210 return_type_specs.type = make_auto ();
11211
11212 if (lambda_specs.locations[ds_constexpr])
11213 {
11214 if (cxx_dialect >= cxx17)
11215 return_type_specs.locations[ds_constexpr]
11216 = lambda_specs.locations[ds_constexpr];
11217 else
11218 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11219 "lambda only available with %<-std=c++17%> or "
11220 "%<-std=gnu++17%>");
11221 }
11222 if (lambda_specs.locations[ds_consteval])
11223 return_type_specs.locations[ds_consteval]
11224 = lambda_specs.locations[ds_consteval];
11225
11226 p = obstack_alloc (&declarator_obstack, 0);
11227
11228 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
11229 LAMBDA_EXPR_LOCATION (lambda_expr));
11230
11231 declarator = make_call_declarator (declarator, param_list, quals,
11232 VIRT_SPEC_UNSPECIFIED,
11233 REF_QUAL_NONE,
11234 tx_qual,
11235 exception_spec,
11236 return_type,
11237 trailing_requires_clause);
11238 declarator->std_attributes = std_attrs;
11239
11240 fco = grokmethod (&return_type_specs,
11241 declarator,
11242 chainon (gnu_attrs, lambda_specs.attributes));
11243 if (fco != error_mark_node)
11244 {
11245 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
11246 DECL_ARTIFICIAL (fco) = 1;
11247 /* Give the object parameter a different name. */
11248 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
11249 DECL_SET_LAMBDA_FUNCTION (fco, true);
11250 }
11251 if (template_param_list)
11252 {
11253 fco = finish_member_template_decl (fco);
11254 finish_template_decl (template_param_list);
11255 --parser->num_template_parameter_lists;
11256 }
11257 else if (parser->fully_implicit_function_template_p)
11258 fco = finish_fully_implicit_template (parser, fco);
11259
11260 finish_member_declaration (fco);
11261
11262 obstack_free (&declarator_obstack, p);
11263
11264 return (fco != error_mark_node);
11265 }
11266 }
11267
11268 /* Parse the body of a lambda expression, which is simply
11269
11270 compound-statement
11271
11272 but which requires special handling.
11273 LAMBDA_EXPR is the current representation of the lambda expression. */
11274
11275 static void
11276 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11277 {
11278 bool nested = (current_function_decl != NULL_TREE);
11279 unsigned char local_variables_forbidden_p
11280 = parser->local_variables_forbidden_p;
11281 bool in_function_body = parser->in_function_body;
11282
11283 /* The body of a lambda-expression is not a subexpression of the enclosing
11284 expression. */
11285 cp_evaluated ev;
11286
11287 if (nested)
11288 push_function_context ();
11289 else
11290 /* Still increment function_depth so that we don't GC in the
11291 middle of an expression. */
11292 ++function_depth;
11293
11294 vec<tree> omp_privatization_save;
11295 save_omp_privatization_clauses (omp_privatization_save);
11296 /* Clear this in case we're in the middle of a default argument. */
11297 parser->local_variables_forbidden_p = 0;
11298 parser->in_function_body = true;
11299
11300 {
11301 local_specialization_stack s (lss_copy);
11302 tree fco = lambda_function (lambda_expr);
11303 tree body = start_lambda_function (fco, lambda_expr);
11304
11305 /* Originally C++11 required us to peek for 'return expr'; and
11306 process it specially here to deduce the return type. N3638
11307 removed the need for that. */
11308 cp_parser_function_body (parser, false);
11309
11310 finish_lambda_function (body);
11311 }
11312
11313 restore_omp_privatization_clauses (omp_privatization_save);
11314 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11315 parser->in_function_body = in_function_body;
11316 if (nested)
11317 pop_function_context();
11318 else
11319 --function_depth;
11320 }
11321
11322 /* Statements [gram.stmt.stmt] */
11323
11324 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11325
11326 static void
11327 add_debug_begin_stmt (location_t loc)
11328 {
11329 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11330 return;
11331 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11332 /* A concept is never expanded normally. */
11333 return;
11334
11335 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11336 SET_EXPR_LOCATION (stmt, loc);
11337 add_stmt (stmt);
11338 }
11339
11340 /* Parse a statement.
11341
11342 statement:
11343 labeled-statement
11344 expression-statement
11345 compound-statement
11346 selection-statement
11347 iteration-statement
11348 jump-statement
11349 declaration-statement
11350 try-block
11351
11352 C++11:
11353
11354 statement:
11355 labeled-statement
11356 attribute-specifier-seq (opt) expression-statement
11357 attribute-specifier-seq (opt) compound-statement
11358 attribute-specifier-seq (opt) selection-statement
11359 attribute-specifier-seq (opt) iteration-statement
11360 attribute-specifier-seq (opt) jump-statement
11361 declaration-statement
11362 attribute-specifier-seq (opt) try-block
11363
11364 init-statement:
11365 expression-statement
11366 simple-declaration
11367
11368 TM Extension:
11369
11370 statement:
11371 atomic-statement
11372
11373 IN_COMPOUND is true when the statement is nested inside a
11374 cp_parser_compound_statement; this matters for certain pragmas.
11375
11376 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11377 is a (possibly labeled) if statement which is not enclosed in braces
11378 and has an else clause. This is used to implement -Wparentheses.
11379
11380 CHAIN is a vector of if-else-if conditions. */
11381
11382 static void
11383 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11384 bool in_compound, bool *if_p, vec<tree> *chain,
11385 location_t *loc_after_labels)
11386 {
11387 tree statement, std_attrs = NULL_TREE;
11388 cp_token *token;
11389 location_t statement_location, attrs_loc;
11390
11391 restart:
11392 if (if_p != NULL)
11393 *if_p = false;
11394 /* There is no statement yet. */
11395 statement = NULL_TREE;
11396
11397 saved_token_sentinel saved_tokens (parser->lexer);
11398 attrs_loc = cp_lexer_peek_token (parser->lexer)->location;
11399 if (c_dialect_objc ())
11400 /* In obj-c++, seeing '[[' might be the either the beginning of
11401 c++11 attributes, or a nested objc-message-expression. So
11402 let's parse the c++11 attributes tentatively. */
11403 cp_parser_parse_tentatively (parser);
11404 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11405 if (std_attrs)
11406 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
11407 if (c_dialect_objc ())
11408 {
11409 if (!cp_parser_parse_definitely (parser))
11410 std_attrs = NULL_TREE;
11411 }
11412
11413 /* Peek at the next token. */
11414 token = cp_lexer_peek_token (parser->lexer);
11415 /* Remember the location of the first token in the statement. */
11416 cp_token *statement_token = token;
11417 statement_location = token->location;
11418 add_debug_begin_stmt (statement_location);
11419 /* If this is a keyword, then that will often determine what kind of
11420 statement we have. */
11421 if (token->type == CPP_KEYWORD)
11422 {
11423 enum rid keyword = token->keyword;
11424
11425 switch (keyword)
11426 {
11427 case RID_CASE:
11428 case RID_DEFAULT:
11429 /* Looks like a labeled-statement with a case label.
11430 Parse the label, and then use tail recursion to parse
11431 the statement. */
11432 cp_parser_label_for_labeled_statement (parser, std_attrs);
11433 in_compound = false;
11434 goto restart;
11435
11436 case RID_IF:
11437 case RID_SWITCH:
11438 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11439 statement = cp_parser_selection_statement (parser, if_p, chain);
11440 break;
11441
11442 case RID_WHILE:
11443 case RID_DO:
11444 case RID_FOR:
11445 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11446 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11447 break;
11448
11449 case RID_BREAK:
11450 case RID_CONTINUE:
11451 case RID_RETURN:
11452 case RID_CO_RETURN:
11453 case RID_GOTO:
11454 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11455 statement = cp_parser_jump_statement (parser);
11456 break;
11457
11458 /* Objective-C++ exception-handling constructs. */
11459 case RID_AT_TRY:
11460 case RID_AT_CATCH:
11461 case RID_AT_FINALLY:
11462 case RID_AT_SYNCHRONIZED:
11463 case RID_AT_THROW:
11464 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11465 statement = cp_parser_objc_statement (parser);
11466 break;
11467
11468 case RID_TRY:
11469 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11470 statement = cp_parser_try_block (parser);
11471 break;
11472
11473 case RID_NAMESPACE:
11474 /* This must be a namespace alias definition. */
11475 if (std_attrs != NULL_TREE)
11476 {
11477 /* Attributes should be parsed as part of the
11478 declaration, so let's un-parse them. */
11479 saved_tokens.rollback();
11480 std_attrs = NULL_TREE;
11481 }
11482 cp_parser_declaration_statement (parser);
11483 return;
11484
11485 case RID_TRANSACTION_ATOMIC:
11486 case RID_TRANSACTION_RELAXED:
11487 case RID_SYNCHRONIZED:
11488 case RID_ATOMIC_NOEXCEPT:
11489 case RID_ATOMIC_CANCEL:
11490 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11491 statement = cp_parser_transaction (parser, token);
11492 break;
11493 case RID_TRANSACTION_CANCEL:
11494 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11495 statement = cp_parser_transaction_cancel (parser);
11496 break;
11497
11498 default:
11499 /* It might be a keyword like `int' that can start a
11500 declaration-statement. */
11501 break;
11502 }
11503 }
11504 else if (token->type == CPP_NAME)
11505 {
11506 /* If the next token is a `:', then we are looking at a
11507 labeled-statement. */
11508 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11509 if (token->type == CPP_COLON)
11510 {
11511 /* Looks like a labeled-statement with an ordinary label.
11512 Parse the label, and then use tail recursion to parse
11513 the statement. */
11514
11515 cp_parser_label_for_labeled_statement (parser, std_attrs);
11516 in_compound = false;
11517 goto restart;
11518 }
11519 }
11520 /* Anything that starts with a `{' must be a compound-statement. */
11521 else if (token->type == CPP_OPEN_BRACE)
11522 {
11523 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11524 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11525 }
11526 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11527 a statement all its own. */
11528 else if (token->type == CPP_PRAGMA)
11529 {
11530 /* Only certain OpenMP pragmas are attached to statements, and thus
11531 are considered statements themselves. All others are not. In
11532 the context of a compound, accept the pragma as a "statement" and
11533 return so that we can check for a close brace. Otherwise we
11534 require a real statement and must go back and read one. */
11535 if (in_compound)
11536 cp_parser_pragma (parser, pragma_compound, if_p);
11537 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11538 goto restart;
11539 return;
11540 }
11541 else if (token->type == CPP_EOF)
11542 {
11543 cp_parser_error (parser, "expected statement");
11544 return;
11545 }
11546
11547 /* Everything else must be a declaration-statement or an
11548 expression-statement. Try for the declaration-statement
11549 first, unless we are looking at a `;', in which case we know that
11550 we have an expression-statement. */
11551 if (!statement)
11552 {
11553 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11554 {
11555 if (std_attrs != NULL_TREE)
11556 /* Attributes should be parsed as part of the declaration,
11557 so let's un-parse them. */
11558 saved_tokens.rollback();
11559
11560 cp_parser_parse_tentatively (parser);
11561 /* Try to parse the declaration-statement. */
11562 cp_parser_declaration_statement (parser);
11563 /* If that worked, we're done. */
11564 if (cp_parser_parse_definitely (parser))
11565 return;
11566 /* It didn't work, restore the post-attribute position. */
11567 if (std_attrs)
11568 cp_lexer_set_token_position (parser->lexer, statement_token);
11569 }
11570 /* All preceding labels have been parsed at this point. */
11571 if (loc_after_labels != NULL)
11572 *loc_after_labels = statement_location;
11573
11574 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11575
11576 /* Look for an expression-statement instead. */
11577 statement = cp_parser_expression_statement (parser, in_statement_expr);
11578
11579 /* Handle [[fallthrough]];. */
11580 if (attribute_fallthrough_p (std_attrs))
11581 {
11582 /* The next token after the fallthrough attribute is ';'. */
11583 if (statement == NULL_TREE)
11584 {
11585 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11586 statement = build_call_expr_internal_loc (statement_location,
11587 IFN_FALLTHROUGH,
11588 void_type_node, 0);
11589 finish_expr_stmt (statement);
11590 }
11591 else
11592 warning_at (statement_location, OPT_Wattributes,
11593 "%<fallthrough%> attribute not followed by %<;%>");
11594 std_attrs = NULL_TREE;
11595 }
11596 }
11597
11598 /* Set the line number for the statement. */
11599 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11600 SET_EXPR_LOCATION (statement, statement_location);
11601
11602 /* Allow "[[fallthrough]];", but warn otherwise. */
11603 if (std_attrs != NULL_TREE)
11604 warning_at (attrs_loc,
11605 OPT_Wattributes,
11606 "attributes at the beginning of statement are ignored");
11607 }
11608
11609 /* Append ATTR to attribute list ATTRS. */
11610
11611 static tree
11612 attr_chainon (tree attrs, tree attr)
11613 {
11614 if (attrs == error_mark_node)
11615 return error_mark_node;
11616 if (attr == error_mark_node)
11617 return error_mark_node;
11618 return chainon (attrs, attr);
11619 }
11620
11621 /* Parse the label for a labeled-statement, i.e.
11622
11623 identifier :
11624 case constant-expression :
11625 default :
11626
11627 GNU Extension:
11628 case constant-expression ... constant-expression : statement
11629
11630 When a label is parsed without errors, the label is added to the
11631 parse tree by the finish_* functions, so this function doesn't
11632 have to return the label. */
11633
11634 static void
11635 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11636 {
11637 cp_token *token;
11638 tree label = NULL_TREE;
11639 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11640
11641 /* The next token should be an identifier. */
11642 token = cp_lexer_peek_token (parser->lexer);
11643 if (token->type != CPP_NAME
11644 && token->type != CPP_KEYWORD)
11645 {
11646 cp_parser_error (parser, "expected labeled-statement");
11647 return;
11648 }
11649
11650 /* Remember whether this case or a user-defined label is allowed to fall
11651 through to. */
11652 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11653
11654 parser->colon_corrects_to_scope_p = false;
11655 switch (token->keyword)
11656 {
11657 case RID_CASE:
11658 {
11659 tree expr, expr_hi;
11660 cp_token *ellipsis;
11661
11662 /* Consume the `case' token. */
11663 cp_lexer_consume_token (parser->lexer);
11664 /* Parse the constant-expression. */
11665 expr = cp_parser_constant_expression (parser);
11666 if (check_for_bare_parameter_packs (expr))
11667 expr = error_mark_node;
11668
11669 ellipsis = cp_lexer_peek_token (parser->lexer);
11670 if (ellipsis->type == CPP_ELLIPSIS)
11671 {
11672 /* Consume the `...' token. */
11673 cp_lexer_consume_token (parser->lexer);
11674 expr_hi = cp_parser_constant_expression (parser);
11675 if (check_for_bare_parameter_packs (expr_hi))
11676 expr_hi = error_mark_node;
11677
11678 /* We don't need to emit warnings here, as the common code
11679 will do this for us. */
11680 }
11681 else
11682 expr_hi = NULL_TREE;
11683
11684 if (parser->in_switch_statement_p)
11685 {
11686 tree l = finish_case_label (token->location, expr, expr_hi);
11687 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11688 {
11689 label = CASE_LABEL (l);
11690 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11691 }
11692 }
11693 else
11694 error_at (token->location,
11695 "case label %qE not within a switch statement",
11696 expr);
11697 }
11698 break;
11699
11700 case RID_DEFAULT:
11701 /* Consume the `default' token. */
11702 cp_lexer_consume_token (parser->lexer);
11703
11704 if (parser->in_switch_statement_p)
11705 {
11706 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11707 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11708 {
11709 label = CASE_LABEL (l);
11710 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11711 }
11712 }
11713 else
11714 error_at (token->location, "case label not within a switch statement");
11715 break;
11716
11717 default:
11718 /* Anything else must be an ordinary label. */
11719 label = finish_label_stmt (cp_parser_identifier (parser));
11720 if (label && TREE_CODE (label) == LABEL_DECL)
11721 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11722 break;
11723 }
11724
11725 /* Require the `:' token. */
11726 cp_parser_require (parser, CPP_COLON, RT_COLON);
11727
11728 /* An ordinary label may optionally be followed by attributes.
11729 However, this is only permitted if the attributes are then
11730 followed by a semicolon. This is because, for backward
11731 compatibility, when parsing
11732 lab: __attribute__ ((unused)) int i;
11733 we want the attribute to attach to "i", not "lab". */
11734 if (label != NULL_TREE
11735 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11736 {
11737 tree attrs;
11738 cp_parser_parse_tentatively (parser);
11739 attrs = cp_parser_gnu_attributes_opt (parser);
11740 if (attrs == NULL_TREE
11741 /* And fallthrough always binds to the expression-statement. */
11742 || attribute_fallthrough_p (attrs)
11743 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11744 cp_parser_abort_tentative_parse (parser);
11745 else if (!cp_parser_parse_definitely (parser))
11746 ;
11747 else
11748 attributes = attr_chainon (attributes, attrs);
11749 }
11750
11751 if (attributes != NULL_TREE)
11752 cplus_decl_attributes (&label, attributes, 0);
11753
11754 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11755 }
11756
11757 /* Parse an expression-statement.
11758
11759 expression-statement:
11760 expression [opt] ;
11761
11762 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11763 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11764 indicates whether this expression-statement is part of an
11765 expression statement. */
11766
11767 static tree
11768 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11769 {
11770 tree statement = NULL_TREE;
11771 cp_token *token = cp_lexer_peek_token (parser->lexer);
11772 location_t loc = token->location;
11773
11774 /* There might be attribute fallthrough. */
11775 tree attr = cp_parser_gnu_attributes_opt (parser);
11776
11777 /* If the next token is a ';', then there is no expression
11778 statement. */
11779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11780 {
11781 statement = cp_parser_expression (parser);
11782 if (statement == error_mark_node
11783 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11784 {
11785 cp_parser_skip_to_end_of_block_or_statement (parser);
11786 return error_mark_node;
11787 }
11788 }
11789
11790 /* Handle [[fallthrough]];. */
11791 if (attribute_fallthrough_p (attr))
11792 {
11793 /* The next token after the fallthrough attribute is ';'. */
11794 if (statement == NULL_TREE)
11795 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11796 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11797 void_type_node, 0);
11798 else
11799 warning_at (loc, OPT_Wattributes,
11800 "%<fallthrough%> attribute not followed by %<;%>");
11801 attr = NULL_TREE;
11802 }
11803
11804 /* Allow "[[fallthrough]];", but warn otherwise. */
11805 if (attr != NULL_TREE)
11806 warning_at (loc, OPT_Wattributes,
11807 "attributes at the beginning of statement are ignored");
11808
11809 /* Give a helpful message for "A<T>::type t;" and the like. */
11810 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11811 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11812 {
11813 if (TREE_CODE (statement) == SCOPE_REF)
11814 error_at (token->location, "need %<typename%> before %qE because "
11815 "%qT is a dependent scope",
11816 statement, TREE_OPERAND (statement, 0));
11817 else if (is_overloaded_fn (statement)
11818 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11819 {
11820 /* A::A a; */
11821 tree fn = get_first_fn (statement);
11822 error_at (token->location,
11823 "%<%T::%D%> names the constructor, not the type",
11824 DECL_CONTEXT (fn), DECL_NAME (fn));
11825 }
11826 }
11827
11828 /* Consume the final `;'. */
11829 cp_parser_consume_semicolon_at_end_of_statement (parser);
11830
11831 if (in_statement_expr
11832 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11833 /* This is the final expression statement of a statement
11834 expression. */
11835 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11836 else if (statement)
11837 statement = finish_expr_stmt (statement);
11838
11839 return statement;
11840 }
11841
11842 /* Parse a compound-statement.
11843
11844 compound-statement:
11845 { statement-seq [opt] }
11846
11847 GNU extension:
11848
11849 compound-statement:
11850 { label-declaration-seq [opt] statement-seq [opt] }
11851
11852 label-declaration-seq:
11853 label-declaration
11854 label-declaration-seq label-declaration
11855
11856 Returns a tree representing the statement. */
11857
11858 static tree
11859 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11860 int bcs_flags, bool function_body)
11861 {
11862 tree compound_stmt;
11863 matching_braces braces;
11864
11865 /* Consume the `{'. */
11866 if (!braces.require_open (parser))
11867 return error_mark_node;
11868 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11869 && !function_body && cxx_dialect < cxx14)
11870 pedwarn (input_location, OPT_Wpedantic,
11871 "compound-statement in %<constexpr%> function");
11872 /* Begin the compound-statement. */
11873 compound_stmt = begin_compound_stmt (bcs_flags);
11874 /* If the next keyword is `__label__' we have a label declaration. */
11875 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11876 cp_parser_label_declaration (parser);
11877 /* Parse an (optional) statement-seq. */
11878 cp_parser_statement_seq_opt (parser, in_statement_expr);
11879
11880 if (function_body)
11881 maybe_splice_retval_cleanup (compound_stmt);
11882
11883 /* Finish the compound-statement. */
11884 finish_compound_stmt (compound_stmt);
11885 /* Consume the `}'. */
11886 braces.require_close (parser);
11887
11888 return compound_stmt;
11889 }
11890
11891 /* Parse an (optional) statement-seq.
11892
11893 statement-seq:
11894 statement
11895 statement-seq [opt] statement */
11896
11897 static void
11898 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11899 {
11900 /* Scan statements until there aren't any more. */
11901 while (true)
11902 {
11903 cp_token *token = cp_lexer_peek_token (parser->lexer);
11904
11905 /* If we are looking at a `}', then we have run out of
11906 statements; the same is true if we have reached the end
11907 of file, or have stumbled upon a stray '@end'. */
11908 if (token->type == CPP_CLOSE_BRACE
11909 || token->type == CPP_EOF
11910 || token->type == CPP_PRAGMA_EOL
11911 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11912 break;
11913
11914 /* If we are in a compound statement and find 'else' then
11915 something went wrong. */
11916 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11917 {
11918 if (parser->in_statement & IN_IF_STMT)
11919 break;
11920 else
11921 {
11922 token = cp_lexer_consume_token (parser->lexer);
11923 error_at (token->location, "%<else%> without a previous %<if%>");
11924 }
11925 }
11926
11927 /* Parse the statement. */
11928 cp_parser_statement (parser, in_statement_expr, true, NULL);
11929 }
11930 }
11931
11932 /* Return true if this is the C++20 version of range-based-for with
11933 init-statement. */
11934
11935 static bool
11936 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11937 {
11938 bool r = false;
11939
11940 /* Save tokens so that we can put them back. */
11941 cp_lexer_save_tokens (parser->lexer);
11942
11943 /* There has to be an unnested ; followed by an unnested :. */
11944 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11945 /*recovering=*/false,
11946 CPP_SEMICOLON,
11947 /*consume_paren=*/false) != -1)
11948 goto out;
11949
11950 /* We found the semicolon, eat it now. */
11951 cp_lexer_consume_token (parser->lexer);
11952
11953 /* Now look for ':' that is not nested in () or {}. */
11954 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11955 /*recovering=*/false,
11956 CPP_COLON,
11957 /*consume_paren=*/false) == -1);
11958
11959 out:
11960 /* Roll back the tokens we skipped. */
11961 cp_lexer_rollback_tokens (parser->lexer);
11962
11963 return r;
11964 }
11965
11966 /* Return true if we're looking at (init; cond), false otherwise. */
11967
11968 static bool
11969 cp_parser_init_statement_p (cp_parser *parser)
11970 {
11971 /* Save tokens so that we can put them back. */
11972 cp_lexer_save_tokens (parser->lexer);
11973
11974 /* Look for ';' that is not nested in () or {}. */
11975 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11976 /*recovering=*/false,
11977 CPP_SEMICOLON,
11978 /*consume_paren=*/false);
11979
11980 /* Roll back the tokens we skipped. */
11981 cp_lexer_rollback_tokens (parser->lexer);
11982
11983 return ret == -1;
11984 }
11985
11986 /* Parse a selection-statement.
11987
11988 selection-statement:
11989 if ( init-statement [opt] condition ) statement
11990 if ( init-statement [opt] condition ) statement else statement
11991 switch ( init-statement [opt] condition ) statement
11992
11993 Returns the new IF_STMT or SWITCH_STMT.
11994
11995 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11996 is a (possibly labeled) if statement which is not enclosed in
11997 braces and has an else clause. This is used to implement
11998 -Wparentheses.
11999
12000 CHAIN is a vector of if-else-if conditions. This is used to implement
12001 -Wduplicated-cond. */
12002
12003 static tree
12004 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
12005 vec<tree> *chain)
12006 {
12007 cp_token *token;
12008 enum rid keyword;
12009 token_indent_info guard_tinfo;
12010
12011 if (if_p != NULL)
12012 *if_p = false;
12013
12014 /* Peek at the next token. */
12015 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
12016 guard_tinfo = get_token_indent_info (token);
12017
12018 /* See what kind of keyword it is. */
12019 keyword = token->keyword;
12020 switch (keyword)
12021 {
12022 case RID_IF:
12023 case RID_SWITCH:
12024 {
12025 tree statement;
12026 tree condition;
12027
12028 bool cx = false;
12029 if (keyword == RID_IF
12030 && cp_lexer_next_token_is_keyword (parser->lexer,
12031 RID_CONSTEXPR))
12032 {
12033 cx = true;
12034 cp_token *tok = cp_lexer_consume_token (parser->lexer);
12035 if (cxx_dialect < cxx17)
12036 pedwarn (tok->location, 0, "%<if constexpr%> only available "
12037 "with %<-std=c++17%> or %<-std=gnu++17%>");
12038 }
12039
12040 /* Look for the `('. */
12041 matching_parens parens;
12042 if (!parens.require_open (parser))
12043 {
12044 cp_parser_skip_to_end_of_statement (parser);
12045 return error_mark_node;
12046 }
12047
12048 /* Begin the selection-statement. */
12049 if (keyword == RID_IF)
12050 {
12051 statement = begin_if_stmt ();
12052 IF_STMT_CONSTEXPR_P (statement) = cx;
12053 }
12054 else
12055 statement = begin_switch_stmt ();
12056
12057 /* Parse the optional init-statement. */
12058 if (cp_parser_init_statement_p (parser))
12059 {
12060 tree decl;
12061 if (cxx_dialect < cxx17)
12062 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12063 "init-statement in selection statements only available "
12064 "with %<-std=c++17%> or %<-std=gnu++17%>");
12065 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12066 {
12067 /* A non-empty init-statement can have arbitrary side
12068 effects. */
12069 delete chain;
12070 chain = NULL;
12071 }
12072 cp_parser_init_statement (parser, &decl);
12073 }
12074
12075 /* Parse the condition. */
12076 condition = cp_parser_condition (parser);
12077 /* Look for the `)'. */
12078 if (!parens.require_close (parser))
12079 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12080 /*consume_paren=*/true);
12081
12082 if (keyword == RID_IF)
12083 {
12084 bool nested_if;
12085 unsigned char in_statement;
12086
12087 /* Add the condition. */
12088 condition = finish_if_stmt_cond (condition, statement);
12089
12090 if (warn_duplicated_cond)
12091 warn_duplicated_cond_add_or_warn (token->location, condition,
12092 &chain);
12093
12094 /* Parse the then-clause. */
12095 in_statement = parser->in_statement;
12096 parser->in_statement |= IN_IF_STMT;
12097
12098 /* Outside a template, the non-selected branch of a constexpr
12099 if is a 'discarded statement', i.e. unevaluated. */
12100 bool was_discarded = in_discarded_stmt;
12101 bool discard_then = (cx && !processing_template_decl
12102 && integer_zerop (condition));
12103 if (discard_then)
12104 {
12105 in_discarded_stmt = true;
12106 ++c_inhibit_evaluation_warnings;
12107 }
12108
12109 cp_parser_implicitly_scoped_statement (parser, &nested_if,
12110 guard_tinfo);
12111
12112 parser->in_statement = in_statement;
12113
12114 finish_then_clause (statement);
12115
12116 if (discard_then)
12117 {
12118 THEN_CLAUSE (statement) = NULL_TREE;
12119 in_discarded_stmt = was_discarded;
12120 --c_inhibit_evaluation_warnings;
12121 }
12122
12123 /* If the next token is `else', parse the else-clause. */
12124 if (cp_lexer_next_token_is_keyword (parser->lexer,
12125 RID_ELSE))
12126 {
12127 bool discard_else = (cx && !processing_template_decl
12128 && integer_nonzerop (condition));
12129 if (discard_else)
12130 {
12131 in_discarded_stmt = true;
12132 ++c_inhibit_evaluation_warnings;
12133 }
12134
12135 guard_tinfo
12136 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12137 /* Consume the `else' keyword. */
12138 cp_lexer_consume_token (parser->lexer);
12139 if (warn_duplicated_cond)
12140 {
12141 if (cp_lexer_next_token_is_keyword (parser->lexer,
12142 RID_IF)
12143 && chain == NULL)
12144 {
12145 /* We've got "if (COND) else if (COND2)". Start
12146 the condition chain and add COND as the first
12147 element. */
12148 chain = new vec<tree> ();
12149 if (!CONSTANT_CLASS_P (condition)
12150 && !TREE_SIDE_EFFECTS (condition))
12151 {
12152 /* Wrap it in a NOP_EXPR so that we can set the
12153 location of the condition. */
12154 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
12155 condition);
12156 SET_EXPR_LOCATION (e, token->location);
12157 chain->safe_push (e);
12158 }
12159 }
12160 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
12161 RID_IF))
12162 {
12163 /* This is if-else without subsequent if. Zap the
12164 condition chain; we would have already warned at
12165 this point. */
12166 delete chain;
12167 chain = NULL;
12168 }
12169 }
12170 begin_else_clause (statement);
12171 /* Parse the else-clause. */
12172 cp_parser_implicitly_scoped_statement (parser, NULL,
12173 guard_tinfo, chain);
12174
12175 finish_else_clause (statement);
12176
12177 /* If we are currently parsing a then-clause, then
12178 IF_P will not be NULL. We set it to true to
12179 indicate that this if statement has an else clause.
12180 This may trigger the Wparentheses warning below
12181 when we get back up to the parent if statement. */
12182 if (if_p != NULL)
12183 *if_p = true;
12184
12185 if (discard_else)
12186 {
12187 ELSE_CLAUSE (statement) = NULL_TREE;
12188 in_discarded_stmt = was_discarded;
12189 --c_inhibit_evaluation_warnings;
12190 }
12191 }
12192 else
12193 {
12194 /* This if statement does not have an else clause. If
12195 NESTED_IF is true, then the then-clause has an if
12196 statement which does have an else clause. We warn
12197 about the potential ambiguity. */
12198 if (nested_if)
12199 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
12200 "suggest explicit braces to avoid ambiguous"
12201 " %<else%>");
12202 if (warn_duplicated_cond)
12203 {
12204 /* We don't need the condition chain anymore. */
12205 delete chain;
12206 chain = NULL;
12207 }
12208 }
12209
12210 /* Now we're all done with the if-statement. */
12211 finish_if_stmt (statement);
12212 }
12213 else
12214 {
12215 bool in_switch_statement_p;
12216 unsigned char in_statement;
12217
12218 /* Add the condition. */
12219 finish_switch_cond (condition, statement);
12220
12221 /* Parse the body of the switch-statement. */
12222 in_switch_statement_p = parser->in_switch_statement_p;
12223 in_statement = parser->in_statement;
12224 parser->in_switch_statement_p = true;
12225 parser->in_statement |= IN_SWITCH_STMT;
12226 cp_parser_implicitly_scoped_statement (parser, if_p,
12227 guard_tinfo);
12228 parser->in_switch_statement_p = in_switch_statement_p;
12229 parser->in_statement = in_statement;
12230
12231 /* Now we're all done with the switch-statement. */
12232 finish_switch_stmt (statement);
12233 }
12234
12235 return statement;
12236 }
12237 break;
12238
12239 default:
12240 cp_parser_error (parser, "expected selection-statement");
12241 return error_mark_node;
12242 }
12243 }
12244
12245 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
12246 If we have seen at least one decl-specifier, and the next token is not
12247 a parenthesis (after "int (" we might be looking at a functional cast)
12248 neither we are dealing with a concept-check expression then we must be
12249 looking at a declaration. */
12250
12251 static void
12252 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
12253 cp_decl_specifier_seq *decl_specs)
12254 {
12255 if (decl_specs->any_specifiers_p
12256 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12257 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12258 && !cp_parser_error_occurred (parser)
12259 && !(decl_specs->type
12260 && TREE_CODE (decl_specs->type) == TYPE_DECL
12261 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
12262 cp_parser_commit_to_tentative_parse (parser);
12263 }
12264
12265 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
12266 The declarator shall not specify a function or an array. Returns
12267 TRUE if the declarator is valid, FALSE otherwise. */
12268
12269 static bool
12270 cp_parser_check_condition_declarator (cp_parser* parser,
12271 cp_declarator *declarator,
12272 location_t loc)
12273 {
12274 if (declarator == cp_error_declarator
12275 || function_declarator_p (declarator)
12276 || declarator->kind == cdk_array)
12277 {
12278 if (declarator == cp_error_declarator)
12279 /* Already complained. */;
12280 else if (declarator->kind == cdk_array)
12281 error_at (loc, "condition declares an array");
12282 else
12283 error_at (loc, "condition declares a function");
12284 if (parser->fully_implicit_function_template_p)
12285 abort_fully_implicit_template (parser);
12286 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
12287 /*or_comma=*/false,
12288 /*consume_paren=*/false);
12289 return false;
12290 }
12291 else
12292 return true;
12293 }
12294
12295 /* Parse a condition.
12296
12297 condition:
12298 expression
12299 type-specifier-seq declarator = initializer-clause
12300 type-specifier-seq declarator braced-init-list
12301
12302 GNU Extension:
12303
12304 condition:
12305 type-specifier-seq declarator asm-specification [opt]
12306 attributes [opt] = assignment-expression
12307
12308 Returns the expression that should be tested. */
12309
12310 static tree
12311 cp_parser_condition (cp_parser* parser)
12312 {
12313 cp_decl_specifier_seq type_specifiers;
12314 const char *saved_message;
12315 int declares_class_or_enum;
12316
12317 /* Try the declaration first. */
12318 cp_parser_parse_tentatively (parser);
12319 /* New types are not allowed in the type-specifier-seq for a
12320 condition. */
12321 saved_message = parser->type_definition_forbidden_message;
12322 parser->type_definition_forbidden_message
12323 = G_("types may not be defined in conditions");
12324 /* Parse the type-specifier-seq. */
12325 cp_parser_decl_specifier_seq (parser,
12326 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
12327 &type_specifiers,
12328 &declares_class_or_enum);
12329 /* Restore the saved message. */
12330 parser->type_definition_forbidden_message = saved_message;
12331
12332 /* Gather the attributes that were provided with the
12333 decl-specifiers. */
12334 tree prefix_attributes = type_specifiers.attributes;
12335
12336 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
12337
12338 /* If all is well, we might be looking at a declaration. */
12339 if (!cp_parser_error_occurred (parser))
12340 {
12341 tree decl;
12342 tree asm_specification;
12343 tree attributes;
12344 cp_declarator *declarator;
12345 tree initializer = NULL_TREE;
12346 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12347
12348 /* Parse the declarator. */
12349 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12350 CP_PARSER_FLAGS_NONE,
12351 /*ctor_dtor_or_conv_p=*/NULL,
12352 /*parenthesized_p=*/NULL,
12353 /*member_p=*/false,
12354 /*friend_p=*/false,
12355 /*static_p=*/false);
12356 /* Parse the attributes. */
12357 attributes = cp_parser_attributes_opt (parser);
12358 /* Parse the asm-specification. */
12359 asm_specification = cp_parser_asm_specification_opt (parser);
12360 /* If the next token is not an `=' or '{', then we might still be
12361 looking at an expression. For example:
12362
12363 if (A(a).x)
12364
12365 looks like a decl-specifier-seq and a declarator -- but then
12366 there is no `=', so this is an expression. */
12367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12368 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12369 cp_parser_simulate_error (parser);
12370
12371 /* If we did see an `=' or '{', then we are looking at a declaration
12372 for sure. */
12373 if (cp_parser_parse_definitely (parser))
12374 {
12375 tree pushed_scope;
12376 bool non_constant_p = false;
12377 int flags = LOOKUP_ONLYCONVERTING;
12378
12379 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12380 return error_mark_node;
12381
12382 /* Create the declaration. */
12383 decl = start_decl (declarator, &type_specifiers,
12384 /*initialized_p=*/true,
12385 attributes, prefix_attributes,
12386 &pushed_scope);
12387
12388 /* Parse the initializer. */
12389 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12390 {
12391 initializer = cp_parser_braced_list (parser, &non_constant_p);
12392 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12393 flags = 0;
12394 }
12395 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12396 {
12397 /* Consume the `='. */
12398 cp_lexer_consume_token (parser->lexer);
12399 initializer = cp_parser_initializer_clause (parser,
12400 &non_constant_p);
12401 }
12402 else
12403 {
12404 cp_parser_error (parser, "expected initializer");
12405 initializer = error_mark_node;
12406 }
12407 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12408 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12409
12410 /* Process the initializer. */
12411 cp_finish_decl (decl,
12412 initializer, !non_constant_p,
12413 asm_specification,
12414 flags);
12415
12416 if (pushed_scope)
12417 pop_scope (pushed_scope);
12418
12419 return convert_from_reference (decl);
12420 }
12421 }
12422 /* If we didn't even get past the declarator successfully, we are
12423 definitely not looking at a declaration. */
12424 else
12425 cp_parser_abort_tentative_parse (parser);
12426
12427 /* Otherwise, we are looking at an expression. */
12428 return cp_parser_expression (parser);
12429 }
12430
12431 /* Parses a for-statement or range-for-statement until the closing ')',
12432 not included. */
12433
12434 static tree
12435 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12436 {
12437 tree init, scope, decl;
12438 bool is_range_for;
12439
12440 /* Begin the for-statement. */
12441 scope = begin_for_scope (&init);
12442
12443 /* Parse the initialization. */
12444 is_range_for = cp_parser_init_statement (parser, &decl);
12445
12446 if (is_range_for)
12447 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12448 false);
12449 else
12450 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12451 }
12452
12453 static tree
12454 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12455 unsigned short unroll)
12456 {
12457 /* Normal for loop */
12458 tree condition = NULL_TREE;
12459 tree expression = NULL_TREE;
12460 tree stmt;
12461
12462 stmt = begin_for_stmt (scope, init);
12463 /* The init-statement has already been parsed in
12464 cp_parser_init_statement, so no work is needed here. */
12465 finish_init_stmt (stmt);
12466
12467 /* If there's a condition, process it. */
12468 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12469 condition = cp_parser_condition (parser);
12470 else if (ivdep)
12471 {
12472 cp_parser_error (parser, "missing loop condition in loop with "
12473 "%<GCC ivdep%> pragma");
12474 condition = error_mark_node;
12475 }
12476 else if (unroll)
12477 {
12478 cp_parser_error (parser, "missing loop condition in loop with "
12479 "%<GCC unroll%> pragma");
12480 condition = error_mark_node;
12481 }
12482 finish_for_cond (condition, stmt, ivdep, unroll);
12483 /* Look for the `;'. */
12484 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12485
12486 /* If there's an expression, process it. */
12487 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12488 expression = cp_parser_expression (parser);
12489 finish_for_expr (expression, stmt);
12490
12491 return stmt;
12492 }
12493
12494 /* Tries to parse a range-based for-statement:
12495
12496 range-based-for:
12497 decl-specifier-seq declarator : expression
12498
12499 The decl-specifier-seq declarator and the `:' are already parsed by
12500 cp_parser_init_statement. If processing_template_decl it returns a
12501 newly created RANGE_FOR_STMT; if not, it is converted to a
12502 regular FOR_STMT. */
12503
12504 static tree
12505 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12506 bool ivdep, unsigned short unroll, bool is_omp)
12507 {
12508 tree stmt, range_expr;
12509 auto_vec <cxx_binding *, 16> bindings;
12510 auto_vec <tree, 16> names;
12511 tree decomp_first_name = NULL_TREE;
12512 unsigned int decomp_cnt = 0;
12513
12514 /* Get the range declaration momentarily out of the way so that
12515 the range expression doesn't clash with it. */
12516 if (range_decl != error_mark_node)
12517 {
12518 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12519 {
12520 tree v = DECL_VALUE_EXPR (range_decl);
12521 /* For decomposition declaration get all of the corresponding
12522 declarations out of the way. */
12523 if (TREE_CODE (v) == ARRAY_REF
12524 && VAR_P (TREE_OPERAND (v, 0))
12525 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12526 {
12527 tree d = range_decl;
12528 range_decl = TREE_OPERAND (v, 0);
12529 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12530 decomp_first_name = d;
12531 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12532 {
12533 tree name = DECL_NAME (d);
12534 names.safe_push (name);
12535 bindings.safe_push (IDENTIFIER_BINDING (name));
12536 IDENTIFIER_BINDING (name)
12537 = IDENTIFIER_BINDING (name)->previous;
12538 }
12539 }
12540 }
12541 if (names.is_empty ())
12542 {
12543 tree name = DECL_NAME (range_decl);
12544 names.safe_push (name);
12545 bindings.safe_push (IDENTIFIER_BINDING (name));
12546 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12547 }
12548 }
12549
12550 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12551 {
12552 bool expr_non_constant_p;
12553 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12554 }
12555 else
12556 range_expr = cp_parser_expression (parser);
12557
12558 /* Put the range declaration(s) back into scope. */
12559 for (unsigned int i = 0; i < names.length (); i++)
12560 {
12561 cxx_binding *binding = bindings[i];
12562 binding->previous = IDENTIFIER_BINDING (names[i]);
12563 IDENTIFIER_BINDING (names[i]) = binding;
12564 }
12565
12566 /* finish_omp_for has its own code for the following, so just
12567 return the range_expr instead. */
12568 if (is_omp)
12569 return range_expr;
12570
12571 /* If in template, STMT is converted to a normal for-statement
12572 at instantiation. If not, it is done just ahead. */
12573 if (processing_template_decl)
12574 {
12575 if (check_for_bare_parameter_packs (range_expr))
12576 range_expr = error_mark_node;
12577 stmt = begin_range_for_stmt (scope, init);
12578 if (ivdep)
12579 RANGE_FOR_IVDEP (stmt) = 1;
12580 if (unroll)
12581 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12582 finish_range_for_decl (stmt, range_decl, range_expr);
12583 if (!type_dependent_expression_p (range_expr)
12584 /* do_auto_deduction doesn't mess with template init-lists. */
12585 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12586 do_range_for_auto_deduction (range_decl, range_expr);
12587 }
12588 else
12589 {
12590 stmt = begin_for_stmt (scope, init);
12591 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12592 decomp_first_name, decomp_cnt, ivdep,
12593 unroll);
12594 }
12595 return stmt;
12596 }
12597
12598 /* Subroutine of cp_convert_range_for: given the initializer expression,
12599 builds up the range temporary. */
12600
12601 static tree
12602 build_range_temp (tree range_expr)
12603 {
12604 tree range_type, range_temp;
12605
12606 /* Find out the type deduced by the declaration
12607 `auto &&__range = range_expr'. */
12608 range_type = cp_build_reference_type (make_auto (), true);
12609 range_type = do_auto_deduction (range_type, range_expr,
12610 type_uses_auto (range_type));
12611
12612 /* Create the __range variable. */
12613 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12614 range_type);
12615 TREE_USED (range_temp) = 1;
12616 DECL_ARTIFICIAL (range_temp) = 1;
12617
12618 return range_temp;
12619 }
12620
12621 /* Used by cp_parser_range_for in template context: we aren't going to
12622 do a full conversion yet, but we still need to resolve auto in the
12623 type of the for-range-declaration if present. This is basically
12624 a shortcut version of cp_convert_range_for. */
12625
12626 static void
12627 do_range_for_auto_deduction (tree decl, tree range_expr)
12628 {
12629 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12630 if (auto_node)
12631 {
12632 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12633 range_temp = convert_from_reference (build_range_temp (range_expr));
12634 iter_type = (cp_parser_perform_range_for_lookup
12635 (range_temp, &begin_dummy, &end_dummy));
12636 if (iter_type)
12637 {
12638 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12639 iter_type);
12640 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12641 RO_UNARY_STAR,
12642 tf_warning_or_error);
12643 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12644 iter_decl, auto_node);
12645 }
12646 }
12647 }
12648
12649 /* Converts a range-based for-statement into a normal
12650 for-statement, as per the definition.
12651
12652 for (RANGE_DECL : RANGE_EXPR)
12653 BLOCK
12654
12655 should be equivalent to:
12656
12657 {
12658 auto &&__range = RANGE_EXPR;
12659 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12660 __begin != __end;
12661 ++__begin)
12662 {
12663 RANGE_DECL = *__begin;
12664 BLOCK
12665 }
12666 }
12667
12668 If RANGE_EXPR is an array:
12669 BEGIN_EXPR = __range
12670 END_EXPR = __range + ARRAY_SIZE(__range)
12671 Else if RANGE_EXPR has a member 'begin' or 'end':
12672 BEGIN_EXPR = __range.begin()
12673 END_EXPR = __range.end()
12674 Else:
12675 BEGIN_EXPR = begin(__range)
12676 END_EXPR = end(__range);
12677
12678 If __range has a member 'begin' but not 'end', or vice versa, we must
12679 still use the second alternative (it will surely fail, however).
12680 When calling begin()/end() in the third alternative we must use
12681 argument dependent lookup, but always considering 'std' as an associated
12682 namespace. */
12683
12684 tree
12685 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12686 tree decomp_first_name, unsigned int decomp_cnt,
12687 bool ivdep, unsigned short unroll)
12688 {
12689 tree begin, end;
12690 tree iter_type, begin_expr, end_expr;
12691 tree condition, expression;
12692
12693 range_expr = mark_lvalue_use (range_expr);
12694
12695 if (range_decl == error_mark_node || range_expr == error_mark_node)
12696 /* If an error happened previously do nothing or else a lot of
12697 unhelpful errors would be issued. */
12698 begin_expr = end_expr = iter_type = error_mark_node;
12699 else
12700 {
12701 tree range_temp;
12702
12703 if (VAR_P (range_expr)
12704 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12705 /* Can't bind a reference to an array of runtime bound. */
12706 range_temp = range_expr;
12707 else
12708 {
12709 range_temp = build_range_temp (range_expr);
12710 pushdecl (range_temp);
12711 cp_finish_decl (range_temp, range_expr,
12712 /*is_constant_init*/false, NULL_TREE,
12713 LOOKUP_ONLYCONVERTING);
12714 range_temp = convert_from_reference (range_temp);
12715 }
12716 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12717 &begin_expr, &end_expr);
12718 }
12719
12720 /* The new for initialization statement. */
12721 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12722 iter_type);
12723 TREE_USED (begin) = 1;
12724 DECL_ARTIFICIAL (begin) = 1;
12725 pushdecl (begin);
12726 cp_finish_decl (begin, begin_expr,
12727 /*is_constant_init*/false, NULL_TREE,
12728 LOOKUP_ONLYCONVERTING);
12729
12730 if (cxx_dialect >= cxx17)
12731 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12732 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12733 TREE_USED (end) = 1;
12734 DECL_ARTIFICIAL (end) = 1;
12735 pushdecl (end);
12736 cp_finish_decl (end, end_expr,
12737 /*is_constant_init*/false, NULL_TREE,
12738 LOOKUP_ONLYCONVERTING);
12739
12740 finish_init_stmt (statement);
12741
12742 /* The new for condition. */
12743 condition = build_x_binary_op (input_location, NE_EXPR,
12744 begin, ERROR_MARK,
12745 end, ERROR_MARK,
12746 NULL, tf_warning_or_error);
12747 finish_for_cond (condition, statement, ivdep, unroll);
12748
12749 /* The new increment expression. */
12750 expression = finish_unary_op_expr (input_location,
12751 PREINCREMENT_EXPR, begin,
12752 tf_warning_or_error);
12753 finish_for_expr (expression, statement);
12754
12755 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12756 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12757
12758 /* The declaration is initialized with *__begin inside the loop body. */
12759 cp_finish_decl (range_decl,
12760 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12761 tf_warning_or_error),
12762 /*is_constant_init*/false, NULL_TREE,
12763 LOOKUP_ONLYCONVERTING);
12764 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12765 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12766
12767 return statement;
12768 }
12769
12770 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12771 We need to solve both at the same time because the method used
12772 depends on the existence of members begin or end.
12773 Returns the type deduced for the iterator expression. */
12774
12775 static tree
12776 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12777 {
12778 if (error_operand_p (range))
12779 {
12780 *begin = *end = error_mark_node;
12781 return error_mark_node;
12782 }
12783
12784 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12785 {
12786 error ("range-based %<for%> expression of type %qT "
12787 "has incomplete type", TREE_TYPE (range));
12788 *begin = *end = error_mark_node;
12789 return error_mark_node;
12790 }
12791 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12792 {
12793 /* If RANGE is an array, we will use pointer arithmetic. */
12794 *begin = decay_conversion (range, tf_warning_or_error);
12795 *end = build_binary_op (input_location, PLUS_EXPR,
12796 range,
12797 array_type_nelts_top (TREE_TYPE (range)),
12798 false);
12799 return TREE_TYPE (*begin);
12800 }
12801 else
12802 {
12803 /* If it is not an array, we must do a bit of magic. */
12804 tree id_begin, id_end;
12805 tree member_begin, member_end;
12806
12807 *begin = *end = error_mark_node;
12808
12809 id_begin = get_identifier ("begin");
12810 id_end = get_identifier ("end");
12811 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12812 /*protect=*/2, /*want_type=*/false,
12813 tf_warning_or_error);
12814 member_end = lookup_member (TREE_TYPE (range), id_end,
12815 /*protect=*/2, /*want_type=*/false,
12816 tf_warning_or_error);
12817
12818 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12819 {
12820 /* Use the member functions. */
12821 *begin = cp_parser_range_for_member_function (range, id_begin);
12822 *end = cp_parser_range_for_member_function (range, id_end);
12823 }
12824 else
12825 {
12826 /* Use global functions with ADL. */
12827 releasing_vec vec;
12828
12829 vec_safe_push (vec, range);
12830
12831 member_begin = perform_koenig_lookup (id_begin, vec,
12832 tf_warning_or_error);
12833 *begin = finish_call_expr (member_begin, &vec, false, true,
12834 tf_warning_or_error);
12835 member_end = perform_koenig_lookup (id_end, vec,
12836 tf_warning_or_error);
12837 *end = finish_call_expr (member_end, &vec, false, true,
12838 tf_warning_or_error);
12839 }
12840
12841 /* Last common checks. */
12842 if (*begin == error_mark_node || *end == error_mark_node)
12843 {
12844 /* If one of the expressions is an error do no more checks. */
12845 *begin = *end = error_mark_node;
12846 return error_mark_node;
12847 }
12848 else if (type_dependent_expression_p (*begin)
12849 || type_dependent_expression_p (*end))
12850 /* Can happen, when, eg, in a template context, Koenig lookup
12851 can't resolve begin/end (c++/58503). */
12852 return NULL_TREE;
12853 else
12854 {
12855 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12856 /* The unqualified type of the __begin and __end temporaries should
12857 be the same, as required by the multiple auto declaration. */
12858 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12859 {
12860 if (cxx_dialect >= cxx17
12861 && (build_x_binary_op (input_location, NE_EXPR,
12862 *begin, ERROR_MARK,
12863 *end, ERROR_MARK,
12864 NULL, tf_none)
12865 != error_mark_node))
12866 /* P0184R0 allows __begin and __end to have different types,
12867 but make sure they are comparable so we can give a better
12868 diagnostic. */;
12869 else
12870 error ("inconsistent begin/end types in range-based %<for%> "
12871 "statement: %qT and %qT",
12872 TREE_TYPE (*begin), TREE_TYPE (*end));
12873 }
12874 return iter_type;
12875 }
12876 }
12877 }
12878
12879 /* Helper function for cp_parser_perform_range_for_lookup.
12880 Builds a tree for RANGE.IDENTIFIER(). */
12881
12882 static tree
12883 cp_parser_range_for_member_function (tree range, tree identifier)
12884 {
12885 tree member, res;
12886
12887 member = finish_class_member_access_expr (range, identifier,
12888 false, tf_warning_or_error);
12889 if (member == error_mark_node)
12890 return error_mark_node;
12891
12892 releasing_vec vec;
12893 res = finish_call_expr (member, &vec,
12894 /*disallow_virtual=*/false,
12895 /*koenig_p=*/false,
12896 tf_warning_or_error);
12897 return res;
12898 }
12899
12900 /* Parse an iteration-statement.
12901
12902 iteration-statement:
12903 while ( condition ) statement
12904 do statement while ( expression ) ;
12905 for ( init-statement condition [opt] ; expression [opt] )
12906 statement
12907
12908 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12909
12910 static tree
12911 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12912 unsigned short unroll)
12913 {
12914 cp_token *token;
12915 enum rid keyword;
12916 tree statement;
12917 unsigned char in_statement;
12918 token_indent_info guard_tinfo;
12919
12920 /* Peek at the next token. */
12921 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12922 if (!token)
12923 return error_mark_node;
12924
12925 guard_tinfo = get_token_indent_info (token);
12926
12927 /* Remember whether or not we are already within an iteration
12928 statement. */
12929 in_statement = parser->in_statement;
12930
12931 /* See what kind of keyword it is. */
12932 keyword = token->keyword;
12933 switch (keyword)
12934 {
12935 case RID_WHILE:
12936 {
12937 tree condition;
12938
12939 /* Begin the while-statement. */
12940 statement = begin_while_stmt ();
12941 /* Look for the `('. */
12942 matching_parens parens;
12943 parens.require_open (parser);
12944 /* Parse the condition. */
12945 condition = cp_parser_condition (parser);
12946 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12947 /* Look for the `)'. */
12948 parens.require_close (parser);
12949 /* Parse the dependent statement. */
12950 parser->in_statement = IN_ITERATION_STMT;
12951 bool prev = note_iteration_stmt_body_start ();
12952 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12953 note_iteration_stmt_body_end (prev);
12954 parser->in_statement = in_statement;
12955 /* We're done with the while-statement. */
12956 finish_while_stmt (statement);
12957 }
12958 break;
12959
12960 case RID_DO:
12961 {
12962 tree expression;
12963
12964 /* Begin the do-statement. */
12965 statement = begin_do_stmt ();
12966 /* Parse the body of the do-statement. */
12967 parser->in_statement = IN_ITERATION_STMT;
12968 bool prev = note_iteration_stmt_body_start ();
12969 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12970 note_iteration_stmt_body_end (prev);
12971 parser->in_statement = in_statement;
12972 finish_do_body (statement);
12973 /* Look for the `while' keyword. */
12974 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12975 /* Look for the `('. */
12976 matching_parens parens;
12977 parens.require_open (parser);
12978 /* Parse the expression. */
12979 expression = cp_parser_expression (parser);
12980 /* We're done with the do-statement. */
12981 finish_do_stmt (expression, statement, ivdep, unroll);
12982 /* Look for the `)'. */
12983 parens.require_close (parser);
12984 /* Look for the `;'. */
12985 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12986 }
12987 break;
12988
12989 case RID_FOR:
12990 {
12991 /* Look for the `('. */
12992 matching_parens parens;
12993 parens.require_open (parser);
12994
12995 statement = cp_parser_for (parser, ivdep, unroll);
12996
12997 /* Look for the `)'. */
12998 parens.require_close (parser);
12999
13000 /* Parse the body of the for-statement. */
13001 parser->in_statement = IN_ITERATION_STMT;
13002 bool prev = note_iteration_stmt_body_start ();
13003 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
13004 note_iteration_stmt_body_end (prev);
13005 parser->in_statement = in_statement;
13006
13007 /* We're done with the for-statement. */
13008 finish_for_stmt (statement);
13009 }
13010 break;
13011
13012 default:
13013 cp_parser_error (parser, "expected iteration-statement");
13014 statement = error_mark_node;
13015 break;
13016 }
13017
13018 return statement;
13019 }
13020
13021 /* Parse a init-statement or the declarator of a range-based-for.
13022 Returns true if a range-based-for declaration is seen.
13023
13024 init-statement:
13025 expression-statement
13026 simple-declaration */
13027
13028 static bool
13029 cp_parser_init_statement (cp_parser *parser, tree *decl)
13030 {
13031 /* If the next token is a `;', then we have an empty
13032 expression-statement. Grammatically, this is also a
13033 simple-declaration, but an invalid one, because it does not
13034 declare anything. Therefore, if we did not handle this case
13035 specially, we would issue an error message about an invalid
13036 declaration. */
13037 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13038 {
13039 bool is_range_for = false;
13040 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13041
13042 /* Try to parse the init-statement. */
13043 if (cp_parser_range_based_for_with_init_p (parser))
13044 {
13045 tree dummy;
13046 cp_parser_parse_tentatively (parser);
13047 /* Parse the declaration. */
13048 cp_parser_simple_declaration (parser,
13049 /*function_definition_allowed_p=*/false,
13050 &dummy);
13051 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13052 if (!cp_parser_parse_definitely (parser))
13053 /* That didn't work, try to parse it as an expression-statement. */
13054 cp_parser_expression_statement (parser, NULL_TREE);
13055
13056 if (cxx_dialect < cxx20)
13057 {
13058 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
13059 "range-based %<for%> loops with initializer only "
13060 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13061 *decl = error_mark_node;
13062 }
13063 }
13064
13065 /* A colon is used in range-based for. */
13066 parser->colon_corrects_to_scope_p = false;
13067
13068 /* We're going to speculatively look for a declaration, falling back
13069 to an expression, if necessary. */
13070 cp_parser_parse_tentatively (parser);
13071 /* Parse the declaration. */
13072 cp_parser_simple_declaration (parser,
13073 /*function_definition_allowed_p=*/false,
13074 decl);
13075 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13076 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13077 {
13078 /* It is a range-for, consume the ':'. */
13079 cp_lexer_consume_token (parser->lexer);
13080 is_range_for = true;
13081 if (cxx_dialect < cxx11)
13082 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
13083 "range-based %<for%> loops only available with "
13084 "%<-std=c++11%> or %<-std=gnu++11%>");
13085 }
13086 else
13087 /* The ';' is not consumed yet because we told
13088 cp_parser_simple_declaration not to. */
13089 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13090
13091 if (cp_parser_parse_definitely (parser))
13092 return is_range_for;
13093 /* If the tentative parse failed, then we shall need to look for an
13094 expression-statement. */
13095 }
13096 /* If we are here, it is an expression-statement. */
13097 cp_parser_expression_statement (parser, NULL_TREE);
13098 return false;
13099 }
13100
13101 /* Parse a jump-statement.
13102
13103 jump-statement:
13104 break ;
13105 continue ;
13106 return expression [opt] ;
13107 return braced-init-list ;
13108 coroutine-return-statement;
13109 goto identifier ;
13110
13111 GNU extension:
13112
13113 jump-statement:
13114 goto * expression ;
13115
13116 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
13117
13118 static tree
13119 cp_parser_jump_statement (cp_parser* parser)
13120 {
13121 tree statement = error_mark_node;
13122 cp_token *token;
13123 enum rid keyword;
13124 unsigned char in_statement;
13125
13126 /* Peek at the next token. */
13127 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
13128 if (!token)
13129 return error_mark_node;
13130
13131 /* See what kind of keyword it is. */
13132 keyword = token->keyword;
13133 switch (keyword)
13134 {
13135 case RID_BREAK:
13136 in_statement = parser->in_statement & ~IN_IF_STMT;
13137 switch (in_statement)
13138 {
13139 case 0:
13140 error_at (token->location, "break statement not within loop or switch");
13141 break;
13142 default:
13143 gcc_assert ((in_statement & IN_SWITCH_STMT)
13144 || in_statement == IN_ITERATION_STMT);
13145 statement = finish_break_stmt ();
13146 if (in_statement == IN_ITERATION_STMT)
13147 break_maybe_infinite_loop ();
13148 break;
13149 case IN_OMP_BLOCK:
13150 error_at (token->location, "invalid exit from OpenMP structured block");
13151 break;
13152 case IN_OMP_FOR:
13153 error_at (token->location, "break statement used with OpenMP for loop");
13154 break;
13155 }
13156 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13157 break;
13158
13159 case RID_CONTINUE:
13160 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
13161 {
13162 case 0:
13163 error_at (token->location, "continue statement not within a loop");
13164 break;
13165 /* Fall through. */
13166 case IN_ITERATION_STMT:
13167 case IN_OMP_FOR:
13168 statement = finish_continue_stmt ();
13169 break;
13170 case IN_OMP_BLOCK:
13171 error_at (token->location, "invalid exit from OpenMP structured block");
13172 break;
13173 default:
13174 gcc_unreachable ();
13175 }
13176 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13177 break;
13178
13179 case RID_CO_RETURN:
13180 case RID_RETURN:
13181 {
13182 tree expr;
13183 bool expr_non_constant_p;
13184
13185 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13186 {
13187 cp_lexer_set_source_position (parser->lexer);
13188 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13189 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
13190 }
13191 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13192 expr = cp_parser_expression (parser);
13193 else
13194 /* If the next token is a `;', then there is no
13195 expression. */
13196 expr = NULL_TREE;
13197 /* Build the return-statement, check co-return first, since type
13198 deduction is not valid there. */
13199 if (keyword == RID_CO_RETURN)
13200 statement = finish_co_return_stmt (token->location, expr);
13201 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
13202 /* Don't deduce from a discarded return statement. */;
13203 else
13204 statement = finish_return_stmt (expr);
13205 /* Look for the final `;'. */
13206 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13207 }
13208 break;
13209
13210 case RID_GOTO:
13211 if (parser->in_function_body
13212 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
13213 {
13214 error ("%<goto%> in %<constexpr%> function");
13215 cp_function_chain->invalid_constexpr = true;
13216 }
13217
13218 /* Create the goto-statement. */
13219 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
13220 {
13221 /* Issue a warning about this use of a GNU extension. */
13222 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
13223 /* Consume the '*' token. */
13224 cp_lexer_consume_token (parser->lexer);
13225 /* Parse the dependent expression. */
13226 finish_goto_stmt (cp_parser_expression (parser));
13227 }
13228 else
13229 finish_goto_stmt (cp_parser_identifier (parser));
13230 /* Look for the final `;'. */
13231 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13232 break;
13233
13234 default:
13235 cp_parser_error (parser, "expected jump-statement");
13236 break;
13237 }
13238
13239 return statement;
13240 }
13241
13242 /* Parse a declaration-statement.
13243
13244 declaration-statement:
13245 block-declaration */
13246
13247 static void
13248 cp_parser_declaration_statement (cp_parser* parser)
13249 {
13250 void *p;
13251
13252 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13253 p = obstack_alloc (&declarator_obstack, 0);
13254
13255 /* Parse the block-declaration. */
13256 cp_parser_block_declaration (parser, /*statement_p=*/true);
13257
13258 /* Free any declarators allocated. */
13259 obstack_free (&declarator_obstack, p);
13260 }
13261
13262 /* Some dependent statements (like `if (cond) statement'), are
13263 implicitly in their own scope. In other words, if the statement is
13264 a single statement (as opposed to a compound-statement), it is
13265 none-the-less treated as if it were enclosed in braces. Any
13266 declarations appearing in the dependent statement are out of scope
13267 after control passes that point. This function parses a statement,
13268 but ensures that is in its own scope, even if it is not a
13269 compound-statement.
13270
13271 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13272 is a (possibly labeled) if statement which is not enclosed in
13273 braces and has an else clause. This is used to implement
13274 -Wparentheses.
13275
13276 CHAIN is a vector of if-else-if conditions. This is used to implement
13277 -Wduplicated-cond.
13278
13279 Returns the new statement. */
13280
13281 static tree
13282 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
13283 const token_indent_info &guard_tinfo,
13284 vec<tree> *chain)
13285 {
13286 tree statement;
13287 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
13288 location_t body_loc_after_labels = UNKNOWN_LOCATION;
13289 token_indent_info body_tinfo
13290 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13291
13292 if (if_p != NULL)
13293 *if_p = false;
13294
13295 /* Mark if () ; with a special NOP_EXPR. */
13296 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13297 {
13298 cp_lexer_consume_token (parser->lexer);
13299 statement = add_stmt (build_empty_stmt (body_loc));
13300
13301 if (guard_tinfo.keyword == RID_IF
13302 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
13303 warning_at (body_loc, OPT_Wempty_body,
13304 "suggest braces around empty body in an %<if%> statement");
13305 else if (guard_tinfo.keyword == RID_ELSE)
13306 warning_at (body_loc, OPT_Wempty_body,
13307 "suggest braces around empty body in an %<else%> statement");
13308 }
13309 /* if a compound is opened, we simply parse the statement directly. */
13310 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13311 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
13312 /* If the token is not a `{', then we must take special action. */
13313 else
13314 {
13315 /* Create a compound-statement. */
13316 statement = begin_compound_stmt (0);
13317 /* Parse the dependent-statement. */
13318 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
13319 &body_loc_after_labels);
13320 /* Finish the dummy compound-statement. */
13321 finish_compound_stmt (statement);
13322 }
13323
13324 token_indent_info next_tinfo
13325 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13326 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13327
13328 if (body_loc_after_labels != UNKNOWN_LOCATION
13329 && next_tinfo.type != CPP_SEMICOLON)
13330 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
13331 guard_tinfo.location, guard_tinfo.keyword);
13332
13333 /* Return the statement. */
13334 return statement;
13335 }
13336
13337 /* For some dependent statements (like `while (cond) statement'), we
13338 have already created a scope. Therefore, even if the dependent
13339 statement is a compound-statement, we do not want to create another
13340 scope. */
13341
13342 static void
13343 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
13344 const token_indent_info &guard_tinfo)
13345 {
13346 /* If the token is a `{', then we must take special action. */
13347 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13348 {
13349 token_indent_info body_tinfo
13350 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13351 location_t loc_after_labels = UNKNOWN_LOCATION;
13352
13353 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
13354 &loc_after_labels);
13355 token_indent_info next_tinfo
13356 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13357 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13358
13359 if (loc_after_labels != UNKNOWN_LOCATION
13360 && next_tinfo.type != CPP_SEMICOLON)
13361 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13362 guard_tinfo.location,
13363 guard_tinfo.keyword);
13364 }
13365 else
13366 {
13367 /* Avoid calling cp_parser_compound_statement, so that we
13368 don't create a new scope. Do everything else by hand. */
13369 matching_braces braces;
13370 braces.require_open (parser);
13371 /* If the next keyword is `__label__' we have a label declaration. */
13372 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13373 cp_parser_label_declaration (parser);
13374 /* Parse an (optional) statement-seq. */
13375 cp_parser_statement_seq_opt (parser, NULL_TREE);
13376 braces.require_close (parser);
13377 }
13378 }
13379
13380 /* Declarations [gram.dcl.dcl] */
13381
13382 /* Parse an optional declaration-sequence.
13383
13384 declaration-seq:
13385 declaration
13386 declaration-seq declaration */
13387
13388 static void
13389 cp_parser_declaration_seq_opt (cp_parser* parser)
13390 {
13391 while (true)
13392 {
13393 cp_token *token = cp_lexer_peek_token (parser->lexer);
13394
13395 if (token->type == CPP_CLOSE_BRACE
13396 || token->type == CPP_EOF)
13397 break;
13398 else
13399 cp_parser_toplevel_declaration (parser);
13400 }
13401 }
13402
13403 /* Parse a declaration.
13404
13405 declaration:
13406 block-declaration
13407 function-definition
13408 template-declaration
13409 explicit-instantiation
13410 explicit-specialization
13411 linkage-specification
13412 namespace-definition
13413
13414 C++17:
13415 deduction-guide
13416
13417 GNU extension:
13418
13419 declaration:
13420 __extension__ declaration */
13421
13422 static void
13423 cp_parser_declaration (cp_parser* parser)
13424 {
13425 int saved_pedantic;
13426
13427 /* Check for the `__extension__' keyword. */
13428 if (cp_parser_extension_opt (parser, &saved_pedantic))
13429 {
13430 /* Parse the qualified declaration. */
13431 cp_parser_declaration (parser);
13432 /* Restore the PEDANTIC flag. */
13433 pedantic = saved_pedantic;
13434
13435 return;
13436 }
13437
13438 /* Try to figure out what kind of declaration is present. */
13439 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
13440 cp_token *token2 = NULL;
13441
13442 if (token1->type != CPP_EOF)
13443 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13444
13445 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13446 void *p = obstack_alloc (&declarator_obstack, 0);
13447
13448 tree attributes = NULL_TREE;
13449
13450 /* If the next token is `extern' and the following token is a string
13451 literal, then we have a linkage specification. */
13452 if (token1->keyword == RID_EXTERN
13453 && cp_parser_is_pure_string_literal (token2))
13454 cp_parser_linkage_specification (parser);
13455 /* If the next token is `template', then we have either a template
13456 declaration, an explicit instantiation, or an explicit
13457 specialization. */
13458 else if (token1->keyword == RID_TEMPLATE)
13459 {
13460 /* `template <>' indicates a template specialization. */
13461 if (token2->type == CPP_LESS
13462 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13463 cp_parser_explicit_specialization (parser);
13464 /* `template <' indicates a template declaration. */
13465 else if (token2->type == CPP_LESS)
13466 cp_parser_template_declaration (parser, /*member_p=*/false);
13467 /* Anything else must be an explicit instantiation. */
13468 else
13469 cp_parser_explicit_instantiation (parser);
13470 }
13471 /* If the next token is `export', then we have a template
13472 declaration. */
13473 else if (token1->keyword == RID_EXPORT)
13474 cp_parser_template_declaration (parser, /*member_p=*/false);
13475 /* If the next token is `extern', 'static' or 'inline' and the one
13476 after that is `template', we have a GNU extended explicit
13477 instantiation directive. */
13478 else if (cp_parser_allow_gnu_extensions_p (parser)
13479 && token2->keyword == RID_TEMPLATE
13480 && (token1->keyword == RID_EXTERN
13481 || token1->keyword == RID_STATIC
13482 || token1->keyword == RID_INLINE))
13483 cp_parser_explicit_instantiation (parser);
13484 /* If the next token is `namespace', check for a named or unnamed
13485 namespace definition. */
13486 else if (token1->keyword == RID_NAMESPACE
13487 && (/* A named namespace definition. */
13488 (token2->type == CPP_NAME
13489 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13490 != CPP_EQ))
13491 || (token2->type == CPP_OPEN_SQUARE
13492 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13493 == CPP_OPEN_SQUARE)
13494 /* An unnamed namespace definition. */
13495 || token2->type == CPP_OPEN_BRACE
13496 || token2->keyword == RID_ATTRIBUTE))
13497 cp_parser_namespace_definition (parser);
13498 /* An inline (associated) namespace definition. */
13499 else if (token2->keyword == RID_NAMESPACE
13500 && token1->keyword == RID_INLINE)
13501 cp_parser_namespace_definition (parser);
13502 /* Objective-C++ declaration/definition. */
13503 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
13504 cp_parser_objc_declaration (parser, NULL_TREE);
13505 else if (c_dialect_objc ()
13506 && token1->keyword == RID_ATTRIBUTE
13507 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13508 cp_parser_objc_declaration (parser, attributes);
13509 /* At this point we may have a template declared by a concept
13510 introduction. */
13511 else if (flag_concepts
13512 && cp_parser_template_declaration_after_export (parser,
13513 /*member_p=*/false))
13514 /* We did. */;
13515 else
13516 /* Try to parse a block-declaration, or a function-definition. */
13517 cp_parser_block_declaration (parser, /*statement_p=*/false);
13518
13519 /* Free any declarators allocated. */
13520 obstack_free (&declarator_obstack, p);
13521 }
13522
13523 /* Parse a namespace-scope declaration. */
13524
13525 static void
13526 cp_parser_toplevel_declaration (cp_parser* parser)
13527 {
13528 cp_token *token = cp_lexer_peek_token (parser->lexer);
13529
13530 if (token->type == CPP_PRAGMA)
13531 /* A top-level declaration can consist solely of a #pragma. A
13532 nested declaration cannot, so this is done here and not in
13533 cp_parser_declaration. (A #pragma at block scope is
13534 handled in cp_parser_statement.) */
13535 cp_parser_pragma (parser, pragma_external, NULL);
13536 else if (token->type == CPP_SEMICOLON)
13537 {
13538 cp_lexer_consume_token (parser->lexer);
13539 /* A declaration consisting of a single semicolon is invalid
13540 * before C++11. Allow it unless we're being pedantic. */
13541 if (cxx_dialect < cxx11)
13542 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13543 }
13544 else
13545 /* Parse the declaration itself. */
13546 cp_parser_declaration (parser);
13547 }
13548
13549 /* Parse a block-declaration.
13550
13551 block-declaration:
13552 simple-declaration
13553 asm-definition
13554 namespace-alias-definition
13555 using-declaration
13556 using-directive
13557
13558 GNU Extension:
13559
13560 block-declaration:
13561 __extension__ block-declaration
13562
13563 C++0x Extension:
13564
13565 block-declaration:
13566 static_assert-declaration
13567
13568 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13569 part of a declaration-statement. */
13570
13571 static void
13572 cp_parser_block_declaration (cp_parser *parser,
13573 bool statement_p)
13574 {
13575 int saved_pedantic;
13576
13577 /* Check for the `__extension__' keyword. */
13578 if (cp_parser_extension_opt (parser, &saved_pedantic))
13579 {
13580 /* Parse the qualified declaration. */
13581 cp_parser_block_declaration (parser, statement_p);
13582 /* Restore the PEDANTIC flag. */
13583 pedantic = saved_pedantic;
13584
13585 return;
13586 }
13587
13588 /* Peek at the next token to figure out which kind of declaration is
13589 present. */
13590 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
13591
13592 /* If the next keyword is `asm', we have an asm-definition. */
13593 if (token1->keyword == RID_ASM)
13594 {
13595 if (statement_p)
13596 cp_parser_commit_to_tentative_parse (parser);
13597 cp_parser_asm_definition (parser);
13598 }
13599 /* If the next keyword is `namespace', we have a
13600 namespace-alias-definition. */
13601 else if (token1->keyword == RID_NAMESPACE)
13602 cp_parser_namespace_alias_definition (parser);
13603 /* If the next keyword is `using', we have a
13604 using-declaration, a using-directive, or an alias-declaration. */
13605 else if (token1->keyword == RID_USING)
13606 {
13607 cp_token *token2;
13608
13609 if (statement_p)
13610 cp_parser_commit_to_tentative_parse (parser);
13611 /* If the token after `using' is `namespace', then we have a
13612 using-directive. */
13613 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13614 if (token2->keyword == RID_NAMESPACE)
13615 cp_parser_using_directive (parser);
13616 /* If the second token after 'using' is '=', then we have an
13617 alias-declaration. */
13618 else if (cxx_dialect >= cxx11
13619 && token2->type == CPP_NAME
13620 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13621 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13622 cp_parser_alias_declaration (parser);
13623 /* Otherwise, it's a using-declaration. */
13624 else
13625 cp_parser_using_declaration (parser,
13626 /*access_declaration_p=*/false);
13627 }
13628 /* If the next keyword is `__label__' we have a misplaced label
13629 declaration. */
13630 else if (token1->keyword == RID_LABEL)
13631 {
13632 cp_lexer_consume_token (parser->lexer);
13633 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13634 cp_parser_skip_to_end_of_statement (parser);
13635 /* If the next token is now a `;', consume it. */
13636 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13637 cp_lexer_consume_token (parser->lexer);
13638 }
13639 /* If the next token is `static_assert' we have a static assertion. */
13640 else if (token1->keyword == RID_STATIC_ASSERT)
13641 cp_parser_static_assert (parser, /*member_p=*/false);
13642 /* Anything else must be a simple-declaration. */
13643 else
13644 cp_parser_simple_declaration (parser, !statement_p,
13645 /*maybe_range_for_decl*/NULL);
13646 }
13647
13648 /* Parse a simple-declaration.
13649
13650 simple-declaration:
13651 decl-specifier-seq [opt] init-declarator-list [opt] ;
13652 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13653 brace-or-equal-initializer ;
13654
13655 init-declarator-list:
13656 init-declarator
13657 init-declarator-list , init-declarator
13658
13659 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13660 function-definition as a simple-declaration.
13661
13662 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13663 parsed declaration if it is an uninitialized single declarator not followed
13664 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13665 if present, will not be consumed. */
13666
13667 static void
13668 cp_parser_simple_declaration (cp_parser* parser,
13669 bool function_definition_allowed_p,
13670 tree *maybe_range_for_decl)
13671 {
13672 cp_decl_specifier_seq decl_specifiers;
13673 int declares_class_or_enum;
13674 bool saw_declarator;
13675 location_t comma_loc = UNKNOWN_LOCATION;
13676 location_t init_loc = UNKNOWN_LOCATION;
13677
13678 if (maybe_range_for_decl)
13679 *maybe_range_for_decl = NULL_TREE;
13680
13681 /* Defer access checks until we know what is being declared; the
13682 checks for names appearing in the decl-specifier-seq should be
13683 done as if we were in the scope of the thing being declared. */
13684 push_deferring_access_checks (dk_deferred);
13685
13686 /* Parse the decl-specifier-seq. We have to keep track of whether
13687 or not the decl-specifier-seq declares a named class or
13688 enumeration type, since that is the only case in which the
13689 init-declarator-list is allowed to be empty.
13690
13691 [dcl.dcl]
13692
13693 In a simple-declaration, the optional init-declarator-list can be
13694 omitted only when declaring a class or enumeration, that is when
13695 the decl-specifier-seq contains either a class-specifier, an
13696 elaborated-type-specifier, or an enum-specifier. */
13697 cp_parser_decl_specifier_seq (parser,
13698 CP_PARSER_FLAGS_OPTIONAL,
13699 &decl_specifiers,
13700 &declares_class_or_enum);
13701 /* We no longer need to defer access checks. */
13702 stop_deferring_access_checks ();
13703
13704 /* In a block scope, a valid declaration must always have a
13705 decl-specifier-seq. By not trying to parse declarators, we can
13706 resolve the declaration/expression ambiguity more quickly. */
13707 if (!function_definition_allowed_p
13708 && !decl_specifiers.any_specifiers_p)
13709 {
13710 cp_parser_error (parser, "expected declaration");
13711 goto done;
13712 }
13713
13714 /* If the next two tokens are both identifiers, the code is
13715 erroneous. The usual cause of this situation is code like:
13716
13717 T t;
13718
13719 where "T" should name a type -- but does not. */
13720 if (!decl_specifiers.any_type_specifiers_p
13721 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13722 {
13723 /* If parsing tentatively, we should commit; we really are
13724 looking at a declaration. */
13725 cp_parser_commit_to_tentative_parse (parser);
13726 /* Give up. */
13727 goto done;
13728 }
13729
13730 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
13731
13732 /* Look for C++17 decomposition declaration. */
13733 for (size_t n = 1; ; n++)
13734 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13735 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13736 continue;
13737 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13738 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13739 && decl_specifiers.any_specifiers_p)
13740 {
13741 tree decl
13742 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13743 maybe_range_for_decl,
13744 &init_loc);
13745
13746 /* The next token should be either a `,' or a `;'. */
13747 cp_token *token = cp_lexer_peek_token (parser->lexer);
13748 /* If it's a `;', we are done. */
13749 if (token->type == CPP_SEMICOLON)
13750 goto finish;
13751 else if (maybe_range_for_decl)
13752 {
13753 if (*maybe_range_for_decl == NULL_TREE)
13754 *maybe_range_for_decl = error_mark_node;
13755 goto finish;
13756 }
13757 /* Anything else is an error. */
13758 else
13759 {
13760 /* If we have already issued an error message we don't need
13761 to issue another one. */
13762 if ((decl != error_mark_node
13763 && DECL_INITIAL (decl) != error_mark_node)
13764 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13765 cp_parser_error (parser, "expected %<;%>");
13766 /* Skip tokens until we reach the end of the statement. */
13767 cp_parser_skip_to_end_of_statement (parser);
13768 /* If the next token is now a `;', consume it. */
13769 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13770 cp_lexer_consume_token (parser->lexer);
13771 goto done;
13772 }
13773 }
13774 else
13775 break;
13776
13777 tree last_type;
13778 bool auto_specifier_p;
13779 /* NULL_TREE if both variable and function declaration are allowed,
13780 error_mark_node if function declaration are not allowed and
13781 a FUNCTION_DECL that should be diagnosed if it is followed by
13782 variable declarations. */
13783 tree auto_function_declaration;
13784
13785 last_type = NULL_TREE;
13786 auto_specifier_p
13787 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13788 auto_function_declaration = NULL_TREE;
13789
13790 /* Keep going until we hit the `;' at the end of the simple
13791 declaration. */
13792 saw_declarator = false;
13793 while (cp_lexer_next_token_is_not (parser->lexer,
13794 CPP_SEMICOLON))
13795 {
13796 cp_token *token;
13797 bool function_definition_p;
13798 tree decl;
13799 tree auto_result = NULL_TREE;
13800
13801 if (saw_declarator)
13802 {
13803 /* If we are processing next declarator, comma is expected */
13804 token = cp_lexer_peek_token (parser->lexer);
13805 gcc_assert (token->type == CPP_COMMA);
13806 cp_lexer_consume_token (parser->lexer);
13807 if (maybe_range_for_decl)
13808 {
13809 *maybe_range_for_decl = error_mark_node;
13810 if (comma_loc == UNKNOWN_LOCATION)
13811 comma_loc = token->location;
13812 }
13813 }
13814 else
13815 saw_declarator = true;
13816
13817 /* Parse the init-declarator. */
13818 decl = cp_parser_init_declarator (parser,
13819 CP_PARSER_FLAGS_NONE,
13820 &decl_specifiers,
13821 /*checks=*/NULL,
13822 function_definition_allowed_p,
13823 /*member_p=*/false,
13824 declares_class_or_enum,
13825 &function_definition_p,
13826 maybe_range_for_decl,
13827 &init_loc,
13828 &auto_result);
13829 /* If an error occurred while parsing tentatively, exit quickly.
13830 (That usually happens when in the body of a function; each
13831 statement is treated as a declaration-statement until proven
13832 otherwise.) */
13833 if (cp_parser_error_occurred (parser))
13834 goto done;
13835
13836 if (auto_specifier_p && cxx_dialect >= cxx14)
13837 {
13838 /* If the init-declarator-list contains more than one
13839 init-declarator, they shall all form declarations of
13840 variables. */
13841 if (auto_function_declaration == NULL_TREE)
13842 auto_function_declaration
13843 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13844 else if (TREE_CODE (decl) == FUNCTION_DECL
13845 || auto_function_declaration != error_mark_node)
13846 {
13847 error_at (decl_specifiers.locations[ds_type_spec],
13848 "non-variable %qD in declaration with more than one "
13849 "declarator with placeholder type",
13850 TREE_CODE (decl) == FUNCTION_DECL
13851 ? decl : auto_function_declaration);
13852 auto_function_declaration = error_mark_node;
13853 }
13854 }
13855
13856 if (auto_result
13857 && (!processing_template_decl || !type_uses_auto (auto_result)))
13858 {
13859 if (last_type
13860 && last_type != error_mark_node
13861 && !same_type_p (auto_result, last_type))
13862 {
13863 /* If the list of declarators contains more than one declarator,
13864 the type of each declared variable is determined as described
13865 above. If the type deduced for the template parameter U is not
13866 the same in each deduction, the program is ill-formed. */
13867 error_at (decl_specifiers.locations[ds_type_spec],
13868 "inconsistent deduction for %qT: %qT and then %qT",
13869 decl_specifiers.type, last_type, auto_result);
13870 last_type = error_mark_node;
13871 }
13872 else
13873 last_type = auto_result;
13874 }
13875
13876 /* Handle function definitions specially. */
13877 if (function_definition_p)
13878 {
13879 /* If the next token is a `,', then we are probably
13880 processing something like:
13881
13882 void f() {}, *p;
13883
13884 which is erroneous. */
13885 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13886 {
13887 cp_token *token = cp_lexer_peek_token (parser->lexer);
13888 error_at (token->location,
13889 "mixing"
13890 " declarations and function-definitions is forbidden");
13891 }
13892 /* Otherwise, we're done with the list of declarators. */
13893 else
13894 {
13895 pop_deferring_access_checks ();
13896 return;
13897 }
13898 }
13899 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13900 *maybe_range_for_decl = decl;
13901 /* The next token should be either a `,' or a `;'. */
13902 token = cp_lexer_peek_token (parser->lexer);
13903 /* If it's a `,', there are more declarators to come. */
13904 if (token->type == CPP_COMMA)
13905 /* will be consumed next time around */;
13906 /* If it's a `;', we are done. */
13907 else if (token->type == CPP_SEMICOLON)
13908 break;
13909 else if (maybe_range_for_decl)
13910 {
13911 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13912 permerror (decl_specifiers.locations[ds_type_spec],
13913 "types may not be defined in a for-range-declaration");
13914 break;
13915 }
13916 /* Anything else is an error. */
13917 else
13918 {
13919 /* If we have already issued an error message we don't need
13920 to issue another one. */
13921 if ((decl != error_mark_node
13922 && DECL_INITIAL (decl) != error_mark_node)
13923 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13924 cp_parser_error (parser, "expected %<,%> or %<;%>");
13925 /* Skip tokens until we reach the end of the statement. */
13926 cp_parser_skip_to_end_of_statement (parser);
13927 /* If the next token is now a `;', consume it. */
13928 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13929 cp_lexer_consume_token (parser->lexer);
13930 goto done;
13931 }
13932 /* After the first time around, a function-definition is not
13933 allowed -- even if it was OK at first. For example:
13934
13935 int i, f() {}
13936
13937 is not valid. */
13938 function_definition_allowed_p = false;
13939 }
13940
13941 /* Issue an error message if no declarators are present, and the
13942 decl-specifier-seq does not itself declare a class or
13943 enumeration: [dcl.dcl]/3. */
13944 if (!saw_declarator)
13945 {
13946 if (cp_parser_declares_only_class_p (parser))
13947 {
13948 if (!declares_class_or_enum
13949 && decl_specifiers.type
13950 && OVERLOAD_TYPE_P (decl_specifiers.type))
13951 /* Ensure an error is issued anyway when finish_decltype_type,
13952 called via cp_parser_decl_specifier_seq, returns a class or
13953 an enumeration (c++/51786). */
13954 decl_specifiers.type = NULL_TREE;
13955 shadow_tag (&decl_specifiers);
13956 }
13957 /* Perform any deferred access checks. */
13958 perform_deferred_access_checks (tf_warning_or_error);
13959 }
13960
13961 /* Consume the `;'. */
13962 finish:
13963 if (!maybe_range_for_decl)
13964 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13965 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13966 {
13967 if (init_loc != UNKNOWN_LOCATION)
13968 error_at (init_loc, "initializer in range-based %<for%> loop");
13969 if (comma_loc != UNKNOWN_LOCATION)
13970 error_at (comma_loc,
13971 "multiple declarations in range-based %<for%> loop");
13972 }
13973
13974 done:
13975 pop_deferring_access_checks ();
13976 }
13977
13978 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13979 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13980 initializer ; */
13981
13982 static tree
13983 cp_parser_decomposition_declaration (cp_parser *parser,
13984 cp_decl_specifier_seq *decl_specifiers,
13985 tree *maybe_range_for_decl,
13986 location_t *init_loc)
13987 {
13988 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13989 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13990 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13991
13992 /* Parse the identifier-list. */
13993 auto_vec<cp_expr, 10> v;
13994 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13995 while (true)
13996 {
13997 cp_expr e = cp_parser_identifier (parser);
13998 if (e.get_value () == error_mark_node)
13999 break;
14000 v.safe_push (e);
14001 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14002 break;
14003 cp_lexer_consume_token (parser->lexer);
14004 }
14005
14006 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
14007 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14008 {
14009 end_loc = UNKNOWN_LOCATION;
14010 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
14011 false);
14012 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
14013 cp_lexer_consume_token (parser->lexer);
14014 else
14015 {
14016 cp_parser_skip_to_end_of_statement (parser);
14017 return error_mark_node;
14018 }
14019 }
14020
14021 if (cxx_dialect < cxx17)
14022 pedwarn (loc, 0, "structured bindings only available with "
14023 "%<-std=c++17%> or %<-std=gnu++17%>");
14024
14025 tree pushed_scope;
14026 cp_declarator *declarator = make_declarator (cdk_decomp);
14027 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
14028 declarator->id_loc = loc;
14029 if (ref_qual != REF_QUAL_NONE)
14030 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
14031 ref_qual == REF_QUAL_RVALUE,
14032 NULL_TREE);
14033 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
14034 NULL_TREE, decl_specifiers->attributes,
14035 &pushed_scope);
14036 tree orig_decl = decl;
14037
14038 unsigned int i;
14039 cp_expr e;
14040 cp_decl_specifier_seq decl_specs;
14041 clear_decl_specs (&decl_specs);
14042 decl_specs.type = make_auto ();
14043 tree prev = decl;
14044 FOR_EACH_VEC_ELT (v, i, e)
14045 {
14046 if (i == 0)
14047 declarator = make_id_declarator (NULL_TREE, e.get_value (),
14048 sfk_none, e.get_location ());
14049 else
14050 {
14051 declarator->u.id.unqualified_name = e.get_value ();
14052 declarator->id_loc = e.get_location ();
14053 }
14054 tree elt_pushed_scope;
14055 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
14056 NULL_TREE, NULL_TREE, &elt_pushed_scope);
14057 if (decl2 == error_mark_node)
14058 decl = error_mark_node;
14059 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
14060 {
14061 /* Ensure we've diagnosed redeclaration if we aren't creating
14062 a new VAR_DECL. */
14063 gcc_assert (errorcount);
14064 decl = error_mark_node;
14065 }
14066 else
14067 prev = decl2;
14068 if (elt_pushed_scope)
14069 pop_scope (elt_pushed_scope);
14070 }
14071
14072 if (v.is_empty ())
14073 {
14074 error_at (loc, "empty structured binding declaration");
14075 decl = error_mark_node;
14076 }
14077
14078 if (maybe_range_for_decl == NULL
14079 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14080 {
14081 bool non_constant_p = false, is_direct_init = false;
14082 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
14083 tree initializer = cp_parser_initializer (parser, &is_direct_init,
14084 &non_constant_p);
14085 if (initializer == NULL_TREE
14086 || (TREE_CODE (initializer) == TREE_LIST
14087 && TREE_CHAIN (initializer))
14088 || (is_direct_init
14089 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
14090 && CONSTRUCTOR_NELTS (initializer) != 1))
14091 {
14092 error_at (loc, "invalid initializer for structured binding "
14093 "declaration");
14094 initializer = error_mark_node;
14095 }
14096
14097 if (decl != error_mark_node)
14098 {
14099 int flags = (decl_spec_seq_has_spec_p (decl_specifiers, ds_constinit)
14100 ? LOOKUP_CONSTINIT : 0);
14101 cp_maybe_mangle_decomp (decl, prev, v.length ());
14102 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
14103 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT)
14104 | flags);
14105 cp_finish_decomp (decl, prev, v.length ());
14106 }
14107 }
14108 else if (decl != error_mark_node)
14109 {
14110 *maybe_range_for_decl = prev;
14111 /* Ensure DECL_VALUE_EXPR is created for all the decls but
14112 the underlying DECL. */
14113 cp_finish_decomp (decl, prev, v.length ());
14114 }
14115
14116 if (pushed_scope)
14117 pop_scope (pushed_scope);
14118
14119 if (decl == error_mark_node && DECL_P (orig_decl))
14120 {
14121 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
14122 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
14123 }
14124
14125 return decl;
14126 }
14127
14128 /* Parse a decl-specifier-seq.
14129
14130 decl-specifier-seq:
14131 decl-specifier-seq [opt] decl-specifier
14132 decl-specifier attribute-specifier-seq [opt] (C++11)
14133
14134 decl-specifier:
14135 storage-class-specifier
14136 type-specifier
14137 function-specifier
14138 friend
14139 typedef
14140
14141 GNU Extension:
14142
14143 decl-specifier:
14144 attributes
14145
14146 Concepts Extension:
14147
14148 decl-specifier:
14149 concept
14150
14151 Set *DECL_SPECS to a representation of the decl-specifier-seq.
14152
14153 The parser flags FLAGS is used to control type-specifier parsing.
14154
14155 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
14156 flags:
14157
14158 1: one of the decl-specifiers is an elaborated-type-specifier
14159 (i.e., a type declaration)
14160 2: one of the decl-specifiers is an enum-specifier or a
14161 class-specifier (i.e., a type definition)
14162
14163 */
14164
14165 static void
14166 cp_parser_decl_specifier_seq (cp_parser* parser,
14167 cp_parser_flags flags,
14168 cp_decl_specifier_seq *decl_specs,
14169 int* declares_class_or_enum)
14170 {
14171 bool constructor_possible_p = !parser->in_declarator_p;
14172 bool found_decl_spec = false;
14173 cp_token *start_token = NULL;
14174 cp_decl_spec ds;
14175
14176 /* Clear DECL_SPECS. */
14177 clear_decl_specs (decl_specs);
14178
14179 /* Assume no class or enumeration type is declared. */
14180 *declares_class_or_enum = 0;
14181
14182 /* Keep reading specifiers until there are no more to read. */
14183 while (true)
14184 {
14185 bool constructor_p;
14186 cp_token *token;
14187 ds = ds_last;
14188
14189 /* Peek at the next token. */
14190 token = cp_lexer_peek_token (parser->lexer);
14191
14192 /* Save the first token of the decl spec list for error
14193 reporting. */
14194 if (!start_token)
14195 start_token = token;
14196 /* Handle attributes. */
14197 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
14198 && cp_next_tokens_can_be_attribute_p (parser))
14199 {
14200 /* Parse the attributes. */
14201 tree attrs = cp_parser_attributes_opt (parser);
14202
14203 /* In a sequence of declaration specifiers, c++11 attributes
14204 appertain to the type that precede them. In that case
14205 [dcl.spec]/1 says:
14206
14207 The attribute-specifier-seq affects the type only for
14208 the declaration it appears in, not other declarations
14209 involving the same type.
14210
14211 But for now let's force the user to position the
14212 attribute either at the beginning of the declaration or
14213 after the declarator-id, which would clearly mean that it
14214 applies to the declarator. */
14215 if (cxx11_attribute_p (attrs))
14216 {
14217 if (!found_decl_spec)
14218 /* The c++11 attribute is at the beginning of the
14219 declaration. It appertains to the entity being
14220 declared. */;
14221 else
14222 {
14223 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
14224 {
14225 /* This is an attribute following a
14226 class-specifier. */
14227 if (decl_specs->type_definition_p)
14228 warn_misplaced_attr_for_class_type (token->location,
14229 decl_specs->type);
14230 attrs = NULL_TREE;
14231 }
14232 else
14233 {
14234 decl_specs->std_attributes
14235 = attr_chainon (decl_specs->std_attributes, attrs);
14236 if (decl_specs->locations[ds_std_attribute] == 0)
14237 decl_specs->locations[ds_std_attribute] = token->location;
14238 }
14239 continue;
14240 }
14241 }
14242
14243 decl_specs->attributes
14244 = attr_chainon (decl_specs->attributes, attrs);
14245 if (decl_specs->locations[ds_attribute] == 0)
14246 decl_specs->locations[ds_attribute] = token->location;
14247 continue;
14248 }
14249 /* Assume we will find a decl-specifier keyword. */
14250 found_decl_spec = true;
14251 /* If the next token is an appropriate keyword, we can simply
14252 add it to the list. */
14253 switch (token->keyword)
14254 {
14255 /* decl-specifier:
14256 friend
14257 constexpr
14258 constinit */
14259 case RID_FRIEND:
14260 if (!at_class_scope_p ())
14261 {
14262 gcc_rich_location richloc (token->location);
14263 richloc.add_fixit_remove ();
14264 error_at (&richloc, "%<friend%> used outside of class");
14265 cp_lexer_purge_token (parser->lexer);
14266 }
14267 else
14268 {
14269 ds = ds_friend;
14270 /* Consume the token. */
14271 cp_lexer_consume_token (parser->lexer);
14272 }
14273 break;
14274
14275 case RID_CONSTEXPR:
14276 ds = ds_constexpr;
14277 cp_lexer_consume_token (parser->lexer);
14278 break;
14279
14280 case RID_CONSTINIT:
14281 ds = ds_constinit;
14282 cp_lexer_consume_token (parser->lexer);
14283 break;
14284
14285 case RID_CONSTEVAL:
14286 ds = ds_consteval;
14287 cp_lexer_consume_token (parser->lexer);
14288 break;
14289
14290 case RID_CONCEPT:
14291 ds = ds_concept;
14292 cp_lexer_consume_token (parser->lexer);
14293
14294 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14295 break;
14296
14297 /* Warn for concept as a decl-specifier. We'll rewrite these as
14298 concept declarations later. */
14299 if (!flag_concepts_ts)
14300 {
14301 cp_token *next = cp_lexer_peek_token (parser->lexer);
14302 if (next->keyword == RID_BOOL)
14303 pedwarn (next->location, 0, "the %<bool%> keyword is not "
14304 "allowed in a C++20 concept definition");
14305 else
14306 pedwarn (token->location, 0, "C++20 concept definition syntax "
14307 "is %<concept <name> = <expr>%>");
14308 }
14309
14310 /* In C++20 a concept definition is just 'concept name = expr;'
14311 Support that syntax as a TS extension by pretending we've seen
14312 the 'bool' specifier. */
14313 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14314 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
14315 && !decl_specs->any_type_specifiers_p)
14316 {
14317 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
14318 token, /*type_definition*/false);
14319 decl_specs->any_type_specifiers_p = true;
14320 }
14321 break;
14322
14323 /* function-specifier:
14324 inline
14325 virtual
14326 explicit */
14327 case RID_INLINE:
14328 case RID_VIRTUAL:
14329 case RID_EXPLICIT:
14330 cp_parser_function_specifier_opt (parser, decl_specs);
14331 break;
14332
14333 /* decl-specifier:
14334 typedef */
14335 case RID_TYPEDEF:
14336 ds = ds_typedef;
14337 /* Consume the token. */
14338 cp_lexer_consume_token (parser->lexer);
14339
14340 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14341 break;
14342
14343 /* A constructor declarator cannot appear in a typedef. */
14344 constructor_possible_p = false;
14345 /* The "typedef" keyword can only occur in a declaration; we
14346 may as well commit at this point. */
14347 cp_parser_commit_to_tentative_parse (parser);
14348
14349 if (decl_specs->storage_class != sc_none)
14350 decl_specs->conflicting_specifiers_p = true;
14351 break;
14352
14353 /* storage-class-specifier:
14354 auto
14355 register
14356 static
14357 extern
14358 mutable
14359
14360 GNU Extension:
14361 thread */
14362 case RID_AUTO:
14363 if (cxx_dialect == cxx98)
14364 {
14365 /* Consume the token. */
14366 cp_lexer_consume_token (parser->lexer);
14367
14368 /* Complain about `auto' as a storage specifier, if
14369 we're complaining about C++0x compatibility. */
14370 gcc_rich_location richloc (token->location);
14371 richloc.add_fixit_remove ();
14372 warning_at (&richloc, OPT_Wc__11_compat,
14373 "%<auto%> changes meaning in C++11; "
14374 "please remove it");
14375
14376 /* Set the storage class anyway. */
14377 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
14378 token);
14379 }
14380 else
14381 /* C++0x auto type-specifier. */
14382 found_decl_spec = false;
14383 break;
14384
14385 case RID_REGISTER:
14386 case RID_STATIC:
14387 case RID_EXTERN:
14388 case RID_MUTABLE:
14389 /* Consume the token. */
14390 cp_lexer_consume_token (parser->lexer);
14391 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
14392 token);
14393 break;
14394 case RID_THREAD:
14395 /* Consume the token. */
14396 ds = ds_thread;
14397 cp_lexer_consume_token (parser->lexer);
14398 break;
14399
14400 default:
14401 /* We did not yet find a decl-specifier yet. */
14402 found_decl_spec = false;
14403 break;
14404 }
14405
14406 if (found_decl_spec
14407 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14408 && token->keyword != RID_CONSTEXPR)
14409 error ("%<decl-specifier%> invalid in condition");
14410
14411 if (found_decl_spec
14412 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14413 && token->keyword != RID_MUTABLE
14414 && token->keyword != RID_CONSTEXPR
14415 && token->keyword != RID_CONSTEVAL)
14416 error_at (token->location, "%qD invalid in lambda",
14417 ridpointers[token->keyword]);
14418
14419 if (ds != ds_last)
14420 set_and_check_decl_spec_loc (decl_specs, ds, token);
14421
14422 /* Constructors are a special case. The `S' in `S()' is not a
14423 decl-specifier; it is the beginning of the declarator. */
14424 constructor_p
14425 = (!found_decl_spec
14426 && constructor_possible_p
14427 && (cp_parser_constructor_declarator_p
14428 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
14429 ds_friend))));
14430
14431 /* If we don't have a DECL_SPEC yet, then we must be looking at
14432 a type-specifier. */
14433 if (!found_decl_spec && !constructor_p)
14434 {
14435 int decl_spec_declares_class_or_enum;
14436 bool is_cv_qualifier;
14437 tree type_spec;
14438
14439 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14440 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14441
14442 type_spec
14443 = cp_parser_type_specifier (parser, flags,
14444 decl_specs,
14445 /*is_declaration=*/true,
14446 &decl_spec_declares_class_or_enum,
14447 &is_cv_qualifier);
14448 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14449
14450 /* If this type-specifier referenced a user-defined type
14451 (a typedef, class-name, etc.), then we can't allow any
14452 more such type-specifiers henceforth.
14453
14454 [dcl.spec]
14455
14456 The longest sequence of decl-specifiers that could
14457 possibly be a type name is taken as the
14458 decl-specifier-seq of a declaration. The sequence shall
14459 be self-consistent as described below.
14460
14461 [dcl.type]
14462
14463 As a general rule, at most one type-specifier is allowed
14464 in the complete decl-specifier-seq of a declaration. The
14465 only exceptions are the following:
14466
14467 -- const or volatile can be combined with any other
14468 type-specifier.
14469
14470 -- signed or unsigned can be combined with char, long,
14471 short, or int.
14472
14473 -- ..
14474
14475 Example:
14476
14477 typedef char* Pc;
14478 void g (const int Pc);
14479
14480 Here, Pc is *not* part of the decl-specifier seq; it's
14481 the declarator. Therefore, once we see a type-specifier
14482 (other than a cv-qualifier), we forbid any additional
14483 user-defined types. We *do* still allow things like `int
14484 int' to be considered a decl-specifier-seq, and issue the
14485 error message later. */
14486 if (type_spec && !is_cv_qualifier)
14487 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14488 /* A constructor declarator cannot follow a type-specifier. */
14489 if (type_spec)
14490 {
14491 constructor_possible_p = false;
14492 found_decl_spec = true;
14493 if (!is_cv_qualifier)
14494 decl_specs->any_type_specifiers_p = true;
14495
14496 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14497 error_at (token->location, "type-specifier invalid in lambda");
14498 }
14499 }
14500
14501 /* If we still do not have a DECL_SPEC, then there are no more
14502 decl-specifiers. */
14503 if (!found_decl_spec)
14504 break;
14505
14506 decl_specs->any_specifiers_p = true;
14507 /* After we see one decl-specifier, further decl-specifiers are
14508 always optional. */
14509 flags |= CP_PARSER_FLAGS_OPTIONAL;
14510 }
14511
14512 /* Don't allow a friend specifier with a class definition. */
14513 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14514 && (*declares_class_or_enum & 2))
14515 error_at (decl_specs->locations[ds_friend],
14516 "class definition may not be declared a friend");
14517 }
14518
14519 /* Parse an (optional) storage-class-specifier.
14520
14521 storage-class-specifier:
14522 auto
14523 register
14524 static
14525 extern
14526 mutable
14527
14528 GNU Extension:
14529
14530 storage-class-specifier:
14531 thread
14532
14533 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14534
14535 static tree
14536 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14537 {
14538 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14539 {
14540 case RID_AUTO:
14541 if (cxx_dialect != cxx98)
14542 return NULL_TREE;
14543 /* Fall through for C++98. */
14544 gcc_fallthrough ();
14545
14546 case RID_REGISTER:
14547 case RID_STATIC:
14548 case RID_EXTERN:
14549 case RID_MUTABLE:
14550 case RID_THREAD:
14551 /* Consume the token. */
14552 return cp_lexer_consume_token (parser->lexer)->u.value;
14553
14554 default:
14555 return NULL_TREE;
14556 }
14557 }
14558
14559 /* Parse an (optional) function-specifier.
14560
14561 function-specifier:
14562 inline
14563 virtual
14564 explicit
14565
14566 C++20 Extension:
14567 explicit(constant-expression)
14568
14569 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14570 Updates DECL_SPECS, if it is non-NULL. */
14571
14572 static tree
14573 cp_parser_function_specifier_opt (cp_parser* parser,
14574 cp_decl_specifier_seq *decl_specs)
14575 {
14576 cp_token *token = cp_lexer_peek_token (parser->lexer);
14577 switch (token->keyword)
14578 {
14579 case RID_INLINE:
14580 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14581 break;
14582
14583 case RID_VIRTUAL:
14584 /* 14.5.2.3 [temp.mem]
14585
14586 A member function template shall not be virtual. */
14587 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14588 && current_class_type)
14589 error_at (token->location, "templates may not be %<virtual%>");
14590 else
14591 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14592 break;
14593
14594 case RID_EXPLICIT:
14595 {
14596 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14597 /* If we see '(', it's C++20 explicit(bool). */
14598 tree expr;
14599 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14600 {
14601 matching_parens parens;
14602 parens.consume_open (parser);
14603
14604 /* New types are not allowed in an explicit-specifier. */
14605 const char *saved_message
14606 = parser->type_definition_forbidden_message;
14607 parser->type_definition_forbidden_message
14608 = G_("types may not be defined in explicit-specifier");
14609
14610 if (cxx_dialect < cxx20)
14611 pedwarn (token->location, 0,
14612 "%<explicit(bool)%> only available with %<-std=c++20%> "
14613 "or %<-std=gnu++20%>");
14614
14615 /* Parse the constant-expression. */
14616 expr = cp_parser_constant_expression (parser);
14617
14618 /* Restore the saved message. */
14619 parser->type_definition_forbidden_message = saved_message;
14620 parens.require_close (parser);
14621 }
14622 else
14623 /* The explicit-specifier explicit without a constant-expression is
14624 equivalent to the explicit-specifier explicit(true). */
14625 expr = boolean_true_node;
14626
14627 /* [dcl.fct.spec]
14628 "the constant-expression, if supplied, shall be a contextually
14629 converted constant expression of type bool." */
14630 expr = build_explicit_specifier (expr, tf_warning_or_error);
14631 /* We could evaluate it -- mark the decl as appropriate. */
14632 if (expr == boolean_true_node)
14633 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14634 else if (expr == boolean_false_node)
14635 /* Don't mark the decl as explicit. */;
14636 else if (decl_specs)
14637 /* The expression was value-dependent. Remember it so that we can
14638 substitute it later. */
14639 decl_specs->explicit_specifier = expr;
14640 return id;
14641 }
14642
14643 default:
14644 return NULL_TREE;
14645 }
14646
14647 /* Consume the token. */
14648 return cp_lexer_consume_token (parser->lexer)->u.value;
14649 }
14650
14651 /* Parse a linkage-specification.
14652
14653 linkage-specification:
14654 extern string-literal { declaration-seq [opt] }
14655 extern string-literal declaration */
14656
14657 static void
14658 cp_parser_linkage_specification (cp_parser* parser)
14659 {
14660 tree linkage;
14661
14662 /* Look for the `extern' keyword. */
14663 cp_token *extern_token
14664 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14665
14666 /* Look for the string-literal. */
14667 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14668 linkage = cp_parser_string_literal (parser, false, false);
14669
14670 /* Transform the literal into an identifier. If the literal is a
14671 wide-character string, or contains embedded NULs, then we can't
14672 handle it as the user wants. */
14673 if (strlen (TREE_STRING_POINTER (linkage))
14674 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14675 {
14676 cp_parser_error (parser, "invalid linkage-specification");
14677 /* Assume C++ linkage. */
14678 linkage = lang_name_cplusplus;
14679 }
14680 else
14681 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14682
14683 /* We're now using the new linkage. */
14684 push_lang_context (linkage);
14685
14686 /* Preserve the location of the innermost linkage specification,
14687 tracking the locations of nested specifications via a local. */
14688 location_t saved_location
14689 = parser->innermost_linkage_specification_location;
14690 /* Construct a location ranging from the start of the "extern" to
14691 the end of the string-literal, with the caret at the start, e.g.:
14692 extern "C" {
14693 ^~~~~~~~~~
14694 */
14695 parser->innermost_linkage_specification_location
14696 = make_location (extern_token->location,
14697 extern_token->location,
14698 get_finish (string_token->location));
14699
14700 /* If the next token is a `{', then we're using the first
14701 production. */
14702 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14703 {
14704 cp_ensure_no_omp_declare_simd (parser);
14705 cp_ensure_no_oacc_routine (parser);
14706
14707 /* Consume the `{' token. */
14708 matching_braces braces;
14709 braces.consume_open (parser);
14710 /* Parse the declarations. */
14711 cp_parser_declaration_seq_opt (parser);
14712 /* Look for the closing `}'. */
14713 braces.require_close (parser);
14714 }
14715 /* Otherwise, there's just one declaration. */
14716 else
14717 {
14718 bool saved_in_unbraced_linkage_specification_p;
14719
14720 saved_in_unbraced_linkage_specification_p
14721 = parser->in_unbraced_linkage_specification_p;
14722 parser->in_unbraced_linkage_specification_p = true;
14723 cp_parser_declaration (parser);
14724 parser->in_unbraced_linkage_specification_p
14725 = saved_in_unbraced_linkage_specification_p;
14726 }
14727
14728 /* We're done with the linkage-specification. */
14729 pop_lang_context ();
14730
14731 /* Restore location of parent linkage specification, if any. */
14732 parser->innermost_linkage_specification_location = saved_location;
14733 }
14734
14735 /* Parse a static_assert-declaration.
14736
14737 static_assert-declaration:
14738 static_assert ( constant-expression , string-literal ) ;
14739 static_assert ( constant-expression ) ; (C++17)
14740
14741 If MEMBER_P, this static_assert is a class member. */
14742
14743 static void
14744 cp_parser_static_assert(cp_parser *parser, bool member_p)
14745 {
14746 cp_expr condition;
14747 location_t token_loc;
14748 tree message;
14749 bool dummy;
14750
14751 /* Peek at the `static_assert' token so we can keep track of exactly
14752 where the static assertion started. */
14753 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14754
14755 /* Look for the `static_assert' keyword. */
14756 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14757 RT_STATIC_ASSERT))
14758 return;
14759
14760 /* We know we are in a static assertion; commit to any tentative
14761 parse. */
14762 if (cp_parser_parsing_tentatively (parser))
14763 cp_parser_commit_to_tentative_parse (parser);
14764
14765 /* Parse the `(' starting the static assertion condition. */
14766 matching_parens parens;
14767 parens.require_open (parser);
14768
14769 /* Parse the constant-expression. Allow a non-constant expression
14770 here in order to give better diagnostics in finish_static_assert. */
14771 condition =
14772 cp_parser_constant_expression (parser,
14773 /*allow_non_constant_p=*/true,
14774 /*non_constant_p=*/&dummy);
14775
14776 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14777 {
14778 if (cxx_dialect < cxx17)
14779 pedwarn (input_location, OPT_Wpedantic,
14780 "%<static_assert%> without a message "
14781 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14782 /* Eat the ')' */
14783 cp_lexer_consume_token (parser->lexer);
14784 message = build_string (1, "");
14785 TREE_TYPE (message) = char_array_type_node;
14786 fix_string_type (message);
14787 }
14788 else
14789 {
14790 /* Parse the separating `,'. */
14791 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14792
14793 /* Parse the string-literal message. */
14794 message = cp_parser_string_literal (parser,
14795 /*translate=*/false,
14796 /*wide_ok=*/true);
14797
14798 /* A `)' completes the static assertion. */
14799 if (!parens.require_close (parser))
14800 cp_parser_skip_to_closing_parenthesis (parser,
14801 /*recovering=*/true,
14802 /*or_comma=*/false,
14803 /*consume_paren=*/true);
14804 }
14805
14806 /* A semicolon terminates the declaration. */
14807 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14808
14809 /* Get the location for the static assertion. Use that of the
14810 condition if available, otherwise, use that of the "static_assert"
14811 token. */
14812 location_t assert_loc = condition.get_location ();
14813 if (assert_loc == UNKNOWN_LOCATION)
14814 assert_loc = token_loc;
14815
14816 /* Complete the static assertion, which may mean either processing
14817 the static assert now or saving it for template instantiation. */
14818 finish_static_assert (condition, message, assert_loc, member_p);
14819 }
14820
14821 /* Parse the expression in decltype ( expression ). */
14822
14823 static tree
14824 cp_parser_decltype_expr (cp_parser *parser,
14825 bool &id_expression_or_member_access_p)
14826 {
14827 cp_token *id_expr_start_token;
14828 tree expr;
14829
14830 /* First, try parsing an id-expression. */
14831 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14832 cp_parser_parse_tentatively (parser);
14833 expr = cp_parser_id_expression (parser,
14834 /*template_keyword_p=*/false,
14835 /*check_dependency_p=*/true,
14836 /*template_p=*/NULL,
14837 /*declarator_p=*/false,
14838 /*optional_p=*/false);
14839
14840 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14841 {
14842 bool non_integral_constant_expression_p = false;
14843 tree id_expression = expr;
14844 cp_id_kind idk;
14845 const char *error_msg;
14846
14847 if (identifier_p (expr))
14848 /* Lookup the name we got back from the id-expression. */
14849 expr = cp_parser_lookup_name_simple (parser, expr,
14850 id_expr_start_token->location);
14851
14852 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14853 /* A template without args is not a complete id-expression. */
14854 expr = error_mark_node;
14855
14856 if (expr
14857 && expr != error_mark_node
14858 && TREE_CODE (expr) != TYPE_DECL
14859 && (TREE_CODE (expr) != BIT_NOT_EXPR
14860 || !TYPE_P (TREE_OPERAND (expr, 0)))
14861 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14862 {
14863 /* Complete lookup of the id-expression. */
14864 expr = (finish_id_expression
14865 (id_expression, expr, parser->scope, &idk,
14866 /*integral_constant_expression_p=*/false,
14867 /*allow_non_integral_constant_expression_p=*/true,
14868 &non_integral_constant_expression_p,
14869 /*template_p=*/false,
14870 /*done=*/true,
14871 /*address_p=*/false,
14872 /*template_arg_p=*/false,
14873 &error_msg,
14874 id_expr_start_token->location));
14875
14876 if (expr == error_mark_node)
14877 /* We found an id-expression, but it was something that we
14878 should not have found. This is an error, not something
14879 we can recover from, so note that we found an
14880 id-expression and we'll recover as gracefully as
14881 possible. */
14882 id_expression_or_member_access_p = true;
14883 }
14884
14885 if (expr
14886 && expr != error_mark_node
14887 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14888 /* We have an id-expression. */
14889 id_expression_or_member_access_p = true;
14890 }
14891
14892 if (!id_expression_or_member_access_p)
14893 {
14894 /* Abort the id-expression parse. */
14895 cp_parser_abort_tentative_parse (parser);
14896
14897 /* Parsing tentatively, again. */
14898 cp_parser_parse_tentatively (parser);
14899
14900 /* Parse a class member access. */
14901 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14902 /*cast_p=*/false, /*decltype*/true,
14903 /*member_access_only_p=*/true, NULL);
14904
14905 if (expr
14906 && expr != error_mark_node
14907 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14908 /* We have an id-expression. */
14909 id_expression_or_member_access_p = true;
14910 }
14911
14912 if (id_expression_or_member_access_p)
14913 /* We have parsed the complete id-expression or member access. */
14914 cp_parser_parse_definitely (parser);
14915 else
14916 {
14917 /* Abort our attempt to parse an id-expression or member access
14918 expression. */
14919 cp_parser_abort_tentative_parse (parser);
14920
14921 /* Parse a full expression. */
14922 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14923 /*decltype_p=*/true);
14924 }
14925
14926 return expr;
14927 }
14928
14929 /* Parse a `decltype' type. Returns the type.
14930
14931 decltype-specifier:
14932 decltype ( expression )
14933 C++14:
14934 decltype ( auto ) */
14935
14936 static tree
14937 cp_parser_decltype (cp_parser *parser)
14938 {
14939 bool id_expression_or_member_access_p = false;
14940 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14941
14942 if (start_token->type == CPP_DECLTYPE)
14943 {
14944 /* Already parsed. */
14945 cp_lexer_consume_token (parser->lexer);
14946 return saved_checks_value (start_token->u.tree_check_value);
14947 }
14948
14949 /* Look for the `decltype' token. */
14950 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14951 return error_mark_node;
14952
14953 /* Parse the opening `('. */
14954 matching_parens parens;
14955 if (!parens.require_open (parser))
14956 return error_mark_node;
14957
14958 /* Since we're going to preserve any side-effects from this parse, set up a
14959 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14960 in the expression. */
14961 tentative_firewall firewall (parser);
14962
14963 /* If in_declarator_p, a reparse as an expression might succeed (60361).
14964 Otherwise, commit now for better diagnostics. */
14965 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
14966 && !parser->in_declarator_p)
14967 cp_parser_commit_to_topmost_tentative_parse (parser);
14968
14969 push_deferring_access_checks (dk_deferred);
14970
14971 tree expr = NULL_TREE;
14972
14973 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
14974 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
14975 {
14976 /* decltype (auto) */
14977 cp_lexer_consume_token (parser->lexer);
14978 if (cxx_dialect < cxx14)
14979 {
14980 error_at (start_token->location,
14981 "%<decltype(auto)%> type specifier only available with "
14982 "%<-std=c++14%> or %<-std=gnu++14%>");
14983 expr = error_mark_node;
14984 }
14985 }
14986 else
14987 {
14988 /* decltype (expression) */
14989
14990 /* Types cannot be defined in a `decltype' expression. Save away the
14991 old message and set the new one. */
14992 const char *saved_message = parser->type_definition_forbidden_message;
14993 parser->type_definition_forbidden_message
14994 = G_("types may not be defined in %<decltype%> expressions");
14995
14996 /* The restrictions on constant-expressions do not apply inside
14997 decltype expressions. */
14998 bool saved_integral_constant_expression_p
14999 = parser->integral_constant_expression_p;
15000 bool saved_non_integral_constant_expression_p
15001 = parser->non_integral_constant_expression_p;
15002 parser->integral_constant_expression_p = false;
15003
15004 /* Within a parenthesized expression, a `>' token is always
15005 the greater-than operator. */
15006 bool saved_greater_than_is_operator_p
15007 = parser->greater_than_is_operator_p;
15008 parser->greater_than_is_operator_p = true;
15009
15010 /* Do not actually evaluate the expression. */
15011 ++cp_unevaluated_operand;
15012
15013 /* Do not warn about problems with the expression. */
15014 ++c_inhibit_evaluation_warnings;
15015
15016 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
15017 STRIP_ANY_LOCATION_WRAPPER (expr);
15018
15019 /* Go back to evaluating expressions. */
15020 --cp_unevaluated_operand;
15021 --c_inhibit_evaluation_warnings;
15022
15023 /* The `>' token might be the end of a template-id or
15024 template-parameter-list now. */
15025 parser->greater_than_is_operator_p
15026 = saved_greater_than_is_operator_p;
15027
15028 /* Restore the old message and the integral constant expression
15029 flags. */
15030 parser->type_definition_forbidden_message = saved_message;
15031 parser->integral_constant_expression_p
15032 = saved_integral_constant_expression_p;
15033 parser->non_integral_constant_expression_p
15034 = saved_non_integral_constant_expression_p;
15035 }
15036
15037 /* Parse to the closing `)'. */
15038 if (expr == error_mark_node || !parens.require_close (parser))
15039 {
15040 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15041 /*consume_paren=*/true);
15042 expr = error_mark_node;
15043 }
15044
15045 /* If we got a parse error while tentative, bail out now. */
15046 if (cp_parser_error_occurred (parser))
15047 {
15048 pop_deferring_access_checks ();
15049 return error_mark_node;
15050 }
15051
15052 if (!expr)
15053 /* Build auto. */
15054 expr = make_decltype_auto ();
15055 else
15056 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
15057 tf_warning_or_error);
15058
15059 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
15060 it again. */
15061 start_token->type = CPP_DECLTYPE;
15062 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15063 start_token->tree_check_p = true;
15064 start_token->u.tree_check_value->value = expr;
15065 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
15066 start_token->keyword = RID_MAX;
15067
15068 location_t loc = start_token->location;
15069 loc = make_location (loc, loc, parser->lexer);
15070 start_token->location = loc;
15071
15072 cp_lexer_purge_tokens_after (parser->lexer, start_token);
15073
15074 pop_to_parent_deferring_access_checks ();
15075
15076 return expr;
15077 }
15078
15079 /* Special member functions [gram.special] */
15080
15081 /* Parse a conversion-function-id.
15082
15083 conversion-function-id:
15084 operator conversion-type-id
15085
15086 Returns an IDENTIFIER_NODE representing the operator. */
15087
15088 static tree
15089 cp_parser_conversion_function_id (cp_parser* parser)
15090 {
15091 tree type;
15092 tree saved_scope;
15093 tree saved_qualifying_scope;
15094 tree saved_object_scope;
15095 tree pushed_scope = NULL_TREE;
15096
15097 /* Look for the `operator' token. */
15098 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15099 return error_mark_node;
15100 /* When we parse the conversion-type-id, the current scope will be
15101 reset. However, we need that information in able to look up the
15102 conversion function later, so we save it here. */
15103 saved_scope = parser->scope;
15104 saved_qualifying_scope = parser->qualifying_scope;
15105 saved_object_scope = parser->object_scope;
15106 /* We must enter the scope of the class so that the names of
15107 entities declared within the class are available in the
15108 conversion-type-id. For example, consider:
15109
15110 struct S {
15111 typedef int I;
15112 operator I();
15113 };
15114
15115 S::operator I() { ... }
15116
15117 In order to see that `I' is a type-name in the definition, we
15118 must be in the scope of `S'. */
15119 if (saved_scope)
15120 pushed_scope = push_scope (saved_scope);
15121 /* Parse the conversion-type-id. */
15122 type = cp_parser_conversion_type_id (parser);
15123 /* Leave the scope of the class, if any. */
15124 if (pushed_scope)
15125 pop_scope (pushed_scope);
15126 /* Restore the saved scope. */
15127 parser->scope = saved_scope;
15128 parser->qualifying_scope = saved_qualifying_scope;
15129 parser->object_scope = saved_object_scope;
15130 /* If the TYPE is invalid, indicate failure. */
15131 if (type == error_mark_node)
15132 return error_mark_node;
15133 return make_conv_op_name (type);
15134 }
15135
15136 /* Parse a conversion-type-id:
15137
15138 conversion-type-id:
15139 type-specifier-seq conversion-declarator [opt]
15140
15141 Returns the TYPE specified. */
15142
15143 static tree
15144 cp_parser_conversion_type_id (cp_parser* parser)
15145 {
15146 tree attributes;
15147 cp_decl_specifier_seq type_specifiers;
15148 cp_declarator *declarator;
15149 tree type_specified;
15150 const char *saved_message;
15151
15152 /* Parse the attributes. */
15153 attributes = cp_parser_attributes_opt (parser);
15154
15155 saved_message = parser->type_definition_forbidden_message;
15156 parser->type_definition_forbidden_message
15157 = G_("types may not be defined in a conversion-type-id");
15158
15159 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
15160 optional in conversion-type-id. */
15161 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15162 /*is_declaration=*/false,
15163 /*is_trailing_return=*/false,
15164 &type_specifiers);
15165
15166 parser->type_definition_forbidden_message = saved_message;
15167
15168 /* If that didn't work, stop. */
15169 if (type_specifiers.type == error_mark_node)
15170 return error_mark_node;
15171 /* Parse the conversion-declarator. */
15172 declarator = cp_parser_conversion_declarator_opt (parser);
15173
15174 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
15175 /*initialized=*/0, &attributes);
15176 if (attributes)
15177 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
15178
15179 /* Don't give this error when parsing tentatively. This happens to
15180 work because we always parse this definitively once. */
15181 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
15182 && type_uses_auto (type_specified))
15183 {
15184 if (cxx_dialect < cxx14)
15185 {
15186 error ("invalid use of %<auto%> in conversion operator");
15187 return error_mark_node;
15188 }
15189 else if (template_parm_scope_p ())
15190 warning (0, "use of %<auto%> in member template "
15191 "conversion operator can never be deduced");
15192 }
15193
15194 return type_specified;
15195 }
15196
15197 /* Parse an (optional) conversion-declarator.
15198
15199 conversion-declarator:
15200 ptr-operator conversion-declarator [opt]
15201
15202 */
15203
15204 static cp_declarator *
15205 cp_parser_conversion_declarator_opt (cp_parser* parser)
15206 {
15207 enum tree_code code;
15208 tree class_type, std_attributes = NULL_TREE;
15209 cp_cv_quals cv_quals;
15210
15211 /* We don't know if there's a ptr-operator next, or not. */
15212 cp_parser_parse_tentatively (parser);
15213 /* Try the ptr-operator. */
15214 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
15215 &std_attributes);
15216 /* If it worked, look for more conversion-declarators. */
15217 if (cp_parser_parse_definitely (parser))
15218 {
15219 cp_declarator *declarator;
15220
15221 /* Parse another optional declarator. */
15222 declarator = cp_parser_conversion_declarator_opt (parser);
15223
15224 declarator = cp_parser_make_indirect_declarator
15225 (code, class_type, cv_quals, declarator, std_attributes);
15226
15227 return declarator;
15228 }
15229
15230 return NULL;
15231 }
15232
15233 /* Parse an (optional) ctor-initializer.
15234
15235 ctor-initializer:
15236 : mem-initializer-list */
15237
15238 static void
15239 cp_parser_ctor_initializer_opt (cp_parser* parser)
15240 {
15241 /* If the next token is not a `:', then there is no
15242 ctor-initializer. */
15243 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
15244 {
15245 /* Do default initialization of any bases and members. */
15246 if (DECL_CONSTRUCTOR_P (current_function_decl))
15247 finish_mem_initializers (NULL_TREE);
15248 return;
15249 }
15250
15251 /* Consume the `:' token. */
15252 cp_lexer_consume_token (parser->lexer);
15253 /* And the mem-initializer-list. */
15254 cp_parser_mem_initializer_list (parser);
15255 }
15256
15257 /* Parse a mem-initializer-list.
15258
15259 mem-initializer-list:
15260 mem-initializer ... [opt]
15261 mem-initializer ... [opt] , mem-initializer-list */
15262
15263 static void
15264 cp_parser_mem_initializer_list (cp_parser* parser)
15265 {
15266 tree mem_initializer_list = NULL_TREE;
15267 tree target_ctor = error_mark_node;
15268 cp_token *token = cp_lexer_peek_token (parser->lexer);
15269
15270 /* Let the semantic analysis code know that we are starting the
15271 mem-initializer-list. */
15272 if (!DECL_CONSTRUCTOR_P (current_function_decl))
15273 error_at (token->location,
15274 "only constructors take member initializers");
15275
15276 /* Loop through the list. */
15277 while (true)
15278 {
15279 tree mem_initializer;
15280
15281 token = cp_lexer_peek_token (parser->lexer);
15282 /* Parse the mem-initializer. */
15283 mem_initializer = cp_parser_mem_initializer (parser);
15284 /* If the next token is a `...', we're expanding member initializers. */
15285 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15286 if (ellipsis
15287 || (mem_initializer != error_mark_node
15288 && check_for_bare_parameter_packs (TREE_PURPOSE
15289 (mem_initializer))))
15290 {
15291 /* Consume the `...'. */
15292 if (ellipsis)
15293 cp_lexer_consume_token (parser->lexer);
15294
15295 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
15296 can be expanded but members cannot. */
15297 if (mem_initializer != error_mark_node
15298 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
15299 {
15300 error_at (token->location,
15301 "cannot expand initializer for member %qD",
15302 TREE_PURPOSE (mem_initializer));
15303 mem_initializer = error_mark_node;
15304 }
15305
15306 /* Construct the pack expansion type. */
15307 if (mem_initializer != error_mark_node)
15308 mem_initializer = make_pack_expansion (mem_initializer);
15309 }
15310 if (target_ctor != error_mark_node
15311 && mem_initializer != error_mark_node)
15312 {
15313 error ("mem-initializer for %qD follows constructor delegation",
15314 TREE_PURPOSE (mem_initializer));
15315 mem_initializer = error_mark_node;
15316 }
15317 /* Look for a target constructor. */
15318 if (mem_initializer != error_mark_node
15319 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
15320 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
15321 {
15322 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
15323 if (mem_initializer_list)
15324 {
15325 error ("constructor delegation follows mem-initializer for %qD",
15326 TREE_PURPOSE (mem_initializer_list));
15327 mem_initializer = error_mark_node;
15328 }
15329 target_ctor = mem_initializer;
15330 }
15331 /* Add it to the list, unless it was erroneous. */
15332 if (mem_initializer != error_mark_node)
15333 {
15334 TREE_CHAIN (mem_initializer) = mem_initializer_list;
15335 mem_initializer_list = mem_initializer;
15336 }
15337 /* If the next token is not a `,', we're done. */
15338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15339 break;
15340 /* Consume the `,' token. */
15341 cp_lexer_consume_token (parser->lexer);
15342 }
15343
15344 /* Perform semantic analysis. */
15345 if (DECL_CONSTRUCTOR_P (current_function_decl))
15346 finish_mem_initializers (mem_initializer_list);
15347 }
15348
15349 /* Parse a mem-initializer.
15350
15351 mem-initializer:
15352 mem-initializer-id ( expression-list [opt] )
15353 mem-initializer-id braced-init-list
15354
15355 GNU extension:
15356
15357 mem-initializer:
15358 ( expression-list [opt] )
15359
15360 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15361 class) or FIELD_DECL (for a non-static data member) to initialize;
15362 the TREE_VALUE is the expression-list. An empty initialization
15363 list is represented by void_list_node. */
15364
15365 static tree
15366 cp_parser_mem_initializer (cp_parser* parser)
15367 {
15368 tree mem_initializer_id;
15369 tree expression_list;
15370 tree member;
15371 cp_token *token = cp_lexer_peek_token (parser->lexer);
15372
15373 /* Find out what is being initialized. */
15374 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15375 {
15376 permerror (token->location,
15377 "anachronistic old-style base class initializer");
15378 mem_initializer_id = NULL_TREE;
15379 }
15380 else
15381 {
15382 mem_initializer_id = cp_parser_mem_initializer_id (parser);
15383 if (mem_initializer_id == error_mark_node)
15384 return mem_initializer_id;
15385 }
15386 member = expand_member_init (mem_initializer_id);
15387 if (member && !DECL_P (member))
15388 in_base_initializer = 1;
15389
15390 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15391 {
15392 bool expr_non_constant_p;
15393 cp_lexer_set_source_position (parser->lexer);
15394 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15395 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
15396 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
15397 expression_list = build_tree_list (NULL_TREE, expression_list);
15398 }
15399 else
15400 {
15401 vec<tree, va_gc> *vec;
15402 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15403 /*cast_p=*/false,
15404 /*allow_expansion_p=*/true,
15405 /*non_constant_p=*/NULL,
15406 /*close_paren_loc=*/NULL,
15407 /*wrap_locations_p=*/true);
15408 if (vec == NULL)
15409 return error_mark_node;
15410 expression_list = build_tree_list_vec (vec);
15411 release_tree_vector (vec);
15412 }
15413
15414 if (expression_list == error_mark_node)
15415 return error_mark_node;
15416 if (!expression_list)
15417 expression_list = void_type_node;
15418
15419 in_base_initializer = 0;
15420
15421 if (!member)
15422 return error_mark_node;
15423 tree node = build_tree_list (member, expression_list);
15424
15425 /* We can't attach the source location of this initializer directly to
15426 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
15427 within the TREE_TYPE of the list node. */
15428 location_t loc
15429 = make_location (token->location, token->location, parser->lexer);
15430 tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
15431 SET_EXPR_LOCATION (dummy, loc);
15432 TREE_TYPE (node) = dummy;
15433
15434 return node;
15435 }
15436
15437 /* Parse a mem-initializer-id.
15438
15439 mem-initializer-id:
15440 :: [opt] nested-name-specifier [opt] class-name
15441 decltype-specifier (C++11)
15442 identifier
15443
15444 Returns a TYPE indicating the class to be initialized for the first
15445 production (and the second in C++11). Returns an IDENTIFIER_NODE
15446 indicating the data member to be initialized for the last production. */
15447
15448 static tree
15449 cp_parser_mem_initializer_id (cp_parser* parser)
15450 {
15451 bool global_scope_p;
15452 bool nested_name_specifier_p;
15453 bool template_p = false;
15454 tree id;
15455
15456 cp_token *token = cp_lexer_peek_token (parser->lexer);
15457
15458 /* `typename' is not allowed in this context ([temp.res]). */
15459 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15460 {
15461 error_at (token->location,
15462 "keyword %<typename%> not allowed in this context (a qualified "
15463 "member initializer is implicitly a type)");
15464 cp_lexer_consume_token (parser->lexer);
15465 }
15466 /* Look for the optional `::' operator. */
15467 global_scope_p
15468 = (cp_parser_global_scope_opt (parser,
15469 /*current_scope_valid_p=*/false)
15470 != NULL_TREE);
15471 /* Look for the optional nested-name-specifier. The simplest way to
15472 implement:
15473
15474 [temp.res]
15475
15476 The keyword `typename' is not permitted in a base-specifier or
15477 mem-initializer; in these contexts a qualified name that
15478 depends on a template-parameter is implicitly assumed to be a
15479 type name.
15480
15481 is to assume that we have seen the `typename' keyword at this
15482 point. */
15483 nested_name_specifier_p
15484 = (cp_parser_nested_name_specifier_opt (parser,
15485 /*typename_keyword_p=*/true,
15486 /*check_dependency_p=*/true,
15487 /*type_p=*/true,
15488 /*is_declaration=*/true)
15489 != NULL_TREE);
15490 if (nested_name_specifier_p)
15491 template_p = cp_parser_optional_template_keyword (parser);
15492 /* If there is a `::' operator or a nested-name-specifier, then we
15493 are definitely looking for a class-name. */
15494 if (global_scope_p || nested_name_specifier_p)
15495 return cp_parser_class_name (parser,
15496 /*typename_keyword_p=*/true,
15497 /*template_keyword_p=*/template_p,
15498 typename_type,
15499 /*check_dependency_p=*/true,
15500 /*class_head_p=*/false,
15501 /*is_declaration=*/true);
15502 /* Otherwise, we could also be looking for an ordinary identifier. */
15503 cp_parser_parse_tentatively (parser);
15504 if (cp_lexer_next_token_is_decltype (parser->lexer))
15505 /* Try a decltype-specifier. */
15506 id = cp_parser_decltype (parser);
15507 else
15508 /* Otherwise, try a class-name. */
15509 id = cp_parser_class_name (parser,
15510 /*typename_keyword_p=*/true,
15511 /*template_keyword_p=*/false,
15512 none_type,
15513 /*check_dependency_p=*/true,
15514 /*class_head_p=*/false,
15515 /*is_declaration=*/true);
15516 /* If we found one, we're done. */
15517 if (cp_parser_parse_definitely (parser))
15518 return id;
15519 /* Otherwise, look for an ordinary identifier. */
15520 return cp_parser_identifier (parser);
15521 }
15522
15523 /* Overloading [gram.over] */
15524
15525 /* Parse an operator-function-id.
15526
15527 operator-function-id:
15528 operator operator
15529
15530 Returns an IDENTIFIER_NODE for the operator which is a
15531 human-readable spelling of the identifier, e.g., `operator +'. */
15532
15533 static cp_expr
15534 cp_parser_operator_function_id (cp_parser* parser)
15535 {
15536 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15537 /* Look for the `operator' keyword. */
15538 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15539 return error_mark_node;
15540 /* And then the name of the operator itself. */
15541 return cp_parser_operator (parser, start_loc);
15542 }
15543
15544 /* Return an identifier node for a user-defined literal operator.
15545 The suffix identifier is chained to the operator name identifier. */
15546
15547 tree
15548 cp_literal_operator_id (const char* name)
15549 {
15550 tree identifier;
15551 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15552 + strlen (name) + 10);
15553 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15554 identifier = get_identifier (buffer);
15555 XDELETEVEC (buffer);
15556
15557 return identifier;
15558 }
15559
15560 /* Parse an operator.
15561
15562 operator:
15563 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15564 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15565 || ++ -- , ->* -> () []
15566
15567 GNU Extensions:
15568
15569 operator:
15570 <? >? <?= >?=
15571
15572 Returns an IDENTIFIER_NODE for the operator which is a
15573 human-readable spelling of the identifier, e.g., `operator +'. */
15574
15575 static cp_expr
15576 cp_parser_operator (cp_parser* parser, location_t start_loc)
15577 {
15578 tree id = NULL_TREE;
15579 cp_token *token;
15580 bool utf8 = false;
15581
15582 /* Peek at the next token. */
15583 token = cp_lexer_peek_token (parser->lexer);
15584
15585 location_t end_loc = token->location;
15586
15587 /* Figure out which operator we have. */
15588 enum tree_code op = ERROR_MARK;
15589 bool assop = false;
15590 bool consumed = false;
15591 switch (token->type)
15592 {
15593 case CPP_KEYWORD:
15594 {
15595 /* The keyword should be either `new', `delete' or `co_await'. */
15596 if (token->keyword == RID_NEW)
15597 op = NEW_EXPR;
15598 else if (token->keyword == RID_DELETE)
15599 op = DELETE_EXPR;
15600 else if (token->keyword == RID_CO_AWAIT)
15601 op = CO_AWAIT_EXPR;
15602 else
15603 break;
15604
15605 /* Consume the `new', `delete' or co_await token. */
15606 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15607
15608 /* Peek at the next token. */
15609 token = cp_lexer_peek_token (parser->lexer);
15610 /* If it's a `[' token then this is the array variant of the
15611 operator. */
15612 if (token->type == CPP_OPEN_SQUARE
15613 && op != CO_AWAIT_EXPR)
15614 {
15615 /* Consume the `[' token. */
15616 cp_lexer_consume_token (parser->lexer);
15617 /* Look for the `]' token. */
15618 if (cp_token *close_token
15619 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15620 end_loc = close_token->location;
15621 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15622 }
15623 consumed = true;
15624 break;
15625 }
15626
15627 case CPP_PLUS:
15628 op = PLUS_EXPR;
15629 break;
15630
15631 case CPP_MINUS:
15632 op = MINUS_EXPR;
15633 break;
15634
15635 case CPP_MULT:
15636 op = MULT_EXPR;
15637 break;
15638
15639 case CPP_DIV:
15640 op = TRUNC_DIV_EXPR;
15641 break;
15642
15643 case CPP_MOD:
15644 op = TRUNC_MOD_EXPR;
15645 break;
15646
15647 case CPP_XOR:
15648 op = BIT_XOR_EXPR;
15649 break;
15650
15651 case CPP_AND:
15652 op = BIT_AND_EXPR;
15653 break;
15654
15655 case CPP_OR:
15656 op = BIT_IOR_EXPR;
15657 break;
15658
15659 case CPP_COMPL:
15660 op = BIT_NOT_EXPR;
15661 break;
15662
15663 case CPP_NOT:
15664 op = TRUTH_NOT_EXPR;
15665 break;
15666
15667 case CPP_EQ:
15668 assop = true;
15669 op = NOP_EXPR;
15670 break;
15671
15672 case CPP_LESS:
15673 op = LT_EXPR;
15674 break;
15675
15676 case CPP_GREATER:
15677 op = GT_EXPR;
15678 break;
15679
15680 case CPP_PLUS_EQ:
15681 assop = true;
15682 op = PLUS_EXPR;
15683 break;
15684
15685 case CPP_MINUS_EQ:
15686 assop = true;
15687 op = MINUS_EXPR;
15688 break;
15689
15690 case CPP_MULT_EQ:
15691 assop = true;
15692 op = MULT_EXPR;
15693 break;
15694
15695 case CPP_DIV_EQ:
15696 assop = true;
15697 op = TRUNC_DIV_EXPR;
15698 break;
15699
15700 case CPP_MOD_EQ:
15701 assop = true;
15702 op = TRUNC_MOD_EXPR;
15703 break;
15704
15705 case CPP_XOR_EQ:
15706 assop = true;
15707 op = BIT_XOR_EXPR;
15708 break;
15709
15710 case CPP_AND_EQ:
15711 assop = true;
15712 op = BIT_AND_EXPR;
15713 break;
15714
15715 case CPP_OR_EQ:
15716 assop = true;
15717 op = BIT_IOR_EXPR;
15718 break;
15719
15720 case CPP_LSHIFT:
15721 op = LSHIFT_EXPR;
15722 break;
15723
15724 case CPP_RSHIFT:
15725 op = RSHIFT_EXPR;
15726 break;
15727
15728 case CPP_LSHIFT_EQ:
15729 assop = true;
15730 op = LSHIFT_EXPR;
15731 break;
15732
15733 case CPP_RSHIFT_EQ:
15734 assop = true;
15735 op = RSHIFT_EXPR;
15736 break;
15737
15738 case CPP_EQ_EQ:
15739 op = EQ_EXPR;
15740 break;
15741
15742 case CPP_NOT_EQ:
15743 op = NE_EXPR;
15744 break;
15745
15746 case CPP_LESS_EQ:
15747 op = LE_EXPR;
15748 break;
15749
15750 case CPP_GREATER_EQ:
15751 op = GE_EXPR;
15752 break;
15753
15754 case CPP_SPACESHIP:
15755 op = SPACESHIP_EXPR;
15756 break;
15757
15758 case CPP_AND_AND:
15759 op = TRUTH_ANDIF_EXPR;
15760 break;
15761
15762 case CPP_OR_OR:
15763 op = TRUTH_ORIF_EXPR;
15764 break;
15765
15766 case CPP_PLUS_PLUS:
15767 op = POSTINCREMENT_EXPR;
15768 break;
15769
15770 case CPP_MINUS_MINUS:
15771 op = PREDECREMENT_EXPR;
15772 break;
15773
15774 case CPP_COMMA:
15775 op = COMPOUND_EXPR;
15776 break;
15777
15778 case CPP_DEREF_STAR:
15779 op = MEMBER_REF;
15780 break;
15781
15782 case CPP_DEREF:
15783 op = COMPONENT_REF;
15784 break;
15785
15786 case CPP_QUERY:
15787 op = COND_EXPR;
15788 /* Consume the `?'. */
15789 cp_lexer_consume_token (parser->lexer);
15790 /* Look for the matching `:'. */
15791 cp_parser_require (parser, CPP_COLON, RT_COLON);
15792 consumed = true;
15793 break;
15794
15795 case CPP_OPEN_PAREN:
15796 {
15797 /* Consume the `('. */
15798 matching_parens parens;
15799 parens.consume_open (parser);
15800 /* Look for the matching `)'. */
15801 token = parens.require_close (parser);
15802 if (token)
15803 end_loc = token->location;
15804 op = CALL_EXPR;
15805 consumed = true;
15806 break;
15807 }
15808
15809 case CPP_OPEN_SQUARE:
15810 /* Consume the `['. */
15811 cp_lexer_consume_token (parser->lexer);
15812 /* Look for the matching `]'. */
15813 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15814 if (token)
15815 end_loc = token->location;
15816 op = ARRAY_REF;
15817 consumed = true;
15818 break;
15819
15820 case CPP_UTF8STRING:
15821 case CPP_UTF8STRING_USERDEF:
15822 utf8 = true;
15823 /* FALLTHRU */
15824 case CPP_STRING:
15825 case CPP_WSTRING:
15826 case CPP_STRING16:
15827 case CPP_STRING32:
15828 case CPP_STRING_USERDEF:
15829 case CPP_WSTRING_USERDEF:
15830 case CPP_STRING16_USERDEF:
15831 case CPP_STRING32_USERDEF:
15832 {
15833 cp_expr str;
15834 tree string_tree;
15835 int sz, len;
15836
15837 if (cxx_dialect == cxx98)
15838 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15839
15840 /* Consume the string. */
15841 str = cp_parser_string_literal (parser, /*translate=*/true,
15842 /*wide_ok=*/true, /*lookup_udlit=*/false);
15843 if (str == error_mark_node)
15844 return error_mark_node;
15845 else if (TREE_CODE (str) == USERDEF_LITERAL)
15846 {
15847 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15848 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15849 end_loc = str.get_location ();
15850 }
15851 else
15852 {
15853 string_tree = str;
15854 /* Look for the suffix identifier. */
15855 token = cp_lexer_peek_token (parser->lexer);
15856 if (token->type == CPP_NAME)
15857 {
15858 id = cp_parser_identifier (parser);
15859 end_loc = token->location;
15860 }
15861 else if (token->type == CPP_KEYWORD)
15862 {
15863 error ("unexpected keyword;"
15864 " remove space between quotes and suffix identifier");
15865 return error_mark_node;
15866 }
15867 else
15868 {
15869 error ("expected suffix identifier");
15870 return error_mark_node;
15871 }
15872 }
15873 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15874 (TREE_TYPE (TREE_TYPE (string_tree))));
15875 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15876 if (len != 0)
15877 {
15878 error ("expected empty string after %<operator%> keyword");
15879 return error_mark_node;
15880 }
15881 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15882 != char_type_node)
15883 {
15884 error ("invalid encoding prefix in literal operator");
15885 return error_mark_node;
15886 }
15887 if (id != error_mark_node)
15888 {
15889 const char *name = IDENTIFIER_POINTER (id);
15890 id = cp_literal_operator_id (name);
15891 }
15892 /* Generate a location of the form:
15893 "" _suffix_identifier
15894 ^~~~~~~~~~~~~~~~~~~~~
15895 with caret == start at the start token, finish at the end of the
15896 suffix identifier. */
15897 location_t combined_loc
15898 = make_location (start_loc, start_loc, parser->lexer);
15899 return cp_expr (id, combined_loc);
15900 }
15901
15902 default:
15903 /* Anything else is an error. */
15904 break;
15905 }
15906
15907 /* If we have selected an identifier, we need to consume the
15908 operator token. */
15909 if (op != ERROR_MARK)
15910 {
15911 id = ovl_op_identifier (assop, op);
15912 if (!consumed)
15913 cp_lexer_consume_token (parser->lexer);
15914 }
15915 /* Otherwise, no valid operator name was present. */
15916 else
15917 {
15918 cp_parser_error (parser, "expected operator");
15919 id = error_mark_node;
15920 }
15921
15922 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15923 return cp_expr (id, start_loc);
15924 }
15925
15926 /* Parse a template-declaration.
15927
15928 template-declaration:
15929 export [opt] template < template-parameter-list > declaration
15930
15931 If MEMBER_P is TRUE, this template-declaration occurs within a
15932 class-specifier.
15933
15934 The grammar rule given by the standard isn't correct. What
15935 is really meant is:
15936
15937 template-declaration:
15938 export [opt] template-parameter-list-seq
15939 decl-specifier-seq [opt] init-declarator [opt] ;
15940 export [opt] template-parameter-list-seq
15941 function-definition
15942
15943 template-parameter-list-seq:
15944 template-parameter-list-seq [opt]
15945 template < template-parameter-list >
15946
15947 Concept Extensions:
15948
15949 template-parameter-list-seq:
15950 template < template-parameter-list > requires-clause [opt]
15951
15952 requires-clause:
15953 requires logical-or-expression */
15954
15955 static void
15956 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15957 {
15958 /* Check for `export'. */
15959 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15960 {
15961 /* Consume the `export' token. */
15962 cp_lexer_consume_token (parser->lexer);
15963 /* Warn that we do not support `export'. */
15964 warning (0, "keyword %<export%> not implemented, and will be ignored");
15965 }
15966
15967 cp_parser_template_declaration_after_export (parser, member_p);
15968 }
15969
15970 /* Parse a template-parameter-list.
15971
15972 template-parameter-list:
15973 template-parameter
15974 template-parameter-list , template-parameter
15975
15976 Returns a TREE_LIST. Each node represents a template parameter.
15977 The nodes are connected via their TREE_CHAINs. */
15978
15979 static tree
15980 cp_parser_template_parameter_list (cp_parser* parser)
15981 {
15982 tree parameter_list = NULL_TREE;
15983
15984 /* Don't create wrapper nodes within a template-parameter-list,
15985 since we don't want to have different types based on the
15986 spelling location of constants and decls within them. */
15987 auto_suppress_location_wrappers sentinel;
15988
15989 begin_template_parm_list ();
15990
15991 /* The loop below parses the template parms. We first need to know
15992 the total number of template parms to be able to compute proper
15993 canonical types of each dependent type. So after the loop, when
15994 we know the total number of template parms,
15995 end_template_parm_list computes the proper canonical types and
15996 fixes up the dependent types accordingly. */
15997 while (true)
15998 {
15999 tree parameter;
16000 bool is_non_type;
16001 bool is_parameter_pack;
16002 location_t parm_loc;
16003
16004 /* Parse the template-parameter. */
16005 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
16006 parameter = cp_parser_template_parameter (parser,
16007 &is_non_type,
16008 &is_parameter_pack);
16009 /* Add it to the list. */
16010 if (parameter != error_mark_node)
16011 parameter_list = process_template_parm (parameter_list,
16012 parm_loc,
16013 parameter,
16014 is_non_type,
16015 is_parameter_pack);
16016 else
16017 {
16018 tree err_parm = build_tree_list (parameter, parameter);
16019 parameter_list = chainon (parameter_list, err_parm);
16020 }
16021
16022 /* If the next token is not a `,', we're done. */
16023 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16024 break;
16025 /* Otherwise, consume the `,' token. */
16026 cp_lexer_consume_token (parser->lexer);
16027 }
16028
16029 return end_template_parm_list (parameter_list);
16030 }
16031
16032 /* Parse a introduction-list.
16033
16034 introduction-list:
16035 introduced-parameter
16036 introduction-list , introduced-parameter
16037
16038 introduced-parameter:
16039 ...[opt] identifier
16040
16041 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
16042 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
16043 WILDCARD_DECL will also have DECL_NAME set and token location in
16044 DECL_SOURCE_LOCATION. */
16045
16046 static tree
16047 cp_parser_introduction_list (cp_parser *parser)
16048 {
16049 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
16050
16051 while (true)
16052 {
16053 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
16054 if (is_pack)
16055 cp_lexer_consume_token (parser->lexer);
16056
16057 tree identifier = cp_parser_identifier (parser);
16058 if (identifier == error_mark_node)
16059 break;
16060
16061 /* Build placeholder. */
16062 tree parm = build_nt (WILDCARD_DECL);
16063 DECL_SOURCE_LOCATION (parm)
16064 = cp_lexer_peek_token (parser->lexer)->location;
16065 DECL_NAME (parm) = identifier;
16066 WILDCARD_PACK_P (parm) = is_pack;
16067 vec_safe_push (introduction_vec, parm);
16068
16069 /* If the next token is not a `,', we're done. */
16070 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16071 break;
16072 /* Otherwise, consume the `,' token. */
16073 cp_lexer_consume_token (parser->lexer);
16074 }
16075
16076 /* Convert the vec into a TREE_VEC. */
16077 tree introduction_list = make_tree_vec (introduction_vec->length ());
16078 unsigned int n;
16079 tree parm;
16080 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
16081 TREE_VEC_ELT (introduction_list, n) = parm;
16082
16083 release_tree_vector (introduction_vec);
16084 return introduction_list;
16085 }
16086
16087 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
16088 is an abstract declarator. */
16089
16090 static inline cp_declarator*
16091 get_id_declarator (cp_declarator *declarator)
16092 {
16093 cp_declarator *d = declarator;
16094 while (d && d->kind != cdk_id)
16095 d = d->declarator;
16096 return d;
16097 }
16098
16099 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
16100 is an abstract declarator. */
16101
16102 static inline tree
16103 get_unqualified_id (cp_declarator *declarator)
16104 {
16105 declarator = get_id_declarator (declarator);
16106 if (declarator)
16107 return declarator->u.id.unqualified_name;
16108 else
16109 return NULL_TREE;
16110 }
16111
16112 /* Returns true if TYPE would declare a constrained constrained-parameter. */
16113
16114 static inline bool
16115 is_constrained_parameter (tree type)
16116 {
16117 return (type
16118 && TREE_CODE (type) == TYPE_DECL
16119 && CONSTRAINED_PARM_CONCEPT (type)
16120 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
16121 }
16122
16123 /* Returns true if PARM declares a constrained-parameter. */
16124
16125 static inline bool
16126 is_constrained_parameter (cp_parameter_declarator *parm)
16127 {
16128 return is_constrained_parameter (parm->decl_specifiers.type);
16129 }
16130
16131 /* Check that the type parameter is only a declarator-id, and that its
16132 type is not cv-qualified. */
16133
16134 bool
16135 cp_parser_check_constrained_type_parm (cp_parser *parser,
16136 cp_parameter_declarator *parm)
16137 {
16138 if (!parm->declarator)
16139 return true;
16140
16141 if (parm->declarator->kind != cdk_id)
16142 {
16143 cp_parser_error (parser, "invalid constrained type parameter");
16144 return false;
16145 }
16146
16147 /* Don't allow cv-qualified type parameters. */
16148 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
16149 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
16150 {
16151 cp_parser_error (parser, "cv-qualified type parameter");
16152 return false;
16153 }
16154
16155 return true;
16156 }
16157
16158 /* Finish parsing/processing a template type parameter and checking
16159 various restrictions. */
16160
16161 static inline tree
16162 cp_parser_constrained_type_template_parm (cp_parser *parser,
16163 tree id,
16164 cp_parameter_declarator* parmdecl)
16165 {
16166 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
16167 return finish_template_type_parm (class_type_node, id);
16168 else
16169 return error_mark_node;
16170 }
16171
16172 static tree
16173 finish_constrained_template_template_parm (tree proto, tree id)
16174 {
16175 /* FIXME: This should probably be copied, and we may need to adjust
16176 the template parameter depths. */
16177 tree saved_parms = current_template_parms;
16178 begin_template_parm_list ();
16179 current_template_parms = DECL_TEMPLATE_PARMS (proto);
16180 end_template_parm_list ();
16181
16182 tree parm = finish_template_template_parm (class_type_node, id);
16183 current_template_parms = saved_parms;
16184
16185 return parm;
16186 }
16187
16188 /* Finish parsing/processing a template template parameter by borrowing
16189 the template parameter list from the prototype parameter. */
16190
16191 static tree
16192 cp_parser_constrained_template_template_parm (cp_parser *parser,
16193 tree proto,
16194 tree id,
16195 cp_parameter_declarator *parmdecl)
16196 {
16197 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
16198 return error_mark_node;
16199 return finish_constrained_template_template_parm (proto, id);
16200 }
16201
16202 /* Create a new non-type template parameter from the given PARM
16203 declarator. */
16204
16205 static tree
16206 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
16207 cp_parameter_declarator *parm)
16208 {
16209 *is_non_type = true;
16210 cp_declarator *decl = parm->declarator;
16211 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
16212 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
16213 return grokdeclarator (decl, specs, TPARM, 0, NULL);
16214 }
16215
16216 /* Build a constrained template parameter based on the PARMDECL
16217 declarator. The type of PARMDECL is the constrained type, which
16218 refers to the prototype template parameter that ultimately
16219 specifies the type of the declared parameter. */
16220
16221 static tree
16222 finish_constrained_parameter (cp_parser *parser,
16223 cp_parameter_declarator *parmdecl,
16224 bool *is_non_type)
16225 {
16226 tree decl = parmdecl->decl_specifiers.type;
16227 tree id = get_unqualified_id (parmdecl->declarator);
16228 tree def = parmdecl->default_argument;
16229 tree proto = DECL_INITIAL (decl);
16230
16231 /* Build the parameter. Return an error if the declarator was invalid. */
16232 tree parm;
16233 if (TREE_CODE (proto) == TYPE_DECL)
16234 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
16235 else if (TREE_CODE (proto) == TEMPLATE_DECL)
16236 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
16237 parmdecl);
16238 else
16239 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
16240 if (parm == error_mark_node)
16241 return error_mark_node;
16242
16243 /* Finish the parameter decl and create a node attaching the
16244 default argument and constraint. */
16245 parm = build_tree_list (def, parm);
16246 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
16247
16248 return parm;
16249 }
16250
16251 /* Returns true if the parsed type actually represents the declaration
16252 of a type template-parameter. */
16253
16254 static bool
16255 declares_constrained_type_template_parameter (tree type)
16256 {
16257 return (is_constrained_parameter (type)
16258 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
16259 }
16260
16261 /* Returns true if the parsed type actually represents the declaration of
16262 a template template-parameter. */
16263
16264 static bool
16265 declares_constrained_template_template_parameter (tree type)
16266 {
16267 return (is_constrained_parameter (type)
16268 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
16269 }
16270
16271 /* Parse a default argument for a type template-parameter.
16272 Note that diagnostics are handled in cp_parser_template_parameter. */
16273
16274 static tree
16275 cp_parser_default_type_template_argument (cp_parser *parser)
16276 {
16277 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
16278
16279 /* Consume the `=' token. */
16280 cp_lexer_consume_token (parser->lexer);
16281
16282 cp_token *token = cp_lexer_peek_token (parser->lexer);
16283
16284 /* Parse the default-argument. */
16285 push_deferring_access_checks (dk_no_deferred);
16286 tree default_argument = cp_parser_type_id (parser,
16287 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16288 NULL);
16289 pop_deferring_access_checks ();
16290
16291 if (flag_concepts && type_uses_auto (default_argument))
16292 {
16293 error_at (token->location,
16294 "invalid use of %<auto%> in default template argument");
16295 return error_mark_node;
16296 }
16297
16298 return default_argument;
16299 }
16300
16301 /* Parse a default argument for a template template-parameter. */
16302
16303 static tree
16304 cp_parser_default_template_template_argument (cp_parser *parser)
16305 {
16306 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
16307
16308 bool is_template;
16309
16310 /* Consume the `='. */
16311 cp_lexer_consume_token (parser->lexer);
16312 /* Parse the id-expression. */
16313 push_deferring_access_checks (dk_no_deferred);
16314 /* save token before parsing the id-expression, for error
16315 reporting */
16316 const cp_token* token = cp_lexer_peek_token (parser->lexer);
16317 tree default_argument
16318 = cp_parser_id_expression (parser,
16319 /*template_keyword_p=*/false,
16320 /*check_dependency_p=*/true,
16321 /*template_p=*/&is_template,
16322 /*declarator_p=*/false,
16323 /*optional_p=*/false);
16324 if (TREE_CODE (default_argument) == TYPE_DECL)
16325 /* If the id-expression was a template-id that refers to
16326 a template-class, we already have the declaration here,
16327 so no further lookup is needed. */
16328 ;
16329 else
16330 /* Look up the name. */
16331 default_argument
16332 = cp_parser_lookup_name (parser, default_argument,
16333 none_type,
16334 /*is_template=*/is_template,
16335 /*is_namespace=*/false,
16336 /*check_dependency=*/true,
16337 /*ambiguous_decls=*/NULL,
16338 token->location);
16339 /* See if the default argument is valid. */
16340 default_argument = check_template_template_default_arg (default_argument);
16341 pop_deferring_access_checks ();
16342 return default_argument;
16343 }
16344
16345 /* Parse a template-parameter.
16346
16347 template-parameter:
16348 type-parameter
16349 parameter-declaration
16350
16351 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
16352 the parameter. The TREE_PURPOSE is the default value, if any.
16353 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
16354 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
16355 set to true iff this parameter is a parameter pack. */
16356
16357 static tree
16358 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
16359 bool *is_parameter_pack)
16360 {
16361 cp_token *token;
16362 cp_parameter_declarator *parameter_declarator;
16363 tree parm;
16364
16365 /* Assume it is a type parameter or a template parameter. */
16366 *is_non_type = false;
16367 /* Assume it not a parameter pack. */
16368 *is_parameter_pack = false;
16369 /* Peek at the next token. */
16370 token = cp_lexer_peek_token (parser->lexer);
16371 /* If it is `template', we have a type-parameter. */
16372 if (token->keyword == RID_TEMPLATE)
16373 return cp_parser_type_parameter (parser, is_parameter_pack);
16374 /* If it is `class' or `typename' we do not know yet whether it is a
16375 type parameter or a non-type parameter. Consider:
16376
16377 template <typename T, typename T::X X> ...
16378
16379 or:
16380
16381 template <class C, class D*> ...
16382
16383 Here, the first parameter is a type parameter, and the second is
16384 a non-type parameter. We can tell by looking at the token after
16385 the identifier -- if it is a `,', `=', or `>' then we have a type
16386 parameter. */
16387 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
16388 {
16389 /* Peek at the token after `class' or `typename'. */
16390 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16391 /* If it's an ellipsis, we have a template type parameter
16392 pack. */
16393 if (token->type == CPP_ELLIPSIS)
16394 return cp_parser_type_parameter (parser, is_parameter_pack);
16395 /* If it's an identifier, skip it. */
16396 if (token->type == CPP_NAME)
16397 token = cp_lexer_peek_nth_token (parser->lexer, 3);
16398 /* Now, see if the token looks like the end of a template
16399 parameter. */
16400 if (token->type == CPP_COMMA
16401 || token->type == CPP_EQ
16402 || token->type == CPP_GREATER)
16403 return cp_parser_type_parameter (parser, is_parameter_pack);
16404 }
16405
16406 /* Otherwise, it is a non-type parameter or a constrained parameter.
16407
16408 [temp.param]
16409
16410 When parsing a default template-argument for a non-type
16411 template-parameter, the first non-nested `>' is taken as the end
16412 of the template parameter-list rather than a greater-than
16413 operator. */
16414 parameter_declarator
16415 = cp_parser_parameter_declaration (parser,
16416 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16417 /*template_parm_p=*/true,
16418 /*parenthesized_p=*/NULL);
16419
16420 if (!parameter_declarator)
16421 return error_mark_node;
16422
16423 /* If the parameter declaration is marked as a parameter pack, set
16424 *IS_PARAMETER_PACK to notify the caller. */
16425 if (parameter_declarator->template_parameter_pack_p)
16426 *is_parameter_pack = true;
16427
16428 if (parameter_declarator->default_argument)
16429 {
16430 /* Can happen in some cases of erroneous input (c++/34892). */
16431 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16432 /* Consume the `...' for better error recovery. */
16433 cp_lexer_consume_token (parser->lexer);
16434 }
16435
16436 /* The parameter may have been constrained type parameter. */
16437 if (is_constrained_parameter (parameter_declarator))
16438 return finish_constrained_parameter (parser,
16439 parameter_declarator,
16440 is_non_type);
16441
16442 // Now we're sure that the parameter is a non-type parameter.
16443 *is_non_type = true;
16444
16445 parm = grokdeclarator (parameter_declarator->declarator,
16446 &parameter_declarator->decl_specifiers,
16447 TPARM, /*initialized=*/0,
16448 /*attrlist=*/NULL);
16449 if (parm == error_mark_node)
16450 return error_mark_node;
16451
16452 return build_tree_list (parameter_declarator->default_argument, parm);
16453 }
16454
16455 /* Parse a type-parameter.
16456
16457 type-parameter:
16458 class identifier [opt]
16459 class identifier [opt] = type-id
16460 typename identifier [opt]
16461 typename identifier [opt] = type-id
16462 template < template-parameter-list > class identifier [opt]
16463 template < template-parameter-list > class identifier [opt]
16464 = id-expression
16465
16466 GNU Extension (variadic templates):
16467
16468 type-parameter:
16469 class ... identifier [opt]
16470 typename ... identifier [opt]
16471
16472 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16473 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16474 the declaration of the parameter.
16475
16476 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16477
16478 static tree
16479 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
16480 {
16481 cp_token *token;
16482 tree parameter;
16483
16484 /* Look for a keyword to tell us what kind of parameter this is. */
16485 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
16486 if (!token)
16487 return error_mark_node;
16488
16489 switch (token->keyword)
16490 {
16491 case RID_CLASS:
16492 case RID_TYPENAME:
16493 {
16494 tree identifier;
16495 tree default_argument;
16496
16497 /* If the next token is an ellipsis, we have a template
16498 argument pack. */
16499 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16500 {
16501 /* Consume the `...' token. */
16502 cp_lexer_consume_token (parser->lexer);
16503 maybe_warn_variadic_templates ();
16504
16505 *is_parameter_pack = true;
16506 }
16507
16508 /* If the next token is an identifier, then it names the
16509 parameter. */
16510 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16511 identifier = cp_parser_identifier (parser);
16512 else
16513 identifier = NULL_TREE;
16514
16515 /* Create the parameter. */
16516 parameter = finish_template_type_parm (class_type_node, identifier);
16517
16518 /* If the next token is an `=', we have a default argument. */
16519 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16520 {
16521 default_argument
16522 = cp_parser_default_type_template_argument (parser);
16523
16524 /* Template parameter packs cannot have default
16525 arguments. */
16526 if (*is_parameter_pack)
16527 {
16528 if (identifier)
16529 error_at (token->location,
16530 "template parameter pack %qD cannot have a "
16531 "default argument", identifier);
16532 else
16533 error_at (token->location,
16534 "template parameter packs cannot have "
16535 "default arguments");
16536 default_argument = NULL_TREE;
16537 }
16538 else if (check_for_bare_parameter_packs (default_argument))
16539 default_argument = error_mark_node;
16540 }
16541 else
16542 default_argument = NULL_TREE;
16543
16544 /* Create the combined representation of the parameter and the
16545 default argument. */
16546 parameter = build_tree_list (default_argument, parameter);
16547 }
16548 break;
16549
16550 case RID_TEMPLATE:
16551 {
16552 tree identifier;
16553 tree default_argument;
16554
16555 /* Look for the `<'. */
16556 cp_parser_require (parser, CPP_LESS, RT_LESS);
16557 /* Parse the template-parameter-list. */
16558 cp_parser_template_parameter_list (parser);
16559 /* Look for the `>'. */
16560 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16561
16562 /* If template requirements are present, parse them. */
16563 if (flag_concepts)
16564 {
16565 tree reqs = get_shorthand_constraints (current_template_parms);
16566 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
16567 reqs = combine_constraint_expressions (reqs, dreqs);
16568 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16569 }
16570
16571 /* Look for the `class' or 'typename' keywords. */
16572 cp_parser_type_parameter_key (parser);
16573 /* If the next token is an ellipsis, we have a template
16574 argument pack. */
16575 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16576 {
16577 /* Consume the `...' token. */
16578 cp_lexer_consume_token (parser->lexer);
16579 maybe_warn_variadic_templates ();
16580
16581 *is_parameter_pack = true;
16582 }
16583 /* If the next token is an `=', then there is a
16584 default-argument. If the next token is a `>', we are at
16585 the end of the parameter-list. If the next token is a `,',
16586 then we are at the end of this parameter. */
16587 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16588 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16589 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16590 {
16591 identifier = cp_parser_identifier (parser);
16592 /* Treat invalid names as if the parameter were nameless. */
16593 if (identifier == error_mark_node)
16594 identifier = NULL_TREE;
16595 }
16596 else
16597 identifier = NULL_TREE;
16598
16599 /* Create the template parameter. */
16600 parameter = finish_template_template_parm (class_type_node,
16601 identifier);
16602
16603 /* If the next token is an `=', then there is a
16604 default-argument. */
16605 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16606 {
16607 default_argument
16608 = cp_parser_default_template_template_argument (parser);
16609
16610 /* Template parameter packs cannot have default
16611 arguments. */
16612 if (*is_parameter_pack)
16613 {
16614 if (identifier)
16615 error_at (token->location,
16616 "template parameter pack %qD cannot "
16617 "have a default argument",
16618 identifier);
16619 else
16620 error_at (token->location, "template parameter packs cannot "
16621 "have default arguments");
16622 default_argument = NULL_TREE;
16623 }
16624 }
16625 else
16626 default_argument = NULL_TREE;
16627
16628 /* Create the combined representation of the parameter and the
16629 default argument. */
16630 parameter = build_tree_list (default_argument, parameter);
16631 }
16632 break;
16633
16634 default:
16635 gcc_unreachable ();
16636 break;
16637 }
16638
16639 return parameter;
16640 }
16641
16642 /* Parse a template-id.
16643
16644 template-id:
16645 template-name < template-argument-list [opt] >
16646
16647 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16648 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16649 returned. Otherwise, if the template-name names a function, or set
16650 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16651 names a class, returns a TYPE_DECL for the specialization.
16652
16653 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16654 uninstantiated templates. */
16655
16656 static tree
16657 cp_parser_template_id (cp_parser *parser,
16658 bool template_keyword_p,
16659 bool check_dependency_p,
16660 enum tag_types tag_type,
16661 bool is_declaration)
16662 {
16663 tree templ;
16664 tree arguments;
16665 tree template_id;
16666 cp_token_position start_of_id = 0;
16667 cp_token *next_token = NULL, *next_token_2 = NULL;
16668 bool is_identifier;
16669
16670 /* If the next token corresponds to a template-id, there is no need
16671 to reparse it. */
16672 cp_token *token = cp_lexer_peek_token (parser->lexer);
16673
16674 if (token->type == CPP_TEMPLATE_ID)
16675 {
16676 cp_lexer_consume_token (parser->lexer);
16677 return saved_checks_value (token->u.tree_check_value);
16678 }
16679
16680 /* Avoid performing name lookup if there is no possibility of
16681 finding a template-id. */
16682 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16683 || (token->type == CPP_NAME
16684 && !cp_parser_nth_token_starts_template_argument_list_p
16685 (parser, 2)))
16686 {
16687 cp_parser_error (parser, "expected template-id");
16688 return error_mark_node;
16689 }
16690
16691 /* Remember where the template-id starts. */
16692 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16693 start_of_id = cp_lexer_token_position (parser->lexer, false);
16694
16695 push_deferring_access_checks (dk_deferred);
16696
16697 /* Parse the template-name. */
16698 is_identifier = false;
16699 templ = cp_parser_template_name (parser, template_keyword_p,
16700 check_dependency_p,
16701 is_declaration,
16702 tag_type,
16703 &is_identifier);
16704
16705 /* Push any access checks inside the firewall we're about to create. */
16706 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16707 pop_deferring_access_checks ();
16708 if (templ == error_mark_node || is_identifier)
16709 return templ;
16710
16711 /* Since we're going to preserve any side-effects from this parse, set up a
16712 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16713 in the template arguments. */
16714 tentative_firewall firewall (parser);
16715 reopen_deferring_access_checks (checks);
16716
16717 /* If we find the sequence `[:' after a template-name, it's probably
16718 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16719 parse correctly the argument list. */
16720 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16721 == CPP_OPEN_SQUARE)
16722 && next_token->flags & DIGRAPH
16723 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16724 == CPP_COLON)
16725 && !(next_token_2->flags & PREV_WHITE))
16726 {
16727 cp_parser_parse_tentatively (parser);
16728 /* Change `:' into `::'. */
16729 next_token_2->type = CPP_SCOPE;
16730 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16731 CPP_LESS. */
16732 cp_lexer_consume_token (parser->lexer);
16733
16734 /* Parse the arguments. */
16735 arguments = cp_parser_enclosed_template_argument_list (parser);
16736 if (!cp_parser_parse_definitely (parser))
16737 {
16738 /* If we couldn't parse an argument list, then we revert our changes
16739 and return simply an error. Maybe this is not a template-id
16740 after all. */
16741 next_token_2->type = CPP_COLON;
16742 cp_parser_error (parser, "expected %<<%>");
16743 pop_deferring_access_checks ();
16744 return error_mark_node;
16745 }
16746 /* Otherwise, emit an error about the invalid digraph, but continue
16747 parsing because we got our argument list. */
16748 if (permerror (next_token->location,
16749 "%<<::%> cannot begin a template-argument list"))
16750 {
16751 static bool hint = false;
16752 inform (next_token->location,
16753 "%<<:%> is an alternate spelling for %<[%>."
16754 " Insert whitespace between %<<%> and %<::%>");
16755 if (!hint && !flag_permissive)
16756 {
16757 inform (next_token->location, "(if you use %<-fpermissive%> "
16758 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16759 "accept your code)");
16760 hint = true;
16761 }
16762 }
16763 }
16764 else
16765 {
16766 /* Look for the `<' that starts the template-argument-list. */
16767 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16768 {
16769 pop_deferring_access_checks ();
16770 return error_mark_node;
16771 }
16772 /* Parse the arguments. */
16773 arguments = cp_parser_enclosed_template_argument_list (parser);
16774
16775 if ((cxx_dialect > cxx17)
16776 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16777 && !template_keyword_p
16778 && (cp_parser_error_occurred (parser)
16779 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16780 {
16781 /* This didn't go well. */
16782 if (TREE_CODE (templ) == FUNCTION_DECL)
16783 {
16784 /* C++20 says that "function-name < a;" is now ill-formed. */
16785 if (cp_parser_error_occurred (parser))
16786 {
16787 error_at (token->location, "invalid template-argument-list");
16788 inform (token->location, "function name as the left hand "
16789 "operand of %<<%> is ill-formed in C++20; wrap the "
16790 "function name in %<()%>");
16791 }
16792 else
16793 /* We expect "f<targs>" to be followed by "(args)". */
16794 error_at (cp_lexer_peek_token (parser->lexer)->location,
16795 "expected %<(%> after template-argument-list");
16796 if (start_of_id)
16797 /* Purge all subsequent tokens. */
16798 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16799 }
16800 else
16801 cp_parser_simulate_error (parser);
16802 pop_deferring_access_checks ();
16803 return error_mark_node;
16804 }
16805 }
16806
16807 /* Set the location to be of the form:
16808 template-name < template-argument-list [opt] >
16809 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16810 with caret == start at the start of the template-name,
16811 ranging until the closing '>'. */
16812 location_t combined_loc
16813 = make_location (token->location, token->location, parser->lexer);
16814
16815 /* Check for concepts autos where they don't belong. We could
16816 identify types in some cases of identifier TEMPL, looking ahead
16817 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16818 types. We reject them in functions, but if what we have is an
16819 identifier, even with none_type we can't conclude it's NOT a
16820 type, we have to wait for template substitution. */
16821 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16822 template_id = error_mark_node;
16823 /* Build a representation of the specialization. */
16824 else if (identifier_p (templ))
16825 template_id = build_min_nt_loc (combined_loc,
16826 TEMPLATE_ID_EXPR,
16827 templ, arguments);
16828 else if (DECL_TYPE_TEMPLATE_P (templ)
16829 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16830 {
16831 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16832 template (rather than some instantiation thereof) only if
16833 is not nested within some other construct. For example, in
16834 "template <typename T> void f(T) { A<T>::", A<T> is just an
16835 instantiation of A. */
16836 bool entering_scope
16837 = (template_parm_scope_p ()
16838 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16839 template_id
16840 = finish_template_type (templ, arguments, entering_scope);
16841 }
16842 else if (concept_definition_p (templ))
16843 {
16844 /* The caller will decide whether this is a concept check or type
16845 constraint. */
16846 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
16847 boolean_type_node, templ, arguments);
16848 }
16849 else if (variable_template_p (templ))
16850 {
16851 template_id = lookup_template_variable (templ, arguments);
16852 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16853 SET_EXPR_LOCATION (template_id, combined_loc);
16854 }
16855 else
16856 {
16857 /* If it's not a class-template or a template-template, it should be
16858 a function-template. */
16859 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
16860
16861 template_id = lookup_template_function (templ, arguments);
16862 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16863 SET_EXPR_LOCATION (template_id, combined_loc);
16864 }
16865
16866 /* If parsing tentatively, replace the sequence of tokens that makes
16867 up the template-id with a CPP_TEMPLATE_ID token. That way,
16868 should we re-parse the token stream, we will not have to repeat
16869 the effort required to do the parse, nor will we issue duplicate
16870 error messages about problems during instantiation of the
16871 template. */
16872 if (start_of_id
16873 /* Don't do this if we had a parse error in a declarator; re-parsing
16874 might succeed if a name changes meaning (60361). */
16875 && !(cp_parser_error_occurred (parser)
16876 && cp_parser_parsing_tentatively (parser)
16877 && parser->in_declarator_p))
16878 {
16879 /* Reset the contents of the START_OF_ID token. */
16880 token->type = CPP_TEMPLATE_ID;
16881 token->location = combined_loc;
16882
16883 /* Retrieve any deferred checks. Do not pop this access checks yet
16884 so the memory will not be reclaimed during token replacing below. */
16885 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16886 token->tree_check_p = true;
16887 token->u.tree_check_value->value = template_id;
16888 token->u.tree_check_value->checks = get_deferred_access_checks ();
16889 token->keyword = RID_MAX;
16890
16891 /* Purge all subsequent tokens. */
16892 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16893
16894 /* ??? Can we actually assume that, if template_id ==
16895 error_mark_node, we will have issued a diagnostic to the
16896 user, as opposed to simply marking the tentative parse as
16897 failed? */
16898 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16899 error_at (token->location, "parse error in template argument list");
16900 }
16901
16902 pop_to_parent_deferring_access_checks ();
16903 return template_id;
16904 }
16905
16906 /* Like cp_parser_template_id, called in non-type context. */
16907
16908 static tree
16909 cp_parser_template_id_expr (cp_parser *parser,
16910 bool template_keyword_p,
16911 bool check_dependency_p,
16912 bool is_declaration)
16913 {
16914 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
16915 none_type, is_declaration);
16916 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
16917 && concept_check_p (x))
16918 /* We didn't check the arguments in cp_parser_template_id; do that now. */
16919 return build_concept_id (x);
16920 return x;
16921 }
16922
16923 /* Parse a template-name.
16924
16925 template-name:
16926 identifier
16927
16928 The standard should actually say:
16929
16930 template-name:
16931 identifier
16932 operator-function-id
16933
16934 A defect report has been filed about this issue.
16935
16936 A conversion-function-id cannot be a template name because they cannot
16937 be part of a template-id. In fact, looking at this code:
16938
16939 a.operator K<int>()
16940
16941 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16942 It is impossible to call a templated conversion-function-id with an
16943 explicit argument list, since the only allowed template parameter is
16944 the type to which it is converting.
16945
16946 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16947 `template' keyword, in a construction like:
16948
16949 T::template f<3>()
16950
16951 In that case `f' is taken to be a template-name, even though there
16952 is no way of knowing for sure.
16953
16954 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16955 name refers to a set of overloaded functions, at least one of which
16956 is a template, or an IDENTIFIER_NODE with the name of the template,
16957 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16958 names are looked up inside uninstantiated templates. */
16959
16960 static tree
16961 cp_parser_template_name (cp_parser* parser,
16962 bool template_keyword_p,
16963 bool check_dependency_p,
16964 bool is_declaration,
16965 enum tag_types tag_type,
16966 bool *is_identifier)
16967 {
16968 tree identifier;
16969 tree decl;
16970 cp_token *token = cp_lexer_peek_token (parser->lexer);
16971
16972 /* If the next token is `operator', then we have either an
16973 operator-function-id or a conversion-function-id. */
16974 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16975 {
16976 /* We don't know whether we're looking at an
16977 operator-function-id or a conversion-function-id. */
16978 cp_parser_parse_tentatively (parser);
16979 /* Try an operator-function-id. */
16980 identifier = cp_parser_operator_function_id (parser);
16981 /* If that didn't work, try a conversion-function-id. */
16982 if (!cp_parser_parse_definitely (parser))
16983 {
16984 cp_parser_error (parser, "expected template-name");
16985 return error_mark_node;
16986 }
16987 }
16988 /* Look for the identifier. */
16989 else
16990 identifier = cp_parser_identifier (parser);
16991
16992 /* If we didn't find an identifier, we don't have a template-id. */
16993 if (identifier == error_mark_node)
16994 return error_mark_node;
16995
16996 /* If the name immediately followed the `template' keyword, then it
16997 is a template-name. However, if the next token is not `<', then
16998 we do not treat it as a template-name, since it is not being used
16999 as part of a template-id. This enables us to handle constructs
17000 like:
17001
17002 template <typename T> struct S { S(); };
17003 template <typename T> S<T>::S();
17004
17005 correctly. We would treat `S' as a template -- if it were `S<T>'
17006 -- but we do not if there is no `<'. */
17007
17008 if (processing_template_decl
17009 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
17010 {
17011 /* In a declaration, in a dependent context, we pretend that the
17012 "template" keyword was present in order to improve error
17013 recovery. For example, given:
17014
17015 template <typename T> void f(T::X<int>);
17016
17017 we want to treat "X<int>" as a template-id. */
17018 if (is_declaration
17019 && !template_keyword_p
17020 && parser->scope && TYPE_P (parser->scope)
17021 && check_dependency_p
17022 && dependent_scope_p (parser->scope)
17023 /* Do not do this for dtors (or ctors), since they never
17024 need the template keyword before their name. */
17025 && !constructor_name_p (identifier, parser->scope))
17026 {
17027 cp_token_position start = 0;
17028
17029 /* Explain what went wrong. */
17030 error_at (token->location, "non-template %qD used as template",
17031 identifier);
17032 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
17033 parser->scope, identifier);
17034 /* If parsing tentatively, find the location of the "<" token. */
17035 if (cp_parser_simulate_error (parser))
17036 start = cp_lexer_token_position (parser->lexer, true);
17037 /* Parse the template arguments so that we can issue error
17038 messages about them. */
17039 cp_lexer_consume_token (parser->lexer);
17040 cp_parser_enclosed_template_argument_list (parser);
17041 /* Skip tokens until we find a good place from which to
17042 continue parsing. */
17043 cp_parser_skip_to_closing_parenthesis (parser,
17044 /*recovering=*/true,
17045 /*or_comma=*/true,
17046 /*consume_paren=*/false);
17047 /* If parsing tentatively, permanently remove the
17048 template argument list. That will prevent duplicate
17049 error messages from being issued about the missing
17050 "template" keyword. */
17051 if (start)
17052 cp_lexer_purge_tokens_after (parser->lexer, start);
17053 if (is_identifier)
17054 *is_identifier = true;
17055 parser->context->object_type = NULL_TREE;
17056 return identifier;
17057 }
17058
17059 /* If the "template" keyword is present, then there is generally
17060 no point in doing name-lookup, so we just return IDENTIFIER.
17061 But, if the qualifying scope is non-dependent then we can
17062 (and must) do name-lookup normally. */
17063 if (template_keyword_p)
17064 {
17065 tree scope = (parser->scope ? parser->scope
17066 : parser->context->object_type);
17067 if (scope && TYPE_P (scope)
17068 && (!CLASS_TYPE_P (scope)
17069 || (check_dependency_p && dependent_type_p (scope))))
17070 {
17071 /* We're optimizing away the call to cp_parser_lookup_name, but
17072 we still need to do this. */
17073 parser->context->object_type = NULL_TREE;
17074 return identifier;
17075 }
17076 }
17077 }
17078
17079 /* cp_parser_lookup_name clears OBJECT_TYPE. */
17080 const bool scoped_p = ((parser->scope ? parser->scope
17081 : parser->context->object_type) != NULL_TREE);
17082
17083 /* Look up the name. */
17084 decl = cp_parser_lookup_name (parser, identifier,
17085 tag_type,
17086 /*is_template=*/true,
17087 /*is_namespace=*/false,
17088 check_dependency_p,
17089 /*ambiguous_decls=*/NULL,
17090 token->location);
17091
17092 decl = strip_using_decl (decl);
17093
17094 /* If DECL is a template, then the name was a template-name. */
17095 if (TREE_CODE (decl) == TEMPLATE_DECL)
17096 {
17097 if (TREE_DEPRECATED (decl)
17098 && deprecated_state != DEPRECATED_SUPPRESS)
17099 {
17100 tree d = DECL_TEMPLATE_RESULT (decl);
17101 tree attr;
17102 if (TREE_CODE (d) == TYPE_DECL)
17103 attr = lookup_attribute ("deprecated",
17104 TYPE_ATTRIBUTES (TREE_TYPE (d)));
17105 else
17106 attr = lookup_attribute ("deprecated",
17107 DECL_ATTRIBUTES (d));
17108 warn_deprecated_use (decl, attr);
17109 }
17110 }
17111 else
17112 {
17113 /* The standard does not explicitly indicate whether a name that
17114 names a set of overloaded declarations, some of which are
17115 templates, is a template-name. However, such a name should
17116 be a template-name; otherwise, there is no way to form a
17117 template-id for the overloaded templates. */
17118 bool found = false;
17119
17120 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
17121 !found && iter; ++iter)
17122 if (TREE_CODE (*iter) == TEMPLATE_DECL)
17123 found = true;
17124
17125 if (!found
17126 && (cxx_dialect > cxx17)
17127 && !scoped_p
17128 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
17129 && tag_type == none_type)
17130 {
17131 /* [temp.names] says "A name is also considered to refer to a template
17132 if it is an unqualified-id followed by a < and name lookup finds
17133 either one or more functions or finds nothing." */
17134
17135 /* The "more functions" case. Just use the OVERLOAD as normally.
17136 We don't use is_overloaded_fn here to avoid considering
17137 BASELINKs. */
17138 if (TREE_CODE (decl) == OVERLOAD
17139 /* Name lookup found one function. */
17140 || TREE_CODE (decl) == FUNCTION_DECL)
17141 found = true;
17142 /* Name lookup found nothing. */
17143 else if (decl == error_mark_node)
17144 return identifier;
17145 }
17146
17147 if (!found)
17148 {
17149 /* The name does not name a template. */
17150 cp_parser_error (parser, "expected template-name");
17151 return error_mark_node;
17152 }
17153 }
17154
17155 return decl;
17156 }
17157
17158 /* Parse a template-argument-list.
17159
17160 template-argument-list:
17161 template-argument ... [opt]
17162 template-argument-list , template-argument ... [opt]
17163
17164 Returns a TREE_VEC containing the arguments. */
17165
17166 static tree
17167 cp_parser_template_argument_list (cp_parser* parser)
17168 {
17169 tree fixed_args[10];
17170 unsigned n_args = 0;
17171 unsigned alloced = 10;
17172 tree *arg_ary = fixed_args;
17173 tree vec;
17174 bool saved_in_template_argument_list_p;
17175 bool saved_ice_p;
17176 bool saved_non_ice_p;
17177
17178 /* Don't create location wrapper nodes within a template-argument-list. */
17179 auto_suppress_location_wrappers sentinel;
17180
17181 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
17182 parser->in_template_argument_list_p = true;
17183 /* Even if the template-id appears in an integral
17184 constant-expression, the contents of the argument list do
17185 not. */
17186 saved_ice_p = parser->integral_constant_expression_p;
17187 parser->integral_constant_expression_p = false;
17188 saved_non_ice_p = parser->non_integral_constant_expression_p;
17189 parser->non_integral_constant_expression_p = false;
17190
17191 /* Parse the arguments. */
17192 do
17193 {
17194 tree argument;
17195
17196 if (n_args)
17197 /* Consume the comma. */
17198 cp_lexer_consume_token (parser->lexer);
17199
17200 /* Parse the template-argument. */
17201 argument = cp_parser_template_argument (parser);
17202
17203 /* If the next token is an ellipsis, we're expanding a template
17204 argument pack. */
17205 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17206 {
17207 if (argument == error_mark_node)
17208 {
17209 cp_token *token = cp_lexer_peek_token (parser->lexer);
17210 error_at (token->location,
17211 "expected parameter pack before %<...%>");
17212 }
17213 /* Consume the `...' token. */
17214 cp_lexer_consume_token (parser->lexer);
17215
17216 /* Make the argument into a TYPE_PACK_EXPANSION or
17217 EXPR_PACK_EXPANSION. */
17218 argument = make_pack_expansion (argument);
17219 }
17220
17221 if (n_args == alloced)
17222 {
17223 alloced *= 2;
17224
17225 if (arg_ary == fixed_args)
17226 {
17227 arg_ary = XNEWVEC (tree, alloced);
17228 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
17229 }
17230 else
17231 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
17232 }
17233 arg_ary[n_args++] = argument;
17234 }
17235 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17236
17237 vec = make_tree_vec (n_args);
17238
17239 while (n_args--)
17240 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
17241
17242 if (arg_ary != fixed_args)
17243 free (arg_ary);
17244 parser->non_integral_constant_expression_p = saved_non_ice_p;
17245 parser->integral_constant_expression_p = saved_ice_p;
17246 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
17247 if (CHECKING_P)
17248 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
17249 return vec;
17250 }
17251
17252 /* Parse a template-argument.
17253
17254 template-argument:
17255 assignment-expression
17256 type-id
17257 id-expression
17258
17259 The representation is that of an assignment-expression, type-id, or
17260 id-expression -- except that the qualified id-expression is
17261 evaluated, so that the value returned is either a DECL or an
17262 OVERLOAD.
17263
17264 Although the standard says "assignment-expression", it forbids
17265 throw-expressions or assignments in the template argument.
17266 Therefore, we use "conditional-expression" instead. */
17267
17268 static tree
17269 cp_parser_template_argument (cp_parser* parser)
17270 {
17271 tree argument;
17272 bool template_p;
17273 bool address_p;
17274 bool maybe_type_id = false;
17275 cp_token *token = NULL, *argument_start_token = NULL;
17276 location_t loc = 0;
17277 cp_id_kind idk;
17278
17279 /* There's really no way to know what we're looking at, so we just
17280 try each alternative in order.
17281
17282 [temp.arg]
17283
17284 In a template-argument, an ambiguity between a type-id and an
17285 expression is resolved to a type-id, regardless of the form of
17286 the corresponding template-parameter.
17287
17288 Therefore, we try a type-id first. */
17289 cp_parser_parse_tentatively (parser);
17290 argument = cp_parser_template_type_arg (parser);
17291 /* If there was no error parsing the type-id but the next token is a
17292 '>>', our behavior depends on which dialect of C++ we're
17293 parsing. In C++98, we probably found a typo for '> >'. But there
17294 are type-id which are also valid expressions. For instance:
17295
17296 struct X { int operator >> (int); };
17297 template <int V> struct Foo {};
17298 Foo<X () >> 5> r;
17299
17300 Here 'X()' is a valid type-id of a function type, but the user just
17301 wanted to write the expression "X() >> 5". Thus, we remember that we
17302 found a valid type-id, but we still try to parse the argument as an
17303 expression to see what happens.
17304
17305 In C++0x, the '>>' will be considered two separate '>'
17306 tokens. */
17307 if (!cp_parser_error_occurred (parser)
17308 && cxx_dialect == cxx98
17309 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17310 {
17311 maybe_type_id = true;
17312 cp_parser_abort_tentative_parse (parser);
17313 }
17314 else
17315 {
17316 /* If the next token isn't a `,' or a `>', then this argument wasn't
17317 really finished. This means that the argument is not a valid
17318 type-id. */
17319 if (!cp_parser_next_token_ends_template_argument_p (parser))
17320 cp_parser_error (parser, "expected template-argument");
17321 /* If that worked, we're done. */
17322 if (cp_parser_parse_definitely (parser))
17323 return argument;
17324 }
17325 /* We're still not sure what the argument will be. */
17326 cp_parser_parse_tentatively (parser);
17327 /* Try a template. */
17328 argument_start_token = cp_lexer_peek_token (parser->lexer);
17329 argument = cp_parser_id_expression (parser,
17330 /*template_keyword_p=*/false,
17331 /*check_dependency_p=*/true,
17332 &template_p,
17333 /*declarator_p=*/false,
17334 /*optional_p=*/false);
17335 /* If the next token isn't a `,' or a `>', then this argument wasn't
17336 really finished. */
17337 if (!cp_parser_next_token_ends_template_argument_p (parser))
17338 cp_parser_error (parser, "expected template-argument");
17339 if (!cp_parser_error_occurred (parser))
17340 {
17341 /* Figure out what is being referred to. If the id-expression
17342 was for a class template specialization, then we will have a
17343 TYPE_DECL at this point. There is no need to do name lookup
17344 at this point in that case. */
17345 if (TREE_CODE (argument) != TYPE_DECL)
17346 argument = cp_parser_lookup_name (parser, argument,
17347 none_type,
17348 /*is_template=*/template_p,
17349 /*is_namespace=*/false,
17350 /*check_dependency=*/true,
17351 /*ambiguous_decls=*/NULL,
17352 argument_start_token->location);
17353 if (TREE_CODE (argument) != TEMPLATE_DECL
17354 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
17355 cp_parser_error (parser, "expected template-name");
17356 }
17357 if (cp_parser_parse_definitely (parser))
17358 {
17359 if (TREE_DEPRECATED (argument))
17360 warn_deprecated_use (argument, NULL_TREE);
17361 return argument;
17362 }
17363 /* It must be a non-type argument. In C++17 any constant-expression is
17364 allowed. */
17365 if (cxx_dialect > cxx14)
17366 goto general_expr;
17367
17368 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
17369
17370 -- an integral constant-expression of integral or enumeration
17371 type; or
17372
17373 -- the name of a non-type template-parameter; or
17374
17375 -- the name of an object or function with external linkage...
17376
17377 -- the address of an object or function with external linkage...
17378
17379 -- a pointer to member... */
17380 /* Look for a non-type template parameter. */
17381 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17382 {
17383 cp_parser_parse_tentatively (parser);
17384 argument = cp_parser_primary_expression (parser,
17385 /*address_p=*/false,
17386 /*cast_p=*/false,
17387 /*template_arg_p=*/true,
17388 &idk);
17389 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
17390 || !cp_parser_next_token_ends_template_argument_p (parser))
17391 cp_parser_simulate_error (parser);
17392 if (cp_parser_parse_definitely (parser))
17393 return argument;
17394 }
17395
17396 /* If the next token is "&", the argument must be the address of an
17397 object or function with external linkage. */
17398 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
17399 if (address_p)
17400 {
17401 loc = cp_lexer_peek_token (parser->lexer)->location;
17402 cp_lexer_consume_token (parser->lexer);
17403 }
17404 /* See if we might have an id-expression. */
17405 token = cp_lexer_peek_token (parser->lexer);
17406 if (token->type == CPP_NAME
17407 || token->keyword == RID_OPERATOR
17408 || token->type == CPP_SCOPE
17409 || token->type == CPP_TEMPLATE_ID
17410 || token->type == CPP_NESTED_NAME_SPECIFIER)
17411 {
17412 cp_parser_parse_tentatively (parser);
17413 argument = cp_parser_primary_expression (parser,
17414 address_p,
17415 /*cast_p=*/false,
17416 /*template_arg_p=*/true,
17417 &idk);
17418 if (cp_parser_error_occurred (parser)
17419 || !cp_parser_next_token_ends_template_argument_p (parser))
17420 cp_parser_abort_tentative_parse (parser);
17421 else
17422 {
17423 tree probe;
17424
17425 if (INDIRECT_REF_P (argument))
17426 {
17427 /* Strip the dereference temporarily. */
17428 gcc_assert (REFERENCE_REF_P (argument));
17429 argument = TREE_OPERAND (argument, 0);
17430 }
17431
17432 /* If we're in a template, we represent a qualified-id referring
17433 to a static data member as a SCOPE_REF even if the scope isn't
17434 dependent so that we can check access control later. */
17435 probe = argument;
17436 if (TREE_CODE (probe) == SCOPE_REF)
17437 probe = TREE_OPERAND (probe, 1);
17438 if (VAR_P (probe))
17439 {
17440 /* A variable without external linkage might still be a
17441 valid constant-expression, so no error is issued here
17442 if the external-linkage check fails. */
17443 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
17444 cp_parser_simulate_error (parser);
17445 }
17446 else if (is_overloaded_fn (argument))
17447 /* All overloaded functions are allowed; if the external
17448 linkage test does not pass, an error will be issued
17449 later. */
17450 ;
17451 else if (address_p
17452 && (TREE_CODE (argument) == OFFSET_REF
17453 || TREE_CODE (argument) == SCOPE_REF))
17454 /* A pointer-to-member. */
17455 ;
17456 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
17457 ;
17458 else
17459 cp_parser_simulate_error (parser);
17460
17461 if (cp_parser_parse_definitely (parser))
17462 {
17463 if (address_p)
17464 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
17465 tf_warning_or_error);
17466 else
17467 argument = convert_from_reference (argument);
17468 return argument;
17469 }
17470 }
17471 }
17472 /* If the argument started with "&", there are no other valid
17473 alternatives at this point. */
17474 if (address_p)
17475 {
17476 cp_parser_error (parser, "invalid non-type template argument");
17477 return error_mark_node;
17478 }
17479
17480 general_expr:
17481 /* If the argument wasn't successfully parsed as a type-id followed
17482 by '>>', the argument can only be a constant expression now.
17483 Otherwise, we try parsing the constant-expression tentatively,
17484 because the argument could really be a type-id. */
17485 if (maybe_type_id)
17486 cp_parser_parse_tentatively (parser);
17487
17488 if (cxx_dialect <= cxx14)
17489 argument = cp_parser_constant_expression (parser);
17490 else
17491 {
17492 /* In C++20, we can encounter a braced-init-list. */
17493 if (cxx_dialect >= cxx20
17494 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17495 {
17496 bool expr_non_constant_p;
17497 return cp_parser_braced_list (parser, &expr_non_constant_p);
17498 }
17499
17500 /* With C++17 generalized non-type template arguments we need to handle
17501 lvalue constant expressions, too. */
17502 argument = cp_parser_assignment_expression (parser);
17503 require_potential_constant_expression (argument);
17504 }
17505
17506 if (!maybe_type_id)
17507 return argument;
17508 if (!cp_parser_next_token_ends_template_argument_p (parser))
17509 cp_parser_error (parser, "expected template-argument");
17510 if (cp_parser_parse_definitely (parser))
17511 return argument;
17512 /* We did our best to parse the argument as a non type-id, but that
17513 was the only alternative that matched (albeit with a '>' after
17514 it). We can assume it's just a typo from the user, and a
17515 diagnostic will then be issued. */
17516 return cp_parser_template_type_arg (parser);
17517 }
17518
17519 /* Parse an explicit-instantiation.
17520
17521 explicit-instantiation:
17522 template declaration
17523
17524 Although the standard says `declaration', what it really means is:
17525
17526 explicit-instantiation:
17527 template decl-specifier-seq [opt] declarator [opt] ;
17528
17529 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17530 supposed to be allowed. A defect report has been filed about this
17531 issue.
17532
17533 GNU Extension:
17534
17535 explicit-instantiation:
17536 storage-class-specifier template
17537 decl-specifier-seq [opt] declarator [opt] ;
17538 function-specifier template
17539 decl-specifier-seq [opt] declarator [opt] ; */
17540
17541 static void
17542 cp_parser_explicit_instantiation (cp_parser* parser)
17543 {
17544 int declares_class_or_enum;
17545 cp_decl_specifier_seq decl_specifiers;
17546 tree extension_specifier = NULL_TREE;
17547
17548 timevar_push (TV_TEMPLATE_INST);
17549
17550 /* Look for an (optional) storage-class-specifier or
17551 function-specifier. */
17552 if (cp_parser_allow_gnu_extensions_p (parser))
17553 {
17554 extension_specifier
17555 = cp_parser_storage_class_specifier_opt (parser);
17556 if (!extension_specifier)
17557 extension_specifier
17558 = cp_parser_function_specifier_opt (parser,
17559 /*decl_specs=*/NULL);
17560 }
17561
17562 /* Look for the `template' keyword. */
17563 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17564 /* Let the front end know that we are processing an explicit
17565 instantiation. */
17566 begin_explicit_instantiation ();
17567 /* [temp.explicit] says that we are supposed to ignore access
17568 control while processing explicit instantiation directives. */
17569 push_deferring_access_checks (dk_no_check);
17570 /* Parse a decl-specifier-seq. */
17571 cp_parser_decl_specifier_seq (parser,
17572 CP_PARSER_FLAGS_OPTIONAL,
17573 &decl_specifiers,
17574 &declares_class_or_enum);
17575 /* If there was exactly one decl-specifier, and it declared a class,
17576 and there's no declarator, then we have an explicit type
17577 instantiation. */
17578 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17579 {
17580 tree type;
17581
17582 type = check_tag_decl (&decl_specifiers,
17583 /*explicit_type_instantiation_p=*/true);
17584 /* Turn access control back on for names used during
17585 template instantiation. */
17586 pop_deferring_access_checks ();
17587 if (type)
17588 do_type_instantiation (type, extension_specifier,
17589 /*complain=*/tf_error);
17590 }
17591 else
17592 {
17593 cp_declarator *declarator;
17594 tree decl;
17595
17596 /* Parse the declarator. */
17597 declarator
17598 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17599 CP_PARSER_FLAGS_NONE,
17600 /*ctor_dtor_or_conv_p=*/NULL,
17601 /*parenthesized_p=*/NULL,
17602 /*member_p=*/false,
17603 /*friend_p=*/false,
17604 /*static_p=*/false);
17605 if (declares_class_or_enum & 2)
17606 cp_parser_check_for_definition_in_return_type (declarator,
17607 decl_specifiers.type,
17608 decl_specifiers.locations[ds_type_spec]);
17609 if (declarator != cp_error_declarator)
17610 {
17611 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17612 permerror (decl_specifiers.locations[ds_inline],
17613 "explicit instantiation shall not use"
17614 " %<inline%> specifier");
17615 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17616 permerror (decl_specifiers.locations[ds_constexpr],
17617 "explicit instantiation shall not use"
17618 " %<constexpr%> specifier");
17619 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
17620 permerror (decl_specifiers.locations[ds_consteval],
17621 "explicit instantiation shall not use"
17622 " %<consteval%> specifier");
17623
17624 decl = grokdeclarator (declarator, &decl_specifiers,
17625 NORMAL, 0, &decl_specifiers.attributes);
17626 /* Turn access control back on for names used during
17627 template instantiation. */
17628 pop_deferring_access_checks ();
17629 /* Do the explicit instantiation. */
17630 do_decl_instantiation (decl, extension_specifier);
17631 }
17632 else
17633 {
17634 pop_deferring_access_checks ();
17635 /* Skip the body of the explicit instantiation. */
17636 cp_parser_skip_to_end_of_statement (parser);
17637 }
17638 }
17639 /* We're done with the instantiation. */
17640 end_explicit_instantiation ();
17641
17642 cp_parser_consume_semicolon_at_end_of_statement (parser);
17643
17644 timevar_pop (TV_TEMPLATE_INST);
17645 }
17646
17647 /* Parse an explicit-specialization.
17648
17649 explicit-specialization:
17650 template < > declaration
17651
17652 Although the standard says `declaration', what it really means is:
17653
17654 explicit-specialization:
17655 template <> decl-specifier [opt] init-declarator [opt] ;
17656 template <> function-definition
17657 template <> explicit-specialization
17658 template <> template-declaration */
17659
17660 static void
17661 cp_parser_explicit_specialization (cp_parser* parser)
17662 {
17663 cp_token *token = cp_lexer_peek_token (parser->lexer);
17664
17665 /* Look for the `template' keyword. */
17666 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17667 /* Look for the `<'. */
17668 cp_parser_require (parser, CPP_LESS, RT_LESS);
17669 /* Look for the `>'. */
17670 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17671 /* We have processed another parameter list. */
17672 ++parser->num_template_parameter_lists;
17673
17674 /* [temp]
17675
17676 A template ... explicit specialization ... shall not have C
17677 linkage. */
17678 bool need_lang_pop = current_lang_name == lang_name_c;
17679 if (need_lang_pop)
17680 {
17681 error_at (token->location, "template specialization with C linkage");
17682 maybe_show_extern_c_location ();
17683
17684 /* Give it C++ linkage to avoid confusing other parts of the
17685 front end. */
17686 push_lang_context (lang_name_cplusplus);
17687 need_lang_pop = true;
17688 }
17689
17690 /* Let the front end know that we are beginning a specialization. */
17691 if (begin_specialization ())
17692 {
17693 /* If the next keyword is `template', we need to figure out
17694 whether or not we're looking a template-declaration. */
17695 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17696 {
17697 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17698 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17699 cp_parser_template_declaration_after_export (parser,
17700 /*member_p=*/false);
17701 else
17702 cp_parser_explicit_specialization (parser);
17703 }
17704 else
17705 /* Parse the dependent declaration. */
17706 cp_parser_single_declaration (parser,
17707 /*checks=*/NULL,
17708 /*member_p=*/false,
17709 /*explicit_specialization_p=*/true,
17710 /*friend_p=*/NULL);
17711 }
17712
17713 /* We're done with the specialization. */
17714 end_specialization ();
17715
17716 /* For the erroneous case of a template with C linkage, we pushed an
17717 implicit C++ linkage scope; exit that scope now. */
17718 if (need_lang_pop)
17719 pop_lang_context ();
17720
17721 /* We're done with this parameter list. */
17722 --parser->num_template_parameter_lists;
17723 }
17724
17725 /* Parse a type-specifier.
17726
17727 type-specifier:
17728 simple-type-specifier
17729 class-specifier
17730 enum-specifier
17731 elaborated-type-specifier
17732 cv-qualifier
17733
17734 GNU Extension:
17735
17736 type-specifier:
17737 __complex__
17738
17739 Returns a representation of the type-specifier. For a
17740 class-specifier, enum-specifier, or elaborated-type-specifier, a
17741 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17742
17743 The parser flags FLAGS is used to control type-specifier parsing.
17744
17745 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17746 in a decl-specifier-seq.
17747
17748 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17749 class-specifier, enum-specifier, or elaborated-type-specifier, then
17750 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17751 if a type is declared; 2 if it is defined. Otherwise, it is set to
17752 zero.
17753
17754 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17755 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17756 is set to FALSE. */
17757
17758 static tree
17759 cp_parser_type_specifier (cp_parser* parser,
17760 cp_parser_flags flags,
17761 cp_decl_specifier_seq *decl_specs,
17762 bool is_declaration,
17763 int* declares_class_or_enum,
17764 bool* is_cv_qualifier)
17765 {
17766 tree type_spec = NULL_TREE;
17767 cp_token *token;
17768 enum rid keyword;
17769 cp_decl_spec ds = ds_last;
17770
17771 /* Assume this type-specifier does not declare a new type. */
17772 if (declares_class_or_enum)
17773 *declares_class_or_enum = 0;
17774 /* And that it does not specify a cv-qualifier. */
17775 if (is_cv_qualifier)
17776 *is_cv_qualifier = false;
17777 /* Peek at the next token. */
17778 token = cp_lexer_peek_token (parser->lexer);
17779
17780 /* If we're looking at a keyword, we can use that to guide the
17781 production we choose. */
17782 keyword = token->keyword;
17783 switch (keyword)
17784 {
17785 case RID_ENUM:
17786 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17787 goto elaborated_type_specifier;
17788
17789 /* Look for the enum-specifier. */
17790 type_spec = cp_parser_enum_specifier (parser);
17791 /* If that worked, we're done. */
17792 if (type_spec)
17793 {
17794 if (declares_class_or_enum)
17795 *declares_class_or_enum = 2;
17796 if (decl_specs)
17797 cp_parser_set_decl_spec_type (decl_specs,
17798 type_spec,
17799 token,
17800 /*type_definition_p=*/true);
17801 return type_spec;
17802 }
17803 else
17804 goto elaborated_type_specifier;
17805
17806 /* Any of these indicate either a class-specifier, or an
17807 elaborated-type-specifier. */
17808 case RID_CLASS:
17809 case RID_STRUCT:
17810 case RID_UNION:
17811 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17812 goto elaborated_type_specifier;
17813
17814 /* Parse tentatively so that we can back up if we don't find a
17815 class-specifier. */
17816 cp_parser_parse_tentatively (parser);
17817 /* Look for the class-specifier. */
17818 type_spec = cp_parser_class_specifier (parser);
17819 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17820 /* If that worked, we're done. */
17821 if (cp_parser_parse_definitely (parser))
17822 {
17823 if (declares_class_or_enum)
17824 *declares_class_or_enum = 2;
17825 if (decl_specs)
17826 cp_parser_set_decl_spec_type (decl_specs,
17827 type_spec,
17828 token,
17829 /*type_definition_p=*/true);
17830 return type_spec;
17831 }
17832
17833 /* Fall through. */
17834 elaborated_type_specifier:
17835 /* We're declaring (not defining) a class or enum. */
17836 if (declares_class_or_enum)
17837 *declares_class_or_enum = 1;
17838
17839 /* Fall through. */
17840 case RID_TYPENAME:
17841 /* Look for an elaborated-type-specifier. */
17842 type_spec
17843 = (cp_parser_elaborated_type_specifier
17844 (parser,
17845 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17846 is_declaration));
17847 if (decl_specs)
17848 cp_parser_set_decl_spec_type (decl_specs,
17849 type_spec,
17850 token,
17851 /*type_definition_p=*/false);
17852 return type_spec;
17853
17854 case RID_CONST:
17855 ds = ds_const;
17856 if (is_cv_qualifier)
17857 *is_cv_qualifier = true;
17858 break;
17859
17860 case RID_VOLATILE:
17861 ds = ds_volatile;
17862 if (is_cv_qualifier)
17863 *is_cv_qualifier = true;
17864 break;
17865
17866 case RID_RESTRICT:
17867 ds = ds_restrict;
17868 if (is_cv_qualifier)
17869 *is_cv_qualifier = true;
17870 break;
17871
17872 case RID_COMPLEX:
17873 /* The `__complex__' keyword is a GNU extension. */
17874 ds = ds_complex;
17875 break;
17876
17877 default:
17878 break;
17879 }
17880
17881 /* Handle simple keywords. */
17882 if (ds != ds_last)
17883 {
17884 if (decl_specs)
17885 {
17886 set_and_check_decl_spec_loc (decl_specs, ds, token);
17887 decl_specs->any_specifiers_p = true;
17888 }
17889 return cp_lexer_consume_token (parser->lexer)->u.value;
17890 }
17891
17892 /* If we do not already have a type-specifier, assume we are looking
17893 at a simple-type-specifier. */
17894 type_spec = cp_parser_simple_type_specifier (parser,
17895 decl_specs,
17896 flags);
17897
17898 /* If we didn't find a type-specifier, and a type-specifier was not
17899 optional in this context, issue an error message. */
17900 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17901 {
17902 cp_parser_error (parser, "expected type specifier");
17903 return error_mark_node;
17904 }
17905
17906 return type_spec;
17907 }
17908
17909 /* Parse a simple-type-specifier.
17910
17911 simple-type-specifier:
17912 :: [opt] nested-name-specifier [opt] type-name
17913 :: [opt] nested-name-specifier template template-id
17914 char
17915 wchar_t
17916 bool
17917 short
17918 int
17919 long
17920 signed
17921 unsigned
17922 float
17923 double
17924 void
17925
17926 C++11 Extension:
17927
17928 simple-type-specifier:
17929 auto
17930 decltype ( expression )
17931 char16_t
17932 char32_t
17933 __underlying_type ( type-id )
17934
17935 C++17 extension:
17936
17937 nested-name-specifier(opt) template-name
17938
17939 GNU Extension:
17940
17941 simple-type-specifier:
17942 __int128
17943 __typeof__ unary-expression
17944 __typeof__ ( type-id )
17945 __typeof__ ( type-id ) { initializer-list , [opt] }
17946
17947 Concepts Extension:
17948
17949 simple-type-specifier:
17950 constrained-type-specifier
17951
17952 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17953 appropriately updated. */
17954
17955 static tree
17956 cp_parser_simple_type_specifier (cp_parser* parser,
17957 cp_decl_specifier_seq *decl_specs,
17958 cp_parser_flags flags)
17959 {
17960 tree type = NULL_TREE;
17961 cp_token *token;
17962 int idx;
17963
17964 /* Peek at the next token. */
17965 token = cp_lexer_peek_token (parser->lexer);
17966
17967 /* If we're looking at a keyword, things are easy. */
17968 switch (token->keyword)
17969 {
17970 case RID_CHAR:
17971 if (decl_specs)
17972 decl_specs->explicit_char_p = true;
17973 type = char_type_node;
17974 break;
17975 case RID_CHAR8:
17976 type = char8_type_node;
17977 break;
17978 case RID_CHAR16:
17979 type = char16_type_node;
17980 break;
17981 case RID_CHAR32:
17982 type = char32_type_node;
17983 break;
17984 case RID_WCHAR:
17985 type = wchar_type_node;
17986 break;
17987 case RID_BOOL:
17988 type = boolean_type_node;
17989 break;
17990 case RID_SHORT:
17991 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17992 type = short_integer_type_node;
17993 break;
17994 case RID_INT:
17995 if (decl_specs)
17996 decl_specs->explicit_int_p = true;
17997 type = integer_type_node;
17998 break;
17999 case RID_INT_N_0:
18000 case RID_INT_N_1:
18001 case RID_INT_N_2:
18002 case RID_INT_N_3:
18003 idx = token->keyword - RID_INT_N_0;
18004 if (! int_n_enabled_p [idx])
18005 break;
18006 if (decl_specs)
18007 {
18008 decl_specs->explicit_intN_p = true;
18009 decl_specs->int_n_idx = idx;
18010 /* Check if the alternate "__intN__" form has been used instead of
18011 "__intN". */
18012 if (strncmp (IDENTIFIER_POINTER (token->u.value)
18013 + (IDENTIFIER_LENGTH (token->u.value) - 2),
18014 "__", 2) == 0)
18015 decl_specs->int_n_alt = true;
18016 }
18017 type = int_n_trees [idx].signed_type;
18018 break;
18019 case RID_LONG:
18020 if (decl_specs)
18021 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
18022 type = long_integer_type_node;
18023 break;
18024 case RID_SIGNED:
18025 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
18026 type = integer_type_node;
18027 break;
18028 case RID_UNSIGNED:
18029 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
18030 type = unsigned_type_node;
18031 break;
18032 case RID_FLOAT:
18033 type = float_type_node;
18034 break;
18035 case RID_DOUBLE:
18036 type = double_type_node;
18037 break;
18038 case RID_VOID:
18039 type = void_type_node;
18040 break;
18041
18042 case RID_AUTO:
18043 maybe_warn_cpp0x (CPP0X_AUTO);
18044 if (parser->auto_is_implicit_function_template_parm_p)
18045 {
18046 /* The 'auto' might be the placeholder return type for a function decl
18047 with trailing return type. */
18048 bool have_trailing_return_fn_decl = false;
18049
18050 cp_parser_parse_tentatively (parser);
18051 cp_lexer_consume_token (parser->lexer);
18052 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18053 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
18054 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18055 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
18056 {
18057 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18058 {
18059 cp_lexer_consume_token (parser->lexer);
18060 cp_parser_skip_to_closing_parenthesis (parser,
18061 /*recovering*/false,
18062 /*or_comma*/false,
18063 /*consume_paren*/true);
18064 continue;
18065 }
18066
18067 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
18068 {
18069 have_trailing_return_fn_decl = true;
18070 break;
18071 }
18072
18073 cp_lexer_consume_token (parser->lexer);
18074 }
18075 cp_parser_abort_tentative_parse (parser);
18076
18077 if (have_trailing_return_fn_decl)
18078 {
18079 type = make_auto ();
18080 break;
18081 }
18082
18083 if (cxx_dialect >= cxx14)
18084 {
18085 type = synthesize_implicit_template_parm (parser, NULL_TREE);
18086 type = TREE_TYPE (type);
18087 }
18088 else
18089 type = error_mark_node;
18090
18091 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
18092 {
18093 if (cxx_dialect < cxx14)
18094 error_at (token->location,
18095 "use of %<auto%> in lambda parameter declaration "
18096 "only available with "
18097 "%<-std=c++14%> or %<-std=gnu++14%>");
18098 }
18099 else if (cxx_dialect < cxx14)
18100 error_at (token->location,
18101 "use of %<auto%> in parameter declaration "
18102 "only available with "
18103 "%<-std=c++14%> or %<-std=gnu++14%>");
18104 else if (!flag_concepts)
18105 pedwarn (token->location, 0,
18106 "use of %<auto%> in parameter declaration "
18107 "only available with %<-fconcepts-ts%>");
18108 }
18109 else
18110 type = make_auto ();
18111 break;
18112
18113 case RID_DECLTYPE:
18114 /* Since DR 743, decltype can either be a simple-type-specifier by
18115 itself or begin a nested-name-specifier. Parsing it will replace
18116 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
18117 handling below decide what to do. */
18118 cp_parser_decltype (parser);
18119 cp_lexer_set_token_position (parser->lexer, token);
18120 break;
18121
18122 case RID_TYPEOF:
18123 /* Consume the `typeof' token. */
18124 cp_lexer_consume_token (parser->lexer);
18125 /* Parse the operand to `typeof'. */
18126 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
18127 /* If it is not already a TYPE, take its type. */
18128 if (!TYPE_P (type))
18129 type = finish_typeof (type);
18130
18131 if (decl_specs)
18132 cp_parser_set_decl_spec_type (decl_specs, type,
18133 token,
18134 /*type_definition_p=*/false);
18135
18136 return type;
18137
18138 case RID_UNDERLYING_TYPE:
18139 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
18140 if (decl_specs)
18141 cp_parser_set_decl_spec_type (decl_specs, type,
18142 token,
18143 /*type_definition_p=*/false);
18144
18145 return type;
18146
18147 case RID_BASES:
18148 case RID_DIRECT_BASES:
18149 type = cp_parser_trait_expr (parser, token->keyword);
18150 if (decl_specs)
18151 cp_parser_set_decl_spec_type (decl_specs, type,
18152 token,
18153 /*type_definition_p=*/false);
18154 return type;
18155 default:
18156 break;
18157 }
18158
18159 /* If token is an already-parsed decltype not followed by ::,
18160 it's a simple-type-specifier. */
18161 if (token->type == CPP_DECLTYPE
18162 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
18163 {
18164 type = saved_checks_value (token->u.tree_check_value);
18165 if (decl_specs)
18166 {
18167 cp_parser_set_decl_spec_type (decl_specs, type,
18168 token,
18169 /*type_definition_p=*/false);
18170 /* Remember that we are handling a decltype in order to
18171 implement the resolution of DR 1510 when the argument
18172 isn't instantiation dependent. */
18173 decl_specs->decltype_p = true;
18174 }
18175 cp_lexer_consume_token (parser->lexer);
18176 return type;
18177 }
18178
18179 /* If the type-specifier was for a built-in type, we're done. */
18180 if (type)
18181 {
18182 /* Record the type. */
18183 if (decl_specs
18184 && (token->keyword != RID_SIGNED
18185 && token->keyword != RID_UNSIGNED
18186 && token->keyword != RID_SHORT
18187 && token->keyword != RID_LONG))
18188 cp_parser_set_decl_spec_type (decl_specs,
18189 type,
18190 token,
18191 /*type_definition_p=*/false);
18192 if (decl_specs)
18193 decl_specs->any_specifiers_p = true;
18194
18195 /* Consume the token. */
18196 cp_lexer_consume_token (parser->lexer);
18197
18198 if (type == error_mark_node)
18199 return error_mark_node;
18200
18201 /* There is no valid C++ program where a non-template type is
18202 followed by a "<". That usually indicates that the user thought
18203 that the type was a template. */
18204 cp_parser_check_for_invalid_template_id (parser, type, none_type,
18205 token->location);
18206
18207 return TYPE_NAME (type);
18208 }
18209
18210 /* The type-specifier must be a user-defined type. */
18211 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
18212 {
18213 bool qualified_p;
18214 bool global_p;
18215 const bool typename_p = (cxx_dialect >= cxx20
18216 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
18217
18218 /* Don't gobble tokens or issue error messages if this is an
18219 optional type-specifier. */
18220 if (flags & CP_PARSER_FLAGS_OPTIONAL)
18221 cp_parser_parse_tentatively (parser);
18222
18223 /* Remember current tentative parsing state -- if we know we need
18224 a type, we can give better diagnostics here. */
18225 bool tent = cp_parser_parsing_tentatively (parser);
18226
18227 token = cp_lexer_peek_token (parser->lexer);
18228
18229 /* Look for the optional `::' operator. */
18230 global_p
18231 = (cp_parser_global_scope_opt (parser,
18232 /*current_scope_valid_p=*/false)
18233 != NULL_TREE);
18234 /* Look for the nested-name specifier. */
18235 qualified_p
18236 = (cp_parser_nested_name_specifier_opt (parser,
18237 /*typename_keyword_p=*/false,
18238 /*check_dependency_p=*/true,
18239 /*type_p=*/false,
18240 /*is_declaration=*/false)
18241 != NULL_TREE);
18242 /* If we have seen a nested-name-specifier, and the next token
18243 is `template', then we are using the template-id production. */
18244 if (parser->scope
18245 && cp_parser_optional_template_keyword (parser))
18246 {
18247 /* Look for the template-id. */
18248 type = cp_parser_template_id (parser,
18249 /*template_keyword_p=*/true,
18250 /*check_dependency_p=*/true,
18251 none_type,
18252 /*is_declaration=*/false);
18253 /* If the template-id did not name a type, we are out of
18254 luck. */
18255 if (TREE_CODE (type) != TYPE_DECL)
18256 {
18257 /* ...unless we pretend we have seen 'typename'. */
18258 if (typename_p)
18259 type = cp_parser_make_typename_type (parser, type,
18260 token->location);
18261 else
18262 {
18263 cp_parser_error (parser, "expected template-id for type");
18264 type = error_mark_node;
18265 }
18266 }
18267 }
18268 /* DR 1812: A < following a qualified-id in a typename-specifier
18269 could safely be assumed to begin a template argument list, so
18270 the template keyword should be optional. */
18271 else if (parser->scope
18272 && qualified_p
18273 && typename_p
18274 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
18275 {
18276 cp_parser_parse_tentatively (parser);
18277
18278 type = cp_parser_template_id (parser,
18279 /*template_keyword_p=*/true,
18280 /*check_dependency_p=*/true,
18281 none_type,
18282 /*is_declaration=*/false);
18283 /* This is handled below, so back off. */
18284 if (type && concept_check_p (type))
18285 cp_parser_simulate_error (parser);
18286
18287 if (!cp_parser_parse_definitely (parser))
18288 type = NULL_TREE;
18289 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
18290 type = make_typename_type (parser->scope, type, typename_type,
18291 /*complain=*/tf_error);
18292 else if (TREE_CODE (type) != TYPE_DECL)
18293 type = NULL_TREE;
18294 }
18295
18296 /* Otherwise, look for a type-name. */
18297 if (!type)
18298 {
18299 if (cxx_dialect >= cxx17)
18300 cp_parser_parse_tentatively (parser);
18301
18302 type = cp_parser_type_name (parser, (qualified_p && typename_p));
18303
18304 if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
18305 type = NULL_TREE;
18306 }
18307
18308 if (!type && flag_concepts && decl_specs)
18309 {
18310 /* Try for a type-constraint with template arguments. We check
18311 decl_specs here to avoid trying this for a functional cast. */
18312
18313 cp_parser_parse_tentatively (parser);
18314
18315 type = cp_parser_template_id (parser,
18316 /*template_keyword_p=*/false,
18317 /*check_dependency_p=*/true,
18318 none_type,
18319 /*is_declaration=*/false);
18320 if (type && concept_check_p (type))
18321 {
18322 location_t loc = EXPR_LOCATION (type);
18323 type = cp_parser_placeholder_type_specifier (parser, loc,
18324 type, tent);
18325 if (tent && type == error_mark_node)
18326 /* Perhaps it's a concept-check expression. */
18327 cp_parser_simulate_error (parser);
18328 }
18329 else
18330 cp_parser_simulate_error (parser);
18331
18332 if (!cp_parser_parse_definitely (parser))
18333 type = NULL_TREE;
18334 }
18335
18336 if (!type && cxx_dialect >= cxx17)
18337 {
18338 /* Try class template argument deduction or type-constraint without
18339 template arguments. */
18340 tree name = cp_parser_identifier (parser);
18341 if (name && TREE_CODE (name) == IDENTIFIER_NODE
18342 && parser->scope != error_mark_node)
18343 {
18344 location_t loc
18345 = cp_lexer_previous_token (parser->lexer)->location;
18346 tree tmpl = cp_parser_lookup_name (parser, name,
18347 none_type,
18348 /*is_template=*/false,
18349 /*is_namespace=*/false,
18350 /*check_dependency=*/true,
18351 /*ambiguous_decls=*/NULL,
18352 token->location);
18353 if (tmpl && tmpl != error_mark_node
18354 && ctad_template_p (tmpl))
18355 type = make_template_placeholder (tmpl);
18356 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
18357 type = cp_parser_placeholder_type_specifier (parser, loc,
18358 tmpl, tent);
18359 else
18360 {
18361 type = error_mark_node;
18362 if (!cp_parser_simulate_error (parser))
18363 cp_parser_name_lookup_error (parser, name, tmpl,
18364 NLE_TYPE, token->location);
18365 }
18366 }
18367 else
18368 type = error_mark_node;
18369 }
18370
18371 /* If it didn't work out, we don't have a TYPE. */
18372 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
18373 && !cp_parser_parse_definitely (parser))
18374 type = NULL_TREE;
18375
18376 /* Keep track of all name-lookups performed in class scopes. */
18377 if (type
18378 && !global_p
18379 && !qualified_p
18380 && TREE_CODE (type) == TYPE_DECL
18381 && identifier_p (DECL_NAME (type)))
18382 maybe_note_name_used_in_class (DECL_NAME (type), type);
18383
18384 if (type && decl_specs)
18385 cp_parser_set_decl_spec_type (decl_specs, type,
18386 token,
18387 /*type_definition_p=*/false);
18388 }
18389
18390 /* If we didn't get a type-name, issue an error message. */
18391 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
18392 {
18393 cp_parser_error (parser, "expected type-name");
18394 return error_mark_node;
18395 }
18396
18397 if (type && type != error_mark_node)
18398 {
18399 /* See if TYPE is an Objective-C type, and if so, parse and
18400 accept any protocol references following it. Do this before
18401 the cp_parser_check_for_invalid_template_id() call, because
18402 Objective-C types can be followed by '<...>' which would
18403 enclose protocol names rather than template arguments, and so
18404 everything is fine. */
18405 if (c_dialect_objc () && !parser->scope
18406 && (objc_is_id (type) || objc_is_class_name (type)))
18407 {
18408 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18409 tree qual_type = objc_get_protocol_qualified_type (type, protos);
18410
18411 /* Clobber the "unqualified" type previously entered into
18412 DECL_SPECS with the new, improved protocol-qualified version. */
18413 if (decl_specs)
18414 decl_specs->type = qual_type;
18415
18416 return qual_type;
18417 }
18418
18419 /* There is no valid C++ program where a non-template type is
18420 followed by a "<". That usually indicates that the user
18421 thought that the type was a template. */
18422 cp_parser_check_for_invalid_template_id (parser, type,
18423 none_type,
18424 token->location);
18425 }
18426
18427 return type;
18428 }
18429
18430 /* Parse the remainder of a placholder-type-specifier.
18431
18432 placeholder-type-specifier:
18433 type-constraint_opt auto
18434 type-constraint_opt decltype(auto)
18435
18436 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
18437 and passed as TMPL. This function converts TMPL to an actual type-constraint,
18438 parses the placeholder type, and performs some contextual syntactic analysis.
18439
18440 LOC provides the location of the template name.
18441
18442 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
18443 don't give an error if TMPL isn't a valid type-constraint, as the template-id
18444 might actually be a concept-check,
18445
18446 Note that the Concepts TS allows the auto or decltype(auto) to be
18447 omitted in a constrained-type-specifier. */
18448
18449 tree
18450 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
18451 tree tmpl, bool tentative)
18452 {
18453 if (tmpl == error_mark_node)
18454 return error_mark_node;
18455
18456 tree orig_tmpl = tmpl;
18457
18458 /* Get the arguments as written for subsequent analysis. */
18459 tree args = NULL_TREE;
18460 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
18461 {
18462 args = TREE_OPERAND (tmpl, 1);
18463 tmpl = TREE_OPERAND (tmpl, 0);
18464 }
18465 if (args == NULL_TREE)
18466 /* A concept-name with no arguments can't be an expression. */
18467 tentative = false;
18468
18469 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
18470
18471 /* Get the concept and prototype parameter for the constraint. */
18472 tree_pair info = finish_type_constraints (tmpl, args, complain);
18473 tree con = info.first;
18474 tree proto = info.second;
18475 if (con == error_mark_node)
18476 return error_mark_node;
18477
18478 /* As per the standard, require auto or decltype(auto), except in some
18479 cases (template parameter lists, -fconcepts-ts enabled). */
18480 cp_token *placeholder = NULL, *close_paren = NULL;
18481 if (cxx_dialect >= cxx20)
18482 {
18483 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
18484 placeholder = cp_lexer_consume_token (parser->lexer);
18485 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
18486 {
18487 placeholder = cp_lexer_consume_token (parser->lexer);
18488 matching_parens parens;
18489 parens.require_open (parser);
18490 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
18491 close_paren = parens.require_close (parser);
18492 }
18493 }
18494
18495 /* A type constraint constrains a contextually determined type or type
18496 parameter pack. However, the Concepts TS does allow concepts
18497 to introduce non-type and template template parameters. */
18498 if (TREE_CODE (proto) != TYPE_DECL)
18499 {
18500 if (!flag_concepts_ts
18501 || !processing_template_parmlist)
18502 {
18503 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
18504 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
18505 return error_mark_node;
18506 }
18507 }
18508
18509 /* In a template parameter list, a type-parameter can be introduced
18510 by type-constraints alone. */
18511 if (processing_template_parmlist && !placeholder)
18512 return build_constrained_parameter (con, proto, args);
18513
18514 /* Diagnose issues placeholder issues. */
18515 if (!flag_concepts_ts
18516 && !parser->in_result_type_constraint_p
18517 && !placeholder)
18518 {
18519 if (tentative)
18520 /* Perhaps it's a concept-check expression (c++/91073). */
18521 return error_mark_node;
18522
18523 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
18524 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
18525 error_at (input_location,
18526 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
18527 /* Fall through. This is an error of omission. */
18528 }
18529 else if (parser->in_result_type_constraint_p && placeholder)
18530 {
18531 /* A trailing return type only allows type-constraints. */
18532 error_at (input_location,
18533 "unexpected placeholder in constrained result type");
18534 }
18535
18536 /* In a parameter-declaration-clause, a placeholder-type-specifier
18537 results in an invented template parameter. */
18538 if (parser->auto_is_implicit_function_template_parm_p)
18539 {
18540 if (close_paren)
18541 {
18542 location_t loc = make_location (placeholder->location,
18543 placeholder->location,
18544 close_paren->location);
18545 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
18546 return error_mark_node;
18547 }
18548 tree parm = build_constrained_parameter (con, proto, args);
18549 return synthesize_implicit_template_parm (parser, parm);
18550 }
18551
18552 /* Determine if the type should be deduced using template argument
18553 deduction or decltype deduction. Note that the latter is always
18554 used for type-constraints in trailing return types. */
18555 bool decltype_p = placeholder
18556 ? placeholder->keyword == RID_DECLTYPE
18557 : parser->in_result_type_constraint_p;
18558
18559 /* Otherwise, this is the type of a variable or return type. */
18560 if (decltype_p)
18561 return make_constrained_decltype_auto (con, args);
18562 else
18563 return make_constrained_auto (con, args);
18564 }
18565
18566 /* Parse a type-name.
18567
18568 type-name:
18569 class-name
18570 enum-name
18571 typedef-name
18572 simple-template-id [in c++0x]
18573
18574 enum-name:
18575 identifier
18576
18577 typedef-name:
18578 identifier
18579
18580 Concepts:
18581
18582 type-name:
18583 concept-name
18584 partial-concept-id
18585
18586 concept-name:
18587 identifier
18588
18589 Returns a TYPE_DECL for the type. */
18590
18591 static tree
18592 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
18593 {
18594 tree type_decl;
18595
18596 /* We can't know yet whether it is a class-name or not. */
18597 cp_parser_parse_tentatively (parser);
18598 /* Try a class-name. */
18599 type_decl = cp_parser_class_name (parser,
18600 typename_keyword_p,
18601 /*template_keyword_p=*/false,
18602 none_type,
18603 /*check_dependency_p=*/true,
18604 /*class_head_p=*/false,
18605 /*is_declaration=*/false);
18606 /* If it's not a class-name, keep looking. */
18607 if (!cp_parser_parse_definitely (parser))
18608 {
18609 if (cxx_dialect < cxx11)
18610 /* It must be a typedef-name or an enum-name. */
18611 return cp_parser_nonclass_name (parser);
18612
18613 cp_parser_parse_tentatively (parser);
18614 /* It is either a simple-template-id representing an
18615 instantiation of an alias template... */
18616 type_decl = cp_parser_template_id (parser,
18617 /*template_keyword_p=*/false,
18618 /*check_dependency_p=*/true,
18619 none_type,
18620 /*is_declaration=*/false);
18621 /* Note that this must be an instantiation of an alias template
18622 because [temp.names]/6 says:
18623
18624 A template-id that names an alias template specialization
18625 is a type-name.
18626
18627 Whereas [temp.names]/7 says:
18628
18629 A simple-template-id that names a class template
18630 specialization is a class-name.
18631
18632 With concepts, this could also be a partial-concept-id that
18633 declares a non-type template parameter. */
18634 if (type_decl != NULL_TREE
18635 && TREE_CODE (type_decl) == TYPE_DECL
18636 && TYPE_DECL_ALIAS_P (type_decl))
18637 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
18638 else
18639 cp_parser_simulate_error (parser);
18640
18641 if (!cp_parser_parse_definitely (parser))
18642 /* ... Or a typedef-name or an enum-name. */
18643 return cp_parser_nonclass_name (parser);
18644 }
18645
18646 return type_decl;
18647 }
18648
18649 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18650 or a concept-name.
18651
18652 enum-name:
18653 identifier
18654
18655 typedef-name:
18656 identifier
18657
18658 concept-name:
18659 identifier
18660
18661 Returns a TYPE_DECL for the type. */
18662
18663 static tree
18664 cp_parser_nonclass_name (cp_parser* parser)
18665 {
18666 tree type_decl;
18667 tree identifier;
18668
18669 cp_token *token = cp_lexer_peek_token (parser->lexer);
18670 identifier = cp_parser_identifier (parser);
18671 if (identifier == error_mark_node)
18672 return error_mark_node;
18673
18674 /* Look up the type-name. */
18675 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18676
18677 type_decl = strip_using_decl (type_decl);
18678
18679 if (TREE_CODE (type_decl) != TYPE_DECL
18680 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18681 {
18682 /* See if this is an Objective-C type. */
18683 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18684 tree type = objc_get_protocol_qualified_type (identifier, protos);
18685 if (type)
18686 type_decl = TYPE_NAME (type);
18687 }
18688
18689 /* Issue an error if we did not find a type-name. */
18690 if (TREE_CODE (type_decl) != TYPE_DECL
18691 /* In Objective-C, we have the complication that class names are
18692 normally type names and start declarations (eg, the
18693 "NSObject" in "NSObject *object;"), but can be used in an
18694 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18695 is an expression. So, a classname followed by a dot is not a
18696 valid type-name. */
18697 || (objc_is_class_name (TREE_TYPE (type_decl))
18698 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18699 {
18700 if (!cp_parser_simulate_error (parser))
18701 cp_parser_name_lookup_error (parser, identifier, type_decl,
18702 NLE_TYPE, token->location);
18703 return error_mark_node;
18704 }
18705 /* Remember that the name was used in the definition of the
18706 current class so that we can check later to see if the
18707 meaning would have been different after the class was
18708 entirely defined. */
18709 else if (type_decl != error_mark_node
18710 && !parser->scope)
18711 maybe_note_name_used_in_class (identifier, type_decl);
18712
18713 return type_decl;
18714 }
18715
18716 /* Parse an elaborated-type-specifier. Note that the grammar given
18717 here incorporates the resolution to DR68.
18718
18719 elaborated-type-specifier:
18720 class-key :: [opt] nested-name-specifier [opt] identifier
18721 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18722 enum-key :: [opt] nested-name-specifier [opt] identifier
18723 typename :: [opt] nested-name-specifier identifier
18724 typename :: [opt] nested-name-specifier template [opt]
18725 template-id
18726
18727 GNU extension:
18728
18729 elaborated-type-specifier:
18730 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18731 class-key attributes :: [opt] nested-name-specifier [opt]
18732 template [opt] template-id
18733 enum attributes :: [opt] nested-name-specifier [opt] identifier
18734
18735 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18736 declared `friend'. If IS_DECLARATION is TRUE, then this
18737 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18738 something is being declared.
18739
18740 Returns the TYPE specified. */
18741
18742 static tree
18743 cp_parser_elaborated_type_specifier (cp_parser* parser,
18744 bool is_friend,
18745 bool is_declaration)
18746 {
18747 enum tag_types tag_type;
18748 tree identifier;
18749 tree type = NULL_TREE;
18750 tree attributes = NULL_TREE;
18751 tree globalscope;
18752 cp_token *token = NULL;
18753
18754 /* For class and enum types the location of the class-key or enum-key. */
18755 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
18756 /* For a scoped enum, the 'class' or 'struct' keyword id. */
18757 rid scoped_key = RID_MAX;
18758
18759 /* See if we're looking at the `enum' keyword. */
18760 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18761 {
18762 /* Consume the `enum' token. */
18763 cp_lexer_consume_token (parser->lexer);
18764 /* Remember that it's an enumeration type. */
18765 tag_type = enum_type;
18766 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18767 enums) is used here. */
18768 cp_token *token = cp_lexer_peek_token (parser->lexer);
18769 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
18770 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
18771 {
18772 location_t loc = token->location;
18773 gcc_rich_location richloc (loc);
18774 richloc.add_range (input_location);
18775 richloc.add_fixit_remove ();
18776 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18777 "a scoped enum must not use the %qD keyword",
18778 token->u.value);
18779 /* Consume the `struct' or `class' and parse it anyway. */
18780 cp_lexer_consume_token (parser->lexer);
18781 /* Create a combined location for the whole scoped-enum-key. */
18782 key_loc = make_location (key_loc, key_loc, loc);
18783 }
18784 else
18785 scoped_key = RID_MAX;
18786
18787 /* Parse the attributes. */
18788 attributes = cp_parser_attributes_opt (parser);
18789 }
18790 /* Or, it might be `typename'. */
18791 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18792 RID_TYPENAME))
18793 {
18794 /* Consume the `typename' token. */
18795 cp_lexer_consume_token (parser->lexer);
18796 /* Remember that it's a `typename' type. */
18797 tag_type = typename_type;
18798 }
18799 /* Otherwise it must be a class-key. */
18800 else
18801 {
18802 key_loc = cp_lexer_peek_token (parser->lexer)->location;
18803 tag_type = cp_parser_class_key (parser);
18804 if (tag_type == none_type)
18805 return error_mark_node;
18806 /* Parse the attributes. */
18807 attributes = cp_parser_attributes_opt (parser);
18808 }
18809
18810 /* Look for the `::' operator. */
18811 globalscope = cp_parser_global_scope_opt (parser,
18812 /*current_scope_valid_p=*/false);
18813 /* Look for the nested-name-specifier. */
18814 tree nested_name_specifier;
18815 if (tag_type == typename_type && !globalscope)
18816 {
18817 nested_name_specifier
18818 = cp_parser_nested_name_specifier (parser,
18819 /*typename_keyword_p=*/true,
18820 /*check_dependency_p=*/true,
18821 /*type_p=*/true,
18822 is_declaration);
18823 if (!nested_name_specifier)
18824 return error_mark_node;
18825 }
18826 else
18827 /* Even though `typename' is not present, the proposed resolution
18828 to Core Issue 180 says that in `class A<T>::B', `B' should be
18829 considered a type-name, even if `A<T>' is dependent. */
18830 nested_name_specifier
18831 = cp_parser_nested_name_specifier_opt (parser,
18832 /*typename_keyword_p=*/true,
18833 /*check_dependency_p=*/true,
18834 /*type_p=*/true,
18835 is_declaration);
18836 /* For everything but enumeration types, consider a template-id.
18837 For an enumeration type, consider only a plain identifier. */
18838 if (tag_type != enum_type)
18839 {
18840 bool template_p = false;
18841 tree decl;
18842
18843 /* Allow the `template' keyword. */
18844 template_p = cp_parser_optional_template_keyword (parser);
18845 /* If we didn't see `template', we don't know if there's a
18846 template-id or not. */
18847 if (!template_p)
18848 cp_parser_parse_tentatively (parser);
18849 /* The `template' keyword must follow a nested-name-specifier. */
18850 else if (!nested_name_specifier && !globalscope)
18851 {
18852 cp_parser_error (parser, "%<template%> must follow a nested-"
18853 "name-specifier");
18854 return error_mark_node;
18855 }
18856
18857 /* Parse the template-id. */
18858 token = cp_lexer_peek_token (parser->lexer);
18859 decl = cp_parser_template_id (parser, template_p,
18860 /*check_dependency_p=*/true,
18861 tag_type,
18862 is_declaration);
18863 /* If we didn't find a template-id, look for an ordinary
18864 identifier. */
18865 if (!template_p && !cp_parser_parse_definitely (parser))
18866 ;
18867 /* We can get here when cp_parser_template_id, called by
18868 cp_parser_class_name with tag_type == none_type, succeeds
18869 and caches a BASELINK. Then, when called again here,
18870 instead of failing and returning an error_mark_node
18871 returns it (see template/typename17.C in C++11).
18872 ??? Could we diagnose this earlier? */
18873 else if (tag_type == typename_type && BASELINK_P (decl))
18874 {
18875 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18876 type = error_mark_node;
18877 }
18878 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18879 in effect, then we must assume that, upon instantiation, the
18880 template will correspond to a class. */
18881 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18882 && tag_type == typename_type)
18883 type = make_typename_type (parser->scope, decl,
18884 typename_type,
18885 /*complain=*/tf_error);
18886 /* If the `typename' keyword is in effect and DECL is not a type
18887 decl, then type is non existent. */
18888 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18889 ;
18890 else if (TREE_CODE (decl) == TYPE_DECL)
18891 {
18892 type = check_elaborated_type_specifier (tag_type, decl,
18893 /*allow_template_p=*/true);
18894
18895 /* If the next token is a semicolon, this must be a specialization,
18896 instantiation, or friend declaration. Check the scope while we
18897 still know whether or not we had a nested-name-specifier. */
18898 if (type != error_mark_node
18899 && !nested_name_specifier && !is_friend
18900 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18901 check_unqualified_spec_or_inst (type, token->location);
18902 }
18903 else if (decl == error_mark_node)
18904 type = error_mark_node;
18905 }
18906
18907 if (!type)
18908 {
18909 token = cp_lexer_peek_token (parser->lexer);
18910 identifier = cp_parser_identifier (parser);
18911
18912 if (identifier == error_mark_node)
18913 {
18914 parser->scope = NULL_TREE;
18915 return error_mark_node;
18916 }
18917
18918 /* For a `typename', we needn't call xref_tag. */
18919 if (tag_type == typename_type
18920 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18921 return cp_parser_make_typename_type (parser, identifier,
18922 token->location);
18923
18924 /* Template parameter lists apply only if we are not within a
18925 function parameter list. */
18926 bool template_parm_lists_apply
18927 = parser->num_template_parameter_lists;
18928 if (template_parm_lists_apply)
18929 for (cp_binding_level *s = current_binding_level;
18930 s && s->kind != sk_template_parms;
18931 s = s->level_chain)
18932 if (s->kind == sk_function_parms)
18933 template_parm_lists_apply = false;
18934
18935 /* Look up a qualified name in the usual way. */
18936 if (parser->scope)
18937 {
18938 tree decl;
18939 tree ambiguous_decls;
18940
18941 decl = cp_parser_lookup_name (parser, identifier,
18942 tag_type,
18943 /*is_template=*/false,
18944 /*is_namespace=*/false,
18945 /*check_dependency=*/true,
18946 &ambiguous_decls,
18947 token->location);
18948
18949 /* If the lookup was ambiguous, an error will already have been
18950 issued. */
18951 if (ambiguous_decls)
18952 return error_mark_node;
18953
18954 /* If we are parsing friend declaration, DECL may be a
18955 TEMPLATE_DECL tree node here. However, we need to check
18956 whether this TEMPLATE_DECL results in valid code. Consider
18957 the following example:
18958
18959 namespace N {
18960 template <class T> class C {};
18961 }
18962 class X {
18963 template <class T> friend class N::C; // #1, valid code
18964 };
18965 template <class T> class Y {
18966 friend class N::C; // #2, invalid code
18967 };
18968
18969 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18970 name lookup of `N::C'. We see that friend declaration must
18971 be template for the code to be valid. Note that
18972 processing_template_decl does not work here since it is
18973 always 1 for the above two cases. */
18974
18975 decl = (cp_parser_maybe_treat_template_as_class
18976 (decl, /*tag_name_p=*/is_friend
18977 && template_parm_lists_apply));
18978
18979 if (TREE_CODE (decl) != TYPE_DECL)
18980 {
18981 cp_parser_diagnose_invalid_type_name (parser,
18982 identifier,
18983 token->location);
18984 return error_mark_node;
18985 }
18986
18987 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18988 {
18989 bool allow_template = (template_parm_lists_apply
18990 || DECL_SELF_REFERENCE_P (decl));
18991 type = check_elaborated_type_specifier (tag_type, decl,
18992 allow_template);
18993
18994 if (type == error_mark_node)
18995 return error_mark_node;
18996 }
18997
18998 /* Forward declarations of nested types, such as
18999
19000 class C1::C2;
19001 class C1::C2::C3;
19002
19003 are invalid unless all components preceding the final '::'
19004 are complete. If all enclosing types are complete, these
19005 declarations become merely pointless.
19006
19007 Invalid forward declarations of nested types are errors
19008 caught elsewhere in parsing. Those that are pointless arrive
19009 here. */
19010
19011 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19012 && !is_friend && is_declaration
19013 && !processing_explicit_instantiation)
19014 warning (0, "declaration %qD does not declare anything", decl);
19015
19016 type = TREE_TYPE (decl);
19017 }
19018 else
19019 {
19020 /* An elaborated-type-specifier sometimes introduces a new type and
19021 sometimes names an existing type. Normally, the rule is that it
19022 introduces a new type only if there is not an existing type of
19023 the same name already in scope. For example, given:
19024
19025 struct S {};
19026 void f() { struct S s; }
19027
19028 the `struct S' in the body of `f' is the same `struct S' as in
19029 the global scope; the existing definition is used. However, if
19030 there were no global declaration, this would introduce a new
19031 local class named `S'.
19032
19033 An exception to this rule applies to the following code:
19034
19035 namespace N { struct S; }
19036
19037 Here, the elaborated-type-specifier names a new type
19038 unconditionally; even if there is already an `S' in the
19039 containing scope this declaration names a new type.
19040 This exception only applies if the elaborated-type-specifier
19041 forms the complete declaration:
19042
19043 [class.name]
19044
19045 A declaration consisting solely of `class-key identifier ;' is
19046 either a redeclaration of the name in the current scope or a
19047 forward declaration of the identifier as a class name. It
19048 introduces the name into the current scope.
19049
19050 We are in this situation precisely when the next token is a `;'.
19051
19052 An exception to the exception is that a `friend' declaration does
19053 *not* name a new type; i.e., given:
19054
19055 struct S { friend struct T; };
19056
19057 `T' is not a new type in the scope of `S'.
19058
19059 Also, `new struct S' or `sizeof (struct S)' never results in the
19060 definition of a new type; a new type can only be declared in a
19061 declaration context. */
19062
19063 tag_scope ts;
19064 bool template_p;
19065
19066 if (is_friend)
19067 /* Friends have special name lookup rules. */
19068 ts = ts_within_enclosing_non_class;
19069 else if (is_declaration
19070 && cp_lexer_next_token_is (parser->lexer,
19071 CPP_SEMICOLON))
19072 /* This is a `class-key identifier ;' */
19073 ts = ts_current;
19074 else
19075 ts = ts_global;
19076
19077 template_p =
19078 (template_parm_lists_apply
19079 && (cp_parser_next_token_starts_class_definition_p (parser)
19080 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
19081 /* An unqualified name was used to reference this type, so
19082 there were no qualifying templates. */
19083 if (template_parm_lists_apply
19084 && !cp_parser_check_template_parameters (parser,
19085 /*num_templates=*/0,
19086 /*template_id*/false,
19087 token->location,
19088 /*declarator=*/NULL))
19089 return error_mark_node;
19090 type = xref_tag (tag_type, identifier, ts, template_p);
19091 }
19092 }
19093
19094 if (type == error_mark_node)
19095 return error_mark_node;
19096
19097 /* Allow attributes on forward declarations of classes. */
19098 if (attributes)
19099 {
19100 if (TREE_CODE (type) == TYPENAME_TYPE)
19101 warning (OPT_Wattributes,
19102 "attributes ignored on uninstantiated type");
19103 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
19104 && ! processing_explicit_instantiation)
19105 warning (OPT_Wattributes,
19106 "attributes ignored on template instantiation");
19107 else if (is_declaration && cp_parser_declares_only_class_p (parser))
19108 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
19109 else
19110 warning (OPT_Wattributes,
19111 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
19112 }
19113
19114 if (tag_type == enum_type)
19115 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
19116 else
19117 {
19118 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
19119 for alias definition. */
19120 bool decl_class = (is_declaration
19121 && cp_parser_declares_only_class_p (parser));
19122 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
19123 decl_class);
19124
19125 /* Indicate whether this class was declared as a `class' or as a
19126 `struct'. */
19127 if (CLASS_TYPE_P (type) && !currently_open_class (type))
19128 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
19129 }
19130
19131 /* A "<" cannot follow an elaborated type specifier. If that
19132 happens, the user was probably trying to form a template-id. */
19133 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
19134 token->location);
19135
19136 return type;
19137 }
19138
19139 /* Parse an enum-specifier.
19140
19141 enum-specifier:
19142 enum-head { enumerator-list [opt] }
19143 enum-head { enumerator-list , } [C++0x]
19144
19145 enum-head:
19146 enum-key identifier [opt] enum-base [opt]
19147 enum-key nested-name-specifier identifier enum-base [opt]
19148
19149 enum-key:
19150 enum
19151 enum class [C++0x]
19152 enum struct [C++0x]
19153
19154 enum-base: [C++0x]
19155 : type-specifier-seq
19156
19157 opaque-enum-specifier:
19158 enum-key identifier enum-base [opt] ;
19159
19160 GNU Extensions:
19161 enum-key attributes[opt] identifier [opt] enum-base [opt]
19162 { enumerator-list [opt] }attributes[opt]
19163 enum-key attributes[opt] identifier [opt] enum-base [opt]
19164 { enumerator-list, }attributes[opt] [C++0x]
19165
19166 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
19167 if the token stream isn't an enum-specifier after all. */
19168
19169 static tree
19170 cp_parser_enum_specifier (cp_parser* parser)
19171 {
19172 tree identifier;
19173 tree type = NULL_TREE;
19174 tree prev_scope;
19175 tree nested_name_specifier = NULL_TREE;
19176 tree attributes;
19177 bool scoped_enum_p = false;
19178 bool has_underlying_type = false;
19179 bool nested_being_defined = false;
19180 bool new_value_list = false;
19181 bool is_new_type = false;
19182 bool is_unnamed = false;
19183 tree underlying_type = NULL_TREE;
19184 cp_token *type_start_token = NULL;
19185 temp_override<bool> cleanup (parser->colon_corrects_to_scope_p, false);
19186
19187 /* Parse tentatively so that we can back up if we don't find a
19188 enum-specifier. */
19189 cp_parser_parse_tentatively (parser);
19190
19191 /* Caller guarantees that the current token is 'enum', an identifier
19192 possibly follows, and the token after that is an opening brace.
19193 If we don't have an identifier, fabricate an anonymous name for
19194 the enumeration being defined. */
19195 cp_lexer_consume_token (parser->lexer);
19196
19197 /* Parse the "class" or "struct", which indicates a scoped
19198 enumeration type in C++0x. */
19199 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
19200 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
19201 {
19202 if (cxx_dialect < cxx11)
19203 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
19204
19205 /* Consume the `struct' or `class' token. */
19206 cp_lexer_consume_token (parser->lexer);
19207
19208 scoped_enum_p = true;
19209 }
19210
19211 attributes = cp_parser_attributes_opt (parser);
19212
19213 /* Clear the qualification. */
19214 parser->scope = NULL_TREE;
19215 parser->qualifying_scope = NULL_TREE;
19216 parser->object_scope = NULL_TREE;
19217
19218 /* Figure out in what scope the declaration is being placed. */
19219 prev_scope = current_scope ();
19220
19221 type_start_token = cp_lexer_peek_token (parser->lexer);
19222
19223 push_deferring_access_checks (dk_no_check);
19224 nested_name_specifier
19225 = cp_parser_nested_name_specifier_opt (parser,
19226 /*typename_keyword_p=*/true,
19227 /*check_dependency_p=*/false,
19228 /*type_p=*/false,
19229 /*is_declaration=*/false);
19230
19231 if (nested_name_specifier)
19232 {
19233 tree name;
19234
19235 identifier = cp_parser_identifier (parser);
19236 name = cp_parser_lookup_name (parser, identifier,
19237 enum_type,
19238 /*is_template=*/false,
19239 /*is_namespace=*/false,
19240 /*check_dependency=*/true,
19241 /*ambiguous_decls=*/NULL,
19242 input_location);
19243 if (name && name != error_mark_node)
19244 {
19245 type = TREE_TYPE (name);
19246 if (TREE_CODE (type) == TYPENAME_TYPE)
19247 {
19248 /* Are template enums allowed in ISO? */
19249 if (template_parm_scope_p ())
19250 pedwarn (type_start_token->location, OPT_Wpedantic,
19251 "%qD is an enumeration template", name);
19252 /* ignore a typename reference, for it will be solved by name
19253 in start_enum. */
19254 type = NULL_TREE;
19255 }
19256 }
19257 else if (nested_name_specifier == error_mark_node)
19258 /* We already issued an error. */;
19259 else
19260 {
19261 error_at (type_start_token->location,
19262 "%qD does not name an enumeration in %qT",
19263 identifier, nested_name_specifier);
19264 nested_name_specifier = error_mark_node;
19265 }
19266 }
19267 else
19268 {
19269 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19270 identifier = cp_parser_identifier (parser);
19271 else
19272 {
19273 identifier = make_anon_name ();
19274 is_unnamed = true;
19275 if (scoped_enum_p)
19276 error_at (type_start_token->location,
19277 "unnamed scoped enum is not allowed");
19278 }
19279 }
19280 pop_deferring_access_checks ();
19281
19282 /* Check for the `:' that denotes a specified underlying type in C++0x.
19283 Note that a ':' could also indicate a bitfield width, however. */
19284 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19285 {
19286 cp_decl_specifier_seq type_specifiers;
19287
19288 /* Consume the `:'. */
19289 cp_lexer_consume_token (parser->lexer);
19290
19291 /* Parse the type-specifier-seq. */
19292 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
19293 /*is_declaration=*/false,
19294 /*is_trailing_return=*/false,
19295 &type_specifiers);
19296
19297 /* At this point this is surely not elaborated type specifier. */
19298 if (!cp_parser_parse_definitely (parser))
19299 return NULL_TREE;
19300
19301 if (cxx_dialect < cxx11)
19302 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
19303
19304 has_underlying_type = true;
19305
19306 /* If that didn't work, stop. */
19307 if (type_specifiers.type != error_mark_node)
19308 {
19309 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
19310 /*initialized=*/0, NULL);
19311 if (underlying_type == error_mark_node
19312 || check_for_bare_parameter_packs (underlying_type))
19313 underlying_type = NULL_TREE;
19314 }
19315 }
19316
19317 /* Look for the `{' but don't consume it yet. */
19318 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19319 {
19320 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
19321 {
19322 if (has_underlying_type)
19323 cp_parser_commit_to_tentative_parse (parser);
19324 cp_parser_error (parser, "expected %<{%>");
19325 if (has_underlying_type)
19326 return error_mark_node;
19327 }
19328 /* An opaque-enum-specifier must have a ';' here. */
19329 if ((scoped_enum_p || underlying_type)
19330 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19331 {
19332 if (has_underlying_type)
19333 cp_parser_commit_to_tentative_parse (parser);
19334 cp_parser_error (parser, "expected %<;%> or %<{%>");
19335 if (has_underlying_type)
19336 return error_mark_node;
19337 }
19338 }
19339
19340 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
19341 return NULL_TREE;
19342
19343 if (nested_name_specifier)
19344 {
19345 if (CLASS_TYPE_P (nested_name_specifier))
19346 {
19347 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
19348 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
19349 push_scope (nested_name_specifier);
19350 }
19351 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19352 push_nested_namespace (nested_name_specifier);
19353 }
19354
19355 /* Issue an error message if type-definitions are forbidden here. */
19356 if (!cp_parser_check_type_definition (parser))
19357 type = error_mark_node;
19358 else
19359 /* Create the new type. We do this before consuming the opening
19360 brace so the enum will be recorded as being on the line of its
19361 tag (or the 'enum' keyword, if there is no tag). */
19362 type = start_enum (identifier, type, underlying_type,
19363 attributes, scoped_enum_p, &is_new_type);
19364
19365 /* If the next token is not '{' it is an opaque-enum-specifier or an
19366 elaborated-type-specifier. */
19367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19368 {
19369 timevar_push (TV_PARSE_ENUM);
19370 if (nested_name_specifier
19371 && nested_name_specifier != error_mark_node)
19372 {
19373 /* The following catches invalid code such as:
19374 enum class S<int>::E { A, B, C }; */
19375 if (!processing_specialization
19376 && CLASS_TYPE_P (nested_name_specifier)
19377 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
19378 error_at (type_start_token->location, "cannot add an enumerator "
19379 "list to a template instantiation");
19380
19381 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
19382 {
19383 error_at (type_start_token->location,
19384 "%<%T::%E%> has not been declared",
19385 TYPE_CONTEXT (nested_name_specifier),
19386 nested_name_specifier);
19387 type = error_mark_node;
19388 }
19389 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
19390 && !CLASS_TYPE_P (nested_name_specifier))
19391 {
19392 error_at (type_start_token->location, "nested name specifier "
19393 "%qT for enum declaration does not name a class "
19394 "or namespace", nested_name_specifier);
19395 type = error_mark_node;
19396 }
19397 /* If that scope does not contain the scope in which the
19398 class was originally declared, the program is invalid. */
19399 else if (prev_scope && !is_ancestor (prev_scope,
19400 nested_name_specifier))
19401 {
19402 if (at_namespace_scope_p ())
19403 error_at (type_start_token->location,
19404 "declaration of %qD in namespace %qD which does not "
19405 "enclose %qD",
19406 type, prev_scope, nested_name_specifier);
19407 else
19408 error_at (type_start_token->location,
19409 "declaration of %qD in %qD which does not "
19410 "enclose %qD",
19411 type, prev_scope, nested_name_specifier);
19412 type = error_mark_node;
19413 }
19414 /* If that scope is the scope where the declaration is being placed
19415 the program is invalid. */
19416 else if (CLASS_TYPE_P (nested_name_specifier)
19417 && CLASS_TYPE_P (prev_scope)
19418 && same_type_p (nested_name_specifier, prev_scope))
19419 {
19420 permerror (type_start_token->location,
19421 "extra qualification not allowed");
19422 nested_name_specifier = NULL_TREE;
19423 }
19424 }
19425
19426 if (scoped_enum_p)
19427 begin_scope (sk_scoped_enum, type);
19428
19429 /* Consume the opening brace. */
19430 matching_braces braces;
19431 braces.consume_open (parser);
19432
19433 if (type == error_mark_node)
19434 ; /* Nothing to add */
19435 else if (OPAQUE_ENUM_P (type)
19436 || (cxx_dialect > cxx98 && processing_specialization))
19437 {
19438 new_value_list = true;
19439 SET_OPAQUE_ENUM_P (type, false);
19440 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
19441 }
19442 else
19443 {
19444 error_at (type_start_token->location,
19445 "multiple definition of %q#T", type);
19446 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
19447 "previous definition here");
19448 type = error_mark_node;
19449 }
19450
19451 if (type == error_mark_node)
19452 cp_parser_skip_to_end_of_block_or_statement (parser);
19453 /* If the next token is not '}', then there are some enumerators. */
19454 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19455 {
19456 if (is_unnamed && !scoped_enum_p)
19457 pedwarn (type_start_token->location, OPT_Wpedantic,
19458 "ISO C++ forbids empty unnamed enum");
19459 }
19460 else
19461 {
19462 /* We've seen a '{' so we know we're in an enum-specifier.
19463 Commit to any tentative parse to get syntax errors. */
19464 cp_parser_commit_to_tentative_parse (parser);
19465 cp_parser_enumerator_list (parser, type);
19466 }
19467
19468 /* Consume the final '}'. */
19469 braces.require_close (parser);
19470
19471 if (scoped_enum_p)
19472 finish_scope ();
19473 timevar_pop (TV_PARSE_ENUM);
19474 }
19475 else
19476 {
19477 /* If a ';' follows, then it is an opaque-enum-specifier
19478 and additional restrictions apply. */
19479 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19480 {
19481 if (is_unnamed)
19482 error_at (type_start_token->location,
19483 "opaque-enum-specifier without name");
19484 else if (nested_name_specifier)
19485 error_at (type_start_token->location,
19486 "opaque-enum-specifier must use a simple identifier");
19487 }
19488 }
19489
19490 /* Look for trailing attributes to apply to this enumeration, and
19491 apply them if appropriate. */
19492 if (cp_parser_allow_gnu_extensions_p (parser))
19493 {
19494 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
19495 cplus_decl_attributes (&type,
19496 trailing_attr,
19497 (int) ATTR_FLAG_TYPE_IN_PLACE);
19498 }
19499
19500 /* Finish up the enumeration. */
19501 if (type != error_mark_node)
19502 {
19503 if (new_value_list)
19504 finish_enum_value_list (type);
19505 if (is_new_type)
19506 finish_enum (type);
19507 }
19508
19509 if (nested_name_specifier)
19510 {
19511 if (CLASS_TYPE_P (nested_name_specifier))
19512 {
19513 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
19514 pop_scope (nested_name_specifier);
19515 }
19516 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19517 pop_nested_namespace (nested_name_specifier);
19518 }
19519 return type;
19520 }
19521
19522 /* Parse an enumerator-list. The enumerators all have the indicated
19523 TYPE.
19524
19525 enumerator-list:
19526 enumerator-definition
19527 enumerator-list , enumerator-definition */
19528
19529 static void
19530 cp_parser_enumerator_list (cp_parser* parser, tree type)
19531 {
19532 while (true)
19533 {
19534 /* Parse an enumerator-definition. */
19535 cp_parser_enumerator_definition (parser, type);
19536
19537 /* If the next token is not a ',', we've reached the end of
19538 the list. */
19539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19540 break;
19541 /* Otherwise, consume the `,' and keep going. */
19542 cp_lexer_consume_token (parser->lexer);
19543 /* If the next token is a `}', there is a trailing comma. */
19544 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19545 {
19546 if (cxx_dialect < cxx11)
19547 pedwarn (input_location, OPT_Wpedantic,
19548 "comma at end of enumerator list");
19549 break;
19550 }
19551 }
19552 }
19553
19554 /* Parse an enumerator-definition. The enumerator has the indicated
19555 TYPE.
19556
19557 enumerator-definition:
19558 enumerator
19559 enumerator = constant-expression
19560
19561 enumerator:
19562 identifier
19563
19564 GNU Extensions:
19565
19566 enumerator-definition:
19567 enumerator attributes [opt]
19568 enumerator attributes [opt] = constant-expression */
19569
19570 static void
19571 cp_parser_enumerator_definition (cp_parser* parser, tree type)
19572 {
19573 tree identifier;
19574 tree value;
19575 location_t loc;
19576
19577 /* Save the input location because we are interested in the location
19578 of the identifier and not the location of the explicit value. */
19579 loc = cp_lexer_peek_token (parser->lexer)->location;
19580
19581 /* Look for the identifier. */
19582 identifier = cp_parser_identifier (parser);
19583 if (identifier == error_mark_node)
19584 return;
19585
19586 /* Parse any specified attributes. */
19587 tree attrs = cp_parser_attributes_opt (parser);
19588
19589 /* If the next token is an '=', then there is an explicit value. */
19590 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19591 {
19592 /* Consume the `=' token. */
19593 cp_lexer_consume_token (parser->lexer);
19594 /* Parse the value. */
19595 value = cp_parser_constant_expression (parser);
19596 }
19597 else
19598 value = NULL_TREE;
19599
19600 /* If we are processing a template, make sure the initializer of the
19601 enumerator doesn't contain any bare template parameter pack. */
19602 if (check_for_bare_parameter_packs (value))
19603 value = error_mark_node;
19604
19605 /* Create the enumerator. */
19606 build_enumerator (identifier, value, type, attrs, loc);
19607 }
19608
19609 /* Parse a namespace-name.
19610
19611 namespace-name:
19612 original-namespace-name
19613 namespace-alias
19614
19615 Returns the NAMESPACE_DECL for the namespace. */
19616
19617 static tree
19618 cp_parser_namespace_name (cp_parser* parser)
19619 {
19620 tree identifier;
19621 tree namespace_decl;
19622
19623 cp_token *token = cp_lexer_peek_token (parser->lexer);
19624
19625 /* Get the name of the namespace. */
19626 identifier = cp_parser_identifier (parser);
19627 if (identifier == error_mark_node)
19628 return error_mark_node;
19629
19630 /* Look up the identifier in the currently active scope. Look only
19631 for namespaces, due to:
19632
19633 [basic.lookup.udir]
19634
19635 When looking up a namespace-name in a using-directive or alias
19636 definition, only namespace names are considered.
19637
19638 And:
19639
19640 [basic.lookup.qual]
19641
19642 During the lookup of a name preceding the :: scope resolution
19643 operator, object, function, and enumerator names are ignored.
19644
19645 (Note that cp_parser_qualifying_entity only calls this
19646 function if the token after the name is the scope resolution
19647 operator.) */
19648 namespace_decl = cp_parser_lookup_name (parser, identifier,
19649 none_type,
19650 /*is_template=*/false,
19651 /*is_namespace=*/true,
19652 /*check_dependency=*/true,
19653 /*ambiguous_decls=*/NULL,
19654 token->location);
19655 /* If it's not a namespace, issue an error. */
19656 if (namespace_decl == error_mark_node
19657 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19658 {
19659 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19660 {
19661 auto_diagnostic_group d;
19662 name_hint hint;
19663 if (namespace_decl == error_mark_node
19664 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19665 hint = suggest_alternative_in_explicit_scope (token->location,
19666 identifier,
19667 parser->scope);
19668 if (const char *suggestion = hint.suggestion ())
19669 {
19670 gcc_rich_location richloc (token->location);
19671 richloc.add_fixit_replace (suggestion);
19672 error_at (&richloc,
19673 "%qD is not a namespace-name; did you mean %qs?",
19674 identifier, suggestion);
19675 }
19676 else
19677 error_at (token->location, "%qD is not a namespace-name",
19678 identifier);
19679 }
19680 else
19681 cp_parser_error (parser, "expected namespace-name");
19682 namespace_decl = error_mark_node;
19683 }
19684
19685 return namespace_decl;
19686 }
19687
19688 /* Parse a namespace-definition.
19689
19690 namespace-definition:
19691 named-namespace-definition
19692 unnamed-namespace-definition
19693
19694 named-namespace-definition:
19695 original-namespace-definition
19696 extension-namespace-definition
19697
19698 original-namespace-definition:
19699 namespace identifier { namespace-body }
19700
19701 extension-namespace-definition:
19702 namespace original-namespace-name { namespace-body }
19703
19704 unnamed-namespace-definition:
19705 namespace { namespace-body } */
19706
19707 static void
19708 cp_parser_namespace_definition (cp_parser* parser)
19709 {
19710 tree identifier;
19711 int nested_definition_count = 0;
19712
19713 cp_ensure_no_omp_declare_simd (parser);
19714 cp_ensure_no_oacc_routine (parser);
19715
19716 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19717 const bool topmost_inline_p = is_inline;
19718
19719 if (is_inline)
19720 {
19721 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19722 cp_lexer_consume_token (parser->lexer);
19723 }
19724
19725 /* Look for the `namespace' keyword. */
19726 cp_token* token
19727 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19728
19729 /* Parse any specified attributes before the identifier. */
19730 tree attribs = cp_parser_attributes_opt (parser);
19731
19732 for (;;)
19733 {
19734 identifier = NULL_TREE;
19735
19736 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19737 RID_INLINE);
19738 if (nested_inline_p && nested_definition_count != 0)
19739 {
19740 if (cxx_dialect < cxx20)
19741 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19742 OPT_Wpedantic, "nested inline namespace definitions only "
19743 "available with %<-std=c++20%> or %<-std=gnu++20%>");
19744 cp_lexer_consume_token (parser->lexer);
19745 }
19746
19747 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19748 {
19749 identifier = cp_parser_identifier (parser);
19750
19751 if (cp_next_tokens_can_be_std_attribute_p (parser))
19752 pedwarn (input_location, OPT_Wpedantic,
19753 "standard attributes on namespaces must precede "
19754 "the namespace name");
19755
19756 /* Parse any attributes specified after the identifier. */
19757 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19758 }
19759
19760 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19761 {
19762 /* Don't forget that the innermost namespace might have been
19763 marked as inline. Use |= because we cannot overwrite
19764 IS_INLINE in case the outermost namespace is inline, but
19765 there are no nested inlines. */
19766 is_inline |= nested_inline_p;
19767 break;
19768 }
19769
19770 if (!nested_definition_count && cxx_dialect < cxx17)
19771 pedwarn (input_location, OPT_Wpedantic,
19772 "nested namespace definitions only available with "
19773 "%<-std=c++17%> or %<-std=gnu++17%>");
19774
19775 /* Nested namespace names can create new namespaces (unlike
19776 other qualified-ids). */
19777 if (int count = (identifier
19778 ? push_namespace (identifier, nested_inline_p)
19779 : 0))
19780 nested_definition_count += count;
19781 else
19782 cp_parser_error (parser, "nested namespace name required");
19783 cp_lexer_consume_token (parser->lexer);
19784 }
19785
19786 if (nested_definition_count && !identifier)
19787 cp_parser_error (parser, "namespace name required");
19788
19789 if (nested_definition_count && attribs)
19790 error_at (token->location,
19791 "a nested namespace definition cannot have attributes");
19792 if (nested_definition_count && topmost_inline_p)
19793 error_at (token->location,
19794 "a nested namespace definition cannot be inline");
19795
19796 /* Start the namespace. */
19797 nested_definition_count += push_namespace (identifier, is_inline);
19798
19799 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19800
19801 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19802
19803 /* Look for the `{' to validate starting the namespace. */
19804 matching_braces braces;
19805 if (braces.require_open (parser))
19806 {
19807 /* Parse the body of the namespace. */
19808 cp_parser_namespace_body (parser);
19809
19810 /* Look for the final `}'. */
19811 braces.require_close (parser);
19812 }
19813
19814 if (has_visibility)
19815 pop_visibility (1);
19816
19817 /* Pop the nested namespace definitions. */
19818 while (nested_definition_count--)
19819 pop_namespace ();
19820 }
19821
19822 /* Parse a namespace-body.
19823
19824 namespace-body:
19825 declaration-seq [opt] */
19826
19827 static void
19828 cp_parser_namespace_body (cp_parser* parser)
19829 {
19830 cp_parser_declaration_seq_opt (parser);
19831 }
19832
19833 /* Parse a namespace-alias-definition.
19834
19835 namespace-alias-definition:
19836 namespace identifier = qualified-namespace-specifier ; */
19837
19838 static void
19839 cp_parser_namespace_alias_definition (cp_parser* parser)
19840 {
19841 tree identifier;
19842 tree namespace_specifier;
19843
19844 cp_token *token = cp_lexer_peek_token (parser->lexer);
19845
19846 /* Look for the `namespace' keyword. */
19847 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19848 /* Look for the identifier. */
19849 identifier = cp_parser_identifier (parser);
19850 if (identifier == error_mark_node)
19851 return;
19852 /* Look for the `=' token. */
19853 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19854 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19855 {
19856 error_at (token->location, "%<namespace%> definition is not allowed here");
19857 /* Skip the definition. */
19858 cp_lexer_consume_token (parser->lexer);
19859 if (cp_parser_skip_to_closing_brace (parser))
19860 cp_lexer_consume_token (parser->lexer);
19861 return;
19862 }
19863 cp_parser_require (parser, CPP_EQ, RT_EQ);
19864 /* Look for the qualified-namespace-specifier. */
19865 namespace_specifier
19866 = cp_parser_qualified_namespace_specifier (parser);
19867 cp_warn_deprecated_use_scopes (namespace_specifier);
19868 /* Look for the `;' token. */
19869 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19870
19871 /* Register the alias in the symbol table. */
19872 do_namespace_alias (identifier, namespace_specifier);
19873 }
19874
19875 /* Parse a qualified-namespace-specifier.
19876
19877 qualified-namespace-specifier:
19878 :: [opt] nested-name-specifier [opt] namespace-name
19879
19880 Returns a NAMESPACE_DECL corresponding to the specified
19881 namespace. */
19882
19883 static tree
19884 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19885 {
19886 /* Look for the optional `::'. */
19887 cp_parser_global_scope_opt (parser,
19888 /*current_scope_valid_p=*/false);
19889
19890 /* Look for the optional nested-name-specifier. */
19891 cp_parser_nested_name_specifier_opt (parser,
19892 /*typename_keyword_p=*/false,
19893 /*check_dependency_p=*/true,
19894 /*type_p=*/false,
19895 /*is_declaration=*/true);
19896
19897 return cp_parser_namespace_name (parser);
19898 }
19899
19900 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19901 access declaration.
19902
19903 using-declaration:
19904 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19905 using :: unqualified-id ;
19906
19907 access-declaration:
19908 qualified-id ;
19909
19910 */
19911
19912 static bool
19913 cp_parser_using_declaration (cp_parser* parser,
19914 bool access_declaration_p)
19915 {
19916 cp_token *token;
19917 bool typename_p = false;
19918 bool global_scope_p;
19919 tree decl;
19920 tree identifier;
19921 tree qscope;
19922 int oldcount = errorcount;
19923 cp_token *diag_token = NULL;
19924
19925 if (access_declaration_p)
19926 {
19927 diag_token = cp_lexer_peek_token (parser->lexer);
19928 cp_parser_parse_tentatively (parser);
19929 }
19930 else
19931 {
19932 /* Look for the `using' keyword. */
19933 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19934
19935 again:
19936 /* Peek at the next token. */
19937 token = cp_lexer_peek_token (parser->lexer);
19938 /* See if it's `typename'. */
19939 if (token->keyword == RID_TYPENAME)
19940 {
19941 /* Remember that we've seen it. */
19942 typename_p = true;
19943 /* Consume the `typename' token. */
19944 cp_lexer_consume_token (parser->lexer);
19945 }
19946 }
19947
19948 /* Look for the optional global scope qualification. */
19949 global_scope_p
19950 = (cp_parser_global_scope_opt (parser,
19951 /*current_scope_valid_p=*/false)
19952 != NULL_TREE);
19953
19954 /* If we saw `typename', or didn't see `::', then there must be a
19955 nested-name-specifier present. */
19956 if (typename_p || !global_scope_p)
19957 {
19958 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19959 /*check_dependency_p=*/true,
19960 /*type_p=*/false,
19961 /*is_declaration=*/true);
19962 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19963 {
19964 cp_parser_skip_to_end_of_block_or_statement (parser);
19965 return false;
19966 }
19967 }
19968 /* Otherwise, we could be in either of the two productions. In that
19969 case, treat the nested-name-specifier as optional. */
19970 else
19971 qscope = cp_parser_nested_name_specifier_opt (parser,
19972 /*typename_keyword_p=*/false,
19973 /*check_dependency_p=*/true,
19974 /*type_p=*/false,
19975 /*is_declaration=*/true);
19976 if (!qscope)
19977 qscope = global_namespace;
19978 else if (UNSCOPED_ENUM_P (qscope)
19979 && !TYPE_FUNCTION_SCOPE_P (qscope))
19980 qscope = CP_TYPE_CONTEXT (qscope);
19981
19982 cp_warn_deprecated_use_scopes (qscope);
19983
19984 if (access_declaration_p && cp_parser_error_occurred (parser))
19985 /* Something has already gone wrong; there's no need to parse
19986 further. Since an error has occurred, the return value of
19987 cp_parser_parse_definitely will be false, as required. */
19988 return cp_parser_parse_definitely (parser);
19989
19990 token = cp_lexer_peek_token (parser->lexer);
19991 /* Parse the unqualified-id. */
19992 identifier = cp_parser_unqualified_id (parser,
19993 /*template_keyword_p=*/false,
19994 /*check_dependency_p=*/true,
19995 /*declarator_p=*/true,
19996 /*optional_p=*/false);
19997
19998 if (access_declaration_p)
19999 {
20000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20001 cp_parser_simulate_error (parser);
20002 if (!cp_parser_parse_definitely (parser))
20003 return false;
20004 }
20005 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20006 {
20007 cp_token *ell = cp_lexer_consume_token (parser->lexer);
20008 if (cxx_dialect < cxx17)
20009 pedwarn (ell->location, 0,
20010 "pack expansion in using-declaration only available "
20011 "with %<-std=c++17%> or %<-std=gnu++17%>");
20012 qscope = make_pack_expansion (qscope);
20013 }
20014
20015 /* The function we call to handle a using-declaration is different
20016 depending on what scope we are in. */
20017 if (qscope == error_mark_node || identifier == error_mark_node)
20018 ;
20019 else if (!identifier_p (identifier)
20020 && TREE_CODE (identifier) != BIT_NOT_EXPR)
20021 /* [namespace.udecl]
20022
20023 A using declaration shall not name a template-id. */
20024 error_at (token->location,
20025 "a template-id may not appear in a using-declaration");
20026 else
20027 {
20028 if (at_class_scope_p ())
20029 {
20030 /* Create the USING_DECL. */
20031 decl = do_class_using_decl (qscope, identifier);
20032
20033 if (decl && typename_p)
20034 USING_DECL_TYPENAME_P (decl) = 1;
20035
20036 if (check_for_bare_parameter_packs (decl))
20037 {
20038 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20039 return false;
20040 }
20041 else
20042 /* Add it to the list of members in this class. */
20043 finish_member_declaration (decl);
20044 }
20045 else
20046 finish_nonmember_using_decl (qscope, identifier);
20047 }
20048
20049 if (!access_declaration_p
20050 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20051 {
20052 cp_token *comma = cp_lexer_consume_token (parser->lexer);
20053 if (cxx_dialect < cxx17)
20054 pedwarn (comma->location, 0,
20055 "comma-separated list in using-declaration only available "
20056 "with %<-std=c++17%> or %<-std=gnu++17%>");
20057 goto again;
20058 }
20059
20060 /* Look for the final `;'. */
20061 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20062
20063 if (access_declaration_p && errorcount == oldcount)
20064 warning_at (diag_token->location, OPT_Wdeprecated,
20065 "access declarations are deprecated "
20066 "in favour of using-declarations; "
20067 "suggestion: add the %<using%> keyword");
20068
20069 return true;
20070 }
20071
20072 /* Parse an alias-declaration.
20073
20074 alias-declaration:
20075 using identifier attribute-specifier-seq [opt] = type-id */
20076
20077 static tree
20078 cp_parser_alias_declaration (cp_parser* parser)
20079 {
20080 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
20081 location_t id_location, type_location;
20082 cp_declarator *declarator;
20083 cp_decl_specifier_seq decl_specs;
20084 bool member_p;
20085 const char *saved_message = NULL;
20086
20087 /* Look for the `using' keyword. */
20088 cp_token *using_token
20089 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
20090 if (using_token == NULL)
20091 return error_mark_node;
20092
20093 id_location = cp_lexer_peek_token (parser->lexer)->location;
20094 id = cp_parser_identifier (parser);
20095 if (id == error_mark_node)
20096 return error_mark_node;
20097
20098 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
20099 attributes = cp_parser_attributes_opt (parser);
20100 if (attributes == error_mark_node)
20101 return error_mark_node;
20102
20103 cp_parser_require (parser, CPP_EQ, RT_EQ);
20104
20105 if (cp_parser_error_occurred (parser))
20106 return error_mark_node;
20107
20108 cp_parser_commit_to_tentative_parse (parser);
20109
20110 /* Now we are going to parse the type-id of the declaration. */
20111
20112 /*
20113 [dcl.type]/3 says:
20114
20115 "A type-specifier-seq shall not define a class or enumeration
20116 unless it appears in the type-id of an alias-declaration (7.1.3) that
20117 is not the declaration of a template-declaration."
20118
20119 In other words, if we currently are in an alias template, the
20120 type-id should not define a type.
20121
20122 So let's set parser->type_definition_forbidden_message in that
20123 case; cp_parser_check_type_definition (called by
20124 cp_parser_class_specifier) will then emit an error if a type is
20125 defined in the type-id. */
20126 if (parser->num_template_parameter_lists)
20127 {
20128 saved_message = parser->type_definition_forbidden_message;
20129 parser->type_definition_forbidden_message =
20130 G_("types may not be defined in alias template declarations");
20131 }
20132
20133 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
20134 &type_location);
20135
20136 /* Restore the error message if need be. */
20137 if (parser->num_template_parameter_lists)
20138 parser->type_definition_forbidden_message = saved_message;
20139
20140 if (type == error_mark_node
20141 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
20142 {
20143 cp_parser_skip_to_end_of_block_or_statement (parser);
20144 return error_mark_node;
20145 }
20146
20147 /* A typedef-name can also be introduced by an alias-declaration. The
20148 identifier following the using keyword becomes a typedef-name. It has
20149 the same semantics as if it were introduced by the typedef
20150 specifier. In particular, it does not define a new type and it shall
20151 not appear in the type-id. */
20152
20153 clear_decl_specs (&decl_specs);
20154 decl_specs.type = type;
20155 if (attributes != NULL_TREE)
20156 {
20157 decl_specs.attributes = attributes;
20158 set_and_check_decl_spec_loc (&decl_specs,
20159 ds_attribute,
20160 attrs_token);
20161 }
20162 set_and_check_decl_spec_loc (&decl_specs,
20163 ds_typedef,
20164 using_token);
20165 set_and_check_decl_spec_loc (&decl_specs,
20166 ds_alias,
20167 using_token);
20168 decl_specs.locations[ds_type_spec] = type_location;
20169
20170 if (parser->num_template_parameter_lists
20171 && !cp_parser_check_template_parameters (parser,
20172 /*num_templates=*/0,
20173 /*template_id*/false,
20174 id_location,
20175 /*declarator=*/NULL))
20176 return error_mark_node;
20177
20178 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
20179
20180 member_p = at_class_scope_p ();
20181 if (member_p)
20182 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
20183 NULL_TREE, attributes);
20184 else
20185 decl = start_decl (declarator, &decl_specs, 0,
20186 attributes, NULL_TREE, &pushed_scope);
20187 if (decl == error_mark_node)
20188 return decl;
20189
20190 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
20191
20192 if (pushed_scope)
20193 pop_scope (pushed_scope);
20194
20195 /* If decl is a template, return its TEMPLATE_DECL so that it gets
20196 added into the symbol table; otherwise, return the TYPE_DECL. */
20197 if (DECL_LANG_SPECIFIC (decl)
20198 && DECL_TEMPLATE_INFO (decl)
20199 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
20200 {
20201 decl = DECL_TI_TEMPLATE (decl);
20202 if (member_p)
20203 check_member_template (decl);
20204 }
20205
20206 return decl;
20207 }
20208
20209 /* Parse a using-directive.
20210
20211 using-directive:
20212 using namespace :: [opt] nested-name-specifier [opt]
20213 namespace-name ; */
20214
20215 static void
20216 cp_parser_using_directive (cp_parser* parser)
20217 {
20218 tree namespace_decl;
20219 tree attribs;
20220
20221 /* Look for the `using' keyword. */
20222 cp_parser_require_keyword (parser, RID_USING, RT_USING);
20223 /* And the `namespace' keyword. */
20224 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
20225 /* Look for the optional `::' operator. */
20226 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20227 /* And the optional nested-name-specifier. */
20228 cp_parser_nested_name_specifier_opt (parser,
20229 /*typename_keyword_p=*/false,
20230 /*check_dependency_p=*/true,
20231 /*type_p=*/false,
20232 /*is_declaration=*/true);
20233 /* Get the namespace being used. */
20234 namespace_decl = cp_parser_namespace_name (parser);
20235 cp_warn_deprecated_use_scopes (namespace_decl);
20236 /* And any specified attributes. */
20237 attribs = cp_parser_attributes_opt (parser);
20238
20239 /* Update the symbol table. */
20240 finish_using_directive (namespace_decl, attribs);
20241
20242 /* Look for the final `;'. */
20243 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20244 }
20245
20246 /* Parse an asm-definition.
20247
20248 asm-qualifier:
20249 volatile
20250 inline
20251 goto
20252
20253 asm-qualifier-list:
20254 asm-qualifier
20255 asm-qualifier-list asm-qualifier
20256
20257 asm-definition:
20258 asm ( string-literal ) ;
20259
20260 GNU Extension:
20261
20262 asm-definition:
20263 asm asm-qualifier-list [opt] ( string-literal ) ;
20264 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
20265 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
20266 : asm-operand-list [opt] ) ;
20267 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
20268 : asm-operand-list [opt]
20269 : asm-clobber-list [opt] ) ;
20270 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
20271 : asm-clobber-list [opt]
20272 : asm-goto-list ) ;
20273
20274 The form with asm-goto-list is valid if and only if the asm-qualifier-list
20275 contains goto, and is the only allowed form in that case. No duplicates are
20276 allowed in an asm-qualifier-list. */
20277
20278 static void
20279 cp_parser_asm_definition (cp_parser* parser)
20280 {
20281 tree string;
20282 tree outputs = NULL_TREE;
20283 tree inputs = NULL_TREE;
20284 tree clobbers = NULL_TREE;
20285 tree labels = NULL_TREE;
20286 tree asm_stmt;
20287 bool extended_p = false;
20288 bool invalid_inputs_p = false;
20289 bool invalid_outputs_p = false;
20290 required_token missing = RT_NONE;
20291 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
20292
20293 /* Look for the `asm' keyword. */
20294 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
20295
20296 /* In C++20, unevaluated inline assembly is permitted in constexpr
20297 functions. */
20298 if (parser->in_function_body
20299 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
20300 && (cxx_dialect < cxx20))
20301 pedwarn (asm_loc, 0, "%<asm%> in %<constexpr%> function only available "
20302 "with %<-std=c++20%> or %<-std=gnu++20%>");
20303
20304 /* Handle the asm-qualifier-list. */
20305 location_t volatile_loc = UNKNOWN_LOCATION;
20306 location_t inline_loc = UNKNOWN_LOCATION;
20307 location_t goto_loc = UNKNOWN_LOCATION;
20308 location_t first_loc = UNKNOWN_LOCATION;
20309
20310 if (cp_parser_allow_gnu_extensions_p (parser))
20311 for (;;)
20312 {
20313 cp_token *token = cp_lexer_peek_token (parser->lexer);
20314 location_t loc = token->location;
20315 switch (cp_lexer_peek_token (parser->lexer)->keyword)
20316 {
20317 case RID_VOLATILE:
20318 if (volatile_loc)
20319 {
20320 error_at (loc, "duplicate %<asm%> qualifier %qT",
20321 token->u.value);
20322 inform (volatile_loc, "first seen here");
20323 }
20324 else
20325 {
20326 if (!parser->in_function_body)
20327 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
20328 "outside of function body", token->u.value);
20329 volatile_loc = loc;
20330 }
20331 cp_lexer_consume_token (parser->lexer);
20332 continue;
20333
20334 case RID_INLINE:
20335 if (inline_loc)
20336 {
20337 error_at (loc, "duplicate %<asm%> qualifier %qT",
20338 token->u.value);
20339 inform (inline_loc, "first seen here");
20340 }
20341 else
20342 inline_loc = loc;
20343 if (!first_loc)
20344 first_loc = loc;
20345 cp_lexer_consume_token (parser->lexer);
20346 continue;
20347
20348 case RID_GOTO:
20349 if (goto_loc)
20350 {
20351 error_at (loc, "duplicate %<asm%> qualifier %qT",
20352 token->u.value);
20353 inform (goto_loc, "first seen here");
20354 }
20355 else
20356 goto_loc = loc;
20357 if (!first_loc)
20358 first_loc = loc;
20359 cp_lexer_consume_token (parser->lexer);
20360 continue;
20361
20362 case RID_CONST:
20363 case RID_RESTRICT:
20364 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
20365 cp_lexer_consume_token (parser->lexer);
20366 continue;
20367
20368 default:
20369 break;
20370 }
20371 break;
20372 }
20373
20374 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
20375 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
20376 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
20377
20378 if (!parser->in_function_body && (inline_p || goto_p))
20379 {
20380 error_at (first_loc, "%<asm%> qualifier outside of function body");
20381 inline_p = goto_p = false;
20382 }
20383
20384 /* Look for the opening `('. */
20385 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20386 return;
20387 /* Look for the string. */
20388 string = cp_parser_string_literal (parser, false, false);
20389 if (string == error_mark_node)
20390 {
20391 cp_parser_skip_to_closing_parenthesis (parser, true, false,
20392 /*consume_paren=*/true);
20393 return;
20394 }
20395
20396 /* If we're allowing GNU extensions, check for the extended assembly
20397 syntax. Unfortunately, the `:' tokens need not be separated by
20398 a space in C, and so, for compatibility, we tolerate that here
20399 too. Doing that means that we have to treat the `::' operator as
20400 two `:' tokens. */
20401 if (cp_parser_allow_gnu_extensions_p (parser)
20402 && parser->in_function_body
20403 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
20404 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
20405 {
20406 bool inputs_p = false;
20407 bool clobbers_p = false;
20408 bool labels_p = false;
20409
20410 /* The extended syntax was used. */
20411 extended_p = true;
20412
20413 /* Look for outputs. */
20414 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20415 {
20416 /* Consume the `:'. */
20417 cp_lexer_consume_token (parser->lexer);
20418 /* Parse the output-operands. */
20419 if (cp_lexer_next_token_is_not (parser->lexer,
20420 CPP_COLON)
20421 && cp_lexer_next_token_is_not (parser->lexer,
20422 CPP_SCOPE)
20423 && cp_lexer_next_token_is_not (parser->lexer,
20424 CPP_CLOSE_PAREN)
20425 && !goto_p)
20426 {
20427 outputs = cp_parser_asm_operand_list (parser);
20428 if (outputs == error_mark_node)
20429 invalid_outputs_p = true;
20430 }
20431 }
20432 /* If the next token is `::', there are no outputs, and the
20433 next token is the beginning of the inputs. */
20434 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20435 /* The inputs are coming next. */
20436 inputs_p = true;
20437
20438 /* Look for inputs. */
20439 if (inputs_p
20440 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20441 {
20442 /* Consume the `:' or `::'. */
20443 cp_lexer_consume_token (parser->lexer);
20444 /* Parse the output-operands. */
20445 if (cp_lexer_next_token_is_not (parser->lexer,
20446 CPP_COLON)
20447 && cp_lexer_next_token_is_not (parser->lexer,
20448 CPP_SCOPE)
20449 && cp_lexer_next_token_is_not (parser->lexer,
20450 CPP_CLOSE_PAREN))
20451 {
20452 inputs = cp_parser_asm_operand_list (parser);
20453 if (inputs == error_mark_node)
20454 invalid_inputs_p = true;
20455 }
20456 }
20457 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20458 /* The clobbers are coming next. */
20459 clobbers_p = true;
20460
20461 /* Look for clobbers. */
20462 if (clobbers_p
20463 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20464 {
20465 clobbers_p = true;
20466 /* Consume the `:' or `::'. */
20467 cp_lexer_consume_token (parser->lexer);
20468 /* Parse the clobbers. */
20469 if (cp_lexer_next_token_is_not (parser->lexer,
20470 CPP_COLON)
20471 && cp_lexer_next_token_is_not (parser->lexer,
20472 CPP_CLOSE_PAREN))
20473 clobbers = cp_parser_asm_clobber_list (parser);
20474 }
20475 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20476 /* The labels are coming next. */
20477 labels_p = true;
20478
20479 /* Look for labels. */
20480 if (labels_p
20481 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
20482 {
20483 labels_p = true;
20484 /* Consume the `:' or `::'. */
20485 cp_lexer_consume_token (parser->lexer);
20486 /* Parse the labels. */
20487 labels = cp_parser_asm_label_list (parser);
20488 }
20489
20490 if (goto_p && !labels_p)
20491 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
20492 }
20493 else if (goto_p)
20494 missing = RT_COLON_SCOPE;
20495
20496 /* Look for the closing `)'. */
20497 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
20498 missing ? missing : RT_CLOSE_PAREN))
20499 cp_parser_skip_to_closing_parenthesis (parser, true, false,
20500 /*consume_paren=*/true);
20501 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20502
20503 if (!invalid_inputs_p && !invalid_outputs_p)
20504 {
20505 /* Create the ASM_EXPR. */
20506 if (parser->in_function_body)
20507 {
20508 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
20509 inputs, clobbers, labels, inline_p);
20510 /* If the extended syntax was not used, mark the ASM_EXPR. */
20511 if (!extended_p)
20512 {
20513 tree temp = asm_stmt;
20514 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
20515 temp = TREE_OPERAND (temp, 0);
20516
20517 ASM_INPUT_P (temp) = 1;
20518 }
20519 }
20520 else
20521 symtab->finalize_toplevel_asm (string);
20522 }
20523 }
20524
20525 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20526 type that comes from the decl-specifier-seq. */
20527
20528 static tree
20529 strip_declarator_types (tree type, cp_declarator *declarator)
20530 {
20531 for (cp_declarator *d = declarator; d;)
20532 switch (d->kind)
20533 {
20534 case cdk_id:
20535 case cdk_decomp:
20536 case cdk_error:
20537 d = NULL;
20538 break;
20539
20540 default:
20541 if (TYPE_PTRMEMFUNC_P (type))
20542 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
20543 type = TREE_TYPE (type);
20544 d = d->declarator;
20545 break;
20546 }
20547
20548 return type;
20549 }
20550
20551 /* Declarators [gram.dcl.decl] */
20552
20553 /* Parse an init-declarator.
20554
20555 init-declarator:
20556 declarator initializer [opt]
20557
20558 GNU Extension:
20559
20560 init-declarator:
20561 declarator asm-specification [opt] attributes [opt] initializer [opt]
20562
20563 function-definition:
20564 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20565 function-body
20566 decl-specifier-seq [opt] declarator function-try-block
20567
20568 GNU Extension:
20569
20570 function-definition:
20571 __extension__ function-definition
20572
20573 TM Extension:
20574
20575 function-definition:
20576 decl-specifier-seq [opt] declarator function-transaction-block
20577
20578 The parser flags FLAGS is used to control type-specifier parsing.
20579
20580 The DECL_SPECIFIERS apply to this declarator. Returns a
20581 representation of the entity declared. If MEMBER_P is TRUE, then
20582 this declarator appears in a class scope. The new DECL created by
20583 this declarator is returned.
20584
20585 The CHECKS are access checks that should be performed once we know
20586 what entity is being declared (and, therefore, what classes have
20587 befriended it).
20588
20589 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20590 for a function-definition here as well. If the declarator is a
20591 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20592 be TRUE upon return. By that point, the function-definition will
20593 have been completely parsed.
20594
20595 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20596 is FALSE.
20597
20598 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20599 parsed declaration if it is an uninitialized single declarator not followed
20600 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20601 if present, will not be consumed. If returned, this declarator will be
20602 created with SD_INITIALIZED but will not call cp_finish_decl.
20603
20604 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20605 and there is an initializer, the pointed location_t is set to the
20606 location of the '=' or `(', or '{' in C++11 token introducing the
20607 initializer. */
20608
20609 static tree
20610 cp_parser_init_declarator (cp_parser* parser,
20611 cp_parser_flags flags,
20612 cp_decl_specifier_seq *decl_specifiers,
20613 vec<deferred_access_check, va_gc> *checks,
20614 bool function_definition_allowed_p,
20615 bool member_p,
20616 int declares_class_or_enum,
20617 bool* function_definition_p,
20618 tree* maybe_range_for_decl,
20619 location_t* init_loc,
20620 tree* auto_result)
20621 {
20622 cp_token *token = NULL, *asm_spec_start_token = NULL,
20623 *attributes_start_token = NULL;
20624 cp_declarator *declarator;
20625 tree prefix_attributes;
20626 tree attributes = NULL;
20627 tree asm_specification;
20628 tree initializer;
20629 tree decl = NULL_TREE;
20630 tree scope;
20631 int is_initialized;
20632 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20633 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20634 "(...)". */
20635 enum cpp_ttype initialization_kind;
20636 bool is_direct_init = false;
20637 bool is_non_constant_init;
20638 int ctor_dtor_or_conv_p;
20639 bool friend_p = cp_parser_friend_p (decl_specifiers);
20640 tree pushed_scope = NULL_TREE;
20641 bool range_for_decl_p = false;
20642 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20643 location_t tmp_init_loc = UNKNOWN_LOCATION;
20644
20645 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
20646 flags |= CP_PARSER_FLAGS_CONSTEVAL;
20647
20648 /* Gather the attributes that were provided with the
20649 decl-specifiers. */
20650 prefix_attributes = decl_specifiers->attributes;
20651
20652 /* Assume that this is not the declarator for a function
20653 definition. */
20654 if (function_definition_p)
20655 *function_definition_p = false;
20656
20657 /* Default arguments are only permitted for function parameters. */
20658 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20659 parser->default_arg_ok_p = false;
20660
20661 /* Defer access checks while parsing the declarator; we cannot know
20662 what names are accessible until we know what is being
20663 declared. */
20664 resume_deferring_access_checks ();
20665
20666 token = cp_lexer_peek_token (parser->lexer);
20667
20668 /* Parse the declarator. */
20669 declarator
20670 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20671 flags, &ctor_dtor_or_conv_p,
20672 /*parenthesized_p=*/NULL,
20673 member_p, friend_p, /*static_p=*/false);
20674 /* Gather up the deferred checks. */
20675 stop_deferring_access_checks ();
20676
20677 parser->default_arg_ok_p = saved_default_arg_ok_p;
20678
20679 /* If the DECLARATOR was erroneous, there's no need to go
20680 further. */
20681 if (declarator == cp_error_declarator)
20682 return error_mark_node;
20683
20684 /* Check that the number of template-parameter-lists is OK. */
20685 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20686 token->location))
20687 return error_mark_node;
20688
20689 if (declares_class_or_enum & 2)
20690 cp_parser_check_for_definition_in_return_type (declarator,
20691 decl_specifiers->type,
20692 decl_specifiers->locations[ds_type_spec]);
20693
20694 /* Figure out what scope the entity declared by the DECLARATOR is
20695 located in. `grokdeclarator' sometimes changes the scope, so
20696 we compute it now. */
20697 scope = get_scope_of_declarator (declarator);
20698
20699 /* Perform any lookups in the declared type which were thought to be
20700 dependent, but are not in the scope of the declarator. */
20701 decl_specifiers->type
20702 = maybe_update_decl_type (decl_specifiers->type, scope);
20703
20704 /* If we're allowing GNU extensions, look for an
20705 asm-specification. */
20706 if (cp_parser_allow_gnu_extensions_p (parser))
20707 {
20708 /* Look for an asm-specification. */
20709 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20710 asm_specification = cp_parser_asm_specification_opt (parser);
20711 }
20712 else
20713 asm_specification = NULL_TREE;
20714
20715 /* Look for attributes. */
20716 attributes_start_token = cp_lexer_peek_token (parser->lexer);
20717 attributes = cp_parser_attributes_opt (parser);
20718
20719 /* Peek at the next token. */
20720 token = cp_lexer_peek_token (parser->lexer);
20721
20722 bool bogus_implicit_tmpl = false;
20723
20724 if (function_declarator_p (declarator))
20725 {
20726 /* Handle C++17 deduction guides. */
20727 if (!decl_specifiers->type
20728 && ctor_dtor_or_conv_p <= 0
20729 && cxx_dialect >= cxx17)
20730 {
20731 cp_declarator *id = get_id_declarator (declarator);
20732 tree name = id->u.id.unqualified_name;
20733 parser->scope = id->u.id.qualifying_scope;
20734 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20735 if (tmpl
20736 && (DECL_CLASS_TEMPLATE_P (tmpl)
20737 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20738 {
20739 id->u.id.unqualified_name = dguide_name (tmpl);
20740 id->u.id.sfk = sfk_deduction_guide;
20741 ctor_dtor_or_conv_p = 1;
20742 }
20743 }
20744
20745 /* Check to see if the token indicates the start of a
20746 function-definition. */
20747 if (cp_parser_token_starts_function_definition_p (token))
20748 {
20749 if (!function_definition_allowed_p)
20750 {
20751 /* If a function-definition should not appear here, issue an
20752 error message. */
20753 cp_parser_error (parser,
20754 "a function-definition is not allowed here");
20755 return error_mark_node;
20756 }
20757
20758 location_t func_brace_location
20759 = cp_lexer_peek_token (parser->lexer)->location;
20760
20761 /* Neither attributes nor an asm-specification are allowed
20762 on a function-definition. */
20763 if (asm_specification)
20764 error_at (asm_spec_start_token->location,
20765 "an %<asm%> specification is not allowed "
20766 "on a function-definition");
20767 if (attributes)
20768 error_at (attributes_start_token->location,
20769 "attributes are not allowed "
20770 "on a function-definition");
20771 /* This is a function-definition. */
20772 *function_definition_p = true;
20773
20774 /* Parse the function definition. */
20775 if (member_p)
20776 decl = cp_parser_save_member_function_body (parser,
20777 decl_specifiers,
20778 declarator,
20779 prefix_attributes);
20780 else
20781 decl =
20782 (cp_parser_function_definition_from_specifiers_and_declarator
20783 (parser, decl_specifiers, prefix_attributes, declarator));
20784
20785 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20786 {
20787 /* This is where the prologue starts... */
20788 DECL_STRUCT_FUNCTION (decl)->function_start_locus
20789 = func_brace_location;
20790 }
20791
20792 return decl;
20793 }
20794 }
20795 else if (parser->fully_implicit_function_template_p)
20796 {
20797 /* A non-template declaration involving a function parameter list
20798 containing an implicit template parameter will be made into a
20799 template. If the resulting declaration is not going to be an
20800 actual function then finish the template scope here to prevent it.
20801 An error message will be issued once we have a decl to talk about.
20802
20803 FIXME probably we should do type deduction rather than create an
20804 implicit template, but the standard currently doesn't allow it. */
20805 bogus_implicit_tmpl = true;
20806 finish_fully_implicit_template (parser, NULL_TREE);
20807 }
20808
20809 /* [dcl.dcl]
20810
20811 Only in function declarations for constructors, destructors, type
20812 conversions, and deduction guides can the decl-specifier-seq be omitted.
20813
20814 We explicitly postpone this check past the point where we handle
20815 function-definitions because we tolerate function-definitions
20816 that are missing their return types in some modes. */
20817 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20818 {
20819 cp_parser_error (parser,
20820 "expected constructor, destructor, or type conversion");
20821 return error_mark_node;
20822 }
20823
20824 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20825 if (token->type == CPP_EQ
20826 || token->type == CPP_OPEN_PAREN
20827 || token->type == CPP_OPEN_BRACE)
20828 {
20829 is_initialized = SD_INITIALIZED;
20830 initialization_kind = token->type;
20831 if (maybe_range_for_decl)
20832 *maybe_range_for_decl = error_mark_node;
20833 tmp_init_loc = token->location;
20834 if (init_loc && *init_loc == UNKNOWN_LOCATION)
20835 *init_loc = tmp_init_loc;
20836
20837 if (token->type == CPP_EQ
20838 && function_declarator_p (declarator))
20839 {
20840 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20841 if (t2->keyword == RID_DEFAULT)
20842 is_initialized = SD_DEFAULTED;
20843 else if (t2->keyword == RID_DELETE)
20844 is_initialized = SD_DELETED;
20845 }
20846 }
20847 else
20848 {
20849 /* If the init-declarator isn't initialized and isn't followed by a
20850 `,' or `;', it's not a valid init-declarator. */
20851 if (token->type != CPP_COMMA
20852 && token->type != CPP_SEMICOLON)
20853 {
20854 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20855 range_for_decl_p = true;
20856 else
20857 {
20858 if (!maybe_range_for_decl)
20859 cp_parser_error (parser, "expected initializer");
20860 return error_mark_node;
20861 }
20862 }
20863 is_initialized = SD_UNINITIALIZED;
20864 initialization_kind = CPP_EOF;
20865 }
20866
20867 /* Because start_decl has side-effects, we should only call it if we
20868 know we're going ahead. By this point, we know that we cannot
20869 possibly be looking at any other construct. */
20870 cp_parser_commit_to_tentative_parse (parser);
20871
20872 /* Enter the newly declared entry in the symbol table. If we're
20873 processing a declaration in a class-specifier, we wait until
20874 after processing the initializer. */
20875 if (!member_p)
20876 {
20877 if (parser->in_unbraced_linkage_specification_p)
20878 decl_specifiers->storage_class = sc_extern;
20879 decl = start_decl (declarator, decl_specifiers,
20880 range_for_decl_p? SD_INITIALIZED : is_initialized,
20881 attributes, prefix_attributes, &pushed_scope);
20882 cp_finalize_omp_declare_simd (parser, decl);
20883 cp_finalize_oacc_routine (parser, decl, false);
20884 /* Adjust location of decl if declarator->id_loc is more appropriate:
20885 set, and decl wasn't merged with another decl, in which case its
20886 location would be different from input_location, and more accurate. */
20887 if (DECL_P (decl)
20888 && declarator->id_loc != UNKNOWN_LOCATION
20889 && DECL_SOURCE_LOCATION (decl) == input_location)
20890 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20891 }
20892 else if (scope)
20893 /* Enter the SCOPE. That way unqualified names appearing in the
20894 initializer will be looked up in SCOPE. */
20895 pushed_scope = push_scope (scope);
20896
20897 /* Perform deferred access control checks, now that we know in which
20898 SCOPE the declared entity resides. */
20899 if (!member_p && decl)
20900 {
20901 tree saved_current_function_decl = NULL_TREE;
20902
20903 /* If the entity being declared is a function, pretend that we
20904 are in its scope. If it is a `friend', it may have access to
20905 things that would not otherwise be accessible. */
20906 if (TREE_CODE (decl) == FUNCTION_DECL)
20907 {
20908 saved_current_function_decl = current_function_decl;
20909 current_function_decl = decl;
20910 }
20911
20912 /* Perform access checks for template parameters. */
20913 cp_parser_perform_template_parameter_access_checks (checks);
20914
20915 /* Perform the access control checks for the declarator and the
20916 decl-specifiers. */
20917 perform_deferred_access_checks (tf_warning_or_error);
20918
20919 /* Restore the saved value. */
20920 if (TREE_CODE (decl) == FUNCTION_DECL)
20921 current_function_decl = saved_current_function_decl;
20922 }
20923
20924 /* Parse the initializer. */
20925 initializer = NULL_TREE;
20926 is_direct_init = false;
20927 is_non_constant_init = true;
20928 if (is_initialized)
20929 {
20930 if (function_declarator_p (declarator))
20931 {
20932 if (initialization_kind == CPP_EQ)
20933 initializer = cp_parser_pure_specifier (parser);
20934 else
20935 {
20936 /* If the declaration was erroneous, we don't really
20937 know what the user intended, so just silently
20938 consume the initializer. */
20939 if (decl != error_mark_node)
20940 error_at (tmp_init_loc, "initializer provided for function");
20941 cp_parser_skip_to_closing_parenthesis (parser,
20942 /*recovering=*/true,
20943 /*or_comma=*/false,
20944 /*consume_paren=*/true);
20945 }
20946 }
20947 else
20948 {
20949 /* We want to record the extra mangling scope for in-class
20950 initializers of class members and initializers of static
20951 data member templates and namespace-scope initializers.
20952 The former involves deferring parsing of the initializer
20953 until end of class as with default arguments. So right
20954 here we only handle the latter two. */
20955 bool has_lambda_scope = false;
20956
20957 if (decl != error_mark_node
20958 && !member_p
20959 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
20960 has_lambda_scope = true;
20961
20962 if (has_lambda_scope)
20963 start_lambda_scope (decl);
20964 initializer = cp_parser_initializer (parser,
20965 &is_direct_init,
20966 &is_non_constant_init);
20967 if (has_lambda_scope)
20968 finish_lambda_scope ();
20969 if (initializer == error_mark_node)
20970 cp_parser_skip_to_end_of_statement (parser);
20971 }
20972 }
20973
20974 /* The old parser allows attributes to appear after a parenthesized
20975 initializer. Mark Mitchell proposed removing this functionality
20976 on the GCC mailing lists on 2002-08-13. This parser accepts the
20977 attributes -- but ignores them. Made a permerror in GCC 8. */
20978 if (cp_parser_allow_gnu_extensions_p (parser)
20979 && initialization_kind == CPP_OPEN_PAREN
20980 && cp_parser_attributes_opt (parser)
20981 && permerror (input_location,
20982 "attributes after parenthesized initializer ignored"))
20983 {
20984 static bool hint;
20985 if (flag_permissive && !hint)
20986 {
20987 hint = true;
20988 inform (input_location,
20989 "this flexibility is deprecated and will be removed");
20990 }
20991 }
20992
20993 /* And now complain about a non-function implicit template. */
20994 if (bogus_implicit_tmpl && decl != error_mark_node)
20995 error_at (DECL_SOURCE_LOCATION (decl),
20996 "non-function %qD declared as implicit template", decl);
20997
20998 /* For an in-class declaration, use `grokfield' to create the
20999 declaration. */
21000 if (member_p)
21001 {
21002 if (pushed_scope)
21003 {
21004 pop_scope (pushed_scope);
21005 pushed_scope = NULL_TREE;
21006 }
21007 decl = grokfield (declarator, decl_specifiers,
21008 initializer, !is_non_constant_init,
21009 /*asmspec=*/NULL_TREE,
21010 attr_chainon (attributes, prefix_attributes));
21011 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
21012 cp_parser_save_default_args (parser, decl);
21013 cp_finalize_omp_declare_simd (parser, decl);
21014 cp_finalize_oacc_routine (parser, decl, false);
21015 }
21016
21017 /* Finish processing the declaration. But, skip member
21018 declarations. */
21019 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
21020 {
21021 int cf = (decl_spec_seq_has_spec_p (decl_specifiers, ds_constinit)
21022 ? LOOKUP_CONSTINIT : 0);
21023 cp_finish_decl (decl,
21024 initializer, !is_non_constant_init,
21025 asm_specification,
21026 /* If the initializer is in parentheses, then this is
21027 a direct-initialization, which means that an
21028 `explicit' constructor is OK. Otherwise, an
21029 `explicit' constructor cannot be used. */
21030 ((is_direct_init || !is_initialized)
21031 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT) | cf);
21032 }
21033 else if ((cxx_dialect != cxx98) && friend_p
21034 && decl && TREE_CODE (decl) == FUNCTION_DECL)
21035 /* Core issue #226 (C++0x only): A default template-argument
21036 shall not be specified in a friend class template
21037 declaration. */
21038 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
21039 /*is_partial=*/false, /*is_friend_decl=*/1);
21040
21041 if (!friend_p && pushed_scope)
21042 pop_scope (pushed_scope);
21043
21044 if (function_declarator_p (declarator)
21045 && parser->fully_implicit_function_template_p)
21046 {
21047 if (member_p)
21048 decl = finish_fully_implicit_template (parser, decl);
21049 else
21050 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
21051 }
21052
21053 if (auto_result && is_initialized && decl_specifiers->type
21054 && type_uses_auto (decl_specifiers->type))
21055 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
21056
21057 return decl;
21058 }
21059
21060 /* Parse a declarator.
21061
21062 declarator:
21063 direct-declarator
21064 ptr-operator declarator
21065
21066 abstract-declarator:
21067 ptr-operator abstract-declarator [opt]
21068 direct-abstract-declarator
21069
21070 GNU Extensions:
21071
21072 declarator:
21073 attributes [opt] direct-declarator
21074 attributes [opt] ptr-operator declarator
21075
21076 abstract-declarator:
21077 attributes [opt] ptr-operator abstract-declarator [opt]
21078 attributes [opt] direct-abstract-declarator
21079
21080 The parser flags FLAGS is used to control type-specifier parsing.
21081
21082 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
21083 detect constructors, destructors, deduction guides, or conversion operators.
21084 It is set to -1 if the declarator is a name, and +1 if it is a
21085 function. Otherwise it is set to zero. Usually you just want to
21086 test for >0, but internally the negative value is used.
21087
21088 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
21089 a decl-specifier-seq unless it declares a constructor, destructor,
21090 or conversion. It might seem that we could check this condition in
21091 semantic analysis, rather than parsing, but that makes it difficult
21092 to handle something like `f()'. We want to notice that there are
21093 no decl-specifiers, and therefore realize that this is an
21094 expression, not a declaration.)
21095
21096 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
21097 the declarator is a direct-declarator of the form "(...)".
21098
21099 MEMBER_P is true iff this declarator is a member-declarator.
21100
21101 FRIEND_P is true iff this declarator is a friend.
21102
21103 STATIC_P is true iff the keyword static was seen. */
21104
21105 static cp_declarator *
21106 cp_parser_declarator (cp_parser* parser,
21107 cp_parser_declarator_kind dcl_kind,
21108 cp_parser_flags flags,
21109 int* ctor_dtor_or_conv_p,
21110 bool* parenthesized_p,
21111 bool member_p, bool friend_p, bool static_p)
21112 {
21113 cp_declarator *declarator;
21114 enum tree_code code;
21115 cp_cv_quals cv_quals;
21116 tree class_type;
21117 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
21118
21119 /* Assume this is not a constructor, destructor, or type-conversion
21120 operator. */
21121 if (ctor_dtor_or_conv_p)
21122 *ctor_dtor_or_conv_p = 0;
21123
21124 if (cp_parser_allow_gnu_extensions_p (parser))
21125 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
21126
21127 /* Check for the ptr-operator production. */
21128 cp_parser_parse_tentatively (parser);
21129 /* Parse the ptr-operator. */
21130 code = cp_parser_ptr_operator (parser,
21131 &class_type,
21132 &cv_quals,
21133 &std_attributes);
21134
21135 /* If that worked, then we have a ptr-operator. */
21136 if (cp_parser_parse_definitely (parser))
21137 {
21138 /* If a ptr-operator was found, then this declarator was not
21139 parenthesized. */
21140 if (parenthesized_p)
21141 *parenthesized_p = true;
21142 /* The dependent declarator is optional if we are parsing an
21143 abstract-declarator. */
21144 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
21145 cp_parser_parse_tentatively (parser);
21146
21147 /* Parse the dependent declarator. */
21148 declarator = cp_parser_declarator (parser, dcl_kind,
21149 CP_PARSER_FLAGS_NONE,
21150 /*ctor_dtor_or_conv_p=*/NULL,
21151 /*parenthesized_p=*/NULL,
21152 /*member_p=*/false,
21153 friend_p, /*static_p=*/false);
21154
21155 /* If we are parsing an abstract-declarator, we must handle the
21156 case where the dependent declarator is absent. */
21157 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
21158 && !cp_parser_parse_definitely (parser))
21159 declarator = NULL;
21160
21161 declarator = cp_parser_make_indirect_declarator
21162 (code, class_type, cv_quals, declarator, std_attributes);
21163 }
21164 /* Everything else is a direct-declarator. */
21165 else
21166 {
21167 if (parenthesized_p)
21168 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
21169 CPP_OPEN_PAREN);
21170 declarator = cp_parser_direct_declarator (parser, dcl_kind,
21171 flags, ctor_dtor_or_conv_p,
21172 member_p, friend_p, static_p);
21173 }
21174
21175 if (gnu_attributes && declarator && declarator != cp_error_declarator)
21176 declarator->attributes = gnu_attributes;
21177 return declarator;
21178 }
21179
21180 /* Parse a direct-declarator or direct-abstract-declarator.
21181
21182 direct-declarator:
21183 declarator-id
21184 direct-declarator ( parameter-declaration-clause )
21185 cv-qualifier-seq [opt]
21186 ref-qualifier [opt]
21187 exception-specification [opt]
21188 direct-declarator [ constant-expression [opt] ]
21189 ( declarator )
21190
21191 direct-abstract-declarator:
21192 direct-abstract-declarator [opt]
21193 ( parameter-declaration-clause )
21194 cv-qualifier-seq [opt]
21195 ref-qualifier [opt]
21196 exception-specification [opt]
21197 direct-abstract-declarator [opt] [ constant-expression [opt] ]
21198 ( abstract-declarator )
21199
21200 Returns a representation of the declarator. DCL_KIND is
21201 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
21202 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
21203 we are parsing a direct-declarator. It is
21204 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
21205 of ambiguity we prefer an abstract declarator, as per
21206 [dcl.ambig.res].
21207 The parser flags FLAGS is used to control type-specifier parsing.
21208 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
21209 as for cp_parser_declarator. */
21210
21211 static cp_declarator *
21212 cp_parser_direct_declarator (cp_parser* parser,
21213 cp_parser_declarator_kind dcl_kind,
21214 cp_parser_flags flags,
21215 int* ctor_dtor_or_conv_p,
21216 bool member_p, bool friend_p, bool static_p)
21217 {
21218 cp_token *token;
21219 cp_declarator *declarator = NULL;
21220 tree scope = NULL_TREE;
21221 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21222 bool saved_in_declarator_p = parser->in_declarator_p;
21223 bool first = true;
21224 tree pushed_scope = NULL_TREE;
21225 cp_token *open_paren = NULL, *close_paren = NULL;
21226
21227 while (true)
21228 {
21229 /* Peek at the next token. */
21230 token = cp_lexer_peek_token (parser->lexer);
21231 if (token->type == CPP_OPEN_PAREN)
21232 {
21233 /* This is either a parameter-declaration-clause, or a
21234 parenthesized declarator. When we know we are parsing a
21235 named declarator, it must be a parenthesized declarator
21236 if FIRST is true. For instance, `(int)' is a
21237 parameter-declaration-clause, with an omitted
21238 direct-abstract-declarator. But `((*))', is a
21239 parenthesized abstract declarator. Finally, when T is a
21240 template parameter `(T)' is a
21241 parameter-declaration-clause, and not a parenthesized
21242 named declarator.
21243
21244 We first try and parse a parameter-declaration-clause,
21245 and then try a nested declarator (if FIRST is true).
21246
21247 It is not an error for it not to be a
21248 parameter-declaration-clause, even when FIRST is
21249 false. Consider,
21250
21251 int i (int);
21252 int i (3);
21253
21254 The first is the declaration of a function while the
21255 second is the definition of a variable, including its
21256 initializer.
21257
21258 Having seen only the parenthesis, we cannot know which of
21259 these two alternatives should be selected. Even more
21260 complex are examples like:
21261
21262 int i (int (a));
21263 int i (int (3));
21264
21265 The former is a function-declaration; the latter is a
21266 variable initialization.
21267
21268 Thus again, we try a parameter-declaration-clause, and if
21269 that fails, we back out and return. */
21270
21271 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
21272 {
21273 tree params;
21274 bool is_declarator = false;
21275
21276 open_paren = NULL;
21277
21278 /* In a member-declarator, the only valid interpretation
21279 of a parenthesis is the start of a
21280 parameter-declaration-clause. (It is invalid to
21281 initialize a static data member with a parenthesized
21282 initializer; only the "=" form of initialization is
21283 permitted.) */
21284 if (!member_p)
21285 cp_parser_parse_tentatively (parser);
21286
21287 /* Consume the `('. */
21288 matching_parens parens;
21289 parens.consume_open (parser);
21290 if (first)
21291 {
21292 /* If this is going to be an abstract declarator, we're
21293 in a declarator and we can't have default args. */
21294 parser->default_arg_ok_p = false;
21295 parser->in_declarator_p = true;
21296 }
21297
21298 begin_scope (sk_function_parms, NULL_TREE);
21299
21300 /* Signal we are in the immediate function context. */
21301 if (flags & CP_PARSER_FLAGS_CONSTEVAL)
21302 current_binding_level->immediate_fn_ctx_p = true;
21303
21304 /* Parse the parameter-declaration-clause. */
21305 params
21306 = cp_parser_parameter_declaration_clause (parser, flags);
21307
21308 /* Consume the `)'. */
21309 parens.require_close (parser);
21310
21311 /* If all went well, parse the cv-qualifier-seq,
21312 ref-qualifier and the exception-specification. */
21313 if (member_p || cp_parser_parse_definitely (parser))
21314 {
21315 cp_cv_quals cv_quals;
21316 cp_virt_specifiers virt_specifiers;
21317 cp_ref_qualifier ref_qual;
21318 tree exception_specification;
21319 tree late_return;
21320 tree attrs;
21321 bool memfn = (member_p || (pushed_scope
21322 && CLASS_TYPE_P (pushed_scope)));
21323 unsigned char local_variables_forbidden_p
21324 = parser->local_variables_forbidden_p;
21325 /* 'this' is not allowed in static member functions. */
21326 if (static_p || friend_p)
21327 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
21328
21329 is_declarator = true;
21330
21331 if (ctor_dtor_or_conv_p)
21332 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
21333 first = false;
21334
21335 /* Parse the cv-qualifier-seq. */
21336 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21337 /* Parse the ref-qualifier. */
21338 ref_qual = cp_parser_ref_qualifier_opt (parser);
21339 /* Parse the tx-qualifier. */
21340 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
21341
21342 tree save_ccp = current_class_ptr;
21343 tree save_ccr = current_class_ref;
21344 if (memfn)
21345 /* DR 1207: 'this' is in scope after the cv-quals. */
21346 inject_this_parameter (current_class_type, cv_quals);
21347
21348 /* If it turned out that this is e.g. a pointer to a
21349 function, we don't want to delay noexcept parsing. */
21350 if (declarator == NULL || declarator->kind != cdk_id)
21351 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
21352
21353 /* Parse the exception-specification. */
21354 exception_specification
21355 = cp_parser_exception_specification_opt (parser,
21356 flags);
21357
21358 attrs = cp_parser_std_attribute_spec_seq (parser);
21359
21360 /* In here, we handle cases where attribute is used after
21361 the function declaration. For example:
21362 void func (int x) __attribute__((vector(..))); */
21363 tree gnu_attrs = NULL_TREE;
21364 tree requires_clause = NULL_TREE;
21365 late_return = (cp_parser_late_return_type_opt
21366 (parser, declarator, requires_clause));
21367
21368 /* Parse the virt-specifier-seq. */
21369 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21370
21371 /* Create the function-declarator. */
21372 declarator = make_call_declarator (declarator,
21373 params,
21374 cv_quals,
21375 virt_specifiers,
21376 ref_qual,
21377 tx_qual,
21378 exception_specification,
21379 late_return,
21380 requires_clause);
21381 declarator->std_attributes = attrs;
21382 declarator->attributes = gnu_attrs;
21383 /* Any subsequent parameter lists are to do with
21384 return type, so are not those of the declared
21385 function. */
21386 parser->default_arg_ok_p = false;
21387
21388 current_class_ptr = save_ccp;
21389 current_class_ref = save_ccr;
21390
21391 /* Restore the state of local_variables_forbidden_p. */
21392 parser->local_variables_forbidden_p
21393 = local_variables_forbidden_p;
21394 }
21395
21396 /* Remove the function parms from scope. */
21397 pop_bindings_and_leave_scope ();
21398
21399 if (is_declarator)
21400 /* Repeat the main loop. */
21401 continue;
21402 }
21403
21404 /* If this is the first, we can try a parenthesized
21405 declarator. */
21406 if (first)
21407 {
21408 bool saved_in_type_id_in_expr_p;
21409
21410 parser->default_arg_ok_p = saved_default_arg_ok_p;
21411 parser->in_declarator_p = saved_in_declarator_p;
21412
21413 open_paren = token;
21414 /* Consume the `('. */
21415 matching_parens parens;
21416 parens.consume_open (parser);
21417 /* Parse the nested declarator. */
21418 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21419 parser->in_type_id_in_expr_p = true;
21420 declarator
21421 = cp_parser_declarator (parser, dcl_kind, flags,
21422 ctor_dtor_or_conv_p,
21423 /*parenthesized_p=*/NULL,
21424 member_p, friend_p,
21425 /*static_p=*/false);
21426 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21427 first = false;
21428 /* Expect a `)'. */
21429 close_paren = cp_lexer_peek_token (parser->lexer);
21430 if (!parens.require_close (parser))
21431 declarator = cp_error_declarator;
21432 if (declarator == cp_error_declarator)
21433 break;
21434
21435 goto handle_declarator;
21436 }
21437 /* Otherwise, we must be done. */
21438 else
21439 break;
21440 }
21441 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
21442 && token->type == CPP_OPEN_SQUARE
21443 && !cp_next_tokens_can_be_attribute_p (parser))
21444 {
21445 /* Parse an array-declarator. */
21446 tree bounds, attrs;
21447
21448 if (ctor_dtor_or_conv_p)
21449 *ctor_dtor_or_conv_p = 0;
21450
21451 open_paren = NULL;
21452 first = false;
21453 parser->default_arg_ok_p = false;
21454 parser->in_declarator_p = true;
21455 /* Consume the `['. */
21456 cp_lexer_consume_token (parser->lexer);
21457 /* Peek at the next token. */
21458 token = cp_lexer_peek_token (parser->lexer);
21459 /* If the next token is `]', then there is no
21460 constant-expression. */
21461 if (token->type != CPP_CLOSE_SQUARE)
21462 {
21463 bool non_constant_p;
21464 bounds
21465 = cp_parser_constant_expression (parser,
21466 /*allow_non_constant=*/true,
21467 &non_constant_p);
21468 if (!non_constant_p)
21469 /* OK */;
21470 else if (error_operand_p (bounds))
21471 /* Already gave an error. */;
21472 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21473 /* Let compute_array_index_type diagnose this. */;
21474 else if (!parser->in_function_body
21475 || current_binding_level->kind == sk_function_parms)
21476 {
21477 /* Normally, the array bound must be an integral constant
21478 expression. However, as an extension, we allow VLAs
21479 in function scopes as long as they aren't part of a
21480 parameter declaration. */
21481 cp_parser_error (parser,
21482 "array bound is not an integer constant");
21483 bounds = error_mark_node;
21484 }
21485 else if (processing_template_decl
21486 && !type_dependent_expression_p (bounds))
21487 {
21488 /* Remember this wasn't a constant-expression. */
21489 bounds = build_nop (TREE_TYPE (bounds), bounds);
21490 TREE_SIDE_EFFECTS (bounds) = 1;
21491 }
21492 }
21493 else
21494 bounds = NULL_TREE;
21495 /* Look for the closing `]'. */
21496 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21497 {
21498 declarator = cp_error_declarator;
21499 break;
21500 }
21501
21502 attrs = cp_parser_std_attribute_spec_seq (parser);
21503 declarator = make_array_declarator (declarator, bounds);
21504 declarator->std_attributes = attrs;
21505 }
21506 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
21507 {
21508 {
21509 tree qualifying_scope;
21510 tree unqualified_name;
21511 tree attrs;
21512 special_function_kind sfk;
21513 bool abstract_ok;
21514 bool pack_expansion_p = false;
21515 cp_token *declarator_id_start_token;
21516
21517 /* Parse a declarator-id */
21518 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
21519 if (abstract_ok)
21520 {
21521 cp_parser_parse_tentatively (parser);
21522
21523 /* If we see an ellipsis, we should be looking at a
21524 parameter pack. */
21525 if (token->type == CPP_ELLIPSIS)
21526 {
21527 /* Consume the `...' */
21528 cp_lexer_consume_token (parser->lexer);
21529
21530 pack_expansion_p = true;
21531 }
21532 }
21533
21534 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
21535 unqualified_name
21536 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
21537 qualifying_scope = parser->scope;
21538 if (abstract_ok)
21539 {
21540 bool okay = false;
21541
21542 if (!unqualified_name && pack_expansion_p)
21543 {
21544 /* Check whether an error occurred. */
21545 okay = !cp_parser_error_occurred (parser);
21546
21547 /* We already consumed the ellipsis to mark a
21548 parameter pack, but we have no way to report it,
21549 so abort the tentative parse. We will be exiting
21550 immediately anyway. */
21551 cp_parser_abort_tentative_parse (parser);
21552 }
21553 else
21554 okay = cp_parser_parse_definitely (parser);
21555
21556 if (!okay)
21557 unqualified_name = error_mark_node;
21558 else if (unqualified_name
21559 && (qualifying_scope
21560 || (!identifier_p (unqualified_name))))
21561 {
21562 cp_parser_error (parser, "expected unqualified-id");
21563 unqualified_name = error_mark_node;
21564 }
21565 }
21566
21567 if (!unqualified_name)
21568 return NULL;
21569 if (unqualified_name == error_mark_node)
21570 {
21571 declarator = cp_error_declarator;
21572 pack_expansion_p = false;
21573 declarator->parameter_pack_p = false;
21574 break;
21575 }
21576
21577 attrs = cp_parser_std_attribute_spec_seq (parser);
21578
21579 if (qualifying_scope && at_namespace_scope_p ()
21580 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
21581 {
21582 /* In the declaration of a member of a template class
21583 outside of the class itself, the SCOPE will sometimes
21584 be a TYPENAME_TYPE. For example, given:
21585
21586 template <typename T>
21587 int S<T>::R::i = 3;
21588
21589 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
21590 this context, we must resolve S<T>::R to an ordinary
21591 type, rather than a typename type.
21592
21593 The reason we normally avoid resolving TYPENAME_TYPEs
21594 is that a specialization of `S' might render
21595 `S<T>::R' not a type. However, if `S' is
21596 specialized, then this `i' will not be used, so there
21597 is no harm in resolving the types here. */
21598 tree type;
21599
21600 /* Resolve the TYPENAME_TYPE. */
21601 type = resolve_typename_type (qualifying_scope,
21602 /*only_current_p=*/false);
21603 /* If that failed, the declarator is invalid. */
21604 if (TREE_CODE (type) == TYPENAME_TYPE)
21605 {
21606 if (typedef_variant_p (type))
21607 error_at (declarator_id_start_token->location,
21608 "cannot define member of dependent typedef "
21609 "%qT", type);
21610 else
21611 error_at (declarator_id_start_token->location,
21612 "%<%T::%E%> is not a type",
21613 TYPE_CONTEXT (qualifying_scope),
21614 TYPE_IDENTIFIER (qualifying_scope));
21615 }
21616 qualifying_scope = type;
21617 }
21618
21619 sfk = sfk_none;
21620
21621 if (unqualified_name)
21622 {
21623 tree class_type;
21624
21625 if (qualifying_scope
21626 && CLASS_TYPE_P (qualifying_scope))
21627 class_type = qualifying_scope;
21628 else
21629 class_type = current_class_type;
21630
21631 if (TREE_CODE (unqualified_name) == TYPE_DECL)
21632 {
21633 tree name_type = TREE_TYPE (unqualified_name);
21634
21635 if (!class_type || !same_type_p (name_type, class_type))
21636 {
21637 /* We do not attempt to print the declarator
21638 here because we do not have enough
21639 information about its original syntactic
21640 form. */
21641 cp_parser_error (parser, "invalid declarator");
21642 declarator = cp_error_declarator;
21643 break;
21644 }
21645 else if (qualifying_scope
21646 && CLASSTYPE_USE_TEMPLATE (name_type))
21647 {
21648 error_at (declarator_id_start_token->location,
21649 "invalid use of constructor as a template");
21650 inform (declarator_id_start_token->location,
21651 "use %<%T::%D%> instead of %<%T::%D%> to "
21652 "name the constructor in a qualified name",
21653 class_type,
21654 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
21655 class_type, name_type);
21656 declarator = cp_error_declarator;
21657 break;
21658 }
21659 unqualified_name = constructor_name (class_type);
21660 }
21661
21662 if (class_type)
21663 {
21664 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
21665 sfk = sfk_destructor;
21666 else if (identifier_p (unqualified_name)
21667 && IDENTIFIER_CONV_OP_P (unqualified_name))
21668 sfk = sfk_conversion;
21669 else if (/* There's no way to declare a constructor
21670 for an unnamed type, even if the type
21671 got a name for linkage purposes. */
21672 !TYPE_WAS_UNNAMED (class_type)
21673 /* Handle correctly (c++/19200):
21674
21675 struct S {
21676 struct T{};
21677 friend void S(T);
21678 };
21679
21680 and also:
21681
21682 namespace N {
21683 void S();
21684 }
21685
21686 struct S {
21687 friend void N::S();
21688 }; */
21689 && (!friend_p || class_type == qualifying_scope)
21690 && constructor_name_p (unqualified_name,
21691 class_type))
21692 sfk = sfk_constructor;
21693 else if (is_overloaded_fn (unqualified_name)
21694 && DECL_CONSTRUCTOR_P (get_first_fn
21695 (unqualified_name)))
21696 sfk = sfk_constructor;
21697
21698 if (ctor_dtor_or_conv_p && sfk != sfk_none)
21699 *ctor_dtor_or_conv_p = -1;
21700 }
21701 }
21702 declarator = make_id_declarator (qualifying_scope,
21703 unqualified_name,
21704 sfk, token->location);
21705 declarator->std_attributes = attrs;
21706 declarator->parameter_pack_p = pack_expansion_p;
21707
21708 if (pack_expansion_p)
21709 maybe_warn_variadic_templates ();
21710
21711 /* We're looking for this case in [temp.res]:
21712 A qualified-id is assumed to name a type if [...]
21713 - it is a decl-specifier of the decl-specifier-seq of a
21714 parameter-declaration in a declarator of a function or
21715 function template declaration, ... */
21716 if (cxx_dialect >= cxx20
21717 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
21718 && declarator->kind == cdk_id
21719 && !at_class_scope_p ()
21720 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21721 {
21722 /* ...whose declarator-id is qualified. If it isn't, never
21723 assume the parameters to refer to types. */
21724 if (qualifying_scope == NULL_TREE)
21725 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21726 else
21727 {
21728 /* Now we have something like
21729 template <typename T> int C::x(S::p);
21730 which can be a function template declaration or a
21731 variable template definition. If name lookup for
21732 the declarator-id C::x finds one or more function
21733 templates, assume S::p to name a type. Otherwise,
21734 don't. */
21735 tree decl
21736 = cp_parser_lookup_name_simple (parser, unqualified_name,
21737 token->location);
21738 if (!is_overloaded_fn (decl)
21739 /* Allow
21740 template<typename T>
21741 A<T>::A(T::type) { } */
21742 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
21743 && constructor_name_p (unqualified_name,
21744 qualifying_scope)))
21745 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21746 }
21747 }
21748 }
21749
21750 handle_declarator:;
21751 scope = get_scope_of_declarator (declarator);
21752 if (scope)
21753 {
21754 /* Any names that appear after the declarator-id for a
21755 member are looked up in the containing scope. */
21756 if (at_function_scope_p ())
21757 {
21758 /* But declarations with qualified-ids can't appear in a
21759 function. */
21760 cp_parser_error (parser, "qualified-id in declaration");
21761 declarator = cp_error_declarator;
21762 break;
21763 }
21764 pushed_scope = push_scope (scope);
21765 }
21766 parser->in_declarator_p = true;
21767 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21768 || (declarator && declarator->kind == cdk_id))
21769 /* Default args are only allowed on function
21770 declarations. */
21771 parser->default_arg_ok_p = saved_default_arg_ok_p;
21772 else
21773 parser->default_arg_ok_p = false;
21774
21775 first = false;
21776 }
21777 /* We're done. */
21778 else
21779 break;
21780 }
21781
21782 /* For an abstract declarator, we might wind up with nothing at this
21783 point. That's an error; the declarator is not optional. */
21784 if (!declarator)
21785 cp_parser_error (parser, "expected declarator");
21786 else if (open_paren)
21787 {
21788 /* Record overly parenthesized declarator so we can give a
21789 diagnostic about confusing decl/expr disambiguation. */
21790 if (declarator->kind == cdk_array)
21791 {
21792 /* If the open and close parens are on different lines, this
21793 is probably a formatting thing, so ignore. */
21794 expanded_location open = expand_location (open_paren->location);
21795 expanded_location close = expand_location (close_paren->location);
21796 if (open.line != close.line || open.file != close.file)
21797 open_paren = NULL;
21798 }
21799 if (open_paren)
21800 declarator->parenthesized = open_paren->location;
21801 }
21802
21803 /* If we entered a scope, we must exit it now. */
21804 if (pushed_scope)
21805 pop_scope (pushed_scope);
21806
21807 parser->default_arg_ok_p = saved_default_arg_ok_p;
21808 parser->in_declarator_p = saved_in_declarator_p;
21809
21810 return declarator;
21811 }
21812
21813 /* Parse a ptr-operator.
21814
21815 ptr-operator:
21816 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21817 * cv-qualifier-seq [opt]
21818 &
21819 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21820 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21821
21822 GNU Extension:
21823
21824 ptr-operator:
21825 & cv-qualifier-seq [opt]
21826
21827 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21828 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21829 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21830 filled in with the TYPE containing the member. *CV_QUALS is
21831 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21832 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21833 Note that the tree codes returned by this function have nothing
21834 to do with the types of trees that will be eventually be created
21835 to represent the pointer or reference type being parsed. They are
21836 just constants with suggestive names. */
21837 static enum tree_code
21838 cp_parser_ptr_operator (cp_parser* parser,
21839 tree* type,
21840 cp_cv_quals *cv_quals,
21841 tree *attributes)
21842 {
21843 enum tree_code code = ERROR_MARK;
21844 cp_token *token;
21845 tree attrs = NULL_TREE;
21846
21847 /* Assume that it's not a pointer-to-member. */
21848 *type = NULL_TREE;
21849 /* And that there are no cv-qualifiers. */
21850 *cv_quals = TYPE_UNQUALIFIED;
21851
21852 /* Peek at the next token. */
21853 token = cp_lexer_peek_token (parser->lexer);
21854
21855 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21856 if (token->type == CPP_MULT)
21857 code = INDIRECT_REF;
21858 else if (token->type == CPP_AND)
21859 code = ADDR_EXPR;
21860 else if ((cxx_dialect != cxx98) &&
21861 token->type == CPP_AND_AND) /* C++0x only */
21862 code = NON_LVALUE_EXPR;
21863
21864 if (code != ERROR_MARK)
21865 {
21866 /* Consume the `*', `&' or `&&'. */
21867 cp_lexer_consume_token (parser->lexer);
21868
21869 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21870 `&', if we are allowing GNU extensions. (The only qualifier
21871 that can legally appear after `&' is `restrict', but that is
21872 enforced during semantic analysis. */
21873 if (code == INDIRECT_REF
21874 || cp_parser_allow_gnu_extensions_p (parser))
21875 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21876
21877 attrs = cp_parser_std_attribute_spec_seq (parser);
21878 if (attributes != NULL)
21879 *attributes = attrs;
21880 }
21881 else
21882 {
21883 /* Try the pointer-to-member case. */
21884 cp_parser_parse_tentatively (parser);
21885 /* Look for the optional `::' operator. */
21886 cp_parser_global_scope_opt (parser,
21887 /*current_scope_valid_p=*/false);
21888 /* Look for the nested-name specifier. */
21889 token = cp_lexer_peek_token (parser->lexer);
21890 cp_parser_nested_name_specifier (parser,
21891 /*typename_keyword_p=*/false,
21892 /*check_dependency_p=*/true,
21893 /*type_p=*/false,
21894 /*is_declaration=*/false);
21895 /* If we found it, and the next token is a `*', then we are
21896 indeed looking at a pointer-to-member operator. */
21897 if (!cp_parser_error_occurred (parser)
21898 && cp_parser_require (parser, CPP_MULT, RT_MULT))
21899 {
21900 /* Indicate that the `*' operator was used. */
21901 code = INDIRECT_REF;
21902
21903 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21904 error_at (token->location, "%qD is a namespace", parser->scope);
21905 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21906 error_at (token->location, "cannot form pointer to member of "
21907 "non-class %q#T", parser->scope);
21908 else
21909 {
21910 /* The type of which the member is a member is given by the
21911 current SCOPE. */
21912 *type = parser->scope;
21913 /* The next name will not be qualified. */
21914 parser->scope = NULL_TREE;
21915 parser->qualifying_scope = NULL_TREE;
21916 parser->object_scope = NULL_TREE;
21917 /* Look for optional c++11 attributes. */
21918 attrs = cp_parser_std_attribute_spec_seq (parser);
21919 if (attributes != NULL)
21920 *attributes = attrs;
21921 /* Look for the optional cv-qualifier-seq. */
21922 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21923 }
21924 }
21925 /* If that didn't work we don't have a ptr-operator. */
21926 if (!cp_parser_parse_definitely (parser))
21927 cp_parser_error (parser, "expected ptr-operator");
21928 }
21929
21930 return code;
21931 }
21932
21933 /* Parse an (optional) cv-qualifier-seq.
21934
21935 cv-qualifier-seq:
21936 cv-qualifier cv-qualifier-seq [opt]
21937
21938 cv-qualifier:
21939 const
21940 volatile
21941
21942 GNU Extension:
21943
21944 cv-qualifier:
21945 __restrict__
21946
21947 Returns a bitmask representing the cv-qualifiers. */
21948
21949 static cp_cv_quals
21950 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21951 {
21952 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21953
21954 while (true)
21955 {
21956 cp_token *token;
21957 cp_cv_quals cv_qualifier;
21958
21959 /* Peek at the next token. */
21960 token = cp_lexer_peek_token (parser->lexer);
21961 /* See if it's a cv-qualifier. */
21962 switch (token->keyword)
21963 {
21964 case RID_CONST:
21965 cv_qualifier = TYPE_QUAL_CONST;
21966 break;
21967
21968 case RID_VOLATILE:
21969 cv_qualifier = TYPE_QUAL_VOLATILE;
21970 break;
21971
21972 case RID_RESTRICT:
21973 cv_qualifier = TYPE_QUAL_RESTRICT;
21974 break;
21975
21976 default:
21977 cv_qualifier = TYPE_UNQUALIFIED;
21978 break;
21979 }
21980
21981 if (!cv_qualifier)
21982 break;
21983
21984 if (cv_quals & cv_qualifier)
21985 {
21986 gcc_rich_location richloc (token->location);
21987 richloc.add_fixit_remove ();
21988 error_at (&richloc, "duplicate cv-qualifier");
21989 cp_lexer_purge_token (parser->lexer);
21990 }
21991 else
21992 {
21993 cp_lexer_consume_token (parser->lexer);
21994 cv_quals |= cv_qualifier;
21995 }
21996 }
21997
21998 return cv_quals;
21999 }
22000
22001 /* Parse an (optional) ref-qualifier
22002
22003 ref-qualifier:
22004 &
22005 &&
22006
22007 Returns cp_ref_qualifier representing ref-qualifier. */
22008
22009 static cp_ref_qualifier
22010 cp_parser_ref_qualifier_opt (cp_parser* parser)
22011 {
22012 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
22013
22014 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
22015 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
22016 return ref_qual;
22017
22018 while (true)
22019 {
22020 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
22021 cp_token *token = cp_lexer_peek_token (parser->lexer);
22022
22023 switch (token->type)
22024 {
22025 case CPP_AND:
22026 curr_ref_qual = REF_QUAL_LVALUE;
22027 break;
22028
22029 case CPP_AND_AND:
22030 curr_ref_qual = REF_QUAL_RVALUE;
22031 break;
22032
22033 default:
22034 curr_ref_qual = REF_QUAL_NONE;
22035 break;
22036 }
22037
22038 if (!curr_ref_qual)
22039 break;
22040 else if (ref_qual)
22041 {
22042 error_at (token->location, "multiple ref-qualifiers");
22043 cp_lexer_purge_token (parser->lexer);
22044 }
22045 else
22046 {
22047 ref_qual = curr_ref_qual;
22048 cp_lexer_consume_token (parser->lexer);
22049 }
22050 }
22051
22052 return ref_qual;
22053 }
22054
22055 /* Parse an optional tx-qualifier.
22056
22057 tx-qualifier:
22058 transaction_safe
22059 transaction_safe_dynamic */
22060
22061 static tree
22062 cp_parser_tx_qualifier_opt (cp_parser *parser)
22063 {
22064 cp_token *token = cp_lexer_peek_token (parser->lexer);
22065 if (token->type == CPP_NAME)
22066 {
22067 tree name = token->u.value;
22068 const char *p = IDENTIFIER_POINTER (name);
22069 const int len = strlen ("transaction_safe");
22070 if (!strncmp (p, "transaction_safe", len))
22071 {
22072 p += len;
22073 if (*p == '\0'
22074 || !strcmp (p, "_dynamic"))
22075 {
22076 cp_lexer_consume_token (parser->lexer);
22077 if (!flag_tm)
22078 {
22079 error ("%qE requires %<-fgnu-tm%>", name);
22080 return NULL_TREE;
22081 }
22082 else
22083 return name;
22084 }
22085 }
22086 }
22087 return NULL_TREE;
22088 }
22089
22090 /* Parse an (optional) virt-specifier-seq.
22091
22092 virt-specifier-seq:
22093 virt-specifier virt-specifier-seq [opt]
22094
22095 virt-specifier:
22096 override
22097 final
22098
22099 Returns a bitmask representing the virt-specifiers. */
22100
22101 static cp_virt_specifiers
22102 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
22103 {
22104 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22105
22106 while (true)
22107 {
22108 cp_token *token;
22109 cp_virt_specifiers virt_specifier;
22110
22111 /* Peek at the next token. */
22112 token = cp_lexer_peek_token (parser->lexer);
22113 /* See if it's a virt-specifier-qualifier. */
22114 if (token->type != CPP_NAME)
22115 break;
22116 if (id_equal (token->u.value, "override"))
22117 {
22118 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
22119 virt_specifier = VIRT_SPEC_OVERRIDE;
22120 }
22121 else if (id_equal (token->u.value, "final"))
22122 {
22123 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
22124 virt_specifier = VIRT_SPEC_FINAL;
22125 }
22126 else if (id_equal (token->u.value, "__final"))
22127 {
22128 virt_specifier = VIRT_SPEC_FINAL;
22129 }
22130 else
22131 break;
22132
22133 if (virt_specifiers & virt_specifier)
22134 {
22135 gcc_rich_location richloc (token->location);
22136 richloc.add_fixit_remove ();
22137 error_at (&richloc, "duplicate virt-specifier");
22138 cp_lexer_purge_token (parser->lexer);
22139 }
22140 else
22141 {
22142 cp_lexer_consume_token (parser->lexer);
22143 virt_specifiers |= virt_specifier;
22144 }
22145 }
22146 return virt_specifiers;
22147 }
22148
22149 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
22150 is in scope even though it isn't real. */
22151
22152 void
22153 inject_this_parameter (tree ctype, cp_cv_quals quals)
22154 {
22155 tree this_parm;
22156
22157 if (current_class_ptr)
22158 {
22159 /* We don't clear this between NSDMIs. Is it already what we want? */
22160 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
22161 if (DECL_P (current_class_ptr)
22162 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
22163 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
22164 && cp_type_quals (type) == quals)
22165 return;
22166 }
22167
22168 this_parm = build_this_parm (NULL_TREE, ctype, quals);
22169 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
22170 current_class_ptr = NULL_TREE;
22171 current_class_ref
22172 = cp_build_fold_indirect_ref (this_parm);
22173 current_class_ptr = this_parm;
22174 }
22175
22176 /* Return true iff our current scope is a non-static data member
22177 initializer. */
22178
22179 bool
22180 parsing_nsdmi (void)
22181 {
22182 /* We recognize NSDMI context by the context-less 'this' pointer set up
22183 by the function above. */
22184 if (current_class_ptr
22185 && TREE_CODE (current_class_ptr) == PARM_DECL
22186 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
22187 return true;
22188 return false;
22189 }
22190
22191 /* Parse a late-specified return type, if any. This is not a separate
22192 non-terminal, but part of a function declarator, which looks like
22193
22194 -> trailing-type-specifier-seq abstract-declarator(opt)
22195
22196 Returns the type indicated by the type-id.
22197
22198 In addition to this, parse any queued up #pragma omp declare simd
22199 clauses, and #pragma acc routine clauses.
22200
22201 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
22202 function. */
22203
22204 static tree
22205 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
22206 tree& requires_clause)
22207 {
22208 cp_token *token;
22209 tree type = NULL_TREE;
22210 bool declare_simd_p = (parser->omp_declare_simd
22211 && declarator
22212 && declarator->kind == cdk_id);
22213
22214 bool oacc_routine_p = (parser->oacc_routine
22215 && declarator
22216 && declarator->kind == cdk_id);
22217
22218 /* Peek at the next token. */
22219 token = cp_lexer_peek_token (parser->lexer);
22220 /* A late-specified return type is indicated by an initial '->'. */
22221 if (token->type != CPP_DEREF
22222 && token->keyword != RID_REQUIRES
22223 && !(token->type == CPP_NAME
22224 && token->u.value == ridpointers[RID_REQUIRES])
22225 && !(declare_simd_p || oacc_routine_p))
22226 return NULL_TREE;
22227
22228 if (token->type == CPP_DEREF)
22229 {
22230 /* Consume the ->. */
22231 cp_lexer_consume_token (parser->lexer);
22232
22233 type = cp_parser_trailing_type_id (parser);
22234 }
22235
22236 /* Function declarations may be followed by a trailing
22237 requires-clause. */
22238 requires_clause = cp_parser_requires_clause_opt (parser, false);
22239
22240 if (declare_simd_p)
22241 declarator->attributes
22242 = cp_parser_late_parsing_omp_declare_simd (parser,
22243 declarator->attributes);
22244 if (oacc_routine_p)
22245 declarator->attributes
22246 = cp_parser_late_parsing_oacc_routine (parser,
22247 declarator->attributes);
22248
22249 return type;
22250 }
22251
22252 /* Parse a declarator-id.
22253
22254 declarator-id:
22255 id-expression
22256 :: [opt] nested-name-specifier [opt] type-name
22257
22258 In the `id-expression' case, the value returned is as for
22259 cp_parser_id_expression if the id-expression was an unqualified-id.
22260 If the id-expression was a qualified-id, then a SCOPE_REF is
22261 returned. The first operand is the scope (either a NAMESPACE_DECL
22262 or TREE_TYPE), but the second is still just a representation of an
22263 unqualified-id. */
22264
22265 static tree
22266 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
22267 {
22268 tree id;
22269 /* The expression must be an id-expression. Assume that qualified
22270 names are the names of types so that:
22271
22272 template <class T>
22273 int S<T>::R::i = 3;
22274
22275 will work; we must treat `S<T>::R' as the name of a type.
22276 Similarly, assume that qualified names are templates, where
22277 required, so that:
22278
22279 template <class T>
22280 int S<T>::R<T>::i = 3;
22281
22282 will work, too. */
22283 id = cp_parser_id_expression (parser,
22284 /*template_keyword_p=*/false,
22285 /*check_dependency_p=*/false,
22286 /*template_p=*/NULL,
22287 /*declarator_p=*/true,
22288 optional_p);
22289 if (id && BASELINK_P (id))
22290 id = BASELINK_FUNCTIONS (id);
22291 return id;
22292 }
22293
22294 /* Parse a type-id.
22295
22296 type-id:
22297 type-specifier-seq abstract-declarator [opt]
22298
22299 The parser flags FLAGS is used to control type-specifier parsing.
22300
22301 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
22302
22303 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
22304 i.e. we've just seen "->".
22305
22306 Returns the TYPE specified. */
22307
22308 static tree
22309 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
22310 bool is_template_arg, bool is_trailing_return,
22311 location_t *type_location)
22312 {
22313 cp_decl_specifier_seq type_specifier_seq;
22314 cp_declarator *abstract_declarator;
22315
22316 /* Parse the type-specifier-seq. */
22317 cp_parser_type_specifier_seq (parser, flags,
22318 /*is_declaration=*/false,
22319 is_trailing_return,
22320 &type_specifier_seq);
22321 if (type_location)
22322 *type_location = type_specifier_seq.locations[ds_type_spec];
22323
22324 if (is_template_arg && type_specifier_seq.type
22325 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
22326 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
22327 /* A bare template name as a template argument is a template template
22328 argument, not a placeholder, so fail parsing it as a type argument. */
22329 {
22330 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
22331 cp_parser_simulate_error (parser);
22332 return error_mark_node;
22333 }
22334 if (type_specifier_seq.type == error_mark_node)
22335 return error_mark_node;
22336
22337 /* There might or might not be an abstract declarator. */
22338 cp_parser_parse_tentatively (parser);
22339 /* Look for the declarator. */
22340 abstract_declarator
22341 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
22342 CP_PARSER_FLAGS_NONE, NULL,
22343 /*parenthesized_p=*/NULL,
22344 /*member_p=*/false,
22345 /*friend_p=*/false,
22346 /*static_p=*/false);
22347 /* Check to see if there really was a declarator. */
22348 if (!cp_parser_parse_definitely (parser))
22349 abstract_declarator = NULL;
22350
22351 if (type_specifier_seq.type
22352 /* The concepts TS allows 'auto' as a type-id. */
22353 && (!flag_concepts || parser->in_type_id_in_expr_p)
22354 /* None of the valid uses of 'auto' in C++14 involve the type-id
22355 nonterminal, but it is valid in a trailing-return-type. */
22356 && !(cxx_dialect >= cxx14 && is_trailing_return))
22357 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
22358 {
22359 /* A type-id with type 'auto' is only ok if the abstract declarator
22360 is a function declarator with a late-specified return type.
22361
22362 A type-id with 'auto' is also valid in a trailing-return-type
22363 in a compound-requirement. */
22364 if (abstract_declarator
22365 && abstract_declarator->kind == cdk_function
22366 && abstract_declarator->u.function.late_return_type)
22367 /* OK */;
22368 else if (parser->in_result_type_constraint_p)
22369 /* OK */;
22370 else
22371 {
22372 location_t loc = type_specifier_seq.locations[ds_type_spec];
22373 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
22374 {
22375 error_at (loc, "missing template arguments after %qT",
22376 auto_node);
22377 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
22378 tmpl);
22379 }
22380 else
22381 error_at (loc, "invalid use of %qT", auto_node);
22382 return error_mark_node;
22383 }
22384 }
22385
22386 return groktypename (&type_specifier_seq, abstract_declarator,
22387 is_template_arg);
22388 }
22389
22390 /* Wrapper for cp_parser_type_id_1. */
22391
22392 static tree
22393 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
22394 location_t *type_location)
22395 {
22396 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
22397 }
22398
22399 /* Wrapper for cp_parser_type_id_1. */
22400
22401 static tree
22402 cp_parser_template_type_arg (cp_parser *parser)
22403 {
22404 tree r;
22405 const char *saved_message = parser->type_definition_forbidden_message;
22406 parser->type_definition_forbidden_message
22407 = G_("types may not be defined in template arguments");
22408 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
22409 parser->type_definition_forbidden_message = saved_message;
22410 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
22411 {
22412 error ("invalid use of %<auto%> in template argument");
22413 r = error_mark_node;
22414 }
22415 return r;
22416 }
22417
22418 /* Wrapper for cp_parser_type_id_1. */
22419
22420 static tree
22421 cp_parser_trailing_type_id (cp_parser *parser)
22422 {
22423 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
22424 false, true, NULL);
22425 }
22426
22427 /* Parse a type-specifier-seq.
22428
22429 type-specifier-seq:
22430 type-specifier type-specifier-seq [opt]
22431
22432 GNU extension:
22433
22434 type-specifier-seq:
22435 attributes type-specifier-seq [opt]
22436
22437 The parser flags FLAGS is used to control type-specifier parsing.
22438
22439 If IS_DECLARATION is true, we are at the start of a "condition" or
22440 exception-declaration, so we might be followed by a declarator-id.
22441
22442 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
22443 i.e. we've just seen "->".
22444
22445 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
22446
22447 static void
22448 cp_parser_type_specifier_seq (cp_parser* parser,
22449 cp_parser_flags flags,
22450 bool is_declaration,
22451 bool is_trailing_return,
22452 cp_decl_specifier_seq *type_specifier_seq)
22453 {
22454 bool seen_type_specifier = false;
22455 cp_token *start_token = NULL;
22456
22457 /* Clear the TYPE_SPECIFIER_SEQ. */
22458 clear_decl_specs (type_specifier_seq);
22459
22460 flags |= CP_PARSER_FLAGS_OPTIONAL;
22461 /* In the context of a trailing return type, enum E { } is an
22462 elaborated-type-specifier followed by a function-body, not an
22463 enum-specifier. */
22464 if (is_trailing_return)
22465 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
22466
22467 /* Parse the type-specifiers and attributes. */
22468 while (true)
22469 {
22470 tree type_specifier;
22471 bool is_cv_qualifier;
22472
22473 /* Check for attributes first. */
22474 if (cp_next_tokens_can_be_attribute_p (parser))
22475 {
22476 /* GNU attributes at the end of a declaration apply to the
22477 declaration as a whole, not to the trailing return type. So look
22478 ahead to see if these attributes are at the end. */
22479 if (seen_type_specifier && is_trailing_return
22480 && cp_next_tokens_can_be_gnu_attribute_p (parser))
22481 {
22482 size_t n = cp_parser_skip_attributes_opt (parser, 1);
22483 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
22484 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
22485 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
22486 break;
22487 }
22488 type_specifier_seq->attributes
22489 = attr_chainon (type_specifier_seq->attributes,
22490 cp_parser_attributes_opt (parser));
22491 continue;
22492 }
22493
22494 /* record the token of the beginning of the type specifier seq,
22495 for error reporting purposes*/
22496 if (!start_token)
22497 start_token = cp_lexer_peek_token (parser->lexer);
22498
22499 /* Look for the type-specifier. */
22500 type_specifier = cp_parser_type_specifier (parser,
22501 flags,
22502 type_specifier_seq,
22503 /*is_declaration=*/false,
22504 NULL,
22505 &is_cv_qualifier);
22506 if (!type_specifier)
22507 {
22508 /* If the first type-specifier could not be found, this is not a
22509 type-specifier-seq at all. */
22510 if (!seen_type_specifier)
22511 {
22512 /* Set in_declarator_p to avoid skipping to the semicolon. */
22513 int in_decl = parser->in_declarator_p;
22514 parser->in_declarator_p = true;
22515
22516 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
22517 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
22518 cp_parser_error (parser, "expected type-specifier");
22519
22520 parser->in_declarator_p = in_decl;
22521
22522 type_specifier_seq->type = error_mark_node;
22523 return;
22524 }
22525 /* If subsequent type-specifiers could not be found, the
22526 type-specifier-seq is complete. */
22527 break;
22528 }
22529
22530 seen_type_specifier = true;
22531 /* The standard says that a condition can be:
22532
22533 type-specifier-seq declarator = assignment-expression
22534
22535 However, given:
22536
22537 struct S {};
22538 if (int S = ...)
22539
22540 we should treat the "S" as a declarator, not as a
22541 type-specifier. The standard doesn't say that explicitly for
22542 type-specifier-seq, but it does say that for
22543 decl-specifier-seq in an ordinary declaration. Perhaps it
22544 would be clearer just to allow a decl-specifier-seq here, and
22545 then add a semantic restriction that if any decl-specifiers
22546 that are not type-specifiers appear, the program is invalid. */
22547 if (is_declaration && !is_cv_qualifier)
22548 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
22549 }
22550 }
22551
22552 /* Return whether the function currently being declared has an associated
22553 template parameter list. */
22554
22555 static bool
22556 function_being_declared_is_template_p (cp_parser* parser)
22557 {
22558 if (!current_template_parms || processing_template_parmlist)
22559 return false;
22560
22561 if (parser->implicit_template_scope)
22562 return true;
22563
22564 if (at_class_scope_p ()
22565 && TYPE_BEING_DEFINED (current_class_type))
22566 return parser->num_template_parameter_lists != 0;
22567
22568 return ((int) parser->num_template_parameter_lists > template_class_depth
22569 (current_class_type));
22570 }
22571
22572 /* Parse a parameter-declaration-clause.
22573
22574 parameter-declaration-clause:
22575 parameter-declaration-list [opt] ... [opt]
22576 parameter-declaration-list , ...
22577
22578 The parser flags FLAGS is used to control type-specifier parsing.
22579
22580 Returns a representation for the parameter declarations. A return
22581 value of NULL indicates a parameter-declaration-clause consisting
22582 only of an ellipsis. */
22583
22584 static tree
22585 cp_parser_parameter_declaration_clause (cp_parser* parser,
22586 cp_parser_flags flags)
22587 {
22588 tree parameters;
22589 cp_token *token;
22590 bool ellipsis_p;
22591
22592 temp_override<bool> cleanup
22593 (parser->auto_is_implicit_function_template_parm_p);
22594
22595 if (!processing_specialization
22596 && !processing_template_parmlist
22597 && !processing_explicit_instantiation
22598 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22599 actual function or a random abstract declarator. */
22600 && parser->default_arg_ok_p)
22601 if (!current_function_decl
22602 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
22603 parser->auto_is_implicit_function_template_parm_p = true;
22604
22605 /* Peek at the next token. */
22606 token = cp_lexer_peek_token (parser->lexer);
22607 /* Check for trivial parameter-declaration-clauses. */
22608 if (token->type == CPP_ELLIPSIS)
22609 {
22610 /* Consume the `...' token. */
22611 cp_lexer_consume_token (parser->lexer);
22612 return NULL_TREE;
22613 }
22614 else if (token->type == CPP_CLOSE_PAREN)
22615 /* There are no parameters. */
22616 return void_list_node;
22617 /* Check for `(void)', too, which is a special case. */
22618 else if (token->keyword == RID_VOID
22619 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22620 == CPP_CLOSE_PAREN))
22621 {
22622 /* Consume the `void' token. */
22623 cp_lexer_consume_token (parser->lexer);
22624 /* There are no parameters. */
22625 return void_list_node;
22626 }
22627
22628 /* Parse the parameter-declaration-list. */
22629 parameters = cp_parser_parameter_declaration_list (parser, flags);
22630 /* If a parse error occurred while parsing the
22631 parameter-declaration-list, then the entire
22632 parameter-declaration-clause is erroneous. */
22633 if (parameters == error_mark_node)
22634 return NULL_TREE;
22635
22636 /* Peek at the next token. */
22637 token = cp_lexer_peek_token (parser->lexer);
22638 /* If it's a `,', the clause should terminate with an ellipsis. */
22639 if (token->type == CPP_COMMA)
22640 {
22641 /* Consume the `,'. */
22642 cp_lexer_consume_token (parser->lexer);
22643 /* Expect an ellipsis. */
22644 ellipsis_p
22645 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
22646 }
22647 /* It might also be `...' if the optional trailing `,' was
22648 omitted. */
22649 else if (token->type == CPP_ELLIPSIS)
22650 {
22651 /* Consume the `...' token. */
22652 cp_lexer_consume_token (parser->lexer);
22653 /* And remember that we saw it. */
22654 ellipsis_p = true;
22655 }
22656 else
22657 ellipsis_p = false;
22658
22659 /* Finish the parameter list. */
22660 if (!ellipsis_p)
22661 parameters = chainon (parameters, void_list_node);
22662
22663 return parameters;
22664 }
22665
22666 /* Parse a parameter-declaration-list.
22667
22668 parameter-declaration-list:
22669 parameter-declaration
22670 parameter-declaration-list , parameter-declaration
22671
22672 The parser flags FLAGS is used to control type-specifier parsing.
22673
22674 Returns a representation of the parameter-declaration-list, as for
22675 cp_parser_parameter_declaration_clause. However, the
22676 `void_list_node' is never appended to the list. */
22677
22678 static tree
22679 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
22680 {
22681 tree parameters = NULL_TREE;
22682 tree *tail = &parameters;
22683 bool saved_in_unbraced_linkage_specification_p;
22684 int index = 0;
22685
22686 /* The special considerations that apply to a function within an
22687 unbraced linkage specifications do not apply to the parameters
22688 to the function. */
22689 saved_in_unbraced_linkage_specification_p
22690 = parser->in_unbraced_linkage_specification_p;
22691 parser->in_unbraced_linkage_specification_p = false;
22692
22693 /* Look for more parameters. */
22694 while (true)
22695 {
22696 cp_parameter_declarator *parameter;
22697 tree decl = error_mark_node;
22698 bool parenthesized_p = false;
22699
22700 /* Parse the parameter. */
22701 parameter
22702 = cp_parser_parameter_declaration (parser, flags,
22703 /*template_parm_p=*/false,
22704 &parenthesized_p);
22705
22706 /* We don't know yet if the enclosing context is deprecated, so wait
22707 and warn in grokparms if appropriate. */
22708 deprecated_state = DEPRECATED_SUPPRESS;
22709
22710 if (parameter)
22711 {
22712 decl = grokdeclarator (parameter->declarator,
22713 &parameter->decl_specifiers,
22714 PARM,
22715 parameter->default_argument != NULL_TREE,
22716 &parameter->decl_specifiers.attributes);
22717 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
22718 DECL_SOURCE_LOCATION (decl) = parameter->loc;
22719 }
22720
22721 deprecated_state = DEPRECATED_NORMAL;
22722
22723 /* If a parse error occurred parsing the parameter declaration,
22724 then the entire parameter-declaration-list is erroneous. */
22725 if (decl == error_mark_node)
22726 {
22727 parameters = error_mark_node;
22728 break;
22729 }
22730
22731 if (parameter->decl_specifiers.attributes)
22732 cplus_decl_attributes (&decl,
22733 parameter->decl_specifiers.attributes,
22734 0);
22735 if (DECL_NAME (decl))
22736 decl = pushdecl (decl);
22737
22738 if (decl != error_mark_node)
22739 {
22740 retrofit_lang_decl (decl);
22741 DECL_PARM_INDEX (decl) = ++index;
22742 DECL_PARM_LEVEL (decl) = function_parm_depth ();
22743 }
22744
22745 /* Add the new parameter to the list. */
22746 *tail = build_tree_list (parameter->default_argument, decl);
22747 tail = &TREE_CHAIN (*tail);
22748
22749 /* Peek at the next token. */
22750 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22751 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22752 /* These are for Objective-C++ */
22753 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22754 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22755 /* The parameter-declaration-list is complete. */
22756 break;
22757 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22758 {
22759 cp_token *token;
22760
22761 /* Peek at the next token. */
22762 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22763 /* If it's an ellipsis, then the list is complete. */
22764 if (token->type == CPP_ELLIPSIS)
22765 break;
22766 /* Otherwise, there must be more parameters. Consume the
22767 `,'. */
22768 cp_lexer_consume_token (parser->lexer);
22769 /* When parsing something like:
22770
22771 int i(float f, double d)
22772
22773 we can tell after seeing the declaration for "f" that we
22774 are not looking at an initialization of a variable "i",
22775 but rather at the declaration of a function "i".
22776
22777 Due to the fact that the parsing of template arguments
22778 (as specified to a template-id) requires backtracking we
22779 cannot use this technique when inside a template argument
22780 list. */
22781 if (!parser->in_template_argument_list_p
22782 && !parser->in_type_id_in_expr_p
22783 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22784 /* However, a parameter-declaration of the form
22785 "float(f)" (which is a valid declaration of a
22786 parameter "f") can also be interpreted as an
22787 expression (the conversion of "f" to "float"). */
22788 && !parenthesized_p)
22789 cp_parser_commit_to_tentative_parse (parser);
22790 }
22791 else
22792 {
22793 cp_parser_error (parser, "expected %<,%> or %<...%>");
22794 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22795 cp_parser_skip_to_closing_parenthesis (parser,
22796 /*recovering=*/true,
22797 /*or_comma=*/false,
22798 /*consume_paren=*/false);
22799 break;
22800 }
22801 }
22802
22803 parser->in_unbraced_linkage_specification_p
22804 = saved_in_unbraced_linkage_specification_p;
22805
22806 /* Reset implicit_template_scope if we are about to leave the function
22807 parameter list that introduced it. Note that for out-of-line member
22808 definitions, there will be one or more class scopes before we get to
22809 the template parameter scope. */
22810
22811 if (cp_binding_level *its = parser->implicit_template_scope)
22812 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
22813 {
22814 while (maybe_its->kind == sk_class)
22815 maybe_its = maybe_its->level_chain;
22816 if (maybe_its == its)
22817 {
22818 parser->implicit_template_parms = 0;
22819 parser->implicit_template_scope = 0;
22820 }
22821 }
22822
22823 return parameters;
22824 }
22825
22826 /* Parse a parameter declaration.
22827
22828 parameter-declaration:
22829 decl-specifier-seq ... [opt] declarator
22830 decl-specifier-seq declarator = assignment-expression
22831 decl-specifier-seq ... [opt] abstract-declarator [opt]
22832 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22833
22834 The parser flags FLAGS is used to control type-specifier parsing.
22835
22836 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22837 declares a template parameter. (In that case, a non-nested `>'
22838 token encountered during the parsing of the assignment-expression
22839 is not interpreted as a greater-than operator.)
22840
22841 Returns a representation of the parameter, or NULL if an error
22842 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22843 true iff the declarator is of the form "(p)". */
22844
22845 static cp_parameter_declarator *
22846 cp_parser_parameter_declaration (cp_parser *parser,
22847 cp_parser_flags flags,
22848 bool template_parm_p,
22849 bool *parenthesized_p)
22850 {
22851 int declares_class_or_enum;
22852 cp_decl_specifier_seq decl_specifiers;
22853 cp_declarator *declarator;
22854 tree default_argument;
22855 cp_token *token = NULL, *declarator_token_start = NULL;
22856 const char *saved_message;
22857 bool template_parameter_pack_p = false;
22858
22859 /* In a template parameter, `>' is not an operator.
22860
22861 [temp.param]
22862
22863 When parsing a default template-argument for a non-type
22864 template-parameter, the first non-nested `>' is taken as the end
22865 of the template parameter-list rather than a greater-than
22866 operator. */
22867
22868 /* Type definitions may not appear in parameter types. */
22869 saved_message = parser->type_definition_forbidden_message;
22870 parser->type_definition_forbidden_message
22871 = G_("types may not be defined in parameter types");
22872
22873 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
22874 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22875 (current_template_parms)) : 0);
22876
22877 /* Parse the declaration-specifiers. */
22878 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22879 cp_parser_decl_specifier_seq (parser,
22880 flags,
22881 &decl_specifiers,
22882 &declares_class_or_enum);
22883
22884 /* Complain about missing 'typename' or other invalid type names. */
22885 if (!decl_specifiers.any_type_specifiers_p
22886 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22887 decl_specifiers.type = error_mark_node;
22888
22889 /* If an error occurred, there's no reason to attempt to parse the
22890 rest of the declaration. */
22891 if (cp_parser_error_occurred (parser))
22892 {
22893 parser->type_definition_forbidden_message = saved_message;
22894 return NULL;
22895 }
22896
22897 /* Peek at the next token. */
22898 token = cp_lexer_peek_token (parser->lexer);
22899
22900 /* If the next token is a `)', `,', `=', `>', or `...', then there
22901 is no declarator. However, when variadic templates are enabled,
22902 there may be a declarator following `...'. */
22903 if (token->type == CPP_CLOSE_PAREN
22904 || token->type == CPP_COMMA
22905 || token->type == CPP_EQ
22906 || token->type == CPP_GREATER)
22907 {
22908 declarator = NULL;
22909 if (parenthesized_p)
22910 *parenthesized_p = false;
22911 }
22912 /* Otherwise, there should be a declarator. */
22913 else
22914 {
22915 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22916 parser->default_arg_ok_p = false;
22917
22918 /* After seeing a decl-specifier-seq, if the next token is not a
22919 "(", there is no possibility that the code is a valid
22920 expression. Therefore, if parsing tentatively, we commit at
22921 this point. */
22922 if (!parser->in_template_argument_list_p
22923 /* In an expression context, having seen:
22924
22925 (int((char ...
22926
22927 we cannot be sure whether we are looking at a
22928 function-type (taking a "char" as a parameter) or a cast
22929 of some object of type "char" to "int". */
22930 && !parser->in_type_id_in_expr_p
22931 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22932 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22933 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22934 cp_parser_commit_to_tentative_parse (parser);
22935 /* Parse the declarator. */
22936 declarator_token_start = token;
22937 declarator = cp_parser_declarator (parser,
22938 CP_PARSER_DECLARATOR_EITHER,
22939 CP_PARSER_FLAGS_NONE,
22940 /*ctor_dtor_or_conv_p=*/NULL,
22941 parenthesized_p,
22942 /*member_p=*/false,
22943 /*friend_p=*/false,
22944 /*static_p=*/false);
22945 parser->default_arg_ok_p = saved_default_arg_ok_p;
22946 /* After the declarator, allow more attributes. */
22947 decl_specifiers.attributes
22948 = attr_chainon (decl_specifiers.attributes,
22949 cp_parser_attributes_opt (parser));
22950
22951 /* If the declarator is a template parameter pack, remember that and
22952 clear the flag in the declarator itself so we don't get errors
22953 from grokdeclarator. */
22954 if (template_parm_p && declarator && declarator->parameter_pack_p)
22955 {
22956 declarator->parameter_pack_p = false;
22957 template_parameter_pack_p = true;
22958 }
22959 }
22960
22961 /* If the next token is an ellipsis, and we have not seen a declarator
22962 name, and if either the type of the declarator contains parameter
22963 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22964 for, eg, abbreviated integral type names), then we actually have a
22965 parameter pack expansion expression. Otherwise, leave the ellipsis
22966 for a C-style variadic function. */
22967 token = cp_lexer_peek_token (parser->lexer);
22968
22969 /* If a function parameter pack was specified and an implicit template
22970 parameter was introduced during cp_parser_parameter_declaration,
22971 change any implicit parameters introduced into packs. */
22972 if (parser->implicit_template_parms
22973 && ((token->type == CPP_ELLIPSIS
22974 && declarator_can_be_parameter_pack (declarator))
22975 || (declarator && declarator->parameter_pack_p)))
22976 {
22977 int latest_template_parm_idx = TREE_VEC_LENGTH
22978 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
22979
22980 if (latest_template_parm_idx != template_parm_idx)
22981 decl_specifiers.type = convert_generic_types_to_packs
22982 (decl_specifiers.type,
22983 template_parm_idx, latest_template_parm_idx);
22984 }
22985
22986 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22987 {
22988 tree type = decl_specifiers.type;
22989
22990 if (type && DECL_P (type))
22991 type = TREE_TYPE (type);
22992
22993 if (((type
22994 && TREE_CODE (type) != TYPE_PACK_EXPANSION
22995 && (template_parm_p || uses_parameter_packs (type)))
22996 || (!type && template_parm_p))
22997 && declarator_can_be_parameter_pack (declarator))
22998 {
22999 /* Consume the `...'. */
23000 cp_lexer_consume_token (parser->lexer);
23001 maybe_warn_variadic_templates ();
23002
23003 /* Build a pack expansion type */
23004 if (template_parm_p)
23005 template_parameter_pack_p = true;
23006 else if (declarator)
23007 declarator->parameter_pack_p = true;
23008 else
23009 decl_specifiers.type = make_pack_expansion (type);
23010 }
23011 }
23012
23013 /* The restriction on defining new types applies only to the type
23014 of the parameter, not to the default argument. */
23015 parser->type_definition_forbidden_message = saved_message;
23016
23017 /* If the next token is `=', then process a default argument. */
23018 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23019 {
23020 tree type = decl_specifiers.type;
23021 token = cp_lexer_peek_token (parser->lexer);
23022 /* If we are defining a class, then the tokens that make up the
23023 default argument must be saved and processed later. */
23024 if (!template_parm_p && at_class_scope_p ()
23025 && TYPE_BEING_DEFINED (current_class_type)
23026 && !LAMBDA_TYPE_P (current_class_type))
23027 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
23028
23029 /* A constrained-type-specifier may declare a type
23030 template-parameter. */
23031 else if (declares_constrained_type_template_parameter (type))
23032 default_argument
23033 = cp_parser_default_type_template_argument (parser);
23034
23035 /* A constrained-type-specifier may declare a
23036 template-template-parameter. */
23037 else if (declares_constrained_template_template_parameter (type))
23038 default_argument
23039 = cp_parser_default_template_template_argument (parser);
23040
23041 /* Outside of a class definition, we can just parse the
23042 assignment-expression. */
23043 else
23044 default_argument
23045 = cp_parser_default_argument (parser, template_parm_p);
23046
23047 if (!parser->default_arg_ok_p)
23048 {
23049 permerror (token->location,
23050 "default arguments are only "
23051 "permitted for function parameters");
23052 }
23053 else if ((declarator && declarator->parameter_pack_p)
23054 || template_parameter_pack_p
23055 || (decl_specifiers.type
23056 && PACK_EXPANSION_P (decl_specifiers.type)))
23057 {
23058 /* Find the name of the parameter pack. */
23059 cp_declarator *id_declarator = declarator;
23060 while (id_declarator && id_declarator->kind != cdk_id)
23061 id_declarator = id_declarator->declarator;
23062
23063 if (id_declarator && id_declarator->kind == cdk_id)
23064 error_at (declarator_token_start->location,
23065 template_parm_p
23066 ? G_("template parameter pack %qD "
23067 "cannot have a default argument")
23068 : G_("parameter pack %qD cannot have "
23069 "a default argument"),
23070 id_declarator->u.id.unqualified_name);
23071 else
23072 error_at (declarator_token_start->location,
23073 template_parm_p
23074 ? G_("template parameter pack cannot have "
23075 "a default argument")
23076 : G_("parameter pack cannot have a "
23077 "default argument"));
23078
23079 default_argument = NULL_TREE;
23080 }
23081 }
23082 else
23083 default_argument = NULL_TREE;
23084
23085 if (default_argument)
23086 STRIP_ANY_LOCATION_WRAPPER (default_argument);
23087
23088 /* Generate a location for the parameter, ranging from the start of the
23089 initial token to the end of the final token (using input_location for
23090 the latter, set up by cp_lexer_set_source_position_from_token when
23091 consuming tokens).
23092
23093 If we have a identifier, then use it for the caret location, e.g.
23094
23095 extern int callee (int one, int (*two)(int, int), float three);
23096 ~~~~~~^~~~~~~~~~~~~~
23097
23098 otherwise, reuse the start location for the caret location e.g.:
23099
23100 extern int callee (int one, int (*)(int, int), float three);
23101 ^~~~~~~~~~~~~~~~~
23102
23103 */
23104 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
23105 ? declarator->id_loc
23106 : decl_spec_token_start->location);
23107 location_t param_loc = make_location (caret_loc,
23108 decl_spec_token_start->location,
23109 input_location);
23110
23111 return make_parameter_declarator (&decl_specifiers,
23112 declarator,
23113 default_argument,
23114 param_loc,
23115 template_parameter_pack_p);
23116 }
23117
23118 /* Parse a default argument and return it.
23119
23120 TEMPLATE_PARM_P is true if this is a default argument for a
23121 non-type template parameter. */
23122 static tree
23123 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
23124 {
23125 tree default_argument = NULL_TREE;
23126 bool saved_greater_than_is_operator_p;
23127 unsigned char saved_local_variables_forbidden_p;
23128 bool non_constant_p, is_direct_init;
23129
23130 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
23131 set correctly. */
23132 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
23133 parser->greater_than_is_operator_p = !template_parm_p;
23134 /* Local variable names (and the `this' keyword) may not
23135 appear in a default argument. */
23136 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23137 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
23138 /* Parse the assignment-expression. */
23139 if (template_parm_p)
23140 push_deferring_access_checks (dk_no_deferred);
23141 tree saved_class_ptr = NULL_TREE;
23142 tree saved_class_ref = NULL_TREE;
23143 /* The "this" pointer is not valid in a default argument. */
23144 if (cfun)
23145 {
23146 saved_class_ptr = current_class_ptr;
23147 cp_function_chain->x_current_class_ptr = NULL_TREE;
23148 saved_class_ref = current_class_ref;
23149 cp_function_chain->x_current_class_ref = NULL_TREE;
23150 }
23151 default_argument
23152 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
23153 /* Restore the "this" pointer. */
23154 if (cfun)
23155 {
23156 cp_function_chain->x_current_class_ptr = saved_class_ptr;
23157 cp_function_chain->x_current_class_ref = saved_class_ref;
23158 }
23159 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
23160 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23161 if (template_parm_p)
23162 pop_deferring_access_checks ();
23163 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
23164 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23165
23166 return default_argument;
23167 }
23168
23169 /* Parse a function-body.
23170
23171 function-body:
23172 compound_statement */
23173
23174 static void
23175 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
23176 {
23177 cp_parser_compound_statement (parser, NULL, (in_function_try_block
23178 ? BCS_TRY_BLOCK : BCS_NORMAL),
23179 true);
23180 }
23181
23182 /* Parse a ctor-initializer-opt followed by a function-body. Return
23183 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
23184 is true we are parsing a function-try-block. */
23185
23186 static void
23187 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
23188 bool in_function_try_block)
23189 {
23190 tree body, list;
23191 const bool check_body_p
23192 = (DECL_CONSTRUCTOR_P (current_function_decl)
23193 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
23194 tree last = NULL;
23195
23196 if (in_function_try_block
23197 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
23198 && cxx_dialect < cxx20)
23199 {
23200 if (DECL_CONSTRUCTOR_P (current_function_decl))
23201 pedwarn (input_location, 0,
23202 "function-try-block body of %<constexpr%> constructor only "
23203 "available with %<-std=c++20%> or %<-std=gnu++20%>");
23204 else
23205 pedwarn (input_location, 0,
23206 "function-try-block body of %<constexpr%> function only "
23207 "available with %<-std=c++20%> or %<-std=gnu++20%>");
23208 }
23209
23210 /* Begin the function body. */
23211 body = begin_function_body ();
23212 /* Parse the optional ctor-initializer. */
23213 cp_parser_ctor_initializer_opt (parser);
23214
23215 /* If we're parsing a constexpr constructor definition, we need
23216 to check that the constructor body is indeed empty. However,
23217 before we get to cp_parser_function_body lot of junk has been
23218 generated, so we can't just check that we have an empty block.
23219 Rather we take a snapshot of the outermost block, and check whether
23220 cp_parser_function_body changed its state. */
23221 if (check_body_p)
23222 {
23223 list = cur_stmt_list;
23224 if (STATEMENT_LIST_TAIL (list))
23225 last = STATEMENT_LIST_TAIL (list)->stmt;
23226 }
23227 /* Parse the function-body. */
23228 cp_parser_function_body (parser, in_function_try_block);
23229 if (check_body_p)
23230 check_constexpr_ctor_body (last, list, /*complain=*/true);
23231 /* Finish the function body. */
23232 finish_function_body (body);
23233 }
23234
23235 /* Parse an initializer.
23236
23237 initializer:
23238 = initializer-clause
23239 ( expression-list )
23240
23241 Returns an expression representing the initializer. If no
23242 initializer is present, NULL_TREE is returned.
23243
23244 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
23245 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
23246 set to TRUE if there is no initializer present. If there is an
23247 initializer, and it is not a constant-expression, *NON_CONSTANT_P
23248 is set to true; otherwise it is set to false. */
23249
23250 static tree
23251 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
23252 bool* non_constant_p, bool subexpression_p)
23253 {
23254 cp_token *token;
23255 tree init;
23256
23257 /* Peek at the next token. */
23258 token = cp_lexer_peek_token (parser->lexer);
23259
23260 /* Let our caller know whether or not this initializer was
23261 parenthesized. */
23262 *is_direct_init = (token->type != CPP_EQ);
23263 /* Assume that the initializer is constant. */
23264 *non_constant_p = false;
23265
23266 if (token->type == CPP_EQ)
23267 {
23268 /* Consume the `='. */
23269 cp_lexer_consume_token (parser->lexer);
23270 /* Parse the initializer-clause. */
23271 init = cp_parser_initializer_clause (parser, non_constant_p);
23272 }
23273 else if (token->type == CPP_OPEN_PAREN)
23274 {
23275 vec<tree, va_gc> *vec;
23276 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23277 /*cast_p=*/false,
23278 /*allow_expansion_p=*/true,
23279 non_constant_p);
23280 if (vec == NULL)
23281 return error_mark_node;
23282 init = build_tree_list_vec (vec);
23283 release_tree_vector (vec);
23284 }
23285 else if (token->type == CPP_OPEN_BRACE)
23286 {
23287 cp_lexer_set_source_position (parser->lexer);
23288 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23289 init = cp_parser_braced_list (parser, non_constant_p);
23290 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
23291 }
23292 else
23293 {
23294 /* Anything else is an error. */
23295 cp_parser_error (parser, "expected initializer");
23296 init = error_mark_node;
23297 }
23298
23299 if (!subexpression_p && check_for_bare_parameter_packs (init))
23300 init = error_mark_node;
23301
23302 return init;
23303 }
23304
23305 /* Parse an initializer-clause.
23306
23307 initializer-clause:
23308 assignment-expression
23309 braced-init-list
23310
23311 Returns an expression representing the initializer.
23312
23313 If the `assignment-expression' production is used the value
23314 returned is simply a representation for the expression.
23315
23316 Otherwise, calls cp_parser_braced_list. */
23317
23318 static cp_expr
23319 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
23320 {
23321 cp_expr initializer;
23322
23323 /* Assume the expression is constant. */
23324 *non_constant_p = false;
23325
23326 /* If it is not a `{', then we are looking at an
23327 assignment-expression. */
23328 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23329 {
23330 initializer
23331 = cp_parser_constant_expression (parser,
23332 /*allow_non_constant_p=*/true,
23333 non_constant_p);
23334 }
23335 else
23336 initializer = cp_parser_braced_list (parser, non_constant_p);
23337
23338 return initializer;
23339 }
23340
23341 /* Parse a brace-enclosed initializer list.
23342
23343 braced-init-list:
23344 { initializer-list , [opt] }
23345 { designated-initializer-list , [opt] }
23346 { }
23347
23348 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
23349 the elements of the initializer-list (or NULL, if the last
23350 production is used). The TREE_TYPE for the CONSTRUCTOR will be
23351 NULL_TREE. There is no way to detect whether or not the optional
23352 trailing `,' was provided. NON_CONSTANT_P is as for
23353 cp_parser_initializer. */
23354
23355 static cp_expr
23356 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
23357 {
23358 tree initializer;
23359 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
23360
23361 /* Consume the `{' token. */
23362 matching_braces braces;
23363 braces.require_open (parser);
23364 /* Create a CONSTRUCTOR to represent the braced-initializer. */
23365 initializer = make_node (CONSTRUCTOR);
23366 /* If it's not a `}', then there is a non-trivial initializer. */
23367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
23368 {
23369 bool designated;
23370 /* Parse the initializer list. */
23371 CONSTRUCTOR_ELTS (initializer)
23372 = cp_parser_initializer_list (parser, non_constant_p, &designated);
23373 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
23374 /* A trailing `,' token is allowed. */
23375 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23376 cp_lexer_consume_token (parser->lexer);
23377 }
23378 else
23379 *non_constant_p = false;
23380 /* Now, there should be a trailing `}'. */
23381 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
23382 braces.require_close (parser);
23383 TREE_TYPE (initializer) = init_list_type_node;
23384
23385 cp_expr result (initializer);
23386 /* Build a location of the form:
23387 { ... }
23388 ^~~~~~~
23389 with caret==start at the open brace, finish at the close brace. */
23390 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
23391 result.set_location (combined_loc);
23392 return result;
23393 }
23394
23395 /* Consume tokens up to, and including, the next non-nested closing `]'.
23396 Returns true iff we found a closing `]'. */
23397
23398 static bool
23399 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
23400 {
23401 unsigned square_depth = 0;
23402
23403 while (true)
23404 {
23405 cp_token * token = cp_lexer_peek_token (parser->lexer);
23406
23407 switch (token->type)
23408 {
23409 case CPP_PRAGMA_EOL:
23410 if (!parser->lexer->in_pragma)
23411 break;
23412 /* FALLTHRU */
23413 case CPP_EOF:
23414 /* If we've run out of tokens, then there is no closing `]'. */
23415 return false;
23416
23417 case CPP_OPEN_SQUARE:
23418 ++square_depth;
23419 break;
23420
23421 case CPP_CLOSE_SQUARE:
23422 if (!square_depth--)
23423 {
23424 cp_lexer_consume_token (parser->lexer);
23425 return true;
23426 }
23427 break;
23428
23429 default:
23430 break;
23431 }
23432
23433 /* Consume the token. */
23434 cp_lexer_consume_token (parser->lexer);
23435 }
23436 }
23437
23438 /* Return true if we are looking at an array-designator, false otherwise. */
23439
23440 static bool
23441 cp_parser_array_designator_p (cp_parser *parser)
23442 {
23443 /* Consume the `['. */
23444 cp_lexer_consume_token (parser->lexer);
23445
23446 cp_lexer_save_tokens (parser->lexer);
23447
23448 /* Skip tokens until the next token is a closing square bracket.
23449 If we find the closing `]', and the next token is a `=', then
23450 we are looking at an array designator. */
23451 bool array_designator_p
23452 = (cp_parser_skip_to_closing_square_bracket (parser)
23453 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
23454
23455 /* Roll back the tokens we skipped. */
23456 cp_lexer_rollback_tokens (parser->lexer);
23457
23458 return array_designator_p;
23459 }
23460
23461 /* Parse an initializer-list.
23462
23463 initializer-list:
23464 initializer-clause ... [opt]
23465 initializer-list , initializer-clause ... [opt]
23466
23467 C++20 Extension:
23468
23469 designated-initializer-list:
23470 designated-initializer-clause
23471 designated-initializer-list , designated-initializer-clause
23472
23473 designated-initializer-clause:
23474 designator brace-or-equal-initializer
23475
23476 designator:
23477 . identifier
23478
23479 GNU Extension:
23480
23481 initializer-list:
23482 designation initializer-clause ...[opt]
23483 initializer-list , designation initializer-clause ...[opt]
23484
23485 designation:
23486 . identifier =
23487 identifier :
23488 [ constant-expression ] =
23489
23490 Returns a vec of constructor_elt. The VALUE of each elt is an expression
23491 for the initializer. If the INDEX of the elt is non-NULL, it is the
23492 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
23493 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
23494 are any designators. */
23495
23496 static vec<constructor_elt, va_gc> *
23497 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
23498 bool *designated)
23499 {
23500 vec<constructor_elt, va_gc> *v = NULL;
23501 bool first_p = true;
23502 tree first_designator = NULL_TREE;
23503
23504 /* Assume all of the expressions are constant. */
23505 *non_constant_p = false;
23506
23507 unsigned nelts = 0;
23508 int suppress = suppress_location_wrappers;
23509
23510 /* Parse the rest of the list. */
23511 while (true)
23512 {
23513 cp_token *token;
23514 tree designator;
23515 tree initializer;
23516 bool clause_non_constant_p;
23517 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23518
23519 /* Handle the C++20 syntax, '. id ='. */
23520 if ((cxx_dialect >= cxx20
23521 || cp_parser_allow_gnu_extensions_p (parser))
23522 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
23523 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
23524 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
23525 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
23526 == CPP_OPEN_BRACE)))
23527 {
23528 if (cxx_dialect < cxx20)
23529 pedwarn (loc, OPT_Wpedantic,
23530 "C++ designated initializers only available with "
23531 "%<-std=c++20%> or %<-std=gnu++20%>");
23532 /* Consume the `.'. */
23533 cp_lexer_consume_token (parser->lexer);
23534 /* Consume the identifier. */
23535 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23536 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23537 /* Consume the `='. */
23538 cp_lexer_consume_token (parser->lexer);
23539 }
23540 /* Also, if the next token is an identifier and the following one is a
23541 colon, we are looking at the GNU designated-initializer
23542 syntax. */
23543 else if (cp_parser_allow_gnu_extensions_p (parser)
23544 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
23545 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23546 == CPP_COLON))
23547 {
23548 /* Warn the user that they are using an extension. */
23549 pedwarn (loc, OPT_Wpedantic,
23550 "ISO C++ does not allow GNU designated initializers");
23551 /* Consume the identifier. */
23552 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23553 /* Consume the `:'. */
23554 cp_lexer_consume_token (parser->lexer);
23555 }
23556 /* Also handle C99 array designators, '[ const ] ='. */
23557 else if (cp_parser_allow_gnu_extensions_p (parser)
23558 && !c_dialect_objc ()
23559 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23560 {
23561 /* In C++11, [ could start a lambda-introducer. */
23562 bool non_const = false;
23563
23564 cp_parser_parse_tentatively (parser);
23565
23566 if (!cp_parser_array_designator_p (parser))
23567 {
23568 cp_parser_simulate_error (parser);
23569 designator = NULL_TREE;
23570 }
23571 else
23572 {
23573 designator = cp_parser_constant_expression (parser, true,
23574 &non_const);
23575 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23576 cp_parser_require (parser, CPP_EQ, RT_EQ);
23577 }
23578
23579 if (!cp_parser_parse_definitely (parser))
23580 designator = NULL_TREE;
23581 else if (non_const
23582 && (!require_potential_rvalue_constant_expression
23583 (designator)))
23584 designator = NULL_TREE;
23585 if (designator)
23586 /* Warn the user that they are using an extension. */
23587 pedwarn (loc, OPT_Wpedantic,
23588 "ISO C++ does not allow C99 designated initializers");
23589 }
23590 else
23591 designator = NULL_TREE;
23592
23593 if (first_p)
23594 {
23595 first_designator = designator;
23596 first_p = false;
23597 }
23598 else if (cxx_dialect >= cxx20
23599 && first_designator != error_mark_node
23600 && (!first_designator != !designator))
23601 {
23602 error_at (loc, "either all initializer clauses should be designated "
23603 "or none of them should be");
23604 first_designator = error_mark_node;
23605 }
23606 else if (cxx_dialect < cxx20 && !first_designator)
23607 first_designator = designator;
23608
23609 /* Parse the initializer. */
23610 initializer = cp_parser_initializer_clause (parser,
23611 &clause_non_constant_p);
23612 /* If any clause is non-constant, so is the entire initializer. */
23613 if (clause_non_constant_p)
23614 *non_constant_p = true;
23615
23616 /* If we have an ellipsis, this is an initializer pack
23617 expansion. */
23618 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23619 {
23620 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23621
23622 /* Consume the `...'. */
23623 cp_lexer_consume_token (parser->lexer);
23624
23625 if (designator && cxx_dialect >= cxx20)
23626 error_at (loc,
23627 "%<...%> not allowed in designated initializer list");
23628
23629 /* Turn the initializer into an initializer expansion. */
23630 initializer = make_pack_expansion (initializer);
23631 }
23632
23633 /* Add it to the vector. */
23634 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
23635
23636 /* If the next token is not a comma, we have reached the end of
23637 the list. */
23638 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23639 break;
23640
23641 /* Peek at the next token. */
23642 token = cp_lexer_peek_nth_token (parser->lexer, 2);
23643 /* If the next token is a `}', then we're still done. An
23644 initializer-clause can have a trailing `,' after the
23645 initializer-list and before the closing `}'. */
23646 if (token->type == CPP_CLOSE_BRACE)
23647 break;
23648
23649 /* Suppress location wrappers in a long initializer to save memory
23650 (14179). The cutoff is chosen arbitrarily. */
23651 const unsigned loc_max = 256;
23652 unsigned incr = 1;
23653 if (TREE_CODE (initializer) == CONSTRUCTOR)
23654 /* Look one level down because it's easy. Looking deeper would require
23655 passing down a nelts pointer, and I don't think multi-level massive
23656 initializers are common enough to justify this. */
23657 incr = CONSTRUCTOR_NELTS (initializer);
23658 nelts += incr;
23659 if (nelts >= loc_max && (nelts - incr) < loc_max)
23660 ++suppress_location_wrappers;
23661
23662 /* Consume the `,' token. */
23663 cp_lexer_consume_token (parser->lexer);
23664 }
23665
23666 /* The same identifier shall not appear in multiple designators
23667 of a designated-initializer-list. */
23668 if (first_designator)
23669 {
23670 unsigned int i;
23671 tree designator, val;
23672 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23673 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23674 {
23675 if (IDENTIFIER_MARKED (designator))
23676 {
23677 error_at (cp_expr_loc_or_input_loc (val),
23678 "%<.%s%> designator used multiple times in "
23679 "the same initializer list",
23680 IDENTIFIER_POINTER (designator));
23681 (*v)[i].index = error_mark_node;
23682 }
23683 else
23684 IDENTIFIER_MARKED (designator) = 1;
23685 }
23686 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23687 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23688 IDENTIFIER_MARKED (designator) = 0;
23689 }
23690
23691 suppress_location_wrappers = suppress;
23692
23693 *designated = first_designator != NULL_TREE;
23694 return v;
23695 }
23696
23697 /* Classes [gram.class] */
23698
23699 /* Parse a class-name.
23700
23701 class-name:
23702 identifier
23703 template-id
23704
23705 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23706 to indicate that names looked up in dependent types should be
23707 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23708 keyword has been used to indicate that the name that appears next
23709 is a template. TAG_TYPE indicates the explicit tag given before
23710 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23711 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23712 is the class being defined in a class-head. If ENUM_OK is TRUE,
23713 enum-names are also accepted.
23714
23715 Returns the TYPE_DECL representing the class. */
23716
23717 static tree
23718 cp_parser_class_name (cp_parser *parser,
23719 bool typename_keyword_p,
23720 bool template_keyword_p,
23721 enum tag_types tag_type,
23722 bool check_dependency_p,
23723 bool class_head_p,
23724 bool is_declaration,
23725 bool enum_ok)
23726 {
23727 tree decl;
23728 tree scope;
23729 bool typename_p;
23730 cp_token *token;
23731 tree identifier = NULL_TREE;
23732
23733 /* All class-names start with an identifier. */
23734 token = cp_lexer_peek_token (parser->lexer);
23735 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
23736 {
23737 cp_parser_error (parser, "expected class-name");
23738 return error_mark_node;
23739 }
23740
23741 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23742 to a template-id, so we save it here. Consider object scope too,
23743 so that make_typename_type below can use it (cp_parser_template_name
23744 considers object scope also). This may happen with code like
23745
23746 p->template A<T>::a()
23747
23748 where we first want to look up A<T>::a in the class of the object
23749 expression, as per [basic.lookup.classref]. */
23750 scope = parser->scope ? parser->scope : parser->context->object_type;
23751 if (scope == error_mark_node)
23752 return error_mark_node;
23753
23754 /* Any name names a type if we're following the `typename' keyword
23755 in a qualified name where the enclosing scope is type-dependent. */
23756 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
23757 && dependent_type_p (scope));
23758 /* Handle the common case (an identifier, but not a template-id)
23759 efficiently. */
23760 if (token->type == CPP_NAME
23761 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
23762 {
23763 cp_token *identifier_token;
23764 bool ambiguous_p;
23765
23766 /* Look for the identifier. */
23767 identifier_token = cp_lexer_peek_token (parser->lexer);
23768 ambiguous_p = identifier_token->error_reported;
23769 identifier = cp_parser_identifier (parser);
23770 /* If the next token isn't an identifier, we are certainly not
23771 looking at a class-name. */
23772 if (identifier == error_mark_node)
23773 decl = error_mark_node;
23774 /* If we know this is a type-name, there's no need to look it
23775 up. */
23776 else if (typename_p)
23777 decl = identifier;
23778 else
23779 {
23780 tree ambiguous_decls;
23781 /* If we already know that this lookup is ambiguous, then
23782 we've already issued an error message; there's no reason
23783 to check again. */
23784 if (ambiguous_p)
23785 {
23786 cp_parser_simulate_error (parser);
23787 return error_mark_node;
23788 }
23789 /* If the next token is a `::', then the name must be a type
23790 name.
23791
23792 [basic.lookup.qual]
23793
23794 During the lookup for a name preceding the :: scope
23795 resolution operator, object, function, and enumerator
23796 names are ignored. */
23797 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23798 tag_type = scope_type;
23799 /* Look up the name. */
23800 decl = cp_parser_lookup_name (parser, identifier,
23801 tag_type,
23802 /*is_template=*/false,
23803 /*is_namespace=*/false,
23804 check_dependency_p,
23805 &ambiguous_decls,
23806 identifier_token->location);
23807 if (ambiguous_decls)
23808 {
23809 if (cp_parser_parsing_tentatively (parser))
23810 cp_parser_simulate_error (parser);
23811 return error_mark_node;
23812 }
23813 }
23814 }
23815 else
23816 {
23817 /* Try a template-id. */
23818 decl = cp_parser_template_id (parser, template_keyword_p,
23819 check_dependency_p,
23820 tag_type,
23821 is_declaration);
23822 if (decl == error_mark_node)
23823 return error_mark_node;
23824 }
23825
23826 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
23827
23828 /* If this is a typename, create a TYPENAME_TYPE. */
23829 if (typename_p
23830 && decl != error_mark_node
23831 && !is_overloaded_fn (decl))
23832 {
23833 decl = make_typename_type (scope, decl, typename_type,
23834 /*complain=*/tf_error);
23835 if (decl != error_mark_node)
23836 decl = TYPE_NAME (decl);
23837 }
23838
23839 decl = strip_using_decl (decl);
23840
23841 /* Check to see that it is really the name of a class. */
23842 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
23843 && identifier_p (TREE_OPERAND (decl, 0))
23844 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23845 /* Situations like this:
23846
23847 template <typename T> struct A {
23848 typename T::template X<int>::I i;
23849 };
23850
23851 are problematic. Is `T::template X<int>' a class-name? The
23852 standard does not seem to be definitive, but there is no other
23853 valid interpretation of the following `::'. Therefore, those
23854 names are considered class-names. */
23855 {
23856 decl = make_typename_type (scope, decl, tag_type, tf_error);
23857 if (decl != error_mark_node)
23858 decl = TYPE_NAME (decl);
23859 }
23860 else if (TREE_CODE (decl) != TYPE_DECL
23861 || TREE_TYPE (decl) == error_mark_node
23862 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
23863 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
23864 /* In Objective-C 2.0, a classname followed by '.' starts a
23865 dot-syntax expression, and it's not a type-name. */
23866 || (c_dialect_objc ()
23867 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
23868 && objc_is_class_name (decl)))
23869 decl = error_mark_node;
23870
23871 if (decl == error_mark_node)
23872 cp_parser_error (parser, "expected class-name");
23873 else if (identifier && !parser->scope)
23874 maybe_note_name_used_in_class (identifier, decl);
23875
23876 return decl;
23877 }
23878
23879 /* Make sure that any member-function parameters are in scope.
23880 For instance, a function's noexcept-specifier can use the function's
23881 parameters:
23882
23883 struct S {
23884 void fn (int p) noexcept(noexcept(p));
23885 };
23886
23887 so we need to make sure name lookup can find them. This is used
23888 when we delay parsing of the noexcept-specifier. */
23889
23890 static void
23891 inject_parm_decls (tree decl)
23892 {
23893 begin_scope (sk_function_parms, decl);
23894 tree args = DECL_ARGUMENTS (decl);
23895
23896 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
23897
23898 if (args && is_this_parameter (args))
23899 {
23900 gcc_checking_assert (current_class_ptr == NULL_TREE);
23901 current_class_ptr = NULL_TREE;
23902 current_class_ref = cp_build_fold_indirect_ref (args);
23903 current_class_ptr = args;
23904 }
23905 }
23906
23907 /* Undo the effects of inject_parm_decls. */
23908
23909 static void
23910 pop_injected_parms (void)
23911 {
23912 pop_bindings_and_leave_scope ();
23913 current_class_ptr = current_class_ref = NULL_TREE;
23914 }
23915
23916 /* Parse a class-specifier.
23917
23918 class-specifier:
23919 class-head { member-specification [opt] }
23920
23921 Returns the TREE_TYPE representing the class. */
23922
23923 static tree
23924 cp_parser_class_specifier_1 (cp_parser* parser)
23925 {
23926 tree type;
23927 tree attributes = NULL_TREE;
23928 bool nested_name_specifier_p;
23929 unsigned saved_num_template_parameter_lists;
23930 bool saved_in_function_body;
23931 unsigned char in_statement;
23932 bool in_switch_statement_p;
23933 bool saved_in_unbraced_linkage_specification_p;
23934 tree old_scope = NULL_TREE;
23935 tree scope = NULL_TREE;
23936 cp_token *closing_brace;
23937
23938 push_deferring_access_checks (dk_no_deferred);
23939
23940 /* Parse the class-head. */
23941 type = cp_parser_class_head (parser,
23942 &nested_name_specifier_p);
23943 /* If the class-head was a semantic disaster, skip the entire body
23944 of the class. */
23945 if (!type)
23946 {
23947 cp_parser_skip_to_end_of_block_or_statement (parser);
23948 pop_deferring_access_checks ();
23949 return error_mark_node;
23950 }
23951
23952 /* Look for the `{'. */
23953 matching_braces braces;
23954 if (!braces.require_open (parser))
23955 {
23956 pop_deferring_access_checks ();
23957 return error_mark_node;
23958 }
23959
23960 cp_ensure_no_omp_declare_simd (parser);
23961 cp_ensure_no_oacc_routine (parser);
23962
23963 /* Issue an error message if type-definitions are forbidden here. */
23964 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
23965 /* Remember that we are defining one more class. */
23966 ++parser->num_classes_being_defined;
23967 /* Inside the class, surrounding template-parameter-lists do not
23968 apply. */
23969 saved_num_template_parameter_lists
23970 = parser->num_template_parameter_lists;
23971 parser->num_template_parameter_lists = 0;
23972 /* We are not in a function body. */
23973 saved_in_function_body = parser->in_function_body;
23974 parser->in_function_body = false;
23975 /* Or in a loop. */
23976 in_statement = parser->in_statement;
23977 parser->in_statement = 0;
23978 /* Or in a switch. */
23979 in_switch_statement_p = parser->in_switch_statement_p;
23980 parser->in_switch_statement_p = false;
23981 /* We are not immediately inside an extern "lang" block. */
23982 saved_in_unbraced_linkage_specification_p
23983 = parser->in_unbraced_linkage_specification_p;
23984 parser->in_unbraced_linkage_specification_p = false;
23985
23986 // Associate constraints with the type.
23987 if (flag_concepts)
23988 type = associate_classtype_constraints (type);
23989
23990 /* Start the class. */
23991 if (nested_name_specifier_p)
23992 {
23993 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23994 /* SCOPE must be a scope nested inside current scope. */
23995 if (is_nested_namespace (current_namespace,
23996 decl_namespace_context (scope)))
23997 old_scope = push_inner_scope (scope);
23998 else
23999 nested_name_specifier_p = false;
24000 }
24001 type = begin_class_definition (type);
24002
24003 if (type == error_mark_node)
24004 /* If the type is erroneous, skip the entire body of the class. */
24005 cp_parser_skip_to_closing_brace (parser);
24006 else
24007 /* Parse the member-specification. */
24008 cp_parser_member_specification_opt (parser);
24009
24010 /* Look for the trailing `}'. */
24011 closing_brace = braces.require_close (parser);
24012 /* Look for trailing attributes to apply to this class. */
24013 if (cp_parser_allow_gnu_extensions_p (parser))
24014 attributes = cp_parser_gnu_attributes_opt (parser);
24015 if (type != error_mark_node)
24016 type = finish_struct (type, attributes);
24017 if (nested_name_specifier_p)
24018 pop_inner_scope (old_scope, scope);
24019
24020 /* We've finished a type definition. Check for the common syntax
24021 error of forgetting a semicolon after the definition. We need to
24022 be careful, as we can't just check for not-a-semicolon and be done
24023 with it; the user might have typed:
24024
24025 class X { } c = ...;
24026 class X { } *p = ...;
24027
24028 and so forth. Instead, enumerate all the possible tokens that
24029 might follow this production; if we don't see one of them, then
24030 complain and silently insert the semicolon. */
24031 {
24032 cp_token *token = cp_lexer_peek_token (parser->lexer);
24033 bool want_semicolon = true;
24034
24035 if (cp_next_tokens_can_be_std_attribute_p (parser))
24036 /* Don't try to parse c++11 attributes here. As per the
24037 grammar, that should be a task for
24038 cp_parser_decl_specifier_seq. */
24039 want_semicolon = false;
24040
24041 switch (token->type)
24042 {
24043 case CPP_NAME:
24044 case CPP_SEMICOLON:
24045 case CPP_MULT:
24046 case CPP_AND:
24047 case CPP_OPEN_PAREN:
24048 case CPP_CLOSE_PAREN:
24049 case CPP_COMMA:
24050 want_semicolon = false;
24051 break;
24052
24053 /* While it's legal for type qualifiers and storage class
24054 specifiers to follow type definitions in the grammar, only
24055 compiler testsuites contain code like that. Assume that if
24056 we see such code, then what we're really seeing is a case
24057 like:
24058
24059 class X { }
24060 const <type> var = ...;
24061
24062 or
24063
24064 class Y { }
24065 static <type> func (...) ...
24066
24067 i.e. the qualifier or specifier applies to the next
24068 declaration. To do so, however, we need to look ahead one
24069 more token to see if *that* token is a type specifier.
24070
24071 This code could be improved to handle:
24072
24073 class Z { }
24074 static const <type> var = ...; */
24075 case CPP_KEYWORD:
24076 if (keyword_is_decl_specifier (token->keyword))
24077 {
24078 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
24079
24080 /* Handling user-defined types here would be nice, but very
24081 tricky. */
24082 want_semicolon
24083 = (lookahead->type == CPP_KEYWORD
24084 && keyword_begins_type_specifier (lookahead->keyword));
24085 }
24086 break;
24087 default:
24088 break;
24089 }
24090
24091 /* If we don't have a type, then something is very wrong and we
24092 shouldn't try to do anything clever. Likewise for not seeing the
24093 closing brace. */
24094 if (closing_brace && TYPE_P (type) && want_semicolon)
24095 {
24096 /* Locate the closing brace. */
24097 cp_token_position prev
24098 = cp_lexer_previous_token_position (parser->lexer);
24099 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
24100 location_t loc = prev_token->location;
24101
24102 /* We want to suggest insertion of a ';' immediately *after* the
24103 closing brace, so, if we can, offset the location by 1 column. */
24104 location_t next_loc = loc;
24105 if (!linemap_location_from_macro_expansion_p (line_table, loc))
24106 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
24107
24108 rich_location richloc (line_table, next_loc);
24109
24110 /* If we successfully offset the location, suggest the fix-it. */
24111 if (next_loc != loc)
24112 richloc.add_fixit_insert_before (next_loc, ";");
24113
24114 if (CLASSTYPE_DECLARED_CLASS (type))
24115 error_at (&richloc,
24116 "expected %<;%> after class definition");
24117 else if (TREE_CODE (type) == RECORD_TYPE)
24118 error_at (&richloc,
24119 "expected %<;%> after struct definition");
24120 else if (TREE_CODE (type) == UNION_TYPE)
24121 error_at (&richloc,
24122 "expected %<;%> after union definition");
24123 else
24124 gcc_unreachable ();
24125
24126 /* Unget one token and smash it to look as though we encountered
24127 a semicolon in the input stream. */
24128 cp_lexer_set_token_position (parser->lexer, prev);
24129 token = cp_lexer_peek_token (parser->lexer);
24130 token->type = CPP_SEMICOLON;
24131 token->keyword = RID_MAX;
24132 }
24133 }
24134
24135 /* If this class is not itself within the scope of another class,
24136 then we need to parse the bodies of all of the queued function
24137 definitions. Note that the queued functions defined in a class
24138 are not always processed immediately following the
24139 class-specifier for that class. Consider:
24140
24141 struct A {
24142 struct B { void f() { sizeof (A); } };
24143 };
24144
24145 If `f' were processed before the processing of `A' were
24146 completed, there would be no way to compute the size of `A'.
24147 Note that the nesting we are interested in here is lexical --
24148 not the semantic nesting given by TYPE_CONTEXT. In particular,
24149 for:
24150
24151 struct A { struct B; };
24152 struct A::B { void f() { } };
24153
24154 there is no need to delay the parsing of `A::B::f'. */
24155 if (--parser->num_classes_being_defined == 0)
24156 {
24157 tree decl;
24158 tree class_type = NULL_TREE;
24159 tree pushed_scope = NULL_TREE;
24160 unsigned ix;
24161 cp_default_arg_entry *e;
24162 tree save_ccp, save_ccr;
24163
24164 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
24165 {
24166 /* Skip default arguments, NSDMIs, etc, in order to improve
24167 error recovery (c++/71169, c++/71832). */
24168 vec_safe_truncate (unparsed_funs_with_default_args, 0);
24169 vec_safe_truncate (unparsed_nsdmis, 0);
24170 vec_safe_truncate (unparsed_funs_with_definitions, 0);
24171 }
24172
24173 /* In a first pass, parse default arguments to the functions.
24174 Then, in a second pass, parse the bodies of the functions.
24175 This two-phased approach handles cases like:
24176
24177 struct S {
24178 void f() { g(); }
24179 void g(int i = 3);
24180 };
24181
24182 */
24183 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
24184 {
24185 decl = e->decl;
24186 /* If there are default arguments that have not yet been processed,
24187 take care of them now. */
24188 if (class_type != e->class_type)
24189 {
24190 if (pushed_scope)
24191 pop_scope (pushed_scope);
24192 class_type = e->class_type;
24193 pushed_scope = push_scope (class_type);
24194 }
24195 /* Make sure that any template parameters are in scope. */
24196 maybe_begin_member_template_processing (decl);
24197 /* Parse the default argument expressions. */
24198 cp_parser_late_parsing_default_args (parser, decl);
24199 /* Remove any template parameters from the symbol table. */
24200 maybe_end_member_template_processing ();
24201 }
24202 vec_safe_truncate (unparsed_funs_with_default_args, 0);
24203 /* Now parse any NSDMIs. */
24204 save_ccp = current_class_ptr;
24205 save_ccr = current_class_ref;
24206 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
24207 {
24208 if (class_type != DECL_CONTEXT (decl))
24209 {
24210 if (pushed_scope)
24211 pop_scope (pushed_scope);
24212 class_type = DECL_CONTEXT (decl);
24213 pushed_scope = push_scope (class_type);
24214 }
24215 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
24216 cp_parser_late_parsing_nsdmi (parser, decl);
24217 }
24218 vec_safe_truncate (unparsed_nsdmis, 0);
24219 current_class_ptr = save_ccp;
24220 current_class_ref = save_ccr;
24221 if (pushed_scope)
24222 pop_scope (pushed_scope);
24223
24224 /* If there are noexcept-specifiers that have not yet been processed,
24225 take care of them now. */
24226 class_type = NULL_TREE;
24227 pushed_scope = NULL_TREE;
24228 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
24229 {
24230 tree ctx = DECL_CONTEXT (decl);
24231 if (class_type != ctx)
24232 {
24233 if (pushed_scope)
24234 pop_scope (pushed_scope);
24235 class_type = ctx;
24236 pushed_scope = push_scope (class_type);
24237 }
24238
24239 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
24240 spec = TREE_PURPOSE (spec);
24241
24242 /* Make sure that any template parameters are in scope. */
24243 maybe_begin_member_template_processing (decl);
24244
24245 /* Make sure that any member-function parameters are in scope. */
24246 inject_parm_decls (decl);
24247
24248 /* 'this' is not allowed in static member functions. */
24249 unsigned char local_variables_forbidden_p
24250 = parser->local_variables_forbidden_p;
24251 if (DECL_THIS_STATIC (decl))
24252 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
24253
24254 /* Now we can parse the noexcept-specifier. */
24255 spec = cp_parser_late_noexcept_specifier (parser, spec);
24256
24257 if (spec != error_mark_node)
24258 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
24259
24260 /* Restore the state of local_variables_forbidden_p. */
24261 parser->local_variables_forbidden_p = local_variables_forbidden_p;
24262
24263 /* The finish_struct call above performed various override checking,
24264 but it skipped unparsed noexcept-specifier operands. Now that we
24265 have resolved them, check again. */
24266 noexcept_override_late_checks (type, decl);
24267
24268 /* Remove any member-function parameters from the symbol table. */
24269 pop_injected_parms ();
24270
24271 /* Remove any template parameters from the symbol table. */
24272 maybe_end_member_template_processing ();
24273 }
24274 vec_safe_truncate (unparsed_noexcepts, 0);
24275 if (pushed_scope)
24276 pop_scope (pushed_scope);
24277
24278 /* Now parse the body of the functions. */
24279 if (flag_openmp)
24280 {
24281 /* OpenMP UDRs need to be parsed before all other functions. */
24282 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
24283 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
24284 cp_parser_late_parsing_for_member (parser, decl);
24285 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
24286 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
24287 cp_parser_late_parsing_for_member (parser, decl);
24288 }
24289 else
24290 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
24291 cp_parser_late_parsing_for_member (parser, decl);
24292 vec_safe_truncate (unparsed_funs_with_definitions, 0);
24293 }
24294
24295 /* Put back any saved access checks. */
24296 pop_deferring_access_checks ();
24297
24298 /* Restore saved state. */
24299 parser->in_switch_statement_p = in_switch_statement_p;
24300 parser->in_statement = in_statement;
24301 parser->in_function_body = saved_in_function_body;
24302 parser->num_template_parameter_lists
24303 = saved_num_template_parameter_lists;
24304 parser->in_unbraced_linkage_specification_p
24305 = saved_in_unbraced_linkage_specification_p;
24306
24307 return type;
24308 }
24309
24310 static tree
24311 cp_parser_class_specifier (cp_parser* parser)
24312 {
24313 tree ret;
24314 timevar_push (TV_PARSE_STRUCT);
24315 ret = cp_parser_class_specifier_1 (parser);
24316 timevar_pop (TV_PARSE_STRUCT);
24317 return ret;
24318 }
24319
24320 /* Parse a class-head.
24321
24322 class-head:
24323 class-key identifier [opt] base-clause [opt]
24324 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
24325 class-key nested-name-specifier [opt] template-id
24326 base-clause [opt]
24327
24328 class-virt-specifier:
24329 final
24330
24331 GNU Extensions:
24332 class-key attributes identifier [opt] base-clause [opt]
24333 class-key attributes nested-name-specifier identifier base-clause [opt]
24334 class-key attributes nested-name-specifier [opt] template-id
24335 base-clause [opt]
24336
24337 Upon return BASES is initialized to the list of base classes (or
24338 NULL, if there are none) in the same form returned by
24339 cp_parser_base_clause.
24340
24341 Returns the TYPE of the indicated class. Sets
24342 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
24343 involving a nested-name-specifier was used, and FALSE otherwise.
24344
24345 Returns error_mark_node if this is not a class-head.
24346
24347 Returns NULL_TREE if the class-head is syntactically valid, but
24348 semantically invalid in a way that means we should skip the entire
24349 body of the class. */
24350
24351 static tree
24352 cp_parser_class_head (cp_parser* parser,
24353 bool* nested_name_specifier_p)
24354 {
24355 tree nested_name_specifier;
24356 enum tag_types class_key;
24357 tree id = NULL_TREE;
24358 tree type = NULL_TREE;
24359 tree attributes;
24360 tree bases;
24361 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
24362 bool template_id_p = false;
24363 bool qualified_p = false;
24364 bool invalid_nested_name_p = false;
24365 bool invalid_explicit_specialization_p = false;
24366 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24367 tree pushed_scope = NULL_TREE;
24368 unsigned num_templates;
24369 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
24370 /* Assume no nested-name-specifier will be present. */
24371 *nested_name_specifier_p = false;
24372 /* Assume no template parameter lists will be used in defining the
24373 type. */
24374 num_templates = 0;
24375 parser->colon_corrects_to_scope_p = false;
24376
24377 /* Look for the class-key. */
24378 class_key = cp_parser_class_key (parser);
24379 if (class_key == none_type)
24380 return error_mark_node;
24381
24382 location_t class_head_start_location = input_location;
24383
24384 /* Parse the attributes. */
24385 attributes = cp_parser_attributes_opt (parser);
24386
24387 /* If the next token is `::', that is invalid -- but sometimes
24388 people do try to write:
24389
24390 struct ::S {};
24391
24392 Handle this gracefully by accepting the extra qualifier, and then
24393 issuing an error about it later if this really is a
24394 class-head. If it turns out just to be an elaborated type
24395 specifier, remain silent. */
24396 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
24397 qualified_p = true;
24398
24399 push_deferring_access_checks (dk_no_check);
24400
24401 /* Determine the name of the class. Begin by looking for an
24402 optional nested-name-specifier. */
24403 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
24404 nested_name_specifier
24405 = cp_parser_nested_name_specifier_opt (parser,
24406 /*typename_keyword_p=*/false,
24407 /*check_dependency_p=*/false,
24408 /*type_p=*/true,
24409 /*is_declaration=*/false);
24410 /* If there was a nested-name-specifier, then there *must* be an
24411 identifier. */
24412
24413 cp_token *bad_template_keyword = NULL;
24414
24415 if (nested_name_specifier)
24416 {
24417 type_start_token = cp_lexer_peek_token (parser->lexer);
24418 /* Although the grammar says `identifier', it really means
24419 `class-name' or `template-name'. You are only allowed to
24420 define a class that has already been declared with this
24421 syntax.
24422
24423 The proposed resolution for Core Issue 180 says that wherever
24424 you see `class T::X' you should treat `X' as a type-name.
24425
24426 It is OK to define an inaccessible class; for example:
24427
24428 class A { class B; };
24429 class A::B {};
24430
24431 We do not know if we will see a class-name, or a
24432 template-name. We look for a class-name first, in case the
24433 class-name is a template-id; if we looked for the
24434 template-name first we would stop after the template-name. */
24435 cp_parser_parse_tentatively (parser);
24436 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24437 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
24438 type = cp_parser_class_name (parser,
24439 /*typename_keyword_p=*/false,
24440 /*template_keyword_p=*/false,
24441 class_type,
24442 /*check_dependency_p=*/false,
24443 /*class_head_p=*/true,
24444 /*is_declaration=*/false);
24445 /* If that didn't work, ignore the nested-name-specifier. */
24446 if (!cp_parser_parse_definitely (parser))
24447 {
24448 invalid_nested_name_p = true;
24449 type_start_token = cp_lexer_peek_token (parser->lexer);
24450 id = cp_parser_identifier (parser);
24451 if (id == error_mark_node)
24452 id = NULL_TREE;
24453 }
24454 /* If we could not find a corresponding TYPE, treat this
24455 declaration like an unqualified declaration. */
24456 if (type == error_mark_node)
24457 nested_name_specifier = NULL_TREE;
24458 /* Otherwise, count the number of templates used in TYPE and its
24459 containing scopes. */
24460 else
24461 num_templates = num_template_headers_for_class (TREE_TYPE (type));
24462 }
24463 /* Otherwise, the identifier is optional. */
24464 else
24465 {
24466 /* We don't know whether what comes next is a template-id,
24467 an identifier, or nothing at all. */
24468 cp_parser_parse_tentatively (parser);
24469 /* Check for a template-id. */
24470 type_start_token = cp_lexer_peek_token (parser->lexer);
24471 id = cp_parser_template_id (parser,
24472 /*template_keyword_p=*/false,
24473 /*check_dependency_p=*/true,
24474 class_key,
24475 /*is_declaration=*/true);
24476 /* If that didn't work, it could still be an identifier. */
24477 if (!cp_parser_parse_definitely (parser))
24478 {
24479 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24480 {
24481 type_start_token = cp_lexer_peek_token (parser->lexer);
24482 id = cp_parser_identifier (parser);
24483 }
24484 else
24485 id = NULL_TREE;
24486 }
24487 else
24488 {
24489 template_id_p = true;
24490 ++num_templates;
24491 }
24492 }
24493
24494 pop_deferring_access_checks ();
24495
24496 if (id)
24497 {
24498 cp_parser_check_for_invalid_template_id (parser, id,
24499 class_key,
24500 type_start_token->location);
24501 }
24502 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
24503
24504 /* If it's not a `:' or a `{' then we can't really be looking at a
24505 class-head, since a class-head only appears as part of a
24506 class-specifier. We have to detect this situation before calling
24507 xref_tag, since that has irreversible side-effects. */
24508 if (!cp_parser_next_token_starts_class_definition_p (parser))
24509 {
24510 cp_parser_error (parser, "expected %<{%> or %<:%>");
24511 type = error_mark_node;
24512 goto out;
24513 }
24514
24515 /* At this point, we're going ahead with the class-specifier, even
24516 if some other problem occurs. */
24517 cp_parser_commit_to_tentative_parse (parser);
24518 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
24519 {
24520 cp_parser_error (parser,
24521 "cannot specify %<override%> for a class");
24522 type = error_mark_node;
24523 goto out;
24524 }
24525 /* Issue the error about the overly-qualified name now. */
24526 if (qualified_p)
24527 {
24528 cp_parser_error (parser,
24529 "global qualification of class name is invalid");
24530 type = error_mark_node;
24531 goto out;
24532 }
24533 else if (invalid_nested_name_p)
24534 {
24535 cp_parser_error (parser,
24536 "qualified name does not name a class");
24537 type = error_mark_node;
24538 goto out;
24539 }
24540 else if (nested_name_specifier)
24541 {
24542 tree scope;
24543
24544 if (bad_template_keyword)
24545 /* [temp.names]: in a qualified-id formed by a class-head-name, the
24546 keyword template shall not appear at the top level. */
24547 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
24548 "keyword %<template%> not allowed in class-head-name");
24549
24550 /* Reject typedef-names in class heads. */
24551 if (!DECL_IMPLICIT_TYPEDEF_P (type))
24552 {
24553 error_at (type_start_token->location,
24554 "invalid class name in declaration of %qD",
24555 type);
24556 type = NULL_TREE;
24557 goto done;
24558 }
24559
24560 /* Figure out in what scope the declaration is being placed. */
24561 scope = current_scope ();
24562 /* If that scope does not contain the scope in which the
24563 class was originally declared, the program is invalid. */
24564 if (scope && !is_ancestor (scope, nested_name_specifier))
24565 {
24566 if (at_namespace_scope_p ())
24567 error_at (type_start_token->location,
24568 "declaration of %qD in namespace %qD which does not "
24569 "enclose %qD",
24570 type, scope, nested_name_specifier);
24571 else
24572 error_at (type_start_token->location,
24573 "declaration of %qD in %qD which does not enclose %qD",
24574 type, scope, nested_name_specifier);
24575 type = NULL_TREE;
24576 goto done;
24577 }
24578 /* [dcl.meaning]
24579
24580 A declarator-id shall not be qualified except for the
24581 definition of a ... nested class outside of its class
24582 ... [or] the definition or explicit instantiation of a
24583 class member of a namespace outside of its namespace. */
24584 if (scope == nested_name_specifier)
24585 permerror (nested_name_specifier_token_start->location,
24586 "extra qualification not allowed");
24587 }
24588 /* An explicit-specialization must be preceded by "template <>". If
24589 it is not, try to recover gracefully. */
24590 if (at_namespace_scope_p ()
24591 && parser->num_template_parameter_lists == 0
24592 && !processing_template_parmlist
24593 && template_id_p)
24594 {
24595 /* Build a location of this form:
24596 struct typename <ARGS>
24597 ^~~~~~~~~~~~~~~~~~~~~~
24598 with caret==start at the start token, and
24599 finishing at the end of the type. */
24600 location_t reported_loc
24601 = make_location (class_head_start_location,
24602 class_head_start_location,
24603 get_finish (type_start_token->location));
24604 rich_location richloc (line_table, reported_loc);
24605 richloc.add_fixit_insert_before (class_head_start_location,
24606 "template <> ");
24607 error_at (&richloc,
24608 "an explicit specialization must be preceded by"
24609 " %<template <>%>");
24610 invalid_explicit_specialization_p = true;
24611 /* Take the same action that would have been taken by
24612 cp_parser_explicit_specialization. */
24613 ++parser->num_template_parameter_lists;
24614 begin_specialization ();
24615 }
24616 /* There must be no "return" statements between this point and the
24617 end of this function; set "type "to the correct return value and
24618 use "goto done;" to return. */
24619 /* Make sure that the right number of template parameters were
24620 present. */
24621 if (!cp_parser_check_template_parameters (parser, num_templates,
24622 template_id_p,
24623 type_start_token->location,
24624 /*declarator=*/NULL))
24625 {
24626 /* If something went wrong, there is no point in even trying to
24627 process the class-definition. */
24628 type = NULL_TREE;
24629 goto done;
24630 }
24631
24632 /* Look up the type. */
24633 if (template_id_p)
24634 {
24635 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
24636 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
24637 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
24638 {
24639 error_at (type_start_token->location,
24640 "function template %qD redeclared as a class template", id);
24641 type = error_mark_node;
24642 }
24643 else
24644 {
24645 type = TREE_TYPE (id);
24646 type = maybe_process_partial_specialization (type);
24647
24648 /* Check the scope while we still know whether or not we had a
24649 nested-name-specifier. */
24650 if (type != error_mark_node)
24651 check_unqualified_spec_or_inst (type, type_start_token->location);
24652 }
24653 if (nested_name_specifier)
24654 pushed_scope = push_scope (nested_name_specifier);
24655 }
24656 else if (nested_name_specifier)
24657 {
24658 tree class_type;
24659
24660 /* Given:
24661
24662 template <typename T> struct S { struct T };
24663 template <typename T> struct S<T>::T { };
24664
24665 we will get a TYPENAME_TYPE when processing the definition of
24666 `S::T'. We need to resolve it to the actual type before we
24667 try to define it. */
24668 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
24669 {
24670 class_type = resolve_typename_type (TREE_TYPE (type),
24671 /*only_current_p=*/false);
24672 if (TREE_CODE (class_type) != TYPENAME_TYPE)
24673 type = TYPE_NAME (class_type);
24674 else
24675 {
24676 cp_parser_error (parser, "could not resolve typename type");
24677 type = error_mark_node;
24678 }
24679 }
24680
24681 if (maybe_process_partial_specialization (TREE_TYPE (type))
24682 == error_mark_node)
24683 {
24684 type = NULL_TREE;
24685 goto done;
24686 }
24687
24688 class_type = current_class_type;
24689 /* Enter the scope indicated by the nested-name-specifier. */
24690 pushed_scope = push_scope (nested_name_specifier);
24691 /* Get the canonical version of this type. */
24692 type = TYPE_MAIN_DECL (TREE_TYPE (type));
24693 /* Call push_template_decl if it seems like we should be defining a
24694 template either from the template headers or the type we're
24695 defining, so that we diagnose both extra and missing headers. */
24696 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24697 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
24698 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
24699 {
24700 type = push_template_decl (type);
24701 if (type == error_mark_node)
24702 {
24703 type = NULL_TREE;
24704 goto done;
24705 }
24706 }
24707
24708 type = TREE_TYPE (type);
24709 *nested_name_specifier_p = true;
24710 }
24711 else /* The name is not a nested name. */
24712 {
24713 /* If the class was unnamed, create a dummy name. */
24714 if (!id)
24715 id = make_anon_name ();
24716 tag_scope tag_scope = (parser->in_type_id_in_expr_p
24717 ? ts_within_enclosing_non_class
24718 : ts_current);
24719 type = xref_tag (class_key, id, tag_scope,
24720 parser->num_template_parameter_lists);
24721 }
24722
24723 /* Diagnose class/struct/union mismatches. */
24724 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
24725 true, true);
24726
24727 /* Indicate whether this class was declared as a `class' or as a
24728 `struct'. */
24729 if (TREE_CODE (type) == RECORD_TYPE)
24730 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
24731
24732 /* If this type was already complete, and we see another definition,
24733 that's an error. Likewise if the type is already being defined:
24734 this can happen, eg, when it's defined from within an expression
24735 (c++/84605). */
24736 if (type != error_mark_node
24737 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
24738 {
24739 error_at (type_start_token->location, "redefinition of %q#T",
24740 type);
24741 inform (location_of (type), "previous definition of %q#T",
24742 type);
24743 type = NULL_TREE;
24744 goto done;
24745 }
24746 else if (type == error_mark_node)
24747 type = NULL_TREE;
24748
24749 if (type)
24750 {
24751 /* Apply attributes now, before any use of the class as a template
24752 argument in its base list. */
24753 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
24754 fixup_attribute_variants (type);
24755 }
24756
24757 /* We will have entered the scope containing the class; the names of
24758 base classes should be looked up in that context. For example:
24759
24760 struct A { struct B {}; struct C; };
24761 struct A::C : B {};
24762
24763 is valid. */
24764
24765 /* Get the list of base-classes, if there is one. */
24766 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24767 {
24768 /* PR59482: enter the class scope so that base-specifiers are looked
24769 up correctly. */
24770 if (type)
24771 pushclass (type);
24772 bases = cp_parser_base_clause (parser);
24773 /* PR59482: get out of the previously pushed class scope so that the
24774 subsequent pops pop the right thing. */
24775 if (type)
24776 popclass ();
24777 }
24778 else
24779 bases = NULL_TREE;
24780
24781 /* If we're really defining a class, process the base classes.
24782 If they're invalid, fail. */
24783 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24784 xref_basetypes (type, bases);
24785
24786 done:
24787 /* Leave the scope given by the nested-name-specifier. We will
24788 enter the class scope itself while processing the members. */
24789 if (pushed_scope)
24790 pop_scope (pushed_scope);
24791
24792 if (invalid_explicit_specialization_p)
24793 {
24794 end_specialization ();
24795 --parser->num_template_parameter_lists;
24796 }
24797
24798 if (type)
24799 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
24800 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
24801 CLASSTYPE_FINAL (type) = 1;
24802 out:
24803 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24804 return type;
24805 }
24806
24807 /* Parse a class-key.
24808
24809 class-key:
24810 class
24811 struct
24812 union
24813
24814 Returns the kind of class-key specified, or none_type to indicate
24815 error. */
24816
24817 static enum tag_types
24818 cp_parser_class_key (cp_parser* parser)
24819 {
24820 cp_token *token;
24821 enum tag_types tag_type;
24822
24823 /* Look for the class-key. */
24824 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
24825 if (!token)
24826 return none_type;
24827
24828 /* Check to see if the TOKEN is a class-key. */
24829 tag_type = cp_parser_token_is_class_key (token);
24830 if (!tag_type)
24831 cp_parser_error (parser, "expected class-key");
24832 return tag_type;
24833 }
24834
24835 /* Parse a type-parameter-key.
24836
24837 type-parameter-key:
24838 class
24839 typename
24840 */
24841
24842 static void
24843 cp_parser_type_parameter_key (cp_parser* parser)
24844 {
24845 /* Look for the type-parameter-key. */
24846 enum tag_types tag_type = none_type;
24847 cp_token *token = cp_lexer_peek_token (parser->lexer);
24848 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
24849 {
24850 cp_lexer_consume_token (parser->lexer);
24851 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
24852 /* typename is not allowed in a template template parameter
24853 by the standard until C++17. */
24854 pedwarn (token->location, OPT_Wpedantic,
24855 "ISO C++ forbids typename key in template template parameter;"
24856 " use %<-std=c++17%> or %<-std=gnu++17%>");
24857 }
24858 else
24859 cp_parser_error (parser, "expected %<class%> or %<typename%>");
24860
24861 return;
24862 }
24863
24864 /* Parse an (optional) member-specification.
24865
24866 member-specification:
24867 member-declaration member-specification [opt]
24868 access-specifier : member-specification [opt] */
24869
24870 static void
24871 cp_parser_member_specification_opt (cp_parser* parser)
24872 {
24873 while (true)
24874 {
24875 cp_token *token;
24876 enum rid keyword;
24877
24878 /* Peek at the next token. */
24879 token = cp_lexer_peek_token (parser->lexer);
24880 /* If it's a `}', or EOF then we've seen all the members. */
24881 if (token->type == CPP_CLOSE_BRACE
24882 || token->type == CPP_EOF
24883 || token->type == CPP_PRAGMA_EOL)
24884 break;
24885
24886 /* See if this token is a keyword. */
24887 keyword = token->keyword;
24888 switch (keyword)
24889 {
24890 case RID_PUBLIC:
24891 case RID_PROTECTED:
24892 case RID_PRIVATE:
24893 /* Consume the access-specifier. */
24894 cp_lexer_consume_token (parser->lexer);
24895 /* Remember which access-specifier is active. */
24896 current_access_specifier = token->u.value;
24897 /* Look for the `:'. */
24898 cp_parser_require (parser, CPP_COLON, RT_COLON);
24899 break;
24900
24901 default:
24902 /* Accept #pragmas at class scope. */
24903 if (token->type == CPP_PRAGMA)
24904 {
24905 cp_parser_pragma (parser, pragma_member, NULL);
24906 break;
24907 }
24908
24909 /* Otherwise, the next construction must be a
24910 member-declaration. */
24911 cp_parser_member_declaration (parser);
24912 }
24913 }
24914 }
24915
24916 /* Parse a member-declaration.
24917
24918 member-declaration:
24919 decl-specifier-seq [opt] member-declarator-list [opt] ;
24920 function-definition ; [opt]
24921 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24922 using-declaration
24923 template-declaration
24924 alias-declaration
24925
24926 member-declarator-list:
24927 member-declarator
24928 member-declarator-list , member-declarator
24929
24930 member-declarator:
24931 declarator pure-specifier [opt]
24932 declarator constant-initializer [opt]
24933 identifier [opt] : constant-expression
24934
24935 GNU Extensions:
24936
24937 member-declaration:
24938 __extension__ member-declaration
24939
24940 member-declarator:
24941 declarator attributes [opt] pure-specifier [opt]
24942 declarator attributes [opt] constant-initializer [opt]
24943 identifier [opt] attributes [opt] : constant-expression
24944
24945 C++0x Extensions:
24946
24947 member-declaration:
24948 static_assert-declaration */
24949
24950 static void
24951 cp_parser_member_declaration (cp_parser* parser)
24952 {
24953 cp_decl_specifier_seq decl_specifiers;
24954 tree prefix_attributes;
24955 tree decl;
24956 int declares_class_or_enum;
24957 bool friend_p;
24958 cp_token *token = NULL;
24959 cp_token *decl_spec_token_start = NULL;
24960 cp_token *initializer_token_start = NULL;
24961 int saved_pedantic;
24962 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24963
24964 /* Check for the `__extension__' keyword. */
24965 if (cp_parser_extension_opt (parser, &saved_pedantic))
24966 {
24967 /* Recurse. */
24968 cp_parser_member_declaration (parser);
24969 /* Restore the old value of the PEDANTIC flag. */
24970 pedantic = saved_pedantic;
24971
24972 return;
24973 }
24974
24975 /* Check for a template-declaration. */
24976 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24977 {
24978 /* An explicit specialization here is an error condition, and we
24979 expect the specialization handler to detect and report this. */
24980 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
24981 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
24982 cp_parser_explicit_specialization (parser);
24983 else
24984 cp_parser_template_declaration (parser, /*member_p=*/true);
24985
24986 return;
24987 }
24988 /* Check for a template introduction. */
24989 else if (cp_parser_template_declaration_after_export (parser, true))
24990 return;
24991
24992 /* Check for a using-declaration. */
24993 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24994 {
24995 if (cxx_dialect < cxx11)
24996 {
24997 /* Parse the using-declaration. */
24998 cp_parser_using_declaration (parser,
24999 /*access_declaration_p=*/false);
25000 return;
25001 }
25002 else
25003 {
25004 tree decl;
25005 bool alias_decl_expected;
25006 cp_parser_parse_tentatively (parser);
25007 decl = cp_parser_alias_declaration (parser);
25008 /* Note that if we actually see the '=' token after the
25009 identifier, cp_parser_alias_declaration commits the
25010 tentative parse. In that case, we really expect an
25011 alias-declaration. Otherwise, we expect a using
25012 declaration. */
25013 alias_decl_expected =
25014 !cp_parser_uncommitted_to_tentative_parse_p (parser);
25015 cp_parser_parse_definitely (parser);
25016
25017 if (alias_decl_expected)
25018 finish_member_declaration (decl);
25019 else
25020 cp_parser_using_declaration (parser,
25021 /*access_declaration_p=*/false);
25022 return;
25023 }
25024 }
25025
25026 /* Check for @defs. */
25027 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
25028 {
25029 tree ivar, member;
25030 tree ivar_chains = cp_parser_objc_defs_expression (parser);
25031 ivar = ivar_chains;
25032 while (ivar)
25033 {
25034 member = ivar;
25035 ivar = TREE_CHAIN (member);
25036 TREE_CHAIN (member) = NULL_TREE;
25037 finish_member_declaration (member);
25038 }
25039 return;
25040 }
25041
25042 /* If the next token is `static_assert' we have a static assertion. */
25043 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
25044 {
25045 cp_parser_static_assert (parser, /*member_p=*/true);
25046 return;
25047 }
25048
25049 parser->colon_corrects_to_scope_p = false;
25050
25051 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
25052 goto out;
25053
25054 /* Parse the decl-specifier-seq. */
25055 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25056 cp_parser_decl_specifier_seq (parser,
25057 (CP_PARSER_FLAGS_OPTIONAL
25058 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
25059 &decl_specifiers,
25060 &declares_class_or_enum);
25061 /* Check for an invalid type-name. */
25062 if (!decl_specifiers.any_type_specifiers_p
25063 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25064 goto out;
25065 /* If there is no declarator, then the decl-specifier-seq should
25066 specify a type. */
25067 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25068 {
25069 /* If there was no decl-specifier-seq, and the next token is a
25070 `;', then we have something like:
25071
25072 struct S { ; };
25073
25074 [class.mem]
25075
25076 Each member-declaration shall declare at least one member
25077 name of the class. */
25078 if (!decl_specifiers.any_specifiers_p)
25079 {
25080 cp_token *token = cp_lexer_peek_token (parser->lexer);
25081 if (!in_system_header_at (token->location))
25082 {
25083 gcc_rich_location richloc (token->location);
25084 richloc.add_fixit_remove ();
25085 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
25086 }
25087 }
25088 else
25089 {
25090 tree type;
25091
25092 /* See if this declaration is a friend. */
25093 friend_p = cp_parser_friend_p (&decl_specifiers);
25094 /* If there were decl-specifiers, check to see if there was
25095 a class-declaration. */
25096 type = check_tag_decl (&decl_specifiers,
25097 /*explicit_type_instantiation_p=*/false);
25098 /* Nested classes have already been added to the class, but
25099 a `friend' needs to be explicitly registered. */
25100 if (friend_p)
25101 {
25102 /* If the `friend' keyword was present, the friend must
25103 be introduced with a class-key. */
25104 if (!declares_class_or_enum && cxx_dialect < cxx11)
25105 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
25106 "in C++03 a class-key must be used "
25107 "when declaring a friend");
25108 /* In this case:
25109
25110 template <typename T> struct A {
25111 friend struct A<T>::B;
25112 };
25113
25114 A<T>::B will be represented by a TYPENAME_TYPE, and
25115 therefore not recognized by check_tag_decl. */
25116 if (!type)
25117 {
25118 type = decl_specifiers.type;
25119 if (type && TREE_CODE (type) == TYPE_DECL)
25120 type = TREE_TYPE (type);
25121 }
25122 if (!type || !TYPE_P (type))
25123 error_at (decl_spec_token_start->location,
25124 "friend declaration does not name a class or "
25125 "function");
25126 else
25127 make_friend_class (current_class_type, type,
25128 /*complain=*/true);
25129 }
25130 /* If there is no TYPE, an error message will already have
25131 been issued. */
25132 else if (!type || type == error_mark_node)
25133 ;
25134 /* An anonymous aggregate has to be handled specially; such
25135 a declaration really declares a data member (with a
25136 particular type), as opposed to a nested class. */
25137 else if (ANON_AGGR_TYPE_P (type))
25138 {
25139 /* C++11 9.5/6. */
25140 if (decl_specifiers.storage_class != sc_none)
25141 error_at (decl_spec_token_start->location,
25142 "a storage class on an anonymous aggregate "
25143 "in class scope is not allowed");
25144
25145 /* Remove constructors and such from TYPE, now that we
25146 know it is an anonymous aggregate. */
25147 fixup_anonymous_aggr (type);
25148 /* And make the corresponding data member. */
25149 decl = build_decl (decl_spec_token_start->location,
25150 FIELD_DECL, NULL_TREE, type);
25151 /* Add it to the class. */
25152 finish_member_declaration (decl);
25153 }
25154 else
25155 cp_parser_check_access_in_redeclaration
25156 (TYPE_NAME (type),
25157 decl_spec_token_start->location);
25158 }
25159 }
25160 else
25161 {
25162 bool assume_semicolon = false;
25163
25164 /* Clear attributes from the decl_specifiers but keep them
25165 around as prefix attributes that apply them to the entity
25166 being declared. */
25167 prefix_attributes = decl_specifiers.attributes;
25168 decl_specifiers.attributes = NULL_TREE;
25169
25170 /* See if these declarations will be friends. */
25171 friend_p = cp_parser_friend_p (&decl_specifiers);
25172
25173 /* Keep going until we hit the `;' at the end of the
25174 declaration. */
25175 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25176 {
25177 tree attributes = NULL_TREE;
25178 tree first_attribute;
25179 tree initializer;
25180 bool named_bitfld = false;
25181
25182 /* Peek at the next token. */
25183 token = cp_lexer_peek_token (parser->lexer);
25184
25185 /* The following code wants to know early if it is a bit-field
25186 or some other declaration. Attributes can appear before
25187 the `:' token. Skip over them without consuming any tokens
25188 to peek if they are followed by `:'. */
25189 if (cp_next_tokens_can_be_attribute_p (parser)
25190 || (token->type == CPP_NAME
25191 && cp_nth_tokens_can_be_attribute_p (parser, 2)
25192 && (named_bitfld = true)))
25193 {
25194 size_t n
25195 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
25196 token = cp_lexer_peek_nth_token (parser->lexer, n);
25197 }
25198
25199 /* Check for a bitfield declaration. */
25200 if (token->type == CPP_COLON
25201 || (token->type == CPP_NAME
25202 && token == cp_lexer_peek_token (parser->lexer)
25203 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
25204 && (named_bitfld = true)))
25205 {
25206 tree identifier;
25207 tree width;
25208 tree late_attributes = NULL_TREE;
25209 location_t id_location
25210 = cp_lexer_peek_token (parser->lexer)->location;
25211
25212 if (named_bitfld)
25213 identifier = cp_parser_identifier (parser);
25214 else
25215 identifier = NULL_TREE;
25216
25217 /* Look for attributes that apply to the bitfield. */
25218 attributes = cp_parser_attributes_opt (parser);
25219
25220 /* Consume the `:' token. */
25221 cp_lexer_consume_token (parser->lexer);
25222
25223 /* Get the width of the bitfield. */
25224 width = cp_parser_constant_expression (parser, false, NULL,
25225 cxx_dialect >= cxx11);
25226
25227 /* In C++20 and as extension for C++11 and above we allow
25228 default member initializers for bit-fields. */
25229 initializer = NULL_TREE;
25230 if (cxx_dialect >= cxx11
25231 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
25232 || cp_lexer_next_token_is (parser->lexer,
25233 CPP_OPEN_BRACE)))
25234 {
25235 location_t loc
25236 = cp_lexer_peek_token (parser->lexer)->location;
25237 if (cxx_dialect < cxx20
25238 && identifier != NULL_TREE)
25239 pedwarn (loc, 0,
25240 "default member initializers for bit-fields "
25241 "only available with %<-std=c++20%> or "
25242 "%<-std=gnu++20%>");
25243
25244 initializer = cp_parser_save_nsdmi (parser);
25245 if (identifier == NULL_TREE)
25246 {
25247 error_at (loc, "default member initializer for "
25248 "unnamed bit-field");
25249 initializer = NULL_TREE;
25250 }
25251 }
25252 else
25253 {
25254 /* Look for attributes that apply to the bitfield after
25255 the `:' token and width. This is where GCC used to
25256 parse attributes in the past, pedwarn if there is
25257 a std attribute. */
25258 if (cp_next_tokens_can_be_std_attribute_p (parser))
25259 pedwarn (input_location, OPT_Wpedantic,
25260 "ISO C++ allows bit-field attributes only "
25261 "before the %<:%> token");
25262
25263 late_attributes = cp_parser_attributes_opt (parser);
25264 }
25265
25266 attributes = attr_chainon (attributes, late_attributes);
25267
25268 /* Remember which attributes are prefix attributes and
25269 which are not. */
25270 first_attribute = attributes;
25271 /* Combine the attributes. */
25272 attributes = attr_chainon (prefix_attributes, attributes);
25273
25274 /* Create the bitfield declaration. */
25275 decl = grokbitfield (identifier
25276 ? make_id_declarator (NULL_TREE,
25277 identifier,
25278 sfk_none,
25279 id_location)
25280 : NULL,
25281 &decl_specifiers,
25282 width, initializer,
25283 attributes);
25284 }
25285 else
25286 {
25287 cp_declarator *declarator;
25288 tree asm_specification;
25289 int ctor_dtor_or_conv_p;
25290 bool static_p = (decl_specifiers.storage_class == sc_static);
25291 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
25292 if (!friend_p
25293 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25294 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
25295
25296 /* Parse the declarator. */
25297 declarator
25298 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25299 flags,
25300 &ctor_dtor_or_conv_p,
25301 /*parenthesized_p=*/NULL,
25302 /*member_p=*/true,
25303 friend_p, static_p);
25304
25305 /* If something went wrong parsing the declarator, make sure
25306 that we at least consume some tokens. */
25307 if (declarator == cp_error_declarator)
25308 {
25309 /* Skip to the end of the statement. */
25310 cp_parser_skip_to_end_of_statement (parser);
25311 /* If the next token is not a semicolon, that is
25312 probably because we just skipped over the body of
25313 a function. So, we consume a semicolon if
25314 present, but do not issue an error message if it
25315 is not present. */
25316 if (cp_lexer_next_token_is (parser->lexer,
25317 CPP_SEMICOLON))
25318 cp_lexer_consume_token (parser->lexer);
25319 goto out;
25320 }
25321
25322 if (declares_class_or_enum & 2)
25323 cp_parser_check_for_definition_in_return_type
25324 (declarator, decl_specifiers.type,
25325 decl_specifiers.locations[ds_type_spec]);
25326
25327 /* Look for an asm-specification. */
25328 asm_specification = cp_parser_asm_specification_opt (parser);
25329 /* Look for attributes that apply to the declaration. */
25330 attributes = cp_parser_attributes_opt (parser);
25331 /* Remember which attributes are prefix attributes and
25332 which are not. */
25333 first_attribute = attributes;
25334 /* Combine the attributes. */
25335 attributes = attr_chainon (prefix_attributes, attributes);
25336
25337 /* If it's an `=', then we have a constant-initializer or a
25338 pure-specifier. It is not correct to parse the
25339 initializer before registering the member declaration
25340 since the member declaration should be in scope while
25341 its initializer is processed. However, the rest of the
25342 front end does not yet provide an interface that allows
25343 us to handle this correctly. */
25344 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25345 {
25346 /* In [class.mem]:
25347
25348 A pure-specifier shall be used only in the declaration of
25349 a virtual function.
25350
25351 A member-declarator can contain a constant-initializer
25352 only if it declares a static member of integral or
25353 enumeration type.
25354
25355 Therefore, if the DECLARATOR is for a function, we look
25356 for a pure-specifier; otherwise, we look for a
25357 constant-initializer. When we call `grokfield', it will
25358 perform more stringent semantics checks. */
25359 initializer_token_start = cp_lexer_peek_token (parser->lexer);
25360 if (function_declarator_p (declarator)
25361 || (decl_specifiers.type
25362 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
25363 && declarator->kind == cdk_id
25364 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
25365 == FUNCTION_TYPE)))
25366 initializer = cp_parser_pure_specifier (parser);
25367 else if (decl_specifiers.storage_class != sc_static)
25368 initializer = cp_parser_save_nsdmi (parser);
25369 else if (cxx_dialect >= cxx11)
25370 {
25371 bool nonconst;
25372 /* Don't require a constant rvalue in C++11, since we
25373 might want a reference constant. We'll enforce
25374 constancy later. */
25375 cp_lexer_consume_token (parser->lexer);
25376 /* Parse the initializer. */
25377 initializer = cp_parser_initializer_clause (parser,
25378 &nonconst);
25379 }
25380 else
25381 /* Parse the initializer. */
25382 initializer = cp_parser_constant_initializer (parser);
25383 }
25384 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
25385 && !function_declarator_p (declarator))
25386 {
25387 bool x;
25388 if (decl_specifiers.storage_class != sc_static)
25389 initializer = cp_parser_save_nsdmi (parser);
25390 else
25391 initializer = cp_parser_initializer (parser, &x, &x);
25392 }
25393 /* Detect invalid bit-field cases such as
25394
25395 int *p : 4;
25396 int &&r : 3;
25397
25398 and similar. */
25399 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
25400 /* If there were no type specifiers, it was a
25401 constructor. */
25402 && decl_specifiers.any_type_specifiers_p)
25403 {
25404 /* This is called for a decent diagnostic only. */
25405 tree d = grokdeclarator (declarator, &decl_specifiers,
25406 BITFIELD, /*initialized=*/false,
25407 &attributes);
25408 if (!error_operand_p (d))
25409 error_at (DECL_SOURCE_LOCATION (d),
25410 "bit-field %qD has non-integral type %qT",
25411 d, TREE_TYPE (d));
25412 cp_parser_skip_to_end_of_statement (parser);
25413 /* Avoid "extra ;" pedwarns. */
25414 if (cp_lexer_next_token_is (parser->lexer,
25415 CPP_SEMICOLON))
25416 cp_lexer_consume_token (parser->lexer);
25417 goto out;
25418 }
25419 /* Otherwise, there is no initializer. */
25420 else
25421 initializer = NULL_TREE;
25422
25423 /* See if we are probably looking at a function
25424 definition. We are certainly not looking at a
25425 member-declarator. Calling `grokfield' has
25426 side-effects, so we must not do it unless we are sure
25427 that we are looking at a member-declarator. */
25428 if (cp_parser_token_starts_function_definition_p
25429 (cp_lexer_peek_token (parser->lexer)))
25430 {
25431 /* The grammar does not allow a pure-specifier to be
25432 used when a member function is defined. (It is
25433 possible that this fact is an oversight in the
25434 standard, since a pure function may be defined
25435 outside of the class-specifier. */
25436 if (initializer && initializer_token_start)
25437 error_at (initializer_token_start->location,
25438 "pure-specifier on function-definition");
25439 decl = cp_parser_save_member_function_body (parser,
25440 &decl_specifiers,
25441 declarator,
25442 attributes);
25443 if (parser->fully_implicit_function_template_p)
25444 decl = finish_fully_implicit_template (parser, decl);
25445 /* If the member was not a friend, declare it here. */
25446 if (!friend_p)
25447 finish_member_declaration (decl);
25448 /* Peek at the next token. */
25449 token = cp_lexer_peek_token (parser->lexer);
25450 /* If the next token is a semicolon, consume it. */
25451 if (token->type == CPP_SEMICOLON)
25452 {
25453 location_t semicolon_loc
25454 = cp_lexer_consume_token (parser->lexer)->location;
25455 gcc_rich_location richloc (semicolon_loc);
25456 richloc.add_fixit_remove ();
25457 warning_at (&richloc, OPT_Wextra_semi,
25458 "extra %<;%> after in-class "
25459 "function definition");
25460 }
25461 goto out;
25462 }
25463 else
25464 if (declarator->kind == cdk_function)
25465 declarator->id_loc = token->location;
25466 /* Create the declaration. */
25467 decl = grokfield (declarator, &decl_specifiers,
25468 initializer, /*init_const_expr_p=*/true,
25469 asm_specification, attributes);
25470 if (parser->fully_implicit_function_template_p)
25471 {
25472 if (friend_p)
25473 finish_fully_implicit_template (parser, 0);
25474 else
25475 decl = finish_fully_implicit_template (parser, decl);
25476 }
25477 }
25478
25479 cp_finalize_omp_declare_simd (parser, decl);
25480 cp_finalize_oacc_routine (parser, decl, false);
25481
25482 /* Reset PREFIX_ATTRIBUTES. */
25483 if (attributes != error_mark_node)
25484 {
25485 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25486 attributes = TREE_CHAIN (attributes);
25487 if (attributes)
25488 TREE_CHAIN (attributes) = NULL_TREE;
25489 }
25490
25491 /* If there is any qualification still in effect, clear it
25492 now; we will be starting fresh with the next declarator. */
25493 parser->scope = NULL_TREE;
25494 parser->qualifying_scope = NULL_TREE;
25495 parser->object_scope = NULL_TREE;
25496 /* If it's a `,', then there are more declarators. */
25497 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25498 {
25499 cp_lexer_consume_token (parser->lexer);
25500 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25501 {
25502 cp_token *token = cp_lexer_previous_token (parser->lexer);
25503 gcc_rich_location richloc (token->location);
25504 richloc.add_fixit_remove ();
25505 error_at (&richloc, "stray %<,%> at end of "
25506 "member declaration");
25507 }
25508 }
25509 /* If the next token isn't a `;', then we have a parse error. */
25510 else if (cp_lexer_next_token_is_not (parser->lexer,
25511 CPP_SEMICOLON))
25512 {
25513 /* The next token might be a ways away from where the
25514 actual semicolon is missing. Find the previous token
25515 and use that for our error position. */
25516 cp_token *token = cp_lexer_previous_token (parser->lexer);
25517 gcc_rich_location richloc (token->location);
25518 richloc.add_fixit_insert_after (";");
25519 error_at (&richloc, "expected %<;%> at end of "
25520 "member declaration");
25521
25522 /* Assume that the user meant to provide a semicolon. If
25523 we were to cp_parser_skip_to_end_of_statement, we might
25524 skip to a semicolon inside a member function definition
25525 and issue nonsensical error messages. */
25526 assume_semicolon = true;
25527 }
25528
25529 if (decl)
25530 {
25531 /* Add DECL to the list of members. */
25532 if (!friend_p
25533 /* Explicitly include, eg, NSDMIs, for better error
25534 recovery (c++/58650). */
25535 || !DECL_DECLARES_FUNCTION_P (decl))
25536 finish_member_declaration (decl);
25537
25538 if (TREE_CODE (decl) == FUNCTION_DECL)
25539 cp_parser_save_default_args (parser, decl);
25540 else if (TREE_CODE (decl) == FIELD_DECL
25541 && DECL_INITIAL (decl))
25542 /* Add DECL to the queue of NSDMI to be parsed later. */
25543 vec_safe_push (unparsed_nsdmis, decl);
25544 }
25545
25546 if (assume_semicolon)
25547 goto out;
25548 }
25549 }
25550
25551 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25552 out:
25553 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
25554 }
25555
25556 /* Parse a pure-specifier.
25557
25558 pure-specifier:
25559 = 0
25560
25561 Returns INTEGER_ZERO_NODE if a pure specifier is found.
25562 Otherwise, ERROR_MARK_NODE is returned. */
25563
25564 static tree
25565 cp_parser_pure_specifier (cp_parser* parser)
25566 {
25567 cp_token *token;
25568
25569 /* Look for the `=' token. */
25570 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25571 return error_mark_node;
25572 /* Look for the `0' token. */
25573 token = cp_lexer_peek_token (parser->lexer);
25574
25575 if (token->type == CPP_EOF
25576 || token->type == CPP_PRAGMA_EOL)
25577 return error_mark_node;
25578
25579 cp_lexer_consume_token (parser->lexer);
25580
25581 /* Accept = default or = delete in c++0x mode. */
25582 if (token->keyword == RID_DEFAULT
25583 || token->keyword == RID_DELETE)
25584 {
25585 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
25586 return token->u.value;
25587 }
25588
25589 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
25590 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
25591 {
25592 cp_parser_error (parser,
25593 "invalid pure specifier (only %<= 0%> is allowed)");
25594 cp_parser_skip_to_end_of_statement (parser);
25595 return error_mark_node;
25596 }
25597 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
25598 {
25599 error_at (token->location, "templates may not be %<virtual%>");
25600 return error_mark_node;
25601 }
25602
25603 return integer_zero_node;
25604 }
25605
25606 /* Parse a constant-initializer.
25607
25608 constant-initializer:
25609 = constant-expression
25610
25611 Returns a representation of the constant-expression. */
25612
25613 static tree
25614 cp_parser_constant_initializer (cp_parser* parser)
25615 {
25616 /* Look for the `=' token. */
25617 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25618 return error_mark_node;
25619
25620 /* It is invalid to write:
25621
25622 struct S { static const int i = { 7 }; };
25623
25624 */
25625 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25626 {
25627 cp_parser_error (parser,
25628 "a brace-enclosed initializer is not allowed here");
25629 /* Consume the opening brace. */
25630 matching_braces braces;
25631 braces.consume_open (parser);
25632 /* Skip the initializer. */
25633 cp_parser_skip_to_closing_brace (parser);
25634 /* Look for the trailing `}'. */
25635 braces.require_close (parser);
25636
25637 return error_mark_node;
25638 }
25639
25640 return cp_parser_constant_expression (parser);
25641 }
25642
25643 /* Derived classes [gram.class.derived] */
25644
25645 /* Parse a base-clause.
25646
25647 base-clause:
25648 : base-specifier-list
25649
25650 base-specifier-list:
25651 base-specifier ... [opt]
25652 base-specifier-list , base-specifier ... [opt]
25653
25654 Returns a TREE_LIST representing the base-classes, in the order in
25655 which they were declared. The representation of each node is as
25656 described by cp_parser_base_specifier.
25657
25658 In the case that no bases are specified, this function will return
25659 NULL_TREE, not ERROR_MARK_NODE. */
25660
25661 static tree
25662 cp_parser_base_clause (cp_parser* parser)
25663 {
25664 tree bases = NULL_TREE;
25665
25666 /* Look for the `:' that begins the list. */
25667 cp_parser_require (parser, CPP_COLON, RT_COLON);
25668
25669 /* Scan the base-specifier-list. */
25670 while (true)
25671 {
25672 cp_token *token;
25673 tree base;
25674 bool pack_expansion_p = false;
25675
25676 /* Look for the base-specifier. */
25677 base = cp_parser_base_specifier (parser);
25678 /* Look for the (optional) ellipsis. */
25679 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25680 {
25681 /* Consume the `...'. */
25682 cp_lexer_consume_token (parser->lexer);
25683
25684 pack_expansion_p = true;
25685 }
25686
25687 /* Add BASE to the front of the list. */
25688 if (base && base != error_mark_node)
25689 {
25690 if (pack_expansion_p)
25691 /* Make this a pack expansion type. */
25692 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
25693
25694 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
25695 {
25696 TREE_CHAIN (base) = bases;
25697 bases = base;
25698 }
25699 }
25700 /* Peek at the next token. */
25701 token = cp_lexer_peek_token (parser->lexer);
25702 /* If it's not a comma, then the list is complete. */
25703 if (token->type != CPP_COMMA)
25704 break;
25705 /* Consume the `,'. */
25706 cp_lexer_consume_token (parser->lexer);
25707 }
25708
25709 /* PARSER->SCOPE may still be non-NULL at this point, if the last
25710 base class had a qualified name. However, the next name that
25711 appears is certainly not qualified. */
25712 parser->scope = NULL_TREE;
25713 parser->qualifying_scope = NULL_TREE;
25714 parser->object_scope = NULL_TREE;
25715
25716 return nreverse (bases);
25717 }
25718
25719 /* Parse a base-specifier.
25720
25721 base-specifier:
25722 :: [opt] nested-name-specifier [opt] class-name
25723 virtual access-specifier [opt] :: [opt] nested-name-specifier
25724 [opt] class-name
25725 access-specifier virtual [opt] :: [opt] nested-name-specifier
25726 [opt] class-name
25727
25728 Returns a TREE_LIST. The TREE_PURPOSE will be one of
25729 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25730 indicate the specifiers provided. The TREE_VALUE will be a TYPE
25731 (or the ERROR_MARK_NODE) indicating the type that was specified. */
25732
25733 static tree
25734 cp_parser_base_specifier (cp_parser* parser)
25735 {
25736 cp_token *token;
25737 bool done = false;
25738 bool virtual_p = false;
25739 bool duplicate_virtual_error_issued_p = false;
25740 bool duplicate_access_error_issued_p = false;
25741 bool class_scope_p, template_p;
25742 tree access = access_default_node;
25743 tree type;
25744
25745 /* Process the optional `virtual' and `access-specifier'. */
25746 while (!done)
25747 {
25748 /* Peek at the next token. */
25749 token = cp_lexer_peek_token (parser->lexer);
25750 /* Process `virtual'. */
25751 switch (token->keyword)
25752 {
25753 case RID_VIRTUAL:
25754 /* If `virtual' appears more than once, issue an error. */
25755 if (virtual_p && !duplicate_virtual_error_issued_p)
25756 {
25757 cp_parser_error (parser,
25758 "%<virtual%> specified more than once in base-specifier");
25759 duplicate_virtual_error_issued_p = true;
25760 }
25761
25762 virtual_p = true;
25763
25764 /* Consume the `virtual' token. */
25765 cp_lexer_consume_token (parser->lexer);
25766
25767 break;
25768
25769 case RID_PUBLIC:
25770 case RID_PROTECTED:
25771 case RID_PRIVATE:
25772 /* If more than one access specifier appears, issue an
25773 error. */
25774 if (access != access_default_node
25775 && !duplicate_access_error_issued_p)
25776 {
25777 cp_parser_error (parser,
25778 "more than one access specifier in base-specifier");
25779 duplicate_access_error_issued_p = true;
25780 }
25781
25782 access = ridpointers[(int) token->keyword];
25783
25784 /* Consume the access-specifier. */
25785 cp_lexer_consume_token (parser->lexer);
25786
25787 break;
25788
25789 default:
25790 done = true;
25791 break;
25792 }
25793 }
25794 /* It is not uncommon to see programs mechanically, erroneously, use
25795 the 'typename' keyword to denote (dependent) qualified types
25796 as base classes. */
25797 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25798 {
25799 token = cp_lexer_peek_token (parser->lexer);
25800 if (!processing_template_decl)
25801 error_at (token->location,
25802 "keyword %<typename%> not allowed outside of templates");
25803 else
25804 error_at (token->location,
25805 "keyword %<typename%> not allowed in this context "
25806 "(the base class is implicitly a type)");
25807 cp_lexer_consume_token (parser->lexer);
25808 }
25809
25810 /* Look for the optional `::' operator. */
25811 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
25812 /* Look for the nested-name-specifier. The simplest way to
25813 implement:
25814
25815 [temp.res]
25816
25817 The keyword `typename' is not permitted in a base-specifier or
25818 mem-initializer; in these contexts a qualified name that
25819 depends on a template-parameter is implicitly assumed to be a
25820 type name.
25821
25822 is to pretend that we have seen the `typename' keyword at this
25823 point. */
25824 cp_parser_nested_name_specifier_opt (parser,
25825 /*typename_keyword_p=*/true,
25826 /*check_dependency_p=*/true,
25827 /*type_p=*/true,
25828 /*is_declaration=*/true);
25829 /* If the base class is given by a qualified name, assume that names
25830 we see are type names or templates, as appropriate. */
25831 class_scope_p = (parser->scope && TYPE_P (parser->scope));
25832 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
25833
25834 if (!parser->scope
25835 && cp_lexer_next_token_is_decltype (parser->lexer))
25836 /* DR 950 allows decltype as a base-specifier. */
25837 type = cp_parser_decltype (parser);
25838 else
25839 {
25840 /* Otherwise, look for the class-name. */
25841 type = cp_parser_class_name (parser,
25842 class_scope_p,
25843 template_p,
25844 typename_type,
25845 /*check_dependency_p=*/true,
25846 /*class_head_p=*/false,
25847 /*is_declaration=*/true);
25848 type = TREE_TYPE (type);
25849 }
25850
25851 if (type == error_mark_node)
25852 return error_mark_node;
25853
25854 return finish_base_specifier (type, access, virtual_p);
25855 }
25856
25857 /* Exception handling [gram.exception] */
25858
25859 /* Save the tokens that make up the noexcept-specifier for a member-function.
25860 Returns a DEFERRED_PARSE. */
25861
25862 static tree
25863 cp_parser_save_noexcept (cp_parser *parser)
25864 {
25865 cp_token *first = parser->lexer->next_token;
25866 /* We want everything up to, including, the final ')'. */
25867 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
25868 cp_token *last = parser->lexer->next_token;
25869
25870 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
25871 to carry the information we will need. */
25872 tree expr = make_node (DEFERRED_PARSE);
25873 /* Save away the noexcept-specifier; we will process it when the
25874 class is complete. */
25875 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
25876 expr = build_tree_list (expr, NULL_TREE);
25877 return expr;
25878 }
25879
25880 /* Used for late processing of noexcept-specifiers of member-functions.
25881 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
25882 we saved for later; parse it now. DECL is the declaration of the
25883 member function. */
25884
25885 static tree
25886 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
25887 {
25888 /* Make sure we've gotten something that hasn't been parsed yet. */
25889 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
25890
25891 push_unparsed_function_queues (parser);
25892
25893 /* Push the saved tokens for the noexcept-specifier onto the parser's
25894 lexer stack. */
25895 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
25896 cp_parser_push_lexer_for_tokens (parser, tokens);
25897
25898 /* Parse the cached noexcept-specifier. */
25899 tree parsed_arg
25900 = cp_parser_noexcept_specification_opt (parser,
25901 CP_PARSER_FLAGS_NONE,
25902 /*require_constexpr=*/true,
25903 /*consumed_expr=*/NULL,
25904 /*return_cond=*/false);
25905
25906 /* Revert to the main lexer. */
25907 cp_parser_pop_lexer (parser);
25908
25909 /* Restore the queue. */
25910 pop_unparsed_function_queues (parser);
25911
25912 /* And we're done. */
25913 return parsed_arg;
25914 }
25915
25916 /* Perform late checking of overriding function with respect to their
25917 noexcept-specifiers. TYPE is the class and FNDECL is the function
25918 that potentially overrides some virtual function with the same
25919 signature. */
25920
25921 static void
25922 noexcept_override_late_checks (tree type, tree fndecl)
25923 {
25924 tree binfo = TYPE_BINFO (type);
25925 tree base_binfo;
25926
25927 if (DECL_STATIC_FUNCTION_P (fndecl))
25928 return;
25929
25930 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
25931 {
25932 tree basetype = BINFO_TYPE (base_binfo);
25933
25934 if (!TYPE_POLYMORPHIC_P (basetype))
25935 continue;
25936
25937 tree fn = look_for_overrides_here (basetype, fndecl);
25938 if (fn)
25939 maybe_check_overriding_exception_spec (fndecl, fn);
25940 }
25941 }
25942
25943 /* Parse an (optional) noexcept-specification.
25944
25945 noexcept-specification:
25946 noexcept ( constant-expression ) [opt]
25947
25948 If no noexcept-specification is present, returns NULL_TREE.
25949 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25950 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25951 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25952 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25953 in which case a boolean condition is returned instead. The parser flags
25954 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
25955 the (member) function is `const'. */
25956
25957 static tree
25958 cp_parser_noexcept_specification_opt (cp_parser* parser,
25959 cp_parser_flags flags,
25960 bool require_constexpr,
25961 bool* consumed_expr,
25962 bool return_cond)
25963 {
25964 cp_token *token;
25965 const char *saved_message;
25966
25967 /* Peek at the next token. */
25968 token = cp_lexer_peek_token (parser->lexer);
25969
25970 /* Is it a noexcept-specification? */
25971 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
25972 {
25973 tree expr;
25974
25975 /* [class.mem]/6 says that a noexcept-specifer (within the
25976 member-specification of the class) is a complete-class context of
25977 a class. So, if the noexcept-specifier has the optional expression,
25978 just save the tokens, and reparse this after we're done with the
25979 class. */
25980 const bool literal_p
25981 = ((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
25982 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
25983 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN));
25984
25985 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
25986 /* No need to delay parsing for a number literal or true/false. */
25987 && !literal_p
25988 && at_class_scope_p ()
25989 /* We don't delay parsing for friend member functions,
25990 alias-declarations, and typedefs, even though the standard seems
25991 to require it. */
25992 && (flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
25993 && TYPE_BEING_DEFINED (current_class_type)
25994 && !LAMBDA_TYPE_P (current_class_type))
25995 return cp_parser_save_noexcept (parser);
25996
25997 cp_lexer_consume_token (parser->lexer);
25998
25999 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
26000 {
26001 matching_parens parens;
26002 parens.consume_open (parser);
26003
26004 if (require_constexpr)
26005 {
26006 /* Types may not be defined in an exception-specification. */
26007 saved_message = parser->type_definition_forbidden_message;
26008 parser->type_definition_forbidden_message
26009 = G_("types may not be defined in an exception-specification");
26010
26011 bool non_constant_p;
26012 expr
26013 = cp_parser_constant_expression (parser,
26014 /*allow_non_constant=*/true,
26015 &non_constant_p);
26016 if (non_constant_p
26017 && !require_potential_rvalue_constant_expression (expr))
26018 {
26019 expr = NULL_TREE;
26020 return_cond = true;
26021 }
26022
26023 /* Restore the saved message. */
26024 parser->type_definition_forbidden_message = saved_message;
26025 }
26026 else
26027 {
26028 expr = cp_parser_expression (parser);
26029 *consumed_expr = true;
26030 }
26031
26032 parens.require_close (parser);
26033 }
26034 else
26035 {
26036 expr = boolean_true_node;
26037 if (!require_constexpr)
26038 *consumed_expr = false;
26039 }
26040
26041 /* We cannot build a noexcept-spec right away because this will check
26042 that expr is a constexpr. */
26043 if (!return_cond)
26044 return build_noexcept_spec (expr, tf_warning_or_error);
26045 else
26046 return expr;
26047 }
26048 else
26049 return NULL_TREE;
26050 }
26051
26052 /* Parse an (optional) exception-specification.
26053
26054 exception-specification:
26055 throw ( type-id-list [opt] )
26056
26057 Returns a TREE_LIST representing the exception-specification. The
26058 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
26059 control parsing. QUALS are qualifiers indicating whether the (member)
26060 function is `const'. */
26061
26062 static tree
26063 cp_parser_exception_specification_opt (cp_parser* parser,
26064 cp_parser_flags flags)
26065 {
26066 cp_token *token;
26067 tree type_id_list;
26068 const char *saved_message;
26069
26070 /* Peek at the next token. */
26071 token = cp_lexer_peek_token (parser->lexer);
26072
26073 /* Is it a noexcept-specification? */
26074 type_id_list
26075 = cp_parser_noexcept_specification_opt (parser, flags,
26076 /*require_constexpr=*/true,
26077 /*consumed_expr=*/NULL,
26078 /*return_cond=*/false);
26079 if (type_id_list != NULL_TREE)
26080 return type_id_list;
26081
26082 /* If it's not `throw', then there's no exception-specification. */
26083 if (!cp_parser_is_keyword (token, RID_THROW))
26084 return NULL_TREE;
26085
26086 location_t loc = token->location;
26087
26088 /* Consume the `throw'. */
26089 cp_lexer_consume_token (parser->lexer);
26090
26091 /* Look for the `('. */
26092 matching_parens parens;
26093 parens.require_open (parser);
26094
26095 /* Peek at the next token. */
26096 token = cp_lexer_peek_token (parser->lexer);
26097 /* If it's not a `)', then there is a type-id-list. */
26098 if (token->type != CPP_CLOSE_PAREN)
26099 {
26100 /* Types may not be defined in an exception-specification. */
26101 saved_message = parser->type_definition_forbidden_message;
26102 parser->type_definition_forbidden_message
26103 = G_("types may not be defined in an exception-specification");
26104 /* Parse the type-id-list. */
26105 type_id_list = cp_parser_type_id_list (parser);
26106 /* Restore the saved message. */
26107 parser->type_definition_forbidden_message = saved_message;
26108
26109 if (cxx_dialect >= cxx17)
26110 {
26111 error_at (loc, "ISO C++17 does not allow dynamic exception "
26112 "specifications");
26113 type_id_list = NULL_TREE;
26114 }
26115 else if (cxx_dialect >= cxx11)
26116 warning_at (loc, OPT_Wdeprecated,
26117 "dynamic exception specifications are deprecated in "
26118 "C++11");
26119 }
26120 /* In C++17, throw() is equivalent to noexcept (true). throw()
26121 is deprecated in C++11 and above as well, but is still widely used,
26122 so don't warn about it yet. */
26123 else if (cxx_dialect >= cxx17)
26124 type_id_list = noexcept_true_spec;
26125 else
26126 type_id_list = empty_except_spec;
26127
26128 /* Look for the `)'. */
26129 parens.require_close (parser);
26130
26131 return type_id_list;
26132 }
26133
26134 /* Parse an (optional) type-id-list.
26135
26136 type-id-list:
26137 type-id ... [opt]
26138 type-id-list , type-id ... [opt]
26139
26140 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
26141 in the order that the types were presented. */
26142
26143 static tree
26144 cp_parser_type_id_list (cp_parser* parser)
26145 {
26146 tree types = NULL_TREE;
26147
26148 while (true)
26149 {
26150 cp_token *token;
26151 tree type;
26152
26153 token = cp_lexer_peek_token (parser->lexer);
26154
26155 /* Get the next type-id. */
26156 type = cp_parser_type_id (parser);
26157 /* Check for invalid 'auto'. */
26158 if (flag_concepts && type_uses_auto (type))
26159 {
26160 error_at (token->location,
26161 "invalid use of %<auto%> in exception-specification");
26162 type = error_mark_node;
26163 }
26164 /* Parse the optional ellipsis. */
26165 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26166 {
26167 /* Consume the `...'. */
26168 cp_lexer_consume_token (parser->lexer);
26169
26170 /* Turn the type into a pack expansion expression. */
26171 type = make_pack_expansion (type);
26172 }
26173 /* Add it to the list. */
26174 types = add_exception_specifier (types, type, /*complain=*/1);
26175 /* Peek at the next token. */
26176 token = cp_lexer_peek_token (parser->lexer);
26177 /* If it is not a `,', we are done. */
26178 if (token->type != CPP_COMMA)
26179 break;
26180 /* Consume the `,'. */
26181 cp_lexer_consume_token (parser->lexer);
26182 }
26183
26184 return nreverse (types);
26185 }
26186
26187 /* Parse a try-block.
26188
26189 try-block:
26190 try compound-statement handler-seq */
26191
26192 static tree
26193 cp_parser_try_block (cp_parser* parser)
26194 {
26195 tree try_block;
26196
26197 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
26198 if (parser->in_function_body
26199 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
26200 && cxx_dialect < cxx20)
26201 pedwarn (input_location, 0,
26202 "%<try%> in %<constexpr%> function only "
26203 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26204
26205 try_block = begin_try_block ();
26206 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
26207 finish_try_block (try_block);
26208 cp_parser_handler_seq (parser);
26209 finish_handler_sequence (try_block);
26210
26211 return try_block;
26212 }
26213
26214 /* Parse a function-try-block.
26215
26216 function-try-block:
26217 try ctor-initializer [opt] function-body handler-seq */
26218
26219 static void
26220 cp_parser_function_try_block (cp_parser* parser)
26221 {
26222 tree compound_stmt;
26223 tree try_block;
26224
26225 /* Look for the `try' keyword. */
26226 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
26227 return;
26228 /* Let the rest of the front end know where we are. */
26229 try_block = begin_function_try_block (&compound_stmt);
26230 /* Parse the function-body. */
26231 cp_parser_ctor_initializer_opt_and_function_body
26232 (parser, /*in_function_try_block=*/true);
26233 /* We're done with the `try' part. */
26234 finish_function_try_block (try_block);
26235 /* Parse the handlers. */
26236 cp_parser_handler_seq (parser);
26237 /* We're done with the handlers. */
26238 finish_function_handler_sequence (try_block, compound_stmt);
26239 }
26240
26241 /* Parse a handler-seq.
26242
26243 handler-seq:
26244 handler handler-seq [opt] */
26245
26246 static void
26247 cp_parser_handler_seq (cp_parser* parser)
26248 {
26249 while (true)
26250 {
26251 cp_token *token;
26252
26253 /* Parse the handler. */
26254 cp_parser_handler (parser);
26255 /* Peek at the next token. */
26256 token = cp_lexer_peek_token (parser->lexer);
26257 /* If it's not `catch' then there are no more handlers. */
26258 if (!cp_parser_is_keyword (token, RID_CATCH))
26259 break;
26260 }
26261 }
26262
26263 /* Parse a handler.
26264
26265 handler:
26266 catch ( exception-declaration ) compound-statement */
26267
26268 static void
26269 cp_parser_handler (cp_parser* parser)
26270 {
26271 tree handler;
26272 tree declaration;
26273
26274 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
26275 handler = begin_handler ();
26276 matching_parens parens;
26277 parens.require_open (parser);
26278 declaration = cp_parser_exception_declaration (parser);
26279 finish_handler_parms (declaration, handler);
26280 parens.require_close (parser);
26281 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
26282 finish_handler (handler);
26283 }
26284
26285 /* Parse an exception-declaration.
26286
26287 exception-declaration:
26288 type-specifier-seq declarator
26289 type-specifier-seq abstract-declarator
26290 type-specifier-seq
26291 ...
26292
26293 Returns a VAR_DECL for the declaration, or NULL_TREE if the
26294 ellipsis variant is used. */
26295
26296 static tree
26297 cp_parser_exception_declaration (cp_parser* parser)
26298 {
26299 cp_decl_specifier_seq type_specifiers;
26300 cp_declarator *declarator;
26301 const char *saved_message;
26302
26303 /* If it's an ellipsis, it's easy to handle. */
26304 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26305 {
26306 /* Consume the `...' token. */
26307 cp_lexer_consume_token (parser->lexer);
26308 return NULL_TREE;
26309 }
26310
26311 /* Types may not be defined in exception-declarations. */
26312 saved_message = parser->type_definition_forbidden_message;
26313 parser->type_definition_forbidden_message
26314 = G_("types may not be defined in exception-declarations");
26315
26316 /* Parse the type-specifier-seq. */
26317 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
26318 /*is_declaration=*/true,
26319 /*is_trailing_return=*/false,
26320 &type_specifiers);
26321 /* If it's a `)', then there is no declarator. */
26322 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26323 declarator = NULL;
26324 else
26325 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
26326 CP_PARSER_FLAGS_NONE,
26327 /*ctor_dtor_or_conv_p=*/NULL,
26328 /*parenthesized_p=*/NULL,
26329 /*member_p=*/false,
26330 /*friend_p=*/false,
26331 /*static_p=*/false);
26332
26333 /* Restore the saved message. */
26334 parser->type_definition_forbidden_message = saved_message;
26335
26336 if (!type_specifiers.any_specifiers_p)
26337 return error_mark_node;
26338
26339 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
26340 }
26341
26342 /* Parse a throw-expression.
26343
26344 throw-expression:
26345 throw assignment-expression [opt]
26346
26347 Returns a THROW_EXPR representing the throw-expression. */
26348
26349 static tree
26350 cp_parser_throw_expression (cp_parser* parser)
26351 {
26352 tree expression;
26353 cp_token* token;
26354 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
26355
26356 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
26357 token = cp_lexer_peek_token (parser->lexer);
26358 /* Figure out whether or not there is an assignment-expression
26359 following the "throw" keyword. */
26360 if (token->type == CPP_COMMA
26361 || token->type == CPP_SEMICOLON
26362 || token->type == CPP_CLOSE_PAREN
26363 || token->type == CPP_CLOSE_SQUARE
26364 || token->type == CPP_CLOSE_BRACE
26365 || token->type == CPP_COLON)
26366 expression = NULL_TREE;
26367 else
26368 expression = cp_parser_assignment_expression (parser);
26369
26370 /* Construct a location e.g.:
26371 throw x
26372 ^~~~~~~
26373 with caret == start at the start of the "throw" token, and
26374 the end at the end of the final token we consumed. */
26375 location_t combined_loc = make_location (start_loc, start_loc,
26376 parser->lexer);
26377 expression = build_throw (combined_loc, expression);
26378
26379 return expression;
26380 }
26381
26382 /* Parse a yield-expression.
26383
26384 yield-expression:
26385 co_yield assignment-expression
26386 co_yield braced-init-list
26387
26388 Returns a CO_YIELD_EXPR representing the yield-expression. */
26389
26390 static tree
26391 cp_parser_yield_expression (cp_parser* parser)
26392 {
26393 tree expr;
26394
26395 cp_token *token = cp_lexer_peek_token (parser->lexer);
26396 location_t kw_loc = token->location; /* Save for later. */
26397
26398 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
26399
26400 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26401 {
26402 bool expr_non_constant_p;
26403 cp_lexer_set_source_position (parser->lexer);
26404 /* ??? : probably a moot point? */
26405 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26406 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
26407 }
26408 else
26409 expr = cp_parser_assignment_expression (parser);
26410
26411 if (expr == error_mark_node)
26412 return expr;
26413
26414 return finish_co_yield_expr (kw_loc, expr);
26415 }
26416
26417 /* GNU Extensions */
26418
26419 /* Parse an (optional) asm-specification.
26420
26421 asm-specification:
26422 asm ( string-literal )
26423
26424 If the asm-specification is present, returns a STRING_CST
26425 corresponding to the string-literal. Otherwise, returns
26426 NULL_TREE. */
26427
26428 static tree
26429 cp_parser_asm_specification_opt (cp_parser* parser)
26430 {
26431 cp_token *token;
26432 tree asm_specification;
26433
26434 /* Peek at the next token. */
26435 token = cp_lexer_peek_token (parser->lexer);
26436 /* If the next token isn't the `asm' keyword, then there's no
26437 asm-specification. */
26438 if (!cp_parser_is_keyword (token, RID_ASM))
26439 return NULL_TREE;
26440
26441 /* Consume the `asm' token. */
26442 cp_lexer_consume_token (parser->lexer);
26443 /* Look for the `('. */
26444 matching_parens parens;
26445 parens.require_open (parser);
26446
26447 /* Look for the string-literal. */
26448 asm_specification = cp_parser_string_literal (parser, false, false);
26449
26450 /* Look for the `)'. */
26451 parens.require_close (parser);
26452
26453 return asm_specification;
26454 }
26455
26456 /* Parse an asm-operand-list.
26457
26458 asm-operand-list:
26459 asm-operand
26460 asm-operand-list , asm-operand
26461
26462 asm-operand:
26463 string-literal ( expression )
26464 [ string-literal ] string-literal ( expression )
26465
26466 Returns a TREE_LIST representing the operands. The TREE_VALUE of
26467 each node is the expression. The TREE_PURPOSE is itself a
26468 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
26469 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
26470 is a STRING_CST for the string literal before the parenthesis. Returns
26471 ERROR_MARK_NODE if any of the operands are invalid. */
26472
26473 static tree
26474 cp_parser_asm_operand_list (cp_parser* parser)
26475 {
26476 tree asm_operands = NULL_TREE;
26477 bool invalid_operands = false;
26478
26479 while (true)
26480 {
26481 tree string_literal;
26482 tree expression;
26483 tree name;
26484
26485 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
26486 {
26487 /* Consume the `[' token. */
26488 cp_lexer_consume_token (parser->lexer);
26489 /* Read the operand name. */
26490 name = cp_parser_identifier (parser);
26491 if (name != error_mark_node)
26492 name = build_string (IDENTIFIER_LENGTH (name),
26493 IDENTIFIER_POINTER (name));
26494 /* Look for the closing `]'. */
26495 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26496 }
26497 else
26498 name = NULL_TREE;
26499 /* Look for the string-literal. */
26500 string_literal = cp_parser_string_literal (parser, false, false);
26501
26502 /* Look for the `('. */
26503 matching_parens parens;
26504 parens.require_open (parser);
26505 /* Parse the expression. */
26506 expression = cp_parser_expression (parser);
26507 /* Look for the `)'. */
26508 parens.require_close (parser);
26509
26510 if (name == error_mark_node
26511 || string_literal == error_mark_node
26512 || expression == error_mark_node)
26513 invalid_operands = true;
26514
26515 /* Add this operand to the list. */
26516 asm_operands = tree_cons (build_tree_list (name, string_literal),
26517 expression,
26518 asm_operands);
26519 /* If the next token is not a `,', there are no more
26520 operands. */
26521 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26522 break;
26523 /* Consume the `,'. */
26524 cp_lexer_consume_token (parser->lexer);
26525 }
26526
26527 return invalid_operands ? error_mark_node : nreverse (asm_operands);
26528 }
26529
26530 /* Parse an asm-clobber-list.
26531
26532 asm-clobber-list:
26533 string-literal
26534 asm-clobber-list , string-literal
26535
26536 Returns a TREE_LIST, indicating the clobbers in the order that they
26537 appeared. The TREE_VALUE of each node is a STRING_CST. */
26538
26539 static tree
26540 cp_parser_asm_clobber_list (cp_parser* parser)
26541 {
26542 tree clobbers = NULL_TREE;
26543
26544 while (true)
26545 {
26546 tree string_literal;
26547
26548 /* Look for the string literal. */
26549 string_literal = cp_parser_string_literal (parser, false, false);
26550 /* Add it to the list. */
26551 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
26552 /* If the next token is not a `,', then the list is
26553 complete. */
26554 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26555 break;
26556 /* Consume the `,' token. */
26557 cp_lexer_consume_token (parser->lexer);
26558 }
26559
26560 return clobbers;
26561 }
26562
26563 /* Parse an asm-label-list.
26564
26565 asm-label-list:
26566 identifier
26567 asm-label-list , identifier
26568
26569 Returns a TREE_LIST, indicating the labels in the order that they
26570 appeared. The TREE_VALUE of each node is a label. */
26571
26572 static tree
26573 cp_parser_asm_label_list (cp_parser* parser)
26574 {
26575 tree labels = NULL_TREE;
26576
26577 while (true)
26578 {
26579 tree identifier, label, name;
26580
26581 /* Look for the identifier. */
26582 identifier = cp_parser_identifier (parser);
26583 if (!error_operand_p (identifier))
26584 {
26585 label = lookup_label (identifier);
26586 if (TREE_CODE (label) == LABEL_DECL)
26587 {
26588 TREE_USED (label) = 1;
26589 check_goto (label);
26590 name = build_string (IDENTIFIER_LENGTH (identifier),
26591 IDENTIFIER_POINTER (identifier));
26592 labels = tree_cons (name, label, labels);
26593 }
26594 }
26595 /* If the next token is not a `,', then the list is
26596 complete. */
26597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26598 break;
26599 /* Consume the `,' token. */
26600 cp_lexer_consume_token (parser->lexer);
26601 }
26602
26603 return nreverse (labels);
26604 }
26605
26606 /* Return TRUE iff the next tokens in the stream are possibly the
26607 beginning of a GNU extension attribute. */
26608
26609 static bool
26610 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
26611 {
26612 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
26613 }
26614
26615 /* Return TRUE iff the next tokens in the stream are possibly the
26616 beginning of a standard C++-11 attribute specifier. */
26617
26618 static bool
26619 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
26620 {
26621 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
26622 }
26623
26624 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26625 beginning of a standard C++-11 attribute specifier. */
26626
26627 static bool
26628 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
26629 {
26630 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
26631
26632 return (cxx_dialect >= cxx11
26633 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
26634 || (token->type == CPP_OPEN_SQUARE
26635 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
26636 && token->type == CPP_OPEN_SQUARE)));
26637 }
26638
26639 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26640 beginning of a GNU extension attribute. */
26641
26642 static bool
26643 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
26644 {
26645 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
26646
26647 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
26648 }
26649
26650 /* Return true iff the next tokens can be the beginning of either a
26651 GNU attribute list, or a standard C++11 attribute sequence. */
26652
26653 static bool
26654 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
26655 {
26656 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
26657 || cp_next_tokens_can_be_std_attribute_p (parser));
26658 }
26659
26660 /* Return true iff the next Nth tokens can be the beginning of either
26661 a GNU attribute list, or a standard C++11 attribute sequence. */
26662
26663 static bool
26664 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
26665 {
26666 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
26667 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
26668 }
26669
26670 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
26671 of GNU attributes, or return NULL. */
26672
26673 static tree
26674 cp_parser_attributes_opt (cp_parser *parser)
26675 {
26676 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
26677 return cp_parser_gnu_attributes_opt (parser);
26678 return cp_parser_std_attribute_spec_seq (parser);
26679 }
26680
26681 /* Parse an (optional) series of attributes.
26682
26683 attributes:
26684 attributes attribute
26685
26686 attribute:
26687 __attribute__ (( attribute-list [opt] ))
26688
26689 The return value is as for cp_parser_gnu_attribute_list. */
26690
26691 static tree
26692 cp_parser_gnu_attributes_opt (cp_parser* parser)
26693 {
26694 tree attributes = NULL_TREE;
26695
26696 temp_override<bool> cleanup
26697 (parser->auto_is_implicit_function_template_parm_p, false);
26698
26699 while (true)
26700 {
26701 cp_token *token;
26702 tree attribute_list;
26703 bool ok = true;
26704
26705 /* Peek at the next token. */
26706 token = cp_lexer_peek_token (parser->lexer);
26707 /* If it's not `__attribute__', then we're done. */
26708 if (token->keyword != RID_ATTRIBUTE)
26709 break;
26710
26711 /* Consume the `__attribute__' keyword. */
26712 cp_lexer_consume_token (parser->lexer);
26713 /* Look for the two `(' tokens. */
26714 matching_parens outer_parens;
26715 if (!outer_parens.require_open (parser))
26716 ok = false;
26717 matching_parens inner_parens;
26718 if (!inner_parens.require_open (parser))
26719 ok = false;
26720
26721 /* Peek at the next token. */
26722 token = cp_lexer_peek_token (parser->lexer);
26723 if (token->type != CPP_CLOSE_PAREN)
26724 /* Parse the attribute-list. */
26725 attribute_list = cp_parser_gnu_attribute_list (parser);
26726 else
26727 /* If the next token is a `)', then there is no attribute
26728 list. */
26729 attribute_list = NULL;
26730
26731 /* Look for the two `)' tokens. */
26732 if (!inner_parens.require_close (parser))
26733 ok = false;
26734 if (!outer_parens.require_close (parser))
26735 ok = false;
26736 if (!ok)
26737 cp_parser_skip_to_end_of_statement (parser);
26738
26739 /* Add these new attributes to the list. */
26740 attributes = attr_chainon (attributes, attribute_list);
26741 }
26742
26743 return attributes;
26744 }
26745
26746 /* Parse a GNU attribute-list.
26747
26748 attribute-list:
26749 attribute
26750 attribute-list , attribute
26751
26752 attribute:
26753 identifier
26754 identifier ( identifier )
26755 identifier ( identifier , expression-list )
26756 identifier ( expression-list )
26757
26758 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
26759 to an attribute. The TREE_PURPOSE of each node is the identifier
26760 indicating which attribute is in use. The TREE_VALUE represents
26761 the arguments, if any. */
26762
26763 static tree
26764 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
26765 {
26766 tree attribute_list = NULL_TREE;
26767 bool save_translate_strings_p = parser->translate_strings_p;
26768
26769 /* Don't create wrapper nodes within attributes: the
26770 handlers don't know how to handle them. */
26771 auto_suppress_location_wrappers sentinel;
26772
26773 parser->translate_strings_p = false;
26774 while (true)
26775 {
26776 cp_token *token;
26777 tree identifier;
26778 tree attribute;
26779
26780 /* Look for the identifier. We also allow keywords here; for
26781 example `__attribute__ ((const))' is legal. */
26782 token = cp_lexer_peek_token (parser->lexer);
26783 if (token->type == CPP_NAME
26784 || token->type == CPP_KEYWORD)
26785 {
26786 tree arguments = NULL_TREE;
26787
26788 /* Consume the token, but save it since we need it for the
26789 SIMD enabled function parsing. */
26790 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
26791
26792 /* Save away the identifier that indicates which attribute
26793 this is. */
26794 identifier = (token->type == CPP_KEYWORD)
26795 /* For keywords, use the canonical spelling, not the
26796 parsed identifier. */
26797 ? ridpointers[(int) token->keyword]
26798 : id_token->u.value;
26799
26800 identifier = canonicalize_attr_name (identifier);
26801 attribute = build_tree_list (identifier, NULL_TREE);
26802
26803 /* Peek at the next token. */
26804 token = cp_lexer_peek_token (parser->lexer);
26805 /* If it's an `(', then parse the attribute arguments. */
26806 if (token->type == CPP_OPEN_PAREN)
26807 {
26808 vec<tree, va_gc> *vec;
26809 int attr_flag = (attribute_takes_identifier_p (identifier)
26810 ? id_attr : normal_attr);
26811 vec = cp_parser_parenthesized_expression_list
26812 (parser, attr_flag, /*cast_p=*/false,
26813 /*allow_expansion_p=*/false,
26814 /*non_constant_p=*/NULL);
26815 if (vec == NULL)
26816 arguments = error_mark_node;
26817 else
26818 {
26819 arguments = build_tree_list_vec (vec);
26820 release_tree_vector (vec);
26821 }
26822 /* Save the arguments away. */
26823 TREE_VALUE (attribute) = arguments;
26824 }
26825
26826 if (arguments != error_mark_node)
26827 {
26828 /* Add this attribute to the list. */
26829 TREE_CHAIN (attribute) = attribute_list;
26830 attribute_list = attribute;
26831 }
26832
26833 token = cp_lexer_peek_token (parser->lexer);
26834 }
26835 /* Unless EXACTLY_ONE is set look for more attributes.
26836 If the next token isn't a `,', we're done. */
26837 if (exactly_one || token->type != CPP_COMMA)
26838 break;
26839
26840 /* Consume the comma and keep going. */
26841 cp_lexer_consume_token (parser->lexer);
26842 }
26843 parser->translate_strings_p = save_translate_strings_p;
26844
26845 /* We built up the list in reverse order. */
26846 return nreverse (attribute_list);
26847 }
26848
26849 /* Parse a standard C++11 attribute.
26850
26851 The returned representation is a TREE_LIST which TREE_PURPOSE is
26852 the scoped name of the attribute, and the TREE_VALUE is its
26853 arguments list.
26854
26855 Note that the scoped name of the attribute is itself a TREE_LIST
26856 which TREE_PURPOSE is the namespace of the attribute, and
26857 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
26858 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
26859 and which TREE_PURPOSE is directly the attribute name.
26860
26861 Clients of the attribute code should use get_attribute_namespace
26862 and get_attribute_name to get the actual namespace and name of
26863 attributes, regardless of their being GNU or C++11 attributes.
26864
26865 attribute:
26866 attribute-token attribute-argument-clause [opt]
26867
26868 attribute-token:
26869 identifier
26870 attribute-scoped-token
26871
26872 attribute-scoped-token:
26873 attribute-namespace :: identifier
26874
26875 attribute-namespace:
26876 identifier
26877
26878 attribute-argument-clause:
26879 ( balanced-token-seq )
26880
26881 balanced-token-seq:
26882 balanced-token [opt]
26883 balanced-token-seq balanced-token
26884
26885 balanced-token:
26886 ( balanced-token-seq )
26887 [ balanced-token-seq ]
26888 { balanced-token-seq }. */
26889
26890 static tree
26891 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
26892 {
26893 tree attribute, attr_id = NULL_TREE, arguments;
26894 cp_token *token;
26895
26896 temp_override<bool> cleanup
26897 (parser->auto_is_implicit_function_template_parm_p, false);
26898
26899 /* First, parse name of the attribute, a.k.a attribute-token. */
26900
26901 token = cp_lexer_peek_token (parser->lexer);
26902 if (token->type == CPP_NAME)
26903 attr_id = token->u.value;
26904 else if (token->type == CPP_KEYWORD)
26905 attr_id = ridpointers[(int) token->keyword];
26906 else if (token->flags & NAMED_OP)
26907 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26908
26909 if (attr_id == NULL_TREE)
26910 return NULL_TREE;
26911
26912 cp_lexer_consume_token (parser->lexer);
26913
26914 token = cp_lexer_peek_token (parser->lexer);
26915 if (token->type == CPP_SCOPE)
26916 {
26917 /* We are seeing a scoped attribute token. */
26918
26919 cp_lexer_consume_token (parser->lexer);
26920 if (attr_ns)
26921 error_at (token->location, "attribute using prefix used together "
26922 "with scoped attribute token");
26923 attr_ns = attr_id;
26924
26925 token = cp_lexer_peek_token (parser->lexer);
26926 if (token->type == CPP_NAME)
26927 attr_id = token->u.value;
26928 else if (token->type == CPP_KEYWORD)
26929 attr_id = ridpointers[(int) token->keyword];
26930 else if (token->flags & NAMED_OP)
26931 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26932 else
26933 {
26934 error_at (token->location,
26935 "expected an identifier for the attribute name");
26936 return error_mark_node;
26937 }
26938 cp_lexer_consume_token (parser->lexer);
26939
26940 attr_ns = canonicalize_attr_name (attr_ns);
26941 attr_id = canonicalize_attr_name (attr_id);
26942 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26943 NULL_TREE);
26944 token = cp_lexer_peek_token (parser->lexer);
26945 }
26946 else if (attr_ns)
26947 {
26948 attr_ns = canonicalize_attr_name (attr_ns);
26949 attr_id = canonicalize_attr_name (attr_id);
26950 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26951 NULL_TREE);
26952 }
26953 else
26954 {
26955 attr_id = canonicalize_attr_name (attr_id);
26956 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
26957 NULL_TREE);
26958 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
26959 but no longer: we have to be able to tell [[noreturn]] and
26960 __attribute__((noreturn)) apart. */
26961 /* C++14 deprecated attribute is equivalent to GNU's. */
26962 if (is_attribute_p ("deprecated", attr_id))
26963 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26964 /* C++17 fallthrough attribute is equivalent to GNU's. */
26965 else if (is_attribute_p ("fallthrough", attr_id))
26966 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26967 /* Transactional Memory TS optimize_for_synchronized attribute is
26968 equivalent to GNU transaction_callable. */
26969 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
26970 TREE_PURPOSE (attribute)
26971 = get_identifier ("transaction_callable");
26972 /* Transactional Memory attributes are GNU attributes. */
26973 else if (tm_attr_to_mask (attr_id))
26974 TREE_PURPOSE (attribute) = attr_id;
26975 }
26976
26977 /* Now parse the optional argument clause of the attribute. */
26978
26979 if (token->type != CPP_OPEN_PAREN)
26980 return attribute;
26981
26982 {
26983 vec<tree, va_gc> *vec;
26984 int attr_flag = normal_attr;
26985
26986 /* Maybe we don't expect to see any arguments for this attribute. */
26987 const attribute_spec *as
26988 = lookup_attribute_spec (TREE_PURPOSE (attribute));
26989 if (as && as->max_length == 0)
26990 {
26991 error_at (token->location, "%qE attribute does not take any arguments",
26992 attr_id);
26993 cp_parser_skip_to_closing_parenthesis (parser,
26994 /*recovering=*/true,
26995 /*or_comma=*/false,
26996 /*consume_paren=*/true);
26997 return error_mark_node;
26998 }
26999
27000 if (attr_ns == gnu_identifier
27001 && attribute_takes_identifier_p (attr_id))
27002 /* A GNU attribute that takes an identifier in parameter. */
27003 attr_flag = id_attr;
27004
27005 if (as == NULL)
27006 {
27007 /* For unknown attributes, just skip balanced tokens instead of
27008 trying to parse the arguments. */
27009 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
27010 cp_lexer_consume_token (parser->lexer);
27011 return attribute;
27012 }
27013
27014 vec = cp_parser_parenthesized_expression_list
27015 (parser, attr_flag, /*cast_p=*/false,
27016 /*allow_expansion_p=*/true,
27017 /*non_constant_p=*/NULL);
27018 if (vec == NULL)
27019 arguments = error_mark_node;
27020 else
27021 {
27022 if (vec->is_empty ())
27023 /* e.g. [[attr()]]. */
27024 error_at (token->location, "parentheses must be omitted if "
27025 "%qE attribute argument list is empty",
27026 attr_id);
27027 arguments = build_tree_list_vec (vec);
27028 release_tree_vector (vec);
27029 }
27030
27031 if (arguments == error_mark_node)
27032 attribute = error_mark_node;
27033 else
27034 TREE_VALUE (attribute) = arguments;
27035 }
27036
27037 return attribute;
27038 }
27039
27040 /* Check that the attribute ATTRIBUTE appears at most once in the
27041 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3),
27042 nodiscard, and deprecated (7.6.5). Note that
27043 carries_dependency (7.6.4) isn't implemented yet in GCC. */
27044
27045 static void
27046 cp_parser_check_std_attribute (tree attributes, tree attribute)
27047 {
27048 if (attributes)
27049 {
27050 tree name = get_attribute_name (attribute);
27051 if (is_attribute_p ("noreturn", name)
27052 && lookup_attribute ("noreturn", attributes))
27053 error ("attribute %<noreturn%> can appear at most once "
27054 "in an attribute-list");
27055 else if (is_attribute_p ("deprecated", name)
27056 && lookup_attribute ("deprecated", attributes))
27057 error ("attribute %<deprecated%> can appear at most once "
27058 "in an attribute-list");
27059 else if (is_attribute_p ("nodiscard", name)
27060 && lookup_attribute ("nodiscard", attributes))
27061 error ("attribute %<nodiscard%> can appear at most once "
27062 "in an attribute-list");
27063 }
27064 }
27065
27066 /* Parse a list of standard C++-11 attributes.
27067
27068 attribute-list:
27069 attribute [opt]
27070 attribute-list , attribute[opt]
27071 attribute ...
27072 attribute-list , attribute ...
27073 */
27074
27075 static tree
27076 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
27077 {
27078 tree attributes = NULL_TREE, attribute = NULL_TREE;
27079 cp_token *token = NULL;
27080
27081 while (true)
27082 {
27083 attribute = cp_parser_std_attribute (parser, attr_ns);
27084 if (attribute == error_mark_node)
27085 break;
27086 if (attribute != NULL_TREE)
27087 {
27088 cp_parser_check_std_attribute (attributes, attribute);
27089 TREE_CHAIN (attribute) = attributes;
27090 attributes = attribute;
27091 }
27092 token = cp_lexer_peek_token (parser->lexer);
27093 if (token->type == CPP_ELLIPSIS)
27094 {
27095 cp_lexer_consume_token (parser->lexer);
27096 if (attribute == NULL_TREE)
27097 error_at (token->location,
27098 "expected attribute before %<...%>");
27099 else
27100 {
27101 tree pack = make_pack_expansion (TREE_VALUE (attribute));
27102 if (pack == error_mark_node)
27103 return error_mark_node;
27104 TREE_VALUE (attribute) = pack;
27105 }
27106 token = cp_lexer_peek_token (parser->lexer);
27107 }
27108 if (token->type != CPP_COMMA)
27109 break;
27110 cp_lexer_consume_token (parser->lexer);
27111 }
27112 attributes = nreverse (attributes);
27113 return attributes;
27114 }
27115
27116 /* Parse a standard C++-11 attribute specifier.
27117
27118 attribute-specifier:
27119 [ [ attribute-using-prefix [opt] attribute-list ] ]
27120 alignment-specifier
27121
27122 attribute-using-prefix:
27123 using attribute-namespace :
27124
27125 alignment-specifier:
27126 alignas ( type-id ... [opt] )
27127 alignas ( alignment-expression ... [opt] ). */
27128
27129 static tree
27130 cp_parser_std_attribute_spec (cp_parser *parser)
27131 {
27132 tree attributes = NULL_TREE;
27133 cp_token *token = cp_lexer_peek_token (parser->lexer);
27134
27135 if (token->type == CPP_OPEN_SQUARE
27136 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
27137 {
27138 tree attr_ns = NULL_TREE;
27139
27140 cp_lexer_consume_token (parser->lexer);
27141 cp_lexer_consume_token (parser->lexer);
27142
27143 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27144 {
27145 token = cp_lexer_peek_nth_token (parser->lexer, 2);
27146 if (token->type == CPP_NAME)
27147 attr_ns = token->u.value;
27148 else if (token->type == CPP_KEYWORD)
27149 attr_ns = ridpointers[(int) token->keyword];
27150 else if (token->flags & NAMED_OP)
27151 attr_ns = get_identifier (cpp_type2name (token->type,
27152 token->flags));
27153 if (attr_ns
27154 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
27155 {
27156 if (cxx_dialect < cxx17)
27157 pedwarn (input_location, 0,
27158 "attribute using prefix only available "
27159 "with %<-std=c++17%> or %<-std=gnu++17%>");
27160
27161 cp_lexer_consume_token (parser->lexer);
27162 cp_lexer_consume_token (parser->lexer);
27163 cp_lexer_consume_token (parser->lexer);
27164 }
27165 else
27166 attr_ns = NULL_TREE;
27167 }
27168
27169 attributes = cp_parser_std_attribute_list (parser, attr_ns);
27170
27171 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
27172 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
27173 cp_parser_skip_to_end_of_statement (parser);
27174 else
27175 /* Warn about parsing c++11 attribute in non-c++11 mode, only
27176 when we are sure that we have actually parsed them. */
27177 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
27178 }
27179 else
27180 {
27181 tree alignas_expr;
27182
27183 /* Look for an alignment-specifier. */
27184
27185 token = cp_lexer_peek_token (parser->lexer);
27186
27187 if (token->type != CPP_KEYWORD
27188 || token->keyword != RID_ALIGNAS)
27189 return NULL_TREE;
27190
27191 cp_lexer_consume_token (parser->lexer);
27192 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
27193
27194 matching_parens parens;
27195 if (!parens.require_open (parser))
27196 return error_mark_node;
27197
27198 cp_parser_parse_tentatively (parser);
27199 alignas_expr = cp_parser_type_id (parser);
27200
27201 if (!cp_parser_parse_definitely (parser))
27202 {
27203 alignas_expr = cp_parser_assignment_expression (parser);
27204 if (alignas_expr == error_mark_node)
27205 cp_parser_skip_to_end_of_statement (parser);
27206 if (alignas_expr == NULL_TREE
27207 || alignas_expr == error_mark_node)
27208 return alignas_expr;
27209 }
27210
27211 alignas_expr = cxx_alignas_expr (alignas_expr);
27212 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
27213
27214 /* Handle alignas (pack...). */
27215 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27216 {
27217 cp_lexer_consume_token (parser->lexer);
27218 alignas_expr = make_pack_expansion (alignas_expr);
27219 }
27220
27221 /* Something went wrong, so don't build the attribute. */
27222 if (alignas_expr == error_mark_node)
27223 return error_mark_node;
27224
27225 /* Missing ')' means the code cannot possibly be valid; go ahead
27226 and commit to make sure we issue a hard error. */
27227 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
27228 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27229 cp_parser_commit_to_tentative_parse (parser);
27230
27231 if (!parens.require_close (parser))
27232 return error_mark_node;
27233
27234 /* Build the C++-11 representation of an 'aligned'
27235 attribute. */
27236 attributes
27237 = build_tree_list (build_tree_list (gnu_identifier,
27238 aligned_identifier), alignas_expr);
27239 }
27240
27241 return attributes;
27242 }
27243
27244 /* Parse a standard C++-11 attribute-specifier-seq.
27245
27246 attribute-specifier-seq:
27247 attribute-specifier-seq [opt] attribute-specifier
27248 */
27249
27250 static tree
27251 cp_parser_std_attribute_spec_seq (cp_parser *parser)
27252 {
27253 tree attr_specs = NULL_TREE;
27254 tree attr_last = NULL_TREE;
27255
27256 /* Don't create wrapper nodes within attributes: the
27257 handlers don't know how to handle them. */
27258 auto_suppress_location_wrappers sentinel;
27259
27260 while (true)
27261 {
27262 tree attr_spec = cp_parser_std_attribute_spec (parser);
27263 if (attr_spec == NULL_TREE)
27264 break;
27265 if (attr_spec == error_mark_node)
27266 return error_mark_node;
27267
27268 if (attr_last)
27269 TREE_CHAIN (attr_last) = attr_spec;
27270 else
27271 attr_specs = attr_last = attr_spec;
27272 attr_last = tree_last (attr_last);
27273 }
27274
27275 return attr_specs;
27276 }
27277
27278 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
27279 return index of the first token after balanced-token, or N on failure. */
27280
27281 static size_t
27282 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
27283 {
27284 size_t orig_n = n;
27285 int nparens = 0, nbraces = 0, nsquares = 0;
27286 do
27287 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
27288 {
27289 case CPP_PRAGMA_EOL:
27290 if (!parser->lexer->in_pragma)
27291 break;
27292 /* FALLTHRU */
27293 case CPP_EOF:
27294 /* Ran out of tokens. */
27295 return orig_n;
27296 case CPP_OPEN_PAREN:
27297 ++nparens;
27298 break;
27299 case CPP_OPEN_BRACE:
27300 ++nbraces;
27301 break;
27302 case CPP_OPEN_SQUARE:
27303 ++nsquares;
27304 break;
27305 case CPP_CLOSE_PAREN:
27306 --nparens;
27307 break;
27308 case CPP_CLOSE_BRACE:
27309 --nbraces;
27310 break;
27311 case CPP_CLOSE_SQUARE:
27312 --nsquares;
27313 break;
27314 default:
27315 break;
27316 }
27317 while (nparens || nbraces || nsquares);
27318 return n;
27319 }
27320
27321 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
27322 return index of the first token after the GNU attribute tokens, or N on
27323 failure. */
27324
27325 static size_t
27326 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
27327 {
27328 while (true)
27329 {
27330 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
27331 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
27332 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
27333 break;
27334
27335 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
27336 if (n2 == n + 2)
27337 break;
27338 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
27339 break;
27340 n = n2 + 1;
27341 }
27342 return n;
27343 }
27344
27345 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
27346 next token), return index of the first token after the standard C++11
27347 attribute tokens, or N on failure. */
27348
27349 static size_t
27350 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
27351 {
27352 while (true)
27353 {
27354 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
27355 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
27356 {
27357 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
27358 if (n2 == n + 1)
27359 break;
27360 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
27361 break;
27362 n = n2 + 1;
27363 }
27364 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
27365 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
27366 {
27367 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
27368 if (n2 == n + 1)
27369 break;
27370 n = n2;
27371 }
27372 else
27373 break;
27374 }
27375 return n;
27376 }
27377
27378 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
27379 as the next token), return index of the first token after the attribute
27380 tokens, or N on failure. */
27381
27382 static size_t
27383 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
27384 {
27385 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
27386 return cp_parser_skip_gnu_attributes_opt (parser, n);
27387 return cp_parser_skip_std_attribute_spec_seq (parser, n);
27388 }
27389
27390 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
27391 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
27392 current value of the PEDANTIC flag, regardless of whether or not
27393 the `__extension__' keyword is present. The caller is responsible
27394 for restoring the value of the PEDANTIC flag. */
27395
27396 static bool
27397 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
27398 {
27399 /* Save the old value of the PEDANTIC flag. */
27400 *saved_pedantic = pedantic;
27401
27402 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
27403 {
27404 /* Consume the `__extension__' token. */
27405 cp_lexer_consume_token (parser->lexer);
27406 /* We're not being pedantic while the `__extension__' keyword is
27407 in effect. */
27408 pedantic = 0;
27409
27410 return true;
27411 }
27412
27413 return false;
27414 }
27415
27416 /* Parse a label declaration.
27417
27418 label-declaration:
27419 __label__ label-declarator-seq ;
27420
27421 label-declarator-seq:
27422 identifier , label-declarator-seq
27423 identifier */
27424
27425 static void
27426 cp_parser_label_declaration (cp_parser* parser)
27427 {
27428 /* Look for the `__label__' keyword. */
27429 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
27430
27431 while (true)
27432 {
27433 tree identifier;
27434
27435 /* Look for an identifier. */
27436 identifier = cp_parser_identifier (parser);
27437 /* If we failed, stop. */
27438 if (identifier == error_mark_node)
27439 break;
27440 /* Declare it as a label. */
27441 finish_label_decl (identifier);
27442 /* If the next token is a `;', stop. */
27443 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27444 break;
27445 /* Look for the `,' separating the label declarations. */
27446 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
27447 }
27448
27449 /* Look for the final `;'. */
27450 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27451 }
27452
27453 // -------------------------------------------------------------------------- //
27454 // Concept definitions
27455
27456 static tree
27457 cp_parser_concept_definition (cp_parser *parser)
27458 {
27459 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
27460 cp_lexer_consume_token (parser->lexer);
27461
27462 cp_expr id = cp_parser_identifier (parser);
27463 if (id == error_mark_node)
27464 {
27465 cp_parser_skip_to_end_of_statement (parser);
27466 cp_parser_consume_semicolon_at_end_of_statement (parser);
27467 return NULL_TREE;
27468 }
27469
27470 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27471 {
27472 cp_parser_skip_to_end_of_statement (parser);
27473 cp_parser_consume_semicolon_at_end_of_statement (parser);
27474 return error_mark_node;
27475 }
27476
27477 processing_constraint_expression_sentinel parsing_constraint;
27478 tree init = cp_parser_constraint_expression (parser);
27479 if (init == error_mark_node)
27480 cp_parser_skip_to_end_of_statement (parser);
27481
27482 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
27483 but continue as if it were. */
27484 cp_parser_consume_semicolon_at_end_of_statement (parser);
27485
27486 return finish_concept_definition (id, init);
27487 }
27488
27489 // -------------------------------------------------------------------------- //
27490 // Requires Clause
27491
27492 /* Diagnose an expression that should appear in ()'s within a requires-clause
27493 and suggest where to place those parentheses. */
27494
27495 static void
27496 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
27497 {
27498 error_at (loc, "expression must be enclosed in parentheses");
27499 }
27500
27501 static void
27502 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
27503 {
27504 gcc_rich_location richloc (loc);
27505 richloc.add_fixit_insert_before ("(");
27506 richloc.add_fixit_insert_after (")");
27507 error_at (&richloc, "expression must be enclosed in parentheses");
27508 }
27509
27510 /* Characterizes the likely kind of expression intended by a mis-written
27511 primary constraint. */
27512 enum primary_constraint_error
27513 {
27514 pce_ok,
27515 pce_maybe_operator,
27516 pce_maybe_postfix
27517 };
27518
27519 /* Returns true if the token(s) following a primary-expression in a
27520 constraint-logical-* expression would require parentheses. */
27521
27522 static primary_constraint_error
27523 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
27524 {
27525 cp_token *token = cp_lexer_peek_token (parser->lexer);
27526 switch (token->type)
27527 {
27528 default:
27529 return pce_ok;
27530
27531 case CPP_EQ:
27532 {
27533 /* An equal sign may be part of the definition of a function,
27534 and not an assignment operator, when parsing the expression
27535 for a trailing requires-clause. For example:
27536
27537 template<typename T>
27538 struct S {
27539 S() requires C<T> = default;
27540 };
27541
27542 Don't try to reparse this a binary operator. */
27543 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
27544 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
27545 return pce_ok;
27546
27547 gcc_fallthrough ();
27548 }
27549
27550 /* Arithmetic operators. */
27551 case CPP_PLUS:
27552 case CPP_MINUS:
27553 case CPP_MULT:
27554 case CPP_DIV:
27555 case CPP_MOD:
27556 /* Bitwise operators. */
27557 case CPP_AND:
27558 case CPP_OR:
27559 case CPP_XOR:
27560 case CPP_RSHIFT:
27561 case CPP_LSHIFT:
27562 /* Relational operators. */
27563 case CPP_EQ_EQ:
27564 case CPP_NOT_EQ:
27565 case CPP_LESS:
27566 case CPP_GREATER:
27567 case CPP_LESS_EQ:
27568 case CPP_GREATER_EQ:
27569 case CPP_SPACESHIP:
27570 /* Pointer-to-member. */
27571 case CPP_DOT_STAR:
27572 case CPP_DEREF_STAR:
27573 /* Assignment operators. */
27574 case CPP_PLUS_EQ:
27575 case CPP_MINUS_EQ:
27576 case CPP_MULT_EQ:
27577 case CPP_DIV_EQ:
27578 case CPP_MOD_EQ:
27579 case CPP_AND_EQ:
27580 case CPP_OR_EQ:
27581 case CPP_XOR_EQ:
27582 case CPP_RSHIFT_EQ:
27583 case CPP_LSHIFT_EQ:
27584 /* Conditional operator */
27585 case CPP_QUERY:
27586 /* Unenclosed binary or conditional operator. */
27587 return pce_maybe_operator;
27588
27589 case CPP_OPEN_PAREN:
27590 {
27591 /* A primary constraint that precedes the parameter-list of a
27592 lambda expression is followed by an open paren.
27593
27594 []<typename T> requires C (T a, T b) { ... }
27595
27596 Don't try to re-parse this as a postfix expression. */
27597 if (lambda_p)
27598 return pce_ok;
27599
27600 gcc_fallthrough ();
27601 }
27602 case CPP_OPEN_SQUARE:
27603 {
27604 /* A primary-constraint-expression followed by a '[[' is not a
27605 postfix expression. */
27606 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
27607 return pce_ok;
27608
27609 gcc_fallthrough ();
27610 }
27611 case CPP_PLUS_PLUS:
27612 case CPP_MINUS_MINUS:
27613 case CPP_DOT:
27614 case CPP_DEREF:
27615 /* Unenclosed postfix operator. */
27616 return pce_maybe_postfix;
27617 }
27618 }
27619
27620 /* Returns true if the next token begins a unary expression, preceded by
27621 an operator or keyword. */
27622
27623 static bool
27624 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
27625 {
27626 cp_token *token = cp_lexer_peek_token (parser->lexer);
27627 switch (token->type)
27628 {
27629 case CPP_NOT:
27630 case CPP_PLUS:
27631 case CPP_MINUS:
27632 case CPP_MULT:
27633 case CPP_COMPL:
27634 case CPP_PLUS_PLUS:
27635 case CPP_MINUS_MINUS:
27636 return true;
27637
27638 case CPP_KEYWORD:
27639 {
27640 switch (token->keyword)
27641 {
27642 case RID_STATCAST:
27643 case RID_DYNCAST:
27644 case RID_REINTCAST:
27645 case RID_CONSTCAST:
27646 case RID_TYPEID:
27647 case RID_SIZEOF:
27648 case RID_ALIGNOF:
27649 case RID_NOEXCEPT:
27650 case RID_NEW:
27651 case RID_DELETE:
27652 case RID_THROW:
27653 return true;
27654
27655 default:
27656 break;
27657 }
27658 }
27659
27660 default:
27661 break;
27662 }
27663
27664 return false;
27665 }
27666
27667 /* Parse a primary expression within a constraint. */
27668
27669 static cp_expr
27670 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
27671 {
27672 /* If this looks like a unary expression, parse it as such, but diagnose
27673 it as ill-formed; it requires parens. */
27674 if (cp_parser_unary_constraint_requires_parens (parser))
27675 {
27676 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
27677 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
27678 return e;
27679 }
27680
27681 cp_lexer_save_tokens (parser->lexer);
27682 cp_id_kind idk;
27683 location_t loc = input_location;
27684 cp_expr expr = cp_parser_primary_expression (parser,
27685 /*address_p=*/false,
27686 /*cast_p=*/false,
27687 /*template_arg_p=*/false,
27688 &idk);
27689 expr.maybe_add_location_wrapper ();
27690
27691 primary_constraint_error pce = pce_ok;
27692 if (expr != error_mark_node)
27693 {
27694 /* The primary-expression could be part of an unenclosed non-logical
27695 compound expression. */
27696 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
27697 }
27698 if (pce == pce_ok)
27699 {
27700 cp_lexer_commit_tokens (parser->lexer);
27701 return finish_constraint_primary_expr (expr);
27702 }
27703
27704 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
27705 error, but return the expression as if it were valid. */
27706 cp_lexer_rollback_tokens (parser->lexer);
27707 cp_parser_parse_tentatively (parser);
27708 if (pce == pce_maybe_operator)
27709 expr = cp_parser_assignment_expression (parser, NULL, false, false);
27710 else
27711 expr = cp_parser_simple_cast_expression (parser);
27712 if (cp_parser_parse_definitely (parser))
27713 {
27714 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
27715 return expr;
27716 }
27717
27718 /* Otherwise, something has gone very wrong, and we can't generate a more
27719 meaningful diagnostic or recover. */
27720 cp_parser_diagnose_ungrouped_constraint_plain (loc);
27721 return error_mark_node;
27722 }
27723
27724 /* Parse a constraint-logical-and-expression.
27725
27726 constraint-logical-and-expression:
27727 primary-expression
27728 constraint-logical-and-expression '&&' primary-expression */
27729
27730 static cp_expr
27731 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
27732 {
27733 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
27734 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
27735 {
27736 cp_token *op = cp_lexer_consume_token (parser->lexer);
27737 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
27738 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
27739 }
27740 return lhs;
27741 }
27742
27743 /* Parse a constraint-logical-or-expression.
27744
27745 constraint-logical-or-expression:
27746 constraint-logical-and-expression
27747 constraint-logical-or-expression '||' constraint-logical-and-expression */
27748
27749 static cp_expr
27750 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
27751 {
27752 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
27753 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
27754 {
27755 cp_token *op = cp_lexer_consume_token (parser->lexer);
27756 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
27757 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
27758 }
27759 return lhs;
27760 }
27761
27762 /* Parse the expression after a requires-clause. This has a different grammar
27763 than that in the concepts TS. */
27764
27765 static tree
27766 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
27767 {
27768 processing_constraint_expression_sentinel parsing_constraint;
27769 temp_override<int> ovr (processing_template_decl);
27770 if (!processing_template_decl)
27771 /* Adjust processing_template_decl so that we always obtain template
27772 trees here. We don't do the usual ++processing_template_decl
27773 because that would skew the template parameter depth of a lambda
27774 within if we're already inside a template. */
27775 processing_template_decl = 1;
27776 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
27777 if (check_for_bare_parameter_packs (expr))
27778 expr = error_mark_node;
27779 return expr;
27780 }
27781
27782 /* Parse a expression after a requires clause.
27783
27784 constraint-expression:
27785 logical-or-expression
27786
27787 The required logical-or-expression must be a constant expression. Note
27788 that we don't check that the expression is constepxr here. We defer until
27789 we analyze constraints and then, we only check atomic constraints. */
27790
27791 static tree
27792 cp_parser_constraint_expression (cp_parser *parser)
27793 {
27794 processing_constraint_expression_sentinel parsing_constraint;
27795 temp_override<int> ovr (processing_template_decl);
27796 if (!processing_template_decl)
27797 /* As in cp_parser_requires_clause_expression. */
27798 processing_template_decl = 1;
27799 cp_expr expr = cp_parser_binary_expression (parser, false, true,
27800 PREC_NOT_OPERATOR, NULL);
27801 if (check_for_bare_parameter_packs (expr))
27802 expr = error_mark_node;
27803 expr.maybe_add_location_wrapper ();
27804 return expr;
27805 }
27806
27807 /* Optionally parse a requires clause:
27808
27809 requires-clause:
27810 `requires` constraint-logical-or-expression.
27811 [ConceptsTS]
27812 `requires constraint-expression.
27813
27814 LAMBDA_P is true when the requires-clause is parsed before the
27815 parameter-list of a lambda-declarator. */
27816
27817 static tree
27818 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
27819 {
27820 cp_token *tok = cp_lexer_peek_token (parser->lexer);
27821 if (tok->keyword != RID_REQUIRES)
27822 {
27823 if (!flag_concepts && tok->type == CPP_NAME
27824 && tok->u.value == ridpointers[RID_REQUIRES])
27825 {
27826 error_at (cp_lexer_peek_token (parser->lexer)->location,
27827 "%<requires%> only available with "
27828 "%<-std=c++20%> or %<-fconcepts%>");
27829 /* Parse and discard the requires-clause. */
27830 cp_lexer_consume_token (parser->lexer);
27831 cp_parser_constraint_expression (parser);
27832 }
27833 return NULL_TREE;
27834 }
27835
27836 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
27837 if (tok2->type == CPP_OPEN_BRACE)
27838 {
27839 /* An opening brace following the start of a requires-clause is
27840 ill-formed; the user likely forgot the second `requires' that
27841 would start a requires-expression. */
27842 gcc_rich_location richloc (tok2->location);
27843 richloc.add_fixit_insert_after (tok->location, " requires");
27844 error_at (&richloc, "missing additional %<requires%> to start "
27845 "a requires-expression");
27846 /* Don't consume the `requires', so that it's reused as the start of a
27847 requires-expression. */
27848 }
27849 else
27850 cp_lexer_consume_token (parser->lexer);
27851
27852 if (!flag_concepts_ts)
27853 return cp_parser_requires_clause_expression (parser, lambda_p);
27854 else
27855 return cp_parser_constraint_expression (parser);
27856 }
27857
27858 /*---------------------------------------------------------------------------
27859 Requires expressions
27860 ---------------------------------------------------------------------------*/
27861
27862 /* Parse a requires expression
27863
27864 requirement-expression:
27865 'requires' requirement-parameter-list [opt] requirement-body */
27866
27867 static tree
27868 cp_parser_requires_expression (cp_parser *parser)
27869 {
27870 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
27871 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
27872
27873 /* Avoid committing to outer tentative parse. */
27874 tentative_firewall firewall (parser);
27875
27876 /* This is definitely a requires-expression. */
27877 cp_parser_commit_to_tentative_parse (parser);
27878
27879 tree parms, reqs;
27880 {
27881 /* Local parameters are delared as variables within the scope
27882 of the expression. They are not visible past the end of
27883 the expression. Expressions within the requires-expression
27884 are unevaluated. */
27885 struct scope_sentinel
27886 {
27887 scope_sentinel ()
27888 {
27889 ++cp_unevaluated_operand;
27890 begin_scope (sk_block, NULL_TREE);
27891 }
27892
27893 ~scope_sentinel ()
27894 {
27895 pop_bindings_and_leave_scope ();
27896 --cp_unevaluated_operand;
27897 }
27898 } s;
27899
27900 /* Parse the optional parameter list. */
27901 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27902 {
27903 parms = cp_parser_requirement_parameter_list (parser);
27904 if (parms == error_mark_node)
27905 return error_mark_node;
27906 }
27907 else
27908 parms = NULL_TREE;
27909
27910 /* Parse the requirement body. */
27911 temp_override<int> ovr (processing_template_decl);
27912 if (!processing_template_decl)
27913 /* As in cp_parser_requires_clause_expression. */
27914 processing_template_decl = 1;
27915 reqs = cp_parser_requirement_body (parser);
27916 if (reqs == error_mark_node)
27917 return error_mark_node;
27918 }
27919
27920 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
27921 the parm chain. */
27922 grokparms (parms, &parms);
27923 loc = make_location (loc, loc, parser->lexer);
27924 tree expr = finish_requires_expr (loc, parms, reqs);
27925 if (!processing_template_decl)
27926 {
27927 /* Perform semantic processing now to diagnose any invalid types and
27928 expressions. */
27929 int saved_errorcount = errorcount;
27930 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
27931 if (errorcount > saved_errorcount)
27932 return error_mark_node;
27933 }
27934 return expr;
27935 }
27936
27937 /* Parse a parameterized requirement.
27938
27939 requirement-parameter-list:
27940 '(' parameter-declaration-clause ')' */
27941
27942 static tree
27943 cp_parser_requirement_parameter_list (cp_parser *parser)
27944 {
27945 matching_parens parens;
27946 if (!parens.require_open (parser))
27947 return error_mark_node;
27948
27949 tree parms = (cp_parser_parameter_declaration_clause
27950 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
27951
27952 if (!parens.require_close (parser))
27953 return error_mark_node;
27954
27955 return parms;
27956 }
27957
27958 /* Parse the body of a requirement.
27959
27960 requirement-body:
27961 '{' requirement-list '}' */
27962 static tree
27963 cp_parser_requirement_body (cp_parser *parser)
27964 {
27965 matching_braces braces;
27966 if (!braces.require_open (parser))
27967 return error_mark_node;
27968
27969 tree reqs = cp_parser_requirement_seq (parser);
27970
27971 if (!braces.require_close (parser))
27972 return error_mark_node;
27973
27974 return reqs;
27975 }
27976
27977 /* Parse a sequence of requirements.
27978
27979 requirement-seq:
27980 requirement
27981 requirement-seq requirement */
27982
27983 static tree
27984 cp_parser_requirement_seq (cp_parser *parser)
27985 {
27986 tree result = NULL_TREE;
27987 do
27988 {
27989 tree req = cp_parser_requirement (parser);
27990 if (req != error_mark_node)
27991 result = tree_cons (NULL_TREE, req, result);
27992 } while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE));
27993
27994 /* If there are no valid requirements, this is not a valid expression. */
27995 if (!result)
27996 return error_mark_node;
27997
27998 /* Reverse the order of requirements so they are analyzed in order. */
27999 return nreverse (result);
28000 }
28001
28002 /* Parse a syntactic requirement or type requirement.
28003
28004 requirement:
28005 simple-requirement
28006 compound-requirement
28007 type-requirement
28008 nested-requirement */
28009
28010 static tree
28011 cp_parser_requirement (cp_parser *parser)
28012 {
28013 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28014 return cp_parser_compound_requirement (parser);
28015 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
28016 return cp_parser_type_requirement (parser);
28017 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
28018 return cp_parser_nested_requirement (parser);
28019 else
28020 return cp_parser_simple_requirement (parser);
28021 }
28022
28023 /* Parse a simple requirement.
28024
28025 simple-requirement:
28026 expression ';' */
28027
28028 static tree
28029 cp_parser_simple_requirement (cp_parser *parser)
28030 {
28031 location_t start = cp_lexer_peek_token (parser->lexer)->location;
28032 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
28033 if (expr == error_mark_node)
28034 cp_parser_skip_to_end_of_statement (parser);
28035
28036 cp_parser_consume_semicolon_at_end_of_statement (parser);
28037
28038 if (!expr || expr == error_mark_node)
28039 return error_mark_node;
28040
28041 /* Sometimes we don't get locations, so use the cached token location
28042 as a reasonable approximation. */
28043 if (expr.get_location() == UNKNOWN_LOCATION)
28044 expr.set_location (start);
28045
28046 return finish_simple_requirement (expr.get_location (), expr);
28047 }
28048
28049 /* Parse a type requirement
28050
28051 type-requirement
28052 nested-name-specifier [opt] required-type-name ';'
28053
28054 required-type-name:
28055 type-name
28056 'template' [opt] simple-template-id */
28057
28058 static tree
28059 cp_parser_type_requirement (cp_parser *parser)
28060 {
28061 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
28062 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28063
28064 // Save the scope before parsing name specifiers.
28065 tree saved_scope = parser->scope;
28066 tree saved_object_scope = parser->object_scope;
28067 tree saved_qualifying_scope = parser->qualifying_scope;
28068 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
28069 cp_parser_nested_name_specifier_opt (parser,
28070 /*typename_keyword_p=*/true,
28071 /*check_dependency_p=*/false,
28072 /*type_p=*/true,
28073 /*is_declaration=*/false);
28074
28075 tree type;
28076 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28077 {
28078 cp_lexer_consume_token (parser->lexer);
28079 type = cp_parser_template_id (parser,
28080 /*template_keyword_p=*/true,
28081 /*check_dependency=*/false,
28082 /*tag_type=*/none_type,
28083 /*is_declaration=*/false);
28084 type = make_typename_type (parser->scope, type, typename_type,
28085 /*complain=*/tf_error);
28086 }
28087 else
28088 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
28089
28090 if (TREE_CODE (type) == TYPE_DECL)
28091 type = TREE_TYPE (type);
28092
28093 parser->scope = saved_scope;
28094 parser->object_scope = saved_object_scope;
28095 parser->qualifying_scope = saved_qualifying_scope;
28096
28097 if (type == error_mark_node)
28098 cp_parser_skip_to_end_of_statement (parser);
28099
28100 cp_parser_consume_semicolon_at_end_of_statement (parser);
28101
28102 if (type == error_mark_node)
28103 return error_mark_node;
28104
28105 loc = make_location (loc, start_tok->location, parser->lexer);
28106 return finish_type_requirement (loc, type);
28107 }
28108
28109 /* Parse a compound requirement
28110
28111 compound-requirement:
28112 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
28113
28114 static tree
28115 cp_parser_compound_requirement (cp_parser *parser)
28116 {
28117 /* Parse an expression enclosed in '{ }'s. */
28118 matching_braces braces;
28119 if (!braces.require_open (parser))
28120 return error_mark_node;
28121
28122 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
28123
28124 tree expr = cp_parser_expression (parser, NULL, false, false);
28125 if (expr == error_mark_node)
28126 cp_parser_skip_to_closing_brace (parser);
28127
28128 if (!braces.require_close (parser))
28129 {
28130 cp_parser_skip_to_end_of_statement (parser);
28131 cp_parser_consume_semicolon_at_end_of_statement (parser);
28132 return error_mark_node;
28133 }
28134
28135 /* If the expression was invalid, skip the remainder of the requirement. */
28136 if (!expr || expr == error_mark_node)
28137 {
28138 cp_parser_skip_to_end_of_statement (parser);
28139 cp_parser_consume_semicolon_at_end_of_statement (parser);
28140 return error_mark_node;
28141 }
28142
28143 /* Parse the optional noexcept. */
28144 bool noexcept_p = false;
28145 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
28146 {
28147 cp_lexer_consume_token (parser->lexer);
28148 noexcept_p = true;
28149 }
28150
28151 /* Parse the optional trailing return type. */
28152 tree type = NULL_TREE;
28153 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
28154 {
28155 cp_lexer_consume_token (parser->lexer);
28156 cp_token *tok = cp_lexer_peek_token (parser->lexer);
28157
28158 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
28159 parser->in_result_type_constraint_p = true;
28160 /* C++20 allows either a type-id or a type-constraint. Parsing
28161 a type-id will subsume the parsing for a type-constraint but
28162 allow for more syntactic forms (e.g., const C<T>*). */
28163 type = cp_parser_trailing_type_id (parser);
28164 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
28165 if (type == error_mark_node)
28166 return error_mark_node;
28167
28168 location_t type_loc = make_location (tok->location, tok->location,
28169 parser->lexer);
28170
28171 /* Check that we haven't written something like 'const C<T>*'. */
28172 if (type_uses_auto (type))
28173 {
28174 if (!is_auto (type))
28175 {
28176 error_at (type_loc,
28177 "result type is not a plain type-constraint");
28178 cp_parser_consume_semicolon_at_end_of_statement (parser);
28179 return error_mark_node;
28180 }
28181 }
28182 else if (!flag_concepts_ts)
28183 /* P1452R2 removed the trailing-return-type option. */
28184 error_at (type_loc,
28185 "return-type-requirement is not a type-constraint");
28186 }
28187
28188 location_t loc = make_location (expr_token->location,
28189 braces.open_location (),
28190 parser->lexer);
28191
28192 cp_parser_consume_semicolon_at_end_of_statement (parser);
28193
28194 if (expr == error_mark_node || type == error_mark_node)
28195 return error_mark_node;
28196
28197 return finish_compound_requirement (loc, expr, type, noexcept_p);
28198 }
28199
28200 /* Parse a nested requirement. This is the same as a requires clause.
28201
28202 nested-requirement:
28203 requires-clause */
28204
28205 static tree
28206 cp_parser_nested_requirement (cp_parser *parser)
28207 {
28208 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
28209 cp_token *tok = cp_lexer_consume_token (parser->lexer);
28210 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28211 tree req = cp_parser_constraint_expression (parser);
28212 if (req == error_mark_node)
28213 cp_parser_skip_to_end_of_statement (parser);
28214 loc = make_location (loc, tok->location, parser->lexer);
28215 cp_parser_consume_semicolon_at_end_of_statement (parser);
28216 if (req == error_mark_node)
28217 return error_mark_node;
28218 return finish_nested_requirement (loc, req);
28219 }
28220
28221 /* Support Functions */
28222
28223 /* Return the appropriate prefer_type argument for lookup_name based on
28224 tag_type. */
28225
28226 static inline LOOK_want
28227 prefer_type_arg (tag_types tag_type)
28228 {
28229 switch (tag_type)
28230 {
28231 case none_type: return LOOK_want::NORMAL; // No preference.
28232 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
28233 default: return LOOK_want::TYPE; // Type only.
28234 }
28235 }
28236
28237 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
28238 NAME should have one of the representations used for an
28239 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
28240 is returned. If PARSER->SCOPE is a dependent type, then a
28241 SCOPE_REF is returned.
28242
28243 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
28244 returned; the name was already resolved when the TEMPLATE_ID_EXPR
28245 was formed. Abstractly, such entities should not be passed to this
28246 function, because they do not need to be looked up, but it is
28247 simpler to check for this special case here, rather than at the
28248 call-sites.
28249
28250 In cases not explicitly covered above, this function returns a
28251 DECL, OVERLOAD, or baselink representing the result of the lookup.
28252 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
28253 is returned.
28254
28255 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
28256 (e.g., "struct") that was used. In that case bindings that do not
28257 refer to types are ignored.
28258
28259 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
28260 ignored.
28261
28262 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
28263 are ignored.
28264
28265 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
28266 types.
28267
28268 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
28269 TREE_LIST of candidates if name-lookup results in an ambiguity, and
28270 NULL_TREE otherwise. */
28271
28272 static cp_expr
28273 cp_parser_lookup_name (cp_parser *parser, tree name,
28274 enum tag_types tag_type,
28275 bool is_template,
28276 bool is_namespace,
28277 bool check_dependency,
28278 tree *ambiguous_decls,
28279 location_t name_location)
28280 {
28281 tree decl;
28282 tree object_type = parser->context->object_type;
28283
28284 /* Assume that the lookup will be unambiguous. */
28285 if (ambiguous_decls)
28286 *ambiguous_decls = NULL_TREE;
28287
28288 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
28289 no longer valid. Note that if we are parsing tentatively, and
28290 the parse fails, OBJECT_TYPE will be automatically restored. */
28291 parser->context->object_type = NULL_TREE;
28292
28293 if (name == error_mark_node)
28294 return error_mark_node;
28295
28296 /* A template-id has already been resolved; there is no lookup to
28297 do. */
28298 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
28299 return name;
28300 if (BASELINK_P (name))
28301 {
28302 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
28303 == TEMPLATE_ID_EXPR);
28304 return name;
28305 }
28306
28307 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
28308 it should already have been checked to make sure that the name
28309 used matches the type being destroyed. */
28310 if (TREE_CODE (name) == BIT_NOT_EXPR)
28311 {
28312 tree type;
28313
28314 /* Figure out to which type this destructor applies. */
28315 if (parser->scope)
28316 type = parser->scope;
28317 else if (object_type)
28318 type = object_type;
28319 else
28320 type = current_class_type;
28321 /* If that's not a class type, there is no destructor. */
28322 if (!type || !CLASS_TYPE_P (type))
28323 return error_mark_node;
28324
28325 /* In a non-static member function, check implicit this->. */
28326 if (current_class_ref)
28327 return lookup_destructor (current_class_ref, parser->scope, name,
28328 tf_warning_or_error);
28329
28330 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
28331 lazily_declare_fn (sfk_destructor, type);
28332
28333 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
28334 return dtor;
28335
28336 return error_mark_node;
28337 }
28338
28339 /* By this point, the NAME should be an ordinary identifier. If
28340 the id-expression was a qualified name, the qualifying scope is
28341 stored in PARSER->SCOPE at this point. */
28342 gcc_assert (identifier_p (name));
28343
28344 /* Perform the lookup. */
28345 if (parser->scope)
28346 {
28347 bool dependent_p;
28348
28349 if (parser->scope == error_mark_node)
28350 return error_mark_node;
28351
28352 /* If the SCOPE is dependent, the lookup must be deferred until
28353 the template is instantiated -- unless we are explicitly
28354 looking up names in uninstantiated templates. Even then, we
28355 cannot look up the name if the scope is not a class type; it
28356 might, for example, be a template type parameter. */
28357 dependent_p = (TYPE_P (parser->scope)
28358 && dependent_scope_p (parser->scope));
28359 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
28360 && dependent_p)
28361 /* Defer lookup. */
28362 decl = error_mark_node;
28363 else
28364 {
28365 tree pushed_scope = NULL_TREE;
28366
28367 /* If PARSER->SCOPE is a dependent type, then it must be a
28368 class type, and we must not be checking dependencies;
28369 otherwise, we would have processed this lookup above. So
28370 that PARSER->SCOPE is not considered a dependent base by
28371 lookup_member, we must enter the scope here. */
28372 if (dependent_p)
28373 pushed_scope = push_scope (parser->scope);
28374
28375 /* If the PARSER->SCOPE is a template specialization, it
28376 may be instantiated during name lookup. In that case,
28377 errors may be issued. Even if we rollback the current
28378 tentative parse, those errors are valid. */
28379 decl = lookup_qualified_name (parser->scope, name,
28380 prefer_type_arg (tag_type),
28381 /*complain=*/true);
28382
28383 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
28384 lookup result and the nested-name-specifier nominates a class C:
28385 * if the name specified after the nested-name-specifier, when
28386 looked up in C, is the injected-class-name of C (Clause 9), or
28387 * if the name specified after the nested-name-specifier is the
28388 same as the identifier or the simple-template-id's template-
28389 name in the last component of the nested-name-specifier,
28390 the name is instead considered to name the constructor of
28391 class C. [ Note: for example, the constructor is not an
28392 acceptable lookup result in an elaborated-type-specifier so
28393 the constructor would not be used in place of the
28394 injected-class-name. --end note ] Such a constructor name
28395 shall be used only in the declarator-id of a declaration that
28396 names a constructor or in a using-declaration. */
28397 if (tag_type == none_type
28398 && DECL_SELF_REFERENCE_P (decl)
28399 && same_type_p (DECL_CONTEXT (decl), parser->scope))
28400 decl = lookup_qualified_name (parser->scope, ctor_identifier,
28401 prefer_type_arg (tag_type),
28402 /*complain=*/true);
28403
28404 /* If we have a single function from a using decl, pull it out. */
28405 if (TREE_CODE (decl) == OVERLOAD
28406 && !really_overloaded_fn (decl))
28407 decl = OVL_FUNCTION (decl);
28408
28409 if (pushed_scope)
28410 pop_scope (pushed_scope);
28411 }
28412
28413 /* If the scope is a dependent type and either we deferred lookup or
28414 we did lookup but didn't find the name, rememeber the name. */
28415 if (decl == error_mark_node && TYPE_P (parser->scope)
28416 && dependent_type_p (parser->scope))
28417 {
28418 if (tag_type)
28419 {
28420 tree type;
28421
28422 /* The resolution to Core Issue 180 says that `struct
28423 A::B' should be considered a type-name, even if `A'
28424 is dependent. */
28425 type = make_typename_type (parser->scope, name, tag_type,
28426 /*complain=*/tf_error);
28427 if (type != error_mark_node)
28428 decl = TYPE_NAME (type);
28429 }
28430 else if (is_template
28431 && (cp_parser_next_token_ends_template_argument_p (parser)
28432 || cp_lexer_next_token_is (parser->lexer,
28433 CPP_CLOSE_PAREN)))
28434 decl = make_unbound_class_template (parser->scope,
28435 name, NULL_TREE,
28436 /*complain=*/tf_error);
28437 else
28438 decl = build_qualified_name (/*type=*/NULL_TREE,
28439 parser->scope, name,
28440 is_template);
28441 }
28442 parser->qualifying_scope = parser->scope;
28443 parser->object_scope = NULL_TREE;
28444 }
28445 else if (object_type)
28446 {
28447 /* Look up the name in the scope of the OBJECT_TYPE, unless the
28448 OBJECT_TYPE is not a class. */
28449 if (CLASS_TYPE_P (object_type))
28450 /* If the OBJECT_TYPE is a template specialization, it may
28451 be instantiated during name lookup. In that case, errors
28452 may be issued. Even if we rollback the current tentative
28453 parse, those errors are valid. */
28454 decl = lookup_member (object_type,
28455 name,
28456 /*protect=*/0,
28457 /*prefer_type=*/tag_type != none_type,
28458 tf_warning_or_error);
28459 else
28460 decl = NULL_TREE;
28461
28462 if (!decl)
28463 /* Look it up in the enclosing context. DR 141: When looking for a
28464 template-name after -> or ., only consider class templates. */
28465 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
28466 /* DR 141: When looking in the
28467 current enclosing context for a
28468 template-name after -> or ., only
28469 consider class templates. */
28470 : is_template ? LOOK_want::TYPE
28471 : prefer_type_arg (tag_type));
28472 parser->object_scope = object_type;
28473 parser->qualifying_scope = NULL_TREE;
28474 }
28475 else
28476 {
28477 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
28478 : prefer_type_arg (tag_type));
28479 parser->qualifying_scope = NULL_TREE;
28480 parser->object_scope = NULL_TREE;
28481 }
28482
28483 /* If the lookup failed, let our caller know. */
28484 if (!decl || decl == error_mark_node)
28485 return error_mark_node;
28486
28487 /* Pull out the template from an injected-class-name (or multiple). */
28488 if (is_template)
28489 decl = maybe_get_template_decl_from_type_decl (decl);
28490
28491 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
28492 if (TREE_CODE (decl) == TREE_LIST)
28493 {
28494 if (ambiguous_decls)
28495 *ambiguous_decls = decl;
28496 /* The error message we have to print is too complicated for
28497 cp_parser_error, so we incorporate its actions directly. */
28498 if (!cp_parser_simulate_error (parser))
28499 {
28500 error_at (name_location, "reference to %qD is ambiguous",
28501 name);
28502 print_candidates (decl);
28503 }
28504 return error_mark_node;
28505 }
28506
28507 gcc_assert (DECL_P (decl)
28508 || TREE_CODE (decl) == OVERLOAD
28509 || TREE_CODE (decl) == SCOPE_REF
28510 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
28511 || BASELINK_P (decl));
28512
28513 /* If we have resolved the name of a member declaration, check to
28514 see if the declaration is accessible. When the name resolves to
28515 set of overloaded functions, accessibility is checked when
28516 overload resolution is done.
28517
28518 During an explicit instantiation, access is not checked at all,
28519 as per [temp.explicit]. */
28520 if (DECL_P (decl))
28521 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
28522 tf_warning_or_error);
28523
28524 maybe_record_typedef_use (decl);
28525
28526 return cp_expr (decl, name_location);
28527 }
28528
28529 /* Like cp_parser_lookup_name, but for use in the typical case where
28530 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
28531 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
28532
28533 static tree
28534 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
28535 {
28536 return cp_parser_lookup_name (parser, name,
28537 none_type,
28538 /*is_template=*/false,
28539 /*is_namespace=*/false,
28540 /*check_dependency=*/true,
28541 /*ambiguous_decls=*/NULL,
28542 location);
28543 }
28544
28545 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
28546 the current context, return the TYPE_DECL. If TAG_NAME_P is
28547 true, the DECL indicates the class being defined in a class-head,
28548 or declared in an elaborated-type-specifier.
28549
28550 Otherwise, return DECL. */
28551
28552 static tree
28553 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
28554 {
28555 /* If the TEMPLATE_DECL is being declared as part of a class-head,
28556 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
28557
28558 struct A {
28559 template <typename T> struct B;
28560 };
28561
28562 template <typename T> struct A::B {};
28563
28564 Similarly, in an elaborated-type-specifier:
28565
28566 namespace N { struct X{}; }
28567
28568 struct A {
28569 template <typename T> friend struct N::X;
28570 };
28571
28572 However, if the DECL refers to a class type, and we are in
28573 the scope of the class, then the name lookup automatically
28574 finds the TYPE_DECL created by build_self_reference rather
28575 than a TEMPLATE_DECL. For example, in:
28576
28577 template <class T> struct S {
28578 S s;
28579 };
28580
28581 there is no need to handle such case. */
28582
28583 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
28584 return DECL_TEMPLATE_RESULT (decl);
28585
28586 return decl;
28587 }
28588
28589 /* If too many, or too few, template-parameter lists apply to the
28590 declarator, issue an error message. Returns TRUE if all went well,
28591 and FALSE otherwise. */
28592
28593 static bool
28594 cp_parser_check_declarator_template_parameters (cp_parser* parser,
28595 cp_declarator *declarator,
28596 location_t declarator_location)
28597 {
28598 switch (declarator->kind)
28599 {
28600 case cdk_id:
28601 {
28602 unsigned num_templates = 0;
28603 tree scope = declarator->u.id.qualifying_scope;
28604 bool template_id_p = false;
28605
28606 if (scope)
28607 num_templates = num_template_headers_for_class (scope);
28608 else if (TREE_CODE (declarator->u.id.unqualified_name)
28609 == TEMPLATE_ID_EXPR)
28610 {
28611 /* If the DECLARATOR has the form `X<y>' then it uses one
28612 additional level of template parameters. */
28613 ++num_templates;
28614 template_id_p = true;
28615 }
28616
28617 return cp_parser_check_template_parameters
28618 (parser, num_templates, template_id_p, declarator_location,
28619 declarator);
28620 }
28621
28622 case cdk_function:
28623 case cdk_array:
28624 case cdk_pointer:
28625 case cdk_reference:
28626 case cdk_ptrmem:
28627 return (cp_parser_check_declarator_template_parameters
28628 (parser, declarator->declarator, declarator_location));
28629
28630 case cdk_decomp:
28631 case cdk_error:
28632 return true;
28633
28634 default:
28635 gcc_unreachable ();
28636 }
28637 return false;
28638 }
28639
28640 /* NUM_TEMPLATES were used in the current declaration. If that is
28641 invalid, return FALSE and issue an error messages. Otherwise,
28642 return TRUE. If DECLARATOR is non-NULL, then we are checking a
28643 declarator and we can print more accurate diagnostics. */
28644
28645 static bool
28646 cp_parser_check_template_parameters (cp_parser* parser,
28647 unsigned num_templates,
28648 bool template_id_p,
28649 location_t location,
28650 cp_declarator *declarator)
28651 {
28652 /* If there are the same number of template classes and parameter
28653 lists, that's OK. */
28654 if (parser->num_template_parameter_lists == num_templates)
28655 return true;
28656 /* If there are more, but only one more, and the name ends in an identifier,
28657 then we are declaring a primary template. That's OK too. */
28658 if (!template_id_p
28659 && parser->num_template_parameter_lists == num_templates + 1)
28660 return true;
28661
28662 if (cp_parser_simulate_error (parser))
28663 return false;
28664
28665 /* If there are more template classes than parameter lists, we have
28666 something like:
28667
28668 template <class T> void S<T>::R<T>::f (); */
28669 if (parser->num_template_parameter_lists < num_templates)
28670 {
28671 if (declarator && !current_function_decl)
28672 error_at (location, "specializing member %<%T::%E%> "
28673 "requires %<template<>%> syntax",
28674 declarator->u.id.qualifying_scope,
28675 declarator->u.id.unqualified_name);
28676 else if (declarator)
28677 error_at (location, "invalid declaration of %<%T::%E%>",
28678 declarator->u.id.qualifying_scope,
28679 declarator->u.id.unqualified_name);
28680 else
28681 error_at (location, "too few template-parameter-lists");
28682 return false;
28683 }
28684 /* Otherwise, there are too many template parameter lists. We have
28685 something like:
28686
28687 template <class T> template <class U> void S::f(); */
28688 error_at (location, "too many template-parameter-lists");
28689 return false;
28690 }
28691
28692 /* Parse an optional `::' token indicating that the following name is
28693 from the global namespace. If so, PARSER->SCOPE is set to the
28694 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
28695 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
28696 Returns the new value of PARSER->SCOPE, if the `::' token is
28697 present, and NULL_TREE otherwise. */
28698
28699 static tree
28700 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
28701 {
28702 cp_token *token;
28703
28704 /* Peek at the next token. */
28705 token = cp_lexer_peek_token (parser->lexer);
28706 /* If we're looking at a `::' token then we're starting from the
28707 global namespace, not our current location. */
28708 if (token->type == CPP_SCOPE)
28709 {
28710 /* Consume the `::' token. */
28711 cp_lexer_consume_token (parser->lexer);
28712 /* Set the SCOPE so that we know where to start the lookup. */
28713 parser->scope = global_namespace;
28714 parser->qualifying_scope = global_namespace;
28715 parser->object_scope = NULL_TREE;
28716
28717 return parser->scope;
28718 }
28719 else if (!current_scope_valid_p)
28720 {
28721 parser->scope = NULL_TREE;
28722 parser->qualifying_scope = NULL_TREE;
28723 parser->object_scope = NULL_TREE;
28724 }
28725
28726 return NULL_TREE;
28727 }
28728
28729 /* Returns TRUE if the upcoming token sequence is the start of a
28730 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
28731 declarator is preceded by the `friend' specifier. The parser flags FLAGS
28732 is used to control type-specifier parsing. */
28733
28734 static bool
28735 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
28736 bool friend_p)
28737 {
28738 bool constructor_p;
28739 bool outside_class_specifier_p;
28740 tree nested_name_specifier;
28741 cp_token *next_token;
28742
28743 /* The common case is that this is not a constructor declarator, so
28744 try to avoid doing lots of work if at all possible. It's not
28745 valid declare a constructor at function scope. */
28746 if (parser->in_function_body)
28747 return false;
28748 /* And only certain tokens can begin a constructor declarator. */
28749 next_token = cp_lexer_peek_token (parser->lexer);
28750 if (next_token->type != CPP_NAME
28751 && next_token->type != CPP_SCOPE
28752 && next_token->type != CPP_NESTED_NAME_SPECIFIER
28753 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
28754 declarator-id of a constructor or destructor. */
28755 && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
28756 return false;
28757
28758 /* Parse tentatively; we are going to roll back all of the tokens
28759 consumed here. */
28760 cp_parser_parse_tentatively (parser);
28761 /* Assume that we are looking at a constructor declarator. */
28762 constructor_p = true;
28763
28764 /* Look for the optional `::' operator. */
28765 cp_parser_global_scope_opt (parser,
28766 /*current_scope_valid_p=*/false);
28767 /* Look for the nested-name-specifier. */
28768 nested_name_specifier
28769 = (cp_parser_nested_name_specifier_opt (parser,
28770 /*typename_keyword_p=*/false,
28771 /*check_dependency_p=*/false,
28772 /*type_p=*/false,
28773 /*is_declaration=*/false));
28774
28775 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
28776 if (nested_name_specifier
28777 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
28778 {
28779 tree s = resolve_typename_type (nested_name_specifier,
28780 /*only_current_p=*/false);
28781 if (TREE_CODE (s) != TYPENAME_TYPE)
28782 nested_name_specifier = s;
28783 }
28784
28785 outside_class_specifier_p = (!at_class_scope_p ()
28786 || !TYPE_BEING_DEFINED (current_class_type)
28787 || friend_p);
28788
28789 /* Outside of a class-specifier, there must be a
28790 nested-name-specifier. Except in C++17 mode, where we
28791 might be declaring a guiding declaration. */
28792 if (!nested_name_specifier && outside_class_specifier_p
28793 && cxx_dialect < cxx17)
28794 constructor_p = false;
28795 else if (nested_name_specifier == error_mark_node)
28796 constructor_p = false;
28797
28798 /* If we have a class scope, this is easy; DR 147 says that S::S always
28799 names the constructor, and no other qualified name could. */
28800 if (constructor_p && nested_name_specifier
28801 && CLASS_TYPE_P (nested_name_specifier))
28802 {
28803 tree id = cp_parser_unqualified_id (parser,
28804 /*template_keyword_p=*/false,
28805 /*check_dependency_p=*/false,
28806 /*declarator_p=*/true,
28807 /*optional_p=*/false);
28808 if (is_overloaded_fn (id))
28809 id = DECL_NAME (get_first_fn (id));
28810 if (!constructor_name_p (id, nested_name_specifier))
28811 constructor_p = false;
28812 }
28813 /* If we still think that this might be a constructor-declarator,
28814 look for a class-name. */
28815 else if (constructor_p)
28816 {
28817 /* If we have:
28818
28819 template <typename T> struct S {
28820 S();
28821 };
28822
28823 we must recognize that the nested `S' names a class. */
28824 if (cxx_dialect >= cxx17)
28825 cp_parser_parse_tentatively (parser);
28826
28827 tree type_decl;
28828 type_decl = cp_parser_class_name (parser,
28829 /*typename_keyword_p=*/false,
28830 /*template_keyword_p=*/false,
28831 none_type,
28832 /*check_dependency_p=*/false,
28833 /*class_head_p=*/false,
28834 /*is_declaration=*/false);
28835
28836 if (cxx_dialect >= cxx17
28837 && !cp_parser_parse_definitely (parser))
28838 {
28839 type_decl = NULL_TREE;
28840 tree tmpl = cp_parser_template_name (parser,
28841 /*template_keyword*/false,
28842 /*check_dependency_p*/false,
28843 /*is_declaration*/false,
28844 none_type,
28845 /*is_identifier*/NULL);
28846 if (DECL_CLASS_TEMPLATE_P (tmpl)
28847 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28848 /* It's a deduction guide, return true. */;
28849 else
28850 cp_parser_simulate_error (parser);
28851 }
28852
28853 /* If there was no class-name, then this is not a constructor.
28854 Otherwise, if we are in a class-specifier and we aren't
28855 handling a friend declaration, check that its type matches
28856 current_class_type (c++/38313). Note: error_mark_node
28857 is left alone for error recovery purposes. */
28858 constructor_p = (!cp_parser_error_occurred (parser)
28859 && (outside_class_specifier_p
28860 || type_decl == NULL_TREE
28861 || type_decl == error_mark_node
28862 || same_type_p (current_class_type,
28863 TREE_TYPE (type_decl))));
28864
28865 /* If we're still considering a constructor, we have to see a `(',
28866 to begin the parameter-declaration-clause, followed by either a
28867 `)', an `...', or a decl-specifier. We need to check for a
28868 type-specifier to avoid being fooled into thinking that:
28869
28870 S (f) (int);
28871
28872 is a constructor. (It is actually a function named `f' that
28873 takes one parameter (of type `int') and returns a value of type
28874 `S'. */
28875 if (constructor_p
28876 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28877 constructor_p = false;
28878
28879 if (constructor_p
28880 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
28881 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
28882 /* A parameter declaration begins with a decl-specifier,
28883 which is either the "attribute" keyword, a storage class
28884 specifier, or (usually) a type-specifier. */
28885 && (!cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
28886 /* GNU attributes can actually appear both at the start of
28887 a parameter and parenthesized declarator.
28888 S (__attribute__((unused)) int);
28889 is a constructor, but
28890 S (__attribute__((unused)) foo) (int);
28891 is a function declaration. */
28892 || (cp_parser_allow_gnu_extensions_p (parser)
28893 && cp_next_tokens_can_be_gnu_attribute_p (parser)))
28894 /* A parameter declaration can also begin with [[attribute]]. */
28895 && !cp_next_tokens_can_be_std_attribute_p (parser))
28896 {
28897 tree type;
28898 tree pushed_scope = NULL_TREE;
28899 unsigned saved_num_template_parameter_lists;
28900
28901 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
28902 {
28903 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
28904 while (--n)
28905 cp_lexer_consume_token (parser->lexer);
28906 }
28907
28908 /* Names appearing in the type-specifier should be looked up
28909 in the scope of the class. */
28910 if (current_class_type)
28911 type = NULL_TREE;
28912 else if (type_decl)
28913 {
28914 type = TREE_TYPE (type_decl);
28915 if (TREE_CODE (type) == TYPENAME_TYPE)
28916 {
28917 type = resolve_typename_type (type,
28918 /*only_current_p=*/false);
28919 if (TREE_CODE (type) == TYPENAME_TYPE)
28920 {
28921 cp_parser_abort_tentative_parse (parser);
28922 return false;
28923 }
28924 }
28925 pushed_scope = push_scope (type);
28926 }
28927
28928 /* Inside the constructor parameter list, surrounding
28929 template-parameter-lists do not apply. */
28930 saved_num_template_parameter_lists
28931 = parser->num_template_parameter_lists;
28932 parser->num_template_parameter_lists = 0;
28933
28934 /* Look for the type-specifier. It's not optional, but its typename
28935 might be. Unless this is a friend declaration; we don't want to
28936 treat
28937
28938 friend S (T::fn)(int);
28939
28940 as a constructor, but with P0634, we might assume a type when
28941 looking for the type-specifier. It is actually a function named
28942 `T::fn' that takes one parameter (of type `int') and returns a
28943 value of type `S'. Constructors can be friends, but they must
28944 use a qualified name.
28945
28946 Parse with an empty set of declaration specifiers since we're
28947 trying to match a decl-specifier-seq of the first parameter.
28948 This must be non-null so that cp_parser_simple_type_specifier
28949 will recognize a constrained placeholder type such as:
28950 'C<int> auto' where C is a type concept. */
28951 cp_decl_specifier_seq ctor_specs;
28952 clear_decl_specs (&ctor_specs);
28953 cp_parser_type_specifier (parser,
28954 (friend_p ? CP_PARSER_FLAGS_NONE
28955 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
28956 /*decl_specs=*/&ctor_specs,
28957 /*is_declarator=*/true,
28958 /*declares_class_or_enum=*/NULL,
28959 /*is_cv_qualifier=*/NULL);
28960
28961 parser->num_template_parameter_lists
28962 = saved_num_template_parameter_lists;
28963
28964 /* Leave the scope of the class. */
28965 if (pushed_scope)
28966 pop_scope (pushed_scope);
28967
28968 constructor_p = !cp_parser_error_occurred (parser);
28969 }
28970 }
28971
28972 /* We did not really want to consume any tokens. */
28973 cp_parser_abort_tentative_parse (parser);
28974
28975 return constructor_p;
28976 }
28977
28978 /* Parse the definition of the function given by the DECL_SPECIFIERS,
28979 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
28980 they must be performed once we are in the scope of the function.
28981
28982 Returns the function defined. */
28983
28984 static tree
28985 cp_parser_function_definition_from_specifiers_and_declarator
28986 (cp_parser* parser,
28987 cp_decl_specifier_seq *decl_specifiers,
28988 tree attributes,
28989 const cp_declarator *declarator)
28990 {
28991 tree fn;
28992 bool success_p;
28993
28994 /* Begin the function-definition. */
28995 success_p = start_function (decl_specifiers, declarator, attributes);
28996
28997 /* The things we're about to see are not directly qualified by any
28998 template headers we've seen thus far. */
28999 reset_specialization ();
29000
29001 /* If there were names looked up in the decl-specifier-seq that we
29002 did not check, check them now. We must wait until we are in the
29003 scope of the function to perform the checks, since the function
29004 might be a friend. */
29005 perform_deferred_access_checks (tf_warning_or_error);
29006
29007 if (success_p)
29008 {
29009 cp_finalize_omp_declare_simd (parser, current_function_decl);
29010 parser->omp_declare_simd = NULL;
29011 cp_finalize_oacc_routine (parser, current_function_decl, true);
29012 parser->oacc_routine = NULL;
29013 }
29014
29015 if (!success_p)
29016 {
29017 /* Skip the entire function. */
29018 cp_parser_skip_to_end_of_block_or_statement (parser);
29019 fn = error_mark_node;
29020 }
29021 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
29022 {
29023 /* Seen already, skip it. An error message has already been output. */
29024 cp_parser_skip_to_end_of_block_or_statement (parser);
29025 fn = current_function_decl;
29026 current_function_decl = NULL_TREE;
29027 /* If this is a function from a class, pop the nested class. */
29028 if (current_class_name)
29029 pop_nested_class ();
29030 }
29031 else
29032 {
29033 timevar_id_t tv;
29034 if (DECL_DECLARED_INLINE_P (current_function_decl))
29035 tv = TV_PARSE_INLINE;
29036 else
29037 tv = TV_PARSE_FUNC;
29038 timevar_push (tv);
29039 fn = cp_parser_function_definition_after_declarator (parser,
29040 /*inline_p=*/false);
29041 timevar_pop (tv);
29042 }
29043
29044 return fn;
29045 }
29046
29047 /* Parse the part of a function-definition that follows the
29048 declarator. INLINE_P is TRUE iff this function is an inline
29049 function defined within a class-specifier.
29050
29051 Returns the function defined. */
29052
29053 static tree
29054 cp_parser_function_definition_after_declarator (cp_parser* parser,
29055 bool inline_p)
29056 {
29057 tree fn;
29058 bool saved_in_unbraced_linkage_specification_p;
29059 bool saved_in_function_body;
29060 unsigned saved_num_template_parameter_lists;
29061 cp_token *token;
29062 bool fully_implicit_function_template_p
29063 = parser->fully_implicit_function_template_p;
29064 parser->fully_implicit_function_template_p = false;
29065 tree implicit_template_parms
29066 = parser->implicit_template_parms;
29067 parser->implicit_template_parms = 0;
29068 cp_binding_level* implicit_template_scope
29069 = parser->implicit_template_scope;
29070 parser->implicit_template_scope = 0;
29071
29072 saved_in_function_body = parser->in_function_body;
29073 parser->in_function_body = true;
29074 /* If the next token is `return', then the code may be trying to
29075 make use of the "named return value" extension that G++ used to
29076 support. */
29077 token = cp_lexer_peek_token (parser->lexer);
29078 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
29079 {
29080 /* Consume the `return' keyword. */
29081 cp_lexer_consume_token (parser->lexer);
29082 /* Look for the identifier that indicates what value is to be
29083 returned. */
29084 cp_parser_identifier (parser);
29085 /* Issue an error message. */
29086 error_at (token->location,
29087 "named return values are no longer supported");
29088 /* Skip tokens until we reach the start of the function body. */
29089 while (true)
29090 {
29091 cp_token *token = cp_lexer_peek_token (parser->lexer);
29092 if (token->type == CPP_OPEN_BRACE
29093 || token->type == CPP_EOF
29094 || token->type == CPP_PRAGMA_EOL)
29095 break;
29096 cp_lexer_consume_token (parser->lexer);
29097 }
29098 }
29099 /* The `extern' in `extern "C" void f () { ... }' does not apply to
29100 anything declared inside `f'. */
29101 saved_in_unbraced_linkage_specification_p
29102 = parser->in_unbraced_linkage_specification_p;
29103 parser->in_unbraced_linkage_specification_p = false;
29104 /* Inside the function, surrounding template-parameter-lists do not
29105 apply. */
29106 saved_num_template_parameter_lists
29107 = parser->num_template_parameter_lists;
29108 parser->num_template_parameter_lists = 0;
29109
29110 /* If the next token is `try', `__transaction_atomic', or
29111 `__transaction_relaxed`, then we are looking at either function-try-block
29112 or function-transaction-block. Note that all of these include the
29113 function-body. */
29114 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
29115 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
29116 else if (cp_lexer_next_token_is_keyword (parser->lexer,
29117 RID_TRANSACTION_RELAXED))
29118 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
29119 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
29120 cp_parser_function_try_block (parser);
29121 else
29122 cp_parser_ctor_initializer_opt_and_function_body
29123 (parser, /*in_function_try_block=*/false);
29124
29125 /* Finish the function. */
29126 fn = finish_function (inline_p);
29127 /* Generate code for it, if necessary. */
29128 expand_or_defer_fn (fn);
29129 /* Restore the saved values. */
29130 parser->in_unbraced_linkage_specification_p
29131 = saved_in_unbraced_linkage_specification_p;
29132 parser->num_template_parameter_lists
29133 = saved_num_template_parameter_lists;
29134 parser->in_function_body = saved_in_function_body;
29135
29136 parser->fully_implicit_function_template_p
29137 = fully_implicit_function_template_p;
29138 parser->implicit_template_parms
29139 = implicit_template_parms;
29140 parser->implicit_template_scope
29141 = implicit_template_scope;
29142
29143 if (parser->fully_implicit_function_template_p)
29144 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
29145
29146 return fn;
29147 }
29148
29149 /* Parse a template-declaration body (following argument list). */
29150
29151 static void
29152 cp_parser_template_declaration_after_parameters (cp_parser* parser,
29153 tree parameter_list,
29154 bool member_p)
29155 {
29156 tree decl = NULL_TREE;
29157 bool friend_p = false;
29158
29159 /* We just processed one more parameter list. */
29160 ++parser->num_template_parameter_lists;
29161
29162 /* Get the deferred access checks from the parameter list. These
29163 will be checked once we know what is being declared, as for a
29164 member template the checks must be performed in the scope of the
29165 class containing the member. */
29166 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
29167
29168 /* Tentatively parse for a new template parameter list, which can either be
29169 the template keyword or a template introduction. */
29170 if (cp_parser_template_declaration_after_export (parser, member_p))
29171 /* OK */;
29172 else if (cxx_dialect >= cxx11
29173 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
29174 decl = cp_parser_alias_declaration (parser);
29175 else if (cxx_dialect >= cxx20 /* Implies flag_concept. */
29176 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
29177 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_BOOL))
29178 /* Allow 'concept bool' to be handled as per the TS. */
29179 decl = cp_parser_concept_definition (parser);
29180 else
29181 {
29182 cp_token *token = cp_lexer_peek_token (parser->lexer);
29183 decl = cp_parser_single_declaration (parser,
29184 checks,
29185 member_p,
29186 /*explicit_specialization_p=*/false,
29187 &friend_p);
29188
29189 /* If this is a member template declaration, let the front
29190 end know. */
29191 if (member_p && !friend_p && decl)
29192 {
29193 if (TREE_CODE (decl) == TYPE_DECL)
29194 cp_parser_check_access_in_redeclaration (decl, token->location);
29195
29196 decl = finish_member_template_decl (decl);
29197 }
29198 else if (friend_p && decl
29199 && DECL_DECLARES_TYPE_P (decl))
29200 make_friend_class (current_class_type, TREE_TYPE (decl),
29201 /*complain=*/true);
29202 }
29203 /* We are done with the current parameter list. */
29204 --parser->num_template_parameter_lists;
29205
29206 pop_deferring_access_checks ();
29207
29208 /* Finish up. */
29209 finish_template_decl (parameter_list);
29210
29211 /* Check the template arguments for a literal operator template. */
29212 if (decl
29213 && DECL_DECLARES_FUNCTION_P (decl)
29214 && UDLIT_OPER_P (DECL_NAME (decl)))
29215 {
29216 bool ok = true;
29217 if (parameter_list == NULL_TREE)
29218 ok = false;
29219 else
29220 {
29221 int num_parms = TREE_VEC_LENGTH (parameter_list);
29222 if (num_parms == 1)
29223 {
29224 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
29225 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
29226 if (TREE_CODE (parm) != PARM_DECL)
29227 ok = false;
29228 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
29229 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
29230 /* OK, C++20 string literal operator template. We don't need
29231 to warn in lower dialects here because we will have already
29232 warned about the template parameter. */;
29233 else if (TREE_TYPE (parm) != char_type_node
29234 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
29235 ok = false;
29236 }
29237 else if (num_parms == 2 && cxx_dialect >= cxx14)
29238 {
29239 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
29240 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
29241 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
29242 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
29243 if (TREE_CODE (parm) != PARM_DECL
29244 || TREE_TYPE (parm) != TREE_TYPE (type)
29245 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
29246 ok = false;
29247 else
29248 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
29249 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
29250 "ISO C++ did not adopt string literal operator templa"
29251 "tes taking an argument pack of characters");
29252 }
29253 else
29254 ok = false;
29255 }
29256 if (!ok)
29257 {
29258 if (cxx_dialect > cxx17)
29259 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
29260 "template %qD has invalid parameter list; expected "
29261 "non-type template parameter pack %<<char...>%> or "
29262 "single non-type parameter of class type",
29263 decl);
29264 else
29265 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
29266 "template %qD has invalid parameter list; expected "
29267 "non-type template parameter pack %<<char...>%>",
29268 decl);
29269 }
29270 }
29271
29272 /* Register member declarations. */
29273 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
29274 finish_member_declaration (decl);
29275 /* If DECL is a function template, we must return to parse it later.
29276 (Even though there is no definition, there might be default
29277 arguments that need handling.) */
29278 if (member_p && decl
29279 && DECL_DECLARES_FUNCTION_P (decl))
29280 vec_safe_push (unparsed_funs_with_definitions, decl);
29281 }
29282
29283 /* Parse a template introduction header for a template-declaration. Returns
29284 false if tentative parse fails. */
29285
29286 static bool
29287 cp_parser_template_introduction (cp_parser* parser, bool member_p)
29288 {
29289 cp_parser_parse_tentatively (parser);
29290
29291 tree saved_scope = parser->scope;
29292 tree saved_object_scope = parser->object_scope;
29293 tree saved_qualifying_scope = parser->qualifying_scope;
29294
29295 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
29296
29297 /* Look for the optional `::' operator. */
29298 cp_parser_global_scope_opt (parser,
29299 /*current_scope_valid_p=*/false);
29300 /* Look for the nested-name-specifier. */
29301 cp_parser_nested_name_specifier_opt (parser,
29302 /*typename_keyword_p=*/false,
29303 /*check_dependency_p=*/true,
29304 /*type_p=*/false,
29305 /*is_declaration=*/false);
29306
29307 cp_token *token = cp_lexer_peek_token (parser->lexer);
29308 tree concept_name = cp_parser_identifier (parser);
29309
29310 /* Look up the concept for which we will be matching
29311 template parameters. */
29312 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
29313 token->location);
29314 parser->scope = saved_scope;
29315 parser->object_scope = saved_object_scope;
29316 parser->qualifying_scope = saved_qualifying_scope;
29317
29318 if (concept_name == error_mark_node
29319 || (seen_error () && !concept_definition_p (tmpl_decl)))
29320 cp_parser_simulate_error (parser);
29321
29322 /* Look for opening brace for introduction. */
29323 matching_braces braces;
29324 braces.require_open (parser);
29325 location_t open_loc = input_location;
29326
29327 if (!cp_parser_parse_definitely (parser))
29328 return false;
29329
29330 push_deferring_access_checks (dk_deferred);
29331
29332 /* Build vector of placeholder parameters and grab
29333 matching identifiers. */
29334 tree introduction_list = cp_parser_introduction_list (parser);
29335
29336 /* Look for closing brace for introduction. */
29337 if (!braces.require_close (parser))
29338 return true;
29339
29340 /* The introduction-list shall not be empty. */
29341 int nargs = TREE_VEC_LENGTH (introduction_list);
29342 if (nargs == 0)
29343 {
29344 /* In cp_parser_introduction_list we have already issued an error. */
29345 return true;
29346 }
29347
29348 if (tmpl_decl == error_mark_node)
29349 {
29350 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
29351 token->location);
29352 return true;
29353 }
29354
29355 /* Build and associate the constraint. */
29356 location_t introduction_loc = make_location (open_loc,
29357 start_token->location,
29358 parser->lexer);
29359 tree parms = finish_template_introduction (tmpl_decl,
29360 introduction_list,
29361 introduction_loc);
29362 if (parms && parms != error_mark_node)
29363 {
29364 if (!flag_concepts_ts)
29365 pedwarn (introduction_loc, 0, "template-introductions"
29366 " are not part of C++20 concepts [-fconcepts-ts]");
29367
29368 cp_parser_template_declaration_after_parameters (parser, parms,
29369 member_p);
29370 return true;
29371 }
29372
29373 if (parms == NULL_TREE)
29374 error_at (token->location, "no matching concept for template-introduction");
29375
29376 return true;
29377 }
29378
29379 /* Parse a normal template-declaration following the template keyword. */
29380
29381 static void
29382 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
29383 {
29384 tree parameter_list;
29385 bool need_lang_pop;
29386 location_t location = input_location;
29387
29388 /* Look for the `<' token. */
29389 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
29390 return;
29391 if (at_class_scope_p () && current_function_decl)
29392 {
29393 /* 14.5.2.2 [temp.mem]
29394
29395 A local class shall not have member templates. */
29396 error_at (location,
29397 "invalid declaration of member template in local class");
29398 cp_parser_skip_to_end_of_block_or_statement (parser);
29399 return;
29400 }
29401 /* [temp]
29402
29403 A template ... shall not have C linkage. */
29404 if (current_lang_name == lang_name_c)
29405 {
29406 error_at (location, "template with C linkage");
29407 maybe_show_extern_c_location ();
29408 /* Give it C++ linkage to avoid confusing other parts of the
29409 front end. */
29410 push_lang_context (lang_name_cplusplus);
29411 need_lang_pop = true;
29412 }
29413 else
29414 need_lang_pop = false;
29415
29416 /* We cannot perform access checks on the template parameter
29417 declarations until we know what is being declared, just as we
29418 cannot check the decl-specifier list. */
29419 push_deferring_access_checks (dk_deferred);
29420
29421 /* If the next token is `>', then we have an invalid
29422 specialization. Rather than complain about an invalid template
29423 parameter, issue an error message here. */
29424 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
29425 {
29426 cp_parser_error (parser, "invalid explicit specialization");
29427 begin_specialization ();
29428 parameter_list = NULL_TREE;
29429 }
29430 else
29431 {
29432 /* Parse the template parameters. */
29433 parameter_list = cp_parser_template_parameter_list (parser);
29434 }
29435
29436 /* Look for the `>'. */
29437 cp_parser_skip_to_end_of_template_parameter_list (parser);
29438
29439 /* Manage template requirements */
29440 if (flag_concepts)
29441 {
29442 tree reqs = get_shorthand_constraints (current_template_parms);
29443 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
29444 reqs = combine_constraint_expressions (reqs, treqs);
29445 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
29446 }
29447
29448 cp_parser_template_declaration_after_parameters (parser, parameter_list,
29449 member_p);
29450
29451 /* For the erroneous case of a template with C linkage, we pushed an
29452 implicit C++ linkage scope; exit that scope now. */
29453 if (need_lang_pop)
29454 pop_lang_context ();
29455 }
29456
29457 /* Parse a template-declaration, assuming that the `export' (and
29458 `extern') keywords, if present, has already been scanned. MEMBER_P
29459 is as for cp_parser_template_declaration. */
29460
29461 static bool
29462 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
29463 {
29464 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29465 {
29466 cp_lexer_consume_token (parser->lexer);
29467 cp_parser_explicit_template_declaration (parser, member_p);
29468 return true;
29469 }
29470 else if (flag_concepts)
29471 return cp_parser_template_introduction (parser, member_p);
29472
29473 return false;
29474 }
29475
29476 /* Perform the deferred access checks from a template-parameter-list.
29477 CHECKS is a TREE_LIST of access checks, as returned by
29478 get_deferred_access_checks. */
29479
29480 static void
29481 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
29482 {
29483 ++processing_template_parmlist;
29484 perform_access_checks (checks, tf_warning_or_error);
29485 --processing_template_parmlist;
29486 }
29487
29488 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
29489 `function-definition' sequence that follows a template header.
29490 If MEMBER_P is true, this declaration appears in a class scope.
29491
29492 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
29493 *FRIEND_P is set to TRUE iff the declaration is a friend. */
29494
29495 static tree
29496 cp_parser_single_declaration (cp_parser* parser,
29497 vec<deferred_access_check, va_gc> *checks,
29498 bool member_p,
29499 bool explicit_specialization_p,
29500 bool* friend_p)
29501 {
29502 int declares_class_or_enum;
29503 tree decl = NULL_TREE;
29504 cp_decl_specifier_seq decl_specifiers;
29505 bool function_definition_p = false;
29506 cp_token *decl_spec_token_start;
29507
29508 /* This function is only used when processing a template
29509 declaration. */
29510 gcc_assert (innermost_scope_kind () == sk_template_parms
29511 || innermost_scope_kind () == sk_template_spec);
29512
29513 /* Defer access checks until we know what is being declared. */
29514 push_deferring_access_checks (dk_deferred);
29515
29516 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
29517 alternative. */
29518 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
29519 cp_parser_decl_specifier_seq (parser,
29520 (CP_PARSER_FLAGS_OPTIONAL
29521 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
29522 &decl_specifiers,
29523 &declares_class_or_enum);
29524 if (friend_p)
29525 *friend_p = cp_parser_friend_p (&decl_specifiers);
29526
29527 /* There are no template typedefs. */
29528 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
29529 {
29530 error_at (decl_spec_token_start->location,
29531 "template declaration of %<typedef%>");
29532 decl = error_mark_node;
29533 }
29534
29535 /* Gather up the access checks that occurred the
29536 decl-specifier-seq. */
29537 stop_deferring_access_checks ();
29538
29539 /* Check for the declaration of a template class. */
29540 if (declares_class_or_enum)
29541 {
29542 if (cp_parser_declares_only_class_p (parser)
29543 || (declares_class_or_enum & 2))
29544 {
29545 /* If this is a declaration, but not a definition, associate
29546 any constraints with the type declaration. Constraints
29547 are associated with definitions in cp_parser_class_specifier. */
29548 if (declares_class_or_enum == 1)
29549 associate_classtype_constraints (decl_specifiers.type);
29550
29551 decl = shadow_tag (&decl_specifiers);
29552
29553 /* In this case:
29554
29555 struct C {
29556 friend template <typename T> struct A<T>::B;
29557 };
29558
29559 A<T>::B will be represented by a TYPENAME_TYPE, and
29560 therefore not recognized by shadow_tag. */
29561 if (friend_p && *friend_p
29562 && !decl
29563 && decl_specifiers.type
29564 && TYPE_P (decl_specifiers.type))
29565 decl = decl_specifiers.type;
29566
29567 if (decl && decl != error_mark_node)
29568 decl = TYPE_NAME (decl);
29569 else
29570 decl = error_mark_node;
29571
29572 /* Perform access checks for template parameters. */
29573 cp_parser_perform_template_parameter_access_checks (checks);
29574
29575 /* Give a helpful diagnostic for
29576 template <class T> struct A { } a;
29577 if we aren't already recovering from an error. */
29578 if (!cp_parser_declares_only_class_p (parser)
29579 && !seen_error ())
29580 {
29581 error_at (cp_lexer_peek_token (parser->lexer)->location,
29582 "a class template declaration must not declare "
29583 "anything else");
29584 cp_parser_skip_to_end_of_block_or_statement (parser);
29585 goto out;
29586 }
29587 }
29588 }
29589
29590 /* Complain about missing 'typename' or other invalid type names. */
29591 if (!decl_specifiers.any_type_specifiers_p
29592 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
29593 {
29594 /* cp_parser_parse_and_diagnose_invalid_type_name calls
29595 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
29596 the rest of this declaration. */
29597 decl = error_mark_node;
29598 goto out;
29599 }
29600
29601 /* If it's not a template class, try for a template function. If
29602 the next token is a `;', then this declaration does not declare
29603 anything. But, if there were errors in the decl-specifiers, then
29604 the error might well have come from an attempted class-specifier.
29605 In that case, there's no need to warn about a missing declarator. */
29606 if (!decl
29607 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
29608 || decl_specifiers.type != error_mark_node))
29609 {
29610 decl = cp_parser_init_declarator (parser,
29611 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
29612 &decl_specifiers,
29613 checks,
29614 /*function_definition_allowed_p=*/true,
29615 member_p,
29616 declares_class_or_enum,
29617 &function_definition_p,
29618 NULL, NULL, NULL);
29619
29620 /* 7.1.1-1 [dcl.stc]
29621
29622 A storage-class-specifier shall not be specified in an explicit
29623 specialization... */
29624 if (decl
29625 && explicit_specialization_p
29626 && decl_specifiers.storage_class != sc_none)
29627 {
29628 error_at (decl_spec_token_start->location,
29629 "explicit template specialization cannot have a storage class");
29630 decl = error_mark_node;
29631 }
29632
29633 if (decl && VAR_P (decl))
29634 check_template_variable (decl);
29635 }
29636
29637 /* Look for a trailing `;' after the declaration. */
29638 if (!function_definition_p
29639 && (decl == error_mark_node
29640 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
29641 cp_parser_skip_to_end_of_block_or_statement (parser);
29642
29643 out:
29644 pop_deferring_access_checks ();
29645
29646 /* Clear any current qualification; whatever comes next is the start
29647 of something new. */
29648 parser->scope = NULL_TREE;
29649 parser->qualifying_scope = NULL_TREE;
29650 parser->object_scope = NULL_TREE;
29651
29652 return decl;
29653 }
29654
29655 /* Parse a cast-expression that is not the operand of a unary "&". */
29656
29657 static cp_expr
29658 cp_parser_simple_cast_expression (cp_parser *parser)
29659 {
29660 return cp_parser_cast_expression (parser, /*address_p=*/false,
29661 /*cast_p=*/false, /*decltype*/false, NULL);
29662 }
29663
29664 /* Parse a functional cast to TYPE. Returns an expression
29665 representing the cast. */
29666
29667 static cp_expr
29668 cp_parser_functional_cast (cp_parser* parser, tree type)
29669 {
29670 vec<tree, va_gc> *vec;
29671 tree expression_list;
29672 cp_expr cast;
29673 bool nonconst_p;
29674
29675 location_t start_loc = input_location;
29676
29677 if (!type)
29678 type = error_mark_node;
29679
29680 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29681 {
29682 cp_lexer_set_source_position (parser->lexer);
29683 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
29684 expression_list = cp_parser_braced_list (parser, &nonconst_p);
29685 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
29686 if (TREE_CODE (type) == TYPE_DECL)
29687 type = TREE_TYPE (type);
29688
29689 cast = finish_compound_literal (type, expression_list,
29690 tf_warning_or_error, fcl_functional);
29691 /* Create a location of the form:
29692 type_name{i, f}
29693 ^~~~~~~~~~~~~~~
29694 with caret == start at the start of the type name,
29695 finishing at the closing brace. */
29696 location_t combined_loc = make_location (start_loc, start_loc,
29697 parser->lexer);
29698 cast.set_location (combined_loc);
29699 return cast;
29700 }
29701
29702
29703 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
29704 /*cast_p=*/true,
29705 /*allow_expansion_p=*/true,
29706 /*non_constant_p=*/NULL);
29707 if (vec == NULL)
29708 expression_list = error_mark_node;
29709 else
29710 {
29711 expression_list = build_tree_list_vec (vec);
29712 release_tree_vector (vec);
29713 }
29714
29715 /* Create a location of the form:
29716 float(i)
29717 ^~~~~~~~
29718 with caret == start at the start of the type name,
29719 finishing at the closing paren. */
29720 location_t combined_loc = make_location (start_loc, start_loc,
29721 parser->lexer);
29722 cast = build_functional_cast (combined_loc, type, expression_list,
29723 tf_warning_or_error);
29724
29725 /* [expr.const]/1: In an integral constant expression "only type
29726 conversions to integral or enumeration type can be used". */
29727 if (TREE_CODE (type) == TYPE_DECL)
29728 type = TREE_TYPE (type);
29729 if (cast != error_mark_node
29730 && !cast_valid_in_integral_constant_expression_p (type)
29731 && cp_parser_non_integral_constant_expression (parser,
29732 NIC_CONSTRUCTOR))
29733 return error_mark_node;
29734
29735 return cast;
29736 }
29737
29738 /* Save the tokens that make up the body of a member function defined
29739 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
29740 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
29741 specifiers applied to the declaration. Returns the FUNCTION_DECL
29742 for the member function. */
29743
29744 static tree
29745 cp_parser_save_member_function_body (cp_parser* parser,
29746 cp_decl_specifier_seq *decl_specifiers,
29747 cp_declarator *declarator,
29748 tree attributes)
29749 {
29750 cp_token *first;
29751 cp_token *last;
29752 tree fn;
29753 bool function_try_block = false;
29754
29755 /* Create the FUNCTION_DECL. */
29756 fn = grokmethod (decl_specifiers, declarator, attributes);
29757 cp_finalize_omp_declare_simd (parser, fn);
29758 cp_finalize_oacc_routine (parser, fn, true);
29759 /* If something went badly wrong, bail out now. */
29760 if (fn == error_mark_node)
29761 {
29762 /* If there's a function-body, skip it. */
29763 if (cp_parser_token_starts_function_definition_p
29764 (cp_lexer_peek_token (parser->lexer)))
29765 cp_parser_skip_to_end_of_block_or_statement (parser);
29766 return error_mark_node;
29767 }
29768
29769 /* Remember it, if there are default args to post process. */
29770 cp_parser_save_default_args (parser, fn);
29771
29772 /* Save away the tokens that make up the body of the
29773 function. */
29774 first = parser->lexer->next_token;
29775
29776 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
29777 cp_lexer_consume_token (parser->lexer);
29778 else if (cp_lexer_next_token_is_keyword (parser->lexer,
29779 RID_TRANSACTION_ATOMIC))
29780 {
29781 cp_lexer_consume_token (parser->lexer);
29782 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
29783 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
29784 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
29785 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
29786 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
29787 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
29788 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
29789 {
29790 cp_lexer_consume_token (parser->lexer);
29791 cp_lexer_consume_token (parser->lexer);
29792 cp_lexer_consume_token (parser->lexer);
29793 cp_lexer_consume_token (parser->lexer);
29794 cp_lexer_consume_token (parser->lexer);
29795 }
29796 else
29797 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
29798 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
29799 {
29800 cp_lexer_consume_token (parser->lexer);
29801 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
29802 break;
29803 }
29804 }
29805
29806 /* Handle function try blocks. */
29807 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
29808 {
29809 cp_lexer_consume_token (parser->lexer);
29810 function_try_block = true;
29811 }
29812 /* We can have braced-init-list mem-initializers before the fn body. */
29813 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29814 {
29815 cp_lexer_consume_token (parser->lexer);
29816 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
29817 {
29818 /* cache_group will stop after an un-nested { } pair, too. */
29819 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
29820 break;
29821
29822 /* variadic mem-inits have ... after the ')'. */
29823 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29824 cp_lexer_consume_token (parser->lexer);
29825 }
29826 }
29827 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29828 /* Handle function try blocks. */
29829 if (function_try_block)
29830 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
29831 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29832 last = parser->lexer->next_token;
29833
29834 /* Save away the inline definition; we will process it when the
29835 class is complete. */
29836 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
29837 DECL_PENDING_INLINE_P (fn) = 1;
29838
29839 /* We need to know that this was defined in the class, so that
29840 friend templates are handled correctly. */
29841 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
29842
29843 /* Add FN to the queue of functions to be parsed later. */
29844 vec_safe_push (unparsed_funs_with_definitions, fn);
29845
29846 return fn;
29847 }
29848
29849 /* Save the tokens that make up the in-class initializer for a non-static
29850 data member. Returns a DEFERRED_PARSE. */
29851
29852 static tree
29853 cp_parser_save_nsdmi (cp_parser* parser)
29854 {
29855 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
29856 }
29857
29858 /* Parse a template-argument-list, as well as the trailing ">" (but
29859 not the opening "<"). See cp_parser_template_argument_list for the
29860 return value. */
29861
29862 static tree
29863 cp_parser_enclosed_template_argument_list (cp_parser* parser)
29864 {
29865 tree arguments;
29866 tree saved_scope;
29867 tree saved_qualifying_scope;
29868 tree saved_object_scope;
29869 bool saved_greater_than_is_operator_p;
29870
29871 /* [temp.names]
29872
29873 When parsing a template-id, the first non-nested `>' is taken as
29874 the end of the template-argument-list rather than a greater-than
29875 operator. */
29876 saved_greater_than_is_operator_p
29877 = parser->greater_than_is_operator_p;
29878 parser->greater_than_is_operator_p = false;
29879 /* Parsing the argument list may modify SCOPE, so we save it
29880 here. */
29881 saved_scope = parser->scope;
29882 saved_qualifying_scope = parser->qualifying_scope;
29883 saved_object_scope = parser->object_scope;
29884 /* We need to evaluate the template arguments, even though this
29885 template-id may be nested within a "sizeof". */
29886 cp_evaluated ev;
29887 /* Parse the template-argument-list itself. */
29888 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
29889 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
29890 arguments = NULL_TREE;
29891 else
29892 arguments = cp_parser_template_argument_list (parser);
29893 /* Look for the `>' that ends the template-argument-list. If we find
29894 a '>>' instead, it's probably just a typo. */
29895 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
29896 {
29897 if (cxx_dialect != cxx98)
29898 {
29899 /* In C++0x, a `>>' in a template argument list or cast
29900 expression is considered to be two separate `>'
29901 tokens. So, change the current token to a `>', but don't
29902 consume it: it will be consumed later when the outer
29903 template argument list (or cast expression) is parsed.
29904 Note that this replacement of `>' for `>>' is necessary
29905 even if we are parsing tentatively: in the tentative
29906 case, after calling
29907 cp_parser_enclosed_template_argument_list we will always
29908 throw away all of the template arguments and the first
29909 closing `>', either because the template argument list
29910 was erroneous or because we are replacing those tokens
29911 with a CPP_TEMPLATE_ID token. The second `>' (which will
29912 not have been thrown away) is needed either to close an
29913 outer template argument list or to complete a new-style
29914 cast. */
29915 cp_token *token = cp_lexer_peek_token (parser->lexer);
29916 token->type = CPP_GREATER;
29917 }
29918 else if (!saved_greater_than_is_operator_p)
29919 {
29920 /* If we're in a nested template argument list, the '>>' has
29921 to be a typo for '> >'. We emit the error message, but we
29922 continue parsing and we push a '>' as next token, so that
29923 the argument list will be parsed correctly. Note that the
29924 global source location is still on the token before the
29925 '>>', so we need to say explicitly where we want it. */
29926 cp_token *token = cp_lexer_peek_token (parser->lexer);
29927 gcc_rich_location richloc (token->location);
29928 richloc.add_fixit_replace ("> >");
29929 error_at (&richloc, "%<>>%> should be %<> >%> "
29930 "within a nested template argument list");
29931
29932 token->type = CPP_GREATER;
29933 }
29934 else
29935 {
29936 /* If this is not a nested template argument list, the '>>'
29937 is a typo for '>'. Emit an error message and continue.
29938 Same deal about the token location, but here we can get it
29939 right by consuming the '>>' before issuing the diagnostic. */
29940 cp_token *token = cp_lexer_consume_token (parser->lexer);
29941 error_at (token->location,
29942 "spurious %<>>%>, use %<>%> to terminate "
29943 "a template argument list");
29944 }
29945 }
29946 else
29947 cp_parser_skip_to_end_of_template_parameter_list (parser);
29948 /* The `>' token might be a greater-than operator again now. */
29949 parser->greater_than_is_operator_p
29950 = saved_greater_than_is_operator_p;
29951 /* Restore the SAVED_SCOPE. */
29952 parser->scope = saved_scope;
29953 parser->qualifying_scope = saved_qualifying_scope;
29954 parser->object_scope = saved_object_scope;
29955
29956 return arguments;
29957 }
29958
29959 /* MEMBER_FUNCTION is a member function, or a friend. If default
29960 arguments, or the body of the function have not yet been parsed,
29961 parse them now. */
29962
29963 static void
29964 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
29965 {
29966 timevar_push (TV_PARSE_INMETH);
29967 /* If this member is a template, get the underlying
29968 FUNCTION_DECL. */
29969 if (DECL_FUNCTION_TEMPLATE_P (member_function))
29970 member_function = DECL_TEMPLATE_RESULT (member_function);
29971
29972 /* There should not be any class definitions in progress at this
29973 point; the bodies of members are only parsed outside of all class
29974 definitions. */
29975 gcc_assert (parser->num_classes_being_defined == 0);
29976 /* While we're parsing the member functions we might encounter more
29977 classes. We want to handle them right away, but we don't want
29978 them getting mixed up with functions that are currently in the
29979 queue. */
29980 push_unparsed_function_queues (parser);
29981
29982 /* Make sure that any template parameters are in scope. */
29983 maybe_begin_member_template_processing (member_function);
29984
29985 /* If the body of the function has not yet been parsed, parse it
29986 now. */
29987 if (DECL_PENDING_INLINE_P (member_function))
29988 {
29989 tree function_scope;
29990 cp_token_cache *tokens;
29991
29992 /* The function is no longer pending; we are processing it. */
29993 tokens = DECL_PENDING_INLINE_INFO (member_function);
29994 DECL_PENDING_INLINE_INFO (member_function) = NULL;
29995 DECL_PENDING_INLINE_P (member_function) = 0;
29996
29997 /* If this is a local class, enter the scope of the containing
29998 function. */
29999 function_scope = current_function_decl;
30000 if (function_scope)
30001 push_function_context ();
30002
30003 /* Push the body of the function onto the lexer stack. */
30004 cp_parser_push_lexer_for_tokens (parser, tokens);
30005
30006 /* Let the front end know that we going to be defining this
30007 function. */
30008 start_preparsed_function (member_function, NULL_TREE,
30009 SF_PRE_PARSED | SF_INCLASS_INLINE);
30010
30011 /* Don't do access checking if it is a templated function. */
30012 if (processing_template_decl)
30013 push_deferring_access_checks (dk_no_check);
30014
30015 /* #pragma omp declare reduction needs special parsing. */
30016 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
30017 {
30018 parser->lexer->in_pragma = true;
30019 cp_parser_omp_declare_reduction_exprs (member_function, parser);
30020 finish_function (/*inline_p=*/true);
30021 cp_check_omp_declare_reduction (member_function);
30022 }
30023 else
30024 /* Now, parse the body of the function. */
30025 cp_parser_function_definition_after_declarator (parser,
30026 /*inline_p=*/true);
30027
30028 if (processing_template_decl)
30029 pop_deferring_access_checks ();
30030
30031 /* Leave the scope of the containing function. */
30032 if (function_scope)
30033 pop_function_context ();
30034 cp_parser_pop_lexer (parser);
30035 }
30036
30037 /* Remove any template parameters from the symbol table. */
30038 maybe_end_member_template_processing ();
30039
30040 /* Restore the queue. */
30041 pop_unparsed_function_queues (parser);
30042 timevar_pop (TV_PARSE_INMETH);
30043 }
30044
30045 /* If DECL contains any default args, remember it on the unparsed
30046 functions queue. */
30047
30048 static void
30049 cp_parser_save_default_args (cp_parser* parser, tree decl)
30050 {
30051 tree probe;
30052
30053 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
30054 probe;
30055 probe = TREE_CHAIN (probe))
30056 if (TREE_PURPOSE (probe))
30057 {
30058 cp_default_arg_entry entry = {current_class_type, decl};
30059 vec_safe_push (unparsed_funs_with_default_args, entry);
30060 break;
30061 }
30062
30063 /* Remember if there is a noexcept-specifier to post process. */
30064 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
30065 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
30066 vec_safe_push (unparsed_noexcepts, decl);
30067 }
30068
30069 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
30070 which is either a FIELD_DECL or PARM_DECL. Parse it and return
30071 the result. For a PARM_DECL, PARMTYPE is the corresponding type
30072 from the parameter-type-list. */
30073
30074 static tree
30075 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
30076 tree default_arg, tree parmtype)
30077 {
30078 cp_token_cache *tokens;
30079 tree parsed_arg;
30080 bool dummy;
30081
30082 if (default_arg == error_mark_node)
30083 return error_mark_node;
30084
30085 /* Push the saved tokens for the default argument onto the parser's
30086 lexer stack. */
30087 tokens = DEFPARSE_TOKENS (default_arg);
30088 cp_parser_push_lexer_for_tokens (parser, tokens);
30089
30090 start_lambda_scope (decl);
30091
30092 /* Parse the default argument. */
30093 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
30094 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
30095 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
30096
30097 finish_lambda_scope ();
30098
30099 if (parsed_arg == error_mark_node)
30100 cp_parser_skip_to_end_of_statement (parser);
30101
30102 if (!processing_template_decl)
30103 {
30104 /* In a non-template class, check conversions now. In a template,
30105 we'll wait and instantiate these as needed. */
30106 if (TREE_CODE (decl) == PARM_DECL)
30107 parsed_arg = check_default_argument (parmtype, parsed_arg,
30108 tf_warning_or_error);
30109 else if (maybe_reject_flexarray_init (decl, parsed_arg))
30110 parsed_arg = error_mark_node;
30111 else
30112 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
30113 }
30114
30115 /* If the token stream has not been completely used up, then
30116 there was extra junk after the end of the default
30117 argument. */
30118 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30119 {
30120 if (TREE_CODE (decl) == PARM_DECL)
30121 cp_parser_error (parser, "expected %<,%>");
30122 else
30123 cp_parser_error (parser, "expected %<;%>");
30124 }
30125
30126 /* Revert to the main lexer. */
30127 cp_parser_pop_lexer (parser);
30128
30129 return parsed_arg;
30130 }
30131
30132 /* FIELD is a non-static data member with an initializer which we saved for
30133 later; parse it now. */
30134
30135 static void
30136 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
30137 {
30138 tree def;
30139
30140 maybe_begin_member_template_processing (field);
30141
30142 push_unparsed_function_queues (parser);
30143 def = cp_parser_late_parse_one_default_arg (parser, field,
30144 DECL_INITIAL (field),
30145 NULL_TREE);
30146 pop_unparsed_function_queues (parser);
30147
30148 maybe_end_member_template_processing ();
30149
30150 DECL_INITIAL (field) = def;
30151 }
30152
30153 /* FN is a FUNCTION_DECL which may contains a parameter with an
30154 unparsed DEFERRED_PARSE. Parse the default args now. This function
30155 assumes that the current scope is the scope in which the default
30156 argument should be processed. */
30157
30158 static void
30159 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
30160 {
30161 unsigned char saved_local_variables_forbidden_p;
30162 tree parm, parmdecl;
30163
30164 /* While we're parsing the default args, we might (due to the
30165 statement expression extension) encounter more classes. We want
30166 to handle them right away, but we don't want them getting mixed
30167 up with default args that are currently in the queue. */
30168 push_unparsed_function_queues (parser);
30169
30170 /* Local variable names (and the `this' keyword) may not appear
30171 in a default argument. */
30172 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
30173 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
30174
30175 push_defarg_context (fn);
30176
30177 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
30178 parmdecl = DECL_ARGUMENTS (fn);
30179 parm && parm != void_list_node;
30180 parm = TREE_CHAIN (parm),
30181 parmdecl = DECL_CHAIN (parmdecl))
30182 {
30183 tree default_arg = TREE_PURPOSE (parm);
30184 tree parsed_arg;
30185 vec<tree, va_gc> *insts;
30186 tree copy;
30187 unsigned ix;
30188
30189 if (!default_arg)
30190 continue;
30191
30192 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
30193 /* This can happen for a friend declaration for a function
30194 already declared with default arguments. */
30195 continue;
30196
30197 parsed_arg
30198 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
30199 default_arg,
30200 TREE_VALUE (parm));
30201 TREE_PURPOSE (parm) = parsed_arg;
30202
30203 /* Update any instantiations we've already created. */
30204 for (insts = DEFPARSE_INSTANTIATIONS (default_arg), ix = 0;
30205 vec_safe_iterate (insts, ix, &copy); ix++)
30206 TREE_PURPOSE (copy) = parsed_arg;
30207 }
30208
30209 pop_defarg_context ();
30210
30211 /* Make sure no default arg is missing. */
30212 check_default_args (fn);
30213
30214 /* Restore the state of local_variables_forbidden_p. */
30215 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
30216
30217 /* Restore the queue. */
30218 pop_unparsed_function_queues (parser);
30219 }
30220
30221 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
30222
30223 sizeof ... ( identifier )
30224
30225 where the 'sizeof' token has already been consumed. */
30226
30227 static tree
30228 cp_parser_sizeof_pack (cp_parser *parser)
30229 {
30230 /* Consume the `...'. */
30231 cp_lexer_consume_token (parser->lexer);
30232 maybe_warn_variadic_templates ();
30233
30234 matching_parens parens;
30235 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
30236 if (paren)
30237 parens.consume_open (parser);
30238 else
30239 permerror (cp_lexer_peek_token (parser->lexer)->location,
30240 "%<sizeof...%> argument must be surrounded by parentheses");
30241
30242 cp_token *token = cp_lexer_peek_token (parser->lexer);
30243 tree name = cp_parser_identifier (parser);
30244 if (name == error_mark_node)
30245 return error_mark_node;
30246 /* The name is not qualified. */
30247 parser->scope = NULL_TREE;
30248 parser->qualifying_scope = NULL_TREE;
30249 parser->object_scope = NULL_TREE;
30250 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
30251 if (expr == error_mark_node)
30252 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
30253 token->location);
30254 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
30255 expr = TREE_TYPE (expr);
30256 else if (TREE_CODE (expr) == CONST_DECL)
30257 expr = DECL_INITIAL (expr);
30258 expr = make_pack_expansion (expr);
30259 PACK_EXPANSION_SIZEOF_P (expr) = true;
30260
30261 if (paren)
30262 parens.require_close (parser);
30263
30264 return expr;
30265 }
30266
30267 /* Parse the operand of `sizeof' (or a similar operator). Returns
30268 either a TYPE or an expression, depending on the form of the
30269 input. The KEYWORD indicates which kind of expression we have
30270 encountered. */
30271
30272 static tree
30273 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
30274 {
30275 tree expr = NULL_TREE;
30276 const char *saved_message;
30277 const char *saved_message_arg;
30278 bool saved_integral_constant_expression_p;
30279 bool saved_non_integral_constant_expression_p;
30280
30281 /* If it's a `...', then we are computing the length of a parameter
30282 pack. */
30283 if (keyword == RID_SIZEOF
30284 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30285 return cp_parser_sizeof_pack (parser);
30286
30287 /* Types cannot be defined in a `sizeof' expression. Save away the
30288 old message. */
30289 saved_message = parser->type_definition_forbidden_message;
30290 saved_message_arg = parser->type_definition_forbidden_message_arg;
30291 parser->type_definition_forbidden_message
30292 = G_("types may not be defined in %qs expressions");
30293 parser->type_definition_forbidden_message_arg
30294 = IDENTIFIER_POINTER (ridpointers[keyword]);
30295
30296 /* The restrictions on constant-expressions do not apply inside
30297 sizeof expressions. */
30298 saved_integral_constant_expression_p
30299 = parser->integral_constant_expression_p;
30300 saved_non_integral_constant_expression_p
30301 = parser->non_integral_constant_expression_p;
30302 parser->integral_constant_expression_p = false;
30303
30304 /* Do not actually evaluate the expression. */
30305 ++cp_unevaluated_operand;
30306 ++c_inhibit_evaluation_warnings;
30307 /* If it's a `(', then we might be looking at the type-id
30308 construction. */
30309 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30310 {
30311 tree type = NULL_TREE;
30312
30313 tentative_firewall firewall (parser);
30314
30315 /* We can't be sure yet whether we're looking at a type-id or an
30316 expression. */
30317 cp_parser_parse_tentatively (parser);
30318
30319 matching_parens parens;
30320 parens.consume_open (parser);
30321
30322 /* Note: as a GNU Extension, compound literals are considered
30323 postfix-expressions as they are in C99, so they are valid
30324 arguments to sizeof. See comment in cp_parser_cast_expression
30325 for details. */
30326 if (cp_parser_compound_literal_p (parser))
30327 cp_parser_simulate_error (parser);
30328 else
30329 {
30330 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
30331 parser->in_type_id_in_expr_p = true;
30332 /* Look for the type-id. */
30333 type = cp_parser_type_id (parser);
30334 /* Look for the closing `)'. */
30335 parens.require_close (parser);
30336 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
30337 }
30338
30339 /* If all went well, then we're done. */
30340 if (cp_parser_parse_definitely (parser))
30341 expr = type;
30342 else
30343 {
30344 /* Commit to the tentative_firewall so we get syntax errors. */
30345 cp_parser_commit_to_tentative_parse (parser);
30346
30347 expr = cp_parser_unary_expression (parser);
30348 }
30349 }
30350 else
30351 expr = cp_parser_unary_expression (parser);
30352
30353 /* Go back to evaluating expressions. */
30354 --cp_unevaluated_operand;
30355 --c_inhibit_evaluation_warnings;
30356
30357 /* And restore the old one. */
30358 parser->type_definition_forbidden_message = saved_message;
30359 parser->type_definition_forbidden_message_arg = saved_message_arg;
30360 parser->integral_constant_expression_p
30361 = saved_integral_constant_expression_p;
30362 parser->non_integral_constant_expression_p
30363 = saved_non_integral_constant_expression_p;
30364
30365 return expr;
30366 }
30367
30368 /* If the current declaration has no declarator, return true. */
30369
30370 static bool
30371 cp_parser_declares_only_class_p (cp_parser *parser)
30372 {
30373 /* If the next token is a `;' or a `,' then there is no
30374 declarator. */
30375 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30376 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
30377 }
30378
30379 /* Update the DECL_SPECS to reflect the storage class indicated by
30380 KEYWORD. */
30381
30382 static void
30383 cp_parser_set_storage_class (cp_parser *parser,
30384 cp_decl_specifier_seq *decl_specs,
30385 enum rid keyword,
30386 cp_token *token)
30387 {
30388 cp_storage_class storage_class;
30389
30390 if (parser->in_unbraced_linkage_specification_p)
30391 {
30392 error_at (token->location, "invalid use of %qD in linkage specification",
30393 ridpointers[keyword]);
30394 return;
30395 }
30396 else if (decl_specs->storage_class != sc_none)
30397 {
30398 decl_specs->conflicting_specifiers_p = true;
30399 return;
30400 }
30401
30402 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
30403 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
30404 && decl_specs->gnu_thread_keyword_p)
30405 {
30406 pedwarn (decl_specs->locations[ds_thread], 0,
30407 "%<__thread%> before %qD", ridpointers[keyword]);
30408 }
30409
30410 switch (keyword)
30411 {
30412 case RID_AUTO:
30413 storage_class = sc_auto;
30414 break;
30415 case RID_REGISTER:
30416 storage_class = sc_register;
30417 break;
30418 case RID_STATIC:
30419 storage_class = sc_static;
30420 break;
30421 case RID_EXTERN:
30422 storage_class = sc_extern;
30423 break;
30424 case RID_MUTABLE:
30425 storage_class = sc_mutable;
30426 break;
30427 default:
30428 gcc_unreachable ();
30429 }
30430 decl_specs->storage_class = storage_class;
30431 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
30432
30433 /* A storage class specifier cannot be applied alongside a typedef
30434 specifier. If there is a typedef specifier present then set
30435 conflicting_specifiers_p which will trigger an error later
30436 on in grokdeclarator. */
30437 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
30438 decl_specs->conflicting_specifiers_p = true;
30439 }
30440
30441 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
30442 is true, the type is a class or enum definition. */
30443
30444 static void
30445 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
30446 tree type_spec,
30447 cp_token *token,
30448 bool type_definition_p)
30449 {
30450 decl_specs->any_specifiers_p = true;
30451
30452 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
30453 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
30454 this is what happened. In system headers, we ignore these
30455 declarations so that G++ can work with system headers that are not
30456 C++-safe. */
30457 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
30458 && !type_definition_p
30459 && (type_spec == boolean_type_node
30460 || type_spec == char8_type_node
30461 || type_spec == char16_type_node
30462 || type_spec == char32_type_node
30463 || type_spec == wchar_type_node)
30464 && (decl_specs->type
30465 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
30466 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
30467 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
30468 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
30469 {
30470 decl_specs->redefined_builtin_type = type_spec;
30471 set_and_check_decl_spec_loc (decl_specs,
30472 ds_redefined_builtin_type_spec,
30473 token);
30474 if (!decl_specs->type)
30475 {
30476 decl_specs->type = type_spec;
30477 decl_specs->type_definition_p = false;
30478 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
30479 }
30480 }
30481 else if (decl_specs->type)
30482 decl_specs->multiple_types_p = true;
30483 else
30484 {
30485 decl_specs->type = type_spec;
30486 decl_specs->type_definition_p = type_definition_p;
30487 decl_specs->redefined_builtin_type = NULL_TREE;
30488 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
30489 }
30490 }
30491
30492 /* True iff TOKEN is the GNU keyword __thread. */
30493
30494 static bool
30495 token_is__thread (cp_token *token)
30496 {
30497 gcc_assert (token->keyword == RID_THREAD);
30498 return id_equal (token->u.value, "__thread");
30499 }
30500
30501 /* Set the location for a declarator specifier and check if it is
30502 duplicated.
30503
30504 DECL_SPECS is the sequence of declarator specifiers onto which to
30505 set the location.
30506
30507 DS is the single declarator specifier to set which location is to
30508 be set onto the existing sequence of declarators.
30509
30510 LOCATION is the location for the declarator specifier to
30511 consider. */
30512
30513 static void
30514 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
30515 cp_decl_spec ds, cp_token *token)
30516 {
30517 gcc_assert (ds < ds_last);
30518
30519 if (decl_specs == NULL)
30520 return;
30521
30522 location_t location = token->location;
30523
30524 if (decl_specs->locations[ds] == 0)
30525 {
30526 decl_specs->locations[ds] = location;
30527 if (ds == ds_thread)
30528 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
30529 }
30530 else
30531 {
30532 if (ds == ds_long)
30533 {
30534 if (decl_specs->locations[ds_long_long] != 0)
30535 error_at (location,
30536 "%<long long long%> is too long for GCC");
30537 else
30538 {
30539 decl_specs->locations[ds_long_long] = location;
30540 pedwarn_cxx98 (location,
30541 OPT_Wlong_long,
30542 "ISO C++ 1998 does not support %<long long%>");
30543 }
30544 }
30545 else if (ds == ds_thread)
30546 {
30547 bool gnu = token_is__thread (token);
30548 gcc_rich_location richloc (location);
30549 if (gnu != decl_specs->gnu_thread_keyword_p)
30550 {
30551 richloc.add_range (decl_specs->locations[ds_thread]);
30552 error_at (&richloc,
30553 "both %<__thread%> and %<thread_local%> specified");
30554 }
30555 else
30556 {
30557 richloc.add_fixit_remove ();
30558 error_at (&richloc, "duplicate %qD", token->u.value);
30559 }
30560 }
30561 else
30562 {
30563 static const char *const decl_spec_names[] = {
30564 "signed",
30565 "unsigned",
30566 "short",
30567 "long",
30568 "const",
30569 "volatile",
30570 "restrict",
30571 "inline",
30572 "virtual",
30573 "explicit",
30574 "friend",
30575 "typedef",
30576 "using",
30577 "constexpr",
30578 "__complex",
30579 "constinit",
30580 "consteval"
30581 };
30582 gcc_rich_location richloc (location);
30583 richloc.add_fixit_remove ();
30584 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
30585 }
30586 }
30587 }
30588
30589 /* Return true iff the declarator specifier DS is present in the
30590 sequence of declarator specifiers DECL_SPECS. */
30591
30592 bool
30593 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
30594 cp_decl_spec ds)
30595 {
30596 gcc_assert (ds < ds_last);
30597
30598 if (decl_specs == NULL)
30599 return false;
30600
30601 return decl_specs->locations[ds] != 0;
30602 }
30603
30604 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
30605 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
30606
30607 static bool
30608 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
30609 {
30610 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
30611 }
30612
30613 /* Issue an error message indicating that TOKEN_DESC was expected.
30614 If KEYWORD is true, it indicated this function is called by
30615 cp_parser_require_keword and the required token can only be
30616 a indicated keyword.
30617
30618 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
30619 within any error as the location of an "opening" token matching
30620 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
30621 RT_CLOSE_PAREN). */
30622
30623 static void
30624 cp_parser_required_error (cp_parser *parser,
30625 required_token token_desc,
30626 bool keyword,
30627 location_t matching_location)
30628 {
30629 if (cp_parser_simulate_error (parser))
30630 return;
30631
30632 const char *gmsgid = NULL;
30633 switch (token_desc)
30634 {
30635 case RT_NEW:
30636 gmsgid = G_("expected %<new%>");
30637 break;
30638 case RT_DELETE:
30639 gmsgid = G_("expected %<delete%>");
30640 break;
30641 case RT_RETURN:
30642 gmsgid = G_("expected %<return%>");
30643 break;
30644 case RT_WHILE:
30645 gmsgid = G_("expected %<while%>");
30646 break;
30647 case RT_EXTERN:
30648 gmsgid = G_("expected %<extern%>");
30649 break;
30650 case RT_STATIC_ASSERT:
30651 gmsgid = G_("expected %<static_assert%>");
30652 break;
30653 case RT_DECLTYPE:
30654 gmsgid = G_("expected %<decltype%>");
30655 break;
30656 case RT_OPERATOR:
30657 gmsgid = G_("expected %<operator%>");
30658 break;
30659 case RT_CLASS:
30660 gmsgid = G_("expected %<class%>");
30661 break;
30662 case RT_TEMPLATE:
30663 gmsgid = G_("expected %<template%>");
30664 break;
30665 case RT_NAMESPACE:
30666 gmsgid = G_("expected %<namespace%>");
30667 break;
30668 case RT_USING:
30669 gmsgid = G_("expected %<using%>");
30670 break;
30671 case RT_ASM:
30672 gmsgid = G_("expected %<asm%>");
30673 break;
30674 case RT_TRY:
30675 gmsgid = G_("expected %<try%>");
30676 break;
30677 case RT_CATCH:
30678 gmsgid = G_("expected %<catch%>");
30679 break;
30680 case RT_THROW:
30681 gmsgid = G_("expected %<throw%>");
30682 break;
30683 case RT_AUTO:
30684 gmsgid = G_("expected %<auto%>");
30685 break;
30686 case RT_LABEL:
30687 gmsgid = G_("expected %<__label__%>");
30688 break;
30689 case RT_AT_TRY:
30690 gmsgid = G_("expected %<@try%>");
30691 break;
30692 case RT_AT_SYNCHRONIZED:
30693 gmsgid = G_("expected %<@synchronized%>");
30694 break;
30695 case RT_AT_THROW:
30696 gmsgid = G_("expected %<@throw%>");
30697 break;
30698 case RT_TRANSACTION_ATOMIC:
30699 gmsgid = G_("expected %<__transaction_atomic%>");
30700 break;
30701 case RT_TRANSACTION_RELAXED:
30702 gmsgid = G_("expected %<__transaction_relaxed%>");
30703 break;
30704 case RT_CO_YIELD:
30705 gmsgid = G_("expected %<co_yield%>");
30706 break;
30707 default:
30708 break;
30709 }
30710
30711 if (!gmsgid && !keyword)
30712 {
30713 switch (token_desc)
30714 {
30715 case RT_SEMICOLON:
30716 gmsgid = G_("expected %<;%>");
30717 break;
30718 case RT_OPEN_PAREN:
30719 gmsgid = G_("expected %<(%>");
30720 break;
30721 case RT_CLOSE_BRACE:
30722 gmsgid = G_("expected %<}%>");
30723 break;
30724 case RT_OPEN_BRACE:
30725 gmsgid = G_("expected %<{%>");
30726 break;
30727 case RT_CLOSE_SQUARE:
30728 gmsgid = G_("expected %<]%>");
30729 break;
30730 case RT_OPEN_SQUARE:
30731 gmsgid = G_("expected %<[%>");
30732 break;
30733 case RT_COMMA:
30734 gmsgid = G_("expected %<,%>");
30735 break;
30736 case RT_SCOPE:
30737 gmsgid = G_("expected %<::%>");
30738 break;
30739 case RT_LESS:
30740 gmsgid = G_("expected %<<%>");
30741 break;
30742 case RT_GREATER:
30743 gmsgid = G_("expected %<>%>");
30744 break;
30745 case RT_EQ:
30746 gmsgid = G_("expected %<=%>");
30747 break;
30748 case RT_ELLIPSIS:
30749 gmsgid = G_("expected %<...%>");
30750 break;
30751 case RT_MULT:
30752 gmsgid = G_("expected %<*%>");
30753 break;
30754 case RT_COMPL:
30755 gmsgid = G_("expected %<~%>");
30756 break;
30757 case RT_COLON:
30758 gmsgid = G_("expected %<:%>");
30759 break;
30760 case RT_COLON_SCOPE:
30761 gmsgid = G_("expected %<:%> or %<::%>");
30762 break;
30763 case RT_CLOSE_PAREN:
30764 gmsgid = G_("expected %<)%>");
30765 break;
30766 case RT_COMMA_CLOSE_PAREN:
30767 gmsgid = G_("expected %<,%> or %<)%>");
30768 break;
30769 case RT_PRAGMA_EOL:
30770 gmsgid = G_("expected end of line");
30771 break;
30772 case RT_NAME:
30773 gmsgid = G_("expected identifier");
30774 break;
30775 case RT_SELECT:
30776 gmsgid = G_("expected selection-statement");
30777 break;
30778 case RT_ITERATION:
30779 gmsgid = G_("expected iteration-statement");
30780 break;
30781 case RT_JUMP:
30782 gmsgid = G_("expected jump-statement");
30783 break;
30784 case RT_CLASS_KEY:
30785 gmsgid = G_("expected class-key");
30786 break;
30787 case RT_CLASS_TYPENAME_TEMPLATE:
30788 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
30789 break;
30790 default:
30791 gcc_unreachable ();
30792 }
30793 }
30794
30795 if (gmsgid)
30796 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
30797 }
30798
30799
30800 /* If the next token is of the indicated TYPE, consume it. Otherwise,
30801 issue an error message indicating that TOKEN_DESC was expected.
30802
30803 Returns the token consumed, if the token had the appropriate type.
30804 Otherwise, returns NULL.
30805
30806 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
30807 within any error as the location of an "opening" token matching
30808 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
30809 RT_CLOSE_PAREN). */
30810
30811 static cp_token *
30812 cp_parser_require (cp_parser* parser,
30813 enum cpp_ttype type,
30814 required_token token_desc,
30815 location_t matching_location)
30816 {
30817 if (cp_lexer_next_token_is (parser->lexer, type))
30818 return cp_lexer_consume_token (parser->lexer);
30819 else
30820 {
30821 /* Output the MESSAGE -- unless we're parsing tentatively. */
30822 if (!cp_parser_simulate_error (parser))
30823 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
30824 matching_location);
30825 return NULL;
30826 }
30827 }
30828
30829 /* An error message is produced if the next token is not '>'.
30830 All further tokens are skipped until the desired token is
30831 found or '{', '}', ';' or an unbalanced ')' or ']'. */
30832
30833 static void
30834 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
30835 {
30836 /* Current level of '< ... >'. */
30837 unsigned level = 0;
30838 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
30839 unsigned nesting_depth = 0;
30840
30841 /* Are we ready, yet? If not, issue error message. */
30842 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
30843 return;
30844
30845 /* Skip tokens until the desired token is found. */
30846 while (true)
30847 {
30848 /* Peek at the next token. */
30849 switch (cp_lexer_peek_token (parser->lexer)->type)
30850 {
30851 case CPP_LESS:
30852 if (!nesting_depth)
30853 ++level;
30854 break;
30855
30856 case CPP_RSHIFT:
30857 if (cxx_dialect == cxx98)
30858 /* C++0x views the `>>' operator as two `>' tokens, but
30859 C++98 does not. */
30860 break;
30861 else if (!nesting_depth && level-- == 0)
30862 {
30863 /* We've hit a `>>' where the first `>' closes the
30864 template argument list, and the second `>' is
30865 spurious. Just consume the `>>' and stop; we've
30866 already produced at least one error. */
30867 cp_lexer_consume_token (parser->lexer);
30868 return;
30869 }
30870 /* Fall through for C++0x, so we handle the second `>' in
30871 the `>>'. */
30872 gcc_fallthrough ();
30873
30874 case CPP_GREATER:
30875 if (!nesting_depth && level-- == 0)
30876 {
30877 /* We've reached the token we want, consume it and stop. */
30878 cp_lexer_consume_token (parser->lexer);
30879 return;
30880 }
30881 break;
30882
30883 case CPP_OPEN_PAREN:
30884 case CPP_OPEN_SQUARE:
30885 ++nesting_depth;
30886 break;
30887
30888 case CPP_CLOSE_PAREN:
30889 case CPP_CLOSE_SQUARE:
30890 if (nesting_depth-- == 0)
30891 return;
30892 break;
30893
30894 case CPP_EOF:
30895 case CPP_PRAGMA_EOL:
30896 case CPP_SEMICOLON:
30897 case CPP_OPEN_BRACE:
30898 case CPP_CLOSE_BRACE:
30899 /* The '>' was probably forgotten, don't look further. */
30900 return;
30901
30902 default:
30903 break;
30904 }
30905
30906 /* Consume this token. */
30907 cp_lexer_consume_token (parser->lexer);
30908 }
30909 }
30910
30911 /* If the next token is the indicated keyword, consume it. Otherwise,
30912 issue an error message indicating that TOKEN_DESC was expected.
30913
30914 Returns the token consumed, if the token had the appropriate type.
30915 Otherwise, returns NULL. */
30916
30917 static cp_token *
30918 cp_parser_require_keyword (cp_parser* parser,
30919 enum rid keyword,
30920 required_token token_desc)
30921 {
30922 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
30923
30924 if (token && token->keyword != keyword)
30925 {
30926 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
30927 UNKNOWN_LOCATION);
30928 return NULL;
30929 }
30930
30931 return token;
30932 }
30933
30934 /* Returns TRUE iff TOKEN is a token that can begin the body of a
30935 function-definition. */
30936
30937 static bool
30938 cp_parser_token_starts_function_definition_p (cp_token* token)
30939 {
30940 return (/* An ordinary function-body begins with an `{'. */
30941 token->type == CPP_OPEN_BRACE
30942 /* A ctor-initializer begins with a `:'. */
30943 || token->type == CPP_COLON
30944 /* A function-try-block begins with `try'. */
30945 || token->keyword == RID_TRY
30946 /* A function-transaction-block begins with `__transaction_atomic'
30947 or `__transaction_relaxed'. */
30948 || token->keyword == RID_TRANSACTION_ATOMIC
30949 || token->keyword == RID_TRANSACTION_RELAXED
30950 /* The named return value extension begins with `return'. */
30951 || token->keyword == RID_RETURN);
30952 }
30953
30954 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
30955 definition. */
30956
30957 static bool
30958 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
30959 {
30960 cp_token *token;
30961
30962 token = cp_lexer_peek_token (parser->lexer);
30963 return (token->type == CPP_OPEN_BRACE
30964 || (token->type == CPP_COLON
30965 && !parser->colon_doesnt_start_class_def_p));
30966 }
30967
30968 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
30969 C++0x) ending a template-argument. */
30970
30971 static bool
30972 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
30973 {
30974 cp_token *token;
30975
30976 token = cp_lexer_peek_token (parser->lexer);
30977 return (token->type == CPP_COMMA
30978 || token->type == CPP_GREATER
30979 || token->type == CPP_ELLIPSIS
30980 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
30981 }
30982
30983 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
30984 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
30985
30986 static bool
30987 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
30988 size_t n)
30989 {
30990 cp_token *token;
30991
30992 token = cp_lexer_peek_nth_token (parser->lexer, n);
30993 if (token->type == CPP_LESS)
30994 return true;
30995 /* Check for the sequence `<::' in the original code. It would be lexed as
30996 `[:', where `[' is a digraph, and there is no whitespace before
30997 `:'. */
30998 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
30999 {
31000 cp_token *token2;
31001 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
31002 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
31003 return true;
31004 }
31005 return false;
31006 }
31007
31008 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
31009 or none_type otherwise. */
31010
31011 static enum tag_types
31012 cp_parser_token_is_class_key (cp_token* token)
31013 {
31014 switch (token->keyword)
31015 {
31016 case RID_CLASS:
31017 return class_type;
31018 case RID_STRUCT:
31019 return record_type;
31020 case RID_UNION:
31021 return union_type;
31022
31023 default:
31024 return none_type;
31025 }
31026 }
31027
31028 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
31029 or none_type otherwise or if the token is null. */
31030
31031 static enum tag_types
31032 cp_parser_token_is_type_parameter_key (cp_token* token)
31033 {
31034 if (!token)
31035 return none_type;
31036
31037 switch (token->keyword)
31038 {
31039 case RID_CLASS:
31040 return class_type;
31041 case RID_TYPENAME:
31042 return typename_type;
31043
31044 default:
31045 return none_type;
31046 }
31047 }
31048
31049 /* Diagnose redundant enum-keys. */
31050
31051 static void
31052 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
31053 tree type, rid scoped_key)
31054 {
31055 if (!warn_redundant_tags)
31056 return;
31057
31058 tree type_decl = TYPE_MAIN_DECL (type);
31059 tree name = DECL_NAME (type_decl);
31060 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
31061 push_deferring_access_checks (dk_no_check);
31062 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
31063 pop_deferring_access_checks ();
31064
31065 /* The enum-key is redundant for uses of the TYPE that are not
31066 declarations and for which name lookup returns just the type
31067 itself. */
31068 if (decl != type_decl)
31069 return;
31070
31071 if (scoped_key != RID_CLASS
31072 && scoped_key != RID_STRUCT
31073 && current_lang_name != lang_name_cplusplus
31074 && current_namespace == global_namespace)
31075 {
31076 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
31077 enum tag in shared C/C++ code in files (such as headers) included
31078 in the main source file. */
31079 const line_map_ordinary *map = NULL;
31080 linemap_resolve_location (line_table, key_loc,
31081 LRK_MACRO_DEFINITION_LOCATION,
31082 &map);
31083 if (!MAIN_FILE_P (map))
31084 return;
31085 }
31086
31087 gcc_rich_location richloc (key_loc);
31088 richloc.add_fixit_remove (key_loc);
31089 warning_at (&richloc, OPT_Wredundant_tags,
31090 "redundant enum-key %<enum%s%> in reference to %q#T",
31091 (scoped_key == RID_CLASS ? " class"
31092 : scoped_key == RID_STRUCT ? " struct" : ""), type);
31093 }
31094
31095 /* Describes the set of declarations of a struct, class, or class template
31096 or its specializations. Used for -Wmismatched-tags. */
31097
31098 class class_decl_loc_t
31099 {
31100 public:
31101
31102 class_decl_loc_t ()
31103 : locvec (), idxdef (), def_class_key ()
31104 {
31105 locvec.create (4);
31106 }
31107
31108 /* Constructs an object for a single declaration of a class with
31109 CLASS_KEY at the current location in the current function (or
31110 at another scope). KEY_REDUNDANT is true if the class-key may
31111 be omitted in the current context without an ambiguity with
31112 another symbol with the same name.
31113 DEF_P is true for a class declaration that is a definition.
31114 CURLOC is the associated location. */
31115 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
31116 location_t curloc = input_location)
31117 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
31118 {
31119 locvec.create (4);
31120 class_key_loc_t ckl (current_function_decl, curloc, class_key,
31121 key_redundant);
31122 locvec.quick_push (ckl);
31123 }
31124
31125 /* Copy, assign, and destroy the object. Necessary because LOCVEC
31126 isn't safely copyable and assignable and doesn't release storage
31127 on its own. */
31128 class_decl_loc_t (const class_decl_loc_t &rhs)
31129 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
31130 def_class_key (rhs.def_class_key)
31131 { }
31132
31133 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
31134 {
31135 if (this == &rhs)
31136 return *this;
31137 locvec.release ();
31138 locvec = rhs.locvec.copy ();
31139 idxdef = rhs.idxdef;
31140 def_class_key = rhs.def_class_key;
31141 return *this;
31142 }
31143
31144 ~class_decl_loc_t ()
31145 {
31146 locvec.release ();
31147 }
31148
31149 /* Issues -Wmismatched-tags for a single class. */
31150 void diag_mismatched_tags (tree);
31151
31152 /* Issues -Wmismatched-tags for all classes. */
31153 static void diag_mismatched_tags ();
31154
31155 /* Adds TYPE_DECL to the collection of class decls and diagnoses
31156 redundant tags (if -Wredundant-tags is enabled). */
31157 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
31158
31159 /* Either adds this decl to the collection of class decls
31160 or diagnoses it, whichever is appropriate. */
31161 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
31162
31163 private:
31164
31165 tree function (unsigned i) const
31166 {
31167 return locvec[i].func;
31168 }
31169
31170 location_t location (unsigned i) const
31171 {
31172 return locvec[i].loc;
31173 }
31174
31175 bool key_redundant (unsigned i) const
31176 {
31177 return locvec[i].key_redundant;
31178 }
31179
31180 tag_types class_key (unsigned i) const
31181 {
31182 return locvec[i].class_key;
31183 }
31184
31185 /* True if a definition for the class has been seen. */
31186 bool def_p () const
31187 {
31188 return idxdef < locvec.length ();
31189 }
31190
31191 /* The location of a single mention of a class type with the given
31192 class-key. */
31193 struct class_key_loc_t
31194 {
31195 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
31196 : func (func), loc (loc), class_key (key), key_redundant (redundant)
31197 { }
31198
31199 /* The function the type is mentioned in. */
31200 tree func;
31201 /* The exact location. */
31202 location_t loc;
31203 /* The class-key used in the mention of the type. */
31204 tag_types class_key;
31205 /* True when the class-key could be omitted at this location
31206 without an ambiguity with another symbol of the same name. */
31207 bool key_redundant;
31208 };
31209 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
31210 vec <class_key_loc_t> locvec;
31211 /* LOCVEC index of the definition or UINT_MAX if none exists. */
31212 unsigned idxdef;
31213 /* The class-key the class was last declared with or none_type when
31214 it has been declared with a mismatched key. */
31215 tag_types def_class_key;
31216
31217 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
31218 description above. */
31219 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
31220 static class_to_loc_map_t class2loc;
31221 };
31222
31223 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
31224
31225 /* Issue an error message if the CLASS_KEY does not match the TYPE.
31226 DEF_P is expected to be set for a definition of class TYPE. DECL_P
31227 is set for a declaration of class TYPE and clear for a reference to
31228 it that is not a declaration of it. */
31229
31230 static void
31231 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
31232 tag_types class_key, tree type, bool def_p,
31233 bool decl_p)
31234 {
31235 if (type == error_mark_node)
31236 return;
31237
31238 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
31239 if (seen_as_union != (class_key == union_type))
31240 {
31241 if (permerror (input_location, "%qs tag used in naming %q#T",
31242 class_key == union_type ? "union"
31243 : class_key == record_type ? "struct" : "class",
31244 type))
31245 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
31246 "%q#T was previously declared here", type);
31247 return;
31248 }
31249
31250 if (!warn_mismatched_tags && !warn_redundant_tags)
31251 return;
31252
31253 /* Only consider the true class-keys below and ignore typename_type,
31254 etc. that are not C++ class-keys. */
31255 if (class_key != class_type
31256 && class_key != record_type
31257 && class_key != union_type)
31258 return;
31259
31260 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
31261 }
31262
31263 /* Returns the template or specialization of one to which the RECORD_TYPE
31264 TYPE corresponds. */
31265
31266 static tree
31267 specialization_of (tree type)
31268 {
31269 tree ret = type;
31270
31271 /* Determine the template or its partial specialization to which TYPE
31272 corresponds. */
31273 if (tree spec = most_specialized_partial_spec (type, tf_none))
31274 if (spec != error_mark_node)
31275 ret = TREE_TYPE (TREE_VALUE (spec));
31276
31277 if (ret == type)
31278 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
31279
31280 return TYPE_MAIN_DECL (ret);
31281 }
31282
31283
31284 /* Adds the class TYPE to the collection of class decls and diagnoses
31285 redundant tags (if -Wredundant-tags is enabled).
31286 DEF_P is expected to be set for a definition of class TYPE. DECL_P
31287 is set for a (likely, based on syntactic context) declaration of class
31288 TYPE and clear for a reference to it that is not a declaration of it. */
31289
31290 void
31291 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
31292 tag_types class_key, tree type, bool def_p, bool decl_p)
31293 {
31294 tree type_decl = TYPE_MAIN_DECL (type);
31295 tree name = DECL_NAME (type_decl);
31296 /* Look up the NAME to see if it unambiguously refers to the TYPE
31297 and set KEY_REDUNDANT if so. */
31298 push_deferring_access_checks (dk_no_check);
31299 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
31300 pop_deferring_access_checks ();
31301
31302 /* The class-key is redundant for uses of the CLASS_TYPE that are
31303 neither definitions of it nor declarations, and for which name
31304 lookup returns just the type itself. */
31305 bool key_redundant = (!def_p && !decl_p
31306 && (decl == type_decl
31307 || TREE_CODE (decl) == TEMPLATE_DECL
31308 || TYPE_BEING_DEFINED (type)));
31309
31310 if (key_redundant
31311 && class_key != class_type
31312 && current_lang_name != lang_name_cplusplus
31313 && current_namespace == global_namespace)
31314 {
31315 /* Avoid issuing the diagnostic for apparently redundant struct
31316 and union class-keys in shared C/C++ code in files (such as
31317 headers) included in the main source file. */
31318 const line_map_ordinary *map = NULL;
31319 linemap_resolve_location (line_table, key_loc,
31320 LRK_MACRO_DEFINITION_LOCATION,
31321 &map);
31322 if (!MAIN_FILE_P (map))
31323 key_redundant = false;
31324 }
31325
31326 /* Set if a declaration of TYPE has previously been seen or if it must
31327 exist in a precompiled header. */
31328 bool exist;
31329 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
31330 if (!exist)
31331 {
31332 tree type = TREE_TYPE (type_decl);
31333 if (def_p || !COMPLETE_TYPE_P (type))
31334 {
31335 /* TYPE_DECL is the first declaration or definition of the type
31336 (outside precompiled headers -- see below). Just create
31337 a new entry for it and return unless it's a declaration
31338 involving a template that may need to be diagnosed by
31339 -Wredundant-tags. */
31340 *rdl = class_decl_loc_t (class_key, false, def_p);
31341 if (TREE_CODE (decl) != TEMPLATE_DECL)
31342 return;
31343 }
31344 else
31345 {
31346 /* TYPE was previously defined in some unknown precompiled hdeader.
31347 Simply add a record of its definition at an unknown location and
31348 proceed below to add a reference to it at the current location.
31349 (Declarations in precompiled headers that are not definitions
31350 are ignored.) */
31351 tag_types def_key
31352 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
31353 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
31354 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
31355 exist = true;
31356 }
31357 }
31358
31359 /* A prior declaration of TYPE_DECL has been seen. */
31360
31361 if (key_redundant)
31362 {
31363 gcc_rich_location richloc (key_loc);
31364 richloc.add_fixit_remove (key_loc);
31365 warning_at (&richloc, OPT_Wredundant_tags,
31366 "redundant class-key %qs in reference to %q#T",
31367 class_key == union_type ? "union"
31368 : class_key == record_type ? "struct" : "class",
31369 type);
31370 }
31371
31372 if (!exist)
31373 /* Do nothing if this is the first declaration of the type. */
31374 return;
31375
31376 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
31377 /* Do nothing if the class-key in this declaration matches
31378 the definition. */
31379 return;
31380
31381 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
31382 def_p);
31383 }
31384
31385 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
31386 of class decls or diagnoses it, whichever is appropriate. */
31387
31388 void
31389 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
31390 tag_types class_key,
31391 bool redundant,
31392 bool def_p)
31393 {
31394 /* Reset the CLASS_KEY associated with this type on mismatch.
31395 This is an optimization that lets the diagnostic code skip
31396 over classes that use the same class-key in all declarations. */
31397 if (def_class_key != class_key)
31398 def_class_key = none_type;
31399
31400 /* Set IDXDEF to the index of the vector corresponding to
31401 the definition. */
31402 if (def_p)
31403 idxdef = locvec.length ();
31404
31405 /* Append a record of this declaration to the vector. */
31406 class_key_loc_t ckl (current_function_decl, input_location, class_key,
31407 redundant);
31408 locvec.safe_push (ckl);
31409
31410 if (idxdef == UINT_MAX)
31411 return;
31412
31413 /* As a space optimization diagnose declarations of a class
31414 whose definition has been seen and purge the LOCVEC of
31415 all entries except the definition. */
31416 diag_mismatched_tags (type_decl);
31417 if (idxdef)
31418 {
31419 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
31420 locvec.release ();
31421 locvec.reserve (2);
31422 locvec.safe_push (ent);
31423 idxdef = 0;
31424 }
31425 else
31426 /* Pop the entry pushed above for this declaration. */
31427 locvec.pop ();
31428 }
31429
31430 /* Issues -Wmismatched-tags for a single class. */
31431
31432 void
31433 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
31434 {
31435 if (!warn_mismatched_tags)
31436 return;
31437
31438 /* Number of uses of the class. */
31439 const unsigned ndecls = locvec.length ();
31440
31441 /* The class (or template) declaration guiding the decisions about
31442 the diagnostic. For ordinary classes it's the same as THIS. For
31443 uses of instantiations of templates other than their declarations
31444 it points to the record for the declaration of the corresponding
31445 primary template or partial specialization. */
31446 class_decl_loc_t *cdlguide = this;
31447
31448 tree type = TREE_TYPE (type_decl);
31449 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type))
31450 {
31451 /* For implicit instantiations of a primary template look up
31452 the primary or partial specialization and use it as
31453 the expected class-key rather than using the class-key of
31454 the first reference to the instantiation. The primary must
31455 be (and inevitably is) at index zero. */
31456 tree spec = specialization_of (type);
31457 cdlguide = class2loc.get (spec);
31458 gcc_assert (cdlguide != NULL);
31459 }
31460 else
31461 {
31462 /* Skip declarations that consistently use the same class-key. */
31463 if (def_class_key != none_type)
31464 return;
31465 }
31466
31467 /* Set if a definition for the class has been seen. */
31468 const bool def_p = cdlguide->def_p ();
31469
31470 /* The index of the declaration whose class-key this declaration
31471 is expected to match. It's either the class-key of the class
31472 definition if one exists or the first declaration otherwise. */
31473 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
31474
31475 /* The class-key the class is expected to be declared with: it's
31476 either the key used in its definition or the first declaration
31477 if no definition has been provided.
31478 For implicit instantiations of a primary template it's
31479 the class-key used to declare the primary with. The primary
31480 must be at index zero. */
31481 const tag_types xpect_key = cdlguide->class_key (idxguide);
31482
31483 unsigned idx = 0;
31484 /* Advance IDX to the first declaration that either is not
31485 a definition or that doesn't match the first declaration
31486 if no definition is provided. */
31487 while (class_key (idx) == xpect_key)
31488 if (++idx == ndecls)
31489 return;
31490
31491 /* Save the current function before changing it below. */
31492 tree save_func = current_function_decl;
31493 /* Set the function declaration to print in diagnostic context. */
31494 current_function_decl = function (idx);
31495
31496 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
31497 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
31498
31499 location_t loc = location (idx);
31500 bool key_redundant_p = key_redundant (idx);
31501 auto_diagnostic_group d;
31502 /* Issue a warning for the first mismatched declaration.
31503 Avoid using "%#qT" since the class-key for the same type will
31504 be the same regardless of which one was used in the declaraion. */
31505 if (warning_at (loc, OPT_Wmismatched_tags,
31506 "%qT declared with a mismatched class-key %qs",
31507 type_decl, xmatchkstr))
31508 {
31509 /* Suggest how to avoid the warning for each instance since
31510 the guidance may be different depending on context. */
31511 inform (loc,
31512 (key_redundant_p
31513 ? G_("remove the class-key or replace it with %qs")
31514 : G_("replace the class-key with %qs")),
31515 xpectkstr);
31516
31517 /* Also point to the first declaration or definition that guided
31518 the decision to issue the warning above. */
31519 inform (cdlguide->location (idxguide),
31520 (def_p
31521 ? G_("%qT defined as %qs here")
31522 : G_("%qT first declared as %qs here")),
31523 type_decl, xpectkstr);
31524 }
31525
31526 /* Issue warnings for the remaining inconsistent declarations. */
31527 for (unsigned i = idx + 1; i != ndecls; ++i)
31528 {
31529 tag_types clskey = class_key (i);
31530 /* Skip over the declarations that match either the definition
31531 if one was provided or the first declaration. */
31532 if (clskey == xpect_key)
31533 continue;
31534
31535 loc = location (i);
31536 key_redundant_p = key_redundant (i);
31537 /* Set the function declaration to print in diagnostic context. */
31538 current_function_decl = function (i);
31539 if (warning_at (loc, OPT_Wmismatched_tags,
31540 "%qT declared with a mismatched class-key %qs",
31541 type_decl, xmatchkstr))
31542 /* Suggest how to avoid the warning for each instance since
31543 the guidance may be different depending on context. */
31544 inform (loc,
31545 (key_redundant_p
31546 ? G_("remove the class-key or replace it with %qs")
31547 : G_("replace the class-key with %qs")),
31548 xpectkstr);
31549 }
31550
31551 /* Restore the current function in case it was replaced above. */
31552 current_function_decl = save_func;
31553 }
31554
31555 /* Issues -Wmismatched-tags for all classes. Called at the end
31556 of processing a translation unit, after declarations of all class
31557 types and their uses have been recorded. */
31558
31559 void
31560 class_decl_loc_t::diag_mismatched_tags ()
31561 {
31562 /* CLASS2LOC should be empty if both -Wmismatched-tags and
31563 -Wredundant-tags are disabled. */
31564 gcc_assert (warn_mismatched_tags
31565 || warn_redundant_tags
31566 || class2loc.is_empty ());
31567
31568 /* Save the current function before changing on return. It should
31569 be null at this point. */
31570 temp_override<tree> cleanup (current_function_decl);
31571
31572 if (warn_mismatched_tags)
31573 {
31574 /* Iterate over the collected class/struct/template declarations. */
31575 typedef class_to_loc_map_t::iterator iter_t;
31576 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
31577 {
31578 tree type_decl = (*it).first;
31579 class_decl_loc_t &recloc = (*it).second;
31580 recloc.diag_mismatched_tags (type_decl);
31581 }
31582 }
31583
31584 class2loc.empty ();
31585 }
31586
31587 /* Issue an error message if DECL is redeclared with different
31588 access than its original declaration [class.access.spec/3].
31589 This applies to nested classes, nested class templates and
31590 enumerations [class.mem/1]. */
31591
31592 static void
31593 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
31594 {
31595 if (!decl
31596 || (!CLASS_TYPE_P (TREE_TYPE (decl))
31597 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
31598 return;
31599
31600 if ((TREE_PRIVATE (decl)
31601 != (current_access_specifier == access_private_node))
31602 || (TREE_PROTECTED (decl)
31603 != (current_access_specifier == access_protected_node)))
31604 error_at (location, "%qD redeclared with different access", decl);
31605 }
31606
31607 /* Look for the `template' keyword, as a syntactic disambiguator.
31608 Return TRUE iff it is present, in which case it will be
31609 consumed. */
31610
31611 static bool
31612 cp_parser_optional_template_keyword (cp_parser *parser)
31613 {
31614 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31615 {
31616 /* In C++98 the `template' keyword can only be used within templates;
31617 outside templates the parser can always figure out what is a
31618 template and what is not. In C++11, per the resolution of DR 468,
31619 `template' is allowed in cases where it is not strictly necessary. */
31620 if (!processing_template_decl
31621 && pedantic && cxx_dialect == cxx98)
31622 {
31623 cp_token *token = cp_lexer_peek_token (parser->lexer);
31624 pedwarn (token->location, OPT_Wpedantic,
31625 "in C++98 %<template%> (as a disambiguator) is only "
31626 "allowed within templates");
31627 /* If this part of the token stream is rescanned, the same
31628 error message would be generated. So, we purge the token
31629 from the stream. */
31630 cp_lexer_purge_token (parser->lexer);
31631 return false;
31632 }
31633 else
31634 {
31635 /* Consume the `template' keyword. */
31636 cp_lexer_consume_token (parser->lexer);
31637 return true;
31638 }
31639 }
31640 return false;
31641 }
31642
31643 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
31644 set PARSER->SCOPE, and perform other related actions. */
31645
31646 static void
31647 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
31648 {
31649 struct tree_check *check_value;
31650
31651 /* Get the stored value. */
31652 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
31653 /* Set the scope from the stored value. */
31654 parser->scope = saved_checks_value (check_value);
31655 parser->qualifying_scope = check_value->qualifying_scope;
31656 parser->object_scope = NULL_TREE;
31657 }
31658
31659 /* Consume tokens up through a non-nested END token. Returns TRUE if we
31660 encounter the end of a block before what we were looking for. */
31661
31662 static bool
31663 cp_parser_cache_group (cp_parser *parser,
31664 enum cpp_ttype end,
31665 unsigned depth)
31666 {
31667 while (true)
31668 {
31669 cp_token *token = cp_lexer_peek_token (parser->lexer);
31670
31671 /* Abort a parenthesized expression if we encounter a semicolon. */
31672 if ((end == CPP_CLOSE_PAREN || depth == 0)
31673 && token->type == CPP_SEMICOLON)
31674 return true;
31675 /* If we've reached the end of the file, stop. */
31676 if (token->type == CPP_EOF
31677 || (end != CPP_PRAGMA_EOL
31678 && token->type == CPP_PRAGMA_EOL))
31679 return true;
31680 if (token->type == CPP_CLOSE_BRACE && depth == 0)
31681 /* We've hit the end of an enclosing block, so there's been some
31682 kind of syntax error. */
31683 return true;
31684
31685 /* Consume the token. */
31686 cp_lexer_consume_token (parser->lexer);
31687 /* See if it starts a new group. */
31688 if (token->type == CPP_OPEN_BRACE)
31689 {
31690 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
31691 /* In theory this should probably check end == '}', but
31692 cp_parser_save_member_function_body needs it to exit
31693 after either '}' or ')' when called with ')'. */
31694 if (depth == 0)
31695 return false;
31696 }
31697 else if (token->type == CPP_OPEN_PAREN)
31698 {
31699 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
31700 if (depth == 0 && end == CPP_CLOSE_PAREN)
31701 return false;
31702 }
31703 else if (token->type == CPP_PRAGMA)
31704 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
31705 else if (token->type == end)
31706 return false;
31707 }
31708 }
31709
31710 /* Like above, for caching a default argument or NSDMI. Both of these are
31711 terminated by a non-nested comma, but it can be unclear whether or not a
31712 comma is nested in a template argument list unless we do more parsing.
31713 In order to handle this ambiguity, when we encounter a ',' after a '<'
31714 we try to parse what follows as a parameter-declaration-list (in the
31715 case of a default argument) or a member-declarator (in the case of an
31716 NSDMI). If that succeeds, then we stop caching. */
31717
31718 static tree
31719 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
31720 {
31721 unsigned depth = 0;
31722 int maybe_template_id = 0;
31723 cp_token *first_token;
31724 cp_token *token;
31725 tree default_argument;
31726
31727 /* Add tokens until we have processed the entire default
31728 argument. We add the range [first_token, token). */
31729 first_token = cp_lexer_peek_token (parser->lexer);
31730 if (first_token->type == CPP_OPEN_BRACE)
31731 {
31732 /* For list-initialization, this is straightforward. */
31733 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
31734 token = cp_lexer_peek_token (parser->lexer);
31735 }
31736 else while (true)
31737 {
31738 bool done = false;
31739
31740 /* Peek at the next token. */
31741 token = cp_lexer_peek_token (parser->lexer);
31742 /* What we do depends on what token we have. */
31743 switch (token->type)
31744 {
31745 /* In valid code, a default argument must be
31746 immediately followed by a `,' `)', or `...'. */
31747 case CPP_COMMA:
31748 if (depth == 0 && maybe_template_id)
31749 {
31750 /* If we've seen a '<', we might be in a
31751 template-argument-list. Until Core issue 325 is
31752 resolved, we don't know how this situation ought
31753 to be handled, so try to DTRT. We check whether
31754 what comes after the comma is a valid parameter
31755 declaration list. If it is, then the comma ends
31756 the default argument; otherwise the default
31757 argument continues. */
31758 bool error = false;
31759 cp_token *peek;
31760
31761 /* Set ITALP so cp_parser_parameter_declaration_list
31762 doesn't decide to commit to this parse. */
31763 bool saved_italp = parser->in_template_argument_list_p;
31764 parser->in_template_argument_list_p = true;
31765
31766 cp_parser_parse_tentatively (parser);
31767
31768 if (nsdmi)
31769 {
31770 /* Parse declarators until we reach a non-comma or
31771 somthing that cannot be an initializer.
31772 Just checking whether we're looking at a single
31773 declarator is insufficient. Consider:
31774 int var = tuple<T,U>::x;
31775 The template parameter 'U' looks exactly like a
31776 declarator. */
31777 do
31778 {
31779 int ctor_dtor_or_conv_p;
31780 cp_lexer_consume_token (parser->lexer);
31781 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31782 CP_PARSER_FLAGS_NONE,
31783 &ctor_dtor_or_conv_p,
31784 /*parenthesized_p=*/NULL,
31785 /*member_p=*/true,
31786 /*friend_p=*/false,
31787 /*static_p=*/false);
31788 peek = cp_lexer_peek_token (parser->lexer);
31789 if (cp_parser_error_occurred (parser))
31790 break;
31791 }
31792 while (peek->type == CPP_COMMA);
31793 /* If we met an '=' or ';' then the original comma
31794 was the end of the NSDMI. Otherwise assume
31795 we're still in the NSDMI. */
31796 error = (peek->type != CPP_EQ
31797 && peek->type != CPP_SEMICOLON);
31798 }
31799 else
31800 {
31801 cp_lexer_consume_token (parser->lexer);
31802 begin_scope (sk_function_parms, NULL_TREE);
31803 tree t = cp_parser_parameter_declaration_list
31804 (parser, CP_PARSER_FLAGS_NONE);
31805 if (t == error_mark_node)
31806 error = true;
31807 pop_bindings_and_leave_scope ();
31808 }
31809 if (!cp_parser_error_occurred (parser) && !error)
31810 done = true;
31811 cp_parser_abort_tentative_parse (parser);
31812
31813 parser->in_template_argument_list_p = saved_italp;
31814 break;
31815 }
31816 /* FALLTHRU */
31817 case CPP_CLOSE_PAREN:
31818 case CPP_ELLIPSIS:
31819 /* If we run into a non-nested `;', `}', or `]',
31820 then the code is invalid -- but the default
31821 argument is certainly over. */
31822 case CPP_SEMICOLON:
31823 case CPP_CLOSE_BRACE:
31824 case CPP_CLOSE_SQUARE:
31825 if (depth == 0
31826 /* Handle correctly int n = sizeof ... ( p ); */
31827 && token->type != CPP_ELLIPSIS)
31828 done = true;
31829 /* Update DEPTH, if necessary. */
31830 else if (token->type == CPP_CLOSE_PAREN
31831 || token->type == CPP_CLOSE_BRACE
31832 || token->type == CPP_CLOSE_SQUARE)
31833 --depth;
31834 break;
31835
31836 case CPP_OPEN_PAREN:
31837 case CPP_OPEN_SQUARE:
31838 case CPP_OPEN_BRACE:
31839 ++depth;
31840 break;
31841
31842 case CPP_LESS:
31843 if (depth == 0)
31844 /* This might be the comparison operator, or it might
31845 start a template argument list. */
31846 ++maybe_template_id;
31847 break;
31848
31849 case CPP_RSHIFT:
31850 if (cxx_dialect == cxx98)
31851 break;
31852 /* Fall through for C++0x, which treats the `>>'
31853 operator like two `>' tokens in certain
31854 cases. */
31855 gcc_fallthrough ();
31856
31857 case CPP_GREATER:
31858 if (depth == 0)
31859 {
31860 /* This might be an operator, or it might close a
31861 template argument list. But if a previous '<'
31862 started a template argument list, this will have
31863 closed it, so we can't be in one anymore. */
31864 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
31865 if (maybe_template_id < 0)
31866 maybe_template_id = 0;
31867 }
31868 break;
31869
31870 /* If we run out of tokens, issue an error message. */
31871 case CPP_EOF:
31872 case CPP_PRAGMA_EOL:
31873 error_at (token->location, "file ends in default argument");
31874 return error_mark_node;
31875
31876 case CPP_NAME:
31877 case CPP_SCOPE:
31878 /* In these cases, we should look for template-ids.
31879 For example, if the default argument is
31880 `X<int, double>()', we need to do name lookup to
31881 figure out whether or not `X' is a template; if
31882 so, the `,' does not end the default argument.
31883
31884 That is not yet done. */
31885 break;
31886
31887 default:
31888 break;
31889 }
31890
31891 /* If we've reached the end, stop. */
31892 if (done)
31893 break;
31894
31895 /* Add the token to the token block. */
31896 token = cp_lexer_consume_token (parser->lexer);
31897 }
31898
31899 /* Create a DEFERRED_PARSE to represent the unparsed default
31900 argument. */
31901 default_argument = make_node (DEFERRED_PARSE);
31902 DEFPARSE_TOKENS (default_argument)
31903 = cp_token_cache_new (first_token, token);
31904 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
31905
31906 return default_argument;
31907 }
31908
31909 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
31910
31911 location_t
31912 defparse_location (tree default_argument)
31913 {
31914 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
31915 location_t start = tokens->first->location;
31916 location_t end = tokens->last->location;
31917 return make_location (start, start, end);
31918 }
31919
31920 /* Begin parsing tentatively. We always save tokens while parsing
31921 tentatively so that if the tentative parsing fails we can restore the
31922 tokens. */
31923
31924 static void
31925 cp_parser_parse_tentatively (cp_parser* parser)
31926 {
31927 /* Enter a new parsing context. */
31928 parser->context = cp_parser_context_new (parser->context);
31929 /* Begin saving tokens. */
31930 cp_lexer_save_tokens (parser->lexer);
31931 /* In order to avoid repetitive access control error messages,
31932 access checks are queued up until we are no longer parsing
31933 tentatively. */
31934 push_deferring_access_checks (dk_deferred);
31935 }
31936
31937 /* Commit to the currently active tentative parse. */
31938
31939 static void
31940 cp_parser_commit_to_tentative_parse (cp_parser* parser)
31941 {
31942 cp_parser_context *context;
31943 cp_lexer *lexer;
31944
31945 /* Mark all of the levels as committed. */
31946 lexer = parser->lexer;
31947 for (context = parser->context; context->next; context = context->next)
31948 {
31949 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
31950 break;
31951 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
31952 while (!cp_lexer_saving_tokens (lexer))
31953 lexer = lexer->next;
31954 cp_lexer_commit_tokens (lexer);
31955 }
31956 }
31957
31958 /* Commit to the topmost currently active tentative parse.
31959
31960 Note that this function shouldn't be called when there are
31961 irreversible side-effects while in a tentative state. For
31962 example, we shouldn't create a permanent entry in the symbol
31963 table, or issue an error message that might not apply if the
31964 tentative parse is aborted. */
31965
31966 static void
31967 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
31968 {
31969 cp_parser_context *context = parser->context;
31970 cp_lexer *lexer = parser->lexer;
31971
31972 if (context)
31973 {
31974 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
31975 return;
31976 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
31977
31978 while (!cp_lexer_saving_tokens (lexer))
31979 lexer = lexer->next;
31980 cp_lexer_commit_tokens (lexer);
31981 }
31982 }
31983
31984 /* Abort the currently active tentative parse. All consumed tokens
31985 will be rolled back, and no diagnostics will be issued. */
31986
31987 static void
31988 cp_parser_abort_tentative_parse (cp_parser* parser)
31989 {
31990 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
31991 || errorcount > 0);
31992 cp_parser_simulate_error (parser);
31993 /* Now, pretend that we want to see if the construct was
31994 successfully parsed. */
31995 cp_parser_parse_definitely (parser);
31996 }
31997
31998 /* Stop parsing tentatively. If a parse error has occurred, restore the
31999 token stream. Otherwise, commit to the tokens we have consumed.
32000 Returns true if no error occurred; false otherwise. */
32001
32002 static bool
32003 cp_parser_parse_definitely (cp_parser* parser)
32004 {
32005 bool error_occurred;
32006 cp_parser_context *context;
32007
32008 /* Remember whether or not an error occurred, since we are about to
32009 destroy that information. */
32010 error_occurred = cp_parser_error_occurred (parser);
32011 /* Remove the topmost context from the stack. */
32012 context = parser->context;
32013 parser->context = context->next;
32014 /* If no parse errors occurred, commit to the tentative parse. */
32015 if (!error_occurred)
32016 {
32017 /* Commit to the tokens read tentatively, unless that was
32018 already done. */
32019 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
32020 cp_lexer_commit_tokens (parser->lexer);
32021
32022 pop_to_parent_deferring_access_checks ();
32023 }
32024 /* Otherwise, if errors occurred, roll back our state so that things
32025 are just as they were before we began the tentative parse. */
32026 else
32027 {
32028 cp_lexer_rollback_tokens (parser->lexer);
32029 pop_deferring_access_checks ();
32030 }
32031 /* Add the context to the front of the free list. */
32032 context->next = cp_parser_context_free_list;
32033 cp_parser_context_free_list = context;
32034
32035 return !error_occurred;
32036 }
32037
32038 /* Returns true if we are parsing tentatively and are not committed to
32039 this tentative parse. */
32040
32041 static bool
32042 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
32043 {
32044 return (cp_parser_parsing_tentatively (parser)
32045 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
32046 }
32047
32048 /* Returns nonzero iff an error has occurred during the most recent
32049 tentative parse. */
32050
32051 static bool
32052 cp_parser_error_occurred (cp_parser* parser)
32053 {
32054 return (cp_parser_parsing_tentatively (parser)
32055 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
32056 }
32057
32058 /* Returns nonzero if GNU extensions are allowed. */
32059
32060 static bool
32061 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
32062 {
32063 return parser->allow_gnu_extensions_p;
32064 }
32065 \f
32066 /* Objective-C++ Productions */
32067
32068
32069 /* Parse an Objective-C expression, which feeds into a primary-expression
32070 above.
32071
32072 objc-expression:
32073 objc-message-expression
32074 objc-string-literal
32075 objc-encode-expression
32076 objc-protocol-expression
32077 objc-selector-expression
32078
32079 Returns a tree representation of the expression. */
32080
32081 static cp_expr
32082 cp_parser_objc_expression (cp_parser* parser)
32083 {
32084 /* Try to figure out what kind of declaration is present. */
32085 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
32086
32087 switch (kwd->type)
32088 {
32089 case CPP_OPEN_SQUARE:
32090 return cp_parser_objc_message_expression (parser);
32091
32092 case CPP_OBJC_STRING:
32093 kwd = cp_lexer_consume_token (parser->lexer);
32094 return objc_build_string_object (kwd->u.value);
32095
32096 case CPP_KEYWORD:
32097 switch (kwd->keyword)
32098 {
32099 case RID_AT_ENCODE:
32100 return cp_parser_objc_encode_expression (parser);
32101
32102 case RID_AT_PROTOCOL:
32103 return cp_parser_objc_protocol_expression (parser);
32104
32105 case RID_AT_SELECTOR:
32106 return cp_parser_objc_selector_expression (parser);
32107
32108 default:
32109 break;
32110 }
32111 /* FALLTHRU */
32112 default:
32113 error_at (kwd->location,
32114 "misplaced %<@%D%> Objective-C++ construct",
32115 kwd->u.value);
32116 cp_parser_skip_to_end_of_block_or_statement (parser);
32117 }
32118
32119 return error_mark_node;
32120 }
32121
32122 /* Parse an Objective-C message expression.
32123
32124 objc-message-expression:
32125 [ objc-message-receiver objc-message-args ]
32126
32127 Returns a representation of an Objective-C message. */
32128
32129 static tree
32130 cp_parser_objc_message_expression (cp_parser* parser)
32131 {
32132 tree receiver, messageargs;
32133
32134 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
32135 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
32136 receiver = cp_parser_objc_message_receiver (parser);
32137 messageargs = cp_parser_objc_message_args (parser);
32138 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
32139 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32140
32141 tree result = objc_build_message_expr (receiver, messageargs);
32142
32143 /* Construct a location e.g.
32144 [self func1:5]
32145 ^~~~~~~~~~~~~~
32146 ranging from the '[' to the ']', with the caret at the start. */
32147 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
32148 protected_set_expr_location (result, combined_loc);
32149
32150 return result;
32151 }
32152
32153 /* Parse an objc-message-receiver.
32154
32155 objc-message-receiver:
32156 expression
32157 simple-type-specifier
32158
32159 Returns a representation of the type or expression. */
32160
32161 static tree
32162 cp_parser_objc_message_receiver (cp_parser* parser)
32163 {
32164 tree rcv;
32165
32166 /* An Objective-C message receiver may be either (1) a type
32167 or (2) an expression. */
32168 cp_parser_parse_tentatively (parser);
32169 rcv = cp_parser_expression (parser);
32170
32171 /* If that worked out, fine. */
32172 if (cp_parser_parse_definitely (parser))
32173 return rcv;
32174
32175 cp_parser_parse_tentatively (parser);
32176 rcv = cp_parser_simple_type_specifier (parser,
32177 /*decl_specs=*/NULL,
32178 CP_PARSER_FLAGS_NONE);
32179
32180 if (cp_parser_parse_definitely (parser))
32181 return objc_get_class_reference (rcv);
32182
32183 cp_parser_error (parser, "objective-c++ message receiver expected");
32184 return error_mark_node;
32185 }
32186
32187 /* Parse the arguments and selectors comprising an Objective-C message.
32188
32189 objc-message-args:
32190 objc-selector
32191 objc-selector-args
32192 objc-selector-args , objc-comma-args
32193
32194 objc-selector-args:
32195 objc-selector [opt] : assignment-expression
32196 objc-selector-args objc-selector [opt] : assignment-expression
32197
32198 objc-comma-args:
32199 assignment-expression
32200 objc-comma-args , assignment-expression
32201
32202 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
32203 selector arguments and TREE_VALUE containing a list of comma
32204 arguments. */
32205
32206 static tree
32207 cp_parser_objc_message_args (cp_parser* parser)
32208 {
32209 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
32210 bool maybe_unary_selector_p = true;
32211 cp_token *token = cp_lexer_peek_token (parser->lexer);
32212
32213 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
32214 {
32215 tree selector = NULL_TREE, arg;
32216
32217 if (token->type != CPP_COLON)
32218 selector = cp_parser_objc_selector (parser);
32219
32220 /* Detect if we have a unary selector. */
32221 if (maybe_unary_selector_p
32222 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
32223 return build_tree_list (selector, NULL_TREE);
32224
32225 maybe_unary_selector_p = false;
32226 cp_parser_require (parser, CPP_COLON, RT_COLON);
32227 arg = cp_parser_assignment_expression (parser);
32228
32229 sel_args
32230 = chainon (sel_args,
32231 build_tree_list (selector, arg));
32232
32233 token = cp_lexer_peek_token (parser->lexer);
32234 }
32235
32236 /* Handle non-selector arguments, if any. */
32237 while (token->type == CPP_COMMA)
32238 {
32239 tree arg;
32240
32241 cp_lexer_consume_token (parser->lexer);
32242 arg = cp_parser_assignment_expression (parser);
32243
32244 addl_args
32245 = chainon (addl_args,
32246 build_tree_list (NULL_TREE, arg));
32247
32248 token = cp_lexer_peek_token (parser->lexer);
32249 }
32250
32251 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
32252 {
32253 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
32254 return build_tree_list (error_mark_node, error_mark_node);
32255 }
32256
32257 return build_tree_list (sel_args, addl_args);
32258 }
32259
32260 /* Parse an Objective-C encode expression.
32261
32262 objc-encode-expression:
32263 @encode objc-typename
32264
32265 Returns an encoded representation of the type argument. */
32266
32267 static cp_expr
32268 cp_parser_objc_encode_expression (cp_parser* parser)
32269 {
32270 tree type;
32271 cp_token *token;
32272 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
32273
32274 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
32275 matching_parens parens;
32276 parens.require_open (parser);
32277 token = cp_lexer_peek_token (parser->lexer);
32278 type = complete_type (cp_parser_type_id (parser));
32279 parens.require_close (parser);
32280
32281 if (!type)
32282 {
32283 error_at (token->location,
32284 "%<@encode%> must specify a type as an argument");
32285 return error_mark_node;
32286 }
32287
32288 /* This happens if we find @encode(T) (where T is a template
32289 typename or something dependent on a template typename) when
32290 parsing a template. In that case, we can't compile it
32291 immediately, but we rather create an AT_ENCODE_EXPR which will
32292 need to be instantiated when the template is used.
32293 */
32294 if (dependent_type_p (type))
32295 {
32296 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
32297 TREE_READONLY (value) = 1;
32298 return value;
32299 }
32300
32301
32302 /* Build a location of the form:
32303 @encode(int)
32304 ^~~~~~~~~~~~
32305 with caret==start at the @ token, finishing at the close paren. */
32306 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
32307
32308 return cp_expr (objc_build_encode_expr (type), combined_loc);
32309 }
32310
32311 /* Parse an Objective-C @defs expression. */
32312
32313 static tree
32314 cp_parser_objc_defs_expression (cp_parser *parser)
32315 {
32316 tree name;
32317
32318 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
32319 matching_parens parens;
32320 parens.require_open (parser);
32321 name = cp_parser_identifier (parser);
32322 parens.require_close (parser);
32323
32324 return objc_get_class_ivars (name);
32325 }
32326
32327 /* Parse an Objective-C protocol expression.
32328
32329 objc-protocol-expression:
32330 @protocol ( identifier )
32331
32332 Returns a representation of the protocol expression. */
32333
32334 static tree
32335 cp_parser_objc_protocol_expression (cp_parser* parser)
32336 {
32337 tree proto;
32338 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
32339
32340 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
32341 matching_parens parens;
32342 parens.require_open (parser);
32343 proto = cp_parser_identifier (parser);
32344 parens.require_close (parser);
32345
32346 /* Build a location of the form:
32347 @protocol(prot)
32348 ^~~~~~~~~~~~~~~
32349 with caret==start at the @ token, finishing at the close paren. */
32350 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
32351 tree result = objc_build_protocol_expr (proto);
32352 protected_set_expr_location (result, combined_loc);
32353 return result;
32354 }
32355
32356 /* Parse an Objective-C selector expression.
32357
32358 objc-selector-expression:
32359 @selector ( objc-method-signature )
32360
32361 objc-method-signature:
32362 objc-selector
32363 objc-selector-seq
32364
32365 objc-selector-seq:
32366 objc-selector :
32367 objc-selector-seq objc-selector :
32368
32369 Returns a representation of the method selector. */
32370
32371 static tree
32372 cp_parser_objc_selector_expression (cp_parser* parser)
32373 {
32374 tree sel_seq = NULL_TREE;
32375 bool maybe_unary_selector_p = true;
32376 cp_token *token;
32377 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32378
32379 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
32380 matching_parens parens;
32381 parens.require_open (parser);
32382 token = cp_lexer_peek_token (parser->lexer);
32383
32384 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
32385 || token->type == CPP_SCOPE)
32386 {
32387 tree selector = NULL_TREE;
32388
32389 if (token->type != CPP_COLON
32390 || token->type == CPP_SCOPE)
32391 selector = cp_parser_objc_selector (parser);
32392
32393 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
32394 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
32395 {
32396 /* Detect if we have a unary selector. */
32397 if (maybe_unary_selector_p)
32398 {
32399 sel_seq = selector;
32400 goto finish_selector;
32401 }
32402 else
32403 {
32404 cp_parser_error (parser, "expected %<:%>");
32405 }
32406 }
32407 maybe_unary_selector_p = false;
32408 token = cp_lexer_consume_token (parser->lexer);
32409
32410 if (token->type == CPP_SCOPE)
32411 {
32412 sel_seq
32413 = chainon (sel_seq,
32414 build_tree_list (selector, NULL_TREE));
32415 sel_seq
32416 = chainon (sel_seq,
32417 build_tree_list (NULL_TREE, NULL_TREE));
32418 }
32419 else
32420 sel_seq
32421 = chainon (sel_seq,
32422 build_tree_list (selector, NULL_TREE));
32423
32424 token = cp_lexer_peek_token (parser->lexer);
32425 }
32426
32427 finish_selector:
32428 parens.require_close (parser);
32429
32430
32431 /* Build a location of the form:
32432 @selector(func)
32433 ^~~~~~~~~~~~~~~
32434 with caret==start at the @ token, finishing at the close paren. */
32435 location_t combined_loc = make_location (loc, loc, parser->lexer);
32436 tree result = objc_build_selector_expr (combined_loc, sel_seq);
32437 /* TODO: objc_build_selector_expr doesn't always honor the location. */
32438 protected_set_expr_location (result, combined_loc);
32439 return result;
32440 }
32441
32442 /* Parse a list of identifiers.
32443
32444 objc-identifier-list:
32445 identifier
32446 objc-identifier-list , identifier
32447
32448 Returns a TREE_LIST of identifier nodes. */
32449
32450 static tree
32451 cp_parser_objc_identifier_list (cp_parser* parser)
32452 {
32453 tree identifier;
32454 tree list;
32455 cp_token *sep;
32456
32457 identifier = cp_parser_identifier (parser);
32458 if (identifier == error_mark_node)
32459 return error_mark_node;
32460
32461 list = build_tree_list (NULL_TREE, identifier);
32462 sep = cp_lexer_peek_token (parser->lexer);
32463
32464 while (sep->type == CPP_COMMA)
32465 {
32466 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
32467 identifier = cp_parser_identifier (parser);
32468 if (identifier == error_mark_node)
32469 return list;
32470
32471 list = chainon (list, build_tree_list (NULL_TREE,
32472 identifier));
32473 sep = cp_lexer_peek_token (parser->lexer);
32474 }
32475
32476 return list;
32477 }
32478
32479 /* Parse an Objective-C alias declaration.
32480
32481 objc-alias-declaration:
32482 @compatibility_alias identifier identifier ;
32483
32484 This function registers the alias mapping with the Objective-C front end.
32485 It returns nothing. */
32486
32487 static void
32488 cp_parser_objc_alias_declaration (cp_parser* parser)
32489 {
32490 tree alias, orig;
32491
32492 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
32493 alias = cp_parser_identifier (parser);
32494 orig = cp_parser_identifier (parser);
32495 objc_declare_alias (alias, orig);
32496 cp_parser_consume_semicolon_at_end_of_statement (parser);
32497 }
32498
32499 /* Parse an Objective-C class forward-declaration.
32500
32501 objc-class-declaration:
32502 @class objc-identifier-list ;
32503
32504 The function registers the forward declarations with the Objective-C
32505 front end. It returns nothing. */
32506
32507 static void
32508 cp_parser_objc_class_declaration (cp_parser* parser)
32509 {
32510 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
32511 while (true)
32512 {
32513 tree id;
32514
32515 id = cp_parser_identifier (parser);
32516 if (id == error_mark_node)
32517 break;
32518
32519 objc_declare_class (id);
32520
32521 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32522 cp_lexer_consume_token (parser->lexer);
32523 else
32524 break;
32525 }
32526 cp_parser_consume_semicolon_at_end_of_statement (parser);
32527 }
32528
32529 /* Parse a list of Objective-C protocol references.
32530
32531 objc-protocol-refs-opt:
32532 objc-protocol-refs [opt]
32533
32534 objc-protocol-refs:
32535 < objc-identifier-list >
32536
32537 Returns a TREE_LIST of identifiers, if any. */
32538
32539 static tree
32540 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
32541 {
32542 tree protorefs = NULL_TREE;
32543
32544 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
32545 {
32546 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
32547 protorefs = cp_parser_objc_identifier_list (parser);
32548 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
32549 }
32550
32551 return protorefs;
32552 }
32553
32554 /* Parse a Objective-C visibility specification. */
32555
32556 static void
32557 cp_parser_objc_visibility_spec (cp_parser* parser)
32558 {
32559 cp_token *vis = cp_lexer_peek_token (parser->lexer);
32560
32561 switch (vis->keyword)
32562 {
32563 case RID_AT_PRIVATE:
32564 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
32565 break;
32566 case RID_AT_PROTECTED:
32567 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
32568 break;
32569 case RID_AT_PUBLIC:
32570 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
32571 break;
32572 case RID_AT_PACKAGE:
32573 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
32574 break;
32575 default:
32576 return;
32577 }
32578
32579 /* Eat '@private'/'@protected'/'@public'. */
32580 cp_lexer_consume_token (parser->lexer);
32581 }
32582
32583 /* Parse an Objective-C method type. Return 'true' if it is a class
32584 (+) method, and 'false' if it is an instance (-) method. */
32585
32586 static inline bool
32587 cp_parser_objc_method_type (cp_parser* parser)
32588 {
32589 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
32590 return true;
32591 else
32592 return false;
32593 }
32594
32595 /* Parse an Objective-C protocol qualifier. */
32596
32597 static tree
32598 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
32599 {
32600 tree quals = NULL_TREE, node;
32601 cp_token *token = cp_lexer_peek_token (parser->lexer);
32602
32603 node = token->u.value;
32604
32605 while (node && identifier_p (node)
32606 && (node == ridpointers [(int) RID_IN]
32607 || node == ridpointers [(int) RID_OUT]
32608 || node == ridpointers [(int) RID_INOUT]
32609 || node == ridpointers [(int) RID_BYCOPY]
32610 || node == ridpointers [(int) RID_BYREF]
32611 || node == ridpointers [(int) RID_ONEWAY]))
32612 {
32613 quals = tree_cons (NULL_TREE, node, quals);
32614 cp_lexer_consume_token (parser->lexer);
32615 token = cp_lexer_peek_token (parser->lexer);
32616 node = token->u.value;
32617 }
32618
32619 return quals;
32620 }
32621
32622 /* Parse an Objective-C typename. */
32623
32624 static tree
32625 cp_parser_objc_typename (cp_parser* parser)
32626 {
32627 tree type_name = NULL_TREE;
32628
32629 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32630 {
32631 tree proto_quals, cp_type = NULL_TREE;
32632
32633 matching_parens parens;
32634 parens.consume_open (parser); /* Eat '('. */
32635 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
32636
32637 /* An ObjC type name may consist of just protocol qualifiers, in which
32638 case the type shall default to 'id'. */
32639 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
32640 {
32641 cp_type = cp_parser_type_id (parser);
32642
32643 /* If the type could not be parsed, an error has already
32644 been produced. For error recovery, behave as if it had
32645 not been specified, which will use the default type
32646 'id'. */
32647 if (cp_type == error_mark_node)
32648 {
32649 cp_type = NULL_TREE;
32650 /* We need to skip to the closing parenthesis as
32651 cp_parser_type_id() does not seem to do it for
32652 us. */
32653 cp_parser_skip_to_closing_parenthesis (parser,
32654 /*recovering=*/true,
32655 /*or_comma=*/false,
32656 /*consume_paren=*/false);
32657 }
32658 }
32659
32660 parens.require_close (parser);
32661 type_name = build_tree_list (proto_quals, cp_type);
32662 }
32663
32664 return type_name;
32665 }
32666
32667 /* Check to see if TYPE refers to an Objective-C selector name. */
32668
32669 static bool
32670 cp_parser_objc_selector_p (enum cpp_ttype type)
32671 {
32672 return (type == CPP_NAME || type == CPP_KEYWORD
32673 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
32674 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
32675 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
32676 || type == CPP_XOR || type == CPP_XOR_EQ);
32677 }
32678
32679 /* Parse an Objective-C selector. */
32680
32681 static tree
32682 cp_parser_objc_selector (cp_parser* parser)
32683 {
32684 cp_token *token = cp_lexer_consume_token (parser->lexer);
32685
32686 if (!cp_parser_objc_selector_p (token->type))
32687 {
32688 error_at (token->location, "invalid Objective-C++ selector name");
32689 return error_mark_node;
32690 }
32691
32692 /* C++ operator names are allowed to appear in ObjC selectors. */
32693 switch (token->type)
32694 {
32695 case CPP_AND_AND: return get_identifier ("and");
32696 case CPP_AND_EQ: return get_identifier ("and_eq");
32697 case CPP_AND: return get_identifier ("bitand");
32698 case CPP_OR: return get_identifier ("bitor");
32699 case CPP_COMPL: return get_identifier ("compl");
32700 case CPP_NOT: return get_identifier ("not");
32701 case CPP_NOT_EQ: return get_identifier ("not_eq");
32702 case CPP_OR_OR: return get_identifier ("or");
32703 case CPP_OR_EQ: return get_identifier ("or_eq");
32704 case CPP_XOR: return get_identifier ("xor");
32705 case CPP_XOR_EQ: return get_identifier ("xor_eq");
32706 default: return token->u.value;
32707 }
32708 }
32709
32710 /* Parse an Objective-C params list. */
32711
32712 static tree
32713 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
32714 {
32715 tree params = NULL_TREE;
32716 bool maybe_unary_selector_p = true;
32717 cp_token *token = cp_lexer_peek_token (parser->lexer);
32718
32719 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
32720 {
32721 tree selector = NULL_TREE, type_name, identifier;
32722 tree parm_attr = NULL_TREE;
32723
32724 if (token->keyword == RID_ATTRIBUTE)
32725 break;
32726
32727 if (token->type != CPP_COLON)
32728 selector = cp_parser_objc_selector (parser);
32729
32730 /* Detect if we have a unary selector. */
32731 if (maybe_unary_selector_p
32732 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
32733 {
32734 params = selector; /* Might be followed by attributes. */
32735 break;
32736 }
32737
32738 maybe_unary_selector_p = false;
32739 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32740 {
32741 /* Something went quite wrong. There should be a colon
32742 here, but there is not. Stop parsing parameters. */
32743 break;
32744 }
32745 type_name = cp_parser_objc_typename (parser);
32746 /* New ObjC allows attributes on parameters too. */
32747 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32748 parm_attr = cp_parser_attributes_opt (parser);
32749 identifier = cp_parser_identifier (parser);
32750
32751 params
32752 = chainon (params,
32753 objc_build_keyword_decl (selector,
32754 type_name,
32755 identifier,
32756 parm_attr));
32757
32758 token = cp_lexer_peek_token (parser->lexer);
32759 }
32760
32761 if (params == NULL_TREE)
32762 {
32763 cp_parser_error (parser, "objective-c++ method declaration is expected");
32764 return error_mark_node;
32765 }
32766
32767 /* We allow tail attributes for the method. */
32768 if (token->keyword == RID_ATTRIBUTE)
32769 {
32770 *attributes = cp_parser_attributes_opt (parser);
32771 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
32772 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32773 return params;
32774 cp_parser_error (parser,
32775 "method attributes must be specified at the end");
32776 return error_mark_node;
32777 }
32778
32779 if (params == NULL_TREE)
32780 {
32781 cp_parser_error (parser, "objective-c++ method declaration is expected");
32782 return error_mark_node;
32783 }
32784 return params;
32785 }
32786
32787 /* Parse the non-keyword Objective-C params. */
32788
32789 static tree
32790 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
32791 tree* attributes)
32792 {
32793 tree params = make_node (TREE_LIST);
32794 cp_token *token = cp_lexer_peek_token (parser->lexer);
32795 *ellipsisp = false; /* Initially, assume no ellipsis. */
32796
32797 while (token->type == CPP_COMMA)
32798 {
32799 cp_parameter_declarator *parmdecl;
32800 tree parm;
32801
32802 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
32803 token = cp_lexer_peek_token (parser->lexer);
32804
32805 if (token->type == CPP_ELLIPSIS)
32806 {
32807 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
32808 *ellipsisp = true;
32809 token = cp_lexer_peek_token (parser->lexer);
32810 break;
32811 }
32812
32813 /* TODO: parse attributes for tail parameters. */
32814 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
32815 false, NULL);
32816 parm = grokdeclarator (parmdecl->declarator,
32817 &parmdecl->decl_specifiers,
32818 PARM, /*initialized=*/0,
32819 /*attrlist=*/NULL);
32820
32821 chainon (params, build_tree_list (NULL_TREE, parm));
32822 token = cp_lexer_peek_token (parser->lexer);
32823 }
32824
32825 /* We allow tail attributes for the method. */
32826 if (token->keyword == RID_ATTRIBUTE)
32827 {
32828 if (*attributes == NULL_TREE)
32829 {
32830 *attributes = cp_parser_attributes_opt (parser);
32831 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
32832 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32833 return params;
32834 }
32835 else
32836 /* We have an error, but parse the attributes, so that we can
32837 carry on. */
32838 *attributes = cp_parser_attributes_opt (parser);
32839
32840 cp_parser_error (parser,
32841 "method attributes must be specified at the end");
32842 return error_mark_node;
32843 }
32844
32845 return params;
32846 }
32847
32848 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
32849
32850 static void
32851 cp_parser_objc_interstitial_code (cp_parser* parser)
32852 {
32853 cp_token *token = cp_lexer_peek_token (parser->lexer);
32854
32855 /* If the next token is `extern' and the following token is a string
32856 literal, then we have a linkage specification. */
32857 if (token->keyword == RID_EXTERN
32858 && cp_parser_is_pure_string_literal
32859 (cp_lexer_peek_nth_token (parser->lexer, 2)))
32860 cp_parser_linkage_specification (parser);
32861 /* Handle #pragma, if any. */
32862 else if (token->type == CPP_PRAGMA)
32863 cp_parser_pragma (parser, pragma_objc_icode, NULL);
32864 /* Allow stray semicolons. */
32865 else if (token->type == CPP_SEMICOLON)
32866 cp_lexer_consume_token (parser->lexer);
32867 /* Mark methods as optional or required, when building protocols. */
32868 else if (token->keyword == RID_AT_OPTIONAL)
32869 {
32870 cp_lexer_consume_token (parser->lexer);
32871 objc_set_method_opt (true);
32872 }
32873 else if (token->keyword == RID_AT_REQUIRED)
32874 {
32875 cp_lexer_consume_token (parser->lexer);
32876 objc_set_method_opt (false);
32877 }
32878 else if (token->keyword == RID_NAMESPACE)
32879 cp_parser_namespace_definition (parser);
32880 /* Other stray characters must generate errors. */
32881 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
32882 {
32883 cp_lexer_consume_token (parser->lexer);
32884 error ("stray %qs between Objective-C++ methods",
32885 token->type == CPP_OPEN_BRACE ? "{" : "}");
32886 }
32887 /* Finally, try to parse a block-declaration, or a function-definition. */
32888 else
32889 cp_parser_block_declaration (parser, /*statement_p=*/false);
32890 }
32891
32892 /* Parse a method signature. */
32893
32894 static tree
32895 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
32896 {
32897 tree rettype, kwdparms, optparms;
32898 bool ellipsis = false;
32899 bool is_class_method;
32900
32901 is_class_method = cp_parser_objc_method_type (parser);
32902 rettype = cp_parser_objc_typename (parser);
32903 *attributes = NULL_TREE;
32904 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
32905 if (kwdparms == error_mark_node)
32906 return error_mark_node;
32907 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
32908 if (optparms == error_mark_node)
32909 return error_mark_node;
32910
32911 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
32912 }
32913
32914 static bool
32915 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
32916 {
32917 tree tattr;
32918 cp_lexer_save_tokens (parser->lexer);
32919 tattr = cp_parser_attributes_opt (parser);
32920 gcc_assert (tattr) ;
32921
32922 /* If the attributes are followed by a method introducer, this is not allowed.
32923 Dump the attributes and flag the situation. */
32924 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
32925 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32926 return true;
32927
32928 /* Otherwise, the attributes introduce some interstitial code, possibly so
32929 rewind to allow that check. */
32930 cp_lexer_rollback_tokens (parser->lexer);
32931 return false;
32932 }
32933
32934 /* Parse an Objective-C method prototype list. */
32935
32936 static void
32937 cp_parser_objc_method_prototype_list (cp_parser* parser)
32938 {
32939 cp_token *token = cp_lexer_peek_token (parser->lexer);
32940
32941 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
32942 {
32943 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
32944 {
32945 tree attributes, sig;
32946 bool is_class_method;
32947 if (token->type == CPP_PLUS)
32948 is_class_method = true;
32949 else
32950 is_class_method = false;
32951 sig = cp_parser_objc_method_signature (parser, &attributes);
32952 if (sig == error_mark_node)
32953 {
32954 cp_parser_skip_to_end_of_block_or_statement (parser);
32955 token = cp_lexer_peek_token (parser->lexer);
32956 continue;
32957 }
32958 objc_add_method_declaration (is_class_method, sig, attributes);
32959 cp_parser_consume_semicolon_at_end_of_statement (parser);
32960 }
32961 else if (token->keyword == RID_AT_PROPERTY)
32962 cp_parser_objc_at_property_declaration (parser);
32963 else if (token->keyword == RID_ATTRIBUTE
32964 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
32965 warning_at (cp_lexer_peek_token (parser->lexer)->location,
32966 OPT_Wattributes,
32967 "prefix attributes are ignored for methods");
32968 else
32969 /* Allow for interspersed non-ObjC++ code. */
32970 cp_parser_objc_interstitial_code (parser);
32971
32972 token = cp_lexer_peek_token (parser->lexer);
32973 }
32974
32975 if (token->type != CPP_EOF)
32976 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
32977 else
32978 cp_parser_error (parser, "expected %<@end%>");
32979
32980 objc_finish_interface ();
32981 }
32982
32983 /* Parse an Objective-C method definition list. */
32984
32985 static void
32986 cp_parser_objc_method_definition_list (cp_parser* parser)
32987 {
32988 cp_token *token = cp_lexer_peek_token (parser->lexer);
32989
32990 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
32991 {
32992 tree meth;
32993
32994 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
32995 {
32996 cp_token *ptk;
32997 tree sig, attribute;
32998 bool is_class_method;
32999 if (token->type == CPP_PLUS)
33000 is_class_method = true;
33001 else
33002 is_class_method = false;
33003 push_deferring_access_checks (dk_deferred);
33004 sig = cp_parser_objc_method_signature (parser, &attribute);
33005 if (sig == error_mark_node)
33006 {
33007 cp_parser_skip_to_end_of_block_or_statement (parser);
33008 token = cp_lexer_peek_token (parser->lexer);
33009 continue;
33010 }
33011 objc_start_method_definition (is_class_method, sig, attribute,
33012 NULL_TREE);
33013
33014 /* For historical reasons, we accept an optional semicolon. */
33015 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33016 cp_lexer_consume_token (parser->lexer);
33017
33018 ptk = cp_lexer_peek_token (parser->lexer);
33019 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
33020 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
33021 {
33022 perform_deferred_access_checks (tf_warning_or_error);
33023 stop_deferring_access_checks ();
33024 meth = cp_parser_function_definition_after_declarator (parser,
33025 false);
33026 pop_deferring_access_checks ();
33027 objc_finish_method_definition (meth);
33028 }
33029 }
33030 /* The following case will be removed once @synthesize is
33031 completely implemented. */
33032 else if (token->keyword == RID_AT_PROPERTY)
33033 cp_parser_objc_at_property_declaration (parser);
33034 else if (token->keyword == RID_AT_SYNTHESIZE)
33035 cp_parser_objc_at_synthesize_declaration (parser);
33036 else if (token->keyword == RID_AT_DYNAMIC)
33037 cp_parser_objc_at_dynamic_declaration (parser);
33038 else if (token->keyword == RID_ATTRIBUTE
33039 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
33040 warning_at (token->location, OPT_Wattributes,
33041 "prefix attributes are ignored for methods");
33042 else
33043 /* Allow for interspersed non-ObjC++ code. */
33044 cp_parser_objc_interstitial_code (parser);
33045
33046 token = cp_lexer_peek_token (parser->lexer);
33047 }
33048
33049 if (token->type != CPP_EOF)
33050 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
33051 else
33052 cp_parser_error (parser, "expected %<@end%>");
33053
33054 objc_finish_implementation ();
33055 }
33056
33057 /* Parse Objective-C ivars. */
33058
33059 static void
33060 cp_parser_objc_class_ivars (cp_parser* parser)
33061 {
33062 cp_token *token = cp_lexer_peek_token (parser->lexer);
33063
33064 if (token->type != CPP_OPEN_BRACE)
33065 return; /* No ivars specified. */
33066
33067 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
33068 token = cp_lexer_peek_token (parser->lexer);
33069
33070 while (token->type != CPP_CLOSE_BRACE
33071 && token->keyword != RID_AT_END && token->type != CPP_EOF)
33072 {
33073 cp_decl_specifier_seq declspecs;
33074 int decl_class_or_enum_p;
33075 tree prefix_attributes;
33076
33077 cp_parser_objc_visibility_spec (parser);
33078
33079 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33080 break;
33081
33082 cp_parser_decl_specifier_seq (parser,
33083 CP_PARSER_FLAGS_OPTIONAL,
33084 &declspecs,
33085 &decl_class_or_enum_p);
33086
33087 /* auto, register, static, extern, mutable. */
33088 if (declspecs.storage_class != sc_none)
33089 {
33090 cp_parser_error (parser, "invalid type for instance variable");
33091 declspecs.storage_class = sc_none;
33092 }
33093
33094 /* thread_local. */
33095 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
33096 {
33097 cp_parser_error (parser, "invalid type for instance variable");
33098 declspecs.locations[ds_thread] = 0;
33099 }
33100
33101 /* typedef. */
33102 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
33103 {
33104 cp_parser_error (parser, "invalid type for instance variable");
33105 declspecs.locations[ds_typedef] = 0;
33106 }
33107
33108 prefix_attributes = declspecs.attributes;
33109 declspecs.attributes = NULL_TREE;
33110
33111 /* Keep going until we hit the `;' at the end of the
33112 declaration. */
33113 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33114 {
33115 tree width = NULL_TREE, attributes, first_attribute, decl;
33116 cp_declarator *declarator = NULL;
33117 int ctor_dtor_or_conv_p;
33118
33119 /* Check for a (possibly unnamed) bitfield declaration. */
33120 token = cp_lexer_peek_token (parser->lexer);
33121 if (token->type == CPP_COLON)
33122 goto eat_colon;
33123
33124 if (token->type == CPP_NAME
33125 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
33126 == CPP_COLON))
33127 {
33128 /* Get the name of the bitfield. */
33129 declarator = make_id_declarator (NULL_TREE,
33130 cp_parser_identifier (parser),
33131 sfk_none, token->location);
33132
33133 eat_colon:
33134 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
33135 /* Get the width of the bitfield. */
33136 width
33137 = cp_parser_constant_expression (parser);
33138 }
33139 else
33140 {
33141 /* Parse the declarator. */
33142 declarator
33143 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
33144 CP_PARSER_FLAGS_NONE,
33145 &ctor_dtor_or_conv_p,
33146 /*parenthesized_p=*/NULL,
33147 /*member_p=*/false,
33148 /*friend_p=*/false,
33149 /*static_p=*/false);
33150 }
33151
33152 /* Look for attributes that apply to the ivar. */
33153 attributes = cp_parser_attributes_opt (parser);
33154 /* Remember which attributes are prefix attributes and
33155 which are not. */
33156 first_attribute = attributes;
33157 /* Combine the attributes. */
33158 attributes = attr_chainon (prefix_attributes, attributes);
33159
33160 if (width)
33161 /* Create the bitfield declaration. */
33162 decl = grokbitfield (declarator, &declspecs,
33163 width, NULL_TREE, attributes);
33164 else
33165 decl = grokfield (declarator, &declspecs,
33166 NULL_TREE, /*init_const_expr_p=*/false,
33167 NULL_TREE, attributes);
33168
33169 /* Add the instance variable. */
33170 if (decl != error_mark_node && decl != NULL_TREE)
33171 objc_add_instance_variable (decl);
33172
33173 /* Reset PREFIX_ATTRIBUTES. */
33174 if (attributes != error_mark_node)
33175 {
33176 while (attributes && TREE_CHAIN (attributes) != first_attribute)
33177 attributes = TREE_CHAIN (attributes);
33178 if (attributes)
33179 TREE_CHAIN (attributes) = NULL_TREE;
33180 }
33181
33182 token = cp_lexer_peek_token (parser->lexer);
33183
33184 if (token->type == CPP_COMMA)
33185 {
33186 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
33187 continue;
33188 }
33189 break;
33190 }
33191
33192 cp_parser_consume_semicolon_at_end_of_statement (parser);
33193 token = cp_lexer_peek_token (parser->lexer);
33194 }
33195
33196 if (token->keyword == RID_AT_END)
33197 cp_parser_error (parser, "expected %<}%>");
33198
33199 /* Do not consume the RID_AT_END, so it will be read again as terminating
33200 the @interface of @implementation. */
33201 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
33202 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
33203
33204 /* For historical reasons, we accept an optional semicolon. */
33205 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33206 cp_lexer_consume_token (parser->lexer);
33207 }
33208
33209 /* Parse an Objective-C protocol declaration. */
33210
33211 static void
33212 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
33213 {
33214 tree proto, protorefs;
33215 cp_token *tok;
33216
33217 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
33218 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33219 {
33220 tok = cp_lexer_peek_token (parser->lexer);
33221 error_at (tok->location, "identifier expected after %<@protocol%>");
33222 cp_parser_consume_semicolon_at_end_of_statement (parser);
33223 return;
33224 }
33225
33226 /* See if we have a forward declaration or a definition. */
33227 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
33228
33229 /* Try a forward declaration first. */
33230 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
33231 {
33232 while (true)
33233 {
33234 tree id;
33235
33236 id = cp_parser_identifier (parser);
33237 if (id == error_mark_node)
33238 break;
33239
33240 objc_declare_protocol (id, attributes);
33241
33242 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33243 cp_lexer_consume_token (parser->lexer);
33244 else
33245 break;
33246 }
33247 cp_parser_consume_semicolon_at_end_of_statement (parser);
33248 }
33249
33250 /* Ok, we got a full-fledged definition (or at least should). */
33251 else
33252 {
33253 proto = cp_parser_identifier (parser);
33254 protorefs = cp_parser_objc_protocol_refs_opt (parser);
33255 objc_start_protocol (proto, protorefs, attributes);
33256 cp_parser_objc_method_prototype_list (parser);
33257 }
33258 }
33259
33260 /* Parse an Objective-C superclass or category. */
33261
33262 static void
33263 cp_parser_objc_superclass_or_category (cp_parser *parser,
33264 bool iface_p,
33265 tree *super,
33266 tree *categ, bool *is_class_extension)
33267 {
33268 cp_token *next = cp_lexer_peek_token (parser->lexer);
33269
33270 *super = *categ = NULL_TREE;
33271 *is_class_extension = false;
33272 if (next->type == CPP_COLON)
33273 {
33274 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
33275 *super = cp_parser_identifier (parser);
33276 }
33277 else if (next->type == CPP_OPEN_PAREN)
33278 {
33279 matching_parens parens;
33280 parens.consume_open (parser); /* Eat '('. */
33281
33282 /* If there is no category name, and this is an @interface, we
33283 have a class extension. */
33284 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33285 {
33286 *categ = NULL_TREE;
33287 *is_class_extension = true;
33288 }
33289 else
33290 *categ = cp_parser_identifier (parser);
33291
33292 parens.require_close (parser);
33293 }
33294 }
33295
33296 /* Parse an Objective-C class interface. */
33297
33298 static void
33299 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
33300 {
33301 tree name, super, categ, protos;
33302 bool is_class_extension;
33303
33304 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
33305 name = cp_parser_identifier (parser);
33306 if (name == error_mark_node)
33307 {
33308 /* It's hard to recover because even if valid @interface stuff
33309 is to follow, we can't compile it (or validate it) if we
33310 don't even know which class it refers to. Let's assume this
33311 was a stray '@interface' token in the stream and skip it.
33312 */
33313 return;
33314 }
33315 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
33316 &is_class_extension);
33317 protos = cp_parser_objc_protocol_refs_opt (parser);
33318
33319 /* We have either a class or a category on our hands. */
33320 if (categ || is_class_extension)
33321 objc_start_category_interface (name, categ, protos, attributes);
33322 else
33323 {
33324 objc_start_class_interface (name, super, protos, attributes);
33325 /* Handle instance variable declarations, if any. */
33326 cp_parser_objc_class_ivars (parser);
33327 objc_continue_interface ();
33328 }
33329
33330 cp_parser_objc_method_prototype_list (parser);
33331 }
33332
33333 /* Parse an Objective-C class implementation. */
33334
33335 static void
33336 cp_parser_objc_class_implementation (cp_parser* parser)
33337 {
33338 tree name, super, categ;
33339 bool is_class_extension;
33340
33341 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
33342 name = cp_parser_identifier (parser);
33343 if (name == error_mark_node)
33344 {
33345 /* It's hard to recover because even if valid @implementation
33346 stuff is to follow, we can't compile it (or validate it) if
33347 we don't even know which class it refers to. Let's assume
33348 this was a stray '@implementation' token in the stream and
33349 skip it.
33350 */
33351 return;
33352 }
33353 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
33354 &is_class_extension);
33355
33356 /* We have either a class or a category on our hands. */
33357 if (categ)
33358 objc_start_category_implementation (name, categ);
33359 else
33360 {
33361 objc_start_class_implementation (name, super);
33362 /* Handle instance variable declarations, if any. */
33363 cp_parser_objc_class_ivars (parser);
33364 objc_continue_implementation ();
33365 }
33366
33367 cp_parser_objc_method_definition_list (parser);
33368 }
33369
33370 /* Consume the @end token and finish off the implementation. */
33371
33372 static void
33373 cp_parser_objc_end_implementation (cp_parser* parser)
33374 {
33375 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
33376 objc_finish_implementation ();
33377 }
33378
33379 /* Parse an Objective-C declaration. */
33380
33381 static void
33382 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
33383 {
33384 /* Try to figure out what kind of declaration is present. */
33385 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
33386
33387 if (attributes)
33388 switch (kwd->keyword)
33389 {
33390 case RID_AT_ALIAS:
33391 case RID_AT_CLASS:
33392 case RID_AT_END:
33393 error_at (kwd->location, "attributes may not be specified before"
33394 " the %<@%D%> Objective-C++ keyword",
33395 kwd->u.value);
33396 attributes = NULL;
33397 break;
33398 case RID_AT_IMPLEMENTATION:
33399 warning_at (kwd->location, OPT_Wattributes,
33400 "prefix attributes are ignored before %<@%D%>",
33401 kwd->u.value);
33402 attributes = NULL;
33403 default:
33404 break;
33405 }
33406
33407 switch (kwd->keyword)
33408 {
33409 case RID_AT_ALIAS:
33410 cp_parser_objc_alias_declaration (parser);
33411 break;
33412 case RID_AT_CLASS:
33413 cp_parser_objc_class_declaration (parser);
33414 break;
33415 case RID_AT_PROTOCOL:
33416 cp_parser_objc_protocol_declaration (parser, attributes);
33417 break;
33418 case RID_AT_INTERFACE:
33419 cp_parser_objc_class_interface (parser, attributes);
33420 break;
33421 case RID_AT_IMPLEMENTATION:
33422 cp_parser_objc_class_implementation (parser);
33423 break;
33424 case RID_AT_END:
33425 cp_parser_objc_end_implementation (parser);
33426 break;
33427 default:
33428 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
33429 kwd->u.value);
33430 cp_parser_skip_to_end_of_block_or_statement (parser);
33431 }
33432 }
33433
33434 /* Parse an Objective-C try-catch-finally statement.
33435
33436 objc-try-catch-finally-stmt:
33437 @try compound-statement objc-catch-clause-seq [opt]
33438 objc-finally-clause [opt]
33439
33440 objc-catch-clause-seq:
33441 objc-catch-clause objc-catch-clause-seq [opt]
33442
33443 objc-catch-clause:
33444 @catch ( objc-exception-declaration ) compound-statement
33445
33446 objc-finally-clause:
33447 @finally compound-statement
33448
33449 objc-exception-declaration:
33450 parameter-declaration
33451 '...'
33452
33453 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
33454
33455 Returns NULL_TREE.
33456
33457 PS: This function is identical to c_parser_objc_try_catch_finally_statement
33458 for C. Keep them in sync. */
33459
33460 static tree
33461 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
33462 {
33463 location_t location;
33464 tree stmt;
33465
33466 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
33467 location = cp_lexer_peek_token (parser->lexer)->location;
33468 objc_maybe_warn_exceptions (location);
33469 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
33470 node, lest it get absorbed into the surrounding block. */
33471 stmt = push_stmt_list ();
33472 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
33473 objc_begin_try_stmt (location, pop_stmt_list (stmt));
33474
33475 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
33476 {
33477 cp_parameter_declarator *parm;
33478 tree parameter_declaration = error_mark_node;
33479 bool seen_open_paren = false;
33480 matching_parens parens;
33481
33482 cp_lexer_consume_token (parser->lexer);
33483 if (parens.require_open (parser))
33484 seen_open_paren = true;
33485 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33486 {
33487 /* We have "@catch (...)" (where the '...' are literally
33488 what is in the code). Skip the '...'.
33489 parameter_declaration is set to NULL_TREE, and
33490 objc_being_catch_clauses() knows that that means
33491 '...'. */
33492 cp_lexer_consume_token (parser->lexer);
33493 parameter_declaration = NULL_TREE;
33494 }
33495 else
33496 {
33497 /* We have "@catch (NSException *exception)" or something
33498 like that. Parse the parameter declaration. */
33499 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
33500 false, NULL);
33501 if (parm == NULL)
33502 parameter_declaration = error_mark_node;
33503 else
33504 parameter_declaration = grokdeclarator (parm->declarator,
33505 &parm->decl_specifiers,
33506 PARM, /*initialized=*/0,
33507 /*attrlist=*/NULL);
33508 }
33509 if (seen_open_paren)
33510 parens.require_close (parser);
33511 else
33512 {
33513 /* If there was no open parenthesis, we are recovering from
33514 an error, and we are trying to figure out what mistake
33515 the user has made. */
33516
33517 /* If there is an immediate closing parenthesis, the user
33518 probably forgot the opening one (ie, they typed "@catch
33519 NSException *e)". Parse the closing parenthesis and keep
33520 going. */
33521 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33522 cp_lexer_consume_token (parser->lexer);
33523
33524 /* If these is no immediate closing parenthesis, the user
33525 probably doesn't know that parenthesis are required at
33526 all (ie, they typed "@catch NSException *e"). So, just
33527 forget about the closing parenthesis and keep going. */
33528 }
33529 objc_begin_catch_clause (parameter_declaration);
33530 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
33531 objc_finish_catch_clause ();
33532 }
33533 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
33534 {
33535 cp_lexer_consume_token (parser->lexer);
33536 location = cp_lexer_peek_token (parser->lexer)->location;
33537 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
33538 node, lest it get absorbed into the surrounding block. */
33539 stmt = push_stmt_list ();
33540 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
33541 objc_build_finally_clause (location, pop_stmt_list (stmt));
33542 }
33543
33544 return objc_finish_try_stmt ();
33545 }
33546
33547 /* Parse an Objective-C synchronized statement.
33548
33549 objc-synchronized-stmt:
33550 @synchronized ( expression ) compound-statement
33551
33552 Returns NULL_TREE. */
33553
33554 static tree
33555 cp_parser_objc_synchronized_statement (cp_parser *parser)
33556 {
33557 location_t location;
33558 tree lock, stmt;
33559
33560 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
33561
33562 location = cp_lexer_peek_token (parser->lexer)->location;
33563 objc_maybe_warn_exceptions (location);
33564 matching_parens parens;
33565 parens.require_open (parser);
33566 lock = cp_parser_expression (parser);
33567 parens.require_close (parser);
33568
33569 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
33570 node, lest it get absorbed into the surrounding block. */
33571 stmt = push_stmt_list ();
33572 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
33573
33574 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
33575 }
33576
33577 /* Parse an Objective-C throw statement.
33578
33579 objc-throw-stmt:
33580 @throw assignment-expression [opt] ;
33581
33582 Returns a constructed '@throw' statement. */
33583
33584 static tree
33585 cp_parser_objc_throw_statement (cp_parser *parser)
33586 {
33587 tree expr = NULL_TREE;
33588 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33589
33590 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
33591
33592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33593 expr = cp_parser_expression (parser);
33594
33595 cp_parser_consume_semicolon_at_end_of_statement (parser);
33596
33597 return objc_build_throw_stmt (loc, expr);
33598 }
33599
33600 /* Parse an Objective-C statement. */
33601
33602 static tree
33603 cp_parser_objc_statement (cp_parser * parser)
33604 {
33605 /* Try to figure out what kind of declaration is present. */
33606 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
33607
33608 switch (kwd->keyword)
33609 {
33610 case RID_AT_TRY:
33611 return cp_parser_objc_try_catch_finally_statement (parser);
33612 case RID_AT_SYNCHRONIZED:
33613 return cp_parser_objc_synchronized_statement (parser);
33614 case RID_AT_THROW:
33615 return cp_parser_objc_throw_statement (parser);
33616 default:
33617 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
33618 kwd->u.value);
33619 cp_parser_skip_to_end_of_block_or_statement (parser);
33620 }
33621
33622 return error_mark_node;
33623 }
33624
33625 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
33626 look ahead to see if an objc keyword follows the attributes. This
33627 is to detect the use of prefix attributes on ObjC @interface and
33628 @protocol. */
33629
33630 static bool
33631 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
33632 {
33633 cp_lexer_save_tokens (parser->lexer);
33634 *attrib = cp_parser_attributes_opt (parser);
33635 gcc_assert (*attrib);
33636 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
33637 {
33638 cp_lexer_commit_tokens (parser->lexer);
33639 return true;
33640 }
33641 cp_lexer_rollback_tokens (parser->lexer);
33642 return false;
33643 }
33644
33645 /* This routine is a minimal replacement for
33646 c_parser_struct_declaration () used when parsing the list of
33647 types/names or ObjC++ properties. For example, when parsing the
33648 code
33649
33650 @property (readonly) int a, b, c;
33651
33652 this function is responsible for parsing "int a, int b, int c" and
33653 returning the declarations as CHAIN of DECLs.
33654
33655 TODO: Share this code with cp_parser_objc_class_ivars. It's very
33656 similar parsing. */
33657 static tree
33658 cp_parser_objc_struct_declaration (cp_parser *parser)
33659 {
33660 tree decls = NULL_TREE;
33661 cp_decl_specifier_seq declspecs;
33662 int decl_class_or_enum_p;
33663 tree prefix_attributes;
33664
33665 cp_parser_decl_specifier_seq (parser,
33666 CP_PARSER_FLAGS_NONE,
33667 &declspecs,
33668 &decl_class_or_enum_p);
33669
33670 if (declspecs.type == error_mark_node)
33671 return error_mark_node;
33672
33673 /* auto, register, static, extern, mutable. */
33674 if (declspecs.storage_class != sc_none)
33675 {
33676 cp_parser_error (parser, "invalid type for property");
33677 declspecs.storage_class = sc_none;
33678 }
33679
33680 /* thread_local. */
33681 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
33682 {
33683 cp_parser_error (parser, "invalid type for property");
33684 declspecs.locations[ds_thread] = 0;
33685 }
33686
33687 /* typedef. */
33688 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
33689 {
33690 cp_parser_error (parser, "invalid type for property");
33691 declspecs.locations[ds_typedef] = 0;
33692 }
33693
33694 prefix_attributes = declspecs.attributes;
33695 declspecs.attributes = NULL_TREE;
33696
33697 /* Keep going until we hit the `;' at the end of the declaration. */
33698 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33699 {
33700 tree attributes, first_attribute, decl;
33701 cp_declarator *declarator;
33702 cp_token *token;
33703
33704 /* Parse the declarator. */
33705 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
33706 CP_PARSER_FLAGS_NONE,
33707 NULL, NULL, false, false, false);
33708
33709 /* Look for attributes that apply to the ivar. */
33710 attributes = cp_parser_attributes_opt (parser);
33711 /* Remember which attributes are prefix attributes and
33712 which are not. */
33713 first_attribute = attributes;
33714 /* Combine the attributes. */
33715 attributes = attr_chainon (prefix_attributes, attributes);
33716
33717 decl = grokfield (declarator, &declspecs,
33718 NULL_TREE, /*init_const_expr_p=*/false,
33719 NULL_TREE, attributes);
33720
33721 if (decl == error_mark_node || decl == NULL_TREE)
33722 return error_mark_node;
33723
33724 /* Reset PREFIX_ATTRIBUTES. */
33725 if (attributes != error_mark_node)
33726 {
33727 while (attributes && TREE_CHAIN (attributes) != first_attribute)
33728 attributes = TREE_CHAIN (attributes);
33729 if (attributes)
33730 TREE_CHAIN (attributes) = NULL_TREE;
33731 }
33732
33733 DECL_CHAIN (decl) = decls;
33734 decls = decl;
33735
33736 token = cp_lexer_peek_token (parser->lexer);
33737 if (token->type == CPP_COMMA)
33738 {
33739 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
33740 continue;
33741 }
33742 else
33743 break;
33744 }
33745 return decls;
33746 }
33747
33748 /* Parse an Objective-C @property declaration. The syntax is:
33749
33750 objc-property-declaration:
33751 '@property' objc-property-attributes[opt] struct-declaration ;
33752
33753 objc-property-attributes:
33754 '(' objc-property-attribute-list ')'
33755
33756 objc-property-attribute-list:
33757 objc-property-attribute
33758 objc-property-attribute-list, objc-property-attribute
33759
33760 objc-property-attribute
33761 'getter' = identifier
33762 'setter' = identifier
33763 'readonly'
33764 'readwrite'
33765 'assign'
33766 'retain'
33767 'copy'
33768 'nonatomic'
33769
33770 For example:
33771 @property NSString *name;
33772 @property (readonly) id object;
33773 @property (retain, nonatomic, getter=getTheName) id name;
33774 @property int a, b, c;
33775
33776 PS: This function is identical to
33777 c_parser_objc_at_property_declaration for C. Keep them in sync. */
33778 static void
33779 cp_parser_objc_at_property_declaration (cp_parser *parser)
33780 {
33781 /* The following variables hold the attributes of the properties as
33782 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
33783 seen. When we see an attribute, we set them to 'true' (if they
33784 are boolean properties) or to the identifier (if they have an
33785 argument, ie, for getter and setter). Note that here we only
33786 parse the list of attributes, check the syntax and accumulate the
33787 attributes that we find. objc_add_property_declaration() will
33788 then process the information. */
33789 bool property_assign = false;
33790 bool property_copy = false;
33791 tree property_getter_ident = NULL_TREE;
33792 bool property_nonatomic = false;
33793 bool property_readonly = false;
33794 bool property_readwrite = false;
33795 bool property_retain = false;
33796 tree property_setter_ident = NULL_TREE;
33797
33798 /* 'properties' is the list of properties that we read. Usually a
33799 single one, but maybe more (eg, in "@property int a, b, c;" there
33800 are three). */
33801 tree properties;
33802 location_t loc;
33803
33804 loc = cp_lexer_peek_token (parser->lexer)->location;
33805
33806 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
33807
33808 /* Parse the optional attribute list... */
33809 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33810 {
33811 /* Eat the '('. */
33812 matching_parens parens;
33813 parens.consume_open (parser);
33814
33815 while (true)
33816 {
33817 bool syntax_error = false;
33818 cp_token *token = cp_lexer_peek_token (parser->lexer);
33819 enum rid keyword;
33820
33821 if (token->type != CPP_NAME)
33822 {
33823 cp_parser_error (parser, "expected identifier");
33824 break;
33825 }
33826 keyword = C_RID_CODE (token->u.value);
33827 cp_lexer_consume_token (parser->lexer);
33828 switch (keyword)
33829 {
33830 case RID_ASSIGN: property_assign = true; break;
33831 case RID_COPY: property_copy = true; break;
33832 case RID_NONATOMIC: property_nonatomic = true; break;
33833 case RID_READONLY: property_readonly = true; break;
33834 case RID_READWRITE: property_readwrite = true; break;
33835 case RID_RETAIN: property_retain = true; break;
33836
33837 case RID_GETTER:
33838 case RID_SETTER:
33839 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33840 {
33841 if (keyword == RID_GETTER)
33842 cp_parser_error (parser,
33843 "missing %<=%> (after %<getter%> attribute)");
33844 else
33845 cp_parser_error (parser,
33846 "missing %<=%> (after %<setter%> attribute)");
33847 syntax_error = true;
33848 break;
33849 }
33850 cp_lexer_consume_token (parser->lexer); /* eat the = */
33851 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
33852 {
33853 cp_parser_error (parser, "expected identifier");
33854 syntax_error = true;
33855 break;
33856 }
33857 if (keyword == RID_SETTER)
33858 {
33859 if (property_setter_ident != NULL_TREE)
33860 {
33861 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
33862 cp_lexer_consume_token (parser->lexer);
33863 }
33864 else
33865 property_setter_ident = cp_parser_objc_selector (parser);
33866 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
33867 cp_parser_error (parser, "setter name must terminate with %<:%>");
33868 else
33869 cp_lexer_consume_token (parser->lexer);
33870 }
33871 else
33872 {
33873 if (property_getter_ident != NULL_TREE)
33874 {
33875 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
33876 cp_lexer_consume_token (parser->lexer);
33877 }
33878 else
33879 property_getter_ident = cp_parser_objc_selector (parser);
33880 }
33881 break;
33882 default:
33883 cp_parser_error (parser, "unknown property attribute");
33884 syntax_error = true;
33885 break;
33886 }
33887
33888 if (syntax_error)
33889 break;
33890
33891 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33892 cp_lexer_consume_token (parser->lexer);
33893 else
33894 break;
33895 }
33896
33897 /* FIXME: "@property (setter, assign);" will generate a spurious
33898 "error: expected ‘)’ before ‘,’ token". This is because
33899 cp_parser_require, unlike the C counterpart, will produce an
33900 error even if we are in error recovery. */
33901 if (!parens.require_close (parser))
33902 {
33903 cp_parser_skip_to_closing_parenthesis (parser,
33904 /*recovering=*/true,
33905 /*or_comma=*/false,
33906 /*consume_paren=*/true);
33907 }
33908 }
33909
33910 /* ... and the property declaration(s). */
33911 properties = cp_parser_objc_struct_declaration (parser);
33912
33913 if (properties == error_mark_node)
33914 {
33915 cp_parser_skip_to_end_of_statement (parser);
33916 /* If the next token is now a `;', consume it. */
33917 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33918 cp_lexer_consume_token (parser->lexer);
33919 return;
33920 }
33921
33922 if (properties == NULL_TREE)
33923 cp_parser_error (parser, "expected identifier");
33924 else
33925 {
33926 /* Comma-separated properties are chained together in
33927 reverse order; add them one by one. */
33928 properties = nreverse (properties);
33929
33930 for (; properties; properties = TREE_CHAIN (properties))
33931 objc_add_property_declaration (loc, copy_node (properties),
33932 property_readonly, property_readwrite,
33933 property_assign, property_retain,
33934 property_copy, property_nonatomic,
33935 property_getter_ident, property_setter_ident);
33936 }
33937
33938 cp_parser_consume_semicolon_at_end_of_statement (parser);
33939 }
33940
33941 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
33942
33943 objc-synthesize-declaration:
33944 @synthesize objc-synthesize-identifier-list ;
33945
33946 objc-synthesize-identifier-list:
33947 objc-synthesize-identifier
33948 objc-synthesize-identifier-list, objc-synthesize-identifier
33949
33950 objc-synthesize-identifier
33951 identifier
33952 identifier = identifier
33953
33954 For example:
33955 @synthesize MyProperty;
33956 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
33957
33958 PS: This function is identical to c_parser_objc_at_synthesize_declaration
33959 for C. Keep them in sync.
33960 */
33961 static void
33962 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
33963 {
33964 tree list = NULL_TREE;
33965 location_t loc;
33966 loc = cp_lexer_peek_token (parser->lexer)->location;
33967
33968 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
33969 while (true)
33970 {
33971 tree property, ivar;
33972 property = cp_parser_identifier (parser);
33973 if (property == error_mark_node)
33974 {
33975 cp_parser_consume_semicolon_at_end_of_statement (parser);
33976 return;
33977 }
33978 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
33979 {
33980 cp_lexer_consume_token (parser->lexer);
33981 ivar = cp_parser_identifier (parser);
33982 if (ivar == error_mark_node)
33983 {
33984 cp_parser_consume_semicolon_at_end_of_statement (parser);
33985 return;
33986 }
33987 }
33988 else
33989 ivar = NULL_TREE;
33990 list = chainon (list, build_tree_list (ivar, property));
33991 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33992 cp_lexer_consume_token (parser->lexer);
33993 else
33994 break;
33995 }
33996 cp_parser_consume_semicolon_at_end_of_statement (parser);
33997 objc_add_synthesize_declaration (loc, list);
33998 }
33999
34000 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
34001
34002 objc-dynamic-declaration:
34003 @dynamic identifier-list ;
34004
34005 For example:
34006 @dynamic MyProperty;
34007 @dynamic MyProperty, AnotherProperty;
34008
34009 PS: This function is identical to c_parser_objc_at_dynamic_declaration
34010 for C. Keep them in sync.
34011 */
34012 static void
34013 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
34014 {
34015 tree list = NULL_TREE;
34016 location_t loc;
34017 loc = cp_lexer_peek_token (parser->lexer)->location;
34018
34019 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
34020 while (true)
34021 {
34022 tree property;
34023 property = cp_parser_identifier (parser);
34024 if (property == error_mark_node)
34025 {
34026 cp_parser_consume_semicolon_at_end_of_statement (parser);
34027 return;
34028 }
34029 list = chainon (list, build_tree_list (NULL, property));
34030 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34031 cp_lexer_consume_token (parser->lexer);
34032 else
34033 break;
34034 }
34035 cp_parser_consume_semicolon_at_end_of_statement (parser);
34036 objc_add_dynamic_declaration (loc, list);
34037 }
34038
34039 \f
34040 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
34041
34042 /* Returns name of the next clause.
34043 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
34044 the token is not consumed. Otherwise appropriate pragma_omp_clause is
34045 returned and the token is consumed. */
34046
34047 static pragma_omp_clause
34048 cp_parser_omp_clause_name (cp_parser *parser)
34049 {
34050 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
34051
34052 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
34053 result = PRAGMA_OACC_CLAUSE_AUTO;
34054 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
34055 result = PRAGMA_OMP_CLAUSE_IF;
34056 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
34057 result = PRAGMA_OMP_CLAUSE_DEFAULT;
34058 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
34059 result = PRAGMA_OACC_CLAUSE_DELETE;
34060 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
34061 result = PRAGMA_OMP_CLAUSE_PRIVATE;
34062 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34063 result = PRAGMA_OMP_CLAUSE_FOR;
34064 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34065 {
34066 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34067 const char *p = IDENTIFIER_POINTER (id);
34068
34069 switch (p[0])
34070 {
34071 case 'a':
34072 if (!strcmp ("aligned", p))
34073 result = PRAGMA_OMP_CLAUSE_ALIGNED;
34074 else if (!strcmp ("async", p))
34075 result = PRAGMA_OACC_CLAUSE_ASYNC;
34076 else if (!strcmp ("attach", p))
34077 result = PRAGMA_OACC_CLAUSE_ATTACH;
34078 break;
34079 case 'b':
34080 if (!strcmp ("bind", p))
34081 result = PRAGMA_OMP_CLAUSE_BIND;
34082 break;
34083 case 'c':
34084 if (!strcmp ("collapse", p))
34085 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
34086 else if (!strcmp ("copy", p))
34087 result = PRAGMA_OACC_CLAUSE_COPY;
34088 else if (!strcmp ("copyin", p))
34089 result = PRAGMA_OMP_CLAUSE_COPYIN;
34090 else if (!strcmp ("copyout", p))
34091 result = PRAGMA_OACC_CLAUSE_COPYOUT;
34092 else if (!strcmp ("copyprivate", p))
34093 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
34094 else if (!strcmp ("create", p))
34095 result = PRAGMA_OACC_CLAUSE_CREATE;
34096 break;
34097 case 'd':
34098 if (!strcmp ("defaultmap", p))
34099 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
34100 else if (!strcmp ("depend", p))
34101 result = PRAGMA_OMP_CLAUSE_DEPEND;
34102 else if (!strcmp ("detach", p))
34103 result = PRAGMA_OACC_CLAUSE_DETACH;
34104 else if (!strcmp ("device", p))
34105 result = PRAGMA_OMP_CLAUSE_DEVICE;
34106 else if (!strcmp ("deviceptr", p))
34107 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
34108 else if (!strcmp ("device_resident", p))
34109 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
34110 else if (!strcmp ("device_type", p))
34111 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
34112 else if (!strcmp ("dist_schedule", p))
34113 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
34114 break;
34115 case 'f':
34116 if (!strcmp ("final", p))
34117 result = PRAGMA_OMP_CLAUSE_FINAL;
34118 else if (!strcmp ("finalize", p))
34119 result = PRAGMA_OACC_CLAUSE_FINALIZE;
34120 else if (!strcmp ("firstprivate", p))
34121 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
34122 else if (!strcmp ("from", p))
34123 result = PRAGMA_OMP_CLAUSE_FROM;
34124 break;
34125 case 'g':
34126 if (!strcmp ("gang", p))
34127 result = PRAGMA_OACC_CLAUSE_GANG;
34128 else if (!strcmp ("grainsize", p))
34129 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
34130 break;
34131 case 'h':
34132 if (!strcmp ("hint", p))
34133 result = PRAGMA_OMP_CLAUSE_HINT;
34134 else if (!strcmp ("host", p))
34135 result = PRAGMA_OACC_CLAUSE_HOST;
34136 break;
34137 case 'i':
34138 if (!strcmp ("if_present", p))
34139 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
34140 else if (!strcmp ("in_reduction", p))
34141 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
34142 else if (!strcmp ("inbranch", p))
34143 result = PRAGMA_OMP_CLAUSE_INBRANCH;
34144 else if (!strcmp ("independent", p))
34145 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
34146 else if (!strcmp ("is_device_ptr", p))
34147 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
34148 break;
34149 case 'l':
34150 if (!strcmp ("lastprivate", p))
34151 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
34152 else if (!strcmp ("linear", p))
34153 result = PRAGMA_OMP_CLAUSE_LINEAR;
34154 else if (!strcmp ("link", p))
34155 result = PRAGMA_OMP_CLAUSE_LINK;
34156 break;
34157 case 'm':
34158 if (!strcmp ("map", p))
34159 result = PRAGMA_OMP_CLAUSE_MAP;
34160 else if (!strcmp ("mergeable", p))
34161 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
34162 break;
34163 case 'n':
34164 if (!strcmp ("no_create", p))
34165 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
34166 else if (!strcmp ("nogroup", p))
34167 result = PRAGMA_OMP_CLAUSE_NOGROUP;
34168 else if (!strcmp ("nontemporal", p))
34169 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
34170 else if (!strcmp ("notinbranch", p))
34171 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
34172 else if (!strcmp ("nowait", p))
34173 result = PRAGMA_OMP_CLAUSE_NOWAIT;
34174 else if (!strcmp ("num_gangs", p))
34175 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
34176 else if (!strcmp ("num_tasks", p))
34177 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
34178 else if (!strcmp ("num_teams", p))
34179 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
34180 else if (!strcmp ("num_threads", p))
34181 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
34182 else if (!strcmp ("num_workers", p))
34183 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
34184 break;
34185 case 'o':
34186 if (!strcmp ("ordered", p))
34187 result = PRAGMA_OMP_CLAUSE_ORDERED;
34188 else if (!strcmp ("order", p))
34189 result = PRAGMA_OMP_CLAUSE_ORDER;
34190 break;
34191 case 'p':
34192 if (!strcmp ("parallel", p))
34193 result = PRAGMA_OMP_CLAUSE_PARALLEL;
34194 else if (!strcmp ("present", p))
34195 result = PRAGMA_OACC_CLAUSE_PRESENT;
34196 else if (!strcmp ("present_or_copy", p)
34197 || !strcmp ("pcopy", p))
34198 result = PRAGMA_OACC_CLAUSE_COPY;
34199 else if (!strcmp ("present_or_copyin", p)
34200 || !strcmp ("pcopyin", p))
34201 result = PRAGMA_OACC_CLAUSE_COPYIN;
34202 else if (!strcmp ("present_or_copyout", p)
34203 || !strcmp ("pcopyout", p))
34204 result = PRAGMA_OACC_CLAUSE_COPYOUT;
34205 else if (!strcmp ("present_or_create", p)
34206 || !strcmp ("pcreate", p))
34207 result = PRAGMA_OACC_CLAUSE_CREATE;
34208 else if (!strcmp ("priority", p))
34209 result = PRAGMA_OMP_CLAUSE_PRIORITY;
34210 else if (!strcmp ("proc_bind", p))
34211 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
34212 break;
34213 case 'r':
34214 if (!strcmp ("reduction", p))
34215 result = PRAGMA_OMP_CLAUSE_REDUCTION;
34216 break;
34217 case 's':
34218 if (!strcmp ("safelen", p))
34219 result = PRAGMA_OMP_CLAUSE_SAFELEN;
34220 else if (!strcmp ("schedule", p))
34221 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
34222 else if (!strcmp ("sections", p))
34223 result = PRAGMA_OMP_CLAUSE_SECTIONS;
34224 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
34225 result = PRAGMA_OACC_CLAUSE_HOST;
34226 else if (!strcmp ("seq", p))
34227 result = PRAGMA_OACC_CLAUSE_SEQ;
34228 else if (!strcmp ("shared", p))
34229 result = PRAGMA_OMP_CLAUSE_SHARED;
34230 else if (!strcmp ("simd", p))
34231 result = PRAGMA_OMP_CLAUSE_SIMD;
34232 else if (!strcmp ("simdlen", p))
34233 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
34234 break;
34235 case 't':
34236 if (!strcmp ("task_reduction", p))
34237 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
34238 else if (!strcmp ("taskgroup", p))
34239 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
34240 else if (!strcmp ("thread_limit", p))
34241 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
34242 else if (!strcmp ("threads", p))
34243 result = PRAGMA_OMP_CLAUSE_THREADS;
34244 else if (!strcmp ("tile", p))
34245 result = PRAGMA_OACC_CLAUSE_TILE;
34246 else if (!strcmp ("to", p))
34247 result = PRAGMA_OMP_CLAUSE_TO;
34248 break;
34249 case 'u':
34250 if (!strcmp ("uniform", p))
34251 result = PRAGMA_OMP_CLAUSE_UNIFORM;
34252 else if (!strcmp ("untied", p))
34253 result = PRAGMA_OMP_CLAUSE_UNTIED;
34254 else if (!strcmp ("use_device", p))
34255 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
34256 else if (!strcmp ("use_device_addr", p))
34257 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
34258 else if (!strcmp ("use_device_ptr", p))
34259 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
34260 break;
34261 case 'v':
34262 if (!strcmp ("vector", p))
34263 result = PRAGMA_OACC_CLAUSE_VECTOR;
34264 else if (!strcmp ("vector_length", p))
34265 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
34266 break;
34267 case 'w':
34268 if (!strcmp ("wait", p))
34269 result = PRAGMA_OACC_CLAUSE_WAIT;
34270 else if (!strcmp ("worker", p))
34271 result = PRAGMA_OACC_CLAUSE_WORKER;
34272 break;
34273 }
34274 }
34275
34276 if (result != PRAGMA_OMP_CLAUSE_NONE)
34277 cp_lexer_consume_token (parser->lexer);
34278
34279 return result;
34280 }
34281
34282 /* Validate that a clause of the given type does not already exist. */
34283
34284 static void
34285 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
34286 const char *name, location_t location)
34287 {
34288 if (omp_find_clause (clauses, code))
34289 error_at (location, "too many %qs clauses", name);
34290 }
34291
34292 /* OpenMP 2.5:
34293 variable-list:
34294 identifier
34295 variable-list , identifier
34296
34297 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
34298 colon). An opening parenthesis will have been consumed by the caller.
34299
34300 If KIND is nonzero, create the appropriate node and install the decl
34301 in OMP_CLAUSE_DECL and add the node to the head of the list.
34302
34303 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
34304 return the list created.
34305
34306 COLON can be NULL if only closing parenthesis should end the list,
34307 or pointer to bool which will receive false if the list is terminated
34308 by closing parenthesis or true if the list is terminated by colon.
34309
34310 The optional ALLOW_DEREF argument is true if list items can use the deref
34311 (->) operator. */
34312
34313 static tree
34314 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
34315 tree list, bool *colon,
34316 bool allow_deref = false)
34317 {
34318 cp_token *token;
34319 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
34320 if (colon)
34321 {
34322 parser->colon_corrects_to_scope_p = false;
34323 *colon = false;
34324 }
34325 while (1)
34326 {
34327 tree name, decl;
34328
34329 if (kind == OMP_CLAUSE_DEPEND)
34330 cp_parser_parse_tentatively (parser);
34331 token = cp_lexer_peek_token (parser->lexer);
34332 if (kind != 0
34333 && current_class_ptr
34334 && cp_parser_is_keyword (token, RID_THIS))
34335 {
34336 decl = finish_this_expr ();
34337 if (TREE_CODE (decl) == NON_LVALUE_EXPR
34338 || CONVERT_EXPR_P (decl))
34339 decl = TREE_OPERAND (decl, 0);
34340 cp_lexer_consume_token (parser->lexer);
34341 }
34342 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
34343 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
34344 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
34345 {
34346 cp_id_kind idk;
34347 decl = cp_parser_primary_expression (parser, false, false, false,
34348 &idk);
34349 }
34350 else
34351 {
34352 name = cp_parser_id_expression (parser, /*template_p=*/false,
34353 /*check_dependency_p=*/true,
34354 /*template_p=*/NULL,
34355 /*declarator_p=*/false,
34356 /*optional_p=*/false);
34357 if (name == error_mark_node)
34358 {
34359 if (kind == OMP_CLAUSE_DEPEND
34360 && cp_parser_simulate_error (parser))
34361 goto depend_lvalue;
34362 goto skip_comma;
34363 }
34364
34365 if (identifier_p (name))
34366 decl = cp_parser_lookup_name_simple (parser, name, token->location);
34367 else
34368 decl = name;
34369 if (decl == error_mark_node)
34370 {
34371 if (kind == OMP_CLAUSE_DEPEND
34372 && cp_parser_simulate_error (parser))
34373 goto depend_lvalue;
34374 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
34375 token->location);
34376 }
34377 }
34378 if (outer_automatic_var_p (decl))
34379 decl = process_outer_var_ref (decl, tf_warning_or_error);
34380 if (decl == error_mark_node)
34381 ;
34382 else if (kind != 0)
34383 {
34384 switch (kind)
34385 {
34386 case OMP_CLAUSE__CACHE_:
34387 /* The OpenACC cache directive explicitly only allows "array
34388 elements or subarrays". */
34389 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
34390 {
34391 error_at (token->location, "expected %<[%>");
34392 decl = error_mark_node;
34393 break;
34394 }
34395 /* FALLTHROUGH. */
34396 case OMP_CLAUSE_MAP:
34397 case OMP_CLAUSE_FROM:
34398 case OMP_CLAUSE_TO:
34399 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
34400 || (allow_deref
34401 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
34402 {
34403 cpp_ttype ttype
34404 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
34405 ? CPP_DOT : CPP_DEREF;
34406 location_t loc
34407 = cp_lexer_peek_token (parser->lexer)->location;
34408 cp_id_kind idk = CP_ID_KIND_NONE;
34409 cp_lexer_consume_token (parser->lexer);
34410 decl = convert_from_reference (decl);
34411 decl
34412 = cp_parser_postfix_dot_deref_expression (parser, ttype,
34413 decl, false,
34414 &idk, loc);
34415 }
34416 /* FALLTHROUGH. */
34417 case OMP_CLAUSE_DEPEND:
34418 case OMP_CLAUSE_REDUCTION:
34419 case OMP_CLAUSE_IN_REDUCTION:
34420 case OMP_CLAUSE_TASK_REDUCTION:
34421 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
34422 {
34423 tree low_bound = NULL_TREE, length = NULL_TREE;
34424
34425 parser->colon_corrects_to_scope_p = false;
34426 cp_lexer_consume_token (parser->lexer);
34427 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34428 low_bound = cp_parser_expression (parser);
34429 if (!colon)
34430 parser->colon_corrects_to_scope_p
34431 = saved_colon_corrects_to_scope_p;
34432 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
34433 length = integer_one_node;
34434 else
34435 {
34436 /* Look for `:'. */
34437 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34438 {
34439 if (kind == OMP_CLAUSE_DEPEND
34440 && cp_parser_simulate_error (parser))
34441 goto depend_lvalue;
34442 goto skip_comma;
34443 }
34444 if (kind == OMP_CLAUSE_DEPEND)
34445 cp_parser_commit_to_tentative_parse (parser);
34446 if (!cp_lexer_next_token_is (parser->lexer,
34447 CPP_CLOSE_SQUARE))
34448 length = cp_parser_expression (parser);
34449 }
34450 /* Look for the closing `]'. */
34451 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
34452 RT_CLOSE_SQUARE))
34453 {
34454 if (kind == OMP_CLAUSE_DEPEND
34455 && cp_parser_simulate_error (parser))
34456 goto depend_lvalue;
34457 goto skip_comma;
34458 }
34459
34460 decl = tree_cons (low_bound, length, decl);
34461 }
34462 break;
34463 default:
34464 break;
34465 }
34466
34467 if (kind == OMP_CLAUSE_DEPEND)
34468 {
34469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
34470 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
34471 && cp_parser_simulate_error (parser))
34472 {
34473 depend_lvalue:
34474 cp_parser_abort_tentative_parse (parser);
34475 decl = cp_parser_assignment_expression (parser, NULL,
34476 false, false);
34477 }
34478 else
34479 cp_parser_parse_definitely (parser);
34480 }
34481
34482 tree u = build_omp_clause (token->location, kind);
34483 OMP_CLAUSE_DECL (u) = decl;
34484 OMP_CLAUSE_CHAIN (u) = list;
34485 list = u;
34486 }
34487 else
34488 list = tree_cons (decl, NULL_TREE, list);
34489
34490 get_comma:
34491 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34492 break;
34493 cp_lexer_consume_token (parser->lexer);
34494 }
34495
34496 if (colon)
34497 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34498
34499 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34500 {
34501 *colon = true;
34502 cp_parser_require (parser, CPP_COLON, RT_COLON);
34503 return list;
34504 }
34505
34506 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34507 {
34508 int ending;
34509
34510 /* Try to resync to an unnested comma. Copied from
34511 cp_parser_parenthesized_expression_list. */
34512 skip_comma:
34513 if (colon)
34514 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34515 ending = cp_parser_skip_to_closing_parenthesis (parser,
34516 /*recovering=*/true,
34517 /*or_comma=*/true,
34518 /*consume_paren=*/true);
34519 if (ending < 0)
34520 goto get_comma;
34521 }
34522
34523 return list;
34524 }
34525
34526 /* Similarly, but expect leading and trailing parenthesis. This is a very
34527 common case for omp clauses. */
34528
34529 static tree
34530 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
34531 bool allow_deref = false)
34532 {
34533 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34534 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
34535 allow_deref);
34536 return list;
34537 }
34538
34539 /* OpenACC 2.0:
34540 copy ( variable-list )
34541 copyin ( variable-list )
34542 copyout ( variable-list )
34543 create ( variable-list )
34544 delete ( variable-list )
34545 present ( variable-list )
34546
34547 OpenACC 2.6:
34548 no_create ( variable-list )
34549 attach ( variable-list )
34550 detach ( variable-list ) */
34551
34552 static tree
34553 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
34554 tree list)
34555 {
34556 enum gomp_map_kind kind;
34557 switch (c_kind)
34558 {
34559 case PRAGMA_OACC_CLAUSE_ATTACH:
34560 kind = GOMP_MAP_ATTACH;
34561 break;
34562 case PRAGMA_OACC_CLAUSE_COPY:
34563 kind = GOMP_MAP_TOFROM;
34564 break;
34565 case PRAGMA_OACC_CLAUSE_COPYIN:
34566 kind = GOMP_MAP_TO;
34567 break;
34568 case PRAGMA_OACC_CLAUSE_COPYOUT:
34569 kind = GOMP_MAP_FROM;
34570 break;
34571 case PRAGMA_OACC_CLAUSE_CREATE:
34572 kind = GOMP_MAP_ALLOC;
34573 break;
34574 case PRAGMA_OACC_CLAUSE_DELETE:
34575 kind = GOMP_MAP_RELEASE;
34576 break;
34577 case PRAGMA_OACC_CLAUSE_DETACH:
34578 kind = GOMP_MAP_DETACH;
34579 break;
34580 case PRAGMA_OACC_CLAUSE_DEVICE:
34581 kind = GOMP_MAP_FORCE_TO;
34582 break;
34583 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34584 kind = GOMP_MAP_DEVICE_RESIDENT;
34585 break;
34586 case PRAGMA_OACC_CLAUSE_HOST:
34587 kind = GOMP_MAP_FORCE_FROM;
34588 break;
34589 case PRAGMA_OACC_CLAUSE_LINK:
34590 kind = GOMP_MAP_LINK;
34591 break;
34592 case PRAGMA_OACC_CLAUSE_NO_CREATE:
34593 kind = GOMP_MAP_IF_PRESENT;
34594 break;
34595 case PRAGMA_OACC_CLAUSE_PRESENT:
34596 kind = GOMP_MAP_FORCE_PRESENT;
34597 break;
34598 default:
34599 gcc_unreachable ();
34600 }
34601 tree nl, c;
34602 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
34603
34604 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
34605 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34606
34607 return nl;
34608 }
34609
34610 /* OpenACC 2.0:
34611 deviceptr ( variable-list ) */
34612
34613 static tree
34614 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
34615 {
34616 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34617 tree vars, t;
34618
34619 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
34620 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
34621 variable-list must only allow for pointer variables. */
34622 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34623 for (t = vars; t; t = TREE_CHAIN (t))
34624 {
34625 tree v = TREE_PURPOSE (t);
34626 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
34627 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
34628 OMP_CLAUSE_DECL (u) = v;
34629 OMP_CLAUSE_CHAIN (u) = list;
34630 list = u;
34631 }
34632
34633 return list;
34634 }
34635
34636 /* OpenACC 2.5:
34637 auto
34638 finalize
34639 independent
34640 nohost
34641 seq */
34642
34643 static tree
34644 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
34645 tree list)
34646 {
34647 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
34648
34649 tree c = build_omp_clause (loc, code);
34650 OMP_CLAUSE_CHAIN (c) = list;
34651
34652 return c;
34653 }
34654
34655 /* OpenACC:
34656 num_gangs ( expression )
34657 num_workers ( expression )
34658 vector_length ( expression ) */
34659
34660 static tree
34661 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
34662 const char *str, tree list)
34663 {
34664 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34665
34666 matching_parens parens;
34667 if (!parens.require_open (parser))
34668 return list;
34669
34670 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
34671
34672 if (t == error_mark_node
34673 || !parens.require_close (parser))
34674 {
34675 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34676 /*or_comma=*/false,
34677 /*consume_paren=*/true);
34678 return list;
34679 }
34680
34681 check_no_duplicate_clause (list, code, str, loc);
34682
34683 tree c = build_omp_clause (loc, code);
34684 OMP_CLAUSE_OPERAND (c, 0) = t;
34685 OMP_CLAUSE_CHAIN (c) = list;
34686 return c;
34687 }
34688
34689 /* OpenACC:
34690
34691 gang [( gang-arg-list )]
34692 worker [( [num:] int-expr )]
34693 vector [( [length:] int-expr )]
34694
34695 where gang-arg is one of:
34696
34697 [num:] int-expr
34698 static: size-expr
34699
34700 and size-expr may be:
34701
34702 *
34703 int-expr
34704 */
34705
34706 static tree
34707 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
34708 omp_clause_code kind,
34709 const char *str, tree list)
34710 {
34711 const char *id = "num";
34712 cp_lexer *lexer = parser->lexer;
34713 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
34714
34715 if (kind == OMP_CLAUSE_VECTOR)
34716 id = "length";
34717
34718 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
34719 {
34720 matching_parens parens;
34721 parens.consume_open (parser);
34722
34723 do
34724 {
34725 cp_token *next = cp_lexer_peek_token (lexer);
34726 int idx = 0;
34727
34728 /* Gang static argument. */
34729 if (kind == OMP_CLAUSE_GANG
34730 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
34731 {
34732 cp_lexer_consume_token (lexer);
34733
34734 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34735 goto cleanup_error;
34736
34737 idx = 1;
34738 if (ops[idx] != NULL)
34739 {
34740 cp_parser_error (parser, "too many %<static%> arguments");
34741 goto cleanup_error;
34742 }
34743
34744 /* Check for the '*' argument. */
34745 if (cp_lexer_next_token_is (lexer, CPP_MULT)
34746 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
34747 || cp_lexer_nth_token_is (parser->lexer, 2,
34748 CPP_CLOSE_PAREN)))
34749 {
34750 cp_lexer_consume_token (lexer);
34751 ops[idx] = integer_minus_one_node;
34752
34753 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
34754 {
34755 cp_lexer_consume_token (lexer);
34756 continue;
34757 }
34758 else break;
34759 }
34760 }
34761 /* Worker num: argument and vector length: arguments. */
34762 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
34763 && id_equal (next->u.value, id)
34764 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
34765 {
34766 cp_lexer_consume_token (lexer); /* id */
34767 cp_lexer_consume_token (lexer); /* ':' */
34768 }
34769
34770 /* Now collect the actual argument. */
34771 if (ops[idx] != NULL_TREE)
34772 {
34773 cp_parser_error (parser, "unexpected argument");
34774 goto cleanup_error;
34775 }
34776
34777 tree expr = cp_parser_assignment_expression (parser, NULL, false,
34778 false);
34779 if (expr == error_mark_node)
34780 goto cleanup_error;
34781
34782 mark_exp_read (expr);
34783 ops[idx] = expr;
34784
34785 if (kind == OMP_CLAUSE_GANG
34786 && cp_lexer_next_token_is (lexer, CPP_COMMA))
34787 {
34788 cp_lexer_consume_token (lexer);
34789 continue;
34790 }
34791 break;
34792 }
34793 while (1);
34794
34795 if (!parens.require_close (parser))
34796 goto cleanup_error;
34797 }
34798
34799 check_no_duplicate_clause (list, kind, str, loc);
34800
34801 c = build_omp_clause (loc, kind);
34802
34803 if (ops[1])
34804 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
34805
34806 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
34807 OMP_CLAUSE_CHAIN (c) = list;
34808
34809 return c;
34810
34811 cleanup_error:
34812 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
34813 return list;
34814 }
34815
34816 /* OpenACC 2.0:
34817 tile ( size-expr-list ) */
34818
34819 static tree
34820 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
34821 {
34822 tree c, expr = error_mark_node;
34823 tree tile = NULL_TREE;
34824
34825 /* Collapse and tile are mutually exclusive. (The spec doesn't say
34826 so, but the spec authors never considered such a case and have
34827 differing opinions on what it might mean, including 'not
34828 allowed'.) */
34829 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
34830 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
34831 clause_loc);
34832
34833 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34834 return list;
34835
34836 do
34837 {
34838 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
34839 return list;
34840
34841 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
34842 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
34843 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
34844 {
34845 cp_lexer_consume_token (parser->lexer);
34846 expr = integer_zero_node;
34847 }
34848 else
34849 expr = cp_parser_constant_expression (parser);
34850
34851 tile = tree_cons (NULL_TREE, expr, tile);
34852 }
34853 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
34854
34855 /* Consume the trailing ')'. */
34856 cp_lexer_consume_token (parser->lexer);
34857
34858 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
34859 tile = nreverse (tile);
34860 OMP_CLAUSE_TILE_LIST (c) = tile;
34861 OMP_CLAUSE_CHAIN (c) = list;
34862 return c;
34863 }
34864
34865 /* OpenACC 2.0
34866 Parse wait clause or directive parameters. */
34867
34868 static tree
34869 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
34870 {
34871 vec<tree, va_gc> *args;
34872 tree t, args_tree;
34873
34874 args = cp_parser_parenthesized_expression_list (parser, non_attr,
34875 /*cast_p=*/false,
34876 /*allow_expansion_p=*/true,
34877 /*non_constant_p=*/NULL);
34878
34879 if (args == NULL || args->length () == 0)
34880 {
34881 if (args != NULL)
34882 {
34883 cp_parser_error (parser, "expected integer expression list");
34884 release_tree_vector (args);
34885 }
34886 return list;
34887 }
34888
34889 args_tree = build_tree_list_vec (args);
34890
34891 release_tree_vector (args);
34892
34893 for (t = args_tree; t; t = TREE_CHAIN (t))
34894 {
34895 tree targ = TREE_VALUE (t);
34896
34897 if (targ != error_mark_node)
34898 {
34899 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
34900 error ("%<wait%> expression must be integral");
34901 else
34902 {
34903 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
34904
34905 targ = mark_rvalue_use (targ);
34906 OMP_CLAUSE_DECL (c) = targ;
34907 OMP_CLAUSE_CHAIN (c) = list;
34908 list = c;
34909 }
34910 }
34911 }
34912
34913 return list;
34914 }
34915
34916 /* OpenACC:
34917 wait [( int-expr-list )] */
34918
34919 static tree
34920 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
34921 {
34922 location_t location = cp_lexer_peek_token (parser->lexer)->location;
34923
34924 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34925 list = cp_parser_oacc_wait_list (parser, location, list);
34926 else
34927 {
34928 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
34929
34930 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34931 OMP_CLAUSE_CHAIN (c) = list;
34932 list = c;
34933 }
34934
34935 return list;
34936 }
34937
34938 /* OpenMP 3.0:
34939 collapse ( constant-expression ) */
34940
34941 static tree
34942 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
34943 {
34944 tree c, num;
34945 location_t loc;
34946 HOST_WIDE_INT n;
34947
34948 loc = cp_lexer_peek_token (parser->lexer)->location;
34949 matching_parens parens;
34950 if (!parens.require_open (parser))
34951 return list;
34952
34953 num = cp_parser_constant_expression (parser);
34954
34955 if (!parens.require_close (parser))
34956 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34957 /*or_comma=*/false,
34958 /*consume_paren=*/true);
34959
34960 if (num == error_mark_node)
34961 return list;
34962 num = fold_non_dependent_expr (num);
34963 if (!tree_fits_shwi_p (num)
34964 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
34965 || (n = tree_to_shwi (num)) <= 0
34966 || (int) n != n)
34967 {
34968 error_at (loc, "collapse argument needs positive constant integer expression");
34969 return list;
34970 }
34971
34972 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
34973 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
34974 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
34975 OMP_CLAUSE_CHAIN (c) = list;
34976 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
34977
34978 return c;
34979 }
34980
34981 /* OpenMP 2.5:
34982 default ( none | shared )
34983
34984 OpenACC:
34985 default ( none | present ) */
34986
34987 static tree
34988 cp_parser_omp_clause_default (cp_parser *parser, tree list,
34989 location_t location, bool is_oacc)
34990 {
34991 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
34992 tree c;
34993
34994 matching_parens parens;
34995 if (!parens.require_open (parser))
34996 return list;
34997 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34998 {
34999 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35000 const char *p = IDENTIFIER_POINTER (id);
35001
35002 switch (p[0])
35003 {
35004 case 'n':
35005 if (strcmp ("none", p) != 0)
35006 goto invalid_kind;
35007 kind = OMP_CLAUSE_DEFAULT_NONE;
35008 break;
35009
35010 case 'p':
35011 if (strcmp ("present", p) != 0 || !is_oacc)
35012 goto invalid_kind;
35013 kind = OMP_CLAUSE_DEFAULT_PRESENT;
35014 break;
35015
35016 case 's':
35017 if (strcmp ("shared", p) != 0 || is_oacc)
35018 goto invalid_kind;
35019 kind = OMP_CLAUSE_DEFAULT_SHARED;
35020 break;
35021
35022 default:
35023 goto invalid_kind;
35024 }
35025
35026 cp_lexer_consume_token (parser->lexer);
35027 }
35028 else
35029 {
35030 invalid_kind:
35031 if (is_oacc)
35032 cp_parser_error (parser, "expected %<none%> or %<present%>");
35033 else
35034 cp_parser_error (parser, "expected %<none%> or %<shared%>");
35035 }
35036
35037 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
35038 || !parens.require_close (parser))
35039 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35040 /*or_comma=*/false,
35041 /*consume_paren=*/true);
35042
35043 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
35044 return list;
35045
35046 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
35047 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
35048 OMP_CLAUSE_CHAIN (c) = list;
35049 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
35050
35051 return c;
35052 }
35053
35054 /* OpenMP 3.1:
35055 final ( expression ) */
35056
35057 static tree
35058 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
35059 {
35060 tree t, c;
35061
35062 matching_parens parens;
35063 if (!parens.require_open (parser))
35064 return list;
35065
35066 t = cp_parser_assignment_expression (parser);
35067
35068 if (t == error_mark_node
35069 || !parens.require_close (parser))
35070 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35071 /*or_comma=*/false,
35072 /*consume_paren=*/true);
35073
35074 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
35075
35076 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
35077 OMP_CLAUSE_FINAL_EXPR (c) = t;
35078 OMP_CLAUSE_CHAIN (c) = list;
35079
35080 return c;
35081 }
35082
35083 /* OpenMP 2.5:
35084 if ( expression )
35085
35086 OpenMP 4.5:
35087 if ( directive-name-modifier : expression )
35088
35089 directive-name-modifier:
35090 parallel | task | taskloop | target data | target | target update
35091 | target enter data | target exit data
35092
35093 OpenMP 5.0:
35094 directive-name-modifier:
35095 ... | simd | cancel */
35096
35097 static tree
35098 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
35099 bool is_omp)
35100 {
35101 tree t, c;
35102 enum tree_code if_modifier = ERROR_MARK;
35103
35104 matching_parens parens;
35105 if (!parens.require_open (parser))
35106 return list;
35107
35108 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35109 {
35110 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35111 const char *p = IDENTIFIER_POINTER (id);
35112 int n = 2;
35113
35114 if (strcmp ("cancel", p) == 0)
35115 if_modifier = VOID_CST;
35116 else if (strcmp ("parallel", p) == 0)
35117 if_modifier = OMP_PARALLEL;
35118 else if (strcmp ("simd", p) == 0)
35119 if_modifier = OMP_SIMD;
35120 else if (strcmp ("task", p) == 0)
35121 if_modifier = OMP_TASK;
35122 else if (strcmp ("taskloop", p) == 0)
35123 if_modifier = OMP_TASKLOOP;
35124 else if (strcmp ("target", p) == 0)
35125 {
35126 if_modifier = OMP_TARGET;
35127 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35128 {
35129 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
35130 p = IDENTIFIER_POINTER (id);
35131 if (strcmp ("data", p) == 0)
35132 if_modifier = OMP_TARGET_DATA;
35133 else if (strcmp ("update", p) == 0)
35134 if_modifier = OMP_TARGET_UPDATE;
35135 else if (strcmp ("enter", p) == 0)
35136 if_modifier = OMP_TARGET_ENTER_DATA;
35137 else if (strcmp ("exit", p) == 0)
35138 if_modifier = OMP_TARGET_EXIT_DATA;
35139 if (if_modifier != OMP_TARGET)
35140 n = 3;
35141 else
35142 {
35143 location_t loc
35144 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
35145 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
35146 "or %<exit%>");
35147 if_modifier = ERROR_MARK;
35148 }
35149 if (if_modifier == OMP_TARGET_ENTER_DATA
35150 || if_modifier == OMP_TARGET_EXIT_DATA)
35151 {
35152 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
35153 {
35154 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
35155 p = IDENTIFIER_POINTER (id);
35156 if (strcmp ("data", p) == 0)
35157 n = 4;
35158 }
35159 if (n != 4)
35160 {
35161 location_t loc
35162 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
35163 error_at (loc, "expected %<data%>");
35164 if_modifier = ERROR_MARK;
35165 }
35166 }
35167 }
35168 }
35169 if (if_modifier != ERROR_MARK)
35170 {
35171 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
35172 {
35173 while (n-- > 0)
35174 cp_lexer_consume_token (parser->lexer);
35175 }
35176 else
35177 {
35178 if (n > 2)
35179 {
35180 location_t loc
35181 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
35182 error_at (loc, "expected %<:%>");
35183 }
35184 if_modifier = ERROR_MARK;
35185 }
35186 }
35187 }
35188
35189 t = cp_parser_assignment_expression (parser);
35190
35191 if (t == error_mark_node
35192 || !parens.require_close (parser))
35193 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35194 /*or_comma=*/false,
35195 /*consume_paren=*/true);
35196
35197 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
35198 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
35199 {
35200 if (if_modifier != ERROR_MARK
35201 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
35202 {
35203 const char *p = NULL;
35204 switch (if_modifier)
35205 {
35206 case VOID_CST: p = "cancel"; break;
35207 case OMP_PARALLEL: p = "parallel"; break;
35208 case OMP_SIMD: p = "simd"; break;
35209 case OMP_TASK: p = "task"; break;
35210 case OMP_TASKLOOP: p = "taskloop"; break;
35211 case OMP_TARGET_DATA: p = "target data"; break;
35212 case OMP_TARGET: p = "target"; break;
35213 case OMP_TARGET_UPDATE: p = "target update"; break;
35214 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
35215 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
35216 default: gcc_unreachable ();
35217 }
35218 error_at (location, "too many %<if%> clauses with %qs modifier",
35219 p);
35220 return list;
35221 }
35222 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
35223 {
35224 if (!is_omp)
35225 error_at (location, "too many %<if%> clauses");
35226 else
35227 error_at (location, "too many %<if%> clauses without modifier");
35228 return list;
35229 }
35230 else if (if_modifier == ERROR_MARK
35231 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
35232 {
35233 error_at (location, "if any %<if%> clause has modifier, then all "
35234 "%<if%> clauses have to use modifier");
35235 return list;
35236 }
35237 }
35238
35239 c = build_omp_clause (location, OMP_CLAUSE_IF);
35240 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
35241 OMP_CLAUSE_IF_EXPR (c) = t;
35242 OMP_CLAUSE_CHAIN (c) = list;
35243
35244 return c;
35245 }
35246
35247 /* OpenMP 3.1:
35248 mergeable */
35249
35250 static tree
35251 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
35252 tree list, location_t location)
35253 {
35254 tree c;
35255
35256 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
35257 location);
35258
35259 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
35260 OMP_CLAUSE_CHAIN (c) = list;
35261 return c;
35262 }
35263
35264 /* OpenMP 2.5:
35265 nowait */
35266
35267 static tree
35268 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
35269 tree list, location_t location)
35270 {
35271 tree c;
35272
35273 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
35274
35275 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
35276 OMP_CLAUSE_CHAIN (c) = list;
35277 return c;
35278 }
35279
35280 /* OpenMP 2.5:
35281 num_threads ( expression ) */
35282
35283 static tree
35284 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
35285 location_t location)
35286 {
35287 tree t, c;
35288
35289 matching_parens parens;
35290 if (!parens.require_open (parser))
35291 return list;
35292
35293 t = cp_parser_assignment_expression (parser);
35294
35295 if (t == error_mark_node
35296 || !parens.require_close (parser))
35297 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35298 /*or_comma=*/false,
35299 /*consume_paren=*/true);
35300
35301 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
35302 "num_threads", location);
35303
35304 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
35305 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
35306 OMP_CLAUSE_CHAIN (c) = list;
35307
35308 return c;
35309 }
35310
35311 /* OpenMP 4.5:
35312 num_tasks ( expression ) */
35313
35314 static tree
35315 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
35316 location_t location)
35317 {
35318 tree t, c;
35319
35320 matching_parens parens;
35321 if (!parens.require_open (parser))
35322 return list;
35323
35324 t = cp_parser_assignment_expression (parser);
35325
35326 if (t == error_mark_node
35327 || !parens.require_close (parser))
35328 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35329 /*or_comma=*/false,
35330 /*consume_paren=*/true);
35331
35332 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
35333 "num_tasks", location);
35334
35335 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
35336 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
35337 OMP_CLAUSE_CHAIN (c) = list;
35338
35339 return c;
35340 }
35341
35342 /* OpenMP 4.5:
35343 grainsize ( expression ) */
35344
35345 static tree
35346 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
35347 location_t location)
35348 {
35349 tree t, c;
35350
35351 matching_parens parens;
35352 if (!parens.require_open (parser))
35353 return list;
35354
35355 t = cp_parser_assignment_expression (parser);
35356
35357 if (t == error_mark_node
35358 || !parens.require_close (parser))
35359 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35360 /*or_comma=*/false,
35361 /*consume_paren=*/true);
35362
35363 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
35364 "grainsize", location);
35365
35366 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
35367 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
35368 OMP_CLAUSE_CHAIN (c) = list;
35369
35370 return c;
35371 }
35372
35373 /* OpenMP 4.5:
35374 priority ( expression ) */
35375
35376 static tree
35377 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
35378 location_t location)
35379 {
35380 tree t, c;
35381
35382 matching_parens parens;
35383 if (!parens.require_open (parser))
35384 return list;
35385
35386 t = cp_parser_assignment_expression (parser);
35387
35388 if (t == error_mark_node
35389 || !parens.require_close (parser))
35390 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35391 /*or_comma=*/false,
35392 /*consume_paren=*/true);
35393
35394 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
35395 "priority", location);
35396
35397 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
35398 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
35399 OMP_CLAUSE_CHAIN (c) = list;
35400
35401 return c;
35402 }
35403
35404 /* OpenMP 4.5:
35405 hint ( expression ) */
35406
35407 static tree
35408 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
35409 {
35410 tree t, c;
35411
35412 matching_parens parens;
35413 if (!parens.require_open (parser))
35414 return list;
35415
35416 t = cp_parser_assignment_expression (parser);
35417
35418 if (t != error_mark_node)
35419 {
35420 t = fold_non_dependent_expr (t);
35421 if (!value_dependent_expression_p (t)
35422 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
35423 || !tree_fits_shwi_p (t)
35424 || tree_int_cst_sgn (t) == -1))
35425 error_at (location, "expected constant integer expression with "
35426 "valid sync-hint value");
35427 }
35428 if (t == error_mark_node
35429 || !parens.require_close (parser))
35430 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35431 /*or_comma=*/false,
35432 /*consume_paren=*/true);
35433 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
35434
35435 c = build_omp_clause (location, OMP_CLAUSE_HINT);
35436 OMP_CLAUSE_HINT_EXPR (c) = t;
35437 OMP_CLAUSE_CHAIN (c) = list;
35438
35439 return c;
35440 }
35441
35442 /* OpenMP 4.5:
35443 defaultmap ( tofrom : scalar )
35444
35445 OpenMP 5.0:
35446 defaultmap ( implicit-behavior [ : variable-category ] ) */
35447
35448 static tree
35449 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
35450 location_t location)
35451 {
35452 tree c, id;
35453 const char *p;
35454 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
35455 enum omp_clause_defaultmap_kind category
35456 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
35457
35458 matching_parens parens;
35459 if (!parens.require_open (parser))
35460 return list;
35461
35462 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
35463 p = "default";
35464 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35465 {
35466 invalid_behavior:
35467 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
35468 "%<tofrom%>, %<firstprivate%>, %<none%> "
35469 "or %<default%>");
35470 goto out_err;
35471 }
35472 else
35473 {
35474 id = cp_lexer_peek_token (parser->lexer)->u.value;
35475 p = IDENTIFIER_POINTER (id);
35476 }
35477
35478 switch (p[0])
35479 {
35480 case 'a':
35481 if (strcmp ("alloc", p) == 0)
35482 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
35483 else
35484 goto invalid_behavior;
35485 break;
35486
35487 case 'd':
35488 if (strcmp ("default", p) == 0)
35489 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
35490 else
35491 goto invalid_behavior;
35492 break;
35493
35494 case 'f':
35495 if (strcmp ("firstprivate", p) == 0)
35496 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
35497 else if (strcmp ("from", p) == 0)
35498 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
35499 else
35500 goto invalid_behavior;
35501 break;
35502
35503 case 'n':
35504 if (strcmp ("none", p) == 0)
35505 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
35506 else
35507 goto invalid_behavior;
35508 break;
35509
35510 case 't':
35511 if (strcmp ("tofrom", p) == 0)
35512 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
35513 else if (strcmp ("to", p) == 0)
35514 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
35515 else
35516 goto invalid_behavior;
35517 break;
35518
35519 default:
35520 goto invalid_behavior;
35521 }
35522 cp_lexer_consume_token (parser->lexer);
35523
35524 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35525 {
35526 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35527 goto out_err;
35528
35529 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35530 {
35531 invalid_category:
35532 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
35533 "%<pointer%>");
35534 goto out_err;
35535 }
35536 id = cp_lexer_peek_token (parser->lexer)->u.value;
35537 p = IDENTIFIER_POINTER (id);
35538
35539 switch (p[0])
35540 {
35541 case 'a':
35542 if (strcmp ("aggregate", p) == 0)
35543 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
35544 else
35545 goto invalid_category;
35546 break;
35547
35548 case 'p':
35549 if (strcmp ("pointer", p) == 0)
35550 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
35551 else
35552 goto invalid_category;
35553 break;
35554
35555 case 's':
35556 if (strcmp ("scalar", p) == 0)
35557 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
35558 else
35559 goto invalid_category;
35560 break;
35561
35562 default:
35563 goto invalid_category;
35564 }
35565
35566 cp_lexer_consume_token (parser->lexer);
35567 }
35568 if (!parens.require_close (parser))
35569 goto out_err;
35570
35571 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
35572 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
35573 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
35574 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
35575 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
35576 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
35577 {
35578 enum omp_clause_defaultmap_kind cat = category;
35579 location_t loc = OMP_CLAUSE_LOCATION (c);
35580 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
35581 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
35582 p = NULL;
35583 switch (cat)
35584 {
35585 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
35586 p = NULL;
35587 break;
35588 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
35589 p = "aggregate";
35590 break;
35591 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
35592 p = "pointer";
35593 break;
35594 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
35595 p = "scalar";
35596 break;
35597 default:
35598 gcc_unreachable ();
35599 }
35600 if (p)
35601 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
35602 p);
35603 else
35604 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
35605 "category");
35606 break;
35607 }
35608
35609 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
35610 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
35611 OMP_CLAUSE_CHAIN (c) = list;
35612 return c;
35613
35614 out_err:
35615 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35616 /*or_comma=*/false,
35617 /*consume_paren=*/true);
35618 return list;
35619 }
35620
35621 /* OpenMP 5.0:
35622 order ( concurrent ) */
35623
35624 static tree
35625 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
35626 {
35627 tree c, id;
35628 const char *p;
35629
35630 matching_parens parens;
35631 if (!parens.require_open (parser))
35632 return list;
35633
35634 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35635 {
35636 cp_parser_error (parser, "expected %<concurrent%>");
35637 goto out_err;
35638 }
35639 else
35640 {
35641 id = cp_lexer_peek_token (parser->lexer)->u.value;
35642 p = IDENTIFIER_POINTER (id);
35643 }
35644 if (strcmp (p, "concurrent") != 0)
35645 {
35646 cp_parser_error (parser, "expected %<concurrent%>");
35647 goto out_err;
35648 }
35649 cp_lexer_consume_token (parser->lexer);
35650 if (!parens.require_close (parser))
35651 goto out_err;
35652
35653 /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location); */
35654 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
35655 OMP_CLAUSE_CHAIN (c) = list;
35656 return c;
35657
35658 out_err:
35659 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35660 /*or_comma=*/false,
35661 /*consume_paren=*/true);
35662 return list;
35663 }
35664
35665 /* OpenMP 5.0:
35666 bind ( teams | parallel | thread ) */
35667
35668 static tree
35669 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
35670 location_t location)
35671 {
35672 tree c;
35673 const char *p;
35674 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
35675
35676 matching_parens parens;
35677 if (!parens.require_open (parser))
35678 return list;
35679
35680 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35681 {
35682 invalid:
35683 cp_parser_error (parser,
35684 "expected %<teams%>, %<parallel%> or %<thread%>");
35685 goto out_err;
35686 }
35687 else
35688 {
35689 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35690 p = IDENTIFIER_POINTER (id);
35691 }
35692 if (strcmp (p, "teams") == 0)
35693 kind = OMP_CLAUSE_BIND_TEAMS;
35694 else if (strcmp (p, "parallel") == 0)
35695 kind = OMP_CLAUSE_BIND_PARALLEL;
35696 else if (strcmp (p, "thread") != 0)
35697 goto invalid;
35698 cp_lexer_consume_token (parser->lexer);
35699 if (!parens.require_close (parser))
35700 goto out_err;
35701
35702 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
35703 c = build_omp_clause (location, OMP_CLAUSE_BIND);
35704 OMP_CLAUSE_BIND_KIND (c) = kind;
35705 OMP_CLAUSE_CHAIN (c) = list;
35706 return c;
35707
35708 out_err:
35709 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35710 /*or_comma=*/false,
35711 /*consume_paren=*/true);
35712 return list;
35713 }
35714
35715 /* OpenMP 2.5:
35716 ordered
35717
35718 OpenMP 4.5:
35719 ordered ( constant-expression ) */
35720
35721 static tree
35722 cp_parser_omp_clause_ordered (cp_parser *parser,
35723 tree list, location_t location)
35724 {
35725 tree c, num = NULL_TREE;
35726 HOST_WIDE_INT n;
35727
35728 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
35729 "ordered", location);
35730
35731 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35732 {
35733 matching_parens parens;
35734 parens.consume_open (parser);
35735
35736 num = cp_parser_constant_expression (parser);
35737
35738 if (!parens.require_close (parser))
35739 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35740 /*or_comma=*/false,
35741 /*consume_paren=*/true);
35742
35743 if (num == error_mark_node)
35744 return list;
35745 num = fold_non_dependent_expr (num);
35746 if (!tree_fits_shwi_p (num)
35747 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
35748 || (n = tree_to_shwi (num)) <= 0
35749 || (int) n != n)
35750 {
35751 error_at (location,
35752 "ordered argument needs positive constant integer "
35753 "expression");
35754 return list;
35755 }
35756 }
35757
35758 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
35759 OMP_CLAUSE_ORDERED_EXPR (c) = num;
35760 OMP_CLAUSE_CHAIN (c) = list;
35761 return c;
35762 }
35763
35764 /* OpenMP 2.5:
35765 reduction ( reduction-operator : variable-list )
35766
35767 reduction-operator:
35768 One of: + * - & ^ | && ||
35769
35770 OpenMP 3.1:
35771
35772 reduction-operator:
35773 One of: + * - & ^ | && || min max
35774
35775 OpenMP 4.0:
35776
35777 reduction-operator:
35778 One of: + * - & ^ | && ||
35779 id-expression
35780
35781 OpenMP 5.0:
35782 reduction ( reduction-modifier, reduction-operator : variable-list )
35783 in_reduction ( reduction-operator : variable-list )
35784 task_reduction ( reduction-operator : variable-list ) */
35785
35786 static tree
35787 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
35788 bool is_omp, tree list)
35789 {
35790 enum tree_code code = ERROR_MARK;
35791 tree nlist, c, id = NULL_TREE;
35792 bool task = false;
35793 bool inscan = false;
35794
35795 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35796 return list;
35797
35798 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
35799 {
35800 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
35801 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
35802 {
35803 cp_lexer_consume_token (parser->lexer);
35804 cp_lexer_consume_token (parser->lexer);
35805 }
35806 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
35807 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
35808 {
35809 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35810 const char *p = IDENTIFIER_POINTER (id);
35811 if (strcmp (p, "task") == 0)
35812 task = true;
35813 else if (strcmp (p, "inscan") == 0)
35814 inscan = true;
35815 if (task || inscan)
35816 {
35817 cp_lexer_consume_token (parser->lexer);
35818 cp_lexer_consume_token (parser->lexer);
35819 }
35820 }
35821 }
35822
35823 switch (cp_lexer_peek_token (parser->lexer)->type)
35824 {
35825 case CPP_PLUS: code = PLUS_EXPR; break;
35826 case CPP_MULT: code = MULT_EXPR; break;
35827 case CPP_MINUS: code = MINUS_EXPR; break;
35828 case CPP_AND: code = BIT_AND_EXPR; break;
35829 case CPP_XOR: code = BIT_XOR_EXPR; break;
35830 case CPP_OR: code = BIT_IOR_EXPR; break;
35831 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
35832 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
35833 default: break;
35834 }
35835
35836 if (code != ERROR_MARK)
35837 cp_lexer_consume_token (parser->lexer);
35838 else
35839 {
35840 bool saved_colon_corrects_to_scope_p;
35841 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
35842 parser->colon_corrects_to_scope_p = false;
35843 id = cp_parser_id_expression (parser, /*template_p=*/false,
35844 /*check_dependency_p=*/true,
35845 /*template_p=*/NULL,
35846 /*declarator_p=*/false,
35847 /*optional_p=*/false);
35848 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
35849 if (identifier_p (id))
35850 {
35851 const char *p = IDENTIFIER_POINTER (id);
35852
35853 if (strcmp (p, "min") == 0)
35854 code = MIN_EXPR;
35855 else if (strcmp (p, "max") == 0)
35856 code = MAX_EXPR;
35857 else if (id == ovl_op_identifier (false, PLUS_EXPR))
35858 code = PLUS_EXPR;
35859 else if (id == ovl_op_identifier (false, MULT_EXPR))
35860 code = MULT_EXPR;
35861 else if (id == ovl_op_identifier (false, MINUS_EXPR))
35862 code = MINUS_EXPR;
35863 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
35864 code = BIT_AND_EXPR;
35865 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
35866 code = BIT_IOR_EXPR;
35867 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
35868 code = BIT_XOR_EXPR;
35869 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
35870 code = TRUTH_ANDIF_EXPR;
35871 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
35872 code = TRUTH_ORIF_EXPR;
35873 id = omp_reduction_id (code, id, NULL_TREE);
35874 tree scope = parser->scope;
35875 if (scope)
35876 id = build_qualified_name (NULL_TREE, scope, id, false);
35877 parser->scope = NULL_TREE;
35878 parser->qualifying_scope = NULL_TREE;
35879 parser->object_scope = NULL_TREE;
35880 }
35881 else
35882 {
35883 error ("invalid reduction-identifier");
35884 resync_fail:
35885 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35886 /*or_comma=*/false,
35887 /*consume_paren=*/true);
35888 return list;
35889 }
35890 }
35891
35892 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35893 goto resync_fail;
35894
35895 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
35896 NULL);
35897 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
35898 {
35899 OMP_CLAUSE_REDUCTION_CODE (c) = code;
35900 if (task)
35901 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
35902 else if (inscan)
35903 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
35904 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
35905 }
35906
35907 return nlist;
35908 }
35909
35910 /* OpenMP 2.5:
35911 schedule ( schedule-kind )
35912 schedule ( schedule-kind , expression )
35913
35914 schedule-kind:
35915 static | dynamic | guided | runtime | auto
35916
35917 OpenMP 4.5:
35918 schedule ( schedule-modifier : schedule-kind )
35919 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
35920
35921 schedule-modifier:
35922 simd
35923 monotonic
35924 nonmonotonic */
35925
35926 static tree
35927 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
35928 {
35929 tree c, t;
35930 int modifiers = 0, nmodifiers = 0;
35931
35932 matching_parens parens;
35933 if (!parens.require_open (parser))
35934 return list;
35935
35936 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
35937
35938 location_t comma = UNKNOWN_LOCATION;
35939 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35940 {
35941 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35942 const char *p = IDENTIFIER_POINTER (id);
35943 if (strcmp ("simd", p) == 0)
35944 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
35945 else if (strcmp ("monotonic", p) == 0)
35946 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
35947 else if (strcmp ("nonmonotonic", p) == 0)
35948 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
35949 else
35950 break;
35951 comma = UNKNOWN_LOCATION;
35952 cp_lexer_consume_token (parser->lexer);
35953 if (nmodifiers++ == 0
35954 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35955 {
35956 comma = cp_lexer_peek_token (parser->lexer)->location;
35957 cp_lexer_consume_token (parser->lexer);
35958 }
35959 else
35960 {
35961 cp_parser_require (parser, CPP_COLON, RT_COLON);
35962 break;
35963 }
35964 }
35965 if (comma != UNKNOWN_LOCATION)
35966 error_at (comma, "expected %<:%>");
35967
35968 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35969 {
35970 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35971 const char *p = IDENTIFIER_POINTER (id);
35972
35973 switch (p[0])
35974 {
35975 case 'd':
35976 if (strcmp ("dynamic", p) != 0)
35977 goto invalid_kind;
35978 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
35979 break;
35980
35981 case 'g':
35982 if (strcmp ("guided", p) != 0)
35983 goto invalid_kind;
35984 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
35985 break;
35986
35987 case 'r':
35988 if (strcmp ("runtime", p) != 0)
35989 goto invalid_kind;
35990 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
35991 break;
35992
35993 default:
35994 goto invalid_kind;
35995 }
35996 }
35997 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
35998 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
35999 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
36000 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
36001 else
36002 goto invalid_kind;
36003 cp_lexer_consume_token (parser->lexer);
36004
36005 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
36006 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
36007 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
36008 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
36009 {
36010 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
36011 "specified");
36012 modifiers = 0;
36013 }
36014
36015 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36016 {
36017 cp_token *token;
36018 cp_lexer_consume_token (parser->lexer);
36019
36020 token = cp_lexer_peek_token (parser->lexer);
36021 t = cp_parser_assignment_expression (parser);
36022
36023 if (t == error_mark_node)
36024 goto resync_fail;
36025 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
36026 error_at (token->location, "schedule %<runtime%> does not take "
36027 "a %<chunk_size%> parameter");
36028 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
36029 error_at (token->location, "schedule %<auto%> does not take "
36030 "a %<chunk_size%> parameter");
36031 else
36032 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
36033
36034 if (!parens.require_close (parser))
36035 goto resync_fail;
36036 }
36037 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
36038 goto resync_fail;
36039
36040 OMP_CLAUSE_SCHEDULE_KIND (c)
36041 = (enum omp_clause_schedule_kind)
36042 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
36043
36044 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
36045 OMP_CLAUSE_CHAIN (c) = list;
36046 return c;
36047
36048 invalid_kind:
36049 cp_parser_error (parser, "invalid schedule kind");
36050 resync_fail:
36051 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36052 /*or_comma=*/false,
36053 /*consume_paren=*/true);
36054 return list;
36055 }
36056
36057 /* OpenMP 3.0:
36058 untied */
36059
36060 static tree
36061 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
36062 tree list, location_t location)
36063 {
36064 tree c;
36065
36066 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
36067
36068 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
36069 OMP_CLAUSE_CHAIN (c) = list;
36070 return c;
36071 }
36072
36073 /* OpenMP 4.0:
36074 inbranch
36075 notinbranch */
36076
36077 static tree
36078 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
36079 tree list, location_t location)
36080 {
36081 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
36082 tree c = build_omp_clause (location, code);
36083 OMP_CLAUSE_CHAIN (c) = list;
36084 return c;
36085 }
36086
36087 /* OpenMP 4.0:
36088 parallel
36089 for
36090 sections
36091 taskgroup */
36092
36093 static tree
36094 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
36095 enum omp_clause_code code,
36096 tree list, location_t location)
36097 {
36098 tree c = build_omp_clause (location, code);
36099 OMP_CLAUSE_CHAIN (c) = list;
36100 return c;
36101 }
36102
36103 /* OpenMP 4.5:
36104 nogroup */
36105
36106 static tree
36107 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
36108 tree list, location_t location)
36109 {
36110 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
36111 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
36112 OMP_CLAUSE_CHAIN (c) = list;
36113 return c;
36114 }
36115
36116 /* OpenMP 4.5:
36117 simd
36118 threads */
36119
36120 static tree
36121 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
36122 enum omp_clause_code code,
36123 tree list, location_t location)
36124 {
36125 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
36126 tree c = build_omp_clause (location, code);
36127 OMP_CLAUSE_CHAIN (c) = list;
36128 return c;
36129 }
36130
36131 /* OpenMP 4.0:
36132 num_teams ( expression ) */
36133
36134 static tree
36135 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
36136 location_t location)
36137 {
36138 tree t, c;
36139
36140 matching_parens parens;
36141 if (!parens.require_open (parser))
36142 return list;
36143
36144 t = cp_parser_assignment_expression (parser);
36145
36146 if (t == error_mark_node
36147 || !parens.require_close (parser))
36148 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36149 /*or_comma=*/false,
36150 /*consume_paren=*/true);
36151
36152 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
36153 "num_teams", location);
36154
36155 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
36156 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
36157 OMP_CLAUSE_CHAIN (c) = list;
36158
36159 return c;
36160 }
36161
36162 /* OpenMP 4.0:
36163 thread_limit ( expression ) */
36164
36165 static tree
36166 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
36167 location_t location)
36168 {
36169 tree t, c;
36170
36171 matching_parens parens;
36172 if (!parens.require_open (parser))
36173 return list;
36174
36175 t = cp_parser_assignment_expression (parser);
36176
36177 if (t == error_mark_node
36178 || !parens.require_close (parser))
36179 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36180 /*or_comma=*/false,
36181 /*consume_paren=*/true);
36182
36183 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
36184 "thread_limit", location);
36185
36186 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
36187 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
36188 OMP_CLAUSE_CHAIN (c) = list;
36189
36190 return c;
36191 }
36192
36193 /* OpenMP 4.0:
36194 aligned ( variable-list )
36195 aligned ( variable-list : constant-expression ) */
36196
36197 static tree
36198 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
36199 {
36200 tree nlist, c, alignment = NULL_TREE;
36201 bool colon;
36202
36203 matching_parens parens;
36204 if (!parens.require_open (parser))
36205 return list;
36206
36207 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
36208 &colon);
36209
36210 if (colon)
36211 {
36212 alignment = cp_parser_constant_expression (parser);
36213
36214 if (!parens.require_close (parser))
36215 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36216 /*or_comma=*/false,
36217 /*consume_paren=*/true);
36218
36219 if (alignment == error_mark_node)
36220 alignment = NULL_TREE;
36221 }
36222
36223 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36224 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
36225
36226 return nlist;
36227 }
36228
36229 /* OpenMP 2.5:
36230 lastprivate ( variable-list )
36231
36232 OpenMP 5.0:
36233 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
36234
36235 static tree
36236 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
36237 {
36238 bool conditional = false;
36239
36240 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36241 return list;
36242
36243 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36244 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
36245 {
36246 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36247 const char *p = IDENTIFIER_POINTER (id);
36248
36249 if (strcmp ("conditional", p) == 0)
36250 {
36251 conditional = true;
36252 cp_lexer_consume_token (parser->lexer);
36253 cp_lexer_consume_token (parser->lexer);
36254 }
36255 }
36256
36257 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
36258 list, NULL);
36259
36260 if (conditional)
36261 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36262 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
36263 return nlist;
36264 }
36265
36266 /* OpenMP 4.0:
36267 linear ( variable-list )
36268 linear ( variable-list : expression )
36269
36270 OpenMP 4.5:
36271 linear ( modifier ( variable-list ) )
36272 linear ( modifier ( variable-list ) : expression ) */
36273
36274 static tree
36275 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
36276 bool declare_simd)
36277 {
36278 tree nlist, c, step = integer_one_node;
36279 bool colon;
36280 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
36281
36282 matching_parens parens;
36283 if (!parens.require_open (parser))
36284 return list;
36285
36286 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36287 {
36288 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36289 const char *p = IDENTIFIER_POINTER (id);
36290
36291 if (strcmp ("ref", p) == 0)
36292 kind = OMP_CLAUSE_LINEAR_REF;
36293 else if (strcmp ("val", p) == 0)
36294 kind = OMP_CLAUSE_LINEAR_VAL;
36295 else if (strcmp ("uval", p) == 0)
36296 kind = OMP_CLAUSE_LINEAR_UVAL;
36297 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
36298 cp_lexer_consume_token (parser->lexer);
36299 else
36300 kind = OMP_CLAUSE_LINEAR_DEFAULT;
36301 }
36302
36303 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
36304 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
36305 &colon);
36306 else
36307 {
36308 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
36309 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
36310 if (colon)
36311 cp_parser_require (parser, CPP_COLON, RT_COLON);
36312 else if (!parens.require_close (parser))
36313 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36314 /*or_comma=*/false,
36315 /*consume_paren=*/true);
36316 }
36317
36318 if (colon)
36319 {
36320 step = NULL_TREE;
36321 if (declare_simd
36322 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36323 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
36324 {
36325 cp_token *token = cp_lexer_peek_token (parser->lexer);
36326 cp_parser_parse_tentatively (parser);
36327 step = cp_parser_id_expression (parser, /*template_p=*/false,
36328 /*check_dependency_p=*/true,
36329 /*template_p=*/NULL,
36330 /*declarator_p=*/false,
36331 /*optional_p=*/false);
36332 if (step != error_mark_node)
36333 step = cp_parser_lookup_name_simple (parser, step, token->location);
36334 if (step == error_mark_node)
36335 {
36336 step = NULL_TREE;
36337 cp_parser_abort_tentative_parse (parser);
36338 }
36339 else if (!cp_parser_parse_definitely (parser))
36340 step = NULL_TREE;
36341 }
36342 if (!step)
36343 step = cp_parser_assignment_expression (parser);
36344
36345 if (!parens.require_close (parser))
36346 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36347 /*or_comma=*/false,
36348 /*consume_paren=*/true);
36349
36350 if (step == error_mark_node)
36351 return list;
36352 }
36353
36354 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36355 {
36356 OMP_CLAUSE_LINEAR_STEP (c) = step;
36357 OMP_CLAUSE_LINEAR_KIND (c) = kind;
36358 }
36359
36360 return nlist;
36361 }
36362
36363 /* OpenMP 4.0:
36364 safelen ( constant-expression ) */
36365
36366 static tree
36367 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
36368 location_t location)
36369 {
36370 tree t, c;
36371
36372 matching_parens parens;
36373 if (!parens.require_open (parser))
36374 return list;
36375
36376 t = cp_parser_constant_expression (parser);
36377
36378 if (t == error_mark_node
36379 || !parens.require_close (parser))
36380 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36381 /*or_comma=*/false,
36382 /*consume_paren=*/true);
36383
36384 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
36385
36386 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
36387 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
36388 OMP_CLAUSE_CHAIN (c) = list;
36389
36390 return c;
36391 }
36392
36393 /* OpenMP 4.0:
36394 simdlen ( constant-expression ) */
36395
36396 static tree
36397 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
36398 location_t location)
36399 {
36400 tree t, c;
36401
36402 matching_parens parens;
36403 if (!parens.require_open (parser))
36404 return list;
36405
36406 t = cp_parser_constant_expression (parser);
36407
36408 if (t == error_mark_node
36409 || !parens.require_close (parser))
36410 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36411 /*or_comma=*/false,
36412 /*consume_paren=*/true);
36413
36414 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
36415
36416 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
36417 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
36418 OMP_CLAUSE_CHAIN (c) = list;
36419
36420 return c;
36421 }
36422
36423 /* OpenMP 4.5:
36424 vec:
36425 identifier [+/- integer]
36426 vec , identifier [+/- integer]
36427 */
36428
36429 static tree
36430 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
36431 tree list)
36432 {
36433 tree vec = NULL;
36434
36435 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36436 {
36437 cp_parser_error (parser, "expected identifier");
36438 return list;
36439 }
36440
36441 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36442 {
36443 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
36444 tree t, identifier = cp_parser_identifier (parser);
36445 tree addend = NULL;
36446
36447 if (identifier == error_mark_node)
36448 t = error_mark_node;
36449 else
36450 {
36451 t = cp_parser_lookup_name_simple
36452 (parser, identifier,
36453 cp_lexer_peek_token (parser->lexer)->location);
36454 if (t == error_mark_node)
36455 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
36456 id_loc);
36457 }
36458
36459 bool neg = false;
36460 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
36461 neg = true;
36462 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
36463 {
36464 addend = integer_zero_node;
36465 goto add_to_vector;
36466 }
36467 cp_lexer_consume_token (parser->lexer);
36468
36469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
36470 {
36471 cp_parser_error (parser, "expected integer");
36472 return list;
36473 }
36474
36475 addend = cp_lexer_peek_token (parser->lexer)->u.value;
36476 if (TREE_CODE (addend) != INTEGER_CST)
36477 {
36478 cp_parser_error (parser, "expected integer");
36479 return list;
36480 }
36481 cp_lexer_consume_token (parser->lexer);
36482
36483 add_to_vector:
36484 if (t != error_mark_node)
36485 {
36486 vec = tree_cons (addend, t, vec);
36487 if (neg)
36488 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
36489 }
36490
36491 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
36492 break;
36493
36494 cp_lexer_consume_token (parser->lexer);
36495 }
36496
36497 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
36498 {
36499 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
36500 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
36501 OMP_CLAUSE_DECL (u) = nreverse (vec);
36502 OMP_CLAUSE_CHAIN (u) = list;
36503 return u;
36504 }
36505 return list;
36506 }
36507
36508 /* OpenMP 5.0:
36509 iterators ( iterators-definition )
36510
36511 iterators-definition:
36512 iterator-specifier
36513 iterator-specifier , iterators-definition
36514
36515 iterator-specifier:
36516 identifier = range-specification
36517 iterator-type identifier = range-specification
36518
36519 range-specification:
36520 begin : end
36521 begin : end : step */
36522
36523 static tree
36524 cp_parser_omp_iterators (cp_parser *parser)
36525 {
36526 tree ret = NULL_TREE, *last = &ret;
36527 cp_lexer_consume_token (parser->lexer);
36528
36529 matching_parens parens;
36530 if (!parens.require_open (parser))
36531 return error_mark_node;
36532
36533 bool saved_colon_corrects_to_scope_p
36534 = parser->colon_corrects_to_scope_p;
36535 bool saved_colon_doesnt_start_class_def_p
36536 = parser->colon_doesnt_start_class_def_p;
36537
36538 do
36539 {
36540 tree iter_type;
36541 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36542 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
36543 iter_type = integer_type_node;
36544 else
36545 {
36546 const char *saved_message
36547 = parser->type_definition_forbidden_message;
36548 parser->type_definition_forbidden_message
36549 = G_("types may not be defined in iterator type");
36550
36551 iter_type = cp_parser_type_id (parser);
36552
36553 parser->type_definition_forbidden_message = saved_message;
36554 }
36555
36556 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36557 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36558 {
36559 cp_parser_error (parser, "expected identifier");
36560 break;
36561 }
36562
36563 tree id = cp_parser_identifier (parser);
36564 if (id == error_mark_node)
36565 break;
36566
36567 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36568 break;
36569
36570 parser->colon_corrects_to_scope_p = false;
36571 parser->colon_doesnt_start_class_def_p = true;
36572 tree begin = cp_parser_assignment_expression (parser);
36573
36574 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36575 break;
36576
36577 tree end = cp_parser_assignment_expression (parser);
36578
36579 tree step = integer_one_node;
36580 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36581 {
36582 cp_lexer_consume_token (parser->lexer);
36583 step = cp_parser_assignment_expression (parser);
36584 }
36585
36586 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
36587 DECL_ARTIFICIAL (iter_var) = 1;
36588 DECL_CONTEXT (iter_var) = current_function_decl;
36589 pushdecl (iter_var);
36590
36591 *last = make_tree_vec (6);
36592 TREE_VEC_ELT (*last, 0) = iter_var;
36593 TREE_VEC_ELT (*last, 1) = begin;
36594 TREE_VEC_ELT (*last, 2) = end;
36595 TREE_VEC_ELT (*last, 3) = step;
36596 last = &TREE_CHAIN (*last);
36597
36598 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36599 {
36600 cp_lexer_consume_token (parser->lexer);
36601 continue;
36602 }
36603 break;
36604 }
36605 while (1);
36606
36607 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36608 parser->colon_doesnt_start_class_def_p
36609 = saved_colon_doesnt_start_class_def_p;
36610
36611 if (!parens.require_close (parser))
36612 cp_parser_skip_to_closing_parenthesis (parser,
36613 /*recovering=*/true,
36614 /*or_comma=*/false,
36615 /*consume_paren=*/true);
36616
36617 return ret ? ret : error_mark_node;
36618 }
36619
36620 /* OpenMP 4.0:
36621 depend ( depend-kind : variable-list )
36622
36623 depend-kind:
36624 in | out | inout
36625
36626 OpenMP 4.5:
36627 depend ( source )
36628
36629 depend ( sink : vec )
36630
36631 OpenMP 5.0:
36632 depend ( depend-modifier , depend-kind: variable-list )
36633
36634 depend-kind:
36635 in | out | inout | mutexinoutset | depobj
36636
36637 depend-modifier:
36638 iterator ( iterators-definition ) */
36639
36640 static tree
36641 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
36642 {
36643 tree nlist, c, iterators = NULL_TREE;
36644 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
36645
36646 matching_parens parens;
36647 if (!parens.require_open (parser))
36648 return list;
36649
36650 do
36651 {
36652 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36653 goto invalid_kind;
36654
36655 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36656 const char *p = IDENTIFIER_POINTER (id);
36657
36658 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
36659 {
36660 begin_scope (sk_omp, NULL);
36661 iterators = cp_parser_omp_iterators (parser);
36662 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
36663 continue;
36664 }
36665 if (strcmp ("in", p) == 0)
36666 kind = OMP_CLAUSE_DEPEND_IN;
36667 else if (strcmp ("inout", p) == 0)
36668 kind = OMP_CLAUSE_DEPEND_INOUT;
36669 else if (strcmp ("mutexinoutset", p) == 0)
36670 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
36671 else if (strcmp ("out", p) == 0)
36672 kind = OMP_CLAUSE_DEPEND_OUT;
36673 else if (strcmp ("depobj", p) == 0)
36674 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
36675 else if (strcmp ("sink", p) == 0)
36676 kind = OMP_CLAUSE_DEPEND_SINK;
36677 else if (strcmp ("source", p) == 0)
36678 kind = OMP_CLAUSE_DEPEND_SOURCE;
36679 else
36680 goto invalid_kind;
36681 break;
36682 }
36683 while (1);
36684
36685 cp_lexer_consume_token (parser->lexer);
36686
36687 if (iterators
36688 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
36689 {
36690 poplevel (0, 1, 0);
36691 error_at (loc, "%<iterator%> modifier incompatible with %qs",
36692 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
36693 iterators = NULL_TREE;
36694 }
36695
36696 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
36697 {
36698 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
36699 OMP_CLAUSE_DEPEND_KIND (c) = kind;
36700 OMP_CLAUSE_DECL (c) = NULL_TREE;
36701 OMP_CLAUSE_CHAIN (c) = list;
36702 if (!parens.require_close (parser))
36703 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36704 /*or_comma=*/false,
36705 /*consume_paren=*/true);
36706 return c;
36707 }
36708
36709 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36710 goto resync_fail;
36711
36712 if (kind == OMP_CLAUSE_DEPEND_SINK)
36713 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
36714 else
36715 {
36716 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
36717 list, NULL);
36718
36719 if (iterators)
36720 {
36721 tree block = poplevel (1, 1, 0);
36722 if (iterators == error_mark_node)
36723 iterators = NULL_TREE;
36724 else
36725 TREE_VEC_ELT (iterators, 5) = block;
36726 }
36727
36728 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36729 {
36730 OMP_CLAUSE_DEPEND_KIND (c) = kind;
36731 if (iterators)
36732 OMP_CLAUSE_DECL (c)
36733 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
36734 }
36735 }
36736 return nlist;
36737
36738 invalid_kind:
36739 cp_parser_error (parser, "invalid depend kind");
36740 resync_fail:
36741 if (iterators)
36742 poplevel (0, 1, 0);
36743 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36744 /*or_comma=*/false,
36745 /*consume_paren=*/true);
36746 return list;
36747 }
36748
36749 /* OpenMP 4.0:
36750 map ( map-kind : variable-list )
36751 map ( variable-list )
36752
36753 map-kind:
36754 alloc | to | from | tofrom
36755
36756 OpenMP 4.5:
36757 map-kind:
36758 alloc | to | from | tofrom | release | delete
36759
36760 map ( always [,] map-kind: variable-list ) */
36761
36762 static tree
36763 cp_parser_omp_clause_map (cp_parser *parser, tree list)
36764 {
36765 tree nlist, c;
36766 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
36767 bool always = false;
36768
36769 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36770 return list;
36771
36772 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36773 {
36774 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36775 const char *p = IDENTIFIER_POINTER (id);
36776
36777 if (strcmp ("always", p) == 0)
36778 {
36779 int nth = 2;
36780 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
36781 nth++;
36782 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
36783 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
36784 == RID_DELETE))
36785 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
36786 == CPP_COLON))
36787 {
36788 always = true;
36789 cp_lexer_consume_token (parser->lexer);
36790 if (nth == 3)
36791 cp_lexer_consume_token (parser->lexer);
36792 }
36793 }
36794 }
36795
36796 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36797 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
36798 {
36799 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36800 const char *p = IDENTIFIER_POINTER (id);
36801
36802 if (strcmp ("alloc", p) == 0)
36803 kind = GOMP_MAP_ALLOC;
36804 else if (strcmp ("to", p) == 0)
36805 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
36806 else if (strcmp ("from", p) == 0)
36807 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
36808 else if (strcmp ("tofrom", p) == 0)
36809 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
36810 else if (strcmp ("release", p) == 0)
36811 kind = GOMP_MAP_RELEASE;
36812 else
36813 {
36814 cp_parser_error (parser, "invalid map kind");
36815 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36816 /*or_comma=*/false,
36817 /*consume_paren=*/true);
36818 return list;
36819 }
36820 cp_lexer_consume_token (parser->lexer);
36821 cp_lexer_consume_token (parser->lexer);
36822 }
36823 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
36824 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
36825 {
36826 kind = GOMP_MAP_DELETE;
36827 cp_lexer_consume_token (parser->lexer);
36828 cp_lexer_consume_token (parser->lexer);
36829 }
36830
36831 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
36832 NULL);
36833
36834 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36835 OMP_CLAUSE_SET_MAP_KIND (c, kind);
36836
36837 return nlist;
36838 }
36839
36840 /* OpenMP 4.0:
36841 device ( expression ) */
36842
36843 static tree
36844 cp_parser_omp_clause_device (cp_parser *parser, tree list,
36845 location_t location)
36846 {
36847 tree t, c;
36848
36849 matching_parens parens;
36850 if (!parens.require_open (parser))
36851 return list;
36852
36853 t = cp_parser_assignment_expression (parser);
36854
36855 if (t == error_mark_node
36856 || !parens.require_close (parser))
36857 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36858 /*or_comma=*/false,
36859 /*consume_paren=*/true);
36860
36861 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
36862 "device", location);
36863
36864 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
36865 OMP_CLAUSE_DEVICE_ID (c) = t;
36866 OMP_CLAUSE_CHAIN (c) = list;
36867
36868 return c;
36869 }
36870
36871 /* OpenMP 4.0:
36872 dist_schedule ( static )
36873 dist_schedule ( static , expression ) */
36874
36875 static tree
36876 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
36877 location_t location)
36878 {
36879 tree c, t;
36880
36881 matching_parens parens;
36882 if (!parens.require_open (parser))
36883 return list;
36884
36885 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
36886
36887 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
36888 goto invalid_kind;
36889 cp_lexer_consume_token (parser->lexer);
36890
36891 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36892 {
36893 cp_lexer_consume_token (parser->lexer);
36894
36895 t = cp_parser_assignment_expression (parser);
36896
36897 if (t == error_mark_node)
36898 goto resync_fail;
36899 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
36900
36901 if (!parens.require_close (parser))
36902 goto resync_fail;
36903 }
36904 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
36905 goto resync_fail;
36906
36907 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
36908 "dist_schedule", location); */
36909 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
36910 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
36911 OMP_CLAUSE_CHAIN (c) = list;
36912 return c;
36913
36914 invalid_kind:
36915 cp_parser_error (parser, "invalid dist_schedule kind");
36916 resync_fail:
36917 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36918 /*or_comma=*/false,
36919 /*consume_paren=*/true);
36920 return list;
36921 }
36922
36923 /* OpenMP 4.0:
36924 proc_bind ( proc-bind-kind )
36925
36926 proc-bind-kind:
36927 master | close | spread */
36928
36929 static tree
36930 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
36931 location_t location)
36932 {
36933 tree c;
36934 enum omp_clause_proc_bind_kind kind;
36935
36936 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36937 return list;
36938
36939 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36940 {
36941 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36942 const char *p = IDENTIFIER_POINTER (id);
36943
36944 if (strcmp ("master", p) == 0)
36945 kind = OMP_CLAUSE_PROC_BIND_MASTER;
36946 else if (strcmp ("close", p) == 0)
36947 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
36948 else if (strcmp ("spread", p) == 0)
36949 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
36950 else
36951 goto invalid_kind;
36952 }
36953 else
36954 goto invalid_kind;
36955
36956 cp_lexer_consume_token (parser->lexer);
36957 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
36958 goto resync_fail;
36959
36960 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
36961 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
36962 location);
36963 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
36964 OMP_CLAUSE_CHAIN (c) = list;
36965 return c;
36966
36967 invalid_kind:
36968 cp_parser_error (parser, "invalid depend kind");
36969 resync_fail:
36970 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36971 /*or_comma=*/false,
36972 /*consume_paren=*/true);
36973 return list;
36974 }
36975
36976 /* OpenMP 5.0:
36977 device_type ( host | nohost | any ) */
36978
36979 static tree
36980 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
36981 location_t location)
36982 {
36983 tree c;
36984 enum omp_clause_device_type_kind kind;
36985
36986 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36987 return list;
36988
36989 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36990 {
36991 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36992 const char *p = IDENTIFIER_POINTER (id);
36993
36994 if (strcmp ("host", p) == 0)
36995 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
36996 else if (strcmp ("nohost", p) == 0)
36997 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
36998 else if (strcmp ("any", p) == 0)
36999 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
37000 else
37001 goto invalid_kind;
37002 }
37003 else
37004 goto invalid_kind;
37005
37006 cp_lexer_consume_token (parser->lexer);
37007 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
37008 goto resync_fail;
37009
37010 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
37011 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
37012 location); */
37013 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
37014 OMP_CLAUSE_CHAIN (c) = list;
37015 return c;
37016
37017 invalid_kind:
37018 cp_parser_error (parser, "invalid depend kind");
37019 resync_fail:
37020 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37021 /*or_comma=*/false,
37022 /*consume_paren=*/true);
37023 return list;
37024 }
37025
37026 /* OpenACC:
37027 async [( int-expr )] */
37028
37029 static tree
37030 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
37031 {
37032 tree c, t;
37033 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37034
37035 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
37036
37037 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37038 {
37039 matching_parens parens;
37040 parens.consume_open (parser);
37041
37042 t = cp_parser_expression (parser);
37043 if (t == error_mark_node
37044 || !parens.require_close (parser))
37045 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37046 /*or_comma=*/false,
37047 /*consume_paren=*/true);
37048 }
37049
37050 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
37051
37052 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
37053 OMP_CLAUSE_ASYNC_EXPR (c) = t;
37054 OMP_CLAUSE_CHAIN (c) = list;
37055 list = c;
37056
37057 return list;
37058 }
37059
37060 /* Parse all OpenACC clauses. The set clauses allowed by the directive
37061 is a bitmask in MASK. Return the list of clauses found. */
37062
37063 static tree
37064 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
37065 const char *where, cp_token *pragma_tok,
37066 bool finish_p = true)
37067 {
37068 tree clauses = NULL;
37069 bool first = true;
37070
37071 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37072 {
37073 location_t here;
37074 pragma_omp_clause c_kind;
37075 omp_clause_code code;
37076 const char *c_name;
37077 tree prev = clauses;
37078
37079 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37080 cp_lexer_consume_token (parser->lexer);
37081
37082 here = cp_lexer_peek_token (parser->lexer)->location;
37083 c_kind = cp_parser_omp_clause_name (parser);
37084
37085 switch (c_kind)
37086 {
37087 case PRAGMA_OACC_CLAUSE_ASYNC:
37088 clauses = cp_parser_oacc_clause_async (parser, clauses);
37089 c_name = "async";
37090 break;
37091 case PRAGMA_OACC_CLAUSE_AUTO:
37092 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
37093 clauses);
37094 c_name = "auto";
37095 break;
37096 case PRAGMA_OACC_CLAUSE_ATTACH:
37097 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37098 c_name = "attach";
37099 break;
37100 case PRAGMA_OACC_CLAUSE_COLLAPSE:
37101 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
37102 c_name = "collapse";
37103 break;
37104 case PRAGMA_OACC_CLAUSE_COPY:
37105 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37106 c_name = "copy";
37107 break;
37108 case PRAGMA_OACC_CLAUSE_COPYIN:
37109 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37110 c_name = "copyin";
37111 break;
37112 case PRAGMA_OACC_CLAUSE_COPYOUT:
37113 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37114 c_name = "copyout";
37115 break;
37116 case PRAGMA_OACC_CLAUSE_CREATE:
37117 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37118 c_name = "create";
37119 break;
37120 case PRAGMA_OACC_CLAUSE_DELETE:
37121 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37122 c_name = "delete";
37123 break;
37124 case PRAGMA_OMP_CLAUSE_DEFAULT:
37125 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
37126 c_name = "default";
37127 break;
37128 case PRAGMA_OACC_CLAUSE_DETACH:
37129 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37130 c_name = "detach";
37131 break;
37132 case PRAGMA_OACC_CLAUSE_DEVICE:
37133 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37134 c_name = "device";
37135 break;
37136 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
37137 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
37138 c_name = "deviceptr";
37139 break;
37140 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
37141 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37142 c_name = "device_resident";
37143 break;
37144 case PRAGMA_OACC_CLAUSE_FINALIZE:
37145 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
37146 clauses);
37147 c_name = "finalize";
37148 break;
37149 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
37150 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37151 clauses);
37152 c_name = "firstprivate";
37153 break;
37154 case PRAGMA_OACC_CLAUSE_GANG:
37155 c_name = "gang";
37156 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
37157 c_name, clauses);
37158 break;
37159 case PRAGMA_OACC_CLAUSE_HOST:
37160 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37161 c_name = "host";
37162 break;
37163 case PRAGMA_OACC_CLAUSE_IF:
37164 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
37165 c_name = "if";
37166 break;
37167 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
37168 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
37169 clauses);
37170 c_name = "if_present";
37171 break;
37172 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
37173 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
37174 clauses);
37175 c_name = "independent";
37176 break;
37177 case PRAGMA_OACC_CLAUSE_LINK:
37178 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37179 c_name = "link";
37180 break;
37181 case PRAGMA_OACC_CLAUSE_NO_CREATE:
37182 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37183 c_name = "no_create";
37184 break;
37185 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
37186 code = OMP_CLAUSE_NUM_GANGS;
37187 c_name = "num_gangs";
37188 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
37189 clauses);
37190 break;
37191 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
37192 c_name = "num_workers";
37193 code = OMP_CLAUSE_NUM_WORKERS;
37194 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
37195 clauses);
37196 break;
37197 case PRAGMA_OACC_CLAUSE_PRESENT:
37198 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37199 c_name = "present";
37200 break;
37201 case PRAGMA_OACC_CLAUSE_PRIVATE:
37202 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
37203 clauses);
37204 c_name = "private";
37205 break;
37206 case PRAGMA_OACC_CLAUSE_REDUCTION:
37207 clauses
37208 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
37209 false, clauses);
37210 c_name = "reduction";
37211 break;
37212 case PRAGMA_OACC_CLAUSE_SEQ:
37213 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
37214 clauses);
37215 c_name = "seq";
37216 break;
37217 case PRAGMA_OACC_CLAUSE_TILE:
37218 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
37219 c_name = "tile";
37220 break;
37221 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
37222 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
37223 clauses);
37224 c_name = "use_device";
37225 break;
37226 case PRAGMA_OACC_CLAUSE_VECTOR:
37227 c_name = "vector";
37228 clauses = cp_parser_oacc_shape_clause (parser, here,
37229 OMP_CLAUSE_VECTOR,
37230 c_name, clauses);
37231 break;
37232 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
37233 c_name = "vector_length";
37234 code = OMP_CLAUSE_VECTOR_LENGTH;
37235 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
37236 clauses);
37237 break;
37238 case PRAGMA_OACC_CLAUSE_WAIT:
37239 clauses = cp_parser_oacc_clause_wait (parser, clauses);
37240 c_name = "wait";
37241 break;
37242 case PRAGMA_OACC_CLAUSE_WORKER:
37243 c_name = "worker";
37244 clauses = cp_parser_oacc_shape_clause (parser, here,
37245 OMP_CLAUSE_WORKER,
37246 c_name, clauses);
37247 break;
37248 default:
37249 cp_parser_error (parser, "expected %<#pragma acc%> clause");
37250 goto saw_error;
37251 }
37252
37253 first = false;
37254
37255 if (((mask >> c_kind) & 1) == 0)
37256 {
37257 /* Remove the invalid clause(s) from the list to avoid
37258 confusing the rest of the compiler. */
37259 clauses = prev;
37260 error_at (here, "%qs is not valid for %qs", c_name, where);
37261 }
37262 }
37263
37264 saw_error:
37265 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37266
37267 if (finish_p)
37268 return finish_omp_clauses (clauses, C_ORT_ACC);
37269
37270 return clauses;
37271 }
37272
37273 /* Parse all OpenMP clauses. The set clauses allowed by the directive
37274 is a bitmask in MASK. Return the list of clauses found.
37275 FINISH_P set if finish_omp_clauses should be called.
37276 NESTED non-zero if clauses should be terminated by closing paren instead
37277 of end of pragma. If it is 2, additionally commas are required in between
37278 the clauses. */
37279
37280 static tree
37281 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
37282 const char *where, cp_token *pragma_tok,
37283 bool finish_p = true, int nested = 0)
37284 {
37285 tree clauses = NULL;
37286 bool first = true;
37287 cp_token *token = NULL;
37288
37289 /* Don't create location wrapper nodes within OpenMP clauses. */
37290 auto_suppress_location_wrappers sentinel;
37291
37292 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37293 {
37294 pragma_omp_clause c_kind;
37295 const char *c_name;
37296 tree prev = clauses;
37297
37298 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37299 break;
37300
37301 if (!first)
37302 {
37303 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37304 cp_lexer_consume_token (parser->lexer);
37305 else if (nested == 2)
37306 error_at (cp_lexer_peek_token (parser->lexer)->location,
37307 "clauses in %<simd%> trait should be separated "
37308 "by %<,%>");
37309 }
37310
37311 token = cp_lexer_peek_token (parser->lexer);
37312 c_kind = cp_parser_omp_clause_name (parser);
37313
37314 switch (c_kind)
37315 {
37316 case PRAGMA_OMP_CLAUSE_BIND:
37317 clauses = cp_parser_omp_clause_bind (parser, clauses,
37318 token->location);
37319 c_name = "bind";
37320 break;
37321 case PRAGMA_OMP_CLAUSE_COLLAPSE:
37322 clauses = cp_parser_omp_clause_collapse (parser, clauses,
37323 token->location);
37324 c_name = "collapse";
37325 break;
37326 case PRAGMA_OMP_CLAUSE_COPYIN:
37327 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
37328 c_name = "copyin";
37329 break;
37330 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
37331 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
37332 clauses);
37333 c_name = "copyprivate";
37334 break;
37335 case PRAGMA_OMP_CLAUSE_DEFAULT:
37336 clauses = cp_parser_omp_clause_default (parser, clauses,
37337 token->location, false);
37338 c_name = "default";
37339 break;
37340 case PRAGMA_OMP_CLAUSE_FINAL:
37341 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
37342 c_name = "final";
37343 break;
37344 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
37345 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37346 clauses);
37347 c_name = "firstprivate";
37348 break;
37349 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
37350 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
37351 token->location);
37352 c_name = "grainsize";
37353 break;
37354 case PRAGMA_OMP_CLAUSE_HINT:
37355 clauses = cp_parser_omp_clause_hint (parser, clauses,
37356 token->location);
37357 c_name = "hint";
37358 break;
37359 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
37360 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
37361 token->location);
37362 c_name = "defaultmap";
37363 break;
37364 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
37365 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
37366 clauses);
37367 c_name = "use_device_ptr";
37368 break;
37369 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
37370 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
37371 clauses);
37372 c_name = "use_device_addr";
37373 break;
37374 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
37375 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
37376 clauses);
37377 c_name = "is_device_ptr";
37378 break;
37379 case PRAGMA_OMP_CLAUSE_IF:
37380 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
37381 true);
37382 c_name = "if";
37383 break;
37384 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
37385 clauses
37386 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
37387 true, clauses);
37388 c_name = "in_reduction";
37389 break;
37390 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
37391 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
37392 c_name = "lastprivate";
37393 break;
37394 case PRAGMA_OMP_CLAUSE_MERGEABLE:
37395 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
37396 token->location);
37397 c_name = "mergeable";
37398 break;
37399 case PRAGMA_OMP_CLAUSE_NOWAIT:
37400 clauses = cp_parser_omp_clause_nowait (parser, clauses,
37401 token->location);
37402 c_name = "nowait";
37403 break;
37404 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
37405 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
37406 token->location);
37407 c_name = "num_tasks";
37408 break;
37409 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
37410 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
37411 token->location);
37412 c_name = "num_threads";
37413 break;
37414 case PRAGMA_OMP_CLAUSE_ORDER:
37415 clauses = cp_parser_omp_clause_order (parser, clauses,
37416 token->location);
37417 c_name = "order";
37418 break;
37419 case PRAGMA_OMP_CLAUSE_ORDERED:
37420 clauses = cp_parser_omp_clause_ordered (parser, clauses,
37421 token->location);
37422 c_name = "ordered";
37423 break;
37424 case PRAGMA_OMP_CLAUSE_PRIORITY:
37425 clauses = cp_parser_omp_clause_priority (parser, clauses,
37426 token->location);
37427 c_name = "priority";
37428 break;
37429 case PRAGMA_OMP_CLAUSE_PRIVATE:
37430 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
37431 clauses);
37432 c_name = "private";
37433 break;
37434 case PRAGMA_OMP_CLAUSE_REDUCTION:
37435 clauses
37436 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
37437 true, clauses);
37438 c_name = "reduction";
37439 break;
37440 case PRAGMA_OMP_CLAUSE_SCHEDULE:
37441 clauses = cp_parser_omp_clause_schedule (parser, clauses,
37442 token->location);
37443 c_name = "schedule";
37444 break;
37445 case PRAGMA_OMP_CLAUSE_SHARED:
37446 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
37447 clauses);
37448 c_name = "shared";
37449 break;
37450 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
37451 clauses
37452 = cp_parser_omp_clause_reduction (parser,
37453 OMP_CLAUSE_TASK_REDUCTION,
37454 true, clauses);
37455 c_name = "task_reduction";
37456 break;
37457 case PRAGMA_OMP_CLAUSE_UNTIED:
37458 clauses = cp_parser_omp_clause_untied (parser, clauses,
37459 token->location);
37460 c_name = "untied";
37461 break;
37462 case PRAGMA_OMP_CLAUSE_INBRANCH:
37463 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
37464 clauses, token->location);
37465 c_name = "inbranch";
37466 break;
37467 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
37468 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
37469 clauses);
37470 c_name = "nontemporal";
37471 break;
37472 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
37473 clauses = cp_parser_omp_clause_branch (parser,
37474 OMP_CLAUSE_NOTINBRANCH,
37475 clauses, token->location);
37476 c_name = "notinbranch";
37477 break;
37478 case PRAGMA_OMP_CLAUSE_PARALLEL:
37479 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
37480 clauses, token->location);
37481 c_name = "parallel";
37482 if (!first)
37483 {
37484 clause_not_first:
37485 error_at (token->location, "%qs must be the first clause of %qs",
37486 c_name, where);
37487 clauses = prev;
37488 }
37489 break;
37490 case PRAGMA_OMP_CLAUSE_FOR:
37491 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
37492 clauses, token->location);
37493 c_name = "for";
37494 if (!first)
37495 goto clause_not_first;
37496 break;
37497 case PRAGMA_OMP_CLAUSE_SECTIONS:
37498 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
37499 clauses, token->location);
37500 c_name = "sections";
37501 if (!first)
37502 goto clause_not_first;
37503 break;
37504 case PRAGMA_OMP_CLAUSE_TASKGROUP:
37505 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
37506 clauses, token->location);
37507 c_name = "taskgroup";
37508 if (!first)
37509 goto clause_not_first;
37510 break;
37511 case PRAGMA_OMP_CLAUSE_LINK:
37512 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
37513 c_name = "to";
37514 break;
37515 case PRAGMA_OMP_CLAUSE_TO:
37516 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
37517 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37518 clauses);
37519 else
37520 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
37521 c_name = "to";
37522 break;
37523 case PRAGMA_OMP_CLAUSE_FROM:
37524 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
37525 c_name = "from";
37526 break;
37527 case PRAGMA_OMP_CLAUSE_UNIFORM:
37528 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
37529 clauses);
37530 c_name = "uniform";
37531 break;
37532 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
37533 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
37534 token->location);
37535 c_name = "num_teams";
37536 break;
37537 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
37538 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
37539 token->location);
37540 c_name = "thread_limit";
37541 break;
37542 case PRAGMA_OMP_CLAUSE_ALIGNED:
37543 clauses = cp_parser_omp_clause_aligned (parser, clauses);
37544 c_name = "aligned";
37545 break;
37546 case PRAGMA_OMP_CLAUSE_LINEAR:
37547 {
37548 bool declare_simd = false;
37549 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
37550 declare_simd = true;
37551 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
37552 }
37553 c_name = "linear";
37554 break;
37555 case PRAGMA_OMP_CLAUSE_DEPEND:
37556 clauses = cp_parser_omp_clause_depend (parser, clauses,
37557 token->location);
37558 c_name = "depend";
37559 break;
37560 case PRAGMA_OMP_CLAUSE_MAP:
37561 clauses = cp_parser_omp_clause_map (parser, clauses);
37562 c_name = "map";
37563 break;
37564 case PRAGMA_OMP_CLAUSE_DEVICE:
37565 clauses = cp_parser_omp_clause_device (parser, clauses,
37566 token->location);
37567 c_name = "device";
37568 break;
37569 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
37570 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
37571 token->location);
37572 c_name = "dist_schedule";
37573 break;
37574 case PRAGMA_OMP_CLAUSE_PROC_BIND:
37575 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
37576 token->location);
37577 c_name = "proc_bind";
37578 break;
37579 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
37580 clauses = cp_parser_omp_clause_device_type (parser, clauses,
37581 token->location);
37582 c_name = "device_type";
37583 break;
37584 case PRAGMA_OMP_CLAUSE_SAFELEN:
37585 clauses = cp_parser_omp_clause_safelen (parser, clauses,
37586 token->location);
37587 c_name = "safelen";
37588 break;
37589 case PRAGMA_OMP_CLAUSE_SIMDLEN:
37590 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
37591 token->location);
37592 c_name = "simdlen";
37593 break;
37594 case PRAGMA_OMP_CLAUSE_NOGROUP:
37595 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
37596 token->location);
37597 c_name = "nogroup";
37598 break;
37599 case PRAGMA_OMP_CLAUSE_THREADS:
37600 clauses
37601 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
37602 clauses, token->location);
37603 c_name = "threads";
37604 break;
37605 case PRAGMA_OMP_CLAUSE_SIMD:
37606 clauses
37607 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
37608 clauses, token->location);
37609 c_name = "simd";
37610 break;
37611 default:
37612 cp_parser_error (parser, "expected %<#pragma omp%> clause");
37613 goto saw_error;
37614 }
37615
37616 first = false;
37617
37618 if (((mask >> c_kind) & 1) == 0)
37619 {
37620 /* Remove the invalid clause(s) from the list to avoid
37621 confusing the rest of the compiler. */
37622 clauses = prev;
37623 error_at (token->location, "%qs is not valid for %qs", c_name, where);
37624 }
37625 }
37626 saw_error:
37627 if (!nested)
37628 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37629 if (finish_p)
37630 {
37631 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
37632 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
37633 else
37634 return finish_omp_clauses (clauses, C_ORT_OMP);
37635 }
37636 return clauses;
37637 }
37638
37639 /* OpenMP 2.5:
37640 structured-block:
37641 statement
37642
37643 In practice, we're also interested in adding the statement to an
37644 outer node. So it is convenient if we work around the fact that
37645 cp_parser_statement calls add_stmt. */
37646
37647 static unsigned
37648 cp_parser_begin_omp_structured_block (cp_parser *parser)
37649 {
37650 unsigned save = parser->in_statement;
37651
37652 /* Only move the values to IN_OMP_BLOCK if they weren't false.
37653 This preserves the "not within loop or switch" style error messages
37654 for nonsense cases like
37655 void foo() {
37656 #pragma omp single
37657 break;
37658 }
37659 */
37660 if (parser->in_statement)
37661 parser->in_statement = IN_OMP_BLOCK;
37662
37663 return save;
37664 }
37665
37666 static void
37667 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
37668 {
37669 parser->in_statement = save;
37670 }
37671
37672 static tree
37673 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
37674 {
37675 tree stmt = begin_omp_structured_block ();
37676 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37677
37678 cp_parser_statement (parser, NULL_TREE, false, if_p);
37679
37680 cp_parser_end_omp_structured_block (parser, save);
37681 return finish_omp_structured_block (stmt);
37682 }
37683
37684 /* OpenMP 2.5:
37685 # pragma omp atomic new-line
37686 expression-stmt
37687
37688 expression-stmt:
37689 x binop= expr | x++ | ++x | x-- | --x
37690 binop:
37691 +, *, -, /, &, ^, |, <<, >>
37692
37693 where x is an lvalue expression with scalar type.
37694
37695 OpenMP 3.1:
37696 # pragma omp atomic new-line
37697 update-stmt
37698
37699 # pragma omp atomic read new-line
37700 read-stmt
37701
37702 # pragma omp atomic write new-line
37703 write-stmt
37704
37705 # pragma omp atomic update new-line
37706 update-stmt
37707
37708 # pragma omp atomic capture new-line
37709 capture-stmt
37710
37711 # pragma omp atomic capture new-line
37712 capture-block
37713
37714 read-stmt:
37715 v = x
37716 write-stmt:
37717 x = expr
37718 update-stmt:
37719 expression-stmt | x = x binop expr
37720 capture-stmt:
37721 v = expression-stmt
37722 capture-block:
37723 { v = x; update-stmt; } | { update-stmt; v = x; }
37724
37725 OpenMP 4.0:
37726 update-stmt:
37727 expression-stmt | x = x binop expr | x = expr binop x
37728 capture-stmt:
37729 v = update-stmt
37730 capture-block:
37731 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
37732
37733 where x and v are lvalue expressions with scalar type. */
37734
37735 static void
37736 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
37737 {
37738 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
37739 tree rhs1 = NULL_TREE, orig_lhs;
37740 location_t loc = pragma_tok->location;
37741 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
37742 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
37743 bool structured_block = false;
37744 bool first = true;
37745 tree clauses = NULL_TREE;
37746
37747 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37748 {
37749 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37750 cp_lexer_consume_token (parser->lexer);
37751
37752 first = false;
37753
37754 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37755 {
37756 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37757 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
37758 const char *p = IDENTIFIER_POINTER (id);
37759 enum tree_code new_code = ERROR_MARK;
37760 enum omp_memory_order new_memory_order
37761 = OMP_MEMORY_ORDER_UNSPECIFIED;
37762
37763 if (!strcmp (p, "read"))
37764 new_code = OMP_ATOMIC_READ;
37765 else if (!strcmp (p, "write"))
37766 new_code = NOP_EXPR;
37767 else if (!strcmp (p, "update"))
37768 new_code = OMP_ATOMIC;
37769 else if (!strcmp (p, "capture"))
37770 new_code = OMP_ATOMIC_CAPTURE_NEW;
37771 else if (!strcmp (p, "seq_cst"))
37772 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
37773 else if (!strcmp (p, "acq_rel"))
37774 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
37775 else if (!strcmp (p, "release"))
37776 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
37777 else if (!strcmp (p, "acquire"))
37778 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
37779 else if (!strcmp (p, "relaxed"))
37780 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
37781 else if (!strcmp (p, "hint"))
37782 {
37783 cp_lexer_consume_token (parser->lexer);
37784 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
37785 continue;
37786 }
37787 else
37788 {
37789 p = NULL;
37790 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
37791 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
37792 "%<release%>, %<relaxed%> or %<hint%> clause");
37793 }
37794 if (p)
37795 {
37796 if (new_code != ERROR_MARK)
37797 {
37798 if (code != ERROR_MARK)
37799 error_at (cloc, "too many atomic clauses");
37800 else
37801 code = new_code;
37802 }
37803 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
37804 {
37805 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
37806 error_at (cloc, "too many memory order clauses");
37807 else
37808 memory_order = new_memory_order;
37809 }
37810 cp_lexer_consume_token (parser->lexer);
37811 continue;
37812 }
37813 }
37814 break;
37815 }
37816 cp_parser_require_pragma_eol (parser, pragma_tok);
37817
37818 if (code == ERROR_MARK)
37819 code = OMP_ATOMIC;
37820 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
37821 {
37822 omp_requires_mask
37823 = (enum omp_requires) (omp_requires_mask
37824 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
37825 switch ((enum omp_memory_order)
37826 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
37827 {
37828 case OMP_MEMORY_ORDER_UNSPECIFIED:
37829 case OMP_MEMORY_ORDER_RELAXED:
37830 memory_order = OMP_MEMORY_ORDER_RELAXED;
37831 break;
37832 case OMP_MEMORY_ORDER_SEQ_CST:
37833 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
37834 break;
37835 case OMP_MEMORY_ORDER_ACQ_REL:
37836 switch (code)
37837 {
37838 case OMP_ATOMIC_READ:
37839 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
37840 break;
37841 case NOP_EXPR: /* atomic write */
37842 case OMP_ATOMIC:
37843 memory_order = OMP_MEMORY_ORDER_RELEASE;
37844 break;
37845 default:
37846 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
37847 break;
37848 }
37849 break;
37850 default:
37851 gcc_unreachable ();
37852 }
37853 }
37854 else
37855 switch (code)
37856 {
37857 case OMP_ATOMIC_READ:
37858 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
37859 || memory_order == OMP_MEMORY_ORDER_RELEASE)
37860 {
37861 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
37862 "%<acq_rel%> or %<release%> clauses");
37863 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
37864 }
37865 break;
37866 case NOP_EXPR: /* atomic write */
37867 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
37868 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
37869 {
37870 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
37871 "%<acq_rel%> or %<acquire%> clauses");
37872 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
37873 }
37874 break;
37875 case OMP_ATOMIC:
37876 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
37877 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
37878 {
37879 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
37880 "%<acq_rel%> or %<acquire%> clauses");
37881 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
37882 }
37883 break;
37884 default:
37885 break;
37886 }
37887
37888 switch (code)
37889 {
37890 case OMP_ATOMIC_READ:
37891 case NOP_EXPR: /* atomic write */
37892 v = cp_parser_unary_expression (parser);
37893 if (v == error_mark_node)
37894 goto saw_error;
37895 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
37896 goto saw_error;
37897 if (code == NOP_EXPR)
37898 lhs = cp_parser_expression (parser);
37899 else
37900 lhs = cp_parser_unary_expression (parser);
37901 if (lhs == error_mark_node)
37902 goto saw_error;
37903 if (code == NOP_EXPR)
37904 {
37905 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
37906 opcode. */
37907 code = OMP_ATOMIC;
37908 rhs = lhs;
37909 lhs = v;
37910 v = NULL_TREE;
37911 }
37912 goto done;
37913 case OMP_ATOMIC_CAPTURE_NEW:
37914 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
37915 {
37916 cp_lexer_consume_token (parser->lexer);
37917 structured_block = true;
37918 }
37919 else
37920 {
37921 v = cp_parser_unary_expression (parser);
37922 if (v == error_mark_node)
37923 goto saw_error;
37924 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
37925 goto saw_error;
37926 }
37927 default:
37928 break;
37929 }
37930
37931 restart:
37932 lhs = cp_parser_unary_expression (parser);
37933 orig_lhs = lhs;
37934 switch (TREE_CODE (lhs))
37935 {
37936 case ERROR_MARK:
37937 goto saw_error;
37938
37939 case POSTINCREMENT_EXPR:
37940 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
37941 code = OMP_ATOMIC_CAPTURE_OLD;
37942 /* FALLTHROUGH */
37943 case PREINCREMENT_EXPR:
37944 lhs = TREE_OPERAND (lhs, 0);
37945 opcode = PLUS_EXPR;
37946 rhs = integer_one_node;
37947 break;
37948
37949 case POSTDECREMENT_EXPR:
37950 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
37951 code = OMP_ATOMIC_CAPTURE_OLD;
37952 /* FALLTHROUGH */
37953 case PREDECREMENT_EXPR:
37954 lhs = TREE_OPERAND (lhs, 0);
37955 opcode = MINUS_EXPR;
37956 rhs = integer_one_node;
37957 break;
37958
37959 case COMPOUND_EXPR:
37960 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
37961 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
37962 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
37963 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
37964 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
37965 (TREE_OPERAND (lhs, 1), 0), 0)))
37966 == BOOLEAN_TYPE)
37967 /* Undo effects of boolean_increment for post {in,de}crement. */
37968 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
37969 /* FALLTHRU */
37970 case MODIFY_EXPR:
37971 if (TREE_CODE (lhs) == MODIFY_EXPR
37972 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
37973 {
37974 /* Undo effects of boolean_increment. */
37975 if (integer_onep (TREE_OPERAND (lhs, 1)))
37976 {
37977 /* This is pre or post increment. */
37978 rhs = TREE_OPERAND (lhs, 1);
37979 lhs = TREE_OPERAND (lhs, 0);
37980 opcode = NOP_EXPR;
37981 if (code == OMP_ATOMIC_CAPTURE_NEW
37982 && !structured_block
37983 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
37984 code = OMP_ATOMIC_CAPTURE_OLD;
37985 break;
37986 }
37987 }
37988 /* FALLTHRU */
37989 default:
37990 switch (cp_lexer_peek_token (parser->lexer)->type)
37991 {
37992 case CPP_MULT_EQ:
37993 opcode = MULT_EXPR;
37994 break;
37995 case CPP_DIV_EQ:
37996 opcode = TRUNC_DIV_EXPR;
37997 break;
37998 case CPP_PLUS_EQ:
37999 opcode = PLUS_EXPR;
38000 break;
38001 case CPP_MINUS_EQ:
38002 opcode = MINUS_EXPR;
38003 break;
38004 case CPP_LSHIFT_EQ:
38005 opcode = LSHIFT_EXPR;
38006 break;
38007 case CPP_RSHIFT_EQ:
38008 opcode = RSHIFT_EXPR;
38009 break;
38010 case CPP_AND_EQ:
38011 opcode = BIT_AND_EXPR;
38012 break;
38013 case CPP_OR_EQ:
38014 opcode = BIT_IOR_EXPR;
38015 break;
38016 case CPP_XOR_EQ:
38017 opcode = BIT_XOR_EXPR;
38018 break;
38019 case CPP_EQ:
38020 enum cp_parser_prec oprec;
38021 cp_token *token;
38022 cp_lexer_consume_token (parser->lexer);
38023 cp_parser_parse_tentatively (parser);
38024 rhs1 = cp_parser_simple_cast_expression (parser);
38025 if (rhs1 == error_mark_node)
38026 {
38027 cp_parser_abort_tentative_parse (parser);
38028 cp_parser_simple_cast_expression (parser);
38029 goto saw_error;
38030 }
38031 token = cp_lexer_peek_token (parser->lexer);
38032 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
38033 {
38034 cp_parser_abort_tentative_parse (parser);
38035 cp_parser_parse_tentatively (parser);
38036 rhs = cp_parser_binary_expression (parser, false, true,
38037 PREC_NOT_OPERATOR, NULL);
38038 if (rhs == error_mark_node)
38039 {
38040 cp_parser_abort_tentative_parse (parser);
38041 cp_parser_binary_expression (parser, false, true,
38042 PREC_NOT_OPERATOR, NULL);
38043 goto saw_error;
38044 }
38045 switch (TREE_CODE (rhs))
38046 {
38047 case MULT_EXPR:
38048 case TRUNC_DIV_EXPR:
38049 case RDIV_EXPR:
38050 case PLUS_EXPR:
38051 case MINUS_EXPR:
38052 case LSHIFT_EXPR:
38053 case RSHIFT_EXPR:
38054 case BIT_AND_EXPR:
38055 case BIT_IOR_EXPR:
38056 case BIT_XOR_EXPR:
38057 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
38058 {
38059 if (cp_parser_parse_definitely (parser))
38060 {
38061 opcode = TREE_CODE (rhs);
38062 rhs1 = TREE_OPERAND (rhs, 0);
38063 rhs = TREE_OPERAND (rhs, 1);
38064 goto stmt_done;
38065 }
38066 else
38067 goto saw_error;
38068 }
38069 break;
38070 default:
38071 break;
38072 }
38073 cp_parser_abort_tentative_parse (parser);
38074 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
38075 {
38076 rhs = cp_parser_expression (parser);
38077 if (rhs == error_mark_node)
38078 goto saw_error;
38079 opcode = NOP_EXPR;
38080 rhs1 = NULL_TREE;
38081 goto stmt_done;
38082 }
38083 cp_parser_error (parser,
38084 "invalid form of %<#pragma omp atomic%>");
38085 goto saw_error;
38086 }
38087 if (!cp_parser_parse_definitely (parser))
38088 goto saw_error;
38089 switch (token->type)
38090 {
38091 case CPP_SEMICOLON:
38092 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
38093 {
38094 code = OMP_ATOMIC_CAPTURE_OLD;
38095 v = lhs;
38096 lhs = NULL_TREE;
38097 lhs1 = rhs1;
38098 rhs1 = NULL_TREE;
38099 cp_lexer_consume_token (parser->lexer);
38100 goto restart;
38101 }
38102 else if (structured_block)
38103 {
38104 opcode = NOP_EXPR;
38105 rhs = rhs1;
38106 rhs1 = NULL_TREE;
38107 goto stmt_done;
38108 }
38109 cp_parser_error (parser,
38110 "invalid form of %<#pragma omp atomic%>");
38111 goto saw_error;
38112 case CPP_MULT:
38113 opcode = MULT_EXPR;
38114 break;
38115 case CPP_DIV:
38116 opcode = TRUNC_DIV_EXPR;
38117 break;
38118 case CPP_PLUS:
38119 opcode = PLUS_EXPR;
38120 break;
38121 case CPP_MINUS:
38122 opcode = MINUS_EXPR;
38123 break;
38124 case CPP_LSHIFT:
38125 opcode = LSHIFT_EXPR;
38126 break;
38127 case CPP_RSHIFT:
38128 opcode = RSHIFT_EXPR;
38129 break;
38130 case CPP_AND:
38131 opcode = BIT_AND_EXPR;
38132 break;
38133 case CPP_OR:
38134 opcode = BIT_IOR_EXPR;
38135 break;
38136 case CPP_XOR:
38137 opcode = BIT_XOR_EXPR;
38138 break;
38139 default:
38140 cp_parser_error (parser,
38141 "invalid operator for %<#pragma omp atomic%>");
38142 goto saw_error;
38143 }
38144 oprec = TOKEN_PRECEDENCE (token);
38145 gcc_assert (oprec != PREC_NOT_OPERATOR);
38146 if (commutative_tree_code (opcode))
38147 oprec = (enum cp_parser_prec) (oprec - 1);
38148 cp_lexer_consume_token (parser->lexer);
38149 rhs = cp_parser_binary_expression (parser, false, false,
38150 oprec, NULL);
38151 if (rhs == error_mark_node)
38152 goto saw_error;
38153 goto stmt_done;
38154 /* FALLTHROUGH */
38155 default:
38156 cp_parser_error (parser,
38157 "invalid operator for %<#pragma omp atomic%>");
38158 goto saw_error;
38159 }
38160 cp_lexer_consume_token (parser->lexer);
38161
38162 rhs = cp_parser_expression (parser);
38163 if (rhs == error_mark_node)
38164 goto saw_error;
38165 break;
38166 }
38167 stmt_done:
38168 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
38169 {
38170 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
38171 goto saw_error;
38172 v = cp_parser_unary_expression (parser);
38173 if (v == error_mark_node)
38174 goto saw_error;
38175 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
38176 goto saw_error;
38177 lhs1 = cp_parser_unary_expression (parser);
38178 if (lhs1 == error_mark_node)
38179 goto saw_error;
38180 }
38181 if (structured_block)
38182 {
38183 cp_parser_consume_semicolon_at_end_of_statement (parser);
38184 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
38185 }
38186 done:
38187 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
38188 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
38189 rhs1, clauses, memory_order);
38190 if (!structured_block)
38191 cp_parser_consume_semicolon_at_end_of_statement (parser);
38192 return;
38193
38194 saw_error:
38195 cp_parser_skip_to_end_of_block_or_statement (parser);
38196 if (structured_block)
38197 {
38198 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
38199 cp_lexer_consume_token (parser->lexer);
38200 else if (code == OMP_ATOMIC_CAPTURE_NEW)
38201 {
38202 cp_parser_skip_to_end_of_block_or_statement (parser);
38203 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
38204 cp_lexer_consume_token (parser->lexer);
38205 }
38206 }
38207 }
38208
38209
38210 /* OpenMP 2.5:
38211 # pragma omp barrier new-line */
38212
38213 static void
38214 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
38215 {
38216 cp_parser_require_pragma_eol (parser, pragma_tok);
38217 finish_omp_barrier ();
38218 }
38219
38220 /* OpenMP 2.5:
38221 # pragma omp critical [(name)] new-line
38222 structured-block
38223
38224 OpenMP 4.5:
38225 # pragma omp critical [(name) [hint(expression)]] new-line
38226 structured-block */
38227
38228 #define OMP_CRITICAL_CLAUSE_MASK \
38229 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
38230
38231 static tree
38232 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38233 {
38234 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
38235
38236 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38237 {
38238 matching_parens parens;
38239 parens.consume_open (parser);
38240
38241 name = cp_parser_identifier (parser);
38242
38243 if (name == error_mark_node
38244 || !parens.require_close (parser))
38245 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38246 /*or_comma=*/false,
38247 /*consume_paren=*/true);
38248 if (name == error_mark_node)
38249 name = NULL;
38250
38251 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
38252 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
38253 cp_lexer_consume_token (parser->lexer);
38254 }
38255
38256 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
38257 "#pragma omp critical", pragma_tok);
38258
38259 stmt = cp_parser_omp_structured_block (parser, if_p);
38260 return c_finish_omp_critical (input_location, stmt, name, clauses);
38261 }
38262
38263 /* OpenMP 5.0:
38264 # pragma omp depobj ( depobj ) depobj-clause new-line
38265
38266 depobj-clause:
38267 depend (dependence-type : locator)
38268 destroy
38269 update (dependence-type)
38270
38271 dependence-type:
38272 in
38273 out
38274 inout
38275 mutexinout */
38276
38277 static void
38278 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
38279 {
38280 location_t loc = pragma_tok->location;
38281 matching_parens parens;
38282 if (!parens.require_open (parser))
38283 {
38284 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38285 return;
38286 }
38287
38288 tree depobj = cp_parser_assignment_expression (parser);
38289
38290 if (!parens.require_close (parser))
38291 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38292 /*or_comma=*/false,
38293 /*consume_paren=*/true);
38294
38295 tree clause = NULL_TREE;
38296 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
38297 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
38298 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38299 {
38300 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38301 const char *p = IDENTIFIER_POINTER (id);
38302
38303 cp_lexer_consume_token (parser->lexer);
38304 if (!strcmp ("depend", p))
38305 {
38306 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
38307 if (clause)
38308 clause = finish_omp_clauses (clause, C_ORT_OMP);
38309 if (!clause)
38310 clause = error_mark_node;
38311 }
38312 else if (!strcmp ("destroy", p))
38313 kind = OMP_CLAUSE_DEPEND_LAST;
38314 else if (!strcmp ("update", p))
38315 {
38316 matching_parens c_parens;
38317 if (c_parens.require_open (parser))
38318 {
38319 location_t c2_loc
38320 = cp_lexer_peek_token (parser->lexer)->location;
38321 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38322 {
38323 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
38324 const char *p2 = IDENTIFIER_POINTER (id2);
38325
38326 cp_lexer_consume_token (parser->lexer);
38327 if (!strcmp ("in", p2))
38328 kind = OMP_CLAUSE_DEPEND_IN;
38329 else if (!strcmp ("out", p2))
38330 kind = OMP_CLAUSE_DEPEND_OUT;
38331 else if (!strcmp ("inout", p2))
38332 kind = OMP_CLAUSE_DEPEND_INOUT;
38333 else if (!strcmp ("mutexinoutset", p2))
38334 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
38335 }
38336 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
38337 {
38338 clause = error_mark_node;
38339 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
38340 "%<mutexinoutset%>");
38341 }
38342 if (!c_parens.require_close (parser))
38343 cp_parser_skip_to_closing_parenthesis (parser,
38344 /*recovering=*/true,
38345 /*or_comma=*/false,
38346 /*consume_paren=*/true);
38347 }
38348 else
38349 clause = error_mark_node;
38350 }
38351 }
38352 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
38353 {
38354 clause = error_mark_node;
38355 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
38356 }
38357 cp_parser_require_pragma_eol (parser, pragma_tok);
38358
38359 finish_omp_depobj (loc, depobj, kind, clause);
38360 }
38361
38362
38363 /* OpenMP 2.5:
38364 # pragma omp flush flush-vars[opt] new-line
38365
38366 flush-vars:
38367 ( variable-list )
38368
38369 OpenMP 5.0:
38370 # pragma omp flush memory-order-clause new-line */
38371
38372 static void
38373 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
38374 {
38375 enum memmodel mo = MEMMODEL_LAST;
38376 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38377 {
38378 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38379 const char *p = IDENTIFIER_POINTER (id);
38380 if (!strcmp (p, "acq_rel"))
38381 mo = MEMMODEL_ACQ_REL;
38382 else if (!strcmp (p, "release"))
38383 mo = MEMMODEL_RELEASE;
38384 else if (!strcmp (p, "acquire"))
38385 mo = MEMMODEL_ACQUIRE;
38386 else
38387 error_at (cp_lexer_peek_token (parser->lexer)->location,
38388 "expected %<acq_rel%>, %<release%> or %<acquire%>");
38389 cp_lexer_consume_token (parser->lexer);
38390 }
38391 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38392 {
38393 if (mo != MEMMODEL_LAST)
38394 error_at (cp_lexer_peek_token (parser->lexer)->location,
38395 "%<flush%> list specified together with memory order "
38396 "clause");
38397 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
38398 }
38399 cp_parser_require_pragma_eol (parser, pragma_tok);
38400
38401 finish_omp_flush (mo);
38402 }
38403
38404 /* Helper function, to parse omp for increment expression. */
38405
38406 static tree
38407 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
38408 {
38409 tree cond = cp_parser_binary_expression (parser, false, true,
38410 PREC_NOT_OPERATOR, NULL);
38411 if (cond == error_mark_node
38412 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
38413 {
38414 cp_parser_skip_to_end_of_statement (parser);
38415 return error_mark_node;
38416 }
38417
38418 switch (TREE_CODE (cond))
38419 {
38420 case GT_EXPR:
38421 case GE_EXPR:
38422 case LT_EXPR:
38423 case LE_EXPR:
38424 break;
38425 case NE_EXPR:
38426 if (code != OACC_LOOP)
38427 break;
38428 gcc_fallthrough ();
38429 default:
38430 return error_mark_node;
38431 }
38432
38433 /* If decl is an iterator, preserve LHS and RHS of the relational
38434 expr until finish_omp_for. */
38435 if (decl
38436 && (type_dependent_expression_p (decl)
38437 || CLASS_TYPE_P (TREE_TYPE (decl))))
38438 return cond;
38439
38440 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
38441 TREE_CODE (cond),
38442 TREE_OPERAND (cond, 0), ERROR_MARK,
38443 TREE_OPERAND (cond, 1), ERROR_MARK,
38444 /*overload=*/NULL, tf_warning_or_error);
38445 }
38446
38447 /* Helper function, to parse omp for increment expression. */
38448
38449 static tree
38450 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
38451 {
38452 cp_token *token = cp_lexer_peek_token (parser->lexer);
38453 enum tree_code op;
38454 tree lhs, rhs;
38455 cp_id_kind idk;
38456 bool decl_first;
38457
38458 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
38459 {
38460 op = (token->type == CPP_PLUS_PLUS
38461 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
38462 cp_lexer_consume_token (parser->lexer);
38463 lhs = cp_parser_simple_cast_expression (parser);
38464 if (lhs != decl
38465 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
38466 return error_mark_node;
38467 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
38468 }
38469
38470 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
38471 if (lhs != decl
38472 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
38473 return error_mark_node;
38474
38475 token = cp_lexer_peek_token (parser->lexer);
38476 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
38477 {
38478 op = (token->type == CPP_PLUS_PLUS
38479 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
38480 cp_lexer_consume_token (parser->lexer);
38481 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
38482 }
38483
38484 op = cp_parser_assignment_operator_opt (parser);
38485 if (op == ERROR_MARK)
38486 return error_mark_node;
38487
38488 if (op != NOP_EXPR)
38489 {
38490 rhs = cp_parser_assignment_expression (parser);
38491 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
38492 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
38493 }
38494
38495 lhs = cp_parser_binary_expression (parser, false, false,
38496 PREC_ADDITIVE_EXPRESSION, NULL);
38497 token = cp_lexer_peek_token (parser->lexer);
38498 decl_first = (lhs == decl
38499 || (processing_template_decl && cp_tree_equal (lhs, decl)));
38500 if (decl_first)
38501 lhs = NULL_TREE;
38502 if (token->type != CPP_PLUS
38503 && token->type != CPP_MINUS)
38504 return error_mark_node;
38505
38506 do
38507 {
38508 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
38509 cp_lexer_consume_token (parser->lexer);
38510 rhs = cp_parser_binary_expression (parser, false, false,
38511 PREC_ADDITIVE_EXPRESSION, NULL);
38512 token = cp_lexer_peek_token (parser->lexer);
38513 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
38514 {
38515 if (lhs == NULL_TREE)
38516 {
38517 if (op == PLUS_EXPR)
38518 lhs = rhs;
38519 else
38520 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
38521 tf_warning_or_error);
38522 }
38523 else
38524 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
38525 ERROR_MARK, NULL, tf_warning_or_error);
38526 }
38527 }
38528 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
38529
38530 if (!decl_first)
38531 {
38532 if ((rhs != decl
38533 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
38534 || op == MINUS_EXPR)
38535 return error_mark_node;
38536 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
38537 }
38538 else
38539 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
38540
38541 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
38542 }
38543
38544 /* Parse the initialization statement of an OpenMP for loop.
38545
38546 Return true if the resulting construct should have an
38547 OMP_CLAUSE_PRIVATE added to it. */
38548
38549 static tree
38550 cp_parser_omp_for_loop_init (cp_parser *parser,
38551 tree &this_pre_body,
38552 releasing_vec &for_block,
38553 tree &init,
38554 tree &orig_init,
38555 tree &decl,
38556 tree &real_decl)
38557 {
38558 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
38559 return NULL_TREE;
38560
38561 tree add_private_clause = NULL_TREE;
38562
38563 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
38564
38565 init-expr:
38566 var = lb
38567 integer-type var = lb
38568 random-access-iterator-type var = lb
38569 pointer-type var = lb
38570 */
38571 cp_decl_specifier_seq type_specifiers;
38572
38573 /* First, try to parse as an initialized declaration. See
38574 cp_parser_condition, from whence the bulk of this is copied. */
38575
38576 cp_parser_parse_tentatively (parser);
38577 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
38578 /*is_declaration=*/true,
38579 /*is_trailing_return=*/false,
38580 &type_specifiers);
38581 if (cp_parser_parse_definitely (parser))
38582 {
38583 /* If parsing a type specifier seq succeeded, then this
38584 MUST be a initialized declaration. */
38585 tree asm_specification, attributes;
38586 cp_declarator *declarator;
38587
38588 declarator = cp_parser_declarator (parser,
38589 CP_PARSER_DECLARATOR_NAMED,
38590 CP_PARSER_FLAGS_NONE,
38591 /*ctor_dtor_or_conv_p=*/NULL,
38592 /*parenthesized_p=*/NULL,
38593 /*member_p=*/false,
38594 /*friend_p=*/false,
38595 /*static_p=*/false);
38596 attributes = cp_parser_attributes_opt (parser);
38597 asm_specification = cp_parser_asm_specification_opt (parser);
38598
38599 if (declarator == cp_error_declarator)
38600 cp_parser_skip_to_end_of_statement (parser);
38601
38602 else
38603 {
38604 tree pushed_scope, auto_node;
38605
38606 decl = start_decl (declarator, &type_specifiers,
38607 SD_INITIALIZED, attributes,
38608 /*prefix_attributes=*/NULL_TREE,
38609 &pushed_scope);
38610
38611 auto_node = type_uses_auto (TREE_TYPE (decl));
38612 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
38613 {
38614 if (cp_lexer_next_token_is (parser->lexer,
38615 CPP_OPEN_PAREN))
38616 error ("parenthesized initialization is not allowed in "
38617 "OpenMP %<for%> loop");
38618 else
38619 /* Trigger an error. */
38620 cp_parser_require (parser, CPP_EQ, RT_EQ);
38621
38622 init = error_mark_node;
38623 cp_parser_skip_to_end_of_statement (parser);
38624 }
38625 else if (CLASS_TYPE_P (TREE_TYPE (decl))
38626 || type_dependent_expression_p (decl)
38627 || auto_node)
38628 {
38629 bool is_direct_init, is_non_constant_init;
38630
38631 init = cp_parser_initializer (parser,
38632 &is_direct_init,
38633 &is_non_constant_init);
38634
38635 if (auto_node)
38636 {
38637 TREE_TYPE (decl)
38638 = do_auto_deduction (TREE_TYPE (decl), init,
38639 auto_node);
38640
38641 if (!CLASS_TYPE_P (TREE_TYPE (decl))
38642 && !type_dependent_expression_p (decl))
38643 goto non_class;
38644 }
38645
38646 cp_finish_decl (decl, init, !is_non_constant_init,
38647 asm_specification,
38648 LOOKUP_ONLYCONVERTING);
38649 orig_init = init;
38650 if (CLASS_TYPE_P (TREE_TYPE (decl)))
38651 {
38652 vec_safe_push (for_block, this_pre_body);
38653 init = NULL_TREE;
38654 }
38655 else
38656 {
38657 init = pop_stmt_list (this_pre_body);
38658 if (init && TREE_CODE (init) == STATEMENT_LIST)
38659 {
38660 tree_stmt_iterator i = tsi_start (init);
38661 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
38662 while (!tsi_end_p (i))
38663 {
38664 tree t = tsi_stmt (i);
38665 if (TREE_CODE (t) == DECL_EXPR
38666 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
38667 {
38668 tsi_delink (&i);
38669 vec_safe_push (for_block, t);
38670 continue;
38671 }
38672 break;
38673 }
38674 if (tsi_one_before_end_p (i))
38675 {
38676 tree t = tsi_stmt (i);
38677 tsi_delink (&i);
38678 free_stmt_list (init);
38679 init = t;
38680 }
38681 }
38682 }
38683 this_pre_body = NULL_TREE;
38684 }
38685 else
38686 {
38687 /* Consume '='. */
38688 cp_lexer_consume_token (parser->lexer);
38689 init = cp_parser_assignment_expression (parser);
38690
38691 non_class:
38692 if (TYPE_REF_P (TREE_TYPE (decl)))
38693 init = error_mark_node;
38694 else
38695 cp_finish_decl (decl, NULL_TREE,
38696 /*init_const_expr_p=*/false,
38697 asm_specification,
38698 LOOKUP_ONLYCONVERTING);
38699 }
38700
38701 if (pushed_scope)
38702 pop_scope (pushed_scope);
38703 }
38704 }
38705 else
38706 {
38707 cp_id_kind idk;
38708 /* If parsing a type specifier sequence failed, then
38709 this MUST be a simple expression. */
38710 cp_parser_parse_tentatively (parser);
38711 decl = cp_parser_primary_expression (parser, false, false,
38712 false, &idk);
38713 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
38714 if (!cp_parser_error_occurred (parser)
38715 && decl
38716 && (TREE_CODE (decl) == COMPONENT_REF
38717 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
38718 {
38719 cp_parser_abort_tentative_parse (parser);
38720 cp_parser_parse_tentatively (parser);
38721 cp_token *token = cp_lexer_peek_token (parser->lexer);
38722 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
38723 /*check_dependency_p=*/true,
38724 /*template_p=*/NULL,
38725 /*declarator_p=*/false,
38726 /*optional_p=*/false);
38727 if (name != error_mark_node
38728 && last_tok == cp_lexer_peek_token (parser->lexer))
38729 {
38730 decl = cp_parser_lookup_name_simple (parser, name,
38731 token->location);
38732 if (TREE_CODE (decl) == FIELD_DECL)
38733 add_private_clause = omp_privatize_field (decl, false);
38734 }
38735 cp_parser_abort_tentative_parse (parser);
38736 cp_parser_parse_tentatively (parser);
38737 decl = cp_parser_primary_expression (parser, false, false,
38738 false, &idk);
38739 }
38740 if (!cp_parser_error_occurred (parser)
38741 && decl
38742 && DECL_P (decl)
38743 && CLASS_TYPE_P (TREE_TYPE (decl)))
38744 {
38745 tree rhs;
38746
38747 cp_parser_parse_definitely (parser);
38748 cp_parser_require (parser, CPP_EQ, RT_EQ);
38749 rhs = cp_parser_assignment_expression (parser);
38750 orig_init = rhs;
38751 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
38752 decl, NOP_EXPR,
38753 rhs,
38754 tf_warning_or_error));
38755 if (!add_private_clause)
38756 add_private_clause = decl;
38757 }
38758 else
38759 {
38760 decl = NULL;
38761 cp_parser_abort_tentative_parse (parser);
38762 init = cp_parser_expression (parser);
38763 if (init)
38764 {
38765 if (TREE_CODE (init) == MODIFY_EXPR
38766 || TREE_CODE (init) == MODOP_EXPR)
38767 real_decl = TREE_OPERAND (init, 0);
38768 }
38769 }
38770 }
38771 return add_private_clause;
38772 }
38773
38774 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
38775
38776 void
38777 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
38778 tree &decl, tree &orig_decl, tree &init,
38779 tree &orig_init, tree &cond, tree &incr)
38780 {
38781 tree begin, end, range_temp_decl = NULL_TREE;
38782 tree iter_type, begin_expr, end_expr;
38783
38784 if (processing_template_decl)
38785 {
38786 if (check_for_bare_parameter_packs (init))
38787 init = error_mark_node;
38788 if (!type_dependent_expression_p (init)
38789 /* do_auto_deduction doesn't mess with template init-lists. */
38790 && !BRACE_ENCLOSED_INITIALIZER_P (init))
38791 {
38792 tree d = decl;
38793 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
38794 {
38795 tree v = DECL_VALUE_EXPR (decl);
38796 if (TREE_CODE (v) == ARRAY_REF
38797 && VAR_P (TREE_OPERAND (v, 0))
38798 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
38799 d = TREE_OPERAND (v, 0);
38800 }
38801 do_range_for_auto_deduction (d, init);
38802 }
38803 cond = global_namespace;
38804 incr = NULL_TREE;
38805 orig_init = init;
38806 if (this_pre_body)
38807 this_pre_body = pop_stmt_list (this_pre_body);
38808 return;
38809 }
38810
38811 init = mark_lvalue_use (init);
38812
38813 if (decl == error_mark_node || init == error_mark_node)
38814 /* If an error happened previously do nothing or else a lot of
38815 unhelpful errors would be issued. */
38816 begin_expr = end_expr = iter_type = error_mark_node;
38817 else
38818 {
38819 tree range_temp;
38820
38821 if (VAR_P (init)
38822 && array_of_runtime_bound_p (TREE_TYPE (init)))
38823 /* Can't bind a reference to an array of runtime bound. */
38824 range_temp = init;
38825 else
38826 {
38827 range_temp = build_range_temp (init);
38828 DECL_NAME (range_temp) = NULL_TREE;
38829 pushdecl (range_temp);
38830 cp_finish_decl (range_temp, init,
38831 /*is_constant_init*/false, NULL_TREE,
38832 LOOKUP_ONLYCONVERTING);
38833 range_temp_decl = range_temp;
38834 range_temp = convert_from_reference (range_temp);
38835 }
38836 iter_type = cp_parser_perform_range_for_lookup (range_temp,
38837 &begin_expr, &end_expr);
38838 }
38839
38840 tree end_iter_type = iter_type;
38841 if (cxx_dialect >= cxx17)
38842 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
38843 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
38844 TREE_USED (end) = 1;
38845 DECL_ARTIFICIAL (end) = 1;
38846 pushdecl (end);
38847 cp_finish_decl (end, end_expr,
38848 /*is_constant_init*/false, NULL_TREE,
38849 LOOKUP_ONLYCONVERTING);
38850
38851 /* The new for initialization statement. */
38852 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
38853 TREE_USED (begin) = 1;
38854 DECL_ARTIFICIAL (begin) = 1;
38855 pushdecl (begin);
38856 orig_init = init;
38857 if (CLASS_TYPE_P (iter_type))
38858 init = NULL_TREE;
38859 else
38860 {
38861 init = begin_expr;
38862 begin_expr = NULL_TREE;
38863 }
38864 cp_finish_decl (begin, begin_expr,
38865 /*is_constant_init*/false, NULL_TREE,
38866 LOOKUP_ONLYCONVERTING);
38867
38868 /* The new for condition. */
38869 if (CLASS_TYPE_P (iter_type))
38870 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
38871 else
38872 cond = build_x_binary_op (input_location, NE_EXPR,
38873 begin, ERROR_MARK,
38874 end, ERROR_MARK,
38875 NULL, tf_warning_or_error);
38876
38877 /* The new increment expression. */
38878 if (CLASS_TYPE_P (iter_type))
38879 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
38880 else
38881 incr = finish_unary_op_expr (input_location,
38882 PREINCREMENT_EXPR, begin,
38883 tf_warning_or_error);
38884
38885 orig_decl = decl;
38886 decl = begin;
38887 if (for_block)
38888 {
38889 vec_safe_push (for_block, this_pre_body);
38890 this_pre_body = NULL_TREE;
38891 }
38892
38893 tree decomp_first_name = NULL_TREE;
38894 unsigned decomp_cnt = 0;
38895 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
38896 {
38897 tree v = DECL_VALUE_EXPR (orig_decl);
38898 if (TREE_CODE (v) == ARRAY_REF
38899 && VAR_P (TREE_OPERAND (v, 0))
38900 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
38901 {
38902 tree d = orig_decl;
38903 orig_decl = TREE_OPERAND (v, 0);
38904 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
38905 decomp_first_name = d;
38906 }
38907 }
38908
38909 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
38910 if (auto_node)
38911 {
38912 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
38913 tf_none);
38914 if (!error_operand_p (t))
38915 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
38916 t, auto_node);
38917 }
38918
38919 tree v = make_tree_vec (decomp_cnt + 3);
38920 TREE_VEC_ELT (v, 0) = range_temp_decl;
38921 TREE_VEC_ELT (v, 1) = end;
38922 TREE_VEC_ELT (v, 2) = orig_decl;
38923 for (unsigned i = 0; i < decomp_cnt; i++)
38924 {
38925 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
38926 decomp_first_name = DECL_CHAIN (decomp_first_name);
38927 }
38928 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
38929 }
38930
38931 /* Helper for cp_parser_omp_for_loop, finalize part of range for
38932 inside of the collapsed body. */
38933
38934 void
38935 cp_finish_omp_range_for (tree orig, tree begin)
38936 {
38937 gcc_assert (TREE_CODE (orig) == TREE_LIST
38938 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
38939 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
38940 tree decomp_first_name = NULL_TREE;
38941 unsigned int decomp_cnt = 0;
38942
38943 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
38944 {
38945 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
38946 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
38947 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
38948 }
38949
38950 /* The declaration is initialized with *__begin inside the loop body. */
38951 cp_finish_decl (decl,
38952 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
38953 tf_warning_or_error),
38954 /*is_constant_init*/false, NULL_TREE,
38955 LOOKUP_ONLYCONVERTING);
38956 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
38957 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
38958 }
38959
38960 /* OpenMP 5.0:
38961
38962 scan-loop-body:
38963 { structured-block scan-directive structured-block } */
38964
38965 static void
38966 cp_parser_omp_scan_loop_body (cp_parser *parser)
38967 {
38968 tree substmt, clauses = NULL_TREE;
38969
38970 matching_braces braces;
38971 if (!braces.require_open (parser))
38972 return;
38973
38974 substmt = cp_parser_omp_structured_block (parser, NULL);
38975 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
38976 add_stmt (substmt);
38977
38978 cp_token *tok = cp_lexer_peek_token (parser->lexer);
38979 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
38980 {
38981 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
38982
38983 cp_lexer_consume_token (parser->lexer);
38984
38985 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38986 {
38987 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38988 const char *p = IDENTIFIER_POINTER (id);
38989 if (strcmp (p, "inclusive") == 0)
38990 clause = OMP_CLAUSE_INCLUSIVE;
38991 else if (strcmp (p, "exclusive") == 0)
38992 clause = OMP_CLAUSE_EXCLUSIVE;
38993 }
38994 if (clause != OMP_CLAUSE_ERROR)
38995 {
38996 cp_lexer_consume_token (parser->lexer);
38997 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
38998 }
38999 else
39000 cp_parser_error (parser, "expected %<inclusive%> or "
39001 "%<exclusive%> clause");
39002
39003 cp_parser_require_pragma_eol (parser, tok);
39004 }
39005 else
39006 error ("expected %<#pragma omp scan%>");
39007
39008 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39009 substmt = cp_parser_omp_structured_block (parser, NULL);
39010 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
39011 clauses);
39012 add_stmt (substmt);
39013
39014 braces.require_close (parser);
39015 }
39016
39017 /* Parse the restricted form of the for statement allowed by OpenMP. */
39018
39019 static tree
39020 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
39021 tree *cclauses, bool *if_p)
39022 {
39023 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
39024 tree orig_decl;
39025 tree real_decl, initv, condv, incrv, declv, orig_declv;
39026 tree this_pre_body, cl, ordered_cl = NULL_TREE;
39027 location_t loc_first;
39028 bool collapse_err = false;
39029 int i, collapse = 1, ordered = 0, count, nbraces = 0;
39030 releasing_vec for_block;
39031 auto_vec<tree, 4> orig_inits;
39032 bool tiling = false;
39033 bool inscan = false;
39034
39035 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
39036 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
39037 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
39038 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
39039 {
39040 tiling = true;
39041 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
39042 }
39043 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
39044 && OMP_CLAUSE_ORDERED_EXPR (cl))
39045 {
39046 ordered_cl = cl;
39047 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
39048 }
39049 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
39050 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
39051 && (code == OMP_SIMD || code == OMP_FOR))
39052 inscan = true;
39053
39054 if (ordered && ordered < collapse)
39055 {
39056 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
39057 "%<ordered%> clause parameter is less than %<collapse%>");
39058 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
39059 = build_int_cst (NULL_TREE, collapse);
39060 ordered = collapse;
39061 }
39062 if (ordered)
39063 {
39064 for (tree *pc = &clauses; *pc; )
39065 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
39066 {
39067 error_at (OMP_CLAUSE_LOCATION (*pc),
39068 "%<linear%> clause may not be specified together "
39069 "with %<ordered%> clause with a parameter");
39070 *pc = OMP_CLAUSE_CHAIN (*pc);
39071 }
39072 else
39073 pc = &OMP_CLAUSE_CHAIN (*pc);
39074 }
39075
39076 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
39077 count = ordered ? ordered : collapse;
39078
39079 declv = make_tree_vec (count);
39080 initv = make_tree_vec (count);
39081 condv = make_tree_vec (count);
39082 incrv = make_tree_vec (count);
39083 orig_declv = NULL_TREE;
39084
39085 loc_first = cp_lexer_peek_token (parser->lexer)->location;
39086
39087 for (i = 0; i < count; i++)
39088 {
39089 int bracecount = 0;
39090 tree add_private_clause = NULL_TREE;
39091 location_t loc;
39092
39093 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
39094 {
39095 if (!collapse_err)
39096 cp_parser_error (parser, "for statement expected");
39097 return NULL;
39098 }
39099 loc = cp_lexer_consume_token (parser->lexer)->location;
39100
39101 /* Don't create location wrapper nodes within an OpenMP "for"
39102 statement. */
39103 auto_suppress_location_wrappers sentinel;
39104
39105 matching_parens parens;
39106 if (!parens.require_open (parser))
39107 return NULL;
39108
39109 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
39110 this_pre_body = push_stmt_list ();
39111
39112 if (code != OACC_LOOP && cxx_dialect >= cxx11)
39113 {
39114 /* Save tokens so that we can put them back. */
39115 cp_lexer_save_tokens (parser->lexer);
39116
39117 /* Look for ':' that is not nested in () or {}. */
39118 bool is_range_for
39119 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
39120 /*recovering=*/false,
39121 CPP_COLON,
39122 /*consume_paren=*/
39123 false) == -1);
39124
39125 /* Roll back the tokens we skipped. */
39126 cp_lexer_rollback_tokens (parser->lexer);
39127
39128 if (is_range_for)
39129 {
39130 bool saved_colon_corrects_to_scope_p
39131 = parser->colon_corrects_to_scope_p;
39132
39133 /* A colon is used in range-based for. */
39134 parser->colon_corrects_to_scope_p = false;
39135
39136 /* Parse the declaration. */
39137 cp_parser_simple_declaration (parser,
39138 /*function_definition_allowed_p=*/
39139 false, &decl);
39140 parser->colon_corrects_to_scope_p
39141 = saved_colon_corrects_to_scope_p;
39142
39143 cp_parser_require (parser, CPP_COLON, RT_COLON);
39144
39145 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
39146 false, 0, true);
39147
39148 cp_convert_omp_range_for (this_pre_body, for_block, decl,
39149 orig_decl, init, orig_init,
39150 cond, incr);
39151 if (this_pre_body)
39152 {
39153 if (pre_body)
39154 {
39155 tree t = pre_body;
39156 pre_body = push_stmt_list ();
39157 add_stmt (t);
39158 add_stmt (this_pre_body);
39159 pre_body = pop_stmt_list (pre_body);
39160 }
39161 else
39162 pre_body = this_pre_body;
39163 }
39164
39165 if (ordered_cl)
39166 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
39167 "%<ordered%> clause with parameter on "
39168 "range-based %<for%> loop");
39169
39170 goto parse_close_paren;
39171 }
39172 }
39173
39174 add_private_clause
39175 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
39176 init, orig_init, decl, real_decl);
39177
39178 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
39179 if (this_pre_body)
39180 {
39181 this_pre_body = pop_stmt_list (this_pre_body);
39182 if (pre_body)
39183 {
39184 tree t = pre_body;
39185 pre_body = push_stmt_list ();
39186 add_stmt (t);
39187 add_stmt (this_pre_body);
39188 pre_body = pop_stmt_list (pre_body);
39189 }
39190 else
39191 pre_body = this_pre_body;
39192 }
39193
39194 if (decl)
39195 real_decl = decl;
39196 if (cclauses != NULL
39197 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
39198 && real_decl != NULL_TREE
39199 && code != OMP_LOOP)
39200 {
39201 tree *c;
39202 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
39203 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
39204 && OMP_CLAUSE_DECL (*c) == real_decl)
39205 {
39206 error_at (loc, "iteration variable %qD"
39207 " should not be firstprivate", real_decl);
39208 *c = OMP_CLAUSE_CHAIN (*c);
39209 }
39210 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
39211 && OMP_CLAUSE_DECL (*c) == real_decl)
39212 {
39213 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
39214 tree l = *c;
39215 *c = OMP_CLAUSE_CHAIN (*c);
39216 if (code == OMP_SIMD)
39217 {
39218 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
39219 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
39220 }
39221 else
39222 {
39223 OMP_CLAUSE_CHAIN (l) = clauses;
39224 clauses = l;
39225 }
39226 add_private_clause = NULL_TREE;
39227 }
39228 else
39229 {
39230 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
39231 && OMP_CLAUSE_DECL (*c) == real_decl)
39232 add_private_clause = NULL_TREE;
39233 c = &OMP_CLAUSE_CHAIN (*c);
39234 }
39235 }
39236
39237 if (add_private_clause)
39238 {
39239 tree c;
39240 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
39241 {
39242 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
39243 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
39244 && OMP_CLAUSE_DECL (c) == decl)
39245 break;
39246 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
39247 && OMP_CLAUSE_DECL (c) == decl)
39248 error_at (loc, "iteration variable %qD "
39249 "should not be firstprivate",
39250 decl);
39251 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
39252 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
39253 && OMP_CLAUSE_DECL (c) == decl)
39254 error_at (loc, "iteration variable %qD should not be reduction",
39255 decl);
39256 }
39257 if (c == NULL)
39258 {
39259 if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
39260 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
39261 else if (code != OMP_SIMD)
39262 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
39263 else
39264 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
39265 OMP_CLAUSE_DECL (c) = add_private_clause;
39266 c = finish_omp_clauses (c, C_ORT_OMP);
39267 if (c)
39268 {
39269 OMP_CLAUSE_CHAIN (c) = clauses;
39270 clauses = c;
39271 /* For linear, signal that we need to fill up
39272 the so far unknown linear step. */
39273 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
39274 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
39275 }
39276 }
39277 }
39278
39279 cond = NULL;
39280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
39281 cond = cp_parser_omp_for_cond (parser, decl, code);
39282 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
39283
39284 incr = NULL;
39285 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
39286 {
39287 /* If decl is an iterator, preserve the operator on decl
39288 until finish_omp_for. */
39289 if (real_decl
39290 && ((processing_template_decl
39291 && (TREE_TYPE (real_decl) == NULL_TREE
39292 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
39293 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
39294 incr = cp_parser_omp_for_incr (parser, real_decl);
39295 else
39296 incr = cp_parser_expression (parser);
39297 protected_set_expr_location_if_unset (incr, input_location);
39298 }
39299
39300 parse_close_paren:
39301 if (!parens.require_close (parser))
39302 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39303 /*or_comma=*/false,
39304 /*consume_paren=*/true);
39305
39306 TREE_VEC_ELT (declv, i) = decl;
39307 TREE_VEC_ELT (initv, i) = init;
39308 TREE_VEC_ELT (condv, i) = cond;
39309 TREE_VEC_ELT (incrv, i) = incr;
39310 if (orig_init)
39311 {
39312 orig_inits.safe_grow_cleared (i + 1, true);
39313 orig_inits[i] = orig_init;
39314 }
39315 if (orig_decl)
39316 {
39317 if (!orig_declv)
39318 orig_declv = copy_node (declv);
39319 TREE_VEC_ELT (orig_declv, i) = orig_decl;
39320 }
39321 else if (orig_declv)
39322 TREE_VEC_ELT (orig_declv, i) = decl;
39323
39324 if (i == count - 1)
39325 break;
39326
39327 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
39328 in between the collapsed for loops to be still considered perfectly
39329 nested. Hopefully the final version clarifies this.
39330 For now handle (multiple) {'s and empty statements. */
39331 cp_parser_parse_tentatively (parser);
39332 for (;;)
39333 {
39334 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
39335 break;
39336 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
39337 {
39338 cp_lexer_consume_token (parser->lexer);
39339 bracecount++;
39340 }
39341 else if (bracecount
39342 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
39343 cp_lexer_consume_token (parser->lexer);
39344 else
39345 {
39346 loc = cp_lexer_peek_token (parser->lexer)->location;
39347 error_at (loc, "not enough for loops to collapse");
39348 collapse_err = true;
39349 cp_parser_abort_tentative_parse (parser);
39350 declv = NULL_TREE;
39351 break;
39352 }
39353 }
39354
39355 if (declv)
39356 {
39357 cp_parser_parse_definitely (parser);
39358 nbraces += bracecount;
39359 }
39360 }
39361
39362 if (nbraces)
39363 if_p = NULL;
39364
39365 /* Note that we saved the original contents of this flag when we entered
39366 the structured block, and so we don't need to re-save it here. */
39367 parser->in_statement = IN_OMP_FOR;
39368
39369 /* Note that the grammar doesn't call for a structured block here,
39370 though the loop as a whole is a structured block. */
39371 if (orig_declv)
39372 {
39373 body = begin_omp_structured_block ();
39374 for (i = 0; i < count; i++)
39375 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
39376 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
39377 TREE_VEC_ELT (declv, i));
39378 }
39379 else
39380 body = push_stmt_list ();
39381 if (inscan)
39382 cp_parser_omp_scan_loop_body (parser);
39383 else
39384 cp_parser_statement (parser, NULL_TREE, false, if_p);
39385 if (orig_declv)
39386 body = finish_omp_structured_block (body);
39387 else
39388 body = pop_stmt_list (body);
39389
39390 if (declv == NULL_TREE)
39391 ret = NULL_TREE;
39392 else
39393 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
39394 incrv, body, pre_body, &orig_inits, clauses);
39395
39396 while (nbraces)
39397 {
39398 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
39399 {
39400 cp_lexer_consume_token (parser->lexer);
39401 nbraces--;
39402 }
39403 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
39404 cp_lexer_consume_token (parser->lexer);
39405 else
39406 {
39407 if (!collapse_err)
39408 {
39409 error_at (cp_lexer_peek_token (parser->lexer)->location,
39410 "collapsed loops not perfectly nested");
39411 }
39412 collapse_err = true;
39413 cp_parser_statement_seq_opt (parser, NULL);
39414 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
39415 break;
39416 }
39417 }
39418
39419 while (!for_block->is_empty ())
39420 {
39421 tree t = for_block->pop ();
39422 if (TREE_CODE (t) == STATEMENT_LIST)
39423 add_stmt (pop_stmt_list (t));
39424 else
39425 add_stmt (t);
39426 }
39427
39428 return ret;
39429 }
39430
39431 /* Helper function for OpenMP parsing, split clauses and call
39432 finish_omp_clauses on each of the set of clauses afterwards. */
39433
39434 static void
39435 cp_omp_split_clauses (location_t loc, enum tree_code code,
39436 omp_clause_mask mask, tree clauses, tree *cclauses)
39437 {
39438 int i;
39439 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
39440 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
39441 if (cclauses[i])
39442 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
39443 }
39444
39445 /* OpenMP 5.0:
39446 #pragma omp loop loop-clause[optseq] new-line
39447 for-loop */
39448
39449 #define OMP_LOOP_CLAUSE_MASK \
39450 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
39455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39456
39457 static tree
39458 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
39459 char *p_name, omp_clause_mask mask, tree *cclauses,
39460 bool *if_p)
39461 {
39462 tree clauses, sb, ret;
39463 unsigned int save;
39464 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39465
39466 strcat (p_name, " loop");
39467 mask |= OMP_LOOP_CLAUSE_MASK;
39468
39469 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39470 cclauses == NULL);
39471 if (cclauses)
39472 {
39473 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
39474 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
39475 }
39476
39477 keep_next_level (true);
39478 sb = begin_omp_structured_block ();
39479 save = cp_parser_begin_omp_structured_block (parser);
39480
39481 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
39482
39483 cp_parser_end_omp_structured_block (parser, save);
39484 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39485
39486 return ret;
39487 }
39488
39489 /* OpenMP 4.0:
39490 #pragma omp simd simd-clause[optseq] new-line
39491 for-loop */
39492
39493 #define OMP_SIMD_CLAUSE_MASK \
39494 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
39495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
39504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39505
39506 static tree
39507 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
39508 char *p_name, omp_clause_mask mask, tree *cclauses,
39509 bool *if_p)
39510 {
39511 tree clauses, sb, ret;
39512 unsigned int save;
39513 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39514
39515 strcat (p_name, " simd");
39516 mask |= OMP_SIMD_CLAUSE_MASK;
39517
39518 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39519 cclauses == NULL);
39520 if (cclauses)
39521 {
39522 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
39523 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
39524 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
39525 OMP_CLAUSE_ORDERED);
39526 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
39527 {
39528 error_at (OMP_CLAUSE_LOCATION (c),
39529 "%<ordered%> clause with parameter may not be specified "
39530 "on %qs construct", p_name);
39531 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
39532 }
39533 }
39534
39535 keep_next_level (true);
39536 sb = begin_omp_structured_block ();
39537 save = cp_parser_begin_omp_structured_block (parser);
39538
39539 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
39540
39541 cp_parser_end_omp_structured_block (parser, save);
39542 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39543
39544 return ret;
39545 }
39546
39547 /* OpenMP 2.5:
39548 #pragma omp for for-clause[optseq] new-line
39549 for-loop
39550
39551 OpenMP 4.0:
39552 #pragma omp for simd for-simd-clause[optseq] new-line
39553 for-loop */
39554
39555 #define OMP_FOR_CLAUSE_MASK \
39556 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
39562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
39563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
39564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39566
39567 static tree
39568 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
39569 char *p_name, omp_clause_mask mask, tree *cclauses,
39570 bool *if_p)
39571 {
39572 tree clauses, sb, ret;
39573 unsigned int save;
39574 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39575
39576 strcat (p_name, " for");
39577 mask |= OMP_FOR_CLAUSE_MASK;
39578 /* parallel for{, simd} disallows nowait clause, but for
39579 target {teams distribute ,}parallel for{, simd} it should be accepted. */
39580 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
39581 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
39582 /* Composite distribute parallel for{, simd} disallows ordered clause. */
39583 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
39584 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
39585
39586 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39587 {
39588 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39589 const char *p = IDENTIFIER_POINTER (id);
39590
39591 if (strcmp (p, "simd") == 0)
39592 {
39593 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39594 if (cclauses == NULL)
39595 cclauses = cclauses_buf;
39596
39597 cp_lexer_consume_token (parser->lexer);
39598 if (!flag_openmp) /* flag_openmp_simd */
39599 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39600 cclauses, if_p);
39601 sb = begin_omp_structured_block ();
39602 save = cp_parser_begin_omp_structured_block (parser);
39603 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39604 cclauses, if_p);
39605 cp_parser_end_omp_structured_block (parser, save);
39606 tree body = finish_omp_structured_block (sb);
39607 if (ret == NULL)
39608 return ret;
39609 ret = make_node (OMP_FOR);
39610 TREE_TYPE (ret) = void_type_node;
39611 OMP_FOR_BODY (ret) = body;
39612 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
39613 SET_EXPR_LOCATION (ret, loc);
39614 add_stmt (ret);
39615 return ret;
39616 }
39617 }
39618 if (!flag_openmp) /* flag_openmp_simd */
39619 {
39620 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39621 return NULL_TREE;
39622 }
39623
39624 /* Composite distribute parallel for disallows linear clause. */
39625 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
39626 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
39627
39628 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39629 cclauses == NULL);
39630 if (cclauses)
39631 {
39632 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
39633 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
39634 }
39635
39636 keep_next_level (true);
39637 sb = begin_omp_structured_block ();
39638 save = cp_parser_begin_omp_structured_block (parser);
39639
39640 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
39641
39642 cp_parser_end_omp_structured_block (parser, save);
39643 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39644
39645 return ret;
39646 }
39647
39648 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
39649 omp_clause_mask, tree *, bool *);
39650
39651 /* OpenMP 2.5:
39652 # pragma omp master new-line
39653 structured-block */
39654
39655 static tree
39656 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
39657 char *p_name, omp_clause_mask mask, tree *cclauses,
39658 bool *if_p)
39659 {
39660 tree clauses, sb, ret;
39661 unsigned int save;
39662 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39663
39664 strcat (p_name, " master");
39665
39666 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39667 {
39668 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39669 const char *p = IDENTIFIER_POINTER (id);
39670
39671 if (strcmp (p, "taskloop") == 0)
39672 {
39673 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39674 if (cclauses == NULL)
39675 cclauses = cclauses_buf;
39676
39677 cp_lexer_consume_token (parser->lexer);
39678 if (!flag_openmp) /* flag_openmp_simd */
39679 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
39680 cclauses, if_p);
39681 sb = begin_omp_structured_block ();
39682 save = cp_parser_begin_omp_structured_block (parser);
39683 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
39684 cclauses, if_p);
39685 cp_parser_end_omp_structured_block (parser, save);
39686 tree body = finish_omp_structured_block (sb);
39687 if (ret == NULL)
39688 return ret;
39689 return c_finish_omp_master (loc, body);
39690 }
39691 }
39692 if (!flag_openmp) /* flag_openmp_simd */
39693 {
39694 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39695 return NULL_TREE;
39696 }
39697
39698 if (cclauses)
39699 {
39700 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39701 false);
39702 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
39703 }
39704 else
39705 cp_parser_require_pragma_eol (parser, pragma_tok);
39706
39707 return c_finish_omp_master (loc,
39708 cp_parser_omp_structured_block (parser, if_p));
39709 }
39710
39711 /* OpenMP 2.5:
39712 # pragma omp ordered new-line
39713 structured-block
39714
39715 OpenMP 4.5:
39716 # pragma omp ordered ordered-clauses new-line
39717 structured-block */
39718
39719 #define OMP_ORDERED_CLAUSE_MASK \
39720 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
39721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
39722
39723 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
39724 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
39725
39726 static bool
39727 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
39728 enum pragma_context context, bool *if_p)
39729 {
39730 location_t loc = pragma_tok->location;
39731
39732 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39733 {
39734 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39735 const char *p = IDENTIFIER_POINTER (id);
39736
39737 if (strcmp (p, "depend") == 0)
39738 {
39739 if (!flag_openmp) /* flag_openmp_simd */
39740 {
39741 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39742 return false;
39743 }
39744 if (context == pragma_stmt)
39745 {
39746 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
39747 "%<depend%> clause may only be used in compound "
39748 "statements");
39749 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39750 return false;
39751 }
39752 tree clauses
39753 = cp_parser_omp_all_clauses (parser,
39754 OMP_ORDERED_DEPEND_CLAUSE_MASK,
39755 "#pragma omp ordered", pragma_tok);
39756 c_finish_omp_ordered (loc, clauses, NULL_TREE);
39757 return false;
39758 }
39759 }
39760
39761 tree clauses
39762 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
39763 "#pragma omp ordered", pragma_tok);
39764
39765 if (!flag_openmp /* flag_openmp_simd */
39766 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
39767 return false;
39768
39769 c_finish_omp_ordered (loc, clauses,
39770 cp_parser_omp_structured_block (parser, if_p));
39771 return true;
39772 }
39773
39774 /* OpenMP 2.5:
39775
39776 section-scope:
39777 { section-sequence }
39778
39779 section-sequence:
39780 section-directive[opt] structured-block
39781 section-sequence section-directive structured-block */
39782
39783 static tree
39784 cp_parser_omp_sections_scope (cp_parser *parser)
39785 {
39786 tree stmt, substmt;
39787 bool error_suppress = false;
39788 cp_token *tok;
39789
39790 matching_braces braces;
39791 if (!braces.require_open (parser))
39792 return NULL_TREE;
39793
39794 stmt = push_stmt_list ();
39795
39796 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
39797 != PRAGMA_OMP_SECTION)
39798 {
39799 substmt = cp_parser_omp_structured_block (parser, NULL);
39800 substmt = build1 (OMP_SECTION, void_type_node, substmt);
39801 add_stmt (substmt);
39802 }
39803
39804 while (1)
39805 {
39806 tok = cp_lexer_peek_token (parser->lexer);
39807 if (tok->type == CPP_CLOSE_BRACE)
39808 break;
39809 if (tok->type == CPP_EOF)
39810 break;
39811
39812 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
39813 {
39814 cp_lexer_consume_token (parser->lexer);
39815 cp_parser_require_pragma_eol (parser, tok);
39816 error_suppress = false;
39817 }
39818 else if (!error_suppress)
39819 {
39820 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
39821 error_suppress = true;
39822 }
39823
39824 substmt = cp_parser_omp_structured_block (parser, NULL);
39825 substmt = build1 (OMP_SECTION, void_type_node, substmt);
39826 add_stmt (substmt);
39827 }
39828 braces.require_close (parser);
39829
39830 substmt = pop_stmt_list (stmt);
39831
39832 stmt = make_node (OMP_SECTIONS);
39833 TREE_TYPE (stmt) = void_type_node;
39834 OMP_SECTIONS_BODY (stmt) = substmt;
39835
39836 add_stmt (stmt);
39837 return stmt;
39838 }
39839
39840 /* OpenMP 2.5:
39841 # pragma omp sections sections-clause[optseq] newline
39842 sections-scope */
39843
39844 #define OMP_SECTIONS_CLAUSE_MASK \
39845 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39850
39851 static tree
39852 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
39853 char *p_name, omp_clause_mask mask, tree *cclauses)
39854 {
39855 tree clauses, ret;
39856 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39857
39858 strcat (p_name, " sections");
39859 mask |= OMP_SECTIONS_CLAUSE_MASK;
39860 if (cclauses)
39861 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
39862
39863 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39864 cclauses == NULL);
39865 if (cclauses)
39866 {
39867 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
39868 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
39869 }
39870
39871 ret = cp_parser_omp_sections_scope (parser);
39872 if (ret)
39873 OMP_SECTIONS_CLAUSES (ret) = clauses;
39874
39875 return ret;
39876 }
39877
39878 /* OpenMP 2.5:
39879 # pragma omp parallel parallel-clause[optseq] new-line
39880 structured-block
39881 # pragma omp parallel for parallel-for-clause[optseq] new-line
39882 structured-block
39883 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
39884 structured-block
39885
39886 OpenMP 4.0:
39887 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
39888 structured-block */
39889
39890 #define OMP_PARALLEL_CLAUSE_MASK \
39891 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
39897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
39899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
39900
39901 static tree
39902 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
39903 char *p_name, omp_clause_mask mask, tree *cclauses,
39904 bool *if_p)
39905 {
39906 tree stmt, clauses, block;
39907 unsigned int save;
39908 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39909
39910 strcat (p_name, " parallel");
39911 mask |= OMP_PARALLEL_CLAUSE_MASK;
39912 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
39913 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
39914 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
39915 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
39916
39917 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
39918 {
39919 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39920 if (cclauses == NULL)
39921 cclauses = cclauses_buf;
39922
39923 cp_lexer_consume_token (parser->lexer);
39924 if (!flag_openmp) /* flag_openmp_simd */
39925 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
39926 if_p);
39927 block = begin_omp_parallel ();
39928 save = cp_parser_begin_omp_structured_block (parser);
39929 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
39930 if_p);
39931 cp_parser_end_omp_structured_block (parser, save);
39932 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
39933 block);
39934 if (ret == NULL_TREE)
39935 return ret;
39936 OMP_PARALLEL_COMBINED (stmt) = 1;
39937 return stmt;
39938 }
39939 /* When combined with distribute, parallel has to be followed by for.
39940 #pragma omp target parallel is allowed though. */
39941 else if (cclauses
39942 && (mask & (OMP_CLAUSE_MASK_1
39943 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
39944 {
39945 error_at (loc, "expected %<for%> after %qs", p_name);
39946 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39947 return NULL_TREE;
39948 }
39949 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39950 {
39951 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39952 const char *p = IDENTIFIER_POINTER (id);
39953 if (cclauses == NULL && strcmp (p, "master") == 0)
39954 {
39955 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39956 cclauses = cclauses_buf;
39957
39958 cp_lexer_consume_token (parser->lexer);
39959 block = begin_omp_parallel ();
39960 save = cp_parser_begin_omp_structured_block (parser);
39961 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
39962 cclauses, if_p);
39963 cp_parser_end_omp_structured_block (parser, save);
39964 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
39965 block);
39966 if (ret == NULL_TREE)
39967 return ret;
39968 OMP_PARALLEL_COMBINED (stmt) = 1;
39969 return stmt;
39970 }
39971 else if (strcmp (p, "loop") == 0)
39972 {
39973 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39974 if (cclauses == NULL)
39975 cclauses = cclauses_buf;
39976
39977 cp_lexer_consume_token (parser->lexer);
39978 if (!flag_openmp) /* flag_openmp_simd */
39979 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
39980 cclauses, if_p);
39981 block = begin_omp_parallel ();
39982 save = cp_parser_begin_omp_structured_block (parser);
39983 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
39984 cclauses, if_p);
39985 cp_parser_end_omp_structured_block (parser, save);
39986 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
39987 block);
39988 if (ret == NULL_TREE)
39989 return ret;
39990 OMP_PARALLEL_COMBINED (stmt) = 1;
39991 return stmt;
39992 }
39993 else if (!flag_openmp) /* flag_openmp_simd */
39994 {
39995 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39996 return NULL_TREE;
39997 }
39998 else if (cclauses == NULL && strcmp (p, "sections") == 0)
39999 {
40000 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40001 cclauses = cclauses_buf;
40002
40003 cp_lexer_consume_token (parser->lexer);
40004 block = begin_omp_parallel ();
40005 save = cp_parser_begin_omp_structured_block (parser);
40006 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
40007 cp_parser_end_omp_structured_block (parser, save);
40008 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
40009 block);
40010 OMP_PARALLEL_COMBINED (stmt) = 1;
40011 return stmt;
40012 }
40013 }
40014 else if (!flag_openmp) /* flag_openmp_simd */
40015 {
40016 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40017 return NULL_TREE;
40018 }
40019
40020 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40021 cclauses == NULL);
40022 if (cclauses)
40023 {
40024 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
40025 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
40026 }
40027
40028 block = begin_omp_parallel ();
40029 save = cp_parser_begin_omp_structured_block (parser);
40030 cp_parser_statement (parser, NULL_TREE, false, if_p);
40031 cp_parser_end_omp_structured_block (parser, save);
40032 stmt = finish_omp_parallel (clauses, block);
40033 return stmt;
40034 }
40035
40036 /* OpenMP 2.5:
40037 # pragma omp single single-clause[optseq] new-line
40038 structured-block */
40039
40040 #define OMP_SINGLE_CLAUSE_MASK \
40041 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
40044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40045
40046 static tree
40047 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40048 {
40049 tree stmt = make_node (OMP_SINGLE);
40050 TREE_TYPE (stmt) = void_type_node;
40051 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40052
40053 OMP_SINGLE_CLAUSES (stmt)
40054 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
40055 "#pragma omp single", pragma_tok);
40056 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
40057
40058 return add_stmt (stmt);
40059 }
40060
40061 /* OpenMP 3.0:
40062 # pragma omp task task-clause[optseq] new-line
40063 structured-block */
40064
40065 #define OMP_TASK_CLAUSE_MASK \
40066 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
40068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
40069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
40073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
40074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
40076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
40077
40078 static tree
40079 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40080 {
40081 tree clauses, block;
40082 unsigned int save;
40083
40084 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
40085 "#pragma omp task", pragma_tok);
40086 block = begin_omp_task ();
40087 save = cp_parser_begin_omp_structured_block (parser);
40088 cp_parser_statement (parser, NULL_TREE, false, if_p);
40089 cp_parser_end_omp_structured_block (parser, save);
40090 return finish_omp_task (clauses, block);
40091 }
40092
40093 /* OpenMP 3.0:
40094 # pragma omp taskwait new-line
40095
40096 OpenMP 5.0:
40097 # pragma omp taskwait taskwait-clause[opt] new-line */
40098
40099 #define OMP_TASKWAIT_CLAUSE_MASK \
40100 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
40101
40102 static void
40103 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
40104 {
40105 tree clauses
40106 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
40107 "#pragma omp taskwait", pragma_tok);
40108
40109 if (clauses)
40110 {
40111 tree stmt = make_node (OMP_TASK);
40112 TREE_TYPE (stmt) = void_node;
40113 OMP_TASK_CLAUSES (stmt) = clauses;
40114 OMP_TASK_BODY (stmt) = NULL_TREE;
40115 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40116 add_stmt (stmt);
40117 }
40118 else
40119 finish_omp_taskwait ();
40120 }
40121
40122 /* OpenMP 3.1:
40123 # pragma omp taskyield new-line */
40124
40125 static void
40126 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
40127 {
40128 cp_parser_require_pragma_eol (parser, pragma_tok);
40129 finish_omp_taskyield ();
40130 }
40131
40132 /* OpenMP 4.0:
40133 # pragma omp taskgroup new-line
40134 structured-block
40135
40136 OpenMP 5.0:
40137 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
40138
40139 #define OMP_TASKGROUP_CLAUSE_MASK \
40140 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
40141
40142 static tree
40143 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40144 {
40145 tree clauses
40146 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
40147 "#pragma omp taskgroup", pragma_tok);
40148 return c_finish_omp_taskgroup (input_location,
40149 cp_parser_omp_structured_block (parser,
40150 if_p),
40151 clauses);
40152 }
40153
40154
40155 /* OpenMP 2.5:
40156 # pragma omp threadprivate (variable-list) */
40157
40158 static void
40159 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
40160 {
40161 tree vars;
40162
40163 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
40164 cp_parser_require_pragma_eol (parser, pragma_tok);
40165
40166 finish_omp_threadprivate (vars);
40167 }
40168
40169 /* OpenMP 4.0:
40170 # pragma omp cancel cancel-clause[optseq] new-line */
40171
40172 #define OMP_CANCEL_CLAUSE_MASK \
40173 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
40174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
40175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
40176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
40177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
40178
40179 static void
40180 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
40181 {
40182 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
40183 "#pragma omp cancel", pragma_tok);
40184 finish_omp_cancel (clauses);
40185 }
40186
40187 /* OpenMP 4.0:
40188 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
40189
40190 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
40191 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
40192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
40193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
40194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
40195
40196 static void
40197 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
40198 enum pragma_context context)
40199 {
40200 tree clauses;
40201 bool point_seen = false;
40202
40203 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40204 {
40205 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40206 const char *p = IDENTIFIER_POINTER (id);
40207
40208 if (strcmp (p, "point") == 0)
40209 {
40210 cp_lexer_consume_token (parser->lexer);
40211 point_seen = true;
40212 }
40213 }
40214 if (!point_seen)
40215 {
40216 cp_parser_error (parser, "expected %<point%>");
40217 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40218 return;
40219 }
40220
40221 if (context != pragma_compound)
40222 {
40223 if (context == pragma_stmt)
40224 error_at (pragma_tok->location,
40225 "%<#pragma %s%> may only be used in compound statements",
40226 "omp cancellation point");
40227 else
40228 cp_parser_error (parser, "expected declaration specifiers");
40229 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40230 return;
40231 }
40232
40233 clauses = cp_parser_omp_all_clauses (parser,
40234 OMP_CANCELLATION_POINT_CLAUSE_MASK,
40235 "#pragma omp cancellation point",
40236 pragma_tok);
40237 finish_omp_cancellation_point (clauses);
40238 }
40239
40240 /* OpenMP 4.0:
40241 #pragma omp distribute distribute-clause[optseq] new-line
40242 for-loop */
40243
40244 #define OMP_DISTRIBUTE_CLAUSE_MASK \
40245 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
40249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
40250
40251 static tree
40252 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
40253 char *p_name, omp_clause_mask mask, tree *cclauses,
40254 bool *if_p)
40255 {
40256 tree clauses, sb, ret;
40257 unsigned int save;
40258 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40259
40260 strcat (p_name, " distribute");
40261 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
40262
40263 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40264 {
40265 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40266 const char *p = IDENTIFIER_POINTER (id);
40267 bool simd = false;
40268 bool parallel = false;
40269
40270 if (strcmp (p, "simd") == 0)
40271 simd = true;
40272 else
40273 parallel = strcmp (p, "parallel") == 0;
40274 if (parallel || simd)
40275 {
40276 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40277 if (cclauses == NULL)
40278 cclauses = cclauses_buf;
40279 cp_lexer_consume_token (parser->lexer);
40280 if (!flag_openmp) /* flag_openmp_simd */
40281 {
40282 if (simd)
40283 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40284 cclauses, if_p);
40285 else
40286 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
40287 cclauses, if_p);
40288 }
40289 sb = begin_omp_structured_block ();
40290 save = cp_parser_begin_omp_structured_block (parser);
40291 if (simd)
40292 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40293 cclauses, if_p);
40294 else
40295 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
40296 cclauses, if_p);
40297 cp_parser_end_omp_structured_block (parser, save);
40298 tree body = finish_omp_structured_block (sb);
40299 if (ret == NULL)
40300 return ret;
40301 ret = make_node (OMP_DISTRIBUTE);
40302 TREE_TYPE (ret) = void_type_node;
40303 OMP_FOR_BODY (ret) = body;
40304 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
40305 SET_EXPR_LOCATION (ret, loc);
40306 add_stmt (ret);
40307 return ret;
40308 }
40309 }
40310 if (!flag_openmp) /* flag_openmp_simd */
40311 {
40312 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40313 return NULL_TREE;
40314 }
40315
40316 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40317 cclauses == NULL);
40318 if (cclauses)
40319 {
40320 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
40321 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
40322 }
40323
40324 keep_next_level (true);
40325 sb = begin_omp_structured_block ();
40326 save = cp_parser_begin_omp_structured_block (parser);
40327
40328 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
40329
40330 cp_parser_end_omp_structured_block (parser, save);
40331 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40332
40333 return ret;
40334 }
40335
40336 /* OpenMP 4.0:
40337 # pragma omp teams teams-clause[optseq] new-line
40338 structured-block */
40339
40340 #define OMP_TEAMS_CLAUSE_MASK \
40341 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
40346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
40347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
40348
40349 static tree
40350 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
40351 char *p_name, omp_clause_mask mask, tree *cclauses,
40352 bool *if_p)
40353 {
40354 tree clauses, sb, ret;
40355 unsigned int save;
40356 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40357
40358 strcat (p_name, " teams");
40359 mask |= OMP_TEAMS_CLAUSE_MASK;
40360
40361 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40362 {
40363 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40364 const char *p = IDENTIFIER_POINTER (id);
40365 if (strcmp (p, "distribute") == 0)
40366 {
40367 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40368 if (cclauses == NULL)
40369 cclauses = cclauses_buf;
40370
40371 cp_lexer_consume_token (parser->lexer);
40372 if (!flag_openmp) /* flag_openmp_simd */
40373 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
40374 cclauses, if_p);
40375 keep_next_level (true);
40376 sb = begin_omp_structured_block ();
40377 save = cp_parser_begin_omp_structured_block (parser);
40378 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
40379 cclauses, if_p);
40380 cp_parser_end_omp_structured_block (parser, save);
40381 tree body = finish_omp_structured_block (sb);
40382 if (ret == NULL)
40383 return ret;
40384 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
40385 ret = make_node (OMP_TEAMS);
40386 TREE_TYPE (ret) = void_type_node;
40387 OMP_TEAMS_CLAUSES (ret) = clauses;
40388 OMP_TEAMS_BODY (ret) = body;
40389 OMP_TEAMS_COMBINED (ret) = 1;
40390 SET_EXPR_LOCATION (ret, loc);
40391 return add_stmt (ret);
40392 }
40393 else if (strcmp (p, "loop") == 0)
40394 {
40395 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40396 if (cclauses == NULL)
40397 cclauses = cclauses_buf;
40398
40399 cp_lexer_consume_token (parser->lexer);
40400 if (!flag_openmp) /* flag_openmp_simd */
40401 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
40402 cclauses, if_p);
40403 keep_next_level (true);
40404 sb = begin_omp_structured_block ();
40405 save = cp_parser_begin_omp_structured_block (parser);
40406 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
40407 cclauses, if_p);
40408 cp_parser_end_omp_structured_block (parser, save);
40409 tree body = finish_omp_structured_block (sb);
40410 if (ret == NULL)
40411 return ret;
40412 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
40413 ret = make_node (OMP_TEAMS);
40414 TREE_TYPE (ret) = void_type_node;
40415 OMP_TEAMS_CLAUSES (ret) = clauses;
40416 OMP_TEAMS_BODY (ret) = body;
40417 OMP_TEAMS_COMBINED (ret) = 1;
40418 SET_EXPR_LOCATION (ret, loc);
40419 return add_stmt (ret);
40420 }
40421 }
40422 if (!flag_openmp) /* flag_openmp_simd */
40423 {
40424 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40425 return NULL_TREE;
40426 }
40427
40428 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40429 cclauses == NULL);
40430 if (cclauses)
40431 {
40432 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
40433 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
40434 }
40435
40436 tree stmt = make_node (OMP_TEAMS);
40437 TREE_TYPE (stmt) = void_type_node;
40438 OMP_TEAMS_CLAUSES (stmt) = clauses;
40439 keep_next_level (true);
40440 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
40441 SET_EXPR_LOCATION (stmt, loc);
40442
40443 return add_stmt (stmt);
40444 }
40445
40446 /* OpenMP 4.0:
40447 # pragma omp target data target-data-clause[optseq] new-line
40448 structured-block */
40449
40450 #define OMP_TARGET_DATA_CLAUSE_MASK \
40451 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
40455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
40456
40457 static tree
40458 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40459 {
40460 tree clauses
40461 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
40462 "#pragma omp target data", pragma_tok);
40463 int map_seen = 0;
40464 for (tree *pc = &clauses; *pc;)
40465 {
40466 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
40467 switch (OMP_CLAUSE_MAP_KIND (*pc))
40468 {
40469 case GOMP_MAP_TO:
40470 case GOMP_MAP_ALWAYS_TO:
40471 case GOMP_MAP_FROM:
40472 case GOMP_MAP_ALWAYS_FROM:
40473 case GOMP_MAP_TOFROM:
40474 case GOMP_MAP_ALWAYS_TOFROM:
40475 case GOMP_MAP_ALLOC:
40476 map_seen = 3;
40477 break;
40478 case GOMP_MAP_FIRSTPRIVATE_POINTER:
40479 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
40480 case GOMP_MAP_ALWAYS_POINTER:
40481 break;
40482 default:
40483 map_seen |= 1;
40484 error_at (OMP_CLAUSE_LOCATION (*pc),
40485 "%<#pragma omp target data%> with map-type other "
40486 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
40487 "on %<map%> clause");
40488 *pc = OMP_CLAUSE_CHAIN (*pc);
40489 continue;
40490 }
40491 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
40492 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
40493 map_seen = 3;
40494 pc = &OMP_CLAUSE_CHAIN (*pc);
40495 }
40496
40497 if (map_seen != 3)
40498 {
40499 if (map_seen == 0)
40500 error_at (pragma_tok->location,
40501 "%<#pragma omp target data%> must contain at least "
40502 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
40503 "clause");
40504 return NULL_TREE;
40505 }
40506
40507 tree stmt = make_node (OMP_TARGET_DATA);
40508 TREE_TYPE (stmt) = void_type_node;
40509 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
40510
40511 keep_next_level (true);
40512 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
40513
40514 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40515 return add_stmt (stmt);
40516 }
40517
40518 /* OpenMP 4.5:
40519 # pragma omp target enter data target-enter-data-clause[optseq] new-line
40520 structured-block */
40521
40522 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
40523 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40528
40529 static tree
40530 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
40531 enum pragma_context context)
40532 {
40533 bool data_seen = false;
40534 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40535 {
40536 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40537 const char *p = IDENTIFIER_POINTER (id);
40538
40539 if (strcmp (p, "data") == 0)
40540 {
40541 cp_lexer_consume_token (parser->lexer);
40542 data_seen = true;
40543 }
40544 }
40545 if (!data_seen)
40546 {
40547 cp_parser_error (parser, "expected %<data%>");
40548 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40549 return NULL_TREE;
40550 }
40551
40552 if (context == pragma_stmt)
40553 {
40554 error_at (pragma_tok->location,
40555 "%<#pragma %s%> may only be used in compound statements",
40556 "omp target enter data");
40557 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40558 return NULL_TREE;
40559 }
40560
40561 tree clauses
40562 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
40563 "#pragma omp target enter data", pragma_tok);
40564 int map_seen = 0;
40565 for (tree *pc = &clauses; *pc;)
40566 {
40567 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
40568 switch (OMP_CLAUSE_MAP_KIND (*pc))
40569 {
40570 case GOMP_MAP_TO:
40571 case GOMP_MAP_ALWAYS_TO:
40572 case GOMP_MAP_ALLOC:
40573 map_seen = 3;
40574 break;
40575 case GOMP_MAP_FIRSTPRIVATE_POINTER:
40576 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
40577 case GOMP_MAP_ALWAYS_POINTER:
40578 break;
40579 default:
40580 map_seen |= 1;
40581 error_at (OMP_CLAUSE_LOCATION (*pc),
40582 "%<#pragma omp target enter data%> with map-type other "
40583 "than %<to%> or %<alloc%> on %<map%> clause");
40584 *pc = OMP_CLAUSE_CHAIN (*pc);
40585 continue;
40586 }
40587 pc = &OMP_CLAUSE_CHAIN (*pc);
40588 }
40589
40590 if (map_seen != 3)
40591 {
40592 if (map_seen == 0)
40593 error_at (pragma_tok->location,
40594 "%<#pragma omp target enter data%> must contain at least "
40595 "one %<map%> clause");
40596 return NULL_TREE;
40597 }
40598
40599 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
40600 TREE_TYPE (stmt) = void_type_node;
40601 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
40602 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40603 return add_stmt (stmt);
40604 }
40605
40606 /* OpenMP 4.5:
40607 # pragma omp target exit data target-enter-data-clause[optseq] new-line
40608 structured-block */
40609
40610 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
40611 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40616
40617 static tree
40618 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
40619 enum pragma_context context)
40620 {
40621 bool data_seen = false;
40622 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40623 {
40624 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40625 const char *p = IDENTIFIER_POINTER (id);
40626
40627 if (strcmp (p, "data") == 0)
40628 {
40629 cp_lexer_consume_token (parser->lexer);
40630 data_seen = true;
40631 }
40632 }
40633 if (!data_seen)
40634 {
40635 cp_parser_error (parser, "expected %<data%>");
40636 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40637 return NULL_TREE;
40638 }
40639
40640 if (context == pragma_stmt)
40641 {
40642 error_at (pragma_tok->location,
40643 "%<#pragma %s%> may only be used in compound statements",
40644 "omp target exit data");
40645 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40646 return NULL_TREE;
40647 }
40648
40649 tree clauses
40650 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
40651 "#pragma omp target exit data", pragma_tok);
40652 int map_seen = 0;
40653 for (tree *pc = &clauses; *pc;)
40654 {
40655 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
40656 switch (OMP_CLAUSE_MAP_KIND (*pc))
40657 {
40658 case GOMP_MAP_FROM:
40659 case GOMP_MAP_ALWAYS_FROM:
40660 case GOMP_MAP_RELEASE:
40661 case GOMP_MAP_DELETE:
40662 map_seen = 3;
40663 break;
40664 case GOMP_MAP_FIRSTPRIVATE_POINTER:
40665 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
40666 case GOMP_MAP_ALWAYS_POINTER:
40667 break;
40668 default:
40669 map_seen |= 1;
40670 error_at (OMP_CLAUSE_LOCATION (*pc),
40671 "%<#pragma omp target exit data%> with map-type other "
40672 "than %<from%>, %<release%> or %<delete%> on %<map%>"
40673 " clause");
40674 *pc = OMP_CLAUSE_CHAIN (*pc);
40675 continue;
40676 }
40677 pc = &OMP_CLAUSE_CHAIN (*pc);
40678 }
40679
40680 if (map_seen != 3)
40681 {
40682 if (map_seen == 0)
40683 error_at (pragma_tok->location,
40684 "%<#pragma omp target exit data%> must contain at least "
40685 "one %<map%> clause");
40686 return NULL_TREE;
40687 }
40688
40689 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
40690 TREE_TYPE (stmt) = void_type_node;
40691 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
40692 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40693 return add_stmt (stmt);
40694 }
40695
40696 /* OpenMP 4.0:
40697 # pragma omp target update target-update-clause[optseq] new-line */
40698
40699 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
40700 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
40701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
40702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40706
40707 static bool
40708 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
40709 enum pragma_context context)
40710 {
40711 if (context == pragma_stmt)
40712 {
40713 error_at (pragma_tok->location,
40714 "%<#pragma %s%> may only be used in compound statements",
40715 "omp target update");
40716 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40717 return false;
40718 }
40719
40720 tree clauses
40721 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
40722 "#pragma omp target update", pragma_tok);
40723 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
40724 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
40725 {
40726 error_at (pragma_tok->location,
40727 "%<#pragma omp target update%> must contain at least one "
40728 "%<from%> or %<to%> clauses");
40729 return false;
40730 }
40731
40732 tree stmt = make_node (OMP_TARGET_UPDATE);
40733 TREE_TYPE (stmt) = void_type_node;
40734 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
40735 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40736 add_stmt (stmt);
40737 return false;
40738 }
40739
40740 /* OpenMP 4.0:
40741 # pragma omp target target-clause[optseq] new-line
40742 structured-block */
40743
40744 #define OMP_TARGET_CLAUSE_MASK \
40745 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
40750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
40753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
40754
40755 static bool
40756 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
40757 enum pragma_context context, bool *if_p)
40758 {
40759 tree *pc = NULL, stmt;
40760
40761 if (flag_openmp)
40762 omp_requires_mask
40763 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
40764
40765 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40766 {
40767 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40768 const char *p = IDENTIFIER_POINTER (id);
40769 enum tree_code ccode = ERROR_MARK;
40770
40771 if (strcmp (p, "teams") == 0)
40772 ccode = OMP_TEAMS;
40773 else if (strcmp (p, "parallel") == 0)
40774 ccode = OMP_PARALLEL;
40775 else if (strcmp (p, "simd") == 0)
40776 ccode = OMP_SIMD;
40777 if (ccode != ERROR_MARK)
40778 {
40779 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
40780 char p_name[sizeof ("#pragma omp target teams distribute "
40781 "parallel for simd")];
40782
40783 cp_lexer_consume_token (parser->lexer);
40784 strcpy (p_name, "#pragma omp target");
40785 if (!flag_openmp) /* flag_openmp_simd */
40786 {
40787 tree stmt;
40788 switch (ccode)
40789 {
40790 case OMP_TEAMS:
40791 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
40792 OMP_TARGET_CLAUSE_MASK,
40793 cclauses, if_p);
40794 break;
40795 case OMP_PARALLEL:
40796 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
40797 OMP_TARGET_CLAUSE_MASK,
40798 cclauses, if_p);
40799 break;
40800 case OMP_SIMD:
40801 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
40802 OMP_TARGET_CLAUSE_MASK,
40803 cclauses, if_p);
40804 break;
40805 default:
40806 gcc_unreachable ();
40807 }
40808 return stmt != NULL_TREE;
40809 }
40810 keep_next_level (true);
40811 tree sb = begin_omp_structured_block (), ret;
40812 unsigned save = cp_parser_begin_omp_structured_block (parser);
40813 switch (ccode)
40814 {
40815 case OMP_TEAMS:
40816 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
40817 OMP_TARGET_CLAUSE_MASK, cclauses,
40818 if_p);
40819 break;
40820 case OMP_PARALLEL:
40821 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
40822 OMP_TARGET_CLAUSE_MASK, cclauses,
40823 if_p);
40824 break;
40825 case OMP_SIMD:
40826 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
40827 OMP_TARGET_CLAUSE_MASK, cclauses,
40828 if_p);
40829 break;
40830 default:
40831 gcc_unreachable ();
40832 }
40833 cp_parser_end_omp_structured_block (parser, save);
40834 tree body = finish_omp_structured_block (sb);
40835 if (ret == NULL_TREE)
40836 return false;
40837 if (ccode == OMP_TEAMS && !processing_template_decl)
40838 {
40839 /* For combined target teams, ensure the num_teams and
40840 thread_limit clause expressions are evaluated on the host,
40841 before entering the target construct. */
40842 tree c;
40843 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
40844 c; c = OMP_CLAUSE_CHAIN (c))
40845 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
40846 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
40847 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
40848 {
40849 tree expr = OMP_CLAUSE_OPERAND (c, 0);
40850 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
40851 if (expr == error_mark_node)
40852 continue;
40853 tree tmp = TARGET_EXPR_SLOT (expr);
40854 add_stmt (expr);
40855 OMP_CLAUSE_OPERAND (c, 0) = expr;
40856 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
40857 OMP_CLAUSE_FIRSTPRIVATE);
40858 OMP_CLAUSE_DECL (tc) = tmp;
40859 OMP_CLAUSE_CHAIN (tc)
40860 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
40861 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
40862 }
40863 }
40864 tree stmt = make_node (OMP_TARGET);
40865 TREE_TYPE (stmt) = void_type_node;
40866 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
40867 OMP_TARGET_BODY (stmt) = body;
40868 OMP_TARGET_COMBINED (stmt) = 1;
40869 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40870 add_stmt (stmt);
40871 pc = &OMP_TARGET_CLAUSES (stmt);
40872 goto check_clauses;
40873 }
40874 else if (!flag_openmp) /* flag_openmp_simd */
40875 {
40876 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40877 return false;
40878 }
40879 else if (strcmp (p, "data") == 0)
40880 {
40881 cp_lexer_consume_token (parser->lexer);
40882 cp_parser_omp_target_data (parser, pragma_tok, if_p);
40883 return true;
40884 }
40885 else if (strcmp (p, "enter") == 0)
40886 {
40887 cp_lexer_consume_token (parser->lexer);
40888 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
40889 return false;
40890 }
40891 else if (strcmp (p, "exit") == 0)
40892 {
40893 cp_lexer_consume_token (parser->lexer);
40894 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
40895 return false;
40896 }
40897 else if (strcmp (p, "update") == 0)
40898 {
40899 cp_lexer_consume_token (parser->lexer);
40900 return cp_parser_omp_target_update (parser, pragma_tok, context);
40901 }
40902 }
40903 if (!flag_openmp) /* flag_openmp_simd */
40904 {
40905 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40906 return false;
40907 }
40908
40909 stmt = make_node (OMP_TARGET);
40910 TREE_TYPE (stmt) = void_type_node;
40911
40912 OMP_TARGET_CLAUSES (stmt)
40913 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
40914 "#pragma omp target", pragma_tok);
40915 pc = &OMP_TARGET_CLAUSES (stmt);
40916 keep_next_level (true);
40917 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
40918
40919 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40920 add_stmt (stmt);
40921
40922 check_clauses:
40923 while (*pc)
40924 {
40925 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
40926 switch (OMP_CLAUSE_MAP_KIND (*pc))
40927 {
40928 case GOMP_MAP_TO:
40929 case GOMP_MAP_ALWAYS_TO:
40930 case GOMP_MAP_FROM:
40931 case GOMP_MAP_ALWAYS_FROM:
40932 case GOMP_MAP_TOFROM:
40933 case GOMP_MAP_ALWAYS_TOFROM:
40934 case GOMP_MAP_ALLOC:
40935 case GOMP_MAP_FIRSTPRIVATE_POINTER:
40936 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
40937 case GOMP_MAP_ALWAYS_POINTER:
40938 break;
40939 default:
40940 error_at (OMP_CLAUSE_LOCATION (*pc),
40941 "%<#pragma omp target%> with map-type other "
40942 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
40943 "on %<map%> clause");
40944 *pc = OMP_CLAUSE_CHAIN (*pc);
40945 continue;
40946 }
40947 pc = &OMP_CLAUSE_CHAIN (*pc);
40948 }
40949 return true;
40950 }
40951
40952 /* OpenACC 2.0:
40953 # pragma acc cache (variable-list) new-line
40954 */
40955
40956 static tree
40957 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
40958 {
40959 tree stmt, clauses;
40960
40961 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
40962 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
40963
40964 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
40965
40966 stmt = make_node (OACC_CACHE);
40967 TREE_TYPE (stmt) = void_type_node;
40968 OACC_CACHE_CLAUSES (stmt) = clauses;
40969 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40970 add_stmt (stmt);
40971
40972 return stmt;
40973 }
40974
40975 /* OpenACC 2.0:
40976 # pragma acc data oacc-data-clause[optseq] new-line
40977 structured-block */
40978
40979 #define OACC_DATA_CLAUSE_MASK \
40980 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
40981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
40986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
40989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
40990
40991 static tree
40992 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40993 {
40994 tree stmt, clauses, block;
40995 unsigned int save;
40996
40997 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
40998 "#pragma acc data", pragma_tok);
40999
41000 block = begin_omp_parallel ();
41001 save = cp_parser_begin_omp_structured_block (parser);
41002 cp_parser_statement (parser, NULL_TREE, false, if_p);
41003 cp_parser_end_omp_structured_block (parser, save);
41004 stmt = finish_oacc_data (clauses, block);
41005 return stmt;
41006 }
41007
41008 /* OpenACC 2.0:
41009 # pragma acc host_data <clauses> new-line
41010 structured-block */
41011
41012 #define OACC_HOST_DATA_CLAUSE_MASK \
41013 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
41014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
41016
41017 static tree
41018 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41019 {
41020 tree stmt, clauses, block;
41021 unsigned int save;
41022
41023 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
41024 "#pragma acc host_data", pragma_tok);
41025
41026 block = begin_omp_parallel ();
41027 save = cp_parser_begin_omp_structured_block (parser);
41028 cp_parser_statement (parser, NULL_TREE, false, if_p);
41029 cp_parser_end_omp_structured_block (parser, save);
41030 stmt = finish_oacc_host_data (clauses, block);
41031 return stmt;
41032 }
41033
41034 /* OpenACC 2.0:
41035 # pragma acc declare oacc-data-clause[optseq] new-line
41036 */
41037
41038 #define OACC_DECLARE_CLAUSE_MASK \
41039 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
41045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
41046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
41047
41048 static tree
41049 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
41050 {
41051 tree clauses, stmt;
41052 bool error = false;
41053 bool found_in_scope = global_bindings_p ();
41054
41055 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
41056 "#pragma acc declare", pragma_tok, true);
41057
41058
41059 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
41060 {
41061 error_at (pragma_tok->location,
41062 "no valid clauses specified in %<#pragma acc declare%>");
41063 return NULL_TREE;
41064 }
41065
41066 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
41067 {
41068 location_t loc = OMP_CLAUSE_LOCATION (t);
41069 tree decl = OMP_CLAUSE_DECL (t);
41070 if (!DECL_P (decl))
41071 {
41072 error_at (loc, "array section in %<#pragma acc declare%>");
41073 error = true;
41074 continue;
41075 }
41076 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
41077 switch (OMP_CLAUSE_MAP_KIND (t))
41078 {
41079 case GOMP_MAP_FIRSTPRIVATE_POINTER:
41080 case GOMP_MAP_ALLOC:
41081 case GOMP_MAP_TO:
41082 case GOMP_MAP_FORCE_DEVICEPTR:
41083 case GOMP_MAP_DEVICE_RESIDENT:
41084 break;
41085
41086 case GOMP_MAP_LINK:
41087 if (!global_bindings_p ()
41088 && (TREE_STATIC (decl)
41089 || !DECL_EXTERNAL (decl)))
41090 {
41091 error_at (loc,
41092 "%qD must be a global variable in "
41093 "%<#pragma acc declare link%>",
41094 decl);
41095 error = true;
41096 continue;
41097 }
41098 break;
41099
41100 default:
41101 if (global_bindings_p ())
41102 {
41103 error_at (loc, "invalid OpenACC clause at file scope");
41104 error = true;
41105 continue;
41106 }
41107 if (DECL_EXTERNAL (decl))
41108 {
41109 error_at (loc,
41110 "invalid use of %<extern%> variable %qD "
41111 "in %<#pragma acc declare%>", decl);
41112 error = true;
41113 continue;
41114 }
41115 else if (TREE_PUBLIC (decl))
41116 {
41117 error_at (loc,
41118 "invalid use of %<global%> variable %qD "
41119 "in %<#pragma acc declare%>", decl);
41120 error = true;
41121 continue;
41122 }
41123 break;
41124 }
41125
41126 if (!found_in_scope)
41127 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
41128 if (d == decl)
41129 {
41130 found_in_scope = true;
41131 break;
41132 }
41133 if (!found_in_scope)
41134 {
41135 error_at (loc,
41136 "%qD must be a variable declared in the same scope as "
41137 "%<#pragma acc declare%>", decl);
41138 error = true;
41139 continue;
41140 }
41141
41142 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
41143 || lookup_attribute ("omp declare target link",
41144 DECL_ATTRIBUTES (decl)))
41145 {
41146 error_at (loc, "variable %qD used more than once with "
41147 "%<#pragma acc declare%>", decl);
41148 error = true;
41149 continue;
41150 }
41151
41152 if (!error)
41153 {
41154 tree id;
41155
41156 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
41157 id = get_identifier ("omp declare target link");
41158 else
41159 id = get_identifier ("omp declare target");
41160
41161 DECL_ATTRIBUTES (decl)
41162 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
41163 if (current_binding_level->kind == sk_namespace)
41164 {
41165 symtab_node *node = symtab_node::get (decl);
41166 if (node != NULL)
41167 {
41168 node->offloadable = 1;
41169 if (ENABLE_OFFLOADING)
41170 {
41171 g->have_offload = true;
41172 if (is_a <varpool_node *> (node))
41173 vec_safe_push (offload_vars, decl);
41174 }
41175 }
41176 }
41177 }
41178 }
41179
41180 if (error || current_binding_level->kind == sk_namespace)
41181 return NULL_TREE;
41182
41183 stmt = make_node (OACC_DECLARE);
41184 TREE_TYPE (stmt) = void_type_node;
41185 OACC_DECLARE_CLAUSES (stmt) = clauses;
41186 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41187
41188 add_stmt (stmt);
41189
41190 return NULL_TREE;
41191 }
41192
41193 /* OpenACC 2.0:
41194 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
41195
41196 or
41197
41198 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
41199
41200 LOC is the location of the #pragma token.
41201 */
41202
41203 #define OACC_ENTER_DATA_CLAUSE_MASK \
41204 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41210
41211 #define OACC_EXIT_DATA_CLAUSE_MASK \
41212 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
41216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
41217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
41218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41219
41220 static tree
41221 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
41222 bool enter)
41223 {
41224 location_t loc = pragma_tok->location;
41225 tree stmt, clauses;
41226 const char *p = "";
41227
41228 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41229 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
41230
41231 if (strcmp (p, "data") != 0)
41232 {
41233 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
41234 enter ? "enter" : "exit");
41235 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41236 return NULL_TREE;
41237 }
41238
41239 cp_lexer_consume_token (parser->lexer);
41240
41241 if (enter)
41242 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
41243 "#pragma acc enter data", pragma_tok);
41244 else
41245 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
41246 "#pragma acc exit data", pragma_tok);
41247
41248 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
41249 {
41250 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
41251 enter ? "enter" : "exit");
41252 return NULL_TREE;
41253 }
41254
41255 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
41256 TREE_TYPE (stmt) = void_type_node;
41257 OMP_STANDALONE_CLAUSES (stmt) = clauses;
41258 SET_EXPR_LOCATION (stmt, loc);
41259 add_stmt (stmt);
41260 return stmt;
41261 }
41262
41263 /* OpenACC 2.0:
41264 # pragma acc loop oacc-loop-clause[optseq] new-line
41265 structured-block */
41266
41267 #define OACC_LOOP_CLAUSE_MASK \
41268 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
41269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
41270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
41271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
41272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
41273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
41274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
41275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
41276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
41277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
41278
41279 static tree
41280 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
41281 omp_clause_mask mask, tree *cclauses, bool *if_p)
41282 {
41283 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
41284
41285 strcat (p_name, " loop");
41286 mask |= OACC_LOOP_CLAUSE_MASK;
41287
41288 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
41289 cclauses == NULL);
41290 if (cclauses)
41291 {
41292 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
41293 if (*cclauses)
41294 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
41295 if (clauses)
41296 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
41297 }
41298
41299 tree block = begin_omp_structured_block ();
41300 int save = cp_parser_begin_omp_structured_block (parser);
41301 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
41302 cp_parser_end_omp_structured_block (parser, save);
41303 add_stmt (finish_omp_structured_block (block));
41304
41305 return stmt;
41306 }
41307
41308 /* OpenACC 2.0:
41309 # pragma acc kernels oacc-kernels-clause[optseq] new-line
41310 structured-block
41311
41312 or
41313
41314 # pragma acc parallel oacc-parallel-clause[optseq] new-line
41315 structured-block
41316
41317 OpenACC 2.6:
41318
41319 # pragma acc serial oacc-serial-clause[optseq] new-line
41320 */
41321
41322 #define OACC_KERNELS_CLAUSE_MASK \
41323 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
41330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
41334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
41335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
41336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
41337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41338
41339 #define OACC_PARALLEL_CLAUSE_MASK \
41340 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
41347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
41349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
41352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
41353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
41354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
41355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
41356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
41357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41358
41359 #define OACC_SERIAL_CLAUSE_MASK \
41360 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
41367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
41371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
41372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
41373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
41374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41375
41376 static tree
41377 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
41378 char *p_name, bool *if_p)
41379 {
41380 omp_clause_mask mask;
41381 enum tree_code code;
41382 switch (cp_parser_pragma_kind (pragma_tok))
41383 {
41384 case PRAGMA_OACC_KERNELS:
41385 strcat (p_name, " kernels");
41386 mask = OACC_KERNELS_CLAUSE_MASK;
41387 code = OACC_KERNELS;
41388 break;
41389 case PRAGMA_OACC_PARALLEL:
41390 strcat (p_name, " parallel");
41391 mask = OACC_PARALLEL_CLAUSE_MASK;
41392 code = OACC_PARALLEL;
41393 break;
41394 case PRAGMA_OACC_SERIAL:
41395 strcat (p_name, " serial");
41396 mask = OACC_SERIAL_CLAUSE_MASK;
41397 code = OACC_SERIAL;
41398 break;
41399 default:
41400 gcc_unreachable ();
41401 }
41402
41403 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41404 {
41405 const char *p
41406 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
41407 if (strcmp (p, "loop") == 0)
41408 {
41409 cp_lexer_consume_token (parser->lexer);
41410 tree block = begin_omp_parallel ();
41411 tree clauses;
41412 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
41413 &clauses, if_p);
41414 protected_set_expr_location (stmt, pragma_tok->location);
41415 return finish_omp_construct (code, block, clauses);
41416 }
41417 }
41418
41419 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
41420
41421 tree block = begin_omp_parallel ();
41422 unsigned int save = cp_parser_begin_omp_structured_block (parser);
41423 cp_parser_statement (parser, NULL_TREE, false, if_p);
41424 cp_parser_end_omp_structured_block (parser, save);
41425 return finish_omp_construct (code, block, clauses);
41426 }
41427
41428 /* OpenACC 2.0:
41429 # pragma acc update oacc-update-clause[optseq] new-line
41430 */
41431
41432 #define OACC_UPDATE_CLAUSE_MASK \
41433 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
41435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
41436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
41438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
41439
41440 static tree
41441 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
41442 {
41443 tree stmt, clauses;
41444
41445 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
41446 "#pragma acc update", pragma_tok);
41447
41448 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
41449 {
41450 error_at (pragma_tok->location,
41451 "%<#pragma acc update%> must contain at least one "
41452 "%<device%> or %<host%> or %<self%> clause");
41453 return NULL_TREE;
41454 }
41455
41456 stmt = make_node (OACC_UPDATE);
41457 TREE_TYPE (stmt) = void_type_node;
41458 OACC_UPDATE_CLAUSES (stmt) = clauses;
41459 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41460 add_stmt (stmt);
41461 return stmt;
41462 }
41463
41464 /* OpenACC 2.0:
41465 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
41466
41467 LOC is the location of the #pragma token.
41468 */
41469
41470 #define OACC_WAIT_CLAUSE_MASK \
41471 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
41472
41473 static tree
41474 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
41475 {
41476 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
41477 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41478
41479 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
41480 list = cp_parser_oacc_wait_list (parser, loc, list);
41481
41482 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
41483 "#pragma acc wait", pragma_tok);
41484
41485 stmt = c_finish_oacc_wait (loc, list, clauses);
41486 stmt = finish_expr_stmt (stmt);
41487
41488 return stmt;
41489 }
41490
41491 /* OpenMP 4.0:
41492 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
41493
41494 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
41495 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
41496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
41497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
41498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
41499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
41500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
41501
41502 static void
41503 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
41504 enum pragma_context context,
41505 bool variant_p)
41506 {
41507 bool first_p = parser->omp_declare_simd == NULL;
41508 cp_omp_declare_simd_data data;
41509 if (first_p)
41510 {
41511 data.error_seen = false;
41512 data.fndecl_seen = false;
41513 data.variant_p = variant_p;
41514 data.tokens = vNULL;
41515 data.clauses = NULL_TREE;
41516 /* It is safe to take the address of a local variable; it will only be
41517 used while this scope is live. */
41518 parser->omp_declare_simd = &data;
41519 }
41520 else if (parser->omp_declare_simd->variant_p != variant_p)
41521 {
41522 error_at (pragma_tok->location,
41523 "%<#pragma omp declare %s%> followed by "
41524 "%<#pragma omp declare %s%>",
41525 parser->omp_declare_simd->variant_p ? "variant" : "simd",
41526 parser->omp_declare_simd->variant_p ? "simd" : "variant");
41527 parser->omp_declare_simd->error_seen = true;
41528 }
41529
41530 /* Store away all pragma tokens. */
41531 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41532 cp_lexer_consume_token (parser->lexer);
41533 cp_parser_require_pragma_eol (parser, pragma_tok);
41534 struct cp_token_cache *cp
41535 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
41536 parser->omp_declare_simd->tokens.safe_push (cp);
41537
41538 if (first_p)
41539 {
41540 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
41541 cp_parser_pragma (parser, context, NULL);
41542 switch (context)
41543 {
41544 case pragma_external:
41545 cp_parser_declaration (parser);
41546 break;
41547 case pragma_member:
41548 cp_parser_member_declaration (parser);
41549 break;
41550 case pragma_objc_icode:
41551 cp_parser_block_declaration (parser, /*statement_p=*/false);
41552 break;
41553 default:
41554 cp_parser_declaration_statement (parser);
41555 break;
41556 }
41557 if (parser->omp_declare_simd
41558 && !parser->omp_declare_simd->error_seen
41559 && !parser->omp_declare_simd->fndecl_seen)
41560 error_at (pragma_tok->location,
41561 "%<#pragma omp declare %s%> not immediately followed by "
41562 "function declaration or definition",
41563 parser->omp_declare_simd->variant_p ? "variant" : "simd");
41564 data.tokens.release ();
41565 parser->omp_declare_simd = NULL;
41566 }
41567 }
41568
41569 static const char *const omp_construct_selectors[] = {
41570 "simd", "target", "teams", "parallel", "for", NULL };
41571 static const char *const omp_device_selectors[] = {
41572 "kind", "isa", "arch", NULL };
41573 static const char *const omp_implementation_selectors[] = {
41574 "vendor", "extension", "atomic_default_mem_order", "unified_address",
41575 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
41576 static const char *const omp_user_selectors[] = {
41577 "condition", NULL };
41578
41579 /* OpenMP 5.0:
41580
41581 trait-selector:
41582 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
41583
41584 trait-score:
41585 score(score-expression) */
41586
41587 static tree
41588 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
41589 {
41590 tree ret = NULL_TREE;
41591 do
41592 {
41593 tree selector;
41594 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
41595 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41596 selector = cp_lexer_peek_token (parser->lexer)->u.value;
41597 else
41598 {
41599 cp_parser_error (parser, "expected trait selector name");
41600 return error_mark_node;
41601 }
41602
41603 tree properties = NULL_TREE;
41604 const char *const *selectors = NULL;
41605 bool allow_score = true;
41606 bool allow_user = false;
41607 int property_limit = 0;
41608 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
41609 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
41610 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
41611 switch (IDENTIFIER_POINTER (set)[0])
41612 {
41613 case 'c': /* construct */
41614 selectors = omp_construct_selectors;
41615 allow_score = false;
41616 property_limit = 1;
41617 property_kind = CTX_PROPERTY_SIMD;
41618 break;
41619 case 'd': /* device */
41620 selectors = omp_device_selectors;
41621 allow_score = false;
41622 allow_user = true;
41623 property_limit = 3;
41624 property_kind = CTX_PROPERTY_NAME_LIST;
41625 break;
41626 case 'i': /* implementation */
41627 selectors = omp_implementation_selectors;
41628 allow_user = true;
41629 property_limit = 3;
41630 property_kind = CTX_PROPERTY_NAME_LIST;
41631 break;
41632 case 'u': /* user */
41633 selectors = omp_user_selectors;
41634 property_limit = 1;
41635 property_kind = CTX_PROPERTY_EXPR;
41636 break;
41637 default:
41638 gcc_unreachable ();
41639 }
41640 for (int i = 0; ; i++)
41641 {
41642 if (selectors[i] == NULL)
41643 {
41644 if (allow_user)
41645 {
41646 property_kind = CTX_PROPERTY_USER;
41647 break;
41648 }
41649 else
41650 {
41651 error ("selector %qs not allowed for context selector "
41652 "set %qs", IDENTIFIER_POINTER (selector),
41653 IDENTIFIER_POINTER (set));
41654 cp_lexer_consume_token (parser->lexer);
41655 return error_mark_node;
41656 }
41657 }
41658 if (i == property_limit)
41659 property_kind = CTX_PROPERTY_NONE;
41660 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
41661 break;
41662 }
41663 if (property_kind == CTX_PROPERTY_NAME_LIST
41664 && IDENTIFIER_POINTER (set)[0] == 'i'
41665 && strcmp (IDENTIFIER_POINTER (selector),
41666 "atomic_default_mem_order") == 0)
41667 property_kind = CTX_PROPERTY_ID;
41668
41669 cp_lexer_consume_token (parser->lexer);
41670
41671 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41672 {
41673 if (property_kind == CTX_PROPERTY_NONE)
41674 {
41675 error ("selector %qs does not accept any properties",
41676 IDENTIFIER_POINTER (selector));
41677 return error_mark_node;
41678 }
41679
41680 matching_parens parens;
41681 parens.consume_open (parser);
41682
41683 cp_token *token = cp_lexer_peek_token (parser->lexer);
41684 if (allow_score
41685 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41686 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
41687 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
41688 {
41689 cp_lexer_save_tokens (parser->lexer);
41690 cp_lexer_consume_token (parser->lexer);
41691 cp_lexer_consume_token (parser->lexer);
41692 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
41693 true)
41694 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
41695 {
41696 cp_lexer_rollback_tokens (parser->lexer);
41697 cp_lexer_consume_token (parser->lexer);
41698
41699 matching_parens parens2;
41700 parens2.require_open (parser);
41701 tree score = cp_parser_constant_expression (parser);
41702 if (!parens2.require_close (parser))
41703 cp_parser_skip_to_closing_parenthesis (parser, true,
41704 false, true);
41705 cp_parser_require (parser, CPP_COLON, RT_COLON);
41706 if (score != error_mark_node)
41707 {
41708 score = fold_non_dependent_expr (score);
41709 if (value_dependent_expression_p (score))
41710 properties = tree_cons (get_identifier (" score"),
41711 score, properties);
41712 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
41713 || TREE_CODE (score) != INTEGER_CST)
41714 error_at (token->location, "score argument must be "
41715 "constant integer expression");
41716 else if (tree_int_cst_sgn (score) < 0)
41717 error_at (token->location, "score argument must be "
41718 "non-negative");
41719 else
41720 properties = tree_cons (get_identifier (" score"),
41721 score, properties);
41722 }
41723 }
41724 else
41725 cp_lexer_rollback_tokens (parser->lexer);
41726
41727 token = cp_lexer_peek_token (parser->lexer);
41728 }
41729
41730 switch (property_kind)
41731 {
41732 tree t;
41733 case CTX_PROPERTY_USER:
41734 do
41735 {
41736 t = cp_parser_constant_expression (parser);
41737 if (t != error_mark_node)
41738 {
41739 t = fold_non_dependent_expr (t);
41740 if (TREE_CODE (t) == STRING_CST)
41741 properties = tree_cons (NULL_TREE, t, properties);
41742 else if (!value_dependent_expression_p (t)
41743 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
41744 || !tree_fits_shwi_p (t)))
41745 error_at (token->location, "property must be "
41746 "constant integer expression or string "
41747 "literal");
41748 else
41749 properties = tree_cons (NULL_TREE, t, properties);
41750 }
41751 else
41752 return error_mark_node;
41753
41754 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41755 cp_lexer_consume_token (parser->lexer);
41756 else
41757 break;
41758 }
41759 while (1);
41760 break;
41761 case CTX_PROPERTY_ID:
41762 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
41763 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41764 {
41765 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
41766 cp_lexer_consume_token (parser->lexer);
41767 properties = tree_cons (prop, NULL_TREE, properties);
41768 }
41769 else
41770 {
41771 cp_parser_error (parser, "expected identifier");
41772 return error_mark_node;
41773 }
41774 break;
41775 case CTX_PROPERTY_NAME_LIST:
41776 do
41777 {
41778 tree prop = NULL_TREE, value = NULL_TREE;
41779 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
41780 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41781 {
41782 prop = cp_lexer_peek_token (parser->lexer)->u.value;
41783 cp_lexer_consume_token (parser->lexer);
41784 }
41785 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
41786 value = cp_parser_string_literal (parser, false, false);
41787 else
41788 {
41789 cp_parser_error (parser, "expected identifier or "
41790 "string literal");
41791 return error_mark_node;
41792 }
41793
41794 properties = tree_cons (prop, value, properties);
41795
41796 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41797 cp_lexer_consume_token (parser->lexer);
41798 else
41799 break;
41800 }
41801 while (1);
41802 break;
41803 case CTX_PROPERTY_EXPR:
41804 t = cp_parser_constant_expression (parser);
41805 if (t != error_mark_node)
41806 {
41807 t = fold_non_dependent_expr (t);
41808 if (!value_dependent_expression_p (t)
41809 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
41810 || !tree_fits_shwi_p (t)))
41811 error_at (token->location, "property must be "
41812 "constant integer expression");
41813 else
41814 properties = tree_cons (NULL_TREE, t, properties);
41815 }
41816 else
41817 return error_mark_node;
41818 break;
41819 case CTX_PROPERTY_SIMD:
41820 if (!has_parms_p)
41821 {
41822 error_at (token->location, "properties for %<simd%> "
41823 "selector may not be specified in "
41824 "%<metadirective%>");
41825 return error_mark_node;
41826 }
41827 properties
41828 = cp_parser_omp_all_clauses (parser,
41829 OMP_DECLARE_SIMD_CLAUSE_MASK,
41830 "simd", NULL, true, 2);
41831 break;
41832 default:
41833 gcc_unreachable ();
41834 }
41835
41836 if (!parens.require_close (parser))
41837 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41838
41839 properties = nreverse (properties);
41840 }
41841 else if (property_kind == CTX_PROPERTY_NAME_LIST
41842 || property_kind == CTX_PROPERTY_ID
41843 || property_kind == CTX_PROPERTY_EXPR)
41844 {
41845 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
41846 return error_mark_node;
41847 }
41848
41849 ret = tree_cons (selector, properties, ret);
41850
41851 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41852 cp_lexer_consume_token (parser->lexer);
41853 else
41854 break;
41855 }
41856 while (1);
41857
41858 return nreverse (ret);
41859 }
41860
41861 /* OpenMP 5.0:
41862
41863 trait-set-selector[,trait-set-selector[,...]]
41864
41865 trait-set-selector:
41866 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
41867
41868 trait-set-selector-name:
41869 constructor
41870 device
41871 implementation
41872 user */
41873
41874 static tree
41875 cp_parser_omp_context_selector_specification (cp_parser *parser,
41876 bool has_parms_p)
41877 {
41878 tree ret = NULL_TREE;
41879 do
41880 {
41881 const char *setp = "";
41882 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41883 setp
41884 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
41885 switch (setp[0])
41886 {
41887 case 'c':
41888 if (strcmp (setp, "construct") == 0)
41889 setp = NULL;
41890 break;
41891 case 'd':
41892 if (strcmp (setp, "device") == 0)
41893 setp = NULL;
41894 break;
41895 case 'i':
41896 if (strcmp (setp, "implementation") == 0)
41897 setp = NULL;
41898 break;
41899 case 'u':
41900 if (strcmp (setp, "user") == 0)
41901 setp = NULL;
41902 break;
41903 default:
41904 break;
41905 }
41906 if (setp)
41907 {
41908 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
41909 "%<implementation%> or %<user%>");
41910 return error_mark_node;
41911 }
41912
41913 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
41914 cp_lexer_consume_token (parser->lexer);
41915
41916 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41917 return error_mark_node;
41918
41919 matching_braces braces;
41920 if (!braces.require_open (parser))
41921 return error_mark_node;
41922
41923 tree selectors
41924 = cp_parser_omp_context_selector (parser, set, has_parms_p);
41925 if (selectors == error_mark_node)
41926 {
41927 cp_parser_skip_to_closing_brace (parser);
41928 ret = error_mark_node;
41929 }
41930 else if (ret != error_mark_node)
41931 ret = tree_cons (set, selectors, ret);
41932
41933 braces.require_close (parser);
41934
41935 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41936 cp_lexer_consume_token (parser->lexer);
41937 else
41938 break;
41939 }
41940 while (1);
41941
41942 if (ret == error_mark_node)
41943 return ret;
41944 return nreverse (ret);
41945 }
41946
41947 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
41948 that into "omp declare variant base" attribute. */
41949
41950 static tree
41951 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
41952 tree attrs)
41953 {
41954 matching_parens parens;
41955 if (!parens.require_open (parser))
41956 {
41957 fail:
41958 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41959 return attrs;
41960 }
41961
41962 bool template_p;
41963 cp_id_kind idk = CP_ID_KIND_NONE;
41964 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
41965 cp_expr varid
41966 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
41967 /*check_dependency_p=*/true,
41968 /*template_p=*/&template_p,
41969 /*declarator_p=*/false,
41970 /*optional_p=*/false);
41971 parens.require_close (parser);
41972
41973 tree variant;
41974 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
41975 || TREE_CODE (varid) == TYPE_DECL
41976 || varid == error_mark_node)
41977 variant = varid;
41978 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
41979 variant = NULL_TREE;
41980 else
41981 {
41982 tree ambiguous_decls;
41983 variant = cp_parser_lookup_name (parser, varid, none_type,
41984 template_p, /*is_namespace=*/false,
41985 /*check_dependency=*/true,
41986 &ambiguous_decls,
41987 varid.get_location ());
41988 if (ambiguous_decls)
41989 variant = NULL_TREE;
41990 }
41991 if (variant == NULL_TREE)
41992 variant = error_mark_node;
41993 else if (TREE_CODE (variant) != SCOPE_REF)
41994 {
41995 const char *error_msg;
41996 variant
41997 = finish_id_expression (varid, variant, parser->scope,
41998 &idk, false, true,
41999 &parser->non_integral_constant_expression_p,
42000 template_p, true, false, false, &error_msg,
42001 varid.get_location ());
42002 if (error_msg)
42003 cp_parser_error (parser, error_msg);
42004 }
42005 location_t caret_loc = get_pure_location (varid.get_location ());
42006 location_t start_loc = get_start (varid_token->location);
42007 location_t finish_loc = get_finish (varid.get_location ());
42008 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
42009
42010 const char *clause = "";
42011 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
42012 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42013 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
42014 if (strcmp (clause, "match"))
42015 {
42016 cp_parser_error (parser, "expected %<match%>");
42017 goto fail;
42018 }
42019
42020 cp_lexer_consume_token (parser->lexer);
42021
42022 if (!parens.require_open (parser))
42023 goto fail;
42024
42025 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
42026 if (ctx == error_mark_node)
42027 goto fail;
42028 ctx = c_omp_check_context_selector (match_loc, ctx);
42029 if (ctx != error_mark_node && variant != error_mark_node)
42030 {
42031 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
42032 match_loc);
42033 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
42034 loc_node = tree_cons (match_loc_node,
42035 build_int_cst (integer_type_node, idk),
42036 build_tree_list (loc_node, integer_zero_node));
42037 attrs = tree_cons (get_identifier ("omp declare variant base"),
42038 tree_cons (variant, ctx, loc_node), attrs);
42039 if (processing_template_decl)
42040 ATTR_IS_DEPENDENT (attrs) = 1;
42041 }
42042
42043 parens.require_close (parser);
42044 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42045 return attrs;
42046 }
42047
42048
42049 /* Finalize #pragma omp declare simd clauses after direct declarator has
42050 been parsed, and put that into "omp declare simd" attribute. */
42051
42052 static tree
42053 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
42054 {
42055 struct cp_token_cache *ce;
42056 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
42057 int i;
42058
42059 if (!data->error_seen && data->fndecl_seen)
42060 {
42061 error ("%<#pragma omp declare %s%> not immediately followed by "
42062 "a single function declaration or definition",
42063 data->variant_p ? "variant" : "simd");
42064 data->error_seen = true;
42065 }
42066 if (data->error_seen)
42067 return attrs;
42068
42069 FOR_EACH_VEC_ELT (data->tokens, i, ce)
42070 {
42071 tree c, cl;
42072
42073 cp_parser_push_lexer_for_tokens (parser, ce);
42074 parser->lexer->in_pragma = true;
42075 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
42076 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
42077 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42078 const char *kind = IDENTIFIER_POINTER (id);
42079 cp_lexer_consume_token (parser->lexer);
42080 if (strcmp (kind, "simd") == 0)
42081 {
42082 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
42083 "#pragma omp declare simd",
42084 pragma_tok);
42085 if (cl)
42086 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
42087 c = build_tree_list (get_identifier ("omp declare simd"), cl);
42088 TREE_CHAIN (c) = attrs;
42089 if (processing_template_decl)
42090 ATTR_IS_DEPENDENT (c) = 1;
42091 attrs = c;
42092 }
42093 else
42094 {
42095 gcc_assert (strcmp (kind, "variant") == 0);
42096 attrs = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
42097 }
42098 cp_parser_pop_lexer (parser);
42099 }
42100
42101 data->fndecl_seen = true;
42102 return attrs;
42103 }
42104
42105
42106 /* OpenMP 4.0:
42107 # pragma omp declare target new-line
42108 declarations and definitions
42109 # pragma omp end declare target new-line
42110
42111 OpenMP 4.5:
42112 # pragma omp declare target ( extended-list ) new-line
42113
42114 # pragma omp declare target declare-target-clauses[seq] new-line */
42115
42116 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
42117 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
42118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
42119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
42120
42121 static void
42122 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
42123 {
42124 tree clauses = NULL_TREE;
42125 int device_type = 0;
42126 bool only_device_type = true;
42127 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42128 clauses
42129 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
42130 "#pragma omp declare target", pragma_tok);
42131 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42132 {
42133 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
42134 clauses);
42135 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
42136 cp_parser_require_pragma_eol (parser, pragma_tok);
42137 }
42138 else
42139 {
42140 cp_parser_require_pragma_eol (parser, pragma_tok);
42141 scope_chain->omp_declare_target_attribute++;
42142 return;
42143 }
42144 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
42145 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
42146 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
42147 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
42148 {
42149 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
42150 continue;
42151 tree t = OMP_CLAUSE_DECL (c), id;
42152 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
42153 tree at2 = lookup_attribute ("omp declare target link",
42154 DECL_ATTRIBUTES (t));
42155 only_device_type = false;
42156 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
42157 {
42158 id = get_identifier ("omp declare target link");
42159 std::swap (at1, at2);
42160 }
42161 else
42162 id = get_identifier ("omp declare target");
42163 if (at2)
42164 {
42165 error_at (OMP_CLAUSE_LOCATION (c),
42166 "%qD specified both in declare target %<link%> and %<to%>"
42167 " clauses", t);
42168 continue;
42169 }
42170 if (!at1)
42171 {
42172 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
42173 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
42174 continue;
42175
42176 symtab_node *node = symtab_node::get (t);
42177 if (node != NULL)
42178 {
42179 node->offloadable = 1;
42180 if (ENABLE_OFFLOADING)
42181 {
42182 g->have_offload = true;
42183 if (is_a <varpool_node *> (node))
42184 vec_safe_push (offload_vars, t);
42185 }
42186 }
42187 }
42188 if (TREE_CODE (t) != FUNCTION_DECL)
42189 continue;
42190 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
42191 {
42192 tree at3 = lookup_attribute ("omp declare target host",
42193 DECL_ATTRIBUTES (t));
42194 if (at3 == NULL_TREE)
42195 {
42196 id = get_identifier ("omp declare target host");
42197 DECL_ATTRIBUTES (t)
42198 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
42199 }
42200 }
42201 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
42202 {
42203 tree at3 = lookup_attribute ("omp declare target nohost",
42204 DECL_ATTRIBUTES (t));
42205 if (at3 == NULL_TREE)
42206 {
42207 id = get_identifier ("omp declare target nohost");
42208 DECL_ATTRIBUTES (t)
42209 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
42210 }
42211 }
42212 }
42213 if (device_type && only_device_type)
42214 warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
42215 "directive with only %<device_type%> clauses ignored");
42216 }
42217
42218 static void
42219 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
42220 {
42221 const char *p = "";
42222 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42223 {
42224 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42225 p = IDENTIFIER_POINTER (id);
42226 }
42227 if (strcmp (p, "declare") == 0)
42228 {
42229 cp_lexer_consume_token (parser->lexer);
42230 p = "";
42231 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42232 {
42233 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42234 p = IDENTIFIER_POINTER (id);
42235 }
42236 if (strcmp (p, "target") == 0)
42237 cp_lexer_consume_token (parser->lexer);
42238 else
42239 {
42240 cp_parser_error (parser, "expected %<target%>");
42241 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42242 return;
42243 }
42244 }
42245 else
42246 {
42247 cp_parser_error (parser, "expected %<declare%>");
42248 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42249 return;
42250 }
42251 cp_parser_require_pragma_eol (parser, pragma_tok);
42252 if (!scope_chain->omp_declare_target_attribute)
42253 error_at (pragma_tok->location,
42254 "%<#pragma omp end declare target%> without corresponding "
42255 "%<#pragma omp declare target%>");
42256 else
42257 scope_chain->omp_declare_target_attribute--;
42258 }
42259
42260 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
42261 expression and optional initializer clause of
42262 #pragma omp declare reduction. We store the expression(s) as
42263 either 3, 6 or 7 special statements inside of the artificial function's
42264 body. The first two statements are DECL_EXPRs for the artificial
42265 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
42266 expression that uses those variables.
42267 If there was any INITIALIZER clause, this is followed by further statements,
42268 the fourth and fifth statements are DECL_EXPRs for the artificial
42269 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
42270 constructor variant (first token after open paren is not omp_priv),
42271 then the sixth statement is a statement with the function call expression
42272 that uses the OMP_PRIV and optionally OMP_ORIG variable.
42273 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
42274 to initialize the OMP_PRIV artificial variable and there is seventh
42275 statement, a DECL_EXPR of the OMP_PRIV statement again. */
42276
42277 static bool
42278 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
42279 {
42280 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
42281 gcc_assert (TYPE_REF_P (type));
42282 type = TREE_TYPE (type);
42283 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
42284 DECL_ARTIFICIAL (omp_out) = 1;
42285 pushdecl (omp_out);
42286 add_decl_expr (omp_out);
42287 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
42288 DECL_ARTIFICIAL (omp_in) = 1;
42289 pushdecl (omp_in);
42290 add_decl_expr (omp_in);
42291 tree combiner;
42292 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
42293
42294 keep_next_level (true);
42295 tree block = begin_omp_structured_block ();
42296 combiner = cp_parser_expression (parser);
42297 finish_expr_stmt (combiner);
42298 block = finish_omp_structured_block (block);
42299 if (processing_template_decl)
42300 block = build_stmt (input_location, EXPR_STMT, block);
42301 add_stmt (block);
42302
42303 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
42304 return false;
42305
42306 const char *p = "";
42307 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42308 {
42309 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42310 p = IDENTIFIER_POINTER (id);
42311 }
42312
42313 if (strcmp (p, "initializer") == 0)
42314 {
42315 cp_lexer_consume_token (parser->lexer);
42316 matching_parens parens;
42317 if (!parens.require_open (parser))
42318 return false;
42319
42320 p = "";
42321 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42322 {
42323 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42324 p = IDENTIFIER_POINTER (id);
42325 }
42326
42327 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
42328 DECL_ARTIFICIAL (omp_priv) = 1;
42329 pushdecl (omp_priv);
42330 add_decl_expr (omp_priv);
42331 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
42332 DECL_ARTIFICIAL (omp_orig) = 1;
42333 pushdecl (omp_orig);
42334 add_decl_expr (omp_orig);
42335
42336 keep_next_level (true);
42337 block = begin_omp_structured_block ();
42338
42339 bool ctor = false;
42340 if (strcmp (p, "omp_priv") == 0)
42341 {
42342 bool is_direct_init, is_non_constant_init;
42343 ctor = true;
42344 cp_lexer_consume_token (parser->lexer);
42345 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
42346 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
42347 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
42348 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
42349 == CPP_CLOSE_PAREN
42350 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
42351 == CPP_CLOSE_PAREN))
42352 {
42353 finish_omp_structured_block (block);
42354 error ("invalid initializer clause");
42355 return false;
42356 }
42357 initializer = cp_parser_initializer (parser, &is_direct_init,
42358 &is_non_constant_init);
42359 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
42360 NULL_TREE, LOOKUP_ONLYCONVERTING);
42361 }
42362 else
42363 {
42364 cp_parser_parse_tentatively (parser);
42365 /* Don't create location wrapper nodes here. */
42366 auto_suppress_location_wrappers sentinel;
42367 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
42368 /*check_dependency_p=*/true,
42369 /*template_p=*/NULL,
42370 /*declarator_p=*/false,
42371 /*optional_p=*/false);
42372 vec<tree, va_gc> *args;
42373 if (fn_name == error_mark_node
42374 || cp_parser_error_occurred (parser)
42375 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
42376 || ((args = cp_parser_parenthesized_expression_list
42377 (parser, non_attr, /*cast_p=*/false,
42378 /*allow_expansion_p=*/true,
42379 /*non_constant_p=*/NULL)),
42380 cp_parser_error_occurred (parser)))
42381 {
42382 finish_omp_structured_block (block);
42383 cp_parser_abort_tentative_parse (parser);
42384 cp_parser_error (parser, "expected id-expression (arguments)");
42385 return false;
42386 }
42387 unsigned int i;
42388 tree arg;
42389 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
42390 if (arg == omp_priv
42391 || (TREE_CODE (arg) == ADDR_EXPR
42392 && TREE_OPERAND (arg, 0) == omp_priv))
42393 break;
42394 cp_parser_abort_tentative_parse (parser);
42395 if (arg == NULL_TREE)
42396 error ("one of the initializer call arguments should be %<omp_priv%>"
42397 " or %<&omp_priv%>");
42398 initializer = cp_parser_postfix_expression (parser, false, false, false,
42399 false, NULL);
42400 finish_expr_stmt (initializer);
42401 }
42402
42403 block = finish_omp_structured_block (block);
42404 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
42405 if (processing_template_decl)
42406 block = build_stmt (input_location, EXPR_STMT, block);
42407 add_stmt (block);
42408
42409 if (ctor)
42410 add_decl_expr (omp_orig);
42411
42412 if (!parens.require_close (parser))
42413 return false;
42414 }
42415
42416 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
42417 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
42418 UNKNOWN_LOCATION);
42419
42420 return true;
42421 }
42422
42423 /* OpenMP 4.0
42424 #pragma omp declare reduction (reduction-id : typename-list : expression) \
42425 initializer-clause[opt] new-line
42426
42427 initializer-clause:
42428 initializer (omp_priv initializer)
42429 initializer (function-name (argument-list)) */
42430
42431 static void
42432 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
42433 enum pragma_context)
42434 {
42435 auto_vec<tree> types;
42436 enum tree_code reduc_code = ERROR_MARK;
42437 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
42438 unsigned int i;
42439 cp_token *first_token;
42440 cp_token_cache *cp;
42441 int errs;
42442 void *p;
42443
42444 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
42445 p = obstack_alloc (&declarator_obstack, 0);
42446
42447 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
42448 goto fail;
42449
42450 switch (cp_lexer_peek_token (parser->lexer)->type)
42451 {
42452 case CPP_PLUS:
42453 reduc_code = PLUS_EXPR;
42454 break;
42455 case CPP_MULT:
42456 reduc_code = MULT_EXPR;
42457 break;
42458 case CPP_MINUS:
42459 reduc_code = MINUS_EXPR;
42460 break;
42461 case CPP_AND:
42462 reduc_code = BIT_AND_EXPR;
42463 break;
42464 case CPP_XOR:
42465 reduc_code = BIT_XOR_EXPR;
42466 break;
42467 case CPP_OR:
42468 reduc_code = BIT_IOR_EXPR;
42469 break;
42470 case CPP_AND_AND:
42471 reduc_code = TRUTH_ANDIF_EXPR;
42472 break;
42473 case CPP_OR_OR:
42474 reduc_code = TRUTH_ORIF_EXPR;
42475 break;
42476 case CPP_NAME:
42477 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
42478 break;
42479 default:
42480 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
42481 "%<|%>, %<&&%>, %<||%> or identifier");
42482 goto fail;
42483 }
42484
42485 if (reduc_code != ERROR_MARK)
42486 cp_lexer_consume_token (parser->lexer);
42487
42488 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
42489 if (reduc_id == error_mark_node)
42490 goto fail;
42491
42492 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
42493 goto fail;
42494
42495 /* Types may not be defined in declare reduction type list. */
42496 const char *saved_message;
42497 saved_message = parser->type_definition_forbidden_message;
42498 parser->type_definition_forbidden_message
42499 = G_("types may not be defined in declare reduction type list");
42500 bool saved_colon_corrects_to_scope_p;
42501 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
42502 parser->colon_corrects_to_scope_p = false;
42503 bool saved_colon_doesnt_start_class_def_p;
42504 saved_colon_doesnt_start_class_def_p
42505 = parser->colon_doesnt_start_class_def_p;
42506 parser->colon_doesnt_start_class_def_p = true;
42507
42508 while (true)
42509 {
42510 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42511 type = cp_parser_type_id (parser);
42512 if (type == error_mark_node)
42513 ;
42514 else if (ARITHMETIC_TYPE_P (type)
42515 && (orig_reduc_id == NULL_TREE
42516 || (TREE_CODE (type) != COMPLEX_TYPE
42517 && (id_equal (orig_reduc_id, "min")
42518 || id_equal (orig_reduc_id, "max")))))
42519 error_at (loc, "predeclared arithmetic type %qT in "
42520 "%<#pragma omp declare reduction%>", type);
42521 else if (FUNC_OR_METHOD_TYPE_P (type)
42522 || TREE_CODE (type) == ARRAY_TYPE)
42523 error_at (loc, "function or array type %qT in "
42524 "%<#pragma omp declare reduction%>", type);
42525 else if (TYPE_REF_P (type))
42526 error_at (loc, "reference type %qT in "
42527 "%<#pragma omp declare reduction%>", type);
42528 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
42529 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
42530 "type %qT in %<#pragma omp declare reduction%>", type);
42531 else
42532 types.safe_push (type);
42533
42534 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42535 cp_lexer_consume_token (parser->lexer);
42536 else
42537 break;
42538 }
42539
42540 /* Restore the saved message. */
42541 parser->type_definition_forbidden_message = saved_message;
42542 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
42543 parser->colon_doesnt_start_class_def_p
42544 = saved_colon_doesnt_start_class_def_p;
42545
42546 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
42547 || types.is_empty ())
42548 {
42549 fail:
42550 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42551 goto done;
42552 }
42553
42554 first_token = cp_lexer_peek_token (parser->lexer);
42555 cp = NULL;
42556 errs = errorcount;
42557 FOR_EACH_VEC_ELT (types, i, type)
42558 {
42559 tree fntype
42560 = build_function_type_list (void_type_node,
42561 cp_build_reference_type (type, false),
42562 NULL_TREE);
42563 tree this_reduc_id = reduc_id;
42564 if (!dependent_type_p (type))
42565 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
42566 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
42567 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
42568 DECL_ARTIFICIAL (fndecl) = 1;
42569 DECL_EXTERNAL (fndecl) = 1;
42570 DECL_DECLARED_INLINE_P (fndecl) = 1;
42571 DECL_IGNORED_P (fndecl) = 1;
42572 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
42573 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
42574 DECL_ATTRIBUTES (fndecl)
42575 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
42576 DECL_ATTRIBUTES (fndecl));
42577 if (processing_template_decl)
42578 fndecl = push_template_decl (fndecl);
42579 bool block_scope = false;
42580 tree block = NULL_TREE;
42581 if (current_function_decl)
42582 {
42583 block_scope = true;
42584 DECL_CONTEXT (fndecl) = global_namespace;
42585 DECL_LOCAL_DECL_P (fndecl) = true;
42586 if (!processing_template_decl)
42587 pushdecl (fndecl);
42588 }
42589 else if (current_class_type)
42590 {
42591 if (cp == NULL)
42592 {
42593 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42594 cp_lexer_consume_token (parser->lexer);
42595 cp = cp_token_cache_new (first_token,
42596 cp_lexer_peek_nth_token (parser->lexer,
42597 2));
42598 }
42599 DECL_STATIC_FUNCTION_P (fndecl) = 1;
42600 finish_member_declaration (fndecl);
42601 DECL_PENDING_INLINE_INFO (fndecl) = cp;
42602 DECL_PENDING_INLINE_P (fndecl) = 1;
42603 vec_safe_push (unparsed_funs_with_definitions, fndecl);
42604 continue;
42605 }
42606 else
42607 {
42608 DECL_CONTEXT (fndecl) = current_namespace;
42609 pushdecl (fndecl);
42610 }
42611 if (!block_scope)
42612 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
42613 else
42614 block = begin_omp_structured_block ();
42615 if (cp)
42616 {
42617 cp_parser_push_lexer_for_tokens (parser, cp);
42618 parser->lexer->in_pragma = true;
42619 }
42620
42621 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
42622
42623 if (cp)
42624 cp_parser_pop_lexer (parser);
42625 if (!block_scope)
42626 finish_function (/*inline_p=*/false);
42627 else
42628 {
42629 DECL_CONTEXT (fndecl) = current_function_decl;
42630 if (DECL_TEMPLATE_INFO (fndecl))
42631 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
42632 }
42633 if (!ok)
42634 goto fail;
42635
42636 if (block_scope)
42637 {
42638 block = finish_omp_structured_block (block);
42639 if (TREE_CODE (block) == BIND_EXPR)
42640 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
42641 else if (TREE_CODE (block) == STATEMENT_LIST)
42642 DECL_SAVED_TREE (fndecl) = block;
42643 if (processing_template_decl)
42644 add_decl_expr (fndecl);
42645 }
42646
42647 cp_check_omp_declare_reduction (fndecl);
42648 if (cp == NULL && types.length () > 1)
42649 cp = cp_token_cache_new (first_token,
42650 cp_lexer_peek_nth_token (parser->lexer, 2));
42651 if (errs != errorcount)
42652 break;
42653 }
42654
42655 cp_parser_require_pragma_eol (parser, pragma_tok);
42656
42657 done:
42658 /* Free any declarators allocated. */
42659 obstack_free (&declarator_obstack, p);
42660 }
42661
42662 /* OpenMP 4.0
42663 #pragma omp declare simd declare-simd-clauses[optseq] new-line
42664 #pragma omp declare reduction (reduction-id : typename-list : expression) \
42665 initializer-clause[opt] new-line
42666 #pragma omp declare target new-line
42667
42668 OpenMP 5.0
42669 #pragma omp declare variant (identifier) match (context-selector) */
42670
42671 static bool
42672 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
42673 enum pragma_context context)
42674 {
42675 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42676 {
42677 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42678 const char *p = IDENTIFIER_POINTER (id);
42679
42680 if (strcmp (p, "simd") == 0)
42681 {
42682 cp_lexer_consume_token (parser->lexer);
42683 cp_parser_omp_declare_simd (parser, pragma_tok,
42684 context, false);
42685 return true;
42686 }
42687 if (flag_openmp && strcmp (p, "variant") == 0)
42688 {
42689 cp_lexer_consume_token (parser->lexer);
42690 cp_parser_omp_declare_simd (parser, pragma_tok,
42691 context, true);
42692 return true;
42693 }
42694 cp_ensure_no_omp_declare_simd (parser);
42695 if (strcmp (p, "reduction") == 0)
42696 {
42697 cp_lexer_consume_token (parser->lexer);
42698 cp_parser_omp_declare_reduction (parser, pragma_tok,
42699 context);
42700 return false;
42701 }
42702 if (!flag_openmp) /* flag_openmp_simd */
42703 {
42704 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42705 return false;
42706 }
42707 if (strcmp (p, "target") == 0)
42708 {
42709 cp_lexer_consume_token (parser->lexer);
42710 cp_parser_omp_declare_target (parser, pragma_tok);
42711 return false;
42712 }
42713 }
42714 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
42715 "%<target%> or %<variant%>");
42716 cp_parser_require_pragma_eol (parser, pragma_tok);
42717 return false;
42718 }
42719
42720 /* OpenMP 5.0
42721 #pragma omp requires clauses[optseq] new-line */
42722
42723 static bool
42724 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
42725 {
42726 bool first = true;
42727 enum omp_requires new_req = (enum omp_requires) 0;
42728
42729 location_t loc = pragma_tok->location;
42730 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42731 {
42732 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42733 cp_lexer_consume_token (parser->lexer);
42734
42735 first = false;
42736
42737 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42738 {
42739 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42740 const char *p = IDENTIFIER_POINTER (id);
42741 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
42742 enum omp_requires this_req = (enum omp_requires) 0;
42743
42744 if (!strcmp (p, "unified_address"))
42745 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
42746 else if (!strcmp (p, "unified_shared_memory"))
42747 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
42748 else if (!strcmp (p, "dynamic_allocators"))
42749 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
42750 else if (!strcmp (p, "reverse_offload"))
42751 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
42752 else if (!strcmp (p, "atomic_default_mem_order"))
42753 {
42754 cp_lexer_consume_token (parser->lexer);
42755
42756 matching_parens parens;
42757 if (parens.require_open (parser))
42758 {
42759 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42760 {
42761 id = cp_lexer_peek_token (parser->lexer)->u.value;
42762 p = IDENTIFIER_POINTER (id);
42763
42764 if (!strcmp (p, "seq_cst"))
42765 this_req
42766 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
42767 else if (!strcmp (p, "relaxed"))
42768 this_req
42769 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
42770 else if (!strcmp (p, "acq_rel"))
42771 this_req
42772 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
42773 }
42774 if (this_req == 0)
42775 {
42776 error_at (cp_lexer_peek_token (parser->lexer)->location,
42777 "expected %<seq_cst%>, %<relaxed%> or "
42778 "%<acq_rel%>");
42779 if (cp_lexer_nth_token_is (parser->lexer, 2,
42780 CPP_CLOSE_PAREN))
42781 cp_lexer_consume_token (parser->lexer);
42782 }
42783 else
42784 cp_lexer_consume_token (parser->lexer);
42785
42786 if (!parens.require_close (parser))
42787 cp_parser_skip_to_closing_parenthesis (parser,
42788 /*recovering=*/true,
42789 /*or_comma=*/false,
42790 /*consume_paren=*/
42791 true);
42792
42793 if (this_req == 0)
42794 {
42795 cp_parser_require_pragma_eol (parser, pragma_tok);
42796 return false;
42797 }
42798 }
42799 p = NULL;
42800 }
42801 else
42802 {
42803 error_at (cloc, "expected %<unified_address%>, "
42804 "%<unified_shared_memory%>, "
42805 "%<dynamic_allocators%>, "
42806 "%<reverse_offload%> "
42807 "or %<atomic_default_mem_order%> clause");
42808 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42809 return false;
42810 }
42811 if (p)
42812 sorry_at (cloc, "%qs clause on %<requires%> directive not "
42813 "supported yet", p);
42814 if (p)
42815 cp_lexer_consume_token (parser->lexer);
42816 if (this_req)
42817 {
42818 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
42819 {
42820 if ((this_req & new_req) != 0)
42821 error_at (cloc, "too many %qs clauses", p);
42822 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
42823 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
42824 error_at (cloc, "%qs clause used lexically after first "
42825 "target construct or offloading API", p);
42826 }
42827 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
42828 {
42829 error_at (cloc, "too many %qs clauses",
42830 "atomic_default_mem_order");
42831 this_req = (enum omp_requires) 0;
42832 }
42833 else if ((omp_requires_mask
42834 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
42835 {
42836 error_at (cloc, "more than one %<atomic_default_mem_order%>"
42837 " clause in a single compilation unit");
42838 this_req
42839 = (enum omp_requires)
42840 (omp_requires_mask
42841 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
42842 }
42843 else if ((omp_requires_mask
42844 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
42845 error_at (cloc, "%<atomic_default_mem_order%> clause used "
42846 "lexically after first %<atomic%> construct "
42847 "without memory order clause");
42848 new_req = (enum omp_requires) (new_req | this_req);
42849 omp_requires_mask
42850 = (enum omp_requires) (omp_requires_mask | this_req);
42851 continue;
42852 }
42853 }
42854 break;
42855 }
42856 cp_parser_require_pragma_eol (parser, pragma_tok);
42857
42858 if (new_req == 0)
42859 error_at (loc, "%<pragma omp requires%> requires at least one clause");
42860 return false;
42861 }
42862
42863
42864 /* OpenMP 4.5:
42865 #pragma omp taskloop taskloop-clause[optseq] new-line
42866 for-loop
42867
42868 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
42869 for-loop */
42870
42871 #define OMP_TASKLOOP_CLAUSE_MASK \
42872 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
42873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
42874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
42875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
42876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
42877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
42878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
42879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
42880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
42881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
42882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
42883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
42884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
42885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
42886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
42887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
42888
42889 static tree
42890 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
42891 char *p_name, omp_clause_mask mask, tree *cclauses,
42892 bool *if_p)
42893 {
42894 tree clauses, sb, ret;
42895 unsigned int save;
42896 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42897
42898 strcat (p_name, " taskloop");
42899 mask |= OMP_TASKLOOP_CLAUSE_MASK;
42900 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
42901 clause. */
42902 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
42903 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
42904
42905 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42906 {
42907 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42908 const char *p = IDENTIFIER_POINTER (id);
42909
42910 if (strcmp (p, "simd") == 0)
42911 {
42912 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
42913 if (cclauses == NULL)
42914 cclauses = cclauses_buf;
42915
42916 cp_lexer_consume_token (parser->lexer);
42917 if (!flag_openmp) /* flag_openmp_simd */
42918 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
42919 cclauses, if_p);
42920 sb = begin_omp_structured_block ();
42921 save = cp_parser_begin_omp_structured_block (parser);
42922 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
42923 cclauses, if_p);
42924 cp_parser_end_omp_structured_block (parser, save);
42925 tree body = finish_omp_structured_block (sb);
42926 if (ret == NULL)
42927 return ret;
42928 ret = make_node (OMP_TASKLOOP);
42929 TREE_TYPE (ret) = void_type_node;
42930 OMP_FOR_BODY (ret) = body;
42931 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
42932 SET_EXPR_LOCATION (ret, loc);
42933 add_stmt (ret);
42934 return ret;
42935 }
42936 }
42937 if (!flag_openmp) /* flag_openmp_simd */
42938 {
42939 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42940 return NULL_TREE;
42941 }
42942
42943 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
42944 cclauses == NULL);
42945 if (cclauses)
42946 {
42947 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
42948 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
42949 }
42950
42951 keep_next_level (true);
42952 sb = begin_omp_structured_block ();
42953 save = cp_parser_begin_omp_structured_block (parser);
42954
42955 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
42956 if_p);
42957
42958 cp_parser_end_omp_structured_block (parser, save);
42959 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
42960
42961 return ret;
42962 }
42963
42964
42965 /* OpenACC 2.0:
42966 # pragma acc routine oacc-routine-clause[optseq] new-line
42967 function-definition
42968
42969 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
42970 */
42971
42972 #define OACC_ROUTINE_CLAUSE_MASK \
42973 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
42974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
42975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
42976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
42977
42978
42979 /* Parse the OpenACC routine pragma. This has an optional '( name )'
42980 component, which must resolve to a declared namespace-scope
42981 function. The clauses are either processed directly (for a named
42982 function), or defered until the immediatley following declaration
42983 is parsed. */
42984
42985 static void
42986 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
42987 enum pragma_context context)
42988 {
42989 gcc_checking_assert (context == pragma_external);
42990 /* The checking for "another pragma following this one" in the "no optional
42991 '( name )'" case makes sure that we dont re-enter. */
42992 gcc_checking_assert (parser->oacc_routine == NULL);
42993
42994 cp_oacc_routine_data data;
42995 data.error_seen = false;
42996 data.fndecl_seen = false;
42997 data.tokens = vNULL;
42998 data.clauses = NULL_TREE;
42999 data.loc = pragma_tok->location;
43000 /* It is safe to take the address of a local variable; it will only be
43001 used while this scope is live. */
43002 parser->oacc_routine = &data;
43003
43004 /* Look for optional '( name )'. */
43005 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
43006 {
43007 matching_parens parens;
43008 parens.consume_open (parser); /* '(' */
43009
43010 /* We parse the name as an id-expression. If it resolves to
43011 anything other than a non-overloaded function at namespace
43012 scope, it's an error. */
43013 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
43014 tree name = cp_parser_id_expression (parser,
43015 /*template_keyword_p=*/false,
43016 /*check_dependency_p=*/false,
43017 /*template_p=*/NULL,
43018 /*declarator_p=*/false,
43019 /*optional_p=*/false);
43020 tree decl = (identifier_p (name)
43021 ? cp_parser_lookup_name_simple (parser, name, name_loc)
43022 : name);
43023 if (name != error_mark_node && decl == error_mark_node)
43024 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
43025
43026 if (decl == error_mark_node
43027 || !parens.require_close (parser))
43028 {
43029 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43030 parser->oacc_routine = NULL;
43031 return;
43032 }
43033
43034 data.clauses
43035 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
43036 "#pragma acc routine",
43037 cp_lexer_peek_token (parser->lexer));
43038 /* The clauses are in reverse order; fix that to make later diagnostic
43039 emission easier. */
43040 data.clauses = nreverse (data.clauses);
43041
43042 if (decl && is_overloaded_fn (decl)
43043 && (TREE_CODE (decl) != FUNCTION_DECL
43044 || DECL_FUNCTION_TEMPLATE_P (decl)))
43045 {
43046 error_at (name_loc,
43047 "%<#pragma acc routine%> names a set of overloads");
43048 parser->oacc_routine = NULL;
43049 return;
43050 }
43051
43052 /* Perhaps we should use the same rule as declarations in different
43053 namespaces? */
43054 if (!DECL_NAMESPACE_SCOPE_P (decl))
43055 {
43056 error_at (name_loc,
43057 "%qD does not refer to a namespace scope function", decl);
43058 parser->oacc_routine = NULL;
43059 return;
43060 }
43061
43062 if (TREE_CODE (decl) != FUNCTION_DECL)
43063 {
43064 error_at (name_loc, "%qD does not refer to a function", decl);
43065 parser->oacc_routine = NULL;
43066 return;
43067 }
43068
43069 cp_finalize_oacc_routine (parser, decl, false);
43070 parser->oacc_routine = NULL;
43071 }
43072 else /* No optional '( name )'. */
43073 {
43074 /* Store away all pragma tokens. */
43075 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
43076 cp_lexer_consume_token (parser->lexer);
43077 cp_parser_require_pragma_eol (parser, pragma_tok);
43078 struct cp_token_cache *cp
43079 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
43080 parser->oacc_routine->tokens.safe_push (cp);
43081
43082 /* Emit a helpful diagnostic if there's another pragma following this
43083 one. */
43084 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
43085 {
43086 cp_ensure_no_oacc_routine (parser);
43087 data.tokens.release ();
43088 /* ..., and then just keep going. */
43089 return;
43090 }
43091
43092 /* We only have to consider the pragma_external case here. */
43093 cp_parser_declaration (parser);
43094 if (parser->oacc_routine
43095 && !parser->oacc_routine->fndecl_seen)
43096 cp_ensure_no_oacc_routine (parser);
43097 else
43098 parser->oacc_routine = NULL;
43099 data.tokens.release ();
43100 }
43101 }
43102
43103 /* Finalize #pragma acc routine clauses after direct declarator has
43104 been parsed. */
43105
43106 static tree
43107 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
43108 {
43109 struct cp_token_cache *ce;
43110 cp_oacc_routine_data *data = parser->oacc_routine;
43111
43112 if (!data->error_seen && data->fndecl_seen)
43113 {
43114 error_at (data->loc,
43115 "%<#pragma acc routine%> not immediately followed by "
43116 "a single function declaration or definition");
43117 data->error_seen = true;
43118 }
43119 if (data->error_seen)
43120 return attrs;
43121
43122 gcc_checking_assert (data->tokens.length () == 1);
43123 ce = data->tokens[0];
43124
43125 cp_parser_push_lexer_for_tokens (parser, ce);
43126 parser->lexer->in_pragma = true;
43127 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
43128
43129 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
43130 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
43131 parser->oacc_routine->clauses
43132 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
43133 "#pragma acc routine", pragma_tok);
43134 /* The clauses are in reverse order; fix that to make later diagnostic
43135 emission easier. */
43136 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
43137 cp_parser_pop_lexer (parser);
43138 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
43139 fndecl_seen. */
43140
43141 return attrs;
43142 }
43143
43144 /* Apply any saved OpenACC routine clauses to a just-parsed
43145 declaration. */
43146
43147 static void
43148 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
43149 {
43150 if (__builtin_expect (parser->oacc_routine != NULL, 0))
43151 {
43152 /* Keep going if we're in error reporting mode. */
43153 if (parser->oacc_routine->error_seen
43154 || fndecl == error_mark_node)
43155 return;
43156
43157 if (parser->oacc_routine->fndecl_seen)
43158 {
43159 error_at (parser->oacc_routine->loc,
43160 "%<#pragma acc routine%> not immediately followed by"
43161 " a single function declaration or definition");
43162 parser->oacc_routine = NULL;
43163 return;
43164 }
43165 if (TREE_CODE (fndecl) != FUNCTION_DECL)
43166 {
43167 cp_ensure_no_oacc_routine (parser);
43168 return;
43169 }
43170
43171 int compatible
43172 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
43173 parser->oacc_routine->loc,
43174 "#pragma acc routine");
43175 if (compatible < 0)
43176 {
43177 parser->oacc_routine = NULL;
43178 return;
43179 }
43180 if (compatible > 0)
43181 {
43182 }
43183 else
43184 {
43185 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
43186 {
43187 error_at (parser->oacc_routine->loc,
43188 TREE_USED (fndecl)
43189 ? G_("%<#pragma acc routine%> must be applied before"
43190 " use")
43191 : G_("%<#pragma acc routine%> must be applied before"
43192 " definition"));
43193 parser->oacc_routine = NULL;
43194 return;
43195 }
43196
43197 /* Set the routine's level of parallelism. */
43198 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
43199 oacc_replace_fn_attrib (fndecl, dims);
43200
43201 /* Add an "omp declare target" attribute. */
43202 DECL_ATTRIBUTES (fndecl)
43203 = tree_cons (get_identifier ("omp declare target"),
43204 parser->oacc_routine->clauses,
43205 DECL_ATTRIBUTES (fndecl));
43206 }
43207
43208 /* Don't unset parser->oacc_routine here: we may still need it to
43209 diagnose wrong usage. But, remember that we've used this "#pragma acc
43210 routine". */
43211 parser->oacc_routine->fndecl_seen = true;
43212 }
43213 }
43214
43215 /* Main entry point to OpenMP statement pragmas. */
43216
43217 static void
43218 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43219 {
43220 tree stmt;
43221 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
43222 omp_clause_mask mask (0);
43223
43224 switch (cp_parser_pragma_kind (pragma_tok))
43225 {
43226 case PRAGMA_OACC_ATOMIC:
43227 cp_parser_omp_atomic (parser, pragma_tok);
43228 return;
43229 case PRAGMA_OACC_CACHE:
43230 stmt = cp_parser_oacc_cache (parser, pragma_tok);
43231 break;
43232 case PRAGMA_OACC_DATA:
43233 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
43234 break;
43235 case PRAGMA_OACC_ENTER_DATA:
43236 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
43237 break;
43238 case PRAGMA_OACC_EXIT_DATA:
43239 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
43240 break;
43241 case PRAGMA_OACC_HOST_DATA:
43242 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
43243 break;
43244 case PRAGMA_OACC_KERNELS:
43245 case PRAGMA_OACC_PARALLEL:
43246 case PRAGMA_OACC_SERIAL:
43247 strcpy (p_name, "#pragma acc");
43248 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
43249 break;
43250 case PRAGMA_OACC_LOOP:
43251 strcpy (p_name, "#pragma acc");
43252 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
43253 if_p);
43254 break;
43255 case PRAGMA_OACC_UPDATE:
43256 stmt = cp_parser_oacc_update (parser, pragma_tok);
43257 break;
43258 case PRAGMA_OACC_WAIT:
43259 stmt = cp_parser_oacc_wait (parser, pragma_tok);
43260 break;
43261 case PRAGMA_OMP_ATOMIC:
43262 cp_parser_omp_atomic (parser, pragma_tok);
43263 return;
43264 case PRAGMA_OMP_CRITICAL:
43265 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
43266 break;
43267 case PRAGMA_OMP_DISTRIBUTE:
43268 strcpy (p_name, "#pragma omp");
43269 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
43270 if_p);
43271 break;
43272 case PRAGMA_OMP_FOR:
43273 strcpy (p_name, "#pragma omp");
43274 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
43275 if_p);
43276 break;
43277 case PRAGMA_OMP_LOOP:
43278 strcpy (p_name, "#pragma omp");
43279 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
43280 if_p);
43281 break;
43282 case PRAGMA_OMP_MASTER:
43283 strcpy (p_name, "#pragma omp");
43284 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
43285 if_p);
43286 break;
43287 case PRAGMA_OMP_PARALLEL:
43288 strcpy (p_name, "#pragma omp");
43289 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
43290 if_p);
43291 break;
43292 case PRAGMA_OMP_SECTIONS:
43293 strcpy (p_name, "#pragma omp");
43294 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
43295 break;
43296 case PRAGMA_OMP_SIMD:
43297 strcpy (p_name, "#pragma omp");
43298 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
43299 if_p);
43300 break;
43301 case PRAGMA_OMP_SINGLE:
43302 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
43303 break;
43304 case PRAGMA_OMP_TASK:
43305 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
43306 break;
43307 case PRAGMA_OMP_TASKGROUP:
43308 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
43309 break;
43310 case PRAGMA_OMP_TASKLOOP:
43311 strcpy (p_name, "#pragma omp");
43312 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
43313 if_p);
43314 break;
43315 case PRAGMA_OMP_TEAMS:
43316 strcpy (p_name, "#pragma omp");
43317 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
43318 if_p);
43319 break;
43320 default:
43321 gcc_unreachable ();
43322 }
43323
43324 protected_set_expr_location (stmt, pragma_tok->location);
43325 }
43326 \f
43327 /* Transactional Memory parsing routines. */
43328
43329 /* Parse a transaction attribute.
43330
43331 txn-attribute:
43332 attribute
43333 [ [ identifier ] ]
43334
43335 We use this instead of cp_parser_attributes_opt for transactions to avoid
43336 the pedwarn in C++98 mode. */
43337
43338 static tree
43339 cp_parser_txn_attribute_opt (cp_parser *parser)
43340 {
43341 cp_token *token;
43342 tree attr_name, attr = NULL;
43343
43344 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
43345 return cp_parser_attributes_opt (parser);
43346
43347 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
43348 return NULL_TREE;
43349 cp_lexer_consume_token (parser->lexer);
43350 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
43351 goto error1;
43352
43353 token = cp_lexer_peek_token (parser->lexer);
43354 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
43355 {
43356 token = cp_lexer_consume_token (parser->lexer);
43357
43358 attr_name = (token->type == CPP_KEYWORD
43359 /* For keywords, use the canonical spelling,
43360 not the parsed identifier. */
43361 ? ridpointers[(int) token->keyword]
43362 : token->u.value);
43363 attr = build_tree_list (attr_name, NULL_TREE);
43364 }
43365 else
43366 cp_parser_error (parser, "expected identifier");
43367
43368 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
43369 error1:
43370 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
43371 return attr;
43372 }
43373
43374 /* Parse a __transaction_atomic or __transaction_relaxed statement.
43375
43376 transaction-statement:
43377 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
43378 compound-statement
43379 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
43380 */
43381
43382 static tree
43383 cp_parser_transaction (cp_parser *parser, cp_token *token)
43384 {
43385 unsigned char old_in = parser->in_transaction;
43386 unsigned char this_in = 1, new_in;
43387 enum rid keyword = token->keyword;
43388 tree stmt, attrs, noex;
43389
43390 cp_lexer_consume_token (parser->lexer);
43391
43392 if (keyword == RID_TRANSACTION_RELAXED
43393 || keyword == RID_SYNCHRONIZED)
43394 this_in |= TM_STMT_ATTR_RELAXED;
43395 else
43396 {
43397 attrs = cp_parser_txn_attribute_opt (parser);
43398 if (attrs)
43399 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
43400 }
43401
43402 /* Parse a noexcept specification. */
43403 if (keyword == RID_ATOMIC_NOEXCEPT)
43404 noex = boolean_true_node;
43405 else if (keyword == RID_ATOMIC_CANCEL)
43406 {
43407 /* cancel-and-throw is unimplemented. */
43408 sorry ("%<atomic_cancel%>");
43409 noex = NULL_TREE;
43410 }
43411 else
43412 noex = cp_parser_noexcept_specification_opt (parser,
43413 CP_PARSER_FLAGS_NONE,
43414 /*require_constexpr=*/true,
43415 /*consumed_expr=*/NULL,
43416 /*return_cond=*/true);
43417
43418 /* Keep track if we're in the lexical scope of an outer transaction. */
43419 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
43420
43421 stmt = begin_transaction_stmt (token->location, NULL, this_in);
43422
43423 parser->in_transaction = new_in;
43424 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
43425 parser->in_transaction = old_in;
43426
43427 finish_transaction_stmt (stmt, NULL, this_in, noex);
43428
43429 return stmt;
43430 }
43431
43432 /* Parse a __transaction_atomic or __transaction_relaxed expression.
43433
43434 transaction-expression:
43435 __transaction_atomic txn-noexcept-spec[opt] ( expression )
43436 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
43437 */
43438
43439 static tree
43440 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
43441 {
43442 unsigned char old_in = parser->in_transaction;
43443 unsigned char this_in = 1;
43444 cp_token *token;
43445 tree expr, noex;
43446 bool noex_expr;
43447 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43448
43449 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
43450 || keyword == RID_TRANSACTION_RELAXED);
43451
43452 if (!flag_tm)
43453 error_at (loc,
43454 keyword == RID_TRANSACTION_RELAXED
43455 ? G_("%<__transaction_relaxed%> without transactional memory "
43456 "support enabled")
43457 : G_("%<__transaction_atomic%> without transactional memory "
43458 "support enabled"));
43459
43460 token = cp_parser_require_keyword (parser, keyword,
43461 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
43462 : RT_TRANSACTION_RELAXED));
43463 gcc_assert (token != NULL);
43464
43465 if (keyword == RID_TRANSACTION_RELAXED)
43466 this_in |= TM_STMT_ATTR_RELAXED;
43467
43468 /* Set this early. This might mean that we allow transaction_cancel in
43469 an expression that we find out later actually has to be a constexpr.
43470 However, we expect that cxx_constant_value will be able to deal with
43471 this; also, if the noexcept has no constexpr, then what we parse next
43472 really is a transaction's body. */
43473 parser->in_transaction = this_in;
43474
43475 /* Parse a noexcept specification. */
43476 noex = cp_parser_noexcept_specification_opt (parser,
43477 CP_PARSER_FLAGS_NONE,
43478 /*require_constexpr=*/false,
43479 &noex_expr,
43480 /*return_cond=*/true);
43481
43482 if (!noex || !noex_expr
43483 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
43484 {
43485 matching_parens parens;
43486 parens.require_open (parser);
43487
43488 expr = cp_parser_expression (parser);
43489 expr = finish_parenthesized_expr (expr);
43490
43491 parens.require_close (parser);
43492 }
43493 else
43494 {
43495 /* The only expression that is available got parsed for the noexcept
43496 already. noexcept is true then. */
43497 expr = noex;
43498 noex = boolean_true_node;
43499 }
43500
43501 expr = build_transaction_expr (token->location, expr, this_in, noex);
43502 parser->in_transaction = old_in;
43503
43504 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
43505 return error_mark_node;
43506
43507 return (flag_tm ? expr : error_mark_node);
43508 }
43509
43510 /* Parse a function-transaction-block.
43511
43512 function-transaction-block:
43513 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
43514 function-body
43515 __transaction_atomic txn-attribute[opt] function-try-block
43516 __transaction_relaxed ctor-initializer[opt] function-body
43517 __transaction_relaxed function-try-block
43518 */
43519
43520 static void
43521 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
43522 {
43523 unsigned char old_in = parser->in_transaction;
43524 unsigned char new_in = 1;
43525 tree compound_stmt, stmt, attrs;
43526 cp_token *token;
43527
43528 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
43529 || keyword == RID_TRANSACTION_RELAXED);
43530 token = cp_parser_require_keyword (parser, keyword,
43531 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
43532 : RT_TRANSACTION_RELAXED));
43533 gcc_assert (token != NULL);
43534
43535 if (keyword == RID_TRANSACTION_RELAXED)
43536 new_in |= TM_STMT_ATTR_RELAXED;
43537 else
43538 {
43539 attrs = cp_parser_txn_attribute_opt (parser);
43540 if (attrs)
43541 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
43542 }
43543
43544 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
43545
43546 parser->in_transaction = new_in;
43547
43548 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
43549 cp_parser_function_try_block (parser);
43550 else
43551 cp_parser_ctor_initializer_opt_and_function_body
43552 (parser, /*in_function_try_block=*/false);
43553
43554 parser->in_transaction = old_in;
43555
43556 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
43557 }
43558
43559 /* Parse a __transaction_cancel statement.
43560
43561 cancel-statement:
43562 __transaction_cancel txn-attribute[opt] ;
43563 __transaction_cancel txn-attribute[opt] throw-expression ;
43564
43565 ??? Cancel and throw is not yet implemented. */
43566
43567 static tree
43568 cp_parser_transaction_cancel (cp_parser *parser)
43569 {
43570 cp_token *token;
43571 bool is_outer = false;
43572 tree stmt, attrs;
43573
43574 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
43575 RT_TRANSACTION_CANCEL);
43576 gcc_assert (token != NULL);
43577
43578 attrs = cp_parser_txn_attribute_opt (parser);
43579 if (attrs)
43580 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
43581
43582 /* ??? Parse cancel-and-throw here. */
43583
43584 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
43585
43586 if (!flag_tm)
43587 {
43588 error_at (token->location, "%<__transaction_cancel%> without "
43589 "transactional memory support enabled");
43590 return error_mark_node;
43591 }
43592 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
43593 {
43594 error_at (token->location, "%<__transaction_cancel%> within a "
43595 "%<__transaction_relaxed%>");
43596 return error_mark_node;
43597 }
43598 else if (is_outer)
43599 {
43600 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
43601 && !is_tm_may_cancel_outer (current_function_decl))
43602 {
43603 error_at (token->location, "outer %<__transaction_cancel%> not "
43604 "within outer %<__transaction_atomic%>");
43605 error_at (token->location,
43606 " or a %<transaction_may_cancel_outer%> function");
43607 return error_mark_node;
43608 }
43609 }
43610 else if (parser->in_transaction == 0)
43611 {
43612 error_at (token->location, "%<__transaction_cancel%> not within "
43613 "%<__transaction_atomic%>");
43614 return error_mark_node;
43615 }
43616
43617 stmt = build_tm_abort_call (token->location, is_outer);
43618 add_stmt (stmt);
43619
43620 return stmt;
43621 }
43622 \f
43623 /* The parser. */
43624
43625 static GTY (()) cp_parser *the_parser;
43626
43627 \f
43628 /* Special handling for the first token or line in the file. The first
43629 thing in the file might be #pragma GCC pch_preprocess, which loads a
43630 PCH file, which is a GC collection point. So we need to handle this
43631 first pragma without benefit of an existing lexer structure.
43632
43633 Always returns one token to the caller in *FIRST_TOKEN. This is
43634 either the true first token of the file, or the first token after
43635 the initial pragma. */
43636
43637 static void
43638 cp_parser_initial_pragma (cp_token *first_token)
43639 {
43640 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
43641 return;
43642
43643 cp_lexer_get_preprocessor_token (0, first_token);
43644
43645 tree name = NULL;
43646 if (first_token->type == CPP_STRING)
43647 {
43648 name = first_token->u.value;
43649
43650 cp_lexer_get_preprocessor_token (0, first_token);
43651 }
43652
43653 /* Skip to the end of the pragma. */
43654 if (first_token->type != CPP_PRAGMA_EOL)
43655 {
43656 error_at (first_token->location,
43657 "malformed %<#pragma GCC pch_preprocess%>");
43658 do
43659 cp_lexer_get_preprocessor_token (0, first_token);
43660 while (first_token->type != CPP_PRAGMA_EOL);
43661 }
43662
43663 /* Now actually load the PCH file. */
43664 if (name)
43665 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
43666
43667 /* Read one more token to return to our caller. We have to do this
43668 after reading the PCH file in, since its pointers have to be
43669 live. */
43670 cp_lexer_get_preprocessor_token (0, first_token);
43671 }
43672
43673 /* Parse a pragma GCC ivdep. */
43674
43675 static bool
43676 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
43677 {
43678 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43679 return true;
43680 }
43681
43682 /* Parse a pragma GCC unroll. */
43683
43684 static unsigned short
43685 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
43686 {
43687 location_t location = cp_lexer_peek_token (parser->lexer)->location;
43688 tree expr = cp_parser_constant_expression (parser);
43689 unsigned short unroll;
43690 expr = maybe_constant_value (expr);
43691 HOST_WIDE_INT lunroll = 0;
43692 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
43693 || TREE_CODE (expr) != INTEGER_CST
43694 || (lunroll = tree_to_shwi (expr)) < 0
43695 || lunroll >= USHRT_MAX)
43696 {
43697 error_at (location, "%<#pragma GCC unroll%> requires an"
43698 " assignment-expression that evaluates to a non-negative"
43699 " integral constant less than %u", USHRT_MAX);
43700 unroll = 0;
43701 }
43702 else
43703 {
43704 unroll = (unsigned short)lunroll;
43705 if (unroll == 0)
43706 unroll = 1;
43707 }
43708 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43709 return unroll;
43710 }
43711
43712 /* Normal parsing of a pragma token. Here we can (and must) use the
43713 regular lexer. */
43714
43715 static bool
43716 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
43717 {
43718 cp_token *pragma_tok;
43719 unsigned int id;
43720 tree stmt;
43721 bool ret;
43722
43723 pragma_tok = cp_lexer_consume_token (parser->lexer);
43724 gcc_assert (pragma_tok->type == CPP_PRAGMA);
43725 parser->lexer->in_pragma = true;
43726
43727 id = cp_parser_pragma_kind (pragma_tok);
43728 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
43729 cp_ensure_no_omp_declare_simd (parser);
43730 switch (id)
43731 {
43732 case PRAGMA_GCC_PCH_PREPROCESS:
43733 error_at (pragma_tok->location,
43734 "%<#pragma GCC pch_preprocess%> must be first");
43735 break;
43736
43737 case PRAGMA_OMP_BARRIER:
43738 switch (context)
43739 {
43740 case pragma_compound:
43741 cp_parser_omp_barrier (parser, pragma_tok);
43742 return false;
43743 case pragma_stmt:
43744 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
43745 "used in compound statements", "omp barrier");
43746 break;
43747 default:
43748 goto bad_stmt;
43749 }
43750 break;
43751
43752 case PRAGMA_OMP_DEPOBJ:
43753 switch (context)
43754 {
43755 case pragma_compound:
43756 cp_parser_omp_depobj (parser, pragma_tok);
43757 return false;
43758 case pragma_stmt:
43759 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
43760 "used in compound statements", "omp depobj");
43761 break;
43762 default:
43763 goto bad_stmt;
43764 }
43765 break;
43766
43767 case PRAGMA_OMP_FLUSH:
43768 switch (context)
43769 {
43770 case pragma_compound:
43771 cp_parser_omp_flush (parser, pragma_tok);
43772 return false;
43773 case pragma_stmt:
43774 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
43775 "used in compound statements", "omp flush");
43776 break;
43777 default:
43778 goto bad_stmt;
43779 }
43780 break;
43781
43782 case PRAGMA_OMP_TASKWAIT:
43783 switch (context)
43784 {
43785 case pragma_compound:
43786 cp_parser_omp_taskwait (parser, pragma_tok);
43787 return false;
43788 case pragma_stmt:
43789 error_at (pragma_tok->location,
43790 "%<#pragma %s%> may only be used in compound statements",
43791 "omp taskwait");
43792 break;
43793 default:
43794 goto bad_stmt;
43795 }
43796 break;
43797
43798 case PRAGMA_OMP_TASKYIELD:
43799 switch (context)
43800 {
43801 case pragma_compound:
43802 cp_parser_omp_taskyield (parser, pragma_tok);
43803 return false;
43804 case pragma_stmt:
43805 error_at (pragma_tok->location,
43806 "%<#pragma %s%> may only be used in compound statements",
43807 "omp taskyield");
43808 break;
43809 default:
43810 goto bad_stmt;
43811 }
43812 break;
43813
43814 case PRAGMA_OMP_CANCEL:
43815 switch (context)
43816 {
43817 case pragma_compound:
43818 cp_parser_omp_cancel (parser, pragma_tok);
43819 return false;
43820 case pragma_stmt:
43821 error_at (pragma_tok->location,
43822 "%<#pragma %s%> may only be used in compound statements",
43823 "omp cancel");
43824 break;
43825 default:
43826 goto bad_stmt;
43827 }
43828 break;
43829
43830 case PRAGMA_OMP_CANCELLATION_POINT:
43831 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
43832 return false;
43833
43834 case PRAGMA_OMP_THREADPRIVATE:
43835 cp_parser_omp_threadprivate (parser, pragma_tok);
43836 return false;
43837
43838 case PRAGMA_OMP_DECLARE:
43839 return cp_parser_omp_declare (parser, pragma_tok, context);
43840
43841 case PRAGMA_OACC_DECLARE:
43842 cp_parser_oacc_declare (parser, pragma_tok);
43843 return false;
43844
43845 case PRAGMA_OACC_ENTER_DATA:
43846 if (context == pragma_stmt)
43847 {
43848 error_at (pragma_tok->location,
43849 "%<#pragma %s%> may only be used in compound statements",
43850 "acc enter data");
43851 break;
43852 }
43853 else if (context != pragma_compound)
43854 goto bad_stmt;
43855 cp_parser_omp_construct (parser, pragma_tok, if_p);
43856 return true;
43857
43858 case PRAGMA_OACC_EXIT_DATA:
43859 if (context == pragma_stmt)
43860 {
43861 error_at (pragma_tok->location,
43862 "%<#pragma %s%> may only be used in compound statements",
43863 "acc exit data");
43864 break;
43865 }
43866 else if (context != pragma_compound)
43867 goto bad_stmt;
43868 cp_parser_omp_construct (parser, pragma_tok, if_p);
43869 return true;
43870
43871 case PRAGMA_OACC_ROUTINE:
43872 if (context != pragma_external)
43873 {
43874 error_at (pragma_tok->location,
43875 "%<#pragma acc routine%> must be at file scope");
43876 break;
43877 }
43878 cp_parser_oacc_routine (parser, pragma_tok, context);
43879 return false;
43880
43881 case PRAGMA_OACC_UPDATE:
43882 if (context == pragma_stmt)
43883 {
43884 error_at (pragma_tok->location,
43885 "%<#pragma %s%> may only be used in compound statements",
43886 "acc update");
43887 break;
43888 }
43889 else if (context != pragma_compound)
43890 goto bad_stmt;
43891 cp_parser_omp_construct (parser, pragma_tok, if_p);
43892 return true;
43893
43894 case PRAGMA_OACC_WAIT:
43895 if (context == pragma_stmt)
43896 {
43897 error_at (pragma_tok->location,
43898 "%<#pragma %s%> may only be used in compound statements",
43899 "acc wait");
43900 break;
43901 }
43902 else if (context != pragma_compound)
43903 goto bad_stmt;
43904 cp_parser_omp_construct (parser, pragma_tok, if_p);
43905 return true;
43906
43907 case PRAGMA_OACC_ATOMIC:
43908 case PRAGMA_OACC_CACHE:
43909 case PRAGMA_OACC_DATA:
43910 case PRAGMA_OACC_HOST_DATA:
43911 case PRAGMA_OACC_KERNELS:
43912 case PRAGMA_OACC_LOOP:
43913 case PRAGMA_OACC_PARALLEL:
43914 case PRAGMA_OACC_SERIAL:
43915 case PRAGMA_OMP_ATOMIC:
43916 case PRAGMA_OMP_CRITICAL:
43917 case PRAGMA_OMP_DISTRIBUTE:
43918 case PRAGMA_OMP_FOR:
43919 case PRAGMA_OMP_LOOP:
43920 case PRAGMA_OMP_MASTER:
43921 case PRAGMA_OMP_PARALLEL:
43922 case PRAGMA_OMP_SECTIONS:
43923 case PRAGMA_OMP_SIMD:
43924 case PRAGMA_OMP_SINGLE:
43925 case PRAGMA_OMP_TASK:
43926 case PRAGMA_OMP_TASKGROUP:
43927 case PRAGMA_OMP_TASKLOOP:
43928 case PRAGMA_OMP_TEAMS:
43929 if (context != pragma_stmt && context != pragma_compound)
43930 goto bad_stmt;
43931 stmt = push_omp_privatization_clauses (false);
43932 cp_parser_omp_construct (parser, pragma_tok, if_p);
43933 pop_omp_privatization_clauses (stmt);
43934 return true;
43935
43936 case PRAGMA_OMP_REQUIRES:
43937 if (context != pragma_external)
43938 {
43939 error_at (pragma_tok->location,
43940 "%<#pragma omp requires%> may only be used at file or "
43941 "namespace scope");
43942 break;
43943 }
43944 return cp_parser_omp_requires (parser, pragma_tok);
43945
43946 case PRAGMA_OMP_ORDERED:
43947 if (context != pragma_stmt && context != pragma_compound)
43948 goto bad_stmt;
43949 stmt = push_omp_privatization_clauses (false);
43950 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
43951 pop_omp_privatization_clauses (stmt);
43952 return ret;
43953
43954 case PRAGMA_OMP_TARGET:
43955 if (context != pragma_stmt && context != pragma_compound)
43956 goto bad_stmt;
43957 stmt = push_omp_privatization_clauses (false);
43958 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
43959 pop_omp_privatization_clauses (stmt);
43960 return ret;
43961
43962 case PRAGMA_OMP_END_DECLARE_TARGET:
43963 cp_parser_omp_end_declare_target (parser, pragma_tok);
43964 return false;
43965
43966 case PRAGMA_OMP_SCAN:
43967 error_at (pragma_tok->location,
43968 "%<#pragma omp scan%> may only be used in "
43969 "a loop construct with %<inscan%> %<reduction%> clause");
43970 break;
43971
43972 case PRAGMA_OMP_SECTION:
43973 error_at (pragma_tok->location,
43974 "%<#pragma omp section%> may only be used in "
43975 "%<#pragma omp sections%> construct");
43976 break;
43977
43978 case PRAGMA_IVDEP:
43979 {
43980 if (context == pragma_external)
43981 {
43982 error_at (pragma_tok->location,
43983 "%<#pragma GCC ivdep%> must be inside a function");
43984 break;
43985 }
43986 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
43987 unsigned short unroll;
43988 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
43989 if (tok->type == CPP_PRAGMA
43990 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
43991 {
43992 tok = cp_lexer_consume_token (parser->lexer);
43993 unroll = cp_parser_pragma_unroll (parser, tok);
43994 tok = cp_lexer_peek_token (the_parser->lexer);
43995 }
43996 else
43997 unroll = 0;
43998 if (tok->type != CPP_KEYWORD
43999 || (tok->keyword != RID_FOR
44000 && tok->keyword != RID_WHILE
44001 && tok->keyword != RID_DO))
44002 {
44003 cp_parser_error (parser, "for, while or do statement expected");
44004 return false;
44005 }
44006 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
44007 return true;
44008 }
44009
44010 case PRAGMA_UNROLL:
44011 {
44012 if (context == pragma_external)
44013 {
44014 error_at (pragma_tok->location,
44015 "%<#pragma GCC unroll%> must be inside a function");
44016 break;
44017 }
44018 const unsigned short unroll
44019 = cp_parser_pragma_unroll (parser, pragma_tok);
44020 bool ivdep;
44021 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
44022 if (tok->type == CPP_PRAGMA
44023 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
44024 {
44025 tok = cp_lexer_consume_token (parser->lexer);
44026 ivdep = cp_parser_pragma_ivdep (parser, tok);
44027 tok = cp_lexer_peek_token (the_parser->lexer);
44028 }
44029 else
44030 ivdep = false;
44031 if (tok->type != CPP_KEYWORD
44032 || (tok->keyword != RID_FOR
44033 && tok->keyword != RID_WHILE
44034 && tok->keyword != RID_DO))
44035 {
44036 cp_parser_error (parser, "for, while or do statement expected");
44037 return false;
44038 }
44039 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
44040 return true;
44041 }
44042
44043 default:
44044 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
44045 c_invoke_pragma_handler (id);
44046 break;
44047
44048 bad_stmt:
44049 cp_parser_error (parser, "expected declaration specifiers");
44050 break;
44051 }
44052
44053 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44054 return false;
44055 }
44056
44057 /* The interface the pragma parsers have to the lexer. */
44058
44059 enum cpp_ttype
44060 pragma_lex (tree *value, location_t *loc)
44061 {
44062 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
44063 enum cpp_ttype ret = tok->type;
44064
44065 *value = tok->u.value;
44066 if (loc)
44067 *loc = tok->location;
44068
44069 if (ret == CPP_PRAGMA_EOL)
44070 ret = CPP_EOF;
44071 else if (ret == CPP_STRING)
44072 *value = cp_parser_string_literal (the_parser, false, false);
44073 else
44074 {
44075 if (ret == CPP_KEYWORD)
44076 ret = CPP_NAME;
44077 cp_lexer_consume_token (the_parser->lexer);
44078 }
44079
44080 return ret;
44081 }
44082
44083 \f
44084 /* External interface. */
44085
44086 /* Parse one entire translation unit. */
44087
44088 void
44089 c_parse_file (void)
44090 {
44091 static bool already_called = false;
44092
44093 if (already_called)
44094 fatal_error (input_location,
44095 "multi-source compilation not implemented for C++");
44096 already_called = true;
44097
44098 /* cp_lexer_new_main is called before doing any GC allocation
44099 because tokenization might load a PCH file. */
44100 cp_lexer *lexer = cp_lexer_new_main ();
44101
44102 the_parser = cp_parser_new (lexer);
44103
44104 cp_parser_translation_unit (the_parser);
44105 class_decl_loc_t::diag_mismatched_tags ();
44106
44107 the_parser = NULL;
44108
44109 finish_translation_unit ();
44110 }
44111
44112 /* Create an identifier for a generic parameter type (a synthesized
44113 template parameter implied by `auto' or a concept identifier). */
44114
44115 static GTY(()) int generic_parm_count;
44116 static tree
44117 make_generic_type_name ()
44118 {
44119 char buf[32];
44120 sprintf (buf, "auto:%d", ++generic_parm_count);
44121 return get_identifier (buf);
44122 }
44123
44124 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
44125 (creating a new template parameter list if necessary). Returns the newly
44126 created template type parm. */
44127
44128 static tree
44129 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
44130 {
44131 /* A requires-clause is not a function and cannot have placeholders. */
44132 if (current_binding_level->kind == sk_block)
44133 {
44134 error ("placeholder type not allowed in this context");
44135 return error_mark_node;
44136 }
44137
44138 gcc_assert (current_binding_level->kind == sk_function_parms);
44139
44140 /* We are either continuing a function template that already contains implicit
44141 template parameters, creating a new fully-implicit function template, or
44142 extending an existing explicit function template with implicit template
44143 parameters. */
44144
44145 cp_binding_level *const entry_scope = current_binding_level;
44146
44147 bool become_template = false;
44148 cp_binding_level *parent_scope = 0;
44149
44150 if (parser->implicit_template_scope)
44151 {
44152 gcc_assert (parser->implicit_template_parms);
44153
44154 current_binding_level = parser->implicit_template_scope;
44155 }
44156 else
44157 {
44158 /* Roll back to the existing template parameter scope (in the case of
44159 extending an explicit function template) or introduce a new template
44160 parameter scope ahead of the function parameter scope (or class scope
44161 in the case of out-of-line member definitions). The function scope is
44162 added back after template parameter synthesis below. */
44163
44164 cp_binding_level *scope = entry_scope;
44165
44166 while (scope->kind == sk_function_parms)
44167 {
44168 parent_scope = scope;
44169 scope = scope->level_chain;
44170 }
44171 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
44172 {
44173 /* If not defining a class, then any class scope is a scope level in
44174 an out-of-line member definition. In this case simply wind back
44175 beyond the first such scope to inject the template parameter list.
44176 Otherwise wind back to the class being defined. The latter can
44177 occur in class member friend declarations such as:
44178
44179 class A {
44180 void foo (auto);
44181 };
44182 class B {
44183 friend void A::foo (auto);
44184 };
44185
44186 The template parameter list synthesized for the friend declaration
44187 must be injected in the scope of 'B'. This can also occur in
44188 erroneous cases such as:
44189
44190 struct A {
44191 struct B {
44192 void foo (auto);
44193 };
44194 void B::foo (auto) {}
44195 };
44196
44197 Here the attempted definition of 'B::foo' within 'A' is ill-formed
44198 but, nevertheless, the template parameter list synthesized for the
44199 declarator should be injected into the scope of 'A' as if the
44200 ill-formed template was specified explicitly. */
44201
44202 while (scope->kind == sk_class && !scope->defining_class_p)
44203 {
44204 parent_scope = scope;
44205 scope = scope->level_chain;
44206 }
44207 }
44208
44209 current_binding_level = scope;
44210
44211 if (scope->kind != sk_template_parms
44212 || !function_being_declared_is_template_p (parser))
44213 {
44214 /* Introduce a new template parameter list for implicit template
44215 parameters. */
44216
44217 become_template = true;
44218
44219 parser->implicit_template_scope
44220 = begin_scope (sk_template_parms, NULL);
44221
44222 ++processing_template_decl;
44223
44224 parser->fully_implicit_function_template_p = true;
44225 ++parser->num_template_parameter_lists;
44226 }
44227 else
44228 {
44229 /* Synthesize implicit template parameters at the end of the explicit
44230 template parameter list. */
44231
44232 gcc_assert (current_template_parms);
44233
44234 parser->implicit_template_scope = scope;
44235
44236 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
44237 parser->implicit_template_parms
44238 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
44239 }
44240 }
44241
44242 /* Synthesize a new template parameter and track the current template
44243 parameter chain with implicit_template_parms. */
44244
44245 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
44246 tree synth_id = make_generic_type_name ();
44247 tree synth_tmpl_parm;
44248 bool non_type = false;
44249
44250 /* Synthesize the type template parameter. */
44251 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
44252 synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
44253
44254 /* Attach the constraint to the parm before processing. */
44255 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
44256 TREE_TYPE (node) = constr;
44257 tree new_parm
44258 = process_template_parm (parser->implicit_template_parms,
44259 input_location,
44260 node,
44261 /*non_type=*/non_type,
44262 /*param_pack=*/false);
44263
44264 /* Mark the synthetic declaration "virtual". This is used when
44265 comparing template-heads to determine if whether an abbreviated
44266 function template is equivalent to an explicit template.
44267
44268 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
44269 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
44270
44271 // Chain the new parameter to the list of implicit parameters.
44272 if (parser->implicit_template_parms)
44273 parser->implicit_template_parms
44274 = TREE_CHAIN (parser->implicit_template_parms);
44275 else
44276 parser->implicit_template_parms = new_parm;
44277
44278 tree new_decl = get_local_decls ();
44279 if (non_type)
44280 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
44281 new_decl = DECL_INITIAL (new_decl);
44282
44283 /* If creating a fully implicit function template, start the new implicit
44284 template parameter list with this synthesized type, otherwise grow the
44285 current template parameter list. */
44286
44287 if (become_template)
44288 {
44289 parent_scope->level_chain = current_binding_level;
44290
44291 tree new_parms = make_tree_vec (1);
44292 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
44293 current_template_parms = tree_cons (size_int (processing_template_decl),
44294 new_parms, current_template_parms);
44295 }
44296 else
44297 {
44298 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
44299 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
44300 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
44301 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
44302 }
44303
44304 /* If the new parameter was constrained, we need to add that to the
44305 constraints in the template parameter list. */
44306 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
44307 {
44308 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
44309 reqs = combine_constraint_expressions (reqs, req);
44310 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
44311 }
44312
44313 current_binding_level = entry_scope;
44314
44315 return new_decl;
44316 }
44317
44318 /* Finish the declaration of a fully implicit function template. Such a
44319 template has no explicit template parameter list so has not been through the
44320 normal template head and tail processing. synthesize_implicit_template_parm
44321 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
44322 provided if the declaration is a class member such that its template
44323 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
44324 form is returned. Otherwise NULL_TREE is returned. */
44325
44326 static tree
44327 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
44328 {
44329 gcc_assert (parser->fully_implicit_function_template_p);
44330
44331 if (member_decl_opt && member_decl_opt != error_mark_node
44332 && DECL_VIRTUAL_P (member_decl_opt))
44333 {
44334 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
44335 "implicit templates may not be %<virtual%>");
44336 DECL_VIRTUAL_P (member_decl_opt) = false;
44337 }
44338
44339 if (member_decl_opt)
44340 member_decl_opt = finish_member_template_decl (member_decl_opt);
44341 end_template_decl ();
44342
44343 parser->fully_implicit_function_template_p = false;
44344 parser->implicit_template_parms = 0;
44345 parser->implicit_template_scope = 0;
44346 --parser->num_template_parameter_lists;
44347
44348 return member_decl_opt;
44349 }
44350
44351 /* Like finish_fully_implicit_template, but to be used in error
44352 recovery, rearranging scopes so that we restore the state we had
44353 before synthesize_implicit_template_parm inserted the implement
44354 template parms scope. */
44355
44356 static void
44357 abort_fully_implicit_template (cp_parser *parser)
44358 {
44359 cp_binding_level *return_to_scope = current_binding_level;
44360
44361 if (parser->implicit_template_scope
44362 && return_to_scope != parser->implicit_template_scope)
44363 {
44364 cp_binding_level *child = return_to_scope;
44365 for (cp_binding_level *scope = child->level_chain;
44366 scope != parser->implicit_template_scope;
44367 scope = child->level_chain)
44368 child = scope;
44369 child->level_chain = parser->implicit_template_scope->level_chain;
44370 parser->implicit_template_scope->level_chain = return_to_scope;
44371 current_binding_level = parser->implicit_template_scope;
44372 }
44373 else
44374 return_to_scope = return_to_scope->level_chain;
44375
44376 finish_fully_implicit_template (parser, NULL);
44377
44378 gcc_assert (current_binding_level == return_to_scope);
44379 }
44380
44381 /* Helper function for diagnostics that have complained about things
44382 being used with 'extern "C"' linkage.
44383
44384 Attempt to issue a note showing where the 'extern "C"' linkage began. */
44385
44386 void
44387 maybe_show_extern_c_location (void)
44388 {
44389 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
44390 inform (the_parser->innermost_linkage_specification_location,
44391 "%<extern \"C\"%> linkage started here");
44392 }
44393
44394 #include "gt-cp-parser.h"