]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
typeck2.c (build_functional_cast): Add location_t parameter and use it.
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2019 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
49 \f
50 /* The lexer. */
51
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
54
55 /* The various kinds of non integral constant we encounter. */
56 enum non_integral_constant {
57 NIC_NONE,
58 /* floating-point literal */
59 NIC_FLOAT,
60 /* %<this%> */
61 NIC_THIS,
62 /* %<__FUNCTION__%> */
63 NIC_FUNC_NAME,
64 /* %<__PRETTY_FUNCTION__%> */
65 NIC_PRETTY_FUNC,
66 /* %<__func__%> */
67 NIC_C99_FUNC,
68 /* "%<va_arg%> */
69 NIC_VA_ARG,
70 /* a cast */
71 NIC_CAST,
72 /* %<typeid%> operator */
73 NIC_TYPEID,
74 /* non-constant compound literals */
75 NIC_NCC,
76 /* a function call */
77 NIC_FUNC_CALL,
78 /* an increment */
79 NIC_INC,
80 /* an decrement */
81 NIC_DEC,
82 /* an array reference */
83 NIC_ARRAY_REF,
84 /* %<->%> */
85 NIC_ARROW,
86 /* %<.%> */
87 NIC_POINT,
88 /* the address of a label */
89 NIC_ADDR_LABEL,
90 /* %<*%> */
91 NIC_STAR,
92 /* %<&%> */
93 NIC_ADDR,
94 /* %<++%> */
95 NIC_PREINCREMENT,
96 /* %<--%> */
97 NIC_PREDECREMENT,
98 /* %<new%> */
99 NIC_NEW,
100 /* %<delete%> */
101 NIC_DEL,
102 /* calls to overloaded operators */
103 NIC_OVERLOADED,
104 /* an assignment */
105 NIC_ASSIGNMENT,
106 /* a comma operator */
107 NIC_COMMA,
108 /* a call to a constructor */
109 NIC_CONSTRUCTOR,
110 /* a transaction expression */
111 NIC_TRANSACTION
112 };
113
114 /* The various kinds of errors about name-lookup failing. */
115 enum name_lookup_error {
116 /* NULL */
117 NLE_NULL,
118 /* is not a type */
119 NLE_TYPE,
120 /* is not a class or namespace */
121 NLE_CXX98,
122 /* is not a class, namespace, or enumeration */
123 NLE_NOT_CXX98
124 };
125
126 /* The various kinds of required token */
127 enum required_token {
128 RT_NONE,
129 RT_SEMICOLON, /* ';' */
130 RT_OPEN_PAREN, /* '(' */
131 RT_CLOSE_BRACE, /* '}' */
132 RT_OPEN_BRACE, /* '{' */
133 RT_CLOSE_SQUARE, /* ']' */
134 RT_OPEN_SQUARE, /* '[' */
135 RT_COMMA, /* ',' */
136 RT_SCOPE, /* '::' */
137 RT_LESS, /* '<' */
138 RT_GREATER, /* '>' */
139 RT_EQ, /* '=' */
140 RT_ELLIPSIS, /* '...' */
141 RT_MULT, /* '*' */
142 RT_COMPL, /* '~' */
143 RT_COLON, /* ':' */
144 RT_COLON_SCOPE, /* ':' or '::' */
145 RT_CLOSE_PAREN, /* ')' */
146 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
147 RT_PRAGMA_EOL, /* end of line */
148 RT_NAME, /* identifier */
149
150 /* The type is CPP_KEYWORD */
151 RT_NEW, /* new */
152 RT_DELETE, /* delete */
153 RT_RETURN, /* return */
154 RT_WHILE, /* while */
155 RT_EXTERN, /* extern */
156 RT_STATIC_ASSERT, /* static_assert */
157 RT_DECLTYPE, /* decltype */
158 RT_OPERATOR, /* operator */
159 RT_CLASS, /* class */
160 RT_TEMPLATE, /* template */
161 RT_NAMESPACE, /* namespace */
162 RT_USING, /* using */
163 RT_ASM, /* asm */
164 RT_TRY, /* try */
165 RT_CATCH, /* catch */
166 RT_THROW, /* throw */
167 RT_AUTO, /* auto */
168 RT_LABEL, /* __label__ */
169 RT_AT_TRY, /* @try */
170 RT_AT_SYNCHRONIZED, /* @synchronized */
171 RT_AT_THROW, /* @throw */
172
173 RT_SELECT, /* selection-statement */
174 RT_ITERATION, /* iteration-statement */
175 RT_JUMP, /* jump-statement */
176 RT_CLASS_KEY, /* class-key */
177 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
178 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
179 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
180 RT_TRANSACTION_CANCEL /* __transaction_cancel */
181 };
182
183 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
184 reverting it on destruction. */
185
186 class type_id_in_expr_sentinel
187 {
188 cp_parser *parser;
189 bool saved;
190 public:
191 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
192 : parser (parser),
193 saved (parser->in_type_id_in_expr_p)
194 { parser->in_type_id_in_expr_p = set; }
195 ~type_id_in_expr_sentinel ()
196 { parser->in_type_id_in_expr_p = saved; }
197 };
198
199 /* Prototypes. */
200
201 static cp_lexer *cp_lexer_new_main
202 (void);
203 static cp_lexer *cp_lexer_new_from_tokens
204 (cp_token_cache *tokens);
205 static void cp_lexer_destroy
206 (cp_lexer *);
207 static int cp_lexer_saving_tokens
208 (const cp_lexer *);
209 static cp_token *cp_lexer_token_at
210 (cp_lexer *, cp_token_position);
211 static void cp_lexer_get_preprocessor_token
212 (cp_lexer *, cp_token *);
213 static inline cp_token *cp_lexer_peek_token
214 (cp_lexer *);
215 static cp_token *cp_lexer_peek_nth_token
216 (cp_lexer *, size_t);
217 static inline bool cp_lexer_next_token_is
218 (cp_lexer *, enum cpp_ttype);
219 static bool cp_lexer_next_token_is_not
220 (cp_lexer *, enum cpp_ttype);
221 static bool cp_lexer_next_token_is_keyword
222 (cp_lexer *, enum rid);
223 static cp_token *cp_lexer_consume_token
224 (cp_lexer *);
225 static void cp_lexer_purge_token
226 (cp_lexer *);
227 static void cp_lexer_purge_tokens_after
228 (cp_lexer *, cp_token_position);
229 static void cp_lexer_save_tokens
230 (cp_lexer *);
231 static void cp_lexer_commit_tokens
232 (cp_lexer *);
233 static void cp_lexer_rollback_tokens
234 (cp_lexer *);
235 static void cp_lexer_print_token
236 (FILE *, cp_token *);
237 static inline bool cp_lexer_debugging_p
238 (cp_lexer *);
239 static void cp_lexer_start_debugging
240 (cp_lexer *) ATTRIBUTE_UNUSED;
241 static void cp_lexer_stop_debugging
242 (cp_lexer *) ATTRIBUTE_UNUSED;
243
244 static cp_token_cache *cp_token_cache_new
245 (cp_token *, cp_token *);
246 static tree cp_parser_late_noexcept_specifier
247 (cp_parser *, tree);
248 static void noexcept_override_late_checks
249 (tree, tree);
250
251 static void cp_parser_initial_pragma
252 (cp_token *);
253
254 static bool cp_parser_omp_declare_reduction_exprs
255 (tree, cp_parser *);
256 static void cp_finalize_oacc_routine
257 (cp_parser *, tree, bool);
258
259 /* Manifest constants. */
260 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
261 #define CP_SAVED_TOKEN_STACK 5
262
263 /* Variables. */
264
265 /* The stream to which debugging output should be written. */
266 static FILE *cp_lexer_debug_stream;
267
268 /* Nonzero if we are parsing an unevaluated operand: an operand to
269 sizeof, typeof, or alignof. */
270 int cp_unevaluated_operand;
271
272 /* Dump up to NUM tokens in BUFFER to FILE starting with token
273 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
274 first token in BUFFER. If NUM is 0, dump all the tokens. If
275 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
276 highlighted by surrounding it in [[ ]]. */
277
278 static void
279 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
280 cp_token *start_token, unsigned num,
281 cp_token *curr_token)
282 {
283 unsigned i, nprinted;
284 cp_token *token;
285 bool do_print;
286
287 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288
289 if (buffer == NULL)
290 return;
291
292 if (num == 0)
293 num = buffer->length ();
294
295 if (start_token == NULL)
296 start_token = buffer->address ();
297
298 if (start_token > buffer->address ())
299 {
300 cp_lexer_print_token (file, &(*buffer)[0]);
301 fprintf (file, " ... ");
302 }
303
304 do_print = false;
305 nprinted = 0;
306 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 {
308 if (token == start_token)
309 do_print = true;
310
311 if (!do_print)
312 continue;
313
314 nprinted++;
315 if (token == curr_token)
316 fprintf (file, "[[");
317
318 cp_lexer_print_token (file, token);
319
320 if (token == curr_token)
321 fprintf (file, "]]");
322
323 switch (token->type)
324 {
325 case CPP_SEMICOLON:
326 case CPP_OPEN_BRACE:
327 case CPP_CLOSE_BRACE:
328 case CPP_EOF:
329 fputc ('\n', file);
330 break;
331
332 default:
333 fputc (' ', file);
334 }
335 }
336
337 if (i == num && i < buffer->length ())
338 {
339 fprintf (file, " ... ");
340 cp_lexer_print_token (file, &buffer->last ());
341 }
342
343 fprintf (file, "\n");
344 }
345
346
347 /* Dump all tokens in BUFFER to stderr. */
348
349 void
350 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 {
352 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
353 }
354
355 DEBUG_FUNCTION void
356 debug (vec<cp_token, va_gc> &ref)
357 {
358 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
359 }
360
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> *ptr)
363 {
364 if (ptr)
365 debug (*ptr);
366 else
367 fprintf (stderr, "<nil>\n");
368 }
369
370
371 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
372 description for T. */
373
374 static void
375 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 {
377 if (t)
378 {
379 fprintf (file, "%s: ", desc);
380 print_node_brief (file, "", t, 0);
381 }
382 }
383
384
385 /* Dump parser context C to FILE. */
386
387 static void
388 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 {
390 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
391 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
392 print_node_brief (file, "", c->object_type, 0);
393 fprintf (file, "}\n");
394 }
395
396
397 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398
399 static void
400 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 {
402 unsigned i;
403 cp_parser_context *c;
404
405 fprintf (file, "Parsing context stack:\n");
406 for (i = 0, c = first; c; c = c->next, i++)
407 {
408 fprintf (file, "\t#%u: ", i);
409 cp_debug_print_context (file, c);
410 }
411 }
412
413
414 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415
416 static void
417 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 {
419 if (flag)
420 fprintf (file, "%s: true\n", desc);
421 }
422
423
424 /* Print an unparsed function entry UF to FILE. */
425
426 static void
427 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 {
429 unsigned i;
430 cp_default_arg_entry *default_arg_fn;
431 tree fn;
432
433 fprintf (file, "\tFunctions with default args:\n");
434 for (i = 0;
435 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
436 i++)
437 {
438 fprintf (file, "\t\tClass type: ");
439 print_node_brief (file, "", default_arg_fn->class_type, 0);
440 fprintf (file, "\t\tDeclaration: ");
441 print_node_brief (file, "", default_arg_fn->decl, 0);
442 fprintf (file, "\n");
443 }
444
445 fprintf (file, "\n\tFunctions with definitions that require "
446 "post-processing\n\t\t");
447 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 {
449 print_node_brief (file, "", fn, 0);
450 fprintf (file, " ");
451 }
452 fprintf (file, "\n");
453
454 fprintf (file, "\n\tNon-static data members with initializers that require "
455 "post-processing\n\t\t");
456 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 {
458 print_node_brief (file, "", fn, 0);
459 fprintf (file, " ");
460 }
461 fprintf (file, "\n");
462 }
463
464
465 /* Print the stack of unparsed member functions S to FILE. */
466
467 static void
468 cp_debug_print_unparsed_queues (FILE *file,
469 vec<cp_unparsed_functions_entry, va_gc> *s)
470 {
471 unsigned i;
472 cp_unparsed_functions_entry *uf;
473
474 fprintf (file, "Unparsed functions\n");
475 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 {
477 fprintf (file, "#%u:\n", i);
478 cp_debug_print_unparsed_function (file, uf);
479 }
480 }
481
482
483 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
484 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485
486 static void
487 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 {
489 cp_token *next_token, *first_token, *start_token;
490
491 if (file == NULL)
492 file = stderr;
493
494 next_token = parser->lexer->next_token;
495 first_token = parser->lexer->buffer->address ();
496 start_token = (next_token > first_token + window_size / 2)
497 ? next_token - window_size / 2
498 : first_token;
499 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
500 next_token);
501 }
502
503
504 /* Dump debugging information for the given PARSER. If FILE is NULL,
505 the output is printed on stderr. */
506
507 void
508 cp_debug_parser (FILE *file, cp_parser *parser)
509 {
510 const size_t window_size = 20;
511 cp_token *token;
512 expanded_location eloc;
513
514 if (file == NULL)
515 file = stderr;
516
517 fprintf (file, "Parser state\n\n");
518 fprintf (file, "Number of tokens: %u\n",
519 vec_safe_length (parser->lexer->buffer));
520 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
521 cp_debug_print_tree_if_set (file, "Object scope",
522 parser->object_scope);
523 cp_debug_print_tree_if_set (file, "Qualifying scope",
524 parser->qualifying_scope);
525 cp_debug_print_context_stack (file, parser->context);
526 cp_debug_print_flag (file, "Allow GNU extensions",
527 parser->allow_gnu_extensions_p);
528 cp_debug_print_flag (file, "'>' token is greater-than",
529 parser->greater_than_is_operator_p);
530 cp_debug_print_flag (file, "Default args allowed in current "
531 "parameter list", parser->default_arg_ok_p);
532 cp_debug_print_flag (file, "Parsing integral constant-expression",
533 parser->integral_constant_expression_p);
534 cp_debug_print_flag (file, "Allow non-constant expression in current "
535 "constant-expression",
536 parser->allow_non_integral_constant_expression_p);
537 cp_debug_print_flag (file, "Seen non-constant expression",
538 parser->non_integral_constant_expression_p);
539 cp_debug_print_flag (file, "Local names forbidden in current context",
540 (parser->local_variables_forbidden_p
541 & LOCAL_VARS_FORBIDDEN));
542 cp_debug_print_flag (file, "'this' forbidden in current context",
543 (parser->local_variables_forbidden_p
544 & THIS_FORBIDDEN));
545 cp_debug_print_flag (file, "In unbraced linkage specification",
546 parser->in_unbraced_linkage_specification_p);
547 cp_debug_print_flag (file, "Parsing a declarator",
548 parser->in_declarator_p);
549 cp_debug_print_flag (file, "In template argument list",
550 parser->in_template_argument_list_p);
551 cp_debug_print_flag (file, "Parsing an iteration statement",
552 parser->in_statement & IN_ITERATION_STMT);
553 cp_debug_print_flag (file, "Parsing a switch statement",
554 parser->in_statement & IN_SWITCH_STMT);
555 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
556 parser->in_statement & IN_OMP_BLOCK);
557 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
558 parser->in_statement & IN_OMP_FOR);
559 cp_debug_print_flag (file, "Parsing an if statement",
560 parser->in_statement & IN_IF_STMT);
561 cp_debug_print_flag (file, "Parsing a type-id in an expression "
562 "context", parser->in_type_id_in_expr_p);
563 cp_debug_print_flag (file, "String expressions should be translated "
564 "to execution character set",
565 parser->translate_strings_p);
566 cp_debug_print_flag (file, "Parsing function body outside of a "
567 "local class", parser->in_function_body);
568 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
569 parser->colon_corrects_to_scope_p);
570 cp_debug_print_flag (file, "Colon doesn't start a class definition",
571 parser->colon_doesnt_start_class_def_p);
572 if (parser->type_definition_forbidden_message)
573 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
574 parser->type_definition_forbidden_message,
575 parser->type_definition_forbidden_message_arg
576 ? parser->type_definition_forbidden_message_arg : "<none>");
577 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
578 fprintf (file, "Number of class definitions in progress: %u\n",
579 parser->num_classes_being_defined);
580 fprintf (file, "Number of template parameter lists for the current "
581 "declaration: %u\n", parser->num_template_parameter_lists);
582 cp_debug_parser_tokens (file, parser, window_size);
583 token = parser->lexer->next_token;
584 fprintf (file, "Next token to parse:\n");
585 fprintf (file, "\tToken: ");
586 cp_lexer_print_token (file, token);
587 eloc = expand_location (token->location);
588 fprintf (file, "\n\tFile: %s\n", eloc.file);
589 fprintf (file, "\tLine: %d\n", eloc.line);
590 fprintf (file, "\tColumn: %d\n", eloc.column);
591 }
592
593 DEBUG_FUNCTION void
594 debug (cp_parser &ref)
595 {
596 cp_debug_parser (stderr, &ref);
597 }
598
599 DEBUG_FUNCTION void
600 debug (cp_parser *ptr)
601 {
602 if (ptr)
603 debug (*ptr);
604 else
605 fprintf (stderr, "<nil>\n");
606 }
607
608 /* Allocate memory for a new lexer object and return it. */
609
610 static cp_lexer *
611 cp_lexer_alloc (void)
612 {
613 cp_lexer *lexer;
614
615 c_common_no_more_pch ();
616
617 /* Allocate the memory. */
618 lexer = ggc_cleared_alloc<cp_lexer> ();
619
620 /* Initially we are not debugging. */
621 lexer->debugging_p = false;
622
623 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
624
625 /* Create the buffer. */
626 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
627
628 return lexer;
629 }
630
631
632 /* Create a new main C++ lexer, the lexer that gets tokens from the
633 preprocessor. */
634
635 static cp_lexer *
636 cp_lexer_new_main (void)
637 {
638 cp_lexer *lexer;
639 cp_token token;
640
641 /* It's possible that parsing the first pragma will load a PCH file,
642 which is a GC collection point. So we have to do that before
643 allocating any memory. */
644 cp_parser_initial_pragma (&token);
645
646 lexer = cp_lexer_alloc ();
647
648 /* Put the first token in the buffer. */
649 lexer->buffer->quick_push (token);
650
651 /* Get the remaining tokens from the preprocessor. */
652 while (token.type != CPP_EOF)
653 {
654 cp_lexer_get_preprocessor_token (lexer, &token);
655 vec_safe_push (lexer->buffer, token);
656 }
657
658 lexer->next_token = lexer->buffer->address ();
659 lexer->last_token = lexer->next_token
660 + lexer->buffer->length ()
661 - 1;
662
663 /* Subsequent preprocessor diagnostics should use compiler
664 diagnostic functions to get the compiler source location. */
665 done_lexing = true;
666
667 gcc_assert (!lexer->next_token->purged_p);
668 return lexer;
669 }
670
671 /* Create a new lexer whose token stream is primed with the tokens in
672 CACHE. When these tokens are exhausted, no new tokens will be read. */
673
674 static cp_lexer *
675 cp_lexer_new_from_tokens (cp_token_cache *cache)
676 {
677 cp_token *first = cache->first;
678 cp_token *last = cache->last;
679 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
680
681 /* We do not own the buffer. */
682 lexer->buffer = NULL;
683
684 /* Insert an EOF token. */
685 lexer->saved_type = last->type;
686 lexer->saved_keyword = last->keyword;
687 last->type = CPP_EOF;
688 last->keyword = RID_MAX;
689
690 lexer->next_token = first;
691 lexer->last_token = last;
692
693 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
694
695 /* Initially we are not debugging. */
696 lexer->debugging_p = false;
697
698 gcc_assert (!lexer->next_token->purged_p);
699 return lexer;
700 }
701
702 /* Frees all resources associated with LEXER. */
703
704 static void
705 cp_lexer_destroy (cp_lexer *lexer)
706 {
707 if (lexer->buffer)
708 vec_free (lexer->buffer);
709 else
710 {
711 /* Restore the token we overwrite with EOF. */
712 lexer->last_token->type = lexer->saved_type;
713 lexer->last_token->keyword = lexer->saved_keyword;
714 }
715 lexer->saved_tokens.release ();
716 ggc_free (lexer);
717 }
718
719 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
720 be used. The point of this flag is to help the compiler to fold away calls
721 to cp_lexer_debugging_p within this source file at compile time, when the
722 lexer is not being debugged. */
723
724 #define LEXER_DEBUGGING_ENABLED_P false
725
726 /* Returns nonzero if debugging information should be output. */
727
728 static inline bool
729 cp_lexer_debugging_p (cp_lexer *lexer)
730 {
731 if (!LEXER_DEBUGGING_ENABLED_P)
732 return false;
733
734 return lexer->debugging_p;
735 }
736
737
738 static inline cp_token_position
739 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
740 {
741 return lexer->next_token - previous_p;
742 }
743
744 static inline cp_token *
745 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
746 {
747 return pos;
748 }
749
750 static inline void
751 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
752 {
753 lexer->next_token = cp_lexer_token_at (lexer, pos);
754 }
755
756 static inline cp_token_position
757 cp_lexer_previous_token_position (cp_lexer *lexer)
758 {
759 return cp_lexer_token_position (lexer, true);
760 }
761
762 static inline cp_token *
763 cp_lexer_previous_token (cp_lexer *lexer)
764 {
765 cp_token_position tp = cp_lexer_previous_token_position (lexer);
766
767 /* Skip past purged tokens. */
768 while (tp->purged_p)
769 {
770 gcc_assert (tp != vec_safe_address (lexer->buffer));
771 tp--;
772 }
773
774 return cp_lexer_token_at (lexer, tp);
775 }
776
777 /* Overload for make_location, taking the lexer to mean the location of the
778 previous token. */
779
780 static inline location_t
781 make_location (location_t caret, location_t start, cp_lexer *lexer)
782 {
783 cp_token *t = cp_lexer_previous_token (lexer);
784 return make_location (caret, start, t->location);
785 }
786
787 /* nonzero if we are presently saving tokens. */
788
789 static inline int
790 cp_lexer_saving_tokens (const cp_lexer* lexer)
791 {
792 return lexer->saved_tokens.length () != 0;
793 }
794
795 /* Store the next token from the preprocessor in *TOKEN. Return true
796 if we reach EOF. If LEXER is NULL, assume we are handling an
797 initial #pragma pch_preprocess, and thus want the lexer to return
798 processed strings. */
799
800 static void
801 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
802 {
803 static int is_extern_c = 0;
804
805 /* Get a new token from the preprocessor. */
806 token->type
807 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
808 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
809 token->keyword = RID_MAX;
810 token->purged_p = false;
811 token->error_reported = false;
812 token->tree_check_p = false;
813
814 /* On some systems, some header files are surrounded by an
815 implicit extern "C" block. Set a flag in the token if it
816 comes from such a header. */
817 is_extern_c += pending_lang_change;
818 pending_lang_change = 0;
819 token->implicit_extern_c = is_extern_c > 0;
820
821 /* Check to see if this token is a keyword. */
822 if (token->type == CPP_NAME)
823 {
824 if (IDENTIFIER_KEYWORD_P (token->u.value))
825 {
826 /* Mark this token as a keyword. */
827 token->type = CPP_KEYWORD;
828 /* Record which keyword. */
829 token->keyword = C_RID_CODE (token->u.value);
830 }
831 else
832 {
833 if (warn_cxx11_compat
834 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
835 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
836 {
837 /* Warn about the C++0x keyword (but still treat it as
838 an identifier). */
839 warning_at (token->location, OPT_Wc__11_compat,
840 "identifier %qE is a keyword in C++11",
841 token->u.value);
842
843 /* Clear out the C_RID_CODE so we don't warn about this
844 particular identifier-turned-keyword again. */
845 C_SET_RID_CODE (token->u.value, RID_MAX);
846 }
847 if (warn_cxx20_compat
848 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
849 && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
850 {
851 /* Warn about the C++20 keyword (but still treat it as
852 an identifier). */
853 warning_at (token->location, OPT_Wc__20_compat,
854 "identifier %qE is a keyword in C++20",
855 token->u.value);
856
857 /* Clear out the C_RID_CODE so we don't warn about this
858 particular identifier-turned-keyword again. */
859 C_SET_RID_CODE (token->u.value, RID_MAX);
860 }
861
862 token->keyword = RID_MAX;
863 }
864 }
865 else if (token->type == CPP_AT_NAME)
866 {
867 /* This only happens in Objective-C++; it must be a keyword. */
868 token->type = CPP_KEYWORD;
869 switch (C_RID_CODE (token->u.value))
870 {
871 /* Replace 'class' with '@class', 'private' with '@private',
872 etc. This prevents confusion with the C++ keyword
873 'class', and makes the tokens consistent with other
874 Objective-C 'AT' keywords. For example '@class' is
875 reported as RID_AT_CLASS which is consistent with
876 '@synchronized', which is reported as
877 RID_AT_SYNCHRONIZED.
878 */
879 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
880 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
881 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
882 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
883 case RID_THROW: token->keyword = RID_AT_THROW; break;
884 case RID_TRY: token->keyword = RID_AT_TRY; break;
885 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
886 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
887 default: token->keyword = C_RID_CODE (token->u.value);
888 }
889 }
890 }
891
892 /* Update the globals input_location and the input file stack from TOKEN. */
893 static inline void
894 cp_lexer_set_source_position_from_token (cp_token *token)
895 {
896 if (token->type != CPP_EOF)
897 {
898 input_location = token->location;
899 }
900 }
901
902 /* Update the globals input_location and the input file stack from LEXER. */
903 static inline void
904 cp_lexer_set_source_position (cp_lexer *lexer)
905 {
906 cp_token *token = cp_lexer_peek_token (lexer);
907 cp_lexer_set_source_position_from_token (token);
908 }
909
910 /* Return a pointer to the next token in the token stream, but do not
911 consume it. */
912
913 static inline cp_token *
914 cp_lexer_peek_token (cp_lexer *lexer)
915 {
916 if (cp_lexer_debugging_p (lexer))
917 {
918 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
919 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
920 putc ('\n', cp_lexer_debug_stream);
921 }
922 return lexer->next_token;
923 }
924
925 /* Return true if the next token has the indicated TYPE. */
926
927 static inline bool
928 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
929 {
930 return cp_lexer_peek_token (lexer)->type == type;
931 }
932
933 /* Return true if the next token does not have the indicated TYPE. */
934
935 static inline bool
936 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
937 {
938 return !cp_lexer_next_token_is (lexer, type);
939 }
940
941 /* Return true if the next token is the indicated KEYWORD. */
942
943 static inline bool
944 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
945 {
946 return cp_lexer_peek_token (lexer)->keyword == keyword;
947 }
948
949 static inline bool
950 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
951 {
952 return cp_lexer_peek_nth_token (lexer, n)->type == type;
953 }
954
955 static inline bool
956 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
957 {
958 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
959 }
960
961 /* Return true if KEYWORD can start a decl-specifier. */
962
963 bool
964 cp_keyword_starts_decl_specifier_p (enum rid keyword)
965 {
966 switch (keyword)
967 {
968 /* auto specifier: storage-class-specifier in C++,
969 simple-type-specifier in C++0x. */
970 case RID_AUTO:
971 /* Storage classes. */
972 case RID_REGISTER:
973 case RID_STATIC:
974 case RID_EXTERN:
975 case RID_MUTABLE:
976 case RID_THREAD:
977 /* Elaborated type specifiers. */
978 case RID_ENUM:
979 case RID_CLASS:
980 case RID_STRUCT:
981 case RID_UNION:
982 case RID_TYPENAME:
983 /* Simple type specifiers. */
984 case RID_CHAR:
985 case RID_CHAR8:
986 case RID_CHAR16:
987 case RID_CHAR32:
988 case RID_WCHAR:
989 case RID_BOOL:
990 case RID_SHORT:
991 case RID_INT:
992 case RID_LONG:
993 case RID_SIGNED:
994 case RID_UNSIGNED:
995 case RID_FLOAT:
996 case RID_DOUBLE:
997 case RID_VOID:
998 /* GNU extensions. */
999 case RID_ATTRIBUTE:
1000 case RID_TYPEOF:
1001 /* C++11 extensions. */
1002 case RID_DECLTYPE:
1003 case RID_UNDERLYING_TYPE:
1004 case RID_CONSTEXPR:
1005 /* C++20 extensions. */
1006 case RID_CONSTINIT:
1007 case RID_CONSTEVAL:
1008 return true;
1009
1010 default:
1011 if (keyword >= RID_FIRST_INT_N
1012 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1013 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1014 return true;
1015 return false;
1016 }
1017 }
1018
1019 /* Return true if the next token is a keyword for a decl-specifier. */
1020
1021 static bool
1022 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1023 {
1024 cp_token *token;
1025
1026 token = cp_lexer_peek_token (lexer);
1027 return cp_keyword_starts_decl_specifier_p (token->keyword);
1028 }
1029
1030 /* Returns TRUE iff the token T begins a decltype type. */
1031
1032 static bool
1033 token_is_decltype (cp_token *t)
1034 {
1035 return (t->keyword == RID_DECLTYPE
1036 || t->type == CPP_DECLTYPE);
1037 }
1038
1039 /* Returns TRUE iff the next token begins a decltype type. */
1040
1041 static bool
1042 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1043 {
1044 cp_token *t = cp_lexer_peek_token (lexer);
1045 return token_is_decltype (t);
1046 }
1047
1048 /* Called when processing a token with tree_check_value; perform or defer the
1049 associated checks and return the value. */
1050
1051 static tree
1052 saved_checks_value (struct tree_check *check_value)
1053 {
1054 /* Perform any access checks that were deferred. */
1055 vec<deferred_access_check, va_gc> *checks;
1056 deferred_access_check *chk;
1057 checks = check_value->checks;
1058 if (checks)
1059 {
1060 int i;
1061 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1062 perform_or_defer_access_check (chk->binfo,
1063 chk->decl,
1064 chk->diag_decl, tf_warning_or_error);
1065 }
1066 /* Return the stored value. */
1067 return check_value->value;
1068 }
1069
1070 /* Return a pointer to the Nth token in the token stream. If N is 1,
1071 then this is precisely equivalent to cp_lexer_peek_token (except
1072 that it is not inline). One would like to disallow that case, but
1073 there is one case (cp_parser_nth_token_starts_template_id) where
1074 the caller passes a variable for N and it might be 1. */
1075
1076 static cp_token *
1077 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1078 {
1079 cp_token *token;
1080
1081 /* N is 1-based, not zero-based. */
1082 gcc_assert (n > 0);
1083
1084 if (cp_lexer_debugging_p (lexer))
1085 fprintf (cp_lexer_debug_stream,
1086 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1087
1088 --n;
1089 token = lexer->next_token;
1090 while (n && token->type != CPP_EOF)
1091 {
1092 ++token;
1093 if (!token->purged_p)
1094 --n;
1095 }
1096
1097 if (cp_lexer_debugging_p (lexer))
1098 {
1099 cp_lexer_print_token (cp_lexer_debug_stream, token);
1100 putc ('\n', cp_lexer_debug_stream);
1101 }
1102
1103 return token;
1104 }
1105
1106 /* Return the next token, and advance the lexer's next_token pointer
1107 to point to the next non-purged token. */
1108
1109 static cp_token *
1110 cp_lexer_consume_token (cp_lexer* lexer)
1111 {
1112 cp_token *token = lexer->next_token;
1113
1114 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1115
1116 do
1117 {
1118 gcc_assert (token->type != CPP_EOF);
1119 lexer->next_token++;
1120 }
1121 while (lexer->next_token->purged_p);
1122
1123 cp_lexer_set_source_position_from_token (token);
1124
1125 /* Provide debugging output. */
1126 if (cp_lexer_debugging_p (lexer))
1127 {
1128 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1129 cp_lexer_print_token (cp_lexer_debug_stream, token);
1130 putc ('\n', cp_lexer_debug_stream);
1131 }
1132
1133 return token;
1134 }
1135
1136 /* Permanently remove the next token from the token stream, and
1137 advance the next_token pointer to refer to the next non-purged
1138 token. */
1139
1140 static void
1141 cp_lexer_purge_token (cp_lexer *lexer)
1142 {
1143 cp_token *tok = lexer->next_token;
1144
1145 gcc_assert (tok->type != CPP_EOF);
1146 tok->purged_p = true;
1147 tok->location = UNKNOWN_LOCATION;
1148 tok->u.value = NULL_TREE;
1149 tok->keyword = RID_MAX;
1150
1151 do
1152 tok++;
1153 while (tok->purged_p);
1154 lexer->next_token = tok;
1155 }
1156
1157 /* Permanently remove all tokens after TOK, up to, but not
1158 including, the token that will be returned next by
1159 cp_lexer_peek_token. */
1160
1161 static void
1162 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1163 {
1164 cp_token *peek = lexer->next_token;
1165
1166 gcc_assert (tok < peek);
1167
1168 for (tok++; tok != peek; tok++)
1169 {
1170 tok->purged_p = true;
1171 tok->location = UNKNOWN_LOCATION;
1172 tok->u.value = NULL_TREE;
1173 tok->keyword = RID_MAX;
1174 }
1175 }
1176
1177 /* Begin saving tokens. All tokens consumed after this point will be
1178 preserved. */
1179
1180 static void
1181 cp_lexer_save_tokens (cp_lexer* lexer)
1182 {
1183 /* Provide debugging output. */
1184 if (cp_lexer_debugging_p (lexer))
1185 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1186
1187 lexer->saved_tokens.safe_push (lexer->next_token);
1188 }
1189
1190 /* Commit to the portion of the token stream most recently saved. */
1191
1192 static void
1193 cp_lexer_commit_tokens (cp_lexer* lexer)
1194 {
1195 /* Provide debugging output. */
1196 if (cp_lexer_debugging_p (lexer))
1197 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1198
1199 lexer->saved_tokens.pop ();
1200 }
1201
1202 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1203 to the token stream. Stop saving tokens. */
1204
1205 static void
1206 cp_lexer_rollback_tokens (cp_lexer* lexer)
1207 {
1208 /* Provide debugging output. */
1209 if (cp_lexer_debugging_p (lexer))
1210 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1211
1212 lexer->next_token = lexer->saved_tokens.pop ();
1213 }
1214
1215 /* RAII wrapper around the above functions, with sanity checking. Creating
1216 a variable saves tokens, which are committed when the variable is
1217 destroyed unless they are explicitly rolled back by calling the rollback
1218 member function. */
1219
1220 struct saved_token_sentinel
1221 {
1222 cp_lexer *lexer;
1223 unsigned len;
1224 bool commit;
1225 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1226 {
1227 len = lexer->saved_tokens.length ();
1228 cp_lexer_save_tokens (lexer);
1229 }
1230 void rollback ()
1231 {
1232 cp_lexer_rollback_tokens (lexer);
1233 commit = false;
1234 }
1235 ~saved_token_sentinel()
1236 {
1237 if (commit)
1238 cp_lexer_commit_tokens (lexer);
1239 gcc_assert (lexer->saved_tokens.length () == len);
1240 }
1241 };
1242
1243 /* Print a representation of the TOKEN on the STREAM. */
1244
1245 static void
1246 cp_lexer_print_token (FILE * stream, cp_token *token)
1247 {
1248 /* We don't use cpp_type2name here because the parser defines
1249 a few tokens of its own. */
1250 static const char *const token_names[] = {
1251 /* cpplib-defined token types */
1252 #define OP(e, s) #e,
1253 #define TK(e, s) #e,
1254 TTYPE_TABLE
1255 #undef OP
1256 #undef TK
1257 /* C++ parser token types - see "Manifest constants", above. */
1258 "KEYWORD",
1259 "TEMPLATE_ID",
1260 "NESTED_NAME_SPECIFIER",
1261 };
1262
1263 /* For some tokens, print the associated data. */
1264 switch (token->type)
1265 {
1266 case CPP_KEYWORD:
1267 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1268 For example, `struct' is mapped to an INTEGER_CST. */
1269 if (!identifier_p (token->u.value))
1270 break;
1271 /* fall through */
1272 case CPP_NAME:
1273 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1274 break;
1275
1276 case CPP_STRING:
1277 case CPP_STRING16:
1278 case CPP_STRING32:
1279 case CPP_WSTRING:
1280 case CPP_UTF8STRING:
1281 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1282 break;
1283
1284 case CPP_NUMBER:
1285 print_generic_expr (stream, token->u.value);
1286 break;
1287
1288 default:
1289 /* If we have a name for the token, print it out. Otherwise, we
1290 simply give the numeric code. */
1291 if (token->type < ARRAY_SIZE(token_names))
1292 fputs (token_names[token->type], stream);
1293 else
1294 fprintf (stream, "[%d]", token->type);
1295 break;
1296 }
1297 }
1298
1299 DEBUG_FUNCTION void
1300 debug (cp_token &ref)
1301 {
1302 cp_lexer_print_token (stderr, &ref);
1303 fprintf (stderr, "\n");
1304 }
1305
1306 DEBUG_FUNCTION void
1307 debug (cp_token *ptr)
1308 {
1309 if (ptr)
1310 debug (*ptr);
1311 else
1312 fprintf (stderr, "<nil>\n");
1313 }
1314
1315
1316 /* Start emitting debugging information. */
1317
1318 static void
1319 cp_lexer_start_debugging (cp_lexer* lexer)
1320 {
1321 if (!LEXER_DEBUGGING_ENABLED_P)
1322 fatal_error (input_location,
1323 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1324
1325 lexer->debugging_p = true;
1326 cp_lexer_debug_stream = stderr;
1327 }
1328
1329 /* Stop emitting debugging information. */
1330
1331 static void
1332 cp_lexer_stop_debugging (cp_lexer* lexer)
1333 {
1334 if (!LEXER_DEBUGGING_ENABLED_P)
1335 fatal_error (input_location,
1336 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1337
1338 lexer->debugging_p = false;
1339 cp_lexer_debug_stream = NULL;
1340 }
1341
1342 /* Create a new cp_token_cache, representing a range of tokens. */
1343
1344 static cp_token_cache *
1345 cp_token_cache_new (cp_token *first, cp_token *last)
1346 {
1347 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1348 cache->first = first;
1349 cache->last = last;
1350 return cache;
1351 }
1352
1353 /* Diagnose if #pragma omp declare simd isn't followed immediately
1354 by function declaration or definition. */
1355
1356 static inline void
1357 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1358 {
1359 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1360 {
1361 error ("%<#pragma omp declare %s%> not immediately followed by "
1362 "function declaration or definition",
1363 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1364 parser->omp_declare_simd = NULL;
1365 }
1366 }
1367
1368 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1369 and put that into "omp declare simd" attribute. */
1370
1371 static inline void
1372 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1373 {
1374 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1375 {
1376 if (fndecl == error_mark_node)
1377 {
1378 parser->omp_declare_simd = NULL;
1379 return;
1380 }
1381 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1382 {
1383 cp_ensure_no_omp_declare_simd (parser);
1384 return;
1385 }
1386 }
1387 }
1388
1389 /* Diagnose if #pragma acc routine isn't followed immediately by function
1390 declaration or definition. */
1391
1392 static inline void
1393 cp_ensure_no_oacc_routine (cp_parser *parser)
1394 {
1395 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1396 {
1397 error_at (parser->oacc_routine->loc,
1398 "%<#pragma acc routine%> not immediately followed by "
1399 "function declaration or definition");
1400 parser->oacc_routine = NULL;
1401 }
1402 }
1403 \f
1404 /* Decl-specifiers. */
1405
1406 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1407
1408 static void
1409 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1410 {
1411 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1412 }
1413
1414 /* Declarators. */
1415
1416 /* Nothing other than the parser should be creating declarators;
1417 declarators are a semi-syntactic representation of C++ entities.
1418 Other parts of the front end that need to create entities (like
1419 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1420
1421 static cp_declarator *make_call_declarator
1422 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1423 static cp_declarator *make_array_declarator
1424 (cp_declarator *, tree);
1425 static cp_declarator *make_pointer_declarator
1426 (cp_cv_quals, cp_declarator *, tree);
1427 static cp_declarator *make_reference_declarator
1428 (cp_cv_quals, cp_declarator *, bool, tree);
1429 static cp_declarator *make_ptrmem_declarator
1430 (cp_cv_quals, tree, cp_declarator *, tree);
1431
1432 /* An erroneous declarator. */
1433 static cp_declarator *cp_error_declarator;
1434
1435 /* The obstack on which declarators and related data structures are
1436 allocated. */
1437 static struct obstack declarator_obstack;
1438
1439 /* Alloc BYTES from the declarator memory pool. */
1440
1441 static inline void *
1442 alloc_declarator (size_t bytes)
1443 {
1444 return obstack_alloc (&declarator_obstack, bytes);
1445 }
1446
1447 /* Allocate a declarator of the indicated KIND. Clear fields that are
1448 common to all declarators. */
1449
1450 static cp_declarator *
1451 make_declarator (cp_declarator_kind kind)
1452 {
1453 cp_declarator *declarator;
1454
1455 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1456 declarator->kind = kind;
1457 declarator->parenthesized = UNKNOWN_LOCATION;
1458 declarator->attributes = NULL_TREE;
1459 declarator->std_attributes = NULL_TREE;
1460 declarator->declarator = NULL;
1461 declarator->parameter_pack_p = false;
1462 declarator->id_loc = UNKNOWN_LOCATION;
1463
1464 return declarator;
1465 }
1466
1467 /* Make a declarator for a generalized identifier. If
1468 QUALIFYING_SCOPE is non-NULL, the identifier is
1469 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1470 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1471 is, if any. */
1472
1473 static cp_declarator *
1474 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1475 special_function_kind sfk, location_t id_location)
1476 {
1477 cp_declarator *declarator;
1478
1479 /* It is valid to write:
1480
1481 class C { void f(); };
1482 typedef C D;
1483 void D::f();
1484
1485 The standard is not clear about whether `typedef const C D' is
1486 legal; as of 2002-09-15 the committee is considering that
1487 question. EDG 3.0 allows that syntax. Therefore, we do as
1488 well. */
1489 if (qualifying_scope && TYPE_P (qualifying_scope))
1490 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1491
1492 gcc_assert (identifier_p (unqualified_name)
1493 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1494 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1495
1496 declarator = make_declarator (cdk_id);
1497 declarator->u.id.qualifying_scope = qualifying_scope;
1498 declarator->u.id.unqualified_name = unqualified_name;
1499 declarator->u.id.sfk = sfk;
1500 declarator->id_loc = id_location;
1501
1502 return declarator;
1503 }
1504
1505 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1506 of modifiers such as const or volatile to apply to the pointer
1507 type, represented as identifiers. ATTRIBUTES represent the attributes that
1508 appertain to the pointer or reference. */
1509
1510 cp_declarator *
1511 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1512 tree attributes)
1513 {
1514 cp_declarator *declarator;
1515
1516 declarator = make_declarator (cdk_pointer);
1517 declarator->declarator = target;
1518 declarator->u.pointer.qualifiers = cv_qualifiers;
1519 declarator->u.pointer.class_type = NULL_TREE;
1520 if (target)
1521 {
1522 declarator->id_loc = target->id_loc;
1523 declarator->parameter_pack_p = target->parameter_pack_p;
1524 target->parameter_pack_p = false;
1525 }
1526 else
1527 declarator->parameter_pack_p = false;
1528
1529 declarator->std_attributes = attributes;
1530
1531 return declarator;
1532 }
1533
1534 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1535 represent the attributes that appertain to the pointer or
1536 reference. */
1537
1538 cp_declarator *
1539 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1540 bool rvalue_ref, tree attributes)
1541 {
1542 cp_declarator *declarator;
1543
1544 declarator = make_declarator (cdk_reference);
1545 declarator->declarator = target;
1546 declarator->u.reference.qualifiers = cv_qualifiers;
1547 declarator->u.reference.rvalue_ref = rvalue_ref;
1548 if (target)
1549 {
1550 declarator->id_loc = target->id_loc;
1551 declarator->parameter_pack_p = target->parameter_pack_p;
1552 target->parameter_pack_p = false;
1553 }
1554 else
1555 declarator->parameter_pack_p = false;
1556
1557 declarator->std_attributes = attributes;
1558
1559 return declarator;
1560 }
1561
1562 /* Like make_pointer_declarator -- but for a pointer to a non-static
1563 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1564 appertain to the pointer or reference. */
1565
1566 cp_declarator *
1567 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1568 cp_declarator *pointee,
1569 tree attributes)
1570 {
1571 cp_declarator *declarator;
1572
1573 declarator = make_declarator (cdk_ptrmem);
1574 declarator->declarator = pointee;
1575 declarator->u.pointer.qualifiers = cv_qualifiers;
1576 declarator->u.pointer.class_type = class_type;
1577
1578 if (pointee)
1579 {
1580 declarator->parameter_pack_p = pointee->parameter_pack_p;
1581 pointee->parameter_pack_p = false;
1582 }
1583 else
1584 declarator->parameter_pack_p = false;
1585
1586 declarator->std_attributes = attributes;
1587
1588 return declarator;
1589 }
1590
1591 /* Make a declarator for the function given by TARGET, with the
1592 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1593 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1594 indicates what exceptions can be thrown. */
1595
1596 cp_declarator *
1597 make_call_declarator (cp_declarator *target,
1598 tree parms,
1599 cp_cv_quals cv_qualifiers,
1600 cp_virt_specifiers virt_specifiers,
1601 cp_ref_qualifier ref_qualifier,
1602 tree tx_qualifier,
1603 tree exception_specification,
1604 tree late_return_type,
1605 tree requires_clause)
1606 {
1607 cp_declarator *declarator;
1608
1609 declarator = make_declarator (cdk_function);
1610 declarator->declarator = target;
1611 declarator->u.function.parameters = parms;
1612 declarator->u.function.qualifiers = cv_qualifiers;
1613 declarator->u.function.virt_specifiers = virt_specifiers;
1614 declarator->u.function.ref_qualifier = ref_qualifier;
1615 declarator->u.function.tx_qualifier = tx_qualifier;
1616 declarator->u.function.exception_specification = exception_specification;
1617 declarator->u.function.late_return_type = late_return_type;
1618 declarator->u.function.requires_clause = requires_clause;
1619 if (target)
1620 {
1621 declarator->id_loc = target->id_loc;
1622 declarator->parameter_pack_p = target->parameter_pack_p;
1623 target->parameter_pack_p = false;
1624 }
1625 else
1626 declarator->parameter_pack_p = false;
1627
1628 return declarator;
1629 }
1630
1631 /* Make a declarator for an array of BOUNDS elements, each of which is
1632 defined by ELEMENT. */
1633
1634 cp_declarator *
1635 make_array_declarator (cp_declarator *element, tree bounds)
1636 {
1637 cp_declarator *declarator;
1638
1639 declarator = make_declarator (cdk_array);
1640 declarator->declarator = element;
1641 declarator->u.array.bounds = bounds;
1642 if (element)
1643 {
1644 declarator->id_loc = element->id_loc;
1645 declarator->parameter_pack_p = element->parameter_pack_p;
1646 element->parameter_pack_p = false;
1647 }
1648 else
1649 declarator->parameter_pack_p = false;
1650
1651 return declarator;
1652 }
1653
1654 /* Determine whether the declarator we've seen so far can be a
1655 parameter pack, when followed by an ellipsis. */
1656 static bool
1657 declarator_can_be_parameter_pack (cp_declarator *declarator)
1658 {
1659 if (declarator && declarator->parameter_pack_p)
1660 /* We already saw an ellipsis. */
1661 return false;
1662
1663 /* Search for a declarator name, or any other declarator that goes
1664 after the point where the ellipsis could appear in a parameter
1665 pack. If we find any of these, then this declarator cannot be
1666 made into a parameter pack. */
1667 bool found = false;
1668 while (declarator && !found)
1669 {
1670 switch ((int)declarator->kind)
1671 {
1672 case cdk_id:
1673 case cdk_array:
1674 case cdk_decomp:
1675 found = true;
1676 break;
1677
1678 case cdk_error:
1679 return true;
1680
1681 default:
1682 declarator = declarator->declarator;
1683 break;
1684 }
1685 }
1686
1687 return !found;
1688 }
1689
1690 cp_parameter_declarator *no_parameters;
1691
1692 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1693 DECLARATOR and DEFAULT_ARGUMENT. */
1694
1695 cp_parameter_declarator *
1696 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1697 cp_declarator *declarator,
1698 tree default_argument,
1699 location_t loc,
1700 bool template_parameter_pack_p = false)
1701 {
1702 cp_parameter_declarator *parameter;
1703
1704 parameter = ((cp_parameter_declarator *)
1705 alloc_declarator (sizeof (cp_parameter_declarator)));
1706 parameter->next = NULL;
1707 if (decl_specifiers)
1708 parameter->decl_specifiers = *decl_specifiers;
1709 else
1710 clear_decl_specs (&parameter->decl_specifiers);
1711 parameter->declarator = declarator;
1712 parameter->default_argument = default_argument;
1713 parameter->template_parameter_pack_p = template_parameter_pack_p;
1714 parameter->loc = loc;
1715
1716 return parameter;
1717 }
1718
1719 /* Returns true iff DECLARATOR is a declaration for a function. */
1720
1721 static bool
1722 function_declarator_p (const cp_declarator *declarator)
1723 {
1724 while (declarator)
1725 {
1726 if (declarator->kind == cdk_function
1727 && declarator->declarator->kind == cdk_id)
1728 return true;
1729 if (declarator->kind == cdk_id
1730 || declarator->kind == cdk_decomp
1731 || declarator->kind == cdk_error)
1732 return false;
1733 declarator = declarator->declarator;
1734 }
1735 return false;
1736 }
1737
1738 /* The parser. */
1739
1740 /* Overview
1741 --------
1742
1743 A cp_parser parses the token stream as specified by the C++
1744 grammar. Its job is purely parsing, not semantic analysis. For
1745 example, the parser breaks the token stream into declarators,
1746 expressions, statements, and other similar syntactic constructs.
1747 It does not check that the types of the expressions on either side
1748 of an assignment-statement are compatible, or that a function is
1749 not declared with a parameter of type `void'.
1750
1751 The parser invokes routines elsewhere in the compiler to perform
1752 semantic analysis and to build up the abstract syntax tree for the
1753 code processed.
1754
1755 The parser (and the template instantiation code, which is, in a
1756 way, a close relative of parsing) are the only parts of the
1757 compiler that should be calling push_scope and pop_scope, or
1758 related functions. The parser (and template instantiation code)
1759 keeps track of what scope is presently active; everything else
1760 should simply honor that. (The code that generates static
1761 initializers may also need to set the scope, in order to check
1762 access control correctly when emitting the initializers.)
1763
1764 Methodology
1765 -----------
1766
1767 The parser is of the standard recursive-descent variety. Upcoming
1768 tokens in the token stream are examined in order to determine which
1769 production to use when parsing a non-terminal. Some C++ constructs
1770 require arbitrary look ahead to disambiguate. For example, it is
1771 impossible, in the general case, to tell whether a statement is an
1772 expression or declaration without scanning the entire statement.
1773 Therefore, the parser is capable of "parsing tentatively." When the
1774 parser is not sure what construct comes next, it enters this mode.
1775 Then, while we attempt to parse the construct, the parser queues up
1776 error messages, rather than issuing them immediately, and saves the
1777 tokens it consumes. If the construct is parsed successfully, the
1778 parser "commits", i.e., it issues any queued error messages and
1779 the tokens that were being preserved are permanently discarded.
1780 If, however, the construct is not parsed successfully, the parser
1781 rolls back its state completely so that it can resume parsing using
1782 a different alternative.
1783
1784 Future Improvements
1785 -------------------
1786
1787 The performance of the parser could probably be improved substantially.
1788 We could often eliminate the need to parse tentatively by looking ahead
1789 a little bit. In some places, this approach might not entirely eliminate
1790 the need to parse tentatively, but it might still speed up the average
1791 case. */
1792
1793 /* Flags that are passed to some parsing functions. These values can
1794 be bitwise-ored together. */
1795
1796 enum
1797 {
1798 /* No flags. */
1799 CP_PARSER_FLAGS_NONE = 0x0,
1800 /* The construct is optional. If it is not present, then no error
1801 should be issued. */
1802 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1803 /* When parsing a type-specifier, treat user-defined type-names
1804 as non-type identifiers. */
1805 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1806 /* When parsing a type-specifier, do not try to parse a class-specifier
1807 or enum-specifier. */
1808 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1809 /* When parsing a decl-specifier-seq, only allow type-specifier or
1810 constexpr. */
1811 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1812 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1813 for C++2A consteval. */
1814 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1815 /* When parsing a decl-specifier-seq, allow missing typename. */
1816 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
1817 /* When parsing of the noexcept-specifier should be delayed. */
1818 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
1819 /* When parsing a consteval declarator. */
1820 CP_PARSER_FLAGS_CONSTEVAL = 0x80
1821 };
1822
1823 /* This type is used for parameters and variables which hold
1824 combinations of the above flags. */
1825 typedef int cp_parser_flags;
1826
1827 /* The different kinds of declarators we want to parse. */
1828
1829 enum cp_parser_declarator_kind
1830 {
1831 /* We want an abstract declarator. */
1832 CP_PARSER_DECLARATOR_ABSTRACT,
1833 /* We want a named declarator. */
1834 CP_PARSER_DECLARATOR_NAMED,
1835 /* We don't mind, but the name must be an unqualified-id. */
1836 CP_PARSER_DECLARATOR_EITHER
1837 };
1838
1839 /* The precedence values used to parse binary expressions. The minimum value
1840 of PREC must be 1, because zero is reserved to quickly discriminate
1841 binary operators from other tokens. */
1842
1843 enum cp_parser_prec
1844 {
1845 PREC_NOT_OPERATOR,
1846 PREC_LOGICAL_OR_EXPRESSION,
1847 PREC_LOGICAL_AND_EXPRESSION,
1848 PREC_INCLUSIVE_OR_EXPRESSION,
1849 PREC_EXCLUSIVE_OR_EXPRESSION,
1850 PREC_AND_EXPRESSION,
1851 PREC_EQUALITY_EXPRESSION,
1852 PREC_RELATIONAL_EXPRESSION,
1853 PREC_SPACESHIP_EXPRESSION,
1854 PREC_SHIFT_EXPRESSION,
1855 PREC_ADDITIVE_EXPRESSION,
1856 PREC_MULTIPLICATIVE_EXPRESSION,
1857 PREC_PM_EXPRESSION,
1858 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1859 };
1860
1861 /* A mapping from a token type to a corresponding tree node type, with a
1862 precedence value. */
1863
1864 struct cp_parser_binary_operations_map_node
1865 {
1866 /* The token type. */
1867 enum cpp_ttype token_type;
1868 /* The corresponding tree code. */
1869 enum tree_code tree_type;
1870 /* The precedence of this operator. */
1871 enum cp_parser_prec prec;
1872 };
1873
1874 struct cp_parser_expression_stack_entry
1875 {
1876 /* Left hand side of the binary operation we are currently
1877 parsing. */
1878 cp_expr lhs;
1879 /* Original tree code for left hand side, if it was a binary
1880 expression itself (used for -Wparentheses). */
1881 enum tree_code lhs_type;
1882 /* Tree code for the binary operation we are parsing. */
1883 enum tree_code tree_type;
1884 /* Precedence of the binary operation we are parsing. */
1885 enum cp_parser_prec prec;
1886 /* Location of the binary operation we are parsing. */
1887 location_t loc;
1888 };
1889
1890 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1891 entries because precedence levels on the stack are monotonically
1892 increasing. */
1893 typedef struct cp_parser_expression_stack_entry
1894 cp_parser_expression_stack[NUM_PREC_VALUES];
1895
1896 /* Prototypes. */
1897
1898 /* Constructors and destructors. */
1899
1900 static cp_parser_context *cp_parser_context_new
1901 (cp_parser_context *);
1902
1903 /* Class variables. */
1904
1905 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1906
1907 /* The operator-precedence table used by cp_parser_binary_expression.
1908 Transformed into an associative array (binops_by_token) by
1909 cp_parser_new. */
1910
1911 static const cp_parser_binary_operations_map_node binops[] = {
1912 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1913 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1914
1915 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1916 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1917 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1918
1919 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1920 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1921
1922 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1923 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1924
1925 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
1926
1927 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1928 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1929 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1930 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1931
1932 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1933 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1934
1935 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1936
1937 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1938
1939 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1940
1941 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1942
1943 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1944 };
1945
1946 /* The same as binops, but initialized by cp_parser_new so that
1947 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1948 for speed. */
1949 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1950
1951 /* Constructors and destructors. */
1952
1953 /* Construct a new context. The context below this one on the stack
1954 is given by NEXT. */
1955
1956 static cp_parser_context *
1957 cp_parser_context_new (cp_parser_context* next)
1958 {
1959 cp_parser_context *context;
1960
1961 /* Allocate the storage. */
1962 if (cp_parser_context_free_list != NULL)
1963 {
1964 /* Pull the first entry from the free list. */
1965 context = cp_parser_context_free_list;
1966 cp_parser_context_free_list = context->next;
1967 memset (context, 0, sizeof (*context));
1968 }
1969 else
1970 context = ggc_cleared_alloc<cp_parser_context> ();
1971
1972 /* No errors have occurred yet in this context. */
1973 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1974 /* If this is not the bottommost context, copy information that we
1975 need from the previous context. */
1976 if (next)
1977 {
1978 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1979 expression, then we are parsing one in this context, too. */
1980 context->object_type = next->object_type;
1981 /* Thread the stack. */
1982 context->next = next;
1983 }
1984
1985 return context;
1986 }
1987
1988 /* Managing the unparsed function queues. */
1989
1990 #define unparsed_funs_with_default_args \
1991 parser->unparsed_queues->last ().funs_with_default_args
1992 #define unparsed_funs_with_definitions \
1993 parser->unparsed_queues->last ().funs_with_definitions
1994 #define unparsed_nsdmis \
1995 parser->unparsed_queues->last ().nsdmis
1996 #define unparsed_noexcepts \
1997 parser->unparsed_queues->last ().noexcepts
1998
1999 static void
2000 push_unparsed_function_queues (cp_parser *parser)
2001 {
2002 cp_unparsed_functions_entry e = { NULL, make_tree_vector (), NULL, NULL };
2003 vec_safe_push (parser->unparsed_queues, e);
2004 }
2005
2006 static void
2007 pop_unparsed_function_queues (cp_parser *parser)
2008 {
2009 release_tree_vector (unparsed_funs_with_definitions);
2010 parser->unparsed_queues->pop ();
2011 }
2012
2013 /* Prototypes. */
2014
2015 /* Constructors and destructors. */
2016
2017 static cp_parser *cp_parser_new
2018 (void);
2019
2020 /* Routines to parse various constructs.
2021
2022 Those that return `tree' will return the error_mark_node (rather
2023 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2024 Sometimes, they will return an ordinary node if error-recovery was
2025 attempted, even though a parse error occurred. So, to check
2026 whether or not a parse error occurred, you should always use
2027 cp_parser_error_occurred. If the construct is optional (indicated
2028 either by an `_opt' in the name of the function that does the
2029 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2030 the construct is not present. */
2031
2032 /* Lexical conventions [gram.lex] */
2033
2034 static cp_expr cp_parser_identifier
2035 (cp_parser *);
2036 static cp_expr cp_parser_string_literal
2037 (cp_parser *, bool, bool, bool);
2038 static cp_expr cp_parser_userdef_char_literal
2039 (cp_parser *);
2040 static tree cp_parser_userdef_string_literal
2041 (tree);
2042 static cp_expr cp_parser_userdef_numeric_literal
2043 (cp_parser *);
2044
2045 /* Basic concepts [gram.basic] */
2046
2047 static void cp_parser_translation_unit (cp_parser *);
2048
2049 /* Expressions [gram.expr] */
2050
2051 static cp_expr cp_parser_primary_expression
2052 (cp_parser *, bool, bool, bool, cp_id_kind *);
2053 static cp_expr cp_parser_id_expression
2054 (cp_parser *, bool, bool, bool *, bool, bool);
2055 static cp_expr cp_parser_unqualified_id
2056 (cp_parser *, bool, bool, bool, bool);
2057 static tree cp_parser_nested_name_specifier_opt
2058 (cp_parser *, bool, bool, bool, bool, bool = false);
2059 static tree cp_parser_nested_name_specifier
2060 (cp_parser *, bool, bool, bool, bool);
2061 static tree cp_parser_qualifying_entity
2062 (cp_parser *, bool, bool, bool, bool, bool);
2063 static cp_expr cp_parser_postfix_expression
2064 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2065 static tree cp_parser_postfix_open_square_expression
2066 (cp_parser *, tree, bool, bool);
2067 static tree cp_parser_postfix_dot_deref_expression
2068 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2069 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2070 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2071 bool = false);
2072 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2073 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2074 static void cp_parser_pseudo_destructor_name
2075 (cp_parser *, tree, tree *, tree *);
2076 static cp_expr cp_parser_unary_expression
2077 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2078 static enum tree_code cp_parser_unary_operator
2079 (cp_token *);
2080 static tree cp_parser_has_attribute_expression
2081 (cp_parser *);
2082 static tree cp_parser_new_expression
2083 (cp_parser *);
2084 static vec<tree, va_gc> *cp_parser_new_placement
2085 (cp_parser *);
2086 static tree cp_parser_new_type_id
2087 (cp_parser *, tree *);
2088 static cp_declarator *cp_parser_new_declarator_opt
2089 (cp_parser *);
2090 static cp_declarator *cp_parser_direct_new_declarator
2091 (cp_parser *);
2092 static vec<tree, va_gc> *cp_parser_new_initializer
2093 (cp_parser *);
2094 static tree cp_parser_delete_expression
2095 (cp_parser *);
2096 static cp_expr cp_parser_cast_expression
2097 (cp_parser *, bool, bool, bool, cp_id_kind *);
2098 static cp_expr cp_parser_binary_expression
2099 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2100 static tree cp_parser_question_colon_clause
2101 (cp_parser *, cp_expr);
2102 static cp_expr cp_parser_assignment_expression
2103 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2104 static enum tree_code cp_parser_assignment_operator_opt
2105 (cp_parser *);
2106 static cp_expr cp_parser_expression
2107 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2108 static cp_expr cp_parser_constant_expression
2109 (cp_parser *, bool = false, bool * = NULL, bool = false);
2110 static cp_expr cp_parser_builtin_offsetof
2111 (cp_parser *);
2112 static cp_expr cp_parser_lambda_expression
2113 (cp_parser *);
2114 static void cp_parser_lambda_introducer
2115 (cp_parser *, tree);
2116 static bool cp_parser_lambda_declarator_opt
2117 (cp_parser *, tree);
2118 static void cp_parser_lambda_body
2119 (cp_parser *, tree);
2120
2121 /* Statements [gram.stmt.stmt] */
2122
2123 static void cp_parser_statement
2124 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2125 static void cp_parser_label_for_labeled_statement
2126 (cp_parser *, tree);
2127 static tree cp_parser_expression_statement
2128 (cp_parser *, tree);
2129 static tree cp_parser_compound_statement
2130 (cp_parser *, tree, int, bool);
2131 static void cp_parser_statement_seq_opt
2132 (cp_parser *, tree);
2133 static tree cp_parser_selection_statement
2134 (cp_parser *, bool *, vec<tree> *);
2135 static tree cp_parser_condition
2136 (cp_parser *);
2137 static tree cp_parser_iteration_statement
2138 (cp_parser *, bool *, bool, unsigned short);
2139 static bool cp_parser_init_statement
2140 (cp_parser *, tree *decl);
2141 static tree cp_parser_for
2142 (cp_parser *, bool, unsigned short);
2143 static tree cp_parser_c_for
2144 (cp_parser *, tree, tree, bool, unsigned short);
2145 static tree cp_parser_range_for
2146 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2147 static void do_range_for_auto_deduction
2148 (tree, tree);
2149 static tree cp_parser_perform_range_for_lookup
2150 (tree, tree *, tree *);
2151 static tree cp_parser_range_for_member_function
2152 (tree, tree);
2153 static tree cp_parser_jump_statement
2154 (cp_parser *);
2155 static void cp_parser_declaration_statement
2156 (cp_parser *);
2157
2158 static tree cp_parser_implicitly_scoped_statement
2159 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2160 static void cp_parser_already_scoped_statement
2161 (cp_parser *, bool *, const token_indent_info &);
2162
2163 /* Declarations [gram.dcl.dcl] */
2164
2165 static void cp_parser_declaration_seq_opt
2166 (cp_parser *);
2167 static void cp_parser_declaration
2168 (cp_parser *);
2169 static void cp_parser_toplevel_declaration
2170 (cp_parser *);
2171 static void cp_parser_block_declaration
2172 (cp_parser *, bool);
2173 static void cp_parser_simple_declaration
2174 (cp_parser *, bool, tree *);
2175 static void cp_parser_decl_specifier_seq
2176 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2177 static tree cp_parser_storage_class_specifier_opt
2178 (cp_parser *);
2179 static tree cp_parser_function_specifier_opt
2180 (cp_parser *, cp_decl_specifier_seq *);
2181 static tree cp_parser_type_specifier
2182 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2183 int *, bool *);
2184 static tree cp_parser_simple_type_specifier
2185 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2186 static tree cp_parser_placeholder_type_specifier
2187 (cp_parser *, location_t, tree, bool);
2188 static tree cp_parser_type_name
2189 (cp_parser *, bool);
2190 static tree cp_parser_nonclass_name
2191 (cp_parser* parser);
2192 static tree cp_parser_elaborated_type_specifier
2193 (cp_parser *, bool, bool);
2194 static tree cp_parser_enum_specifier
2195 (cp_parser *);
2196 static void cp_parser_enumerator_list
2197 (cp_parser *, tree);
2198 static void cp_parser_enumerator_definition
2199 (cp_parser *, tree);
2200 static tree cp_parser_namespace_name
2201 (cp_parser *);
2202 static void cp_parser_namespace_definition
2203 (cp_parser *);
2204 static void cp_parser_namespace_body
2205 (cp_parser *);
2206 static tree cp_parser_qualified_namespace_specifier
2207 (cp_parser *);
2208 static void cp_parser_namespace_alias_definition
2209 (cp_parser *);
2210 static bool cp_parser_using_declaration
2211 (cp_parser *, bool);
2212 static void cp_parser_using_directive
2213 (cp_parser *);
2214 static tree cp_parser_alias_declaration
2215 (cp_parser *);
2216 static void cp_parser_asm_definition
2217 (cp_parser *);
2218 static void cp_parser_linkage_specification
2219 (cp_parser *);
2220 static void cp_parser_static_assert
2221 (cp_parser *, bool);
2222 static tree cp_parser_decltype
2223 (cp_parser *);
2224 static tree cp_parser_decomposition_declaration
2225 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2226
2227 /* Declarators [gram.dcl.decl] */
2228
2229 static tree cp_parser_init_declarator
2230 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2231 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2232 location_t *, tree *);
2233 static cp_declarator *cp_parser_declarator
2234 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2235 bool, bool, bool);
2236 static cp_declarator *cp_parser_direct_declarator
2237 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2238 bool);
2239 static enum tree_code cp_parser_ptr_operator
2240 (cp_parser *, tree *, cp_cv_quals *, tree *);
2241 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2242 (cp_parser *);
2243 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2244 (cp_parser *);
2245 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2246 (cp_parser *);
2247 static tree cp_parser_tx_qualifier_opt
2248 (cp_parser *);
2249 static tree cp_parser_late_return_type_opt
2250 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2251 static tree cp_parser_declarator_id
2252 (cp_parser *, bool);
2253 static tree cp_parser_type_id
2254 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2255 static tree cp_parser_template_type_arg
2256 (cp_parser *);
2257 static tree cp_parser_trailing_type_id (cp_parser *);
2258 static tree cp_parser_type_id_1
2259 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2260 static void cp_parser_type_specifier_seq
2261 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2262 static tree cp_parser_parameter_declaration_clause
2263 (cp_parser *, cp_parser_flags);
2264 static tree cp_parser_parameter_declaration_list
2265 (cp_parser *, cp_parser_flags);
2266 static cp_parameter_declarator *cp_parser_parameter_declaration
2267 (cp_parser *, cp_parser_flags, bool, bool *);
2268 static tree cp_parser_default_argument
2269 (cp_parser *, bool);
2270 static void cp_parser_function_body
2271 (cp_parser *, bool);
2272 static tree cp_parser_initializer
2273 (cp_parser *, bool *, bool *, bool = false);
2274 static cp_expr cp_parser_initializer_clause
2275 (cp_parser *, bool *);
2276 static cp_expr cp_parser_braced_list
2277 (cp_parser*, bool*);
2278 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2279 (cp_parser *, bool *, bool *);
2280
2281 static void cp_parser_ctor_initializer_opt_and_function_body
2282 (cp_parser *, bool);
2283
2284 static tree cp_parser_late_parsing_omp_declare_simd
2285 (cp_parser *, tree);
2286
2287 static tree cp_parser_late_parsing_oacc_routine
2288 (cp_parser *, tree);
2289
2290 static tree synthesize_implicit_template_parm
2291 (cp_parser *, tree);
2292 static tree finish_fully_implicit_template
2293 (cp_parser *, tree);
2294 static void abort_fully_implicit_template
2295 (cp_parser *);
2296
2297 /* Classes [gram.class] */
2298
2299 static tree cp_parser_class_name
2300 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2301 static tree cp_parser_class_specifier
2302 (cp_parser *);
2303 static tree cp_parser_class_head
2304 (cp_parser *, bool *);
2305 static enum tag_types cp_parser_class_key
2306 (cp_parser *);
2307 static void cp_parser_type_parameter_key
2308 (cp_parser* parser);
2309 static void cp_parser_member_specification_opt
2310 (cp_parser *);
2311 static void cp_parser_member_declaration
2312 (cp_parser *);
2313 static tree cp_parser_pure_specifier
2314 (cp_parser *);
2315 static tree cp_parser_constant_initializer
2316 (cp_parser *);
2317
2318 /* Derived classes [gram.class.derived] */
2319
2320 static tree cp_parser_base_clause
2321 (cp_parser *);
2322 static tree cp_parser_base_specifier
2323 (cp_parser *);
2324
2325 /* Special member functions [gram.special] */
2326
2327 static tree cp_parser_conversion_function_id
2328 (cp_parser *);
2329 static tree cp_parser_conversion_type_id
2330 (cp_parser *);
2331 static cp_declarator *cp_parser_conversion_declarator_opt
2332 (cp_parser *);
2333 static void cp_parser_ctor_initializer_opt
2334 (cp_parser *);
2335 static void cp_parser_mem_initializer_list
2336 (cp_parser *);
2337 static tree cp_parser_mem_initializer
2338 (cp_parser *);
2339 static tree cp_parser_mem_initializer_id
2340 (cp_parser *);
2341
2342 /* Overloading [gram.over] */
2343
2344 static cp_expr cp_parser_operator_function_id
2345 (cp_parser *);
2346 static cp_expr cp_parser_operator
2347 (cp_parser *, location_t);
2348
2349 /* Templates [gram.temp] */
2350
2351 static void cp_parser_template_declaration
2352 (cp_parser *, bool);
2353 static tree cp_parser_template_parameter_list
2354 (cp_parser *);
2355 static tree cp_parser_template_parameter
2356 (cp_parser *, bool *, bool *);
2357 static tree cp_parser_type_parameter
2358 (cp_parser *, bool *);
2359 static tree cp_parser_template_id
2360 (cp_parser *, bool, bool, enum tag_types, bool);
2361 static tree cp_parser_template_id_expr
2362 (cp_parser *, bool, bool, bool);
2363 static tree cp_parser_template_name
2364 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2365 static tree cp_parser_template_argument_list
2366 (cp_parser *);
2367 static tree cp_parser_template_argument
2368 (cp_parser *);
2369 static void cp_parser_explicit_instantiation
2370 (cp_parser *);
2371 static void cp_parser_explicit_specialization
2372 (cp_parser *);
2373
2374 /* Exception handling [gram.except] */
2375
2376 static tree cp_parser_try_block
2377 (cp_parser *);
2378 static void cp_parser_function_try_block
2379 (cp_parser *);
2380 static void cp_parser_handler_seq
2381 (cp_parser *);
2382 static void cp_parser_handler
2383 (cp_parser *);
2384 static tree cp_parser_exception_declaration
2385 (cp_parser *);
2386 static tree cp_parser_throw_expression
2387 (cp_parser *);
2388 static tree cp_parser_exception_specification_opt
2389 (cp_parser *, cp_parser_flags);
2390 static tree cp_parser_type_id_list
2391 (cp_parser *);
2392 static tree cp_parser_noexcept_specification_opt
2393 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2394
2395 /* GNU Extensions */
2396
2397 static tree cp_parser_asm_specification_opt
2398 (cp_parser *);
2399 static tree cp_parser_asm_operand_list
2400 (cp_parser *);
2401 static tree cp_parser_asm_clobber_list
2402 (cp_parser *);
2403 static tree cp_parser_asm_label_list
2404 (cp_parser *);
2405 static bool cp_next_tokens_can_be_attribute_p
2406 (cp_parser *);
2407 static bool cp_next_tokens_can_be_gnu_attribute_p
2408 (cp_parser *);
2409 static bool cp_next_tokens_can_be_std_attribute_p
2410 (cp_parser *);
2411 static bool cp_nth_tokens_can_be_std_attribute_p
2412 (cp_parser *, size_t);
2413 static bool cp_nth_tokens_can_be_gnu_attribute_p
2414 (cp_parser *, size_t);
2415 static bool cp_nth_tokens_can_be_attribute_p
2416 (cp_parser *, size_t);
2417 static tree cp_parser_attributes_opt
2418 (cp_parser *);
2419 static tree cp_parser_gnu_attributes_opt
2420 (cp_parser *);
2421 static tree cp_parser_gnu_attribute_list
2422 (cp_parser *, bool = false);
2423 static tree cp_parser_std_attribute
2424 (cp_parser *, tree);
2425 static tree cp_parser_std_attribute_spec
2426 (cp_parser *);
2427 static tree cp_parser_std_attribute_spec_seq
2428 (cp_parser *);
2429 static size_t cp_parser_skip_attributes_opt
2430 (cp_parser *, size_t);
2431 static bool cp_parser_extension_opt
2432 (cp_parser *, int *);
2433 static void cp_parser_label_declaration
2434 (cp_parser *);
2435
2436 /* Concept Extensions */
2437
2438 static tree cp_parser_concept_definition
2439 (cp_parser *);
2440 static tree cp_parser_constraint_expression
2441 (cp_parser *);
2442 static tree cp_parser_requires_clause_opt
2443 (cp_parser *, bool);
2444 static tree cp_parser_requires_expression
2445 (cp_parser *);
2446 static tree cp_parser_requirement_parameter_list
2447 (cp_parser *);
2448 static tree cp_parser_requirement_body
2449 (cp_parser *);
2450 static tree cp_parser_requirement_seq
2451 (cp_parser *);
2452 static tree cp_parser_requirement
2453 (cp_parser *);
2454 static tree cp_parser_simple_requirement
2455 (cp_parser *);
2456 static tree cp_parser_compound_requirement
2457 (cp_parser *);
2458 static tree cp_parser_type_requirement
2459 (cp_parser *);
2460 static tree cp_parser_nested_requirement
2461 (cp_parser *);
2462
2463 /* Transactional Memory Extensions */
2464
2465 static tree cp_parser_transaction
2466 (cp_parser *, cp_token *);
2467 static tree cp_parser_transaction_expression
2468 (cp_parser *, enum rid);
2469 static void cp_parser_function_transaction
2470 (cp_parser *, enum rid);
2471 static tree cp_parser_transaction_cancel
2472 (cp_parser *);
2473
2474 enum pragma_context {
2475 pragma_external,
2476 pragma_member,
2477 pragma_objc_icode,
2478 pragma_stmt,
2479 pragma_compound
2480 };
2481 static bool cp_parser_pragma
2482 (cp_parser *, enum pragma_context, bool *);
2483
2484 /* Objective-C++ Productions */
2485
2486 static tree cp_parser_objc_message_receiver
2487 (cp_parser *);
2488 static tree cp_parser_objc_message_args
2489 (cp_parser *);
2490 static tree cp_parser_objc_message_expression
2491 (cp_parser *);
2492 static cp_expr cp_parser_objc_encode_expression
2493 (cp_parser *);
2494 static tree cp_parser_objc_defs_expression
2495 (cp_parser *);
2496 static tree cp_parser_objc_protocol_expression
2497 (cp_parser *);
2498 static tree cp_parser_objc_selector_expression
2499 (cp_parser *);
2500 static cp_expr cp_parser_objc_expression
2501 (cp_parser *);
2502 static bool cp_parser_objc_selector_p
2503 (enum cpp_ttype);
2504 static tree cp_parser_objc_selector
2505 (cp_parser *);
2506 static tree cp_parser_objc_protocol_refs_opt
2507 (cp_parser *);
2508 static void cp_parser_objc_declaration
2509 (cp_parser *, tree);
2510 static tree cp_parser_objc_statement
2511 (cp_parser *);
2512 static bool cp_parser_objc_valid_prefix_attributes
2513 (cp_parser *, tree *);
2514 static void cp_parser_objc_at_property_declaration
2515 (cp_parser *) ;
2516 static void cp_parser_objc_at_synthesize_declaration
2517 (cp_parser *) ;
2518 static void cp_parser_objc_at_dynamic_declaration
2519 (cp_parser *) ;
2520 static tree cp_parser_objc_struct_declaration
2521 (cp_parser *) ;
2522
2523 /* Utility Routines */
2524
2525 static cp_expr cp_parser_lookup_name
2526 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2527 static tree cp_parser_lookup_name_simple
2528 (cp_parser *, tree, location_t);
2529 static tree cp_parser_maybe_treat_template_as_class
2530 (tree, bool);
2531 static bool cp_parser_check_declarator_template_parameters
2532 (cp_parser *, cp_declarator *, location_t);
2533 static bool cp_parser_check_template_parameters
2534 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2535 static cp_expr cp_parser_simple_cast_expression
2536 (cp_parser *);
2537 static tree cp_parser_global_scope_opt
2538 (cp_parser *, bool);
2539 static bool cp_parser_constructor_declarator_p
2540 (cp_parser *, cp_parser_flags, bool);
2541 static tree cp_parser_function_definition_from_specifiers_and_declarator
2542 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2543 static tree cp_parser_function_definition_after_declarator
2544 (cp_parser *, bool);
2545 static bool cp_parser_template_declaration_after_export
2546 (cp_parser *, bool);
2547 static void cp_parser_perform_template_parameter_access_checks
2548 (vec<deferred_access_check, va_gc> *);
2549 static tree cp_parser_single_declaration
2550 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2551 static cp_expr cp_parser_functional_cast
2552 (cp_parser *, tree);
2553 static tree cp_parser_save_member_function_body
2554 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2555 static tree cp_parser_save_nsdmi
2556 (cp_parser *);
2557 static tree cp_parser_enclosed_template_argument_list
2558 (cp_parser *);
2559 static void cp_parser_save_default_args
2560 (cp_parser *, tree);
2561 static void cp_parser_late_parsing_for_member
2562 (cp_parser *, tree);
2563 static tree cp_parser_late_parse_one_default_arg
2564 (cp_parser *, tree, tree, tree);
2565 static void cp_parser_late_parsing_nsdmi
2566 (cp_parser *, tree);
2567 static void cp_parser_late_parsing_default_args
2568 (cp_parser *, tree);
2569 static tree cp_parser_sizeof_operand
2570 (cp_parser *, enum rid);
2571 static cp_expr cp_parser_trait_expr
2572 (cp_parser *, enum rid);
2573 static bool cp_parser_declares_only_class_p
2574 (cp_parser *);
2575 static void cp_parser_set_storage_class
2576 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2577 static void cp_parser_set_decl_spec_type
2578 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2579 static void set_and_check_decl_spec_loc
2580 (cp_decl_specifier_seq *decl_specs,
2581 cp_decl_spec ds, cp_token *);
2582 static bool cp_parser_friend_p
2583 (const cp_decl_specifier_seq *);
2584 static void cp_parser_required_error
2585 (cp_parser *, required_token, bool, location_t);
2586 static cp_token *cp_parser_require
2587 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2588 static cp_token *cp_parser_require_keyword
2589 (cp_parser *, enum rid, required_token);
2590 static bool cp_parser_token_starts_function_definition_p
2591 (cp_token *);
2592 static bool cp_parser_next_token_starts_class_definition_p
2593 (cp_parser *);
2594 static bool cp_parser_next_token_ends_template_argument_p
2595 (cp_parser *);
2596 static bool cp_parser_nth_token_starts_template_argument_list_p
2597 (cp_parser *, size_t);
2598 static enum tag_types cp_parser_token_is_class_key
2599 (cp_token *);
2600 static enum tag_types cp_parser_token_is_type_parameter_key
2601 (cp_token *);
2602 static void cp_parser_check_class_key
2603 (enum tag_types, tree type);
2604 static void cp_parser_check_access_in_redeclaration
2605 (tree type, location_t location);
2606 static bool cp_parser_optional_template_keyword
2607 (cp_parser *);
2608 static void cp_parser_pre_parsed_nested_name_specifier
2609 (cp_parser *);
2610 static bool cp_parser_cache_group
2611 (cp_parser *, enum cpp_ttype, unsigned);
2612 static tree cp_parser_cache_defarg
2613 (cp_parser *parser, bool nsdmi);
2614 static void cp_parser_parse_tentatively
2615 (cp_parser *);
2616 static void cp_parser_commit_to_tentative_parse
2617 (cp_parser *);
2618 static void cp_parser_commit_to_topmost_tentative_parse
2619 (cp_parser *);
2620 static void cp_parser_abort_tentative_parse
2621 (cp_parser *);
2622 static bool cp_parser_parse_definitely
2623 (cp_parser *);
2624 static inline bool cp_parser_parsing_tentatively
2625 (cp_parser *);
2626 static bool cp_parser_uncommitted_to_tentative_parse_p
2627 (cp_parser *);
2628 static void cp_parser_error
2629 (cp_parser *, const char *);
2630 static void cp_parser_name_lookup_error
2631 (cp_parser *, tree, tree, name_lookup_error, location_t);
2632 static bool cp_parser_simulate_error
2633 (cp_parser *);
2634 static bool cp_parser_check_type_definition
2635 (cp_parser *);
2636 static void cp_parser_check_for_definition_in_return_type
2637 (cp_declarator *, tree, location_t type_location);
2638 static void cp_parser_check_for_invalid_template_id
2639 (cp_parser *, tree, enum tag_types, location_t location);
2640 static bool cp_parser_non_integral_constant_expression
2641 (cp_parser *, non_integral_constant);
2642 static void cp_parser_diagnose_invalid_type_name
2643 (cp_parser *, tree, location_t);
2644 static bool cp_parser_parse_and_diagnose_invalid_type_name
2645 (cp_parser *);
2646 static int cp_parser_skip_to_closing_parenthesis
2647 (cp_parser *, bool, bool, bool);
2648 static void cp_parser_skip_to_end_of_statement
2649 (cp_parser *);
2650 static void cp_parser_consume_semicolon_at_end_of_statement
2651 (cp_parser *);
2652 static void cp_parser_skip_to_end_of_block_or_statement
2653 (cp_parser *);
2654 static bool cp_parser_skip_to_closing_brace
2655 (cp_parser *);
2656 static void cp_parser_skip_to_end_of_template_parameter_list
2657 (cp_parser *);
2658 static void cp_parser_skip_to_pragma_eol
2659 (cp_parser*, cp_token *);
2660 static bool cp_parser_error_occurred
2661 (cp_parser *);
2662 static bool cp_parser_allow_gnu_extensions_p
2663 (cp_parser *);
2664 static bool cp_parser_is_pure_string_literal
2665 (cp_token *);
2666 static bool cp_parser_is_string_literal
2667 (cp_token *);
2668 static bool cp_parser_is_keyword
2669 (cp_token *, enum rid);
2670 static tree cp_parser_make_typename_type
2671 (cp_parser *, tree, location_t location);
2672 static cp_declarator * cp_parser_make_indirect_declarator
2673 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2674 static bool cp_parser_compound_literal_p
2675 (cp_parser *);
2676 static bool cp_parser_array_designator_p
2677 (cp_parser *);
2678 static bool cp_parser_init_statement_p
2679 (cp_parser *);
2680 static bool cp_parser_skip_to_closing_square_bracket
2681 (cp_parser *);
2682 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2683
2684 // -------------------------------------------------------------------------- //
2685 // Unevaluated Operand Guard
2686 //
2687 // Implementation of an RAII helper for unevaluated operand parsing.
2688 cp_unevaluated::cp_unevaluated ()
2689 {
2690 ++cp_unevaluated_operand;
2691 ++c_inhibit_evaluation_warnings;
2692 }
2693
2694 cp_unevaluated::~cp_unevaluated ()
2695 {
2696 --c_inhibit_evaluation_warnings;
2697 --cp_unevaluated_operand;
2698 }
2699
2700 // -------------------------------------------------------------------------- //
2701 // Tentative Parsing
2702
2703 /* Returns nonzero if we are parsing tentatively. */
2704
2705 static inline bool
2706 cp_parser_parsing_tentatively (cp_parser* parser)
2707 {
2708 return parser->context->next != NULL;
2709 }
2710
2711 /* Returns nonzero if TOKEN is a string literal. */
2712
2713 static bool
2714 cp_parser_is_pure_string_literal (cp_token* token)
2715 {
2716 return (token->type == CPP_STRING ||
2717 token->type == CPP_STRING16 ||
2718 token->type == CPP_STRING32 ||
2719 token->type == CPP_WSTRING ||
2720 token->type == CPP_UTF8STRING);
2721 }
2722
2723 /* Returns nonzero if TOKEN is a string literal
2724 of a user-defined string literal. */
2725
2726 static bool
2727 cp_parser_is_string_literal (cp_token* token)
2728 {
2729 return (cp_parser_is_pure_string_literal (token) ||
2730 token->type == CPP_STRING_USERDEF ||
2731 token->type == CPP_STRING16_USERDEF ||
2732 token->type == CPP_STRING32_USERDEF ||
2733 token->type == CPP_WSTRING_USERDEF ||
2734 token->type == CPP_UTF8STRING_USERDEF);
2735 }
2736
2737 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2738
2739 static bool
2740 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2741 {
2742 return token->keyword == keyword;
2743 }
2744
2745 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2746 PRAGMA_NONE. */
2747
2748 static enum pragma_kind
2749 cp_parser_pragma_kind (cp_token *token)
2750 {
2751 if (token->type != CPP_PRAGMA)
2752 return PRAGMA_NONE;
2753 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2754 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2755 }
2756
2757 /* Helper function for cp_parser_error.
2758 Having peeked a token of kind TOK1_KIND that might signify
2759 a conflict marker, peek successor tokens to determine
2760 if we actually do have a conflict marker.
2761 Specifically, we consider a run of 7 '<', '=' or '>' characters
2762 at the start of a line as a conflict marker.
2763 These come through the lexer as three pairs and a single,
2764 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2765 If it returns true, *OUT_LOC is written to with the location/range
2766 of the marker. */
2767
2768 static bool
2769 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2770 location_t *out_loc)
2771 {
2772 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2773 if (token2->type != tok1_kind)
2774 return false;
2775 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2776 if (token3->type != tok1_kind)
2777 return false;
2778 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2779 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2780 return false;
2781
2782 /* It must be at the start of the line. */
2783 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2784 if (LOCATION_COLUMN (start_loc) != 1)
2785 return false;
2786
2787 /* We have a conflict marker. Construct a location of the form:
2788 <<<<<<<
2789 ^~~~~~~
2790 with start == caret, finishing at the end of the marker. */
2791 location_t finish_loc = get_finish (token4->location);
2792 *out_loc = make_location (start_loc, start_loc, finish_loc);
2793
2794 return true;
2795 }
2796
2797 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2798 RT_CLOSE_PAREN. */
2799
2800 static const char *
2801 get_matching_symbol (required_token token_desc)
2802 {
2803 switch (token_desc)
2804 {
2805 default:
2806 gcc_unreachable ();
2807 return "";
2808 case RT_CLOSE_BRACE:
2809 return "{";
2810 case RT_CLOSE_PAREN:
2811 return "(";
2812 }
2813 }
2814
2815 /* Attempt to convert TOKEN_DESC from a required_token to an
2816 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2817
2818 static enum cpp_ttype
2819 get_required_cpp_ttype (required_token token_desc)
2820 {
2821 switch (token_desc)
2822 {
2823 case RT_SEMICOLON:
2824 return CPP_SEMICOLON;
2825 case RT_OPEN_PAREN:
2826 return CPP_OPEN_PAREN;
2827 case RT_CLOSE_BRACE:
2828 return CPP_CLOSE_BRACE;
2829 case RT_OPEN_BRACE:
2830 return CPP_OPEN_BRACE;
2831 case RT_CLOSE_SQUARE:
2832 return CPP_CLOSE_SQUARE;
2833 case RT_OPEN_SQUARE:
2834 return CPP_OPEN_SQUARE;
2835 case RT_COMMA:
2836 return CPP_COMMA;
2837 case RT_COLON:
2838 return CPP_COLON;
2839 case RT_CLOSE_PAREN:
2840 return CPP_CLOSE_PAREN;
2841
2842 default:
2843 /* Use CPP_EOF as a "no completions possible" code. */
2844 return CPP_EOF;
2845 }
2846 }
2847
2848
2849 /* Subroutine of cp_parser_error and cp_parser_required_error.
2850
2851 Issue a diagnostic of the form
2852 FILE:LINE: MESSAGE before TOKEN
2853 where TOKEN is the next token in the input stream. MESSAGE
2854 (specified by the caller) is usually of the form "expected
2855 OTHER-TOKEN".
2856
2857 This bypasses the check for tentative passing, and potentially
2858 adds material needed by cp_parser_required_error.
2859
2860 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2861 suggesting insertion of the missing token.
2862
2863 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2864 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2865 location. */
2866
2867 static void
2868 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2869 required_token missing_token_desc,
2870 location_t matching_location)
2871 {
2872 cp_token *token = cp_lexer_peek_token (parser->lexer);
2873 /* This diagnostic makes more sense if it is tagged to the line
2874 of the token we just peeked at. */
2875 cp_lexer_set_source_position_from_token (token);
2876
2877 if (token->type == CPP_PRAGMA)
2878 {
2879 error_at (token->location,
2880 "%<#pragma%> is not allowed here");
2881 cp_parser_skip_to_pragma_eol (parser, token);
2882 return;
2883 }
2884
2885 /* If this is actually a conflict marker, report it as such. */
2886 if (token->type == CPP_LSHIFT
2887 || token->type == CPP_RSHIFT
2888 || token->type == CPP_EQ_EQ)
2889 {
2890 location_t loc;
2891 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2892 {
2893 error_at (loc, "version control conflict marker in file");
2894 expanded_location token_exploc = expand_location (token->location);
2895 /* Consume tokens until the end of the source line. */
2896 for (;;)
2897 {
2898 cp_lexer_consume_token (parser->lexer);
2899 cp_token *next = cp_lexer_peek_token (parser->lexer);
2900 if (next->type == CPP_EOF)
2901 break;
2902 if (next->location == UNKNOWN_LOCATION
2903 || loc == UNKNOWN_LOCATION)
2904 break;
2905
2906 expanded_location next_exploc = expand_location (next->location);
2907 if (next_exploc.file != token_exploc.file)
2908 break;
2909 if (next_exploc.line != token_exploc.line)
2910 break;
2911 }
2912 return;
2913 }
2914 }
2915
2916 gcc_rich_location richloc (input_location);
2917
2918 bool added_matching_location = false;
2919
2920 if (missing_token_desc != RT_NONE)
2921 {
2922 /* Potentially supply a fix-it hint, suggesting to add the
2923 missing token immediately after the *previous* token.
2924 This may move the primary location within richloc. */
2925 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2926 location_t prev_token_loc
2927 = cp_lexer_previous_token (parser->lexer)->location;
2928 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2929
2930 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2931 Attempt to consolidate diagnostics by printing it as a
2932 secondary range within the main diagnostic. */
2933 if (matching_location != UNKNOWN_LOCATION)
2934 added_matching_location
2935 = richloc.add_location_if_nearby (matching_location);
2936 }
2937
2938 /* Actually emit the error. */
2939 c_parse_error (gmsgid,
2940 /* Because c_parser_error does not understand
2941 CPP_KEYWORD, keywords are treated like
2942 identifiers. */
2943 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2944 token->u.value, token->flags, &richloc);
2945
2946 if (missing_token_desc != RT_NONE)
2947 {
2948 /* If we weren't able to consolidate matching_location, then
2949 print it as a secondary diagnostic. */
2950 if (matching_location != UNKNOWN_LOCATION
2951 && !added_matching_location)
2952 inform (matching_location, "to match this %qs",
2953 get_matching_symbol (missing_token_desc));
2954 }
2955 }
2956
2957 /* If not parsing tentatively, issue a diagnostic of the form
2958 FILE:LINE: MESSAGE before TOKEN
2959 where TOKEN is the next token in the input stream. MESSAGE
2960 (specified by the caller) is usually of the form "expected
2961 OTHER-TOKEN". */
2962
2963 static void
2964 cp_parser_error (cp_parser* parser, const char* gmsgid)
2965 {
2966 if (!cp_parser_simulate_error (parser))
2967 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2968 }
2969
2970 /* Issue an error about name-lookup failing. NAME is the
2971 IDENTIFIER_NODE DECL is the result of
2972 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2973 the thing that we hoped to find. */
2974
2975 static void
2976 cp_parser_name_lookup_error (cp_parser* parser,
2977 tree name,
2978 tree decl,
2979 name_lookup_error desired,
2980 location_t location)
2981 {
2982 /* If name lookup completely failed, tell the user that NAME was not
2983 declared. */
2984 if (decl == error_mark_node)
2985 {
2986 if (parser->scope && parser->scope != global_namespace)
2987 error_at (location, "%<%E::%E%> has not been declared",
2988 parser->scope, name);
2989 else if (parser->scope == global_namespace)
2990 error_at (location, "%<::%E%> has not been declared", name);
2991 else if (parser->object_scope
2992 && !CLASS_TYPE_P (parser->object_scope))
2993 error_at (location, "request for member %qE in non-class type %qT",
2994 name, parser->object_scope);
2995 else if (parser->object_scope)
2996 error_at (location, "%<%T::%E%> has not been declared",
2997 parser->object_scope, name);
2998 else
2999 error_at (location, "%qE has not been declared", name);
3000 }
3001 else if (parser->scope && parser->scope != global_namespace)
3002 {
3003 switch (desired)
3004 {
3005 case NLE_TYPE:
3006 error_at (location, "%<%E::%E%> is not a type",
3007 parser->scope, name);
3008 break;
3009 case NLE_CXX98:
3010 error_at (location, "%<%E::%E%> is not a class or namespace",
3011 parser->scope, name);
3012 break;
3013 case NLE_NOT_CXX98:
3014 error_at (location,
3015 "%<%E::%E%> is not a class, namespace, or enumeration",
3016 parser->scope, name);
3017 break;
3018 default:
3019 gcc_unreachable ();
3020
3021 }
3022 }
3023 else if (parser->scope == global_namespace)
3024 {
3025 switch (desired)
3026 {
3027 case NLE_TYPE:
3028 error_at (location, "%<::%E%> is not a type", name);
3029 break;
3030 case NLE_CXX98:
3031 error_at (location, "%<::%E%> is not a class or namespace", name);
3032 break;
3033 case NLE_NOT_CXX98:
3034 error_at (location,
3035 "%<::%E%> is not a class, namespace, or enumeration",
3036 name);
3037 break;
3038 default:
3039 gcc_unreachable ();
3040 }
3041 }
3042 else
3043 {
3044 switch (desired)
3045 {
3046 case NLE_TYPE:
3047 error_at (location, "%qE is not a type", name);
3048 break;
3049 case NLE_CXX98:
3050 error_at (location, "%qE is not a class or namespace", name);
3051 break;
3052 case NLE_NOT_CXX98:
3053 error_at (location,
3054 "%qE is not a class, namespace, or enumeration", name);
3055 break;
3056 default:
3057 gcc_unreachable ();
3058 }
3059 }
3060 }
3061
3062 /* If we are parsing tentatively, remember that an error has occurred
3063 during this tentative parse. Returns true if the error was
3064 simulated; false if a message should be issued by the caller. */
3065
3066 static bool
3067 cp_parser_simulate_error (cp_parser* parser)
3068 {
3069 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3070 {
3071 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3072 return true;
3073 }
3074 return false;
3075 }
3076
3077 /* This function is called when a type is defined. If type
3078 definitions are forbidden at this point, an error message is
3079 issued. */
3080
3081 static bool
3082 cp_parser_check_type_definition (cp_parser* parser)
3083 {
3084 /* If types are forbidden here, issue a message. */
3085 if (parser->type_definition_forbidden_message)
3086 {
3087 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3088 or %qs in the message need to be interpreted. */
3089 error (parser->type_definition_forbidden_message,
3090 parser->type_definition_forbidden_message_arg);
3091 return false;
3092 }
3093 return true;
3094 }
3095
3096 /* This function is called when the DECLARATOR is processed. The TYPE
3097 was a type defined in the decl-specifiers. If it is invalid to
3098 define a type in the decl-specifiers for DECLARATOR, an error is
3099 issued. TYPE_LOCATION is the location of TYPE and is used
3100 for error reporting. */
3101
3102 static void
3103 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3104 tree type, location_t type_location)
3105 {
3106 /* [dcl.fct] forbids type definitions in return types.
3107 Unfortunately, it's not easy to know whether or not we are
3108 processing a return type until after the fact. */
3109 while (declarator
3110 && (declarator->kind == cdk_pointer
3111 || declarator->kind == cdk_reference
3112 || declarator->kind == cdk_ptrmem))
3113 declarator = declarator->declarator;
3114 if (declarator
3115 && declarator->kind == cdk_function)
3116 {
3117 error_at (type_location,
3118 "new types may not be defined in a return type");
3119 inform (type_location,
3120 "(perhaps a semicolon is missing after the definition of %qT)",
3121 type);
3122 }
3123 }
3124
3125 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3126 "<" in any valid C++ program. If the next token is indeed "<",
3127 issue a message warning the user about what appears to be an
3128 invalid attempt to form a template-id. LOCATION is the location
3129 of the type-specifier (TYPE) */
3130
3131 static void
3132 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3133 tree type,
3134 enum tag_types tag_type,
3135 location_t location)
3136 {
3137 cp_token_position start = 0;
3138
3139 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3140 {
3141 if (TREE_CODE (type) == TYPE_DECL)
3142 type = TREE_TYPE (type);
3143 if (TYPE_P (type) && !template_placeholder_p (type))
3144 error_at (location, "%qT is not a template", type);
3145 else if (identifier_p (type))
3146 {
3147 if (tag_type != none_type)
3148 error_at (location, "%qE is not a class template", type);
3149 else
3150 error_at (location, "%qE is not a template", type);
3151 }
3152 else
3153 error_at (location, "invalid template-id");
3154 /* Remember the location of the invalid "<". */
3155 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3156 start = cp_lexer_token_position (parser->lexer, true);
3157 /* Consume the "<". */
3158 cp_lexer_consume_token (parser->lexer);
3159 /* Parse the template arguments. */
3160 cp_parser_enclosed_template_argument_list (parser);
3161 /* Permanently remove the invalid template arguments so that
3162 this error message is not issued again. */
3163 if (start)
3164 cp_lexer_purge_tokens_after (parser->lexer, start);
3165 }
3166 }
3167
3168 /* If parsing an integral constant-expression, issue an error message
3169 about the fact that THING appeared and return true. Otherwise,
3170 return false. In either case, set
3171 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3172
3173 static bool
3174 cp_parser_non_integral_constant_expression (cp_parser *parser,
3175 non_integral_constant thing)
3176 {
3177 parser->non_integral_constant_expression_p = true;
3178 if (parser->integral_constant_expression_p)
3179 {
3180 if (!parser->allow_non_integral_constant_expression_p)
3181 {
3182 const char *msg = NULL;
3183 switch (thing)
3184 {
3185 case NIC_FLOAT:
3186 pedwarn (input_location, OPT_Wpedantic,
3187 "ISO C++ forbids using a floating-point literal "
3188 "in a constant-expression");
3189 return true;
3190 case NIC_CAST:
3191 error ("a cast to a type other than an integral or "
3192 "enumeration type cannot appear in a "
3193 "constant-expression");
3194 return true;
3195 case NIC_TYPEID:
3196 error ("%<typeid%> operator "
3197 "cannot appear in a constant-expression");
3198 return true;
3199 case NIC_NCC:
3200 error ("non-constant compound literals "
3201 "cannot appear in a constant-expression");
3202 return true;
3203 case NIC_FUNC_CALL:
3204 error ("a function call "
3205 "cannot appear in a constant-expression");
3206 return true;
3207 case NIC_INC:
3208 error ("an increment "
3209 "cannot appear in a constant-expression");
3210 return true;
3211 case NIC_DEC:
3212 error ("an decrement "
3213 "cannot appear in a constant-expression");
3214 return true;
3215 case NIC_ARRAY_REF:
3216 error ("an array reference "
3217 "cannot appear in a constant-expression");
3218 return true;
3219 case NIC_ADDR_LABEL:
3220 error ("the address of a label "
3221 "cannot appear in a constant-expression");
3222 return true;
3223 case NIC_OVERLOADED:
3224 error ("calls to overloaded operators "
3225 "cannot appear in a constant-expression");
3226 return true;
3227 case NIC_ASSIGNMENT:
3228 error ("an assignment cannot appear in a constant-expression");
3229 return true;
3230 case NIC_COMMA:
3231 error ("a comma operator "
3232 "cannot appear in a constant-expression");
3233 return true;
3234 case NIC_CONSTRUCTOR:
3235 error ("a call to a constructor "
3236 "cannot appear in a constant-expression");
3237 return true;
3238 case NIC_TRANSACTION:
3239 error ("a transaction expression "
3240 "cannot appear in a constant-expression");
3241 return true;
3242 case NIC_THIS:
3243 msg = "this";
3244 break;
3245 case NIC_FUNC_NAME:
3246 msg = "__FUNCTION__";
3247 break;
3248 case NIC_PRETTY_FUNC:
3249 msg = "__PRETTY_FUNCTION__";
3250 break;
3251 case NIC_C99_FUNC:
3252 msg = "__func__";
3253 break;
3254 case NIC_VA_ARG:
3255 msg = "va_arg";
3256 break;
3257 case NIC_ARROW:
3258 msg = "->";
3259 break;
3260 case NIC_POINT:
3261 msg = ".";
3262 break;
3263 case NIC_STAR:
3264 msg = "*";
3265 break;
3266 case NIC_ADDR:
3267 msg = "&";
3268 break;
3269 case NIC_PREINCREMENT:
3270 msg = "++";
3271 break;
3272 case NIC_PREDECREMENT:
3273 msg = "--";
3274 break;
3275 case NIC_NEW:
3276 msg = "new";
3277 break;
3278 case NIC_DEL:
3279 msg = "delete";
3280 break;
3281 default:
3282 gcc_unreachable ();
3283 }
3284 if (msg)
3285 error ("%qs cannot appear in a constant-expression", msg);
3286 return true;
3287 }
3288 }
3289 return false;
3290 }
3291
3292 /* Emit a diagnostic for an invalid type name. This function commits
3293 to the current active tentative parse, if any. (Otherwise, the
3294 problematic construct might be encountered again later, resulting
3295 in duplicate error messages.) LOCATION is the location of ID. */
3296
3297 static void
3298 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3299 location_t location)
3300 {
3301 tree decl, ambiguous_decls;
3302 cp_parser_commit_to_tentative_parse (parser);
3303 /* Try to lookup the identifier. */
3304 decl = cp_parser_lookup_name (parser, id, none_type,
3305 /*is_template=*/false,
3306 /*is_namespace=*/false,
3307 /*check_dependency=*/true,
3308 &ambiguous_decls, location);
3309 if (ambiguous_decls)
3310 /* If the lookup was ambiguous, an error will already have
3311 been issued. */
3312 return;
3313 /* If the lookup found a template-name, it means that the user forgot
3314 to specify an argument list. Emit a useful error message. */
3315 if (DECL_TYPE_TEMPLATE_P (decl))
3316 {
3317 auto_diagnostic_group d;
3318 error_at (location,
3319 "invalid use of template-name %qE without an argument list",
3320 decl);
3321 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3322 inform (location, "class template argument deduction is only available "
3323 "with %<-std=c++17%> or %<-std=gnu++17%>");
3324 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3325 }
3326 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3327 error_at (location, "invalid use of destructor %qD as a type", id);
3328 else if (TREE_CODE (decl) == TYPE_DECL)
3329 /* Something like 'unsigned A a;' */
3330 error_at (location, "invalid combination of multiple type-specifiers");
3331 else if (!parser->scope)
3332 {
3333 /* Issue an error message. */
3334 auto_diagnostic_group d;
3335 name_hint hint;
3336 if (TREE_CODE (id) == IDENTIFIER_NODE)
3337 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3338 if (const char *suggestion = hint.suggestion ())
3339 {
3340 gcc_rich_location richloc (location);
3341 richloc.add_fixit_replace (suggestion);
3342 error_at (&richloc,
3343 "%qE does not name a type; did you mean %qs?",
3344 id, suggestion);
3345 }
3346 else
3347 error_at (location, "%qE does not name a type", id);
3348 /* If we're in a template class, it's possible that the user was
3349 referring to a type from a base class. For example:
3350
3351 template <typename T> struct A { typedef T X; };
3352 template <typename T> struct B : public A<T> { X x; };
3353
3354 The user should have said "typename A<T>::X". */
3355 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3356 inform (location, "C++11 %<constexpr%> only available with "
3357 "%<-std=c++11%> or %<-std=gnu++11%>");
3358 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3359 inform (location, "C++11 %<noexcept%> only available with "
3360 "%<-std=c++11%> or %<-std=gnu++11%>");
3361 else if (cxx_dialect < cxx11
3362 && TREE_CODE (id) == IDENTIFIER_NODE
3363 && id_equal (id, "thread_local"))
3364 inform (location, "C++11 %<thread_local%> only available with "
3365 "%<-std=c++11%> or %<-std=gnu++11%>");
3366 else if (cxx_dialect < cxx2a && id == ridpointers[(int)RID_CONSTINIT])
3367 inform (location, "C++20 %<constinit%> only available with "
3368 "%<-std=c++2a%> or %<-std=gnu++2a%>");
3369 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3370 inform (location, "%<concept%> only available with %<-fconcepts%>");
3371 else if (processing_template_decl && current_class_type
3372 && TYPE_BINFO (current_class_type))
3373 {
3374 tree b;
3375
3376 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3377 b;
3378 b = TREE_CHAIN (b))
3379 {
3380 tree base_type = BINFO_TYPE (b);
3381 if (CLASS_TYPE_P (base_type)
3382 && dependent_type_p (base_type))
3383 {
3384 tree field;
3385 /* Go from a particular instantiation of the
3386 template (which will have an empty TYPE_FIELDs),
3387 to the main version. */
3388 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3389 for (field = TYPE_FIELDS (base_type);
3390 field;
3391 field = DECL_CHAIN (field))
3392 if (TREE_CODE (field) == TYPE_DECL
3393 && DECL_NAME (field) == id)
3394 {
3395 inform (location,
3396 "(perhaps %<typename %T::%E%> was intended)",
3397 BINFO_TYPE (b), id);
3398 break;
3399 }
3400 if (field)
3401 break;
3402 }
3403 }
3404 }
3405 }
3406 /* Here we diagnose qualified-ids where the scope is actually correct,
3407 but the identifier does not resolve to a valid type name. */
3408 else if (parser->scope != error_mark_node)
3409 {
3410 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3411 {
3412 auto_diagnostic_group d;
3413 name_hint hint;
3414 if (decl == error_mark_node)
3415 hint = suggest_alternative_in_explicit_scope (location, id,
3416 parser->scope);
3417 const char *suggestion = hint.suggestion ();
3418 gcc_rich_location richloc (location_of (id));
3419 if (suggestion)
3420 richloc.add_fixit_replace (suggestion);
3421 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3422 {
3423 if (suggestion)
3424 error_at (&richloc,
3425 "%qE in namespace %qE does not name a template"
3426 " type; did you mean %qs?",
3427 id, parser->scope, suggestion);
3428 else
3429 error_at (&richloc,
3430 "%qE in namespace %qE does not name a template type",
3431 id, parser->scope);
3432 }
3433 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3434 {
3435 if (suggestion)
3436 error_at (&richloc,
3437 "%qE in namespace %qE does not name a template"
3438 " type; did you mean %qs?",
3439 TREE_OPERAND (id, 0), parser->scope, suggestion);
3440 else
3441 error_at (&richloc,
3442 "%qE in namespace %qE does not name a template"
3443 " type",
3444 TREE_OPERAND (id, 0), parser->scope);
3445 }
3446 else
3447 {
3448 if (suggestion)
3449 error_at (&richloc,
3450 "%qE in namespace %qE does not name a type"
3451 "; did you mean %qs?",
3452 id, parser->scope, suggestion);
3453 else
3454 error_at (&richloc,
3455 "%qE in namespace %qE does not name a type",
3456 id, parser->scope);
3457 }
3458 if (DECL_P (decl))
3459 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3460 }
3461 else if (CLASS_TYPE_P (parser->scope)
3462 && constructor_name_p (id, parser->scope))
3463 {
3464 /* A<T>::A<T>() */
3465 auto_diagnostic_group d;
3466 error_at (location, "%<%T::%E%> names the constructor, not"
3467 " the type", parser->scope, id);
3468 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3469 error_at (location, "and %qT has no template constructors",
3470 parser->scope);
3471 }
3472 else if (TYPE_P (parser->scope)
3473 && dependent_scope_p (parser->scope))
3474 {
3475 gcc_rich_location richloc (location);
3476 richloc.add_fixit_insert_before ("typename ");
3477 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3478 error_at (&richloc,
3479 "need %<typename%> before %<%T::%D::%E%> because "
3480 "%<%T::%D%> is a dependent scope",
3481 TYPE_CONTEXT (parser->scope),
3482 TYPENAME_TYPE_FULLNAME (parser->scope),
3483 id,
3484 TYPE_CONTEXT (parser->scope),
3485 TYPENAME_TYPE_FULLNAME (parser->scope));
3486 else
3487 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3488 "%qT is a dependent scope",
3489 parser->scope, id, parser->scope);
3490 }
3491 else if (TYPE_P (parser->scope))
3492 {
3493 auto_diagnostic_group d;
3494 if (!COMPLETE_TYPE_P (parser->scope))
3495 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3496 parser->scope);
3497 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3498 error_at (location_of (id),
3499 "%qE in %q#T does not name a template type",
3500 id, parser->scope);
3501 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3502 error_at (location_of (id),
3503 "%qE in %q#T does not name a template type",
3504 TREE_OPERAND (id, 0), parser->scope);
3505 else
3506 error_at (location_of (id),
3507 "%qE in %q#T does not name a type",
3508 id, parser->scope);
3509 if (DECL_P (decl))
3510 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3511 }
3512 else
3513 gcc_unreachable ();
3514 }
3515 }
3516
3517 /* Check for a common situation where a type-name should be present,
3518 but is not, and issue a sensible error message. Returns true if an
3519 invalid type-name was detected.
3520
3521 The situation handled by this function are variable declarations of the
3522 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3523 Usually, `ID' should name a type, but if we got here it means that it
3524 does not. We try to emit the best possible error message depending on
3525 how exactly the id-expression looks like. */
3526
3527 static bool
3528 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3529 {
3530 tree id;
3531 cp_token *token = cp_lexer_peek_token (parser->lexer);
3532
3533 /* Avoid duplicate error about ambiguous lookup. */
3534 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3535 {
3536 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3537 if (next->type == CPP_NAME && next->error_reported)
3538 goto out;
3539 }
3540
3541 cp_parser_parse_tentatively (parser);
3542 id = cp_parser_id_expression (parser,
3543 /*template_keyword_p=*/false,
3544 /*check_dependency_p=*/true,
3545 /*template_p=*/NULL,
3546 /*declarator_p=*/false,
3547 /*optional_p=*/false);
3548 /* If the next token is a (, this is a function with no explicit return
3549 type, i.e. constructor, destructor or conversion op. */
3550 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3551 || TREE_CODE (id) == TYPE_DECL)
3552 {
3553 cp_parser_abort_tentative_parse (parser);
3554 return false;
3555 }
3556 if (!cp_parser_parse_definitely (parser))
3557 return false;
3558
3559 /* Emit a diagnostic for the invalid type. */
3560 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3561 out:
3562 /* If we aren't in the middle of a declarator (i.e. in a
3563 parameter-declaration-clause), skip to the end of the declaration;
3564 there's no point in trying to process it. */
3565 if (!parser->in_declarator_p)
3566 cp_parser_skip_to_end_of_block_or_statement (parser);
3567 return true;
3568 }
3569
3570 /* Consume tokens up to, and including, the next non-nested closing `)'.
3571 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3572 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3573 found an unnested token of that type. */
3574
3575 static int
3576 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3577 bool recovering,
3578 cpp_ttype or_ttype,
3579 bool consume_paren)
3580 {
3581 unsigned paren_depth = 0;
3582 unsigned brace_depth = 0;
3583 unsigned square_depth = 0;
3584 unsigned condop_depth = 0;
3585
3586 if (recovering && or_ttype == CPP_EOF
3587 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3588 return 0;
3589
3590 while (true)
3591 {
3592 cp_token * token = cp_lexer_peek_token (parser->lexer);
3593
3594 /* Have we found what we're looking for before the closing paren? */
3595 if (token->type == or_ttype && or_ttype != CPP_EOF
3596 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3597 return -1;
3598
3599 switch (token->type)
3600 {
3601 case CPP_PRAGMA_EOL:
3602 if (!parser->lexer->in_pragma)
3603 break;
3604 /* FALLTHRU */
3605 case CPP_EOF:
3606 /* If we've run out of tokens, then there is no closing `)'. */
3607 return 0;
3608
3609 /* This is good for lambda expression capture-lists. */
3610 case CPP_OPEN_SQUARE:
3611 ++square_depth;
3612 break;
3613 case CPP_CLOSE_SQUARE:
3614 if (!square_depth--)
3615 return 0;
3616 break;
3617
3618 case CPP_SEMICOLON:
3619 /* This matches the processing in skip_to_end_of_statement. */
3620 if (!brace_depth)
3621 return 0;
3622 break;
3623
3624 case CPP_OPEN_BRACE:
3625 ++brace_depth;
3626 break;
3627 case CPP_CLOSE_BRACE:
3628 if (!brace_depth--)
3629 return 0;
3630 break;
3631
3632 case CPP_OPEN_PAREN:
3633 if (!brace_depth)
3634 ++paren_depth;
3635 break;
3636
3637 case CPP_CLOSE_PAREN:
3638 if (!brace_depth && !paren_depth--)
3639 {
3640 if (consume_paren)
3641 cp_lexer_consume_token (parser->lexer);
3642 return 1;
3643 }
3644 break;
3645
3646 case CPP_QUERY:
3647 if (!brace_depth && !paren_depth && !square_depth)
3648 ++condop_depth;
3649 break;
3650
3651 case CPP_COLON:
3652 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3653 condop_depth--;
3654 break;
3655
3656 default:
3657 break;
3658 }
3659
3660 /* Consume the token. */
3661 cp_lexer_consume_token (parser->lexer);
3662 }
3663 }
3664
3665 /* Consume tokens up to, and including, the next non-nested closing `)'.
3666 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3667 are doing error recovery. Returns -1 if OR_COMMA is true and we
3668 found an unnested token of that type. */
3669
3670 static int
3671 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3672 bool recovering,
3673 bool or_comma,
3674 bool consume_paren)
3675 {
3676 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3677 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3678 ttype, consume_paren);
3679 }
3680
3681 /* Consume tokens until we reach the end of the current statement.
3682 Normally, that will be just before consuming a `;'. However, if a
3683 non-nested `}' comes first, then we stop before consuming that. */
3684
3685 static void
3686 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3687 {
3688 unsigned nesting_depth = 0;
3689
3690 /* Unwind generic function template scope if necessary. */
3691 if (parser->fully_implicit_function_template_p)
3692 abort_fully_implicit_template (parser);
3693
3694 while (true)
3695 {
3696 cp_token *token = cp_lexer_peek_token (parser->lexer);
3697
3698 switch (token->type)
3699 {
3700 case CPP_PRAGMA_EOL:
3701 if (!parser->lexer->in_pragma)
3702 break;
3703 /* FALLTHRU */
3704 case CPP_EOF:
3705 /* If we've run out of tokens, stop. */
3706 return;
3707
3708 case CPP_SEMICOLON:
3709 /* If the next token is a `;', we have reached the end of the
3710 statement. */
3711 if (!nesting_depth)
3712 return;
3713 break;
3714
3715 case CPP_CLOSE_BRACE:
3716 /* If this is a non-nested '}', stop before consuming it.
3717 That way, when confronted with something like:
3718
3719 { 3 + }
3720
3721 we stop before consuming the closing '}', even though we
3722 have not yet reached a `;'. */
3723 if (nesting_depth == 0)
3724 return;
3725
3726 /* If it is the closing '}' for a block that we have
3727 scanned, stop -- but only after consuming the token.
3728 That way given:
3729
3730 void f g () { ... }
3731 typedef int I;
3732
3733 we will stop after the body of the erroneously declared
3734 function, but before consuming the following `typedef'
3735 declaration. */
3736 if (--nesting_depth == 0)
3737 {
3738 cp_lexer_consume_token (parser->lexer);
3739 return;
3740 }
3741 break;
3742
3743 case CPP_OPEN_BRACE:
3744 ++nesting_depth;
3745 break;
3746
3747 default:
3748 break;
3749 }
3750
3751 /* Consume the token. */
3752 cp_lexer_consume_token (parser->lexer);
3753 }
3754 }
3755
3756 /* This function is called at the end of a statement or declaration.
3757 If the next token is a semicolon, it is consumed; otherwise, error
3758 recovery is attempted. */
3759
3760 static void
3761 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3762 {
3763 /* Look for the trailing `;'. */
3764 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3765 {
3766 /* If there is additional (erroneous) input, skip to the end of
3767 the statement. */
3768 cp_parser_skip_to_end_of_statement (parser);
3769 /* If the next token is now a `;', consume it. */
3770 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3771 cp_lexer_consume_token (parser->lexer);
3772 }
3773 }
3774
3775 /* Skip tokens until we have consumed an entire block, or until we
3776 have consumed a non-nested `;'. */
3777
3778 static void
3779 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3780 {
3781 int nesting_depth = 0;
3782
3783 /* Unwind generic function template scope if necessary. */
3784 if (parser->fully_implicit_function_template_p)
3785 abort_fully_implicit_template (parser);
3786
3787 while (nesting_depth >= 0)
3788 {
3789 cp_token *token = cp_lexer_peek_token (parser->lexer);
3790
3791 switch (token->type)
3792 {
3793 case CPP_PRAGMA_EOL:
3794 if (!parser->lexer->in_pragma)
3795 break;
3796 /* FALLTHRU */
3797 case CPP_EOF:
3798 /* If we've run out of tokens, stop. */
3799 return;
3800
3801 case CPP_SEMICOLON:
3802 /* Stop if this is an unnested ';'. */
3803 if (!nesting_depth)
3804 nesting_depth = -1;
3805 break;
3806
3807 case CPP_CLOSE_BRACE:
3808 /* Stop if this is an unnested '}', or closes the outermost
3809 nesting level. */
3810 nesting_depth--;
3811 if (nesting_depth < 0)
3812 return;
3813 if (!nesting_depth)
3814 nesting_depth = -1;
3815 break;
3816
3817 case CPP_OPEN_BRACE:
3818 /* Nest. */
3819 nesting_depth++;
3820 break;
3821
3822 default:
3823 break;
3824 }
3825
3826 /* Consume the token. */
3827 cp_lexer_consume_token (parser->lexer);
3828 }
3829 }
3830
3831 /* Skip tokens until a non-nested closing curly brace is the next
3832 token, or there are no more tokens. Return true in the first case,
3833 false otherwise. */
3834
3835 static bool
3836 cp_parser_skip_to_closing_brace (cp_parser *parser)
3837 {
3838 unsigned nesting_depth = 0;
3839
3840 while (true)
3841 {
3842 cp_token *token = cp_lexer_peek_token (parser->lexer);
3843
3844 switch (token->type)
3845 {
3846 case CPP_PRAGMA_EOL:
3847 if (!parser->lexer->in_pragma)
3848 break;
3849 /* FALLTHRU */
3850 case CPP_EOF:
3851 /* If we've run out of tokens, stop. */
3852 return false;
3853
3854 case CPP_CLOSE_BRACE:
3855 /* If the next token is a non-nested `}', then we have reached
3856 the end of the current block. */
3857 if (nesting_depth-- == 0)
3858 return true;
3859 break;
3860
3861 case CPP_OPEN_BRACE:
3862 /* If it the next token is a `{', then we are entering a new
3863 block. Consume the entire block. */
3864 ++nesting_depth;
3865 break;
3866
3867 default:
3868 break;
3869 }
3870
3871 /* Consume the token. */
3872 cp_lexer_consume_token (parser->lexer);
3873 }
3874 }
3875
3876 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3877 parameter is the PRAGMA token, allowing us to purge the entire pragma
3878 sequence. */
3879
3880 static void
3881 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3882 {
3883 cp_token *token;
3884
3885 parser->lexer->in_pragma = false;
3886
3887 do
3888 token = cp_lexer_consume_token (parser->lexer);
3889 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3890
3891 /* Ensure that the pragma is not parsed again. */
3892 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3893 }
3894
3895 /* Require pragma end of line, resyncing with it as necessary. The
3896 arguments are as for cp_parser_skip_to_pragma_eol. */
3897
3898 static void
3899 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3900 {
3901 parser->lexer->in_pragma = false;
3902 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3903 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3904 }
3905
3906 /* This is a simple wrapper around make_typename_type. When the id is
3907 an unresolved identifier node, we can provide a superior diagnostic
3908 using cp_parser_diagnose_invalid_type_name. */
3909
3910 static tree
3911 cp_parser_make_typename_type (cp_parser *parser, tree id,
3912 location_t id_location)
3913 {
3914 tree result;
3915 if (identifier_p (id))
3916 {
3917 result = make_typename_type (parser->scope, id, typename_type,
3918 /*complain=*/tf_none);
3919 if (result == error_mark_node)
3920 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3921 return result;
3922 }
3923 return make_typename_type (parser->scope, id, typename_type, tf_error);
3924 }
3925
3926 /* This is a wrapper around the
3927 make_{pointer,ptrmem,reference}_declarator functions that decides
3928 which one to call based on the CODE and CLASS_TYPE arguments. The
3929 CODE argument should be one of the values returned by
3930 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3931 appertain to the pointer or reference. */
3932
3933 static cp_declarator *
3934 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3935 cp_cv_quals cv_qualifiers,
3936 cp_declarator *target,
3937 tree attributes)
3938 {
3939 if (code == ERROR_MARK || target == cp_error_declarator)
3940 return cp_error_declarator;
3941
3942 if (code == INDIRECT_REF)
3943 if (class_type == NULL_TREE)
3944 return make_pointer_declarator (cv_qualifiers, target, attributes);
3945 else
3946 return make_ptrmem_declarator (cv_qualifiers, class_type,
3947 target, attributes);
3948 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3949 return make_reference_declarator (cv_qualifiers, target,
3950 false, attributes);
3951 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3952 return make_reference_declarator (cv_qualifiers, target,
3953 true, attributes);
3954 gcc_unreachable ();
3955 }
3956
3957 /* Create a new C++ parser. */
3958
3959 static cp_parser *
3960 cp_parser_new (void)
3961 {
3962 cp_parser *parser;
3963 cp_lexer *lexer;
3964 unsigned i;
3965
3966 /* cp_lexer_new_main is called before doing GC allocation because
3967 cp_lexer_new_main might load a PCH file. */
3968 lexer = cp_lexer_new_main ();
3969
3970 /* Initialize the binops_by_token so that we can get the tree
3971 directly from the token. */
3972 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3973 binops_by_token[binops[i].token_type] = binops[i];
3974
3975 parser = ggc_cleared_alloc<cp_parser> ();
3976 parser->lexer = lexer;
3977 parser->context = cp_parser_context_new (NULL);
3978
3979 /* For now, we always accept GNU extensions. */
3980 parser->allow_gnu_extensions_p = 1;
3981
3982 /* The `>' token is a greater-than operator, not the end of a
3983 template-id. */
3984 parser->greater_than_is_operator_p = true;
3985
3986 parser->default_arg_ok_p = true;
3987
3988 /* We are not parsing a constant-expression. */
3989 parser->integral_constant_expression_p = false;
3990 parser->allow_non_integral_constant_expression_p = false;
3991 parser->non_integral_constant_expression_p = false;
3992
3993 /* Local variable names are not forbidden. */
3994 parser->local_variables_forbidden_p = 0;
3995
3996 /* We are not processing an `extern "C"' declaration. */
3997 parser->in_unbraced_linkage_specification_p = false;
3998
3999 /* We are not processing a declarator. */
4000 parser->in_declarator_p = false;
4001
4002 /* We are not processing a template-argument-list. */
4003 parser->in_template_argument_list_p = false;
4004
4005 /* We are not in an iteration statement. */
4006 parser->in_statement = 0;
4007
4008 /* We are not in a switch statement. */
4009 parser->in_switch_statement_p = false;
4010
4011 /* We are not parsing a type-id inside an expression. */
4012 parser->in_type_id_in_expr_p = false;
4013
4014 /* String literals should be translated to the execution character set. */
4015 parser->translate_strings_p = true;
4016
4017 /* We are not parsing a function body. */
4018 parser->in_function_body = false;
4019
4020 /* We can correct until told otherwise. */
4021 parser->colon_corrects_to_scope_p = true;
4022
4023 /* The unparsed function queue is empty. */
4024 push_unparsed_function_queues (parser);
4025
4026 /* There are no classes being defined. */
4027 parser->num_classes_being_defined = 0;
4028
4029 /* No template parameters apply. */
4030 parser->num_template_parameter_lists = 0;
4031
4032 /* Special parsing data structures. */
4033 parser->omp_declare_simd = NULL;
4034 parser->oacc_routine = NULL;
4035
4036 /* Not declaring an implicit function template. */
4037 parser->auto_is_implicit_function_template_parm_p = false;
4038 parser->fully_implicit_function_template_p = false;
4039 parser->implicit_template_parms = 0;
4040 parser->implicit_template_scope = 0;
4041
4042 /* Allow constrained-type-specifiers. */
4043 parser->prevent_constrained_type_specifiers = 0;
4044
4045 /* We haven't yet seen an 'extern "C"'. */
4046 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4047
4048 return parser;
4049 }
4050
4051 /* Create a cp_lexer structure which will emit the tokens in CACHE
4052 and push it onto the parser's lexer stack. This is used for delayed
4053 parsing of in-class method bodies and default arguments, and should
4054 not be confused with tentative parsing. */
4055 static void
4056 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4057 {
4058 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4059 lexer->next = parser->lexer;
4060 parser->lexer = lexer;
4061
4062 /* Move the current source position to that of the first token in the
4063 new lexer. */
4064 cp_lexer_set_source_position_from_token (lexer->next_token);
4065 }
4066
4067 /* Pop the top lexer off the parser stack. This is never used for the
4068 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4069 static void
4070 cp_parser_pop_lexer (cp_parser *parser)
4071 {
4072 cp_lexer *lexer = parser->lexer;
4073 parser->lexer = lexer->next;
4074 cp_lexer_destroy (lexer);
4075
4076 /* Put the current source position back where it was before this
4077 lexer was pushed. */
4078 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4079 }
4080
4081 /* Lexical conventions [gram.lex] */
4082
4083 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4084 identifier. */
4085
4086 static cp_expr
4087 cp_parser_identifier (cp_parser* parser)
4088 {
4089 cp_token *token;
4090
4091 /* Look for the identifier. */
4092 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4093 /* Return the value. */
4094 if (token)
4095 return cp_expr (token->u.value, token->location);
4096 else
4097 return error_mark_node;
4098 }
4099
4100 /* Parse a sequence of adjacent string constants. Returns a
4101 TREE_STRING representing the combined, nul-terminated string
4102 constant. If TRANSLATE is true, translate the string to the
4103 execution character set. If WIDE_OK is true, a wide string is
4104 invalid here.
4105
4106 C++98 [lex.string] says that if a narrow string literal token is
4107 adjacent to a wide string literal token, the behavior is undefined.
4108 However, C99 6.4.5p4 says that this results in a wide string literal.
4109 We follow C99 here, for consistency with the C front end.
4110
4111 This code is largely lifted from lex_string() in c-lex.c.
4112
4113 FUTURE: ObjC++ will need to handle @-strings here. */
4114 static cp_expr
4115 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4116 bool lookup_udlit = true)
4117 {
4118 tree value;
4119 size_t count;
4120 struct obstack str_ob;
4121 struct obstack loc_ob;
4122 cpp_string str, istr, *strs;
4123 cp_token *tok;
4124 enum cpp_ttype type, curr_type;
4125 int have_suffix_p = 0;
4126 tree string_tree;
4127 tree suffix_id = NULL_TREE;
4128 bool curr_tok_is_userdef_p = false;
4129
4130 tok = cp_lexer_peek_token (parser->lexer);
4131 if (!cp_parser_is_string_literal (tok))
4132 {
4133 cp_parser_error (parser, "expected string-literal");
4134 return error_mark_node;
4135 }
4136
4137 location_t loc = tok->location;
4138
4139 if (cpp_userdef_string_p (tok->type))
4140 {
4141 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4142 curr_type = cpp_userdef_string_remove_type (tok->type);
4143 curr_tok_is_userdef_p = true;
4144 }
4145 else
4146 {
4147 string_tree = tok->u.value;
4148 curr_type = tok->type;
4149 }
4150 type = curr_type;
4151
4152 /* Try to avoid the overhead of creating and destroying an obstack
4153 for the common case of just one string. */
4154 if (!cp_parser_is_string_literal
4155 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4156 {
4157 cp_lexer_consume_token (parser->lexer);
4158
4159 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4160 str.len = TREE_STRING_LENGTH (string_tree);
4161 count = 1;
4162
4163 if (curr_tok_is_userdef_p)
4164 {
4165 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4166 have_suffix_p = 1;
4167 curr_type = cpp_userdef_string_remove_type (tok->type);
4168 }
4169 else
4170 curr_type = tok->type;
4171
4172 strs = &str;
4173 }
4174 else
4175 {
4176 location_t last_tok_loc = tok->location;
4177 gcc_obstack_init (&str_ob);
4178 gcc_obstack_init (&loc_ob);
4179 count = 0;
4180
4181 do
4182 {
4183 cp_lexer_consume_token (parser->lexer);
4184 count++;
4185 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4186 str.len = TREE_STRING_LENGTH (string_tree);
4187
4188 if (curr_tok_is_userdef_p)
4189 {
4190 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4191 if (have_suffix_p == 0)
4192 {
4193 suffix_id = curr_suffix_id;
4194 have_suffix_p = 1;
4195 }
4196 else if (have_suffix_p == 1
4197 && curr_suffix_id != suffix_id)
4198 {
4199 error ("inconsistent user-defined literal suffixes"
4200 " %qD and %qD in string literal",
4201 suffix_id, curr_suffix_id);
4202 have_suffix_p = -1;
4203 }
4204 curr_type = cpp_userdef_string_remove_type (tok->type);
4205 }
4206 else
4207 curr_type = tok->type;
4208
4209 if (type != curr_type)
4210 {
4211 if (type == CPP_STRING)
4212 type = curr_type;
4213 else if (curr_type != CPP_STRING)
4214 {
4215 rich_location rich_loc (line_table, tok->location);
4216 rich_loc.add_range (last_tok_loc);
4217 error_at (&rich_loc,
4218 "unsupported non-standard concatenation "
4219 "of string literals");
4220 }
4221 }
4222
4223 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4224 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4225
4226 last_tok_loc = tok->location;
4227
4228 tok = cp_lexer_peek_token (parser->lexer);
4229 if (cpp_userdef_string_p (tok->type))
4230 {
4231 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4232 curr_type = cpp_userdef_string_remove_type (tok->type);
4233 curr_tok_is_userdef_p = true;
4234 }
4235 else
4236 {
4237 string_tree = tok->u.value;
4238 curr_type = tok->type;
4239 curr_tok_is_userdef_p = false;
4240 }
4241 }
4242 while (cp_parser_is_string_literal (tok));
4243
4244 /* A string literal built by concatenation has its caret=start at
4245 the start of the initial string, and its finish at the finish of
4246 the final string literal. */
4247 loc = make_location (loc, loc, get_finish (last_tok_loc));
4248
4249 strs = (cpp_string *) obstack_finish (&str_ob);
4250 }
4251
4252 if (type != CPP_STRING && !wide_ok)
4253 {
4254 cp_parser_error (parser, "a wide string is invalid in this context");
4255 type = CPP_STRING;
4256 }
4257
4258 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4259 (parse_in, strs, count, &istr, type))
4260 {
4261 value = build_string (istr.len, (const char *)istr.text);
4262 free (CONST_CAST (unsigned char *, istr.text));
4263 if (count > 1)
4264 {
4265 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4266 gcc_assert (g_string_concat_db);
4267 g_string_concat_db->record_string_concatenation (count, locs);
4268 }
4269
4270 switch (type)
4271 {
4272 default:
4273 case CPP_STRING:
4274 TREE_TYPE (value) = char_array_type_node;
4275 break;
4276 case CPP_UTF8STRING:
4277 if (flag_char8_t)
4278 TREE_TYPE (value) = char8_array_type_node;
4279 else
4280 TREE_TYPE (value) = char_array_type_node;
4281 break;
4282 case CPP_STRING16:
4283 TREE_TYPE (value) = char16_array_type_node;
4284 break;
4285 case CPP_STRING32:
4286 TREE_TYPE (value) = char32_array_type_node;
4287 break;
4288 case CPP_WSTRING:
4289 TREE_TYPE (value) = wchar_array_type_node;
4290 break;
4291 }
4292
4293 value = fix_string_type (value);
4294
4295 if (have_suffix_p)
4296 {
4297 tree literal = build_userdef_literal (suffix_id, value,
4298 OT_NONE, NULL_TREE);
4299 if (lookup_udlit)
4300 value = cp_parser_userdef_string_literal (literal);
4301 else
4302 value = literal;
4303 }
4304 }
4305 else
4306 /* cpp_interpret_string has issued an error. */
4307 value = error_mark_node;
4308
4309 if (count > 1)
4310 {
4311 obstack_free (&str_ob, 0);
4312 obstack_free (&loc_ob, 0);
4313 }
4314
4315 return cp_expr (value, loc);
4316 }
4317
4318 /* Look up a literal operator with the name and the exact arguments. */
4319
4320 static tree
4321 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4322 {
4323 tree decl = lookup_name (name);
4324 if (!decl || !is_overloaded_fn (decl))
4325 return error_mark_node;
4326
4327 for (lkp_iterator iter (decl); iter; ++iter)
4328 {
4329 tree fn = *iter;
4330
4331 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4332 {
4333 unsigned int ix;
4334 bool found = true;
4335
4336 for (ix = 0;
4337 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4338 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4339 {
4340 tree tparm = TREE_VALUE (parmtypes);
4341 tree targ = TREE_TYPE ((*args)[ix]);
4342 bool ptr = TYPE_PTR_P (tparm);
4343 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4344 if ((ptr || arr || !same_type_p (tparm, targ))
4345 && (!ptr || !arr
4346 || !same_type_p (TREE_TYPE (tparm),
4347 TREE_TYPE (targ))))
4348 found = false;
4349 }
4350
4351 if (found
4352 && ix == vec_safe_length (args)
4353 /* May be this should be sufficient_parms_p instead,
4354 depending on how exactly should user-defined literals
4355 work in presence of default arguments on the literal
4356 operator parameters. */
4357 && parmtypes == void_list_node)
4358 return decl;
4359 }
4360 }
4361
4362 return error_mark_node;
4363 }
4364
4365 /* Parse a user-defined char constant. Returns a call to a user-defined
4366 literal operator taking the character as an argument. */
4367
4368 static cp_expr
4369 cp_parser_userdef_char_literal (cp_parser *parser)
4370 {
4371 cp_token *token = cp_lexer_consume_token (parser->lexer);
4372 tree literal = token->u.value;
4373 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4374 tree value = USERDEF_LITERAL_VALUE (literal);
4375 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4376 tree decl, result;
4377
4378 /* Build up a call to the user-defined operator */
4379 /* Lookup the name we got back from the id-expression. */
4380 releasing_vec args;
4381 vec_safe_push (args, value);
4382 decl = lookup_literal_operator (name, args);
4383 if (!decl || decl == error_mark_node)
4384 {
4385 error ("unable to find character literal operator %qD with %qT argument",
4386 name, TREE_TYPE (value));
4387 return error_mark_node;
4388 }
4389 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4390 return result;
4391 }
4392
4393 /* A subroutine of cp_parser_userdef_numeric_literal to
4394 create a char... template parameter pack from a string node. */
4395
4396 static tree
4397 make_char_string_pack (tree value)
4398 {
4399 tree charvec;
4400 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4401 const char *str = TREE_STRING_POINTER (value);
4402 int i, len = TREE_STRING_LENGTH (value) - 1;
4403 tree argvec = make_tree_vec (1);
4404
4405 /* Fill in CHARVEC with all of the parameters. */
4406 charvec = make_tree_vec (len);
4407 for (i = 0; i < len; ++i)
4408 {
4409 unsigned char s[3] = { '\'', str[i], '\'' };
4410 cpp_string in = { 3, s };
4411 cpp_string out = { 0, 0 };
4412 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4413 return NULL_TREE;
4414 gcc_assert (out.len == 2);
4415 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4416 out.text[0]);
4417 }
4418
4419 /* Build the argument packs. */
4420 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4421
4422 TREE_VEC_ELT (argvec, 0) = argpack;
4423
4424 return argvec;
4425 }
4426
4427 /* A subroutine of cp_parser_userdef_numeric_literal to
4428 create a char... template parameter pack from a string node. */
4429
4430 static tree
4431 make_string_pack (tree value)
4432 {
4433 tree charvec;
4434 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4435 const unsigned char *str
4436 = (const unsigned char *) TREE_STRING_POINTER (value);
4437 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4438 int len = TREE_STRING_LENGTH (value) / sz - 1;
4439 tree argvec = make_tree_vec (2);
4440
4441 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4442 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4443
4444 /* First template parm is character type. */
4445 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4446
4447 /* Fill in CHARVEC with all of the parameters. */
4448 charvec = make_tree_vec (len);
4449 for (int i = 0; i < len; ++i)
4450 TREE_VEC_ELT (charvec, i)
4451 = double_int_to_tree (str_char_type_node,
4452 double_int::from_buffer (str + i * sz, sz));
4453
4454 /* Build the argument packs. */
4455 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4456
4457 TREE_VEC_ELT (argvec, 1) = argpack;
4458
4459 return argvec;
4460 }
4461
4462 /* Parse a user-defined numeric constant. returns a call to a user-defined
4463 literal operator. */
4464
4465 static cp_expr
4466 cp_parser_userdef_numeric_literal (cp_parser *parser)
4467 {
4468 cp_token *token = cp_lexer_consume_token (parser->lexer);
4469 tree literal = token->u.value;
4470 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4471 tree value = USERDEF_LITERAL_VALUE (literal);
4472 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4473 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4474 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4475 tree decl, result;
4476
4477 /* Look for a literal operator taking the exact type of numeric argument
4478 as the literal value. */
4479 releasing_vec args;
4480 vec_safe_push (args, value);
4481 decl = lookup_literal_operator (name, args);
4482 if (decl && decl != error_mark_node)
4483 {
4484 result = finish_call_expr (decl, &args, false, true,
4485 tf_warning_or_error);
4486
4487 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4488 {
4489 warning_at (token->location, OPT_Woverflow,
4490 "integer literal exceeds range of %qT type",
4491 long_long_unsigned_type_node);
4492 }
4493 else
4494 {
4495 if (overflow > 0)
4496 warning_at (token->location, OPT_Woverflow,
4497 "floating literal exceeds range of %qT type",
4498 long_double_type_node);
4499 else if (overflow < 0)
4500 warning_at (token->location, OPT_Woverflow,
4501 "floating literal truncated to zero");
4502 }
4503
4504 return result;
4505 }
4506
4507 /* If the numeric argument didn't work, look for a raw literal
4508 operator taking a const char* argument consisting of the number
4509 in string format. */
4510 args->truncate (0);
4511 vec_safe_push (args, num_string);
4512 decl = lookup_literal_operator (name, args);
4513 if (decl && decl != error_mark_node)
4514 {
4515 result = finish_call_expr (decl, &args, false, true,
4516 tf_warning_or_error);
4517 return result;
4518 }
4519
4520 /* If the raw literal didn't work, look for a non-type template
4521 function with parameter pack char.... Call the function with
4522 template parameter characters representing the number. */
4523 args->truncate (0);
4524 decl = lookup_literal_operator (name, args);
4525 if (decl && decl != error_mark_node)
4526 {
4527 tree tmpl_args = make_char_string_pack (num_string);
4528 if (tmpl_args == NULL_TREE)
4529 {
4530 error ("failed to translate literal to execution character set %qT",
4531 num_string);
4532 return error_mark_node;
4533 }
4534 decl = lookup_template_function (decl, tmpl_args);
4535 result = finish_call_expr (decl, &args, false, true,
4536 tf_warning_or_error);
4537 return result;
4538 }
4539
4540 /* In C++14 the standard library defines complex number suffixes that
4541 conflict with GNU extensions. Prefer them if <complex> is #included. */
4542 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4543 bool i14 = (cxx_dialect > cxx11
4544 && (id_equal (suffix_id, "i")
4545 || id_equal (suffix_id, "if")
4546 || id_equal (suffix_id, "il")));
4547 diagnostic_t kind = DK_ERROR;
4548 int opt = 0;
4549
4550 if (i14 && ext)
4551 {
4552 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
4553 0, false);
4554 if (cxlit == error_mark_node)
4555 {
4556 /* No <complex>, so pedwarn and use GNU semantics. */
4557 kind = DK_PEDWARN;
4558 opt = OPT_Wpedantic;
4559 }
4560 }
4561
4562 bool complained
4563 = emit_diagnostic (kind, input_location, opt,
4564 "unable to find numeric literal operator %qD", name);
4565
4566 if (!complained)
4567 /* Don't inform either. */;
4568 else if (i14)
4569 {
4570 inform (token->location, "add %<using namespace std::complex_literals%> "
4571 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4572 "suffixes");
4573 if (ext)
4574 inform (token->location, "or use %<j%> instead of %<i%> for the "
4575 "GNU built-in suffix");
4576 }
4577 else if (!ext)
4578 inform (token->location, "use %<-fext-numeric-literals%> "
4579 "to enable more built-in suffixes");
4580
4581 if (kind == DK_ERROR)
4582 value = error_mark_node;
4583 else
4584 {
4585 /* Use the built-in semantics. */
4586 tree type;
4587 if (id_equal (suffix_id, "i"))
4588 {
4589 if (TREE_CODE (value) == INTEGER_CST)
4590 type = integer_type_node;
4591 else
4592 type = double_type_node;
4593 }
4594 else if (id_equal (suffix_id, "if"))
4595 type = float_type_node;
4596 else /* if (id_equal (suffix_id, "il")) */
4597 type = long_double_type_node;
4598
4599 value = build_complex (build_complex_type (type),
4600 fold_convert (type, integer_zero_node),
4601 fold_convert (type, value));
4602 }
4603
4604 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4605 /* Avoid repeated diagnostics. */
4606 token->u.value = value;
4607 return value;
4608 }
4609
4610 /* Parse a user-defined string constant. Returns a call to a user-defined
4611 literal operator taking a character pointer and the length of the string
4612 as arguments. */
4613
4614 static tree
4615 cp_parser_userdef_string_literal (tree literal)
4616 {
4617 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4618 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4619 tree value = USERDEF_LITERAL_VALUE (literal);
4620 int len = TREE_STRING_LENGTH (value)
4621 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4622 tree decl;
4623
4624 /* Build up a call to the user-defined operator. */
4625 /* Lookup the name we got back from the id-expression. */
4626 releasing_vec args;
4627 vec_safe_push (args, value);
4628 vec_safe_push (args, build_int_cst (size_type_node, len));
4629 decl = lookup_literal_operator (name, args);
4630
4631 if (decl && decl != error_mark_node)
4632 return finish_call_expr (decl, &args, false, true,
4633 tf_warning_or_error);
4634
4635 /* Look for a suitable template function, either (C++20) with a single
4636 parameter of class type, or (N3599) with typename parameter CharT and
4637 parameter pack CharT... */
4638 args->truncate (0);
4639 decl = lookup_literal_operator (name, args);
4640 if (decl && decl != error_mark_node)
4641 {
4642 /* Use resolve_nondeduced_context to try to choose one form of template
4643 or the other. */
4644 tree tmpl_args = make_tree_vec (1);
4645 TREE_VEC_ELT (tmpl_args, 0) = value;
4646 decl = lookup_template_function (decl, tmpl_args);
4647 tree res = resolve_nondeduced_context (decl, tf_none);
4648 if (DECL_P (res))
4649 decl = res;
4650 else
4651 {
4652 TREE_OPERAND (decl, 1) = make_string_pack (value);
4653 res = resolve_nondeduced_context (decl, tf_none);
4654 if (DECL_P (res))
4655 decl = res;
4656 }
4657 if (!DECL_P (decl) && cxx_dialect > cxx17)
4658 TREE_OPERAND (decl, 1) = tmpl_args;
4659 return finish_call_expr (decl, &args, false, true,
4660 tf_warning_or_error);
4661 }
4662
4663 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4664 name, TREE_TYPE (value), size_type_node);
4665 return error_mark_node;
4666 }
4667
4668
4669 /* Basic concepts [gram.basic] */
4670
4671 /* Parse a translation-unit.
4672
4673 translation-unit:
4674 declaration-seq [opt] */
4675
4676 static void
4677 cp_parser_translation_unit (cp_parser* parser)
4678 {
4679 gcc_checking_assert (!cp_error_declarator);
4680
4681 /* Create the declarator obstack. */
4682 gcc_obstack_init (&declarator_obstack);
4683 /* Create the error declarator. */
4684 cp_error_declarator = make_declarator (cdk_error);
4685 /* Create the empty parameter list. */
4686 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4687 UNKNOWN_LOCATION);
4688 /* Remember where the base of the declarator obstack lies. */
4689 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4690
4691 bool implicit_extern_c = false;
4692
4693 for (;;)
4694 {
4695 cp_token *token = cp_lexer_peek_token (parser->lexer);
4696
4697 /* If we're entering or exiting a region that's implicitly
4698 extern "C", modify the lang context appropriately. */
4699 if (implicit_extern_c
4700 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4701 {
4702 implicit_extern_c = !implicit_extern_c;
4703 if (implicit_extern_c)
4704 push_lang_context (lang_name_c);
4705 else
4706 pop_lang_context ();
4707 }
4708
4709 if (token->type == CPP_EOF)
4710 break;
4711
4712 if (token->type == CPP_CLOSE_BRACE)
4713 {
4714 cp_parser_error (parser, "expected declaration");
4715 cp_lexer_consume_token (parser->lexer);
4716 /* If the next token is now a `;', consume it. */
4717 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4718 cp_lexer_consume_token (parser->lexer);
4719 }
4720 else
4721 cp_parser_toplevel_declaration (parser);
4722 }
4723
4724 /* Get rid of the token array; we don't need it any more. */
4725 cp_lexer_destroy (parser->lexer);
4726 parser->lexer = NULL;
4727
4728 /* The EOF should have reset this. */
4729 gcc_checking_assert (!implicit_extern_c);
4730
4731 /* Make sure the declarator obstack was fully cleaned up. */
4732 gcc_assert (obstack_next_free (&declarator_obstack)
4733 == declarator_obstack_base);
4734 }
4735
4736 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4737 decltype context. */
4738
4739 static inline tsubst_flags_t
4740 complain_flags (bool decltype_p)
4741 {
4742 tsubst_flags_t complain = tf_warning_or_error;
4743 if (decltype_p)
4744 complain |= tf_decltype;
4745 return complain;
4746 }
4747
4748 /* We're about to parse a collection of statements. If we're currently
4749 parsing tentatively, set up a firewall so that any nested
4750 cp_parser_commit_to_tentative_parse won't affect the current context. */
4751
4752 static cp_token_position
4753 cp_parser_start_tentative_firewall (cp_parser *parser)
4754 {
4755 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4756 return 0;
4757
4758 cp_parser_parse_tentatively (parser);
4759 cp_parser_commit_to_topmost_tentative_parse (parser);
4760 return cp_lexer_token_position (parser->lexer, false);
4761 }
4762
4763 /* We've finished parsing the collection of statements. Wrap up the
4764 firewall and replace the relevant tokens with the parsed form. */
4765
4766 static void
4767 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4768 tree expr)
4769 {
4770 if (!start)
4771 return;
4772
4773 /* Finish the firewall level. */
4774 cp_parser_parse_definitely (parser);
4775 /* And remember the result of the parse for when we try again. */
4776 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4777 token->type = CPP_PREPARSED_EXPR;
4778 token->u.value = expr;
4779 token->keyword = RID_MAX;
4780 cp_lexer_purge_tokens_after (parser->lexer, start);
4781 }
4782
4783 /* Like the above functions, but let the user modify the tokens. Used by
4784 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4785 later parses, so it makes sense to localize the effects of
4786 cp_parser_commit_to_tentative_parse. */
4787
4788 struct tentative_firewall
4789 {
4790 cp_parser *parser;
4791 bool set;
4792
4793 tentative_firewall (cp_parser *p): parser(p)
4794 {
4795 /* If we're currently parsing tentatively, start a committed level as a
4796 firewall and then an inner tentative parse. */
4797 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4798 {
4799 cp_parser_parse_tentatively (parser);
4800 cp_parser_commit_to_topmost_tentative_parse (parser);
4801 cp_parser_parse_tentatively (parser);
4802 }
4803 }
4804
4805 ~tentative_firewall()
4806 {
4807 if (set)
4808 {
4809 /* Finish the inner tentative parse and the firewall, propagating any
4810 uncommitted error state to the outer tentative parse. */
4811 bool err = cp_parser_error_occurred (parser);
4812 cp_parser_parse_definitely (parser);
4813 cp_parser_parse_definitely (parser);
4814 if (err)
4815 cp_parser_simulate_error (parser);
4816 }
4817 }
4818 };
4819
4820 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4821 This class is for tracking such a matching pair of symbols.
4822 In particular, it tracks the location of the first token,
4823 so that if the second token is missing, we can highlight the
4824 location of the first token when notifying the user about the
4825 problem. */
4826
4827 template <typename traits_t>
4828 class token_pair
4829 {
4830 public:
4831 /* token_pair's ctor. */
4832 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4833
4834 /* If the next token is the opening symbol for this pair, consume it and
4835 return true.
4836 Otherwise, issue an error and return false.
4837 In either case, record the location of the opening token. */
4838
4839 bool require_open (cp_parser *parser)
4840 {
4841 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4842 return cp_parser_require (parser, traits_t::open_token_type,
4843 traits_t::required_token_open);
4844 }
4845
4846 /* Consume the next token from PARSER, recording its location as
4847 that of the opening token within the pair. */
4848
4849 cp_token * consume_open (cp_parser *parser)
4850 {
4851 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4852 gcc_assert (tok->type == traits_t::open_token_type);
4853 m_open_loc = tok->location;
4854 return tok;
4855 }
4856
4857 /* If the next token is the closing symbol for this pair, consume it
4858 and return it.
4859 Otherwise, issue an error, highlighting the location of the
4860 corresponding opening token, and return NULL. */
4861
4862 cp_token *require_close (cp_parser *parser) const
4863 {
4864 return cp_parser_require (parser, traits_t::close_token_type,
4865 traits_t::required_token_close,
4866 m_open_loc);
4867 }
4868
4869 location_t open_location () const { return m_open_loc; }
4870
4871 private:
4872 location_t m_open_loc;
4873 };
4874
4875 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4876
4877 struct matching_paren_traits
4878 {
4879 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4880 static const enum required_token required_token_open = RT_OPEN_PAREN;
4881 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4882 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4883 };
4884
4885 /* "matching_parens" is a token_pair<T> class for tracking matching
4886 pairs of parentheses. */
4887
4888 typedef token_pair<matching_paren_traits> matching_parens;
4889
4890 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4891
4892 struct matching_brace_traits
4893 {
4894 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4895 static const enum required_token required_token_open = RT_OPEN_BRACE;
4896 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4897 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4898 };
4899
4900 /* "matching_braces" is a token_pair<T> class for tracking matching
4901 pairs of braces. */
4902
4903 typedef token_pair<matching_brace_traits> matching_braces;
4904
4905
4906 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4907 enclosing parentheses. */
4908
4909 static cp_expr
4910 cp_parser_statement_expr (cp_parser *parser)
4911 {
4912 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4913
4914 /* Consume the '('. */
4915 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4916 matching_parens parens;
4917 parens.consume_open (parser);
4918 /* Start the statement-expression. */
4919 tree expr = begin_stmt_expr ();
4920 /* Parse the compound-statement. */
4921 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4922 /* Finish up. */
4923 expr = finish_stmt_expr (expr, false);
4924 /* Consume the ')'. */
4925 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4926 if (!parens.require_close (parser))
4927 cp_parser_skip_to_end_of_statement (parser);
4928
4929 cp_parser_end_tentative_firewall (parser, start, expr);
4930 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4931 return cp_expr (expr, combined_loc);
4932 }
4933
4934 /* Expressions [gram.expr] */
4935
4936 /* Parse a fold-operator.
4937
4938 fold-operator:
4939 - * / % ^ & | = < > << >>
4940 = -= *= /= %= ^= &= |= <<= >>=
4941 == != <= >= && || , .* ->*
4942
4943 This returns the tree code corresponding to the matched operator
4944 as an int. When the current token matches a compound assignment
4945 operator, the resulting tree code is the negative value of the
4946 non-assignment operator. */
4947
4948 static int
4949 cp_parser_fold_operator (cp_token *token)
4950 {
4951 switch (token->type)
4952 {
4953 case CPP_PLUS: return PLUS_EXPR;
4954 case CPP_MINUS: return MINUS_EXPR;
4955 case CPP_MULT: return MULT_EXPR;
4956 case CPP_DIV: return TRUNC_DIV_EXPR;
4957 case CPP_MOD: return TRUNC_MOD_EXPR;
4958 case CPP_XOR: return BIT_XOR_EXPR;
4959 case CPP_AND: return BIT_AND_EXPR;
4960 case CPP_OR: return BIT_IOR_EXPR;
4961 case CPP_LSHIFT: return LSHIFT_EXPR;
4962 case CPP_RSHIFT: return RSHIFT_EXPR;
4963
4964 case CPP_EQ: return -NOP_EXPR;
4965 case CPP_PLUS_EQ: return -PLUS_EXPR;
4966 case CPP_MINUS_EQ: return -MINUS_EXPR;
4967 case CPP_MULT_EQ: return -MULT_EXPR;
4968 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4969 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4970 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4971 case CPP_AND_EQ: return -BIT_AND_EXPR;
4972 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4973 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4974 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4975
4976 case CPP_EQ_EQ: return EQ_EXPR;
4977 case CPP_NOT_EQ: return NE_EXPR;
4978 case CPP_LESS: return LT_EXPR;
4979 case CPP_GREATER: return GT_EXPR;
4980 case CPP_LESS_EQ: return LE_EXPR;
4981 case CPP_GREATER_EQ: return GE_EXPR;
4982
4983 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4984 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4985
4986 case CPP_COMMA: return COMPOUND_EXPR;
4987
4988 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4989 case CPP_DEREF_STAR: return MEMBER_REF;
4990
4991 default: return ERROR_MARK;
4992 }
4993 }
4994
4995 /* Returns true if CODE indicates a binary expression, which is not allowed in
4996 the LHS of a fold-expression. More codes will need to be added to use this
4997 function in other contexts. */
4998
4999 static bool
5000 is_binary_op (tree_code code)
5001 {
5002 switch (code)
5003 {
5004 case PLUS_EXPR:
5005 case POINTER_PLUS_EXPR:
5006 case MINUS_EXPR:
5007 case MULT_EXPR:
5008 case TRUNC_DIV_EXPR:
5009 case TRUNC_MOD_EXPR:
5010 case BIT_XOR_EXPR:
5011 case BIT_AND_EXPR:
5012 case BIT_IOR_EXPR:
5013 case LSHIFT_EXPR:
5014 case RSHIFT_EXPR:
5015
5016 case MODOP_EXPR:
5017
5018 case EQ_EXPR:
5019 case NE_EXPR:
5020 case LE_EXPR:
5021 case GE_EXPR:
5022 case LT_EXPR:
5023 case GT_EXPR:
5024
5025 case TRUTH_ANDIF_EXPR:
5026 case TRUTH_ORIF_EXPR:
5027
5028 case COMPOUND_EXPR:
5029
5030 case DOTSTAR_EXPR:
5031 case MEMBER_REF:
5032 return true;
5033
5034 default:
5035 return false;
5036 }
5037 }
5038
5039 /* If the next token is a suitable fold operator, consume it and return as
5040 the function above. */
5041
5042 static int
5043 cp_parser_fold_operator (cp_parser *parser)
5044 {
5045 cp_token* token = cp_lexer_peek_token (parser->lexer);
5046 int code = cp_parser_fold_operator (token);
5047 if (code != ERROR_MARK)
5048 cp_lexer_consume_token (parser->lexer);
5049 return code;
5050 }
5051
5052 /* Parse a fold-expression.
5053
5054 fold-expression:
5055 ( ... folding-operator cast-expression)
5056 ( cast-expression folding-operator ... )
5057 ( cast-expression folding operator ... folding-operator cast-expression)
5058
5059 Note that the '(' and ')' are matched in primary expression. */
5060
5061 static cp_expr
5062 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5063 {
5064 cp_id_kind pidk;
5065
5066 // Left fold.
5067 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5068 {
5069 cp_lexer_consume_token (parser->lexer);
5070 int op = cp_parser_fold_operator (parser);
5071 if (op == ERROR_MARK)
5072 {
5073 cp_parser_error (parser, "expected binary operator");
5074 return error_mark_node;
5075 }
5076
5077 tree expr = cp_parser_cast_expression (parser, false, false,
5078 false, &pidk);
5079 if (expr == error_mark_node)
5080 return error_mark_node;
5081 return finish_left_unary_fold_expr (expr, op);
5082 }
5083
5084 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5085 int op = cp_parser_fold_operator (parser);
5086 if (op == ERROR_MARK)
5087 {
5088 cp_parser_error (parser, "expected binary operator");
5089 return error_mark_node;
5090 }
5091
5092 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5093 {
5094 cp_parser_error (parser, "expected ...");
5095 return error_mark_node;
5096 }
5097 cp_lexer_consume_token (parser->lexer);
5098
5099 /* The operands of a fold-expression are cast-expressions, so binary or
5100 conditional expressions are not allowed. We check this here to avoid
5101 tentative parsing. */
5102 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5103 /* OK, the expression was parenthesized. */;
5104 else if (is_binary_op (TREE_CODE (expr1)))
5105 error_at (location_of (expr1),
5106 "binary expression in operand of fold-expression");
5107 else if (TREE_CODE (expr1) == COND_EXPR
5108 || (REFERENCE_REF_P (expr1)
5109 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5110 error_at (location_of (expr1),
5111 "conditional expression in operand of fold-expression");
5112
5113 // Right fold.
5114 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5115 return finish_right_unary_fold_expr (expr1, op);
5116
5117 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5118 {
5119 cp_parser_error (parser, "mismatched operator in fold-expression");
5120 return error_mark_node;
5121 }
5122 cp_lexer_consume_token (parser->lexer);
5123
5124 // Binary left or right fold.
5125 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5126 if (expr2 == error_mark_node)
5127 return error_mark_node;
5128 return finish_binary_fold_expr (expr1, expr2, op);
5129 }
5130
5131 /* Parse a primary-expression.
5132
5133 primary-expression:
5134 literal
5135 this
5136 ( expression )
5137 id-expression
5138 lambda-expression (C++11)
5139
5140 GNU Extensions:
5141
5142 primary-expression:
5143 ( compound-statement )
5144 __builtin_va_arg ( assignment-expression , type-id )
5145 __builtin_offsetof ( type-id , offsetof-expression )
5146
5147 C++ Extensions:
5148 __has_nothrow_assign ( type-id )
5149 __has_nothrow_constructor ( type-id )
5150 __has_nothrow_copy ( type-id )
5151 __has_trivial_assign ( type-id )
5152 __has_trivial_constructor ( type-id )
5153 __has_trivial_copy ( type-id )
5154 __has_trivial_destructor ( type-id )
5155 __has_virtual_destructor ( type-id )
5156 __is_abstract ( type-id )
5157 __is_base_of ( type-id , type-id )
5158 __is_class ( type-id )
5159 __is_empty ( type-id )
5160 __is_enum ( type-id )
5161 __is_final ( type-id )
5162 __is_literal_type ( type-id )
5163 __is_pod ( type-id )
5164 __is_polymorphic ( type-id )
5165 __is_std_layout ( type-id )
5166 __is_trivial ( type-id )
5167 __is_union ( type-id )
5168
5169 Objective-C++ Extension:
5170
5171 primary-expression:
5172 objc-expression
5173
5174 literal:
5175 __null
5176
5177 ADDRESS_P is true iff this expression was immediately preceded by
5178 "&" and therefore might denote a pointer-to-member. CAST_P is true
5179 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5180 true iff this expression is a template argument.
5181
5182 Returns a representation of the expression. Upon return, *IDK
5183 indicates what kind of id-expression (if any) was present. */
5184
5185 static cp_expr
5186 cp_parser_primary_expression (cp_parser *parser,
5187 bool address_p,
5188 bool cast_p,
5189 bool template_arg_p,
5190 bool decltype_p,
5191 cp_id_kind *idk)
5192 {
5193 cp_token *token = NULL;
5194
5195 /* Assume the primary expression is not an id-expression. */
5196 *idk = CP_ID_KIND_NONE;
5197
5198 /* Peek at the next token. */
5199 token = cp_lexer_peek_token (parser->lexer);
5200 switch ((int) token->type)
5201 {
5202 /* literal:
5203 integer-literal
5204 character-literal
5205 floating-literal
5206 string-literal
5207 boolean-literal
5208 pointer-literal
5209 user-defined-literal */
5210 case CPP_CHAR:
5211 case CPP_CHAR16:
5212 case CPP_CHAR32:
5213 case CPP_WCHAR:
5214 case CPP_UTF8CHAR:
5215 case CPP_NUMBER:
5216 case CPP_PREPARSED_EXPR:
5217 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5218 return cp_parser_userdef_numeric_literal (parser);
5219 token = cp_lexer_consume_token (parser->lexer);
5220 if (TREE_CODE (token->u.value) == FIXED_CST)
5221 {
5222 error_at (token->location,
5223 "fixed-point types not supported in C++");
5224 return error_mark_node;
5225 }
5226 /* Floating-point literals are only allowed in an integral
5227 constant expression if they are cast to an integral or
5228 enumeration type. */
5229 if (TREE_CODE (token->u.value) == REAL_CST
5230 && parser->integral_constant_expression_p
5231 && pedantic)
5232 {
5233 /* CAST_P will be set even in invalid code like "int(2.7 +
5234 ...)". Therefore, we have to check that the next token
5235 is sure to end the cast. */
5236 if (cast_p)
5237 {
5238 cp_token *next_token;
5239
5240 next_token = cp_lexer_peek_token (parser->lexer);
5241 if (/* The comma at the end of an
5242 enumerator-definition. */
5243 next_token->type != CPP_COMMA
5244 /* The curly brace at the end of an enum-specifier. */
5245 && next_token->type != CPP_CLOSE_BRACE
5246 /* The end of a statement. */
5247 && next_token->type != CPP_SEMICOLON
5248 /* The end of the cast-expression. */
5249 && next_token->type != CPP_CLOSE_PAREN
5250 /* The end of an array bound. */
5251 && next_token->type != CPP_CLOSE_SQUARE
5252 /* The closing ">" in a template-argument-list. */
5253 && (next_token->type != CPP_GREATER
5254 || parser->greater_than_is_operator_p)
5255 /* C++0x only: A ">>" treated like two ">" tokens,
5256 in a template-argument-list. */
5257 && (next_token->type != CPP_RSHIFT
5258 || (cxx_dialect == cxx98)
5259 || parser->greater_than_is_operator_p))
5260 cast_p = false;
5261 }
5262
5263 /* If we are within a cast, then the constraint that the
5264 cast is to an integral or enumeration type will be
5265 checked at that point. If we are not within a cast, then
5266 this code is invalid. */
5267 if (!cast_p)
5268 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5269 }
5270 return (cp_expr (token->u.value, token->location)
5271 .maybe_add_location_wrapper ());
5272
5273 case CPP_CHAR_USERDEF:
5274 case CPP_CHAR16_USERDEF:
5275 case CPP_CHAR32_USERDEF:
5276 case CPP_WCHAR_USERDEF:
5277 case CPP_UTF8CHAR_USERDEF:
5278 return cp_parser_userdef_char_literal (parser);
5279
5280 case CPP_STRING:
5281 case CPP_STRING16:
5282 case CPP_STRING32:
5283 case CPP_WSTRING:
5284 case CPP_UTF8STRING:
5285 case CPP_STRING_USERDEF:
5286 case CPP_STRING16_USERDEF:
5287 case CPP_STRING32_USERDEF:
5288 case CPP_WSTRING_USERDEF:
5289 case CPP_UTF8STRING_USERDEF:
5290 /* ??? Should wide strings be allowed when parser->translate_strings_p
5291 is false (i.e. in attributes)? If not, we can kill the third
5292 argument to cp_parser_string_literal. */
5293 return (cp_parser_string_literal (parser,
5294 parser->translate_strings_p,
5295 true)
5296 .maybe_add_location_wrapper ());
5297
5298 case CPP_OPEN_PAREN:
5299 /* If we see `( { ' then we are looking at the beginning of
5300 a GNU statement-expression. */
5301 if (cp_parser_allow_gnu_extensions_p (parser)
5302 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5303 {
5304 /* Statement-expressions are not allowed by the standard. */
5305 pedwarn (token->location, OPT_Wpedantic,
5306 "ISO C++ forbids braced-groups within expressions");
5307
5308 /* And they're not allowed outside of a function-body; you
5309 cannot, for example, write:
5310
5311 int i = ({ int j = 3; j + 1; });
5312
5313 at class or namespace scope. */
5314 if (!parser->in_function_body
5315 || parser->in_template_argument_list_p)
5316 {
5317 error_at (token->location,
5318 "statement-expressions are not allowed outside "
5319 "functions nor in template-argument lists");
5320 cp_parser_skip_to_end_of_block_or_statement (parser);
5321 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5322 cp_lexer_consume_token (parser->lexer);
5323 return error_mark_node;
5324 }
5325 else
5326 return cp_parser_statement_expr (parser);
5327 }
5328 /* Otherwise it's a normal parenthesized expression. */
5329 {
5330 cp_expr expr;
5331 bool saved_greater_than_is_operator_p;
5332
5333 location_t open_paren_loc = token->location;
5334
5335 /* Consume the `('. */
5336 matching_parens parens;
5337 parens.consume_open (parser);
5338 /* Within a parenthesized expression, a `>' token is always
5339 the greater-than operator. */
5340 saved_greater_than_is_operator_p
5341 = parser->greater_than_is_operator_p;
5342 parser->greater_than_is_operator_p = true;
5343
5344 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5345 /* Left fold expression. */
5346 expr = NULL_TREE;
5347 else
5348 /* Parse the parenthesized expression. */
5349 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5350
5351 token = cp_lexer_peek_token (parser->lexer);
5352 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5353 {
5354 expr = cp_parser_fold_expression (parser, expr);
5355 if (expr != error_mark_node
5356 && cxx_dialect < cxx17)
5357 pedwarn (input_location, 0, "fold-expressions only available "
5358 "with %<-std=c++17%> or %<-std=gnu++17%>");
5359 }
5360 else
5361 /* Let the front end know that this expression was
5362 enclosed in parentheses. This matters in case, for
5363 example, the expression is of the form `A::B', since
5364 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5365 not. */
5366 expr = finish_parenthesized_expr (expr);
5367
5368 /* DR 705: Wrapping an unqualified name in parentheses
5369 suppresses arg-dependent lookup. We want to pass back
5370 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5371 (c++/37862), but none of the others. */
5372 if (*idk != CP_ID_KIND_QUALIFIED)
5373 *idk = CP_ID_KIND_NONE;
5374
5375 /* The `>' token might be the end of a template-id or
5376 template-parameter-list now. */
5377 parser->greater_than_is_operator_p
5378 = saved_greater_than_is_operator_p;
5379
5380 /* Consume the `)'. */
5381 token = cp_lexer_peek_token (parser->lexer);
5382 location_t close_paren_loc = token->location;
5383 expr.set_range (open_paren_loc, close_paren_loc);
5384 if (!parens.require_close (parser)
5385 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5386 cp_parser_skip_to_end_of_statement (parser);
5387
5388 return expr;
5389 }
5390
5391 case CPP_OPEN_SQUARE:
5392 {
5393 if (c_dialect_objc ())
5394 {
5395 /* We might have an Objective-C++ message. */
5396 cp_parser_parse_tentatively (parser);
5397 tree msg = cp_parser_objc_message_expression (parser);
5398 /* If that works out, we're done ... */
5399 if (cp_parser_parse_definitely (parser))
5400 return msg;
5401 /* ... else, fall though to see if it's a lambda. */
5402 }
5403 cp_expr lam = cp_parser_lambda_expression (parser);
5404 /* Don't warn about a failed tentative parse. */
5405 if (cp_parser_error_occurred (parser))
5406 return error_mark_node;
5407 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5408 return lam;
5409 }
5410
5411 case CPP_OBJC_STRING:
5412 if (c_dialect_objc ())
5413 /* We have an Objective-C++ string literal. */
5414 return cp_parser_objc_expression (parser);
5415 cp_parser_error (parser, "expected primary-expression");
5416 return error_mark_node;
5417
5418 case CPP_KEYWORD:
5419 switch (token->keyword)
5420 {
5421 /* These two are the boolean literals. */
5422 case RID_TRUE:
5423 cp_lexer_consume_token (parser->lexer);
5424 return cp_expr (boolean_true_node, token->location);
5425 case RID_FALSE:
5426 cp_lexer_consume_token (parser->lexer);
5427 return cp_expr (boolean_false_node, token->location);
5428
5429 /* The `__null' literal. */
5430 case RID_NULL:
5431 cp_lexer_consume_token (parser->lexer);
5432 return cp_expr (null_node, token->location);
5433
5434 /* The `nullptr' literal. */
5435 case RID_NULLPTR:
5436 cp_lexer_consume_token (parser->lexer);
5437 return cp_expr (nullptr_node, token->location);
5438
5439 /* Recognize the `this' keyword. */
5440 case RID_THIS:
5441 cp_lexer_consume_token (parser->lexer);
5442 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5443 {
5444 error_at (token->location,
5445 "%<this%> may not be used in this context");
5446 return error_mark_node;
5447 }
5448 /* Pointers cannot appear in constant-expressions. */
5449 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5450 return error_mark_node;
5451 return cp_expr (finish_this_expr (), token->location);
5452
5453 /* The `operator' keyword can be the beginning of an
5454 id-expression. */
5455 case RID_OPERATOR:
5456 goto id_expression;
5457
5458 case RID_FUNCTION_NAME:
5459 case RID_PRETTY_FUNCTION_NAME:
5460 case RID_C99_FUNCTION_NAME:
5461 {
5462 non_integral_constant name;
5463
5464 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5465 __func__ are the names of variables -- but they are
5466 treated specially. Therefore, they are handled here,
5467 rather than relying on the generic id-expression logic
5468 below. Grammatically, these names are id-expressions.
5469
5470 Consume the token. */
5471 token = cp_lexer_consume_token (parser->lexer);
5472
5473 switch (token->keyword)
5474 {
5475 case RID_FUNCTION_NAME:
5476 name = NIC_FUNC_NAME;
5477 break;
5478 case RID_PRETTY_FUNCTION_NAME:
5479 name = NIC_PRETTY_FUNC;
5480 break;
5481 case RID_C99_FUNCTION_NAME:
5482 name = NIC_C99_FUNC;
5483 break;
5484 default:
5485 gcc_unreachable ();
5486 }
5487
5488 if (cp_parser_non_integral_constant_expression (parser, name))
5489 return error_mark_node;
5490
5491 /* Look up the name. */
5492 return finish_fname (token->u.value);
5493 }
5494
5495 case RID_VA_ARG:
5496 {
5497 tree expression;
5498 tree type;
5499 location_t type_location;
5500 location_t start_loc
5501 = cp_lexer_peek_token (parser->lexer)->location;
5502 /* The `__builtin_va_arg' construct is used to handle
5503 `va_arg'. Consume the `__builtin_va_arg' token. */
5504 cp_lexer_consume_token (parser->lexer);
5505 /* Look for the opening `('. */
5506 matching_parens parens;
5507 parens.require_open (parser);
5508 /* Now, parse the assignment-expression. */
5509 expression = cp_parser_assignment_expression (parser);
5510 /* Look for the `,'. */
5511 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5512 type_location = cp_lexer_peek_token (parser->lexer)->location;
5513 /* Parse the type-id. */
5514 {
5515 type_id_in_expr_sentinel s (parser);
5516 type = cp_parser_type_id (parser);
5517 }
5518 /* Look for the closing `)'. */
5519 location_t finish_loc
5520 = cp_lexer_peek_token (parser->lexer)->location;
5521 parens.require_close (parser);
5522 /* Using `va_arg' in a constant-expression is not
5523 allowed. */
5524 if (cp_parser_non_integral_constant_expression (parser,
5525 NIC_VA_ARG))
5526 return error_mark_node;
5527 /* Construct a location of the form:
5528 __builtin_va_arg (v, int)
5529 ~~~~~~~~~~~~~~~~~~~~~^~~~
5530 with the caret at the type, ranging from the start of the
5531 "__builtin_va_arg" token to the close paren. */
5532 location_t combined_loc
5533 = make_location (type_location, start_loc, finish_loc);
5534 return build_x_va_arg (combined_loc, expression, type);
5535 }
5536
5537 case RID_OFFSETOF:
5538 return cp_parser_builtin_offsetof (parser);
5539
5540 case RID_HAS_NOTHROW_ASSIGN:
5541 case RID_HAS_NOTHROW_CONSTRUCTOR:
5542 case RID_HAS_NOTHROW_COPY:
5543 case RID_HAS_TRIVIAL_ASSIGN:
5544 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5545 case RID_HAS_TRIVIAL_COPY:
5546 case RID_HAS_TRIVIAL_DESTRUCTOR:
5547 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5548 case RID_HAS_VIRTUAL_DESTRUCTOR:
5549 case RID_IS_ABSTRACT:
5550 case RID_IS_AGGREGATE:
5551 case RID_IS_BASE_OF:
5552 case RID_IS_CLASS:
5553 case RID_IS_EMPTY:
5554 case RID_IS_ENUM:
5555 case RID_IS_FINAL:
5556 case RID_IS_LITERAL_TYPE:
5557 case RID_IS_POD:
5558 case RID_IS_POLYMORPHIC:
5559 case RID_IS_SAME_AS:
5560 case RID_IS_STD_LAYOUT:
5561 case RID_IS_TRIVIAL:
5562 case RID_IS_TRIVIALLY_ASSIGNABLE:
5563 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5564 case RID_IS_TRIVIALLY_COPYABLE:
5565 case RID_IS_UNION:
5566 case RID_IS_ASSIGNABLE:
5567 case RID_IS_CONSTRUCTIBLE:
5568 return cp_parser_trait_expr (parser, token->keyword);
5569
5570 // C++ concepts
5571 case RID_REQUIRES:
5572 return cp_parser_requires_expression (parser);
5573
5574 /* Objective-C++ expressions. */
5575 case RID_AT_ENCODE:
5576 case RID_AT_PROTOCOL:
5577 case RID_AT_SELECTOR:
5578 return cp_parser_objc_expression (parser);
5579
5580 case RID_TEMPLATE:
5581 if (parser->in_function_body
5582 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5583 == CPP_LESS))
5584 {
5585 error_at (token->location,
5586 "a template declaration cannot appear at block scope");
5587 cp_parser_skip_to_end_of_block_or_statement (parser);
5588 return error_mark_node;
5589 }
5590 /* FALLTHRU */
5591 default:
5592 cp_parser_error (parser, "expected primary-expression");
5593 return error_mark_node;
5594 }
5595
5596 /* An id-expression can start with either an identifier, a
5597 `::' as the beginning of a qualified-id, or the "operator"
5598 keyword. */
5599 case CPP_NAME:
5600 case CPP_SCOPE:
5601 case CPP_TEMPLATE_ID:
5602 case CPP_NESTED_NAME_SPECIFIER:
5603 {
5604 id_expression:
5605 cp_expr id_expression;
5606 cp_expr decl;
5607 const char *error_msg;
5608 bool template_p;
5609 bool done;
5610 cp_token *id_expr_token;
5611
5612 /* Parse the id-expression. */
5613 id_expression
5614 = cp_parser_id_expression (parser,
5615 /*template_keyword_p=*/false,
5616 /*check_dependency_p=*/true,
5617 &template_p,
5618 /*declarator_p=*/false,
5619 /*optional_p=*/false);
5620 if (id_expression == error_mark_node)
5621 return error_mark_node;
5622 id_expr_token = token;
5623 token = cp_lexer_peek_token (parser->lexer);
5624 done = (token->type != CPP_OPEN_SQUARE
5625 && token->type != CPP_OPEN_PAREN
5626 && token->type != CPP_DOT
5627 && token->type != CPP_DEREF
5628 && token->type != CPP_PLUS_PLUS
5629 && token->type != CPP_MINUS_MINUS);
5630 /* If we have a template-id, then no further lookup is
5631 required. If the template-id was for a template-class, we
5632 will sometimes have a TYPE_DECL at this point. */
5633 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5634 || TREE_CODE (id_expression) == TYPE_DECL)
5635 decl = id_expression;
5636 /* Look up the name. */
5637 else
5638 {
5639 tree ambiguous_decls;
5640
5641 /* If we already know that this lookup is ambiguous, then
5642 we've already issued an error message; there's no reason
5643 to check again. */
5644 if (id_expr_token->type == CPP_NAME
5645 && id_expr_token->error_reported)
5646 {
5647 cp_parser_simulate_error (parser);
5648 return error_mark_node;
5649 }
5650
5651 decl = cp_parser_lookup_name (parser, id_expression,
5652 none_type,
5653 template_p,
5654 /*is_namespace=*/false,
5655 /*check_dependency=*/true,
5656 &ambiguous_decls,
5657 id_expression.get_location ());
5658 /* If the lookup was ambiguous, an error will already have
5659 been issued. */
5660 if (ambiguous_decls)
5661 return error_mark_node;
5662
5663 /* In Objective-C++, we may have an Objective-C 2.0
5664 dot-syntax for classes here. */
5665 if (c_dialect_objc ()
5666 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5667 && TREE_CODE (decl) == TYPE_DECL
5668 && objc_is_class_name (decl))
5669 {
5670 tree component;
5671 cp_lexer_consume_token (parser->lexer);
5672 component = cp_parser_identifier (parser);
5673 if (component == error_mark_node)
5674 return error_mark_node;
5675
5676 tree result = objc_build_class_component_ref (id_expression,
5677 component);
5678 /* Build a location of the form:
5679 expr.component
5680 ~~~~~^~~~~~~~~
5681 with caret at the start of the component name (at
5682 input_location), ranging from the start of the id_expression
5683 to the end of the component name. */
5684 location_t combined_loc
5685 = make_location (input_location, id_expression.get_start (),
5686 get_finish (input_location));
5687 protected_set_expr_location (result, combined_loc);
5688 return result;
5689 }
5690
5691 /* In Objective-C++, an instance variable (ivar) may be preferred
5692 to whatever cp_parser_lookup_name() found.
5693 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5694 rest of c-family, we have to do a little extra work to preserve
5695 any location information in cp_expr "decl". Given that
5696 objc_lookup_ivar is implemented in "c-family" and "objc", we
5697 have a trip through the pure "tree" type, rather than cp_expr.
5698 Naively copying it back to "decl" would implicitly give the
5699 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5700 store an EXPR_LOCATION. Hence we only update "decl" (and
5701 hence its location_t) if we get back a different tree node. */
5702 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5703 id_expression);
5704 if (decl_tree != decl.get_value ())
5705 decl = cp_expr (decl_tree);
5706
5707 /* If name lookup gives us a SCOPE_REF, then the
5708 qualifying scope was dependent. */
5709 if (TREE_CODE (decl) == SCOPE_REF)
5710 {
5711 /* At this point, we do not know if DECL is a valid
5712 integral constant expression. We assume that it is
5713 in fact such an expression, so that code like:
5714
5715 template <int N> struct A {
5716 int a[B<N>::i];
5717 };
5718
5719 is accepted. At template-instantiation time, we
5720 will check that B<N>::i is actually a constant. */
5721 return decl;
5722 }
5723 /* Check to see if DECL is a local variable in a context
5724 where that is forbidden. */
5725 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
5726 && local_variable_p (decl))
5727 {
5728 error_at (id_expression.get_location (),
5729 "local variable %qD may not appear in this context",
5730 decl.get_value ());
5731 return error_mark_node;
5732 }
5733 }
5734
5735 decl = (finish_id_expression
5736 (id_expression, decl, parser->scope,
5737 idk,
5738 parser->integral_constant_expression_p,
5739 parser->allow_non_integral_constant_expression_p,
5740 &parser->non_integral_constant_expression_p,
5741 template_p, done, address_p,
5742 template_arg_p,
5743 &error_msg,
5744 id_expression.get_location ()));
5745 if (error_msg)
5746 cp_parser_error (parser, error_msg);
5747 /* Build a location for an id-expression of the form:
5748 ::ns::id
5749 ~~~~~~^~
5750 or:
5751 id
5752 ^~
5753 i.e. from the start of the first token to the end of the final
5754 token, with the caret at the start of the unqualified-id. */
5755 location_t caret_loc = get_pure_location (id_expression.get_location ());
5756 location_t start_loc = get_start (id_expr_token->location);
5757 location_t finish_loc = get_finish (id_expression.get_location ());
5758 location_t combined_loc
5759 = make_location (caret_loc, start_loc, finish_loc);
5760
5761 decl.set_location (combined_loc);
5762 return decl;
5763 }
5764
5765 /* Anything else is an error. */
5766 default:
5767 cp_parser_error (parser, "expected primary-expression");
5768 return error_mark_node;
5769 }
5770 }
5771
5772 static inline cp_expr
5773 cp_parser_primary_expression (cp_parser *parser,
5774 bool address_p,
5775 bool cast_p,
5776 bool template_arg_p,
5777 cp_id_kind *idk)
5778 {
5779 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5780 /*decltype*/false, idk);
5781 }
5782
5783 /* Parse an id-expression.
5784
5785 id-expression:
5786 unqualified-id
5787 qualified-id
5788
5789 qualified-id:
5790 :: [opt] nested-name-specifier template [opt] unqualified-id
5791 :: identifier
5792 :: operator-function-id
5793 :: template-id
5794
5795 Return a representation of the unqualified portion of the
5796 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5797 a `::' or nested-name-specifier.
5798
5799 Often, if the id-expression was a qualified-id, the caller will
5800 want to make a SCOPE_REF to represent the qualified-id. This
5801 function does not do this in order to avoid wastefully creating
5802 SCOPE_REFs when they are not required.
5803
5804 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5805 `template' keyword.
5806
5807 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5808 uninstantiated templates.
5809
5810 If *TEMPLATE_P is non-NULL, it is set to true iff the
5811 `template' keyword is used to explicitly indicate that the entity
5812 named is a template.
5813
5814 If DECLARATOR_P is true, the id-expression is appearing as part of
5815 a declarator, rather than as part of an expression. */
5816
5817 static cp_expr
5818 cp_parser_id_expression (cp_parser *parser,
5819 bool template_keyword_p,
5820 bool check_dependency_p,
5821 bool *template_p,
5822 bool declarator_p,
5823 bool optional_p)
5824 {
5825 bool global_scope_p;
5826 bool nested_name_specifier_p;
5827
5828 /* Assume the `template' keyword was not used. */
5829 if (template_p)
5830 *template_p = template_keyword_p;
5831
5832 /* Look for the optional `::' operator. */
5833 global_scope_p
5834 = (!template_keyword_p
5835 && (cp_parser_global_scope_opt (parser,
5836 /*current_scope_valid_p=*/false)
5837 != NULL_TREE));
5838
5839 /* Look for the optional nested-name-specifier. */
5840 nested_name_specifier_p
5841 = (cp_parser_nested_name_specifier_opt (parser,
5842 /*typename_keyword_p=*/false,
5843 check_dependency_p,
5844 /*type_p=*/false,
5845 declarator_p,
5846 template_keyword_p)
5847 != NULL_TREE);
5848
5849 /* If there is a nested-name-specifier, then we are looking at
5850 the first qualified-id production. */
5851 if (nested_name_specifier_p)
5852 {
5853 tree saved_scope;
5854 tree saved_object_scope;
5855 tree saved_qualifying_scope;
5856 cp_expr unqualified_id;
5857 bool is_template;
5858
5859 /* See if the next token is the `template' keyword. */
5860 if (!template_p)
5861 template_p = &is_template;
5862 *template_p = cp_parser_optional_template_keyword (parser);
5863 /* Name lookup we do during the processing of the
5864 unqualified-id might obliterate SCOPE. */
5865 saved_scope = parser->scope;
5866 saved_object_scope = parser->object_scope;
5867 saved_qualifying_scope = parser->qualifying_scope;
5868 /* Process the final unqualified-id. */
5869 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5870 check_dependency_p,
5871 declarator_p,
5872 /*optional_p=*/false);
5873 /* Restore the SAVED_SCOPE for our caller. */
5874 parser->scope = saved_scope;
5875 parser->object_scope = saved_object_scope;
5876 parser->qualifying_scope = saved_qualifying_scope;
5877
5878 return unqualified_id;
5879 }
5880 /* Otherwise, if we are in global scope, then we are looking at one
5881 of the other qualified-id productions. */
5882 else if (global_scope_p)
5883 {
5884 cp_token *token;
5885 tree id;
5886
5887 /* Peek at the next token. */
5888 token = cp_lexer_peek_token (parser->lexer);
5889
5890 /* If it's an identifier, and the next token is not a "<", then
5891 we can avoid the template-id case. This is an optimization
5892 for this common case. */
5893 if (token->type == CPP_NAME
5894 && !cp_parser_nth_token_starts_template_argument_list_p
5895 (parser, 2))
5896 return cp_parser_identifier (parser);
5897
5898 cp_parser_parse_tentatively (parser);
5899 /* Try a template-id. */
5900 id = cp_parser_template_id_expr (parser,
5901 /*template_keyword_p=*/false,
5902 /*check_dependency_p=*/true,
5903 declarator_p);
5904 /* If that worked, we're done. */
5905 if (cp_parser_parse_definitely (parser))
5906 return id;
5907
5908 /* Peek at the next token. (Changes in the token buffer may
5909 have invalidated the pointer obtained above.) */
5910 token = cp_lexer_peek_token (parser->lexer);
5911
5912 switch (token->type)
5913 {
5914 case CPP_NAME:
5915 return cp_parser_identifier (parser);
5916
5917 case CPP_KEYWORD:
5918 if (token->keyword == RID_OPERATOR)
5919 return cp_parser_operator_function_id (parser);
5920 /* Fall through. */
5921
5922 default:
5923 cp_parser_error (parser, "expected id-expression");
5924 return error_mark_node;
5925 }
5926 }
5927 else
5928 return cp_parser_unqualified_id (parser, template_keyword_p,
5929 /*check_dependency_p=*/true,
5930 declarator_p,
5931 optional_p);
5932 }
5933
5934 /* Parse an unqualified-id.
5935
5936 unqualified-id:
5937 identifier
5938 operator-function-id
5939 conversion-function-id
5940 ~ class-name
5941 template-id
5942
5943 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5944 keyword, in a construct like `A::template ...'.
5945
5946 Returns a representation of unqualified-id. For the `identifier'
5947 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5948 production a BIT_NOT_EXPR is returned; the operand of the
5949 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5950 other productions, see the documentation accompanying the
5951 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5952 names are looked up in uninstantiated templates. If DECLARATOR_P
5953 is true, the unqualified-id is appearing as part of a declarator,
5954 rather than as part of an expression. */
5955
5956 static cp_expr
5957 cp_parser_unqualified_id (cp_parser* parser,
5958 bool template_keyword_p,
5959 bool check_dependency_p,
5960 bool declarator_p,
5961 bool optional_p)
5962 {
5963 cp_token *token;
5964
5965 /* Peek at the next token. */
5966 token = cp_lexer_peek_token (parser->lexer);
5967
5968 switch ((int) token->type)
5969 {
5970 case CPP_NAME:
5971 {
5972 tree id;
5973
5974 /* We don't know yet whether or not this will be a
5975 template-id. */
5976 cp_parser_parse_tentatively (parser);
5977 /* Try a template-id. */
5978 id = cp_parser_template_id_expr (parser, template_keyword_p,
5979 check_dependency_p,
5980 declarator_p);
5981 /* If it worked, we're done. */
5982 if (cp_parser_parse_definitely (parser))
5983 return id;
5984 /* Otherwise, it's an ordinary identifier. */
5985 return cp_parser_identifier (parser);
5986 }
5987
5988 case CPP_TEMPLATE_ID:
5989 return cp_parser_template_id_expr (parser, template_keyword_p,
5990 check_dependency_p,
5991 declarator_p);
5992
5993 case CPP_COMPL:
5994 {
5995 tree type_decl;
5996 tree qualifying_scope;
5997 tree object_scope;
5998 tree scope;
5999 bool done;
6000 location_t tilde_loc = token->location;
6001
6002 /* Consume the `~' token. */
6003 cp_lexer_consume_token (parser->lexer);
6004 /* Parse the class-name. The standard, as written, seems to
6005 say that:
6006
6007 template <typename T> struct S { ~S (); };
6008 template <typename T> S<T>::~S() {}
6009
6010 is invalid, since `~' must be followed by a class-name, but
6011 `S<T>' is dependent, and so not known to be a class.
6012 That's not right; we need to look in uninstantiated
6013 templates. A further complication arises from:
6014
6015 template <typename T> void f(T t) {
6016 t.T::~T();
6017 }
6018
6019 Here, it is not possible to look up `T' in the scope of `T'
6020 itself. We must look in both the current scope, and the
6021 scope of the containing complete expression.
6022
6023 Yet another issue is:
6024
6025 struct S {
6026 int S;
6027 ~S();
6028 };
6029
6030 S::~S() {}
6031
6032 The standard does not seem to say that the `S' in `~S'
6033 should refer to the type `S' and not the data member
6034 `S::S'. */
6035
6036 /* DR 244 says that we look up the name after the "~" in the
6037 same scope as we looked up the qualifying name. That idea
6038 isn't fully worked out; it's more complicated than that. */
6039 scope = parser->scope;
6040 object_scope = parser->object_scope;
6041 qualifying_scope = parser->qualifying_scope;
6042
6043 /* Check for invalid scopes. */
6044 if (scope == error_mark_node)
6045 {
6046 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6047 cp_lexer_consume_token (parser->lexer);
6048 return error_mark_node;
6049 }
6050 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6051 {
6052 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6053 error_at (token->location,
6054 "scope %qT before %<~%> is not a class-name",
6055 scope);
6056 cp_parser_simulate_error (parser);
6057 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6058 cp_lexer_consume_token (parser->lexer);
6059 return error_mark_node;
6060 }
6061 gcc_assert (!scope || TYPE_P (scope));
6062
6063 token = cp_lexer_peek_token (parser->lexer);
6064
6065 /* Create a location with caret == start at the tilde,
6066 finishing at the end of the peeked token, e.g:
6067 ~token
6068 ^~~~~~. */
6069 location_t loc
6070 = make_location (tilde_loc, tilde_loc, token->location);
6071
6072 /* If the name is of the form "X::~X" it's OK even if X is a
6073 typedef. */
6074
6075 if (scope
6076 && token->type == CPP_NAME
6077 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6078 != CPP_LESS)
6079 && (token->u.value == TYPE_IDENTIFIER (scope)
6080 || (CLASS_TYPE_P (scope)
6081 && constructor_name_p (token->u.value, scope))))
6082 {
6083 cp_lexer_consume_token (parser->lexer);
6084 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6085 }
6086
6087 /* ~auto means the destructor of whatever the object is. */
6088 if (cp_parser_is_keyword (token, RID_AUTO))
6089 {
6090 if (cxx_dialect < cxx14)
6091 pedwarn (loc, 0,
6092 "%<~auto%> only available with "
6093 "%<-std=c++14%> or %<-std=gnu++14%>");
6094 cp_lexer_consume_token (parser->lexer);
6095 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6096 }
6097
6098 /* If there was an explicit qualification (S::~T), first look
6099 in the scope given by the qualification (i.e., S).
6100
6101 Note: in the calls to cp_parser_class_name below we pass
6102 typename_type so that lookup finds the injected-class-name
6103 rather than the constructor. */
6104 done = false;
6105 type_decl = NULL_TREE;
6106 if (scope)
6107 {
6108 cp_parser_parse_tentatively (parser);
6109 type_decl = cp_parser_class_name (parser,
6110 /*typename_keyword_p=*/false,
6111 /*template_keyword_p=*/false,
6112 typename_type,
6113 /*check_dependency=*/false,
6114 /*class_head_p=*/false,
6115 declarator_p);
6116 if (cp_parser_parse_definitely (parser))
6117 done = true;
6118 }
6119 /* In "N::S::~S", look in "N" as well. */
6120 if (!done && scope && qualifying_scope)
6121 {
6122 cp_parser_parse_tentatively (parser);
6123 parser->scope = qualifying_scope;
6124 parser->object_scope = NULL_TREE;
6125 parser->qualifying_scope = NULL_TREE;
6126 type_decl
6127 = cp_parser_class_name (parser,
6128 /*typename_keyword_p=*/false,
6129 /*template_keyword_p=*/false,
6130 typename_type,
6131 /*check_dependency=*/false,
6132 /*class_head_p=*/false,
6133 declarator_p);
6134 if (cp_parser_parse_definitely (parser))
6135 done = true;
6136 }
6137 /* In "p->S::~T", look in the scope given by "*p" as well. */
6138 else if (!done && object_scope)
6139 {
6140 cp_parser_parse_tentatively (parser);
6141 parser->scope = object_scope;
6142 parser->object_scope = NULL_TREE;
6143 parser->qualifying_scope = NULL_TREE;
6144 type_decl
6145 = cp_parser_class_name (parser,
6146 /*typename_keyword_p=*/false,
6147 /*template_keyword_p=*/false,
6148 typename_type,
6149 /*check_dependency=*/false,
6150 /*class_head_p=*/false,
6151 declarator_p);
6152 if (cp_parser_parse_definitely (parser))
6153 done = true;
6154 }
6155 /* Look in the surrounding context. */
6156 if (!done)
6157 {
6158 parser->scope = NULL_TREE;
6159 parser->object_scope = NULL_TREE;
6160 parser->qualifying_scope = NULL_TREE;
6161 if (processing_template_decl)
6162 cp_parser_parse_tentatively (parser);
6163 type_decl
6164 = cp_parser_class_name (parser,
6165 /*typename_keyword_p=*/false,
6166 /*template_keyword_p=*/false,
6167 typename_type,
6168 /*check_dependency=*/false,
6169 /*class_head_p=*/false,
6170 declarator_p);
6171 if (processing_template_decl
6172 && ! cp_parser_parse_definitely (parser))
6173 {
6174 /* We couldn't find a type with this name. If we're parsing
6175 tentatively, fail and try something else. */
6176 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6177 {
6178 cp_parser_simulate_error (parser);
6179 return error_mark_node;
6180 }
6181 /* Otherwise, accept it and check for a match at instantiation
6182 time. */
6183 type_decl = cp_parser_identifier (parser);
6184 if (type_decl != error_mark_node)
6185 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6186 return type_decl;
6187 }
6188 }
6189 /* If an error occurred, assume that the name of the
6190 destructor is the same as the name of the qualifying
6191 class. That allows us to keep parsing after running
6192 into ill-formed destructor names. */
6193 if (type_decl == error_mark_node && scope)
6194 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6195 else if (type_decl == error_mark_node)
6196 return error_mark_node;
6197
6198 /* Check that destructor name and scope match. */
6199 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6200 {
6201 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6202 error_at (loc,
6203 "declaration of %<~%T%> as member of %qT",
6204 type_decl, scope);
6205 cp_parser_simulate_error (parser);
6206 return error_mark_node;
6207 }
6208
6209 /* [class.dtor]
6210
6211 A typedef-name that names a class shall not be used as the
6212 identifier in the declarator for a destructor declaration. */
6213 if (declarator_p
6214 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6215 && !DECL_SELF_REFERENCE_P (type_decl)
6216 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6217 error_at (loc,
6218 "typedef-name %qD used as destructor declarator",
6219 type_decl);
6220
6221 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6222 }
6223
6224 case CPP_KEYWORD:
6225 if (token->keyword == RID_OPERATOR)
6226 {
6227 cp_expr id;
6228
6229 /* This could be a template-id, so we try that first. */
6230 cp_parser_parse_tentatively (parser);
6231 /* Try a template-id. */
6232 id = cp_parser_template_id_expr (parser, template_keyword_p,
6233 /*check_dependency_p=*/true,
6234 declarator_p);
6235 /* If that worked, we're done. */
6236 if (cp_parser_parse_definitely (parser))
6237 return id;
6238 /* We still don't know whether we're looking at an
6239 operator-function-id or a conversion-function-id. */
6240 cp_parser_parse_tentatively (parser);
6241 /* Try an operator-function-id. */
6242 id = cp_parser_operator_function_id (parser);
6243 /* If that didn't work, try a conversion-function-id. */
6244 if (!cp_parser_parse_definitely (parser))
6245 id = cp_parser_conversion_function_id (parser);
6246
6247 return id;
6248 }
6249 /* Fall through. */
6250
6251 default:
6252 if (optional_p)
6253 return NULL_TREE;
6254 cp_parser_error (parser, "expected unqualified-id");
6255 return error_mark_node;
6256 }
6257 }
6258
6259 /* Parse an (optional) nested-name-specifier.
6260
6261 nested-name-specifier: [C++98]
6262 class-or-namespace-name :: nested-name-specifier [opt]
6263 class-or-namespace-name :: template nested-name-specifier [opt]
6264
6265 nested-name-specifier: [C++0x]
6266 type-name ::
6267 namespace-name ::
6268 nested-name-specifier identifier ::
6269 nested-name-specifier template [opt] simple-template-id ::
6270
6271 PARSER->SCOPE should be set appropriately before this function is
6272 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6273 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6274 in name lookups.
6275
6276 Sets PARSER->SCOPE to the class (TYPE) or namespace
6277 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6278 it unchanged if there is no nested-name-specifier. Returns the new
6279 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6280
6281 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6282 part of a declaration and/or decl-specifier. */
6283
6284 static tree
6285 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6286 bool typename_keyword_p,
6287 bool check_dependency_p,
6288 bool type_p,
6289 bool is_declaration,
6290 bool template_keyword_p /* = false */)
6291 {
6292 bool success = false;
6293 cp_token_position start = 0;
6294 cp_token *token;
6295
6296 /* Remember where the nested-name-specifier starts. */
6297 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6298 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6299 {
6300 start = cp_lexer_token_position (parser->lexer, false);
6301 push_deferring_access_checks (dk_deferred);
6302 }
6303
6304 while (true)
6305 {
6306 tree new_scope;
6307 tree old_scope;
6308 tree saved_qualifying_scope;
6309
6310 /* Spot cases that cannot be the beginning of a
6311 nested-name-specifier. */
6312 token = cp_lexer_peek_token (parser->lexer);
6313
6314 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6315 the already parsed nested-name-specifier. */
6316 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6317 {
6318 /* Grab the nested-name-specifier and continue the loop. */
6319 cp_parser_pre_parsed_nested_name_specifier (parser);
6320 /* If we originally encountered this nested-name-specifier
6321 with IS_DECLARATION set to false, we will not have
6322 resolved TYPENAME_TYPEs, so we must do so here. */
6323 if (is_declaration
6324 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6325 {
6326 new_scope = resolve_typename_type (parser->scope,
6327 /*only_current_p=*/false);
6328 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6329 parser->scope = new_scope;
6330 }
6331 success = true;
6332 continue;
6333 }
6334
6335 /* Spot cases that cannot be the beginning of a
6336 nested-name-specifier. On the second and subsequent times
6337 through the loop, we look for the `template' keyword. */
6338 if (success && token->keyword == RID_TEMPLATE)
6339 ;
6340 /* A template-id can start a nested-name-specifier. */
6341 else if (token->type == CPP_TEMPLATE_ID)
6342 ;
6343 /* DR 743: decltype can be used in a nested-name-specifier. */
6344 else if (token_is_decltype (token))
6345 ;
6346 else
6347 {
6348 /* If the next token is not an identifier, then it is
6349 definitely not a type-name or namespace-name. */
6350 if (token->type != CPP_NAME)
6351 break;
6352 /* If the following token is neither a `<' (to begin a
6353 template-id), nor a `::', then we are not looking at a
6354 nested-name-specifier. */
6355 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6356
6357 if (token->type == CPP_COLON
6358 && parser->colon_corrects_to_scope_p
6359 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6360 {
6361 gcc_rich_location richloc (token->location);
6362 richloc.add_fixit_replace ("::");
6363 error_at (&richloc,
6364 "found %<:%> in nested-name-specifier, "
6365 "expected %<::%>");
6366 token->type = CPP_SCOPE;
6367 }
6368
6369 if (token->type != CPP_SCOPE
6370 && !cp_parser_nth_token_starts_template_argument_list_p
6371 (parser, 2))
6372 break;
6373 }
6374
6375 /* The nested-name-specifier is optional, so we parse
6376 tentatively. */
6377 cp_parser_parse_tentatively (parser);
6378
6379 /* Look for the optional `template' keyword, if this isn't the
6380 first time through the loop. */
6381 if (success)
6382 template_keyword_p = cp_parser_optional_template_keyword (parser);
6383
6384 /* Save the old scope since the name lookup we are about to do
6385 might destroy it. */
6386 old_scope = parser->scope;
6387 saved_qualifying_scope = parser->qualifying_scope;
6388 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6389 look up names in "X<T>::I" in order to determine that "Y" is
6390 a template. So, if we have a typename at this point, we make
6391 an effort to look through it. */
6392 if (is_declaration
6393 && !typename_keyword_p
6394 && parser->scope
6395 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6396 parser->scope = resolve_typename_type (parser->scope,
6397 /*only_current_p=*/false);
6398 /* Parse the qualifying entity. */
6399 new_scope
6400 = cp_parser_qualifying_entity (parser,
6401 typename_keyword_p,
6402 template_keyword_p,
6403 check_dependency_p,
6404 type_p,
6405 is_declaration);
6406 /* Look for the `::' token. */
6407 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6408
6409 /* If we found what we wanted, we keep going; otherwise, we're
6410 done. */
6411 if (!cp_parser_parse_definitely (parser))
6412 {
6413 bool error_p = false;
6414
6415 /* Restore the OLD_SCOPE since it was valid before the
6416 failed attempt at finding the last
6417 class-or-namespace-name. */
6418 parser->scope = old_scope;
6419 parser->qualifying_scope = saved_qualifying_scope;
6420
6421 /* If the next token is a decltype, and the one after that is a
6422 `::', then the decltype has failed to resolve to a class or
6423 enumeration type. Give this error even when parsing
6424 tentatively since it can't possibly be valid--and we're going
6425 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6426 won't get another chance.*/
6427 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6428 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6429 == CPP_SCOPE))
6430 {
6431 token = cp_lexer_consume_token (parser->lexer);
6432 tree dtype = token->u.tree_check_value->value;
6433 if (dtype != error_mark_node)
6434 error_at (token->location, "%<decltype%> evaluates to %qT, "
6435 "which is not a class or enumeration type",
6436 dtype);
6437 parser->scope = error_mark_node;
6438 error_p = true;
6439 /* As below. */
6440 success = true;
6441 cp_lexer_consume_token (parser->lexer);
6442 }
6443
6444 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6445 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6446 {
6447 /* If we have a non-type template-id followed by ::, it can't
6448 possibly be valid. */
6449 token = cp_lexer_peek_token (parser->lexer);
6450 tree tid = token->u.tree_check_value->value;
6451 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6452 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6453 {
6454 tree tmpl = NULL_TREE;
6455 if (is_overloaded_fn (tid))
6456 {
6457 tree fns = get_fns (tid);
6458 if (OVL_SINGLE_P (fns))
6459 tmpl = OVL_FIRST (fns);
6460 error_at (token->location, "function template-id %qD "
6461 "in nested-name-specifier", tid);
6462 }
6463 else
6464 {
6465 /* Variable template. */
6466 tmpl = TREE_OPERAND (tid, 0);
6467 gcc_assert (variable_template_p (tmpl));
6468 error_at (token->location, "variable template-id %qD "
6469 "in nested-name-specifier", tid);
6470 }
6471 if (tmpl)
6472 inform (DECL_SOURCE_LOCATION (tmpl),
6473 "%qD declared here", tmpl);
6474
6475 parser->scope = error_mark_node;
6476 error_p = true;
6477 /* As below. */
6478 success = true;
6479 cp_lexer_consume_token (parser->lexer);
6480 cp_lexer_consume_token (parser->lexer);
6481 }
6482 }
6483
6484 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6485 break;
6486 /* If the next token is an identifier, and the one after
6487 that is a `::', then any valid interpretation would have
6488 found a class-or-namespace-name. */
6489 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6490 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6491 == CPP_SCOPE)
6492 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6493 != CPP_COMPL))
6494 {
6495 token = cp_lexer_consume_token (parser->lexer);
6496 if (!error_p)
6497 {
6498 if (!token->error_reported)
6499 {
6500 tree decl;
6501 tree ambiguous_decls;
6502
6503 decl = cp_parser_lookup_name (parser, token->u.value,
6504 none_type,
6505 /*is_template=*/false,
6506 /*is_namespace=*/false,
6507 /*check_dependency=*/true,
6508 &ambiguous_decls,
6509 token->location);
6510 if (TREE_CODE (decl) == TEMPLATE_DECL)
6511 error_at (token->location,
6512 "%qD used without template arguments",
6513 decl);
6514 else if (ambiguous_decls)
6515 {
6516 // cp_parser_lookup_name has the same diagnostic,
6517 // thus make sure to emit it at most once.
6518 if (cp_parser_uncommitted_to_tentative_parse_p
6519 (parser))
6520 {
6521 error_at (token->location,
6522 "reference to %qD is ambiguous",
6523 token->u.value);
6524 print_candidates (ambiguous_decls);
6525 }
6526 decl = error_mark_node;
6527 }
6528 else
6529 {
6530 if (cxx_dialect != cxx98)
6531 cp_parser_name_lookup_error
6532 (parser, token->u.value, decl, NLE_NOT_CXX98,
6533 token->location);
6534 else
6535 cp_parser_name_lookup_error
6536 (parser, token->u.value, decl, NLE_CXX98,
6537 token->location);
6538 }
6539 }
6540 parser->scope = error_mark_node;
6541 error_p = true;
6542 /* Treat this as a successful nested-name-specifier
6543 due to:
6544
6545 [basic.lookup.qual]
6546
6547 If the name found is not a class-name (clause
6548 _class_) or namespace-name (_namespace.def_), the
6549 program is ill-formed. */
6550 success = true;
6551 }
6552 cp_lexer_consume_token (parser->lexer);
6553 }
6554 break;
6555 }
6556 /* We've found one valid nested-name-specifier. */
6557 success = true;
6558 /* Name lookup always gives us a DECL. */
6559 if (TREE_CODE (new_scope) == TYPE_DECL)
6560 new_scope = TREE_TYPE (new_scope);
6561 /* Uses of "template" must be followed by actual templates. */
6562 if (template_keyword_p
6563 && !(CLASS_TYPE_P (new_scope)
6564 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6565 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6566 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6567 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6568 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6569 == TEMPLATE_ID_EXPR)))
6570 permerror (input_location, TYPE_P (new_scope)
6571 ? G_("%qT is not a template")
6572 : G_("%qD is not a template"),
6573 new_scope);
6574 /* If it is a class scope, try to complete it; we are about to
6575 be looking up names inside the class. */
6576 if (TYPE_P (new_scope)
6577 /* Since checking types for dependency can be expensive,
6578 avoid doing it if the type is already complete. */
6579 && !COMPLETE_TYPE_P (new_scope)
6580 /* Do not try to complete dependent types. */
6581 && !dependent_type_p (new_scope))
6582 {
6583 new_scope = complete_type (new_scope);
6584 /* If it is a typedef to current class, use the current
6585 class instead, as the typedef won't have any names inside
6586 it yet. */
6587 if (!COMPLETE_TYPE_P (new_scope)
6588 && currently_open_class (new_scope))
6589 new_scope = TYPE_MAIN_VARIANT (new_scope);
6590 }
6591 /* Make sure we look in the right scope the next time through
6592 the loop. */
6593 parser->scope = new_scope;
6594 }
6595
6596 /* If parsing tentatively, replace the sequence of tokens that makes
6597 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6598 token. That way, should we re-parse the token stream, we will
6599 not have to repeat the effort required to do the parse, nor will
6600 we issue duplicate error messages. */
6601 if (success && start)
6602 {
6603 cp_token *token;
6604
6605 token = cp_lexer_token_at (parser->lexer, start);
6606 /* Reset the contents of the START token. */
6607 token->type = CPP_NESTED_NAME_SPECIFIER;
6608 /* Retrieve any deferred checks. Do not pop this access checks yet
6609 so the memory will not be reclaimed during token replacing below. */
6610 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6611 token->tree_check_p = true;
6612 token->u.tree_check_value->value = parser->scope;
6613 token->u.tree_check_value->checks = get_deferred_access_checks ();
6614 token->u.tree_check_value->qualifying_scope =
6615 parser->qualifying_scope;
6616 token->keyword = RID_MAX;
6617
6618 /* Purge all subsequent tokens. */
6619 cp_lexer_purge_tokens_after (parser->lexer, start);
6620 }
6621
6622 if (start)
6623 pop_to_parent_deferring_access_checks ();
6624
6625 return success ? parser->scope : NULL_TREE;
6626 }
6627
6628 /* Parse a nested-name-specifier. See
6629 cp_parser_nested_name_specifier_opt for details. This function
6630 behaves identically, except that it will an issue an error if no
6631 nested-name-specifier is present. */
6632
6633 static tree
6634 cp_parser_nested_name_specifier (cp_parser *parser,
6635 bool typename_keyword_p,
6636 bool check_dependency_p,
6637 bool type_p,
6638 bool is_declaration)
6639 {
6640 tree scope;
6641
6642 /* Look for the nested-name-specifier. */
6643 scope = cp_parser_nested_name_specifier_opt (parser,
6644 typename_keyword_p,
6645 check_dependency_p,
6646 type_p,
6647 is_declaration);
6648 /* If it was not present, issue an error message. */
6649 if (!scope)
6650 {
6651 cp_parser_error (parser, "expected nested-name-specifier");
6652 parser->scope = NULL_TREE;
6653 }
6654
6655 return scope;
6656 }
6657
6658 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6659 this is either a class-name or a namespace-name (which corresponds
6660 to the class-or-namespace-name production in the grammar). For
6661 C++0x, it can also be a type-name that refers to an enumeration
6662 type or a simple-template-id.
6663
6664 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6665 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6666 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6667 TYPE_P is TRUE iff the next name should be taken as a class-name,
6668 even the same name is declared to be another entity in the same
6669 scope.
6670
6671 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6672 specified by the class-or-namespace-name. If neither is found the
6673 ERROR_MARK_NODE is returned. */
6674
6675 static tree
6676 cp_parser_qualifying_entity (cp_parser *parser,
6677 bool typename_keyword_p,
6678 bool template_keyword_p,
6679 bool check_dependency_p,
6680 bool type_p,
6681 bool is_declaration)
6682 {
6683 tree saved_scope;
6684 tree saved_qualifying_scope;
6685 tree saved_object_scope;
6686 tree scope;
6687 bool only_class_p;
6688 bool successful_parse_p;
6689
6690 /* DR 743: decltype can appear in a nested-name-specifier. */
6691 if (cp_lexer_next_token_is_decltype (parser->lexer))
6692 {
6693 scope = cp_parser_decltype (parser);
6694 if (TREE_CODE (scope) != ENUMERAL_TYPE
6695 && !MAYBE_CLASS_TYPE_P (scope))
6696 {
6697 cp_parser_simulate_error (parser);
6698 return error_mark_node;
6699 }
6700 if (TYPE_NAME (scope))
6701 scope = TYPE_NAME (scope);
6702 return scope;
6703 }
6704
6705 /* Before we try to parse the class-name, we must save away the
6706 current PARSER->SCOPE since cp_parser_class_name will destroy
6707 it. */
6708 saved_scope = parser->scope;
6709 saved_qualifying_scope = parser->qualifying_scope;
6710 saved_object_scope = parser->object_scope;
6711 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6712 there is no need to look for a namespace-name. */
6713 only_class_p = template_keyword_p
6714 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6715 if (!only_class_p)
6716 cp_parser_parse_tentatively (parser);
6717 scope = cp_parser_class_name (parser,
6718 typename_keyword_p,
6719 template_keyword_p,
6720 type_p ? class_type : none_type,
6721 check_dependency_p,
6722 /*class_head_p=*/false,
6723 is_declaration,
6724 /*enum_ok=*/cxx_dialect > cxx98);
6725 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6726 /* If that didn't work, try for a namespace-name. */
6727 if (!only_class_p && !successful_parse_p)
6728 {
6729 /* Restore the saved scope. */
6730 parser->scope = saved_scope;
6731 parser->qualifying_scope = saved_qualifying_scope;
6732 parser->object_scope = saved_object_scope;
6733 /* If we are not looking at an identifier followed by the scope
6734 resolution operator, then this is not part of a
6735 nested-name-specifier. (Note that this function is only used
6736 to parse the components of a nested-name-specifier.) */
6737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6738 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6739 return error_mark_node;
6740 scope = cp_parser_namespace_name (parser);
6741 }
6742
6743 return scope;
6744 }
6745
6746 /* Return true if we are looking at a compound-literal, false otherwise. */
6747
6748 static bool
6749 cp_parser_compound_literal_p (cp_parser *parser)
6750 {
6751 cp_lexer_save_tokens (parser->lexer);
6752
6753 /* Skip tokens until the next token is a closing parenthesis.
6754 If we find the closing `)', and the next token is a `{', then
6755 we are looking at a compound-literal. */
6756 bool compound_literal_p
6757 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6758 /*consume_paren=*/true)
6759 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6760
6761 /* Roll back the tokens we skipped. */
6762 cp_lexer_rollback_tokens (parser->lexer);
6763
6764 return compound_literal_p;
6765 }
6766
6767 /* Return true if EXPR is the integer constant zero or a complex constant
6768 of zero, without any folding, but ignoring location wrappers. */
6769
6770 bool
6771 literal_integer_zerop (const_tree expr)
6772 {
6773 return (location_wrapper_p (expr)
6774 && integer_zerop (TREE_OPERAND (expr, 0)));
6775 }
6776
6777 /* Parse a postfix-expression.
6778
6779 postfix-expression:
6780 primary-expression
6781 postfix-expression [ expression ]
6782 postfix-expression ( expression-list [opt] )
6783 simple-type-specifier ( expression-list [opt] )
6784 typename :: [opt] nested-name-specifier identifier
6785 ( expression-list [opt] )
6786 typename :: [opt] nested-name-specifier template [opt] template-id
6787 ( expression-list [opt] )
6788 postfix-expression . template [opt] id-expression
6789 postfix-expression -> template [opt] id-expression
6790 postfix-expression . pseudo-destructor-name
6791 postfix-expression -> pseudo-destructor-name
6792 postfix-expression ++
6793 postfix-expression --
6794 dynamic_cast < type-id > ( expression )
6795 static_cast < type-id > ( expression )
6796 reinterpret_cast < type-id > ( expression )
6797 const_cast < type-id > ( expression )
6798 typeid ( expression )
6799 typeid ( type-id )
6800
6801 GNU Extension:
6802
6803 postfix-expression:
6804 ( type-id ) { initializer-list , [opt] }
6805
6806 This extension is a GNU version of the C99 compound-literal
6807 construct. (The C99 grammar uses `type-name' instead of `type-id',
6808 but they are essentially the same concept.)
6809
6810 If ADDRESS_P is true, the postfix expression is the operand of the
6811 `&' operator. CAST_P is true if this expression is the target of a
6812 cast.
6813
6814 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6815 class member access expressions [expr.ref].
6816
6817 Returns a representation of the expression. */
6818
6819 static cp_expr
6820 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6821 bool member_access_only_p, bool decltype_p,
6822 cp_id_kind * pidk_return)
6823 {
6824 cp_token *token;
6825 location_t loc;
6826 enum rid keyword;
6827 cp_id_kind idk = CP_ID_KIND_NONE;
6828 cp_expr postfix_expression = NULL_TREE;
6829 bool is_member_access = false;
6830
6831 /* Peek at the next token. */
6832 token = cp_lexer_peek_token (parser->lexer);
6833 loc = token->location;
6834 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6835
6836 /* Some of the productions are determined by keywords. */
6837 keyword = token->keyword;
6838 switch (keyword)
6839 {
6840 case RID_DYNCAST:
6841 case RID_STATCAST:
6842 case RID_REINTCAST:
6843 case RID_CONSTCAST:
6844 {
6845 tree type;
6846 cp_expr expression;
6847 const char *saved_message;
6848 bool saved_in_type_id_in_expr_p;
6849
6850 /* All of these can be handled in the same way from the point
6851 of view of parsing. Begin by consuming the token
6852 identifying the cast. */
6853 cp_lexer_consume_token (parser->lexer);
6854
6855 /* New types cannot be defined in the cast. */
6856 saved_message = parser->type_definition_forbidden_message;
6857 parser->type_definition_forbidden_message
6858 = G_("types may not be defined in casts");
6859
6860 /* Look for the opening `<'. */
6861 cp_parser_require (parser, CPP_LESS, RT_LESS);
6862 /* Parse the type to which we are casting. */
6863 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6864 parser->in_type_id_in_expr_p = true;
6865 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
6866 NULL);
6867 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6868 /* Look for the closing `>'. */
6869 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6870 /* Restore the old message. */
6871 parser->type_definition_forbidden_message = saved_message;
6872
6873 bool saved_greater_than_is_operator_p
6874 = parser->greater_than_is_operator_p;
6875 parser->greater_than_is_operator_p = true;
6876
6877 /* And the expression which is being cast. */
6878 matching_parens parens;
6879 parens.require_open (parser);
6880 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6881 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6882 RT_CLOSE_PAREN);
6883 location_t end_loc = close_paren ?
6884 close_paren->location : UNKNOWN_LOCATION;
6885
6886 parser->greater_than_is_operator_p
6887 = saved_greater_than_is_operator_p;
6888
6889 /* Only type conversions to integral or enumeration types
6890 can be used in constant-expressions. */
6891 if (!cast_valid_in_integral_constant_expression_p (type)
6892 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6893 {
6894 postfix_expression = error_mark_node;
6895 break;
6896 }
6897
6898 switch (keyword)
6899 {
6900 case RID_DYNCAST:
6901 postfix_expression
6902 = build_dynamic_cast (type, expression, tf_warning_or_error);
6903 break;
6904 case RID_STATCAST:
6905 postfix_expression
6906 = build_static_cast (type, expression, tf_warning_or_error);
6907 break;
6908 case RID_REINTCAST:
6909 postfix_expression
6910 = build_reinterpret_cast (type, expression,
6911 tf_warning_or_error);
6912 break;
6913 case RID_CONSTCAST:
6914 postfix_expression
6915 = build_const_cast (type, expression, tf_warning_or_error);
6916 break;
6917 default:
6918 gcc_unreachable ();
6919 }
6920
6921 /* Construct a location e.g. :
6922 reinterpret_cast <int *> (expr)
6923 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6924 ranging from the start of the "*_cast" token to the final closing
6925 paren, with the caret at the start. */
6926 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6927 postfix_expression.set_location (cp_cast_loc);
6928 }
6929 break;
6930
6931 case RID_TYPEID:
6932 {
6933 tree type;
6934 const char *saved_message;
6935 bool saved_in_type_id_in_expr_p;
6936
6937 /* Consume the `typeid' token. */
6938 cp_lexer_consume_token (parser->lexer);
6939 /* Look for the `(' token. */
6940 matching_parens parens;
6941 parens.require_open (parser);
6942 /* Types cannot be defined in a `typeid' expression. */
6943 saved_message = parser->type_definition_forbidden_message;
6944 parser->type_definition_forbidden_message
6945 = G_("types may not be defined in a %<typeid%> expression");
6946 /* We can't be sure yet whether we're looking at a type-id or an
6947 expression. */
6948 cp_parser_parse_tentatively (parser);
6949 /* Try a type-id first. */
6950 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6951 parser->in_type_id_in_expr_p = true;
6952 type = cp_parser_type_id (parser);
6953 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6954 /* Look for the `)' token. Otherwise, we can't be sure that
6955 we're not looking at an expression: consider `typeid (int
6956 (3))', for example. */
6957 cp_token *close_paren = parens.require_close (parser);
6958 /* If all went well, simply lookup the type-id. */
6959 if (cp_parser_parse_definitely (parser))
6960 postfix_expression = get_typeid (type, tf_warning_or_error);
6961 /* Otherwise, fall back to the expression variant. */
6962 else
6963 {
6964 tree expression;
6965
6966 /* Look for an expression. */
6967 expression = cp_parser_expression (parser, & idk);
6968 /* Compute its typeid. */
6969 postfix_expression = build_typeid (expression, tf_warning_or_error);
6970 /* Look for the `)' token. */
6971 close_paren = parens.require_close (parser);
6972 }
6973 /* Restore the saved message. */
6974 parser->type_definition_forbidden_message = saved_message;
6975 /* `typeid' may not appear in an integral constant expression. */
6976 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6977 postfix_expression = error_mark_node;
6978
6979 /* Construct a location e.g. :
6980 typeid (expr)
6981 ^~~~~~~~~~~~~
6982 ranging from the start of the "typeid" token to the final closing
6983 paren, with the caret at the start. */
6984 if (close_paren)
6985 {
6986 location_t typeid_loc
6987 = make_location (start_loc, start_loc, close_paren->location);
6988 postfix_expression.set_location (typeid_loc);
6989 postfix_expression.maybe_add_location_wrapper ();
6990 }
6991 }
6992 break;
6993
6994 case RID_TYPENAME:
6995 {
6996 tree type;
6997 /* The syntax permitted here is the same permitted for an
6998 elaborated-type-specifier. */
6999 ++parser->prevent_constrained_type_specifiers;
7000 type = cp_parser_elaborated_type_specifier (parser,
7001 /*is_friend=*/false,
7002 /*is_declaration=*/false);
7003 --parser->prevent_constrained_type_specifiers;
7004 postfix_expression = cp_parser_functional_cast (parser, type);
7005 }
7006 break;
7007
7008 case RID_ADDRESSOF:
7009 case RID_BUILTIN_SHUFFLE:
7010 case RID_BUILTIN_LAUNDER:
7011 {
7012 vec<tree, va_gc> *vec;
7013 unsigned int i;
7014 tree p;
7015
7016 cp_lexer_consume_token (parser->lexer);
7017 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7018 /*cast_p=*/false, /*allow_expansion_p=*/true,
7019 /*non_constant_p=*/NULL);
7020 if (vec == NULL)
7021 {
7022 postfix_expression = error_mark_node;
7023 break;
7024 }
7025
7026 FOR_EACH_VEC_ELT (*vec, i, p)
7027 mark_exp_read (p);
7028
7029 switch (keyword)
7030 {
7031 case RID_ADDRESSOF:
7032 if (vec->length () == 1)
7033 postfix_expression
7034 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7035 else
7036 {
7037 error_at (loc, "wrong number of arguments to "
7038 "%<__builtin_addressof%>");
7039 postfix_expression = error_mark_node;
7040 }
7041 break;
7042
7043 case RID_BUILTIN_LAUNDER:
7044 if (vec->length () == 1)
7045 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7046 tf_warning_or_error);
7047 else
7048 {
7049 error_at (loc, "wrong number of arguments to "
7050 "%<__builtin_launder%>");
7051 postfix_expression = error_mark_node;
7052 }
7053 break;
7054
7055 case RID_BUILTIN_SHUFFLE:
7056 if (vec->length () == 2)
7057 postfix_expression
7058 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7059 (*vec)[1], tf_warning_or_error);
7060 else if (vec->length () == 3)
7061 postfix_expression
7062 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7063 (*vec)[2], tf_warning_or_error);
7064 else
7065 {
7066 error_at (loc, "wrong number of arguments to "
7067 "%<__builtin_shuffle%>");
7068 postfix_expression = error_mark_node;
7069 }
7070 break;
7071
7072 default:
7073 gcc_unreachable ();
7074 }
7075 break;
7076 }
7077
7078 case RID_BUILTIN_CONVERTVECTOR:
7079 {
7080 tree expression;
7081 tree type;
7082 /* Consume the `__builtin_convertvector' token. */
7083 cp_lexer_consume_token (parser->lexer);
7084 /* Look for the opening `('. */
7085 matching_parens parens;
7086 parens.require_open (parser);
7087 /* Now, parse the assignment-expression. */
7088 expression = cp_parser_assignment_expression (parser);
7089 /* Look for the `,'. */
7090 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7091 location_t type_location
7092 = cp_lexer_peek_token (parser->lexer)->location;
7093 /* Parse the type-id. */
7094 {
7095 type_id_in_expr_sentinel s (parser);
7096 type = cp_parser_type_id (parser);
7097 }
7098 /* Look for the closing `)'. */
7099 parens.require_close (parser);
7100 return cp_build_vec_convert (expression, type_location, type,
7101 tf_warning_or_error);
7102 }
7103
7104 default:
7105 {
7106 tree type;
7107
7108 /* If the next thing is a simple-type-specifier, we may be
7109 looking at a functional cast. We could also be looking at
7110 an id-expression. So, we try the functional cast, and if
7111 that doesn't work we fall back to the primary-expression. */
7112 cp_parser_parse_tentatively (parser);
7113 /* Look for the simple-type-specifier. */
7114 ++parser->prevent_constrained_type_specifiers;
7115 type = cp_parser_simple_type_specifier (parser,
7116 /*decl_specs=*/NULL,
7117 CP_PARSER_FLAGS_NONE);
7118 --parser->prevent_constrained_type_specifiers;
7119 /* Parse the cast itself. */
7120 if (!cp_parser_error_occurred (parser))
7121 postfix_expression
7122 = cp_parser_functional_cast (parser, type);
7123 /* If that worked, we're done. */
7124 if (cp_parser_parse_definitely (parser))
7125 break;
7126
7127 /* If the functional-cast didn't work out, try a
7128 compound-literal. */
7129 if (cp_parser_allow_gnu_extensions_p (parser)
7130 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7131 {
7132 cp_expr initializer = NULL_TREE;
7133
7134 cp_parser_parse_tentatively (parser);
7135
7136 matching_parens parens;
7137 parens.consume_open (parser);
7138
7139 /* Avoid calling cp_parser_type_id pointlessly, see comment
7140 in cp_parser_cast_expression about c++/29234. */
7141 if (!cp_parser_compound_literal_p (parser))
7142 cp_parser_simulate_error (parser);
7143 else
7144 {
7145 /* Parse the type. */
7146 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7147 parser->in_type_id_in_expr_p = true;
7148 type = cp_parser_type_id (parser);
7149 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7150 parens.require_close (parser);
7151 }
7152
7153 /* If things aren't going well, there's no need to
7154 keep going. */
7155 if (!cp_parser_error_occurred (parser))
7156 {
7157 bool non_constant_p;
7158 /* Parse the brace-enclosed initializer list. */
7159 initializer = cp_parser_braced_list (parser,
7160 &non_constant_p);
7161 }
7162 /* If that worked, we're definitely looking at a
7163 compound-literal expression. */
7164 if (cp_parser_parse_definitely (parser))
7165 {
7166 /* Warn the user that a compound literal is not
7167 allowed in standard C++. */
7168 pedwarn (input_location, OPT_Wpedantic,
7169 "ISO C++ forbids compound-literals");
7170 /* For simplicity, we disallow compound literals in
7171 constant-expressions. We could
7172 allow compound literals of integer type, whose
7173 initializer was a constant, in constant
7174 expressions. Permitting that usage, as a further
7175 extension, would not change the meaning of any
7176 currently accepted programs. (Of course, as
7177 compound literals are not part of ISO C++, the
7178 standard has nothing to say.) */
7179 if (cp_parser_non_integral_constant_expression (parser,
7180 NIC_NCC))
7181 {
7182 postfix_expression = error_mark_node;
7183 break;
7184 }
7185 /* Form the representation of the compound-literal. */
7186 postfix_expression
7187 = finish_compound_literal (type, initializer,
7188 tf_warning_or_error, fcl_c99);
7189 postfix_expression.set_location (initializer.get_location ());
7190 break;
7191 }
7192 }
7193
7194 /* It must be a primary-expression. */
7195 postfix_expression
7196 = cp_parser_primary_expression (parser, address_p, cast_p,
7197 /*template_arg_p=*/false,
7198 decltype_p,
7199 &idk);
7200 }
7201 break;
7202 }
7203
7204 /* Note that we don't need to worry about calling build_cplus_new on a
7205 class-valued CALL_EXPR in decltype when it isn't the end of the
7206 postfix-expression; unary_complex_lvalue will take care of that for
7207 all these cases. */
7208
7209 /* Keep looping until the postfix-expression is complete. */
7210 while (true)
7211 {
7212 if (idk == CP_ID_KIND_UNQUALIFIED
7213 && identifier_p (postfix_expression)
7214 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7215 /* It is not a Koenig lookup function call. */
7216 postfix_expression
7217 = unqualified_name_lookup_error (postfix_expression);
7218
7219 /* Peek at the next token. */
7220 token = cp_lexer_peek_token (parser->lexer);
7221
7222 switch (token->type)
7223 {
7224 case CPP_OPEN_SQUARE:
7225 if (cp_next_tokens_can_be_std_attribute_p (parser))
7226 {
7227 cp_parser_error (parser,
7228 "two consecutive %<[%> shall "
7229 "only introduce an attribute");
7230 return error_mark_node;
7231 }
7232 postfix_expression
7233 = cp_parser_postfix_open_square_expression (parser,
7234 postfix_expression,
7235 false,
7236 decltype_p);
7237 postfix_expression.set_range (start_loc,
7238 postfix_expression.get_location ());
7239
7240 idk = CP_ID_KIND_NONE;
7241 is_member_access = false;
7242 break;
7243
7244 case CPP_OPEN_PAREN:
7245 /* postfix-expression ( expression-list [opt] ) */
7246 {
7247 bool koenig_p;
7248 bool is_builtin_constant_p;
7249 bool saved_integral_constant_expression_p = false;
7250 bool saved_non_integral_constant_expression_p = false;
7251 tsubst_flags_t complain = complain_flags (decltype_p);
7252 vec<tree, va_gc> *args;
7253 location_t close_paren_loc = UNKNOWN_LOCATION;
7254
7255 is_member_access = false;
7256
7257 tree stripped_expression
7258 = tree_strip_any_location_wrapper (postfix_expression);
7259 is_builtin_constant_p
7260 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7261 if (is_builtin_constant_p)
7262 {
7263 /* The whole point of __builtin_constant_p is to allow
7264 non-constant expressions to appear as arguments. */
7265 saved_integral_constant_expression_p
7266 = parser->integral_constant_expression_p;
7267 saved_non_integral_constant_expression_p
7268 = parser->non_integral_constant_expression_p;
7269 parser->integral_constant_expression_p = false;
7270 }
7271 args = (cp_parser_parenthesized_expression_list
7272 (parser, non_attr,
7273 /*cast_p=*/false, /*allow_expansion_p=*/true,
7274 /*non_constant_p=*/NULL,
7275 /*close_paren_loc=*/&close_paren_loc,
7276 /*wrap_locations_p=*/true));
7277 if (is_builtin_constant_p)
7278 {
7279 parser->integral_constant_expression_p
7280 = saved_integral_constant_expression_p;
7281 parser->non_integral_constant_expression_p
7282 = saved_non_integral_constant_expression_p;
7283 }
7284
7285 if (args == NULL)
7286 {
7287 postfix_expression = error_mark_node;
7288 break;
7289 }
7290
7291 /* Function calls are not permitted in
7292 constant-expressions. */
7293 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7294 && cp_parser_non_integral_constant_expression (parser,
7295 NIC_FUNC_CALL))
7296 {
7297 postfix_expression = error_mark_node;
7298 release_tree_vector (args);
7299 break;
7300 }
7301
7302 koenig_p = false;
7303 if (idk == CP_ID_KIND_UNQUALIFIED
7304 || idk == CP_ID_KIND_TEMPLATE_ID)
7305 {
7306 if (identifier_p (postfix_expression)
7307 /* In C++2A, we may need to perform ADL for a template
7308 name. */
7309 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7310 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7311 {
7312 if (!args->is_empty ())
7313 {
7314 koenig_p = true;
7315 if (!any_type_dependent_arguments_p (args))
7316 postfix_expression
7317 = perform_koenig_lookup (postfix_expression, args,
7318 complain);
7319 }
7320 else
7321 postfix_expression
7322 = unqualified_fn_lookup_error (postfix_expression);
7323 }
7324 /* We do not perform argument-dependent lookup if
7325 normal lookup finds a non-function, in accordance
7326 with the expected resolution of DR 218. */
7327 else if (!args->is_empty ()
7328 && is_overloaded_fn (postfix_expression))
7329 {
7330 /* We only need to look at the first function,
7331 because all the fns share the attribute we're
7332 concerned with (all member fns or all local
7333 fns). */
7334 tree fn = get_first_fn (postfix_expression);
7335 fn = STRIP_TEMPLATE (fn);
7336
7337 /* Do not do argument dependent lookup if regular
7338 lookup finds a member function or a block-scope
7339 function declaration. [basic.lookup.argdep]/3 */
7340 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7341 || DECL_FUNCTION_MEMBER_P (fn)
7342 || DECL_LOCAL_FUNCTION_P (fn)))
7343 {
7344 koenig_p = true;
7345 if (!any_type_dependent_arguments_p (args))
7346 postfix_expression
7347 = perform_koenig_lookup (postfix_expression, args,
7348 complain);
7349 }
7350 }
7351 }
7352
7353 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7354 {
7355 tree instance = TREE_OPERAND (postfix_expression, 0);
7356 tree fn = TREE_OPERAND (postfix_expression, 1);
7357
7358 if (processing_template_decl
7359 && (type_dependent_object_expression_p (instance)
7360 || (!BASELINK_P (fn)
7361 && TREE_CODE (fn) != FIELD_DECL)
7362 || type_dependent_expression_p (fn)
7363 || any_type_dependent_arguments_p (args)))
7364 {
7365 maybe_generic_this_capture (instance, fn);
7366 postfix_expression
7367 = build_min_nt_call_vec (postfix_expression, args);
7368 }
7369 else if (BASELINK_P (fn))
7370 {
7371 postfix_expression
7372 = (build_new_method_call
7373 (instance, fn, &args, NULL_TREE,
7374 (idk == CP_ID_KIND_QUALIFIED
7375 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7376 : LOOKUP_NORMAL),
7377 /*fn_p=*/NULL,
7378 complain));
7379 }
7380 else
7381 postfix_expression
7382 = finish_call_expr (postfix_expression, &args,
7383 /*disallow_virtual=*/false,
7384 /*koenig_p=*/false,
7385 complain);
7386 }
7387 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7388 || TREE_CODE (postfix_expression) == MEMBER_REF
7389 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7390 postfix_expression = (build_offset_ref_call_from_tree
7391 (postfix_expression, &args,
7392 complain));
7393 else if (idk == CP_ID_KIND_QUALIFIED)
7394 /* A call to a static class member, or a namespace-scope
7395 function. */
7396 postfix_expression
7397 = finish_call_expr (postfix_expression, &args,
7398 /*disallow_virtual=*/true,
7399 koenig_p,
7400 complain);
7401 else
7402 /* All other function calls. */
7403 postfix_expression
7404 = finish_call_expr (postfix_expression, &args,
7405 /*disallow_virtual=*/false,
7406 koenig_p,
7407 complain);
7408
7409 if (close_paren_loc != UNKNOWN_LOCATION)
7410 {
7411 location_t combined_loc = make_location (token->location,
7412 start_loc,
7413 close_paren_loc);
7414 postfix_expression.set_location (combined_loc);
7415 }
7416
7417 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7418 idk = CP_ID_KIND_NONE;
7419
7420 release_tree_vector (args);
7421 }
7422 break;
7423
7424 case CPP_DOT:
7425 case CPP_DEREF:
7426 /* postfix-expression . template [opt] id-expression
7427 postfix-expression . pseudo-destructor-name
7428 postfix-expression -> template [opt] id-expression
7429 postfix-expression -> pseudo-destructor-name */
7430
7431 /* Consume the `.' or `->' operator. */
7432 cp_lexer_consume_token (parser->lexer);
7433
7434 postfix_expression
7435 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7436 postfix_expression,
7437 false, &idk, loc);
7438
7439 is_member_access = true;
7440 break;
7441
7442 case CPP_PLUS_PLUS:
7443 /* postfix-expression ++ */
7444 /* Consume the `++' token. */
7445 cp_lexer_consume_token (parser->lexer);
7446 /* Generate a representation for the complete expression. */
7447 postfix_expression
7448 = finish_increment_expr (postfix_expression,
7449 POSTINCREMENT_EXPR);
7450 /* Increments may not appear in constant-expressions. */
7451 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7452 postfix_expression = error_mark_node;
7453 idk = CP_ID_KIND_NONE;
7454 is_member_access = false;
7455 break;
7456
7457 case CPP_MINUS_MINUS:
7458 /* postfix-expression -- */
7459 /* Consume the `--' token. */
7460 cp_lexer_consume_token (parser->lexer);
7461 /* Generate a representation for the complete expression. */
7462 postfix_expression
7463 = finish_increment_expr (postfix_expression,
7464 POSTDECREMENT_EXPR);
7465 /* Decrements may not appear in constant-expressions. */
7466 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7467 postfix_expression = error_mark_node;
7468 idk = CP_ID_KIND_NONE;
7469 is_member_access = false;
7470 break;
7471
7472 default:
7473 if (pidk_return != NULL)
7474 * pidk_return = idk;
7475 if (member_access_only_p)
7476 return is_member_access
7477 ? postfix_expression
7478 : cp_expr (error_mark_node);
7479 else
7480 return postfix_expression;
7481 }
7482 }
7483
7484 /* We should never get here. */
7485 gcc_unreachable ();
7486 return error_mark_node;
7487 }
7488
7489 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7490 by cp_parser_builtin_offsetof. We're looking for
7491
7492 postfix-expression [ expression ]
7493 postfix-expression [ braced-init-list ] (C++11)
7494
7495 FOR_OFFSETOF is set if we're being called in that context, which
7496 changes how we deal with integer constant expressions. */
7497
7498 static tree
7499 cp_parser_postfix_open_square_expression (cp_parser *parser,
7500 tree postfix_expression,
7501 bool for_offsetof,
7502 bool decltype_p)
7503 {
7504 tree index = NULL_TREE;
7505 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7506 bool saved_greater_than_is_operator_p;
7507
7508 /* Consume the `[' token. */
7509 cp_lexer_consume_token (parser->lexer);
7510
7511 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7512 parser->greater_than_is_operator_p = true;
7513
7514 /* Parse the index expression. */
7515 /* ??? For offsetof, there is a question of what to allow here. If
7516 offsetof is not being used in an integral constant expression context,
7517 then we *could* get the right answer by computing the value at runtime.
7518 If we are in an integral constant expression context, then we might
7519 could accept any constant expression; hard to say without analysis.
7520 Rather than open the barn door too wide right away, allow only integer
7521 constant expressions here. */
7522 if (for_offsetof)
7523 index = cp_parser_constant_expression (parser);
7524 else
7525 {
7526 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7527 {
7528 bool expr_nonconst_p;
7529 cp_lexer_set_source_position (parser->lexer);
7530 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7531 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7532 }
7533 else
7534 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
7535 /*decltype_p=*/false,
7536 /*warn_comma_p=*/warn_comma_subscript);
7537 }
7538
7539 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7540
7541 /* Look for the closing `]'. */
7542 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7543
7544 /* Build the ARRAY_REF. */
7545 postfix_expression = grok_array_decl (loc, postfix_expression,
7546 index, decltype_p);
7547
7548 /* When not doing offsetof, array references are not permitted in
7549 constant-expressions. */
7550 if (!for_offsetof
7551 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7552 postfix_expression = error_mark_node;
7553
7554 return postfix_expression;
7555 }
7556
7557 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7558 dereference of incomplete type, returns true if error_mark_node should
7559 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7560 and *DEPENDENT_P. */
7561
7562 bool
7563 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7564 bool *dependent_p)
7565 {
7566 /* In a template, be permissive by treating an object expression
7567 of incomplete type as dependent (after a pedwarn). */
7568 diagnostic_t kind = (processing_template_decl
7569 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7570
7571 switch (TREE_CODE (*postfix_expression))
7572 {
7573 case CAST_EXPR:
7574 case REINTERPRET_CAST_EXPR:
7575 case CONST_CAST_EXPR:
7576 case STATIC_CAST_EXPR:
7577 case DYNAMIC_CAST_EXPR:
7578 case IMPLICIT_CONV_EXPR:
7579 case VIEW_CONVERT_EXPR:
7580 case NON_LVALUE_EXPR:
7581 kind = DK_ERROR;
7582 break;
7583 case OVERLOAD:
7584 /* Don't emit any diagnostic for OVERLOADs. */
7585 kind = DK_IGNORED;
7586 break;
7587 default:
7588 /* Avoid clobbering e.g. DECLs. */
7589 if (!EXPR_P (*postfix_expression))
7590 kind = DK_ERROR;
7591 break;
7592 }
7593
7594 if (kind == DK_IGNORED)
7595 return false;
7596
7597 location_t exploc = location_of (*postfix_expression);
7598 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7599 if (!MAYBE_CLASS_TYPE_P (*scope))
7600 return true;
7601 if (kind == DK_ERROR)
7602 *scope = *postfix_expression = error_mark_node;
7603 else if (processing_template_decl)
7604 {
7605 *dependent_p = true;
7606 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7607 }
7608 return false;
7609 }
7610
7611 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7612 by cp_parser_builtin_offsetof. We're looking for
7613
7614 postfix-expression . template [opt] id-expression
7615 postfix-expression . pseudo-destructor-name
7616 postfix-expression -> template [opt] id-expression
7617 postfix-expression -> pseudo-destructor-name
7618
7619 FOR_OFFSETOF is set if we're being called in that context. That sorta
7620 limits what of the above we'll actually accept, but nevermind.
7621 TOKEN_TYPE is the "." or "->" token, which will already have been
7622 removed from the stream. */
7623
7624 static tree
7625 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7626 enum cpp_ttype token_type,
7627 cp_expr postfix_expression,
7628 bool for_offsetof, cp_id_kind *idk,
7629 location_t location)
7630 {
7631 tree name;
7632 bool dependent_p;
7633 bool pseudo_destructor_p;
7634 tree scope = NULL_TREE;
7635 location_t start_loc = postfix_expression.get_start ();
7636
7637 /* If this is a `->' operator, dereference the pointer. */
7638 if (token_type == CPP_DEREF)
7639 postfix_expression = build_x_arrow (location, postfix_expression,
7640 tf_warning_or_error);
7641 /* Check to see whether or not the expression is type-dependent and
7642 not the current instantiation. */
7643 dependent_p = type_dependent_object_expression_p (postfix_expression);
7644 /* The identifier following the `->' or `.' is not qualified. */
7645 parser->scope = NULL_TREE;
7646 parser->qualifying_scope = NULL_TREE;
7647 parser->object_scope = NULL_TREE;
7648 *idk = CP_ID_KIND_NONE;
7649
7650 /* Enter the scope corresponding to the type of the object
7651 given by the POSTFIX_EXPRESSION. */
7652 if (!dependent_p)
7653 {
7654 scope = TREE_TYPE (postfix_expression);
7655 /* According to the standard, no expression should ever have
7656 reference type. Unfortunately, we do not currently match
7657 the standard in this respect in that our internal representation
7658 of an expression may have reference type even when the standard
7659 says it does not. Therefore, we have to manually obtain the
7660 underlying type here. */
7661 scope = non_reference (scope);
7662 /* The type of the POSTFIX_EXPRESSION must be complete. */
7663 /* Unlike the object expression in other contexts, *this is not
7664 required to be of complete type for purposes of class member
7665 access (5.2.5) outside the member function body. */
7666 if (postfix_expression != current_class_ref
7667 && scope != error_mark_node
7668 && !currently_open_class (scope))
7669 {
7670 scope = complete_type (scope);
7671 if (!COMPLETE_TYPE_P (scope)
7672 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7673 &dependent_p))
7674 return error_mark_node;
7675 }
7676
7677 if (!dependent_p)
7678 {
7679 /* Let the name lookup machinery know that we are processing a
7680 class member access expression. */
7681 parser->context->object_type = scope;
7682 /* If something went wrong, we want to be able to discern that case,
7683 as opposed to the case where there was no SCOPE due to the type
7684 of expression being dependent. */
7685 if (!scope)
7686 scope = error_mark_node;
7687 /* If the SCOPE was erroneous, make the various semantic analysis
7688 functions exit quickly -- and without issuing additional error
7689 messages. */
7690 if (scope == error_mark_node)
7691 postfix_expression = error_mark_node;
7692 }
7693 }
7694
7695 if (dependent_p)
7696 /* Tell cp_parser_lookup_name that there was an object, even though it's
7697 type-dependent. */
7698 parser->context->object_type = unknown_type_node;
7699
7700 /* Assume this expression is not a pseudo-destructor access. */
7701 pseudo_destructor_p = false;
7702
7703 /* If the SCOPE is a scalar type, then, if this is a valid program,
7704 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7705 is type dependent, it can be pseudo-destructor-name or something else.
7706 Try to parse it as pseudo-destructor-name first. */
7707 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7708 {
7709 tree s;
7710 tree type;
7711
7712 cp_parser_parse_tentatively (parser);
7713 /* Parse the pseudo-destructor-name. */
7714 s = NULL_TREE;
7715 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7716 &s, &type);
7717 if (dependent_p
7718 && (cp_parser_error_occurred (parser)
7719 || !SCALAR_TYPE_P (type)))
7720 cp_parser_abort_tentative_parse (parser);
7721 else if (cp_parser_parse_definitely (parser))
7722 {
7723 pseudo_destructor_p = true;
7724 postfix_expression
7725 = finish_pseudo_destructor_expr (postfix_expression,
7726 s, type, location);
7727 }
7728 }
7729
7730 if (!pseudo_destructor_p)
7731 {
7732 /* If the SCOPE is not a scalar type, we are looking at an
7733 ordinary class member access expression, rather than a
7734 pseudo-destructor-name. */
7735 bool template_p;
7736 cp_token *token = cp_lexer_peek_token (parser->lexer);
7737 /* Parse the id-expression. */
7738 name = (cp_parser_id_expression
7739 (parser,
7740 cp_parser_optional_template_keyword (parser),
7741 /*check_dependency_p=*/true,
7742 &template_p,
7743 /*declarator_p=*/false,
7744 /*optional_p=*/false));
7745 /* In general, build a SCOPE_REF if the member name is qualified.
7746 However, if the name was not dependent and has already been
7747 resolved; there is no need to build the SCOPE_REF. For example;
7748
7749 struct X { void f(); };
7750 template <typename T> void f(T* t) { t->X::f(); }
7751
7752 Even though "t" is dependent, "X::f" is not and has been resolved
7753 to a BASELINK; there is no need to include scope information. */
7754
7755 /* But we do need to remember that there was an explicit scope for
7756 virtual function calls. */
7757 if (parser->scope)
7758 *idk = CP_ID_KIND_QUALIFIED;
7759
7760 /* If the name is a template-id that names a type, we will get a
7761 TYPE_DECL here. That is invalid code. */
7762 if (TREE_CODE (name) == TYPE_DECL)
7763 {
7764 error_at (token->location, "invalid use of %qD", name);
7765 postfix_expression = error_mark_node;
7766 }
7767 else
7768 {
7769 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7770 {
7771 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7772 {
7773 error_at (token->location, "%<%D::%D%> is not a class member",
7774 parser->scope, name);
7775 postfix_expression = error_mark_node;
7776 }
7777 else
7778 name = build_qualified_name (/*type=*/NULL_TREE,
7779 parser->scope,
7780 name,
7781 template_p);
7782 parser->scope = NULL_TREE;
7783 parser->qualifying_scope = NULL_TREE;
7784 parser->object_scope = NULL_TREE;
7785 }
7786 if (parser->scope && name && BASELINK_P (name))
7787 adjust_result_of_qualified_name_lookup
7788 (name, parser->scope, scope);
7789 postfix_expression
7790 = finish_class_member_access_expr (postfix_expression, name,
7791 template_p,
7792 tf_warning_or_error);
7793 /* Build a location e.g.:
7794 ptr->access_expr
7795 ~~~^~~~~~~~~~~~~
7796 where the caret is at the deref token, ranging from
7797 the start of postfix_expression to the end of the access expr. */
7798 location_t combined_loc
7799 = make_location (input_location, start_loc, parser->lexer);
7800 protected_set_expr_location (postfix_expression, combined_loc);
7801 }
7802 }
7803
7804 /* We no longer need to look up names in the scope of the object on
7805 the left-hand side of the `.' or `->' operator. */
7806 parser->context->object_type = NULL_TREE;
7807
7808 /* Outside of offsetof, these operators may not appear in
7809 constant-expressions. */
7810 if (!for_offsetof
7811 && (cp_parser_non_integral_constant_expression
7812 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7813 postfix_expression = error_mark_node;
7814
7815 return postfix_expression;
7816 }
7817
7818 /* Parse a parenthesized expression-list.
7819
7820 expression-list:
7821 assignment-expression
7822 expression-list, assignment-expression
7823
7824 attribute-list:
7825 expression-list
7826 identifier
7827 identifier, expression-list
7828
7829 CAST_P is true if this expression is the target of a cast.
7830
7831 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7832 argument pack.
7833
7834 WRAP_LOCATIONS_P is true if expressions within this list for which
7835 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7836 their source locations.
7837
7838 Returns a vector of trees. Each element is a representation of an
7839 assignment-expression. NULL is returned if the ( and or ) are
7840 missing. An empty, but allocated, vector is returned on no
7841 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7842 if we are parsing an attribute list for an attribute that wants a
7843 plain identifier argument, normal_attr for an attribute that wants
7844 an expression, or non_attr if we aren't parsing an attribute list. If
7845 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7846 not all of the expressions in the list were constant.
7847 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7848 will be written to with the location of the closing parenthesis. If
7849 an error occurs, it may or may not be written to. */
7850
7851 static vec<tree, va_gc> *
7852 cp_parser_parenthesized_expression_list (cp_parser* parser,
7853 int is_attribute_list,
7854 bool cast_p,
7855 bool allow_expansion_p,
7856 bool *non_constant_p,
7857 location_t *close_paren_loc,
7858 bool wrap_locations_p)
7859 {
7860 vec<tree, va_gc> *expression_list;
7861 bool fold_expr_p = is_attribute_list != non_attr;
7862 tree identifier = NULL_TREE;
7863 bool saved_greater_than_is_operator_p;
7864
7865 /* Assume all the expressions will be constant. */
7866 if (non_constant_p)
7867 *non_constant_p = false;
7868
7869 matching_parens parens;
7870 if (!parens.require_open (parser))
7871 return NULL;
7872
7873 expression_list = make_tree_vector ();
7874
7875 /* Within a parenthesized expression, a `>' token is always
7876 the greater-than operator. */
7877 saved_greater_than_is_operator_p
7878 = parser->greater_than_is_operator_p;
7879 parser->greater_than_is_operator_p = true;
7880
7881 cp_expr expr (NULL_TREE);
7882
7883 /* Consume expressions until there are no more. */
7884 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7885 while (true)
7886 {
7887 /* At the beginning of attribute lists, check to see if the
7888 next token is an identifier. */
7889 if (is_attribute_list == id_attr
7890 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7891 {
7892 cp_token *token;
7893
7894 /* Consume the identifier. */
7895 token = cp_lexer_consume_token (parser->lexer);
7896 /* Save the identifier. */
7897 identifier = token->u.value;
7898 }
7899 else
7900 {
7901 bool expr_non_constant_p;
7902
7903 /* Parse the next assignment-expression. */
7904 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7905 {
7906 /* A braced-init-list. */
7907 cp_lexer_set_source_position (parser->lexer);
7908 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7909 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7910 if (non_constant_p && expr_non_constant_p)
7911 *non_constant_p = true;
7912 }
7913 else if (non_constant_p)
7914 {
7915 expr = (cp_parser_constant_expression
7916 (parser, /*allow_non_constant_p=*/true,
7917 &expr_non_constant_p));
7918 if (expr_non_constant_p)
7919 *non_constant_p = true;
7920 }
7921 else
7922 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7923 cast_p);
7924
7925 if (fold_expr_p)
7926 expr = instantiate_non_dependent_expr (expr);
7927
7928 /* If we have an ellipsis, then this is an expression
7929 expansion. */
7930 if (allow_expansion_p
7931 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7932 {
7933 /* Consume the `...'. */
7934 cp_lexer_consume_token (parser->lexer);
7935
7936 /* Build the argument pack. */
7937 expr = make_pack_expansion (expr);
7938 }
7939
7940 if (wrap_locations_p)
7941 expr.maybe_add_location_wrapper ();
7942
7943 /* Add it to the list. We add error_mark_node
7944 expressions to the list, so that we can still tell if
7945 the correct form for a parenthesized expression-list
7946 is found. That gives better errors. */
7947 vec_safe_push (expression_list, expr.get_value ());
7948
7949 if (expr == error_mark_node)
7950 goto skip_comma;
7951 }
7952
7953 /* After the first item, attribute lists look the same as
7954 expression lists. */
7955 is_attribute_list = non_attr;
7956
7957 get_comma:;
7958 /* If the next token isn't a `,', then we are done. */
7959 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7960 break;
7961
7962 /* Otherwise, consume the `,' and keep going. */
7963 cp_lexer_consume_token (parser->lexer);
7964 }
7965
7966 if (close_paren_loc)
7967 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7968
7969 if (!parens.require_close (parser))
7970 {
7971 int ending;
7972
7973 skip_comma:;
7974 /* We try and resync to an unnested comma, as that will give the
7975 user better diagnostics. */
7976 ending = cp_parser_skip_to_closing_parenthesis (parser,
7977 /*recovering=*/true,
7978 /*or_comma=*/true,
7979 /*consume_paren=*/true);
7980 if (ending < 0)
7981 goto get_comma;
7982 if (!ending)
7983 {
7984 parser->greater_than_is_operator_p
7985 = saved_greater_than_is_operator_p;
7986 return NULL;
7987 }
7988 }
7989
7990 parser->greater_than_is_operator_p
7991 = saved_greater_than_is_operator_p;
7992
7993 if (identifier)
7994 vec_safe_insert (expression_list, 0, identifier);
7995
7996 return expression_list;
7997 }
7998
7999 /* Parse a pseudo-destructor-name.
8000
8001 pseudo-destructor-name:
8002 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8003 :: [opt] nested-name-specifier template template-id :: ~ type-name
8004 :: [opt] nested-name-specifier [opt] ~ type-name
8005
8006 If either of the first two productions is used, sets *SCOPE to the
8007 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8008 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8009 or ERROR_MARK_NODE if the parse fails. */
8010
8011 static void
8012 cp_parser_pseudo_destructor_name (cp_parser* parser,
8013 tree object,
8014 tree* scope,
8015 tree* type)
8016 {
8017 bool nested_name_specifier_p;
8018
8019 /* Handle ~auto. */
8020 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8021 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8022 && !type_dependent_expression_p (object))
8023 {
8024 if (cxx_dialect < cxx14)
8025 pedwarn (input_location, 0,
8026 "%<~auto%> only available with "
8027 "%<-std=c++14%> or %<-std=gnu++14%>");
8028 cp_lexer_consume_token (parser->lexer);
8029 cp_lexer_consume_token (parser->lexer);
8030 *scope = NULL_TREE;
8031 *type = TREE_TYPE (object);
8032 return;
8033 }
8034
8035 /* Assume that things will not work out. */
8036 *type = error_mark_node;
8037
8038 /* Look for the optional `::' operator. */
8039 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8040 /* Look for the optional nested-name-specifier. */
8041 nested_name_specifier_p
8042 = (cp_parser_nested_name_specifier_opt (parser,
8043 /*typename_keyword_p=*/false,
8044 /*check_dependency_p=*/true,
8045 /*type_p=*/false,
8046 /*is_declaration=*/false)
8047 != NULL_TREE);
8048 /* Now, if we saw a nested-name-specifier, we might be doing the
8049 second production. */
8050 if (nested_name_specifier_p
8051 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8052 {
8053 /* Consume the `template' keyword. */
8054 cp_lexer_consume_token (parser->lexer);
8055 /* Parse the template-id. */
8056 cp_parser_template_id (parser,
8057 /*template_keyword_p=*/true,
8058 /*check_dependency_p=*/false,
8059 class_type,
8060 /*is_declaration=*/true);
8061 /* Look for the `::' token. */
8062 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8063 }
8064 /* If the next token is not a `~', then there might be some
8065 additional qualification. */
8066 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8067 {
8068 /* At this point, we're looking for "type-name :: ~". The type-name
8069 must not be a class-name, since this is a pseudo-destructor. So,
8070 it must be either an enum-name, or a typedef-name -- both of which
8071 are just identifiers. So, we peek ahead to check that the "::"
8072 and "~" tokens are present; if they are not, then we can avoid
8073 calling type_name. */
8074 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8075 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8076 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8077 {
8078 cp_parser_error (parser, "non-scalar type");
8079 return;
8080 }
8081
8082 /* Look for the type-name. */
8083 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8084 if (*scope == error_mark_node)
8085 return;
8086
8087 /* Look for the `::' token. */
8088 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8089 }
8090 else
8091 *scope = NULL_TREE;
8092
8093 /* Look for the `~'. */
8094 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8095
8096 /* Once we see the ~, this has to be a pseudo-destructor. */
8097 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8098 cp_parser_commit_to_topmost_tentative_parse (parser);
8099
8100 /* Look for the type-name again. We are not responsible for
8101 checking that it matches the first type-name. */
8102 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8103 }
8104
8105 /* Parse a unary-expression.
8106
8107 unary-expression:
8108 postfix-expression
8109 ++ cast-expression
8110 -- cast-expression
8111 unary-operator cast-expression
8112 sizeof unary-expression
8113 sizeof ( type-id )
8114 alignof ( type-id ) [C++0x]
8115 new-expression
8116 delete-expression
8117
8118 GNU Extensions:
8119
8120 unary-expression:
8121 __extension__ cast-expression
8122 __alignof__ unary-expression
8123 __alignof__ ( type-id )
8124 alignof unary-expression [C++0x]
8125 __real__ cast-expression
8126 __imag__ cast-expression
8127 && identifier
8128 sizeof ( type-id ) { initializer-list , [opt] }
8129 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8130 __alignof__ ( type-id ) { initializer-list , [opt] }
8131
8132 ADDRESS_P is true iff the unary-expression is appearing as the
8133 operand of the `&' operator. CAST_P is true if this expression is
8134 the target of a cast.
8135
8136 Returns a representation of the expression. */
8137
8138 static cp_expr
8139 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8140 bool address_p, bool cast_p, bool decltype_p)
8141 {
8142 cp_token *token;
8143 enum tree_code unary_operator;
8144
8145 /* Peek at the next token. */
8146 token = cp_lexer_peek_token (parser->lexer);
8147 /* Some keywords give away the kind of expression. */
8148 if (token->type == CPP_KEYWORD)
8149 {
8150 enum rid keyword = token->keyword;
8151
8152 switch (keyword)
8153 {
8154 case RID_ALIGNOF:
8155 case RID_SIZEOF:
8156 {
8157 tree operand, ret;
8158 enum tree_code op;
8159 location_t start_loc = token->location;
8160
8161 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8162 bool std_alignof = id_equal (token->u.value, "alignof");
8163
8164 /* Consume the token. */
8165 cp_lexer_consume_token (parser->lexer);
8166 /* Parse the operand. */
8167 operand = cp_parser_sizeof_operand (parser, keyword);
8168
8169 if (TYPE_P (operand))
8170 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8171 true);
8172 else
8173 {
8174 /* ISO C++ defines alignof only with types, not with
8175 expressions. So pedwarn if alignof is used with a non-
8176 type expression. However, __alignof__ is ok. */
8177 if (std_alignof)
8178 pedwarn (token->location, OPT_Wpedantic,
8179 "ISO C++ does not allow %<alignof%> "
8180 "with a non-type");
8181
8182 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8183 }
8184 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8185 SIZEOF_EXPR with the original operand. */
8186 if (op == SIZEOF_EXPR && ret != error_mark_node)
8187 {
8188 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8189 {
8190 if (!processing_template_decl && TYPE_P (operand))
8191 {
8192 ret = build_min (SIZEOF_EXPR, size_type_node,
8193 build1 (NOP_EXPR, operand,
8194 error_mark_node));
8195 SIZEOF_EXPR_TYPE_P (ret) = 1;
8196 }
8197 else
8198 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8199 TREE_SIDE_EFFECTS (ret) = 0;
8200 TREE_READONLY (ret) = 1;
8201 }
8202 }
8203
8204 /* Construct a location e.g. :
8205 alignof (expr)
8206 ^~~~~~~~~~~~~~
8207 with start == caret at the start of the "alignof"/"sizeof"
8208 token, with the endpoint at the final closing paren. */
8209 location_t compound_loc
8210 = make_location (start_loc, start_loc, parser->lexer);
8211
8212 cp_expr ret_expr (ret);
8213 ret_expr.set_location (compound_loc);
8214 ret_expr = ret_expr.maybe_add_location_wrapper ();
8215 return ret_expr;
8216 }
8217
8218 case RID_BUILTIN_HAS_ATTRIBUTE:
8219 return cp_parser_has_attribute_expression (parser);
8220
8221 case RID_NEW:
8222 return cp_parser_new_expression (parser);
8223
8224 case RID_DELETE:
8225 return cp_parser_delete_expression (parser);
8226
8227 case RID_EXTENSION:
8228 {
8229 /* The saved value of the PEDANTIC flag. */
8230 int saved_pedantic;
8231 tree expr;
8232
8233 /* Save away the PEDANTIC flag. */
8234 cp_parser_extension_opt (parser, &saved_pedantic);
8235 /* Parse the cast-expression. */
8236 expr = cp_parser_simple_cast_expression (parser);
8237 /* Restore the PEDANTIC flag. */
8238 pedantic = saved_pedantic;
8239
8240 return expr;
8241 }
8242
8243 case RID_REALPART:
8244 case RID_IMAGPART:
8245 {
8246 tree expression;
8247
8248 /* Consume the `__real__' or `__imag__' token. */
8249 cp_lexer_consume_token (parser->lexer);
8250 /* Parse the cast-expression. */
8251 expression = cp_parser_simple_cast_expression (parser);
8252 /* Create the complete representation. */
8253 return build_x_unary_op (token->location,
8254 (keyword == RID_REALPART
8255 ? REALPART_EXPR : IMAGPART_EXPR),
8256 expression,
8257 tf_warning_or_error);
8258 }
8259 break;
8260
8261 case RID_TRANSACTION_ATOMIC:
8262 case RID_TRANSACTION_RELAXED:
8263 return cp_parser_transaction_expression (parser, keyword);
8264
8265 case RID_NOEXCEPT:
8266 {
8267 tree expr;
8268 const char *saved_message;
8269 bool saved_integral_constant_expression_p;
8270 bool saved_non_integral_constant_expression_p;
8271 bool saved_greater_than_is_operator_p;
8272
8273 location_t start_loc = token->location;
8274
8275 cp_lexer_consume_token (parser->lexer);
8276 matching_parens parens;
8277 parens.require_open (parser);
8278
8279 saved_message = parser->type_definition_forbidden_message;
8280 parser->type_definition_forbidden_message
8281 = G_("types may not be defined in %<noexcept%> expressions");
8282
8283 saved_integral_constant_expression_p
8284 = parser->integral_constant_expression_p;
8285 saved_non_integral_constant_expression_p
8286 = parser->non_integral_constant_expression_p;
8287 parser->integral_constant_expression_p = false;
8288
8289 saved_greater_than_is_operator_p
8290 = parser->greater_than_is_operator_p;
8291 parser->greater_than_is_operator_p = true;
8292
8293 ++cp_unevaluated_operand;
8294 ++c_inhibit_evaluation_warnings;
8295 ++cp_noexcept_operand;
8296 expr = cp_parser_expression (parser);
8297 --cp_noexcept_operand;
8298 --c_inhibit_evaluation_warnings;
8299 --cp_unevaluated_operand;
8300
8301 parser->greater_than_is_operator_p
8302 = saved_greater_than_is_operator_p;
8303
8304 parser->integral_constant_expression_p
8305 = saved_integral_constant_expression_p;
8306 parser->non_integral_constant_expression_p
8307 = saved_non_integral_constant_expression_p;
8308
8309 parser->type_definition_forbidden_message = saved_message;
8310
8311 parens.require_close (parser);
8312
8313 /* Construct a location of the form:
8314 noexcept (expr)
8315 ^~~~~~~~~~~~~~~
8316 with start == caret, finishing at the close-paren. */
8317 location_t noexcept_loc
8318 = make_location (start_loc, start_loc, parser->lexer);
8319
8320 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8321 noexcept_loc);
8322 }
8323
8324 default:
8325 break;
8326 }
8327 }
8328
8329 /* Look for the `:: new' and `:: delete', which also signal the
8330 beginning of a new-expression, or delete-expression,
8331 respectively. If the next token is `::', then it might be one of
8332 these. */
8333 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8334 {
8335 enum rid keyword;
8336
8337 /* See if the token after the `::' is one of the keywords in
8338 which we're interested. */
8339 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8340 /* If it's `new', we have a new-expression. */
8341 if (keyword == RID_NEW)
8342 return cp_parser_new_expression (parser);
8343 /* Similarly, for `delete'. */
8344 else if (keyword == RID_DELETE)
8345 return cp_parser_delete_expression (parser);
8346 }
8347
8348 /* Look for a unary operator. */
8349 unary_operator = cp_parser_unary_operator (token);
8350 /* The `++' and `--' operators can be handled similarly, even though
8351 they are not technically unary-operators in the grammar. */
8352 if (unary_operator == ERROR_MARK)
8353 {
8354 if (token->type == CPP_PLUS_PLUS)
8355 unary_operator = PREINCREMENT_EXPR;
8356 else if (token->type == CPP_MINUS_MINUS)
8357 unary_operator = PREDECREMENT_EXPR;
8358 /* Handle the GNU address-of-label extension. */
8359 else if (cp_parser_allow_gnu_extensions_p (parser)
8360 && token->type == CPP_AND_AND)
8361 {
8362 tree identifier;
8363 tree expression;
8364 location_t start_loc = token->location;
8365
8366 /* Consume the '&&' token. */
8367 cp_lexer_consume_token (parser->lexer);
8368 /* Look for the identifier. */
8369 identifier = cp_parser_identifier (parser);
8370 /* Construct a location of the form:
8371 &&label
8372 ^~~~~~~
8373 with caret==start at the "&&", finish at the end of the label. */
8374 location_t combined_loc
8375 = make_location (start_loc, start_loc, parser->lexer);
8376 /* Create an expression representing the address. */
8377 expression = finish_label_address_expr (identifier, combined_loc);
8378 if (cp_parser_non_integral_constant_expression (parser,
8379 NIC_ADDR_LABEL))
8380 expression = error_mark_node;
8381 return expression;
8382 }
8383 }
8384 if (unary_operator != ERROR_MARK)
8385 {
8386 cp_expr cast_expression;
8387 cp_expr expression = error_mark_node;
8388 non_integral_constant non_constant_p = NIC_NONE;
8389 location_t loc = token->location;
8390 tsubst_flags_t complain = complain_flags (decltype_p);
8391
8392 /* Consume the operator token. */
8393 token = cp_lexer_consume_token (parser->lexer);
8394 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8395
8396 /* Parse the cast-expression. */
8397 cast_expression
8398 = cp_parser_cast_expression (parser,
8399 unary_operator == ADDR_EXPR,
8400 /*cast_p=*/false,
8401 /*decltype*/false,
8402 pidk);
8403
8404 /* Make a location:
8405 OP_TOKEN CAST_EXPRESSION
8406 ^~~~~~~~~~~~~~~~~~~~~~~~~
8407 with start==caret at the operator token, and
8408 extending to the end of the cast_expression. */
8409 loc = make_location (loc, loc, cast_expression.get_finish ());
8410
8411 /* Now, build an appropriate representation. */
8412 switch (unary_operator)
8413 {
8414 case INDIRECT_REF:
8415 non_constant_p = NIC_STAR;
8416 expression = build_x_indirect_ref (loc, cast_expression,
8417 RO_UNARY_STAR,
8418 complain);
8419 /* TODO: build_x_indirect_ref does not always honor the
8420 location, so ensure it is set. */
8421 expression.set_location (loc);
8422 break;
8423
8424 case ADDR_EXPR:
8425 non_constant_p = NIC_ADDR;
8426 /* Fall through. */
8427 case BIT_NOT_EXPR:
8428 expression = build_x_unary_op (loc, unary_operator,
8429 cast_expression,
8430 complain);
8431 /* TODO: build_x_unary_op does not always honor the location,
8432 so ensure it is set. */
8433 expression.set_location (loc);
8434 break;
8435
8436 case PREINCREMENT_EXPR:
8437 case PREDECREMENT_EXPR:
8438 non_constant_p = unary_operator == PREINCREMENT_EXPR
8439 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8440 /* Fall through. */
8441 case NEGATE_EXPR:
8442 /* Immediately fold negation of a constant, unless the constant is 0
8443 (since -0 == 0) or it would overflow. */
8444 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8445 {
8446 tree stripped_expr
8447 = tree_strip_any_location_wrapper (cast_expression);
8448 if (CONSTANT_CLASS_P (stripped_expr)
8449 && !integer_zerop (stripped_expr)
8450 && !TREE_OVERFLOW (stripped_expr))
8451 {
8452 tree folded = fold_build1 (unary_operator,
8453 TREE_TYPE (stripped_expr),
8454 stripped_expr);
8455 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8456 {
8457 expression = maybe_wrap_with_location (folded, loc);
8458 break;
8459 }
8460 }
8461 }
8462 /* Fall through. */
8463 case UNARY_PLUS_EXPR:
8464 case TRUTH_NOT_EXPR:
8465 expression = finish_unary_op_expr (loc, unary_operator,
8466 cast_expression, complain);
8467 break;
8468
8469 default:
8470 gcc_unreachable ();
8471 }
8472
8473 if (non_constant_p != NIC_NONE
8474 && cp_parser_non_integral_constant_expression (parser,
8475 non_constant_p))
8476 expression = error_mark_node;
8477
8478 return expression;
8479 }
8480
8481 return cp_parser_postfix_expression (parser, address_p, cast_p,
8482 /*member_access_only_p=*/false,
8483 decltype_p,
8484 pidk);
8485 }
8486
8487 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8488 unary-operator, the corresponding tree code is returned. */
8489
8490 static enum tree_code
8491 cp_parser_unary_operator (cp_token* token)
8492 {
8493 switch (token->type)
8494 {
8495 case CPP_MULT:
8496 return INDIRECT_REF;
8497
8498 case CPP_AND:
8499 return ADDR_EXPR;
8500
8501 case CPP_PLUS:
8502 return UNARY_PLUS_EXPR;
8503
8504 case CPP_MINUS:
8505 return NEGATE_EXPR;
8506
8507 case CPP_NOT:
8508 return TRUTH_NOT_EXPR;
8509
8510 case CPP_COMPL:
8511 return BIT_NOT_EXPR;
8512
8513 default:
8514 return ERROR_MARK;
8515 }
8516 }
8517
8518 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8519 Returns a representation of the expression. */
8520
8521 static tree
8522 cp_parser_has_attribute_expression (cp_parser *parser)
8523 {
8524 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8525
8526 /* Consume the __builtin_has_attribute token. */
8527 cp_lexer_consume_token (parser->lexer);
8528
8529 matching_parens parens;
8530 if (!parens.require_open (parser))
8531 return error_mark_node;
8532
8533 /* Types cannot be defined in a `sizeof' expression. Save away the
8534 old message. */
8535 const char *saved_message = parser->type_definition_forbidden_message;
8536 const char *saved_message_arg
8537 = parser->type_definition_forbidden_message_arg;
8538 parser->type_definition_forbidden_message
8539 = G_("types may not be defined in %qs expressions");
8540 parser->type_definition_forbidden_message_arg
8541 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
8542
8543 /* The restrictions on constant-expressions do not apply inside
8544 sizeof expressions. */
8545 bool saved_integral_constant_expression_p
8546 = parser->integral_constant_expression_p;
8547 bool saved_non_integral_constant_expression_p
8548 = parser->non_integral_constant_expression_p;
8549 parser->integral_constant_expression_p = false;
8550
8551 /* Do not actually evaluate the expression. */
8552 ++cp_unevaluated_operand;
8553 ++c_inhibit_evaluation_warnings;
8554
8555 tree oper = NULL_TREE;
8556
8557 /* We can't be sure yet whether we're looking at a type-id or an
8558 expression. */
8559 cp_parser_parse_tentatively (parser);
8560
8561 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8562 parser->in_type_id_in_expr_p = true;
8563 /* Look for the type-id. */
8564 oper = cp_parser_type_id (parser);
8565 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8566
8567 cp_parser_parse_definitely (parser);
8568
8569 /* If the type-id production did not work out, then we must be
8570 looking at an expression. */
8571 if (!oper || oper == error_mark_node)
8572 oper = cp_parser_assignment_expression (parser);
8573
8574 STRIP_ANY_LOCATION_WRAPPER (oper);
8575
8576 /* Go back to evaluating expressions. */
8577 --cp_unevaluated_operand;
8578 --c_inhibit_evaluation_warnings;
8579
8580 /* And restore the old one. */
8581 parser->type_definition_forbidden_message = saved_message;
8582 parser->type_definition_forbidden_message_arg = saved_message_arg;
8583 parser->integral_constant_expression_p
8584 = saved_integral_constant_expression_p;
8585 parser->non_integral_constant_expression_p
8586 = saved_non_integral_constant_expression_p;
8587
8588 /* Consume the comma if it's there. */
8589 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8590 {
8591 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8592 /*consume_paren=*/true);
8593 return error_mark_node;
8594 }
8595
8596 /* Parse the attribute specification. */
8597 bool ret = false;
8598 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8599 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8600 {
8601 if (oper != error_mark_node)
8602 {
8603 /* Fold constant expressions used in attributes first. */
8604 cp_check_const_attributes (attr);
8605
8606 /* Finally, see if OPER has been declared with ATTR. */
8607 ret = has_attribute (atloc, oper, attr, default_conversion);
8608 }
8609
8610 parens.require_close (parser);
8611 }
8612 else
8613 {
8614 error_at (atloc, "expected identifier");
8615 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8616 }
8617
8618 /* Construct a location e.g. :
8619 __builtin_has_attribute (oper, attr)
8620 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8621 with start == caret at the start of the built-in token,
8622 and with the endpoint at the final closing paren. */
8623 location_t compound_loc
8624 = make_location (start_loc, start_loc, parser->lexer);
8625
8626 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8627 ret_expr.set_location (compound_loc);
8628 ret_expr = ret_expr.maybe_add_location_wrapper ();
8629 return ret_expr;
8630 }
8631
8632 /* Parse a new-expression.
8633
8634 new-expression:
8635 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8636 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8637
8638 Returns a representation of the expression. */
8639
8640 static tree
8641 cp_parser_new_expression (cp_parser* parser)
8642 {
8643 bool global_scope_p;
8644 vec<tree, va_gc> *placement;
8645 tree type;
8646 vec<tree, va_gc> *initializer;
8647 tree nelts = NULL_TREE;
8648 tree ret;
8649
8650 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8651
8652 /* Look for the optional `::' operator. */
8653 global_scope_p
8654 = (cp_parser_global_scope_opt (parser,
8655 /*current_scope_valid_p=*/false)
8656 != NULL_TREE);
8657 /* Look for the `new' operator. */
8658 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8659 /* There's no easy way to tell a new-placement from the
8660 `( type-id )' construct. */
8661 cp_parser_parse_tentatively (parser);
8662 /* Look for a new-placement. */
8663 placement = cp_parser_new_placement (parser);
8664 /* If that didn't work out, there's no new-placement. */
8665 if (!cp_parser_parse_definitely (parser))
8666 {
8667 if (placement != NULL)
8668 release_tree_vector (placement);
8669 placement = NULL;
8670 }
8671
8672 /* If the next token is a `(', then we have a parenthesized
8673 type-id. */
8674 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8675 {
8676 cp_token *token;
8677 const char *saved_message = parser->type_definition_forbidden_message;
8678
8679 /* Consume the `('. */
8680 matching_parens parens;
8681 parens.consume_open (parser);
8682
8683 /* Parse the type-id. */
8684 parser->type_definition_forbidden_message
8685 = G_("types may not be defined in a new-expression");
8686 {
8687 type_id_in_expr_sentinel s (parser);
8688 type = cp_parser_type_id (parser);
8689 }
8690 parser->type_definition_forbidden_message = saved_message;
8691
8692 /* Look for the closing `)'. */
8693 parens.require_close (parser);
8694 token = cp_lexer_peek_token (parser->lexer);
8695 /* There should not be a direct-new-declarator in this production,
8696 but GCC used to allowed this, so we check and emit a sensible error
8697 message for this case. */
8698 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8699 {
8700 error_at (token->location,
8701 "array bound forbidden after parenthesized type-id");
8702 inform (token->location,
8703 "try removing the parentheses around the type-id");
8704 cp_parser_direct_new_declarator (parser);
8705 }
8706 }
8707 /* Otherwise, there must be a new-type-id. */
8708 else
8709 type = cp_parser_new_type_id (parser, &nelts);
8710
8711 /* If the next token is a `(' or '{', then we have a new-initializer. */
8712 cp_token *token = cp_lexer_peek_token (parser->lexer);
8713 if (token->type == CPP_OPEN_PAREN
8714 || token->type == CPP_OPEN_BRACE)
8715 initializer = cp_parser_new_initializer (parser);
8716 else
8717 initializer = NULL;
8718
8719 /* A new-expression may not appear in an integral constant
8720 expression. */
8721 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8722 ret = error_mark_node;
8723 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8724 of a new-type-id or type-id of a new-expression, the new-expression shall
8725 contain a new-initializer of the form ( assignment-expression )".
8726 Additionally, consistently with the spirit of DR 1467, we want to accept
8727 'new auto { 2 }' too. */
8728 else if ((ret = type_uses_auto (type))
8729 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8730 && (vec_safe_length (initializer) != 1
8731 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8732 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8733 {
8734 error_at (token->location,
8735 "initialization of new-expression for type %<auto%> "
8736 "requires exactly one element");
8737 ret = error_mark_node;
8738 }
8739 else
8740 {
8741 /* Construct a location e.g.:
8742 ptr = new int[100]
8743 ^~~~~~~~~~~~
8744 with caret == start at the start of the "new" token, and the end
8745 at the end of the final token we consumed. */
8746 location_t combined_loc = make_location (start_loc, start_loc,
8747 parser->lexer);
8748
8749 /* Create a representation of the new-expression. */
8750 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8751 tf_warning_or_error);
8752 protected_set_expr_location (ret, combined_loc);
8753 }
8754
8755 if (placement != NULL)
8756 release_tree_vector (placement);
8757 if (initializer != NULL)
8758 release_tree_vector (initializer);
8759
8760 return ret;
8761 }
8762
8763 /* Parse a new-placement.
8764
8765 new-placement:
8766 ( expression-list )
8767
8768 Returns the same representation as for an expression-list. */
8769
8770 static vec<tree, va_gc> *
8771 cp_parser_new_placement (cp_parser* parser)
8772 {
8773 vec<tree, va_gc> *expression_list;
8774
8775 /* Parse the expression-list. */
8776 expression_list = (cp_parser_parenthesized_expression_list
8777 (parser, non_attr, /*cast_p=*/false,
8778 /*allow_expansion_p=*/true,
8779 /*non_constant_p=*/NULL));
8780
8781 if (expression_list && expression_list->is_empty ())
8782 error ("expected expression-list or type-id");
8783
8784 return expression_list;
8785 }
8786
8787 /* Parse a new-type-id.
8788
8789 new-type-id:
8790 type-specifier-seq new-declarator [opt]
8791
8792 Returns the TYPE allocated. If the new-type-id indicates an array
8793 type, *NELTS is set to the number of elements in the last array
8794 bound; the TYPE will not include the last array bound. */
8795
8796 static tree
8797 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8798 {
8799 cp_decl_specifier_seq type_specifier_seq;
8800 cp_declarator *new_declarator;
8801 cp_declarator *declarator;
8802 cp_declarator *outer_declarator;
8803 const char *saved_message;
8804
8805 /* The type-specifier sequence must not contain type definitions.
8806 (It cannot contain declarations of new types either, but if they
8807 are not definitions we will catch that because they are not
8808 complete.) */
8809 saved_message = parser->type_definition_forbidden_message;
8810 parser->type_definition_forbidden_message
8811 = G_("types may not be defined in a new-type-id");
8812 /* Parse the type-specifier-seq. */
8813 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
8814 /*is_declaration=*/false,
8815 /*is_trailing_return=*/false,
8816 &type_specifier_seq);
8817 /* Restore the old message. */
8818 parser->type_definition_forbidden_message = saved_message;
8819
8820 if (type_specifier_seq.type == error_mark_node)
8821 return error_mark_node;
8822
8823 /* Parse the new-declarator. */
8824 new_declarator = cp_parser_new_declarator_opt (parser);
8825
8826 /* Determine the number of elements in the last array dimension, if
8827 any. */
8828 *nelts = NULL_TREE;
8829 /* Skip down to the last array dimension. */
8830 declarator = new_declarator;
8831 outer_declarator = NULL;
8832 while (declarator && (declarator->kind == cdk_pointer
8833 || declarator->kind == cdk_ptrmem))
8834 {
8835 outer_declarator = declarator;
8836 declarator = declarator->declarator;
8837 }
8838 while (declarator
8839 && declarator->kind == cdk_array
8840 && declarator->declarator
8841 && declarator->declarator->kind == cdk_array)
8842 {
8843 outer_declarator = declarator;
8844 declarator = declarator->declarator;
8845 }
8846
8847 if (declarator && declarator->kind == cdk_array)
8848 {
8849 *nelts = declarator->u.array.bounds;
8850 if (*nelts == error_mark_node)
8851 *nelts = integer_one_node;
8852
8853 if (outer_declarator)
8854 outer_declarator->declarator = declarator->declarator;
8855 else
8856 new_declarator = NULL;
8857 }
8858
8859 return groktypename (&type_specifier_seq, new_declarator, false);
8860 }
8861
8862 /* Parse an (optional) new-declarator.
8863
8864 new-declarator:
8865 ptr-operator new-declarator [opt]
8866 direct-new-declarator
8867
8868 Returns the declarator. */
8869
8870 static cp_declarator *
8871 cp_parser_new_declarator_opt (cp_parser* parser)
8872 {
8873 enum tree_code code;
8874 tree type, std_attributes = NULL_TREE;
8875 cp_cv_quals cv_quals;
8876
8877 /* We don't know if there's a ptr-operator next, or not. */
8878 cp_parser_parse_tentatively (parser);
8879 /* Look for a ptr-operator. */
8880 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8881 /* If that worked, look for more new-declarators. */
8882 if (cp_parser_parse_definitely (parser))
8883 {
8884 cp_declarator *declarator;
8885
8886 /* Parse another optional declarator. */
8887 declarator = cp_parser_new_declarator_opt (parser);
8888
8889 declarator = cp_parser_make_indirect_declarator
8890 (code, type, cv_quals, declarator, std_attributes);
8891
8892 return declarator;
8893 }
8894
8895 /* If the next token is a `[', there is a direct-new-declarator. */
8896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8897 return cp_parser_direct_new_declarator (parser);
8898
8899 return NULL;
8900 }
8901
8902 /* Parse a direct-new-declarator.
8903
8904 direct-new-declarator:
8905 [ expression ]
8906 direct-new-declarator [constant-expression]
8907
8908 */
8909
8910 static cp_declarator *
8911 cp_parser_direct_new_declarator (cp_parser* parser)
8912 {
8913 cp_declarator *declarator = NULL;
8914
8915 while (true)
8916 {
8917 tree expression;
8918 cp_token *token;
8919
8920 /* Look for the opening `['. */
8921 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8922
8923 token = cp_lexer_peek_token (parser->lexer);
8924 expression = cp_parser_expression (parser);
8925 /* The standard requires that the expression have integral
8926 type. DR 74 adds enumeration types. We believe that the
8927 real intent is that these expressions be handled like the
8928 expression in a `switch' condition, which also allows
8929 classes with a single conversion to integral or
8930 enumeration type. */
8931 if (!processing_template_decl)
8932 {
8933 expression
8934 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8935 expression,
8936 /*complain=*/true);
8937 if (!expression)
8938 {
8939 error_at (token->location,
8940 "expression in new-declarator must have integral "
8941 "or enumeration type");
8942 expression = error_mark_node;
8943 }
8944 }
8945
8946 /* Look for the closing `]'. */
8947 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8948
8949 /* Add this bound to the declarator. */
8950 declarator = make_array_declarator (declarator, expression);
8951
8952 /* If the next token is not a `[', then there are no more
8953 bounds. */
8954 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8955 break;
8956 }
8957
8958 return declarator;
8959 }
8960
8961 /* Parse a new-initializer.
8962
8963 new-initializer:
8964 ( expression-list [opt] )
8965 braced-init-list
8966
8967 Returns a representation of the expression-list. */
8968
8969 static vec<tree, va_gc> *
8970 cp_parser_new_initializer (cp_parser* parser)
8971 {
8972 vec<tree, va_gc> *expression_list;
8973
8974 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8975 {
8976 tree t;
8977 bool expr_non_constant_p;
8978 cp_lexer_set_source_position (parser->lexer);
8979 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8980 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8981 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8982 expression_list = make_tree_vector_single (t);
8983 }
8984 else
8985 expression_list = (cp_parser_parenthesized_expression_list
8986 (parser, non_attr, /*cast_p=*/false,
8987 /*allow_expansion_p=*/true,
8988 /*non_constant_p=*/NULL));
8989
8990 return expression_list;
8991 }
8992
8993 /* Parse a delete-expression.
8994
8995 delete-expression:
8996 :: [opt] delete cast-expression
8997 :: [opt] delete [ ] cast-expression
8998
8999 Returns a representation of the expression. */
9000
9001 static tree
9002 cp_parser_delete_expression (cp_parser* parser)
9003 {
9004 bool global_scope_p;
9005 bool array_p;
9006 tree expression;
9007 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9008
9009 /* Look for the optional `::' operator. */
9010 global_scope_p
9011 = (cp_parser_global_scope_opt (parser,
9012 /*current_scope_valid_p=*/false)
9013 != NULL_TREE);
9014 /* Look for the `delete' keyword. */
9015 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9016 /* See if the array syntax is in use. */
9017 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9018 {
9019 /* Consume the `[' token. */
9020 cp_lexer_consume_token (parser->lexer);
9021 /* Look for the `]' token. */
9022 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9023 /* Remember that this is the `[]' construct. */
9024 array_p = true;
9025 }
9026 else
9027 array_p = false;
9028
9029 /* Parse the cast-expression. */
9030 expression = cp_parser_simple_cast_expression (parser);
9031
9032 /* A delete-expression may not appear in an integral constant
9033 expression. */
9034 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9035 return error_mark_node;
9036
9037 /* Construct a location e.g.:
9038 delete [ ] ptr
9039 ^~~~~~~~~~~~~~
9040 with caret == start at the start of the "delete" token, and
9041 the end at the end of the final token we consumed. */
9042 location_t combined_loc = make_location (start_loc, start_loc,
9043 parser->lexer);
9044 expression = delete_sanity (expression, NULL_TREE, array_p,
9045 global_scope_p, tf_warning_or_error);
9046 protected_set_expr_location (expression, combined_loc);
9047
9048 return expression;
9049 }
9050
9051 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9052 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9053 0 otherwise. */
9054
9055 static int
9056 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9057 {
9058 cp_token *token = cp_lexer_peek_token (parser->lexer);
9059 switch (token->type)
9060 {
9061 case CPP_COMMA:
9062 case CPP_SEMICOLON:
9063 case CPP_QUERY:
9064 case CPP_COLON:
9065 case CPP_CLOSE_SQUARE:
9066 case CPP_CLOSE_PAREN:
9067 case CPP_CLOSE_BRACE:
9068 case CPP_OPEN_BRACE:
9069 case CPP_DOT:
9070 case CPP_DOT_STAR:
9071 case CPP_DEREF:
9072 case CPP_DEREF_STAR:
9073 case CPP_DIV:
9074 case CPP_MOD:
9075 case CPP_LSHIFT:
9076 case CPP_RSHIFT:
9077 case CPP_LESS:
9078 case CPP_GREATER:
9079 case CPP_LESS_EQ:
9080 case CPP_GREATER_EQ:
9081 case CPP_EQ_EQ:
9082 case CPP_NOT_EQ:
9083 case CPP_EQ:
9084 case CPP_MULT_EQ:
9085 case CPP_DIV_EQ:
9086 case CPP_MOD_EQ:
9087 case CPP_PLUS_EQ:
9088 case CPP_MINUS_EQ:
9089 case CPP_RSHIFT_EQ:
9090 case CPP_LSHIFT_EQ:
9091 case CPP_AND_EQ:
9092 case CPP_XOR_EQ:
9093 case CPP_OR_EQ:
9094 case CPP_XOR:
9095 case CPP_OR:
9096 case CPP_OR_OR:
9097 case CPP_EOF:
9098 case CPP_ELLIPSIS:
9099 return 0;
9100
9101 case CPP_OPEN_PAREN:
9102 /* In ((type ()) () the last () isn't a valid cast-expression,
9103 so the whole must be parsed as postfix-expression. */
9104 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9105 != CPP_CLOSE_PAREN;
9106
9107 case CPP_OPEN_SQUARE:
9108 /* '[' may start a primary-expression in obj-c++ and in C++11,
9109 as a lambda-expression, eg, '(void)[]{}'. */
9110 if (cxx_dialect >= cxx11)
9111 return -1;
9112 return c_dialect_objc ();
9113
9114 case CPP_PLUS_PLUS:
9115 case CPP_MINUS_MINUS:
9116 /* '++' and '--' may or may not start a cast-expression:
9117
9118 struct T { void operator++(int); };
9119 void f() { (T())++; }
9120
9121 vs
9122
9123 int a;
9124 (int)++a; */
9125 return -1;
9126
9127 default:
9128 return 1;
9129 }
9130 }
9131
9132 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9133 in the order: const_cast, static_cast, reinterpret_cast.
9134
9135 Don't suggest dynamic_cast.
9136
9137 Return the first legal cast kind found, or NULL otherwise. */
9138
9139 static const char *
9140 get_cast_suggestion (tree dst_type, tree orig_expr)
9141 {
9142 tree trial;
9143
9144 /* Reuse the parser logic by attempting to build the various kinds of
9145 cast, with "complain" disabled.
9146 Identify the first such cast that is valid. */
9147
9148 /* Don't attempt to run such logic within template processing. */
9149 if (processing_template_decl)
9150 return NULL;
9151
9152 /* First try const_cast. */
9153 trial = build_const_cast (dst_type, orig_expr, tf_none);
9154 if (trial != error_mark_node)
9155 return "const_cast";
9156
9157 /* If that fails, try static_cast. */
9158 trial = build_static_cast (dst_type, orig_expr, tf_none);
9159 if (trial != error_mark_node)
9160 return "static_cast";
9161
9162 /* Finally, try reinterpret_cast. */
9163 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
9164 if (trial != error_mark_node)
9165 return "reinterpret_cast";
9166
9167 /* No such cast possible. */
9168 return NULL;
9169 }
9170
9171 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9172 suggesting how to convert a C-style cast of the form:
9173
9174 (DST_TYPE)ORIG_EXPR
9175
9176 to a C++-style cast.
9177
9178 The primary range of RICHLOC is asssumed to be that of the original
9179 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9180 of the parens in the C-style cast. */
9181
9182 static void
9183 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9184 location_t close_paren_loc, tree orig_expr,
9185 tree dst_type)
9186 {
9187 /* This function is non-trivial, so bail out now if the warning isn't
9188 going to be emitted. */
9189 if (!warn_old_style_cast)
9190 return;
9191
9192 /* Try to find a legal C++ cast, trying them in order:
9193 const_cast, static_cast, reinterpret_cast. */
9194 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9195 if (!cast_suggestion)
9196 return;
9197
9198 /* Replace the open paren with "CAST_SUGGESTION<". */
9199 pretty_printer pp;
9200 pp_printf (&pp, "%s<", cast_suggestion);
9201 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9202
9203 /* Replace the close paren with "> (". */
9204 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9205
9206 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9207 rich_loc->add_fixit_insert_after (")");
9208 }
9209
9210
9211 /* Parse a cast-expression.
9212
9213 cast-expression:
9214 unary-expression
9215 ( type-id ) cast-expression
9216
9217 ADDRESS_P is true iff the unary-expression is appearing as the
9218 operand of the `&' operator. CAST_P is true if this expression is
9219 the target of a cast.
9220
9221 Returns a representation of the expression. */
9222
9223 static cp_expr
9224 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9225 bool decltype_p, cp_id_kind * pidk)
9226 {
9227 /* If it's a `(', then we might be looking at a cast. */
9228 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9229 {
9230 tree type = NULL_TREE;
9231 cp_expr expr (NULL_TREE);
9232 int cast_expression = 0;
9233 const char *saved_message;
9234
9235 /* There's no way to know yet whether or not this is a cast.
9236 For example, `(int (3))' is a unary-expression, while `(int)
9237 3' is a cast. So, we resort to parsing tentatively. */
9238 cp_parser_parse_tentatively (parser);
9239 /* Types may not be defined in a cast. */
9240 saved_message = parser->type_definition_forbidden_message;
9241 parser->type_definition_forbidden_message
9242 = G_("types may not be defined in casts");
9243 /* Consume the `('. */
9244 matching_parens parens;
9245 cp_token *open_paren = parens.consume_open (parser);
9246 location_t open_paren_loc = open_paren->location;
9247 location_t close_paren_loc = UNKNOWN_LOCATION;
9248
9249 /* A very tricky bit is that `(struct S) { 3 }' is a
9250 compound-literal (which we permit in C++ as an extension).
9251 But, that construct is not a cast-expression -- it is a
9252 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9253 is legal; if the compound-literal were a cast-expression,
9254 you'd need an extra set of parentheses.) But, if we parse
9255 the type-id, and it happens to be a class-specifier, then we
9256 will commit to the parse at that point, because we cannot
9257 undo the action that is done when creating a new class. So,
9258 then we cannot back up and do a postfix-expression.
9259
9260 Another tricky case is the following (c++/29234):
9261
9262 struct S { void operator () (); };
9263
9264 void foo ()
9265 {
9266 ( S()() );
9267 }
9268
9269 As a type-id we parse the parenthesized S()() as a function
9270 returning a function, groktypename complains and we cannot
9271 back up in this case either.
9272
9273 Therefore, we scan ahead to the closing `)', and check to see
9274 if the tokens after the `)' can start a cast-expression. Otherwise
9275 we are dealing with an unary-expression, a postfix-expression
9276 or something else.
9277
9278 Yet another tricky case, in C++11, is the following (c++/54891):
9279
9280 (void)[]{};
9281
9282 The issue is that usually, besides the case of lambda-expressions,
9283 the parenthesized type-id cannot be followed by '[', and, eg, we
9284 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9285 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9286 we don't commit, we try a cast-expression, then an unary-expression.
9287
9288 Save tokens so that we can put them back. */
9289 cp_lexer_save_tokens (parser->lexer);
9290
9291 /* We may be looking at a cast-expression. */
9292 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9293 /*consume_paren=*/true))
9294 cast_expression
9295 = cp_parser_tokens_start_cast_expression (parser);
9296
9297 /* Roll back the tokens we skipped. */
9298 cp_lexer_rollback_tokens (parser->lexer);
9299 /* If we aren't looking at a cast-expression, simulate an error so
9300 that the call to cp_parser_error_occurred below returns true. */
9301 if (!cast_expression)
9302 cp_parser_simulate_error (parser);
9303 else
9304 {
9305 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9306 parser->in_type_id_in_expr_p = true;
9307 /* Look for the type-id. */
9308 type = cp_parser_type_id (parser);
9309 /* Look for the closing `)'. */
9310 cp_token *close_paren = parens.require_close (parser);
9311 if (close_paren)
9312 close_paren_loc = close_paren->location;
9313 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9314 }
9315
9316 /* Restore the saved message. */
9317 parser->type_definition_forbidden_message = saved_message;
9318
9319 /* At this point this can only be either a cast or a
9320 parenthesized ctor such as `(T ())' that looks like a cast to
9321 function returning T. */
9322 if (!cp_parser_error_occurred (parser))
9323 {
9324 /* Only commit if the cast-expression doesn't start with
9325 '++', '--', or '[' in C++11. */
9326 if (cast_expression > 0)
9327 cp_parser_commit_to_topmost_tentative_parse (parser);
9328
9329 expr = cp_parser_cast_expression (parser,
9330 /*address_p=*/false,
9331 /*cast_p=*/true,
9332 /*decltype_p=*/false,
9333 pidk);
9334
9335 if (cp_parser_parse_definitely (parser))
9336 {
9337 /* Warn about old-style casts, if so requested. */
9338 if (warn_old_style_cast
9339 && !in_system_header_at (input_location)
9340 && !VOID_TYPE_P (type)
9341 && current_lang_name != lang_name_c)
9342 {
9343 gcc_rich_location rich_loc (input_location);
9344 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9345 expr, type);
9346 warning_at (&rich_loc, OPT_Wold_style_cast,
9347 "use of old-style cast to %q#T", type);
9348 }
9349
9350 /* Only type conversions to integral or enumeration types
9351 can be used in constant-expressions. */
9352 if (!cast_valid_in_integral_constant_expression_p (type)
9353 && cp_parser_non_integral_constant_expression (parser,
9354 NIC_CAST))
9355 return error_mark_node;
9356
9357 /* Perform the cast. */
9358 /* Make a location:
9359 (TYPE) EXPR
9360 ^~~~~~~~~~~
9361 with start==caret at the open paren, extending to the
9362 end of "expr". */
9363 location_t cast_loc = make_location (open_paren_loc,
9364 open_paren_loc,
9365 expr.get_finish ());
9366 expr = build_c_cast (cast_loc, type, expr);
9367 return expr;
9368 }
9369 }
9370 else
9371 cp_parser_abort_tentative_parse (parser);
9372 }
9373
9374 /* If we get here, then it's not a cast, so it must be a
9375 unary-expression. */
9376 return cp_parser_unary_expression (parser, pidk, address_p,
9377 cast_p, decltype_p);
9378 }
9379
9380 /* Parse a binary expression of the general form:
9381
9382 pm-expression:
9383 cast-expression
9384 pm-expression .* cast-expression
9385 pm-expression ->* cast-expression
9386
9387 multiplicative-expression:
9388 pm-expression
9389 multiplicative-expression * pm-expression
9390 multiplicative-expression / pm-expression
9391 multiplicative-expression % pm-expression
9392
9393 additive-expression:
9394 multiplicative-expression
9395 additive-expression + multiplicative-expression
9396 additive-expression - multiplicative-expression
9397
9398 shift-expression:
9399 additive-expression
9400 shift-expression << additive-expression
9401 shift-expression >> additive-expression
9402
9403 relational-expression:
9404 shift-expression
9405 relational-expression < shift-expression
9406 relational-expression > shift-expression
9407 relational-expression <= shift-expression
9408 relational-expression >= shift-expression
9409
9410 GNU Extension:
9411
9412 relational-expression:
9413 relational-expression <? shift-expression
9414 relational-expression >? shift-expression
9415
9416 equality-expression:
9417 relational-expression
9418 equality-expression == relational-expression
9419 equality-expression != relational-expression
9420
9421 and-expression:
9422 equality-expression
9423 and-expression & equality-expression
9424
9425 exclusive-or-expression:
9426 and-expression
9427 exclusive-or-expression ^ and-expression
9428
9429 inclusive-or-expression:
9430 exclusive-or-expression
9431 inclusive-or-expression | exclusive-or-expression
9432
9433 logical-and-expression:
9434 inclusive-or-expression
9435 logical-and-expression && inclusive-or-expression
9436
9437 logical-or-expression:
9438 logical-and-expression
9439 logical-or-expression || logical-and-expression
9440
9441 All these are implemented with a single function like:
9442
9443 binary-expression:
9444 simple-cast-expression
9445 binary-expression <token> binary-expression
9446
9447 CAST_P is true if this expression is the target of a cast.
9448
9449 The binops_by_token map is used to get the tree codes for each <token> type.
9450 binary-expressions are associated according to a precedence table. */
9451
9452 #define TOKEN_PRECEDENCE(token) \
9453 (((token->type == CPP_GREATER \
9454 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9455 && !parser->greater_than_is_operator_p) \
9456 ? PREC_NOT_OPERATOR \
9457 : binops_by_token[token->type].prec)
9458
9459 static cp_expr
9460 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9461 bool no_toplevel_fold_p,
9462 bool decltype_p,
9463 enum cp_parser_prec prec,
9464 cp_id_kind * pidk)
9465 {
9466 cp_parser_expression_stack stack;
9467 cp_parser_expression_stack_entry *sp = &stack[0];
9468 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
9469 cp_parser_expression_stack_entry current;
9470 cp_expr rhs;
9471 cp_token *token;
9472 enum tree_code rhs_type;
9473 enum cp_parser_prec new_prec, lookahead_prec;
9474 tree overload;
9475
9476 /* Parse the first expression. */
9477 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9478 ? TRUTH_NOT_EXPR : ERROR_MARK);
9479 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9480 cast_p, decltype_p, pidk);
9481 current.prec = prec;
9482
9483 if (cp_parser_error_occurred (parser))
9484 return error_mark_node;
9485
9486 for (;;)
9487 {
9488 /* Get an operator token. */
9489 token = cp_lexer_peek_token (parser->lexer);
9490
9491 if (warn_cxx11_compat
9492 && token->type == CPP_RSHIFT
9493 && !parser->greater_than_is_operator_p)
9494 {
9495 if (warning_at (token->location, OPT_Wc__11_compat,
9496 "%<>>%> operator is treated"
9497 " as two right angle brackets in C++11"))
9498 inform (token->location,
9499 "suggest parentheses around %<>>%> expression");
9500 }
9501
9502 new_prec = TOKEN_PRECEDENCE (token);
9503 if (new_prec != PREC_NOT_OPERATOR
9504 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9505 /* This is a fold-expression; handle it later. */
9506 new_prec = PREC_NOT_OPERATOR;
9507
9508 /* Popping an entry off the stack means we completed a subexpression:
9509 - either we found a token which is not an operator (`>' where it is not
9510 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9511 will happen repeatedly;
9512 - or, we found an operator which has lower priority. This is the case
9513 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9514 parsing `3 * 4'. */
9515 if (new_prec <= current.prec)
9516 {
9517 if (sp == stack)
9518 break;
9519 else
9520 goto pop;
9521 }
9522
9523 get_rhs:
9524 current.tree_type = binops_by_token[token->type].tree_type;
9525 current.loc = token->location;
9526
9527 /* We used the operator token. */
9528 cp_lexer_consume_token (parser->lexer);
9529
9530 /* For "false && x" or "true || x", x will never be executed;
9531 disable warnings while evaluating it. */
9532 if ((current.tree_type == TRUTH_ANDIF_EXPR
9533 && cp_fully_fold (current.lhs) == truthvalue_false_node)
9534 || (current.tree_type == TRUTH_ORIF_EXPR
9535 && cp_fully_fold (current.lhs) == truthvalue_true_node))
9536 {
9537 disable_warnings_sp = sp;
9538 ++c_inhibit_evaluation_warnings;
9539 }
9540
9541 /* Extract another operand. It may be the RHS of this expression
9542 or the LHS of a new, higher priority expression. */
9543 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9544 ? TRUTH_NOT_EXPR : ERROR_MARK);
9545 rhs = cp_parser_simple_cast_expression (parser);
9546
9547 /* Get another operator token. Look up its precedence to avoid
9548 building a useless (immediately popped) stack entry for common
9549 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9550 token = cp_lexer_peek_token (parser->lexer);
9551 lookahead_prec = TOKEN_PRECEDENCE (token);
9552 if (lookahead_prec != PREC_NOT_OPERATOR
9553 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9554 lookahead_prec = PREC_NOT_OPERATOR;
9555 if (lookahead_prec > new_prec)
9556 {
9557 /* ... and prepare to parse the RHS of the new, higher priority
9558 expression. Since precedence levels on the stack are
9559 monotonically increasing, we do not have to care about
9560 stack overflows. */
9561 *sp = current;
9562 ++sp;
9563 current.lhs = rhs;
9564 current.lhs_type = rhs_type;
9565 current.prec = new_prec;
9566 new_prec = lookahead_prec;
9567 goto get_rhs;
9568
9569 pop:
9570 lookahead_prec = new_prec;
9571 /* If the stack is not empty, we have parsed into LHS the right side
9572 (`4' in the example above) of an expression we had suspended.
9573 We can use the information on the stack to recover the LHS (`3')
9574 from the stack together with the tree code (`MULT_EXPR'), and
9575 the precedence of the higher level subexpression
9576 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9577 which will be used to actually build the additive expression. */
9578 rhs = current.lhs;
9579 rhs_type = current.lhs_type;
9580 --sp;
9581 current = *sp;
9582 }
9583
9584 /* Undo the disabling of warnings done above. */
9585 if (sp == disable_warnings_sp)
9586 {
9587 disable_warnings_sp = NULL;
9588 --c_inhibit_evaluation_warnings;
9589 }
9590
9591 if (warn_logical_not_paren
9592 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9593 && current.lhs_type == TRUTH_NOT_EXPR
9594 /* Avoid warning for !!x == y. */
9595 && (TREE_CODE (current.lhs) != NE_EXPR
9596 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9597 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9598 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9599 /* Avoid warning for !b == y where b is boolean. */
9600 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9601 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9602 != BOOLEAN_TYPE))))
9603 /* Avoid warning for !!b == y where b is boolean. */
9604 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
9605 || TREE_TYPE (current.lhs) == NULL_TREE
9606 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9607 warn_logical_not_parentheses (current.loc, current.tree_type,
9608 current.lhs, maybe_constant_value (rhs));
9609
9610 overload = NULL;
9611
9612 location_t combined_loc = make_location (current.loc,
9613 current.lhs.get_start (),
9614 rhs.get_finish ());
9615
9616 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9617 ERROR_MARK for everything that is not a binary expression.
9618 This makes warn_about_parentheses miss some warnings that
9619 involve unary operators. For unary expressions we should
9620 pass the correct tree_code unless the unary expression was
9621 surrounded by parentheses.
9622 */
9623 if (no_toplevel_fold_p
9624 && lookahead_prec <= current.prec
9625 && sp == stack)
9626 {
9627 if (current.lhs == error_mark_node || rhs == error_mark_node)
9628 current.lhs = error_mark_node;
9629 else
9630 {
9631 current.lhs.maybe_add_location_wrapper ();
9632 rhs.maybe_add_location_wrapper ();
9633 current.lhs
9634 = build_min (current.tree_type,
9635 TREE_CODE_CLASS (current.tree_type)
9636 == tcc_comparison
9637 ? boolean_type_node : TREE_TYPE (current.lhs),
9638 current.lhs.get_value (), rhs.get_value ());
9639 SET_EXPR_LOCATION (current.lhs, combined_loc);
9640 }
9641 }
9642 else
9643 {
9644 op_location_t op_loc (current.loc, combined_loc);
9645 current.lhs = build_x_binary_op (op_loc, current.tree_type,
9646 current.lhs, current.lhs_type,
9647 rhs, rhs_type, &overload,
9648 complain_flags (decltype_p));
9649 /* TODO: build_x_binary_op doesn't always honor the location. */
9650 current.lhs.set_location (combined_loc);
9651 }
9652 current.lhs_type = current.tree_type;
9653
9654 /* If the binary operator required the use of an overloaded operator,
9655 then this expression cannot be an integral constant-expression.
9656 An overloaded operator can be used even if both operands are
9657 otherwise permissible in an integral constant-expression if at
9658 least one of the operands is of enumeration type. */
9659
9660 if (overload
9661 && cp_parser_non_integral_constant_expression (parser,
9662 NIC_OVERLOADED))
9663 return error_mark_node;
9664 }
9665
9666 return current.lhs;
9667 }
9668
9669 static cp_expr
9670 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9671 bool no_toplevel_fold_p,
9672 enum cp_parser_prec prec,
9673 cp_id_kind * pidk)
9674 {
9675 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9676 /*decltype*/false, prec, pidk);
9677 }
9678
9679 /* Parse the `? expression : assignment-expression' part of a
9680 conditional-expression. The LOGICAL_OR_EXPR is the
9681 logical-or-expression that started the conditional-expression.
9682 Returns a representation of the entire conditional-expression.
9683
9684 This routine is used by cp_parser_assignment_expression.
9685
9686 ? expression : assignment-expression
9687
9688 GNU Extensions:
9689
9690 ? : assignment-expression */
9691
9692 static tree
9693 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9694 {
9695 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9696 cp_expr assignment_expr;
9697 struct cp_token *token;
9698 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9699
9700 /* Consume the `?' token. */
9701 cp_lexer_consume_token (parser->lexer);
9702 token = cp_lexer_peek_token (parser->lexer);
9703 if (cp_parser_allow_gnu_extensions_p (parser)
9704 && token->type == CPP_COLON)
9705 {
9706 pedwarn (token->location, OPT_Wpedantic,
9707 "ISO C++ does not allow %<?:%> with omitted middle operand");
9708 /* Implicit true clause. */
9709 expr = NULL_TREE;
9710 c_inhibit_evaluation_warnings +=
9711 folded_logical_or_expr == truthvalue_true_node;
9712 warn_for_omitted_condop (token->location, logical_or_expr);
9713 }
9714 else
9715 {
9716 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9717 parser->colon_corrects_to_scope_p = false;
9718 /* Parse the expression. */
9719 c_inhibit_evaluation_warnings +=
9720 folded_logical_or_expr == truthvalue_false_node;
9721 expr = cp_parser_expression (parser);
9722 c_inhibit_evaluation_warnings +=
9723 ((folded_logical_or_expr == truthvalue_true_node)
9724 - (folded_logical_or_expr == truthvalue_false_node));
9725 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9726 }
9727
9728 /* The next token should be a `:'. */
9729 cp_parser_require (parser, CPP_COLON, RT_COLON);
9730 /* Parse the assignment-expression. */
9731 assignment_expr = cp_parser_assignment_expression (parser);
9732 c_inhibit_evaluation_warnings -=
9733 folded_logical_or_expr == truthvalue_true_node;
9734
9735 /* Make a location:
9736 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9737 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9738 with the caret at the "?", ranging from the start of
9739 the logical_or_expr to the end of the assignment_expr. */
9740 loc = make_location (loc,
9741 logical_or_expr.get_start (),
9742 assignment_expr.get_finish ());
9743
9744 /* Build the conditional-expression. */
9745 return build_x_conditional_expr (loc, logical_or_expr,
9746 expr,
9747 assignment_expr,
9748 tf_warning_or_error);
9749 }
9750
9751 /* Parse an assignment-expression.
9752
9753 assignment-expression:
9754 conditional-expression
9755 logical-or-expression assignment-operator assignment_expression
9756 throw-expression
9757
9758 CAST_P is true if this expression is the target of a cast.
9759 DECLTYPE_P is true if this expression is the operand of decltype.
9760
9761 Returns a representation for the expression. */
9762
9763 static cp_expr
9764 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9765 bool cast_p, bool decltype_p)
9766 {
9767 cp_expr expr;
9768
9769 /* If the next token is the `throw' keyword, then we're looking at
9770 a throw-expression. */
9771 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9772 expr = cp_parser_throw_expression (parser);
9773 /* Otherwise, it must be that we are looking at a
9774 logical-or-expression. */
9775 else
9776 {
9777 /* Parse the binary expressions (logical-or-expression). */
9778 expr = cp_parser_binary_expression (parser, cast_p, false,
9779 decltype_p,
9780 PREC_NOT_OPERATOR, pidk);
9781 /* If the next token is a `?' then we're actually looking at a
9782 conditional-expression. */
9783 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9784 return cp_parser_question_colon_clause (parser, expr);
9785 else
9786 {
9787 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9788
9789 /* If it's an assignment-operator, we're using the second
9790 production. */
9791 enum tree_code assignment_operator
9792 = cp_parser_assignment_operator_opt (parser);
9793 if (assignment_operator != ERROR_MARK)
9794 {
9795 bool non_constant_p;
9796
9797 /* Parse the right-hand side of the assignment. */
9798 cp_expr rhs = cp_parser_initializer_clause (parser,
9799 &non_constant_p);
9800
9801 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9802 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9803
9804 /* An assignment may not appear in a
9805 constant-expression. */
9806 if (cp_parser_non_integral_constant_expression (parser,
9807 NIC_ASSIGNMENT))
9808 return error_mark_node;
9809 /* Build the assignment expression. Its default
9810 location:
9811 LHS = RHS
9812 ~~~~^~~~~
9813 is the location of the '=' token as the
9814 caret, ranging from the start of the lhs to the
9815 end of the rhs. */
9816 loc = make_location (loc,
9817 expr.get_start (),
9818 rhs.get_finish ());
9819 expr = build_x_modify_expr (loc, expr,
9820 assignment_operator,
9821 rhs,
9822 complain_flags (decltype_p));
9823 /* TODO: build_x_modify_expr doesn't honor the location,
9824 so we must set it here. */
9825 expr.set_location (loc);
9826 }
9827 }
9828 }
9829
9830 return expr;
9831 }
9832
9833 /* Parse an (optional) assignment-operator.
9834
9835 assignment-operator: one of
9836 = *= /= %= += -= >>= <<= &= ^= |=
9837
9838 GNU Extension:
9839
9840 assignment-operator: one of
9841 <?= >?=
9842
9843 If the next token is an assignment operator, the corresponding tree
9844 code is returned, and the token is consumed. For example, for
9845 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9846 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9847 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9848 operator, ERROR_MARK is returned. */
9849
9850 static enum tree_code
9851 cp_parser_assignment_operator_opt (cp_parser* parser)
9852 {
9853 enum tree_code op;
9854 cp_token *token;
9855
9856 /* Peek at the next token. */
9857 token = cp_lexer_peek_token (parser->lexer);
9858
9859 switch (token->type)
9860 {
9861 case CPP_EQ:
9862 op = NOP_EXPR;
9863 break;
9864
9865 case CPP_MULT_EQ:
9866 op = MULT_EXPR;
9867 break;
9868
9869 case CPP_DIV_EQ:
9870 op = TRUNC_DIV_EXPR;
9871 break;
9872
9873 case CPP_MOD_EQ:
9874 op = TRUNC_MOD_EXPR;
9875 break;
9876
9877 case CPP_PLUS_EQ:
9878 op = PLUS_EXPR;
9879 break;
9880
9881 case CPP_MINUS_EQ:
9882 op = MINUS_EXPR;
9883 break;
9884
9885 case CPP_RSHIFT_EQ:
9886 op = RSHIFT_EXPR;
9887 break;
9888
9889 case CPP_LSHIFT_EQ:
9890 op = LSHIFT_EXPR;
9891 break;
9892
9893 case CPP_AND_EQ:
9894 op = BIT_AND_EXPR;
9895 break;
9896
9897 case CPP_XOR_EQ:
9898 op = BIT_XOR_EXPR;
9899 break;
9900
9901 case CPP_OR_EQ:
9902 op = BIT_IOR_EXPR;
9903 break;
9904
9905 default:
9906 /* Nothing else is an assignment operator. */
9907 op = ERROR_MARK;
9908 }
9909
9910 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9911 if (op != ERROR_MARK
9912 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9913 op = ERROR_MARK;
9914
9915 /* If it was an assignment operator, consume it. */
9916 if (op != ERROR_MARK)
9917 cp_lexer_consume_token (parser->lexer);
9918
9919 return op;
9920 }
9921
9922 /* Parse an expression.
9923
9924 expression:
9925 assignment-expression
9926 expression , assignment-expression
9927
9928 CAST_P is true if this expression is the target of a cast.
9929 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9930 except possibly parenthesized or on the RHS of a comma (N3276).
9931 WARN_COMMA_P is true if a comma should be diagnosed.
9932
9933 Returns a representation of the expression. */
9934
9935 static cp_expr
9936 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9937 bool cast_p, bool decltype_p, bool warn_comma_p)
9938 {
9939 cp_expr expression = NULL_TREE;
9940 location_t loc = UNKNOWN_LOCATION;
9941
9942 while (true)
9943 {
9944 cp_expr assignment_expression;
9945
9946 /* Parse the next assignment-expression. */
9947 assignment_expression
9948 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9949
9950 /* We don't create a temporary for a call that is the immediate operand
9951 of decltype or on the RHS of a comma. But when we see a comma, we
9952 need to create a temporary for a call on the LHS. */
9953 if (decltype_p && !processing_template_decl
9954 && TREE_CODE (assignment_expression) == CALL_EXPR
9955 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9956 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9957 assignment_expression
9958 = build_cplus_new (TREE_TYPE (assignment_expression),
9959 assignment_expression, tf_warning_or_error);
9960
9961 /* If this is the first assignment-expression, we can just
9962 save it away. */
9963 if (!expression)
9964 expression = assignment_expression;
9965 else
9966 {
9967 /* Create a location with caret at the comma, ranging
9968 from the start of the LHS to the end of the RHS. */
9969 loc = make_location (loc,
9970 expression.get_start (),
9971 assignment_expression.get_finish ());
9972 expression = build_x_compound_expr (loc, expression,
9973 assignment_expression,
9974 complain_flags (decltype_p));
9975 expression.set_location (loc);
9976 }
9977 /* If the next token is not a comma, or we're in a fold-expression, then
9978 we are done with the expression. */
9979 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9980 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9981 break;
9982 /* Consume the `,'. */
9983 loc = cp_lexer_peek_token (parser->lexer)->location;
9984 if (warn_comma_p)
9985 {
9986 /* [depr.comma.subscript]: A comma expression appearing as
9987 the expr-or-braced-init-list of a subscripting expression
9988 is deprecated. A parenthesized comma expression is not
9989 deprecated. */
9990 warning_at (loc, OPT_Wcomma_subscript,
9991 "top-level comma expression in array subscript "
9992 "is deprecated");
9993 warn_comma_p = false;
9994 }
9995 cp_lexer_consume_token (parser->lexer);
9996 /* A comma operator cannot appear in a constant-expression. */
9997 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9998 expression = error_mark_node;
9999 }
10000
10001 return expression;
10002 }
10003
10004 /* Parse a constant-expression.
10005
10006 constant-expression:
10007 conditional-expression
10008
10009 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10010 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10011 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10012 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
10013 only parse a conditional-expression, otherwise parse an
10014 assignment-expression. See below for rationale. */
10015
10016 static cp_expr
10017 cp_parser_constant_expression (cp_parser* parser,
10018 bool allow_non_constant_p,
10019 bool *non_constant_p,
10020 bool strict_p)
10021 {
10022 bool saved_integral_constant_expression_p;
10023 bool saved_allow_non_integral_constant_expression_p;
10024 bool saved_non_integral_constant_expression_p;
10025 cp_expr expression;
10026
10027 /* It might seem that we could simply parse the
10028 conditional-expression, and then check to see if it were
10029 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10030 one that the compiler can figure out is constant, possibly after
10031 doing some simplifications or optimizations. The standard has a
10032 precise definition of constant-expression, and we must honor
10033 that, even though it is somewhat more restrictive.
10034
10035 For example:
10036
10037 int i[(2, 3)];
10038
10039 is not a legal declaration, because `(2, 3)' is not a
10040 constant-expression. The `,' operator is forbidden in a
10041 constant-expression. However, GCC's constant-folding machinery
10042 will fold this operation to an INTEGER_CST for `3'. */
10043
10044 /* Save the old settings. */
10045 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10046 saved_allow_non_integral_constant_expression_p
10047 = parser->allow_non_integral_constant_expression_p;
10048 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10049 /* We are now parsing a constant-expression. */
10050 parser->integral_constant_expression_p = true;
10051 parser->allow_non_integral_constant_expression_p
10052 = (allow_non_constant_p || cxx_dialect >= cxx11);
10053 parser->non_integral_constant_expression_p = false;
10054 /* Although the grammar says "conditional-expression", when not STRICT_P,
10055 we parse an "assignment-expression", which also permits
10056 "throw-expression" and the use of assignment operators. In the case
10057 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10058 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10059 actually essential that we look for an assignment-expression.
10060 For example, cp_parser_initializer_clauses uses this function to
10061 determine whether a particular assignment-expression is in fact
10062 constant. */
10063 if (strict_p)
10064 {
10065 /* Parse the binary expressions (logical-or-expression). */
10066 expression = cp_parser_binary_expression (parser, false, false, false,
10067 PREC_NOT_OPERATOR, NULL);
10068 /* If the next token is a `?' then we're actually looking at
10069 a conditional-expression; otherwise we're done. */
10070 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10071 expression = cp_parser_question_colon_clause (parser, expression);
10072 }
10073 else
10074 expression = cp_parser_assignment_expression (parser);
10075 /* Restore the old settings. */
10076 parser->integral_constant_expression_p
10077 = saved_integral_constant_expression_p;
10078 parser->allow_non_integral_constant_expression_p
10079 = saved_allow_non_integral_constant_expression_p;
10080 if (cxx_dialect >= cxx11)
10081 {
10082 /* Require an rvalue constant expression here; that's what our
10083 callers expect. Reference constant expressions are handled
10084 separately in e.g. cp_parser_template_argument. */
10085 tree decay = expression;
10086 if (TREE_TYPE (expression)
10087 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10088 decay = build_address (expression);
10089 bool is_const = potential_rvalue_constant_expression (decay);
10090 parser->non_integral_constant_expression_p = !is_const;
10091 if (!is_const && !allow_non_constant_p)
10092 require_potential_rvalue_constant_expression (decay);
10093 }
10094 if (allow_non_constant_p)
10095 *non_constant_p = parser->non_integral_constant_expression_p;
10096 parser->non_integral_constant_expression_p
10097 = saved_non_integral_constant_expression_p;
10098
10099 return expression;
10100 }
10101
10102 /* Parse __builtin_offsetof.
10103
10104 offsetof-expression:
10105 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10106
10107 offsetof-member-designator:
10108 id-expression
10109 | offsetof-member-designator "." id-expression
10110 | offsetof-member-designator "[" expression "]"
10111 | offsetof-member-designator "->" id-expression */
10112
10113 static cp_expr
10114 cp_parser_builtin_offsetof (cp_parser *parser)
10115 {
10116 int save_ice_p, save_non_ice_p;
10117 tree type;
10118 cp_expr expr;
10119 cp_id_kind dummy;
10120 cp_token *token;
10121 location_t finish_loc;
10122
10123 /* We're about to accept non-integral-constant things, but will
10124 definitely yield an integral constant expression. Save and
10125 restore these values around our local parsing. */
10126 save_ice_p = parser->integral_constant_expression_p;
10127 save_non_ice_p = parser->non_integral_constant_expression_p;
10128
10129 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10130
10131 /* Consume the "__builtin_offsetof" token. */
10132 cp_lexer_consume_token (parser->lexer);
10133 /* Consume the opening `('. */
10134 matching_parens parens;
10135 parens.require_open (parser);
10136 /* Parse the type-id. */
10137 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10138 {
10139 const char *saved_message = parser->type_definition_forbidden_message;
10140 parser->type_definition_forbidden_message
10141 = G_("types may not be defined within %<__builtin_offsetof%>");
10142 type = cp_parser_type_id (parser);
10143 parser->type_definition_forbidden_message = saved_message;
10144 }
10145 /* Look for the `,'. */
10146 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10147 token = cp_lexer_peek_token (parser->lexer);
10148
10149 /* Build the (type *)null that begins the traditional offsetof macro. */
10150 tree object_ptr
10151 = build_static_cast (build_pointer_type (type), null_pointer_node,
10152 tf_warning_or_error);
10153
10154 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10155 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10156 true, &dummy, token->location);
10157 while (true)
10158 {
10159 token = cp_lexer_peek_token (parser->lexer);
10160 switch (token->type)
10161 {
10162 case CPP_OPEN_SQUARE:
10163 /* offsetof-member-designator "[" expression "]" */
10164 expr = cp_parser_postfix_open_square_expression (parser, expr,
10165 true, false);
10166 break;
10167
10168 case CPP_DEREF:
10169 /* offsetof-member-designator "->" identifier */
10170 expr = grok_array_decl (token->location, expr,
10171 integer_zero_node, false);
10172 /* FALLTHRU */
10173
10174 case CPP_DOT:
10175 /* offsetof-member-designator "." identifier */
10176 cp_lexer_consume_token (parser->lexer);
10177 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10178 expr, true, &dummy,
10179 token->location);
10180 break;
10181
10182 case CPP_CLOSE_PAREN:
10183 /* Consume the ")" token. */
10184 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10185 cp_lexer_consume_token (parser->lexer);
10186 goto success;
10187
10188 default:
10189 /* Error. We know the following require will fail, but
10190 that gives the proper error message. */
10191 parens.require_close (parser);
10192 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10193 expr = error_mark_node;
10194 goto failure;
10195 }
10196 }
10197
10198 success:
10199 /* Make a location of the form:
10200 __builtin_offsetof (struct s, f)
10201 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10202 with caret at the type-id, ranging from the start of the
10203 "_builtin_offsetof" token to the close paren. */
10204 loc = make_location (loc, start_loc, finish_loc);
10205 /* The result will be an INTEGER_CST, so we need to explicitly
10206 preserve the location. */
10207 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10208
10209 failure:
10210 parser->integral_constant_expression_p = save_ice_p;
10211 parser->non_integral_constant_expression_p = save_non_ice_p;
10212
10213 expr = expr.maybe_add_location_wrapper ();
10214 return expr;
10215 }
10216
10217 /* Parse a trait expression.
10218
10219 Returns a representation of the expression, the underlying type
10220 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10221
10222 static cp_expr
10223 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10224 {
10225 cp_trait_kind kind;
10226 tree type1, type2 = NULL_TREE;
10227 bool binary = false;
10228 bool variadic = false;
10229
10230 switch (keyword)
10231 {
10232 case RID_HAS_NOTHROW_ASSIGN:
10233 kind = CPTK_HAS_NOTHROW_ASSIGN;
10234 break;
10235 case RID_HAS_NOTHROW_CONSTRUCTOR:
10236 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10237 break;
10238 case RID_HAS_NOTHROW_COPY:
10239 kind = CPTK_HAS_NOTHROW_COPY;
10240 break;
10241 case RID_HAS_TRIVIAL_ASSIGN:
10242 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10243 break;
10244 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10245 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10246 break;
10247 case RID_HAS_TRIVIAL_COPY:
10248 kind = CPTK_HAS_TRIVIAL_COPY;
10249 break;
10250 case RID_HAS_TRIVIAL_DESTRUCTOR:
10251 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10252 break;
10253 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10254 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10255 break;
10256 case RID_HAS_VIRTUAL_DESTRUCTOR:
10257 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10258 break;
10259 case RID_IS_ABSTRACT:
10260 kind = CPTK_IS_ABSTRACT;
10261 break;
10262 case RID_IS_AGGREGATE:
10263 kind = CPTK_IS_AGGREGATE;
10264 break;
10265 case RID_IS_BASE_OF:
10266 kind = CPTK_IS_BASE_OF;
10267 binary = true;
10268 break;
10269 case RID_IS_CLASS:
10270 kind = CPTK_IS_CLASS;
10271 break;
10272 case RID_IS_EMPTY:
10273 kind = CPTK_IS_EMPTY;
10274 break;
10275 case RID_IS_ENUM:
10276 kind = CPTK_IS_ENUM;
10277 break;
10278 case RID_IS_FINAL:
10279 kind = CPTK_IS_FINAL;
10280 break;
10281 case RID_IS_LITERAL_TYPE:
10282 kind = CPTK_IS_LITERAL_TYPE;
10283 break;
10284 case RID_IS_POD:
10285 kind = CPTK_IS_POD;
10286 break;
10287 case RID_IS_POLYMORPHIC:
10288 kind = CPTK_IS_POLYMORPHIC;
10289 break;
10290 case RID_IS_SAME_AS:
10291 kind = CPTK_IS_SAME_AS;
10292 binary = true;
10293 break;
10294 case RID_IS_STD_LAYOUT:
10295 kind = CPTK_IS_STD_LAYOUT;
10296 break;
10297 case RID_IS_TRIVIAL:
10298 kind = CPTK_IS_TRIVIAL;
10299 break;
10300 case RID_IS_TRIVIALLY_ASSIGNABLE:
10301 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10302 binary = true;
10303 break;
10304 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10305 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10306 variadic = true;
10307 break;
10308 case RID_IS_TRIVIALLY_COPYABLE:
10309 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10310 break;
10311 case RID_IS_UNION:
10312 kind = CPTK_IS_UNION;
10313 break;
10314 case RID_UNDERLYING_TYPE:
10315 kind = CPTK_UNDERLYING_TYPE;
10316 break;
10317 case RID_BASES:
10318 kind = CPTK_BASES;
10319 break;
10320 case RID_DIRECT_BASES:
10321 kind = CPTK_DIRECT_BASES;
10322 break;
10323 case RID_IS_ASSIGNABLE:
10324 kind = CPTK_IS_ASSIGNABLE;
10325 binary = true;
10326 break;
10327 case RID_IS_CONSTRUCTIBLE:
10328 kind = CPTK_IS_CONSTRUCTIBLE;
10329 variadic = true;
10330 break;
10331 default:
10332 gcc_unreachable ();
10333 }
10334
10335 /* Get location of initial token. */
10336 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10337
10338 /* Consume the token. */
10339 cp_lexer_consume_token (parser->lexer);
10340
10341 matching_parens parens;
10342 parens.require_open (parser);
10343
10344 {
10345 type_id_in_expr_sentinel s (parser);
10346 type1 = cp_parser_type_id (parser);
10347 }
10348
10349 if (type1 == error_mark_node)
10350 return error_mark_node;
10351
10352 if (binary)
10353 {
10354 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10355
10356 {
10357 type_id_in_expr_sentinel s (parser);
10358 type2 = cp_parser_type_id (parser);
10359 }
10360
10361 if (type2 == error_mark_node)
10362 return error_mark_node;
10363 }
10364 else if (variadic)
10365 {
10366 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10367 {
10368 cp_lexer_consume_token (parser->lexer);
10369 tree elt = cp_parser_type_id (parser);
10370 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10371 {
10372 cp_lexer_consume_token (parser->lexer);
10373 elt = make_pack_expansion (elt);
10374 }
10375 if (elt == error_mark_node)
10376 return error_mark_node;
10377 type2 = tree_cons (NULL_TREE, elt, type2);
10378 }
10379 }
10380
10381 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10382 parens.require_close (parser);
10383
10384 /* Construct a location of the form:
10385 __is_trivially_copyable(_Tp)
10386 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10387 with start == caret, finishing at the close-paren. */
10388 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10389
10390 /* Complete the trait expression, which may mean either processing
10391 the trait expr now or saving it for template instantiation. */
10392 switch (kind)
10393 {
10394 case CPTK_UNDERLYING_TYPE:
10395 return cp_expr (finish_underlying_type (type1), trait_loc);
10396 case CPTK_BASES:
10397 return cp_expr (finish_bases (type1, false), trait_loc);
10398 case CPTK_DIRECT_BASES:
10399 return cp_expr (finish_bases (type1, true), trait_loc);
10400 default:
10401 return finish_trait_expr (trait_loc, kind, type1, type2);
10402 }
10403 }
10404
10405 /* Parse a lambda expression.
10406
10407 lambda-expression:
10408 lambda-introducer lambda-declarator [opt] compound-statement
10409
10410 Returns a representation of the expression. */
10411
10412 static cp_expr
10413 cp_parser_lambda_expression (cp_parser* parser)
10414 {
10415 tree lambda_expr = build_lambda_expr ();
10416 tree type;
10417 bool ok = true;
10418 cp_token *token = cp_lexer_peek_token (parser->lexer);
10419 cp_token_position start = 0;
10420
10421 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10422
10423 if (cxx_dialect >= cxx2a)
10424 /* C++20 allows lambdas in unevaluated context. */;
10425 else if (cp_unevaluated_operand)
10426 {
10427 if (!token->error_reported)
10428 {
10429 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10430 "lambda-expression in unevaluated context"
10431 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10432 token->error_reported = true;
10433 }
10434 ok = false;
10435 }
10436 else if (parser->in_template_argument_list_p || processing_template_parmlist)
10437 {
10438 if (!token->error_reported)
10439 {
10440 error_at (token->location, "lambda-expression in template-argument"
10441 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10442 token->error_reported = true;
10443 }
10444 ok = false;
10445 }
10446
10447 /* We may be in the middle of deferred access check. Disable
10448 it now. */
10449 push_deferring_access_checks (dk_no_deferred);
10450
10451 cp_parser_lambda_introducer (parser, lambda_expr);
10452 if (cp_parser_error_occurred (parser))
10453 return error_mark_node;
10454
10455 type = begin_lambda_type (lambda_expr);
10456 if (type == error_mark_node)
10457 return error_mark_node;
10458
10459 record_lambda_scope (lambda_expr);
10460
10461 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10462 determine_visibility (TYPE_NAME (type));
10463
10464 /* Now that we've started the type, add the capture fields for any
10465 explicit captures. */
10466 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10467
10468 {
10469 /* Inside the class, surrounding template-parameter-lists do not apply. */
10470 unsigned int saved_num_template_parameter_lists
10471 = parser->num_template_parameter_lists;
10472 unsigned char in_statement = parser->in_statement;
10473 bool in_switch_statement_p = parser->in_switch_statement_p;
10474 bool fully_implicit_function_template_p
10475 = parser->fully_implicit_function_template_p;
10476 tree implicit_template_parms = parser->implicit_template_parms;
10477 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10478 bool auto_is_implicit_function_template_parm_p
10479 = parser->auto_is_implicit_function_template_parm_p;
10480
10481 parser->num_template_parameter_lists = 0;
10482 parser->in_statement = 0;
10483 parser->in_switch_statement_p = false;
10484 parser->fully_implicit_function_template_p = false;
10485 parser->implicit_template_parms = 0;
10486 parser->implicit_template_scope = 0;
10487 parser->auto_is_implicit_function_template_parm_p = false;
10488
10489 /* By virtue of defining a local class, a lambda expression has access to
10490 the private variables of enclosing classes. */
10491
10492 if (cp_parser_start_tentative_firewall (parser))
10493 start = token;
10494
10495 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10496
10497 if (ok && cp_parser_error_occurred (parser))
10498 ok = false;
10499
10500 if (ok)
10501 {
10502 cp_parser_lambda_body (parser, lambda_expr);
10503 }
10504 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10505 {
10506 if (cp_parser_skip_to_closing_brace (parser))
10507 cp_lexer_consume_token (parser->lexer);
10508 }
10509
10510 /* The capture list was built up in reverse order; fix that now. */
10511 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10512 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10513
10514 if (ok)
10515 maybe_add_lambda_conv_op (type);
10516
10517 finish_struct (type, /*attributes=*/NULL_TREE);
10518
10519 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10520 parser->in_statement = in_statement;
10521 parser->in_switch_statement_p = in_switch_statement_p;
10522 parser->fully_implicit_function_template_p
10523 = fully_implicit_function_template_p;
10524 parser->implicit_template_parms = implicit_template_parms;
10525 parser->implicit_template_scope = implicit_template_scope;
10526 parser->auto_is_implicit_function_template_parm_p
10527 = auto_is_implicit_function_template_parm_p;
10528 }
10529
10530 /* This field is only used during parsing of the lambda. */
10531 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10532
10533 /* This lambda shouldn't have any proxies left at this point. */
10534 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10535 /* And now that we're done, push proxies for an enclosing lambda. */
10536 insert_pending_capture_proxies ();
10537
10538 /* Update the lambda expression to a range. */
10539 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10540 token->location,
10541 parser->lexer);
10542
10543 if (ok)
10544 lambda_expr = build_lambda_object (lambda_expr);
10545 else
10546 lambda_expr = error_mark_node;
10547
10548 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10549
10550 pop_deferring_access_checks ();
10551
10552 return lambda_expr;
10553 }
10554
10555 /* Parse the beginning of a lambda expression.
10556
10557 lambda-introducer:
10558 [ lambda-capture [opt] ]
10559
10560 LAMBDA_EXPR is the current representation of the lambda expression. */
10561
10562 static void
10563 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10564 {
10565 /* Need commas after the first capture. */
10566 bool first = true;
10567
10568 /* Eat the leading `['. */
10569 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10570
10571 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10572 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10573 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
10574 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10575 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10576 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10577 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10578
10579 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10580 {
10581 cp_lexer_consume_token (parser->lexer);
10582 first = false;
10583
10584 if (!(at_function_scope_p () || parsing_nsdmi ()))
10585 error ("non-local lambda expression cannot have a capture-default");
10586 }
10587
10588 hash_set<tree, true> ids;
10589 tree first_capture_id = NULL_TREE;
10590 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10591 {
10592 cp_token* capture_token;
10593 tree capture_id;
10594 tree capture_init_expr;
10595 cp_id_kind idk = CP_ID_KIND_NONE;
10596 bool explicit_init_p = false;
10597
10598 enum capture_kind_type
10599 {
10600 BY_COPY,
10601 BY_REFERENCE
10602 };
10603 enum capture_kind_type capture_kind = BY_COPY;
10604
10605 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10606 {
10607 error ("expected end of capture-list");
10608 return;
10609 }
10610
10611 if (first)
10612 first = false;
10613 else
10614 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10615
10616 /* Possibly capture `this'. */
10617 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10618 {
10619 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10620 if (cxx_dialect < cxx2a
10621 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10622 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10623 "with by-copy capture default");
10624 cp_lexer_consume_token (parser->lexer);
10625 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10626 pedwarn (input_location, 0,
10627 "already captured %qD in lambda expression",
10628 this_identifier);
10629 else
10630 add_capture (lambda_expr, /*id=*/this_identifier,
10631 /*initializer=*/finish_this_expr (),
10632 /*by_reference_p=*/true, explicit_init_p);
10633 continue;
10634 }
10635
10636 /* Possibly capture `*this'. */
10637 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10638 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10639 {
10640 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10641 if (cxx_dialect < cxx17)
10642 pedwarn (loc, 0, "%<*this%> capture only available with "
10643 "%<-std=c++17%> or %<-std=gnu++17%>");
10644 cp_lexer_consume_token (parser->lexer);
10645 cp_lexer_consume_token (parser->lexer);
10646 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10647 pedwarn (input_location, 0,
10648 "already captured %qD in lambda expression",
10649 this_identifier);
10650 else
10651 add_capture (lambda_expr, /*id=*/this_identifier,
10652 /*initializer=*/finish_this_expr (),
10653 /*by_reference_p=*/false, explicit_init_p);
10654 continue;
10655 }
10656
10657 /* But reject `&this'. */
10658 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10659 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10660 {
10661 error_at (cp_lexer_peek_token (parser->lexer)->location,
10662 "%<this%> cannot be captured by reference");
10663 cp_lexer_consume_token (parser->lexer);
10664 cp_lexer_consume_token (parser->lexer);
10665 continue;
10666 }
10667
10668 bool init_pack_expansion = false;
10669 location_t ellipsis_loc = UNKNOWN_LOCATION;
10670 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10671 {
10672 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
10673 if (cxx_dialect < cxx2a)
10674 pedwarn (ellipsis_loc, 0, "pack init-capture only available with "
10675 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10676 cp_lexer_consume_token (parser->lexer);
10677 init_pack_expansion = true;
10678 }
10679
10680 /* Remember whether we want to capture as a reference or not. */
10681 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10682 {
10683 capture_kind = BY_REFERENCE;
10684 cp_lexer_consume_token (parser->lexer);
10685 }
10686
10687 /* Get the identifier. */
10688 capture_token = cp_lexer_peek_token (parser->lexer);
10689 capture_id = cp_parser_identifier (parser);
10690
10691 if (capture_id == error_mark_node)
10692 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10693 delimiters, but I modified this to stop on unnested ']' as well. It
10694 was already changed to stop on unnested '}', so the
10695 "closing_parenthesis" name is no more misleading with my change. */
10696 {
10697 cp_parser_skip_to_closing_parenthesis (parser,
10698 /*recovering=*/true,
10699 /*or_comma=*/true,
10700 /*consume_paren=*/true);
10701 break;
10702 }
10703
10704 /* Find the initializer for this capture. */
10705 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10706 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10707 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10708 {
10709 bool direct, non_constant;
10710 /* An explicit initializer exists. */
10711 if (cxx_dialect < cxx14)
10712 pedwarn (input_location, 0,
10713 "lambda capture initializers "
10714 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10715 capture_init_expr = cp_parser_initializer (parser, &direct,
10716 &non_constant, true);
10717 explicit_init_p = true;
10718 if (capture_init_expr == NULL_TREE)
10719 {
10720 error ("empty initializer for lambda init-capture");
10721 capture_init_expr = error_mark_node;
10722 }
10723 if (init_pack_expansion)
10724 capture_init_expr = make_pack_expansion (capture_init_expr);
10725 }
10726 else
10727 {
10728 const char* error_msg;
10729
10730 /* Turn the identifier into an id-expression. */
10731 capture_init_expr
10732 = cp_parser_lookup_name_simple (parser, capture_id,
10733 capture_token->location);
10734
10735 if (capture_init_expr == error_mark_node)
10736 {
10737 unqualified_name_lookup_error (capture_id);
10738 continue;
10739 }
10740 else if (!VAR_P (capture_init_expr)
10741 && TREE_CODE (capture_init_expr) != PARM_DECL)
10742 {
10743 error_at (capture_token->location,
10744 "capture of non-variable %qE",
10745 capture_init_expr);
10746 if (DECL_P (capture_init_expr))
10747 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10748 "%q#D declared here", capture_init_expr);
10749 continue;
10750 }
10751 if (VAR_P (capture_init_expr)
10752 && decl_storage_duration (capture_init_expr) != dk_auto)
10753 {
10754 if (pedwarn (capture_token->location, 0, "capture of variable "
10755 "%qD with non-automatic storage duration",
10756 capture_init_expr))
10757 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10758 "%q#D declared here", capture_init_expr);
10759 continue;
10760 }
10761
10762 capture_init_expr
10763 = finish_id_expression
10764 (capture_id,
10765 capture_init_expr,
10766 parser->scope,
10767 &idk,
10768 /*integral_constant_expression_p=*/false,
10769 /*allow_non_integral_constant_expression_p=*/false,
10770 /*non_integral_constant_expression_p=*/NULL,
10771 /*template_p=*/false,
10772 /*done=*/true,
10773 /*address_p=*/false,
10774 /*template_arg_p=*/false,
10775 &error_msg,
10776 capture_token->location);
10777
10778 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10779 {
10780 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10781 cp_lexer_consume_token (parser->lexer);
10782 capture_init_expr = make_pack_expansion (capture_init_expr);
10783 if (init_pack_expansion)
10784 {
10785 /* If what follows is an initializer, the second '...' is
10786 invalid. But for cases like [...xs...], the first one
10787 is invalid. */
10788 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10789 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10790 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10791 ellipsis_loc = loc;
10792 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
10793 continue;
10794 }
10795 }
10796 }
10797
10798 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10799 && !explicit_init_p)
10800 {
10801 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10802 && capture_kind == BY_COPY)
10803 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10804 "of %qD redundant with by-copy capture default",
10805 capture_id);
10806 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10807 && capture_kind == BY_REFERENCE)
10808 pedwarn (capture_token->location, 0, "explicit by-reference "
10809 "capture of %qD redundant with by-reference capture "
10810 "default", capture_id);
10811 }
10812
10813 /* Check for duplicates.
10814 Optimize for the zero or one explicit captures cases and only create
10815 the hash_set after adding second capture. */
10816 bool found = false;
10817 if (!ids.is_empty ())
10818 found = ids.add (capture_id);
10819 else if (first_capture_id == NULL_TREE)
10820 first_capture_id = capture_id;
10821 else if (capture_id == first_capture_id)
10822 found = true;
10823 else
10824 {
10825 ids.add (first_capture_id);
10826 ids.add (capture_id);
10827 }
10828 if (found)
10829 pedwarn (input_location, 0,
10830 "already captured %qD in lambda expression", capture_id);
10831 else
10832 add_capture (lambda_expr, capture_id, capture_init_expr,
10833 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10834 explicit_init_p);
10835
10836 /* If there is any qualification still in effect, clear it
10837 now; we will be starting fresh with the next capture. */
10838 parser->scope = NULL_TREE;
10839 parser->qualifying_scope = NULL_TREE;
10840 parser->object_scope = NULL_TREE;
10841 }
10842
10843 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10844 }
10845
10846 /* Parse the (optional) middle of a lambda expression.
10847
10848 lambda-declarator:
10849 < template-parameter-list [opt] >
10850 requires-clause [opt]
10851 ( parameter-declaration-clause [opt] )
10852 attribute-specifier [opt]
10853 decl-specifier-seq [opt]
10854 exception-specification [opt]
10855 lambda-return-type-clause [opt]
10856 requires-clause [opt]
10857
10858 LAMBDA_EXPR is the current representation of the lambda expression. */
10859
10860 static bool
10861 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10862 {
10863 /* 5.1.1.4 of the standard says:
10864 If a lambda-expression does not include a lambda-declarator, it is as if
10865 the lambda-declarator were ().
10866 This means an empty parameter list, no attributes, and no exception
10867 specification. */
10868 tree param_list = void_list_node;
10869 tree std_attrs = NULL_TREE;
10870 tree gnu_attrs = NULL_TREE;
10871 tree exception_spec = NULL_TREE;
10872 tree template_param_list = NULL_TREE;
10873 tree tx_qual = NULL_TREE;
10874 tree return_type = NULL_TREE;
10875 tree trailing_requires_clause = NULL_TREE;
10876 cp_decl_specifier_seq lambda_specs;
10877 clear_decl_specs (&lambda_specs);
10878
10879 /* The template-parameter-list is optional, but must begin with
10880 an opening angle if present. */
10881 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10882 {
10883 if (cxx_dialect < cxx14)
10884 pedwarn (parser->lexer->next_token->location, 0,
10885 "lambda templates are only available with "
10886 "%<-std=c++14%> or %<-std=gnu++14%>");
10887 else if (cxx_dialect < cxx2a)
10888 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10889 "lambda templates are only available with "
10890 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10891
10892 cp_lexer_consume_token (parser->lexer);
10893
10894 template_param_list = cp_parser_template_parameter_list (parser);
10895 cp_parser_skip_to_end_of_template_parameter_list (parser);
10896
10897 /* We may have a constrained generic lambda; parse the requires-clause
10898 immediately after the template-parameter-list and combine with any
10899 shorthand constraints present. */
10900 tree dreqs = cp_parser_requires_clause_opt (parser, true);
10901 if (flag_concepts)
10902 {
10903 tree reqs = get_shorthand_constraints (current_template_parms);
10904 if (dreqs)
10905 reqs = combine_constraint_expressions (reqs, dreqs);
10906 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
10907 }
10908
10909 /* We just processed one more parameter list. */
10910 ++parser->num_template_parameter_lists;
10911 }
10912
10913 /* The parameter-declaration-clause is optional (unless
10914 template-parameter-list was given), but must begin with an
10915 opening parenthesis if present. */
10916 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10917 {
10918 bool is_consteval = false;
10919 /* For C++20, before parsing the parameter list check if there is
10920 a consteval specifier in the corresponding decl-specifier-seq. */
10921 if (cxx_dialect >= cxx2a)
10922 {
10923 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1);
10924 cp_lexer_nth_token_is (parser->lexer, n, CPP_KEYWORD); n++)
10925 {
10926 if (cp_lexer_peek_nth_token (parser->lexer, n)->keyword
10927 == RID_CONSTEVAL)
10928 {
10929 is_consteval = true;
10930 break;
10931 }
10932 }
10933 }
10934
10935 matching_parens parens;
10936 parens.consume_open (parser);
10937
10938 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10939
10940 if (is_consteval)
10941 current_binding_level->immediate_fn_ctx_p = true;
10942
10943 /* Parse parameters. */
10944 param_list
10945 = cp_parser_parameter_declaration_clause
10946 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
10947
10948 /* Default arguments shall not be specified in the
10949 parameter-declaration-clause of a lambda-declarator. */
10950 if (cxx_dialect < cxx14)
10951 for (tree t = param_list; t; t = TREE_CHAIN (t))
10952 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10953 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10954 "default argument specified for lambda parameter");
10955
10956 parens.require_close (parser);
10957
10958 /* In the decl-specifier-seq of the lambda-declarator, each
10959 decl-specifier shall either be mutable or constexpr. */
10960 int declares_class_or_enum;
10961 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
10962 && !cp_next_tokens_can_be_gnu_attribute_p (parser))
10963 cp_parser_decl_specifier_seq (parser,
10964 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10965 &lambda_specs, &declares_class_or_enum);
10966 if (lambda_specs.storage_class == sc_mutable)
10967 {
10968 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10969 if (lambda_specs.conflicting_specifiers_p)
10970 error_at (lambda_specs.locations[ds_storage_class],
10971 "duplicate %<mutable%>");
10972 }
10973
10974 tx_qual = cp_parser_tx_qualifier_opt (parser);
10975
10976 /* Parse optional exception specification. */
10977 exception_spec
10978 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
10979
10980 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10981
10982 /* Parse optional trailing return type. */
10983 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10984 {
10985 cp_lexer_consume_token (parser->lexer);
10986 return_type = cp_parser_trailing_type_id (parser);
10987 }
10988
10989 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
10990 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
10991
10992 /* Parse optional trailing requires clause. */
10993 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
10994
10995 /* The function parameters must be in scope all the way until after the
10996 trailing-return-type in case of decltype. */
10997 pop_bindings_and_leave_scope ();
10998 }
10999 else if (template_param_list != NULL_TREE) // generate diagnostic
11000 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11001
11002 /* Create the function call operator.
11003
11004 Messing with declarators like this is no uglier than building up the
11005 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11006 other code. */
11007 {
11008 cp_decl_specifier_seq return_type_specs;
11009 cp_declarator* declarator;
11010 tree fco;
11011 int quals;
11012 void *p;
11013
11014 clear_decl_specs (&return_type_specs);
11015 return_type_specs.type = make_auto ();
11016
11017 if (lambda_specs.locations[ds_constexpr])
11018 {
11019 if (cxx_dialect >= cxx17)
11020 return_type_specs.locations[ds_constexpr]
11021 = lambda_specs.locations[ds_constexpr];
11022 else
11023 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11024 "lambda only available with %<-std=c++17%> or "
11025 "%<-std=gnu++17%>");
11026 }
11027 if (lambda_specs.locations[ds_consteval])
11028 return_type_specs.locations[ds_consteval]
11029 = lambda_specs.locations[ds_consteval];
11030
11031 p = obstack_alloc (&declarator_obstack, 0);
11032
11033 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
11034 LAMBDA_EXPR_LOCATION (lambda_expr));
11035
11036 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
11037 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
11038 declarator = make_call_declarator (declarator, param_list, quals,
11039 VIRT_SPEC_UNSPECIFIED,
11040 REF_QUAL_NONE,
11041 tx_qual,
11042 exception_spec,
11043 return_type,
11044 trailing_requires_clause);
11045 declarator->std_attributes = std_attrs;
11046
11047 fco = grokmethod (&return_type_specs,
11048 declarator,
11049 gnu_attrs);
11050 if (fco != error_mark_node)
11051 {
11052 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
11053 DECL_ARTIFICIAL (fco) = 1;
11054 /* Give the object parameter a different name. */
11055 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
11056 DECL_SET_LAMBDA_FUNCTION (fco, true);
11057 }
11058 if (template_param_list)
11059 {
11060 fco = finish_member_template_decl (fco);
11061 finish_template_decl (template_param_list);
11062 --parser->num_template_parameter_lists;
11063 }
11064 else if (parser->fully_implicit_function_template_p)
11065 fco = finish_fully_implicit_template (parser, fco);
11066
11067 finish_member_declaration (fco);
11068
11069 obstack_free (&declarator_obstack, p);
11070
11071 return (fco != error_mark_node);
11072 }
11073 }
11074
11075 /* Parse the body of a lambda expression, which is simply
11076
11077 compound-statement
11078
11079 but which requires special handling.
11080 LAMBDA_EXPR is the current representation of the lambda expression. */
11081
11082 static void
11083 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11084 {
11085 bool nested = (current_function_decl != NULL_TREE);
11086 unsigned char local_variables_forbidden_p
11087 = parser->local_variables_forbidden_p;
11088 bool in_function_body = parser->in_function_body;
11089
11090 /* The body of a lambda-expression is not a subexpression of the enclosing
11091 expression. */
11092 cp_evaluated ev;
11093
11094 if (nested)
11095 push_function_context ();
11096 else
11097 /* Still increment function_depth so that we don't GC in the
11098 middle of an expression. */
11099 ++function_depth;
11100
11101 vec<tree> omp_privatization_save;
11102 save_omp_privatization_clauses (omp_privatization_save);
11103 /* Clear this in case we're in the middle of a default argument. */
11104 parser->local_variables_forbidden_p = 0;
11105 parser->in_function_body = true;
11106
11107 {
11108 local_specialization_stack s (lss_copy);
11109 tree fco = lambda_function (lambda_expr);
11110 tree body = start_lambda_function (fco, lambda_expr);
11111 matching_braces braces;
11112
11113 if (braces.require_open (parser))
11114 {
11115 tree compound_stmt = begin_compound_stmt (0);
11116
11117 /* Originally C++11 required us to peek for 'return expr'; and
11118 process it specially here to deduce the return type. N3638
11119 removed the need for that. */
11120
11121 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11122 cp_parser_label_declaration (parser);
11123 cp_parser_statement_seq_opt (parser, NULL_TREE);
11124 braces.require_close (parser);
11125
11126 finish_compound_stmt (compound_stmt);
11127 }
11128
11129 finish_lambda_function (body);
11130 }
11131
11132 restore_omp_privatization_clauses (omp_privatization_save);
11133 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11134 parser->in_function_body = in_function_body;
11135 if (nested)
11136 pop_function_context();
11137 else
11138 --function_depth;
11139 }
11140
11141 /* Statements [gram.stmt.stmt] */
11142
11143 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11144
11145 static void
11146 add_debug_begin_stmt (location_t loc)
11147 {
11148 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11149 return;
11150 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11151 /* A concept is never expanded normally. */
11152 return;
11153
11154 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11155 SET_EXPR_LOCATION (stmt, loc);
11156 add_stmt (stmt);
11157 }
11158
11159 /* Parse a statement.
11160
11161 statement:
11162 labeled-statement
11163 expression-statement
11164 compound-statement
11165 selection-statement
11166 iteration-statement
11167 jump-statement
11168 declaration-statement
11169 try-block
11170
11171 C++11:
11172
11173 statement:
11174 labeled-statement
11175 attribute-specifier-seq (opt) expression-statement
11176 attribute-specifier-seq (opt) compound-statement
11177 attribute-specifier-seq (opt) selection-statement
11178 attribute-specifier-seq (opt) iteration-statement
11179 attribute-specifier-seq (opt) jump-statement
11180 declaration-statement
11181 attribute-specifier-seq (opt) try-block
11182
11183 init-statement:
11184 expression-statement
11185 simple-declaration
11186
11187 TM Extension:
11188
11189 statement:
11190 atomic-statement
11191
11192 IN_COMPOUND is true when the statement is nested inside a
11193 cp_parser_compound_statement; this matters for certain pragmas.
11194
11195 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11196 is a (possibly labeled) if statement which is not enclosed in braces
11197 and has an else clause. This is used to implement -Wparentheses.
11198
11199 CHAIN is a vector of if-else-if conditions. */
11200
11201 static void
11202 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11203 bool in_compound, bool *if_p, vec<tree> *chain,
11204 location_t *loc_after_labels)
11205 {
11206 tree statement, std_attrs = NULL_TREE;
11207 cp_token *token;
11208 location_t statement_location, attrs_loc;
11209
11210 restart:
11211 if (if_p != NULL)
11212 *if_p = false;
11213 /* There is no statement yet. */
11214 statement = NULL_TREE;
11215
11216 saved_token_sentinel saved_tokens (parser->lexer);
11217 attrs_loc = cp_lexer_peek_token (parser->lexer)->location;
11218 if (c_dialect_objc ())
11219 /* In obj-c++, seeing '[[' might be the either the beginning of
11220 c++11 attributes, or a nested objc-message-expression. So
11221 let's parse the c++11 attributes tentatively. */
11222 cp_parser_parse_tentatively (parser);
11223 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11224 if (std_attrs)
11225 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
11226 if (c_dialect_objc ())
11227 {
11228 if (!cp_parser_parse_definitely (parser))
11229 std_attrs = NULL_TREE;
11230 }
11231
11232 /* Peek at the next token. */
11233 token = cp_lexer_peek_token (parser->lexer);
11234 /* Remember the location of the first token in the statement. */
11235 cp_token *statement_token = token;
11236 statement_location = token->location;
11237 add_debug_begin_stmt (statement_location);
11238 /* If this is a keyword, then that will often determine what kind of
11239 statement we have. */
11240 if (token->type == CPP_KEYWORD)
11241 {
11242 enum rid keyword = token->keyword;
11243
11244 switch (keyword)
11245 {
11246 case RID_CASE:
11247 case RID_DEFAULT:
11248 /* Looks like a labeled-statement with a case label.
11249 Parse the label, and then use tail recursion to parse
11250 the statement. */
11251 cp_parser_label_for_labeled_statement (parser, std_attrs);
11252 in_compound = false;
11253 goto restart;
11254
11255 case RID_IF:
11256 case RID_SWITCH:
11257 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11258 statement = cp_parser_selection_statement (parser, if_p, chain);
11259 break;
11260
11261 case RID_WHILE:
11262 case RID_DO:
11263 case RID_FOR:
11264 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11265 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11266 break;
11267
11268 case RID_BREAK:
11269 case RID_CONTINUE:
11270 case RID_RETURN:
11271 case RID_GOTO:
11272 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11273 statement = cp_parser_jump_statement (parser);
11274 break;
11275
11276 /* Objective-C++ exception-handling constructs. */
11277 case RID_AT_TRY:
11278 case RID_AT_CATCH:
11279 case RID_AT_FINALLY:
11280 case RID_AT_SYNCHRONIZED:
11281 case RID_AT_THROW:
11282 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11283 statement = cp_parser_objc_statement (parser);
11284 break;
11285
11286 case RID_TRY:
11287 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11288 statement = cp_parser_try_block (parser);
11289 break;
11290
11291 case RID_NAMESPACE:
11292 /* This must be a namespace alias definition. */
11293 if (std_attrs != NULL_TREE)
11294 {
11295 /* Attributes should be parsed as part of the the
11296 declaration, so let's un-parse them. */
11297 saved_tokens.rollback();
11298 std_attrs = NULL_TREE;
11299 }
11300 cp_parser_declaration_statement (parser);
11301 return;
11302
11303 case RID_TRANSACTION_ATOMIC:
11304 case RID_TRANSACTION_RELAXED:
11305 case RID_SYNCHRONIZED:
11306 case RID_ATOMIC_NOEXCEPT:
11307 case RID_ATOMIC_CANCEL:
11308 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11309 statement = cp_parser_transaction (parser, token);
11310 break;
11311 case RID_TRANSACTION_CANCEL:
11312 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11313 statement = cp_parser_transaction_cancel (parser);
11314 break;
11315
11316 default:
11317 /* It might be a keyword like `int' that can start a
11318 declaration-statement. */
11319 break;
11320 }
11321 }
11322 else if (token->type == CPP_NAME)
11323 {
11324 /* If the next token is a `:', then we are looking at a
11325 labeled-statement. */
11326 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11327 if (token->type == CPP_COLON)
11328 {
11329 /* Looks like a labeled-statement with an ordinary label.
11330 Parse the label, and then use tail recursion to parse
11331 the statement. */
11332
11333 cp_parser_label_for_labeled_statement (parser, std_attrs);
11334 in_compound = false;
11335 goto restart;
11336 }
11337 }
11338 /* Anything that starts with a `{' must be a compound-statement. */
11339 else if (token->type == CPP_OPEN_BRACE)
11340 {
11341 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11342 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11343 }
11344 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11345 a statement all its own. */
11346 else if (token->type == CPP_PRAGMA)
11347 {
11348 /* Only certain OpenMP pragmas are attached to statements, and thus
11349 are considered statements themselves. All others are not. In
11350 the context of a compound, accept the pragma as a "statement" and
11351 return so that we can check for a close brace. Otherwise we
11352 require a real statement and must go back and read one. */
11353 if (in_compound)
11354 cp_parser_pragma (parser, pragma_compound, if_p);
11355 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11356 goto restart;
11357 return;
11358 }
11359 else if (token->type == CPP_EOF)
11360 {
11361 cp_parser_error (parser, "expected statement");
11362 return;
11363 }
11364
11365 /* Everything else must be a declaration-statement or an
11366 expression-statement. Try for the declaration-statement
11367 first, unless we are looking at a `;', in which case we know that
11368 we have an expression-statement. */
11369 if (!statement)
11370 {
11371 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11372 {
11373 if (std_attrs != NULL_TREE)
11374 /* Attributes should be parsed as part of the declaration,
11375 so let's un-parse them. */
11376 saved_tokens.rollback();
11377
11378 cp_parser_parse_tentatively (parser);
11379 /* Try to parse the declaration-statement. */
11380 cp_parser_declaration_statement (parser);
11381 /* If that worked, we're done. */
11382 if (cp_parser_parse_definitely (parser))
11383 return;
11384 /* It didn't work, restore the post-attribute position. */
11385 if (std_attrs)
11386 cp_lexer_set_token_position (parser->lexer, statement_token);
11387 }
11388 /* All preceding labels have been parsed at this point. */
11389 if (loc_after_labels != NULL)
11390 *loc_after_labels = statement_location;
11391
11392 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11393
11394 /* Look for an expression-statement instead. */
11395 statement = cp_parser_expression_statement (parser, in_statement_expr);
11396
11397 /* Handle [[fallthrough]];. */
11398 if (attribute_fallthrough_p (std_attrs))
11399 {
11400 /* The next token after the fallthrough attribute is ';'. */
11401 if (statement == NULL_TREE)
11402 {
11403 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11404 statement = build_call_expr_internal_loc (statement_location,
11405 IFN_FALLTHROUGH,
11406 void_type_node, 0);
11407 finish_expr_stmt (statement);
11408 }
11409 else
11410 warning_at (statement_location, OPT_Wattributes,
11411 "%<fallthrough%> attribute not followed by %<;%>");
11412 std_attrs = NULL_TREE;
11413 }
11414 }
11415
11416 /* Set the line number for the statement. */
11417 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11418 SET_EXPR_LOCATION (statement, statement_location);
11419
11420 /* Allow "[[fallthrough]];", but warn otherwise. */
11421 if (std_attrs != NULL_TREE)
11422 warning_at (attrs_loc,
11423 OPT_Wattributes,
11424 "attributes at the beginning of statement are ignored");
11425 }
11426
11427 /* Append ATTR to attribute list ATTRS. */
11428
11429 static tree
11430 attr_chainon (tree attrs, tree attr)
11431 {
11432 if (attrs == error_mark_node)
11433 return error_mark_node;
11434 if (attr == error_mark_node)
11435 return error_mark_node;
11436 return chainon (attrs, attr);
11437 }
11438
11439 /* Parse the label for a labeled-statement, i.e.
11440
11441 identifier :
11442 case constant-expression :
11443 default :
11444
11445 GNU Extension:
11446 case constant-expression ... constant-expression : statement
11447
11448 When a label is parsed without errors, the label is added to the
11449 parse tree by the finish_* functions, so this function doesn't
11450 have to return the label. */
11451
11452 static void
11453 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11454 {
11455 cp_token *token;
11456 tree label = NULL_TREE;
11457 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11458
11459 /* The next token should be an identifier. */
11460 token = cp_lexer_peek_token (parser->lexer);
11461 if (token->type != CPP_NAME
11462 && token->type != CPP_KEYWORD)
11463 {
11464 cp_parser_error (parser, "expected labeled-statement");
11465 return;
11466 }
11467
11468 /* Remember whether this case or a user-defined label is allowed to fall
11469 through to. */
11470 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11471
11472 parser->colon_corrects_to_scope_p = false;
11473 switch (token->keyword)
11474 {
11475 case RID_CASE:
11476 {
11477 tree expr, expr_hi;
11478 cp_token *ellipsis;
11479
11480 /* Consume the `case' token. */
11481 cp_lexer_consume_token (parser->lexer);
11482 /* Parse the constant-expression. */
11483 expr = cp_parser_constant_expression (parser);
11484 if (check_for_bare_parameter_packs (expr))
11485 expr = error_mark_node;
11486
11487 ellipsis = cp_lexer_peek_token (parser->lexer);
11488 if (ellipsis->type == CPP_ELLIPSIS)
11489 {
11490 /* Consume the `...' token. */
11491 cp_lexer_consume_token (parser->lexer);
11492 expr_hi = cp_parser_constant_expression (parser);
11493 if (check_for_bare_parameter_packs (expr_hi))
11494 expr_hi = error_mark_node;
11495
11496 /* We don't need to emit warnings here, as the common code
11497 will do this for us. */
11498 }
11499 else
11500 expr_hi = NULL_TREE;
11501
11502 if (parser->in_switch_statement_p)
11503 {
11504 tree l = finish_case_label (token->location, expr, expr_hi);
11505 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11506 {
11507 label = CASE_LABEL (l);
11508 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11509 }
11510 }
11511 else
11512 error_at (token->location,
11513 "case label %qE not within a switch statement",
11514 expr);
11515 }
11516 break;
11517
11518 case RID_DEFAULT:
11519 /* Consume the `default' token. */
11520 cp_lexer_consume_token (parser->lexer);
11521
11522 if (parser->in_switch_statement_p)
11523 {
11524 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11525 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11526 {
11527 label = CASE_LABEL (l);
11528 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11529 }
11530 }
11531 else
11532 error_at (token->location, "case label not within a switch statement");
11533 break;
11534
11535 default:
11536 /* Anything else must be an ordinary label. */
11537 label = finish_label_stmt (cp_parser_identifier (parser));
11538 if (label && TREE_CODE (label) == LABEL_DECL)
11539 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11540 break;
11541 }
11542
11543 /* Require the `:' token. */
11544 cp_parser_require (parser, CPP_COLON, RT_COLON);
11545
11546 /* An ordinary label may optionally be followed by attributes.
11547 However, this is only permitted if the attributes are then
11548 followed by a semicolon. This is because, for backward
11549 compatibility, when parsing
11550 lab: __attribute__ ((unused)) int i;
11551 we want the attribute to attach to "i", not "lab". */
11552 if (label != NULL_TREE
11553 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11554 {
11555 tree attrs;
11556 cp_parser_parse_tentatively (parser);
11557 attrs = cp_parser_gnu_attributes_opt (parser);
11558 if (attrs == NULL_TREE
11559 /* And fallthrough always binds to the expression-statement. */
11560 || attribute_fallthrough_p (attrs)
11561 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11562 cp_parser_abort_tentative_parse (parser);
11563 else if (!cp_parser_parse_definitely (parser))
11564 ;
11565 else
11566 attributes = attr_chainon (attributes, attrs);
11567 }
11568
11569 if (attributes != NULL_TREE)
11570 cplus_decl_attributes (&label, attributes, 0);
11571
11572 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11573 }
11574
11575 /* Parse an expression-statement.
11576
11577 expression-statement:
11578 expression [opt] ;
11579
11580 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11581 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11582 indicates whether this expression-statement is part of an
11583 expression statement. */
11584
11585 static tree
11586 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11587 {
11588 tree statement = NULL_TREE;
11589 cp_token *token = cp_lexer_peek_token (parser->lexer);
11590 location_t loc = token->location;
11591
11592 /* There might be attribute fallthrough. */
11593 tree attr = cp_parser_gnu_attributes_opt (parser);
11594
11595 /* If the next token is a ';', then there is no expression
11596 statement. */
11597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11598 {
11599 statement = cp_parser_expression (parser);
11600 if (statement == error_mark_node
11601 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11602 {
11603 cp_parser_skip_to_end_of_block_or_statement (parser);
11604 return error_mark_node;
11605 }
11606 }
11607
11608 /* Handle [[fallthrough]];. */
11609 if (attribute_fallthrough_p (attr))
11610 {
11611 /* The next token after the fallthrough attribute is ';'. */
11612 if (statement == NULL_TREE)
11613 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11614 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11615 void_type_node, 0);
11616 else
11617 warning_at (loc, OPT_Wattributes,
11618 "%<fallthrough%> attribute not followed by %<;%>");
11619 attr = NULL_TREE;
11620 }
11621
11622 /* Allow "[[fallthrough]];", but warn otherwise. */
11623 if (attr != NULL_TREE)
11624 warning_at (loc, OPT_Wattributes,
11625 "attributes at the beginning of statement are ignored");
11626
11627 /* Give a helpful message for "A<T>::type t;" and the like. */
11628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11629 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11630 {
11631 if (TREE_CODE (statement) == SCOPE_REF)
11632 error_at (token->location, "need %<typename%> before %qE because "
11633 "%qT is a dependent scope",
11634 statement, TREE_OPERAND (statement, 0));
11635 else if (is_overloaded_fn (statement)
11636 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11637 {
11638 /* A::A a; */
11639 tree fn = get_first_fn (statement);
11640 error_at (token->location,
11641 "%<%T::%D%> names the constructor, not the type",
11642 DECL_CONTEXT (fn), DECL_NAME (fn));
11643 }
11644 }
11645
11646 /* Consume the final `;'. */
11647 cp_parser_consume_semicolon_at_end_of_statement (parser);
11648
11649 if (in_statement_expr
11650 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11651 /* This is the final expression statement of a statement
11652 expression. */
11653 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11654 else if (statement)
11655 statement = finish_expr_stmt (statement);
11656
11657 return statement;
11658 }
11659
11660 /* Parse a compound-statement.
11661
11662 compound-statement:
11663 { statement-seq [opt] }
11664
11665 GNU extension:
11666
11667 compound-statement:
11668 { label-declaration-seq [opt] statement-seq [opt] }
11669
11670 label-declaration-seq:
11671 label-declaration
11672 label-declaration-seq label-declaration
11673
11674 Returns a tree representing the statement. */
11675
11676 static tree
11677 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11678 int bcs_flags, bool function_body)
11679 {
11680 tree compound_stmt;
11681 matching_braces braces;
11682
11683 /* Consume the `{'. */
11684 if (!braces.require_open (parser))
11685 return error_mark_node;
11686 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11687 && !function_body && cxx_dialect < cxx14)
11688 pedwarn (input_location, OPT_Wpedantic,
11689 "compound-statement in %<constexpr%> function");
11690 /* Begin the compound-statement. */
11691 compound_stmt = begin_compound_stmt (bcs_flags);
11692 /* If the next keyword is `__label__' we have a label declaration. */
11693 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11694 cp_parser_label_declaration (parser);
11695 /* Parse an (optional) statement-seq. */
11696 cp_parser_statement_seq_opt (parser, in_statement_expr);
11697 /* Finish the compound-statement. */
11698 finish_compound_stmt (compound_stmt);
11699 /* Consume the `}'. */
11700 braces.require_close (parser);
11701
11702 return compound_stmt;
11703 }
11704
11705 /* Parse an (optional) statement-seq.
11706
11707 statement-seq:
11708 statement
11709 statement-seq [opt] statement */
11710
11711 static void
11712 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11713 {
11714 /* Scan statements until there aren't any more. */
11715 while (true)
11716 {
11717 cp_token *token = cp_lexer_peek_token (parser->lexer);
11718
11719 /* If we are looking at a `}', then we have run out of
11720 statements; the same is true if we have reached the end
11721 of file, or have stumbled upon a stray '@end'. */
11722 if (token->type == CPP_CLOSE_BRACE
11723 || token->type == CPP_EOF
11724 || token->type == CPP_PRAGMA_EOL
11725 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11726 break;
11727
11728 /* If we are in a compound statement and find 'else' then
11729 something went wrong. */
11730 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11731 {
11732 if (parser->in_statement & IN_IF_STMT)
11733 break;
11734 else
11735 {
11736 token = cp_lexer_consume_token (parser->lexer);
11737 error_at (token->location, "%<else%> without a previous %<if%>");
11738 }
11739 }
11740
11741 /* Parse the statement. */
11742 cp_parser_statement (parser, in_statement_expr, true, NULL);
11743 }
11744 }
11745
11746 /* Return true if this is the C++20 version of range-based-for with
11747 init-statement. */
11748
11749 static bool
11750 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11751 {
11752 bool r = false;
11753
11754 /* Save tokens so that we can put them back. */
11755 cp_lexer_save_tokens (parser->lexer);
11756
11757 /* There has to be an unnested ; followed by an unnested :. */
11758 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11759 /*recovering=*/false,
11760 CPP_SEMICOLON,
11761 /*consume_paren=*/false) != -1)
11762 goto out;
11763
11764 /* We found the semicolon, eat it now. */
11765 cp_lexer_consume_token (parser->lexer);
11766
11767 /* Now look for ':' that is not nested in () or {}. */
11768 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11769 /*recovering=*/false,
11770 CPP_COLON,
11771 /*consume_paren=*/false) == -1);
11772
11773 out:
11774 /* Roll back the tokens we skipped. */
11775 cp_lexer_rollback_tokens (parser->lexer);
11776
11777 return r;
11778 }
11779
11780 /* Return true if we're looking at (init; cond), false otherwise. */
11781
11782 static bool
11783 cp_parser_init_statement_p (cp_parser *parser)
11784 {
11785 /* Save tokens so that we can put them back. */
11786 cp_lexer_save_tokens (parser->lexer);
11787
11788 /* Look for ';' that is not nested in () or {}. */
11789 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11790 /*recovering=*/false,
11791 CPP_SEMICOLON,
11792 /*consume_paren=*/false);
11793
11794 /* Roll back the tokens we skipped. */
11795 cp_lexer_rollback_tokens (parser->lexer);
11796
11797 return ret == -1;
11798 }
11799
11800 /* Parse a selection-statement.
11801
11802 selection-statement:
11803 if ( init-statement [opt] condition ) statement
11804 if ( init-statement [opt] condition ) statement else statement
11805 switch ( init-statement [opt] condition ) statement
11806
11807 Returns the new IF_STMT or SWITCH_STMT.
11808
11809 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11810 is a (possibly labeled) if statement which is not enclosed in
11811 braces and has an else clause. This is used to implement
11812 -Wparentheses.
11813
11814 CHAIN is a vector of if-else-if conditions. This is used to implement
11815 -Wduplicated-cond. */
11816
11817 static tree
11818 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11819 vec<tree> *chain)
11820 {
11821 cp_token *token;
11822 enum rid keyword;
11823 token_indent_info guard_tinfo;
11824
11825 if (if_p != NULL)
11826 *if_p = false;
11827
11828 /* Peek at the next token. */
11829 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11830 guard_tinfo = get_token_indent_info (token);
11831
11832 /* See what kind of keyword it is. */
11833 keyword = token->keyword;
11834 switch (keyword)
11835 {
11836 case RID_IF:
11837 case RID_SWITCH:
11838 {
11839 tree statement;
11840 tree condition;
11841
11842 bool cx = false;
11843 if (keyword == RID_IF
11844 && cp_lexer_next_token_is_keyword (parser->lexer,
11845 RID_CONSTEXPR))
11846 {
11847 cx = true;
11848 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11849 if (cxx_dialect < cxx17)
11850 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11851 "with %<-std=c++17%> or %<-std=gnu++17%>");
11852 }
11853
11854 /* Look for the `('. */
11855 matching_parens parens;
11856 if (!parens.require_open (parser))
11857 {
11858 cp_parser_skip_to_end_of_statement (parser);
11859 return error_mark_node;
11860 }
11861
11862 /* Begin the selection-statement. */
11863 if (keyword == RID_IF)
11864 {
11865 statement = begin_if_stmt ();
11866 IF_STMT_CONSTEXPR_P (statement) = cx;
11867 }
11868 else
11869 statement = begin_switch_stmt ();
11870
11871 /* Parse the optional init-statement. */
11872 if (cp_parser_init_statement_p (parser))
11873 {
11874 tree decl;
11875 if (cxx_dialect < cxx17)
11876 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11877 "init-statement in selection statements only available "
11878 "with %<-std=c++17%> or %<-std=gnu++17%>");
11879 cp_parser_init_statement (parser, &decl);
11880 }
11881
11882 /* Parse the condition. */
11883 condition = cp_parser_condition (parser);
11884 /* Look for the `)'. */
11885 if (!parens.require_close (parser))
11886 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11887 /*consume_paren=*/true);
11888
11889 if (keyword == RID_IF)
11890 {
11891 bool nested_if;
11892 unsigned char in_statement;
11893
11894 /* Add the condition. */
11895 condition = finish_if_stmt_cond (condition, statement);
11896
11897 if (warn_duplicated_cond)
11898 warn_duplicated_cond_add_or_warn (token->location, condition,
11899 &chain);
11900
11901 /* Parse the then-clause. */
11902 in_statement = parser->in_statement;
11903 parser->in_statement |= IN_IF_STMT;
11904
11905 /* Outside a template, the non-selected branch of a constexpr
11906 if is a 'discarded statement', i.e. unevaluated. */
11907 bool was_discarded = in_discarded_stmt;
11908 bool discard_then = (cx && !processing_template_decl
11909 && integer_zerop (condition));
11910 if (discard_then)
11911 {
11912 in_discarded_stmt = true;
11913 ++c_inhibit_evaluation_warnings;
11914 }
11915
11916 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11917 guard_tinfo);
11918
11919 parser->in_statement = in_statement;
11920
11921 finish_then_clause (statement);
11922
11923 if (discard_then)
11924 {
11925 THEN_CLAUSE (statement) = NULL_TREE;
11926 in_discarded_stmt = was_discarded;
11927 --c_inhibit_evaluation_warnings;
11928 }
11929
11930 /* If the next token is `else', parse the else-clause. */
11931 if (cp_lexer_next_token_is_keyword (parser->lexer,
11932 RID_ELSE))
11933 {
11934 bool discard_else = (cx && !processing_template_decl
11935 && integer_nonzerop (condition));
11936 if (discard_else)
11937 {
11938 in_discarded_stmt = true;
11939 ++c_inhibit_evaluation_warnings;
11940 }
11941
11942 guard_tinfo
11943 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11944 /* Consume the `else' keyword. */
11945 cp_lexer_consume_token (parser->lexer);
11946 if (warn_duplicated_cond)
11947 {
11948 if (cp_lexer_next_token_is_keyword (parser->lexer,
11949 RID_IF)
11950 && chain == NULL)
11951 {
11952 /* We've got "if (COND) else if (COND2)". Start
11953 the condition chain and add COND as the first
11954 element. */
11955 chain = new vec<tree> ();
11956 if (!CONSTANT_CLASS_P (condition)
11957 && !TREE_SIDE_EFFECTS (condition))
11958 {
11959 /* Wrap it in a NOP_EXPR so that we can set the
11960 location of the condition. */
11961 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11962 condition);
11963 SET_EXPR_LOCATION (e, token->location);
11964 chain->safe_push (e);
11965 }
11966 }
11967 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11968 RID_IF))
11969 {
11970 /* This is if-else without subsequent if. Zap the
11971 condition chain; we would have already warned at
11972 this point. */
11973 delete chain;
11974 chain = NULL;
11975 }
11976 }
11977 begin_else_clause (statement);
11978 /* Parse the else-clause. */
11979 cp_parser_implicitly_scoped_statement (parser, NULL,
11980 guard_tinfo, chain);
11981
11982 finish_else_clause (statement);
11983
11984 /* If we are currently parsing a then-clause, then
11985 IF_P will not be NULL. We set it to true to
11986 indicate that this if statement has an else clause.
11987 This may trigger the Wparentheses warning below
11988 when we get back up to the parent if statement. */
11989 if (if_p != NULL)
11990 *if_p = true;
11991
11992 if (discard_else)
11993 {
11994 ELSE_CLAUSE (statement) = NULL_TREE;
11995 in_discarded_stmt = was_discarded;
11996 --c_inhibit_evaluation_warnings;
11997 }
11998 }
11999 else
12000 {
12001 /* This if statement does not have an else clause. If
12002 NESTED_IF is true, then the then-clause has an if
12003 statement which does have an else clause. We warn
12004 about the potential ambiguity. */
12005 if (nested_if)
12006 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
12007 "suggest explicit braces to avoid ambiguous"
12008 " %<else%>");
12009 if (warn_duplicated_cond)
12010 {
12011 /* We don't need the condition chain anymore. */
12012 delete chain;
12013 chain = NULL;
12014 }
12015 }
12016
12017 /* Now we're all done with the if-statement. */
12018 finish_if_stmt (statement);
12019 }
12020 else
12021 {
12022 bool in_switch_statement_p;
12023 unsigned char in_statement;
12024
12025 /* Add the condition. */
12026 finish_switch_cond (condition, statement);
12027
12028 /* Parse the body of the switch-statement. */
12029 in_switch_statement_p = parser->in_switch_statement_p;
12030 in_statement = parser->in_statement;
12031 parser->in_switch_statement_p = true;
12032 parser->in_statement |= IN_SWITCH_STMT;
12033 cp_parser_implicitly_scoped_statement (parser, if_p,
12034 guard_tinfo);
12035 parser->in_switch_statement_p = in_switch_statement_p;
12036 parser->in_statement = in_statement;
12037
12038 /* Now we're all done with the switch-statement. */
12039 finish_switch_stmt (statement);
12040 }
12041
12042 return statement;
12043 }
12044 break;
12045
12046 default:
12047 cp_parser_error (parser, "expected selection-statement");
12048 return error_mark_node;
12049 }
12050 }
12051
12052 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
12053 If we have seen at least one decl-specifier, and the next token
12054 is not a parenthesis, then we must be looking at a declaration.
12055 (After "int (" we might be looking at a functional cast.) */
12056
12057 static void
12058 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
12059 bool any_specifiers_p)
12060 {
12061 if (any_specifiers_p
12062 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12063 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12064 && !cp_parser_error_occurred (parser))
12065 cp_parser_commit_to_tentative_parse (parser);
12066 }
12067
12068 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
12069 The declarator shall not specify a function or an array. Returns
12070 TRUE if the declarator is valid, FALSE otherwise. */
12071
12072 static bool
12073 cp_parser_check_condition_declarator (cp_parser* parser,
12074 cp_declarator *declarator,
12075 location_t loc)
12076 {
12077 if (declarator == cp_error_declarator
12078 || function_declarator_p (declarator)
12079 || declarator->kind == cdk_array)
12080 {
12081 if (declarator == cp_error_declarator)
12082 /* Already complained. */;
12083 else if (declarator->kind == cdk_array)
12084 error_at (loc, "condition declares an array");
12085 else
12086 error_at (loc, "condition declares a function");
12087 if (parser->fully_implicit_function_template_p)
12088 abort_fully_implicit_template (parser);
12089 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
12090 /*or_comma=*/false,
12091 /*consume_paren=*/false);
12092 return false;
12093 }
12094 else
12095 return true;
12096 }
12097
12098 /* Parse a condition.
12099
12100 condition:
12101 expression
12102 type-specifier-seq declarator = initializer-clause
12103 type-specifier-seq declarator braced-init-list
12104
12105 GNU Extension:
12106
12107 condition:
12108 type-specifier-seq declarator asm-specification [opt]
12109 attributes [opt] = assignment-expression
12110
12111 Returns the expression that should be tested. */
12112
12113 static tree
12114 cp_parser_condition (cp_parser* parser)
12115 {
12116 cp_decl_specifier_seq type_specifiers;
12117 const char *saved_message;
12118 int declares_class_or_enum;
12119
12120 /* Try the declaration first. */
12121 cp_parser_parse_tentatively (parser);
12122 /* New types are not allowed in the type-specifier-seq for a
12123 condition. */
12124 saved_message = parser->type_definition_forbidden_message;
12125 parser->type_definition_forbidden_message
12126 = G_("types may not be defined in conditions");
12127 /* Parse the type-specifier-seq. */
12128 cp_parser_decl_specifier_seq (parser,
12129 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
12130 &type_specifiers,
12131 &declares_class_or_enum);
12132 /* Restore the saved message. */
12133 parser->type_definition_forbidden_message = saved_message;
12134
12135 /* Gather the attributes that were provided with the
12136 decl-specifiers. */
12137 tree prefix_attributes = type_specifiers.attributes;
12138
12139 cp_parser_maybe_commit_to_declaration (parser,
12140 type_specifiers.any_specifiers_p);
12141
12142 /* If all is well, we might be looking at a declaration. */
12143 if (!cp_parser_error_occurred (parser))
12144 {
12145 tree decl;
12146 tree asm_specification;
12147 tree attributes;
12148 cp_declarator *declarator;
12149 tree initializer = NULL_TREE;
12150 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12151
12152 /* Parse the declarator. */
12153 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12154 CP_PARSER_FLAGS_NONE,
12155 /*ctor_dtor_or_conv_p=*/NULL,
12156 /*parenthesized_p=*/NULL,
12157 /*member_p=*/false,
12158 /*friend_p=*/false,
12159 /*static_p=*/false);
12160 /* Parse the attributes. */
12161 attributes = cp_parser_attributes_opt (parser);
12162 /* Parse the asm-specification. */
12163 asm_specification = cp_parser_asm_specification_opt (parser);
12164 /* If the next token is not an `=' or '{', then we might still be
12165 looking at an expression. For example:
12166
12167 if (A(a).x)
12168
12169 looks like a decl-specifier-seq and a declarator -- but then
12170 there is no `=', so this is an expression. */
12171 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12172 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12173 cp_parser_simulate_error (parser);
12174
12175 /* If we did see an `=' or '{', then we are looking at a declaration
12176 for sure. */
12177 if (cp_parser_parse_definitely (parser))
12178 {
12179 tree pushed_scope;
12180 bool non_constant_p = false;
12181 int flags = LOOKUP_ONLYCONVERTING;
12182
12183 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12184 return error_mark_node;
12185
12186 /* Create the declaration. */
12187 decl = start_decl (declarator, &type_specifiers,
12188 /*initialized_p=*/true,
12189 attributes, prefix_attributes,
12190 &pushed_scope);
12191
12192 /* Parse the initializer. */
12193 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12194 {
12195 initializer = cp_parser_braced_list (parser, &non_constant_p);
12196 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12197 flags = 0;
12198 }
12199 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12200 {
12201 /* Consume the `='. */
12202 cp_lexer_consume_token (parser->lexer);
12203 initializer = cp_parser_initializer_clause (parser,
12204 &non_constant_p);
12205 }
12206 else
12207 {
12208 cp_parser_error (parser, "expected initializer");
12209 initializer = error_mark_node;
12210 }
12211 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12212 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12213
12214 /* Process the initializer. */
12215 cp_finish_decl (decl,
12216 initializer, !non_constant_p,
12217 asm_specification,
12218 flags);
12219
12220 if (pushed_scope)
12221 pop_scope (pushed_scope);
12222
12223 return convert_from_reference (decl);
12224 }
12225 }
12226 /* If we didn't even get past the declarator successfully, we are
12227 definitely not looking at a declaration. */
12228 else
12229 cp_parser_abort_tentative_parse (parser);
12230
12231 /* Otherwise, we are looking at an expression. */
12232 return cp_parser_expression (parser);
12233 }
12234
12235 /* Parses a for-statement or range-for-statement until the closing ')',
12236 not included. */
12237
12238 static tree
12239 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12240 {
12241 tree init, scope, decl;
12242 bool is_range_for;
12243
12244 /* Begin the for-statement. */
12245 scope = begin_for_scope (&init);
12246
12247 /* Parse the initialization. */
12248 is_range_for = cp_parser_init_statement (parser, &decl);
12249
12250 if (is_range_for)
12251 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12252 false);
12253 else
12254 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12255 }
12256
12257 static tree
12258 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12259 unsigned short unroll)
12260 {
12261 /* Normal for loop */
12262 tree condition = NULL_TREE;
12263 tree expression = NULL_TREE;
12264 tree stmt;
12265
12266 stmt = begin_for_stmt (scope, init);
12267 /* The init-statement has already been parsed in
12268 cp_parser_init_statement, so no work is needed here. */
12269 finish_init_stmt (stmt);
12270
12271 /* If there's a condition, process it. */
12272 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12273 condition = cp_parser_condition (parser);
12274 else if (ivdep)
12275 {
12276 cp_parser_error (parser, "missing loop condition in loop with "
12277 "%<GCC ivdep%> pragma");
12278 condition = error_mark_node;
12279 }
12280 else if (unroll)
12281 {
12282 cp_parser_error (parser, "missing loop condition in loop with "
12283 "%<GCC unroll%> pragma");
12284 condition = error_mark_node;
12285 }
12286 finish_for_cond (condition, stmt, ivdep, unroll);
12287 /* Look for the `;'. */
12288 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12289
12290 /* If there's an expression, process it. */
12291 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12292 expression = cp_parser_expression (parser);
12293 finish_for_expr (expression, stmt);
12294
12295 return stmt;
12296 }
12297
12298 /* Tries to parse a range-based for-statement:
12299
12300 range-based-for:
12301 decl-specifier-seq declarator : expression
12302
12303 The decl-specifier-seq declarator and the `:' are already parsed by
12304 cp_parser_init_statement. If processing_template_decl it returns a
12305 newly created RANGE_FOR_STMT; if not, it is converted to a
12306 regular FOR_STMT. */
12307
12308 static tree
12309 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12310 bool ivdep, unsigned short unroll, bool is_omp)
12311 {
12312 tree stmt, range_expr;
12313 auto_vec <cxx_binding *, 16> bindings;
12314 auto_vec <tree, 16> names;
12315 tree decomp_first_name = NULL_TREE;
12316 unsigned int decomp_cnt = 0;
12317
12318 /* Get the range declaration momentarily out of the way so that
12319 the range expression doesn't clash with it. */
12320 if (range_decl != error_mark_node)
12321 {
12322 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12323 {
12324 tree v = DECL_VALUE_EXPR (range_decl);
12325 /* For decomposition declaration get all of the corresponding
12326 declarations out of the way. */
12327 if (TREE_CODE (v) == ARRAY_REF
12328 && VAR_P (TREE_OPERAND (v, 0))
12329 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12330 {
12331 tree d = range_decl;
12332 range_decl = TREE_OPERAND (v, 0);
12333 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12334 decomp_first_name = d;
12335 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12336 {
12337 tree name = DECL_NAME (d);
12338 names.safe_push (name);
12339 bindings.safe_push (IDENTIFIER_BINDING (name));
12340 IDENTIFIER_BINDING (name)
12341 = IDENTIFIER_BINDING (name)->previous;
12342 }
12343 }
12344 }
12345 if (names.is_empty ())
12346 {
12347 tree name = DECL_NAME (range_decl);
12348 names.safe_push (name);
12349 bindings.safe_push (IDENTIFIER_BINDING (name));
12350 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12351 }
12352 }
12353
12354 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12355 {
12356 bool expr_non_constant_p;
12357 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12358 }
12359 else
12360 range_expr = cp_parser_expression (parser);
12361
12362 /* Put the range declaration(s) back into scope. */
12363 for (unsigned int i = 0; i < names.length (); i++)
12364 {
12365 cxx_binding *binding = bindings[i];
12366 binding->previous = IDENTIFIER_BINDING (names[i]);
12367 IDENTIFIER_BINDING (names[i]) = binding;
12368 }
12369
12370 /* finish_omp_for has its own code for the following, so just
12371 return the range_expr instead. */
12372 if (is_omp)
12373 return range_expr;
12374
12375 /* If in template, STMT is converted to a normal for-statement
12376 at instantiation. If not, it is done just ahead. */
12377 if (processing_template_decl)
12378 {
12379 if (check_for_bare_parameter_packs (range_expr))
12380 range_expr = error_mark_node;
12381 stmt = begin_range_for_stmt (scope, init);
12382 if (ivdep)
12383 RANGE_FOR_IVDEP (stmt) = 1;
12384 if (unroll)
12385 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12386 finish_range_for_decl (stmt, range_decl, range_expr);
12387 if (!type_dependent_expression_p (range_expr)
12388 /* do_auto_deduction doesn't mess with template init-lists. */
12389 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12390 do_range_for_auto_deduction (range_decl, range_expr);
12391 }
12392 else
12393 {
12394 stmt = begin_for_stmt (scope, init);
12395 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12396 decomp_first_name, decomp_cnt, ivdep,
12397 unroll);
12398 }
12399 return stmt;
12400 }
12401
12402 /* Subroutine of cp_convert_range_for: given the initializer expression,
12403 builds up the range temporary. */
12404
12405 static tree
12406 build_range_temp (tree range_expr)
12407 {
12408 tree range_type, range_temp;
12409
12410 /* Find out the type deduced by the declaration
12411 `auto &&__range = range_expr'. */
12412 range_type = cp_build_reference_type (make_auto (), true);
12413 range_type = do_auto_deduction (range_type, range_expr,
12414 type_uses_auto (range_type));
12415
12416 /* Create the __range variable. */
12417 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12418 range_type);
12419 TREE_USED (range_temp) = 1;
12420 DECL_ARTIFICIAL (range_temp) = 1;
12421
12422 return range_temp;
12423 }
12424
12425 /* Used by cp_parser_range_for in template context: we aren't going to
12426 do a full conversion yet, but we still need to resolve auto in the
12427 type of the for-range-declaration if present. This is basically
12428 a shortcut version of cp_convert_range_for. */
12429
12430 static void
12431 do_range_for_auto_deduction (tree decl, tree range_expr)
12432 {
12433 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12434 if (auto_node)
12435 {
12436 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12437 range_temp = convert_from_reference (build_range_temp (range_expr));
12438 iter_type = (cp_parser_perform_range_for_lookup
12439 (range_temp, &begin_dummy, &end_dummy));
12440 if (iter_type)
12441 {
12442 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12443 iter_type);
12444 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12445 RO_UNARY_STAR,
12446 tf_warning_or_error);
12447 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12448 iter_decl, auto_node);
12449 }
12450 }
12451 }
12452
12453 /* Converts a range-based for-statement into a normal
12454 for-statement, as per the definition.
12455
12456 for (RANGE_DECL : RANGE_EXPR)
12457 BLOCK
12458
12459 should be equivalent to:
12460
12461 {
12462 auto &&__range = RANGE_EXPR;
12463 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12464 __begin != __end;
12465 ++__begin)
12466 {
12467 RANGE_DECL = *__begin;
12468 BLOCK
12469 }
12470 }
12471
12472 If RANGE_EXPR is an array:
12473 BEGIN_EXPR = __range
12474 END_EXPR = __range + ARRAY_SIZE(__range)
12475 Else if RANGE_EXPR has a member 'begin' or 'end':
12476 BEGIN_EXPR = __range.begin()
12477 END_EXPR = __range.end()
12478 Else:
12479 BEGIN_EXPR = begin(__range)
12480 END_EXPR = end(__range);
12481
12482 If __range has a member 'begin' but not 'end', or vice versa, we must
12483 still use the second alternative (it will surely fail, however).
12484 When calling begin()/end() in the third alternative we must use
12485 argument dependent lookup, but always considering 'std' as an associated
12486 namespace. */
12487
12488 tree
12489 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12490 tree decomp_first_name, unsigned int decomp_cnt,
12491 bool ivdep, unsigned short unroll)
12492 {
12493 tree begin, end;
12494 tree iter_type, begin_expr, end_expr;
12495 tree condition, expression;
12496
12497 range_expr = mark_lvalue_use (range_expr);
12498
12499 if (range_decl == error_mark_node || range_expr == error_mark_node)
12500 /* If an error happened previously do nothing or else a lot of
12501 unhelpful errors would be issued. */
12502 begin_expr = end_expr = iter_type = error_mark_node;
12503 else
12504 {
12505 tree range_temp;
12506
12507 if (VAR_P (range_expr)
12508 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12509 /* Can't bind a reference to an array of runtime bound. */
12510 range_temp = range_expr;
12511 else
12512 {
12513 range_temp = build_range_temp (range_expr);
12514 pushdecl (range_temp);
12515 cp_finish_decl (range_temp, range_expr,
12516 /*is_constant_init*/false, NULL_TREE,
12517 LOOKUP_ONLYCONVERTING);
12518 range_temp = convert_from_reference (range_temp);
12519 }
12520 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12521 &begin_expr, &end_expr);
12522 }
12523
12524 /* The new for initialization statement. */
12525 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12526 iter_type);
12527 TREE_USED (begin) = 1;
12528 DECL_ARTIFICIAL (begin) = 1;
12529 pushdecl (begin);
12530 cp_finish_decl (begin, begin_expr,
12531 /*is_constant_init*/false, NULL_TREE,
12532 LOOKUP_ONLYCONVERTING);
12533
12534 if (cxx_dialect >= cxx17)
12535 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12536 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12537 TREE_USED (end) = 1;
12538 DECL_ARTIFICIAL (end) = 1;
12539 pushdecl (end);
12540 cp_finish_decl (end, end_expr,
12541 /*is_constant_init*/false, NULL_TREE,
12542 LOOKUP_ONLYCONVERTING);
12543
12544 finish_init_stmt (statement);
12545
12546 /* The new for condition. */
12547 condition = build_x_binary_op (input_location, NE_EXPR,
12548 begin, ERROR_MARK,
12549 end, ERROR_MARK,
12550 NULL, tf_warning_or_error);
12551 finish_for_cond (condition, statement, ivdep, unroll);
12552
12553 /* The new increment expression. */
12554 expression = finish_unary_op_expr (input_location,
12555 PREINCREMENT_EXPR, begin,
12556 tf_warning_or_error);
12557 finish_for_expr (expression, statement);
12558
12559 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12560 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12561
12562 /* The declaration is initialized with *__begin inside the loop body. */
12563 cp_finish_decl (range_decl,
12564 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12565 tf_warning_or_error),
12566 /*is_constant_init*/false, NULL_TREE,
12567 LOOKUP_ONLYCONVERTING);
12568 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12569 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12570
12571 return statement;
12572 }
12573
12574 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12575 We need to solve both at the same time because the method used
12576 depends on the existence of members begin or end.
12577 Returns the type deduced for the iterator expression. */
12578
12579 static tree
12580 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12581 {
12582 if (error_operand_p (range))
12583 {
12584 *begin = *end = error_mark_node;
12585 return error_mark_node;
12586 }
12587
12588 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12589 {
12590 error ("range-based %<for%> expression of type %qT "
12591 "has incomplete type", TREE_TYPE (range));
12592 *begin = *end = error_mark_node;
12593 return error_mark_node;
12594 }
12595 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12596 {
12597 /* If RANGE is an array, we will use pointer arithmetic. */
12598 *begin = decay_conversion (range, tf_warning_or_error);
12599 *end = build_binary_op (input_location, PLUS_EXPR,
12600 range,
12601 array_type_nelts_top (TREE_TYPE (range)),
12602 false);
12603 return TREE_TYPE (*begin);
12604 }
12605 else
12606 {
12607 /* If it is not an array, we must do a bit of magic. */
12608 tree id_begin, id_end;
12609 tree member_begin, member_end;
12610
12611 *begin = *end = error_mark_node;
12612
12613 id_begin = get_identifier ("begin");
12614 id_end = get_identifier ("end");
12615 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12616 /*protect=*/2, /*want_type=*/false,
12617 tf_warning_or_error);
12618 member_end = lookup_member (TREE_TYPE (range), id_end,
12619 /*protect=*/2, /*want_type=*/false,
12620 tf_warning_or_error);
12621
12622 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12623 {
12624 /* Use the member functions. */
12625 *begin = cp_parser_range_for_member_function (range, id_begin);
12626 *end = cp_parser_range_for_member_function (range, id_end);
12627 }
12628 else
12629 {
12630 /* Use global functions with ADL. */
12631 releasing_vec vec;
12632
12633 vec_safe_push (vec, range);
12634
12635 member_begin = perform_koenig_lookup (id_begin, vec,
12636 tf_warning_or_error);
12637 *begin = finish_call_expr (member_begin, &vec, false, true,
12638 tf_warning_or_error);
12639 member_end = perform_koenig_lookup (id_end, vec,
12640 tf_warning_or_error);
12641 *end = finish_call_expr (member_end, &vec, false, true,
12642 tf_warning_or_error);
12643 }
12644
12645 /* Last common checks. */
12646 if (*begin == error_mark_node || *end == error_mark_node)
12647 {
12648 /* If one of the expressions is an error do no more checks. */
12649 *begin = *end = error_mark_node;
12650 return error_mark_node;
12651 }
12652 else if (type_dependent_expression_p (*begin)
12653 || type_dependent_expression_p (*end))
12654 /* Can happen, when, eg, in a template context, Koenig lookup
12655 can't resolve begin/end (c++/58503). */
12656 return NULL_TREE;
12657 else
12658 {
12659 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12660 /* The unqualified type of the __begin and __end temporaries should
12661 be the same, as required by the multiple auto declaration. */
12662 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12663 {
12664 if (cxx_dialect >= cxx17
12665 && (build_x_binary_op (input_location, NE_EXPR,
12666 *begin, ERROR_MARK,
12667 *end, ERROR_MARK,
12668 NULL, tf_none)
12669 != error_mark_node))
12670 /* P0184R0 allows __begin and __end to have different types,
12671 but make sure they are comparable so we can give a better
12672 diagnostic. */;
12673 else
12674 error ("inconsistent begin/end types in range-based %<for%> "
12675 "statement: %qT and %qT",
12676 TREE_TYPE (*begin), TREE_TYPE (*end));
12677 }
12678 return iter_type;
12679 }
12680 }
12681 }
12682
12683 /* Helper function for cp_parser_perform_range_for_lookup.
12684 Builds a tree for RANGE.IDENTIFIER(). */
12685
12686 static tree
12687 cp_parser_range_for_member_function (tree range, tree identifier)
12688 {
12689 tree member, res;
12690
12691 member = finish_class_member_access_expr (range, identifier,
12692 false, tf_warning_or_error);
12693 if (member == error_mark_node)
12694 return error_mark_node;
12695
12696 releasing_vec vec;
12697 res = finish_call_expr (member, &vec,
12698 /*disallow_virtual=*/false,
12699 /*koenig_p=*/false,
12700 tf_warning_or_error);
12701 return res;
12702 }
12703
12704 /* Parse an iteration-statement.
12705
12706 iteration-statement:
12707 while ( condition ) statement
12708 do statement while ( expression ) ;
12709 for ( init-statement condition [opt] ; expression [opt] )
12710 statement
12711
12712 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12713
12714 static tree
12715 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12716 unsigned short unroll)
12717 {
12718 cp_token *token;
12719 enum rid keyword;
12720 tree statement;
12721 unsigned char in_statement;
12722 token_indent_info guard_tinfo;
12723
12724 /* Peek at the next token. */
12725 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12726 if (!token)
12727 return error_mark_node;
12728
12729 guard_tinfo = get_token_indent_info (token);
12730
12731 /* Remember whether or not we are already within an iteration
12732 statement. */
12733 in_statement = parser->in_statement;
12734
12735 /* See what kind of keyword it is. */
12736 keyword = token->keyword;
12737 switch (keyword)
12738 {
12739 case RID_WHILE:
12740 {
12741 tree condition;
12742
12743 /* Begin the while-statement. */
12744 statement = begin_while_stmt ();
12745 /* Look for the `('. */
12746 matching_parens parens;
12747 parens.require_open (parser);
12748 /* Parse the condition. */
12749 condition = cp_parser_condition (parser);
12750 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12751 /* Look for the `)'. */
12752 parens.require_close (parser);
12753 /* Parse the dependent statement. */
12754 parser->in_statement = IN_ITERATION_STMT;
12755 bool prev = note_iteration_stmt_body_start ();
12756 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12757 note_iteration_stmt_body_end (prev);
12758 parser->in_statement = in_statement;
12759 /* We're done with the while-statement. */
12760 finish_while_stmt (statement);
12761 }
12762 break;
12763
12764 case RID_DO:
12765 {
12766 tree expression;
12767
12768 /* Begin the do-statement. */
12769 statement = begin_do_stmt ();
12770 /* Parse the body of the do-statement. */
12771 parser->in_statement = IN_ITERATION_STMT;
12772 bool prev = note_iteration_stmt_body_start ();
12773 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12774 note_iteration_stmt_body_end (prev);
12775 parser->in_statement = in_statement;
12776 finish_do_body (statement);
12777 /* Look for the `while' keyword. */
12778 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12779 /* Look for the `('. */
12780 matching_parens parens;
12781 parens.require_open (parser);
12782 /* Parse the expression. */
12783 expression = cp_parser_expression (parser);
12784 /* We're done with the do-statement. */
12785 finish_do_stmt (expression, statement, ivdep, unroll);
12786 /* Look for the `)'. */
12787 parens.require_close (parser);
12788 /* Look for the `;'. */
12789 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12790 }
12791 break;
12792
12793 case RID_FOR:
12794 {
12795 /* Look for the `('. */
12796 matching_parens parens;
12797 parens.require_open (parser);
12798
12799 statement = cp_parser_for (parser, ivdep, unroll);
12800
12801 /* Look for the `)'. */
12802 parens.require_close (parser);
12803
12804 /* Parse the body of the for-statement. */
12805 parser->in_statement = IN_ITERATION_STMT;
12806 bool prev = note_iteration_stmt_body_start ();
12807 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12808 note_iteration_stmt_body_end (prev);
12809 parser->in_statement = in_statement;
12810
12811 /* We're done with the for-statement. */
12812 finish_for_stmt (statement);
12813 }
12814 break;
12815
12816 default:
12817 cp_parser_error (parser, "expected iteration-statement");
12818 statement = error_mark_node;
12819 break;
12820 }
12821
12822 return statement;
12823 }
12824
12825 /* Parse a init-statement or the declarator of a range-based-for.
12826 Returns true if a range-based-for declaration is seen.
12827
12828 init-statement:
12829 expression-statement
12830 simple-declaration */
12831
12832 static bool
12833 cp_parser_init_statement (cp_parser *parser, tree *decl)
12834 {
12835 /* If the next token is a `;', then we have an empty
12836 expression-statement. Grammatically, this is also a
12837 simple-declaration, but an invalid one, because it does not
12838 declare anything. Therefore, if we did not handle this case
12839 specially, we would issue an error message about an invalid
12840 declaration. */
12841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12842 {
12843 bool is_range_for = false;
12844 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12845
12846 /* Try to parse the init-statement. */
12847 if (cp_parser_range_based_for_with_init_p (parser))
12848 {
12849 tree dummy;
12850 cp_parser_parse_tentatively (parser);
12851 /* Parse the declaration. */
12852 cp_parser_simple_declaration (parser,
12853 /*function_definition_allowed_p=*/false,
12854 &dummy);
12855 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12856 if (!cp_parser_parse_definitely (parser))
12857 /* That didn't work, try to parse it as an expression-statement. */
12858 cp_parser_expression_statement (parser, NULL_TREE);
12859
12860 if (cxx_dialect < cxx2a)
12861 {
12862 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12863 "range-based %<for%> loops with initializer only "
12864 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
12865 *decl = error_mark_node;
12866 }
12867 }
12868
12869 /* A colon is used in range-based for. */
12870 parser->colon_corrects_to_scope_p = false;
12871
12872 /* We're going to speculatively look for a declaration, falling back
12873 to an expression, if necessary. */
12874 cp_parser_parse_tentatively (parser);
12875 /* Parse the declaration. */
12876 cp_parser_simple_declaration (parser,
12877 /*function_definition_allowed_p=*/false,
12878 decl);
12879 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12880 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12881 {
12882 /* It is a range-for, consume the ':'. */
12883 cp_lexer_consume_token (parser->lexer);
12884 is_range_for = true;
12885 if (cxx_dialect < cxx11)
12886 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12887 "range-based %<for%> loops only available with "
12888 "%<-std=c++11%> or %<-std=gnu++11%>");
12889 }
12890 else
12891 /* The ';' is not consumed yet because we told
12892 cp_parser_simple_declaration not to. */
12893 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12894
12895 if (cp_parser_parse_definitely (parser))
12896 return is_range_for;
12897 /* If the tentative parse failed, then we shall need to look for an
12898 expression-statement. */
12899 }
12900 /* If we are here, it is an expression-statement. */
12901 cp_parser_expression_statement (parser, NULL_TREE);
12902 return false;
12903 }
12904
12905 /* Parse a jump-statement.
12906
12907 jump-statement:
12908 break ;
12909 continue ;
12910 return expression [opt] ;
12911 return braced-init-list ;
12912 goto identifier ;
12913
12914 GNU extension:
12915
12916 jump-statement:
12917 goto * expression ;
12918
12919 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12920
12921 static tree
12922 cp_parser_jump_statement (cp_parser* parser)
12923 {
12924 tree statement = error_mark_node;
12925 cp_token *token;
12926 enum rid keyword;
12927 unsigned char in_statement;
12928
12929 /* Peek at the next token. */
12930 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12931 if (!token)
12932 return error_mark_node;
12933
12934 /* See what kind of keyword it is. */
12935 keyword = token->keyword;
12936 switch (keyword)
12937 {
12938 case RID_BREAK:
12939 in_statement = parser->in_statement & ~IN_IF_STMT;
12940 switch (in_statement)
12941 {
12942 case 0:
12943 error_at (token->location, "break statement not within loop or switch");
12944 break;
12945 default:
12946 gcc_assert ((in_statement & IN_SWITCH_STMT)
12947 || in_statement == IN_ITERATION_STMT);
12948 statement = finish_break_stmt ();
12949 if (in_statement == IN_ITERATION_STMT)
12950 break_maybe_infinite_loop ();
12951 break;
12952 case IN_OMP_BLOCK:
12953 error_at (token->location, "invalid exit from OpenMP structured block");
12954 break;
12955 case IN_OMP_FOR:
12956 error_at (token->location, "break statement used with OpenMP for loop");
12957 break;
12958 }
12959 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12960 break;
12961
12962 case RID_CONTINUE:
12963 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12964 {
12965 case 0:
12966 error_at (token->location, "continue statement not within a loop");
12967 break;
12968 /* Fall through. */
12969 case IN_ITERATION_STMT:
12970 case IN_OMP_FOR:
12971 statement = finish_continue_stmt ();
12972 break;
12973 case IN_OMP_BLOCK:
12974 error_at (token->location, "invalid exit from OpenMP structured block");
12975 break;
12976 default:
12977 gcc_unreachable ();
12978 }
12979 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12980 break;
12981
12982 case RID_RETURN:
12983 {
12984 tree expr;
12985 bool expr_non_constant_p;
12986
12987 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12988 {
12989 cp_lexer_set_source_position (parser->lexer);
12990 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12991 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12992 }
12993 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12994 expr = cp_parser_expression (parser);
12995 else
12996 /* If the next token is a `;', then there is no
12997 expression. */
12998 expr = NULL_TREE;
12999 /* Build the return-statement. */
13000 if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
13001 /* Don't deduce from a discarded return statement. */;
13002 else
13003 statement = finish_return_stmt (expr);
13004 /* Look for the final `;'. */
13005 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13006 }
13007 break;
13008
13009 case RID_GOTO:
13010 if (parser->in_function_body
13011 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
13012 {
13013 error ("%<goto%> in %<constexpr%> function");
13014 cp_function_chain->invalid_constexpr = true;
13015 }
13016
13017 /* Create the goto-statement. */
13018 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
13019 {
13020 /* Issue a warning about this use of a GNU extension. */
13021 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
13022 /* Consume the '*' token. */
13023 cp_lexer_consume_token (parser->lexer);
13024 /* Parse the dependent expression. */
13025 finish_goto_stmt (cp_parser_expression (parser));
13026 }
13027 else
13028 finish_goto_stmt (cp_parser_identifier (parser));
13029 /* Look for the final `;'. */
13030 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13031 break;
13032
13033 default:
13034 cp_parser_error (parser, "expected jump-statement");
13035 break;
13036 }
13037
13038 return statement;
13039 }
13040
13041 /* Parse a declaration-statement.
13042
13043 declaration-statement:
13044 block-declaration */
13045
13046 static void
13047 cp_parser_declaration_statement (cp_parser* parser)
13048 {
13049 void *p;
13050
13051 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13052 p = obstack_alloc (&declarator_obstack, 0);
13053
13054 /* Parse the block-declaration. */
13055 cp_parser_block_declaration (parser, /*statement_p=*/true);
13056
13057 /* Free any declarators allocated. */
13058 obstack_free (&declarator_obstack, p);
13059 }
13060
13061 /* Some dependent statements (like `if (cond) statement'), are
13062 implicitly in their own scope. In other words, if the statement is
13063 a single statement (as opposed to a compound-statement), it is
13064 none-the-less treated as if it were enclosed in braces. Any
13065 declarations appearing in the dependent statement are out of scope
13066 after control passes that point. This function parses a statement,
13067 but ensures that is in its own scope, even if it is not a
13068 compound-statement.
13069
13070 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13071 is a (possibly labeled) if statement which is not enclosed in
13072 braces and has an else clause. This is used to implement
13073 -Wparentheses.
13074
13075 CHAIN is a vector of if-else-if conditions. This is used to implement
13076 -Wduplicated-cond.
13077
13078 Returns the new statement. */
13079
13080 static tree
13081 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
13082 const token_indent_info &guard_tinfo,
13083 vec<tree> *chain)
13084 {
13085 tree statement;
13086 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
13087 location_t body_loc_after_labels = UNKNOWN_LOCATION;
13088 token_indent_info body_tinfo
13089 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13090
13091 if (if_p != NULL)
13092 *if_p = false;
13093
13094 /* Mark if () ; with a special NOP_EXPR. */
13095 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13096 {
13097 cp_lexer_consume_token (parser->lexer);
13098 statement = add_stmt (build_empty_stmt (body_loc));
13099
13100 if (guard_tinfo.keyword == RID_IF
13101 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
13102 warning_at (body_loc, OPT_Wempty_body,
13103 "suggest braces around empty body in an %<if%> statement");
13104 else if (guard_tinfo.keyword == RID_ELSE)
13105 warning_at (body_loc, OPT_Wempty_body,
13106 "suggest braces around empty body in an %<else%> statement");
13107 }
13108 /* if a compound is opened, we simply parse the statement directly. */
13109 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13110 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
13111 /* If the token is not a `{', then we must take special action. */
13112 else
13113 {
13114 /* Create a compound-statement. */
13115 statement = begin_compound_stmt (0);
13116 /* Parse the dependent-statement. */
13117 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
13118 &body_loc_after_labels);
13119 /* Finish the dummy compound-statement. */
13120 finish_compound_stmt (statement);
13121 }
13122
13123 token_indent_info next_tinfo
13124 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13125 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13126
13127 if (body_loc_after_labels != UNKNOWN_LOCATION
13128 && next_tinfo.type != CPP_SEMICOLON)
13129 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
13130 guard_tinfo.location, guard_tinfo.keyword);
13131
13132 /* Return the statement. */
13133 return statement;
13134 }
13135
13136 /* For some dependent statements (like `while (cond) statement'), we
13137 have already created a scope. Therefore, even if the dependent
13138 statement is a compound-statement, we do not want to create another
13139 scope. */
13140
13141 static void
13142 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
13143 const token_indent_info &guard_tinfo)
13144 {
13145 /* If the token is a `{', then we must take special action. */
13146 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13147 {
13148 token_indent_info body_tinfo
13149 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13150 location_t loc_after_labels = UNKNOWN_LOCATION;
13151
13152 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
13153 &loc_after_labels);
13154 token_indent_info next_tinfo
13155 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13156 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13157
13158 if (loc_after_labels != UNKNOWN_LOCATION
13159 && next_tinfo.type != CPP_SEMICOLON)
13160 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13161 guard_tinfo.location,
13162 guard_tinfo.keyword);
13163 }
13164 else
13165 {
13166 /* Avoid calling cp_parser_compound_statement, so that we
13167 don't create a new scope. Do everything else by hand. */
13168 matching_braces braces;
13169 braces.require_open (parser);
13170 /* If the next keyword is `__label__' we have a label declaration. */
13171 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13172 cp_parser_label_declaration (parser);
13173 /* Parse an (optional) statement-seq. */
13174 cp_parser_statement_seq_opt (parser, NULL_TREE);
13175 braces.require_close (parser);
13176 }
13177 }
13178
13179 /* Declarations [gram.dcl.dcl] */
13180
13181 /* Parse an optional declaration-sequence.
13182
13183 declaration-seq:
13184 declaration
13185 declaration-seq declaration */
13186
13187 static void
13188 cp_parser_declaration_seq_opt (cp_parser* parser)
13189 {
13190 while (true)
13191 {
13192 cp_token *token = cp_lexer_peek_token (parser->lexer);
13193
13194 if (token->type == CPP_CLOSE_BRACE
13195 || token->type == CPP_EOF)
13196 break;
13197 else
13198 cp_parser_toplevel_declaration (parser);
13199 }
13200 }
13201
13202 /* Parse a declaration.
13203
13204 declaration:
13205 block-declaration
13206 function-definition
13207 template-declaration
13208 explicit-instantiation
13209 explicit-specialization
13210 linkage-specification
13211 namespace-definition
13212
13213 C++17:
13214 deduction-guide
13215
13216 GNU extension:
13217
13218 declaration:
13219 __extension__ declaration */
13220
13221 static void
13222 cp_parser_declaration (cp_parser* parser)
13223 {
13224 cp_token token1;
13225 cp_token token2;
13226 int saved_pedantic;
13227 void *p;
13228 tree attributes = NULL_TREE;
13229
13230 /* Check for the `__extension__' keyword. */
13231 if (cp_parser_extension_opt (parser, &saved_pedantic))
13232 {
13233 /* Parse the qualified declaration. */
13234 cp_parser_declaration (parser);
13235 /* Restore the PEDANTIC flag. */
13236 pedantic = saved_pedantic;
13237
13238 return;
13239 }
13240
13241 /* Try to figure out what kind of declaration is present. */
13242 token1 = *cp_lexer_peek_token (parser->lexer);
13243
13244 if (token1.type != CPP_EOF)
13245 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13246 else
13247 {
13248 token2.type = CPP_EOF;
13249 token2.keyword = RID_MAX;
13250 }
13251
13252 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13253 p = obstack_alloc (&declarator_obstack, 0);
13254
13255 /* If the next token is `extern' and the following token is a string
13256 literal, then we have a linkage specification. */
13257 if (token1.keyword == RID_EXTERN
13258 && cp_parser_is_pure_string_literal (&token2))
13259 cp_parser_linkage_specification (parser);
13260 /* If the next token is `template', then we have either a template
13261 declaration, an explicit instantiation, or an explicit
13262 specialization. */
13263 else if (token1.keyword == RID_TEMPLATE)
13264 {
13265 /* `template <>' indicates a template specialization. */
13266 if (token2.type == CPP_LESS
13267 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13268 cp_parser_explicit_specialization (parser);
13269 /* `template <' indicates a template declaration. */
13270 else if (token2.type == CPP_LESS)
13271 cp_parser_template_declaration (parser, /*member_p=*/false);
13272 /* Anything else must be an explicit instantiation. */
13273 else
13274 cp_parser_explicit_instantiation (parser);
13275 }
13276 /* If the next token is `export', then we have a template
13277 declaration. */
13278 else if (token1.keyword == RID_EXPORT)
13279 cp_parser_template_declaration (parser, /*member_p=*/false);
13280 /* If the next token is `extern', 'static' or 'inline' and the one
13281 after that is `template', we have a GNU extended explicit
13282 instantiation directive. */
13283 else if (cp_parser_allow_gnu_extensions_p (parser)
13284 && (token1.keyword == RID_EXTERN
13285 || token1.keyword == RID_STATIC
13286 || token1.keyword == RID_INLINE)
13287 && token2.keyword == RID_TEMPLATE)
13288 cp_parser_explicit_instantiation (parser);
13289 /* If the next token is `namespace', check for a named or unnamed
13290 namespace definition. */
13291 else if (token1.keyword == RID_NAMESPACE
13292 && (/* A named namespace definition. */
13293 (token2.type == CPP_NAME
13294 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13295 != CPP_EQ))
13296 || (token2.type == CPP_OPEN_SQUARE
13297 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13298 == CPP_OPEN_SQUARE)
13299 /* An unnamed namespace definition. */
13300 || token2.type == CPP_OPEN_BRACE
13301 || token2.keyword == RID_ATTRIBUTE))
13302 cp_parser_namespace_definition (parser);
13303 /* An inline (associated) namespace definition. */
13304 else if (token1.keyword == RID_INLINE
13305 && token2.keyword == RID_NAMESPACE)
13306 cp_parser_namespace_definition (parser);
13307 /* Objective-C++ declaration/definition. */
13308 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13309 cp_parser_objc_declaration (parser, NULL_TREE);
13310 else if (c_dialect_objc ()
13311 && token1.keyword == RID_ATTRIBUTE
13312 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13313 cp_parser_objc_declaration (parser, attributes);
13314 /* At this point we may have a template declared by a concept
13315 introduction. */
13316 else if (flag_concepts
13317 && cp_parser_template_declaration_after_export (parser,
13318 /*member_p=*/false))
13319 /* We did. */;
13320 else
13321 /* Try to parse a block-declaration, or a function-definition. */
13322 cp_parser_block_declaration (parser, /*statement_p=*/false);
13323
13324 /* Free any declarators allocated. */
13325 obstack_free (&declarator_obstack, p);
13326 }
13327
13328 /* Parse a namespace-scope declaration. */
13329
13330 static void
13331 cp_parser_toplevel_declaration (cp_parser* parser)
13332 {
13333 cp_token *token = cp_lexer_peek_token (parser->lexer);
13334
13335 if (token->type == CPP_PRAGMA)
13336 /* A top-level declaration can consist solely of a #pragma. A
13337 nested declaration cannot, so this is done here and not in
13338 cp_parser_declaration. (A #pragma at block scope is
13339 handled in cp_parser_statement.) */
13340 cp_parser_pragma (parser, pragma_external, NULL);
13341 else if (token->type == CPP_SEMICOLON)
13342 {
13343 /* A declaration consisting of a single semicolon is
13344 invalid. Allow it unless we're being pedantic. */
13345 cp_lexer_consume_token (parser->lexer);
13346 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13347 }
13348 else
13349 /* Parse the declaration itself. */
13350 cp_parser_declaration (parser);
13351 }
13352
13353 /* Parse a block-declaration.
13354
13355 block-declaration:
13356 simple-declaration
13357 asm-definition
13358 namespace-alias-definition
13359 using-declaration
13360 using-directive
13361
13362 GNU Extension:
13363
13364 block-declaration:
13365 __extension__ block-declaration
13366
13367 C++0x Extension:
13368
13369 block-declaration:
13370 static_assert-declaration
13371
13372 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13373 part of a declaration-statement. */
13374
13375 static void
13376 cp_parser_block_declaration (cp_parser *parser,
13377 bool statement_p)
13378 {
13379 cp_token *token1;
13380 int saved_pedantic;
13381
13382 /* Check for the `__extension__' keyword. */
13383 if (cp_parser_extension_opt (parser, &saved_pedantic))
13384 {
13385 /* Parse the qualified declaration. */
13386 cp_parser_block_declaration (parser, statement_p);
13387 /* Restore the PEDANTIC flag. */
13388 pedantic = saved_pedantic;
13389
13390 return;
13391 }
13392
13393 /* Peek at the next token to figure out which kind of declaration is
13394 present. */
13395 token1 = cp_lexer_peek_token (parser->lexer);
13396
13397 /* If the next keyword is `asm', we have an asm-definition. */
13398 if (token1->keyword == RID_ASM)
13399 {
13400 if (statement_p)
13401 cp_parser_commit_to_tentative_parse (parser);
13402 cp_parser_asm_definition (parser);
13403 }
13404 /* If the next keyword is `namespace', we have a
13405 namespace-alias-definition. */
13406 else if (token1->keyword == RID_NAMESPACE)
13407 cp_parser_namespace_alias_definition (parser);
13408 /* If the next keyword is `using', we have a
13409 using-declaration, a using-directive, or an alias-declaration. */
13410 else if (token1->keyword == RID_USING)
13411 {
13412 cp_token *token2;
13413
13414 if (statement_p)
13415 cp_parser_commit_to_tentative_parse (parser);
13416 /* If the token after `using' is `namespace', then we have a
13417 using-directive. */
13418 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13419 if (token2->keyword == RID_NAMESPACE)
13420 cp_parser_using_directive (parser);
13421 /* If the second token after 'using' is '=', then we have an
13422 alias-declaration. */
13423 else if (cxx_dialect >= cxx11
13424 && token2->type == CPP_NAME
13425 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13426 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13427 cp_parser_alias_declaration (parser);
13428 /* Otherwise, it's a using-declaration. */
13429 else
13430 cp_parser_using_declaration (parser,
13431 /*access_declaration_p=*/false);
13432 }
13433 /* If the next keyword is `__label__' we have a misplaced label
13434 declaration. */
13435 else if (token1->keyword == RID_LABEL)
13436 {
13437 cp_lexer_consume_token (parser->lexer);
13438 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13439 cp_parser_skip_to_end_of_statement (parser);
13440 /* If the next token is now a `;', consume it. */
13441 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13442 cp_lexer_consume_token (parser->lexer);
13443 }
13444 /* If the next token is `static_assert' we have a static assertion. */
13445 else if (token1->keyword == RID_STATIC_ASSERT)
13446 cp_parser_static_assert (parser, /*member_p=*/false);
13447 /* Anything else must be a simple-declaration. */
13448 else
13449 cp_parser_simple_declaration (parser, !statement_p,
13450 /*maybe_range_for_decl*/NULL);
13451 }
13452
13453 /* Parse a simple-declaration.
13454
13455 simple-declaration:
13456 decl-specifier-seq [opt] init-declarator-list [opt] ;
13457 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13458 brace-or-equal-initializer ;
13459
13460 init-declarator-list:
13461 init-declarator
13462 init-declarator-list , init-declarator
13463
13464 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13465 function-definition as a simple-declaration.
13466
13467 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13468 parsed declaration if it is an uninitialized single declarator not followed
13469 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13470 if present, will not be consumed. */
13471
13472 static void
13473 cp_parser_simple_declaration (cp_parser* parser,
13474 bool function_definition_allowed_p,
13475 tree *maybe_range_for_decl)
13476 {
13477 cp_decl_specifier_seq decl_specifiers;
13478 int declares_class_or_enum;
13479 bool saw_declarator;
13480 location_t comma_loc = UNKNOWN_LOCATION;
13481 location_t init_loc = UNKNOWN_LOCATION;
13482
13483 if (maybe_range_for_decl)
13484 *maybe_range_for_decl = NULL_TREE;
13485
13486 /* Defer access checks until we know what is being declared; the
13487 checks for names appearing in the decl-specifier-seq should be
13488 done as if we were in the scope of the thing being declared. */
13489 push_deferring_access_checks (dk_deferred);
13490
13491 /* Parse the decl-specifier-seq. We have to keep track of whether
13492 or not the decl-specifier-seq declares a named class or
13493 enumeration type, since that is the only case in which the
13494 init-declarator-list is allowed to be empty.
13495
13496 [dcl.dcl]
13497
13498 In a simple-declaration, the optional init-declarator-list can be
13499 omitted only when declaring a class or enumeration, that is when
13500 the decl-specifier-seq contains either a class-specifier, an
13501 elaborated-type-specifier, or an enum-specifier. */
13502 cp_parser_decl_specifier_seq (parser,
13503 CP_PARSER_FLAGS_OPTIONAL,
13504 &decl_specifiers,
13505 &declares_class_or_enum);
13506 /* We no longer need to defer access checks. */
13507 stop_deferring_access_checks ();
13508
13509 /* In a block scope, a valid declaration must always have a
13510 decl-specifier-seq. By not trying to parse declarators, we can
13511 resolve the declaration/expression ambiguity more quickly. */
13512 if (!function_definition_allowed_p
13513 && !decl_specifiers.any_specifiers_p)
13514 {
13515 cp_parser_error (parser, "expected declaration");
13516 goto done;
13517 }
13518
13519 /* If the next two tokens are both identifiers, the code is
13520 erroneous. The usual cause of this situation is code like:
13521
13522 T t;
13523
13524 where "T" should name a type -- but does not. */
13525 if (!decl_specifiers.any_type_specifiers_p
13526 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13527 {
13528 /* If parsing tentatively, we should commit; we really are
13529 looking at a declaration. */
13530 cp_parser_commit_to_tentative_parse (parser);
13531 /* Give up. */
13532 goto done;
13533 }
13534
13535 cp_parser_maybe_commit_to_declaration (parser,
13536 decl_specifiers.any_specifiers_p);
13537
13538 /* Look for C++17 decomposition declaration. */
13539 for (size_t n = 1; ; n++)
13540 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13541 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13542 continue;
13543 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13544 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13545 && decl_specifiers.any_specifiers_p)
13546 {
13547 tree decl
13548 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13549 maybe_range_for_decl,
13550 &init_loc);
13551
13552 /* The next token should be either a `,' or a `;'. */
13553 cp_token *token = cp_lexer_peek_token (parser->lexer);
13554 /* If it's a `;', we are done. */
13555 if (token->type == CPP_SEMICOLON)
13556 goto finish;
13557 else if (maybe_range_for_decl)
13558 {
13559 if (*maybe_range_for_decl == NULL_TREE)
13560 *maybe_range_for_decl = error_mark_node;
13561 goto finish;
13562 }
13563 /* Anything else is an error. */
13564 else
13565 {
13566 /* If we have already issued an error message we don't need
13567 to issue another one. */
13568 if ((decl != error_mark_node
13569 && DECL_INITIAL (decl) != error_mark_node)
13570 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13571 cp_parser_error (parser, "expected %<,%> or %<;%>");
13572 /* Skip tokens until we reach the end of the statement. */
13573 cp_parser_skip_to_end_of_statement (parser);
13574 /* If the next token is now a `;', consume it. */
13575 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13576 cp_lexer_consume_token (parser->lexer);
13577 goto done;
13578 }
13579 }
13580 else
13581 break;
13582
13583 tree last_type;
13584 bool auto_specifier_p;
13585 /* NULL_TREE if both variable and function declaration are allowed,
13586 error_mark_node if function declaration are not allowed and
13587 a FUNCTION_DECL that should be diagnosed if it is followed by
13588 variable declarations. */
13589 tree auto_function_declaration;
13590
13591 last_type = NULL_TREE;
13592 auto_specifier_p
13593 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13594 auto_function_declaration = NULL_TREE;
13595
13596 /* Keep going until we hit the `;' at the end of the simple
13597 declaration. */
13598 saw_declarator = false;
13599 while (cp_lexer_next_token_is_not (parser->lexer,
13600 CPP_SEMICOLON))
13601 {
13602 cp_token *token;
13603 bool function_definition_p;
13604 tree decl;
13605 tree auto_result = NULL_TREE;
13606
13607 if (saw_declarator)
13608 {
13609 /* If we are processing next declarator, comma is expected */
13610 token = cp_lexer_peek_token (parser->lexer);
13611 gcc_assert (token->type == CPP_COMMA);
13612 cp_lexer_consume_token (parser->lexer);
13613 if (maybe_range_for_decl)
13614 {
13615 *maybe_range_for_decl = error_mark_node;
13616 if (comma_loc == UNKNOWN_LOCATION)
13617 comma_loc = token->location;
13618 }
13619 }
13620 else
13621 saw_declarator = true;
13622
13623 /* Parse the init-declarator. */
13624 decl = cp_parser_init_declarator (parser,
13625 CP_PARSER_FLAGS_NONE,
13626 &decl_specifiers,
13627 /*checks=*/NULL,
13628 function_definition_allowed_p,
13629 /*member_p=*/false,
13630 declares_class_or_enum,
13631 &function_definition_p,
13632 maybe_range_for_decl,
13633 &init_loc,
13634 &auto_result);
13635 /* If an error occurred while parsing tentatively, exit quickly.
13636 (That usually happens when in the body of a function; each
13637 statement is treated as a declaration-statement until proven
13638 otherwise.) */
13639 if (cp_parser_error_occurred (parser))
13640 goto done;
13641
13642 if (auto_specifier_p && cxx_dialect >= cxx14)
13643 {
13644 /* If the init-declarator-list contains more than one
13645 init-declarator, they shall all form declarations of
13646 variables. */
13647 if (auto_function_declaration == NULL_TREE)
13648 auto_function_declaration
13649 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13650 else if (TREE_CODE (decl) == FUNCTION_DECL
13651 || auto_function_declaration != error_mark_node)
13652 {
13653 error_at (decl_specifiers.locations[ds_type_spec],
13654 "non-variable %qD in declaration with more than one "
13655 "declarator with placeholder type",
13656 TREE_CODE (decl) == FUNCTION_DECL
13657 ? decl : auto_function_declaration);
13658 auto_function_declaration = error_mark_node;
13659 }
13660 }
13661
13662 if (auto_result
13663 && (!processing_template_decl || !type_uses_auto (auto_result)))
13664 {
13665 if (last_type
13666 && last_type != error_mark_node
13667 && !same_type_p (auto_result, last_type))
13668 {
13669 /* If the list of declarators contains more than one declarator,
13670 the type of each declared variable is determined as described
13671 above. If the type deduced for the template parameter U is not
13672 the same in each deduction, the program is ill-formed. */
13673 error_at (decl_specifiers.locations[ds_type_spec],
13674 "inconsistent deduction for %qT: %qT and then %qT",
13675 decl_specifiers.type, last_type, auto_result);
13676 last_type = error_mark_node;
13677 }
13678 else
13679 last_type = auto_result;
13680 }
13681
13682 /* Handle function definitions specially. */
13683 if (function_definition_p)
13684 {
13685 /* If the next token is a `,', then we are probably
13686 processing something like:
13687
13688 void f() {}, *p;
13689
13690 which is erroneous. */
13691 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13692 {
13693 cp_token *token = cp_lexer_peek_token (parser->lexer);
13694 error_at (token->location,
13695 "mixing"
13696 " declarations and function-definitions is forbidden");
13697 }
13698 /* Otherwise, we're done with the list of declarators. */
13699 else
13700 {
13701 pop_deferring_access_checks ();
13702 return;
13703 }
13704 }
13705 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13706 *maybe_range_for_decl = decl;
13707 /* The next token should be either a `,' or a `;'. */
13708 token = cp_lexer_peek_token (parser->lexer);
13709 /* If it's a `,', there are more declarators to come. */
13710 if (token->type == CPP_COMMA)
13711 /* will be consumed next time around */;
13712 /* If it's a `;', we are done. */
13713 else if (token->type == CPP_SEMICOLON)
13714 break;
13715 else if (maybe_range_for_decl)
13716 {
13717 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13718 permerror (decl_specifiers.locations[ds_type_spec],
13719 "types may not be defined in a for-range-declaration");
13720 break;
13721 }
13722 /* Anything else is an error. */
13723 else
13724 {
13725 /* If we have already issued an error message we don't need
13726 to issue another one. */
13727 if ((decl != error_mark_node
13728 && DECL_INITIAL (decl) != error_mark_node)
13729 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13730 cp_parser_error (parser, "expected %<,%> or %<;%>");
13731 /* Skip tokens until we reach the end of the statement. */
13732 cp_parser_skip_to_end_of_statement (parser);
13733 /* If the next token is now a `;', consume it. */
13734 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13735 cp_lexer_consume_token (parser->lexer);
13736 goto done;
13737 }
13738 /* After the first time around, a function-definition is not
13739 allowed -- even if it was OK at first. For example:
13740
13741 int i, f() {}
13742
13743 is not valid. */
13744 function_definition_allowed_p = false;
13745 }
13746
13747 /* Issue an error message if no declarators are present, and the
13748 decl-specifier-seq does not itself declare a class or
13749 enumeration: [dcl.dcl]/3. */
13750 if (!saw_declarator)
13751 {
13752 if (cp_parser_declares_only_class_p (parser))
13753 {
13754 if (!declares_class_or_enum
13755 && decl_specifiers.type
13756 && OVERLOAD_TYPE_P (decl_specifiers.type))
13757 /* Ensure an error is issued anyway when finish_decltype_type,
13758 called via cp_parser_decl_specifier_seq, returns a class or
13759 an enumeration (c++/51786). */
13760 decl_specifiers.type = NULL_TREE;
13761 shadow_tag (&decl_specifiers);
13762 }
13763 /* Perform any deferred access checks. */
13764 perform_deferred_access_checks (tf_warning_or_error);
13765 }
13766
13767 /* Consume the `;'. */
13768 finish:
13769 if (!maybe_range_for_decl)
13770 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13771 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13772 {
13773 if (init_loc != UNKNOWN_LOCATION)
13774 error_at (init_loc, "initializer in range-based %<for%> loop");
13775 if (comma_loc != UNKNOWN_LOCATION)
13776 error_at (comma_loc,
13777 "multiple declarations in range-based %<for%> loop");
13778 }
13779
13780 done:
13781 pop_deferring_access_checks ();
13782 }
13783
13784 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13785 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13786 initializer ; */
13787
13788 static tree
13789 cp_parser_decomposition_declaration (cp_parser *parser,
13790 cp_decl_specifier_seq *decl_specifiers,
13791 tree *maybe_range_for_decl,
13792 location_t *init_loc)
13793 {
13794 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13795 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13796 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13797
13798 /* Parse the identifier-list. */
13799 auto_vec<cp_expr, 10> v;
13800 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13801 while (true)
13802 {
13803 cp_expr e = cp_parser_identifier (parser);
13804 if (e.get_value () == error_mark_node)
13805 break;
13806 v.safe_push (e);
13807 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13808 break;
13809 cp_lexer_consume_token (parser->lexer);
13810 }
13811
13812 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13813 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13814 {
13815 end_loc = UNKNOWN_LOCATION;
13816 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13817 false);
13818 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13819 cp_lexer_consume_token (parser->lexer);
13820 else
13821 {
13822 cp_parser_skip_to_end_of_statement (parser);
13823 return error_mark_node;
13824 }
13825 }
13826
13827 if (cxx_dialect < cxx17)
13828 pedwarn (loc, 0, "structured bindings only available with "
13829 "%<-std=c++17%> or %<-std=gnu++17%>");
13830
13831 tree pushed_scope;
13832 cp_declarator *declarator = make_declarator (cdk_decomp);
13833 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13834 declarator->id_loc = loc;
13835 if (ref_qual != REF_QUAL_NONE)
13836 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13837 ref_qual == REF_QUAL_RVALUE,
13838 NULL_TREE);
13839 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13840 NULL_TREE, decl_specifiers->attributes,
13841 &pushed_scope);
13842 tree orig_decl = decl;
13843
13844 unsigned int i;
13845 cp_expr e;
13846 cp_decl_specifier_seq decl_specs;
13847 clear_decl_specs (&decl_specs);
13848 decl_specs.type = make_auto ();
13849 tree prev = decl;
13850 FOR_EACH_VEC_ELT (v, i, e)
13851 {
13852 if (i == 0)
13853 declarator = make_id_declarator (NULL_TREE, e.get_value (),
13854 sfk_none, e.get_location ());
13855 else
13856 {
13857 declarator->u.id.unqualified_name = e.get_value ();
13858 declarator->id_loc = e.get_location ();
13859 }
13860 tree elt_pushed_scope;
13861 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13862 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13863 if (decl2 == error_mark_node)
13864 decl = error_mark_node;
13865 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13866 {
13867 /* Ensure we've diagnosed redeclaration if we aren't creating
13868 a new VAR_DECL. */
13869 gcc_assert (errorcount);
13870 decl = error_mark_node;
13871 }
13872 else
13873 prev = decl2;
13874 if (elt_pushed_scope)
13875 pop_scope (elt_pushed_scope);
13876 }
13877
13878 if (v.is_empty ())
13879 {
13880 error_at (loc, "empty structured binding declaration");
13881 decl = error_mark_node;
13882 }
13883
13884 if (maybe_range_for_decl == NULL
13885 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13886 {
13887 bool non_constant_p = false, is_direct_init = false;
13888 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13889 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13890 &non_constant_p);
13891 if (initializer == NULL_TREE
13892 || (TREE_CODE (initializer) == TREE_LIST
13893 && TREE_CHAIN (initializer))
13894 || (is_direct_init
13895 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13896 && CONSTRUCTOR_NELTS (initializer) != 1))
13897 {
13898 error_at (loc, "invalid initializer for structured binding "
13899 "declaration");
13900 initializer = error_mark_node;
13901 }
13902
13903 if (decl != error_mark_node)
13904 {
13905 int flags = (decl_spec_seq_has_spec_p (decl_specifiers, ds_constinit)
13906 ? LOOKUP_CONSTINIT : 0);
13907 cp_maybe_mangle_decomp (decl, prev, v.length ());
13908 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13909 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT)
13910 | flags);
13911 cp_finish_decomp (decl, prev, v.length ());
13912 }
13913 }
13914 else if (decl != error_mark_node)
13915 {
13916 *maybe_range_for_decl = prev;
13917 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13918 the underlying DECL. */
13919 cp_finish_decomp (decl, prev, v.length ());
13920 }
13921
13922 if (pushed_scope)
13923 pop_scope (pushed_scope);
13924
13925 if (decl == error_mark_node && DECL_P (orig_decl))
13926 {
13927 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13928 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13929 }
13930
13931 return decl;
13932 }
13933
13934 /* Parse a decl-specifier-seq.
13935
13936 decl-specifier-seq:
13937 decl-specifier-seq [opt] decl-specifier
13938 decl-specifier attribute-specifier-seq [opt] (C++11)
13939
13940 decl-specifier:
13941 storage-class-specifier
13942 type-specifier
13943 function-specifier
13944 friend
13945 typedef
13946
13947 GNU Extension:
13948
13949 decl-specifier:
13950 attributes
13951
13952 Concepts Extension:
13953
13954 decl-specifier:
13955 concept
13956
13957 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13958
13959 The parser flags FLAGS is used to control type-specifier parsing.
13960
13961 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13962 flags:
13963
13964 1: one of the decl-specifiers is an elaborated-type-specifier
13965 (i.e., a type declaration)
13966 2: one of the decl-specifiers is an enum-specifier or a
13967 class-specifier (i.e., a type definition)
13968
13969 */
13970
13971 static void
13972 cp_parser_decl_specifier_seq (cp_parser* parser,
13973 cp_parser_flags flags,
13974 cp_decl_specifier_seq *decl_specs,
13975 int* declares_class_or_enum)
13976 {
13977 bool constructor_possible_p = !parser->in_declarator_p;
13978 bool found_decl_spec = false;
13979 cp_token *start_token = NULL;
13980 cp_decl_spec ds;
13981
13982 /* Clear DECL_SPECS. */
13983 clear_decl_specs (decl_specs);
13984
13985 /* Assume no class or enumeration type is declared. */
13986 *declares_class_or_enum = 0;
13987
13988 /* Keep reading specifiers until there are no more to read. */
13989 while (true)
13990 {
13991 bool constructor_p;
13992 cp_token *token;
13993 ds = ds_last;
13994
13995 /* Peek at the next token. */
13996 token = cp_lexer_peek_token (parser->lexer);
13997
13998 /* Save the first token of the decl spec list for error
13999 reporting. */
14000 if (!start_token)
14001 start_token = token;
14002 /* Handle attributes. */
14003 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
14004 && cp_next_tokens_can_be_attribute_p (parser))
14005 {
14006 /* Parse the attributes. */
14007 tree attrs = cp_parser_attributes_opt (parser);
14008
14009 /* In a sequence of declaration specifiers, c++11 attributes
14010 appertain to the type that precede them. In that case
14011 [dcl.spec]/1 says:
14012
14013 The attribute-specifier-seq affects the type only for
14014 the declaration it appears in, not other declarations
14015 involving the same type.
14016
14017 But for now let's force the user to position the
14018 attribute either at the beginning of the declaration or
14019 after the declarator-id, which would clearly mean that it
14020 applies to the declarator. */
14021 if (cxx11_attribute_p (attrs))
14022 {
14023 if (!found_decl_spec)
14024 /* The c++11 attribute is at the beginning of the
14025 declaration. It appertains to the entity being
14026 declared. */;
14027 else
14028 {
14029 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
14030 {
14031 /* This is an attribute following a
14032 class-specifier. */
14033 if (decl_specs->type_definition_p)
14034 warn_misplaced_attr_for_class_type (token->location,
14035 decl_specs->type);
14036 attrs = NULL_TREE;
14037 }
14038 else
14039 {
14040 decl_specs->std_attributes
14041 = attr_chainon (decl_specs->std_attributes, attrs);
14042 if (decl_specs->locations[ds_std_attribute] == 0)
14043 decl_specs->locations[ds_std_attribute] = token->location;
14044 }
14045 continue;
14046 }
14047 }
14048
14049 decl_specs->attributes
14050 = attr_chainon (decl_specs->attributes, attrs);
14051 if (decl_specs->locations[ds_attribute] == 0)
14052 decl_specs->locations[ds_attribute] = token->location;
14053 continue;
14054 }
14055 /* Assume we will find a decl-specifier keyword. */
14056 found_decl_spec = true;
14057 /* If the next token is an appropriate keyword, we can simply
14058 add it to the list. */
14059 switch (token->keyword)
14060 {
14061 /* decl-specifier:
14062 friend
14063 constexpr
14064 constinit */
14065 case RID_FRIEND:
14066 if (!at_class_scope_p ())
14067 {
14068 gcc_rich_location richloc (token->location);
14069 richloc.add_fixit_remove ();
14070 error_at (&richloc, "%<friend%> used outside of class");
14071 cp_lexer_purge_token (parser->lexer);
14072 }
14073 else
14074 {
14075 ds = ds_friend;
14076 /* Consume the token. */
14077 cp_lexer_consume_token (parser->lexer);
14078 }
14079 break;
14080
14081 case RID_CONSTEXPR:
14082 ds = ds_constexpr;
14083 cp_lexer_consume_token (parser->lexer);
14084 break;
14085
14086 case RID_CONSTINIT:
14087 ds = ds_constinit;
14088 cp_lexer_consume_token (parser->lexer);
14089 break;
14090
14091 case RID_CONSTEVAL:
14092 ds = ds_consteval;
14093 cp_lexer_consume_token (parser->lexer);
14094 break;
14095
14096 case RID_CONCEPT:
14097 ds = ds_concept;
14098 cp_lexer_consume_token (parser->lexer);
14099
14100 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14101 break;
14102
14103 /* Warn for concept as a decl-specifier. We'll rewrite these as
14104 concept declarations later. */
14105 if (!flag_concepts_ts)
14106 {
14107 cp_token *next = cp_lexer_peek_token (parser->lexer);
14108 if (next->keyword == RID_BOOL)
14109 pedwarn (next->location, 0, "the %<bool%> keyword is not "
14110 "allowed in a C++20 concept definition");
14111 else
14112 pedwarn (token->location, 0, "C++20 concept definition syntax "
14113 "is %<concept <name> = <expr>%> ");
14114 }
14115
14116 /* In C++20 a concept definition is just 'concept name = expr;'
14117 Support that syntax as a TS extension by pretending we've seen
14118 the 'bool' specifier. */
14119 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14120 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
14121 && !decl_specs->any_type_specifiers_p)
14122 {
14123 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
14124 token, /*type_definition*/false);
14125 decl_specs->any_type_specifiers_p = true;
14126 }
14127 break;
14128
14129 /* function-specifier:
14130 inline
14131 virtual
14132 explicit */
14133 case RID_INLINE:
14134 case RID_VIRTUAL:
14135 case RID_EXPLICIT:
14136 cp_parser_function_specifier_opt (parser, decl_specs);
14137 break;
14138
14139 /* decl-specifier:
14140 typedef */
14141 case RID_TYPEDEF:
14142 ds = ds_typedef;
14143 /* Consume the token. */
14144 cp_lexer_consume_token (parser->lexer);
14145
14146 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14147 break;
14148
14149 /* A constructor declarator cannot appear in a typedef. */
14150 constructor_possible_p = false;
14151 /* The "typedef" keyword can only occur in a declaration; we
14152 may as well commit at this point. */
14153 cp_parser_commit_to_tentative_parse (parser);
14154
14155 if (decl_specs->storage_class != sc_none)
14156 decl_specs->conflicting_specifiers_p = true;
14157 break;
14158
14159 /* storage-class-specifier:
14160 auto
14161 register
14162 static
14163 extern
14164 mutable
14165
14166 GNU Extension:
14167 thread */
14168 case RID_AUTO:
14169 if (cxx_dialect == cxx98)
14170 {
14171 /* Consume the token. */
14172 cp_lexer_consume_token (parser->lexer);
14173
14174 /* Complain about `auto' as a storage specifier, if
14175 we're complaining about C++0x compatibility. */
14176 gcc_rich_location richloc (token->location);
14177 richloc.add_fixit_remove ();
14178 warning_at (&richloc, OPT_Wc__11_compat,
14179 "%<auto%> changes meaning in C++11; "
14180 "please remove it");
14181
14182 /* Set the storage class anyway. */
14183 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
14184 token);
14185 }
14186 else
14187 /* C++0x auto type-specifier. */
14188 found_decl_spec = false;
14189 break;
14190
14191 case RID_REGISTER:
14192 case RID_STATIC:
14193 case RID_EXTERN:
14194 case RID_MUTABLE:
14195 /* Consume the token. */
14196 cp_lexer_consume_token (parser->lexer);
14197 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
14198 token);
14199 break;
14200 case RID_THREAD:
14201 /* Consume the token. */
14202 ds = ds_thread;
14203 cp_lexer_consume_token (parser->lexer);
14204 break;
14205
14206 default:
14207 /* We did not yet find a decl-specifier yet. */
14208 found_decl_spec = false;
14209 break;
14210 }
14211
14212 if (found_decl_spec
14213 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14214 && token->keyword != RID_CONSTEXPR)
14215 error ("%<decl-specifier%> invalid in condition");
14216
14217 if (found_decl_spec
14218 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14219 && token->keyword != RID_MUTABLE
14220 && token->keyword != RID_CONSTEXPR
14221 && token->keyword != RID_CONSTEVAL)
14222 error_at (token->location, "%qD invalid in lambda",
14223 ridpointers[token->keyword]);
14224
14225 if (ds != ds_last)
14226 set_and_check_decl_spec_loc (decl_specs, ds, token);
14227
14228 /* Constructors are a special case. The `S' in `S()' is not a
14229 decl-specifier; it is the beginning of the declarator. */
14230 constructor_p
14231 = (!found_decl_spec
14232 && constructor_possible_p
14233 && (cp_parser_constructor_declarator_p
14234 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
14235 ds_friend))));
14236
14237 /* If we don't have a DECL_SPEC yet, then we must be looking at
14238 a type-specifier. */
14239 if (!found_decl_spec && !constructor_p)
14240 {
14241 int decl_spec_declares_class_or_enum;
14242 bool is_cv_qualifier;
14243 tree type_spec;
14244
14245 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14246 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14247
14248 type_spec
14249 = cp_parser_type_specifier (parser, flags,
14250 decl_specs,
14251 /*is_declaration=*/true,
14252 &decl_spec_declares_class_or_enum,
14253 &is_cv_qualifier);
14254 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14255
14256 /* If this type-specifier referenced a user-defined type
14257 (a typedef, class-name, etc.), then we can't allow any
14258 more such type-specifiers henceforth.
14259
14260 [dcl.spec]
14261
14262 The longest sequence of decl-specifiers that could
14263 possibly be a type name is taken as the
14264 decl-specifier-seq of a declaration. The sequence shall
14265 be self-consistent as described below.
14266
14267 [dcl.type]
14268
14269 As a general rule, at most one type-specifier is allowed
14270 in the complete decl-specifier-seq of a declaration. The
14271 only exceptions are the following:
14272
14273 -- const or volatile can be combined with any other
14274 type-specifier.
14275
14276 -- signed or unsigned can be combined with char, long,
14277 short, or int.
14278
14279 -- ..
14280
14281 Example:
14282
14283 typedef char* Pc;
14284 void g (const int Pc);
14285
14286 Here, Pc is *not* part of the decl-specifier seq; it's
14287 the declarator. Therefore, once we see a type-specifier
14288 (other than a cv-qualifier), we forbid any additional
14289 user-defined types. We *do* still allow things like `int
14290 int' to be considered a decl-specifier-seq, and issue the
14291 error message later. */
14292 if (type_spec && !is_cv_qualifier)
14293 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14294 /* A constructor declarator cannot follow a type-specifier. */
14295 if (type_spec)
14296 {
14297 constructor_possible_p = false;
14298 found_decl_spec = true;
14299 if (!is_cv_qualifier)
14300 decl_specs->any_type_specifiers_p = true;
14301
14302 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14303 error_at (token->location, "type-specifier invalid in lambda");
14304 }
14305 }
14306
14307 /* If we still do not have a DECL_SPEC, then there are no more
14308 decl-specifiers. */
14309 if (!found_decl_spec)
14310 break;
14311
14312 decl_specs->any_specifiers_p = true;
14313 /* After we see one decl-specifier, further decl-specifiers are
14314 always optional. */
14315 flags |= CP_PARSER_FLAGS_OPTIONAL;
14316 }
14317
14318 /* Don't allow a friend specifier with a class definition. */
14319 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14320 && (*declares_class_or_enum & 2))
14321 error_at (decl_specs->locations[ds_friend],
14322 "class definition may not be declared a friend");
14323 }
14324
14325 /* Parse an (optional) storage-class-specifier.
14326
14327 storage-class-specifier:
14328 auto
14329 register
14330 static
14331 extern
14332 mutable
14333
14334 GNU Extension:
14335
14336 storage-class-specifier:
14337 thread
14338
14339 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14340
14341 static tree
14342 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14343 {
14344 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14345 {
14346 case RID_AUTO:
14347 if (cxx_dialect != cxx98)
14348 return NULL_TREE;
14349 /* Fall through for C++98. */
14350 gcc_fallthrough ();
14351
14352 case RID_REGISTER:
14353 case RID_STATIC:
14354 case RID_EXTERN:
14355 case RID_MUTABLE:
14356 case RID_THREAD:
14357 /* Consume the token. */
14358 return cp_lexer_consume_token (parser->lexer)->u.value;
14359
14360 default:
14361 return NULL_TREE;
14362 }
14363 }
14364
14365 /* Parse an (optional) function-specifier.
14366
14367 function-specifier:
14368 inline
14369 virtual
14370 explicit
14371
14372 C++2A Extension:
14373 explicit(constant-expression)
14374
14375 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14376 Updates DECL_SPECS, if it is non-NULL. */
14377
14378 static tree
14379 cp_parser_function_specifier_opt (cp_parser* parser,
14380 cp_decl_specifier_seq *decl_specs)
14381 {
14382 cp_token *token = cp_lexer_peek_token (parser->lexer);
14383 switch (token->keyword)
14384 {
14385 case RID_INLINE:
14386 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14387 break;
14388
14389 case RID_VIRTUAL:
14390 /* 14.5.2.3 [temp.mem]
14391
14392 A member function template shall not be virtual. */
14393 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14394 && current_class_type)
14395 error_at (token->location, "templates may not be %<virtual%>");
14396 else
14397 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14398 break;
14399
14400 case RID_EXPLICIT:
14401 {
14402 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14403 /* If we see '(', it's C++20 explicit(bool). */
14404 tree expr;
14405 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14406 {
14407 matching_parens parens;
14408 parens.consume_open (parser);
14409
14410 /* New types are not allowed in an explicit-specifier. */
14411 const char *saved_message
14412 = parser->type_definition_forbidden_message;
14413 parser->type_definition_forbidden_message
14414 = G_("types may not be defined in explicit-specifier");
14415
14416 if (cxx_dialect < cxx2a)
14417 pedwarn (token->location, 0,
14418 "%<explicit(bool)%> only available with %<-std=c++2a%> "
14419 "or %<-std=gnu++2a%>");
14420
14421 /* Parse the constant-expression. */
14422 expr = cp_parser_constant_expression (parser);
14423
14424 /* Restore the saved message. */
14425 parser->type_definition_forbidden_message = saved_message;
14426 parens.require_close (parser);
14427 }
14428 else
14429 /* The explicit-specifier explicit without a constant-expression is
14430 equivalent to the explicit-specifier explicit(true). */
14431 expr = boolean_true_node;
14432
14433 /* [dcl.fct.spec]
14434 "the constant-expression, if supplied, shall be a contextually
14435 converted constant expression of type bool." */
14436 expr = build_explicit_specifier (expr, tf_warning_or_error);
14437 /* We could evaluate it -- mark the decl as appropriate. */
14438 if (expr == boolean_true_node)
14439 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14440 else if (expr == boolean_false_node)
14441 /* Don't mark the decl as explicit. */;
14442 else if (decl_specs)
14443 /* The expression was value-dependent. Remember it so that we can
14444 substitute it later. */
14445 decl_specs->explicit_specifier = expr;
14446 return id;
14447 }
14448
14449 default:
14450 return NULL_TREE;
14451 }
14452
14453 /* Consume the token. */
14454 return cp_lexer_consume_token (parser->lexer)->u.value;
14455 }
14456
14457 /* Parse a linkage-specification.
14458
14459 linkage-specification:
14460 extern string-literal { declaration-seq [opt] }
14461 extern string-literal declaration */
14462
14463 static void
14464 cp_parser_linkage_specification (cp_parser* parser)
14465 {
14466 tree linkage;
14467
14468 /* Look for the `extern' keyword. */
14469 cp_token *extern_token
14470 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14471
14472 /* Look for the string-literal. */
14473 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14474 linkage = cp_parser_string_literal (parser, false, false);
14475
14476 /* Transform the literal into an identifier. If the literal is a
14477 wide-character string, or contains embedded NULs, then we can't
14478 handle it as the user wants. */
14479 if (strlen (TREE_STRING_POINTER (linkage))
14480 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14481 {
14482 cp_parser_error (parser, "invalid linkage-specification");
14483 /* Assume C++ linkage. */
14484 linkage = lang_name_cplusplus;
14485 }
14486 else
14487 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14488
14489 /* We're now using the new linkage. */
14490 push_lang_context (linkage);
14491
14492 /* Preserve the location of the the innermost linkage specification,
14493 tracking the locations of nested specifications via a local. */
14494 location_t saved_location
14495 = parser->innermost_linkage_specification_location;
14496 /* Construct a location ranging from the start of the "extern" to
14497 the end of the string-literal, with the caret at the start, e.g.:
14498 extern "C" {
14499 ^~~~~~~~~~
14500 */
14501 parser->innermost_linkage_specification_location
14502 = make_location (extern_token->location,
14503 extern_token->location,
14504 get_finish (string_token->location));
14505
14506 /* If the next token is a `{', then we're using the first
14507 production. */
14508 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14509 {
14510 cp_ensure_no_omp_declare_simd (parser);
14511 cp_ensure_no_oacc_routine (parser);
14512
14513 /* Consume the `{' token. */
14514 matching_braces braces;
14515 braces.consume_open (parser);
14516 /* Parse the declarations. */
14517 cp_parser_declaration_seq_opt (parser);
14518 /* Look for the closing `}'. */
14519 braces.require_close (parser);
14520 }
14521 /* Otherwise, there's just one declaration. */
14522 else
14523 {
14524 bool saved_in_unbraced_linkage_specification_p;
14525
14526 saved_in_unbraced_linkage_specification_p
14527 = parser->in_unbraced_linkage_specification_p;
14528 parser->in_unbraced_linkage_specification_p = true;
14529 cp_parser_declaration (parser);
14530 parser->in_unbraced_linkage_specification_p
14531 = saved_in_unbraced_linkage_specification_p;
14532 }
14533
14534 /* We're done with the linkage-specification. */
14535 pop_lang_context ();
14536
14537 /* Restore location of parent linkage specification, if any. */
14538 parser->innermost_linkage_specification_location = saved_location;
14539 }
14540
14541 /* Parse a static_assert-declaration.
14542
14543 static_assert-declaration:
14544 static_assert ( constant-expression , string-literal ) ;
14545 static_assert ( constant-expression ) ; (C++17)
14546
14547 If MEMBER_P, this static_assert is a class member. */
14548
14549 static void
14550 cp_parser_static_assert(cp_parser *parser, bool member_p)
14551 {
14552 cp_expr condition;
14553 location_t token_loc;
14554 tree message;
14555 bool dummy;
14556
14557 /* Peek at the `static_assert' token so we can keep track of exactly
14558 where the static assertion started. */
14559 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14560
14561 /* Look for the `static_assert' keyword. */
14562 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14563 RT_STATIC_ASSERT))
14564 return;
14565
14566 /* We know we are in a static assertion; commit to any tentative
14567 parse. */
14568 if (cp_parser_parsing_tentatively (parser))
14569 cp_parser_commit_to_tentative_parse (parser);
14570
14571 /* Parse the `(' starting the static assertion condition. */
14572 matching_parens parens;
14573 parens.require_open (parser);
14574
14575 /* Parse the constant-expression. Allow a non-constant expression
14576 here in order to give better diagnostics in finish_static_assert. */
14577 condition =
14578 cp_parser_constant_expression (parser,
14579 /*allow_non_constant_p=*/true,
14580 /*non_constant_p=*/&dummy);
14581
14582 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14583 {
14584 if (cxx_dialect < cxx17)
14585 pedwarn (input_location, OPT_Wpedantic,
14586 "%<static_assert%> without a message "
14587 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14588 /* Eat the ')' */
14589 cp_lexer_consume_token (parser->lexer);
14590 message = build_string (1, "");
14591 TREE_TYPE (message) = char_array_type_node;
14592 fix_string_type (message);
14593 }
14594 else
14595 {
14596 /* Parse the separating `,'. */
14597 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14598
14599 /* Parse the string-literal message. */
14600 message = cp_parser_string_literal (parser,
14601 /*translate=*/false,
14602 /*wide_ok=*/true);
14603
14604 /* A `)' completes the static assertion. */
14605 if (!parens.require_close (parser))
14606 cp_parser_skip_to_closing_parenthesis (parser,
14607 /*recovering=*/true,
14608 /*or_comma=*/false,
14609 /*consume_paren=*/true);
14610 }
14611
14612 /* A semicolon terminates the declaration. */
14613 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14614
14615 /* Get the location for the static assertion. Use that of the
14616 condition if available, otherwise, use that of the "static_assert"
14617 token. */
14618 location_t assert_loc = condition.get_location ();
14619 if (assert_loc == UNKNOWN_LOCATION)
14620 assert_loc = token_loc;
14621
14622 /* Complete the static assertion, which may mean either processing
14623 the static assert now or saving it for template instantiation. */
14624 finish_static_assert (condition, message, assert_loc, member_p);
14625 }
14626
14627 /* Parse the expression in decltype ( expression ). */
14628
14629 static tree
14630 cp_parser_decltype_expr (cp_parser *parser,
14631 bool &id_expression_or_member_access_p)
14632 {
14633 cp_token *id_expr_start_token;
14634 tree expr;
14635
14636 /* Since we're going to preserve any side-effects from this parse, set up a
14637 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14638 in the expression. */
14639 tentative_firewall firewall (parser);
14640
14641 /* First, try parsing an id-expression. */
14642 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14643 cp_parser_parse_tentatively (parser);
14644 expr = cp_parser_id_expression (parser,
14645 /*template_keyword_p=*/false,
14646 /*check_dependency_p=*/true,
14647 /*template_p=*/NULL,
14648 /*declarator_p=*/false,
14649 /*optional_p=*/false);
14650
14651 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14652 {
14653 bool non_integral_constant_expression_p = false;
14654 tree id_expression = expr;
14655 cp_id_kind idk;
14656 const char *error_msg;
14657
14658 if (identifier_p (expr))
14659 /* Lookup the name we got back from the id-expression. */
14660 expr = cp_parser_lookup_name_simple (parser, expr,
14661 id_expr_start_token->location);
14662
14663 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14664 /* A template without args is not a complete id-expression. */
14665 expr = error_mark_node;
14666
14667 if (expr
14668 && expr != error_mark_node
14669 && TREE_CODE (expr) != TYPE_DECL
14670 && (TREE_CODE (expr) != BIT_NOT_EXPR
14671 || !TYPE_P (TREE_OPERAND (expr, 0)))
14672 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14673 {
14674 /* Complete lookup of the id-expression. */
14675 expr = (finish_id_expression
14676 (id_expression, expr, parser->scope, &idk,
14677 /*integral_constant_expression_p=*/false,
14678 /*allow_non_integral_constant_expression_p=*/true,
14679 &non_integral_constant_expression_p,
14680 /*template_p=*/false,
14681 /*done=*/true,
14682 /*address_p=*/false,
14683 /*template_arg_p=*/false,
14684 &error_msg,
14685 id_expr_start_token->location));
14686
14687 if (expr == error_mark_node)
14688 /* We found an id-expression, but it was something that we
14689 should not have found. This is an error, not something
14690 we can recover from, so note that we found an
14691 id-expression and we'll recover as gracefully as
14692 possible. */
14693 id_expression_or_member_access_p = true;
14694 }
14695
14696 if (expr
14697 && expr != error_mark_node
14698 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14699 /* We have an id-expression. */
14700 id_expression_or_member_access_p = true;
14701 }
14702
14703 if (!id_expression_or_member_access_p)
14704 {
14705 /* Abort the id-expression parse. */
14706 cp_parser_abort_tentative_parse (parser);
14707
14708 /* Parsing tentatively, again. */
14709 cp_parser_parse_tentatively (parser);
14710
14711 /* Parse a class member access. */
14712 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14713 /*cast_p=*/false, /*decltype*/true,
14714 /*member_access_only_p=*/true, NULL);
14715
14716 if (expr
14717 && expr != error_mark_node
14718 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14719 /* We have an id-expression. */
14720 id_expression_or_member_access_p = true;
14721 }
14722
14723 if (id_expression_or_member_access_p)
14724 /* We have parsed the complete id-expression or member access. */
14725 cp_parser_parse_definitely (parser);
14726 else
14727 {
14728 /* Abort our attempt to parse an id-expression or member access
14729 expression. */
14730 cp_parser_abort_tentative_parse (parser);
14731
14732 /* Commit to the tentative_firewall so we get syntax errors. */
14733 cp_parser_commit_to_tentative_parse (parser);
14734
14735 /* Parse a full expression. */
14736 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14737 /*decltype_p=*/true);
14738 }
14739
14740 return expr;
14741 }
14742
14743 /* Parse a `decltype' type. Returns the type.
14744
14745 simple-type-specifier:
14746 decltype ( expression )
14747 C++14 proposal:
14748 decltype ( auto ) */
14749
14750 static tree
14751 cp_parser_decltype (cp_parser *parser)
14752 {
14753 bool id_expression_or_member_access_p = false;
14754 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14755
14756 if (start_token->type == CPP_DECLTYPE)
14757 {
14758 /* Already parsed. */
14759 cp_lexer_consume_token (parser->lexer);
14760 return saved_checks_value (start_token->u.tree_check_value);
14761 }
14762
14763 /* Look for the `decltype' token. */
14764 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14765 return error_mark_node;
14766
14767 /* Parse the opening `('. */
14768 matching_parens parens;
14769 if (!parens.require_open (parser))
14770 return error_mark_node;
14771
14772 push_deferring_access_checks (dk_deferred);
14773
14774 tree expr = NULL_TREE;
14775
14776 if (cxx_dialect >= cxx14
14777 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14778 /* decltype (auto) */
14779 cp_lexer_consume_token (parser->lexer);
14780 else
14781 {
14782 /* decltype (expression) */
14783
14784 /* Types cannot be defined in a `decltype' expression. Save away the
14785 old message and set the new one. */
14786 const char *saved_message = parser->type_definition_forbidden_message;
14787 parser->type_definition_forbidden_message
14788 = G_("types may not be defined in %<decltype%> expressions");
14789
14790 /* The restrictions on constant-expressions do not apply inside
14791 decltype expressions. */
14792 bool saved_integral_constant_expression_p
14793 = parser->integral_constant_expression_p;
14794 bool saved_non_integral_constant_expression_p
14795 = parser->non_integral_constant_expression_p;
14796 parser->integral_constant_expression_p = false;
14797
14798 /* Within a parenthesized expression, a `>' token is always
14799 the greater-than operator. */
14800 bool saved_greater_than_is_operator_p
14801 = parser->greater_than_is_operator_p;
14802 parser->greater_than_is_operator_p = true;
14803
14804 /* Do not actually evaluate the expression. */
14805 ++cp_unevaluated_operand;
14806
14807 /* Do not warn about problems with the expression. */
14808 ++c_inhibit_evaluation_warnings;
14809
14810 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14811 STRIP_ANY_LOCATION_WRAPPER (expr);
14812
14813 /* Go back to evaluating expressions. */
14814 --cp_unevaluated_operand;
14815 --c_inhibit_evaluation_warnings;
14816
14817 /* The `>' token might be the end of a template-id or
14818 template-parameter-list now. */
14819 parser->greater_than_is_operator_p
14820 = saved_greater_than_is_operator_p;
14821
14822 /* Restore the old message and the integral constant expression
14823 flags. */
14824 parser->type_definition_forbidden_message = saved_message;
14825 parser->integral_constant_expression_p
14826 = saved_integral_constant_expression_p;
14827 parser->non_integral_constant_expression_p
14828 = saved_non_integral_constant_expression_p;
14829 }
14830
14831 /* Parse to the closing `)'. */
14832 if (!parens.require_close (parser))
14833 {
14834 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14835 /*consume_paren=*/true);
14836 pop_deferring_access_checks ();
14837 return error_mark_node;
14838 }
14839
14840 if (!expr)
14841 {
14842 /* Build auto. */
14843 expr = make_decltype_auto ();
14844 AUTO_IS_DECLTYPE (expr) = true;
14845 }
14846 else
14847 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14848 tf_warning_or_error);
14849
14850 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14851 it again. */
14852 start_token->type = CPP_DECLTYPE;
14853 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14854 start_token->tree_check_p = true;
14855 start_token->u.tree_check_value->value = expr;
14856 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14857 start_token->keyword = RID_MAX;
14858 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14859
14860 pop_to_parent_deferring_access_checks ();
14861
14862 return expr;
14863 }
14864
14865 /* Special member functions [gram.special] */
14866
14867 /* Parse a conversion-function-id.
14868
14869 conversion-function-id:
14870 operator conversion-type-id
14871
14872 Returns an IDENTIFIER_NODE representing the operator. */
14873
14874 static tree
14875 cp_parser_conversion_function_id (cp_parser* parser)
14876 {
14877 tree type;
14878 tree saved_scope;
14879 tree saved_qualifying_scope;
14880 tree saved_object_scope;
14881 tree pushed_scope = NULL_TREE;
14882
14883 /* Look for the `operator' token. */
14884 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14885 return error_mark_node;
14886 /* When we parse the conversion-type-id, the current scope will be
14887 reset. However, we need that information in able to look up the
14888 conversion function later, so we save it here. */
14889 saved_scope = parser->scope;
14890 saved_qualifying_scope = parser->qualifying_scope;
14891 saved_object_scope = parser->object_scope;
14892 /* We must enter the scope of the class so that the names of
14893 entities declared within the class are available in the
14894 conversion-type-id. For example, consider:
14895
14896 struct S {
14897 typedef int I;
14898 operator I();
14899 };
14900
14901 S::operator I() { ... }
14902
14903 In order to see that `I' is a type-name in the definition, we
14904 must be in the scope of `S'. */
14905 if (saved_scope)
14906 pushed_scope = push_scope (saved_scope);
14907 /* Parse the conversion-type-id. */
14908 type = cp_parser_conversion_type_id (parser);
14909 /* Leave the scope of the class, if any. */
14910 if (pushed_scope)
14911 pop_scope (pushed_scope);
14912 /* Restore the saved scope. */
14913 parser->scope = saved_scope;
14914 parser->qualifying_scope = saved_qualifying_scope;
14915 parser->object_scope = saved_object_scope;
14916 /* If the TYPE is invalid, indicate failure. */
14917 if (type == error_mark_node)
14918 return error_mark_node;
14919 return make_conv_op_name (type);
14920 }
14921
14922 /* Parse a conversion-type-id:
14923
14924 conversion-type-id:
14925 type-specifier-seq conversion-declarator [opt]
14926
14927 Returns the TYPE specified. */
14928
14929 static tree
14930 cp_parser_conversion_type_id (cp_parser* parser)
14931 {
14932 tree attributes;
14933 cp_decl_specifier_seq type_specifiers;
14934 cp_declarator *declarator;
14935 tree type_specified;
14936 const char *saved_message;
14937
14938 /* Parse the attributes. */
14939 attributes = cp_parser_attributes_opt (parser);
14940
14941 saved_message = parser->type_definition_forbidden_message;
14942 parser->type_definition_forbidden_message
14943 = G_("types may not be defined in a conversion-type-id");
14944
14945 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
14946 optional in conversion-type-id. */
14947 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
14948 /*is_declaration=*/false,
14949 /*is_trailing_return=*/false,
14950 &type_specifiers);
14951
14952 parser->type_definition_forbidden_message = saved_message;
14953
14954 /* If that didn't work, stop. */
14955 if (type_specifiers.type == error_mark_node)
14956 return error_mark_node;
14957 /* Parse the conversion-declarator. */
14958 declarator = cp_parser_conversion_declarator_opt (parser);
14959
14960 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14961 /*initialized=*/0, &attributes);
14962 if (attributes)
14963 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14964
14965 /* Don't give this error when parsing tentatively. This happens to
14966 work because we always parse this definitively once. */
14967 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14968 && type_uses_auto (type_specified))
14969 {
14970 if (cxx_dialect < cxx14)
14971 {
14972 error ("invalid use of %<auto%> in conversion operator");
14973 return error_mark_node;
14974 }
14975 else if (template_parm_scope_p ())
14976 warning (0, "use of %<auto%> in member template "
14977 "conversion operator can never be deduced");
14978 }
14979
14980 return type_specified;
14981 }
14982
14983 /* Parse an (optional) conversion-declarator.
14984
14985 conversion-declarator:
14986 ptr-operator conversion-declarator [opt]
14987
14988 */
14989
14990 static cp_declarator *
14991 cp_parser_conversion_declarator_opt (cp_parser* parser)
14992 {
14993 enum tree_code code;
14994 tree class_type, std_attributes = NULL_TREE;
14995 cp_cv_quals cv_quals;
14996
14997 /* We don't know if there's a ptr-operator next, or not. */
14998 cp_parser_parse_tentatively (parser);
14999 /* Try the ptr-operator. */
15000 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
15001 &std_attributes);
15002 /* If it worked, look for more conversion-declarators. */
15003 if (cp_parser_parse_definitely (parser))
15004 {
15005 cp_declarator *declarator;
15006
15007 /* Parse another optional declarator. */
15008 declarator = cp_parser_conversion_declarator_opt (parser);
15009
15010 declarator = cp_parser_make_indirect_declarator
15011 (code, class_type, cv_quals, declarator, std_attributes);
15012
15013 return declarator;
15014 }
15015
15016 return NULL;
15017 }
15018
15019 /* Parse an (optional) ctor-initializer.
15020
15021 ctor-initializer:
15022 : mem-initializer-list */
15023
15024 static void
15025 cp_parser_ctor_initializer_opt (cp_parser* parser)
15026 {
15027 /* If the next token is not a `:', then there is no
15028 ctor-initializer. */
15029 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
15030 {
15031 /* Do default initialization of any bases and members. */
15032 if (DECL_CONSTRUCTOR_P (current_function_decl))
15033 finish_mem_initializers (NULL_TREE);
15034 return;
15035 }
15036
15037 /* Consume the `:' token. */
15038 cp_lexer_consume_token (parser->lexer);
15039 /* And the mem-initializer-list. */
15040 cp_parser_mem_initializer_list (parser);
15041 }
15042
15043 /* Parse a mem-initializer-list.
15044
15045 mem-initializer-list:
15046 mem-initializer ... [opt]
15047 mem-initializer ... [opt] , mem-initializer-list */
15048
15049 static void
15050 cp_parser_mem_initializer_list (cp_parser* parser)
15051 {
15052 tree mem_initializer_list = NULL_TREE;
15053 tree target_ctor = error_mark_node;
15054 cp_token *token = cp_lexer_peek_token (parser->lexer);
15055
15056 /* Let the semantic analysis code know that we are starting the
15057 mem-initializer-list. */
15058 if (!DECL_CONSTRUCTOR_P (current_function_decl))
15059 error_at (token->location,
15060 "only constructors take member initializers");
15061
15062 /* Loop through the list. */
15063 while (true)
15064 {
15065 tree mem_initializer;
15066
15067 token = cp_lexer_peek_token (parser->lexer);
15068 /* Parse the mem-initializer. */
15069 mem_initializer = cp_parser_mem_initializer (parser);
15070 /* If the next token is a `...', we're expanding member initializers. */
15071 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15072 if (ellipsis
15073 || (mem_initializer != error_mark_node
15074 && check_for_bare_parameter_packs (TREE_PURPOSE
15075 (mem_initializer))))
15076 {
15077 /* Consume the `...'. */
15078 if (ellipsis)
15079 cp_lexer_consume_token (parser->lexer);
15080
15081 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
15082 can be expanded but members cannot. */
15083 if (mem_initializer != error_mark_node
15084 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
15085 {
15086 error_at (token->location,
15087 "cannot expand initializer for member %qD",
15088 TREE_PURPOSE (mem_initializer));
15089 mem_initializer = error_mark_node;
15090 }
15091
15092 /* Construct the pack expansion type. */
15093 if (mem_initializer != error_mark_node)
15094 mem_initializer = make_pack_expansion (mem_initializer);
15095 }
15096 if (target_ctor != error_mark_node
15097 && mem_initializer != error_mark_node)
15098 {
15099 error ("mem-initializer for %qD follows constructor delegation",
15100 TREE_PURPOSE (mem_initializer));
15101 mem_initializer = error_mark_node;
15102 }
15103 /* Look for a target constructor. */
15104 if (mem_initializer != error_mark_node
15105 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
15106 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
15107 {
15108 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
15109 if (mem_initializer_list)
15110 {
15111 error ("constructor delegation follows mem-initializer for %qD",
15112 TREE_PURPOSE (mem_initializer_list));
15113 mem_initializer = error_mark_node;
15114 }
15115 target_ctor = mem_initializer;
15116 }
15117 /* Add it to the list, unless it was erroneous. */
15118 if (mem_initializer != error_mark_node)
15119 {
15120 TREE_CHAIN (mem_initializer) = mem_initializer_list;
15121 mem_initializer_list = mem_initializer;
15122 }
15123 /* If the next token is not a `,', we're done. */
15124 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15125 break;
15126 /* Consume the `,' token. */
15127 cp_lexer_consume_token (parser->lexer);
15128 }
15129
15130 /* Perform semantic analysis. */
15131 if (DECL_CONSTRUCTOR_P (current_function_decl))
15132 finish_mem_initializers (mem_initializer_list);
15133 }
15134
15135 /* Parse a mem-initializer.
15136
15137 mem-initializer:
15138 mem-initializer-id ( expression-list [opt] )
15139 mem-initializer-id braced-init-list
15140
15141 GNU extension:
15142
15143 mem-initializer:
15144 ( expression-list [opt] )
15145
15146 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15147 class) or FIELD_DECL (for a non-static data member) to initialize;
15148 the TREE_VALUE is the expression-list. An empty initialization
15149 list is represented by void_list_node. */
15150
15151 static tree
15152 cp_parser_mem_initializer (cp_parser* parser)
15153 {
15154 tree mem_initializer_id;
15155 tree expression_list;
15156 tree member;
15157 cp_token *token = cp_lexer_peek_token (parser->lexer);
15158
15159 /* Find out what is being initialized. */
15160 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15161 {
15162 permerror (token->location,
15163 "anachronistic old-style base class initializer");
15164 mem_initializer_id = NULL_TREE;
15165 }
15166 else
15167 {
15168 mem_initializer_id = cp_parser_mem_initializer_id (parser);
15169 if (mem_initializer_id == error_mark_node)
15170 return mem_initializer_id;
15171 }
15172 member = expand_member_init (mem_initializer_id);
15173 if (member && !DECL_P (member))
15174 in_base_initializer = 1;
15175
15176 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15177 {
15178 bool expr_non_constant_p;
15179 cp_lexer_set_source_position (parser->lexer);
15180 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15181 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
15182 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
15183 expression_list = build_tree_list (NULL_TREE, expression_list);
15184 }
15185 else
15186 {
15187 vec<tree, va_gc> *vec;
15188 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15189 /*cast_p=*/false,
15190 /*allow_expansion_p=*/true,
15191 /*non_constant_p=*/NULL,
15192 /*close_paren_loc=*/NULL,
15193 /*wrap_locations_p=*/true);
15194 if (vec == NULL)
15195 return error_mark_node;
15196 expression_list = build_tree_list_vec (vec);
15197 release_tree_vector (vec);
15198 }
15199
15200 if (expression_list == error_mark_node)
15201 return error_mark_node;
15202 if (!expression_list)
15203 expression_list = void_type_node;
15204
15205 in_base_initializer = 0;
15206
15207 return member ? build_tree_list (member, expression_list) : error_mark_node;
15208 }
15209
15210 /* Parse a mem-initializer-id.
15211
15212 mem-initializer-id:
15213 :: [opt] nested-name-specifier [opt] class-name
15214 decltype-specifier (C++11)
15215 identifier
15216
15217 Returns a TYPE indicating the class to be initialized for the first
15218 production (and the second in C++11). Returns an IDENTIFIER_NODE
15219 indicating the data member to be initialized for the last production. */
15220
15221 static tree
15222 cp_parser_mem_initializer_id (cp_parser* parser)
15223 {
15224 bool global_scope_p;
15225 bool nested_name_specifier_p;
15226 bool template_p = false;
15227 tree id;
15228
15229 cp_token *token = cp_lexer_peek_token (parser->lexer);
15230
15231 /* `typename' is not allowed in this context ([temp.res]). */
15232 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15233 {
15234 error_at (token->location,
15235 "keyword %<typename%> not allowed in this context (a qualified "
15236 "member initializer is implicitly a type)");
15237 cp_lexer_consume_token (parser->lexer);
15238 }
15239 /* Look for the optional `::' operator. */
15240 global_scope_p
15241 = (cp_parser_global_scope_opt (parser,
15242 /*current_scope_valid_p=*/false)
15243 != NULL_TREE);
15244 /* Look for the optional nested-name-specifier. The simplest way to
15245 implement:
15246
15247 [temp.res]
15248
15249 The keyword `typename' is not permitted in a base-specifier or
15250 mem-initializer; in these contexts a qualified name that
15251 depends on a template-parameter is implicitly assumed to be a
15252 type name.
15253
15254 is to assume that we have seen the `typename' keyword at this
15255 point. */
15256 nested_name_specifier_p
15257 = (cp_parser_nested_name_specifier_opt (parser,
15258 /*typename_keyword_p=*/true,
15259 /*check_dependency_p=*/true,
15260 /*type_p=*/true,
15261 /*is_declaration=*/true)
15262 != NULL_TREE);
15263 if (nested_name_specifier_p)
15264 template_p = cp_parser_optional_template_keyword (parser);
15265 /* If there is a `::' operator or a nested-name-specifier, then we
15266 are definitely looking for a class-name. */
15267 if (global_scope_p || nested_name_specifier_p)
15268 return cp_parser_class_name (parser,
15269 /*typename_keyword_p=*/true,
15270 /*template_keyword_p=*/template_p,
15271 typename_type,
15272 /*check_dependency_p=*/true,
15273 /*class_head_p=*/false,
15274 /*is_declaration=*/true);
15275 /* Otherwise, we could also be looking for an ordinary identifier. */
15276 cp_parser_parse_tentatively (parser);
15277 if (cp_lexer_next_token_is_decltype (parser->lexer))
15278 /* Try a decltype-specifier. */
15279 id = cp_parser_decltype (parser);
15280 else
15281 /* Otherwise, try a class-name. */
15282 id = cp_parser_class_name (parser,
15283 /*typename_keyword_p=*/true,
15284 /*template_keyword_p=*/false,
15285 none_type,
15286 /*check_dependency_p=*/true,
15287 /*class_head_p=*/false,
15288 /*is_declaration=*/true);
15289 /* If we found one, we're done. */
15290 if (cp_parser_parse_definitely (parser))
15291 return id;
15292 /* Otherwise, look for an ordinary identifier. */
15293 return cp_parser_identifier (parser);
15294 }
15295
15296 /* Overloading [gram.over] */
15297
15298 /* Parse an operator-function-id.
15299
15300 operator-function-id:
15301 operator operator
15302
15303 Returns an IDENTIFIER_NODE for the operator which is a
15304 human-readable spelling of the identifier, e.g., `operator +'. */
15305
15306 static cp_expr
15307 cp_parser_operator_function_id (cp_parser* parser)
15308 {
15309 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15310 /* Look for the `operator' keyword. */
15311 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15312 return error_mark_node;
15313 /* And then the name of the operator itself. */
15314 return cp_parser_operator (parser, start_loc);
15315 }
15316
15317 /* Return an identifier node for a user-defined literal operator.
15318 The suffix identifier is chained to the operator name identifier. */
15319
15320 tree
15321 cp_literal_operator_id (const char* name)
15322 {
15323 tree identifier;
15324 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15325 + strlen (name) + 10);
15326 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15327 identifier = get_identifier (buffer);
15328
15329 return identifier;
15330 }
15331
15332 /* Parse an operator.
15333
15334 operator:
15335 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15336 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15337 || ++ -- , ->* -> () []
15338
15339 GNU Extensions:
15340
15341 operator:
15342 <? >? <?= >?=
15343
15344 Returns an IDENTIFIER_NODE for the operator which is a
15345 human-readable spelling of the identifier, e.g., `operator +'. */
15346
15347 static cp_expr
15348 cp_parser_operator (cp_parser* parser, location_t start_loc)
15349 {
15350 tree id = NULL_TREE;
15351 cp_token *token;
15352 bool utf8 = false;
15353
15354 /* Peek at the next token. */
15355 token = cp_lexer_peek_token (parser->lexer);
15356
15357 location_t end_loc = token->location;
15358
15359 /* Figure out which operator we have. */
15360 enum tree_code op = ERROR_MARK;
15361 bool assop = false;
15362 bool consumed = false;
15363 switch (token->type)
15364 {
15365 case CPP_KEYWORD:
15366 {
15367 /* The keyword should be either `new' or `delete'. */
15368 if (token->keyword == RID_NEW)
15369 op = NEW_EXPR;
15370 else if (token->keyword == RID_DELETE)
15371 op = DELETE_EXPR;
15372 else
15373 break;
15374
15375 /* Consume the `new' or `delete' token. */
15376 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15377
15378 /* Peek at the next token. */
15379 token = cp_lexer_peek_token (parser->lexer);
15380 /* If it's a `[' token then this is the array variant of the
15381 operator. */
15382 if (token->type == CPP_OPEN_SQUARE)
15383 {
15384 /* Consume the `[' token. */
15385 cp_lexer_consume_token (parser->lexer);
15386 /* Look for the `]' token. */
15387 if (cp_token *close_token
15388 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15389 end_loc = close_token->location;
15390 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15391 }
15392 consumed = true;
15393 break;
15394 }
15395
15396 case CPP_PLUS:
15397 op = PLUS_EXPR;
15398 break;
15399
15400 case CPP_MINUS:
15401 op = MINUS_EXPR;
15402 break;
15403
15404 case CPP_MULT:
15405 op = MULT_EXPR;
15406 break;
15407
15408 case CPP_DIV:
15409 op = TRUNC_DIV_EXPR;
15410 break;
15411
15412 case CPP_MOD:
15413 op = TRUNC_MOD_EXPR;
15414 break;
15415
15416 case CPP_XOR:
15417 op = BIT_XOR_EXPR;
15418 break;
15419
15420 case CPP_AND:
15421 op = BIT_AND_EXPR;
15422 break;
15423
15424 case CPP_OR:
15425 op = BIT_IOR_EXPR;
15426 break;
15427
15428 case CPP_COMPL:
15429 op = BIT_NOT_EXPR;
15430 break;
15431
15432 case CPP_NOT:
15433 op = TRUTH_NOT_EXPR;
15434 break;
15435
15436 case CPP_EQ:
15437 assop = true;
15438 op = NOP_EXPR;
15439 break;
15440
15441 case CPP_LESS:
15442 op = LT_EXPR;
15443 break;
15444
15445 case CPP_GREATER:
15446 op = GT_EXPR;
15447 break;
15448
15449 case CPP_PLUS_EQ:
15450 assop = true;
15451 op = PLUS_EXPR;
15452 break;
15453
15454 case CPP_MINUS_EQ:
15455 assop = true;
15456 op = MINUS_EXPR;
15457 break;
15458
15459 case CPP_MULT_EQ:
15460 assop = true;
15461 op = MULT_EXPR;
15462 break;
15463
15464 case CPP_DIV_EQ:
15465 assop = true;
15466 op = TRUNC_DIV_EXPR;
15467 break;
15468
15469 case CPP_MOD_EQ:
15470 assop = true;
15471 op = TRUNC_MOD_EXPR;
15472 break;
15473
15474 case CPP_XOR_EQ:
15475 assop = true;
15476 op = BIT_XOR_EXPR;
15477 break;
15478
15479 case CPP_AND_EQ:
15480 assop = true;
15481 op = BIT_AND_EXPR;
15482 break;
15483
15484 case CPP_OR_EQ:
15485 assop = true;
15486 op = BIT_IOR_EXPR;
15487 break;
15488
15489 case CPP_LSHIFT:
15490 op = LSHIFT_EXPR;
15491 break;
15492
15493 case CPP_RSHIFT:
15494 op = RSHIFT_EXPR;
15495 break;
15496
15497 case CPP_LSHIFT_EQ:
15498 assop = true;
15499 op = LSHIFT_EXPR;
15500 break;
15501
15502 case CPP_RSHIFT_EQ:
15503 assop = true;
15504 op = RSHIFT_EXPR;
15505 break;
15506
15507 case CPP_EQ_EQ:
15508 op = EQ_EXPR;
15509 break;
15510
15511 case CPP_NOT_EQ:
15512 op = NE_EXPR;
15513 break;
15514
15515 case CPP_LESS_EQ:
15516 op = LE_EXPR;
15517 break;
15518
15519 case CPP_GREATER_EQ:
15520 op = GE_EXPR;
15521 break;
15522
15523 case CPP_SPACESHIP:
15524 op = SPACESHIP_EXPR;
15525 break;
15526
15527 case CPP_AND_AND:
15528 op = TRUTH_ANDIF_EXPR;
15529 break;
15530
15531 case CPP_OR_OR:
15532 op = TRUTH_ORIF_EXPR;
15533 break;
15534
15535 case CPP_PLUS_PLUS:
15536 op = POSTINCREMENT_EXPR;
15537 break;
15538
15539 case CPP_MINUS_MINUS:
15540 op = PREDECREMENT_EXPR;
15541 break;
15542
15543 case CPP_COMMA:
15544 op = COMPOUND_EXPR;
15545 break;
15546
15547 case CPP_DEREF_STAR:
15548 op = MEMBER_REF;
15549 break;
15550
15551 case CPP_DEREF:
15552 op = COMPONENT_REF;
15553 break;
15554
15555 case CPP_QUERY:
15556 op = COND_EXPR;
15557 /* Consume the `?'. */
15558 cp_lexer_consume_token (parser->lexer);
15559 /* Look for the matching `:'. */
15560 cp_parser_require (parser, CPP_COLON, RT_COLON);
15561 consumed = true;
15562 break;
15563
15564 case CPP_OPEN_PAREN:
15565 {
15566 /* Consume the `('. */
15567 matching_parens parens;
15568 parens.consume_open (parser);
15569 /* Look for the matching `)'. */
15570 token = parens.require_close (parser);
15571 if (token)
15572 end_loc = token->location;
15573 op = CALL_EXPR;
15574 consumed = true;
15575 break;
15576 }
15577
15578 case CPP_OPEN_SQUARE:
15579 /* Consume the `['. */
15580 cp_lexer_consume_token (parser->lexer);
15581 /* Look for the matching `]'. */
15582 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15583 if (token)
15584 end_loc = token->location;
15585 op = ARRAY_REF;
15586 consumed = true;
15587 break;
15588
15589 case CPP_UTF8STRING:
15590 case CPP_UTF8STRING_USERDEF:
15591 utf8 = true;
15592 /* FALLTHRU */
15593 case CPP_STRING:
15594 case CPP_WSTRING:
15595 case CPP_STRING16:
15596 case CPP_STRING32:
15597 case CPP_STRING_USERDEF:
15598 case CPP_WSTRING_USERDEF:
15599 case CPP_STRING16_USERDEF:
15600 case CPP_STRING32_USERDEF:
15601 {
15602 cp_expr str;
15603 tree string_tree;
15604 int sz, len;
15605
15606 if (cxx_dialect == cxx98)
15607 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15608
15609 /* Consume the string. */
15610 str = cp_parser_string_literal (parser, /*translate=*/true,
15611 /*wide_ok=*/true, /*lookup_udlit=*/false);
15612 if (str == error_mark_node)
15613 return error_mark_node;
15614 else if (TREE_CODE (str) == USERDEF_LITERAL)
15615 {
15616 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15617 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15618 end_loc = str.get_location ();
15619 }
15620 else
15621 {
15622 string_tree = str;
15623 /* Look for the suffix identifier. */
15624 token = cp_lexer_peek_token (parser->lexer);
15625 if (token->type == CPP_NAME)
15626 {
15627 id = cp_parser_identifier (parser);
15628 end_loc = token->location;
15629 }
15630 else if (token->type == CPP_KEYWORD)
15631 {
15632 error ("unexpected keyword;"
15633 " remove space between quotes and suffix identifier");
15634 return error_mark_node;
15635 }
15636 else
15637 {
15638 error ("expected suffix identifier");
15639 return error_mark_node;
15640 }
15641 }
15642 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15643 (TREE_TYPE (TREE_TYPE (string_tree))));
15644 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15645 if (len != 0)
15646 {
15647 error ("expected empty string after %<operator%> keyword");
15648 return error_mark_node;
15649 }
15650 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15651 != char_type_node)
15652 {
15653 error ("invalid encoding prefix in literal operator");
15654 return error_mark_node;
15655 }
15656 if (id != error_mark_node)
15657 {
15658 const char *name = IDENTIFIER_POINTER (id);
15659 id = cp_literal_operator_id (name);
15660 }
15661 /* Generate a location of the form:
15662 "" _suffix_identifier
15663 ^~~~~~~~~~~~~~~~~~~~~
15664 with caret == start at the start token, finish at the end of the
15665 suffix identifier. */
15666 location_t combined_loc
15667 = make_location (start_loc, start_loc, parser->lexer);
15668 return cp_expr (id, combined_loc);
15669 }
15670
15671 default:
15672 /* Anything else is an error. */
15673 break;
15674 }
15675
15676 /* If we have selected an identifier, we need to consume the
15677 operator token. */
15678 if (op != ERROR_MARK)
15679 {
15680 id = ovl_op_identifier (assop, op);
15681 if (!consumed)
15682 cp_lexer_consume_token (parser->lexer);
15683 }
15684 /* Otherwise, no valid operator name was present. */
15685 else
15686 {
15687 cp_parser_error (parser, "expected operator");
15688 id = error_mark_node;
15689 }
15690
15691 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15692 return cp_expr (id, start_loc);
15693 }
15694
15695 /* Parse a template-declaration.
15696
15697 template-declaration:
15698 export [opt] template < template-parameter-list > declaration
15699
15700 If MEMBER_P is TRUE, this template-declaration occurs within a
15701 class-specifier.
15702
15703 The grammar rule given by the standard isn't correct. What
15704 is really meant is:
15705
15706 template-declaration:
15707 export [opt] template-parameter-list-seq
15708 decl-specifier-seq [opt] init-declarator [opt] ;
15709 export [opt] template-parameter-list-seq
15710 function-definition
15711
15712 template-parameter-list-seq:
15713 template-parameter-list-seq [opt]
15714 template < template-parameter-list >
15715
15716 Concept Extensions:
15717
15718 template-parameter-list-seq:
15719 template < template-parameter-list > requires-clause [opt]
15720
15721 requires-clause:
15722 requires logical-or-expression */
15723
15724 static void
15725 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15726 {
15727 /* Check for `export'. */
15728 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15729 {
15730 /* Consume the `export' token. */
15731 cp_lexer_consume_token (parser->lexer);
15732 /* Warn that we do not support `export'. */
15733 warning (0, "keyword %<export%> not implemented, and will be ignored");
15734 }
15735
15736 cp_parser_template_declaration_after_export (parser, member_p);
15737 }
15738
15739 /* Parse a template-parameter-list.
15740
15741 template-parameter-list:
15742 template-parameter
15743 template-parameter-list , template-parameter
15744
15745 Returns a TREE_LIST. Each node represents a template parameter.
15746 The nodes are connected via their TREE_CHAINs. */
15747
15748 static tree
15749 cp_parser_template_parameter_list (cp_parser* parser)
15750 {
15751 tree parameter_list = NULL_TREE;
15752
15753 /* Don't create wrapper nodes within a template-parameter-list,
15754 since we don't want to have different types based on the
15755 spelling location of constants and decls within them. */
15756 auto_suppress_location_wrappers sentinel;
15757
15758 begin_template_parm_list ();
15759
15760 /* The loop below parses the template parms. We first need to know
15761 the total number of template parms to be able to compute proper
15762 canonical types of each dependent type. So after the loop, when
15763 we know the total number of template parms,
15764 end_template_parm_list computes the proper canonical types and
15765 fixes up the dependent types accordingly. */
15766 while (true)
15767 {
15768 tree parameter;
15769 bool is_non_type;
15770 bool is_parameter_pack;
15771 location_t parm_loc;
15772
15773 /* Parse the template-parameter. */
15774 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15775 parameter = cp_parser_template_parameter (parser,
15776 &is_non_type,
15777 &is_parameter_pack);
15778 /* Add it to the list. */
15779 if (parameter != error_mark_node)
15780 parameter_list = process_template_parm (parameter_list,
15781 parm_loc,
15782 parameter,
15783 is_non_type,
15784 is_parameter_pack);
15785 else
15786 {
15787 tree err_parm = build_tree_list (parameter, parameter);
15788 parameter_list = chainon (parameter_list, err_parm);
15789 }
15790
15791 /* If the next token is not a `,', we're done. */
15792 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15793 break;
15794 /* Otherwise, consume the `,' token. */
15795 cp_lexer_consume_token (parser->lexer);
15796 }
15797
15798 return end_template_parm_list (parameter_list);
15799 }
15800
15801 /* Parse a introduction-list.
15802
15803 introduction-list:
15804 introduced-parameter
15805 introduction-list , introduced-parameter
15806
15807 introduced-parameter:
15808 ...[opt] identifier
15809
15810 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15811 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15812 WILDCARD_DECL will also have DECL_NAME set and token location in
15813 DECL_SOURCE_LOCATION. */
15814
15815 static tree
15816 cp_parser_introduction_list (cp_parser *parser)
15817 {
15818 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15819
15820 while (true)
15821 {
15822 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15823 if (is_pack)
15824 cp_lexer_consume_token (parser->lexer);
15825
15826 tree identifier = cp_parser_identifier (parser);
15827 if (identifier == error_mark_node)
15828 break;
15829
15830 /* Build placeholder. */
15831 tree parm = build_nt (WILDCARD_DECL);
15832 DECL_SOURCE_LOCATION (parm)
15833 = cp_lexer_peek_token (parser->lexer)->location;
15834 DECL_NAME (parm) = identifier;
15835 WILDCARD_PACK_P (parm) = is_pack;
15836 vec_safe_push (introduction_vec, parm);
15837
15838 /* If the next token is not a `,', we're done. */
15839 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15840 break;
15841 /* Otherwise, consume the `,' token. */
15842 cp_lexer_consume_token (parser->lexer);
15843 }
15844
15845 /* Convert the vec into a TREE_VEC. */
15846 tree introduction_list = make_tree_vec (introduction_vec->length ());
15847 unsigned int n;
15848 tree parm;
15849 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15850 TREE_VEC_ELT (introduction_list, n) = parm;
15851
15852 release_tree_vector (introduction_vec);
15853 return introduction_list;
15854 }
15855
15856 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15857 is an abstract declarator. */
15858
15859 static inline cp_declarator*
15860 get_id_declarator (cp_declarator *declarator)
15861 {
15862 cp_declarator *d = declarator;
15863 while (d && d->kind != cdk_id)
15864 d = d->declarator;
15865 return d;
15866 }
15867
15868 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15869 is an abstract declarator. */
15870
15871 static inline tree
15872 get_unqualified_id (cp_declarator *declarator)
15873 {
15874 declarator = get_id_declarator (declarator);
15875 if (declarator)
15876 return declarator->u.id.unqualified_name;
15877 else
15878 return NULL_TREE;
15879 }
15880
15881 /* Returns true if TYPE would declare a constrained constrained-parameter. */
15882
15883 static inline bool
15884 is_constrained_parameter (tree type)
15885 {
15886 return (type
15887 && TREE_CODE (type) == TYPE_DECL
15888 && CONSTRAINED_PARM_CONCEPT (type)
15889 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
15890 }
15891
15892 /* Returns true if PARM declares a constrained-parameter. */
15893
15894 static inline bool
15895 is_constrained_parameter (cp_parameter_declarator *parm)
15896 {
15897 return is_constrained_parameter (parm->decl_specifiers.type);
15898 }
15899
15900 /* Check that the type parameter is only a declarator-id, and that its
15901 type is not cv-qualified. */
15902
15903 bool
15904 cp_parser_check_constrained_type_parm (cp_parser *parser,
15905 cp_parameter_declarator *parm)
15906 {
15907 if (!parm->declarator)
15908 return true;
15909
15910 if (parm->declarator->kind != cdk_id)
15911 {
15912 cp_parser_error (parser, "invalid constrained type parameter");
15913 return false;
15914 }
15915
15916 /* Don't allow cv-qualified type parameters. */
15917 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15918 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15919 {
15920 cp_parser_error (parser, "cv-qualified type parameter");
15921 return false;
15922 }
15923
15924 return true;
15925 }
15926
15927 /* Finish parsing/processing a template type parameter and checking
15928 various restrictions. */
15929
15930 static inline tree
15931 cp_parser_constrained_type_template_parm (cp_parser *parser,
15932 tree id,
15933 cp_parameter_declarator* parmdecl)
15934 {
15935 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15936 return finish_template_type_parm (class_type_node, id);
15937 else
15938 return error_mark_node;
15939 }
15940
15941 static tree
15942 finish_constrained_template_template_parm (tree proto, tree id)
15943 {
15944 /* FIXME: This should probably be copied, and we may need to adjust
15945 the template parameter depths. */
15946 tree saved_parms = current_template_parms;
15947 begin_template_parm_list ();
15948 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15949 end_template_parm_list ();
15950
15951 tree parm = finish_template_template_parm (class_type_node, id);
15952 current_template_parms = saved_parms;
15953
15954 return parm;
15955 }
15956
15957 /* Finish parsing/processing a template template parameter by borrowing
15958 the template parameter list from the prototype parameter. */
15959
15960 static tree
15961 cp_parser_constrained_template_template_parm (cp_parser *parser,
15962 tree proto,
15963 tree id,
15964 cp_parameter_declarator *parmdecl)
15965 {
15966 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15967 return error_mark_node;
15968 return finish_constrained_template_template_parm (proto, id);
15969 }
15970
15971 /* Create a new non-type template parameter from the given PARM
15972 declarator. */
15973
15974 static tree
15975 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
15976 cp_parameter_declarator *parm)
15977 {
15978 *is_non_type = true;
15979 cp_declarator *decl = parm->declarator;
15980 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15981 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15982 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15983 }
15984
15985 /* Build a constrained template parameter based on the PARMDECL
15986 declarator. The type of PARMDECL is the constrained type, which
15987 refers to the prototype template parameter that ultimately
15988 specifies the type of the declared parameter. */
15989
15990 static tree
15991 finish_constrained_parameter (cp_parser *parser,
15992 cp_parameter_declarator *parmdecl,
15993 bool *is_non_type)
15994 {
15995 tree decl = parmdecl->decl_specifiers.type;
15996 tree id = get_unqualified_id (parmdecl->declarator);
15997 tree def = parmdecl->default_argument;
15998 tree proto = DECL_INITIAL (decl);
15999
16000 /* Build the parameter. Return an error if the declarator was invalid. */
16001 tree parm;
16002 if (TREE_CODE (proto) == TYPE_DECL)
16003 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
16004 else if (TREE_CODE (proto) == TEMPLATE_DECL)
16005 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
16006 parmdecl);
16007 else
16008 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
16009 if (parm == error_mark_node)
16010 return error_mark_node;
16011
16012 /* Finish the parameter decl and create a node attaching the
16013 default argument and constraint. */
16014 parm = build_tree_list (def, parm);
16015 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
16016
16017 return parm;
16018 }
16019
16020 /* Returns true if the parsed type actually represents the declaration
16021 of a type template-parameter. */
16022
16023 static bool
16024 declares_constrained_type_template_parameter (tree type)
16025 {
16026 return (is_constrained_parameter (type)
16027 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
16028 }
16029
16030 /* Returns true if the parsed type actually represents the declaration of
16031 a template template-parameter. */
16032
16033 static bool
16034 declares_constrained_template_template_parameter (tree type)
16035 {
16036 return (is_constrained_parameter (type)
16037 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
16038 }
16039
16040 /* Parse a default argument for a type template-parameter.
16041 Note that diagnostics are handled in cp_parser_template_parameter. */
16042
16043 static tree
16044 cp_parser_default_type_template_argument (cp_parser *parser)
16045 {
16046 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
16047
16048 /* Consume the `=' token. */
16049 cp_lexer_consume_token (parser->lexer);
16050
16051 cp_token *token = cp_lexer_peek_token (parser->lexer);
16052
16053 /* Parse the default-argument. */
16054 push_deferring_access_checks (dk_no_deferred);
16055 tree default_argument = cp_parser_type_id (parser,
16056 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16057 NULL);
16058 pop_deferring_access_checks ();
16059
16060 if (flag_concepts && type_uses_auto (default_argument))
16061 {
16062 error_at (token->location,
16063 "invalid use of %<auto%> in default template argument");
16064 return error_mark_node;
16065 }
16066
16067 return default_argument;
16068 }
16069
16070 /* Parse a default argument for a template template-parameter. */
16071
16072 static tree
16073 cp_parser_default_template_template_argument (cp_parser *parser)
16074 {
16075 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
16076
16077 bool is_template;
16078
16079 /* Consume the `='. */
16080 cp_lexer_consume_token (parser->lexer);
16081 /* Parse the id-expression. */
16082 push_deferring_access_checks (dk_no_deferred);
16083 /* save token before parsing the id-expression, for error
16084 reporting */
16085 const cp_token* token = cp_lexer_peek_token (parser->lexer);
16086 tree default_argument
16087 = cp_parser_id_expression (parser,
16088 /*template_keyword_p=*/false,
16089 /*check_dependency_p=*/true,
16090 /*template_p=*/&is_template,
16091 /*declarator_p=*/false,
16092 /*optional_p=*/false);
16093 if (TREE_CODE (default_argument) == TYPE_DECL)
16094 /* If the id-expression was a template-id that refers to
16095 a template-class, we already have the declaration here,
16096 so no further lookup is needed. */
16097 ;
16098 else
16099 /* Look up the name. */
16100 default_argument
16101 = cp_parser_lookup_name (parser, default_argument,
16102 none_type,
16103 /*is_template=*/is_template,
16104 /*is_namespace=*/false,
16105 /*check_dependency=*/true,
16106 /*ambiguous_decls=*/NULL,
16107 token->location);
16108 /* See if the default argument is valid. */
16109 default_argument = check_template_template_default_arg (default_argument);
16110 pop_deferring_access_checks ();
16111 return default_argument;
16112 }
16113
16114 /* Parse a template-parameter.
16115
16116 template-parameter:
16117 type-parameter
16118 parameter-declaration
16119
16120 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
16121 the parameter. The TREE_PURPOSE is the default value, if any.
16122 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
16123 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
16124 set to true iff this parameter is a parameter pack. */
16125
16126 static tree
16127 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
16128 bool *is_parameter_pack)
16129 {
16130 cp_token *token;
16131 cp_parameter_declarator *parameter_declarator;
16132 tree parm;
16133
16134 /* Assume it is a type parameter or a template parameter. */
16135 *is_non_type = false;
16136 /* Assume it not a parameter pack. */
16137 *is_parameter_pack = false;
16138 /* Peek at the next token. */
16139 token = cp_lexer_peek_token (parser->lexer);
16140 /* If it is `template', we have a type-parameter. */
16141 if (token->keyword == RID_TEMPLATE)
16142 return cp_parser_type_parameter (parser, is_parameter_pack);
16143 /* If it is `class' or `typename' we do not know yet whether it is a
16144 type parameter or a non-type parameter. Consider:
16145
16146 template <typename T, typename T::X X> ...
16147
16148 or:
16149
16150 template <class C, class D*> ...
16151
16152 Here, the first parameter is a type parameter, and the second is
16153 a non-type parameter. We can tell by looking at the token after
16154 the identifier -- if it is a `,', `=', or `>' then we have a type
16155 parameter. */
16156 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
16157 {
16158 /* Peek at the token after `class' or `typename'. */
16159 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16160 /* If it's an ellipsis, we have a template type parameter
16161 pack. */
16162 if (token->type == CPP_ELLIPSIS)
16163 return cp_parser_type_parameter (parser, is_parameter_pack);
16164 /* If it's an identifier, skip it. */
16165 if (token->type == CPP_NAME)
16166 token = cp_lexer_peek_nth_token (parser->lexer, 3);
16167 /* Now, see if the token looks like the end of a template
16168 parameter. */
16169 if (token->type == CPP_COMMA
16170 || token->type == CPP_EQ
16171 || token->type == CPP_GREATER)
16172 return cp_parser_type_parameter (parser, is_parameter_pack);
16173 }
16174
16175 /* Otherwise, it is a non-type parameter or a constrained parameter.
16176
16177 [temp.param]
16178
16179 When parsing a default template-argument for a non-type
16180 template-parameter, the first non-nested `>' is taken as the end
16181 of the template parameter-list rather than a greater-than
16182 operator. */
16183 parameter_declarator
16184 = cp_parser_parameter_declaration (parser,
16185 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16186 /*template_parm_p=*/true,
16187 /*parenthesized_p=*/NULL);
16188
16189 if (!parameter_declarator)
16190 return error_mark_node;
16191
16192 /* If the parameter declaration is marked as a parameter pack, set
16193 *IS_PARAMETER_PACK to notify the caller. */
16194 if (parameter_declarator->template_parameter_pack_p)
16195 *is_parameter_pack = true;
16196
16197 if (parameter_declarator->default_argument)
16198 {
16199 /* Can happen in some cases of erroneous input (c++/34892). */
16200 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16201 /* Consume the `...' for better error recovery. */
16202 cp_lexer_consume_token (parser->lexer);
16203 }
16204
16205 /* The parameter may have been constrained type parameter. */
16206 if (is_constrained_parameter (parameter_declarator))
16207 return finish_constrained_parameter (parser,
16208 parameter_declarator,
16209 is_non_type);
16210
16211 // Now we're sure that the parameter is a non-type parameter.
16212 *is_non_type = true;
16213
16214 parm = grokdeclarator (parameter_declarator->declarator,
16215 &parameter_declarator->decl_specifiers,
16216 TPARM, /*initialized=*/0,
16217 /*attrlist=*/NULL);
16218 if (parm == error_mark_node)
16219 return error_mark_node;
16220
16221 return build_tree_list (parameter_declarator->default_argument, parm);
16222 }
16223
16224 /* Parse a type-parameter.
16225
16226 type-parameter:
16227 class identifier [opt]
16228 class identifier [opt] = type-id
16229 typename identifier [opt]
16230 typename identifier [opt] = type-id
16231 template < template-parameter-list > class identifier [opt]
16232 template < template-parameter-list > class identifier [opt]
16233 = id-expression
16234
16235 GNU Extension (variadic templates):
16236
16237 type-parameter:
16238 class ... identifier [opt]
16239 typename ... identifier [opt]
16240
16241 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16242 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16243 the declaration of the parameter.
16244
16245 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16246
16247 static tree
16248 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
16249 {
16250 cp_token *token;
16251 tree parameter;
16252
16253 /* Look for a keyword to tell us what kind of parameter this is. */
16254 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
16255 if (!token)
16256 return error_mark_node;
16257
16258 switch (token->keyword)
16259 {
16260 case RID_CLASS:
16261 case RID_TYPENAME:
16262 {
16263 tree identifier;
16264 tree default_argument;
16265
16266 /* If the next token is an ellipsis, we have a template
16267 argument pack. */
16268 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16269 {
16270 /* Consume the `...' token. */
16271 cp_lexer_consume_token (parser->lexer);
16272 maybe_warn_variadic_templates ();
16273
16274 *is_parameter_pack = true;
16275 }
16276
16277 /* If the next token is an identifier, then it names the
16278 parameter. */
16279 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16280 identifier = cp_parser_identifier (parser);
16281 else
16282 identifier = NULL_TREE;
16283
16284 /* Create the parameter. */
16285 parameter = finish_template_type_parm (class_type_node, identifier);
16286
16287 /* If the next token is an `=', we have a default argument. */
16288 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16289 {
16290 default_argument
16291 = cp_parser_default_type_template_argument (parser);
16292
16293 /* Template parameter packs cannot have default
16294 arguments. */
16295 if (*is_parameter_pack)
16296 {
16297 if (identifier)
16298 error_at (token->location,
16299 "template parameter pack %qD cannot have a "
16300 "default argument", identifier);
16301 else
16302 error_at (token->location,
16303 "template parameter packs cannot have "
16304 "default arguments");
16305 default_argument = NULL_TREE;
16306 }
16307 else if (check_for_bare_parameter_packs (default_argument))
16308 default_argument = error_mark_node;
16309 }
16310 else
16311 default_argument = NULL_TREE;
16312
16313 /* Create the combined representation of the parameter and the
16314 default argument. */
16315 parameter = build_tree_list (default_argument, parameter);
16316 }
16317 break;
16318
16319 case RID_TEMPLATE:
16320 {
16321 tree identifier;
16322 tree default_argument;
16323
16324 /* Look for the `<'. */
16325 cp_parser_require (parser, CPP_LESS, RT_LESS);
16326 /* Parse the template-parameter-list. */
16327 cp_parser_template_parameter_list (parser);
16328 /* Look for the `>'. */
16329 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16330
16331 /* If template requirements are present, parse them. */
16332 if (flag_concepts)
16333 {
16334 tree reqs = get_shorthand_constraints (current_template_parms);
16335 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
16336 reqs = combine_constraint_expressions (reqs, dreqs);
16337 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16338 }
16339
16340 /* Look for the `class' or 'typename' keywords. */
16341 cp_parser_type_parameter_key (parser);
16342 /* If the next token is an ellipsis, we have a template
16343 argument pack. */
16344 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16345 {
16346 /* Consume the `...' token. */
16347 cp_lexer_consume_token (parser->lexer);
16348 maybe_warn_variadic_templates ();
16349
16350 *is_parameter_pack = true;
16351 }
16352 /* If the next token is an `=', then there is a
16353 default-argument. If the next token is a `>', we are at
16354 the end of the parameter-list. If the next token is a `,',
16355 then we are at the end of this parameter. */
16356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16357 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16358 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16359 {
16360 identifier = cp_parser_identifier (parser);
16361 /* Treat invalid names as if the parameter were nameless. */
16362 if (identifier == error_mark_node)
16363 identifier = NULL_TREE;
16364 }
16365 else
16366 identifier = NULL_TREE;
16367
16368 /* Create the template parameter. */
16369 parameter = finish_template_template_parm (class_type_node,
16370 identifier);
16371
16372 /* If the next token is an `=', then there is a
16373 default-argument. */
16374 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16375 {
16376 default_argument
16377 = cp_parser_default_template_template_argument (parser);
16378
16379 /* Template parameter packs cannot have default
16380 arguments. */
16381 if (*is_parameter_pack)
16382 {
16383 if (identifier)
16384 error_at (token->location,
16385 "template parameter pack %qD cannot "
16386 "have a default argument",
16387 identifier);
16388 else
16389 error_at (token->location, "template parameter packs cannot "
16390 "have default arguments");
16391 default_argument = NULL_TREE;
16392 }
16393 }
16394 else
16395 default_argument = NULL_TREE;
16396
16397 /* Create the combined representation of the parameter and the
16398 default argument. */
16399 parameter = build_tree_list (default_argument, parameter);
16400 }
16401 break;
16402
16403 default:
16404 gcc_unreachable ();
16405 break;
16406 }
16407
16408 return parameter;
16409 }
16410
16411 /* Parse a template-id.
16412
16413 template-id:
16414 template-name < template-argument-list [opt] >
16415
16416 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16417 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16418 returned. Otherwise, if the template-name names a function, or set
16419 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16420 names a class, returns a TYPE_DECL for the specialization.
16421
16422 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16423 uninstantiated templates. */
16424
16425 static tree
16426 cp_parser_template_id (cp_parser *parser,
16427 bool template_keyword_p,
16428 bool check_dependency_p,
16429 enum tag_types tag_type,
16430 bool is_declaration)
16431 {
16432 tree templ;
16433 tree arguments;
16434 tree template_id;
16435 cp_token_position start_of_id = 0;
16436 cp_token *next_token = NULL, *next_token_2 = NULL;
16437 bool is_identifier;
16438
16439 /* If the next token corresponds to a template-id, there is no need
16440 to reparse it. */
16441 cp_token *token = cp_lexer_peek_token (parser->lexer);
16442
16443 if (token->type == CPP_TEMPLATE_ID)
16444 {
16445 cp_lexer_consume_token (parser->lexer);
16446 return saved_checks_value (token->u.tree_check_value);
16447 }
16448
16449 /* Avoid performing name lookup if there is no possibility of
16450 finding a template-id. */
16451 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16452 || (token->type == CPP_NAME
16453 && !cp_parser_nth_token_starts_template_argument_list_p
16454 (parser, 2)))
16455 {
16456 cp_parser_error (parser, "expected template-id");
16457 return error_mark_node;
16458 }
16459
16460 /* Remember where the template-id starts. */
16461 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16462 start_of_id = cp_lexer_token_position (parser->lexer, false);
16463
16464 push_deferring_access_checks (dk_deferred);
16465
16466 /* Parse the template-name. */
16467 is_identifier = false;
16468 templ = cp_parser_template_name (parser, template_keyword_p,
16469 check_dependency_p,
16470 is_declaration,
16471 tag_type,
16472 &is_identifier);
16473
16474 /* Push any access checks inside the firewall we're about to create. */
16475 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16476 pop_deferring_access_checks ();
16477 if (templ == error_mark_node || is_identifier)
16478 return templ;
16479
16480 /* Since we're going to preserve any side-effects from this parse, set up a
16481 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16482 in the template arguments. */
16483 tentative_firewall firewall (parser);
16484 reopen_deferring_access_checks (checks);
16485
16486 /* If we find the sequence `[:' after a template-name, it's probably
16487 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16488 parse correctly the argument list. */
16489 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16490 == CPP_OPEN_SQUARE)
16491 && next_token->flags & DIGRAPH
16492 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16493 == CPP_COLON)
16494 && !(next_token_2->flags & PREV_WHITE))
16495 {
16496 cp_parser_parse_tentatively (parser);
16497 /* Change `:' into `::'. */
16498 next_token_2->type = CPP_SCOPE;
16499 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16500 CPP_LESS. */
16501 cp_lexer_consume_token (parser->lexer);
16502
16503 /* Parse the arguments. */
16504 arguments = cp_parser_enclosed_template_argument_list (parser);
16505 if (!cp_parser_parse_definitely (parser))
16506 {
16507 /* If we couldn't parse an argument list, then we revert our changes
16508 and return simply an error. Maybe this is not a template-id
16509 after all. */
16510 next_token_2->type = CPP_COLON;
16511 cp_parser_error (parser, "expected %<<%>");
16512 pop_deferring_access_checks ();
16513 return error_mark_node;
16514 }
16515 /* Otherwise, emit an error about the invalid digraph, but continue
16516 parsing because we got our argument list. */
16517 if (permerror (next_token->location,
16518 "%<<::%> cannot begin a template-argument list"))
16519 {
16520 static bool hint = false;
16521 inform (next_token->location,
16522 "%<<:%> is an alternate spelling for %<[%>."
16523 " Insert whitespace between %<<%> and %<::%>");
16524 if (!hint && !flag_permissive)
16525 {
16526 inform (next_token->location, "(if you use %<-fpermissive%> "
16527 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16528 "accept your code)");
16529 hint = true;
16530 }
16531 }
16532 }
16533 else
16534 {
16535 /* Look for the `<' that starts the template-argument-list. */
16536 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16537 {
16538 pop_deferring_access_checks ();
16539 return error_mark_node;
16540 }
16541 /* Parse the arguments. */
16542 arguments = cp_parser_enclosed_template_argument_list (parser);
16543
16544 if ((cxx_dialect > cxx17)
16545 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16546 && !template_keyword_p
16547 && (cp_parser_error_occurred (parser)
16548 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16549 {
16550 /* This didn't go well. */
16551 if (TREE_CODE (templ) == FUNCTION_DECL)
16552 {
16553 /* C++2A says that "function-name < a;" is now ill-formed. */
16554 if (cp_parser_error_occurred (parser))
16555 {
16556 error_at (token->location, "invalid template-argument-list");
16557 inform (token->location, "function name as the left hand "
16558 "operand of %<<%> is ill-formed in C++2a; wrap the "
16559 "function name in %<()%>");
16560 }
16561 else
16562 /* We expect "f<targs>" to be followed by "(args)". */
16563 error_at (cp_lexer_peek_token (parser->lexer)->location,
16564 "expected %<(%> after template-argument-list");
16565 if (start_of_id)
16566 /* Purge all subsequent tokens. */
16567 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16568 }
16569 else
16570 cp_parser_simulate_error (parser);
16571 pop_deferring_access_checks ();
16572 return error_mark_node;
16573 }
16574 }
16575
16576 /* Set the location to be of the form:
16577 template-name < template-argument-list [opt] >
16578 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16579 with caret == start at the start of the template-name,
16580 ranging until the closing '>'. */
16581 location_t combined_loc
16582 = make_location (token->location, token->location, parser->lexer);
16583
16584 /* Check for concepts autos where they don't belong. We could
16585 identify types in some cases of identifier TEMPL, looking ahead
16586 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16587 types. We reject them in functions, but if what we have is an
16588 identifier, even with none_type we can't conclude it's NOT a
16589 type, we have to wait for template substitution. */
16590 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16591 template_id = error_mark_node;
16592 /* Build a representation of the specialization. */
16593 else if (identifier_p (templ))
16594 template_id = build_min_nt_loc (combined_loc,
16595 TEMPLATE_ID_EXPR,
16596 templ, arguments);
16597 else if (DECL_TYPE_TEMPLATE_P (templ)
16598 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16599 {
16600 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16601 template (rather than some instantiation thereof) only if
16602 is not nested within some other construct. For example, in
16603 "template <typename T> void f(T) { A<T>::", A<T> is just an
16604 instantiation of A. */
16605 bool entering_scope
16606 = (template_parm_scope_p ()
16607 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16608 template_id
16609 = finish_template_type (templ, arguments, entering_scope);
16610 }
16611 else if (concept_definition_p (templ))
16612 {
16613 /* The caller will decide whether this is a concept check or type
16614 constraint. */
16615 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
16616 boolean_type_node, templ, arguments);
16617 }
16618 else if (variable_template_p (templ))
16619 {
16620 template_id = lookup_template_variable (templ, arguments);
16621 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16622 SET_EXPR_LOCATION (template_id, combined_loc);
16623 }
16624 else
16625 {
16626 /* If it's not a class-template or a template-template, it should be
16627 a function-template. */
16628 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
16629
16630 template_id = lookup_template_function (templ, arguments);
16631 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16632 SET_EXPR_LOCATION (template_id, combined_loc);
16633 }
16634
16635 /* If parsing tentatively, replace the sequence of tokens that makes
16636 up the template-id with a CPP_TEMPLATE_ID token. That way,
16637 should we re-parse the token stream, we will not have to repeat
16638 the effort required to do the parse, nor will we issue duplicate
16639 error messages about problems during instantiation of the
16640 template. */
16641 if (start_of_id
16642 /* Don't do this if we had a parse error in a declarator; re-parsing
16643 might succeed if a name changes meaning (60361). */
16644 && !(cp_parser_error_occurred (parser)
16645 && cp_parser_parsing_tentatively (parser)
16646 && parser->in_declarator_p))
16647 {
16648 /* Reset the contents of the START_OF_ID token. */
16649 token->type = CPP_TEMPLATE_ID;
16650 token->location = combined_loc;
16651
16652 /* Retrieve any deferred checks. Do not pop this access checks yet
16653 so the memory will not be reclaimed during token replacing below. */
16654 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16655 token->tree_check_p = true;
16656 token->u.tree_check_value->value = template_id;
16657 token->u.tree_check_value->checks = get_deferred_access_checks ();
16658 token->keyword = RID_MAX;
16659
16660 /* Purge all subsequent tokens. */
16661 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16662
16663 /* ??? Can we actually assume that, if template_id ==
16664 error_mark_node, we will have issued a diagnostic to the
16665 user, as opposed to simply marking the tentative parse as
16666 failed? */
16667 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16668 error_at (token->location, "parse error in template argument list");
16669 }
16670
16671 pop_to_parent_deferring_access_checks ();
16672 return template_id;
16673 }
16674
16675 /* Like cp_parser_template_id, called in non-type context. */
16676
16677 static tree
16678 cp_parser_template_id_expr (cp_parser *parser,
16679 bool template_keyword_p,
16680 bool check_dependency_p,
16681 bool is_declaration)
16682 {
16683 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
16684 none_type, is_declaration);
16685 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
16686 && concept_check_p (x))
16687 /* We didn't check the arguments in cp_parser_template_id; do that now. */
16688 return build_concept_id (x);
16689 return x;
16690 }
16691
16692 /* Parse a template-name.
16693
16694 template-name:
16695 identifier
16696
16697 The standard should actually say:
16698
16699 template-name:
16700 identifier
16701 operator-function-id
16702
16703 A defect report has been filed about this issue.
16704
16705 A conversion-function-id cannot be a template name because they cannot
16706 be part of a template-id. In fact, looking at this code:
16707
16708 a.operator K<int>()
16709
16710 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16711 It is impossible to call a templated conversion-function-id with an
16712 explicit argument list, since the only allowed template parameter is
16713 the type to which it is converting.
16714
16715 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16716 `template' keyword, in a construction like:
16717
16718 T::template f<3>()
16719
16720 In that case `f' is taken to be a template-name, even though there
16721 is no way of knowing for sure.
16722
16723 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16724 name refers to a set of overloaded functions, at least one of which
16725 is a template, or an IDENTIFIER_NODE with the name of the template,
16726 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16727 names are looked up inside uninstantiated templates. */
16728
16729 static tree
16730 cp_parser_template_name (cp_parser* parser,
16731 bool template_keyword_p,
16732 bool check_dependency_p,
16733 bool is_declaration,
16734 enum tag_types tag_type,
16735 bool *is_identifier)
16736 {
16737 tree identifier;
16738 tree decl;
16739 cp_token *token = cp_lexer_peek_token (parser->lexer);
16740
16741 /* If the next token is `operator', then we have either an
16742 operator-function-id or a conversion-function-id. */
16743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16744 {
16745 /* We don't know whether we're looking at an
16746 operator-function-id or a conversion-function-id. */
16747 cp_parser_parse_tentatively (parser);
16748 /* Try an operator-function-id. */
16749 identifier = cp_parser_operator_function_id (parser);
16750 /* If that didn't work, try a conversion-function-id. */
16751 if (!cp_parser_parse_definitely (parser))
16752 {
16753 cp_parser_error (parser, "expected template-name");
16754 return error_mark_node;
16755 }
16756 }
16757 /* Look for the identifier. */
16758 else
16759 identifier = cp_parser_identifier (parser);
16760
16761 /* If we didn't find an identifier, we don't have a template-id. */
16762 if (identifier == error_mark_node)
16763 return error_mark_node;
16764
16765 /* If the name immediately followed the `template' keyword, then it
16766 is a template-name. However, if the next token is not `<', then
16767 we do not treat it as a template-name, since it is not being used
16768 as part of a template-id. This enables us to handle constructs
16769 like:
16770
16771 template <typename T> struct S { S(); };
16772 template <typename T> S<T>::S();
16773
16774 correctly. We would treat `S' as a template -- if it were `S<T>'
16775 -- but we do not if there is no `<'. */
16776
16777 if (processing_template_decl
16778 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16779 {
16780 /* In a declaration, in a dependent context, we pretend that the
16781 "template" keyword was present in order to improve error
16782 recovery. For example, given:
16783
16784 template <typename T> void f(T::X<int>);
16785
16786 we want to treat "X<int>" as a template-id. */
16787 if (is_declaration
16788 && !template_keyword_p
16789 && parser->scope && TYPE_P (parser->scope)
16790 && check_dependency_p
16791 && dependent_scope_p (parser->scope)
16792 /* Do not do this for dtors (or ctors), since they never
16793 need the template keyword before their name. */
16794 && !constructor_name_p (identifier, parser->scope))
16795 {
16796 cp_token_position start = 0;
16797
16798 /* Explain what went wrong. */
16799 error_at (token->location, "non-template %qD used as template",
16800 identifier);
16801 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16802 parser->scope, identifier);
16803 /* If parsing tentatively, find the location of the "<" token. */
16804 if (cp_parser_simulate_error (parser))
16805 start = cp_lexer_token_position (parser->lexer, true);
16806 /* Parse the template arguments so that we can issue error
16807 messages about them. */
16808 cp_lexer_consume_token (parser->lexer);
16809 cp_parser_enclosed_template_argument_list (parser);
16810 /* Skip tokens until we find a good place from which to
16811 continue parsing. */
16812 cp_parser_skip_to_closing_parenthesis (parser,
16813 /*recovering=*/true,
16814 /*or_comma=*/true,
16815 /*consume_paren=*/false);
16816 /* If parsing tentatively, permanently remove the
16817 template argument list. That will prevent duplicate
16818 error messages from being issued about the missing
16819 "template" keyword. */
16820 if (start)
16821 cp_lexer_purge_tokens_after (parser->lexer, start);
16822 if (is_identifier)
16823 *is_identifier = true;
16824 parser->context->object_type = NULL_TREE;
16825 return identifier;
16826 }
16827
16828 /* If the "template" keyword is present, then there is generally
16829 no point in doing name-lookup, so we just return IDENTIFIER.
16830 But, if the qualifying scope is non-dependent then we can
16831 (and must) do name-lookup normally. */
16832 if (template_keyword_p)
16833 {
16834 tree scope = (parser->scope ? parser->scope
16835 : parser->context->object_type);
16836 if (scope && TYPE_P (scope)
16837 && (!CLASS_TYPE_P (scope)
16838 || (check_dependency_p && dependent_type_p (scope))))
16839 {
16840 /* We're optimizing away the call to cp_parser_lookup_name, but
16841 we still need to do this. */
16842 parser->context->object_type = NULL_TREE;
16843 return identifier;
16844 }
16845 }
16846 }
16847
16848 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16849 const bool scoped_p = ((parser->scope ? parser->scope
16850 : parser->context->object_type) != NULL_TREE);
16851
16852 /* Look up the name. */
16853 decl = cp_parser_lookup_name (parser, identifier,
16854 tag_type,
16855 /*is_template=*/true,
16856 /*is_namespace=*/false,
16857 check_dependency_p,
16858 /*ambiguous_decls=*/NULL,
16859 token->location);
16860
16861 decl = strip_using_decl (decl);
16862
16863 /* If DECL is a template, then the name was a template-name. */
16864 if (TREE_CODE (decl) == TEMPLATE_DECL)
16865 {
16866 if (TREE_DEPRECATED (decl)
16867 && deprecated_state != DEPRECATED_SUPPRESS)
16868 warn_deprecated_use (decl, NULL_TREE);
16869 }
16870 else
16871 {
16872 /* The standard does not explicitly indicate whether a name that
16873 names a set of overloaded declarations, some of which are
16874 templates, is a template-name. However, such a name should
16875 be a template-name; otherwise, there is no way to form a
16876 template-id for the overloaded templates. */
16877 bool found = false;
16878
16879 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16880 !found && iter; ++iter)
16881 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16882 found = true;
16883
16884 if (!found
16885 && (cxx_dialect > cxx17)
16886 && !scoped_p
16887 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
16888 && tag_type == none_type)
16889 {
16890 /* [temp.names] says "A name is also considered to refer to a template
16891 if it is an unqualified-id followed by a < and name lookup finds
16892 either one or more functions or finds nothing." */
16893
16894 /* The "more functions" case. Just use the OVERLOAD as normally.
16895 We don't use is_overloaded_fn here to avoid considering
16896 BASELINKs. */
16897 if (TREE_CODE (decl) == OVERLOAD
16898 /* Name lookup found one function. */
16899 || TREE_CODE (decl) == FUNCTION_DECL)
16900 found = true;
16901 /* Name lookup found nothing. */
16902 else if (decl == error_mark_node)
16903 return identifier;
16904 }
16905
16906 if (!found)
16907 {
16908 /* The name does not name a template. */
16909 cp_parser_error (parser, "expected template-name");
16910 return error_mark_node;
16911 }
16912 }
16913
16914 return decl;
16915 }
16916
16917 /* Parse a template-argument-list.
16918
16919 template-argument-list:
16920 template-argument ... [opt]
16921 template-argument-list , template-argument ... [opt]
16922
16923 Returns a TREE_VEC containing the arguments. */
16924
16925 static tree
16926 cp_parser_template_argument_list (cp_parser* parser)
16927 {
16928 tree fixed_args[10];
16929 unsigned n_args = 0;
16930 unsigned alloced = 10;
16931 tree *arg_ary = fixed_args;
16932 tree vec;
16933 bool saved_in_template_argument_list_p;
16934 bool saved_ice_p;
16935 bool saved_non_ice_p;
16936
16937 /* Don't create location wrapper nodes within a template-argument-list. */
16938 auto_suppress_location_wrappers sentinel;
16939
16940 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16941 parser->in_template_argument_list_p = true;
16942 /* Even if the template-id appears in an integral
16943 constant-expression, the contents of the argument list do
16944 not. */
16945 saved_ice_p = parser->integral_constant_expression_p;
16946 parser->integral_constant_expression_p = false;
16947 saved_non_ice_p = parser->non_integral_constant_expression_p;
16948 parser->non_integral_constant_expression_p = false;
16949
16950 /* Parse the arguments. */
16951 do
16952 {
16953 tree argument;
16954
16955 if (n_args)
16956 /* Consume the comma. */
16957 cp_lexer_consume_token (parser->lexer);
16958
16959 /* Parse the template-argument. */
16960 argument = cp_parser_template_argument (parser);
16961
16962 /* If the next token is an ellipsis, we're expanding a template
16963 argument pack. */
16964 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16965 {
16966 if (argument == error_mark_node)
16967 {
16968 cp_token *token = cp_lexer_peek_token (parser->lexer);
16969 error_at (token->location,
16970 "expected parameter pack before %<...%>");
16971 }
16972 /* Consume the `...' token. */
16973 cp_lexer_consume_token (parser->lexer);
16974
16975 /* Make the argument into a TYPE_PACK_EXPANSION or
16976 EXPR_PACK_EXPANSION. */
16977 argument = make_pack_expansion (argument);
16978 }
16979
16980 if (n_args == alloced)
16981 {
16982 alloced *= 2;
16983
16984 if (arg_ary == fixed_args)
16985 {
16986 arg_ary = XNEWVEC (tree, alloced);
16987 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16988 }
16989 else
16990 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16991 }
16992 arg_ary[n_args++] = argument;
16993 }
16994 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16995
16996 vec = make_tree_vec (n_args);
16997
16998 while (n_args--)
16999 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
17000
17001 if (arg_ary != fixed_args)
17002 free (arg_ary);
17003 parser->non_integral_constant_expression_p = saved_non_ice_p;
17004 parser->integral_constant_expression_p = saved_ice_p;
17005 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
17006 if (CHECKING_P)
17007 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
17008 return vec;
17009 }
17010
17011 /* Parse a template-argument.
17012
17013 template-argument:
17014 assignment-expression
17015 type-id
17016 id-expression
17017
17018 The representation is that of an assignment-expression, type-id, or
17019 id-expression -- except that the qualified id-expression is
17020 evaluated, so that the value returned is either a DECL or an
17021 OVERLOAD.
17022
17023 Although the standard says "assignment-expression", it forbids
17024 throw-expressions or assignments in the template argument.
17025 Therefore, we use "conditional-expression" instead. */
17026
17027 static tree
17028 cp_parser_template_argument (cp_parser* parser)
17029 {
17030 tree argument;
17031 bool template_p;
17032 bool address_p;
17033 bool maybe_type_id = false;
17034 cp_token *token = NULL, *argument_start_token = NULL;
17035 location_t loc = 0;
17036 cp_id_kind idk;
17037
17038 /* There's really no way to know what we're looking at, so we just
17039 try each alternative in order.
17040
17041 [temp.arg]
17042
17043 In a template-argument, an ambiguity between a type-id and an
17044 expression is resolved to a type-id, regardless of the form of
17045 the corresponding template-parameter.
17046
17047 Therefore, we try a type-id first. */
17048 cp_parser_parse_tentatively (parser);
17049 argument = cp_parser_template_type_arg (parser);
17050 /* If there was no error parsing the type-id but the next token is a
17051 '>>', our behavior depends on which dialect of C++ we're
17052 parsing. In C++98, we probably found a typo for '> >'. But there
17053 are type-id which are also valid expressions. For instance:
17054
17055 struct X { int operator >> (int); };
17056 template <int V> struct Foo {};
17057 Foo<X () >> 5> r;
17058
17059 Here 'X()' is a valid type-id of a function type, but the user just
17060 wanted to write the expression "X() >> 5". Thus, we remember that we
17061 found a valid type-id, but we still try to parse the argument as an
17062 expression to see what happens.
17063
17064 In C++0x, the '>>' will be considered two separate '>'
17065 tokens. */
17066 if (!cp_parser_error_occurred (parser)
17067 && cxx_dialect == cxx98
17068 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17069 {
17070 maybe_type_id = true;
17071 cp_parser_abort_tentative_parse (parser);
17072 }
17073 else
17074 {
17075 /* If the next token isn't a `,' or a `>', then this argument wasn't
17076 really finished. This means that the argument is not a valid
17077 type-id. */
17078 if (!cp_parser_next_token_ends_template_argument_p (parser))
17079 cp_parser_error (parser, "expected template-argument");
17080 /* If that worked, we're done. */
17081 if (cp_parser_parse_definitely (parser))
17082 return argument;
17083 }
17084 /* We're still not sure what the argument will be. */
17085 cp_parser_parse_tentatively (parser);
17086 /* Try a template. */
17087 argument_start_token = cp_lexer_peek_token (parser->lexer);
17088 argument = cp_parser_id_expression (parser,
17089 /*template_keyword_p=*/false,
17090 /*check_dependency_p=*/true,
17091 &template_p,
17092 /*declarator_p=*/false,
17093 /*optional_p=*/false);
17094 /* If the next token isn't a `,' or a `>', then this argument wasn't
17095 really finished. */
17096 if (!cp_parser_next_token_ends_template_argument_p (parser))
17097 cp_parser_error (parser, "expected template-argument");
17098 if (!cp_parser_error_occurred (parser))
17099 {
17100 /* Figure out what is being referred to. If the id-expression
17101 was for a class template specialization, then we will have a
17102 TYPE_DECL at this point. There is no need to do name lookup
17103 at this point in that case. */
17104 if (TREE_CODE (argument) != TYPE_DECL)
17105 argument = cp_parser_lookup_name (parser, argument,
17106 none_type,
17107 /*is_template=*/template_p,
17108 /*is_namespace=*/false,
17109 /*check_dependency=*/true,
17110 /*ambiguous_decls=*/NULL,
17111 argument_start_token->location);
17112 if (TREE_CODE (argument) != TEMPLATE_DECL
17113 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
17114 cp_parser_error (parser, "expected template-name");
17115 }
17116 if (cp_parser_parse_definitely (parser))
17117 {
17118 if (TREE_DEPRECATED (argument))
17119 warn_deprecated_use (argument, NULL_TREE);
17120 return argument;
17121 }
17122 /* It must be a non-type argument. In C++17 any constant-expression is
17123 allowed. */
17124 if (cxx_dialect > cxx14)
17125 goto general_expr;
17126
17127 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
17128
17129 -- an integral constant-expression of integral or enumeration
17130 type; or
17131
17132 -- the name of a non-type template-parameter; or
17133
17134 -- the name of an object or function with external linkage...
17135
17136 -- the address of an object or function with external linkage...
17137
17138 -- a pointer to member... */
17139 /* Look for a non-type template parameter. */
17140 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17141 {
17142 cp_parser_parse_tentatively (parser);
17143 argument = cp_parser_primary_expression (parser,
17144 /*address_p=*/false,
17145 /*cast_p=*/false,
17146 /*template_arg_p=*/true,
17147 &idk);
17148 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
17149 || !cp_parser_next_token_ends_template_argument_p (parser))
17150 cp_parser_simulate_error (parser);
17151 if (cp_parser_parse_definitely (parser))
17152 return argument;
17153 }
17154
17155 /* If the next token is "&", the argument must be the address of an
17156 object or function with external linkage. */
17157 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
17158 if (address_p)
17159 {
17160 loc = cp_lexer_peek_token (parser->lexer)->location;
17161 cp_lexer_consume_token (parser->lexer);
17162 }
17163 /* See if we might have an id-expression. */
17164 token = cp_lexer_peek_token (parser->lexer);
17165 if (token->type == CPP_NAME
17166 || token->keyword == RID_OPERATOR
17167 || token->type == CPP_SCOPE
17168 || token->type == CPP_TEMPLATE_ID
17169 || token->type == CPP_NESTED_NAME_SPECIFIER)
17170 {
17171 cp_parser_parse_tentatively (parser);
17172 argument = cp_parser_primary_expression (parser,
17173 address_p,
17174 /*cast_p=*/false,
17175 /*template_arg_p=*/true,
17176 &idk);
17177 if (cp_parser_error_occurred (parser)
17178 || !cp_parser_next_token_ends_template_argument_p (parser))
17179 cp_parser_abort_tentative_parse (parser);
17180 else
17181 {
17182 tree probe;
17183
17184 if (INDIRECT_REF_P (argument))
17185 {
17186 /* Strip the dereference temporarily. */
17187 gcc_assert (REFERENCE_REF_P (argument));
17188 argument = TREE_OPERAND (argument, 0);
17189 }
17190
17191 /* If we're in a template, we represent a qualified-id referring
17192 to a static data member as a SCOPE_REF even if the scope isn't
17193 dependent so that we can check access control later. */
17194 probe = argument;
17195 if (TREE_CODE (probe) == SCOPE_REF)
17196 probe = TREE_OPERAND (probe, 1);
17197 if (VAR_P (probe))
17198 {
17199 /* A variable without external linkage might still be a
17200 valid constant-expression, so no error is issued here
17201 if the external-linkage check fails. */
17202 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
17203 cp_parser_simulate_error (parser);
17204 }
17205 else if (is_overloaded_fn (argument))
17206 /* All overloaded functions are allowed; if the external
17207 linkage test does not pass, an error will be issued
17208 later. */
17209 ;
17210 else if (address_p
17211 && (TREE_CODE (argument) == OFFSET_REF
17212 || TREE_CODE (argument) == SCOPE_REF))
17213 /* A pointer-to-member. */
17214 ;
17215 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
17216 ;
17217 else
17218 cp_parser_simulate_error (parser);
17219
17220 if (cp_parser_parse_definitely (parser))
17221 {
17222 if (address_p)
17223 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
17224 tf_warning_or_error);
17225 else
17226 argument = convert_from_reference (argument);
17227 return argument;
17228 }
17229 }
17230 }
17231 /* If the argument started with "&", there are no other valid
17232 alternatives at this point. */
17233 if (address_p)
17234 {
17235 cp_parser_error (parser, "invalid non-type template argument");
17236 return error_mark_node;
17237 }
17238
17239 general_expr:
17240 /* If the argument wasn't successfully parsed as a type-id followed
17241 by '>>', the argument can only be a constant expression now.
17242 Otherwise, we try parsing the constant-expression tentatively,
17243 because the argument could really be a type-id. */
17244 if (maybe_type_id)
17245 cp_parser_parse_tentatively (parser);
17246
17247 if (cxx_dialect <= cxx14)
17248 argument = cp_parser_constant_expression (parser);
17249 else
17250 {
17251 /* In C++20, we can encounter a braced-init-list. */
17252 if (cxx_dialect >= cxx2a
17253 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17254 {
17255 bool expr_non_constant_p;
17256 return cp_parser_braced_list (parser, &expr_non_constant_p);
17257 }
17258
17259 /* With C++17 generalized non-type template arguments we need to handle
17260 lvalue constant expressions, too. */
17261 argument = cp_parser_assignment_expression (parser);
17262 require_potential_constant_expression (argument);
17263 }
17264
17265 if (!maybe_type_id)
17266 return argument;
17267 if (!cp_parser_next_token_ends_template_argument_p (parser))
17268 cp_parser_error (parser, "expected template-argument");
17269 if (cp_parser_parse_definitely (parser))
17270 return argument;
17271 /* We did our best to parse the argument as a non type-id, but that
17272 was the only alternative that matched (albeit with a '>' after
17273 it). We can assume it's just a typo from the user, and a
17274 diagnostic will then be issued. */
17275 return cp_parser_template_type_arg (parser);
17276 }
17277
17278 /* Parse an explicit-instantiation.
17279
17280 explicit-instantiation:
17281 template declaration
17282
17283 Although the standard says `declaration', what it really means is:
17284
17285 explicit-instantiation:
17286 template decl-specifier-seq [opt] declarator [opt] ;
17287
17288 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17289 supposed to be allowed. A defect report has been filed about this
17290 issue.
17291
17292 GNU Extension:
17293
17294 explicit-instantiation:
17295 storage-class-specifier template
17296 decl-specifier-seq [opt] declarator [opt] ;
17297 function-specifier template
17298 decl-specifier-seq [opt] declarator [opt] ; */
17299
17300 static void
17301 cp_parser_explicit_instantiation (cp_parser* parser)
17302 {
17303 int declares_class_or_enum;
17304 cp_decl_specifier_seq decl_specifiers;
17305 tree extension_specifier = NULL_TREE;
17306
17307 timevar_push (TV_TEMPLATE_INST);
17308
17309 /* Look for an (optional) storage-class-specifier or
17310 function-specifier. */
17311 if (cp_parser_allow_gnu_extensions_p (parser))
17312 {
17313 extension_specifier
17314 = cp_parser_storage_class_specifier_opt (parser);
17315 if (!extension_specifier)
17316 extension_specifier
17317 = cp_parser_function_specifier_opt (parser,
17318 /*decl_specs=*/NULL);
17319 }
17320
17321 /* Look for the `template' keyword. */
17322 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17323 /* Let the front end know that we are processing an explicit
17324 instantiation. */
17325 begin_explicit_instantiation ();
17326 /* [temp.explicit] says that we are supposed to ignore access
17327 control while processing explicit instantiation directives. */
17328 push_deferring_access_checks (dk_no_check);
17329 /* Parse a decl-specifier-seq. */
17330 cp_parser_decl_specifier_seq (parser,
17331 CP_PARSER_FLAGS_OPTIONAL,
17332 &decl_specifiers,
17333 &declares_class_or_enum);
17334 /* If there was exactly one decl-specifier, and it declared a class,
17335 and there's no declarator, then we have an explicit type
17336 instantiation. */
17337 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17338 {
17339 tree type;
17340
17341 type = check_tag_decl (&decl_specifiers,
17342 /*explicit_type_instantiation_p=*/true);
17343 /* Turn access control back on for names used during
17344 template instantiation. */
17345 pop_deferring_access_checks ();
17346 if (type)
17347 do_type_instantiation (type, extension_specifier,
17348 /*complain=*/tf_error);
17349 }
17350 else
17351 {
17352 cp_declarator *declarator;
17353 tree decl;
17354
17355 /* Parse the declarator. */
17356 declarator
17357 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17358 CP_PARSER_FLAGS_NONE,
17359 /*ctor_dtor_or_conv_p=*/NULL,
17360 /*parenthesized_p=*/NULL,
17361 /*member_p=*/false,
17362 /*friend_p=*/false,
17363 /*static_p=*/false);
17364 if (declares_class_or_enum & 2)
17365 cp_parser_check_for_definition_in_return_type (declarator,
17366 decl_specifiers.type,
17367 decl_specifiers.locations[ds_type_spec]);
17368 if (declarator != cp_error_declarator)
17369 {
17370 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17371 permerror (decl_specifiers.locations[ds_inline],
17372 "explicit instantiation shall not use"
17373 " %<inline%> specifier");
17374 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17375 permerror (decl_specifiers.locations[ds_constexpr],
17376 "explicit instantiation shall not use"
17377 " %<constexpr%> specifier");
17378 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
17379 permerror (decl_specifiers.locations[ds_consteval],
17380 "explicit instantiation shall not use"
17381 " %<consteval%> specifier");
17382
17383 decl = grokdeclarator (declarator, &decl_specifiers,
17384 NORMAL, 0, &decl_specifiers.attributes);
17385 /* Turn access control back on for names used during
17386 template instantiation. */
17387 pop_deferring_access_checks ();
17388 /* Do the explicit instantiation. */
17389 do_decl_instantiation (decl, extension_specifier);
17390 }
17391 else
17392 {
17393 pop_deferring_access_checks ();
17394 /* Skip the body of the explicit instantiation. */
17395 cp_parser_skip_to_end_of_statement (parser);
17396 }
17397 }
17398 /* We're done with the instantiation. */
17399 end_explicit_instantiation ();
17400
17401 cp_parser_consume_semicolon_at_end_of_statement (parser);
17402
17403 timevar_pop (TV_TEMPLATE_INST);
17404 }
17405
17406 /* Parse an explicit-specialization.
17407
17408 explicit-specialization:
17409 template < > declaration
17410
17411 Although the standard says `declaration', what it really means is:
17412
17413 explicit-specialization:
17414 template <> decl-specifier [opt] init-declarator [opt] ;
17415 template <> function-definition
17416 template <> explicit-specialization
17417 template <> template-declaration */
17418
17419 static void
17420 cp_parser_explicit_specialization (cp_parser* parser)
17421 {
17422 bool need_lang_pop;
17423 cp_token *token = cp_lexer_peek_token (parser->lexer);
17424
17425 /* Look for the `template' keyword. */
17426 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17427 /* Look for the `<'. */
17428 cp_parser_require (parser, CPP_LESS, RT_LESS);
17429 /* Look for the `>'. */
17430 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17431 /* We have processed another parameter list. */
17432 ++parser->num_template_parameter_lists;
17433 /* [temp]
17434
17435 A template ... explicit specialization ... shall not have C
17436 linkage. */
17437 if (current_lang_name == lang_name_c)
17438 {
17439 error_at (token->location, "template specialization with C linkage");
17440 maybe_show_extern_c_location ();
17441 /* Give it C++ linkage to avoid confusing other parts of the
17442 front end. */
17443 push_lang_context (lang_name_cplusplus);
17444 need_lang_pop = true;
17445 }
17446 else
17447 need_lang_pop = false;
17448 /* Let the front end know that we are beginning a specialization. */
17449 if (!begin_specialization ())
17450 {
17451 end_specialization ();
17452 return;
17453 }
17454
17455 /* If the next keyword is `template', we need to figure out whether
17456 or not we're looking a template-declaration. */
17457 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17458 {
17459 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17460 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17461 cp_parser_template_declaration_after_export (parser,
17462 /*member_p=*/false);
17463 else
17464 cp_parser_explicit_specialization (parser);
17465 }
17466 else
17467 /* Parse the dependent declaration. */
17468 cp_parser_single_declaration (parser,
17469 /*checks=*/NULL,
17470 /*member_p=*/false,
17471 /*explicit_specialization_p=*/true,
17472 /*friend_p=*/NULL);
17473 /* We're done with the specialization. */
17474 end_specialization ();
17475 /* For the erroneous case of a template with C linkage, we pushed an
17476 implicit C++ linkage scope; exit that scope now. */
17477 if (need_lang_pop)
17478 pop_lang_context ();
17479 /* We're done with this parameter list. */
17480 --parser->num_template_parameter_lists;
17481 }
17482
17483 /* Parse a type-specifier.
17484
17485 type-specifier:
17486 simple-type-specifier
17487 class-specifier
17488 enum-specifier
17489 elaborated-type-specifier
17490 cv-qualifier
17491
17492 GNU Extension:
17493
17494 type-specifier:
17495 __complex__
17496
17497 Returns a representation of the type-specifier. For a
17498 class-specifier, enum-specifier, or elaborated-type-specifier, a
17499 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17500
17501 The parser flags FLAGS is used to control type-specifier parsing.
17502
17503 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17504 in a decl-specifier-seq.
17505
17506 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17507 class-specifier, enum-specifier, or elaborated-type-specifier, then
17508 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17509 if a type is declared; 2 if it is defined. Otherwise, it is set to
17510 zero.
17511
17512 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17513 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17514 is set to FALSE. */
17515
17516 static tree
17517 cp_parser_type_specifier (cp_parser* parser,
17518 cp_parser_flags flags,
17519 cp_decl_specifier_seq *decl_specs,
17520 bool is_declaration,
17521 int* declares_class_or_enum,
17522 bool* is_cv_qualifier)
17523 {
17524 tree type_spec = NULL_TREE;
17525 cp_token *token;
17526 enum rid keyword;
17527 cp_decl_spec ds = ds_last;
17528
17529 /* Assume this type-specifier does not declare a new type. */
17530 if (declares_class_or_enum)
17531 *declares_class_or_enum = 0;
17532 /* And that it does not specify a cv-qualifier. */
17533 if (is_cv_qualifier)
17534 *is_cv_qualifier = false;
17535 /* Peek at the next token. */
17536 token = cp_lexer_peek_token (parser->lexer);
17537
17538 /* If we're looking at a keyword, we can use that to guide the
17539 production we choose. */
17540 keyword = token->keyword;
17541 switch (keyword)
17542 {
17543 case RID_ENUM:
17544 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17545 goto elaborated_type_specifier;
17546
17547 /* Look for the enum-specifier. */
17548 type_spec = cp_parser_enum_specifier (parser);
17549 /* If that worked, we're done. */
17550 if (type_spec)
17551 {
17552 if (declares_class_or_enum)
17553 *declares_class_or_enum = 2;
17554 if (decl_specs)
17555 cp_parser_set_decl_spec_type (decl_specs,
17556 type_spec,
17557 token,
17558 /*type_definition_p=*/true);
17559 return type_spec;
17560 }
17561 else
17562 goto elaborated_type_specifier;
17563
17564 /* Any of these indicate either a class-specifier, or an
17565 elaborated-type-specifier. */
17566 case RID_CLASS:
17567 case RID_STRUCT:
17568 case RID_UNION:
17569 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17570 goto elaborated_type_specifier;
17571
17572 /* Parse tentatively so that we can back up if we don't find a
17573 class-specifier. */
17574 cp_parser_parse_tentatively (parser);
17575 /* Look for the class-specifier. */
17576 type_spec = cp_parser_class_specifier (parser);
17577 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17578 /* If that worked, we're done. */
17579 if (cp_parser_parse_definitely (parser))
17580 {
17581 if (declares_class_or_enum)
17582 *declares_class_or_enum = 2;
17583 if (decl_specs)
17584 cp_parser_set_decl_spec_type (decl_specs,
17585 type_spec,
17586 token,
17587 /*type_definition_p=*/true);
17588 return type_spec;
17589 }
17590
17591 /* Fall through. */
17592 elaborated_type_specifier:
17593 /* We're declaring (not defining) a class or enum. */
17594 if (declares_class_or_enum)
17595 *declares_class_or_enum = 1;
17596
17597 /* Fall through. */
17598 case RID_TYPENAME:
17599 /* Look for an elaborated-type-specifier. */
17600 type_spec
17601 = (cp_parser_elaborated_type_specifier
17602 (parser,
17603 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17604 is_declaration));
17605 if (decl_specs)
17606 cp_parser_set_decl_spec_type (decl_specs,
17607 type_spec,
17608 token,
17609 /*type_definition_p=*/false);
17610 return type_spec;
17611
17612 case RID_CONST:
17613 ds = ds_const;
17614 if (is_cv_qualifier)
17615 *is_cv_qualifier = true;
17616 break;
17617
17618 case RID_VOLATILE:
17619 ds = ds_volatile;
17620 if (is_cv_qualifier)
17621 *is_cv_qualifier = true;
17622 break;
17623
17624 case RID_RESTRICT:
17625 ds = ds_restrict;
17626 if (is_cv_qualifier)
17627 *is_cv_qualifier = true;
17628 break;
17629
17630 case RID_COMPLEX:
17631 /* The `__complex__' keyword is a GNU extension. */
17632 ds = ds_complex;
17633 break;
17634
17635 default:
17636 break;
17637 }
17638
17639 /* Handle simple keywords. */
17640 if (ds != ds_last)
17641 {
17642 if (decl_specs)
17643 {
17644 set_and_check_decl_spec_loc (decl_specs, ds, token);
17645 decl_specs->any_specifiers_p = true;
17646 }
17647 return cp_lexer_consume_token (parser->lexer)->u.value;
17648 }
17649
17650 /* If we do not already have a type-specifier, assume we are looking
17651 at a simple-type-specifier. */
17652 type_spec = cp_parser_simple_type_specifier (parser,
17653 decl_specs,
17654 flags);
17655
17656 /* If we didn't find a type-specifier, and a type-specifier was not
17657 optional in this context, issue an error message. */
17658 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17659 {
17660 cp_parser_error (parser, "expected type specifier");
17661 return error_mark_node;
17662 }
17663
17664 return type_spec;
17665 }
17666
17667 /* Parse a simple-type-specifier.
17668
17669 simple-type-specifier:
17670 :: [opt] nested-name-specifier [opt] type-name
17671 :: [opt] nested-name-specifier template template-id
17672 char
17673 wchar_t
17674 bool
17675 short
17676 int
17677 long
17678 signed
17679 unsigned
17680 float
17681 double
17682 void
17683
17684 C++11 Extension:
17685
17686 simple-type-specifier:
17687 auto
17688 decltype ( expression )
17689 char16_t
17690 char32_t
17691 __underlying_type ( type-id )
17692
17693 C++17 extension:
17694
17695 nested-name-specifier(opt) template-name
17696
17697 GNU Extension:
17698
17699 simple-type-specifier:
17700 __int128
17701 __typeof__ unary-expression
17702 __typeof__ ( type-id )
17703 __typeof__ ( type-id ) { initializer-list , [opt] }
17704
17705 Concepts Extension:
17706
17707 simple-type-specifier:
17708 constrained-type-specifier
17709
17710 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17711 appropriately updated. */
17712
17713 static tree
17714 cp_parser_simple_type_specifier (cp_parser* parser,
17715 cp_decl_specifier_seq *decl_specs,
17716 cp_parser_flags flags)
17717 {
17718 tree type = NULL_TREE;
17719 cp_token *token;
17720 int idx;
17721
17722 /* Peek at the next token. */
17723 token = cp_lexer_peek_token (parser->lexer);
17724
17725 /* If we're looking at a keyword, things are easy. */
17726 switch (token->keyword)
17727 {
17728 case RID_CHAR:
17729 if (decl_specs)
17730 decl_specs->explicit_char_p = true;
17731 type = char_type_node;
17732 break;
17733 case RID_CHAR8:
17734 type = char8_type_node;
17735 break;
17736 case RID_CHAR16:
17737 type = char16_type_node;
17738 break;
17739 case RID_CHAR32:
17740 type = char32_type_node;
17741 break;
17742 case RID_WCHAR:
17743 type = wchar_type_node;
17744 break;
17745 case RID_BOOL:
17746 type = boolean_type_node;
17747 break;
17748 case RID_SHORT:
17749 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17750 type = short_integer_type_node;
17751 break;
17752 case RID_INT:
17753 if (decl_specs)
17754 decl_specs->explicit_int_p = true;
17755 type = integer_type_node;
17756 break;
17757 case RID_INT_N_0:
17758 case RID_INT_N_1:
17759 case RID_INT_N_2:
17760 case RID_INT_N_3:
17761 idx = token->keyword - RID_INT_N_0;
17762 if (! int_n_enabled_p [idx])
17763 break;
17764 if (decl_specs)
17765 {
17766 decl_specs->explicit_intN_p = true;
17767 decl_specs->int_n_idx = idx;
17768 /* Check if the alternate "__intN__" form has been used instead of
17769 "__intN". */
17770 if (strncmp (IDENTIFIER_POINTER (token->u.value)
17771 + (IDENTIFIER_LENGTH (token->u.value) - 2),
17772 "__", 2) == 0)
17773 decl_specs->int_n_alt = true;
17774 }
17775 type = int_n_trees [idx].signed_type;
17776 break;
17777 case RID_LONG:
17778 if (decl_specs)
17779 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17780 type = long_integer_type_node;
17781 break;
17782 case RID_SIGNED:
17783 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17784 type = integer_type_node;
17785 break;
17786 case RID_UNSIGNED:
17787 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17788 type = unsigned_type_node;
17789 break;
17790 case RID_FLOAT:
17791 type = float_type_node;
17792 break;
17793 case RID_DOUBLE:
17794 type = double_type_node;
17795 break;
17796 case RID_VOID:
17797 type = void_type_node;
17798 break;
17799
17800 case RID_AUTO:
17801 maybe_warn_cpp0x (CPP0X_AUTO);
17802 if (parser->auto_is_implicit_function_template_parm_p)
17803 {
17804 /* The 'auto' might be the placeholder return type for a function decl
17805 with trailing return type. */
17806 bool have_trailing_return_fn_decl = false;
17807
17808 cp_parser_parse_tentatively (parser);
17809 cp_lexer_consume_token (parser->lexer);
17810 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17811 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17812 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17813 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17814 {
17815 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17816 {
17817 cp_lexer_consume_token (parser->lexer);
17818 cp_parser_skip_to_closing_parenthesis (parser,
17819 /*recovering*/false,
17820 /*or_comma*/false,
17821 /*consume_paren*/true);
17822 continue;
17823 }
17824
17825 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17826 {
17827 have_trailing_return_fn_decl = true;
17828 break;
17829 }
17830
17831 cp_lexer_consume_token (parser->lexer);
17832 }
17833 cp_parser_abort_tentative_parse (parser);
17834
17835 if (have_trailing_return_fn_decl)
17836 {
17837 type = make_auto ();
17838 break;
17839 }
17840
17841 if (cxx_dialect >= cxx14)
17842 {
17843 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17844 type = TREE_TYPE (type);
17845 }
17846 else
17847 type = error_mark_node;
17848
17849 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17850 {
17851 if (cxx_dialect < cxx14)
17852 error_at (token->location,
17853 "use of %<auto%> in lambda parameter declaration "
17854 "only available with "
17855 "%<-std=c++14%> or %<-std=gnu++14%>");
17856 }
17857 else if (cxx_dialect < cxx14)
17858 error_at (token->location,
17859 "use of %<auto%> in parameter declaration "
17860 "only available with "
17861 "%<-std=c++14%> or %<-std=gnu++14%>");
17862 else if (!flag_concepts)
17863 pedwarn (token->location, 0,
17864 "use of %<auto%> in parameter declaration "
17865 "only available with %<-fconcepts-ts%>");
17866 }
17867 else
17868 type = make_auto ();
17869 break;
17870
17871 case RID_DECLTYPE:
17872 /* Since DR 743, decltype can either be a simple-type-specifier by
17873 itself or begin a nested-name-specifier. Parsing it will replace
17874 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17875 handling below decide what to do. */
17876 cp_parser_decltype (parser);
17877 cp_lexer_set_token_position (parser->lexer, token);
17878 break;
17879
17880 case RID_TYPEOF:
17881 /* Consume the `typeof' token. */
17882 cp_lexer_consume_token (parser->lexer);
17883 /* Parse the operand to `typeof'. */
17884 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17885 /* If it is not already a TYPE, take its type. */
17886 if (!TYPE_P (type))
17887 type = finish_typeof (type);
17888
17889 if (decl_specs)
17890 cp_parser_set_decl_spec_type (decl_specs, type,
17891 token,
17892 /*type_definition_p=*/false);
17893
17894 return type;
17895
17896 case RID_UNDERLYING_TYPE:
17897 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17898 if (decl_specs)
17899 cp_parser_set_decl_spec_type (decl_specs, type,
17900 token,
17901 /*type_definition_p=*/false);
17902
17903 return type;
17904
17905 case RID_BASES:
17906 case RID_DIRECT_BASES:
17907 type = cp_parser_trait_expr (parser, token->keyword);
17908 if (decl_specs)
17909 cp_parser_set_decl_spec_type (decl_specs, type,
17910 token,
17911 /*type_definition_p=*/false);
17912 return type;
17913 default:
17914 break;
17915 }
17916
17917 /* If token is an already-parsed decltype not followed by ::,
17918 it's a simple-type-specifier. */
17919 if (token->type == CPP_DECLTYPE
17920 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17921 {
17922 type = saved_checks_value (token->u.tree_check_value);
17923 if (decl_specs)
17924 {
17925 cp_parser_set_decl_spec_type (decl_specs, type,
17926 token,
17927 /*type_definition_p=*/false);
17928 /* Remember that we are handling a decltype in order to
17929 implement the resolution of DR 1510 when the argument
17930 isn't instantiation dependent. */
17931 decl_specs->decltype_p = true;
17932 }
17933 cp_lexer_consume_token (parser->lexer);
17934 return type;
17935 }
17936
17937 /* If the type-specifier was for a built-in type, we're done. */
17938 if (type)
17939 {
17940 /* Record the type. */
17941 if (decl_specs
17942 && (token->keyword != RID_SIGNED
17943 && token->keyword != RID_UNSIGNED
17944 && token->keyword != RID_SHORT
17945 && token->keyword != RID_LONG))
17946 cp_parser_set_decl_spec_type (decl_specs,
17947 type,
17948 token,
17949 /*type_definition_p=*/false);
17950 if (decl_specs)
17951 decl_specs->any_specifiers_p = true;
17952
17953 /* Consume the token. */
17954 cp_lexer_consume_token (parser->lexer);
17955
17956 if (type == error_mark_node)
17957 return error_mark_node;
17958
17959 /* There is no valid C++ program where a non-template type is
17960 followed by a "<". That usually indicates that the user thought
17961 that the type was a template. */
17962 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17963 token->location);
17964
17965 return TYPE_NAME (type);
17966 }
17967
17968 /* The type-specifier must be a user-defined type. */
17969 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17970 {
17971 bool qualified_p;
17972 bool global_p;
17973 const bool typename_p = (cxx_dialect >= cxx2a
17974 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
17975
17976 /* Don't gobble tokens or issue error messages if this is an
17977 optional type-specifier. */
17978 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17979 cp_parser_parse_tentatively (parser);
17980
17981 /* Remember current tentative parsing state -- if we know we need
17982 a type, we can give better diagnostics here. */
17983 bool tent = cp_parser_parsing_tentatively (parser);
17984
17985 token = cp_lexer_peek_token (parser->lexer);
17986
17987 /* Look for the optional `::' operator. */
17988 global_p
17989 = (cp_parser_global_scope_opt (parser,
17990 /*current_scope_valid_p=*/false)
17991 != NULL_TREE);
17992 /* Look for the nested-name specifier. */
17993 qualified_p
17994 = (cp_parser_nested_name_specifier_opt (parser,
17995 /*typename_keyword_p=*/false,
17996 /*check_dependency_p=*/true,
17997 /*type_p=*/false,
17998 /*is_declaration=*/false)
17999 != NULL_TREE);
18000 /* If we have seen a nested-name-specifier, and the next token
18001 is `template', then we are using the template-id production. */
18002 if (parser->scope
18003 && cp_parser_optional_template_keyword (parser))
18004 {
18005 /* Look for the template-id. */
18006 type = cp_parser_template_id (parser,
18007 /*template_keyword_p=*/true,
18008 /*check_dependency_p=*/true,
18009 none_type,
18010 /*is_declaration=*/false);
18011 /* If the template-id did not name a type, we are out of
18012 luck. */
18013 if (TREE_CODE (type) != TYPE_DECL)
18014 {
18015 /* ...unless we pretend we have seen 'typename'. */
18016 if (typename_p)
18017 type = cp_parser_make_typename_type (parser, type,
18018 token->location);
18019 else
18020 {
18021 cp_parser_error (parser, "expected template-id for type");
18022 type = error_mark_node;
18023 }
18024 }
18025 }
18026
18027 /* Otherwise, look for a type-name. */
18028 if (!type)
18029 {
18030 if (cxx_dialect >= cxx17)
18031 cp_parser_parse_tentatively (parser);
18032
18033 type = cp_parser_type_name (parser, (qualified_p && typename_p));
18034
18035 if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
18036 type = NULL_TREE;
18037 }
18038
18039 if (!type && flag_concepts && decl_specs)
18040 {
18041 /* Try for a type-constraint with template arguments. We check
18042 decl_specs here to avoid trying this for a functional cast. */
18043
18044 cp_parser_parse_tentatively (parser);
18045
18046 type = cp_parser_template_id (parser,
18047 /*template_keyword_p=*/false,
18048 /*check_dependency_p=*/true,
18049 none_type,
18050 /*is_declaration=*/false);
18051 if (type && concept_check_p (type))
18052 {
18053 location_t loc = EXPR_LOCATION (type);
18054 type = cp_parser_placeholder_type_specifier (parser, loc,
18055 type, tent);
18056 if (tent && type == error_mark_node)
18057 /* Perhaps it's a concept-check expression. */
18058 cp_parser_simulate_error (parser);
18059 }
18060 else
18061 cp_parser_simulate_error (parser);
18062
18063 if (!cp_parser_parse_definitely (parser))
18064 type = NULL_TREE;
18065 }
18066
18067 if (!type && cxx_dialect >= cxx17)
18068 {
18069 /* Try class template argument deduction or type-constraint without
18070 template arguments. */
18071 tree name = cp_parser_identifier (parser);
18072 if (name && TREE_CODE (name) == IDENTIFIER_NODE
18073 && parser->scope != error_mark_node)
18074 {
18075 location_t loc
18076 = cp_lexer_previous_token (parser->lexer)->location;
18077 tree tmpl = cp_parser_lookup_name (parser, name,
18078 none_type,
18079 /*is_template=*/false,
18080 /*is_namespace=*/false,
18081 /*check_dependency=*/true,
18082 /*ambiguous_decls=*/NULL,
18083 token->location);
18084 if (tmpl && tmpl != error_mark_node
18085 && ctad_template_p (tmpl))
18086 type = make_template_placeholder (tmpl);
18087 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
18088 type = cp_parser_placeholder_type_specifier (parser, loc,
18089 tmpl, tent);
18090 else
18091 {
18092 type = error_mark_node;
18093 if (!cp_parser_simulate_error (parser))
18094 cp_parser_name_lookup_error (parser, name, tmpl,
18095 NLE_TYPE, token->location);
18096 }
18097 }
18098 else
18099 type = error_mark_node;
18100 }
18101
18102 /* If it didn't work out, we don't have a TYPE. */
18103 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
18104 && !cp_parser_parse_definitely (parser))
18105 type = NULL_TREE;
18106
18107 /* Keep track of all name-lookups performed in class scopes. */
18108 if (type
18109 && !global_p
18110 && !qualified_p
18111 && TREE_CODE (type) == TYPE_DECL
18112 && identifier_p (DECL_NAME (type)))
18113 maybe_note_name_used_in_class (DECL_NAME (type), type);
18114
18115 if (type && decl_specs)
18116 cp_parser_set_decl_spec_type (decl_specs, type,
18117 token,
18118 /*type_definition_p=*/false);
18119 }
18120
18121 /* If we didn't get a type-name, issue an error message. */
18122 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
18123 {
18124 cp_parser_error (parser, "expected type-name");
18125 return error_mark_node;
18126 }
18127
18128 if (type && type != error_mark_node)
18129 {
18130 /* See if TYPE is an Objective-C type, and if so, parse and
18131 accept any protocol references following it. Do this before
18132 the cp_parser_check_for_invalid_template_id() call, because
18133 Objective-C types can be followed by '<...>' which would
18134 enclose protocol names rather than template arguments, and so
18135 everything is fine. */
18136 if (c_dialect_objc () && !parser->scope
18137 && (objc_is_id (type) || objc_is_class_name (type)))
18138 {
18139 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18140 tree qual_type = objc_get_protocol_qualified_type (type, protos);
18141
18142 /* Clobber the "unqualified" type previously entered into
18143 DECL_SPECS with the new, improved protocol-qualified version. */
18144 if (decl_specs)
18145 decl_specs->type = qual_type;
18146
18147 return qual_type;
18148 }
18149
18150 /* There is no valid C++ program where a non-template type is
18151 followed by a "<". That usually indicates that the user
18152 thought that the type was a template. */
18153 cp_parser_check_for_invalid_template_id (parser, type,
18154 none_type,
18155 token->location);
18156 }
18157
18158 return type;
18159 }
18160
18161 /* Parse the remainder of a placholder-type-specifier.
18162
18163 placeholder-type-specifier:
18164 type-constraint_opt auto
18165 type-constraint_opt decltype(auto)
18166
18167 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
18168 and passed as TMPL. This function converts TMPL to an actual type-constraint,
18169 parses the placeholder type, and performs some contextual syntactic analysis.
18170
18171 LOC provides the location of the template name.
18172
18173 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
18174 don't give an error if TMPL isn't a valid type-constraint, as the template-id
18175 might actually be a concept-check,
18176
18177 Note that the Concepts TS allows the auto or decltype(auto) to be
18178 omitted in a constrained-type-specifier. */
18179
18180 tree
18181 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
18182 tree tmpl, bool tentative)
18183 {
18184 if (tmpl == error_mark_node)
18185 return error_mark_node;
18186
18187 tree orig_tmpl = tmpl;
18188
18189 /* Get the arguments as written for subsequent analysis. */
18190 tree args = NULL_TREE;
18191 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
18192 {
18193 args = TREE_OPERAND (tmpl, 1);
18194 tmpl = TREE_OPERAND (tmpl, 0);
18195 }
18196 if (args == NULL_TREE)
18197 /* A concept-name with no arguments can't be an expression. */
18198 tentative = false;
18199
18200 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
18201
18202 /* Get the concept and prototype parameter for the constraint. */
18203 tree_pair info = finish_type_constraints (tmpl, args, complain);
18204 tree con = info.first;
18205 tree proto = info.second;
18206 if (con == error_mark_node)
18207 return error_mark_node;
18208
18209 /* As per the standard, require auto or decltype(auto), except in some
18210 cases (template parameter lists, -fconcepts-ts enabled). */
18211 cp_token *placeholder = NULL, *open_paren = NULL, *close_paren = NULL;
18212 if (cxx_dialect >= cxx2a)
18213 {
18214 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
18215 placeholder = cp_lexer_consume_token (parser->lexer);
18216 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
18217 {
18218 placeholder = cp_lexer_consume_token (parser->lexer);
18219 open_paren = cp_parser_require (parser, CPP_OPEN_PAREN,
18220 RT_OPEN_PAREN);
18221 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
18222 close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
18223 RT_CLOSE_PAREN,
18224 open_paren->location);
18225 }
18226 }
18227
18228 /* A type constraint constrains a contextually determined type or type
18229 parameter pack. However, the the Concepts TS does allow concepts
18230 to introduce non-type and template template parameters. */
18231 if (TREE_CODE (proto) != TYPE_DECL)
18232 {
18233 if (!flag_concepts_ts
18234 || !processing_template_parmlist)
18235 {
18236 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
18237 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
18238 return error_mark_node;
18239 }
18240 }
18241
18242 /* In a template parameter list, a type-parameter can be introduced
18243 by type-constraints alone. */
18244 if (processing_template_parmlist && !placeholder)
18245 return build_constrained_parameter (con, proto, args);
18246
18247 /* Diagnose issues placeholder issues. */
18248 if (!flag_concepts_ts
18249 && !parser->in_result_type_constraint_p
18250 && !placeholder)
18251 {
18252 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
18253 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
18254 error_at (input_location,
18255 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
18256 /* Fall through. This is an error of omission. */
18257 }
18258 else if (parser->in_result_type_constraint_p && placeholder)
18259 {
18260 /* A trailing return type only allows type-constraints. */
18261 error_at (input_location,
18262 "unexpected placeholder in constrained result type");
18263 }
18264
18265 /* In a parameter-declaration-clause, a placeholder-type-specifier
18266 results in an invented template parameter. */
18267 if (parser->auto_is_implicit_function_template_parm_p)
18268 {
18269 if (placeholder && token_is_decltype (placeholder))
18270 {
18271 location_t loc = make_location (placeholder->location,
18272 placeholder->location,
18273 close_paren->location);
18274 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
18275 return error_mark_node;
18276 }
18277 tree parm = build_constrained_parameter (con, proto, args);
18278 return synthesize_implicit_template_parm (parser, parm);
18279 }
18280
18281 /* Determine if the type should be deduced using template argument
18282 deduction or decltype deduction. Note that the latter is always
18283 used for type-constraints in trailing return types. */
18284 bool decltype_p = placeholder
18285 ? placeholder->keyword == RID_DECLTYPE
18286 : parser->in_result_type_constraint_p;
18287
18288 /* Otherwise, this is the type of a variable or return type. */
18289 if (decltype_p)
18290 return make_constrained_decltype_auto (con, args);
18291 else
18292 return make_constrained_auto (con, args);
18293 }
18294
18295 /* Parse a type-name.
18296
18297 type-name:
18298 class-name
18299 enum-name
18300 typedef-name
18301 simple-template-id [in c++0x]
18302
18303 enum-name:
18304 identifier
18305
18306 typedef-name:
18307 identifier
18308
18309 Concepts:
18310
18311 type-name:
18312 concept-name
18313 partial-concept-id
18314
18315 concept-name:
18316 identifier
18317
18318 Returns a TYPE_DECL for the type. */
18319
18320 static tree
18321 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
18322 {
18323 tree type_decl;
18324
18325 /* We can't know yet whether it is a class-name or not. */
18326 cp_parser_parse_tentatively (parser);
18327 /* Try a class-name. */
18328 type_decl = cp_parser_class_name (parser,
18329 typename_keyword_p,
18330 /*template_keyword_p=*/false,
18331 none_type,
18332 /*check_dependency_p=*/true,
18333 /*class_head_p=*/false,
18334 /*is_declaration=*/false);
18335 /* If it's not a class-name, keep looking. */
18336 if (!cp_parser_parse_definitely (parser))
18337 {
18338 if (cxx_dialect < cxx11)
18339 /* It must be a typedef-name or an enum-name. */
18340 return cp_parser_nonclass_name (parser);
18341
18342 cp_parser_parse_tentatively (parser);
18343 /* It is either a simple-template-id representing an
18344 instantiation of an alias template... */
18345 type_decl = cp_parser_template_id (parser,
18346 /*template_keyword_p=*/false,
18347 /*check_dependency_p=*/true,
18348 none_type,
18349 /*is_declaration=*/false);
18350 /* Note that this must be an instantiation of an alias template
18351 because [temp.names]/6 says:
18352
18353 A template-id that names an alias template specialization
18354 is a type-name.
18355
18356 Whereas [temp.names]/7 says:
18357
18358 A simple-template-id that names a class template
18359 specialization is a class-name.
18360
18361 With concepts, this could also be a partial-concept-id that
18362 declares a non-type template parameter. */
18363 if (type_decl != NULL_TREE
18364 && TREE_CODE (type_decl) == TYPE_DECL
18365 && TYPE_DECL_ALIAS_P (type_decl))
18366 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
18367 else
18368 cp_parser_simulate_error (parser);
18369
18370 if (!cp_parser_parse_definitely (parser))
18371 /* ... Or a typedef-name or an enum-name. */
18372 return cp_parser_nonclass_name (parser);
18373 }
18374
18375 return type_decl;
18376 }
18377
18378 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18379 or a concept-name.
18380
18381 enum-name:
18382 identifier
18383
18384 typedef-name:
18385 identifier
18386
18387 concept-name:
18388 identifier
18389
18390 Returns a TYPE_DECL for the type. */
18391
18392 static tree
18393 cp_parser_nonclass_name (cp_parser* parser)
18394 {
18395 tree type_decl;
18396 tree identifier;
18397
18398 cp_token *token = cp_lexer_peek_token (parser->lexer);
18399 identifier = cp_parser_identifier (parser);
18400 if (identifier == error_mark_node)
18401 return error_mark_node;
18402
18403 /* Look up the type-name. */
18404 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18405
18406 type_decl = strip_using_decl (type_decl);
18407
18408 if (TREE_CODE (type_decl) != TYPE_DECL
18409 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18410 {
18411 /* See if this is an Objective-C type. */
18412 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18413 tree type = objc_get_protocol_qualified_type (identifier, protos);
18414 if (type)
18415 type_decl = TYPE_NAME (type);
18416 }
18417
18418 /* Issue an error if we did not find a type-name. */
18419 if (TREE_CODE (type_decl) != TYPE_DECL
18420 /* In Objective-C, we have the complication that class names are
18421 normally type names and start declarations (eg, the
18422 "NSObject" in "NSObject *object;"), but can be used in an
18423 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18424 is an expression. So, a classname followed by a dot is not a
18425 valid type-name. */
18426 || (objc_is_class_name (TREE_TYPE (type_decl))
18427 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18428 {
18429 if (!cp_parser_simulate_error (parser))
18430 cp_parser_name_lookup_error (parser, identifier, type_decl,
18431 NLE_TYPE, token->location);
18432 return error_mark_node;
18433 }
18434 /* Remember that the name was used in the definition of the
18435 current class so that we can check later to see if the
18436 meaning would have been different after the class was
18437 entirely defined. */
18438 else if (type_decl != error_mark_node
18439 && !parser->scope)
18440 maybe_note_name_used_in_class (identifier, type_decl);
18441
18442 return type_decl;
18443 }
18444
18445 /* Parse an elaborated-type-specifier. Note that the grammar given
18446 here incorporates the resolution to DR68.
18447
18448 elaborated-type-specifier:
18449 class-key :: [opt] nested-name-specifier [opt] identifier
18450 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18451 enum-key :: [opt] nested-name-specifier [opt] identifier
18452 typename :: [opt] nested-name-specifier identifier
18453 typename :: [opt] nested-name-specifier template [opt]
18454 template-id
18455
18456 GNU extension:
18457
18458 elaborated-type-specifier:
18459 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18460 class-key attributes :: [opt] nested-name-specifier [opt]
18461 template [opt] template-id
18462 enum attributes :: [opt] nested-name-specifier [opt] identifier
18463
18464 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18465 declared `friend'. If IS_DECLARATION is TRUE, then this
18466 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18467 something is being declared.
18468
18469 Returns the TYPE specified. */
18470
18471 static tree
18472 cp_parser_elaborated_type_specifier (cp_parser* parser,
18473 bool is_friend,
18474 bool is_declaration)
18475 {
18476 enum tag_types tag_type;
18477 tree identifier;
18478 tree type = NULL_TREE;
18479 tree attributes = NULL_TREE;
18480 tree globalscope;
18481 cp_token *token = NULL;
18482
18483 /* See if we're looking at the `enum' keyword. */
18484 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18485 {
18486 /* Consume the `enum' token. */
18487 cp_lexer_consume_token (parser->lexer);
18488 /* Remember that it's an enumeration type. */
18489 tag_type = enum_type;
18490 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18491 enums) is used here. */
18492 cp_token *token = cp_lexer_peek_token (parser->lexer);
18493 if (cp_parser_is_keyword (token, RID_CLASS)
18494 || cp_parser_is_keyword (token, RID_STRUCT))
18495 {
18496 gcc_rich_location richloc (token->location);
18497 richloc.add_range (input_location);
18498 richloc.add_fixit_remove ();
18499 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18500 "a scoped enum must not use the %qD keyword",
18501 token->u.value);
18502 /* Consume the `struct' or `class' and parse it anyway. */
18503 cp_lexer_consume_token (parser->lexer);
18504 }
18505 /* Parse the attributes. */
18506 attributes = cp_parser_attributes_opt (parser);
18507 }
18508 /* Or, it might be `typename'. */
18509 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18510 RID_TYPENAME))
18511 {
18512 /* Consume the `typename' token. */
18513 cp_lexer_consume_token (parser->lexer);
18514 /* Remember that it's a `typename' type. */
18515 tag_type = typename_type;
18516 }
18517 /* Otherwise it must be a class-key. */
18518 else
18519 {
18520 tag_type = cp_parser_class_key (parser);
18521 if (tag_type == none_type)
18522 return error_mark_node;
18523 /* Parse the attributes. */
18524 attributes = cp_parser_attributes_opt (parser);
18525 }
18526
18527 /* Look for the `::' operator. */
18528 globalscope = cp_parser_global_scope_opt (parser,
18529 /*current_scope_valid_p=*/false);
18530 /* Look for the nested-name-specifier. */
18531 tree nested_name_specifier;
18532 if (tag_type == typename_type && !globalscope)
18533 {
18534 nested_name_specifier
18535 = cp_parser_nested_name_specifier (parser,
18536 /*typename_keyword_p=*/true,
18537 /*check_dependency_p=*/true,
18538 /*type_p=*/true,
18539 is_declaration);
18540 if (!nested_name_specifier)
18541 return error_mark_node;
18542 }
18543 else
18544 /* Even though `typename' is not present, the proposed resolution
18545 to Core Issue 180 says that in `class A<T>::B', `B' should be
18546 considered a type-name, even if `A<T>' is dependent. */
18547 nested_name_specifier
18548 = cp_parser_nested_name_specifier_opt (parser,
18549 /*typename_keyword_p=*/true,
18550 /*check_dependency_p=*/true,
18551 /*type_p=*/true,
18552 is_declaration);
18553 /* For everything but enumeration types, consider a template-id.
18554 For an enumeration type, consider only a plain identifier. */
18555 if (tag_type != enum_type)
18556 {
18557 bool template_p = false;
18558 tree decl;
18559
18560 /* Allow the `template' keyword. */
18561 template_p = cp_parser_optional_template_keyword (parser);
18562 /* If we didn't see `template', we don't know if there's a
18563 template-id or not. */
18564 if (!template_p)
18565 cp_parser_parse_tentatively (parser);
18566 /* The `template' keyword must follow a nested-name-specifier. */
18567 else if (!nested_name_specifier)
18568 {
18569 cp_parser_error (parser, "%<template%> must follow a nested-"
18570 "name-specifier");
18571 return error_mark_node;
18572 }
18573
18574 /* Parse the template-id. */
18575 token = cp_lexer_peek_token (parser->lexer);
18576 decl = cp_parser_template_id (parser, template_p,
18577 /*check_dependency_p=*/true,
18578 tag_type,
18579 is_declaration);
18580 /* If we didn't find a template-id, look for an ordinary
18581 identifier. */
18582 if (!template_p && !cp_parser_parse_definitely (parser))
18583 ;
18584 /* We can get here when cp_parser_template_id, called by
18585 cp_parser_class_name with tag_type == none_type, succeeds
18586 and caches a BASELINK. Then, when called again here,
18587 instead of failing and returning an error_mark_node
18588 returns it (see template/typename17.C in C++11).
18589 ??? Could we diagnose this earlier? */
18590 else if (tag_type == typename_type && BASELINK_P (decl))
18591 {
18592 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18593 type = error_mark_node;
18594 }
18595 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18596 in effect, then we must assume that, upon instantiation, the
18597 template will correspond to a class. */
18598 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18599 && tag_type == typename_type)
18600 type = make_typename_type (parser->scope, decl,
18601 typename_type,
18602 /*complain=*/tf_error);
18603 /* If the `typename' keyword is in effect and DECL is not a type
18604 decl, then type is non existent. */
18605 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18606 ;
18607 else if (TREE_CODE (decl) == TYPE_DECL)
18608 {
18609 type = check_elaborated_type_specifier (tag_type, decl,
18610 /*allow_template_p=*/true);
18611
18612 /* If the next token is a semicolon, this must be a specialization,
18613 instantiation, or friend declaration. Check the scope while we
18614 still know whether or not we had a nested-name-specifier. */
18615 if (type != error_mark_node
18616 && !nested_name_specifier && !is_friend
18617 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18618 check_unqualified_spec_or_inst (type, token->location);
18619 }
18620 else if (decl == error_mark_node)
18621 type = error_mark_node;
18622 }
18623
18624 if (!type)
18625 {
18626 token = cp_lexer_peek_token (parser->lexer);
18627 identifier = cp_parser_identifier (parser);
18628
18629 if (identifier == error_mark_node)
18630 {
18631 parser->scope = NULL_TREE;
18632 return error_mark_node;
18633 }
18634
18635 /* For a `typename', we needn't call xref_tag. */
18636 if (tag_type == typename_type
18637 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18638 return cp_parser_make_typename_type (parser, identifier,
18639 token->location);
18640
18641 /* Template parameter lists apply only if we are not within a
18642 function parameter list. */
18643 bool template_parm_lists_apply
18644 = parser->num_template_parameter_lists;
18645 if (template_parm_lists_apply)
18646 for (cp_binding_level *s = current_binding_level;
18647 s && s->kind != sk_template_parms;
18648 s = s->level_chain)
18649 if (s->kind == sk_function_parms)
18650 template_parm_lists_apply = false;
18651
18652 /* Look up a qualified name in the usual way. */
18653 if (parser->scope)
18654 {
18655 tree decl;
18656 tree ambiguous_decls;
18657
18658 decl = cp_parser_lookup_name (parser, identifier,
18659 tag_type,
18660 /*is_template=*/false,
18661 /*is_namespace=*/false,
18662 /*check_dependency=*/true,
18663 &ambiguous_decls,
18664 token->location);
18665
18666 /* If the lookup was ambiguous, an error will already have been
18667 issued. */
18668 if (ambiguous_decls)
18669 return error_mark_node;
18670
18671 /* If we are parsing friend declaration, DECL may be a
18672 TEMPLATE_DECL tree node here. However, we need to check
18673 whether this TEMPLATE_DECL results in valid code. Consider
18674 the following example:
18675
18676 namespace N {
18677 template <class T> class C {};
18678 }
18679 class X {
18680 template <class T> friend class N::C; // #1, valid code
18681 };
18682 template <class T> class Y {
18683 friend class N::C; // #2, invalid code
18684 };
18685
18686 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18687 name lookup of `N::C'. We see that friend declaration must
18688 be template for the code to be valid. Note that
18689 processing_template_decl does not work here since it is
18690 always 1 for the above two cases. */
18691
18692 decl = (cp_parser_maybe_treat_template_as_class
18693 (decl, /*tag_name_p=*/is_friend
18694 && template_parm_lists_apply));
18695
18696 if (TREE_CODE (decl) != TYPE_DECL)
18697 {
18698 cp_parser_diagnose_invalid_type_name (parser,
18699 identifier,
18700 token->location);
18701 return error_mark_node;
18702 }
18703
18704 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18705 {
18706 bool allow_template = (template_parm_lists_apply
18707 || DECL_SELF_REFERENCE_P (decl));
18708 type = check_elaborated_type_specifier (tag_type, decl,
18709 allow_template);
18710
18711 if (type == error_mark_node)
18712 return error_mark_node;
18713 }
18714
18715 /* Forward declarations of nested types, such as
18716
18717 class C1::C2;
18718 class C1::C2::C3;
18719
18720 are invalid unless all components preceding the final '::'
18721 are complete. If all enclosing types are complete, these
18722 declarations become merely pointless.
18723
18724 Invalid forward declarations of nested types are errors
18725 caught elsewhere in parsing. Those that are pointless arrive
18726 here. */
18727
18728 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18729 && !is_friend && !processing_explicit_instantiation)
18730 warning (0, "declaration %qD does not declare anything", decl);
18731
18732 type = TREE_TYPE (decl);
18733 }
18734 else
18735 {
18736 /* An elaborated-type-specifier sometimes introduces a new type and
18737 sometimes names an existing type. Normally, the rule is that it
18738 introduces a new type only if there is not an existing type of
18739 the same name already in scope. For example, given:
18740
18741 struct S {};
18742 void f() { struct S s; }
18743
18744 the `struct S' in the body of `f' is the same `struct S' as in
18745 the global scope; the existing definition is used. However, if
18746 there were no global declaration, this would introduce a new
18747 local class named `S'.
18748
18749 An exception to this rule applies to the following code:
18750
18751 namespace N { struct S; }
18752
18753 Here, the elaborated-type-specifier names a new type
18754 unconditionally; even if there is already an `S' in the
18755 containing scope this declaration names a new type.
18756 This exception only applies if the elaborated-type-specifier
18757 forms the complete declaration:
18758
18759 [class.name]
18760
18761 A declaration consisting solely of `class-key identifier ;' is
18762 either a redeclaration of the name in the current scope or a
18763 forward declaration of the identifier as a class name. It
18764 introduces the name into the current scope.
18765
18766 We are in this situation precisely when the next token is a `;'.
18767
18768 An exception to the exception is that a `friend' declaration does
18769 *not* name a new type; i.e., given:
18770
18771 struct S { friend struct T; };
18772
18773 `T' is not a new type in the scope of `S'.
18774
18775 Also, `new struct S' or `sizeof (struct S)' never results in the
18776 definition of a new type; a new type can only be declared in a
18777 declaration context. */
18778
18779 tag_scope ts;
18780 bool template_p;
18781
18782 if (is_friend)
18783 /* Friends have special name lookup rules. */
18784 ts = ts_within_enclosing_non_class;
18785 else if (is_declaration
18786 && cp_lexer_next_token_is (parser->lexer,
18787 CPP_SEMICOLON))
18788 /* This is a `class-key identifier ;' */
18789 ts = ts_current;
18790 else
18791 ts = ts_global;
18792
18793 template_p =
18794 (template_parm_lists_apply
18795 && (cp_parser_next_token_starts_class_definition_p (parser)
18796 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18797 /* An unqualified name was used to reference this type, so
18798 there were no qualifying templates. */
18799 if (template_parm_lists_apply
18800 && !cp_parser_check_template_parameters (parser,
18801 /*num_templates=*/0,
18802 /*template_id*/false,
18803 token->location,
18804 /*declarator=*/NULL))
18805 return error_mark_node;
18806 type = xref_tag (tag_type, identifier, ts, template_p);
18807 }
18808 }
18809
18810 if (type == error_mark_node)
18811 return error_mark_node;
18812
18813 /* Allow attributes on forward declarations of classes. */
18814 if (attributes)
18815 {
18816 if (TREE_CODE (type) == TYPENAME_TYPE)
18817 warning (OPT_Wattributes,
18818 "attributes ignored on uninstantiated type");
18819 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18820 && ! processing_explicit_instantiation)
18821 warning (OPT_Wattributes,
18822 "attributes ignored on template instantiation");
18823 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18824 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18825 else
18826 warning (OPT_Wattributes,
18827 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18828 }
18829
18830 if (tag_type != enum_type)
18831 {
18832 /* Indicate whether this class was declared as a `class' or as a
18833 `struct'. */
18834 if (CLASS_TYPE_P (type))
18835 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18836 cp_parser_check_class_key (tag_type, type);
18837 }
18838
18839 /* A "<" cannot follow an elaborated type specifier. If that
18840 happens, the user was probably trying to form a template-id. */
18841 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18842 token->location);
18843
18844 return type;
18845 }
18846
18847 /* Parse an enum-specifier.
18848
18849 enum-specifier:
18850 enum-head { enumerator-list [opt] }
18851 enum-head { enumerator-list , } [C++0x]
18852
18853 enum-head:
18854 enum-key identifier [opt] enum-base [opt]
18855 enum-key nested-name-specifier identifier enum-base [opt]
18856
18857 enum-key:
18858 enum
18859 enum class [C++0x]
18860 enum struct [C++0x]
18861
18862 enum-base: [C++0x]
18863 : type-specifier-seq
18864
18865 opaque-enum-specifier:
18866 enum-key identifier enum-base [opt] ;
18867
18868 GNU Extensions:
18869 enum-key attributes[opt] identifier [opt] enum-base [opt]
18870 { enumerator-list [opt] }attributes[opt]
18871 enum-key attributes[opt] identifier [opt] enum-base [opt]
18872 { enumerator-list, }attributes[opt] [C++0x]
18873
18874 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18875 if the token stream isn't an enum-specifier after all. */
18876
18877 static tree
18878 cp_parser_enum_specifier (cp_parser* parser)
18879 {
18880 tree identifier;
18881 tree type = NULL_TREE;
18882 tree prev_scope;
18883 tree nested_name_specifier = NULL_TREE;
18884 tree attributes;
18885 bool scoped_enum_p = false;
18886 bool has_underlying_type = false;
18887 bool nested_being_defined = false;
18888 bool new_value_list = false;
18889 bool is_new_type = false;
18890 bool is_unnamed = false;
18891 tree underlying_type = NULL_TREE;
18892 cp_token *type_start_token = NULL;
18893 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18894
18895 parser->colon_corrects_to_scope_p = false;
18896
18897 /* Parse tentatively so that we can back up if we don't find a
18898 enum-specifier. */
18899 cp_parser_parse_tentatively (parser);
18900
18901 /* Caller guarantees that the current token is 'enum', an identifier
18902 possibly follows, and the token after that is an opening brace.
18903 If we don't have an identifier, fabricate an anonymous name for
18904 the enumeration being defined. */
18905 cp_lexer_consume_token (parser->lexer);
18906
18907 /* Parse the "class" or "struct", which indicates a scoped
18908 enumeration type in C++0x. */
18909 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18910 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18911 {
18912 if (cxx_dialect < cxx11)
18913 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18914
18915 /* Consume the `struct' or `class' token. */
18916 cp_lexer_consume_token (parser->lexer);
18917
18918 scoped_enum_p = true;
18919 }
18920
18921 attributes = cp_parser_attributes_opt (parser);
18922
18923 /* Clear the qualification. */
18924 parser->scope = NULL_TREE;
18925 parser->qualifying_scope = NULL_TREE;
18926 parser->object_scope = NULL_TREE;
18927
18928 /* Figure out in what scope the declaration is being placed. */
18929 prev_scope = current_scope ();
18930
18931 type_start_token = cp_lexer_peek_token (parser->lexer);
18932
18933 push_deferring_access_checks (dk_no_check);
18934 nested_name_specifier
18935 = cp_parser_nested_name_specifier_opt (parser,
18936 /*typename_keyword_p=*/true,
18937 /*check_dependency_p=*/false,
18938 /*type_p=*/false,
18939 /*is_declaration=*/false);
18940
18941 if (nested_name_specifier)
18942 {
18943 tree name;
18944
18945 identifier = cp_parser_identifier (parser);
18946 name = cp_parser_lookup_name (parser, identifier,
18947 enum_type,
18948 /*is_template=*/false,
18949 /*is_namespace=*/false,
18950 /*check_dependency=*/true,
18951 /*ambiguous_decls=*/NULL,
18952 input_location);
18953 if (name && name != error_mark_node)
18954 {
18955 type = TREE_TYPE (name);
18956 if (TREE_CODE (type) == TYPENAME_TYPE)
18957 {
18958 /* Are template enums allowed in ISO? */
18959 if (template_parm_scope_p ())
18960 pedwarn (type_start_token->location, OPT_Wpedantic,
18961 "%qD is an enumeration template", name);
18962 /* ignore a typename reference, for it will be solved by name
18963 in start_enum. */
18964 type = NULL_TREE;
18965 }
18966 }
18967 else if (nested_name_specifier == error_mark_node)
18968 /* We already issued an error. */;
18969 else
18970 {
18971 error_at (type_start_token->location,
18972 "%qD does not name an enumeration in %qT",
18973 identifier, nested_name_specifier);
18974 nested_name_specifier = error_mark_node;
18975 }
18976 }
18977 else
18978 {
18979 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18980 identifier = cp_parser_identifier (parser);
18981 else
18982 {
18983 identifier = make_anon_name ();
18984 is_unnamed = true;
18985 if (scoped_enum_p)
18986 error_at (type_start_token->location,
18987 "unnamed scoped enum is not allowed");
18988 }
18989 }
18990 pop_deferring_access_checks ();
18991
18992 /* Check for the `:' that denotes a specified underlying type in C++0x.
18993 Note that a ':' could also indicate a bitfield width, however. */
18994 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18995 {
18996 cp_decl_specifier_seq type_specifiers;
18997
18998 /* Consume the `:'. */
18999 cp_lexer_consume_token (parser->lexer);
19000
19001 /* Parse the type-specifier-seq. */
19002 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
19003 /*is_declaration=*/false,
19004 /*is_trailing_return=*/false,
19005 &type_specifiers);
19006
19007 /* At this point this is surely not elaborated type specifier. */
19008 if (!cp_parser_parse_definitely (parser))
19009 return NULL_TREE;
19010
19011 if (cxx_dialect < cxx11)
19012 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
19013
19014 has_underlying_type = true;
19015
19016 /* If that didn't work, stop. */
19017 if (type_specifiers.type != error_mark_node)
19018 {
19019 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
19020 /*initialized=*/0, NULL);
19021 if (underlying_type == error_mark_node
19022 || check_for_bare_parameter_packs (underlying_type))
19023 underlying_type = NULL_TREE;
19024 }
19025 }
19026
19027 /* Look for the `{' but don't consume it yet. */
19028 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19029 {
19030 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
19031 {
19032 cp_parser_error (parser, "expected %<{%>");
19033 if (has_underlying_type)
19034 {
19035 type = NULL_TREE;
19036 goto out;
19037 }
19038 }
19039 /* An opaque-enum-specifier must have a ';' here. */
19040 if ((scoped_enum_p || underlying_type)
19041 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19042 {
19043 cp_parser_error (parser, "expected %<;%> or %<{%>");
19044 if (has_underlying_type)
19045 {
19046 type = NULL_TREE;
19047 goto out;
19048 }
19049 }
19050 }
19051
19052 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
19053 return NULL_TREE;
19054
19055 if (nested_name_specifier)
19056 {
19057 if (CLASS_TYPE_P (nested_name_specifier))
19058 {
19059 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
19060 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
19061 push_scope (nested_name_specifier);
19062 }
19063 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19064 {
19065 push_nested_namespace (nested_name_specifier);
19066 }
19067 }
19068
19069 /* Issue an error message if type-definitions are forbidden here. */
19070 if (!cp_parser_check_type_definition (parser))
19071 type = error_mark_node;
19072 else
19073 /* Create the new type. We do this before consuming the opening
19074 brace so the enum will be recorded as being on the line of its
19075 tag (or the 'enum' keyword, if there is no tag). */
19076 type = start_enum (identifier, type, underlying_type,
19077 attributes, scoped_enum_p, &is_new_type);
19078
19079 /* If the next token is not '{' it is an opaque-enum-specifier or an
19080 elaborated-type-specifier. */
19081 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19082 {
19083 timevar_push (TV_PARSE_ENUM);
19084 if (nested_name_specifier
19085 && nested_name_specifier != error_mark_node)
19086 {
19087 /* The following catches invalid code such as:
19088 enum class S<int>::E { A, B, C }; */
19089 if (!processing_specialization
19090 && CLASS_TYPE_P (nested_name_specifier)
19091 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
19092 error_at (type_start_token->location, "cannot add an enumerator "
19093 "list to a template instantiation");
19094
19095 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
19096 {
19097 error_at (type_start_token->location,
19098 "%<%T::%E%> has not been declared",
19099 TYPE_CONTEXT (nested_name_specifier),
19100 nested_name_specifier);
19101 type = error_mark_node;
19102 }
19103 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
19104 && !CLASS_TYPE_P (nested_name_specifier))
19105 {
19106 error_at (type_start_token->location, "nested name specifier "
19107 "%qT for enum declaration does not name a class "
19108 "or namespace", nested_name_specifier);
19109 type = error_mark_node;
19110 }
19111 /* If that scope does not contain the scope in which the
19112 class was originally declared, the program is invalid. */
19113 else if (prev_scope && !is_ancestor (prev_scope,
19114 nested_name_specifier))
19115 {
19116 if (at_namespace_scope_p ())
19117 error_at (type_start_token->location,
19118 "declaration of %qD in namespace %qD which does not "
19119 "enclose %qD",
19120 type, prev_scope, nested_name_specifier);
19121 else
19122 error_at (type_start_token->location,
19123 "declaration of %qD in %qD which does not "
19124 "enclose %qD",
19125 type, prev_scope, nested_name_specifier);
19126 type = error_mark_node;
19127 }
19128 /* If that scope is the scope where the declaration is being placed
19129 the program is invalid. */
19130 else if (CLASS_TYPE_P (nested_name_specifier)
19131 && CLASS_TYPE_P (prev_scope)
19132 && same_type_p (nested_name_specifier, prev_scope))
19133 {
19134 permerror (type_start_token->location,
19135 "extra qualification not allowed");
19136 nested_name_specifier = NULL_TREE;
19137 }
19138 }
19139
19140 if (scoped_enum_p)
19141 begin_scope (sk_scoped_enum, type);
19142
19143 /* Consume the opening brace. */
19144 matching_braces braces;
19145 braces.consume_open (parser);
19146
19147 if (type == error_mark_node)
19148 ; /* Nothing to add */
19149 else if (OPAQUE_ENUM_P (type)
19150 || (cxx_dialect > cxx98 && processing_specialization))
19151 {
19152 new_value_list = true;
19153 SET_OPAQUE_ENUM_P (type, false);
19154 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
19155 }
19156 else
19157 {
19158 error_at (type_start_token->location,
19159 "multiple definition of %q#T", type);
19160 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
19161 "previous definition here");
19162 type = error_mark_node;
19163 }
19164
19165 if (type == error_mark_node)
19166 cp_parser_skip_to_end_of_block_or_statement (parser);
19167 /* If the next token is not '}', then there are some enumerators. */
19168 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19169 {
19170 if (is_unnamed && !scoped_enum_p)
19171 pedwarn (type_start_token->location, OPT_Wpedantic,
19172 "ISO C++ forbids empty unnamed enum");
19173 }
19174 else
19175 cp_parser_enumerator_list (parser, type);
19176
19177 /* Consume the final '}'. */
19178 braces.require_close (parser);
19179
19180 if (scoped_enum_p)
19181 finish_scope ();
19182 timevar_pop (TV_PARSE_ENUM);
19183 }
19184 else
19185 {
19186 /* If a ';' follows, then it is an opaque-enum-specifier
19187 and additional restrictions apply. */
19188 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19189 {
19190 if (is_unnamed)
19191 error_at (type_start_token->location,
19192 "opaque-enum-specifier without name");
19193 else if (nested_name_specifier)
19194 error_at (type_start_token->location,
19195 "opaque-enum-specifier must use a simple identifier");
19196 }
19197 }
19198
19199 /* Look for trailing attributes to apply to this enumeration, and
19200 apply them if appropriate. */
19201 if (cp_parser_allow_gnu_extensions_p (parser))
19202 {
19203 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
19204 cplus_decl_attributes (&type,
19205 trailing_attr,
19206 (int) ATTR_FLAG_TYPE_IN_PLACE);
19207 }
19208
19209 /* Finish up the enumeration. */
19210 if (type != error_mark_node)
19211 {
19212 if (new_value_list)
19213 finish_enum_value_list (type);
19214 if (is_new_type)
19215 finish_enum (type);
19216 }
19217
19218 if (nested_name_specifier)
19219 {
19220 if (CLASS_TYPE_P (nested_name_specifier))
19221 {
19222 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
19223 pop_scope (nested_name_specifier);
19224 }
19225 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19226 {
19227 pop_nested_namespace (nested_name_specifier);
19228 }
19229 }
19230 out:
19231 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19232 return type;
19233 }
19234
19235 /* Parse an enumerator-list. The enumerators all have the indicated
19236 TYPE.
19237
19238 enumerator-list:
19239 enumerator-definition
19240 enumerator-list , enumerator-definition */
19241
19242 static void
19243 cp_parser_enumerator_list (cp_parser* parser, tree type)
19244 {
19245 while (true)
19246 {
19247 /* Parse an enumerator-definition. */
19248 cp_parser_enumerator_definition (parser, type);
19249
19250 /* If the next token is not a ',', we've reached the end of
19251 the list. */
19252 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19253 break;
19254 /* Otherwise, consume the `,' and keep going. */
19255 cp_lexer_consume_token (parser->lexer);
19256 /* If the next token is a `}', there is a trailing comma. */
19257 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19258 {
19259 if (cxx_dialect < cxx11)
19260 pedwarn (input_location, OPT_Wpedantic,
19261 "comma at end of enumerator list");
19262 break;
19263 }
19264 }
19265 }
19266
19267 /* Parse an enumerator-definition. The enumerator has the indicated
19268 TYPE.
19269
19270 enumerator-definition:
19271 enumerator
19272 enumerator = constant-expression
19273
19274 enumerator:
19275 identifier
19276
19277 GNU Extensions:
19278
19279 enumerator-definition:
19280 enumerator attributes [opt]
19281 enumerator attributes [opt] = constant-expression */
19282
19283 static void
19284 cp_parser_enumerator_definition (cp_parser* parser, tree type)
19285 {
19286 tree identifier;
19287 tree value;
19288 location_t loc;
19289
19290 /* Save the input location because we are interested in the location
19291 of the identifier and not the location of the explicit value. */
19292 loc = cp_lexer_peek_token (parser->lexer)->location;
19293
19294 /* Look for the identifier. */
19295 identifier = cp_parser_identifier (parser);
19296 if (identifier == error_mark_node)
19297 return;
19298
19299 /* Parse any specified attributes. */
19300 tree attrs = cp_parser_attributes_opt (parser);
19301
19302 /* If the next token is an '=', then there is an explicit value. */
19303 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19304 {
19305 /* Consume the `=' token. */
19306 cp_lexer_consume_token (parser->lexer);
19307 /* Parse the value. */
19308 value = cp_parser_constant_expression (parser);
19309 }
19310 else
19311 value = NULL_TREE;
19312
19313 /* If we are processing a template, make sure the initializer of the
19314 enumerator doesn't contain any bare template parameter pack. */
19315 if (check_for_bare_parameter_packs (value))
19316 value = error_mark_node;
19317
19318 /* Create the enumerator. */
19319 build_enumerator (identifier, value, type, attrs, loc);
19320 }
19321
19322 /* Parse a namespace-name.
19323
19324 namespace-name:
19325 original-namespace-name
19326 namespace-alias
19327
19328 Returns the NAMESPACE_DECL for the namespace. */
19329
19330 static tree
19331 cp_parser_namespace_name (cp_parser* parser)
19332 {
19333 tree identifier;
19334 tree namespace_decl;
19335
19336 cp_token *token = cp_lexer_peek_token (parser->lexer);
19337
19338 /* Get the name of the namespace. */
19339 identifier = cp_parser_identifier (parser);
19340 if (identifier == error_mark_node)
19341 return error_mark_node;
19342
19343 /* Look up the identifier in the currently active scope. Look only
19344 for namespaces, due to:
19345
19346 [basic.lookup.udir]
19347
19348 When looking up a namespace-name in a using-directive or alias
19349 definition, only namespace names are considered.
19350
19351 And:
19352
19353 [basic.lookup.qual]
19354
19355 During the lookup of a name preceding the :: scope resolution
19356 operator, object, function, and enumerator names are ignored.
19357
19358 (Note that cp_parser_qualifying_entity only calls this
19359 function if the token after the name is the scope resolution
19360 operator.) */
19361 namespace_decl = cp_parser_lookup_name (parser, identifier,
19362 none_type,
19363 /*is_template=*/false,
19364 /*is_namespace=*/true,
19365 /*check_dependency=*/true,
19366 /*ambiguous_decls=*/NULL,
19367 token->location);
19368 /* If it's not a namespace, issue an error. */
19369 if (namespace_decl == error_mark_node
19370 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19371 {
19372 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19373 {
19374 auto_diagnostic_group d;
19375 name_hint hint;
19376 if (namespace_decl == error_mark_node
19377 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19378 hint = suggest_alternative_in_explicit_scope (token->location,
19379 identifier,
19380 parser->scope);
19381 if (const char *suggestion = hint.suggestion ())
19382 {
19383 gcc_rich_location richloc (token->location);
19384 richloc.add_fixit_replace (suggestion);
19385 error_at (&richloc,
19386 "%qD is not a namespace-name; did you mean %qs?",
19387 identifier, suggestion);
19388 }
19389 else
19390 error_at (token->location, "%qD is not a namespace-name",
19391 identifier);
19392 }
19393 else
19394 cp_parser_error (parser, "expected namespace-name");
19395 namespace_decl = error_mark_node;
19396 }
19397
19398 return namespace_decl;
19399 }
19400
19401 /* Parse a namespace-definition.
19402
19403 namespace-definition:
19404 named-namespace-definition
19405 unnamed-namespace-definition
19406
19407 named-namespace-definition:
19408 original-namespace-definition
19409 extension-namespace-definition
19410
19411 original-namespace-definition:
19412 namespace identifier { namespace-body }
19413
19414 extension-namespace-definition:
19415 namespace original-namespace-name { namespace-body }
19416
19417 unnamed-namespace-definition:
19418 namespace { namespace-body } */
19419
19420 static void
19421 cp_parser_namespace_definition (cp_parser* parser)
19422 {
19423 tree identifier;
19424 int nested_definition_count = 0;
19425
19426 cp_ensure_no_omp_declare_simd (parser);
19427 cp_ensure_no_oacc_routine (parser);
19428
19429 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19430 const bool topmost_inline_p = is_inline;
19431
19432 if (is_inline)
19433 {
19434 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19435 cp_lexer_consume_token (parser->lexer);
19436 }
19437
19438 /* Look for the `namespace' keyword. */
19439 cp_token* token
19440 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19441
19442 /* Parse any specified attributes before the identifier. */
19443 tree attribs = cp_parser_attributes_opt (parser);
19444
19445 for (;;)
19446 {
19447 identifier = NULL_TREE;
19448
19449 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19450 RID_INLINE);
19451 if (nested_inline_p && nested_definition_count != 0)
19452 {
19453 if (cxx_dialect < cxx2a)
19454 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19455 OPT_Wpedantic, "nested inline namespace definitions only "
19456 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
19457 cp_lexer_consume_token (parser->lexer);
19458 }
19459
19460 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19461 {
19462 identifier = cp_parser_identifier (parser);
19463
19464 if (cp_next_tokens_can_be_std_attribute_p (parser))
19465 pedwarn (input_location, OPT_Wpedantic,
19466 "standard attributes on namespaces must precede "
19467 "the namespace name");
19468
19469 /* Parse any attributes specified after the identifier. */
19470 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19471 }
19472
19473 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19474 {
19475 /* Don't forget that the innermost namespace might have been
19476 marked as inline. Use |= because we cannot overwrite
19477 IS_INLINE in case the outermost namespace is inline, but
19478 there are no nested inlines. */
19479 is_inline |= nested_inline_p;
19480 break;
19481 }
19482
19483 if (!nested_definition_count && cxx_dialect < cxx17)
19484 pedwarn (input_location, OPT_Wpedantic,
19485 "nested namespace definitions only available with "
19486 "%<-std=c++17%> or %<-std=gnu++17%>");
19487
19488 /* Nested namespace names can create new namespaces (unlike
19489 other qualified-ids). */
19490 if (int count = (identifier
19491 ? push_namespace (identifier, nested_inline_p)
19492 : 0))
19493 nested_definition_count += count;
19494 else
19495 cp_parser_error (parser, "nested namespace name required");
19496 cp_lexer_consume_token (parser->lexer);
19497 }
19498
19499 if (nested_definition_count && !identifier)
19500 cp_parser_error (parser, "namespace name required");
19501
19502 if (nested_definition_count && attribs)
19503 error_at (token->location,
19504 "a nested namespace definition cannot have attributes");
19505 if (nested_definition_count && topmost_inline_p)
19506 error_at (token->location,
19507 "a nested namespace definition cannot be inline");
19508
19509 /* Start the namespace. */
19510 nested_definition_count += push_namespace (identifier, is_inline);
19511
19512 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19513
19514 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19515
19516 /* Look for the `{' to validate starting the namespace. */
19517 matching_braces braces;
19518 if (braces.require_open (parser))
19519 {
19520 /* Parse the body of the namespace. */
19521 cp_parser_namespace_body (parser);
19522
19523 /* Look for the final `}'. */
19524 braces.require_close (parser);
19525 }
19526
19527 if (has_visibility)
19528 pop_visibility (1);
19529
19530 /* Pop the nested namespace definitions. */
19531 while (nested_definition_count--)
19532 pop_namespace ();
19533 }
19534
19535 /* Parse a namespace-body.
19536
19537 namespace-body:
19538 declaration-seq [opt] */
19539
19540 static void
19541 cp_parser_namespace_body (cp_parser* parser)
19542 {
19543 cp_parser_declaration_seq_opt (parser);
19544 }
19545
19546 /* Parse a namespace-alias-definition.
19547
19548 namespace-alias-definition:
19549 namespace identifier = qualified-namespace-specifier ; */
19550
19551 static void
19552 cp_parser_namespace_alias_definition (cp_parser* parser)
19553 {
19554 tree identifier;
19555 tree namespace_specifier;
19556
19557 cp_token *token = cp_lexer_peek_token (parser->lexer);
19558
19559 /* Look for the `namespace' keyword. */
19560 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19561 /* Look for the identifier. */
19562 identifier = cp_parser_identifier (parser);
19563 if (identifier == error_mark_node)
19564 return;
19565 /* Look for the `=' token. */
19566 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19567 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19568 {
19569 error_at (token->location, "%<namespace%> definition is not allowed here");
19570 /* Skip the definition. */
19571 cp_lexer_consume_token (parser->lexer);
19572 if (cp_parser_skip_to_closing_brace (parser))
19573 cp_lexer_consume_token (parser->lexer);
19574 return;
19575 }
19576 cp_parser_require (parser, CPP_EQ, RT_EQ);
19577 /* Look for the qualified-namespace-specifier. */
19578 namespace_specifier
19579 = cp_parser_qualified_namespace_specifier (parser);
19580 cp_warn_deprecated_use_scopes (namespace_specifier);
19581 /* Look for the `;' token. */
19582 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19583
19584 /* Register the alias in the symbol table. */
19585 do_namespace_alias (identifier, namespace_specifier);
19586 }
19587
19588 /* Parse a qualified-namespace-specifier.
19589
19590 qualified-namespace-specifier:
19591 :: [opt] nested-name-specifier [opt] namespace-name
19592
19593 Returns a NAMESPACE_DECL corresponding to the specified
19594 namespace. */
19595
19596 static tree
19597 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19598 {
19599 /* Look for the optional `::'. */
19600 cp_parser_global_scope_opt (parser,
19601 /*current_scope_valid_p=*/false);
19602
19603 /* Look for the optional nested-name-specifier. */
19604 cp_parser_nested_name_specifier_opt (parser,
19605 /*typename_keyword_p=*/false,
19606 /*check_dependency_p=*/true,
19607 /*type_p=*/false,
19608 /*is_declaration=*/true);
19609
19610 return cp_parser_namespace_name (parser);
19611 }
19612
19613 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19614 access declaration.
19615
19616 using-declaration:
19617 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19618 using :: unqualified-id ;
19619
19620 access-declaration:
19621 qualified-id ;
19622
19623 */
19624
19625 static bool
19626 cp_parser_using_declaration (cp_parser* parser,
19627 bool access_declaration_p)
19628 {
19629 cp_token *token;
19630 bool typename_p = false;
19631 bool global_scope_p;
19632 tree decl;
19633 tree identifier;
19634 tree qscope;
19635 int oldcount = errorcount;
19636 cp_token *diag_token = NULL;
19637
19638 if (access_declaration_p)
19639 {
19640 diag_token = cp_lexer_peek_token (parser->lexer);
19641 cp_parser_parse_tentatively (parser);
19642 }
19643 else
19644 {
19645 /* Look for the `using' keyword. */
19646 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19647
19648 again:
19649 /* Peek at the next token. */
19650 token = cp_lexer_peek_token (parser->lexer);
19651 /* See if it's `typename'. */
19652 if (token->keyword == RID_TYPENAME)
19653 {
19654 /* Remember that we've seen it. */
19655 typename_p = true;
19656 /* Consume the `typename' token. */
19657 cp_lexer_consume_token (parser->lexer);
19658 }
19659 }
19660
19661 /* Look for the optional global scope qualification. */
19662 global_scope_p
19663 = (cp_parser_global_scope_opt (parser,
19664 /*current_scope_valid_p=*/false)
19665 != NULL_TREE);
19666
19667 /* If we saw `typename', or didn't see `::', then there must be a
19668 nested-name-specifier present. */
19669 if (typename_p || !global_scope_p)
19670 {
19671 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19672 /*check_dependency_p=*/true,
19673 /*type_p=*/false,
19674 /*is_declaration=*/true);
19675 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19676 {
19677 cp_parser_skip_to_end_of_block_or_statement (parser);
19678 return false;
19679 }
19680 }
19681 /* Otherwise, we could be in either of the two productions. In that
19682 case, treat the nested-name-specifier as optional. */
19683 else
19684 qscope = cp_parser_nested_name_specifier_opt (parser,
19685 /*typename_keyword_p=*/false,
19686 /*check_dependency_p=*/true,
19687 /*type_p=*/false,
19688 /*is_declaration=*/true);
19689 if (!qscope)
19690 qscope = global_namespace;
19691 else if (UNSCOPED_ENUM_P (qscope)
19692 && !TYPE_FUNCTION_SCOPE_P (qscope))
19693 qscope = CP_TYPE_CONTEXT (qscope);
19694
19695 cp_warn_deprecated_use_scopes (qscope);
19696
19697 if (access_declaration_p && cp_parser_error_occurred (parser))
19698 /* Something has already gone wrong; there's no need to parse
19699 further. Since an error has occurred, the return value of
19700 cp_parser_parse_definitely will be false, as required. */
19701 return cp_parser_parse_definitely (parser);
19702
19703 token = cp_lexer_peek_token (parser->lexer);
19704 /* Parse the unqualified-id. */
19705 identifier = cp_parser_unqualified_id (parser,
19706 /*template_keyword_p=*/false,
19707 /*check_dependency_p=*/true,
19708 /*declarator_p=*/true,
19709 /*optional_p=*/false);
19710
19711 if (access_declaration_p)
19712 {
19713 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19714 cp_parser_simulate_error (parser);
19715 if (!cp_parser_parse_definitely (parser))
19716 return false;
19717 }
19718 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19719 {
19720 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19721 if (cxx_dialect < cxx17)
19722 pedwarn (ell->location, 0,
19723 "pack expansion in using-declaration only available "
19724 "with %<-std=c++17%> or %<-std=gnu++17%>");
19725 qscope = make_pack_expansion (qscope);
19726 }
19727
19728 /* The function we call to handle a using-declaration is different
19729 depending on what scope we are in. */
19730 if (qscope == error_mark_node || identifier == error_mark_node)
19731 ;
19732 else if (!identifier_p (identifier)
19733 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19734 /* [namespace.udecl]
19735
19736 A using declaration shall not name a template-id. */
19737 error_at (token->location,
19738 "a template-id may not appear in a using-declaration");
19739 else
19740 {
19741 if (at_class_scope_p ())
19742 {
19743 /* Create the USING_DECL. */
19744 decl = do_class_using_decl (qscope, identifier);
19745
19746 if (decl && typename_p)
19747 USING_DECL_TYPENAME_P (decl) = 1;
19748
19749 if (check_for_bare_parameter_packs (decl))
19750 {
19751 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19752 return false;
19753 }
19754 else
19755 /* Add it to the list of members in this class. */
19756 finish_member_declaration (decl);
19757 }
19758 else
19759 finish_nonmember_using_decl (qscope, identifier);
19760 }
19761
19762 if (!access_declaration_p
19763 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19764 {
19765 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19766 if (cxx_dialect < cxx17)
19767 pedwarn (comma->location, 0,
19768 "comma-separated list in using-declaration only available "
19769 "with %<-std=c++17%> or %<-std=gnu++17%>");
19770 goto again;
19771 }
19772
19773 /* Look for the final `;'. */
19774 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19775
19776 if (access_declaration_p && errorcount == oldcount)
19777 warning_at (diag_token->location, OPT_Wdeprecated,
19778 "access declarations are deprecated "
19779 "in favour of using-declarations; "
19780 "suggestion: add the %<using%> keyword");
19781
19782 return true;
19783 }
19784
19785 /* Parse an alias-declaration.
19786
19787 alias-declaration:
19788 using identifier attribute-specifier-seq [opt] = type-id */
19789
19790 static tree
19791 cp_parser_alias_declaration (cp_parser* parser)
19792 {
19793 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19794 location_t id_location, type_location;
19795 cp_declarator *declarator;
19796 cp_decl_specifier_seq decl_specs;
19797 bool member_p;
19798 const char *saved_message = NULL;
19799
19800 /* Look for the `using' keyword. */
19801 cp_token *using_token
19802 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19803 if (using_token == NULL)
19804 return error_mark_node;
19805
19806 id_location = cp_lexer_peek_token (parser->lexer)->location;
19807 id = cp_parser_identifier (parser);
19808 if (id == error_mark_node)
19809 return error_mark_node;
19810
19811 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19812 attributes = cp_parser_attributes_opt (parser);
19813 if (attributes == error_mark_node)
19814 return error_mark_node;
19815
19816 cp_parser_require (parser, CPP_EQ, RT_EQ);
19817
19818 if (cp_parser_error_occurred (parser))
19819 return error_mark_node;
19820
19821 cp_parser_commit_to_tentative_parse (parser);
19822
19823 /* Now we are going to parse the type-id of the declaration. */
19824
19825 /*
19826 [dcl.type]/3 says:
19827
19828 "A type-specifier-seq shall not define a class or enumeration
19829 unless it appears in the type-id of an alias-declaration (7.1.3) that
19830 is not the declaration of a template-declaration."
19831
19832 In other words, if we currently are in an alias template, the
19833 type-id should not define a type.
19834
19835 So let's set parser->type_definition_forbidden_message in that
19836 case; cp_parser_check_type_definition (called by
19837 cp_parser_class_specifier) will then emit an error if a type is
19838 defined in the type-id. */
19839 if (parser->num_template_parameter_lists)
19840 {
19841 saved_message = parser->type_definition_forbidden_message;
19842 parser->type_definition_forbidden_message =
19843 G_("types may not be defined in alias template declarations");
19844 }
19845
19846 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
19847 &type_location);
19848
19849 /* Restore the error message if need be. */
19850 if (parser->num_template_parameter_lists)
19851 parser->type_definition_forbidden_message = saved_message;
19852
19853 if (type == error_mark_node
19854 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19855 {
19856 cp_parser_skip_to_end_of_block_or_statement (parser);
19857 return error_mark_node;
19858 }
19859
19860 /* A typedef-name can also be introduced by an alias-declaration. The
19861 identifier following the using keyword becomes a typedef-name. It has
19862 the same semantics as if it were introduced by the typedef
19863 specifier. In particular, it does not define a new type and it shall
19864 not appear in the type-id. */
19865
19866 clear_decl_specs (&decl_specs);
19867 decl_specs.type = type;
19868 if (attributes != NULL_TREE)
19869 {
19870 decl_specs.attributes = attributes;
19871 set_and_check_decl_spec_loc (&decl_specs,
19872 ds_attribute,
19873 attrs_token);
19874 }
19875 set_and_check_decl_spec_loc (&decl_specs,
19876 ds_typedef,
19877 using_token);
19878 set_and_check_decl_spec_loc (&decl_specs,
19879 ds_alias,
19880 using_token);
19881 decl_specs.locations[ds_type_spec] = type_location;
19882
19883 if (parser->num_template_parameter_lists
19884 && !cp_parser_check_template_parameters (parser,
19885 /*num_templates=*/0,
19886 /*template_id*/false,
19887 id_location,
19888 /*declarator=*/NULL))
19889 return error_mark_node;
19890
19891 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19892
19893 member_p = at_class_scope_p ();
19894 if (member_p)
19895 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19896 NULL_TREE, attributes);
19897 else
19898 decl = start_decl (declarator, &decl_specs, 0,
19899 attributes, NULL_TREE, &pushed_scope);
19900 if (decl == error_mark_node)
19901 return decl;
19902
19903 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19904
19905 if (pushed_scope)
19906 pop_scope (pushed_scope);
19907
19908 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19909 added into the symbol table; otherwise, return the TYPE_DECL. */
19910 if (DECL_LANG_SPECIFIC (decl)
19911 && DECL_TEMPLATE_INFO (decl)
19912 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19913 {
19914 decl = DECL_TI_TEMPLATE (decl);
19915 if (member_p)
19916 check_member_template (decl);
19917 }
19918
19919 return decl;
19920 }
19921
19922 /* Parse a using-directive.
19923
19924 using-directive:
19925 using namespace :: [opt] nested-name-specifier [opt]
19926 namespace-name ; */
19927
19928 static void
19929 cp_parser_using_directive (cp_parser* parser)
19930 {
19931 tree namespace_decl;
19932 tree attribs;
19933
19934 /* Look for the `using' keyword. */
19935 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19936 /* And the `namespace' keyword. */
19937 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19938 /* Look for the optional `::' operator. */
19939 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19940 /* And the optional nested-name-specifier. */
19941 cp_parser_nested_name_specifier_opt (parser,
19942 /*typename_keyword_p=*/false,
19943 /*check_dependency_p=*/true,
19944 /*type_p=*/false,
19945 /*is_declaration=*/true);
19946 /* Get the namespace being used. */
19947 namespace_decl = cp_parser_namespace_name (parser);
19948 cp_warn_deprecated_use_scopes (namespace_decl);
19949 /* And any specified attributes. */
19950 attribs = cp_parser_attributes_opt (parser);
19951
19952 /* Update the symbol table. */
19953 finish_using_directive (namespace_decl, attribs);
19954
19955 /* Look for the final `;'. */
19956 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19957 }
19958
19959 /* Parse an asm-definition.
19960
19961 asm-qualifier:
19962 volatile
19963 inline
19964 goto
19965
19966 asm-qualifier-list:
19967 asm-qualifier
19968 asm-qualifier-list asm-qualifier
19969
19970 asm-definition:
19971 asm ( string-literal ) ;
19972
19973 GNU Extension:
19974
19975 asm-definition:
19976 asm asm-qualifier-list [opt] ( string-literal ) ;
19977 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19978 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19979 : asm-operand-list [opt] ) ;
19980 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19981 : asm-operand-list [opt]
19982 : asm-clobber-list [opt] ) ;
19983 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19984 : asm-clobber-list [opt]
19985 : asm-goto-list ) ;
19986
19987 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19988 contains goto, and is the only allowed form in that case. No duplicates are
19989 allowed in an asm-qualifier-list. */
19990
19991 static void
19992 cp_parser_asm_definition (cp_parser* parser)
19993 {
19994 tree string;
19995 tree outputs = NULL_TREE;
19996 tree inputs = NULL_TREE;
19997 tree clobbers = NULL_TREE;
19998 tree labels = NULL_TREE;
19999 tree asm_stmt;
20000 bool extended_p = false;
20001 bool invalid_inputs_p = false;
20002 bool invalid_outputs_p = false;
20003 required_token missing = RT_NONE;
20004 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
20005
20006 /* Look for the `asm' keyword. */
20007 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
20008
20009 /* In C++2a, unevaluated inline assembly is permitted in constexpr
20010 functions. */
20011 if (parser->in_function_body
20012 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
20013 && (cxx_dialect < cxx2a))
20014 pedwarn (asm_loc, 0, "%<asm%> in %<constexpr%> function only available "
20015 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
20016
20017 /* Handle the asm-qualifier-list. */
20018 location_t volatile_loc = UNKNOWN_LOCATION;
20019 location_t inline_loc = UNKNOWN_LOCATION;
20020 location_t goto_loc = UNKNOWN_LOCATION;
20021 location_t first_loc = UNKNOWN_LOCATION;
20022
20023 if (cp_parser_allow_gnu_extensions_p (parser))
20024 for (;;)
20025 {
20026 cp_token *token = cp_lexer_peek_token (parser->lexer);
20027 location_t loc = token->location;
20028 switch (cp_lexer_peek_token (parser->lexer)->keyword)
20029 {
20030 case RID_VOLATILE:
20031 if (volatile_loc)
20032 {
20033 error_at (loc, "duplicate %<asm%> qualifier %qT",
20034 token->u.value);
20035 inform (volatile_loc, "first seen here");
20036 }
20037 else
20038 {
20039 if (!parser->in_function_body)
20040 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
20041 "outside of function body", token->u.value);
20042 volatile_loc = loc;
20043 }
20044 cp_lexer_consume_token (parser->lexer);
20045 continue;
20046
20047 case RID_INLINE:
20048 if (inline_loc)
20049 {
20050 error_at (loc, "duplicate %<asm%> qualifier %qT",
20051 token->u.value);
20052 inform (inline_loc, "first seen here");
20053 }
20054 else
20055 inline_loc = loc;
20056 if (!first_loc)
20057 first_loc = loc;
20058 cp_lexer_consume_token (parser->lexer);
20059 continue;
20060
20061 case RID_GOTO:
20062 if (goto_loc)
20063 {
20064 error_at (loc, "duplicate %<asm%> qualifier %qT",
20065 token->u.value);
20066 inform (goto_loc, "first seen here");
20067 }
20068 else
20069 goto_loc = loc;
20070 if (!first_loc)
20071 first_loc = loc;
20072 cp_lexer_consume_token (parser->lexer);
20073 continue;
20074
20075 case RID_CONST:
20076 case RID_RESTRICT:
20077 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
20078 cp_lexer_consume_token (parser->lexer);
20079 continue;
20080
20081 default:
20082 break;
20083 }
20084 break;
20085 }
20086
20087 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
20088 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
20089 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
20090
20091 if (!parser->in_function_body && (inline_p || goto_p))
20092 {
20093 error_at (first_loc, "%<asm%> qualifier outside of function body");
20094 inline_p = goto_p = false;
20095 }
20096
20097 /* Look for the opening `('. */
20098 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20099 return;
20100 /* Look for the string. */
20101 string = cp_parser_string_literal (parser, false, false);
20102 if (string == error_mark_node)
20103 {
20104 cp_parser_skip_to_closing_parenthesis (parser, true, false,
20105 /*consume_paren=*/true);
20106 return;
20107 }
20108
20109 /* If we're allowing GNU extensions, check for the extended assembly
20110 syntax. Unfortunately, the `:' tokens need not be separated by
20111 a space in C, and so, for compatibility, we tolerate that here
20112 too. Doing that means that we have to treat the `::' operator as
20113 two `:' tokens. */
20114 if (cp_parser_allow_gnu_extensions_p (parser)
20115 && parser->in_function_body
20116 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
20117 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
20118 {
20119 bool inputs_p = false;
20120 bool clobbers_p = false;
20121 bool labels_p = false;
20122
20123 /* The extended syntax was used. */
20124 extended_p = true;
20125
20126 /* Look for outputs. */
20127 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20128 {
20129 /* Consume the `:'. */
20130 cp_lexer_consume_token (parser->lexer);
20131 /* Parse the output-operands. */
20132 if (cp_lexer_next_token_is_not (parser->lexer,
20133 CPP_COLON)
20134 && cp_lexer_next_token_is_not (parser->lexer,
20135 CPP_SCOPE)
20136 && cp_lexer_next_token_is_not (parser->lexer,
20137 CPP_CLOSE_PAREN)
20138 && !goto_p)
20139 {
20140 outputs = cp_parser_asm_operand_list (parser);
20141 if (outputs == error_mark_node)
20142 invalid_outputs_p = true;
20143 }
20144 }
20145 /* If the next token is `::', there are no outputs, and the
20146 next token is the beginning of the inputs. */
20147 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20148 /* The inputs are coming next. */
20149 inputs_p = true;
20150
20151 /* Look for inputs. */
20152 if (inputs_p
20153 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20154 {
20155 /* Consume the `:' or `::'. */
20156 cp_lexer_consume_token (parser->lexer);
20157 /* Parse the output-operands. */
20158 if (cp_lexer_next_token_is_not (parser->lexer,
20159 CPP_COLON)
20160 && cp_lexer_next_token_is_not (parser->lexer,
20161 CPP_SCOPE)
20162 && cp_lexer_next_token_is_not (parser->lexer,
20163 CPP_CLOSE_PAREN))
20164 {
20165 inputs = cp_parser_asm_operand_list (parser);
20166 if (inputs == error_mark_node)
20167 invalid_inputs_p = true;
20168 }
20169 }
20170 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20171 /* The clobbers are coming next. */
20172 clobbers_p = true;
20173
20174 /* Look for clobbers. */
20175 if (clobbers_p
20176 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20177 {
20178 clobbers_p = true;
20179 /* Consume the `:' or `::'. */
20180 cp_lexer_consume_token (parser->lexer);
20181 /* Parse the clobbers. */
20182 if (cp_lexer_next_token_is_not (parser->lexer,
20183 CPP_COLON)
20184 && cp_lexer_next_token_is_not (parser->lexer,
20185 CPP_CLOSE_PAREN))
20186 clobbers = cp_parser_asm_clobber_list (parser);
20187 }
20188 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20189 /* The labels are coming next. */
20190 labels_p = true;
20191
20192 /* Look for labels. */
20193 if (labels_p
20194 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
20195 {
20196 labels_p = true;
20197 /* Consume the `:' or `::'. */
20198 cp_lexer_consume_token (parser->lexer);
20199 /* Parse the labels. */
20200 labels = cp_parser_asm_label_list (parser);
20201 }
20202
20203 if (goto_p && !labels_p)
20204 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
20205 }
20206 else if (goto_p)
20207 missing = RT_COLON_SCOPE;
20208
20209 /* Look for the closing `)'. */
20210 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
20211 missing ? missing : RT_CLOSE_PAREN))
20212 cp_parser_skip_to_closing_parenthesis (parser, true, false,
20213 /*consume_paren=*/true);
20214 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20215
20216 if (!invalid_inputs_p && !invalid_outputs_p)
20217 {
20218 /* Create the ASM_EXPR. */
20219 if (parser->in_function_body)
20220 {
20221 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
20222 inputs, clobbers, labels, inline_p);
20223 /* If the extended syntax was not used, mark the ASM_EXPR. */
20224 if (!extended_p)
20225 {
20226 tree temp = asm_stmt;
20227 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
20228 temp = TREE_OPERAND (temp, 0);
20229
20230 ASM_INPUT_P (temp) = 1;
20231 }
20232 }
20233 else
20234 symtab->finalize_toplevel_asm (string);
20235 }
20236 }
20237
20238 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20239 type that comes from the decl-specifier-seq. */
20240
20241 static tree
20242 strip_declarator_types (tree type, cp_declarator *declarator)
20243 {
20244 for (cp_declarator *d = declarator; d;)
20245 switch (d->kind)
20246 {
20247 case cdk_id:
20248 case cdk_decomp:
20249 case cdk_error:
20250 d = NULL;
20251 break;
20252
20253 default:
20254 if (TYPE_PTRMEMFUNC_P (type))
20255 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
20256 type = TREE_TYPE (type);
20257 d = d->declarator;
20258 break;
20259 }
20260
20261 return type;
20262 }
20263
20264 /* Declarators [gram.dcl.decl] */
20265
20266 /* Parse an init-declarator.
20267
20268 init-declarator:
20269 declarator initializer [opt]
20270
20271 GNU Extension:
20272
20273 init-declarator:
20274 declarator asm-specification [opt] attributes [opt] initializer [opt]
20275
20276 function-definition:
20277 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20278 function-body
20279 decl-specifier-seq [opt] declarator function-try-block
20280
20281 GNU Extension:
20282
20283 function-definition:
20284 __extension__ function-definition
20285
20286 TM Extension:
20287
20288 function-definition:
20289 decl-specifier-seq [opt] declarator function-transaction-block
20290
20291 The parser flags FLAGS is used to control type-specifier parsing.
20292
20293 The DECL_SPECIFIERS apply to this declarator. Returns a
20294 representation of the entity declared. If MEMBER_P is TRUE, then
20295 this declarator appears in a class scope. The new DECL created by
20296 this declarator is returned.
20297
20298 The CHECKS are access checks that should be performed once we know
20299 what entity is being declared (and, therefore, what classes have
20300 befriended it).
20301
20302 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20303 for a function-definition here as well. If the declarator is a
20304 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20305 be TRUE upon return. By that point, the function-definition will
20306 have been completely parsed.
20307
20308 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20309 is FALSE.
20310
20311 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20312 parsed declaration if it is an uninitialized single declarator not followed
20313 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20314 if present, will not be consumed. If returned, this declarator will be
20315 created with SD_INITIALIZED but will not call cp_finish_decl.
20316
20317 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20318 and there is an initializer, the pointed location_t is set to the
20319 location of the '=' or `(', or '{' in C++11 token introducing the
20320 initializer. */
20321
20322 static tree
20323 cp_parser_init_declarator (cp_parser* parser,
20324 cp_parser_flags flags,
20325 cp_decl_specifier_seq *decl_specifiers,
20326 vec<deferred_access_check, va_gc> *checks,
20327 bool function_definition_allowed_p,
20328 bool member_p,
20329 int declares_class_or_enum,
20330 bool* function_definition_p,
20331 tree* maybe_range_for_decl,
20332 location_t* init_loc,
20333 tree* auto_result)
20334 {
20335 cp_token *token = NULL, *asm_spec_start_token = NULL,
20336 *attributes_start_token = NULL;
20337 cp_declarator *declarator;
20338 tree prefix_attributes;
20339 tree attributes = NULL;
20340 tree asm_specification;
20341 tree initializer;
20342 tree decl = NULL_TREE;
20343 tree scope;
20344 int is_initialized;
20345 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20346 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20347 "(...)". */
20348 enum cpp_ttype initialization_kind;
20349 bool is_direct_init = false;
20350 bool is_non_constant_init;
20351 int ctor_dtor_or_conv_p;
20352 bool friend_p = cp_parser_friend_p (decl_specifiers);
20353 tree pushed_scope = NULL_TREE;
20354 bool range_for_decl_p = false;
20355 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20356 location_t tmp_init_loc = UNKNOWN_LOCATION;
20357
20358 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
20359 flags |= CP_PARSER_FLAGS_CONSTEVAL;
20360
20361 /* Gather the attributes that were provided with the
20362 decl-specifiers. */
20363 prefix_attributes = decl_specifiers->attributes;
20364
20365 /* Assume that this is not the declarator for a function
20366 definition. */
20367 if (function_definition_p)
20368 *function_definition_p = false;
20369
20370 /* Default arguments are only permitted for function parameters. */
20371 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20372 parser->default_arg_ok_p = false;
20373
20374 /* Defer access checks while parsing the declarator; we cannot know
20375 what names are accessible until we know what is being
20376 declared. */
20377 resume_deferring_access_checks ();
20378
20379 token = cp_lexer_peek_token (parser->lexer);
20380
20381 /* Parse the declarator. */
20382 declarator
20383 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20384 flags, &ctor_dtor_or_conv_p,
20385 /*parenthesized_p=*/NULL,
20386 member_p, friend_p, /*static_p=*/false);
20387 /* Gather up the deferred checks. */
20388 stop_deferring_access_checks ();
20389
20390 parser->default_arg_ok_p = saved_default_arg_ok_p;
20391
20392 /* If the DECLARATOR was erroneous, there's no need to go
20393 further. */
20394 if (declarator == cp_error_declarator)
20395 return error_mark_node;
20396
20397 /* Check that the number of template-parameter-lists is OK. */
20398 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20399 token->location))
20400 return error_mark_node;
20401
20402 if (declares_class_or_enum & 2)
20403 cp_parser_check_for_definition_in_return_type (declarator,
20404 decl_specifiers->type,
20405 decl_specifiers->locations[ds_type_spec]);
20406
20407 /* Figure out what scope the entity declared by the DECLARATOR is
20408 located in. `grokdeclarator' sometimes changes the scope, so
20409 we compute it now. */
20410 scope = get_scope_of_declarator (declarator);
20411
20412 /* Perform any lookups in the declared type which were thought to be
20413 dependent, but are not in the scope of the declarator. */
20414 decl_specifiers->type
20415 = maybe_update_decl_type (decl_specifiers->type, scope);
20416
20417 /* If we're allowing GNU extensions, look for an
20418 asm-specification. */
20419 if (cp_parser_allow_gnu_extensions_p (parser))
20420 {
20421 /* Look for an asm-specification. */
20422 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20423 asm_specification = cp_parser_asm_specification_opt (parser);
20424 }
20425 else
20426 asm_specification = NULL_TREE;
20427
20428 /* Look for attributes. */
20429 attributes_start_token = cp_lexer_peek_token (parser->lexer);
20430 attributes = cp_parser_attributes_opt (parser);
20431
20432 /* Peek at the next token. */
20433 token = cp_lexer_peek_token (parser->lexer);
20434
20435 bool bogus_implicit_tmpl = false;
20436
20437 if (function_declarator_p (declarator))
20438 {
20439 /* Handle C++17 deduction guides. */
20440 if (!decl_specifiers->type
20441 && ctor_dtor_or_conv_p <= 0
20442 && cxx_dialect >= cxx17)
20443 {
20444 cp_declarator *id = get_id_declarator (declarator);
20445 tree name = id->u.id.unqualified_name;
20446 parser->scope = id->u.id.qualifying_scope;
20447 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20448 if (tmpl
20449 && (DECL_CLASS_TEMPLATE_P (tmpl)
20450 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20451 {
20452 id->u.id.unqualified_name = dguide_name (tmpl);
20453 id->u.id.sfk = sfk_deduction_guide;
20454 ctor_dtor_or_conv_p = 1;
20455 }
20456 }
20457
20458 /* Check to see if the token indicates the start of a
20459 function-definition. */
20460 if (cp_parser_token_starts_function_definition_p (token))
20461 {
20462 if (!function_definition_allowed_p)
20463 {
20464 /* If a function-definition should not appear here, issue an
20465 error message. */
20466 cp_parser_error (parser,
20467 "a function-definition is not allowed here");
20468 return error_mark_node;
20469 }
20470
20471 location_t func_brace_location
20472 = cp_lexer_peek_token (parser->lexer)->location;
20473
20474 /* Neither attributes nor an asm-specification are allowed
20475 on a function-definition. */
20476 if (asm_specification)
20477 error_at (asm_spec_start_token->location,
20478 "an %<asm%> specification is not allowed "
20479 "on a function-definition");
20480 if (attributes)
20481 error_at (attributes_start_token->location,
20482 "attributes are not allowed "
20483 "on a function-definition");
20484 /* This is a function-definition. */
20485 *function_definition_p = true;
20486
20487 /* Parse the function definition. */
20488 if (member_p)
20489 decl = cp_parser_save_member_function_body (parser,
20490 decl_specifiers,
20491 declarator,
20492 prefix_attributes);
20493 else
20494 decl =
20495 (cp_parser_function_definition_from_specifiers_and_declarator
20496 (parser, decl_specifiers, prefix_attributes, declarator));
20497
20498 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20499 {
20500 /* This is where the prologue starts... */
20501 DECL_STRUCT_FUNCTION (decl)->function_start_locus
20502 = func_brace_location;
20503 }
20504
20505 return decl;
20506 }
20507 }
20508 else if (parser->fully_implicit_function_template_p)
20509 {
20510 /* A non-template declaration involving a function parameter list
20511 containing an implicit template parameter will be made into a
20512 template. If the resulting declaration is not going to be an
20513 actual function then finish the template scope here to prevent it.
20514 An error message will be issued once we have a decl to talk about.
20515
20516 FIXME probably we should do type deduction rather than create an
20517 implicit template, but the standard currently doesn't allow it. */
20518 bogus_implicit_tmpl = true;
20519 finish_fully_implicit_template (parser, NULL_TREE);
20520 }
20521
20522 /* [dcl.dcl]
20523
20524 Only in function declarations for constructors, destructors, type
20525 conversions, and deduction guides can the decl-specifier-seq be omitted.
20526
20527 We explicitly postpone this check past the point where we handle
20528 function-definitions because we tolerate function-definitions
20529 that are missing their return types in some modes. */
20530 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20531 {
20532 cp_parser_error (parser,
20533 "expected constructor, destructor, or type conversion");
20534 return error_mark_node;
20535 }
20536
20537 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20538 if (token->type == CPP_EQ
20539 || token->type == CPP_OPEN_PAREN
20540 || token->type == CPP_OPEN_BRACE)
20541 {
20542 is_initialized = SD_INITIALIZED;
20543 initialization_kind = token->type;
20544 if (maybe_range_for_decl)
20545 *maybe_range_for_decl = error_mark_node;
20546 tmp_init_loc = token->location;
20547 if (init_loc && *init_loc == UNKNOWN_LOCATION)
20548 *init_loc = tmp_init_loc;
20549
20550 if (token->type == CPP_EQ
20551 && function_declarator_p (declarator))
20552 {
20553 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20554 if (t2->keyword == RID_DEFAULT)
20555 is_initialized = SD_DEFAULTED;
20556 else if (t2->keyword == RID_DELETE)
20557 is_initialized = SD_DELETED;
20558 }
20559 }
20560 else
20561 {
20562 /* If the init-declarator isn't initialized and isn't followed by a
20563 `,' or `;', it's not a valid init-declarator. */
20564 if (token->type != CPP_COMMA
20565 && token->type != CPP_SEMICOLON)
20566 {
20567 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20568 range_for_decl_p = true;
20569 else
20570 {
20571 if (!maybe_range_for_decl)
20572 cp_parser_error (parser, "expected initializer");
20573 return error_mark_node;
20574 }
20575 }
20576 is_initialized = SD_UNINITIALIZED;
20577 initialization_kind = CPP_EOF;
20578 }
20579
20580 /* Because start_decl has side-effects, we should only call it if we
20581 know we're going ahead. By this point, we know that we cannot
20582 possibly be looking at any other construct. */
20583 cp_parser_commit_to_tentative_parse (parser);
20584
20585 /* Enter the newly declared entry in the symbol table. If we're
20586 processing a declaration in a class-specifier, we wait until
20587 after processing the initializer. */
20588 if (!member_p)
20589 {
20590 if (parser->in_unbraced_linkage_specification_p)
20591 decl_specifiers->storage_class = sc_extern;
20592 decl = start_decl (declarator, decl_specifiers,
20593 range_for_decl_p? SD_INITIALIZED : is_initialized,
20594 attributes, prefix_attributes, &pushed_scope);
20595 cp_finalize_omp_declare_simd (parser, decl);
20596 cp_finalize_oacc_routine (parser, decl, false);
20597 /* Adjust location of decl if declarator->id_loc is more appropriate:
20598 set, and decl wasn't merged with another decl, in which case its
20599 location would be different from input_location, and more accurate. */
20600 if (DECL_P (decl)
20601 && declarator->id_loc != UNKNOWN_LOCATION
20602 && DECL_SOURCE_LOCATION (decl) == input_location)
20603 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20604 }
20605 else if (scope)
20606 /* Enter the SCOPE. That way unqualified names appearing in the
20607 initializer will be looked up in SCOPE. */
20608 pushed_scope = push_scope (scope);
20609
20610 /* Perform deferred access control checks, now that we know in which
20611 SCOPE the declared entity resides. */
20612 if (!member_p && decl)
20613 {
20614 tree saved_current_function_decl = NULL_TREE;
20615
20616 /* If the entity being declared is a function, pretend that we
20617 are in its scope. If it is a `friend', it may have access to
20618 things that would not otherwise be accessible. */
20619 if (TREE_CODE (decl) == FUNCTION_DECL)
20620 {
20621 saved_current_function_decl = current_function_decl;
20622 current_function_decl = decl;
20623 }
20624
20625 /* Perform access checks for template parameters. */
20626 cp_parser_perform_template_parameter_access_checks (checks);
20627
20628 /* Perform the access control checks for the declarator and the
20629 decl-specifiers. */
20630 perform_deferred_access_checks (tf_warning_or_error);
20631
20632 /* Restore the saved value. */
20633 if (TREE_CODE (decl) == FUNCTION_DECL)
20634 current_function_decl = saved_current_function_decl;
20635 }
20636
20637 /* Parse the initializer. */
20638 initializer = NULL_TREE;
20639 is_direct_init = false;
20640 is_non_constant_init = true;
20641 if (is_initialized)
20642 {
20643 if (function_declarator_p (declarator))
20644 {
20645 if (initialization_kind == CPP_EQ)
20646 initializer = cp_parser_pure_specifier (parser);
20647 else
20648 {
20649 /* If the declaration was erroneous, we don't really
20650 know what the user intended, so just silently
20651 consume the initializer. */
20652 if (decl != error_mark_node)
20653 error_at (tmp_init_loc, "initializer provided for function");
20654 cp_parser_skip_to_closing_parenthesis (parser,
20655 /*recovering=*/true,
20656 /*or_comma=*/false,
20657 /*consume_paren=*/true);
20658 }
20659 }
20660 else
20661 {
20662 /* We want to record the extra mangling scope for in-class
20663 initializers of class members and initializers of static data
20664 member templates. The former involves deferring
20665 parsing of the initializer until end of class as with default
20666 arguments. So right here we only handle the latter. */
20667 if (!member_p && processing_template_decl && decl != error_mark_node)
20668 start_lambda_scope (decl);
20669 initializer = cp_parser_initializer (parser,
20670 &is_direct_init,
20671 &is_non_constant_init);
20672 if (!member_p && processing_template_decl && decl != error_mark_node)
20673 finish_lambda_scope ();
20674 if (initializer == error_mark_node)
20675 cp_parser_skip_to_end_of_statement (parser);
20676 }
20677 }
20678
20679 /* The old parser allows attributes to appear after a parenthesized
20680 initializer. Mark Mitchell proposed removing this functionality
20681 on the GCC mailing lists on 2002-08-13. This parser accepts the
20682 attributes -- but ignores them. Made a permerror in GCC 8. */
20683 if (cp_parser_allow_gnu_extensions_p (parser)
20684 && initialization_kind == CPP_OPEN_PAREN
20685 && cp_parser_attributes_opt (parser)
20686 && permerror (input_location,
20687 "attributes after parenthesized initializer ignored"))
20688 {
20689 static bool hint;
20690 if (flag_permissive && !hint)
20691 {
20692 hint = true;
20693 inform (input_location,
20694 "this flexibility is deprecated and will be removed");
20695 }
20696 }
20697
20698 /* And now complain about a non-function implicit template. */
20699 if (bogus_implicit_tmpl && decl != error_mark_node)
20700 error_at (DECL_SOURCE_LOCATION (decl),
20701 "non-function %qD declared as implicit template", decl);
20702
20703 /* For an in-class declaration, use `grokfield' to create the
20704 declaration. */
20705 if (member_p)
20706 {
20707 if (pushed_scope)
20708 {
20709 pop_scope (pushed_scope);
20710 pushed_scope = NULL_TREE;
20711 }
20712 decl = grokfield (declarator, decl_specifiers,
20713 initializer, !is_non_constant_init,
20714 /*asmspec=*/NULL_TREE,
20715 attr_chainon (attributes, prefix_attributes));
20716 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20717 cp_parser_save_default_args (parser, decl);
20718 cp_finalize_omp_declare_simd (parser, decl);
20719 cp_finalize_oacc_routine (parser, decl, false);
20720 }
20721
20722 /* Finish processing the declaration. But, skip member
20723 declarations. */
20724 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20725 {
20726 int cf = (decl_spec_seq_has_spec_p (decl_specifiers, ds_constinit)
20727 ? LOOKUP_CONSTINIT : 0);
20728 cp_finish_decl (decl,
20729 initializer, !is_non_constant_init,
20730 asm_specification,
20731 /* If the initializer is in parentheses, then this is
20732 a direct-initialization, which means that an
20733 `explicit' constructor is OK. Otherwise, an
20734 `explicit' constructor cannot be used. */
20735 ((is_direct_init || !is_initialized)
20736 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT) | cf);
20737 }
20738 else if ((cxx_dialect != cxx98) && friend_p
20739 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20740 /* Core issue #226 (C++0x only): A default template-argument
20741 shall not be specified in a friend class template
20742 declaration. */
20743 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20744 /*is_partial=*/false, /*is_friend_decl=*/1);
20745
20746 if (!friend_p && pushed_scope)
20747 pop_scope (pushed_scope);
20748
20749 if (function_declarator_p (declarator)
20750 && parser->fully_implicit_function_template_p)
20751 {
20752 if (member_p)
20753 decl = finish_fully_implicit_template (parser, decl);
20754 else
20755 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20756 }
20757
20758 if (auto_result && is_initialized && decl_specifiers->type
20759 && type_uses_auto (decl_specifiers->type))
20760 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20761
20762 return decl;
20763 }
20764
20765 /* Parse a declarator.
20766
20767 declarator:
20768 direct-declarator
20769 ptr-operator declarator
20770
20771 abstract-declarator:
20772 ptr-operator abstract-declarator [opt]
20773 direct-abstract-declarator
20774
20775 GNU Extensions:
20776
20777 declarator:
20778 attributes [opt] direct-declarator
20779 attributes [opt] ptr-operator declarator
20780
20781 abstract-declarator:
20782 attributes [opt] ptr-operator abstract-declarator [opt]
20783 attributes [opt] direct-abstract-declarator
20784
20785 The parser flags FLAGS is used to control type-specifier parsing.
20786
20787 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20788 detect constructors, destructors, deduction guides, or conversion operators.
20789 It is set to -1 if the declarator is a name, and +1 if it is a
20790 function. Otherwise it is set to zero. Usually you just want to
20791 test for >0, but internally the negative value is used.
20792
20793 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20794 a decl-specifier-seq unless it declares a constructor, destructor,
20795 or conversion. It might seem that we could check this condition in
20796 semantic analysis, rather than parsing, but that makes it difficult
20797 to handle something like `f()'. We want to notice that there are
20798 no decl-specifiers, and therefore realize that this is an
20799 expression, not a declaration.)
20800
20801 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20802 the declarator is a direct-declarator of the form "(...)".
20803
20804 MEMBER_P is true iff this declarator is a member-declarator.
20805
20806 FRIEND_P is true iff this declarator is a friend.
20807
20808 STATIC_P is true iff the keyword static was seen. */
20809
20810 static cp_declarator *
20811 cp_parser_declarator (cp_parser* parser,
20812 cp_parser_declarator_kind dcl_kind,
20813 cp_parser_flags flags,
20814 int* ctor_dtor_or_conv_p,
20815 bool* parenthesized_p,
20816 bool member_p, bool friend_p, bool static_p)
20817 {
20818 cp_declarator *declarator;
20819 enum tree_code code;
20820 cp_cv_quals cv_quals;
20821 tree class_type;
20822 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20823
20824 /* Assume this is not a constructor, destructor, or type-conversion
20825 operator. */
20826 if (ctor_dtor_or_conv_p)
20827 *ctor_dtor_or_conv_p = 0;
20828
20829 if (cp_parser_allow_gnu_extensions_p (parser))
20830 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20831
20832 /* Check for the ptr-operator production. */
20833 cp_parser_parse_tentatively (parser);
20834 /* Parse the ptr-operator. */
20835 code = cp_parser_ptr_operator (parser,
20836 &class_type,
20837 &cv_quals,
20838 &std_attributes);
20839
20840 /* If that worked, then we have a ptr-operator. */
20841 if (cp_parser_parse_definitely (parser))
20842 {
20843 /* If a ptr-operator was found, then this declarator was not
20844 parenthesized. */
20845 if (parenthesized_p)
20846 *parenthesized_p = true;
20847 /* The dependent declarator is optional if we are parsing an
20848 abstract-declarator. */
20849 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20850 cp_parser_parse_tentatively (parser);
20851
20852 /* Parse the dependent declarator. */
20853 declarator = cp_parser_declarator (parser, dcl_kind,
20854 CP_PARSER_FLAGS_NONE,
20855 /*ctor_dtor_or_conv_p=*/NULL,
20856 /*parenthesized_p=*/NULL,
20857 /*member_p=*/false,
20858 friend_p, /*static_p=*/false);
20859
20860 /* If we are parsing an abstract-declarator, we must handle the
20861 case where the dependent declarator is absent. */
20862 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20863 && !cp_parser_parse_definitely (parser))
20864 declarator = NULL;
20865
20866 declarator = cp_parser_make_indirect_declarator
20867 (code, class_type, cv_quals, declarator, std_attributes);
20868 }
20869 /* Everything else is a direct-declarator. */
20870 else
20871 {
20872 if (parenthesized_p)
20873 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20874 CPP_OPEN_PAREN);
20875 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20876 flags, ctor_dtor_or_conv_p,
20877 member_p, friend_p, static_p);
20878 }
20879
20880 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20881 declarator->attributes = gnu_attributes;
20882 return declarator;
20883 }
20884
20885 /* Parse a direct-declarator or direct-abstract-declarator.
20886
20887 direct-declarator:
20888 declarator-id
20889 direct-declarator ( parameter-declaration-clause )
20890 cv-qualifier-seq [opt]
20891 ref-qualifier [opt]
20892 exception-specification [opt]
20893 direct-declarator [ constant-expression [opt] ]
20894 ( declarator )
20895
20896 direct-abstract-declarator:
20897 direct-abstract-declarator [opt]
20898 ( parameter-declaration-clause )
20899 cv-qualifier-seq [opt]
20900 ref-qualifier [opt]
20901 exception-specification [opt]
20902 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20903 ( abstract-declarator )
20904
20905 Returns a representation of the declarator. DCL_KIND is
20906 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20907 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20908 we are parsing a direct-declarator. It is
20909 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20910 of ambiguity we prefer an abstract declarator, as per
20911 [dcl.ambig.res].
20912 The parser flags FLAGS is used to control type-specifier parsing.
20913 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20914 as for cp_parser_declarator. */
20915
20916 static cp_declarator *
20917 cp_parser_direct_declarator (cp_parser* parser,
20918 cp_parser_declarator_kind dcl_kind,
20919 cp_parser_flags flags,
20920 int* ctor_dtor_or_conv_p,
20921 bool member_p, bool friend_p, bool static_p)
20922 {
20923 cp_token *token;
20924 cp_declarator *declarator = NULL;
20925 tree scope = NULL_TREE;
20926 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20927 bool saved_in_declarator_p = parser->in_declarator_p;
20928 bool first = true;
20929 tree pushed_scope = NULL_TREE;
20930 cp_token *open_paren = NULL, *close_paren = NULL;
20931
20932 while (true)
20933 {
20934 /* Peek at the next token. */
20935 token = cp_lexer_peek_token (parser->lexer);
20936 if (token->type == CPP_OPEN_PAREN)
20937 {
20938 /* This is either a parameter-declaration-clause, or a
20939 parenthesized declarator. When we know we are parsing a
20940 named declarator, it must be a parenthesized declarator
20941 if FIRST is true. For instance, `(int)' is a
20942 parameter-declaration-clause, with an omitted
20943 direct-abstract-declarator. But `((*))', is a
20944 parenthesized abstract declarator. Finally, when T is a
20945 template parameter `(T)' is a
20946 parameter-declaration-clause, and not a parenthesized
20947 named declarator.
20948
20949 We first try and parse a parameter-declaration-clause,
20950 and then try a nested declarator (if FIRST is true).
20951
20952 It is not an error for it not to be a
20953 parameter-declaration-clause, even when FIRST is
20954 false. Consider,
20955
20956 int i (int);
20957 int i (3);
20958
20959 The first is the declaration of a function while the
20960 second is the definition of a variable, including its
20961 initializer.
20962
20963 Having seen only the parenthesis, we cannot know which of
20964 these two alternatives should be selected. Even more
20965 complex are examples like:
20966
20967 int i (int (a));
20968 int i (int (3));
20969
20970 The former is a function-declaration; the latter is a
20971 variable initialization.
20972
20973 Thus again, we try a parameter-declaration-clause, and if
20974 that fails, we back out and return. */
20975
20976 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20977 {
20978 tree params;
20979 bool is_declarator = false;
20980
20981 open_paren = NULL;
20982
20983 /* In a member-declarator, the only valid interpretation
20984 of a parenthesis is the start of a
20985 parameter-declaration-clause. (It is invalid to
20986 initialize a static data member with a parenthesized
20987 initializer; only the "=" form of initialization is
20988 permitted.) */
20989 if (!member_p)
20990 cp_parser_parse_tentatively (parser);
20991
20992 /* Consume the `('. */
20993 matching_parens parens;
20994 parens.consume_open (parser);
20995 if (first)
20996 {
20997 /* If this is going to be an abstract declarator, we're
20998 in a declarator and we can't have default args. */
20999 parser->default_arg_ok_p = false;
21000 parser->in_declarator_p = true;
21001 }
21002
21003 begin_scope (sk_function_parms, NULL_TREE);
21004
21005 /* Signal we are in the immediate function context. */
21006 if (flags & CP_PARSER_FLAGS_CONSTEVAL)
21007 current_binding_level->immediate_fn_ctx_p = true;
21008
21009 /* Parse the parameter-declaration-clause. */
21010 params
21011 = cp_parser_parameter_declaration_clause (parser, flags);
21012
21013 /* Consume the `)'. */
21014 parens.require_close (parser);
21015
21016 /* If all went well, parse the cv-qualifier-seq,
21017 ref-qualifier and the exception-specification. */
21018 if (member_p || cp_parser_parse_definitely (parser))
21019 {
21020 cp_cv_quals cv_quals;
21021 cp_virt_specifiers virt_specifiers;
21022 cp_ref_qualifier ref_qual;
21023 tree exception_specification;
21024 tree late_return;
21025 tree attrs;
21026 bool memfn = (member_p || (pushed_scope
21027 && CLASS_TYPE_P (pushed_scope)));
21028 unsigned char local_variables_forbidden_p
21029 = parser->local_variables_forbidden_p;
21030 /* 'this' is not allowed in static member functions. */
21031 if (static_p || friend_p)
21032 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
21033
21034 is_declarator = true;
21035
21036 if (ctor_dtor_or_conv_p)
21037 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
21038 first = false;
21039
21040 /* Parse the cv-qualifier-seq. */
21041 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21042 /* Parse the ref-qualifier. */
21043 ref_qual = cp_parser_ref_qualifier_opt (parser);
21044 /* Parse the tx-qualifier. */
21045 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
21046 /* And the exception-specification. */
21047 exception_specification
21048 = cp_parser_exception_specification_opt (parser, flags);
21049
21050 attrs = cp_parser_std_attribute_spec_seq (parser);
21051
21052 /* In here, we handle cases where attribute is used after
21053 the function declaration. For example:
21054 void func (int x) __attribute__((vector(..))); */
21055 tree gnu_attrs = NULL_TREE;
21056 tree requires_clause = NULL_TREE;
21057 late_return = (cp_parser_late_return_type_opt
21058 (parser, declarator, requires_clause,
21059 memfn ? cv_quals : -1));
21060
21061 /* Parse the virt-specifier-seq. */
21062 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21063
21064 /* Create the function-declarator. */
21065 declarator = make_call_declarator (declarator,
21066 params,
21067 cv_quals,
21068 virt_specifiers,
21069 ref_qual,
21070 tx_qual,
21071 exception_specification,
21072 late_return,
21073 requires_clause);
21074 declarator->std_attributes = attrs;
21075 declarator->attributes = gnu_attrs;
21076 /* Any subsequent parameter lists are to do with
21077 return type, so are not those of the declared
21078 function. */
21079 parser->default_arg_ok_p = false;
21080
21081 /* Restore the state of local_variables_forbidden_p. */
21082 parser->local_variables_forbidden_p
21083 = local_variables_forbidden_p;
21084 }
21085
21086 /* Remove the function parms from scope. */
21087 pop_bindings_and_leave_scope ();
21088
21089 if (is_declarator)
21090 /* Repeat the main loop. */
21091 continue;
21092 }
21093
21094 /* If this is the first, we can try a parenthesized
21095 declarator. */
21096 if (first)
21097 {
21098 bool saved_in_type_id_in_expr_p;
21099
21100 parser->default_arg_ok_p = saved_default_arg_ok_p;
21101 parser->in_declarator_p = saved_in_declarator_p;
21102
21103 open_paren = token;
21104 /* Consume the `('. */
21105 matching_parens parens;
21106 parens.consume_open (parser);
21107 /* Parse the nested declarator. */
21108 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21109 parser->in_type_id_in_expr_p = true;
21110 declarator
21111 = cp_parser_declarator (parser, dcl_kind, flags,
21112 ctor_dtor_or_conv_p,
21113 /*parenthesized_p=*/NULL,
21114 member_p, friend_p,
21115 /*static_p=*/false);
21116 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21117 first = false;
21118 /* Expect a `)'. */
21119 close_paren = cp_lexer_peek_token (parser->lexer);
21120 if (!parens.require_close (parser))
21121 declarator = cp_error_declarator;
21122 if (declarator == cp_error_declarator)
21123 break;
21124
21125 goto handle_declarator;
21126 }
21127 /* Otherwise, we must be done. */
21128 else
21129 break;
21130 }
21131 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
21132 && token->type == CPP_OPEN_SQUARE
21133 && !cp_next_tokens_can_be_attribute_p (parser))
21134 {
21135 /* Parse an array-declarator. */
21136 tree bounds, attrs;
21137
21138 if (ctor_dtor_or_conv_p)
21139 *ctor_dtor_or_conv_p = 0;
21140
21141 open_paren = NULL;
21142 first = false;
21143 parser->default_arg_ok_p = false;
21144 parser->in_declarator_p = true;
21145 /* Consume the `['. */
21146 cp_lexer_consume_token (parser->lexer);
21147 /* Peek at the next token. */
21148 token = cp_lexer_peek_token (parser->lexer);
21149 /* If the next token is `]', then there is no
21150 constant-expression. */
21151 if (token->type != CPP_CLOSE_SQUARE)
21152 {
21153 bool non_constant_p;
21154 bounds
21155 = cp_parser_constant_expression (parser,
21156 /*allow_non_constant=*/true,
21157 &non_constant_p);
21158 if (!non_constant_p)
21159 /* OK */;
21160 else if (error_operand_p (bounds))
21161 /* Already gave an error. */;
21162 else if (!parser->in_function_body
21163 || current_binding_level->kind == sk_function_parms)
21164 {
21165 /* Normally, the array bound must be an integral constant
21166 expression. However, as an extension, we allow VLAs
21167 in function scopes as long as they aren't part of a
21168 parameter declaration. */
21169 cp_parser_error (parser,
21170 "array bound is not an integer constant");
21171 bounds = error_mark_node;
21172 }
21173 else if (processing_template_decl
21174 && !type_dependent_expression_p (bounds))
21175 {
21176 /* Remember this wasn't a constant-expression. */
21177 bounds = build_nop (TREE_TYPE (bounds), bounds);
21178 TREE_SIDE_EFFECTS (bounds) = 1;
21179 }
21180 }
21181 else
21182 bounds = NULL_TREE;
21183 /* Look for the closing `]'. */
21184 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21185 {
21186 declarator = cp_error_declarator;
21187 break;
21188 }
21189
21190 attrs = cp_parser_std_attribute_spec_seq (parser);
21191 declarator = make_array_declarator (declarator, bounds);
21192 declarator->std_attributes = attrs;
21193 }
21194 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
21195 {
21196 {
21197 tree qualifying_scope;
21198 tree unqualified_name;
21199 tree attrs;
21200 special_function_kind sfk;
21201 bool abstract_ok;
21202 bool pack_expansion_p = false;
21203 cp_token *declarator_id_start_token;
21204
21205 /* Parse a declarator-id */
21206 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
21207 if (abstract_ok)
21208 {
21209 cp_parser_parse_tentatively (parser);
21210
21211 /* If we see an ellipsis, we should be looking at a
21212 parameter pack. */
21213 if (token->type == CPP_ELLIPSIS)
21214 {
21215 /* Consume the `...' */
21216 cp_lexer_consume_token (parser->lexer);
21217
21218 pack_expansion_p = true;
21219 }
21220 }
21221
21222 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
21223 unqualified_name
21224 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
21225 qualifying_scope = parser->scope;
21226 if (abstract_ok)
21227 {
21228 bool okay = false;
21229
21230 if (!unqualified_name && pack_expansion_p)
21231 {
21232 /* Check whether an error occurred. */
21233 okay = !cp_parser_error_occurred (parser);
21234
21235 /* We already consumed the ellipsis to mark a
21236 parameter pack, but we have no way to report it,
21237 so abort the tentative parse. We will be exiting
21238 immediately anyway. */
21239 cp_parser_abort_tentative_parse (parser);
21240 }
21241 else
21242 okay = cp_parser_parse_definitely (parser);
21243
21244 if (!okay)
21245 unqualified_name = error_mark_node;
21246 else if (unqualified_name
21247 && (qualifying_scope
21248 || (!identifier_p (unqualified_name))))
21249 {
21250 cp_parser_error (parser, "expected unqualified-id");
21251 unqualified_name = error_mark_node;
21252 }
21253 }
21254
21255 if (!unqualified_name)
21256 return NULL;
21257 if (unqualified_name == error_mark_node)
21258 {
21259 declarator = cp_error_declarator;
21260 pack_expansion_p = false;
21261 declarator->parameter_pack_p = false;
21262 break;
21263 }
21264
21265 attrs = cp_parser_std_attribute_spec_seq (parser);
21266
21267 if (qualifying_scope && at_namespace_scope_p ()
21268 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
21269 {
21270 /* In the declaration of a member of a template class
21271 outside of the class itself, the SCOPE will sometimes
21272 be a TYPENAME_TYPE. For example, given:
21273
21274 template <typename T>
21275 int S<T>::R::i = 3;
21276
21277 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
21278 this context, we must resolve S<T>::R to an ordinary
21279 type, rather than a typename type.
21280
21281 The reason we normally avoid resolving TYPENAME_TYPEs
21282 is that a specialization of `S' might render
21283 `S<T>::R' not a type. However, if `S' is
21284 specialized, then this `i' will not be used, so there
21285 is no harm in resolving the types here. */
21286 tree type;
21287
21288 /* Resolve the TYPENAME_TYPE. */
21289 type = resolve_typename_type (qualifying_scope,
21290 /*only_current_p=*/false);
21291 /* If that failed, the declarator is invalid. */
21292 if (TREE_CODE (type) == TYPENAME_TYPE)
21293 {
21294 if (typedef_variant_p (type))
21295 error_at (declarator_id_start_token->location,
21296 "cannot define member of dependent typedef "
21297 "%qT", type);
21298 else
21299 error_at (declarator_id_start_token->location,
21300 "%<%T::%E%> is not a type",
21301 TYPE_CONTEXT (qualifying_scope),
21302 TYPE_IDENTIFIER (qualifying_scope));
21303 }
21304 qualifying_scope = type;
21305 }
21306
21307 sfk = sfk_none;
21308
21309 if (unqualified_name)
21310 {
21311 tree class_type;
21312
21313 if (qualifying_scope
21314 && CLASS_TYPE_P (qualifying_scope))
21315 class_type = qualifying_scope;
21316 else
21317 class_type = current_class_type;
21318
21319 if (TREE_CODE (unqualified_name) == TYPE_DECL)
21320 {
21321 tree name_type = TREE_TYPE (unqualified_name);
21322
21323 if (!class_type || !same_type_p (name_type, class_type))
21324 {
21325 /* We do not attempt to print the declarator
21326 here because we do not have enough
21327 information about its original syntactic
21328 form. */
21329 cp_parser_error (parser, "invalid declarator");
21330 declarator = cp_error_declarator;
21331 break;
21332 }
21333 else if (qualifying_scope
21334 && CLASSTYPE_USE_TEMPLATE (name_type))
21335 {
21336 error_at (declarator_id_start_token->location,
21337 "invalid use of constructor as a template");
21338 inform (declarator_id_start_token->location,
21339 "use %<%T::%D%> instead of %<%T::%D%> to "
21340 "name the constructor in a qualified name",
21341 class_type,
21342 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
21343 class_type, name_type);
21344 declarator = cp_error_declarator;
21345 break;
21346 }
21347 unqualified_name = constructor_name (class_type);
21348 }
21349
21350 if (class_type)
21351 {
21352 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
21353 sfk = sfk_destructor;
21354 else if (identifier_p (unqualified_name)
21355 && IDENTIFIER_CONV_OP_P (unqualified_name))
21356 sfk = sfk_conversion;
21357 else if (/* There's no way to declare a constructor
21358 for an unnamed type, even if the type
21359 got a name for linkage purposes. */
21360 !TYPE_WAS_UNNAMED (class_type)
21361 /* Handle correctly (c++/19200):
21362
21363 struct S {
21364 struct T{};
21365 friend void S(T);
21366 };
21367
21368 and also:
21369
21370 namespace N {
21371 void S();
21372 }
21373
21374 struct S {
21375 friend void N::S();
21376 }; */
21377 && (!friend_p || class_type == qualifying_scope)
21378 && constructor_name_p (unqualified_name,
21379 class_type))
21380 sfk = sfk_constructor;
21381 else if (is_overloaded_fn (unqualified_name)
21382 && DECL_CONSTRUCTOR_P (get_first_fn
21383 (unqualified_name)))
21384 sfk = sfk_constructor;
21385
21386 if (ctor_dtor_or_conv_p && sfk != sfk_none)
21387 *ctor_dtor_or_conv_p = -1;
21388 }
21389 }
21390 declarator = make_id_declarator (qualifying_scope,
21391 unqualified_name,
21392 sfk, token->location);
21393 declarator->std_attributes = attrs;
21394 declarator->parameter_pack_p = pack_expansion_p;
21395
21396 if (pack_expansion_p)
21397 maybe_warn_variadic_templates ();
21398
21399 /* We're looking for this case in [temp.res]:
21400 A qualified-id is assumed to name a type if [...]
21401 - it is a decl-specifier of the decl-specifier-seq of a
21402 parameter-declaration in a declarator of a function or
21403 function template declaration, ... */
21404 if (cxx_dialect >= cxx2a
21405 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
21406 && declarator->kind == cdk_id
21407 && !at_class_scope_p ()
21408 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21409 {
21410 /* ...whose declarator-id is qualified. If it isn't, never
21411 assume the parameters to refer to types. */
21412 if (qualifying_scope == NULL_TREE)
21413 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21414 else
21415 {
21416 /* Now we have something like
21417 template <typename T> int C::x(S::p);
21418 which can be a function template declaration or a
21419 variable template definition. If name lookup for
21420 the declarator-id C::x finds one or more function
21421 templates, assume S::p to name a type. Otherwise,
21422 don't. */
21423 tree decl
21424 = cp_parser_lookup_name_simple (parser, unqualified_name,
21425 token->location);
21426 if (!is_overloaded_fn (decl)
21427 /* Allow
21428 template<typename T>
21429 A<T>::A(T::type) { } */
21430 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
21431 && constructor_name_p (unqualified_name,
21432 qualifying_scope)))
21433 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21434 }
21435 }
21436 }
21437
21438 handle_declarator:;
21439 scope = get_scope_of_declarator (declarator);
21440 if (scope)
21441 {
21442 /* Any names that appear after the declarator-id for a
21443 member are looked up in the containing scope. */
21444 if (at_function_scope_p ())
21445 {
21446 /* But declarations with qualified-ids can't appear in a
21447 function. */
21448 cp_parser_error (parser, "qualified-id in declaration");
21449 declarator = cp_error_declarator;
21450 break;
21451 }
21452 pushed_scope = push_scope (scope);
21453 }
21454 parser->in_declarator_p = true;
21455 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21456 || (declarator && declarator->kind == cdk_id))
21457 /* Default args are only allowed on function
21458 declarations. */
21459 parser->default_arg_ok_p = saved_default_arg_ok_p;
21460 else
21461 parser->default_arg_ok_p = false;
21462
21463 first = false;
21464 }
21465 /* We're done. */
21466 else
21467 break;
21468 }
21469
21470 /* For an abstract declarator, we might wind up with nothing at this
21471 point. That's an error; the declarator is not optional. */
21472 if (!declarator)
21473 cp_parser_error (parser, "expected declarator");
21474 else if (open_paren)
21475 {
21476 /* Record overly parenthesized declarator so we can give a
21477 diagnostic about confusing decl/expr disambiguation. */
21478 if (declarator->kind == cdk_array)
21479 {
21480 /* If the open and close parens are on different lines, this
21481 is probably a formatting thing, so ignore. */
21482 expanded_location open = expand_location (open_paren->location);
21483 expanded_location close = expand_location (close_paren->location);
21484 if (open.line != close.line || open.file != close.file)
21485 open_paren = NULL;
21486 }
21487 if (open_paren)
21488 declarator->parenthesized = open_paren->location;
21489 }
21490
21491 /* If we entered a scope, we must exit it now. */
21492 if (pushed_scope)
21493 pop_scope (pushed_scope);
21494
21495 parser->default_arg_ok_p = saved_default_arg_ok_p;
21496 parser->in_declarator_p = saved_in_declarator_p;
21497
21498 return declarator;
21499 }
21500
21501 /* Parse a ptr-operator.
21502
21503 ptr-operator:
21504 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21505 * cv-qualifier-seq [opt]
21506 &
21507 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21508 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21509
21510 GNU Extension:
21511
21512 ptr-operator:
21513 & cv-qualifier-seq [opt]
21514
21515 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21516 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21517 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21518 filled in with the TYPE containing the member. *CV_QUALS is
21519 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21520 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21521 Note that the tree codes returned by this function have nothing
21522 to do with the types of trees that will be eventually be created
21523 to represent the pointer or reference type being parsed. They are
21524 just constants with suggestive names. */
21525 static enum tree_code
21526 cp_parser_ptr_operator (cp_parser* parser,
21527 tree* type,
21528 cp_cv_quals *cv_quals,
21529 tree *attributes)
21530 {
21531 enum tree_code code = ERROR_MARK;
21532 cp_token *token;
21533 tree attrs = NULL_TREE;
21534
21535 /* Assume that it's not a pointer-to-member. */
21536 *type = NULL_TREE;
21537 /* And that there are no cv-qualifiers. */
21538 *cv_quals = TYPE_UNQUALIFIED;
21539
21540 /* Peek at the next token. */
21541 token = cp_lexer_peek_token (parser->lexer);
21542
21543 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21544 if (token->type == CPP_MULT)
21545 code = INDIRECT_REF;
21546 else if (token->type == CPP_AND)
21547 code = ADDR_EXPR;
21548 else if ((cxx_dialect != cxx98) &&
21549 token->type == CPP_AND_AND) /* C++0x only */
21550 code = NON_LVALUE_EXPR;
21551
21552 if (code != ERROR_MARK)
21553 {
21554 /* Consume the `*', `&' or `&&'. */
21555 cp_lexer_consume_token (parser->lexer);
21556
21557 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21558 `&', if we are allowing GNU extensions. (The only qualifier
21559 that can legally appear after `&' is `restrict', but that is
21560 enforced during semantic analysis. */
21561 if (code == INDIRECT_REF
21562 || cp_parser_allow_gnu_extensions_p (parser))
21563 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21564
21565 attrs = cp_parser_std_attribute_spec_seq (parser);
21566 if (attributes != NULL)
21567 *attributes = attrs;
21568 }
21569 else
21570 {
21571 /* Try the pointer-to-member case. */
21572 cp_parser_parse_tentatively (parser);
21573 /* Look for the optional `::' operator. */
21574 cp_parser_global_scope_opt (parser,
21575 /*current_scope_valid_p=*/false);
21576 /* Look for the nested-name specifier. */
21577 token = cp_lexer_peek_token (parser->lexer);
21578 cp_parser_nested_name_specifier (parser,
21579 /*typename_keyword_p=*/false,
21580 /*check_dependency_p=*/true,
21581 /*type_p=*/false,
21582 /*is_declaration=*/false);
21583 /* If we found it, and the next token is a `*', then we are
21584 indeed looking at a pointer-to-member operator. */
21585 if (!cp_parser_error_occurred (parser)
21586 && cp_parser_require (parser, CPP_MULT, RT_MULT))
21587 {
21588 /* Indicate that the `*' operator was used. */
21589 code = INDIRECT_REF;
21590
21591 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21592 error_at (token->location, "%qD is a namespace", parser->scope);
21593 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21594 error_at (token->location, "cannot form pointer to member of "
21595 "non-class %q#T", parser->scope);
21596 else
21597 {
21598 /* The type of which the member is a member is given by the
21599 current SCOPE. */
21600 *type = parser->scope;
21601 /* The next name will not be qualified. */
21602 parser->scope = NULL_TREE;
21603 parser->qualifying_scope = NULL_TREE;
21604 parser->object_scope = NULL_TREE;
21605 /* Look for optional c++11 attributes. */
21606 attrs = cp_parser_std_attribute_spec_seq (parser);
21607 if (attributes != NULL)
21608 *attributes = attrs;
21609 /* Look for the optional cv-qualifier-seq. */
21610 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21611 }
21612 }
21613 /* If that didn't work we don't have a ptr-operator. */
21614 if (!cp_parser_parse_definitely (parser))
21615 cp_parser_error (parser, "expected ptr-operator");
21616 }
21617
21618 return code;
21619 }
21620
21621 /* Parse an (optional) cv-qualifier-seq.
21622
21623 cv-qualifier-seq:
21624 cv-qualifier cv-qualifier-seq [opt]
21625
21626 cv-qualifier:
21627 const
21628 volatile
21629
21630 GNU Extension:
21631
21632 cv-qualifier:
21633 __restrict__
21634
21635 Returns a bitmask representing the cv-qualifiers. */
21636
21637 static cp_cv_quals
21638 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21639 {
21640 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21641
21642 while (true)
21643 {
21644 cp_token *token;
21645 cp_cv_quals cv_qualifier;
21646
21647 /* Peek at the next token. */
21648 token = cp_lexer_peek_token (parser->lexer);
21649 /* See if it's a cv-qualifier. */
21650 switch (token->keyword)
21651 {
21652 case RID_CONST:
21653 cv_qualifier = TYPE_QUAL_CONST;
21654 break;
21655
21656 case RID_VOLATILE:
21657 cv_qualifier = TYPE_QUAL_VOLATILE;
21658 break;
21659
21660 case RID_RESTRICT:
21661 cv_qualifier = TYPE_QUAL_RESTRICT;
21662 break;
21663
21664 default:
21665 cv_qualifier = TYPE_UNQUALIFIED;
21666 break;
21667 }
21668
21669 if (!cv_qualifier)
21670 break;
21671
21672 if (cv_quals & cv_qualifier)
21673 {
21674 gcc_rich_location richloc (token->location);
21675 richloc.add_fixit_remove ();
21676 error_at (&richloc, "duplicate cv-qualifier");
21677 cp_lexer_purge_token (parser->lexer);
21678 }
21679 else
21680 {
21681 cp_lexer_consume_token (parser->lexer);
21682 cv_quals |= cv_qualifier;
21683 }
21684 }
21685
21686 return cv_quals;
21687 }
21688
21689 /* Parse an (optional) ref-qualifier
21690
21691 ref-qualifier:
21692 &
21693 &&
21694
21695 Returns cp_ref_qualifier representing ref-qualifier. */
21696
21697 static cp_ref_qualifier
21698 cp_parser_ref_qualifier_opt (cp_parser* parser)
21699 {
21700 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21701
21702 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21703 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21704 return ref_qual;
21705
21706 while (true)
21707 {
21708 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21709 cp_token *token = cp_lexer_peek_token (parser->lexer);
21710
21711 switch (token->type)
21712 {
21713 case CPP_AND:
21714 curr_ref_qual = REF_QUAL_LVALUE;
21715 break;
21716
21717 case CPP_AND_AND:
21718 curr_ref_qual = REF_QUAL_RVALUE;
21719 break;
21720
21721 default:
21722 curr_ref_qual = REF_QUAL_NONE;
21723 break;
21724 }
21725
21726 if (!curr_ref_qual)
21727 break;
21728 else if (ref_qual)
21729 {
21730 error_at (token->location, "multiple ref-qualifiers");
21731 cp_lexer_purge_token (parser->lexer);
21732 }
21733 else
21734 {
21735 ref_qual = curr_ref_qual;
21736 cp_lexer_consume_token (parser->lexer);
21737 }
21738 }
21739
21740 return ref_qual;
21741 }
21742
21743 /* Parse an optional tx-qualifier.
21744
21745 tx-qualifier:
21746 transaction_safe
21747 transaction_safe_dynamic */
21748
21749 static tree
21750 cp_parser_tx_qualifier_opt (cp_parser *parser)
21751 {
21752 cp_token *token = cp_lexer_peek_token (parser->lexer);
21753 if (token->type == CPP_NAME)
21754 {
21755 tree name = token->u.value;
21756 const char *p = IDENTIFIER_POINTER (name);
21757 const int len = strlen ("transaction_safe");
21758 if (!strncmp (p, "transaction_safe", len))
21759 {
21760 p += len;
21761 if (*p == '\0'
21762 || !strcmp (p, "_dynamic"))
21763 {
21764 cp_lexer_consume_token (parser->lexer);
21765 if (!flag_tm)
21766 {
21767 error ("%qE requires %<-fgnu-tm%>", name);
21768 return NULL_TREE;
21769 }
21770 else
21771 return name;
21772 }
21773 }
21774 }
21775 return NULL_TREE;
21776 }
21777
21778 /* Parse an (optional) virt-specifier-seq.
21779
21780 virt-specifier-seq:
21781 virt-specifier virt-specifier-seq [opt]
21782
21783 virt-specifier:
21784 override
21785 final
21786
21787 Returns a bitmask representing the virt-specifiers. */
21788
21789 static cp_virt_specifiers
21790 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21791 {
21792 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21793
21794 while (true)
21795 {
21796 cp_token *token;
21797 cp_virt_specifiers virt_specifier;
21798
21799 /* Peek at the next token. */
21800 token = cp_lexer_peek_token (parser->lexer);
21801 /* See if it's a virt-specifier-qualifier. */
21802 if (token->type != CPP_NAME)
21803 break;
21804 if (id_equal (token->u.value, "override"))
21805 {
21806 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21807 virt_specifier = VIRT_SPEC_OVERRIDE;
21808 }
21809 else if (id_equal (token->u.value, "final"))
21810 {
21811 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21812 virt_specifier = VIRT_SPEC_FINAL;
21813 }
21814 else if (id_equal (token->u.value, "__final"))
21815 {
21816 virt_specifier = VIRT_SPEC_FINAL;
21817 }
21818 else
21819 break;
21820
21821 if (virt_specifiers & virt_specifier)
21822 {
21823 gcc_rich_location richloc (token->location);
21824 richloc.add_fixit_remove ();
21825 error_at (&richloc, "duplicate virt-specifier");
21826 cp_lexer_purge_token (parser->lexer);
21827 }
21828 else
21829 {
21830 cp_lexer_consume_token (parser->lexer);
21831 virt_specifiers |= virt_specifier;
21832 }
21833 }
21834 return virt_specifiers;
21835 }
21836
21837 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21838 is in scope even though it isn't real. */
21839
21840 void
21841 inject_this_parameter (tree ctype, cp_cv_quals quals)
21842 {
21843 tree this_parm;
21844
21845 if (current_class_ptr)
21846 {
21847 /* We don't clear this between NSDMIs. Is it already what we want? */
21848 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21849 if (DECL_P (current_class_ptr)
21850 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21851 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21852 && cp_type_quals (type) == quals)
21853 return;
21854 }
21855
21856 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21857 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21858 current_class_ptr = NULL_TREE;
21859 current_class_ref
21860 = cp_build_fold_indirect_ref (this_parm);
21861 current_class_ptr = this_parm;
21862 }
21863
21864 /* Return true iff our current scope is a non-static data member
21865 initializer. */
21866
21867 bool
21868 parsing_nsdmi (void)
21869 {
21870 /* We recognize NSDMI context by the context-less 'this' pointer set up
21871 by the function above. */
21872 if (current_class_ptr
21873 && TREE_CODE (current_class_ptr) == PARM_DECL
21874 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21875 return true;
21876 return false;
21877 }
21878
21879 /* Parse a late-specified return type, if any. This is not a separate
21880 non-terminal, but part of a function declarator, which looks like
21881
21882 -> trailing-type-specifier-seq abstract-declarator(opt)
21883
21884 Returns the type indicated by the type-id.
21885
21886 In addition to this, parse any queued up #pragma omp declare simd
21887 clauses, and #pragma acc routine clauses.
21888
21889 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21890 function. */
21891
21892 static tree
21893 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21894 tree& requires_clause, cp_cv_quals quals)
21895 {
21896 cp_token *token;
21897 tree type = NULL_TREE;
21898 bool declare_simd_p = (parser->omp_declare_simd
21899 && declarator
21900 && declarator->kind == cdk_id);
21901
21902 bool oacc_routine_p = (parser->oacc_routine
21903 && declarator
21904 && declarator->kind == cdk_id);
21905
21906 /* Peek at the next token. */
21907 token = cp_lexer_peek_token (parser->lexer);
21908 /* A late-specified return type is indicated by an initial '->'. */
21909 if (token->type != CPP_DEREF
21910 && token->keyword != RID_REQUIRES
21911 && !(token->type == CPP_NAME
21912 && token->u.value == ridpointers[RID_REQUIRES])
21913 && !(declare_simd_p || oacc_routine_p))
21914 return NULL_TREE;
21915
21916 tree save_ccp = current_class_ptr;
21917 tree save_ccr = current_class_ref;
21918 if (quals >= 0)
21919 {
21920 /* DR 1207: 'this' is in scope in the trailing return type. */
21921 inject_this_parameter (current_class_type, quals);
21922 }
21923
21924 if (token->type == CPP_DEREF)
21925 {
21926 /* Consume the ->. */
21927 cp_lexer_consume_token (parser->lexer);
21928
21929 type = cp_parser_trailing_type_id (parser);
21930 }
21931
21932 /* Function declarations may be followed by a trailing
21933 requires-clause. */
21934 requires_clause = cp_parser_requires_clause_opt (parser, false);
21935
21936 if (declare_simd_p)
21937 declarator->attributes
21938 = cp_parser_late_parsing_omp_declare_simd (parser,
21939 declarator->attributes);
21940 if (oacc_routine_p)
21941 declarator->attributes
21942 = cp_parser_late_parsing_oacc_routine (parser,
21943 declarator->attributes);
21944
21945 if (quals >= 0)
21946 {
21947 current_class_ptr = save_ccp;
21948 current_class_ref = save_ccr;
21949 }
21950
21951 return type;
21952 }
21953
21954 /* Parse a declarator-id.
21955
21956 declarator-id:
21957 id-expression
21958 :: [opt] nested-name-specifier [opt] type-name
21959
21960 In the `id-expression' case, the value returned is as for
21961 cp_parser_id_expression if the id-expression was an unqualified-id.
21962 If the id-expression was a qualified-id, then a SCOPE_REF is
21963 returned. The first operand is the scope (either a NAMESPACE_DECL
21964 or TREE_TYPE), but the second is still just a representation of an
21965 unqualified-id. */
21966
21967 static tree
21968 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21969 {
21970 tree id;
21971 /* The expression must be an id-expression. Assume that qualified
21972 names are the names of types so that:
21973
21974 template <class T>
21975 int S<T>::R::i = 3;
21976
21977 will work; we must treat `S<T>::R' as the name of a type.
21978 Similarly, assume that qualified names are templates, where
21979 required, so that:
21980
21981 template <class T>
21982 int S<T>::R<T>::i = 3;
21983
21984 will work, too. */
21985 id = cp_parser_id_expression (parser,
21986 /*template_keyword_p=*/false,
21987 /*check_dependency_p=*/false,
21988 /*template_p=*/NULL,
21989 /*declarator_p=*/true,
21990 optional_p);
21991 if (id && BASELINK_P (id))
21992 id = BASELINK_FUNCTIONS (id);
21993 return id;
21994 }
21995
21996 /* Parse a type-id.
21997
21998 type-id:
21999 type-specifier-seq abstract-declarator [opt]
22000
22001 The parser flags FLAGS is used to control type-specifier parsing.
22002
22003 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
22004
22005 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
22006 i.e. we've just seen "->".
22007
22008 Returns the TYPE specified. */
22009
22010 static tree
22011 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
22012 bool is_template_arg, bool is_trailing_return,
22013 location_t *type_location)
22014 {
22015 cp_decl_specifier_seq type_specifier_seq;
22016 cp_declarator *abstract_declarator;
22017
22018 /* Parse the type-specifier-seq. */
22019 cp_parser_type_specifier_seq (parser, flags,
22020 /*is_declaration=*/false,
22021 is_trailing_return,
22022 &type_specifier_seq);
22023 if (type_location)
22024 *type_location = type_specifier_seq.locations[ds_type_spec];
22025
22026 if (is_template_arg && type_specifier_seq.type
22027 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
22028 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
22029 /* A bare template name as a template argument is a template template
22030 argument, not a placeholder, so fail parsing it as a type argument. */
22031 {
22032 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
22033 cp_parser_simulate_error (parser);
22034 return error_mark_node;
22035 }
22036 if (type_specifier_seq.type == error_mark_node)
22037 return error_mark_node;
22038
22039 /* There might or might not be an abstract declarator. */
22040 cp_parser_parse_tentatively (parser);
22041 /* Look for the declarator. */
22042 abstract_declarator
22043 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
22044 CP_PARSER_FLAGS_NONE, NULL,
22045 /*parenthesized_p=*/NULL,
22046 /*member_p=*/false,
22047 /*friend_p=*/false,
22048 /*static_p=*/false);
22049 /* Check to see if there really was a declarator. */
22050 if (!cp_parser_parse_definitely (parser))
22051 abstract_declarator = NULL;
22052
22053 if (type_specifier_seq.type
22054 /* The concepts TS allows 'auto' as a type-id. */
22055 && (!flag_concepts || parser->in_type_id_in_expr_p)
22056 /* None of the valid uses of 'auto' in C++14 involve the type-id
22057 nonterminal, but it is valid in a trailing-return-type. */
22058 && !(cxx_dialect >= cxx14 && is_trailing_return))
22059 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
22060 {
22061 /* A type-id with type 'auto' is only ok if the abstract declarator
22062 is a function declarator with a late-specified return type.
22063
22064 A type-id with 'auto' is also valid in a trailing-return-type
22065 in a compound-requirement. */
22066 if (abstract_declarator
22067 && abstract_declarator->kind == cdk_function
22068 && abstract_declarator->u.function.late_return_type)
22069 /* OK */;
22070 else if (parser->in_result_type_constraint_p)
22071 /* OK */;
22072 else
22073 {
22074 location_t loc = type_specifier_seq.locations[ds_type_spec];
22075 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
22076 {
22077 error_at (loc, "missing template arguments after %qT",
22078 auto_node);
22079 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
22080 tmpl);
22081 }
22082 else
22083 error_at (loc, "invalid use of %qT", auto_node);
22084 return error_mark_node;
22085 }
22086 }
22087
22088 return groktypename (&type_specifier_seq, abstract_declarator,
22089 is_template_arg);
22090 }
22091
22092 /* Wrapper for cp_parser_type_id_1. */
22093
22094 static tree
22095 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
22096 location_t *type_location)
22097 {
22098 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
22099 }
22100
22101 /* Wrapper for cp_parser_type_id_1. */
22102
22103 static tree
22104 cp_parser_template_type_arg (cp_parser *parser)
22105 {
22106 tree r;
22107 const char *saved_message = parser->type_definition_forbidden_message;
22108 parser->type_definition_forbidden_message
22109 = G_("types may not be defined in template arguments");
22110 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
22111 parser->type_definition_forbidden_message = saved_message;
22112 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
22113 {
22114 error ("invalid use of %<auto%> in template argument");
22115 r = error_mark_node;
22116 }
22117 return r;
22118 }
22119
22120 /* Wrapper for cp_parser_type_id_1. */
22121
22122 static tree
22123 cp_parser_trailing_type_id (cp_parser *parser)
22124 {
22125 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
22126 false, true, NULL);
22127 }
22128
22129 /* Parse a type-specifier-seq.
22130
22131 type-specifier-seq:
22132 type-specifier type-specifier-seq [opt]
22133
22134 GNU extension:
22135
22136 type-specifier-seq:
22137 attributes type-specifier-seq [opt]
22138
22139 The parser flags FLAGS is used to control type-specifier parsing.
22140
22141 If IS_DECLARATION is true, we are at the start of a "condition" or
22142 exception-declaration, so we might be followed by a declarator-id.
22143
22144 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
22145 i.e. we've just seen "->".
22146
22147 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
22148
22149 static void
22150 cp_parser_type_specifier_seq (cp_parser* parser,
22151 cp_parser_flags flags,
22152 bool is_declaration,
22153 bool is_trailing_return,
22154 cp_decl_specifier_seq *type_specifier_seq)
22155 {
22156 bool seen_type_specifier = false;
22157 cp_token *start_token = NULL;
22158
22159 /* Clear the TYPE_SPECIFIER_SEQ. */
22160 clear_decl_specs (type_specifier_seq);
22161
22162 flags |= CP_PARSER_FLAGS_OPTIONAL;
22163 /* In the context of a trailing return type, enum E { } is an
22164 elaborated-type-specifier followed by a function-body, not an
22165 enum-specifier. */
22166 if (is_trailing_return)
22167 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
22168
22169 /* Parse the type-specifiers and attributes. */
22170 while (true)
22171 {
22172 tree type_specifier;
22173 bool is_cv_qualifier;
22174
22175 /* Check for attributes first. */
22176 if (cp_next_tokens_can_be_attribute_p (parser))
22177 {
22178 type_specifier_seq->attributes
22179 = attr_chainon (type_specifier_seq->attributes,
22180 cp_parser_attributes_opt (parser));
22181 continue;
22182 }
22183
22184 /* record the token of the beginning of the type specifier seq,
22185 for error reporting purposes*/
22186 if (!start_token)
22187 start_token = cp_lexer_peek_token (parser->lexer);
22188
22189 /* Look for the type-specifier. */
22190 type_specifier = cp_parser_type_specifier (parser,
22191 flags,
22192 type_specifier_seq,
22193 /*is_declaration=*/false,
22194 NULL,
22195 &is_cv_qualifier);
22196 if (!type_specifier)
22197 {
22198 /* If the first type-specifier could not be found, this is not a
22199 type-specifier-seq at all. */
22200 if (!seen_type_specifier)
22201 {
22202 /* Set in_declarator_p to avoid skipping to the semicolon. */
22203 int in_decl = parser->in_declarator_p;
22204 parser->in_declarator_p = true;
22205
22206 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
22207 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
22208 cp_parser_error (parser, "expected type-specifier");
22209
22210 parser->in_declarator_p = in_decl;
22211
22212 type_specifier_seq->type = error_mark_node;
22213 return;
22214 }
22215 /* If subsequent type-specifiers could not be found, the
22216 type-specifier-seq is complete. */
22217 break;
22218 }
22219
22220 seen_type_specifier = true;
22221 /* The standard says that a condition can be:
22222
22223 type-specifier-seq declarator = assignment-expression
22224
22225 However, given:
22226
22227 struct S {};
22228 if (int S = ...)
22229
22230 we should treat the "S" as a declarator, not as a
22231 type-specifier. The standard doesn't say that explicitly for
22232 type-specifier-seq, but it does say that for
22233 decl-specifier-seq in an ordinary declaration. Perhaps it
22234 would be clearer just to allow a decl-specifier-seq here, and
22235 then add a semantic restriction that if any decl-specifiers
22236 that are not type-specifiers appear, the program is invalid. */
22237 if (is_declaration && !is_cv_qualifier)
22238 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
22239 }
22240 }
22241
22242 /* Return whether the function currently being declared has an associated
22243 template parameter list. */
22244
22245 static bool
22246 function_being_declared_is_template_p (cp_parser* parser)
22247 {
22248 if (!current_template_parms || processing_template_parmlist)
22249 return false;
22250
22251 if (parser->implicit_template_scope)
22252 return true;
22253
22254 if (at_class_scope_p ()
22255 && TYPE_BEING_DEFINED (current_class_type))
22256 return parser->num_template_parameter_lists != 0;
22257
22258 return ((int) parser->num_template_parameter_lists > template_class_depth
22259 (current_class_type));
22260 }
22261
22262 /* Parse a parameter-declaration-clause.
22263
22264 parameter-declaration-clause:
22265 parameter-declaration-list [opt] ... [opt]
22266 parameter-declaration-list , ...
22267
22268 The parser flags FLAGS is used to control type-specifier parsing.
22269
22270 Returns a representation for the parameter declarations. A return
22271 value of NULL indicates a parameter-declaration-clause consisting
22272 only of an ellipsis. */
22273
22274 static tree
22275 cp_parser_parameter_declaration_clause (cp_parser* parser,
22276 cp_parser_flags flags)
22277 {
22278 tree parameters;
22279 cp_token *token;
22280 bool ellipsis_p;
22281
22282 temp_override<bool> cleanup
22283 (parser->auto_is_implicit_function_template_parm_p);
22284
22285 if (!processing_specialization
22286 && !processing_template_parmlist
22287 && !processing_explicit_instantiation
22288 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22289 actual function or a random abstract declarator. */
22290 && parser->default_arg_ok_p)
22291 if (!current_function_decl
22292 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
22293 parser->auto_is_implicit_function_template_parm_p = true;
22294
22295 /* Peek at the next token. */
22296 token = cp_lexer_peek_token (parser->lexer);
22297 /* Check for trivial parameter-declaration-clauses. */
22298 if (token->type == CPP_ELLIPSIS)
22299 {
22300 /* Consume the `...' token. */
22301 cp_lexer_consume_token (parser->lexer);
22302 return NULL_TREE;
22303 }
22304 else if (token->type == CPP_CLOSE_PAREN)
22305 /* There are no parameters. */
22306 return void_list_node;
22307 /* Check for `(void)', too, which is a special case. */
22308 else if (token->keyword == RID_VOID
22309 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22310 == CPP_CLOSE_PAREN))
22311 {
22312 /* Consume the `void' token. */
22313 cp_lexer_consume_token (parser->lexer);
22314 /* There are no parameters. */
22315 return void_list_node;
22316 }
22317
22318 /* Parse the parameter-declaration-list. */
22319 parameters = cp_parser_parameter_declaration_list (parser, flags);
22320 /* If a parse error occurred while parsing the
22321 parameter-declaration-list, then the entire
22322 parameter-declaration-clause is erroneous. */
22323 if (parameters == error_mark_node)
22324 return NULL_TREE;
22325
22326 /* Peek at the next token. */
22327 token = cp_lexer_peek_token (parser->lexer);
22328 /* If it's a `,', the clause should terminate with an ellipsis. */
22329 if (token->type == CPP_COMMA)
22330 {
22331 /* Consume the `,'. */
22332 cp_lexer_consume_token (parser->lexer);
22333 /* Expect an ellipsis. */
22334 ellipsis_p
22335 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
22336 }
22337 /* It might also be `...' if the optional trailing `,' was
22338 omitted. */
22339 else if (token->type == CPP_ELLIPSIS)
22340 {
22341 /* Consume the `...' token. */
22342 cp_lexer_consume_token (parser->lexer);
22343 /* And remember that we saw it. */
22344 ellipsis_p = true;
22345 }
22346 else
22347 ellipsis_p = false;
22348
22349 /* Finish the parameter list. */
22350 if (!ellipsis_p)
22351 parameters = chainon (parameters, void_list_node);
22352
22353 return parameters;
22354 }
22355
22356 /* Parse a parameter-declaration-list.
22357
22358 parameter-declaration-list:
22359 parameter-declaration
22360 parameter-declaration-list , parameter-declaration
22361
22362 The parser flags FLAGS is used to control type-specifier parsing.
22363
22364 Returns a representation of the parameter-declaration-list, as for
22365 cp_parser_parameter_declaration_clause. However, the
22366 `void_list_node' is never appended to the list. */
22367
22368 static tree
22369 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
22370 {
22371 tree parameters = NULL_TREE;
22372 tree *tail = &parameters;
22373 bool saved_in_unbraced_linkage_specification_p;
22374 int index = 0;
22375
22376 /* The special considerations that apply to a function within an
22377 unbraced linkage specifications do not apply to the parameters
22378 to the function. */
22379 saved_in_unbraced_linkage_specification_p
22380 = parser->in_unbraced_linkage_specification_p;
22381 parser->in_unbraced_linkage_specification_p = false;
22382
22383 /* Look for more parameters. */
22384 while (true)
22385 {
22386 cp_parameter_declarator *parameter;
22387 tree decl = error_mark_node;
22388 bool parenthesized_p = false;
22389
22390 /* Parse the parameter. */
22391 parameter
22392 = cp_parser_parameter_declaration (parser, flags,
22393 /*template_parm_p=*/false,
22394 &parenthesized_p);
22395
22396 /* We don't know yet if the enclosing context is deprecated, so wait
22397 and warn in grokparms if appropriate. */
22398 deprecated_state = DEPRECATED_SUPPRESS;
22399
22400 if (parameter)
22401 {
22402 decl = grokdeclarator (parameter->declarator,
22403 &parameter->decl_specifiers,
22404 PARM,
22405 parameter->default_argument != NULL_TREE,
22406 &parameter->decl_specifiers.attributes);
22407 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
22408 DECL_SOURCE_LOCATION (decl) = parameter->loc;
22409 }
22410
22411 deprecated_state = DEPRECATED_NORMAL;
22412
22413 /* If a parse error occurred parsing the parameter declaration,
22414 then the entire parameter-declaration-list is erroneous. */
22415 if (decl == error_mark_node)
22416 {
22417 parameters = error_mark_node;
22418 break;
22419 }
22420
22421 if (parameter->decl_specifiers.attributes)
22422 cplus_decl_attributes (&decl,
22423 parameter->decl_specifiers.attributes,
22424 0);
22425 if (DECL_NAME (decl))
22426 decl = pushdecl (decl);
22427
22428 if (decl != error_mark_node)
22429 {
22430 retrofit_lang_decl (decl);
22431 DECL_PARM_INDEX (decl) = ++index;
22432 DECL_PARM_LEVEL (decl) = function_parm_depth ();
22433 }
22434
22435 /* Add the new parameter to the list. */
22436 *tail = build_tree_list (parameter->default_argument, decl);
22437 tail = &TREE_CHAIN (*tail);
22438
22439 /* Peek at the next token. */
22440 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22441 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22442 /* These are for Objective-C++ */
22443 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22444 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22445 /* The parameter-declaration-list is complete. */
22446 break;
22447 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22448 {
22449 cp_token *token;
22450
22451 /* Peek at the next token. */
22452 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22453 /* If it's an ellipsis, then the list is complete. */
22454 if (token->type == CPP_ELLIPSIS)
22455 break;
22456 /* Otherwise, there must be more parameters. Consume the
22457 `,'. */
22458 cp_lexer_consume_token (parser->lexer);
22459 /* When parsing something like:
22460
22461 int i(float f, double d)
22462
22463 we can tell after seeing the declaration for "f" that we
22464 are not looking at an initialization of a variable "i",
22465 but rather at the declaration of a function "i".
22466
22467 Due to the fact that the parsing of template arguments
22468 (as specified to a template-id) requires backtracking we
22469 cannot use this technique when inside a template argument
22470 list. */
22471 if (!parser->in_template_argument_list_p
22472 && !parser->in_type_id_in_expr_p
22473 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22474 /* However, a parameter-declaration of the form
22475 "float(f)" (which is a valid declaration of a
22476 parameter "f") can also be interpreted as an
22477 expression (the conversion of "f" to "float"). */
22478 && !parenthesized_p)
22479 cp_parser_commit_to_tentative_parse (parser);
22480 }
22481 else
22482 {
22483 cp_parser_error (parser, "expected %<,%> or %<...%>");
22484 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22485 cp_parser_skip_to_closing_parenthesis (parser,
22486 /*recovering=*/true,
22487 /*or_comma=*/false,
22488 /*consume_paren=*/false);
22489 break;
22490 }
22491 }
22492
22493 parser->in_unbraced_linkage_specification_p
22494 = saved_in_unbraced_linkage_specification_p;
22495
22496 /* Reset implicit_template_scope if we are about to leave the function
22497 parameter list that introduced it. Note that for out-of-line member
22498 definitions, there will be one or more class scopes before we get to
22499 the template parameter scope. */
22500
22501 if (cp_binding_level *its = parser->implicit_template_scope)
22502 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
22503 {
22504 while (maybe_its->kind == sk_class)
22505 maybe_its = maybe_its->level_chain;
22506 if (maybe_its == its)
22507 {
22508 parser->implicit_template_parms = 0;
22509 parser->implicit_template_scope = 0;
22510 }
22511 }
22512
22513 return parameters;
22514 }
22515
22516 /* Parse a parameter declaration.
22517
22518 parameter-declaration:
22519 decl-specifier-seq ... [opt] declarator
22520 decl-specifier-seq declarator = assignment-expression
22521 decl-specifier-seq ... [opt] abstract-declarator [opt]
22522 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22523
22524 The parser flags FLAGS is used to control type-specifier parsing.
22525
22526 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22527 declares a template parameter. (In that case, a non-nested `>'
22528 token encountered during the parsing of the assignment-expression
22529 is not interpreted as a greater-than operator.)
22530
22531 Returns a representation of the parameter, or NULL if an error
22532 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22533 true iff the declarator is of the form "(p)". */
22534
22535 static cp_parameter_declarator *
22536 cp_parser_parameter_declaration (cp_parser *parser,
22537 cp_parser_flags flags,
22538 bool template_parm_p,
22539 bool *parenthesized_p)
22540 {
22541 int declares_class_or_enum;
22542 cp_decl_specifier_seq decl_specifiers;
22543 cp_declarator *declarator;
22544 tree default_argument;
22545 cp_token *token = NULL, *declarator_token_start = NULL;
22546 const char *saved_message;
22547 bool template_parameter_pack_p = false;
22548
22549 /* In a template parameter, `>' is not an operator.
22550
22551 [temp.param]
22552
22553 When parsing a default template-argument for a non-type
22554 template-parameter, the first non-nested `>' is taken as the end
22555 of the template parameter-list rather than a greater-than
22556 operator. */
22557
22558 /* Type definitions may not appear in parameter types. */
22559 saved_message = parser->type_definition_forbidden_message;
22560 parser->type_definition_forbidden_message
22561 = G_("types may not be defined in parameter types");
22562
22563 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
22564 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22565 (current_template_parms)) : 0);
22566
22567 /* Parse the declaration-specifiers. */
22568 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22569 cp_parser_decl_specifier_seq (parser,
22570 flags,
22571 &decl_specifiers,
22572 &declares_class_or_enum);
22573
22574 /* Complain about missing 'typename' or other invalid type names. */
22575 if (!decl_specifiers.any_type_specifiers_p
22576 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22577 decl_specifiers.type = error_mark_node;
22578
22579 /* If an error occurred, there's no reason to attempt to parse the
22580 rest of the declaration. */
22581 if (cp_parser_error_occurred (parser))
22582 {
22583 parser->type_definition_forbidden_message = saved_message;
22584 return NULL;
22585 }
22586
22587 /* Peek at the next token. */
22588 token = cp_lexer_peek_token (parser->lexer);
22589
22590 /* If the next token is a `)', `,', `=', `>', or `...', then there
22591 is no declarator. However, when variadic templates are enabled,
22592 there may be a declarator following `...'. */
22593 if (token->type == CPP_CLOSE_PAREN
22594 || token->type == CPP_COMMA
22595 || token->type == CPP_EQ
22596 || token->type == CPP_GREATER)
22597 {
22598 declarator = NULL;
22599 if (parenthesized_p)
22600 *parenthesized_p = false;
22601 }
22602 /* Otherwise, there should be a declarator. */
22603 else
22604 {
22605 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22606 parser->default_arg_ok_p = false;
22607
22608 /* After seeing a decl-specifier-seq, if the next token is not a
22609 "(", there is no possibility that the code is a valid
22610 expression. Therefore, if parsing tentatively, we commit at
22611 this point. */
22612 if (!parser->in_template_argument_list_p
22613 /* In an expression context, having seen:
22614
22615 (int((char ...
22616
22617 we cannot be sure whether we are looking at a
22618 function-type (taking a "char" as a parameter) or a cast
22619 of some object of type "char" to "int". */
22620 && !parser->in_type_id_in_expr_p
22621 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22622 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22623 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22624 cp_parser_commit_to_tentative_parse (parser);
22625 /* Parse the declarator. */
22626 declarator_token_start = token;
22627 declarator = cp_parser_declarator (parser,
22628 CP_PARSER_DECLARATOR_EITHER,
22629 CP_PARSER_FLAGS_NONE,
22630 /*ctor_dtor_or_conv_p=*/NULL,
22631 parenthesized_p,
22632 /*member_p=*/false,
22633 /*friend_p=*/false,
22634 /*static_p=*/false);
22635 parser->default_arg_ok_p = saved_default_arg_ok_p;
22636 /* After the declarator, allow more attributes. */
22637 decl_specifiers.attributes
22638 = attr_chainon (decl_specifiers.attributes,
22639 cp_parser_attributes_opt (parser));
22640
22641 /* If the declarator is a template parameter pack, remember that and
22642 clear the flag in the declarator itself so we don't get errors
22643 from grokdeclarator. */
22644 if (template_parm_p && declarator && declarator->parameter_pack_p)
22645 {
22646 declarator->parameter_pack_p = false;
22647 template_parameter_pack_p = true;
22648 }
22649 }
22650
22651 /* If the next token is an ellipsis, and we have not seen a declarator
22652 name, and if either the type of the declarator contains parameter
22653 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22654 for, eg, abbreviated integral type names), then we actually have a
22655 parameter pack expansion expression. Otherwise, leave the ellipsis
22656 for a C-style variadic function. */
22657 token = cp_lexer_peek_token (parser->lexer);
22658
22659 /* If a function parameter pack was specified and an implicit template
22660 parameter was introduced during cp_parser_parameter_declaration,
22661 change any implicit parameters introduced into packs. */
22662 if (parser->implicit_template_parms
22663 && ((token->type == CPP_ELLIPSIS
22664 && declarator_can_be_parameter_pack (declarator))
22665 || (declarator && declarator->parameter_pack_p)))
22666 {
22667 int latest_template_parm_idx = TREE_VEC_LENGTH
22668 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
22669
22670 if (latest_template_parm_idx != template_parm_idx)
22671 decl_specifiers.type = convert_generic_types_to_packs
22672 (decl_specifiers.type,
22673 template_parm_idx, latest_template_parm_idx);
22674 }
22675
22676 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22677 {
22678 tree type = decl_specifiers.type;
22679
22680 if (type && DECL_P (type))
22681 type = TREE_TYPE (type);
22682
22683 if (((type
22684 && TREE_CODE (type) != TYPE_PACK_EXPANSION
22685 && (template_parm_p || uses_parameter_packs (type)))
22686 || (!type && template_parm_p))
22687 && declarator_can_be_parameter_pack (declarator))
22688 {
22689 /* Consume the `...'. */
22690 cp_lexer_consume_token (parser->lexer);
22691 maybe_warn_variadic_templates ();
22692
22693 /* Build a pack expansion type */
22694 if (template_parm_p)
22695 template_parameter_pack_p = true;
22696 else if (declarator)
22697 declarator->parameter_pack_p = true;
22698 else
22699 decl_specifiers.type = make_pack_expansion (type);
22700 }
22701 }
22702
22703 /* The restriction on defining new types applies only to the type
22704 of the parameter, not to the default argument. */
22705 parser->type_definition_forbidden_message = saved_message;
22706
22707 /* If the next token is `=', then process a default argument. */
22708 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22709 {
22710 tree type = decl_specifiers.type;
22711 token = cp_lexer_peek_token (parser->lexer);
22712 /* If we are defining a class, then the tokens that make up the
22713 default argument must be saved and processed later. */
22714 if (!template_parm_p && at_class_scope_p ()
22715 && TYPE_BEING_DEFINED (current_class_type)
22716 && !LAMBDA_TYPE_P (current_class_type))
22717 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22718
22719 /* A constrained-type-specifier may declare a type
22720 template-parameter. */
22721 else if (declares_constrained_type_template_parameter (type))
22722 default_argument
22723 = cp_parser_default_type_template_argument (parser);
22724
22725 /* A constrained-type-specifier may declare a
22726 template-template-parameter. */
22727 else if (declares_constrained_template_template_parameter (type))
22728 default_argument
22729 = cp_parser_default_template_template_argument (parser);
22730
22731 /* Outside of a class definition, we can just parse the
22732 assignment-expression. */
22733 else
22734 default_argument
22735 = cp_parser_default_argument (parser, template_parm_p);
22736
22737 if (!parser->default_arg_ok_p)
22738 {
22739 permerror (token->location,
22740 "default arguments are only "
22741 "permitted for function parameters");
22742 }
22743 else if ((declarator && declarator->parameter_pack_p)
22744 || template_parameter_pack_p
22745 || (decl_specifiers.type
22746 && PACK_EXPANSION_P (decl_specifiers.type)))
22747 {
22748 /* Find the name of the parameter pack. */
22749 cp_declarator *id_declarator = declarator;
22750 while (id_declarator && id_declarator->kind != cdk_id)
22751 id_declarator = id_declarator->declarator;
22752
22753 if (id_declarator && id_declarator->kind == cdk_id)
22754 error_at (declarator_token_start->location,
22755 template_parm_p
22756 ? G_("template parameter pack %qD "
22757 "cannot have a default argument")
22758 : G_("parameter pack %qD cannot have "
22759 "a default argument"),
22760 id_declarator->u.id.unqualified_name);
22761 else
22762 error_at (declarator_token_start->location,
22763 template_parm_p
22764 ? G_("template parameter pack cannot have "
22765 "a default argument")
22766 : G_("parameter pack cannot have a "
22767 "default argument"));
22768
22769 default_argument = NULL_TREE;
22770 }
22771 }
22772 else
22773 default_argument = NULL_TREE;
22774
22775 if (default_argument)
22776 STRIP_ANY_LOCATION_WRAPPER (default_argument);
22777
22778 /* Generate a location for the parameter, ranging from the start of the
22779 initial token to the end of the final token (using input_location for
22780 the latter, set up by cp_lexer_set_source_position_from_token when
22781 consuming tokens).
22782
22783 If we have a identifier, then use it for the caret location, e.g.
22784
22785 extern int callee (int one, int (*two)(int, int), float three);
22786 ~~~~~~^~~~~~~~~~~~~~
22787
22788 otherwise, reuse the start location for the caret location e.g.:
22789
22790 extern int callee (int one, int (*)(int, int), float three);
22791 ^~~~~~~~~~~~~~~~~
22792
22793 */
22794 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22795 ? declarator->id_loc
22796 : decl_spec_token_start->location);
22797 location_t param_loc = make_location (caret_loc,
22798 decl_spec_token_start->location,
22799 input_location);
22800
22801 return make_parameter_declarator (&decl_specifiers,
22802 declarator,
22803 default_argument,
22804 param_loc,
22805 template_parameter_pack_p);
22806 }
22807
22808 /* Parse a default argument and return it.
22809
22810 TEMPLATE_PARM_P is true if this is a default argument for a
22811 non-type template parameter. */
22812 static tree
22813 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22814 {
22815 tree default_argument = NULL_TREE;
22816 bool saved_greater_than_is_operator_p;
22817 unsigned char saved_local_variables_forbidden_p;
22818 bool non_constant_p, is_direct_init;
22819
22820 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22821 set correctly. */
22822 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22823 parser->greater_than_is_operator_p = !template_parm_p;
22824 /* Local variable names (and the `this' keyword) may not
22825 appear in a default argument. */
22826 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22827 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
22828 /* Parse the assignment-expression. */
22829 if (template_parm_p)
22830 push_deferring_access_checks (dk_no_deferred);
22831 tree saved_class_ptr = NULL_TREE;
22832 tree saved_class_ref = NULL_TREE;
22833 /* The "this" pointer is not valid in a default argument. */
22834 if (cfun)
22835 {
22836 saved_class_ptr = current_class_ptr;
22837 cp_function_chain->x_current_class_ptr = NULL_TREE;
22838 saved_class_ref = current_class_ref;
22839 cp_function_chain->x_current_class_ref = NULL_TREE;
22840 }
22841 default_argument
22842 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22843 /* Restore the "this" pointer. */
22844 if (cfun)
22845 {
22846 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22847 cp_function_chain->x_current_class_ref = saved_class_ref;
22848 }
22849 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22850 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22851 if (template_parm_p)
22852 pop_deferring_access_checks ();
22853 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22854 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22855
22856 return default_argument;
22857 }
22858
22859 /* Parse a function-body.
22860
22861 function-body:
22862 compound_statement */
22863
22864 static void
22865 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22866 {
22867 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22868 ? BCS_TRY_BLOCK : BCS_NORMAL),
22869 true);
22870 }
22871
22872 /* Parse a ctor-initializer-opt followed by a function-body. Return
22873 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22874 is true we are parsing a function-try-block. */
22875
22876 static void
22877 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22878 bool in_function_try_block)
22879 {
22880 tree body, list;
22881 const bool check_body_p
22882 = (DECL_CONSTRUCTOR_P (current_function_decl)
22883 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
22884 tree last = NULL;
22885
22886 if (in_function_try_block
22887 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22888 && cxx_dialect < cxx2a)
22889 {
22890 if (DECL_CONSTRUCTOR_P (current_function_decl))
22891 pedwarn (input_location, 0,
22892 "function-try-block body of %<constexpr%> constructor only "
22893 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22894 else
22895 pedwarn (input_location, 0,
22896 "function-try-block body of %<constexpr%> function only "
22897 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22898 }
22899
22900 /* Begin the function body. */
22901 body = begin_function_body ();
22902 /* Parse the optional ctor-initializer. */
22903 cp_parser_ctor_initializer_opt (parser);
22904
22905 /* If we're parsing a constexpr constructor definition, we need
22906 to check that the constructor body is indeed empty. However,
22907 before we get to cp_parser_function_body lot of junk has been
22908 generated, so we can't just check that we have an empty block.
22909 Rather we take a snapshot of the outermost block, and check whether
22910 cp_parser_function_body changed its state. */
22911 if (check_body_p)
22912 {
22913 list = cur_stmt_list;
22914 if (STATEMENT_LIST_TAIL (list))
22915 last = STATEMENT_LIST_TAIL (list)->stmt;
22916 }
22917 /* Parse the function-body. */
22918 cp_parser_function_body (parser, in_function_try_block);
22919 if (check_body_p)
22920 check_constexpr_ctor_body (last, list, /*complain=*/true);
22921 /* Finish the function body. */
22922 finish_function_body (body);
22923 }
22924
22925 /* Parse an initializer.
22926
22927 initializer:
22928 = initializer-clause
22929 ( expression-list )
22930
22931 Returns an expression representing the initializer. If no
22932 initializer is present, NULL_TREE is returned.
22933
22934 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22935 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22936 set to TRUE if there is no initializer present. If there is an
22937 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22938 is set to true; otherwise it is set to false. */
22939
22940 static tree
22941 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22942 bool* non_constant_p, bool subexpression_p)
22943 {
22944 cp_token *token;
22945 tree init;
22946
22947 /* Peek at the next token. */
22948 token = cp_lexer_peek_token (parser->lexer);
22949
22950 /* Let our caller know whether or not this initializer was
22951 parenthesized. */
22952 *is_direct_init = (token->type != CPP_EQ);
22953 /* Assume that the initializer is constant. */
22954 *non_constant_p = false;
22955
22956 if (token->type == CPP_EQ)
22957 {
22958 /* Consume the `='. */
22959 cp_lexer_consume_token (parser->lexer);
22960 /* Parse the initializer-clause. */
22961 init = cp_parser_initializer_clause (parser, non_constant_p);
22962 }
22963 else if (token->type == CPP_OPEN_PAREN)
22964 {
22965 vec<tree, va_gc> *vec;
22966 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22967 /*cast_p=*/false,
22968 /*allow_expansion_p=*/true,
22969 non_constant_p);
22970 if (vec == NULL)
22971 return error_mark_node;
22972 init = build_tree_list_vec (vec);
22973 release_tree_vector (vec);
22974 }
22975 else if (token->type == CPP_OPEN_BRACE)
22976 {
22977 cp_lexer_set_source_position (parser->lexer);
22978 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22979 init = cp_parser_braced_list (parser, non_constant_p);
22980 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22981 }
22982 else
22983 {
22984 /* Anything else is an error. */
22985 cp_parser_error (parser, "expected initializer");
22986 init = error_mark_node;
22987 }
22988
22989 if (!subexpression_p && check_for_bare_parameter_packs (init))
22990 init = error_mark_node;
22991
22992 return init;
22993 }
22994
22995 /* Parse an initializer-clause.
22996
22997 initializer-clause:
22998 assignment-expression
22999 braced-init-list
23000
23001 Returns an expression representing the initializer.
23002
23003 If the `assignment-expression' production is used the value
23004 returned is simply a representation for the expression.
23005
23006 Otherwise, calls cp_parser_braced_list. */
23007
23008 static cp_expr
23009 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
23010 {
23011 cp_expr initializer;
23012
23013 /* Assume the expression is constant. */
23014 *non_constant_p = false;
23015
23016 /* If it is not a `{', then we are looking at an
23017 assignment-expression. */
23018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23019 {
23020 initializer
23021 = cp_parser_constant_expression (parser,
23022 /*allow_non_constant_p=*/true,
23023 non_constant_p);
23024 }
23025 else
23026 initializer = cp_parser_braced_list (parser, non_constant_p);
23027
23028 return initializer;
23029 }
23030
23031 /* Parse a brace-enclosed initializer list.
23032
23033 braced-init-list:
23034 { initializer-list , [opt] }
23035 { designated-initializer-list , [opt] }
23036 { }
23037
23038 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
23039 the elements of the initializer-list (or NULL, if the last
23040 production is used). The TREE_TYPE for the CONSTRUCTOR will be
23041 NULL_TREE. There is no way to detect whether or not the optional
23042 trailing `,' was provided. NON_CONSTANT_P is as for
23043 cp_parser_initializer. */
23044
23045 static cp_expr
23046 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
23047 {
23048 tree initializer;
23049 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
23050
23051 /* Consume the `{' token. */
23052 matching_braces braces;
23053 braces.require_open (parser);
23054 /* Create a CONSTRUCTOR to represent the braced-initializer. */
23055 initializer = make_node (CONSTRUCTOR);
23056 /* If it's not a `}', then there is a non-trivial initializer. */
23057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
23058 {
23059 bool designated;
23060 /* Parse the initializer list. */
23061 CONSTRUCTOR_ELTS (initializer)
23062 = cp_parser_initializer_list (parser, non_constant_p, &designated);
23063 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
23064 /* A trailing `,' token is allowed. */
23065 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23066 cp_lexer_consume_token (parser->lexer);
23067 }
23068 else
23069 *non_constant_p = false;
23070 /* Now, there should be a trailing `}'. */
23071 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
23072 braces.require_close (parser);
23073 TREE_TYPE (initializer) = init_list_type_node;
23074
23075 cp_expr result (initializer);
23076 /* Build a location of the form:
23077 { ... }
23078 ^~~~~~~
23079 with caret==start at the open brace, finish at the close brace. */
23080 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
23081 result.set_location (combined_loc);
23082 return result;
23083 }
23084
23085 /* Consume tokens up to, and including, the next non-nested closing `]'.
23086 Returns true iff we found a closing `]'. */
23087
23088 static bool
23089 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
23090 {
23091 unsigned square_depth = 0;
23092
23093 while (true)
23094 {
23095 cp_token * token = cp_lexer_peek_token (parser->lexer);
23096
23097 switch (token->type)
23098 {
23099 case CPP_PRAGMA_EOL:
23100 if (!parser->lexer->in_pragma)
23101 break;
23102 /* FALLTHRU */
23103 case CPP_EOF:
23104 /* If we've run out of tokens, then there is no closing `]'. */
23105 return false;
23106
23107 case CPP_OPEN_SQUARE:
23108 ++square_depth;
23109 break;
23110
23111 case CPP_CLOSE_SQUARE:
23112 if (!square_depth--)
23113 {
23114 cp_lexer_consume_token (parser->lexer);
23115 return true;
23116 }
23117 break;
23118
23119 default:
23120 break;
23121 }
23122
23123 /* Consume the token. */
23124 cp_lexer_consume_token (parser->lexer);
23125 }
23126 }
23127
23128 /* Return true if we are looking at an array-designator, false otherwise. */
23129
23130 static bool
23131 cp_parser_array_designator_p (cp_parser *parser)
23132 {
23133 /* Consume the `['. */
23134 cp_lexer_consume_token (parser->lexer);
23135
23136 cp_lexer_save_tokens (parser->lexer);
23137
23138 /* Skip tokens until the next token is a closing square bracket.
23139 If we find the closing `]', and the next token is a `=', then
23140 we are looking at an array designator. */
23141 bool array_designator_p
23142 = (cp_parser_skip_to_closing_square_bracket (parser)
23143 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
23144
23145 /* Roll back the tokens we skipped. */
23146 cp_lexer_rollback_tokens (parser->lexer);
23147
23148 return array_designator_p;
23149 }
23150
23151 /* Parse an initializer-list.
23152
23153 initializer-list:
23154 initializer-clause ... [opt]
23155 initializer-list , initializer-clause ... [opt]
23156
23157 C++2A Extension:
23158
23159 designated-initializer-list:
23160 designated-initializer-clause
23161 designated-initializer-list , designated-initializer-clause
23162
23163 designated-initializer-clause:
23164 designator brace-or-equal-initializer
23165
23166 designator:
23167 . identifier
23168
23169 GNU Extension:
23170
23171 initializer-list:
23172 designation initializer-clause ...[opt]
23173 initializer-list , designation initializer-clause ...[opt]
23174
23175 designation:
23176 . identifier =
23177 identifier :
23178 [ constant-expression ] =
23179
23180 Returns a vec of constructor_elt. The VALUE of each elt is an expression
23181 for the initializer. If the INDEX of the elt is non-NULL, it is the
23182 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
23183 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
23184 are any designators. */
23185
23186 static vec<constructor_elt, va_gc> *
23187 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
23188 bool *designated)
23189 {
23190 vec<constructor_elt, va_gc> *v = NULL;
23191 bool first_p = true;
23192 tree first_designator = NULL_TREE;
23193
23194 /* Assume all of the expressions are constant. */
23195 *non_constant_p = false;
23196
23197 /* Parse the rest of the list. */
23198 while (true)
23199 {
23200 cp_token *token;
23201 tree designator;
23202 tree initializer;
23203 bool clause_non_constant_p;
23204 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23205
23206 /* Handle the C++2A syntax, '. id ='. */
23207 if ((cxx_dialect >= cxx2a
23208 || cp_parser_allow_gnu_extensions_p (parser))
23209 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
23210 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
23211 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
23212 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
23213 == CPP_OPEN_BRACE)))
23214 {
23215 if (cxx_dialect < cxx2a)
23216 pedwarn (loc, OPT_Wpedantic,
23217 "C++ designated initializers only available with "
23218 "%<-std=c++2a%> or %<-std=gnu++2a%>");
23219 /* Consume the `.'. */
23220 cp_lexer_consume_token (parser->lexer);
23221 /* Consume the identifier. */
23222 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23223 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23224 /* Consume the `='. */
23225 cp_lexer_consume_token (parser->lexer);
23226 }
23227 /* Also, if the next token is an identifier and the following one is a
23228 colon, we are looking at the GNU designated-initializer
23229 syntax. */
23230 else if (cp_parser_allow_gnu_extensions_p (parser)
23231 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
23232 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23233 == CPP_COLON))
23234 {
23235 /* Warn the user that they are using an extension. */
23236 pedwarn (loc, OPT_Wpedantic,
23237 "ISO C++ does not allow GNU designated initializers");
23238 /* Consume the identifier. */
23239 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23240 /* Consume the `:'. */
23241 cp_lexer_consume_token (parser->lexer);
23242 }
23243 /* Also handle C99 array designators, '[ const ] ='. */
23244 else if (cp_parser_allow_gnu_extensions_p (parser)
23245 && !c_dialect_objc ()
23246 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23247 {
23248 /* In C++11, [ could start a lambda-introducer. */
23249 bool non_const = false;
23250
23251 cp_parser_parse_tentatively (parser);
23252
23253 if (!cp_parser_array_designator_p (parser))
23254 {
23255 cp_parser_simulate_error (parser);
23256 designator = NULL_TREE;
23257 }
23258 else
23259 {
23260 designator = cp_parser_constant_expression (parser, true,
23261 &non_const);
23262 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23263 cp_parser_require (parser, CPP_EQ, RT_EQ);
23264 }
23265
23266 if (!cp_parser_parse_definitely (parser))
23267 designator = NULL_TREE;
23268 else if (non_const
23269 && (!require_potential_rvalue_constant_expression
23270 (designator)))
23271 designator = NULL_TREE;
23272 if (designator)
23273 /* Warn the user that they are using an extension. */
23274 pedwarn (loc, OPT_Wpedantic,
23275 "ISO C++ does not allow C99 designated initializers");
23276 }
23277 else
23278 designator = NULL_TREE;
23279
23280 if (first_p)
23281 {
23282 first_designator = designator;
23283 first_p = false;
23284 }
23285 else if (cxx_dialect >= cxx2a
23286 && first_designator != error_mark_node
23287 && (!first_designator != !designator))
23288 {
23289 error_at (loc, "either all initializer clauses should be designated "
23290 "or none of them should be");
23291 first_designator = error_mark_node;
23292 }
23293 else if (cxx_dialect < cxx2a && !first_designator)
23294 first_designator = designator;
23295
23296 /* Parse the initializer. */
23297 initializer = cp_parser_initializer_clause (parser,
23298 &clause_non_constant_p);
23299 /* If any clause is non-constant, so is the entire initializer. */
23300 if (clause_non_constant_p)
23301 *non_constant_p = true;
23302
23303 /* If we have an ellipsis, this is an initializer pack
23304 expansion. */
23305 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23306 {
23307 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23308
23309 /* Consume the `...'. */
23310 cp_lexer_consume_token (parser->lexer);
23311
23312 if (designator && cxx_dialect >= cxx2a)
23313 error_at (loc,
23314 "%<...%> not allowed in designated initializer list");
23315
23316 /* Turn the initializer into an initializer expansion. */
23317 initializer = make_pack_expansion (initializer);
23318 }
23319
23320 /* Add it to the vector. */
23321 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
23322
23323 /* If the next token is not a comma, we have reached the end of
23324 the list. */
23325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23326 break;
23327
23328 /* Peek at the next token. */
23329 token = cp_lexer_peek_nth_token (parser->lexer, 2);
23330 /* If the next token is a `}', then we're still done. An
23331 initializer-clause can have a trailing `,' after the
23332 initializer-list and before the closing `}'. */
23333 if (token->type == CPP_CLOSE_BRACE)
23334 break;
23335
23336 /* Consume the `,' token. */
23337 cp_lexer_consume_token (parser->lexer);
23338 }
23339
23340 /* The same identifier shall not appear in multiple designators
23341 of a designated-initializer-list. */
23342 if (first_designator)
23343 {
23344 unsigned int i;
23345 tree designator, val;
23346 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23347 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23348 {
23349 if (IDENTIFIER_MARKED (designator))
23350 {
23351 error_at (cp_expr_loc_or_input_loc (val),
23352 "%<.%s%> designator used multiple times in "
23353 "the same initializer list",
23354 IDENTIFIER_POINTER (designator));
23355 (*v)[i].index = error_mark_node;
23356 }
23357 else
23358 IDENTIFIER_MARKED (designator) = 1;
23359 }
23360 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23361 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23362 IDENTIFIER_MARKED (designator) = 0;
23363 }
23364
23365 *designated = first_designator != NULL_TREE;
23366 return v;
23367 }
23368
23369 /* Classes [gram.class] */
23370
23371 /* Parse a class-name.
23372
23373 class-name:
23374 identifier
23375 template-id
23376
23377 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23378 to indicate that names looked up in dependent types should be
23379 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23380 keyword has been used to indicate that the name that appears next
23381 is a template. TAG_TYPE indicates the explicit tag given before
23382 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23383 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23384 is the class being defined in a class-head. If ENUM_OK is TRUE,
23385 enum-names are also accepted.
23386
23387 Returns the TYPE_DECL representing the class. */
23388
23389 static tree
23390 cp_parser_class_name (cp_parser *parser,
23391 bool typename_keyword_p,
23392 bool template_keyword_p,
23393 enum tag_types tag_type,
23394 bool check_dependency_p,
23395 bool class_head_p,
23396 bool is_declaration,
23397 bool enum_ok)
23398 {
23399 tree decl;
23400 tree scope;
23401 bool typename_p;
23402 cp_token *token;
23403 tree identifier = NULL_TREE;
23404
23405 /* All class-names start with an identifier. */
23406 token = cp_lexer_peek_token (parser->lexer);
23407 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
23408 {
23409 cp_parser_error (parser, "expected class-name");
23410 return error_mark_node;
23411 }
23412
23413 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23414 to a template-id, so we save it here. */
23415 scope = parser->scope;
23416 if (scope == error_mark_node)
23417 return error_mark_node;
23418
23419 /* Any name names a type if we're following the `typename' keyword
23420 in a qualified name where the enclosing scope is type-dependent. */
23421 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
23422 && dependent_type_p (scope));
23423 /* Handle the common case (an identifier, but not a template-id)
23424 efficiently. */
23425 if (token->type == CPP_NAME
23426 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
23427 {
23428 cp_token *identifier_token;
23429 bool ambiguous_p;
23430
23431 /* Look for the identifier. */
23432 identifier_token = cp_lexer_peek_token (parser->lexer);
23433 ambiguous_p = identifier_token->error_reported;
23434 identifier = cp_parser_identifier (parser);
23435 /* If the next token isn't an identifier, we are certainly not
23436 looking at a class-name. */
23437 if (identifier == error_mark_node)
23438 decl = error_mark_node;
23439 /* If we know this is a type-name, there's no need to look it
23440 up. */
23441 else if (typename_p)
23442 decl = identifier;
23443 else
23444 {
23445 tree ambiguous_decls;
23446 /* If we already know that this lookup is ambiguous, then
23447 we've already issued an error message; there's no reason
23448 to check again. */
23449 if (ambiguous_p)
23450 {
23451 cp_parser_simulate_error (parser);
23452 return error_mark_node;
23453 }
23454 /* If the next token is a `::', then the name must be a type
23455 name.
23456
23457 [basic.lookup.qual]
23458
23459 During the lookup for a name preceding the :: scope
23460 resolution operator, object, function, and enumerator
23461 names are ignored. */
23462 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23463 tag_type = scope_type;
23464 /* Look up the name. */
23465 decl = cp_parser_lookup_name (parser, identifier,
23466 tag_type,
23467 /*is_template=*/false,
23468 /*is_namespace=*/false,
23469 check_dependency_p,
23470 &ambiguous_decls,
23471 identifier_token->location);
23472 if (ambiguous_decls)
23473 {
23474 if (cp_parser_parsing_tentatively (parser))
23475 cp_parser_simulate_error (parser);
23476 return error_mark_node;
23477 }
23478 }
23479 }
23480 else
23481 {
23482 /* Try a template-id. */
23483 decl = cp_parser_template_id (parser, template_keyword_p,
23484 check_dependency_p,
23485 tag_type,
23486 is_declaration);
23487 if (decl == error_mark_node)
23488 return error_mark_node;
23489 }
23490
23491 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
23492
23493 /* If this is a typename, create a TYPENAME_TYPE. */
23494 if (typename_p
23495 && decl != error_mark_node
23496 && !is_overloaded_fn (decl))
23497 {
23498 decl = make_typename_type (scope, decl, typename_type,
23499 /*complain=*/tf_error);
23500 if (decl != error_mark_node)
23501 decl = TYPE_NAME (decl);
23502 }
23503
23504 decl = strip_using_decl (decl);
23505
23506 /* Check to see that it is really the name of a class. */
23507 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
23508 && identifier_p (TREE_OPERAND (decl, 0))
23509 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23510 /* Situations like this:
23511
23512 template <typename T> struct A {
23513 typename T::template X<int>::I i;
23514 };
23515
23516 are problematic. Is `T::template X<int>' a class-name? The
23517 standard does not seem to be definitive, but there is no other
23518 valid interpretation of the following `::'. Therefore, those
23519 names are considered class-names. */
23520 {
23521 decl = make_typename_type (scope, decl, tag_type, tf_error);
23522 if (decl != error_mark_node)
23523 decl = TYPE_NAME (decl);
23524 }
23525 else if (TREE_CODE (decl) != TYPE_DECL
23526 || TREE_TYPE (decl) == error_mark_node
23527 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
23528 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
23529 /* In Objective-C 2.0, a classname followed by '.' starts a
23530 dot-syntax expression, and it's not a type-name. */
23531 || (c_dialect_objc ()
23532 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
23533 && objc_is_class_name (decl)))
23534 decl = error_mark_node;
23535
23536 if (decl == error_mark_node)
23537 cp_parser_error (parser, "expected class-name");
23538 else if (identifier && !parser->scope)
23539 maybe_note_name_used_in_class (identifier, decl);
23540
23541 return decl;
23542 }
23543
23544 /* Make sure that any member-function parameters are in scope.
23545 For instance, a function's noexcept-specifier can use the function's
23546 parameters:
23547
23548 struct S {
23549 void fn (int p) noexcept(noexcept(p));
23550 };
23551
23552 so we need to make sure name lookup can find them. This is used
23553 when we delay parsing of the noexcept-specifier. */
23554
23555 static void
23556 inject_parm_decls (tree decl)
23557 {
23558 begin_scope (sk_function_parms, decl);
23559 tree args = DECL_ARGUMENTS (decl);
23560
23561 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
23562 }
23563
23564 /* Undo the effects of inject_parm_decls. */
23565
23566 static void
23567 pop_injected_parms (void)
23568 {
23569 pop_bindings_and_leave_scope ();
23570 }
23571
23572 /* Parse a class-specifier.
23573
23574 class-specifier:
23575 class-head { member-specification [opt] }
23576
23577 Returns the TREE_TYPE representing the class. */
23578
23579 static tree
23580 cp_parser_class_specifier_1 (cp_parser* parser)
23581 {
23582 tree type;
23583 tree attributes = NULL_TREE;
23584 bool nested_name_specifier_p;
23585 unsigned saved_num_template_parameter_lists;
23586 bool saved_in_function_body;
23587 unsigned char in_statement;
23588 bool in_switch_statement_p;
23589 bool saved_in_unbraced_linkage_specification_p;
23590 tree old_scope = NULL_TREE;
23591 tree scope = NULL_TREE;
23592 cp_token *closing_brace;
23593
23594 push_deferring_access_checks (dk_no_deferred);
23595
23596 /* Parse the class-head. */
23597 type = cp_parser_class_head (parser,
23598 &nested_name_specifier_p);
23599 /* If the class-head was a semantic disaster, skip the entire body
23600 of the class. */
23601 if (!type)
23602 {
23603 cp_parser_skip_to_end_of_block_or_statement (parser);
23604 pop_deferring_access_checks ();
23605 return error_mark_node;
23606 }
23607
23608 /* Look for the `{'. */
23609 matching_braces braces;
23610 if (!braces.require_open (parser))
23611 {
23612 pop_deferring_access_checks ();
23613 return error_mark_node;
23614 }
23615
23616 cp_ensure_no_omp_declare_simd (parser);
23617 cp_ensure_no_oacc_routine (parser);
23618
23619 /* Issue an error message if type-definitions are forbidden here. */
23620 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
23621 /* Remember that we are defining one more class. */
23622 ++parser->num_classes_being_defined;
23623 /* Inside the class, surrounding template-parameter-lists do not
23624 apply. */
23625 saved_num_template_parameter_lists
23626 = parser->num_template_parameter_lists;
23627 parser->num_template_parameter_lists = 0;
23628 /* We are not in a function body. */
23629 saved_in_function_body = parser->in_function_body;
23630 parser->in_function_body = false;
23631 /* Or in a loop. */
23632 in_statement = parser->in_statement;
23633 parser->in_statement = 0;
23634 /* Or in a switch. */
23635 in_switch_statement_p = parser->in_switch_statement_p;
23636 parser->in_switch_statement_p = false;
23637 /* We are not immediately inside an extern "lang" block. */
23638 saved_in_unbraced_linkage_specification_p
23639 = parser->in_unbraced_linkage_specification_p;
23640 parser->in_unbraced_linkage_specification_p = false;
23641
23642 // Associate constraints with the type.
23643 if (flag_concepts)
23644 type = associate_classtype_constraints (type);
23645
23646 /* Start the class. */
23647 if (nested_name_specifier_p)
23648 {
23649 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23650 old_scope = push_inner_scope (scope);
23651 }
23652 type = begin_class_definition (type);
23653
23654 if (type == error_mark_node)
23655 /* If the type is erroneous, skip the entire body of the class. */
23656 cp_parser_skip_to_closing_brace (parser);
23657 else
23658 /* Parse the member-specification. */
23659 cp_parser_member_specification_opt (parser);
23660
23661 /* Look for the trailing `}'. */
23662 closing_brace = braces.require_close (parser);
23663 /* Look for trailing attributes to apply to this class. */
23664 if (cp_parser_allow_gnu_extensions_p (parser))
23665 attributes = cp_parser_gnu_attributes_opt (parser);
23666 if (type != error_mark_node)
23667 type = finish_struct (type, attributes);
23668 if (nested_name_specifier_p)
23669 pop_inner_scope (old_scope, scope);
23670
23671 /* We've finished a type definition. Check for the common syntax
23672 error of forgetting a semicolon after the definition. We need to
23673 be careful, as we can't just check for not-a-semicolon and be done
23674 with it; the user might have typed:
23675
23676 class X { } c = ...;
23677 class X { } *p = ...;
23678
23679 and so forth. Instead, enumerate all the possible tokens that
23680 might follow this production; if we don't see one of them, then
23681 complain and silently insert the semicolon. */
23682 {
23683 cp_token *token = cp_lexer_peek_token (parser->lexer);
23684 bool want_semicolon = true;
23685
23686 if (cp_next_tokens_can_be_std_attribute_p (parser))
23687 /* Don't try to parse c++11 attributes here. As per the
23688 grammar, that should be a task for
23689 cp_parser_decl_specifier_seq. */
23690 want_semicolon = false;
23691
23692 switch (token->type)
23693 {
23694 case CPP_NAME:
23695 case CPP_SEMICOLON:
23696 case CPP_MULT:
23697 case CPP_AND:
23698 case CPP_OPEN_PAREN:
23699 case CPP_CLOSE_PAREN:
23700 case CPP_COMMA:
23701 want_semicolon = false;
23702 break;
23703
23704 /* While it's legal for type qualifiers and storage class
23705 specifiers to follow type definitions in the grammar, only
23706 compiler testsuites contain code like that. Assume that if
23707 we see such code, then what we're really seeing is a case
23708 like:
23709
23710 class X { }
23711 const <type> var = ...;
23712
23713 or
23714
23715 class Y { }
23716 static <type> func (...) ...
23717
23718 i.e. the qualifier or specifier applies to the next
23719 declaration. To do so, however, we need to look ahead one
23720 more token to see if *that* token is a type specifier.
23721
23722 This code could be improved to handle:
23723
23724 class Z { }
23725 static const <type> var = ...; */
23726 case CPP_KEYWORD:
23727 if (keyword_is_decl_specifier (token->keyword))
23728 {
23729 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
23730
23731 /* Handling user-defined types here would be nice, but very
23732 tricky. */
23733 want_semicolon
23734 = (lookahead->type == CPP_KEYWORD
23735 && keyword_begins_type_specifier (lookahead->keyword));
23736 }
23737 break;
23738 default:
23739 break;
23740 }
23741
23742 /* If we don't have a type, then something is very wrong and we
23743 shouldn't try to do anything clever. Likewise for not seeing the
23744 closing brace. */
23745 if (closing_brace && TYPE_P (type) && want_semicolon)
23746 {
23747 /* Locate the closing brace. */
23748 cp_token_position prev
23749 = cp_lexer_previous_token_position (parser->lexer);
23750 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
23751 location_t loc = prev_token->location;
23752
23753 /* We want to suggest insertion of a ';' immediately *after* the
23754 closing brace, so, if we can, offset the location by 1 column. */
23755 location_t next_loc = loc;
23756 if (!linemap_location_from_macro_expansion_p (line_table, loc))
23757 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
23758
23759 rich_location richloc (line_table, next_loc);
23760
23761 /* If we successfully offset the location, suggest the fix-it. */
23762 if (next_loc != loc)
23763 richloc.add_fixit_insert_before (next_loc, ";");
23764
23765 if (CLASSTYPE_DECLARED_CLASS (type))
23766 error_at (&richloc,
23767 "expected %<;%> after class definition");
23768 else if (TREE_CODE (type) == RECORD_TYPE)
23769 error_at (&richloc,
23770 "expected %<;%> after struct definition");
23771 else if (TREE_CODE (type) == UNION_TYPE)
23772 error_at (&richloc,
23773 "expected %<;%> after union definition");
23774 else
23775 gcc_unreachable ();
23776
23777 /* Unget one token and smash it to look as though we encountered
23778 a semicolon in the input stream. */
23779 cp_lexer_set_token_position (parser->lexer, prev);
23780 token = cp_lexer_peek_token (parser->lexer);
23781 token->type = CPP_SEMICOLON;
23782 token->keyword = RID_MAX;
23783 }
23784 }
23785
23786 /* If this class is not itself within the scope of another class,
23787 then we need to parse the bodies of all of the queued function
23788 definitions. Note that the queued functions defined in a class
23789 are not always processed immediately following the
23790 class-specifier for that class. Consider:
23791
23792 struct A {
23793 struct B { void f() { sizeof (A); } };
23794 };
23795
23796 If `f' were processed before the processing of `A' were
23797 completed, there would be no way to compute the size of `A'.
23798 Note that the nesting we are interested in here is lexical --
23799 not the semantic nesting given by TYPE_CONTEXT. In particular,
23800 for:
23801
23802 struct A { struct B; };
23803 struct A::B { void f() { } };
23804
23805 there is no need to delay the parsing of `A::B::f'. */
23806 if (--parser->num_classes_being_defined == 0)
23807 {
23808 tree decl;
23809 tree class_type = NULL_TREE;
23810 tree pushed_scope = NULL_TREE;
23811 unsigned ix;
23812 cp_default_arg_entry *e;
23813 tree save_ccp, save_ccr;
23814
23815 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
23816 {
23817 /* Skip default arguments, NSDMIs, etc, in order to improve
23818 error recovery (c++/71169, c++/71832). */
23819 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23820 vec_safe_truncate (unparsed_nsdmis, 0);
23821 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23822 }
23823
23824 /* In a first pass, parse default arguments to the functions.
23825 Then, in a second pass, parse the bodies of the functions.
23826 This two-phased approach handles cases like:
23827
23828 struct S {
23829 void f() { g(); }
23830 void g(int i = 3);
23831 };
23832
23833 */
23834 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23835 {
23836 decl = e->decl;
23837 /* If there are default arguments that have not yet been processed,
23838 take care of them now. */
23839 if (class_type != e->class_type)
23840 {
23841 if (pushed_scope)
23842 pop_scope (pushed_scope);
23843 class_type = e->class_type;
23844 pushed_scope = push_scope (class_type);
23845 }
23846 /* Make sure that any template parameters are in scope. */
23847 maybe_begin_member_template_processing (decl);
23848 /* Parse the default argument expressions. */
23849 cp_parser_late_parsing_default_args (parser, decl);
23850 /* Remove any template parameters from the symbol table. */
23851 maybe_end_member_template_processing ();
23852 }
23853 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23854 /* Now parse any NSDMIs. */
23855 save_ccp = current_class_ptr;
23856 save_ccr = current_class_ref;
23857 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23858 {
23859 if (class_type != DECL_CONTEXT (decl))
23860 {
23861 if (pushed_scope)
23862 pop_scope (pushed_scope);
23863 class_type = DECL_CONTEXT (decl);
23864 pushed_scope = push_scope (class_type);
23865 }
23866 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23867 cp_parser_late_parsing_nsdmi (parser, decl);
23868 }
23869 vec_safe_truncate (unparsed_nsdmis, 0);
23870 current_class_ptr = save_ccp;
23871 current_class_ref = save_ccr;
23872 if (pushed_scope)
23873 pop_scope (pushed_scope);
23874
23875 /* If there are noexcept-specifiers that have not yet been processed,
23876 take care of them now. */
23877 class_type = NULL_TREE;
23878 pushed_scope = NULL_TREE;
23879 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
23880 {
23881 tree ctx = DECL_CONTEXT (decl);
23882 if (class_type != ctx)
23883 {
23884 if (pushed_scope)
23885 pop_scope (pushed_scope);
23886 class_type = ctx;
23887 pushed_scope = push_scope (class_type);
23888 }
23889
23890 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
23891 spec = TREE_PURPOSE (spec);
23892
23893 /* Make sure that any template parameters are in scope. */
23894 maybe_begin_member_template_processing (decl);
23895
23896 /* Make sure that any member-function parameters are in scope. */
23897 inject_parm_decls (decl);
23898
23899 /* 'this' is not allowed in static member functions. */
23900 unsigned char local_variables_forbidden_p
23901 = parser->local_variables_forbidden_p;
23902 if (DECL_THIS_STATIC (decl))
23903 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23904
23905 /* Now we can parse the noexcept-specifier. */
23906 spec = cp_parser_late_noexcept_specifier (parser, spec);
23907
23908 if (spec != error_mark_node)
23909 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
23910
23911 /* Restore the state of local_variables_forbidden_p. */
23912 parser->local_variables_forbidden_p = local_variables_forbidden_p;
23913
23914 /* The finish_struct call above performed various override checking,
23915 but it skipped unparsed noexcept-specifier operands. Now that we
23916 have resolved them, check again. */
23917 noexcept_override_late_checks (type, decl);
23918
23919 /* Remove any member-function parameters from the symbol table. */
23920 pop_injected_parms ();
23921
23922 /* Remove any template parameters from the symbol table. */
23923 maybe_end_member_template_processing ();
23924 }
23925 vec_safe_truncate (unparsed_noexcepts, 0);
23926 if (pushed_scope)
23927 pop_scope (pushed_scope);
23928
23929 /* Now parse the body of the functions. */
23930 if (flag_openmp)
23931 {
23932 /* OpenMP UDRs need to be parsed before all other functions. */
23933 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23934 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23935 cp_parser_late_parsing_for_member (parser, decl);
23936 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23937 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23938 cp_parser_late_parsing_for_member (parser, decl);
23939 }
23940 else
23941 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23942 cp_parser_late_parsing_for_member (parser, decl);
23943 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23944 }
23945
23946 /* Put back any saved access checks. */
23947 pop_deferring_access_checks ();
23948
23949 /* Restore saved state. */
23950 parser->in_switch_statement_p = in_switch_statement_p;
23951 parser->in_statement = in_statement;
23952 parser->in_function_body = saved_in_function_body;
23953 parser->num_template_parameter_lists
23954 = saved_num_template_parameter_lists;
23955 parser->in_unbraced_linkage_specification_p
23956 = saved_in_unbraced_linkage_specification_p;
23957
23958 return type;
23959 }
23960
23961 static tree
23962 cp_parser_class_specifier (cp_parser* parser)
23963 {
23964 tree ret;
23965 timevar_push (TV_PARSE_STRUCT);
23966 ret = cp_parser_class_specifier_1 (parser);
23967 timevar_pop (TV_PARSE_STRUCT);
23968 return ret;
23969 }
23970
23971 /* Parse a class-head.
23972
23973 class-head:
23974 class-key identifier [opt] base-clause [opt]
23975 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23976 class-key nested-name-specifier [opt] template-id
23977 base-clause [opt]
23978
23979 class-virt-specifier:
23980 final
23981
23982 GNU Extensions:
23983 class-key attributes identifier [opt] base-clause [opt]
23984 class-key attributes nested-name-specifier identifier base-clause [opt]
23985 class-key attributes nested-name-specifier [opt] template-id
23986 base-clause [opt]
23987
23988 Upon return BASES is initialized to the list of base classes (or
23989 NULL, if there are none) in the same form returned by
23990 cp_parser_base_clause.
23991
23992 Returns the TYPE of the indicated class. Sets
23993 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23994 involving a nested-name-specifier was used, and FALSE otherwise.
23995
23996 Returns error_mark_node if this is not a class-head.
23997
23998 Returns NULL_TREE if the class-head is syntactically valid, but
23999 semantically invalid in a way that means we should skip the entire
24000 body of the class. */
24001
24002 static tree
24003 cp_parser_class_head (cp_parser* parser,
24004 bool* nested_name_specifier_p)
24005 {
24006 tree nested_name_specifier;
24007 enum tag_types class_key;
24008 tree id = NULL_TREE;
24009 tree type = NULL_TREE;
24010 tree attributes;
24011 tree bases;
24012 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
24013 bool template_id_p = false;
24014 bool qualified_p = false;
24015 bool invalid_nested_name_p = false;
24016 bool invalid_explicit_specialization_p = false;
24017 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24018 tree pushed_scope = NULL_TREE;
24019 unsigned num_templates;
24020 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
24021 /* Assume no nested-name-specifier will be present. */
24022 *nested_name_specifier_p = false;
24023 /* Assume no template parameter lists will be used in defining the
24024 type. */
24025 num_templates = 0;
24026 parser->colon_corrects_to_scope_p = false;
24027
24028 /* Look for the class-key. */
24029 class_key = cp_parser_class_key (parser);
24030 if (class_key == none_type)
24031 return error_mark_node;
24032
24033 location_t class_head_start_location = input_location;
24034
24035 /* Parse the attributes. */
24036 attributes = cp_parser_attributes_opt (parser);
24037
24038 /* If the next token is `::', that is invalid -- but sometimes
24039 people do try to write:
24040
24041 struct ::S {};
24042
24043 Handle this gracefully by accepting the extra qualifier, and then
24044 issuing an error about it later if this really is a
24045 class-head. If it turns out just to be an elaborated type
24046 specifier, remain silent. */
24047 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
24048 qualified_p = true;
24049
24050 push_deferring_access_checks (dk_no_check);
24051
24052 /* Determine the name of the class. Begin by looking for an
24053 optional nested-name-specifier. */
24054 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
24055 nested_name_specifier
24056 = cp_parser_nested_name_specifier_opt (parser,
24057 /*typename_keyword_p=*/false,
24058 /*check_dependency_p=*/false,
24059 /*type_p=*/true,
24060 /*is_declaration=*/false);
24061 /* If there was a nested-name-specifier, then there *must* be an
24062 identifier. */
24063
24064 cp_token *bad_template_keyword = NULL;
24065
24066 if (nested_name_specifier)
24067 {
24068 type_start_token = cp_lexer_peek_token (parser->lexer);
24069 /* Although the grammar says `identifier', it really means
24070 `class-name' or `template-name'. You are only allowed to
24071 define a class that has already been declared with this
24072 syntax.
24073
24074 The proposed resolution for Core Issue 180 says that wherever
24075 you see `class T::X' you should treat `X' as a type-name.
24076
24077 It is OK to define an inaccessible class; for example:
24078
24079 class A { class B; };
24080 class A::B {};
24081
24082 We do not know if we will see a class-name, or a
24083 template-name. We look for a class-name first, in case the
24084 class-name is a template-id; if we looked for the
24085 template-name first we would stop after the template-name. */
24086 cp_parser_parse_tentatively (parser);
24087 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24088 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
24089 type = cp_parser_class_name (parser,
24090 /*typename_keyword_p=*/false,
24091 /*template_keyword_p=*/false,
24092 class_type,
24093 /*check_dependency_p=*/false,
24094 /*class_head_p=*/true,
24095 /*is_declaration=*/false);
24096 /* If that didn't work, ignore the nested-name-specifier. */
24097 if (!cp_parser_parse_definitely (parser))
24098 {
24099 invalid_nested_name_p = true;
24100 type_start_token = cp_lexer_peek_token (parser->lexer);
24101 id = cp_parser_identifier (parser);
24102 if (id == error_mark_node)
24103 id = NULL_TREE;
24104 }
24105 /* If we could not find a corresponding TYPE, treat this
24106 declaration like an unqualified declaration. */
24107 if (type == error_mark_node)
24108 nested_name_specifier = NULL_TREE;
24109 /* Otherwise, count the number of templates used in TYPE and its
24110 containing scopes. */
24111 else
24112 num_templates = num_template_headers_for_class (TREE_TYPE (type));
24113 }
24114 /* Otherwise, the identifier is optional. */
24115 else
24116 {
24117 /* We don't know whether what comes next is a template-id,
24118 an identifier, or nothing at all. */
24119 cp_parser_parse_tentatively (parser);
24120 /* Check for a template-id. */
24121 type_start_token = cp_lexer_peek_token (parser->lexer);
24122 id = cp_parser_template_id (parser,
24123 /*template_keyword_p=*/false,
24124 /*check_dependency_p=*/true,
24125 class_key,
24126 /*is_declaration=*/true);
24127 /* If that didn't work, it could still be an identifier. */
24128 if (!cp_parser_parse_definitely (parser))
24129 {
24130 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24131 {
24132 type_start_token = cp_lexer_peek_token (parser->lexer);
24133 id = cp_parser_identifier (parser);
24134 }
24135 else
24136 id = NULL_TREE;
24137 }
24138 else
24139 {
24140 template_id_p = true;
24141 ++num_templates;
24142 }
24143 }
24144
24145 pop_deferring_access_checks ();
24146
24147 if (id)
24148 {
24149 cp_parser_check_for_invalid_template_id (parser, id,
24150 class_key,
24151 type_start_token->location);
24152 }
24153 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
24154
24155 /* If it's not a `:' or a `{' then we can't really be looking at a
24156 class-head, since a class-head only appears as part of a
24157 class-specifier. We have to detect this situation before calling
24158 xref_tag, since that has irreversible side-effects. */
24159 if (!cp_parser_next_token_starts_class_definition_p (parser))
24160 {
24161 cp_parser_error (parser, "expected %<{%> or %<:%>");
24162 type = error_mark_node;
24163 goto out;
24164 }
24165
24166 /* At this point, we're going ahead with the class-specifier, even
24167 if some other problem occurs. */
24168 cp_parser_commit_to_tentative_parse (parser);
24169 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
24170 {
24171 cp_parser_error (parser,
24172 "cannot specify %<override%> for a class");
24173 type = error_mark_node;
24174 goto out;
24175 }
24176 /* Issue the error about the overly-qualified name now. */
24177 if (qualified_p)
24178 {
24179 cp_parser_error (parser,
24180 "global qualification of class name is invalid");
24181 type = error_mark_node;
24182 goto out;
24183 }
24184 else if (invalid_nested_name_p)
24185 {
24186 cp_parser_error (parser,
24187 "qualified name does not name a class");
24188 type = error_mark_node;
24189 goto out;
24190 }
24191 else if (nested_name_specifier)
24192 {
24193 tree scope;
24194
24195 if (bad_template_keyword)
24196 /* [temp.names]: in a qualified-id formed by a class-head-name, the
24197 keyword template shall not appear at the top level. */
24198 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
24199 "keyword %<template%> not allowed in class-head-name");
24200
24201 /* Reject typedef-names in class heads. */
24202 if (!DECL_IMPLICIT_TYPEDEF_P (type))
24203 {
24204 error_at (type_start_token->location,
24205 "invalid class name in declaration of %qD",
24206 type);
24207 type = NULL_TREE;
24208 goto done;
24209 }
24210
24211 /* Figure out in what scope the declaration is being placed. */
24212 scope = current_scope ();
24213 /* If that scope does not contain the scope in which the
24214 class was originally declared, the program is invalid. */
24215 if (scope && !is_ancestor (scope, nested_name_specifier))
24216 {
24217 if (at_namespace_scope_p ())
24218 error_at (type_start_token->location,
24219 "declaration of %qD in namespace %qD which does not "
24220 "enclose %qD",
24221 type, scope, nested_name_specifier);
24222 else
24223 error_at (type_start_token->location,
24224 "declaration of %qD in %qD which does not enclose %qD",
24225 type, scope, nested_name_specifier);
24226 type = NULL_TREE;
24227 goto done;
24228 }
24229 /* [dcl.meaning]
24230
24231 A declarator-id shall not be qualified except for the
24232 definition of a ... nested class outside of its class
24233 ... [or] the definition or explicit instantiation of a
24234 class member of a namespace outside of its namespace. */
24235 if (scope == nested_name_specifier)
24236 permerror (nested_name_specifier_token_start->location,
24237 "extra qualification not allowed");
24238 }
24239 /* An explicit-specialization must be preceded by "template <>". If
24240 it is not, try to recover gracefully. */
24241 if (at_namespace_scope_p ()
24242 && parser->num_template_parameter_lists == 0
24243 && !processing_template_parmlist
24244 && template_id_p)
24245 {
24246 /* Build a location of this form:
24247 struct typename <ARGS>
24248 ^~~~~~~~~~~~~~~~~~~~~~
24249 with caret==start at the start token, and
24250 finishing at the end of the type. */
24251 location_t reported_loc
24252 = make_location (class_head_start_location,
24253 class_head_start_location,
24254 get_finish (type_start_token->location));
24255 rich_location richloc (line_table, reported_loc);
24256 richloc.add_fixit_insert_before (class_head_start_location,
24257 "template <> ");
24258 error_at (&richloc,
24259 "an explicit specialization must be preceded by"
24260 " %<template <>%>");
24261 invalid_explicit_specialization_p = true;
24262 /* Take the same action that would have been taken by
24263 cp_parser_explicit_specialization. */
24264 ++parser->num_template_parameter_lists;
24265 begin_specialization ();
24266 }
24267 /* There must be no "return" statements between this point and the
24268 end of this function; set "type "to the correct return value and
24269 use "goto done;" to return. */
24270 /* Make sure that the right number of template parameters were
24271 present. */
24272 if (!cp_parser_check_template_parameters (parser, num_templates,
24273 template_id_p,
24274 type_start_token->location,
24275 /*declarator=*/NULL))
24276 {
24277 /* If something went wrong, there is no point in even trying to
24278 process the class-definition. */
24279 type = NULL_TREE;
24280 goto done;
24281 }
24282
24283 /* Look up the type. */
24284 if (template_id_p)
24285 {
24286 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
24287 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
24288 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
24289 {
24290 error_at (type_start_token->location,
24291 "function template %qD redeclared as a class template", id);
24292 type = error_mark_node;
24293 }
24294 else
24295 {
24296 type = TREE_TYPE (id);
24297 type = maybe_process_partial_specialization (type);
24298
24299 /* Check the scope while we still know whether or not we had a
24300 nested-name-specifier. */
24301 if (type != error_mark_node)
24302 check_unqualified_spec_or_inst (type, type_start_token->location);
24303 }
24304 if (nested_name_specifier)
24305 pushed_scope = push_scope (nested_name_specifier);
24306 }
24307 else if (nested_name_specifier)
24308 {
24309 tree class_type;
24310
24311 /* Given:
24312
24313 template <typename T> struct S { struct T };
24314 template <typename T> struct S<T>::T { };
24315
24316 we will get a TYPENAME_TYPE when processing the definition of
24317 `S::T'. We need to resolve it to the actual type before we
24318 try to define it. */
24319 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
24320 {
24321 class_type = resolve_typename_type (TREE_TYPE (type),
24322 /*only_current_p=*/false);
24323 if (TREE_CODE (class_type) != TYPENAME_TYPE)
24324 type = TYPE_NAME (class_type);
24325 else
24326 {
24327 cp_parser_error (parser, "could not resolve typename type");
24328 type = error_mark_node;
24329 }
24330 }
24331
24332 if (maybe_process_partial_specialization (TREE_TYPE (type))
24333 == error_mark_node)
24334 {
24335 type = NULL_TREE;
24336 goto done;
24337 }
24338
24339 class_type = current_class_type;
24340 /* Enter the scope indicated by the nested-name-specifier. */
24341 pushed_scope = push_scope (nested_name_specifier);
24342 /* Get the canonical version of this type. */
24343 type = TYPE_MAIN_DECL (TREE_TYPE (type));
24344 /* Call push_template_decl if it seems like we should be defining a
24345 template either from the template headers or the type we're
24346 defining, so that we diagnose both extra and missing headers. */
24347 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24348 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
24349 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
24350 {
24351 type = push_template_decl (type);
24352 if (type == error_mark_node)
24353 {
24354 type = NULL_TREE;
24355 goto done;
24356 }
24357 }
24358
24359 type = TREE_TYPE (type);
24360 *nested_name_specifier_p = true;
24361 }
24362 else /* The name is not a nested name. */
24363 {
24364 /* If the class was unnamed, create a dummy name. */
24365 if (!id)
24366 id = make_anon_name ();
24367 tag_scope tag_scope = (parser->in_type_id_in_expr_p
24368 ? ts_within_enclosing_non_class
24369 : ts_current);
24370 type = xref_tag (class_key, id, tag_scope,
24371 parser->num_template_parameter_lists);
24372 }
24373
24374 /* Indicate whether this class was declared as a `class' or as a
24375 `struct'. */
24376 if (TREE_CODE (type) == RECORD_TYPE)
24377 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
24378 cp_parser_check_class_key (class_key, type);
24379
24380 /* If this type was already complete, and we see another definition,
24381 that's an error. Likewise if the type is already being defined:
24382 this can happen, eg, when it's defined from within an expression
24383 (c++/84605). */
24384 if (type != error_mark_node
24385 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
24386 {
24387 error_at (type_start_token->location, "redefinition of %q#T",
24388 type);
24389 inform (location_of (type), "previous definition of %q#T",
24390 type);
24391 type = NULL_TREE;
24392 goto done;
24393 }
24394 else if (type == error_mark_node)
24395 type = NULL_TREE;
24396
24397 if (type)
24398 {
24399 /* Apply attributes now, before any use of the class as a template
24400 argument in its base list. */
24401 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
24402 fixup_attribute_variants (type);
24403 }
24404
24405 /* We will have entered the scope containing the class; the names of
24406 base classes should be looked up in that context. For example:
24407
24408 struct A { struct B {}; struct C; };
24409 struct A::C : B {};
24410
24411 is valid. */
24412
24413 /* Get the list of base-classes, if there is one. */
24414 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24415 {
24416 /* PR59482: enter the class scope so that base-specifiers are looked
24417 up correctly. */
24418 if (type)
24419 pushclass (type);
24420 bases = cp_parser_base_clause (parser);
24421 /* PR59482: get out of the previously pushed class scope so that the
24422 subsequent pops pop the right thing. */
24423 if (type)
24424 popclass ();
24425 }
24426 else
24427 bases = NULL_TREE;
24428
24429 /* If we're really defining a class, process the base classes.
24430 If they're invalid, fail. */
24431 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24432 xref_basetypes (type, bases);
24433
24434 done:
24435 /* Leave the scope given by the nested-name-specifier. We will
24436 enter the class scope itself while processing the members. */
24437 if (pushed_scope)
24438 pop_scope (pushed_scope);
24439
24440 if (invalid_explicit_specialization_p)
24441 {
24442 end_specialization ();
24443 --parser->num_template_parameter_lists;
24444 }
24445
24446 if (type)
24447 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
24448 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
24449 CLASSTYPE_FINAL (type) = 1;
24450 out:
24451 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24452 return type;
24453 }
24454
24455 /* Parse a class-key.
24456
24457 class-key:
24458 class
24459 struct
24460 union
24461
24462 Returns the kind of class-key specified, or none_type to indicate
24463 error. */
24464
24465 static enum tag_types
24466 cp_parser_class_key (cp_parser* parser)
24467 {
24468 cp_token *token;
24469 enum tag_types tag_type;
24470
24471 /* Look for the class-key. */
24472 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
24473 if (!token)
24474 return none_type;
24475
24476 /* Check to see if the TOKEN is a class-key. */
24477 tag_type = cp_parser_token_is_class_key (token);
24478 if (!tag_type)
24479 cp_parser_error (parser, "expected class-key");
24480 return tag_type;
24481 }
24482
24483 /* Parse a type-parameter-key.
24484
24485 type-parameter-key:
24486 class
24487 typename
24488 */
24489
24490 static void
24491 cp_parser_type_parameter_key (cp_parser* parser)
24492 {
24493 /* Look for the type-parameter-key. */
24494 enum tag_types tag_type = none_type;
24495 cp_token *token = cp_lexer_peek_token (parser->lexer);
24496 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
24497 {
24498 cp_lexer_consume_token (parser->lexer);
24499 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
24500 /* typename is not allowed in a template template parameter
24501 by the standard until C++17. */
24502 pedwarn (token->location, OPT_Wpedantic,
24503 "ISO C++ forbids typename key in template template parameter;"
24504 " use %<-std=c++17%> or %<-std=gnu++17%>");
24505 }
24506 else
24507 cp_parser_error (parser, "expected %<class%> or %<typename%>");
24508
24509 return;
24510 }
24511
24512 /* Parse an (optional) member-specification.
24513
24514 member-specification:
24515 member-declaration member-specification [opt]
24516 access-specifier : member-specification [opt] */
24517
24518 static void
24519 cp_parser_member_specification_opt (cp_parser* parser)
24520 {
24521 while (true)
24522 {
24523 cp_token *token;
24524 enum rid keyword;
24525
24526 /* Peek at the next token. */
24527 token = cp_lexer_peek_token (parser->lexer);
24528 /* If it's a `}', or EOF then we've seen all the members. */
24529 if (token->type == CPP_CLOSE_BRACE
24530 || token->type == CPP_EOF
24531 || token->type == CPP_PRAGMA_EOL)
24532 break;
24533
24534 /* See if this token is a keyword. */
24535 keyword = token->keyword;
24536 switch (keyword)
24537 {
24538 case RID_PUBLIC:
24539 case RID_PROTECTED:
24540 case RID_PRIVATE:
24541 /* Consume the access-specifier. */
24542 cp_lexer_consume_token (parser->lexer);
24543 /* Remember which access-specifier is active. */
24544 current_access_specifier = token->u.value;
24545 /* Look for the `:'. */
24546 cp_parser_require (parser, CPP_COLON, RT_COLON);
24547 break;
24548
24549 default:
24550 /* Accept #pragmas at class scope. */
24551 if (token->type == CPP_PRAGMA)
24552 {
24553 cp_parser_pragma (parser, pragma_member, NULL);
24554 break;
24555 }
24556
24557 /* Otherwise, the next construction must be a
24558 member-declaration. */
24559 cp_parser_member_declaration (parser);
24560 }
24561 }
24562 }
24563
24564 /* Parse a member-declaration.
24565
24566 member-declaration:
24567 decl-specifier-seq [opt] member-declarator-list [opt] ;
24568 function-definition ; [opt]
24569 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24570 using-declaration
24571 template-declaration
24572 alias-declaration
24573
24574 member-declarator-list:
24575 member-declarator
24576 member-declarator-list , member-declarator
24577
24578 member-declarator:
24579 declarator pure-specifier [opt]
24580 declarator constant-initializer [opt]
24581 identifier [opt] : constant-expression
24582
24583 GNU Extensions:
24584
24585 member-declaration:
24586 __extension__ member-declaration
24587
24588 member-declarator:
24589 declarator attributes [opt] pure-specifier [opt]
24590 declarator attributes [opt] constant-initializer [opt]
24591 identifier [opt] attributes [opt] : constant-expression
24592
24593 C++0x Extensions:
24594
24595 member-declaration:
24596 static_assert-declaration */
24597
24598 static void
24599 cp_parser_member_declaration (cp_parser* parser)
24600 {
24601 cp_decl_specifier_seq decl_specifiers;
24602 tree prefix_attributes;
24603 tree decl;
24604 int declares_class_or_enum;
24605 bool friend_p;
24606 cp_token *token = NULL;
24607 cp_token *decl_spec_token_start = NULL;
24608 cp_token *initializer_token_start = NULL;
24609 int saved_pedantic;
24610 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24611
24612 /* Check for the `__extension__' keyword. */
24613 if (cp_parser_extension_opt (parser, &saved_pedantic))
24614 {
24615 /* Recurse. */
24616 cp_parser_member_declaration (parser);
24617 /* Restore the old value of the PEDANTIC flag. */
24618 pedantic = saved_pedantic;
24619
24620 return;
24621 }
24622
24623 /* Check for a template-declaration. */
24624 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24625 {
24626 /* An explicit specialization here is an error condition, and we
24627 expect the specialization handler to detect and report this. */
24628 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
24629 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
24630 cp_parser_explicit_specialization (parser);
24631 else
24632 cp_parser_template_declaration (parser, /*member_p=*/true);
24633
24634 return;
24635 }
24636 /* Check for a template introduction. */
24637 else if (cp_parser_template_declaration_after_export (parser, true))
24638 return;
24639
24640 /* Check for a using-declaration. */
24641 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24642 {
24643 if (cxx_dialect < cxx11)
24644 {
24645 /* Parse the using-declaration. */
24646 cp_parser_using_declaration (parser,
24647 /*access_declaration_p=*/false);
24648 return;
24649 }
24650 else
24651 {
24652 tree decl;
24653 bool alias_decl_expected;
24654 cp_parser_parse_tentatively (parser);
24655 decl = cp_parser_alias_declaration (parser);
24656 /* Note that if we actually see the '=' token after the
24657 identifier, cp_parser_alias_declaration commits the
24658 tentative parse. In that case, we really expect an
24659 alias-declaration. Otherwise, we expect a using
24660 declaration. */
24661 alias_decl_expected =
24662 !cp_parser_uncommitted_to_tentative_parse_p (parser);
24663 cp_parser_parse_definitely (parser);
24664
24665 if (alias_decl_expected)
24666 finish_member_declaration (decl);
24667 else
24668 cp_parser_using_declaration (parser,
24669 /*access_declaration_p=*/false);
24670 return;
24671 }
24672 }
24673
24674 /* Check for @defs. */
24675 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
24676 {
24677 tree ivar, member;
24678 tree ivar_chains = cp_parser_objc_defs_expression (parser);
24679 ivar = ivar_chains;
24680 while (ivar)
24681 {
24682 member = ivar;
24683 ivar = TREE_CHAIN (member);
24684 TREE_CHAIN (member) = NULL_TREE;
24685 finish_member_declaration (member);
24686 }
24687 return;
24688 }
24689
24690 /* If the next token is `static_assert' we have a static assertion. */
24691 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
24692 {
24693 cp_parser_static_assert (parser, /*member_p=*/true);
24694 return;
24695 }
24696
24697 parser->colon_corrects_to_scope_p = false;
24698
24699 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
24700 goto out;
24701
24702 /* Parse the decl-specifier-seq. */
24703 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24704 cp_parser_decl_specifier_seq (parser,
24705 (CP_PARSER_FLAGS_OPTIONAL
24706 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
24707 &decl_specifiers,
24708 &declares_class_or_enum);
24709 /* Check for an invalid type-name. */
24710 if (!decl_specifiers.any_type_specifiers_p
24711 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24712 goto out;
24713 /* If there is no declarator, then the decl-specifier-seq should
24714 specify a type. */
24715 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24716 {
24717 /* If there was no decl-specifier-seq, and the next token is a
24718 `;', then we have something like:
24719
24720 struct S { ; };
24721
24722 [class.mem]
24723
24724 Each member-declaration shall declare at least one member
24725 name of the class. */
24726 if (!decl_specifiers.any_specifiers_p)
24727 {
24728 cp_token *token = cp_lexer_peek_token (parser->lexer);
24729 if (!in_system_header_at (token->location))
24730 {
24731 gcc_rich_location richloc (token->location);
24732 richloc.add_fixit_remove ();
24733 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
24734 }
24735 }
24736 else
24737 {
24738 tree type;
24739
24740 /* See if this declaration is a friend. */
24741 friend_p = cp_parser_friend_p (&decl_specifiers);
24742 /* If there were decl-specifiers, check to see if there was
24743 a class-declaration. */
24744 type = check_tag_decl (&decl_specifiers,
24745 /*explicit_type_instantiation_p=*/false);
24746 /* Nested classes have already been added to the class, but
24747 a `friend' needs to be explicitly registered. */
24748 if (friend_p)
24749 {
24750 /* If the `friend' keyword was present, the friend must
24751 be introduced with a class-key. */
24752 if (!declares_class_or_enum && cxx_dialect < cxx11)
24753 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
24754 "in C++03 a class-key must be used "
24755 "when declaring a friend");
24756 /* In this case:
24757
24758 template <typename T> struct A {
24759 friend struct A<T>::B;
24760 };
24761
24762 A<T>::B will be represented by a TYPENAME_TYPE, and
24763 therefore not recognized by check_tag_decl. */
24764 if (!type)
24765 {
24766 type = decl_specifiers.type;
24767 if (type && TREE_CODE (type) == TYPE_DECL)
24768 type = TREE_TYPE (type);
24769 }
24770 if (!type || !TYPE_P (type))
24771 error_at (decl_spec_token_start->location,
24772 "friend declaration does not name a class or "
24773 "function");
24774 else
24775 make_friend_class (current_class_type, type,
24776 /*complain=*/true);
24777 }
24778 /* If there is no TYPE, an error message will already have
24779 been issued. */
24780 else if (!type || type == error_mark_node)
24781 ;
24782 /* An anonymous aggregate has to be handled specially; such
24783 a declaration really declares a data member (with a
24784 particular type), as opposed to a nested class. */
24785 else if (ANON_AGGR_TYPE_P (type))
24786 {
24787 /* C++11 9.5/6. */
24788 if (decl_specifiers.storage_class != sc_none)
24789 error_at (decl_spec_token_start->location,
24790 "a storage class on an anonymous aggregate "
24791 "in class scope is not allowed");
24792
24793 /* Remove constructors and such from TYPE, now that we
24794 know it is an anonymous aggregate. */
24795 fixup_anonymous_aggr (type);
24796 /* And make the corresponding data member. */
24797 decl = build_decl (decl_spec_token_start->location,
24798 FIELD_DECL, NULL_TREE, type);
24799 /* Add it to the class. */
24800 finish_member_declaration (decl);
24801 }
24802 else
24803 cp_parser_check_access_in_redeclaration
24804 (TYPE_NAME (type),
24805 decl_spec_token_start->location);
24806 }
24807 }
24808 else
24809 {
24810 bool assume_semicolon = false;
24811
24812 /* Clear attributes from the decl_specifiers but keep them
24813 around as prefix attributes that apply them to the entity
24814 being declared. */
24815 prefix_attributes = decl_specifiers.attributes;
24816 decl_specifiers.attributes = NULL_TREE;
24817
24818 /* See if these declarations will be friends. */
24819 friend_p = cp_parser_friend_p (&decl_specifiers);
24820
24821 /* Keep going until we hit the `;' at the end of the
24822 declaration. */
24823 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24824 {
24825 tree attributes = NULL_TREE;
24826 tree first_attribute;
24827 tree initializer;
24828 bool named_bitfld = false;
24829
24830 /* Peek at the next token. */
24831 token = cp_lexer_peek_token (parser->lexer);
24832
24833 /* The following code wants to know early if it is a bit-field
24834 or some other declaration. Attributes can appear before
24835 the `:' token. Skip over them without consuming any tokens
24836 to peek if they are followed by `:'. */
24837 if (cp_next_tokens_can_be_attribute_p (parser)
24838 || (token->type == CPP_NAME
24839 && cp_nth_tokens_can_be_attribute_p (parser, 2)
24840 && (named_bitfld = true)))
24841 {
24842 size_t n
24843 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24844 token = cp_lexer_peek_nth_token (parser->lexer, n);
24845 }
24846
24847 /* Check for a bitfield declaration. */
24848 if (token->type == CPP_COLON
24849 || (token->type == CPP_NAME
24850 && token == cp_lexer_peek_token (parser->lexer)
24851 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24852 && (named_bitfld = true)))
24853 {
24854 tree identifier;
24855 tree width;
24856 tree late_attributes = NULL_TREE;
24857 location_t id_location
24858 = cp_lexer_peek_token (parser->lexer)->location;
24859
24860 if (named_bitfld)
24861 identifier = cp_parser_identifier (parser);
24862 else
24863 identifier = NULL_TREE;
24864
24865 /* Look for attributes that apply to the bitfield. */
24866 attributes = cp_parser_attributes_opt (parser);
24867
24868 /* Consume the `:' token. */
24869 cp_lexer_consume_token (parser->lexer);
24870
24871 /* Get the width of the bitfield. */
24872 width = cp_parser_constant_expression (parser, false, NULL,
24873 cxx_dialect >= cxx11);
24874
24875 /* In C++2A and as extension for C++11 and above we allow
24876 default member initializers for bit-fields. */
24877 initializer = NULL_TREE;
24878 if (cxx_dialect >= cxx11
24879 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24880 || cp_lexer_next_token_is (parser->lexer,
24881 CPP_OPEN_BRACE)))
24882 {
24883 location_t loc
24884 = cp_lexer_peek_token (parser->lexer)->location;
24885 if (cxx_dialect < cxx2a
24886 && identifier != NULL_TREE)
24887 pedwarn (loc, 0,
24888 "default member initializers for bit-fields "
24889 "only available with %<-std=c++2a%> or "
24890 "%<-std=gnu++2a%>");
24891
24892 initializer = cp_parser_save_nsdmi (parser);
24893 if (identifier == NULL_TREE)
24894 {
24895 error_at (loc, "default member initializer for "
24896 "unnamed bit-field");
24897 initializer = NULL_TREE;
24898 }
24899 }
24900 else
24901 {
24902 /* Look for attributes that apply to the bitfield after
24903 the `:' token and width. This is where GCC used to
24904 parse attributes in the past, pedwarn if there is
24905 a std attribute. */
24906 if (cp_next_tokens_can_be_std_attribute_p (parser))
24907 pedwarn (input_location, OPT_Wpedantic,
24908 "ISO C++ allows bit-field attributes only "
24909 "before the %<:%> token");
24910
24911 late_attributes = cp_parser_attributes_opt (parser);
24912 }
24913
24914 attributes = attr_chainon (attributes, late_attributes);
24915
24916 /* Remember which attributes are prefix attributes and
24917 which are not. */
24918 first_attribute = attributes;
24919 /* Combine the attributes. */
24920 attributes = attr_chainon (prefix_attributes, attributes);
24921
24922 /* Create the bitfield declaration. */
24923 decl = grokbitfield (identifier
24924 ? make_id_declarator (NULL_TREE,
24925 identifier,
24926 sfk_none,
24927 id_location)
24928 : NULL,
24929 &decl_specifiers,
24930 width, initializer,
24931 attributes);
24932 }
24933 else
24934 {
24935 cp_declarator *declarator;
24936 tree asm_specification;
24937 int ctor_dtor_or_conv_p;
24938 bool static_p = (decl_specifiers.storage_class == sc_static);
24939 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
24940 if (!friend_p
24941 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
24942 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
24943
24944 /* Parse the declarator. */
24945 declarator
24946 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24947 flags,
24948 &ctor_dtor_or_conv_p,
24949 /*parenthesized_p=*/NULL,
24950 /*member_p=*/true,
24951 friend_p, static_p);
24952
24953 /* If something went wrong parsing the declarator, make sure
24954 that we at least consume some tokens. */
24955 if (declarator == cp_error_declarator)
24956 {
24957 /* Skip to the end of the statement. */
24958 cp_parser_skip_to_end_of_statement (parser);
24959 /* If the next token is not a semicolon, that is
24960 probably because we just skipped over the body of
24961 a function. So, we consume a semicolon if
24962 present, but do not issue an error message if it
24963 is not present. */
24964 if (cp_lexer_next_token_is (parser->lexer,
24965 CPP_SEMICOLON))
24966 cp_lexer_consume_token (parser->lexer);
24967 goto out;
24968 }
24969
24970 if (declares_class_or_enum & 2)
24971 cp_parser_check_for_definition_in_return_type
24972 (declarator, decl_specifiers.type,
24973 decl_specifiers.locations[ds_type_spec]);
24974
24975 /* Look for an asm-specification. */
24976 asm_specification = cp_parser_asm_specification_opt (parser);
24977 /* Look for attributes that apply to the declaration. */
24978 attributes = cp_parser_attributes_opt (parser);
24979 /* Remember which attributes are prefix attributes and
24980 which are not. */
24981 first_attribute = attributes;
24982 /* Combine the attributes. */
24983 attributes = attr_chainon (prefix_attributes, attributes);
24984
24985 /* If it's an `=', then we have a constant-initializer or a
24986 pure-specifier. It is not correct to parse the
24987 initializer before registering the member declaration
24988 since the member declaration should be in scope while
24989 its initializer is processed. However, the rest of the
24990 front end does not yet provide an interface that allows
24991 us to handle this correctly. */
24992 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24993 {
24994 /* In [class.mem]:
24995
24996 A pure-specifier shall be used only in the declaration of
24997 a virtual function.
24998
24999 A member-declarator can contain a constant-initializer
25000 only if it declares a static member of integral or
25001 enumeration type.
25002
25003 Therefore, if the DECLARATOR is for a function, we look
25004 for a pure-specifier; otherwise, we look for a
25005 constant-initializer. When we call `grokfield', it will
25006 perform more stringent semantics checks. */
25007 initializer_token_start = cp_lexer_peek_token (parser->lexer);
25008 if (function_declarator_p (declarator)
25009 || (decl_specifiers.type
25010 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
25011 && declarator->kind == cdk_id
25012 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
25013 == FUNCTION_TYPE)))
25014 initializer = cp_parser_pure_specifier (parser);
25015 else if (decl_specifiers.storage_class != sc_static)
25016 initializer = cp_parser_save_nsdmi (parser);
25017 else if (cxx_dialect >= cxx11)
25018 {
25019 bool nonconst;
25020 /* Don't require a constant rvalue in C++11, since we
25021 might want a reference constant. We'll enforce
25022 constancy later. */
25023 cp_lexer_consume_token (parser->lexer);
25024 /* Parse the initializer. */
25025 initializer = cp_parser_initializer_clause (parser,
25026 &nonconst);
25027 }
25028 else
25029 /* Parse the initializer. */
25030 initializer = cp_parser_constant_initializer (parser);
25031 }
25032 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
25033 && !function_declarator_p (declarator))
25034 {
25035 bool x;
25036 if (decl_specifiers.storage_class != sc_static)
25037 initializer = cp_parser_save_nsdmi (parser);
25038 else
25039 initializer = cp_parser_initializer (parser, &x, &x);
25040 }
25041 /* Detect invalid bit-field cases such as
25042
25043 int *p : 4;
25044 int &&r : 3;
25045
25046 and similar. */
25047 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
25048 /* If there were no type specifiers, it was a
25049 constructor. */
25050 && decl_specifiers.any_type_specifiers_p)
25051 {
25052 /* This is called for a decent diagnostic only. */
25053 tree d = grokdeclarator (declarator, &decl_specifiers,
25054 BITFIELD, /*initialized=*/false,
25055 &attributes);
25056 if (!error_operand_p (d))
25057 error_at (DECL_SOURCE_LOCATION (d),
25058 "bit-field %qD has non-integral type %qT",
25059 d, TREE_TYPE (d));
25060 cp_parser_skip_to_end_of_statement (parser);
25061 /* Avoid "extra ;" pedwarns. */
25062 if (cp_lexer_next_token_is (parser->lexer,
25063 CPP_SEMICOLON))
25064 cp_lexer_consume_token (parser->lexer);
25065 goto out;
25066 }
25067 /* Otherwise, there is no initializer. */
25068 else
25069 initializer = NULL_TREE;
25070
25071 /* See if we are probably looking at a function
25072 definition. We are certainly not looking at a
25073 member-declarator. Calling `grokfield' has
25074 side-effects, so we must not do it unless we are sure
25075 that we are looking at a member-declarator. */
25076 if (cp_parser_token_starts_function_definition_p
25077 (cp_lexer_peek_token (parser->lexer)))
25078 {
25079 /* The grammar does not allow a pure-specifier to be
25080 used when a member function is defined. (It is
25081 possible that this fact is an oversight in the
25082 standard, since a pure function may be defined
25083 outside of the class-specifier. */
25084 if (initializer && initializer_token_start)
25085 error_at (initializer_token_start->location,
25086 "pure-specifier on function-definition");
25087 decl = cp_parser_save_member_function_body (parser,
25088 &decl_specifiers,
25089 declarator,
25090 attributes);
25091 if (parser->fully_implicit_function_template_p)
25092 decl = finish_fully_implicit_template (parser, decl);
25093 /* If the member was not a friend, declare it here. */
25094 if (!friend_p)
25095 finish_member_declaration (decl);
25096 /* Peek at the next token. */
25097 token = cp_lexer_peek_token (parser->lexer);
25098 /* If the next token is a semicolon, consume it. */
25099 if (token->type == CPP_SEMICOLON)
25100 {
25101 location_t semicolon_loc
25102 = cp_lexer_consume_token (parser->lexer)->location;
25103 gcc_rich_location richloc (semicolon_loc);
25104 richloc.add_fixit_remove ();
25105 warning_at (&richloc, OPT_Wextra_semi,
25106 "extra %<;%> after in-class "
25107 "function definition");
25108 }
25109 goto out;
25110 }
25111 else
25112 if (declarator->kind == cdk_function)
25113 declarator->id_loc = token->location;
25114 /* Create the declaration. */
25115 decl = grokfield (declarator, &decl_specifiers,
25116 initializer, /*init_const_expr_p=*/true,
25117 asm_specification, attributes);
25118 if (parser->fully_implicit_function_template_p)
25119 {
25120 if (friend_p)
25121 finish_fully_implicit_template (parser, 0);
25122 else
25123 decl = finish_fully_implicit_template (parser, decl);
25124 }
25125 }
25126
25127 cp_finalize_omp_declare_simd (parser, decl);
25128 cp_finalize_oacc_routine (parser, decl, false);
25129
25130 /* Reset PREFIX_ATTRIBUTES. */
25131 if (attributes != error_mark_node)
25132 {
25133 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25134 attributes = TREE_CHAIN (attributes);
25135 if (attributes)
25136 TREE_CHAIN (attributes) = NULL_TREE;
25137 }
25138
25139 /* If there is any qualification still in effect, clear it
25140 now; we will be starting fresh with the next declarator. */
25141 parser->scope = NULL_TREE;
25142 parser->qualifying_scope = NULL_TREE;
25143 parser->object_scope = NULL_TREE;
25144 /* If it's a `,', then there are more declarators. */
25145 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25146 {
25147 cp_lexer_consume_token (parser->lexer);
25148 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25149 {
25150 cp_token *token = cp_lexer_previous_token (parser->lexer);
25151 gcc_rich_location richloc (token->location);
25152 richloc.add_fixit_remove ();
25153 error_at (&richloc, "stray %<,%> at end of "
25154 "member declaration");
25155 }
25156 }
25157 /* If the next token isn't a `;', then we have a parse error. */
25158 else if (cp_lexer_next_token_is_not (parser->lexer,
25159 CPP_SEMICOLON))
25160 {
25161 /* The next token might be a ways away from where the
25162 actual semicolon is missing. Find the previous token
25163 and use that for our error position. */
25164 cp_token *token = cp_lexer_previous_token (parser->lexer);
25165 gcc_rich_location richloc (token->location);
25166 richloc.add_fixit_insert_after (";");
25167 error_at (&richloc, "expected %<;%> at end of "
25168 "member declaration");
25169
25170 /* Assume that the user meant to provide a semicolon. If
25171 we were to cp_parser_skip_to_end_of_statement, we might
25172 skip to a semicolon inside a member function definition
25173 and issue nonsensical error messages. */
25174 assume_semicolon = true;
25175 }
25176
25177 if (decl)
25178 {
25179 /* Add DECL to the list of members. */
25180 if (!friend_p
25181 /* Explicitly include, eg, NSDMIs, for better error
25182 recovery (c++/58650). */
25183 || !DECL_DECLARES_FUNCTION_P (decl))
25184 finish_member_declaration (decl);
25185
25186 if (TREE_CODE (decl) == FUNCTION_DECL)
25187 cp_parser_save_default_args (parser, decl);
25188 else if (TREE_CODE (decl) == FIELD_DECL
25189 && DECL_INITIAL (decl))
25190 /* Add DECL to the queue of NSDMI to be parsed later. */
25191 vec_safe_push (unparsed_nsdmis, decl);
25192 }
25193
25194 if (assume_semicolon)
25195 goto out;
25196 }
25197 }
25198
25199 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25200 out:
25201 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
25202 }
25203
25204 /* Parse a pure-specifier.
25205
25206 pure-specifier:
25207 = 0
25208
25209 Returns INTEGER_ZERO_NODE if a pure specifier is found.
25210 Otherwise, ERROR_MARK_NODE is returned. */
25211
25212 static tree
25213 cp_parser_pure_specifier (cp_parser* parser)
25214 {
25215 cp_token *token;
25216
25217 /* Look for the `=' token. */
25218 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25219 return error_mark_node;
25220 /* Look for the `0' token. */
25221 token = cp_lexer_peek_token (parser->lexer);
25222
25223 if (token->type == CPP_EOF
25224 || token->type == CPP_PRAGMA_EOL)
25225 return error_mark_node;
25226
25227 cp_lexer_consume_token (parser->lexer);
25228
25229 /* Accept = default or = delete in c++0x mode. */
25230 if (token->keyword == RID_DEFAULT
25231 || token->keyword == RID_DELETE)
25232 {
25233 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
25234 return token->u.value;
25235 }
25236
25237 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
25238 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
25239 {
25240 cp_parser_error (parser,
25241 "invalid pure specifier (only %<= 0%> is allowed)");
25242 cp_parser_skip_to_end_of_statement (parser);
25243 return error_mark_node;
25244 }
25245 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
25246 {
25247 error_at (token->location, "templates may not be %<virtual%>");
25248 return error_mark_node;
25249 }
25250
25251 return integer_zero_node;
25252 }
25253
25254 /* Parse a constant-initializer.
25255
25256 constant-initializer:
25257 = constant-expression
25258
25259 Returns a representation of the constant-expression. */
25260
25261 static tree
25262 cp_parser_constant_initializer (cp_parser* parser)
25263 {
25264 /* Look for the `=' token. */
25265 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25266 return error_mark_node;
25267
25268 /* It is invalid to write:
25269
25270 struct S { static const int i = { 7 }; };
25271
25272 */
25273 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25274 {
25275 cp_parser_error (parser,
25276 "a brace-enclosed initializer is not allowed here");
25277 /* Consume the opening brace. */
25278 matching_braces braces;
25279 braces.consume_open (parser);
25280 /* Skip the initializer. */
25281 cp_parser_skip_to_closing_brace (parser);
25282 /* Look for the trailing `}'. */
25283 braces.require_close (parser);
25284
25285 return error_mark_node;
25286 }
25287
25288 return cp_parser_constant_expression (parser);
25289 }
25290
25291 /* Derived classes [gram.class.derived] */
25292
25293 /* Parse a base-clause.
25294
25295 base-clause:
25296 : base-specifier-list
25297
25298 base-specifier-list:
25299 base-specifier ... [opt]
25300 base-specifier-list , base-specifier ... [opt]
25301
25302 Returns a TREE_LIST representing the base-classes, in the order in
25303 which they were declared. The representation of each node is as
25304 described by cp_parser_base_specifier.
25305
25306 In the case that no bases are specified, this function will return
25307 NULL_TREE, not ERROR_MARK_NODE. */
25308
25309 static tree
25310 cp_parser_base_clause (cp_parser* parser)
25311 {
25312 tree bases = NULL_TREE;
25313
25314 /* Look for the `:' that begins the list. */
25315 cp_parser_require (parser, CPP_COLON, RT_COLON);
25316
25317 /* Scan the base-specifier-list. */
25318 while (true)
25319 {
25320 cp_token *token;
25321 tree base;
25322 bool pack_expansion_p = false;
25323
25324 /* Look for the base-specifier. */
25325 base = cp_parser_base_specifier (parser);
25326 /* Look for the (optional) ellipsis. */
25327 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25328 {
25329 /* Consume the `...'. */
25330 cp_lexer_consume_token (parser->lexer);
25331
25332 pack_expansion_p = true;
25333 }
25334
25335 /* Add BASE to the front of the list. */
25336 if (base && base != error_mark_node)
25337 {
25338 if (pack_expansion_p)
25339 /* Make this a pack expansion type. */
25340 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
25341
25342 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
25343 {
25344 TREE_CHAIN (base) = bases;
25345 bases = base;
25346 }
25347 }
25348 /* Peek at the next token. */
25349 token = cp_lexer_peek_token (parser->lexer);
25350 /* If it's not a comma, then the list is complete. */
25351 if (token->type != CPP_COMMA)
25352 break;
25353 /* Consume the `,'. */
25354 cp_lexer_consume_token (parser->lexer);
25355 }
25356
25357 /* PARSER->SCOPE may still be non-NULL at this point, if the last
25358 base class had a qualified name. However, the next name that
25359 appears is certainly not qualified. */
25360 parser->scope = NULL_TREE;
25361 parser->qualifying_scope = NULL_TREE;
25362 parser->object_scope = NULL_TREE;
25363
25364 return nreverse (bases);
25365 }
25366
25367 /* Parse a base-specifier.
25368
25369 base-specifier:
25370 :: [opt] nested-name-specifier [opt] class-name
25371 virtual access-specifier [opt] :: [opt] nested-name-specifier
25372 [opt] class-name
25373 access-specifier virtual [opt] :: [opt] nested-name-specifier
25374 [opt] class-name
25375
25376 Returns a TREE_LIST. The TREE_PURPOSE will be one of
25377 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25378 indicate the specifiers provided. The TREE_VALUE will be a TYPE
25379 (or the ERROR_MARK_NODE) indicating the type that was specified. */
25380
25381 static tree
25382 cp_parser_base_specifier (cp_parser* parser)
25383 {
25384 cp_token *token;
25385 bool done = false;
25386 bool virtual_p = false;
25387 bool duplicate_virtual_error_issued_p = false;
25388 bool duplicate_access_error_issued_p = false;
25389 bool class_scope_p, template_p;
25390 tree access = access_default_node;
25391 tree type;
25392
25393 /* Process the optional `virtual' and `access-specifier'. */
25394 while (!done)
25395 {
25396 /* Peek at the next token. */
25397 token = cp_lexer_peek_token (parser->lexer);
25398 /* Process `virtual'. */
25399 switch (token->keyword)
25400 {
25401 case RID_VIRTUAL:
25402 /* If `virtual' appears more than once, issue an error. */
25403 if (virtual_p && !duplicate_virtual_error_issued_p)
25404 {
25405 cp_parser_error (parser,
25406 "%<virtual%> specified more than once in base-specifier");
25407 duplicate_virtual_error_issued_p = true;
25408 }
25409
25410 virtual_p = true;
25411
25412 /* Consume the `virtual' token. */
25413 cp_lexer_consume_token (parser->lexer);
25414
25415 break;
25416
25417 case RID_PUBLIC:
25418 case RID_PROTECTED:
25419 case RID_PRIVATE:
25420 /* If more than one access specifier appears, issue an
25421 error. */
25422 if (access != access_default_node
25423 && !duplicate_access_error_issued_p)
25424 {
25425 cp_parser_error (parser,
25426 "more than one access specifier in base-specifier");
25427 duplicate_access_error_issued_p = true;
25428 }
25429
25430 access = ridpointers[(int) token->keyword];
25431
25432 /* Consume the access-specifier. */
25433 cp_lexer_consume_token (parser->lexer);
25434
25435 break;
25436
25437 default:
25438 done = true;
25439 break;
25440 }
25441 }
25442 /* It is not uncommon to see programs mechanically, erroneously, use
25443 the 'typename' keyword to denote (dependent) qualified types
25444 as base classes. */
25445 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25446 {
25447 token = cp_lexer_peek_token (parser->lexer);
25448 if (!processing_template_decl)
25449 error_at (token->location,
25450 "keyword %<typename%> not allowed outside of templates");
25451 else
25452 error_at (token->location,
25453 "keyword %<typename%> not allowed in this context "
25454 "(the base class is implicitly a type)");
25455 cp_lexer_consume_token (parser->lexer);
25456 }
25457
25458 /* Look for the optional `::' operator. */
25459 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
25460 /* Look for the nested-name-specifier. The simplest way to
25461 implement:
25462
25463 [temp.res]
25464
25465 The keyword `typename' is not permitted in a base-specifier or
25466 mem-initializer; in these contexts a qualified name that
25467 depends on a template-parameter is implicitly assumed to be a
25468 type name.
25469
25470 is to pretend that we have seen the `typename' keyword at this
25471 point. */
25472 cp_parser_nested_name_specifier_opt (parser,
25473 /*typename_keyword_p=*/true,
25474 /*check_dependency_p=*/true,
25475 /*type_p=*/true,
25476 /*is_declaration=*/true);
25477 /* If the base class is given by a qualified name, assume that names
25478 we see are type names or templates, as appropriate. */
25479 class_scope_p = (parser->scope && TYPE_P (parser->scope));
25480 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
25481
25482 if (!parser->scope
25483 && cp_lexer_next_token_is_decltype (parser->lexer))
25484 /* DR 950 allows decltype as a base-specifier. */
25485 type = cp_parser_decltype (parser);
25486 else
25487 {
25488 /* Otherwise, look for the class-name. */
25489 type = cp_parser_class_name (parser,
25490 class_scope_p,
25491 template_p,
25492 typename_type,
25493 /*check_dependency_p=*/true,
25494 /*class_head_p=*/false,
25495 /*is_declaration=*/true);
25496 type = TREE_TYPE (type);
25497 }
25498
25499 if (type == error_mark_node)
25500 return error_mark_node;
25501
25502 return finish_base_specifier (type, access, virtual_p);
25503 }
25504
25505 /* Exception handling [gram.exception] */
25506
25507 /* Save the tokens that make up the noexcept-specifier for a member-function.
25508 Returns a DEFERRED_PARSE. */
25509
25510 static tree
25511 cp_parser_save_noexcept (cp_parser *parser)
25512 {
25513 cp_token *first = parser->lexer->next_token;
25514 /* We want everything up to, including, the final ')'. */
25515 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
25516 cp_token *last = parser->lexer->next_token;
25517
25518 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
25519 to carry the information we will need. */
25520 tree expr = make_node (DEFERRED_PARSE);
25521 /* Save away the noexcept-specifier; we will process it when the
25522 class is complete. */
25523 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
25524 expr = build_tree_list (expr, NULL_TREE);
25525 return expr;
25526 }
25527
25528 /* Used for late processing of noexcept-specifiers of member-functions.
25529 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
25530 we saved for later; parse it now. */
25531
25532 static tree
25533 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
25534 {
25535 /* Make sure we've gotten something that hasn't been parsed yet. */
25536 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
25537
25538 push_unparsed_function_queues (parser);
25539
25540 /* Push the saved tokens for the noexcept-specifier onto the parser's
25541 lexer stack. */
25542 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
25543 cp_parser_push_lexer_for_tokens (parser, tokens);
25544
25545 /* Parse the cached noexcept-specifier. */
25546 tree parsed_arg
25547 = cp_parser_noexcept_specification_opt (parser,
25548 CP_PARSER_FLAGS_NONE,
25549 /*require_constexpr=*/true,
25550 /*consumed_expr=*/NULL,
25551 /*return_cond=*/false);
25552
25553 /* Revert to the main lexer. */
25554 cp_parser_pop_lexer (parser);
25555
25556 /* Restore the queue. */
25557 pop_unparsed_function_queues (parser);
25558
25559 /* And we're done. */
25560 return parsed_arg;
25561 }
25562
25563 /* Perform late checking of overriding function with respect to their
25564 noexcept-specifiers. TYPE is the class and FNDECL is the function
25565 that potentially overrides some virtual function with the same
25566 signature. */
25567
25568 static void
25569 noexcept_override_late_checks (tree type, tree fndecl)
25570 {
25571 tree binfo = TYPE_BINFO (type);
25572 tree base_binfo;
25573
25574 if (DECL_STATIC_FUNCTION_P (fndecl))
25575 return;
25576
25577 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
25578 {
25579 tree basetype = BINFO_TYPE (base_binfo);
25580
25581 if (!TYPE_POLYMORPHIC_P (basetype))
25582 continue;
25583
25584 tree fn = look_for_overrides_here (basetype, fndecl);
25585 if (fn)
25586 maybe_check_overriding_exception_spec (fndecl, fn);
25587 }
25588 }
25589
25590 /* Parse an (optional) noexcept-specification.
25591
25592 noexcept-specification:
25593 noexcept ( constant-expression ) [opt]
25594
25595 If no noexcept-specification is present, returns NULL_TREE.
25596 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25597 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25598 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25599 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25600 in which case a boolean condition is returned instead. The parser flags
25601 FLAGS is used to control parsing. */
25602
25603 static tree
25604 cp_parser_noexcept_specification_opt (cp_parser* parser,
25605 cp_parser_flags flags,
25606 bool require_constexpr,
25607 bool* consumed_expr,
25608 bool return_cond)
25609 {
25610 cp_token *token;
25611 const char *saved_message;
25612
25613 /* Peek at the next token. */
25614 token = cp_lexer_peek_token (parser->lexer);
25615
25616 /* Is it a noexcept-specification? */
25617 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
25618 {
25619 tree expr;
25620
25621 /* [class.mem]/6 says that a noexcept-specifer (within the
25622 member-specification of the class) is a complete-class context of
25623 a class. So, if the noexcept-specifier has the optional expression,
25624 just save the tokens, and reparse this after we're done with the
25625 class. */
25626 const bool literal_p
25627 = ((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
25628 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
25629 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN));
25630
25631 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
25632 /* No need to delay parsing for a number literal or true/false. */
25633 && !literal_p
25634 && at_class_scope_p ()
25635 /* We don't delay parsing for friend member functions,
25636 alias-declarations, and typedefs, even though the standard seems
25637 to require it. */
25638 && (flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
25639 && TYPE_BEING_DEFINED (current_class_type)
25640 && !LAMBDA_TYPE_P (current_class_type))
25641 return cp_parser_save_noexcept (parser);
25642
25643 cp_lexer_consume_token (parser->lexer);
25644
25645 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
25646 {
25647 matching_parens parens;
25648 parens.consume_open (parser);
25649
25650 tree save_ccp = current_class_ptr;
25651 tree save_ccr = current_class_ref;
25652
25653 if (current_class_type)
25654 inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
25655
25656 if (require_constexpr)
25657 {
25658 /* Types may not be defined in an exception-specification. */
25659 saved_message = parser->type_definition_forbidden_message;
25660 parser->type_definition_forbidden_message
25661 = G_("types may not be defined in an exception-specification");
25662
25663 bool non_constant_p;
25664 expr
25665 = cp_parser_constant_expression (parser,
25666 /*allow_non_constant=*/true,
25667 &non_constant_p);
25668 if (non_constant_p
25669 && !require_potential_rvalue_constant_expression (expr))
25670 {
25671 expr = NULL_TREE;
25672 return_cond = true;
25673 }
25674
25675 /* Restore the saved message. */
25676 parser->type_definition_forbidden_message = saved_message;
25677 }
25678 else
25679 {
25680 expr = cp_parser_expression (parser);
25681 *consumed_expr = true;
25682 }
25683
25684 parens.require_close (parser);
25685
25686 current_class_ptr = save_ccp;
25687 current_class_ref = save_ccr;
25688 }
25689 else
25690 {
25691 expr = boolean_true_node;
25692 if (!require_constexpr)
25693 *consumed_expr = false;
25694 }
25695
25696 /* We cannot build a noexcept-spec right away because this will check
25697 that expr is a constexpr. */
25698 if (!return_cond)
25699 return build_noexcept_spec (expr, tf_warning_or_error);
25700 else
25701 return expr;
25702 }
25703 else
25704 return NULL_TREE;
25705 }
25706
25707 /* Parse an (optional) exception-specification.
25708
25709 exception-specification:
25710 throw ( type-id-list [opt] )
25711
25712 Returns a TREE_LIST representing the exception-specification. The
25713 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
25714 control parsing. */
25715
25716 static tree
25717 cp_parser_exception_specification_opt (cp_parser* parser, cp_parser_flags flags)
25718 {
25719 cp_token *token;
25720 tree type_id_list;
25721 const char *saved_message;
25722
25723 /* Peek at the next token. */
25724 token = cp_lexer_peek_token (parser->lexer);
25725
25726 /* Is it a noexcept-specification? */
25727 type_id_list
25728 = cp_parser_noexcept_specification_opt (parser, flags,
25729 /*require_constexpr=*/true,
25730 /*consumed_expr=*/NULL,
25731 /*return_cond=*/false);
25732 if (type_id_list != NULL_TREE)
25733 return type_id_list;
25734
25735 /* If it's not `throw', then there's no exception-specification. */
25736 if (!cp_parser_is_keyword (token, RID_THROW))
25737 return NULL_TREE;
25738
25739 location_t loc = token->location;
25740
25741 /* Consume the `throw'. */
25742 cp_lexer_consume_token (parser->lexer);
25743
25744 /* Look for the `('. */
25745 matching_parens parens;
25746 parens.require_open (parser);
25747
25748 /* Peek at the next token. */
25749 token = cp_lexer_peek_token (parser->lexer);
25750 /* If it's not a `)', then there is a type-id-list. */
25751 if (token->type != CPP_CLOSE_PAREN)
25752 {
25753 /* Types may not be defined in an exception-specification. */
25754 saved_message = parser->type_definition_forbidden_message;
25755 parser->type_definition_forbidden_message
25756 = G_("types may not be defined in an exception-specification");
25757 /* Parse the type-id-list. */
25758 type_id_list = cp_parser_type_id_list (parser);
25759 /* Restore the saved message. */
25760 parser->type_definition_forbidden_message = saved_message;
25761
25762 if (cxx_dialect >= cxx17)
25763 {
25764 error_at (loc, "ISO C++17 does not allow dynamic exception "
25765 "specifications");
25766 type_id_list = NULL_TREE;
25767 }
25768 else if (cxx_dialect >= cxx11)
25769 warning_at (loc, OPT_Wdeprecated,
25770 "dynamic exception specifications are deprecated in "
25771 "C++11");
25772 }
25773 /* In C++17, throw() is equivalent to noexcept (true). throw()
25774 is deprecated in C++11 and above as well, but is still widely used,
25775 so don't warn about it yet. */
25776 else if (cxx_dialect >= cxx17)
25777 type_id_list = noexcept_true_spec;
25778 else
25779 type_id_list = empty_except_spec;
25780
25781 /* Look for the `)'. */
25782 parens.require_close (parser);
25783
25784 return type_id_list;
25785 }
25786
25787 /* Parse an (optional) type-id-list.
25788
25789 type-id-list:
25790 type-id ... [opt]
25791 type-id-list , type-id ... [opt]
25792
25793 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25794 in the order that the types were presented. */
25795
25796 static tree
25797 cp_parser_type_id_list (cp_parser* parser)
25798 {
25799 tree types = NULL_TREE;
25800
25801 while (true)
25802 {
25803 cp_token *token;
25804 tree type;
25805
25806 token = cp_lexer_peek_token (parser->lexer);
25807
25808 /* Get the next type-id. */
25809 type = cp_parser_type_id (parser);
25810 /* Check for invalid 'auto'. */
25811 if (flag_concepts && type_uses_auto (type))
25812 {
25813 error_at (token->location,
25814 "invalid use of %<auto%> in exception-specification");
25815 type = error_mark_node;
25816 }
25817 /* Parse the optional ellipsis. */
25818 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25819 {
25820 /* Consume the `...'. */
25821 cp_lexer_consume_token (parser->lexer);
25822
25823 /* Turn the type into a pack expansion expression. */
25824 type = make_pack_expansion (type);
25825 }
25826 /* Add it to the list. */
25827 types = add_exception_specifier (types, type, /*complain=*/1);
25828 /* Peek at the next token. */
25829 token = cp_lexer_peek_token (parser->lexer);
25830 /* If it is not a `,', we are done. */
25831 if (token->type != CPP_COMMA)
25832 break;
25833 /* Consume the `,'. */
25834 cp_lexer_consume_token (parser->lexer);
25835 }
25836
25837 return nreverse (types);
25838 }
25839
25840 /* Parse a try-block.
25841
25842 try-block:
25843 try compound-statement handler-seq */
25844
25845 static tree
25846 cp_parser_try_block (cp_parser* parser)
25847 {
25848 tree try_block;
25849
25850 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
25851 if (parser->in_function_body
25852 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25853 && cxx_dialect < cxx2a)
25854 pedwarn (input_location, 0,
25855 "%<try%> in %<constexpr%> function only "
25856 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
25857
25858 try_block = begin_try_block ();
25859 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
25860 finish_try_block (try_block);
25861 cp_parser_handler_seq (parser);
25862 finish_handler_sequence (try_block);
25863
25864 return try_block;
25865 }
25866
25867 /* Parse a function-try-block.
25868
25869 function-try-block:
25870 try ctor-initializer [opt] function-body handler-seq */
25871
25872 static void
25873 cp_parser_function_try_block (cp_parser* parser)
25874 {
25875 tree compound_stmt;
25876 tree try_block;
25877
25878 /* Look for the `try' keyword. */
25879 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
25880 return;
25881 /* Let the rest of the front end know where we are. */
25882 try_block = begin_function_try_block (&compound_stmt);
25883 /* Parse the function-body. */
25884 cp_parser_ctor_initializer_opt_and_function_body
25885 (parser, /*in_function_try_block=*/true);
25886 /* We're done with the `try' part. */
25887 finish_function_try_block (try_block);
25888 /* Parse the handlers. */
25889 cp_parser_handler_seq (parser);
25890 /* We're done with the handlers. */
25891 finish_function_handler_sequence (try_block, compound_stmt);
25892 }
25893
25894 /* Parse a handler-seq.
25895
25896 handler-seq:
25897 handler handler-seq [opt] */
25898
25899 static void
25900 cp_parser_handler_seq (cp_parser* parser)
25901 {
25902 while (true)
25903 {
25904 cp_token *token;
25905
25906 /* Parse the handler. */
25907 cp_parser_handler (parser);
25908 /* Peek at the next token. */
25909 token = cp_lexer_peek_token (parser->lexer);
25910 /* If it's not `catch' then there are no more handlers. */
25911 if (!cp_parser_is_keyword (token, RID_CATCH))
25912 break;
25913 }
25914 }
25915
25916 /* Parse a handler.
25917
25918 handler:
25919 catch ( exception-declaration ) compound-statement */
25920
25921 static void
25922 cp_parser_handler (cp_parser* parser)
25923 {
25924 tree handler;
25925 tree declaration;
25926
25927 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
25928 handler = begin_handler ();
25929 matching_parens parens;
25930 parens.require_open (parser);
25931 declaration = cp_parser_exception_declaration (parser);
25932 finish_handler_parms (declaration, handler);
25933 parens.require_close (parser);
25934 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
25935 finish_handler (handler);
25936 }
25937
25938 /* Parse an exception-declaration.
25939
25940 exception-declaration:
25941 type-specifier-seq declarator
25942 type-specifier-seq abstract-declarator
25943 type-specifier-seq
25944 ...
25945
25946 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25947 ellipsis variant is used. */
25948
25949 static tree
25950 cp_parser_exception_declaration (cp_parser* parser)
25951 {
25952 cp_decl_specifier_seq type_specifiers;
25953 cp_declarator *declarator;
25954 const char *saved_message;
25955
25956 /* If it's an ellipsis, it's easy to handle. */
25957 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25958 {
25959 /* Consume the `...' token. */
25960 cp_lexer_consume_token (parser->lexer);
25961 return NULL_TREE;
25962 }
25963
25964 /* Types may not be defined in exception-declarations. */
25965 saved_message = parser->type_definition_forbidden_message;
25966 parser->type_definition_forbidden_message
25967 = G_("types may not be defined in exception-declarations");
25968
25969 /* Parse the type-specifier-seq. */
25970 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
25971 /*is_declaration=*/true,
25972 /*is_trailing_return=*/false,
25973 &type_specifiers);
25974 /* If it's a `)', then there is no declarator. */
25975 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25976 declarator = NULL;
25977 else
25978 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
25979 CP_PARSER_FLAGS_NONE,
25980 /*ctor_dtor_or_conv_p=*/NULL,
25981 /*parenthesized_p=*/NULL,
25982 /*member_p=*/false,
25983 /*friend_p=*/false,
25984 /*static_p=*/false);
25985
25986 /* Restore the saved message. */
25987 parser->type_definition_forbidden_message = saved_message;
25988
25989 if (!type_specifiers.any_specifiers_p)
25990 return error_mark_node;
25991
25992 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
25993 }
25994
25995 /* Parse a throw-expression.
25996
25997 throw-expression:
25998 throw assignment-expression [opt]
25999
26000 Returns a THROW_EXPR representing the throw-expression. */
26001
26002 static tree
26003 cp_parser_throw_expression (cp_parser* parser)
26004 {
26005 tree expression;
26006 cp_token* token;
26007 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
26008
26009 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
26010 token = cp_lexer_peek_token (parser->lexer);
26011 /* Figure out whether or not there is an assignment-expression
26012 following the "throw" keyword. */
26013 if (token->type == CPP_COMMA
26014 || token->type == CPP_SEMICOLON
26015 || token->type == CPP_CLOSE_PAREN
26016 || token->type == CPP_CLOSE_SQUARE
26017 || token->type == CPP_CLOSE_BRACE
26018 || token->type == CPP_COLON)
26019 expression = NULL_TREE;
26020 else
26021 expression = cp_parser_assignment_expression (parser);
26022
26023 /* Construct a location e.g.:
26024 throw x
26025 ^~~~~~~
26026 with caret == start at the start of the "throw" token, and
26027 the end at the end of the final token we consumed. */
26028 location_t combined_loc = make_location (start_loc, start_loc,
26029 parser->lexer);
26030 expression = build_throw (expression);
26031 protected_set_expr_location (expression, combined_loc);
26032
26033 return expression;
26034 }
26035
26036 /* GNU Extensions */
26037
26038 /* Parse an (optional) asm-specification.
26039
26040 asm-specification:
26041 asm ( string-literal )
26042
26043 If the asm-specification is present, returns a STRING_CST
26044 corresponding to the string-literal. Otherwise, returns
26045 NULL_TREE. */
26046
26047 static tree
26048 cp_parser_asm_specification_opt (cp_parser* parser)
26049 {
26050 cp_token *token;
26051 tree asm_specification;
26052
26053 /* Peek at the next token. */
26054 token = cp_lexer_peek_token (parser->lexer);
26055 /* If the next token isn't the `asm' keyword, then there's no
26056 asm-specification. */
26057 if (!cp_parser_is_keyword (token, RID_ASM))
26058 return NULL_TREE;
26059
26060 /* Consume the `asm' token. */
26061 cp_lexer_consume_token (parser->lexer);
26062 /* Look for the `('. */
26063 matching_parens parens;
26064 parens.require_open (parser);
26065
26066 /* Look for the string-literal. */
26067 asm_specification = cp_parser_string_literal (parser, false, false);
26068
26069 /* Look for the `)'. */
26070 parens.require_close (parser);
26071
26072 return asm_specification;
26073 }
26074
26075 /* Parse an asm-operand-list.
26076
26077 asm-operand-list:
26078 asm-operand
26079 asm-operand-list , asm-operand
26080
26081 asm-operand:
26082 string-literal ( expression )
26083 [ string-literal ] string-literal ( expression )
26084
26085 Returns a TREE_LIST representing the operands. The TREE_VALUE of
26086 each node is the expression. The TREE_PURPOSE is itself a
26087 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
26088 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
26089 is a STRING_CST for the string literal before the parenthesis. Returns
26090 ERROR_MARK_NODE if any of the operands are invalid. */
26091
26092 static tree
26093 cp_parser_asm_operand_list (cp_parser* parser)
26094 {
26095 tree asm_operands = NULL_TREE;
26096 bool invalid_operands = false;
26097
26098 while (true)
26099 {
26100 tree string_literal;
26101 tree expression;
26102 tree name;
26103
26104 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
26105 {
26106 /* Consume the `[' token. */
26107 cp_lexer_consume_token (parser->lexer);
26108 /* Read the operand name. */
26109 name = cp_parser_identifier (parser);
26110 if (name != error_mark_node)
26111 name = build_string (IDENTIFIER_LENGTH (name),
26112 IDENTIFIER_POINTER (name));
26113 /* Look for the closing `]'. */
26114 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26115 }
26116 else
26117 name = NULL_TREE;
26118 /* Look for the string-literal. */
26119 string_literal = cp_parser_string_literal (parser, false, false);
26120
26121 /* Look for the `('. */
26122 matching_parens parens;
26123 parens.require_open (parser);
26124 /* Parse the expression. */
26125 expression = cp_parser_expression (parser);
26126 /* Look for the `)'. */
26127 parens.require_close (parser);
26128
26129 if (name == error_mark_node
26130 || string_literal == error_mark_node
26131 || expression == error_mark_node)
26132 invalid_operands = true;
26133
26134 /* Add this operand to the list. */
26135 asm_operands = tree_cons (build_tree_list (name, string_literal),
26136 expression,
26137 asm_operands);
26138 /* If the next token is not a `,', there are no more
26139 operands. */
26140 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26141 break;
26142 /* Consume the `,'. */
26143 cp_lexer_consume_token (parser->lexer);
26144 }
26145
26146 return invalid_operands ? error_mark_node : nreverse (asm_operands);
26147 }
26148
26149 /* Parse an asm-clobber-list.
26150
26151 asm-clobber-list:
26152 string-literal
26153 asm-clobber-list , string-literal
26154
26155 Returns a TREE_LIST, indicating the clobbers in the order that they
26156 appeared. The TREE_VALUE of each node is a STRING_CST. */
26157
26158 static tree
26159 cp_parser_asm_clobber_list (cp_parser* parser)
26160 {
26161 tree clobbers = NULL_TREE;
26162
26163 while (true)
26164 {
26165 tree string_literal;
26166
26167 /* Look for the string literal. */
26168 string_literal = cp_parser_string_literal (parser, false, false);
26169 /* Add it to the list. */
26170 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
26171 /* If the next token is not a `,', then the list is
26172 complete. */
26173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26174 break;
26175 /* Consume the `,' token. */
26176 cp_lexer_consume_token (parser->lexer);
26177 }
26178
26179 return clobbers;
26180 }
26181
26182 /* Parse an asm-label-list.
26183
26184 asm-label-list:
26185 identifier
26186 asm-label-list , identifier
26187
26188 Returns a TREE_LIST, indicating the labels in the order that they
26189 appeared. The TREE_VALUE of each node is a label. */
26190
26191 static tree
26192 cp_parser_asm_label_list (cp_parser* parser)
26193 {
26194 tree labels = NULL_TREE;
26195
26196 while (true)
26197 {
26198 tree identifier, label, name;
26199
26200 /* Look for the identifier. */
26201 identifier = cp_parser_identifier (parser);
26202 if (!error_operand_p (identifier))
26203 {
26204 label = lookup_label (identifier);
26205 if (TREE_CODE (label) == LABEL_DECL)
26206 {
26207 TREE_USED (label) = 1;
26208 check_goto (label);
26209 name = build_string (IDENTIFIER_LENGTH (identifier),
26210 IDENTIFIER_POINTER (identifier));
26211 labels = tree_cons (name, label, labels);
26212 }
26213 }
26214 /* If the next token is not a `,', then the list is
26215 complete. */
26216 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26217 break;
26218 /* Consume the `,' token. */
26219 cp_lexer_consume_token (parser->lexer);
26220 }
26221
26222 return nreverse (labels);
26223 }
26224
26225 /* Return TRUE iff the next tokens in the stream are possibly the
26226 beginning of a GNU extension attribute. */
26227
26228 static bool
26229 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
26230 {
26231 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
26232 }
26233
26234 /* Return TRUE iff the next tokens in the stream are possibly the
26235 beginning of a standard C++-11 attribute specifier. */
26236
26237 static bool
26238 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
26239 {
26240 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
26241 }
26242
26243 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26244 beginning of a standard C++-11 attribute specifier. */
26245
26246 static bool
26247 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
26248 {
26249 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
26250
26251 return (cxx_dialect >= cxx11
26252 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
26253 || (token->type == CPP_OPEN_SQUARE
26254 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
26255 && token->type == CPP_OPEN_SQUARE)));
26256 }
26257
26258 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26259 beginning of a GNU extension attribute. */
26260
26261 static bool
26262 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
26263 {
26264 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
26265
26266 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
26267 }
26268
26269 /* Return true iff the next tokens can be the beginning of either a
26270 GNU attribute list, or a standard C++11 attribute sequence. */
26271
26272 static bool
26273 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
26274 {
26275 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
26276 || cp_next_tokens_can_be_std_attribute_p (parser));
26277 }
26278
26279 /* Return true iff the next Nth tokens can be the beginning of either
26280 a GNU attribute list, or a standard C++11 attribute sequence. */
26281
26282 static bool
26283 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
26284 {
26285 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
26286 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
26287 }
26288
26289 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
26290 of GNU attributes, or return NULL. */
26291
26292 static tree
26293 cp_parser_attributes_opt (cp_parser *parser)
26294 {
26295 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
26296 return cp_parser_gnu_attributes_opt (parser);
26297 return cp_parser_std_attribute_spec_seq (parser);
26298 }
26299
26300 /* Parse an (optional) series of attributes.
26301
26302 attributes:
26303 attributes attribute
26304
26305 attribute:
26306 __attribute__ (( attribute-list [opt] ))
26307
26308 The return value is as for cp_parser_gnu_attribute_list. */
26309
26310 static tree
26311 cp_parser_gnu_attributes_opt (cp_parser* parser)
26312 {
26313 tree attributes = NULL_TREE;
26314
26315 temp_override<bool> cleanup
26316 (parser->auto_is_implicit_function_template_parm_p, false);
26317
26318 while (true)
26319 {
26320 cp_token *token;
26321 tree attribute_list;
26322 bool ok = true;
26323
26324 /* Peek at the next token. */
26325 token = cp_lexer_peek_token (parser->lexer);
26326 /* If it's not `__attribute__', then we're done. */
26327 if (token->keyword != RID_ATTRIBUTE)
26328 break;
26329
26330 /* Consume the `__attribute__' keyword. */
26331 cp_lexer_consume_token (parser->lexer);
26332 /* Look for the two `(' tokens. */
26333 matching_parens outer_parens;
26334 if (!outer_parens.require_open (parser))
26335 ok = false;
26336 matching_parens inner_parens;
26337 if (!inner_parens.require_open (parser))
26338 ok = false;
26339
26340 /* Peek at the next token. */
26341 token = cp_lexer_peek_token (parser->lexer);
26342 if (token->type != CPP_CLOSE_PAREN)
26343 /* Parse the attribute-list. */
26344 attribute_list = cp_parser_gnu_attribute_list (parser);
26345 else
26346 /* If the next token is a `)', then there is no attribute
26347 list. */
26348 attribute_list = NULL;
26349
26350 /* Look for the two `)' tokens. */
26351 if (!inner_parens.require_close (parser))
26352 ok = false;
26353 if (!outer_parens.require_close (parser))
26354 ok = false;
26355 if (!ok)
26356 cp_parser_skip_to_end_of_statement (parser);
26357
26358 /* Add these new attributes to the list. */
26359 attributes = attr_chainon (attributes, attribute_list);
26360 }
26361
26362 return attributes;
26363 }
26364
26365 /* Parse a GNU attribute-list.
26366
26367 attribute-list:
26368 attribute
26369 attribute-list , attribute
26370
26371 attribute:
26372 identifier
26373 identifier ( identifier )
26374 identifier ( identifier , expression-list )
26375 identifier ( expression-list )
26376
26377 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
26378 to an attribute. The TREE_PURPOSE of each node is the identifier
26379 indicating which attribute is in use. The TREE_VALUE represents
26380 the arguments, if any. */
26381
26382 static tree
26383 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
26384 {
26385 tree attribute_list = NULL_TREE;
26386 bool save_translate_strings_p = parser->translate_strings_p;
26387
26388 /* Don't create wrapper nodes within attributes: the
26389 handlers don't know how to handle them. */
26390 auto_suppress_location_wrappers sentinel;
26391
26392 parser->translate_strings_p = false;
26393 while (true)
26394 {
26395 cp_token *token;
26396 tree identifier;
26397 tree attribute;
26398
26399 /* Look for the identifier. We also allow keywords here; for
26400 example `__attribute__ ((const))' is legal. */
26401 token = cp_lexer_peek_token (parser->lexer);
26402 if (token->type == CPP_NAME
26403 || token->type == CPP_KEYWORD)
26404 {
26405 tree arguments = NULL_TREE;
26406
26407 /* Consume the token, but save it since we need it for the
26408 SIMD enabled function parsing. */
26409 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
26410
26411 /* Save away the identifier that indicates which attribute
26412 this is. */
26413 identifier = (token->type == CPP_KEYWORD)
26414 /* For keywords, use the canonical spelling, not the
26415 parsed identifier. */
26416 ? ridpointers[(int) token->keyword]
26417 : id_token->u.value;
26418
26419 identifier = canonicalize_attr_name (identifier);
26420 attribute = build_tree_list (identifier, NULL_TREE);
26421
26422 /* Peek at the next token. */
26423 token = cp_lexer_peek_token (parser->lexer);
26424 /* If it's an `(', then parse the attribute arguments. */
26425 if (token->type == CPP_OPEN_PAREN)
26426 {
26427 vec<tree, va_gc> *vec;
26428 int attr_flag = (attribute_takes_identifier_p (identifier)
26429 ? id_attr : normal_attr);
26430 vec = cp_parser_parenthesized_expression_list
26431 (parser, attr_flag, /*cast_p=*/false,
26432 /*allow_expansion_p=*/false,
26433 /*non_constant_p=*/NULL);
26434 if (vec == NULL)
26435 arguments = error_mark_node;
26436 else
26437 {
26438 arguments = build_tree_list_vec (vec);
26439 release_tree_vector (vec);
26440 }
26441 /* Save the arguments away. */
26442 TREE_VALUE (attribute) = arguments;
26443 }
26444
26445 if (arguments != error_mark_node)
26446 {
26447 /* Add this attribute to the list. */
26448 TREE_CHAIN (attribute) = attribute_list;
26449 attribute_list = attribute;
26450 }
26451
26452 token = cp_lexer_peek_token (parser->lexer);
26453 }
26454 /* Unless EXACTLY_ONE is set look for more attributes.
26455 If the next token isn't a `,', we're done. */
26456 if (exactly_one || token->type != CPP_COMMA)
26457 break;
26458
26459 /* Consume the comma and keep going. */
26460 cp_lexer_consume_token (parser->lexer);
26461 }
26462 parser->translate_strings_p = save_translate_strings_p;
26463
26464 /* We built up the list in reverse order. */
26465 return nreverse (attribute_list);
26466 }
26467
26468 /* Parse a standard C++11 attribute.
26469
26470 The returned representation is a TREE_LIST which TREE_PURPOSE is
26471 the scoped name of the attribute, and the TREE_VALUE is its
26472 arguments list.
26473
26474 Note that the scoped name of the attribute is itself a TREE_LIST
26475 which TREE_PURPOSE is the namespace of the attribute, and
26476 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
26477 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
26478 and which TREE_PURPOSE is directly the attribute name.
26479
26480 Clients of the attribute code should use get_attribute_namespace
26481 and get_attribute_name to get the actual namespace and name of
26482 attributes, regardless of their being GNU or C++11 attributes.
26483
26484 attribute:
26485 attribute-token attribute-argument-clause [opt]
26486
26487 attribute-token:
26488 identifier
26489 attribute-scoped-token
26490
26491 attribute-scoped-token:
26492 attribute-namespace :: identifier
26493
26494 attribute-namespace:
26495 identifier
26496
26497 attribute-argument-clause:
26498 ( balanced-token-seq )
26499
26500 balanced-token-seq:
26501 balanced-token [opt]
26502 balanced-token-seq balanced-token
26503
26504 balanced-token:
26505 ( balanced-token-seq )
26506 [ balanced-token-seq ]
26507 { balanced-token-seq }. */
26508
26509 static tree
26510 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
26511 {
26512 tree attribute, attr_id = NULL_TREE, arguments;
26513 cp_token *token;
26514
26515 temp_override<bool> cleanup
26516 (parser->auto_is_implicit_function_template_parm_p, false);
26517
26518 /* First, parse name of the attribute, a.k.a attribute-token. */
26519
26520 token = cp_lexer_peek_token (parser->lexer);
26521 if (token->type == CPP_NAME)
26522 attr_id = token->u.value;
26523 else if (token->type == CPP_KEYWORD)
26524 attr_id = ridpointers[(int) token->keyword];
26525 else if (token->flags & NAMED_OP)
26526 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26527
26528 if (attr_id == NULL_TREE)
26529 return NULL_TREE;
26530
26531 cp_lexer_consume_token (parser->lexer);
26532
26533 token = cp_lexer_peek_token (parser->lexer);
26534 if (token->type == CPP_SCOPE)
26535 {
26536 /* We are seeing a scoped attribute token. */
26537
26538 cp_lexer_consume_token (parser->lexer);
26539 if (attr_ns)
26540 error_at (token->location, "attribute using prefix used together "
26541 "with scoped attribute token");
26542 attr_ns = attr_id;
26543
26544 token = cp_lexer_consume_token (parser->lexer);
26545 if (token->type == CPP_NAME)
26546 attr_id = token->u.value;
26547 else if (token->type == CPP_KEYWORD)
26548 attr_id = ridpointers[(int) token->keyword];
26549 else if (token->flags & NAMED_OP)
26550 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26551 else
26552 {
26553 error_at (token->location,
26554 "expected an identifier for the attribute name");
26555 return error_mark_node;
26556 }
26557
26558 attr_ns = canonicalize_attr_name (attr_ns);
26559 attr_id = canonicalize_attr_name (attr_id);
26560 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26561 NULL_TREE);
26562 token = cp_lexer_peek_token (parser->lexer);
26563 }
26564 else if (attr_ns)
26565 {
26566 attr_ns = canonicalize_attr_name (attr_ns);
26567 attr_id = canonicalize_attr_name (attr_id);
26568 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26569 NULL_TREE);
26570 }
26571 else
26572 {
26573 attr_id = canonicalize_attr_name (attr_id);
26574 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
26575 NULL_TREE);
26576 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
26577 but no longer: we have to be able to tell [[noreturn]] and
26578 __attribute__((noreturn)) apart. */
26579 /* C++14 deprecated attribute is equivalent to GNU's. */
26580 if (is_attribute_p ("deprecated", attr_id))
26581 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26582 /* C++17 fallthrough attribute is equivalent to GNU's. */
26583 else if (is_attribute_p ("fallthrough", attr_id))
26584 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26585 /* Transactional Memory TS optimize_for_synchronized attribute is
26586 equivalent to GNU transaction_callable. */
26587 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
26588 TREE_PURPOSE (attribute)
26589 = get_identifier ("transaction_callable");
26590 /* Transactional Memory attributes are GNU attributes. */
26591 else if (tm_attr_to_mask (attr_id))
26592 TREE_PURPOSE (attribute) = attr_id;
26593 }
26594
26595 /* Now parse the optional argument clause of the attribute. */
26596
26597 if (token->type != CPP_OPEN_PAREN)
26598 return attribute;
26599
26600 {
26601 vec<tree, va_gc> *vec;
26602 int attr_flag = normal_attr;
26603
26604 /* Maybe we don't expect to see any arguments for this attribute. */
26605 const attribute_spec *as
26606 = lookup_attribute_spec (TREE_PURPOSE (attribute));
26607 if (as && as->max_length == 0)
26608 {
26609 error_at (token->location, "%qE attribute does not take any arguments",
26610 attr_id);
26611 cp_parser_skip_to_closing_parenthesis (parser,
26612 /*recovering=*/true,
26613 /*or_comma=*/false,
26614 /*consume_paren=*/true);
26615 return error_mark_node;
26616 }
26617
26618 if (attr_ns == gnu_identifier
26619 && attribute_takes_identifier_p (attr_id))
26620 /* A GNU attribute that takes an identifier in parameter. */
26621 attr_flag = id_attr;
26622
26623 if (as == NULL)
26624 {
26625 /* For unknown attributes, just skip balanced tokens instead of
26626 trying to parse the arguments. */
26627 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
26628 cp_lexer_consume_token (parser->lexer);
26629 return attribute;
26630 }
26631
26632 vec = cp_parser_parenthesized_expression_list
26633 (parser, attr_flag, /*cast_p=*/false,
26634 /*allow_expansion_p=*/true,
26635 /*non_constant_p=*/NULL);
26636 if (vec == NULL)
26637 arguments = error_mark_node;
26638 else
26639 {
26640 if (vec->is_empty ())
26641 /* e.g. [[attr()]]. */
26642 error_at (token->location, "parentheses must be omitted if "
26643 "%qE attribute argument list is empty",
26644 attr_id);
26645 arguments = build_tree_list_vec (vec);
26646 release_tree_vector (vec);
26647 }
26648
26649 if (arguments == error_mark_node)
26650 attribute = error_mark_node;
26651 else
26652 TREE_VALUE (attribute) = arguments;
26653 }
26654
26655 return attribute;
26656 }
26657
26658 /* Check that the attribute ATTRIBUTE appears at most once in the
26659 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3),
26660 nodiscard, and deprecated (7.6.5). Note that
26661 carries_dependency (7.6.4) isn't implemented yet in GCC. */
26662
26663 static void
26664 cp_parser_check_std_attribute (tree attributes, tree attribute)
26665 {
26666 if (attributes)
26667 {
26668 tree name = get_attribute_name (attribute);
26669 if (is_attribute_p ("noreturn", name)
26670 && lookup_attribute ("noreturn", attributes))
26671 error ("attribute %<noreturn%> can appear at most once "
26672 "in an attribute-list");
26673 else if (is_attribute_p ("deprecated", name)
26674 && lookup_attribute ("deprecated", attributes))
26675 error ("attribute %<deprecated%> can appear at most once "
26676 "in an attribute-list");
26677 else if (is_attribute_p ("nodiscard", name)
26678 && lookup_attribute ("nodiscard", attributes))
26679 error ("attribute %<nodiscard%> can appear at most once "
26680 "in an attribute-list");
26681 }
26682 }
26683
26684 /* Parse a list of standard C++-11 attributes.
26685
26686 attribute-list:
26687 attribute [opt]
26688 attribute-list , attribute[opt]
26689 attribute ...
26690 attribute-list , attribute ...
26691 */
26692
26693 static tree
26694 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
26695 {
26696 tree attributes = NULL_TREE, attribute = NULL_TREE;
26697 cp_token *token = NULL;
26698
26699 while (true)
26700 {
26701 attribute = cp_parser_std_attribute (parser, attr_ns);
26702 if (attribute == error_mark_node)
26703 break;
26704 if (attribute != NULL_TREE)
26705 {
26706 cp_parser_check_std_attribute (attributes, attribute);
26707 TREE_CHAIN (attribute) = attributes;
26708 attributes = attribute;
26709 }
26710 token = cp_lexer_peek_token (parser->lexer);
26711 if (token->type == CPP_ELLIPSIS)
26712 {
26713 cp_lexer_consume_token (parser->lexer);
26714 if (attribute == NULL_TREE)
26715 error_at (token->location,
26716 "expected attribute before %<...%>");
26717 else
26718 {
26719 tree pack = make_pack_expansion (TREE_VALUE (attribute));
26720 if (pack == error_mark_node)
26721 return error_mark_node;
26722 TREE_VALUE (attribute) = pack;
26723 }
26724 token = cp_lexer_peek_token (parser->lexer);
26725 }
26726 if (token->type != CPP_COMMA)
26727 break;
26728 cp_lexer_consume_token (parser->lexer);
26729 }
26730 attributes = nreverse (attributes);
26731 return attributes;
26732 }
26733
26734 /* Parse a standard C++-11 attribute specifier.
26735
26736 attribute-specifier:
26737 [ [ attribute-using-prefix [opt] attribute-list ] ]
26738 alignment-specifier
26739
26740 attribute-using-prefix:
26741 using attribute-namespace :
26742
26743 alignment-specifier:
26744 alignas ( type-id ... [opt] )
26745 alignas ( alignment-expression ... [opt] ). */
26746
26747 static tree
26748 cp_parser_std_attribute_spec (cp_parser *parser)
26749 {
26750 tree attributes = NULL_TREE;
26751 cp_token *token = cp_lexer_peek_token (parser->lexer);
26752
26753 if (token->type == CPP_OPEN_SQUARE
26754 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
26755 {
26756 tree attr_ns = NULL_TREE;
26757
26758 cp_lexer_consume_token (parser->lexer);
26759 cp_lexer_consume_token (parser->lexer);
26760
26761 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26762 {
26763 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26764 if (token->type == CPP_NAME)
26765 attr_ns = token->u.value;
26766 else if (token->type == CPP_KEYWORD)
26767 attr_ns = ridpointers[(int) token->keyword];
26768 else if (token->flags & NAMED_OP)
26769 attr_ns = get_identifier (cpp_type2name (token->type,
26770 token->flags));
26771 if (attr_ns
26772 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
26773 {
26774 if (cxx_dialect < cxx17)
26775 pedwarn (input_location, 0,
26776 "attribute using prefix only available "
26777 "with %<-std=c++17%> or %<-std=gnu++17%>");
26778
26779 cp_lexer_consume_token (parser->lexer);
26780 cp_lexer_consume_token (parser->lexer);
26781 cp_lexer_consume_token (parser->lexer);
26782 }
26783 else
26784 attr_ns = NULL_TREE;
26785 }
26786
26787 attributes = cp_parser_std_attribute_list (parser, attr_ns);
26788
26789 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
26790 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
26791 cp_parser_skip_to_end_of_statement (parser);
26792 else
26793 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26794 when we are sure that we have actually parsed them. */
26795 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26796 }
26797 else
26798 {
26799 tree alignas_expr;
26800
26801 /* Look for an alignment-specifier. */
26802
26803 token = cp_lexer_peek_token (parser->lexer);
26804
26805 if (token->type != CPP_KEYWORD
26806 || token->keyword != RID_ALIGNAS)
26807 return NULL_TREE;
26808
26809 cp_lexer_consume_token (parser->lexer);
26810 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26811
26812 matching_parens parens;
26813 if (!parens.require_open (parser))
26814 return error_mark_node;
26815
26816 cp_parser_parse_tentatively (parser);
26817 alignas_expr = cp_parser_type_id (parser);
26818
26819 if (!cp_parser_parse_definitely (parser))
26820 {
26821 alignas_expr = cp_parser_assignment_expression (parser);
26822 if (alignas_expr == error_mark_node)
26823 cp_parser_skip_to_end_of_statement (parser);
26824 if (alignas_expr == NULL_TREE
26825 || alignas_expr == error_mark_node)
26826 return alignas_expr;
26827 }
26828
26829 alignas_expr = cxx_alignas_expr (alignas_expr);
26830 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
26831
26832 /* Handle alignas (pack...). */
26833 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26834 {
26835 cp_lexer_consume_token (parser->lexer);
26836 alignas_expr = make_pack_expansion (alignas_expr);
26837 }
26838
26839 /* Something went wrong, so don't build the attribute. */
26840 if (alignas_expr == error_mark_node)
26841 return error_mark_node;
26842
26843 /* Missing ')' means the code cannot possibly be valid; go ahead
26844 and commit to make sure we issue a hard error. */
26845 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
26846 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26847 cp_parser_commit_to_tentative_parse (parser);
26848
26849 if (!parens.require_close (parser))
26850 return error_mark_node;
26851
26852 /* Build the C++-11 representation of an 'aligned'
26853 attribute. */
26854 attributes
26855 = build_tree_list (build_tree_list (gnu_identifier,
26856 aligned_identifier), alignas_expr);
26857 }
26858
26859 return attributes;
26860 }
26861
26862 /* Parse a standard C++-11 attribute-specifier-seq.
26863
26864 attribute-specifier-seq:
26865 attribute-specifier-seq [opt] attribute-specifier
26866 */
26867
26868 static tree
26869 cp_parser_std_attribute_spec_seq (cp_parser *parser)
26870 {
26871 tree attr_specs = NULL_TREE;
26872 tree attr_last = NULL_TREE;
26873
26874 /* Don't create wrapper nodes within attributes: the
26875 handlers don't know how to handle them. */
26876 auto_suppress_location_wrappers sentinel;
26877
26878 while (true)
26879 {
26880 tree attr_spec = cp_parser_std_attribute_spec (parser);
26881 if (attr_spec == NULL_TREE)
26882 break;
26883 if (attr_spec == error_mark_node)
26884 return error_mark_node;
26885
26886 if (attr_last)
26887 TREE_CHAIN (attr_last) = attr_spec;
26888 else
26889 attr_specs = attr_last = attr_spec;
26890 attr_last = tree_last (attr_last);
26891 }
26892
26893 return attr_specs;
26894 }
26895
26896 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26897 return index of the first token after balanced-token, or N on failure. */
26898
26899 static size_t
26900 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
26901 {
26902 size_t orig_n = n;
26903 int nparens = 0, nbraces = 0, nsquares = 0;
26904 do
26905 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
26906 {
26907 case CPP_PRAGMA_EOL:
26908 if (!parser->lexer->in_pragma)
26909 break;
26910 /* FALLTHRU */
26911 case CPP_EOF:
26912 /* Ran out of tokens. */
26913 return orig_n;
26914 case CPP_OPEN_PAREN:
26915 ++nparens;
26916 break;
26917 case CPP_OPEN_BRACE:
26918 ++nbraces;
26919 break;
26920 case CPP_OPEN_SQUARE:
26921 ++nsquares;
26922 break;
26923 case CPP_CLOSE_PAREN:
26924 --nparens;
26925 break;
26926 case CPP_CLOSE_BRACE:
26927 --nbraces;
26928 break;
26929 case CPP_CLOSE_SQUARE:
26930 --nsquares;
26931 break;
26932 default:
26933 break;
26934 }
26935 while (nparens || nbraces || nsquares);
26936 return n;
26937 }
26938
26939 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26940 return index of the first token after the GNU attribute tokens, or N on
26941 failure. */
26942
26943 static size_t
26944 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
26945 {
26946 while (true)
26947 {
26948 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
26949 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
26950 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
26951 break;
26952
26953 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
26954 if (n2 == n + 2)
26955 break;
26956 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
26957 break;
26958 n = n2 + 1;
26959 }
26960 return n;
26961 }
26962
26963 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26964 next token), return index of the first token after the standard C++11
26965 attribute tokens, or N on failure. */
26966
26967 static size_t
26968 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
26969 {
26970 while (true)
26971 {
26972 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
26973 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
26974 {
26975 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26976 if (n2 == n + 1)
26977 break;
26978 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
26979 break;
26980 n = n2 + 1;
26981 }
26982 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
26983 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
26984 {
26985 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26986 if (n2 == n + 1)
26987 break;
26988 n = n2;
26989 }
26990 else
26991 break;
26992 }
26993 return n;
26994 }
26995
26996 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26997 as the next token), return index of the first token after the attribute
26998 tokens, or N on failure. */
26999
27000 static size_t
27001 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
27002 {
27003 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
27004 return cp_parser_skip_gnu_attributes_opt (parser, n);
27005 return cp_parser_skip_std_attribute_spec_seq (parser, n);
27006 }
27007
27008 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
27009 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
27010 current value of the PEDANTIC flag, regardless of whether or not
27011 the `__extension__' keyword is present. The caller is responsible
27012 for restoring the value of the PEDANTIC flag. */
27013
27014 static bool
27015 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
27016 {
27017 /* Save the old value of the PEDANTIC flag. */
27018 *saved_pedantic = pedantic;
27019
27020 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
27021 {
27022 /* Consume the `__extension__' token. */
27023 cp_lexer_consume_token (parser->lexer);
27024 /* We're not being pedantic while the `__extension__' keyword is
27025 in effect. */
27026 pedantic = 0;
27027
27028 return true;
27029 }
27030
27031 return false;
27032 }
27033
27034 /* Parse a label declaration.
27035
27036 label-declaration:
27037 __label__ label-declarator-seq ;
27038
27039 label-declarator-seq:
27040 identifier , label-declarator-seq
27041 identifier */
27042
27043 static void
27044 cp_parser_label_declaration (cp_parser* parser)
27045 {
27046 /* Look for the `__label__' keyword. */
27047 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
27048
27049 while (true)
27050 {
27051 tree identifier;
27052
27053 /* Look for an identifier. */
27054 identifier = cp_parser_identifier (parser);
27055 /* If we failed, stop. */
27056 if (identifier == error_mark_node)
27057 break;
27058 /* Declare it as a label. */
27059 finish_label_decl (identifier);
27060 /* If the next token is a `;', stop. */
27061 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27062 break;
27063 /* Look for the `,' separating the label declarations. */
27064 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
27065 }
27066
27067 /* Look for the final `;'. */
27068 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27069 }
27070
27071 // -------------------------------------------------------------------------- //
27072 // Concept definitions
27073
27074 static tree
27075 cp_parser_concept_definition (cp_parser *parser)
27076 {
27077 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
27078 cp_lexer_consume_token (parser->lexer);
27079
27080 cp_expr id = cp_parser_identifier (parser);
27081 if (id == error_mark_node)
27082 {
27083 cp_parser_skip_to_end_of_statement (parser);
27084 cp_parser_consume_semicolon_at_end_of_statement (parser);
27085 return NULL_TREE;
27086 }
27087
27088 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27089 {
27090 cp_parser_skip_to_end_of_statement (parser);
27091 cp_parser_consume_semicolon_at_end_of_statement (parser);
27092 return error_mark_node;
27093 }
27094
27095 processing_constraint_expression_sentinel parsing_constraint;
27096 tree init = cp_parser_constraint_expression (parser);
27097 if (init == error_mark_node)
27098 cp_parser_skip_to_end_of_statement (parser);
27099
27100 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
27101 but continue as if it were. */
27102 cp_parser_consume_semicolon_at_end_of_statement (parser);
27103
27104 return finish_concept_definition (id, init);
27105 }
27106
27107 // -------------------------------------------------------------------------- //
27108 // Requires Clause
27109
27110 /* Diagnose an expression that should appear in ()'s within a requires-clause
27111 and suggest where to place those parentheses. */
27112
27113 static void
27114 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
27115 {
27116 error_at (loc, "expression must be enclosed in parentheses");
27117 }
27118
27119 static void
27120 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
27121 {
27122 gcc_rich_location richloc (loc);
27123 richloc.add_fixit_insert_before ("(");
27124 richloc.add_fixit_insert_after (")");
27125 error_at (&richloc, "expression must be enclosed in parentheses");
27126 }
27127
27128 /* Characterizes the likely kind of expression intended by a mis-written
27129 primary constraint. */
27130 enum primary_constraint_error
27131 {
27132 pce_ok,
27133 pce_maybe_operator,
27134 pce_maybe_postfix
27135 };
27136
27137 /* Returns true if the token(s) following a primary-expression in a
27138 constraint-logical-* expression would require parentheses. */
27139
27140 static primary_constraint_error
27141 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
27142 {
27143 cp_token *token = cp_lexer_peek_token (parser->lexer);
27144 switch (token->type)
27145 {
27146 default:
27147 return pce_ok;
27148
27149 case CPP_EQ:
27150 {
27151 /* An equal sign may be part of the the definition of a function,
27152 and not an assignment operator, when parsing the expression
27153 for a trailing requires-clause. For example:
27154
27155 template<typename T>
27156 struct S {
27157 S() requires C<T> = default;
27158 };
27159
27160 Don't try to reparse this a binary operator. */
27161 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
27162 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
27163 return pce_ok;
27164
27165 gcc_fallthrough ();
27166 }
27167
27168 /* Arithmetic operators. */
27169 case CPP_PLUS:
27170 case CPP_MINUS:
27171 case CPP_MULT:
27172 case CPP_DIV:
27173 case CPP_MOD:
27174 /* Bitwise operators. */
27175 case CPP_AND:
27176 case CPP_OR:
27177 case CPP_XOR:
27178 case CPP_RSHIFT:
27179 case CPP_LSHIFT:
27180 /* Relational operators. */
27181 case CPP_EQ_EQ:
27182 case CPP_NOT_EQ:
27183 case CPP_LESS:
27184 case CPP_GREATER:
27185 case CPP_LESS_EQ:
27186 case CPP_GREATER_EQ:
27187 case CPP_SPACESHIP:
27188 /* Pointer-to-member. */
27189 case CPP_DOT_STAR:
27190 case CPP_DEREF_STAR:
27191 /* Assignment operators. */
27192 case CPP_PLUS_EQ:
27193 case CPP_MINUS_EQ:
27194 case CPP_MULT_EQ:
27195 case CPP_DIV_EQ:
27196 case CPP_MOD_EQ:
27197 case CPP_AND_EQ:
27198 case CPP_OR_EQ:
27199 case CPP_XOR_EQ:
27200 case CPP_RSHIFT_EQ:
27201 case CPP_LSHIFT_EQ:
27202 /* Conditional operator */
27203 case CPP_QUERY:
27204 /* Unenclosed binary or conditional operator. */
27205 return pce_maybe_operator;
27206
27207 case CPP_OPEN_PAREN:
27208 {
27209 /* A primary constraint that precedes the parameter-list of a
27210 lambda expression is followed by an open paren.
27211
27212 []<typename T> requires C (T a, T b) { ... }
27213
27214 Don't try to re-parse this as a postfix expression. */
27215 if (lambda_p)
27216 return pce_ok;
27217
27218 gcc_fallthrough ();
27219 }
27220 case CPP_OPEN_SQUARE:
27221 case CPP_PLUS_PLUS:
27222 case CPP_MINUS_MINUS:
27223 case CPP_DOT:
27224 case CPP_DEREF:
27225 /* Unenclosed postfix operator. */
27226 return pce_maybe_postfix;
27227 }
27228 }
27229
27230 /* Returns true if the next token begins a unary expression, preceded by
27231 an operator or keyword. */
27232
27233 static bool
27234 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
27235 {
27236 cp_token *token = cp_lexer_peek_token (parser->lexer);
27237 switch (token->type)
27238 {
27239 case CPP_NOT:
27240 case CPP_PLUS:
27241 case CPP_MINUS:
27242 case CPP_MULT:
27243 case CPP_COMPL:
27244 case CPP_PLUS_PLUS:
27245 case CPP_MINUS_MINUS:
27246 return true;
27247
27248 case CPP_KEYWORD:
27249 {
27250 switch (token->keyword)
27251 {
27252 case RID_STATCAST:
27253 case RID_DYNCAST:
27254 case RID_REINTCAST:
27255 case RID_CONSTCAST:
27256 case RID_TYPEID:
27257 case RID_SIZEOF:
27258 case RID_ALIGNOF:
27259 case RID_NOEXCEPT:
27260 case RID_NEW:
27261 case RID_DELETE:
27262 case RID_THROW:
27263 return true;
27264
27265 default:
27266 break;
27267 }
27268 }
27269
27270 default:
27271 break;
27272 }
27273
27274 return false;
27275 }
27276
27277 /* Parse a primary expression within a constraint. */
27278
27279 static cp_expr
27280 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
27281 {
27282 /* If this looks like a unary expression, parse it as such, but diagnose
27283 it as ill-formed; it requires parens. */
27284 if (cp_parser_unary_constraint_requires_parens (parser))
27285 {
27286 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
27287 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
27288 return e;
27289 }
27290
27291 cp_parser_parse_tentatively (parser);
27292 cp_id_kind idk;
27293 location_t loc = input_location;
27294 cp_expr expr = cp_parser_primary_expression (parser,
27295 /*address_p=*/false,
27296 /*cast_p=*/false,
27297 /*template_arg_p=*/false,
27298 &idk);
27299 expr.maybe_add_location_wrapper ();
27300
27301 primary_constraint_error pce = pce_ok;
27302 if (expr != error_mark_node)
27303 {
27304 /* The primary-expression could be part of an unenclosed non-logical
27305 compound expression. */
27306 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
27307 if (pce != pce_ok)
27308 cp_parser_simulate_error (parser);
27309 else
27310 expr = finish_constraint_primary_expr (expr);
27311 }
27312 if (cp_parser_parse_definitely (parser))
27313 return expr;
27314 if (expr == error_mark_node)
27315 return error_mark_node;
27316
27317 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
27318 error, but return the expression as if it were valid. */
27319 gcc_assert (pce != pce_ok);
27320 cp_parser_parse_tentatively (parser);
27321 if (pce == pce_maybe_operator)
27322 expr = cp_parser_assignment_expression (parser, NULL, false, false);
27323 else
27324 expr = cp_parser_simple_cast_expression (parser);
27325 if (cp_parser_parse_definitely (parser))
27326 {
27327 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
27328 return expr;
27329 }
27330
27331 /* Otherwise, something has gone very wrong, and we can't generate a more
27332 meaningful diagnostic or recover. */
27333 cp_parser_diagnose_ungrouped_constraint_plain (loc);
27334 return error_mark_node;
27335 }
27336
27337 /* Parse a constraint-logical-and-expression.
27338
27339 constraint-logical-and-expression:
27340 primary-expression
27341 constraint-logical-and-expression '&&' primary-expression */
27342
27343 static cp_expr
27344 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
27345 {
27346 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
27347 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
27348 {
27349 cp_token *op = cp_lexer_consume_token (parser->lexer);
27350 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
27351 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
27352 }
27353 return lhs;
27354 }
27355
27356 /* Parse a constraint-logical-or-expression.
27357
27358 constraint-logical-or-expression:
27359 constraint-logical-and-expression
27360 constraint-logical-or-expression '||' constraint-logical-and-expression */
27361
27362 static cp_expr
27363 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
27364 {
27365 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
27366 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
27367 {
27368 cp_token *op = cp_lexer_consume_token (parser->lexer);
27369 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
27370 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
27371 }
27372 return lhs;
27373 }
27374
27375 /* Parse the expression after a requires-clause. This has a different grammar
27376 than that in the concepts TS. */
27377
27378 static tree
27379 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
27380 {
27381 processing_constraint_expression_sentinel parsing_constraint;
27382 ++processing_template_decl;
27383 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
27384 if (check_for_bare_parameter_packs (expr))
27385 expr = error_mark_node;
27386 --processing_template_decl;
27387 return expr;
27388 }
27389
27390 /* Parse a expression after a requires clause.
27391
27392 constraint-expression:
27393 logical-or-expression
27394
27395 The required logical-or-expression must be a constant expression. Note
27396 that we don't check that the expression is constepxr here. We defer until
27397 we analyze constraints and then, we only check atomic constraints. */
27398
27399 static tree
27400 cp_parser_constraint_expression (cp_parser *parser)
27401 {
27402 processing_constraint_expression_sentinel parsing_constraint;
27403 ++processing_template_decl;
27404 cp_expr expr = cp_parser_binary_expression (parser, false, true,
27405 PREC_NOT_OPERATOR, NULL);
27406 if (check_for_bare_parameter_packs (expr))
27407 expr = error_mark_node;
27408 --processing_template_decl;
27409 expr.maybe_add_location_wrapper ();
27410 return expr;
27411 }
27412
27413 /* Optionally parse a requires clause:
27414
27415 requires-clause:
27416 `requires` constraint-logical-or-expression.
27417 [ConceptsTS]
27418 `requires constraint-expression.
27419
27420 LAMBDA_P is true when the requires-clause is parsed before the
27421 parameter-list of a lambda-declarator. */
27422
27423 static tree
27424 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
27425 {
27426 cp_token *tok = cp_lexer_peek_token (parser->lexer);
27427 if (tok->keyword != RID_REQUIRES)
27428 {
27429 if (!flag_concepts && tok->type == CPP_NAME
27430 && tok->u.value == ridpointers[RID_REQUIRES])
27431 {
27432 error_at (cp_lexer_peek_token (parser->lexer)->location,
27433 "%<requires%> only available with "
27434 "%<-std=c++2a%> or %<-fconcepts%>");
27435 /* Parse and discard the requires-clause. */
27436 cp_lexer_consume_token (parser->lexer);
27437 cp_parser_constraint_expression (parser);
27438 }
27439 return NULL_TREE;
27440 }
27441 cp_lexer_consume_token (parser->lexer);
27442
27443 if (!flag_concepts_ts)
27444 return cp_parser_requires_clause_expression (parser, lambda_p);
27445 else
27446 return cp_parser_constraint_expression (parser);
27447 }
27448
27449 /*---------------------------------------------------------------------------
27450 Requires expressions
27451 ---------------------------------------------------------------------------*/
27452
27453 /* Parse a requires expression
27454
27455 requirement-expression:
27456 'requires' requirement-parameter-list [opt] requirement-body */
27457
27458 static tree
27459 cp_parser_requires_expression (cp_parser *parser)
27460 {
27461 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
27462 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
27463
27464 /* This is definitely a requires-expression. */
27465 cp_parser_commit_to_tentative_parse (parser);
27466
27467 tree parms, reqs;
27468 {
27469 /* Local parameters are delared as variables within the scope
27470 of the expression. They are not visible past the end of
27471 the expression. Expressions within the requires-expression
27472 are unevaluated. */
27473 struct scope_sentinel
27474 {
27475 scope_sentinel ()
27476 {
27477 ++cp_unevaluated_operand;
27478 begin_scope (sk_block, NULL_TREE);
27479 }
27480
27481 ~scope_sentinel ()
27482 {
27483 pop_bindings_and_leave_scope ();
27484 --cp_unevaluated_operand;
27485 }
27486 } s;
27487
27488 /* Parse the optional parameter list. */
27489 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27490 {
27491 parms = cp_parser_requirement_parameter_list (parser);
27492 if (parms == error_mark_node)
27493 return error_mark_node;
27494 }
27495 else
27496 parms = NULL_TREE;
27497
27498 /* Parse the requirement body. */
27499 reqs = cp_parser_requirement_body (parser);
27500 if (reqs == error_mark_node)
27501 return error_mark_node;
27502 }
27503
27504 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
27505 the parm chain. */
27506 grokparms (parms, &parms);
27507 loc = make_location (loc, loc, parser->lexer);
27508 return finish_requires_expr (loc, parms, reqs);
27509 }
27510
27511 /* Parse a parameterized requirement.
27512
27513 requirement-parameter-list:
27514 '(' parameter-declaration-clause ')' */
27515
27516 static tree
27517 cp_parser_requirement_parameter_list (cp_parser *parser)
27518 {
27519 matching_parens parens;
27520 if (!parens.require_open (parser))
27521 return error_mark_node;
27522
27523 tree parms
27524 = cp_parser_parameter_declaration_clause (parser, CP_PARSER_FLAGS_NONE);
27525
27526 if (!parens.require_close (parser))
27527 return error_mark_node;
27528
27529 return parms;
27530 }
27531
27532 /* Parse the body of a requirement.
27533
27534 requirement-body:
27535 '{' requirement-list '}' */
27536 static tree
27537 cp_parser_requirement_body (cp_parser *parser)
27538 {
27539 matching_braces braces;
27540 if (!braces.require_open (parser))
27541 return error_mark_node;
27542
27543 tree reqs = cp_parser_requirement_seq (parser);
27544
27545 if (!braces.require_close (parser))
27546 return error_mark_node;
27547
27548 return reqs;
27549 }
27550
27551 /* Parse a sequence of requirements.
27552
27553 requirement-seq:
27554 requirement
27555 requirement-seq requirement */
27556
27557 static tree
27558 cp_parser_requirement_seq (cp_parser *parser)
27559 {
27560 tree result = NULL_TREE;
27561 do
27562 {
27563 tree req = cp_parser_requirement (parser);
27564 if (req != error_mark_node)
27565 result = tree_cons (NULL_TREE, req, result);
27566 } while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE));
27567
27568 /* If there are no valid requirements, this is not a valid expression. */
27569 if (!result)
27570 return error_mark_node;
27571
27572 /* Reverse the order of requirements so they are analyzed in order. */
27573 return nreverse (result);
27574 }
27575
27576 /* Parse a syntactic requirement or type requirement.
27577
27578 requirement:
27579 simple-requirement
27580 compound-requirement
27581 type-requirement
27582 nested-requirement */
27583
27584 static tree
27585 cp_parser_requirement (cp_parser *parser)
27586 {
27587 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27588 return cp_parser_compound_requirement (parser);
27589 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
27590 return cp_parser_type_requirement (parser);
27591 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
27592 return cp_parser_nested_requirement (parser);
27593 else
27594 return cp_parser_simple_requirement (parser);
27595 }
27596
27597 /* Parse a simple requirement.
27598
27599 simple-requirement:
27600 expression ';' */
27601
27602 static tree
27603 cp_parser_simple_requirement (cp_parser *parser)
27604 {
27605 location_t start = cp_lexer_peek_token (parser->lexer)->location;
27606 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
27607 if (expr == error_mark_node)
27608 cp_parser_skip_to_end_of_statement (parser);
27609
27610 cp_parser_consume_semicolon_at_end_of_statement (parser);
27611
27612 if (!expr || expr == error_mark_node)
27613 return error_mark_node;
27614
27615 /* Sometimes we don't get locations, so use the cached token location
27616 as a reasonable approximation. */
27617 if (expr.get_location() == UNKNOWN_LOCATION)
27618 expr.set_location (start);
27619
27620 return finish_simple_requirement (expr.get_location (), expr);
27621 }
27622
27623 /* Parse a type requirement
27624
27625 type-requirement
27626 nested-name-specifier [opt] required-type-name ';'
27627
27628 required-type-name:
27629 type-name
27630 'template' [opt] simple-template-id */
27631
27632 static tree
27633 cp_parser_type_requirement (cp_parser *parser)
27634 {
27635 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
27636 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27637
27638 // Save the scope before parsing name specifiers.
27639 tree saved_scope = parser->scope;
27640 tree saved_object_scope = parser->object_scope;
27641 tree saved_qualifying_scope = parser->qualifying_scope;
27642 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
27643 cp_parser_nested_name_specifier_opt (parser,
27644 /*typename_keyword_p=*/true,
27645 /*check_dependency_p=*/false,
27646 /*type_p=*/true,
27647 /*is_declaration=*/false);
27648
27649 tree type;
27650 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27651 {
27652 cp_lexer_consume_token (parser->lexer);
27653 type = cp_parser_template_id (parser,
27654 /*template_keyword_p=*/true,
27655 /*check_dependency=*/false,
27656 /*tag_type=*/none_type,
27657 /*is_declaration=*/false);
27658 type = make_typename_type (parser->scope, type, typename_type,
27659 /*complain=*/tf_error);
27660 }
27661 else
27662 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
27663
27664 if (TREE_CODE (type) == TYPE_DECL)
27665 type = TREE_TYPE (type);
27666
27667 parser->scope = saved_scope;
27668 parser->object_scope = saved_object_scope;
27669 parser->qualifying_scope = saved_qualifying_scope;
27670
27671 if (type == error_mark_node)
27672 cp_parser_skip_to_end_of_statement (parser);
27673
27674 cp_parser_consume_semicolon_at_end_of_statement (parser);
27675
27676 if (type == error_mark_node)
27677 return error_mark_node;
27678
27679 loc = make_location (loc, start_tok->location, parser->lexer);
27680 return finish_type_requirement (loc, type);
27681 }
27682
27683 /* Parse a compound requirement
27684
27685 compound-requirement:
27686 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
27687
27688 static tree
27689 cp_parser_compound_requirement (cp_parser *parser)
27690 {
27691 /* Parse an expression enclosed in '{ }'s. */
27692 matching_braces braces;
27693 if (!braces.require_open (parser))
27694 return error_mark_node;
27695
27696 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
27697
27698 tree expr = cp_parser_expression (parser, NULL, false, false);
27699 if (expr == error_mark_node)
27700 cp_parser_skip_to_closing_brace (parser);
27701
27702 if (!braces.require_close (parser))
27703 {
27704 cp_parser_skip_to_end_of_statement (parser);
27705 cp_parser_consume_semicolon_at_end_of_statement (parser);
27706 return error_mark_node;
27707 }
27708
27709 /* If the expression was invalid, skip the remainder of the requirement. */
27710 if (!expr || expr == error_mark_node)
27711 {
27712 cp_parser_skip_to_end_of_statement (parser);
27713 cp_parser_consume_semicolon_at_end_of_statement (parser);
27714 return error_mark_node;
27715 }
27716
27717 /* Parse the optional noexcept. */
27718 bool noexcept_p = false;
27719 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
27720 {
27721 cp_lexer_consume_token (parser->lexer);
27722 noexcept_p = true;
27723 }
27724
27725 /* Parse the optional trailing return type. */
27726 tree type = NULL_TREE;
27727 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
27728 {
27729 cp_lexer_consume_token (parser->lexer);
27730 cp_token *tok = cp_lexer_peek_token (parser->lexer);
27731
27732 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
27733 parser->in_result_type_constraint_p = true;
27734 /* C++2a allows either a type-id or a type-constraint. Parsing
27735 a type-id will subsume the parsing for a type-constraint but
27736 allow for more syntactic forms (e.g., const C<T>*). */
27737 type = cp_parser_trailing_type_id (parser);
27738 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
27739 if (type == error_mark_node)
27740 return error_mark_node;
27741
27742 location_t type_loc = make_location (tok->location, tok->location,
27743 parser->lexer);
27744
27745 /* Check that we haven't written something like 'const C<T>*'. */
27746 if (type_uses_auto (type))
27747 {
27748 if (!is_auto (type))
27749 {
27750 error_at (type_loc,
27751 "result type is not a plain type-constraint");
27752 cp_parser_consume_semicolon_at_end_of_statement (parser);
27753 return error_mark_node;
27754 }
27755 }
27756 else if (!flag_concepts_ts)
27757 /* P1452R2 removed the trailing-return-type option. */
27758 error_at (type_loc,
27759 "return-type-requirement is not a type-constraint");
27760 }
27761
27762 location_t loc = make_location (expr_token->location,
27763 braces.open_location (),
27764 parser->lexer);
27765
27766 cp_parser_consume_semicolon_at_end_of_statement (parser);
27767
27768 if (expr == error_mark_node || type == error_mark_node)
27769 return error_mark_node;
27770
27771 return finish_compound_requirement (loc, expr, type, noexcept_p);
27772 }
27773
27774 /* Parse a nested requirement. This is the same as a requires clause.
27775
27776 nested-requirement:
27777 requires-clause */
27778
27779 static tree
27780 cp_parser_nested_requirement (cp_parser *parser)
27781 {
27782 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
27783 cp_token *tok = cp_lexer_consume_token (parser->lexer);
27784 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27785 tree req = cp_parser_constraint_expression (parser);
27786 if (req == error_mark_node)
27787 cp_parser_skip_to_end_of_statement (parser);
27788 loc = make_location (loc, tok->location, parser->lexer);
27789 cp_parser_consume_semicolon_at_end_of_statement (parser);
27790 if (req == error_mark_node)
27791 return error_mark_node;
27792 return finish_nested_requirement (loc, req);
27793 }
27794
27795 /* Support Functions */
27796
27797 /* Return the appropriate prefer_type argument for lookup_name_real based on
27798 tag_type and template_mem_access. */
27799
27800 static inline int
27801 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
27802 {
27803 /* DR 141: When looking in the current enclosing context for a template-name
27804 after -> or ., only consider class templates. */
27805 if (template_mem_access)
27806 return 2;
27807 switch (tag_type)
27808 {
27809 case none_type: return 0; // No preference.
27810 case scope_type: return 1; // Type or namespace.
27811 default: return 2; // Type only.
27812 }
27813 }
27814
27815 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
27816 NAME should have one of the representations used for an
27817 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
27818 is returned. If PARSER->SCOPE is a dependent type, then a
27819 SCOPE_REF is returned.
27820
27821 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
27822 returned; the name was already resolved when the TEMPLATE_ID_EXPR
27823 was formed. Abstractly, such entities should not be passed to this
27824 function, because they do not need to be looked up, but it is
27825 simpler to check for this special case here, rather than at the
27826 call-sites.
27827
27828 In cases not explicitly covered above, this function returns a
27829 DECL, OVERLOAD, or baselink representing the result of the lookup.
27830 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
27831 is returned.
27832
27833 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
27834 (e.g., "struct") that was used. In that case bindings that do not
27835 refer to types are ignored.
27836
27837 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
27838 ignored.
27839
27840 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
27841 are ignored.
27842
27843 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
27844 types.
27845
27846 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
27847 TREE_LIST of candidates if name-lookup results in an ambiguity, and
27848 NULL_TREE otherwise. */
27849
27850 static cp_expr
27851 cp_parser_lookup_name (cp_parser *parser, tree name,
27852 enum tag_types tag_type,
27853 bool is_template,
27854 bool is_namespace,
27855 bool check_dependency,
27856 tree *ambiguous_decls,
27857 location_t name_location)
27858 {
27859 tree decl;
27860 tree object_type = parser->context->object_type;
27861
27862 /* Assume that the lookup will be unambiguous. */
27863 if (ambiguous_decls)
27864 *ambiguous_decls = NULL_TREE;
27865
27866 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
27867 no longer valid. Note that if we are parsing tentatively, and
27868 the parse fails, OBJECT_TYPE will be automatically restored. */
27869 parser->context->object_type = NULL_TREE;
27870
27871 if (name == error_mark_node)
27872 return error_mark_node;
27873
27874 /* A template-id has already been resolved; there is no lookup to
27875 do. */
27876 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
27877 return name;
27878 if (BASELINK_P (name))
27879 {
27880 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
27881 == TEMPLATE_ID_EXPR);
27882 return name;
27883 }
27884
27885 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
27886 it should already have been checked to make sure that the name
27887 used matches the type being destroyed. */
27888 if (TREE_CODE (name) == BIT_NOT_EXPR)
27889 {
27890 tree type;
27891
27892 /* Figure out to which type this destructor applies. */
27893 if (parser->scope)
27894 type = parser->scope;
27895 else if (object_type)
27896 type = object_type;
27897 else
27898 type = current_class_type;
27899 /* If that's not a class type, there is no destructor. */
27900 if (!type || !CLASS_TYPE_P (type))
27901 return error_mark_node;
27902
27903 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
27904 lazily_declare_fn (sfk_destructor, type);
27905
27906 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
27907 return dtor;
27908
27909 return error_mark_node;
27910 }
27911
27912 /* By this point, the NAME should be an ordinary identifier. If
27913 the id-expression was a qualified name, the qualifying scope is
27914 stored in PARSER->SCOPE at this point. */
27915 gcc_assert (identifier_p (name));
27916
27917 /* Perform the lookup. */
27918 if (parser->scope)
27919 {
27920 bool dependent_p;
27921
27922 if (parser->scope == error_mark_node)
27923 return error_mark_node;
27924
27925 /* If the SCOPE is dependent, the lookup must be deferred until
27926 the template is instantiated -- unless we are explicitly
27927 looking up names in uninstantiated templates. Even then, we
27928 cannot look up the name if the scope is not a class type; it
27929 might, for example, be a template type parameter. */
27930 dependent_p = (TYPE_P (parser->scope)
27931 && dependent_scope_p (parser->scope));
27932 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
27933 && dependent_p)
27934 /* Defer lookup. */
27935 decl = error_mark_node;
27936 else
27937 {
27938 tree pushed_scope = NULL_TREE;
27939
27940 /* If PARSER->SCOPE is a dependent type, then it must be a
27941 class type, and we must not be checking dependencies;
27942 otherwise, we would have processed this lookup above. So
27943 that PARSER->SCOPE is not considered a dependent base by
27944 lookup_member, we must enter the scope here. */
27945 if (dependent_p)
27946 pushed_scope = push_scope (parser->scope);
27947
27948 /* If the PARSER->SCOPE is a template specialization, it
27949 may be instantiated during name lookup. In that case,
27950 errors may be issued. Even if we rollback the current
27951 tentative parse, those errors are valid. */
27952 decl = lookup_qualified_name (parser->scope, name,
27953 prefer_type_arg (tag_type),
27954 /*complain=*/true);
27955
27956 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
27957 lookup result and the nested-name-specifier nominates a class C:
27958 * if the name specified after the nested-name-specifier, when
27959 looked up in C, is the injected-class-name of C (Clause 9), or
27960 * if the name specified after the nested-name-specifier is the
27961 same as the identifier or the simple-template-id's template-
27962 name in the last component of the nested-name-specifier,
27963 the name is instead considered to name the constructor of
27964 class C. [ Note: for example, the constructor is not an
27965 acceptable lookup result in an elaborated-type-specifier so
27966 the constructor would not be used in place of the
27967 injected-class-name. --end note ] Such a constructor name
27968 shall be used only in the declarator-id of a declaration that
27969 names a constructor or in a using-declaration. */
27970 if (tag_type == none_type
27971 && DECL_SELF_REFERENCE_P (decl)
27972 && same_type_p (DECL_CONTEXT (decl), parser->scope))
27973 decl = lookup_qualified_name (parser->scope, ctor_identifier,
27974 prefer_type_arg (tag_type),
27975 /*complain=*/true);
27976
27977 /* If we have a single function from a using decl, pull it out. */
27978 if (TREE_CODE (decl) == OVERLOAD
27979 && !really_overloaded_fn (decl))
27980 decl = OVL_FUNCTION (decl);
27981
27982 if (pushed_scope)
27983 pop_scope (pushed_scope);
27984 }
27985
27986 /* If the scope is a dependent type and either we deferred lookup or
27987 we did lookup but didn't find the name, rememeber the name. */
27988 if (decl == error_mark_node && TYPE_P (parser->scope)
27989 && dependent_type_p (parser->scope))
27990 {
27991 if (tag_type)
27992 {
27993 tree type;
27994
27995 /* The resolution to Core Issue 180 says that `struct
27996 A::B' should be considered a type-name, even if `A'
27997 is dependent. */
27998 type = make_typename_type (parser->scope, name, tag_type,
27999 /*complain=*/tf_error);
28000 if (type != error_mark_node)
28001 decl = TYPE_NAME (type);
28002 }
28003 else if (is_template
28004 && (cp_parser_next_token_ends_template_argument_p (parser)
28005 || cp_lexer_next_token_is (parser->lexer,
28006 CPP_CLOSE_PAREN)))
28007 decl = make_unbound_class_template (parser->scope,
28008 name, NULL_TREE,
28009 /*complain=*/tf_error);
28010 else
28011 decl = build_qualified_name (/*type=*/NULL_TREE,
28012 parser->scope, name,
28013 is_template);
28014 }
28015 parser->qualifying_scope = parser->scope;
28016 parser->object_scope = NULL_TREE;
28017 }
28018 else if (object_type)
28019 {
28020 /* Look up the name in the scope of the OBJECT_TYPE, unless the
28021 OBJECT_TYPE is not a class. */
28022 if (CLASS_TYPE_P (object_type))
28023 /* If the OBJECT_TYPE is a template specialization, it may
28024 be instantiated during name lookup. In that case, errors
28025 may be issued. Even if we rollback the current tentative
28026 parse, those errors are valid. */
28027 decl = lookup_member (object_type,
28028 name,
28029 /*protect=*/0,
28030 prefer_type_arg (tag_type),
28031 tf_warning_or_error);
28032 else
28033 decl = NULL_TREE;
28034
28035 if (!decl)
28036 /* Look it up in the enclosing context. DR 141: When looking for a
28037 template-name after -> or ., only consider class templates. */
28038 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
28039 /*nonclass=*/0,
28040 /*block_p=*/true, is_namespace, 0);
28041 if (object_type == unknown_type_node)
28042 /* The object is type-dependent, so we can't look anything up; we used
28043 this to get the DR 141 behavior. */
28044 object_type = NULL_TREE;
28045 parser->object_scope = object_type;
28046 parser->qualifying_scope = NULL_TREE;
28047 }
28048 else
28049 {
28050 decl = lookup_name_real (name, prefer_type_arg (tag_type),
28051 /*nonclass=*/0,
28052 /*block_p=*/true, is_namespace, 0);
28053 parser->qualifying_scope = NULL_TREE;
28054 parser->object_scope = NULL_TREE;
28055 }
28056
28057 /* If the lookup failed, let our caller know. */
28058 if (!decl || decl == error_mark_node)
28059 return error_mark_node;
28060
28061 /* Pull out the template from an injected-class-name (or multiple). */
28062 if (is_template)
28063 decl = maybe_get_template_decl_from_type_decl (decl);
28064
28065 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
28066 if (TREE_CODE (decl) == TREE_LIST)
28067 {
28068 if (ambiguous_decls)
28069 *ambiguous_decls = decl;
28070 /* The error message we have to print is too complicated for
28071 cp_parser_error, so we incorporate its actions directly. */
28072 if (!cp_parser_simulate_error (parser))
28073 {
28074 error_at (name_location, "reference to %qD is ambiguous",
28075 name);
28076 print_candidates (decl);
28077 }
28078 return error_mark_node;
28079 }
28080
28081 gcc_assert (DECL_P (decl)
28082 || TREE_CODE (decl) == OVERLOAD
28083 || TREE_CODE (decl) == SCOPE_REF
28084 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
28085 || BASELINK_P (decl));
28086
28087 /* If we have resolved the name of a member declaration, check to
28088 see if the declaration is accessible. When the name resolves to
28089 set of overloaded functions, accessibility is checked when
28090 overload resolution is done.
28091
28092 During an explicit instantiation, access is not checked at all,
28093 as per [temp.explicit]. */
28094 if (DECL_P (decl))
28095 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
28096
28097 maybe_record_typedef_use (decl);
28098
28099 return cp_expr (decl, name_location);
28100 }
28101
28102 /* Like cp_parser_lookup_name, but for use in the typical case where
28103 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
28104 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
28105
28106 static tree
28107 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
28108 {
28109 return cp_parser_lookup_name (parser, name,
28110 none_type,
28111 /*is_template=*/false,
28112 /*is_namespace=*/false,
28113 /*check_dependency=*/true,
28114 /*ambiguous_decls=*/NULL,
28115 location);
28116 }
28117
28118 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
28119 the current context, return the TYPE_DECL. If TAG_NAME_P is
28120 true, the DECL indicates the class being defined in a class-head,
28121 or declared in an elaborated-type-specifier.
28122
28123 Otherwise, return DECL. */
28124
28125 static tree
28126 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
28127 {
28128 /* If the TEMPLATE_DECL is being declared as part of a class-head,
28129 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
28130
28131 struct A {
28132 template <typename T> struct B;
28133 };
28134
28135 template <typename T> struct A::B {};
28136
28137 Similarly, in an elaborated-type-specifier:
28138
28139 namespace N { struct X{}; }
28140
28141 struct A {
28142 template <typename T> friend struct N::X;
28143 };
28144
28145 However, if the DECL refers to a class type, and we are in
28146 the scope of the class, then the name lookup automatically
28147 finds the TYPE_DECL created by build_self_reference rather
28148 than a TEMPLATE_DECL. For example, in:
28149
28150 template <class T> struct S {
28151 S s;
28152 };
28153
28154 there is no need to handle such case. */
28155
28156 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
28157 return DECL_TEMPLATE_RESULT (decl);
28158
28159 return decl;
28160 }
28161
28162 /* If too many, or too few, template-parameter lists apply to the
28163 declarator, issue an error message. Returns TRUE if all went well,
28164 and FALSE otherwise. */
28165
28166 static bool
28167 cp_parser_check_declarator_template_parameters (cp_parser* parser,
28168 cp_declarator *declarator,
28169 location_t declarator_location)
28170 {
28171 switch (declarator->kind)
28172 {
28173 case cdk_id:
28174 {
28175 unsigned num_templates = 0;
28176 tree scope = declarator->u.id.qualifying_scope;
28177 bool template_id_p = false;
28178
28179 if (scope)
28180 num_templates = num_template_headers_for_class (scope);
28181 else if (TREE_CODE (declarator->u.id.unqualified_name)
28182 == TEMPLATE_ID_EXPR)
28183 {
28184 /* If the DECLARATOR has the form `X<y>' then it uses one
28185 additional level of template parameters. */
28186 ++num_templates;
28187 template_id_p = true;
28188 }
28189
28190 return cp_parser_check_template_parameters
28191 (parser, num_templates, template_id_p, declarator_location,
28192 declarator);
28193 }
28194
28195 case cdk_function:
28196 case cdk_array:
28197 case cdk_pointer:
28198 case cdk_reference:
28199 case cdk_ptrmem:
28200 return (cp_parser_check_declarator_template_parameters
28201 (parser, declarator->declarator, declarator_location));
28202
28203 case cdk_decomp:
28204 case cdk_error:
28205 return true;
28206
28207 default:
28208 gcc_unreachable ();
28209 }
28210 return false;
28211 }
28212
28213 /* NUM_TEMPLATES were used in the current declaration. If that is
28214 invalid, return FALSE and issue an error messages. Otherwise,
28215 return TRUE. If DECLARATOR is non-NULL, then we are checking a
28216 declarator and we can print more accurate diagnostics. */
28217
28218 static bool
28219 cp_parser_check_template_parameters (cp_parser* parser,
28220 unsigned num_templates,
28221 bool template_id_p,
28222 location_t location,
28223 cp_declarator *declarator)
28224 {
28225 /* If there are the same number of template classes and parameter
28226 lists, that's OK. */
28227 if (parser->num_template_parameter_lists == num_templates)
28228 return true;
28229 /* If there are more, but only one more, and the name ends in an identifier,
28230 then we are declaring a primary template. That's OK too. */
28231 if (!template_id_p
28232 && parser->num_template_parameter_lists == num_templates + 1)
28233 return true;
28234 /* If there are more template classes than parameter lists, we have
28235 something like:
28236
28237 template <class T> void S<T>::R<T>::f (); */
28238 if (parser->num_template_parameter_lists < num_templates)
28239 {
28240 if (declarator && !current_function_decl)
28241 error_at (location, "specializing member %<%T::%E%> "
28242 "requires %<template<>%> syntax",
28243 declarator->u.id.qualifying_scope,
28244 declarator->u.id.unqualified_name);
28245 else if (declarator)
28246 error_at (location, "invalid declaration of %<%T::%E%>",
28247 declarator->u.id.qualifying_scope,
28248 declarator->u.id.unqualified_name);
28249 else
28250 error_at (location, "too few template-parameter-lists");
28251 return false;
28252 }
28253 /* Otherwise, there are too many template parameter lists. We have
28254 something like:
28255
28256 template <class T> template <class U> void S::f(); */
28257 error_at (location, "too many template-parameter-lists");
28258 return false;
28259 }
28260
28261 /* Parse an optional `::' token indicating that the following name is
28262 from the global namespace. If so, PARSER->SCOPE is set to the
28263 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
28264 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
28265 Returns the new value of PARSER->SCOPE, if the `::' token is
28266 present, and NULL_TREE otherwise. */
28267
28268 static tree
28269 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
28270 {
28271 cp_token *token;
28272
28273 /* Peek at the next token. */
28274 token = cp_lexer_peek_token (parser->lexer);
28275 /* If we're looking at a `::' token then we're starting from the
28276 global namespace, not our current location. */
28277 if (token->type == CPP_SCOPE)
28278 {
28279 /* Consume the `::' token. */
28280 cp_lexer_consume_token (parser->lexer);
28281 /* Set the SCOPE so that we know where to start the lookup. */
28282 parser->scope = global_namespace;
28283 parser->qualifying_scope = global_namespace;
28284 parser->object_scope = NULL_TREE;
28285
28286 return parser->scope;
28287 }
28288 else if (!current_scope_valid_p)
28289 {
28290 parser->scope = NULL_TREE;
28291 parser->qualifying_scope = NULL_TREE;
28292 parser->object_scope = NULL_TREE;
28293 }
28294
28295 return NULL_TREE;
28296 }
28297
28298 /* Returns TRUE if the upcoming token sequence is the start of a
28299 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
28300 declarator is preceded by the `friend' specifier. The parser flags FLAGS
28301 is used to control type-specifier parsing. */
28302
28303 static bool
28304 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
28305 bool friend_p)
28306 {
28307 bool constructor_p;
28308 bool outside_class_specifier_p;
28309 tree nested_name_specifier;
28310 cp_token *next_token;
28311
28312 /* The common case is that this is not a constructor declarator, so
28313 try to avoid doing lots of work if at all possible. It's not
28314 valid declare a constructor at function scope. */
28315 if (parser->in_function_body)
28316 return false;
28317 /* And only certain tokens can begin a constructor declarator. */
28318 next_token = cp_lexer_peek_token (parser->lexer);
28319 if (next_token->type != CPP_NAME
28320 && next_token->type != CPP_SCOPE
28321 && next_token->type != CPP_NESTED_NAME_SPECIFIER
28322 && next_token->type != CPP_TEMPLATE_ID)
28323 return false;
28324
28325 /* Parse tentatively; we are going to roll back all of the tokens
28326 consumed here. */
28327 cp_parser_parse_tentatively (parser);
28328 /* Assume that we are looking at a constructor declarator. */
28329 constructor_p = true;
28330
28331 /* Look for the optional `::' operator. */
28332 cp_parser_global_scope_opt (parser,
28333 /*current_scope_valid_p=*/false);
28334 /* Look for the nested-name-specifier. */
28335 nested_name_specifier
28336 = (cp_parser_nested_name_specifier_opt (parser,
28337 /*typename_keyword_p=*/false,
28338 /*check_dependency_p=*/false,
28339 /*type_p=*/false,
28340 /*is_declaration=*/false));
28341
28342 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
28343 if (nested_name_specifier
28344 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
28345 {
28346 tree s = resolve_typename_type (nested_name_specifier,
28347 /*only_current_p=*/false);
28348 if (TREE_CODE (s) != TYPENAME_TYPE)
28349 nested_name_specifier = s;
28350 }
28351
28352 outside_class_specifier_p = (!at_class_scope_p ()
28353 || !TYPE_BEING_DEFINED (current_class_type)
28354 || friend_p);
28355
28356 /* Outside of a class-specifier, there must be a
28357 nested-name-specifier. Except in C++17 mode, where we
28358 might be declaring a guiding declaration. */
28359 if (!nested_name_specifier && outside_class_specifier_p
28360 && cxx_dialect < cxx17)
28361 constructor_p = false;
28362 else if (nested_name_specifier == error_mark_node)
28363 constructor_p = false;
28364
28365 /* If we have a class scope, this is easy; DR 147 says that S::S always
28366 names the constructor, and no other qualified name could. */
28367 if (constructor_p && nested_name_specifier
28368 && CLASS_TYPE_P (nested_name_specifier))
28369 {
28370 tree id = cp_parser_unqualified_id (parser,
28371 /*template_keyword_p=*/false,
28372 /*check_dependency_p=*/false,
28373 /*declarator_p=*/true,
28374 /*optional_p=*/false);
28375 if (is_overloaded_fn (id))
28376 id = DECL_NAME (get_first_fn (id));
28377 if (!constructor_name_p (id, nested_name_specifier))
28378 constructor_p = false;
28379 }
28380 /* If we still think that this might be a constructor-declarator,
28381 look for a class-name. */
28382 else if (constructor_p)
28383 {
28384 /* If we have:
28385
28386 template <typename T> struct S {
28387 S();
28388 };
28389
28390 we must recognize that the nested `S' names a class. */
28391 if (cxx_dialect >= cxx17)
28392 cp_parser_parse_tentatively (parser);
28393
28394 tree type_decl;
28395 type_decl = cp_parser_class_name (parser,
28396 /*typename_keyword_p=*/false,
28397 /*template_keyword_p=*/false,
28398 none_type,
28399 /*check_dependency_p=*/false,
28400 /*class_head_p=*/false,
28401 /*is_declaration=*/false);
28402
28403 if (cxx_dialect >= cxx17
28404 && !cp_parser_parse_definitely (parser))
28405 {
28406 type_decl = NULL_TREE;
28407 tree tmpl = cp_parser_template_name (parser,
28408 /*template_keyword*/false,
28409 /*check_dependency_p*/false,
28410 /*is_declaration*/false,
28411 none_type,
28412 /*is_identifier*/NULL);
28413 if (DECL_CLASS_TEMPLATE_P (tmpl)
28414 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28415 /* It's a deduction guide, return true. */;
28416 else
28417 cp_parser_simulate_error (parser);
28418 }
28419
28420 /* If there was no class-name, then this is not a constructor.
28421 Otherwise, if we are in a class-specifier and we aren't
28422 handling a friend declaration, check that its type matches
28423 current_class_type (c++/38313). Note: error_mark_node
28424 is left alone for error recovery purposes. */
28425 constructor_p = (!cp_parser_error_occurred (parser)
28426 && (outside_class_specifier_p
28427 || type_decl == NULL_TREE
28428 || type_decl == error_mark_node
28429 || same_type_p (current_class_type,
28430 TREE_TYPE (type_decl))));
28431
28432 /* If we're still considering a constructor, we have to see a `(',
28433 to begin the parameter-declaration-clause, followed by either a
28434 `)', an `...', or a decl-specifier. We need to check for a
28435 type-specifier to avoid being fooled into thinking that:
28436
28437 S (f) (int);
28438
28439 is a constructor. (It is actually a function named `f' that
28440 takes one parameter (of type `int') and returns a value of type
28441 `S'. */
28442 if (constructor_p
28443 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28444 constructor_p = false;
28445
28446 if (constructor_p
28447 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
28448 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
28449 /* A parameter declaration begins with a decl-specifier,
28450 which is either the "attribute" keyword, a storage class
28451 specifier, or (usually) a type-specifier. */
28452 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
28453 /* A parameter declaration can also begin with [[attribute]]. */
28454 && !cp_next_tokens_can_be_std_attribute_p (parser))
28455 {
28456 tree type;
28457 tree pushed_scope = NULL_TREE;
28458 unsigned saved_num_template_parameter_lists;
28459
28460 /* Names appearing in the type-specifier should be looked up
28461 in the scope of the class. */
28462 if (current_class_type)
28463 type = NULL_TREE;
28464 else if (type_decl)
28465 {
28466 type = TREE_TYPE (type_decl);
28467 if (TREE_CODE (type) == TYPENAME_TYPE)
28468 {
28469 type = resolve_typename_type (type,
28470 /*only_current_p=*/false);
28471 if (TREE_CODE (type) == TYPENAME_TYPE)
28472 {
28473 cp_parser_abort_tentative_parse (parser);
28474 return false;
28475 }
28476 }
28477 pushed_scope = push_scope (type);
28478 }
28479
28480 /* Inside the constructor parameter list, surrounding
28481 template-parameter-lists do not apply. */
28482 saved_num_template_parameter_lists
28483 = parser->num_template_parameter_lists;
28484 parser->num_template_parameter_lists = 0;
28485
28486 /* Look for the type-specifier. It's not optional, but its typename
28487 might be. Unless this is a friend declaration; we don't want to
28488 treat
28489
28490 friend S (T::fn)(int);
28491
28492 as a constructor, but with P0634, we might assume a type when
28493 looking for the type-specifier. It is actually a function named
28494 `T::fn' that takes one parameter (of type `int') and returns a
28495 value of type `S'. Constructors can be friends, but they must
28496 use a qualified name.
28497
28498 Parse with an empty set of declaration specifiers since we're
28499 trying to match a decl-specifier-seq of the first parameter.
28500 This must be non-null so that cp_parser_simple_type_specifier
28501 will recognize a constrained placeholder type such as:
28502 'C<int> auto' where C is a type concept. */
28503 cp_decl_specifier_seq ctor_specs;
28504 clear_decl_specs (&ctor_specs);
28505 cp_parser_type_specifier (parser,
28506 (friend_p ? CP_PARSER_FLAGS_NONE
28507 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
28508 /*decl_specs=*/&ctor_specs,
28509 /*is_declarator=*/true,
28510 /*declares_class_or_enum=*/NULL,
28511 /*is_cv_qualifier=*/NULL);
28512
28513 parser->num_template_parameter_lists
28514 = saved_num_template_parameter_lists;
28515
28516 /* Leave the scope of the class. */
28517 if (pushed_scope)
28518 pop_scope (pushed_scope);
28519
28520 constructor_p = !cp_parser_error_occurred (parser);
28521 }
28522 }
28523
28524 /* We did not really want to consume any tokens. */
28525 cp_parser_abort_tentative_parse (parser);
28526
28527 return constructor_p;
28528 }
28529
28530 /* Parse the definition of the function given by the DECL_SPECIFIERS,
28531 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
28532 they must be performed once we are in the scope of the function.
28533
28534 Returns the function defined. */
28535
28536 static tree
28537 cp_parser_function_definition_from_specifiers_and_declarator
28538 (cp_parser* parser,
28539 cp_decl_specifier_seq *decl_specifiers,
28540 tree attributes,
28541 const cp_declarator *declarator)
28542 {
28543 tree fn;
28544 bool success_p;
28545
28546 /* Begin the function-definition. */
28547 success_p = start_function (decl_specifiers, declarator, attributes);
28548
28549 /* The things we're about to see are not directly qualified by any
28550 template headers we've seen thus far. */
28551 reset_specialization ();
28552
28553 /* If there were names looked up in the decl-specifier-seq that we
28554 did not check, check them now. We must wait until we are in the
28555 scope of the function to perform the checks, since the function
28556 might be a friend. */
28557 perform_deferred_access_checks (tf_warning_or_error);
28558
28559 if (success_p)
28560 {
28561 cp_finalize_omp_declare_simd (parser, current_function_decl);
28562 parser->omp_declare_simd = NULL;
28563 cp_finalize_oacc_routine (parser, current_function_decl, true);
28564 parser->oacc_routine = NULL;
28565 }
28566
28567 if (!success_p)
28568 {
28569 /* Skip the entire function. */
28570 cp_parser_skip_to_end_of_block_or_statement (parser);
28571 fn = error_mark_node;
28572 }
28573 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
28574 {
28575 /* Seen already, skip it. An error message has already been output. */
28576 cp_parser_skip_to_end_of_block_or_statement (parser);
28577 fn = current_function_decl;
28578 current_function_decl = NULL_TREE;
28579 /* If this is a function from a class, pop the nested class. */
28580 if (current_class_name)
28581 pop_nested_class ();
28582 }
28583 else
28584 {
28585 timevar_id_t tv;
28586 if (DECL_DECLARED_INLINE_P (current_function_decl))
28587 tv = TV_PARSE_INLINE;
28588 else
28589 tv = TV_PARSE_FUNC;
28590 timevar_push (tv);
28591 fn = cp_parser_function_definition_after_declarator (parser,
28592 /*inline_p=*/false);
28593 timevar_pop (tv);
28594 }
28595
28596 return fn;
28597 }
28598
28599 /* Parse the part of a function-definition that follows the
28600 declarator. INLINE_P is TRUE iff this function is an inline
28601 function defined within a class-specifier.
28602
28603 Returns the function defined. */
28604
28605 static tree
28606 cp_parser_function_definition_after_declarator (cp_parser* parser,
28607 bool inline_p)
28608 {
28609 tree fn;
28610 bool saved_in_unbraced_linkage_specification_p;
28611 bool saved_in_function_body;
28612 unsigned saved_num_template_parameter_lists;
28613 cp_token *token;
28614 bool fully_implicit_function_template_p
28615 = parser->fully_implicit_function_template_p;
28616 parser->fully_implicit_function_template_p = false;
28617 tree implicit_template_parms
28618 = parser->implicit_template_parms;
28619 parser->implicit_template_parms = 0;
28620 cp_binding_level* implicit_template_scope
28621 = parser->implicit_template_scope;
28622 parser->implicit_template_scope = 0;
28623
28624 saved_in_function_body = parser->in_function_body;
28625 parser->in_function_body = true;
28626 /* If the next token is `return', then the code may be trying to
28627 make use of the "named return value" extension that G++ used to
28628 support. */
28629 token = cp_lexer_peek_token (parser->lexer);
28630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
28631 {
28632 /* Consume the `return' keyword. */
28633 cp_lexer_consume_token (parser->lexer);
28634 /* Look for the identifier that indicates what value is to be
28635 returned. */
28636 cp_parser_identifier (parser);
28637 /* Issue an error message. */
28638 error_at (token->location,
28639 "named return values are no longer supported");
28640 /* Skip tokens until we reach the start of the function body. */
28641 while (true)
28642 {
28643 cp_token *token = cp_lexer_peek_token (parser->lexer);
28644 if (token->type == CPP_OPEN_BRACE
28645 || token->type == CPP_EOF
28646 || token->type == CPP_PRAGMA_EOL)
28647 break;
28648 cp_lexer_consume_token (parser->lexer);
28649 }
28650 }
28651 /* The `extern' in `extern "C" void f () { ... }' does not apply to
28652 anything declared inside `f'. */
28653 saved_in_unbraced_linkage_specification_p
28654 = parser->in_unbraced_linkage_specification_p;
28655 parser->in_unbraced_linkage_specification_p = false;
28656 /* Inside the function, surrounding template-parameter-lists do not
28657 apply. */
28658 saved_num_template_parameter_lists
28659 = parser->num_template_parameter_lists;
28660 parser->num_template_parameter_lists = 0;
28661
28662 /* If the next token is `try', `__transaction_atomic', or
28663 `__transaction_relaxed`, then we are looking at either function-try-block
28664 or function-transaction-block. Note that all of these include the
28665 function-body. */
28666 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
28667 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
28668 else if (cp_lexer_next_token_is_keyword (parser->lexer,
28669 RID_TRANSACTION_RELAXED))
28670 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
28671 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28672 cp_parser_function_try_block (parser);
28673 else
28674 cp_parser_ctor_initializer_opt_and_function_body
28675 (parser, /*in_function_try_block=*/false);
28676
28677 /* Finish the function. */
28678 fn = finish_function (inline_p);
28679 /* Generate code for it, if necessary. */
28680 expand_or_defer_fn (fn);
28681 /* Restore the saved values. */
28682 parser->in_unbraced_linkage_specification_p
28683 = saved_in_unbraced_linkage_specification_p;
28684 parser->num_template_parameter_lists
28685 = saved_num_template_parameter_lists;
28686 parser->in_function_body = saved_in_function_body;
28687
28688 parser->fully_implicit_function_template_p
28689 = fully_implicit_function_template_p;
28690 parser->implicit_template_parms
28691 = implicit_template_parms;
28692 parser->implicit_template_scope
28693 = implicit_template_scope;
28694
28695 if (parser->fully_implicit_function_template_p)
28696 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
28697
28698 return fn;
28699 }
28700
28701 /* Parse a template-declaration body (following argument list). */
28702
28703 static void
28704 cp_parser_template_declaration_after_parameters (cp_parser* parser,
28705 tree parameter_list,
28706 bool member_p)
28707 {
28708 tree decl = NULL_TREE;
28709 bool friend_p = false;
28710
28711 /* We just processed one more parameter list. */
28712 ++parser->num_template_parameter_lists;
28713
28714 /* Get the deferred access checks from the parameter list. These
28715 will be checked once we know what is being declared, as for a
28716 member template the checks must be performed in the scope of the
28717 class containing the member. */
28718 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
28719
28720 /* Tentatively parse for a new template parameter list, which can either be
28721 the template keyword or a template introduction. */
28722 if (cp_parser_template_declaration_after_export (parser, member_p))
28723 /* OK */;
28724 else if (cxx_dialect >= cxx11
28725 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
28726 decl = cp_parser_alias_declaration (parser);
28727 else if (cxx_dialect >= cxx2a /* Implies flag_concept. */
28728 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
28729 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_BOOL))
28730 /* Allow 'concept bool' to be handled as per the TS. */
28731 decl = cp_parser_concept_definition (parser);
28732 else
28733 {
28734 /* There are no access checks when parsing a template, as we do not
28735 know if a specialization will be a friend. */
28736 push_deferring_access_checks (dk_no_check);
28737 cp_token *token = cp_lexer_peek_token (parser->lexer);
28738 decl = cp_parser_single_declaration (parser,
28739 checks,
28740 member_p,
28741 /*explicit_specialization_p=*/false,
28742 &friend_p);
28743 pop_deferring_access_checks ();
28744
28745 /* If this is a member template declaration, let the front
28746 end know. */
28747 if (member_p && !friend_p && decl)
28748 {
28749 if (TREE_CODE (decl) == TYPE_DECL)
28750 cp_parser_check_access_in_redeclaration (decl, token->location);
28751
28752 decl = finish_member_template_decl (decl);
28753 }
28754 else if (friend_p && decl
28755 && DECL_DECLARES_TYPE_P (decl))
28756 make_friend_class (current_class_type, TREE_TYPE (decl),
28757 /*complain=*/true);
28758 }
28759 /* We are done with the current parameter list. */
28760 --parser->num_template_parameter_lists;
28761
28762 pop_deferring_access_checks ();
28763
28764 /* Finish up. */
28765 finish_template_decl (parameter_list);
28766
28767 /* Check the template arguments for a literal operator template. */
28768 if (decl
28769 && DECL_DECLARES_FUNCTION_P (decl)
28770 && UDLIT_OPER_P (DECL_NAME (decl)))
28771 {
28772 bool ok = true;
28773 if (parameter_list == NULL_TREE)
28774 ok = false;
28775 else
28776 {
28777 int num_parms = TREE_VEC_LENGTH (parameter_list);
28778 if (num_parms == 1)
28779 {
28780 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
28781 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
28782 if (TREE_CODE (parm) != PARM_DECL)
28783 ok = false;
28784 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
28785 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
28786 /* OK, C++20 string literal operator template. We don't need
28787 to warn in lower dialects here because we will have already
28788 warned about the template parameter. */;
28789 else if (TREE_TYPE (parm) != char_type_node
28790 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
28791 ok = false;
28792 }
28793 else if (num_parms == 2 && cxx_dialect >= cxx14)
28794 {
28795 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
28796 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
28797 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
28798 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
28799 if (TREE_CODE (parm) != PARM_DECL
28800 || TREE_TYPE (parm) != TREE_TYPE (type)
28801 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
28802 ok = false;
28803 else
28804 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
28805 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
28806 "ISO C++ did not adopt string literal operator templa"
28807 "tes taking an argument pack of characters");
28808 }
28809 else
28810 ok = false;
28811 }
28812 if (!ok)
28813 {
28814 if (cxx_dialect > cxx17)
28815 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
28816 "template %qD has invalid parameter list; expected "
28817 "non-type template parameter pack %<<char...>%> or "
28818 "single non-type parameter of class type",
28819 decl);
28820 else
28821 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
28822 "template %qD has invalid parameter list; expected "
28823 "non-type template parameter pack %<<char...>%>",
28824 decl);
28825 }
28826 }
28827
28828 /* Register member declarations. */
28829 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
28830 finish_member_declaration (decl);
28831 /* If DECL is a function template, we must return to parse it later.
28832 (Even though there is no definition, there might be default
28833 arguments that need handling.) */
28834 if (member_p && decl
28835 && DECL_DECLARES_FUNCTION_P (decl))
28836 vec_safe_push (unparsed_funs_with_definitions, decl);
28837 }
28838
28839 /* Parse a template introduction header for a template-declaration. Returns
28840 false if tentative parse fails. */
28841
28842 static bool
28843 cp_parser_template_introduction (cp_parser* parser, bool member_p)
28844 {
28845 cp_parser_parse_tentatively (parser);
28846
28847 tree saved_scope = parser->scope;
28848 tree saved_object_scope = parser->object_scope;
28849 tree saved_qualifying_scope = parser->qualifying_scope;
28850
28851 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
28852
28853 /* Look for the optional `::' operator. */
28854 cp_parser_global_scope_opt (parser,
28855 /*current_scope_valid_p=*/false);
28856 /* Look for the nested-name-specifier. */
28857 cp_parser_nested_name_specifier_opt (parser,
28858 /*typename_keyword_p=*/false,
28859 /*check_dependency_p=*/true,
28860 /*type_p=*/false,
28861 /*is_declaration=*/false);
28862
28863 cp_token *token = cp_lexer_peek_token (parser->lexer);
28864 tree concept_name = cp_parser_identifier (parser);
28865
28866 /* Look up the concept for which we will be matching
28867 template parameters. */
28868 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
28869 token->location);
28870 parser->scope = saved_scope;
28871 parser->object_scope = saved_object_scope;
28872 parser->qualifying_scope = saved_qualifying_scope;
28873
28874 if (concept_name == error_mark_node
28875 || (seen_error () && !concept_definition_p (tmpl_decl)))
28876 cp_parser_simulate_error (parser);
28877
28878 /* Look for opening brace for introduction. */
28879 matching_braces braces;
28880 braces.require_open (parser);
28881 location_t open_loc = input_location;
28882
28883 if (!cp_parser_parse_definitely (parser))
28884 return false;
28885
28886 push_deferring_access_checks (dk_deferred);
28887
28888 /* Build vector of placeholder parameters and grab
28889 matching identifiers. */
28890 tree introduction_list = cp_parser_introduction_list (parser);
28891
28892 /* Look for closing brace for introduction. */
28893 if (!braces.require_close (parser))
28894 return true;
28895
28896 /* The introduction-list shall not be empty. */
28897 int nargs = TREE_VEC_LENGTH (introduction_list);
28898 if (nargs == 0)
28899 {
28900 /* In cp_parser_introduction_list we have already issued an error. */
28901 return true;
28902 }
28903
28904 if (tmpl_decl == error_mark_node)
28905 {
28906 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
28907 token->location);
28908 return true;
28909 }
28910
28911 /* Build and associate the constraint. */
28912 location_t introduction_loc = make_location (open_loc,
28913 start_token->location,
28914 parser->lexer);
28915 tree parms = finish_template_introduction (tmpl_decl,
28916 introduction_list,
28917 introduction_loc);
28918 if (parms && parms != error_mark_node)
28919 {
28920 if (!flag_concepts_ts)
28921 pedwarn (introduction_loc, 0, "template-introductions"
28922 " are not part of C++20 concepts [-fconcepts-ts]");
28923
28924 cp_parser_template_declaration_after_parameters (parser, parms,
28925 member_p);
28926 return true;
28927 }
28928
28929 if (parms == NULL_TREE)
28930 error_at (token->location, "no matching concept for template-introduction");
28931
28932 return true;
28933 }
28934
28935 /* Parse a normal template-declaration following the template keyword. */
28936
28937 static void
28938 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
28939 {
28940 tree parameter_list;
28941 bool need_lang_pop;
28942 location_t location = input_location;
28943
28944 /* Look for the `<' token. */
28945 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
28946 return;
28947 if (at_class_scope_p () && current_function_decl)
28948 {
28949 /* 14.5.2.2 [temp.mem]
28950
28951 A local class shall not have member templates. */
28952 error_at (location,
28953 "invalid declaration of member template in local class");
28954 cp_parser_skip_to_end_of_block_or_statement (parser);
28955 return;
28956 }
28957 /* [temp]
28958
28959 A template ... shall not have C linkage. */
28960 if (current_lang_name == lang_name_c)
28961 {
28962 error_at (location, "template with C linkage");
28963 maybe_show_extern_c_location ();
28964 /* Give it C++ linkage to avoid confusing other parts of the
28965 front end. */
28966 push_lang_context (lang_name_cplusplus);
28967 need_lang_pop = true;
28968 }
28969 else
28970 need_lang_pop = false;
28971
28972 /* We cannot perform access checks on the template parameter
28973 declarations until we know what is being declared, just as we
28974 cannot check the decl-specifier list. */
28975 push_deferring_access_checks (dk_deferred);
28976
28977 /* If the next token is `>', then we have an invalid
28978 specialization. Rather than complain about an invalid template
28979 parameter, issue an error message here. */
28980 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
28981 {
28982 cp_parser_error (parser, "invalid explicit specialization");
28983 begin_specialization ();
28984 parameter_list = NULL_TREE;
28985 }
28986 else
28987 {
28988 /* Parse the template parameters. */
28989 parameter_list = cp_parser_template_parameter_list (parser);
28990 }
28991
28992 /* Look for the `>'. */
28993 cp_parser_skip_to_end_of_template_parameter_list (parser);
28994
28995 /* Manage template requirements */
28996 if (flag_concepts)
28997 {
28998 tree reqs = get_shorthand_constraints (current_template_parms);
28999 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
29000 reqs = combine_constraint_expressions (reqs, treqs);
29001 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
29002 }
29003
29004 cp_parser_template_declaration_after_parameters (parser, parameter_list,
29005 member_p);
29006
29007 /* For the erroneous case of a template with C linkage, we pushed an
29008 implicit C++ linkage scope; exit that scope now. */
29009 if (need_lang_pop)
29010 pop_lang_context ();
29011 }
29012
29013 /* Parse a template-declaration, assuming that the `export' (and
29014 `extern') keywords, if present, has already been scanned. MEMBER_P
29015 is as for cp_parser_template_declaration. */
29016
29017 static bool
29018 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
29019 {
29020 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29021 {
29022 cp_lexer_consume_token (parser->lexer);
29023 cp_parser_explicit_template_declaration (parser, member_p);
29024 return true;
29025 }
29026 else if (flag_concepts)
29027 return cp_parser_template_introduction (parser, member_p);
29028
29029 return false;
29030 }
29031
29032 /* Perform the deferred access checks from a template-parameter-list.
29033 CHECKS is a TREE_LIST of access checks, as returned by
29034 get_deferred_access_checks. */
29035
29036 static void
29037 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
29038 {
29039 ++processing_template_parmlist;
29040 perform_access_checks (checks, tf_warning_or_error);
29041 --processing_template_parmlist;
29042 }
29043
29044 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
29045 `function-definition' sequence that follows a template header.
29046 If MEMBER_P is true, this declaration appears in a class scope.
29047
29048 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
29049 *FRIEND_P is set to TRUE iff the declaration is a friend. */
29050
29051 static tree
29052 cp_parser_single_declaration (cp_parser* parser,
29053 vec<deferred_access_check, va_gc> *checks,
29054 bool member_p,
29055 bool explicit_specialization_p,
29056 bool* friend_p)
29057 {
29058 int declares_class_or_enum;
29059 tree decl = NULL_TREE;
29060 cp_decl_specifier_seq decl_specifiers;
29061 bool function_definition_p = false;
29062 cp_token *decl_spec_token_start;
29063
29064 /* This function is only used when processing a template
29065 declaration. */
29066 gcc_assert (innermost_scope_kind () == sk_template_parms
29067 || innermost_scope_kind () == sk_template_spec);
29068
29069 /* Defer access checks until we know what is being declared. */
29070 push_deferring_access_checks (dk_deferred);
29071
29072 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
29073 alternative. */
29074 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
29075 cp_parser_decl_specifier_seq (parser,
29076 (CP_PARSER_FLAGS_OPTIONAL
29077 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
29078 &decl_specifiers,
29079 &declares_class_or_enum);
29080 if (friend_p)
29081 *friend_p = cp_parser_friend_p (&decl_specifiers);
29082
29083 /* There are no template typedefs. */
29084 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
29085 {
29086 error_at (decl_spec_token_start->location,
29087 "template declaration of %<typedef%>");
29088 decl = error_mark_node;
29089 }
29090
29091 /* Gather up the access checks that occurred the
29092 decl-specifier-seq. */
29093 stop_deferring_access_checks ();
29094
29095 /* Check for the declaration of a template class. */
29096 if (declares_class_or_enum)
29097 {
29098 if (cp_parser_declares_only_class_p (parser)
29099 || (declares_class_or_enum & 2))
29100 {
29101 /* If this is a declaration, but not a definition, associate
29102 any constraints with the type declaration. Constraints
29103 are associated with definitions in cp_parser_class_specifier. */
29104 if (declares_class_or_enum == 1)
29105 associate_classtype_constraints (decl_specifiers.type);
29106
29107 decl = shadow_tag (&decl_specifiers);
29108
29109 /* In this case:
29110
29111 struct C {
29112 friend template <typename T> struct A<T>::B;
29113 };
29114
29115 A<T>::B will be represented by a TYPENAME_TYPE, and
29116 therefore not recognized by shadow_tag. */
29117 if (friend_p && *friend_p
29118 && !decl
29119 && decl_specifiers.type
29120 && TYPE_P (decl_specifiers.type))
29121 decl = decl_specifiers.type;
29122
29123 if (decl && decl != error_mark_node)
29124 decl = TYPE_NAME (decl);
29125 else
29126 decl = error_mark_node;
29127
29128 /* Perform access checks for template parameters. */
29129 cp_parser_perform_template_parameter_access_checks (checks);
29130
29131 /* Give a helpful diagnostic for
29132 template <class T> struct A { } a;
29133 if we aren't already recovering from an error. */
29134 if (!cp_parser_declares_only_class_p (parser)
29135 && !seen_error ())
29136 {
29137 error_at (cp_lexer_peek_token (parser->lexer)->location,
29138 "a class template declaration must not declare "
29139 "anything else");
29140 cp_parser_skip_to_end_of_block_or_statement (parser);
29141 goto out;
29142 }
29143 }
29144 }
29145
29146 /* Complain about missing 'typename' or other invalid type names. */
29147 if (!decl_specifiers.any_type_specifiers_p
29148 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
29149 {
29150 /* cp_parser_parse_and_diagnose_invalid_type_name calls
29151 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
29152 the rest of this declaration. */
29153 decl = error_mark_node;
29154 goto out;
29155 }
29156
29157 /* If it's not a template class, try for a template function. If
29158 the next token is a `;', then this declaration does not declare
29159 anything. But, if there were errors in the decl-specifiers, then
29160 the error might well have come from an attempted class-specifier.
29161 In that case, there's no need to warn about a missing declarator. */
29162 if (!decl
29163 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
29164 || decl_specifiers.type != error_mark_node))
29165 {
29166 decl = cp_parser_init_declarator (parser,
29167 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
29168 &decl_specifiers,
29169 checks,
29170 /*function_definition_allowed_p=*/true,
29171 member_p,
29172 declares_class_or_enum,
29173 &function_definition_p,
29174 NULL, NULL, NULL);
29175
29176 /* 7.1.1-1 [dcl.stc]
29177
29178 A storage-class-specifier shall not be specified in an explicit
29179 specialization... */
29180 if (decl
29181 && explicit_specialization_p
29182 && decl_specifiers.storage_class != sc_none)
29183 {
29184 error_at (decl_spec_token_start->location,
29185 "explicit template specialization cannot have a storage class");
29186 decl = error_mark_node;
29187 }
29188
29189 if (decl && VAR_P (decl))
29190 check_template_variable (decl);
29191 }
29192
29193 /* Look for a trailing `;' after the declaration. */
29194 if (!function_definition_p
29195 && (decl == error_mark_node
29196 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
29197 cp_parser_skip_to_end_of_block_or_statement (parser);
29198
29199 out:
29200 pop_deferring_access_checks ();
29201
29202 /* Clear any current qualification; whatever comes next is the start
29203 of something new. */
29204 parser->scope = NULL_TREE;
29205 parser->qualifying_scope = NULL_TREE;
29206 parser->object_scope = NULL_TREE;
29207
29208 return decl;
29209 }
29210
29211 /* Parse a cast-expression that is not the operand of a unary "&". */
29212
29213 static cp_expr
29214 cp_parser_simple_cast_expression (cp_parser *parser)
29215 {
29216 return cp_parser_cast_expression (parser, /*address_p=*/false,
29217 /*cast_p=*/false, /*decltype*/false, NULL);
29218 }
29219
29220 /* Parse a functional cast to TYPE. Returns an expression
29221 representing the cast. */
29222
29223 static cp_expr
29224 cp_parser_functional_cast (cp_parser* parser, tree type)
29225 {
29226 vec<tree, va_gc> *vec;
29227 tree expression_list;
29228 cp_expr cast;
29229 bool nonconst_p;
29230
29231 location_t start_loc = input_location;
29232
29233 if (!type)
29234 type = error_mark_node;
29235
29236 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29237 {
29238 cp_lexer_set_source_position (parser->lexer);
29239 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
29240 expression_list = cp_parser_braced_list (parser, &nonconst_p);
29241 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
29242 if (TREE_CODE (type) == TYPE_DECL)
29243 type = TREE_TYPE (type);
29244
29245 cast = finish_compound_literal (type, expression_list,
29246 tf_warning_or_error, fcl_functional);
29247 /* Create a location of the form:
29248 type_name{i, f}
29249 ^~~~~~~~~~~~~~~
29250 with caret == start at the start of the type name,
29251 finishing at the closing brace. */
29252 location_t combined_loc = make_location (start_loc, start_loc,
29253 parser->lexer);
29254 cast.set_location (combined_loc);
29255 return cast;
29256 }
29257
29258
29259 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
29260 /*cast_p=*/true,
29261 /*allow_expansion_p=*/true,
29262 /*non_constant_p=*/NULL);
29263 if (vec == NULL)
29264 expression_list = error_mark_node;
29265 else
29266 {
29267 expression_list = build_tree_list_vec (vec);
29268 release_tree_vector (vec);
29269 }
29270
29271 /* Create a location of the form:
29272 float(i)
29273 ^~~~~~~~
29274 with caret == start at the start of the type name,
29275 finishing at the closing paren. */
29276 location_t combined_loc = make_location (start_loc, start_loc,
29277 parser->lexer);
29278 cast = build_functional_cast (combined_loc, type, expression_list,
29279 tf_warning_or_error);
29280 cast.set_location (combined_loc);
29281
29282 /* [expr.const]/1: In an integral constant expression "only type
29283 conversions to integral or enumeration type can be used". */
29284 if (TREE_CODE (type) == TYPE_DECL)
29285 type = TREE_TYPE (type);
29286 if (cast != error_mark_node
29287 && !cast_valid_in_integral_constant_expression_p (type)
29288 && cp_parser_non_integral_constant_expression (parser,
29289 NIC_CONSTRUCTOR))
29290 return error_mark_node;
29291
29292 return cast;
29293 }
29294
29295 /* Save the tokens that make up the body of a member function defined
29296 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
29297 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
29298 specifiers applied to the declaration. Returns the FUNCTION_DECL
29299 for the member function. */
29300
29301 static tree
29302 cp_parser_save_member_function_body (cp_parser* parser,
29303 cp_decl_specifier_seq *decl_specifiers,
29304 cp_declarator *declarator,
29305 tree attributes)
29306 {
29307 cp_token *first;
29308 cp_token *last;
29309 tree fn;
29310 bool function_try_block = false;
29311
29312 /* Create the FUNCTION_DECL. */
29313 fn = grokmethod (decl_specifiers, declarator, attributes);
29314 cp_finalize_omp_declare_simd (parser, fn);
29315 cp_finalize_oacc_routine (parser, fn, true);
29316 /* If something went badly wrong, bail out now. */
29317 if (fn == error_mark_node)
29318 {
29319 /* If there's a function-body, skip it. */
29320 if (cp_parser_token_starts_function_definition_p
29321 (cp_lexer_peek_token (parser->lexer)))
29322 cp_parser_skip_to_end_of_block_or_statement (parser);
29323 return error_mark_node;
29324 }
29325
29326 /* Remember it, if there are default args to post process. */
29327 cp_parser_save_default_args (parser, fn);
29328
29329 /* Save away the tokens that make up the body of the
29330 function. */
29331 first = parser->lexer->next_token;
29332
29333 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
29334 cp_lexer_consume_token (parser->lexer);
29335 else if (cp_lexer_next_token_is_keyword (parser->lexer,
29336 RID_TRANSACTION_ATOMIC))
29337 {
29338 cp_lexer_consume_token (parser->lexer);
29339 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
29340 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
29341 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
29342 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
29343 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
29344 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
29345 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
29346 {
29347 cp_lexer_consume_token (parser->lexer);
29348 cp_lexer_consume_token (parser->lexer);
29349 cp_lexer_consume_token (parser->lexer);
29350 cp_lexer_consume_token (parser->lexer);
29351 cp_lexer_consume_token (parser->lexer);
29352 }
29353 else
29354 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
29355 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
29356 {
29357 cp_lexer_consume_token (parser->lexer);
29358 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
29359 break;
29360 }
29361 }
29362
29363 /* Handle function try blocks. */
29364 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
29365 {
29366 cp_lexer_consume_token (parser->lexer);
29367 function_try_block = true;
29368 }
29369 /* We can have braced-init-list mem-initializers before the fn body. */
29370 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29371 {
29372 cp_lexer_consume_token (parser->lexer);
29373 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
29374 {
29375 /* cache_group will stop after an un-nested { } pair, too. */
29376 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
29377 break;
29378
29379 /* variadic mem-inits have ... after the ')'. */
29380 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29381 cp_lexer_consume_token (parser->lexer);
29382 }
29383 }
29384 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29385 /* Handle function try blocks. */
29386 if (function_try_block)
29387 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
29388 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29389 last = parser->lexer->next_token;
29390
29391 /* Save away the inline definition; we will process it when the
29392 class is complete. */
29393 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
29394 DECL_PENDING_INLINE_P (fn) = 1;
29395
29396 /* We need to know that this was defined in the class, so that
29397 friend templates are handled correctly. */
29398 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
29399
29400 /* Add FN to the queue of functions to be parsed later. */
29401 vec_safe_push (unparsed_funs_with_definitions, fn);
29402
29403 return fn;
29404 }
29405
29406 /* Save the tokens that make up the in-class initializer for a non-static
29407 data member. Returns a DEFERRED_PARSE. */
29408
29409 static tree
29410 cp_parser_save_nsdmi (cp_parser* parser)
29411 {
29412 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
29413 }
29414
29415 /* Parse a template-argument-list, as well as the trailing ">" (but
29416 not the opening "<"). See cp_parser_template_argument_list for the
29417 return value. */
29418
29419 static tree
29420 cp_parser_enclosed_template_argument_list (cp_parser* parser)
29421 {
29422 tree arguments;
29423 tree saved_scope;
29424 tree saved_qualifying_scope;
29425 tree saved_object_scope;
29426 bool saved_greater_than_is_operator_p;
29427
29428 /* [temp.names]
29429
29430 When parsing a template-id, the first non-nested `>' is taken as
29431 the end of the template-argument-list rather than a greater-than
29432 operator. */
29433 saved_greater_than_is_operator_p
29434 = parser->greater_than_is_operator_p;
29435 parser->greater_than_is_operator_p = false;
29436 /* Parsing the argument list may modify SCOPE, so we save it
29437 here. */
29438 saved_scope = parser->scope;
29439 saved_qualifying_scope = parser->qualifying_scope;
29440 saved_object_scope = parser->object_scope;
29441 /* We need to evaluate the template arguments, even though this
29442 template-id may be nested within a "sizeof". */
29443 cp_evaluated ev;
29444 /* Parse the template-argument-list itself. */
29445 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
29446 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
29447 arguments = NULL_TREE;
29448 else
29449 arguments = cp_parser_template_argument_list (parser);
29450 /* Look for the `>' that ends the template-argument-list. If we find
29451 a '>>' instead, it's probably just a typo. */
29452 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
29453 {
29454 if (cxx_dialect != cxx98)
29455 {
29456 /* In C++0x, a `>>' in a template argument list or cast
29457 expression is considered to be two separate `>'
29458 tokens. So, change the current token to a `>', but don't
29459 consume it: it will be consumed later when the outer
29460 template argument list (or cast expression) is parsed.
29461 Note that this replacement of `>' for `>>' is necessary
29462 even if we are parsing tentatively: in the tentative
29463 case, after calling
29464 cp_parser_enclosed_template_argument_list we will always
29465 throw away all of the template arguments and the first
29466 closing `>', either because the template argument list
29467 was erroneous or because we are replacing those tokens
29468 with a CPP_TEMPLATE_ID token. The second `>' (which will
29469 not have been thrown away) is needed either to close an
29470 outer template argument list or to complete a new-style
29471 cast. */
29472 cp_token *token = cp_lexer_peek_token (parser->lexer);
29473 token->type = CPP_GREATER;
29474 }
29475 else if (!saved_greater_than_is_operator_p)
29476 {
29477 /* If we're in a nested template argument list, the '>>' has
29478 to be a typo for '> >'. We emit the error message, but we
29479 continue parsing and we push a '>' as next token, so that
29480 the argument list will be parsed correctly. Note that the
29481 global source location is still on the token before the
29482 '>>', so we need to say explicitly where we want it. */
29483 cp_token *token = cp_lexer_peek_token (parser->lexer);
29484 gcc_rich_location richloc (token->location);
29485 richloc.add_fixit_replace ("> >");
29486 error_at (&richloc, "%<>>%> should be %<> >%> "
29487 "within a nested template argument list");
29488
29489 token->type = CPP_GREATER;
29490 }
29491 else
29492 {
29493 /* If this is not a nested template argument list, the '>>'
29494 is a typo for '>'. Emit an error message and continue.
29495 Same deal about the token location, but here we can get it
29496 right by consuming the '>>' before issuing the diagnostic. */
29497 cp_token *token = cp_lexer_consume_token (parser->lexer);
29498 error_at (token->location,
29499 "spurious %<>>%>, use %<>%> to terminate "
29500 "a template argument list");
29501 }
29502 }
29503 else
29504 cp_parser_skip_to_end_of_template_parameter_list (parser);
29505 /* The `>' token might be a greater-than operator again now. */
29506 parser->greater_than_is_operator_p
29507 = saved_greater_than_is_operator_p;
29508 /* Restore the SAVED_SCOPE. */
29509 parser->scope = saved_scope;
29510 parser->qualifying_scope = saved_qualifying_scope;
29511 parser->object_scope = saved_object_scope;
29512
29513 return arguments;
29514 }
29515
29516 /* MEMBER_FUNCTION is a member function, or a friend. If default
29517 arguments, or the body of the function have not yet been parsed,
29518 parse them now. */
29519
29520 static void
29521 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
29522 {
29523 timevar_push (TV_PARSE_INMETH);
29524 /* If this member is a template, get the underlying
29525 FUNCTION_DECL. */
29526 if (DECL_FUNCTION_TEMPLATE_P (member_function))
29527 member_function = DECL_TEMPLATE_RESULT (member_function);
29528
29529 /* There should not be any class definitions in progress at this
29530 point; the bodies of members are only parsed outside of all class
29531 definitions. */
29532 gcc_assert (parser->num_classes_being_defined == 0);
29533 /* While we're parsing the member functions we might encounter more
29534 classes. We want to handle them right away, but we don't want
29535 them getting mixed up with functions that are currently in the
29536 queue. */
29537 push_unparsed_function_queues (parser);
29538
29539 /* Make sure that any template parameters are in scope. */
29540 maybe_begin_member_template_processing (member_function);
29541
29542 /* If the body of the function has not yet been parsed, parse it
29543 now. */
29544 if (DECL_PENDING_INLINE_P (member_function))
29545 {
29546 tree function_scope;
29547 cp_token_cache *tokens;
29548
29549 /* The function is no longer pending; we are processing it. */
29550 tokens = DECL_PENDING_INLINE_INFO (member_function);
29551 DECL_PENDING_INLINE_INFO (member_function) = NULL;
29552 DECL_PENDING_INLINE_P (member_function) = 0;
29553
29554 /* If this is a local class, enter the scope of the containing
29555 function. */
29556 function_scope = current_function_decl;
29557 if (function_scope)
29558 push_function_context ();
29559
29560 /* Push the body of the function onto the lexer stack. */
29561 cp_parser_push_lexer_for_tokens (parser, tokens);
29562
29563 /* Let the front end know that we going to be defining this
29564 function. */
29565 start_preparsed_function (member_function, NULL_TREE,
29566 SF_PRE_PARSED | SF_INCLASS_INLINE);
29567
29568 /* Don't do access checking if it is a templated function. */
29569 if (processing_template_decl)
29570 push_deferring_access_checks (dk_no_check);
29571
29572 /* #pragma omp declare reduction needs special parsing. */
29573 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
29574 {
29575 parser->lexer->in_pragma = true;
29576 cp_parser_omp_declare_reduction_exprs (member_function, parser);
29577 finish_function (/*inline_p=*/true);
29578 cp_check_omp_declare_reduction (member_function);
29579 }
29580 else
29581 /* Now, parse the body of the function. */
29582 cp_parser_function_definition_after_declarator (parser,
29583 /*inline_p=*/true);
29584
29585 if (processing_template_decl)
29586 pop_deferring_access_checks ();
29587
29588 /* Leave the scope of the containing function. */
29589 if (function_scope)
29590 pop_function_context ();
29591 cp_parser_pop_lexer (parser);
29592 }
29593
29594 /* Remove any template parameters from the symbol table. */
29595 maybe_end_member_template_processing ();
29596
29597 /* Restore the queue. */
29598 pop_unparsed_function_queues (parser);
29599 timevar_pop (TV_PARSE_INMETH);
29600 }
29601
29602 /* If DECL contains any default args, remember it on the unparsed
29603 functions queue. */
29604
29605 static void
29606 cp_parser_save_default_args (cp_parser* parser, tree decl)
29607 {
29608 tree probe;
29609
29610 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
29611 probe;
29612 probe = TREE_CHAIN (probe))
29613 if (TREE_PURPOSE (probe))
29614 {
29615 cp_default_arg_entry entry = {current_class_type, decl};
29616 vec_safe_push (unparsed_funs_with_default_args, entry);
29617 break;
29618 }
29619
29620 /* Remember if there is a noexcept-specifier to post process. */
29621 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
29622 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
29623 vec_safe_push (unparsed_noexcepts, decl);
29624 }
29625
29626 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
29627 which is either a FIELD_DECL or PARM_DECL. Parse it and return
29628 the result. For a PARM_DECL, PARMTYPE is the corresponding type
29629 from the parameter-type-list. */
29630
29631 static tree
29632 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
29633 tree default_arg, tree parmtype)
29634 {
29635 cp_token_cache *tokens;
29636 tree parsed_arg;
29637 bool dummy;
29638
29639 if (default_arg == error_mark_node)
29640 return error_mark_node;
29641
29642 /* Push the saved tokens for the default argument onto the parser's
29643 lexer stack. */
29644 tokens = DEFPARSE_TOKENS (default_arg);
29645 cp_parser_push_lexer_for_tokens (parser, tokens);
29646
29647 start_lambda_scope (decl);
29648
29649 /* Parse the default argument. */
29650 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
29651 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
29652 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
29653
29654 finish_lambda_scope ();
29655
29656 if (parsed_arg == error_mark_node)
29657 cp_parser_skip_to_end_of_statement (parser);
29658
29659 if (!processing_template_decl)
29660 {
29661 /* In a non-template class, check conversions now. In a template,
29662 we'll wait and instantiate these as needed. */
29663 if (TREE_CODE (decl) == PARM_DECL)
29664 parsed_arg = check_default_argument (parmtype, parsed_arg,
29665 tf_warning_or_error);
29666 else if (maybe_reject_flexarray_init (decl, parsed_arg))
29667 parsed_arg = error_mark_node;
29668 else
29669 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
29670 }
29671
29672 /* If the token stream has not been completely used up, then
29673 there was extra junk after the end of the default
29674 argument. */
29675 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29676 {
29677 if (TREE_CODE (decl) == PARM_DECL)
29678 cp_parser_error (parser, "expected %<,%>");
29679 else
29680 cp_parser_error (parser, "expected %<;%>");
29681 }
29682
29683 /* Revert to the main lexer. */
29684 cp_parser_pop_lexer (parser);
29685
29686 return parsed_arg;
29687 }
29688
29689 /* FIELD is a non-static data member with an initializer which we saved for
29690 later; parse it now. */
29691
29692 static void
29693 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
29694 {
29695 tree def;
29696
29697 maybe_begin_member_template_processing (field);
29698
29699 push_unparsed_function_queues (parser);
29700 def = cp_parser_late_parse_one_default_arg (parser, field,
29701 DECL_INITIAL (field),
29702 NULL_TREE);
29703 pop_unparsed_function_queues (parser);
29704
29705 maybe_end_member_template_processing ();
29706
29707 DECL_INITIAL (field) = def;
29708 }
29709
29710 /* FN is a FUNCTION_DECL which may contains a parameter with an
29711 unparsed DEFERRED_PARSE. Parse the default args now. This function
29712 assumes that the current scope is the scope in which the default
29713 argument should be processed. */
29714
29715 static void
29716 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
29717 {
29718 unsigned char saved_local_variables_forbidden_p;
29719 tree parm, parmdecl;
29720
29721 /* While we're parsing the default args, we might (due to the
29722 statement expression extension) encounter more classes. We want
29723 to handle them right away, but we don't want them getting mixed
29724 up with default args that are currently in the queue. */
29725 push_unparsed_function_queues (parser);
29726
29727 /* Local variable names (and the `this' keyword) may not appear
29728 in a default argument. */
29729 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
29730 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
29731
29732 push_defarg_context (fn);
29733
29734 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
29735 parmdecl = DECL_ARGUMENTS (fn);
29736 parm && parm != void_list_node;
29737 parm = TREE_CHAIN (parm),
29738 parmdecl = DECL_CHAIN (parmdecl))
29739 {
29740 tree default_arg = TREE_PURPOSE (parm);
29741 tree parsed_arg;
29742 vec<tree, va_gc> *insts;
29743 tree copy;
29744 unsigned ix;
29745
29746 if (!default_arg)
29747 continue;
29748
29749 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
29750 /* This can happen for a friend declaration for a function
29751 already declared with default arguments. */
29752 continue;
29753
29754 parsed_arg
29755 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
29756 default_arg,
29757 TREE_VALUE (parm));
29758 TREE_PURPOSE (parm) = parsed_arg;
29759
29760 /* Update any instantiations we've already created. */
29761 for (insts = DEFPARSE_INSTANTIATIONS (default_arg), ix = 0;
29762 vec_safe_iterate (insts, ix, &copy); ix++)
29763 TREE_PURPOSE (copy) = parsed_arg;
29764 }
29765
29766 pop_defarg_context ();
29767
29768 /* Make sure no default arg is missing. */
29769 check_default_args (fn);
29770
29771 /* Restore the state of local_variables_forbidden_p. */
29772 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
29773
29774 /* Restore the queue. */
29775 pop_unparsed_function_queues (parser);
29776 }
29777
29778 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
29779
29780 sizeof ... ( identifier )
29781
29782 where the 'sizeof' token has already been consumed. */
29783
29784 static tree
29785 cp_parser_sizeof_pack (cp_parser *parser)
29786 {
29787 /* Consume the `...'. */
29788 cp_lexer_consume_token (parser->lexer);
29789 maybe_warn_variadic_templates ();
29790
29791 matching_parens parens;
29792 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
29793 if (paren)
29794 parens.consume_open (parser);
29795 else
29796 permerror (cp_lexer_peek_token (parser->lexer)->location,
29797 "%<sizeof...%> argument must be surrounded by parentheses");
29798
29799 cp_token *token = cp_lexer_peek_token (parser->lexer);
29800 tree name = cp_parser_identifier (parser);
29801 if (name == error_mark_node)
29802 return error_mark_node;
29803 /* The name is not qualified. */
29804 parser->scope = NULL_TREE;
29805 parser->qualifying_scope = NULL_TREE;
29806 parser->object_scope = NULL_TREE;
29807 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
29808 if (expr == error_mark_node)
29809 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
29810 token->location);
29811 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
29812 expr = TREE_TYPE (expr);
29813 else if (TREE_CODE (expr) == CONST_DECL)
29814 expr = DECL_INITIAL (expr);
29815 expr = make_pack_expansion (expr);
29816 PACK_EXPANSION_SIZEOF_P (expr) = true;
29817
29818 if (paren)
29819 parens.require_close (parser);
29820
29821 return expr;
29822 }
29823
29824 /* Parse the operand of `sizeof' (or a similar operator). Returns
29825 either a TYPE or an expression, depending on the form of the
29826 input. The KEYWORD indicates which kind of expression we have
29827 encountered. */
29828
29829 static tree
29830 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
29831 {
29832 tree expr = NULL_TREE;
29833 const char *saved_message;
29834 const char *saved_message_arg;
29835 bool saved_integral_constant_expression_p;
29836 bool saved_non_integral_constant_expression_p;
29837
29838 /* If it's a `...', then we are computing the length of a parameter
29839 pack. */
29840 if (keyword == RID_SIZEOF
29841 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29842 return cp_parser_sizeof_pack (parser);
29843
29844 /* Types cannot be defined in a `sizeof' expression. Save away the
29845 old message. */
29846 saved_message = parser->type_definition_forbidden_message;
29847 saved_message_arg = parser->type_definition_forbidden_message_arg;
29848 parser->type_definition_forbidden_message
29849 = G_("types may not be defined in %qs expressions");
29850 parser->type_definition_forbidden_message_arg
29851 = IDENTIFIER_POINTER (ridpointers[keyword]);
29852
29853 /* The restrictions on constant-expressions do not apply inside
29854 sizeof expressions. */
29855 saved_integral_constant_expression_p
29856 = parser->integral_constant_expression_p;
29857 saved_non_integral_constant_expression_p
29858 = parser->non_integral_constant_expression_p;
29859 parser->integral_constant_expression_p = false;
29860
29861 /* Do not actually evaluate the expression. */
29862 ++cp_unevaluated_operand;
29863 ++c_inhibit_evaluation_warnings;
29864 /* If it's a `(', then we might be looking at the type-id
29865 construction. */
29866 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29867 {
29868 tree type = NULL_TREE;
29869
29870 tentative_firewall firewall (parser);
29871
29872 /* We can't be sure yet whether we're looking at a type-id or an
29873 expression. */
29874 cp_parser_parse_tentatively (parser);
29875
29876 matching_parens parens;
29877 parens.consume_open (parser);
29878
29879 /* Note: as a GNU Extension, compound literals are considered
29880 postfix-expressions as they are in C99, so they are valid
29881 arguments to sizeof. See comment in cp_parser_cast_expression
29882 for details. */
29883 if (cp_parser_compound_literal_p (parser))
29884 cp_parser_simulate_error (parser);
29885 else
29886 {
29887 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
29888 parser->in_type_id_in_expr_p = true;
29889 /* Look for the type-id. */
29890 type = cp_parser_type_id (parser);
29891 /* Look for the closing `)'. */
29892 parens.require_close (parser);
29893 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
29894 }
29895
29896 /* If all went well, then we're done. */
29897 if (cp_parser_parse_definitely (parser))
29898 expr = type;
29899 else
29900 {
29901 /* Commit to the tentative_firewall so we get syntax errors. */
29902 cp_parser_commit_to_tentative_parse (parser);
29903
29904 expr = cp_parser_unary_expression (parser);
29905 }
29906 }
29907 else
29908 expr = cp_parser_unary_expression (parser);
29909
29910 /* Go back to evaluating expressions. */
29911 --cp_unevaluated_operand;
29912 --c_inhibit_evaluation_warnings;
29913
29914 /* And restore the old one. */
29915 parser->type_definition_forbidden_message = saved_message;
29916 parser->type_definition_forbidden_message_arg = saved_message_arg;
29917 parser->integral_constant_expression_p
29918 = saved_integral_constant_expression_p;
29919 parser->non_integral_constant_expression_p
29920 = saved_non_integral_constant_expression_p;
29921
29922 return expr;
29923 }
29924
29925 /* If the current declaration has no declarator, return true. */
29926
29927 static bool
29928 cp_parser_declares_only_class_p (cp_parser *parser)
29929 {
29930 /* If the next token is a `;' or a `,' then there is no
29931 declarator. */
29932 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29933 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
29934 }
29935
29936 /* Update the DECL_SPECS to reflect the storage class indicated by
29937 KEYWORD. */
29938
29939 static void
29940 cp_parser_set_storage_class (cp_parser *parser,
29941 cp_decl_specifier_seq *decl_specs,
29942 enum rid keyword,
29943 cp_token *token)
29944 {
29945 cp_storage_class storage_class;
29946
29947 if (parser->in_unbraced_linkage_specification_p)
29948 {
29949 error_at (token->location, "invalid use of %qD in linkage specification",
29950 ridpointers[keyword]);
29951 return;
29952 }
29953 else if (decl_specs->storage_class != sc_none)
29954 {
29955 decl_specs->conflicting_specifiers_p = true;
29956 return;
29957 }
29958
29959 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
29960 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
29961 && decl_specs->gnu_thread_keyword_p)
29962 {
29963 pedwarn (decl_specs->locations[ds_thread], 0,
29964 "%<__thread%> before %qD", ridpointers[keyword]);
29965 }
29966
29967 switch (keyword)
29968 {
29969 case RID_AUTO:
29970 storage_class = sc_auto;
29971 break;
29972 case RID_REGISTER:
29973 storage_class = sc_register;
29974 break;
29975 case RID_STATIC:
29976 storage_class = sc_static;
29977 break;
29978 case RID_EXTERN:
29979 storage_class = sc_extern;
29980 break;
29981 case RID_MUTABLE:
29982 storage_class = sc_mutable;
29983 break;
29984 default:
29985 gcc_unreachable ();
29986 }
29987 decl_specs->storage_class = storage_class;
29988 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
29989
29990 /* A storage class specifier cannot be applied alongside a typedef
29991 specifier. If there is a typedef specifier present then set
29992 conflicting_specifiers_p which will trigger an error later
29993 on in grokdeclarator. */
29994 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
29995 decl_specs->conflicting_specifiers_p = true;
29996 }
29997
29998 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
29999 is true, the type is a class or enum definition. */
30000
30001 static void
30002 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
30003 tree type_spec,
30004 cp_token *token,
30005 bool type_definition_p)
30006 {
30007 decl_specs->any_specifiers_p = true;
30008
30009 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
30010 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
30011 this is what happened. In system headers, we ignore these
30012 declarations so that G++ can work with system headers that are not
30013 C++-safe. */
30014 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
30015 && !type_definition_p
30016 && (type_spec == boolean_type_node
30017 || type_spec == char8_type_node
30018 || type_spec == char16_type_node
30019 || type_spec == char32_type_node
30020 || type_spec == wchar_type_node)
30021 && (decl_specs->type
30022 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
30023 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
30024 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
30025 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
30026 {
30027 decl_specs->redefined_builtin_type = type_spec;
30028 set_and_check_decl_spec_loc (decl_specs,
30029 ds_redefined_builtin_type_spec,
30030 token);
30031 if (!decl_specs->type)
30032 {
30033 decl_specs->type = type_spec;
30034 decl_specs->type_definition_p = false;
30035 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
30036 }
30037 }
30038 else if (decl_specs->type)
30039 decl_specs->multiple_types_p = true;
30040 else
30041 {
30042 decl_specs->type = type_spec;
30043 decl_specs->type_definition_p = type_definition_p;
30044 decl_specs->redefined_builtin_type = NULL_TREE;
30045 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
30046 }
30047 }
30048
30049 /* True iff TOKEN is the GNU keyword __thread. */
30050
30051 static bool
30052 token_is__thread (cp_token *token)
30053 {
30054 gcc_assert (token->keyword == RID_THREAD);
30055 return id_equal (token->u.value, "__thread");
30056 }
30057
30058 /* Set the location for a declarator specifier and check if it is
30059 duplicated.
30060
30061 DECL_SPECS is the sequence of declarator specifiers onto which to
30062 set the location.
30063
30064 DS is the single declarator specifier to set which location is to
30065 be set onto the existing sequence of declarators.
30066
30067 LOCATION is the location for the declarator specifier to
30068 consider. */
30069
30070 static void
30071 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
30072 cp_decl_spec ds, cp_token *token)
30073 {
30074 gcc_assert (ds < ds_last);
30075
30076 if (decl_specs == NULL)
30077 return;
30078
30079 location_t location = token->location;
30080
30081 if (decl_specs->locations[ds] == 0)
30082 {
30083 decl_specs->locations[ds] = location;
30084 if (ds == ds_thread)
30085 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
30086 }
30087 else
30088 {
30089 if (ds == ds_long)
30090 {
30091 if (decl_specs->locations[ds_long_long] != 0)
30092 error_at (location,
30093 "%<long long long%> is too long for GCC");
30094 else
30095 {
30096 decl_specs->locations[ds_long_long] = location;
30097 pedwarn_cxx98 (location,
30098 OPT_Wlong_long,
30099 "ISO C++ 1998 does not support %<long long%>");
30100 }
30101 }
30102 else if (ds == ds_thread)
30103 {
30104 bool gnu = token_is__thread (token);
30105 gcc_rich_location richloc (location);
30106 if (gnu != decl_specs->gnu_thread_keyword_p)
30107 {
30108 richloc.add_range (decl_specs->locations[ds_thread]);
30109 error_at (&richloc,
30110 "both %<__thread%> and %<thread_local%> specified");
30111 }
30112 else
30113 {
30114 richloc.add_fixit_remove ();
30115 error_at (&richloc, "duplicate %qD", token->u.value);
30116 }
30117 }
30118 else
30119 {
30120 static const char *const decl_spec_names[] = {
30121 "signed",
30122 "unsigned",
30123 "short",
30124 "long",
30125 "const",
30126 "volatile",
30127 "restrict",
30128 "inline",
30129 "virtual",
30130 "explicit",
30131 "friend",
30132 "typedef",
30133 "using",
30134 "constexpr",
30135 "__complex",
30136 "constinit",
30137 "consteval"
30138 };
30139 gcc_rich_location richloc (location);
30140 richloc.add_fixit_remove ();
30141 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
30142 }
30143 }
30144 }
30145
30146 /* Return true iff the declarator specifier DS is present in the
30147 sequence of declarator specifiers DECL_SPECS. */
30148
30149 bool
30150 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
30151 cp_decl_spec ds)
30152 {
30153 gcc_assert (ds < ds_last);
30154
30155 if (decl_specs == NULL)
30156 return false;
30157
30158 return decl_specs->locations[ds] != 0;
30159 }
30160
30161 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
30162 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
30163
30164 static bool
30165 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
30166 {
30167 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
30168 }
30169
30170 /* Issue an error message indicating that TOKEN_DESC was expected.
30171 If KEYWORD is true, it indicated this function is called by
30172 cp_parser_require_keword and the required token can only be
30173 a indicated keyword.
30174
30175 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
30176 within any error as the location of an "opening" token matching
30177 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
30178 RT_CLOSE_PAREN). */
30179
30180 static void
30181 cp_parser_required_error (cp_parser *parser,
30182 required_token token_desc,
30183 bool keyword,
30184 location_t matching_location)
30185 {
30186 if (cp_parser_simulate_error (parser))
30187 return;
30188
30189 const char *gmsgid = NULL;
30190 switch (token_desc)
30191 {
30192 case RT_NEW:
30193 gmsgid = G_("expected %<new%>");
30194 break;
30195 case RT_DELETE:
30196 gmsgid = G_("expected %<delete%>");
30197 break;
30198 case RT_RETURN:
30199 gmsgid = G_("expected %<return%>");
30200 break;
30201 case RT_WHILE:
30202 gmsgid = G_("expected %<while%>");
30203 break;
30204 case RT_EXTERN:
30205 gmsgid = G_("expected %<extern%>");
30206 break;
30207 case RT_STATIC_ASSERT:
30208 gmsgid = G_("expected %<static_assert%>");
30209 break;
30210 case RT_DECLTYPE:
30211 gmsgid = G_("expected %<decltype%>");
30212 break;
30213 case RT_OPERATOR:
30214 gmsgid = G_("expected %<operator%>");
30215 break;
30216 case RT_CLASS:
30217 gmsgid = G_("expected %<class%>");
30218 break;
30219 case RT_TEMPLATE:
30220 gmsgid = G_("expected %<template%>");
30221 break;
30222 case RT_NAMESPACE:
30223 gmsgid = G_("expected %<namespace%>");
30224 break;
30225 case RT_USING:
30226 gmsgid = G_("expected %<using%>");
30227 break;
30228 case RT_ASM:
30229 gmsgid = G_("expected %<asm%>");
30230 break;
30231 case RT_TRY:
30232 gmsgid = G_("expected %<try%>");
30233 break;
30234 case RT_CATCH:
30235 gmsgid = G_("expected %<catch%>");
30236 break;
30237 case RT_THROW:
30238 gmsgid = G_("expected %<throw%>");
30239 break;
30240 case RT_AUTO:
30241 gmsgid = G_("expected %<auto%>");
30242 break;
30243 case RT_LABEL:
30244 gmsgid = G_("expected %<__label__%>");
30245 break;
30246 case RT_AT_TRY:
30247 gmsgid = G_("expected %<@try%>");
30248 break;
30249 case RT_AT_SYNCHRONIZED:
30250 gmsgid = G_("expected %<@synchronized%>");
30251 break;
30252 case RT_AT_THROW:
30253 gmsgid = G_("expected %<@throw%>");
30254 break;
30255 case RT_TRANSACTION_ATOMIC:
30256 gmsgid = G_("expected %<__transaction_atomic%>");
30257 break;
30258 case RT_TRANSACTION_RELAXED:
30259 gmsgid = G_("expected %<__transaction_relaxed%>");
30260 break;
30261 default:
30262 break;
30263 }
30264
30265 if (!gmsgid && !keyword)
30266 {
30267 switch (token_desc)
30268 {
30269 case RT_SEMICOLON:
30270 gmsgid = G_("expected %<;%>");
30271 break;
30272 case RT_OPEN_PAREN:
30273 gmsgid = G_("expected %<(%>");
30274 break;
30275 case RT_CLOSE_BRACE:
30276 gmsgid = G_("expected %<}%>");
30277 break;
30278 case RT_OPEN_BRACE:
30279 gmsgid = G_("expected %<{%>");
30280 break;
30281 case RT_CLOSE_SQUARE:
30282 gmsgid = G_("expected %<]%>");
30283 break;
30284 case RT_OPEN_SQUARE:
30285 gmsgid = G_("expected %<[%>");
30286 break;
30287 case RT_COMMA:
30288 gmsgid = G_("expected %<,%>");
30289 break;
30290 case RT_SCOPE:
30291 gmsgid = G_("expected %<::%>");
30292 break;
30293 case RT_LESS:
30294 gmsgid = G_("expected %<<%>");
30295 break;
30296 case RT_GREATER:
30297 gmsgid = G_("expected %<>%>");
30298 break;
30299 case RT_EQ:
30300 gmsgid = G_("expected %<=%>");
30301 break;
30302 case RT_ELLIPSIS:
30303 gmsgid = G_("expected %<...%>");
30304 break;
30305 case RT_MULT:
30306 gmsgid = G_("expected %<*%>");
30307 break;
30308 case RT_COMPL:
30309 gmsgid = G_("expected %<~%>");
30310 break;
30311 case RT_COLON:
30312 gmsgid = G_("expected %<:%>");
30313 break;
30314 case RT_COLON_SCOPE:
30315 gmsgid = G_("expected %<:%> or %<::%>");
30316 break;
30317 case RT_CLOSE_PAREN:
30318 gmsgid = G_("expected %<)%>");
30319 break;
30320 case RT_COMMA_CLOSE_PAREN:
30321 gmsgid = G_("expected %<,%> or %<)%>");
30322 break;
30323 case RT_PRAGMA_EOL:
30324 gmsgid = G_("expected end of line");
30325 break;
30326 case RT_NAME:
30327 gmsgid = G_("expected identifier");
30328 break;
30329 case RT_SELECT:
30330 gmsgid = G_("expected selection-statement");
30331 break;
30332 case RT_ITERATION:
30333 gmsgid = G_("expected iteration-statement");
30334 break;
30335 case RT_JUMP:
30336 gmsgid = G_("expected jump-statement");
30337 break;
30338 case RT_CLASS_KEY:
30339 gmsgid = G_("expected class-key");
30340 break;
30341 case RT_CLASS_TYPENAME_TEMPLATE:
30342 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
30343 break;
30344 default:
30345 gcc_unreachable ();
30346 }
30347 }
30348
30349 if (gmsgid)
30350 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
30351 }
30352
30353
30354 /* If the next token is of the indicated TYPE, consume it. Otherwise,
30355 issue an error message indicating that TOKEN_DESC was expected.
30356
30357 Returns the token consumed, if the token had the appropriate type.
30358 Otherwise, returns NULL.
30359
30360 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
30361 within any error as the location of an "opening" token matching
30362 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
30363 RT_CLOSE_PAREN). */
30364
30365 static cp_token *
30366 cp_parser_require (cp_parser* parser,
30367 enum cpp_ttype type,
30368 required_token token_desc,
30369 location_t matching_location)
30370 {
30371 if (cp_lexer_next_token_is (parser->lexer, type))
30372 return cp_lexer_consume_token (parser->lexer);
30373 else
30374 {
30375 /* Output the MESSAGE -- unless we're parsing tentatively. */
30376 if (!cp_parser_simulate_error (parser))
30377 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
30378 matching_location);
30379 return NULL;
30380 }
30381 }
30382
30383 /* An error message is produced if the next token is not '>'.
30384 All further tokens are skipped until the desired token is
30385 found or '{', '}', ';' or an unbalanced ')' or ']'. */
30386
30387 static void
30388 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
30389 {
30390 /* Current level of '< ... >'. */
30391 unsigned level = 0;
30392 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
30393 unsigned nesting_depth = 0;
30394
30395 /* Are we ready, yet? If not, issue error message. */
30396 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
30397 return;
30398
30399 /* Skip tokens until the desired token is found. */
30400 while (true)
30401 {
30402 /* Peek at the next token. */
30403 switch (cp_lexer_peek_token (parser->lexer)->type)
30404 {
30405 case CPP_LESS:
30406 if (!nesting_depth)
30407 ++level;
30408 break;
30409
30410 case CPP_RSHIFT:
30411 if (cxx_dialect == cxx98)
30412 /* C++0x views the `>>' operator as two `>' tokens, but
30413 C++98 does not. */
30414 break;
30415 else if (!nesting_depth && level-- == 0)
30416 {
30417 /* We've hit a `>>' where the first `>' closes the
30418 template argument list, and the second `>' is
30419 spurious. Just consume the `>>' and stop; we've
30420 already produced at least one error. */
30421 cp_lexer_consume_token (parser->lexer);
30422 return;
30423 }
30424 /* Fall through for C++0x, so we handle the second `>' in
30425 the `>>'. */
30426 gcc_fallthrough ();
30427
30428 case CPP_GREATER:
30429 if (!nesting_depth && level-- == 0)
30430 {
30431 /* We've reached the token we want, consume it and stop. */
30432 cp_lexer_consume_token (parser->lexer);
30433 return;
30434 }
30435 break;
30436
30437 case CPP_OPEN_PAREN:
30438 case CPP_OPEN_SQUARE:
30439 ++nesting_depth;
30440 break;
30441
30442 case CPP_CLOSE_PAREN:
30443 case CPP_CLOSE_SQUARE:
30444 if (nesting_depth-- == 0)
30445 return;
30446 break;
30447
30448 case CPP_EOF:
30449 case CPP_PRAGMA_EOL:
30450 case CPP_SEMICOLON:
30451 case CPP_OPEN_BRACE:
30452 case CPP_CLOSE_BRACE:
30453 /* The '>' was probably forgotten, don't look further. */
30454 return;
30455
30456 default:
30457 break;
30458 }
30459
30460 /* Consume this token. */
30461 cp_lexer_consume_token (parser->lexer);
30462 }
30463 }
30464
30465 /* If the next token is the indicated keyword, consume it. Otherwise,
30466 issue an error message indicating that TOKEN_DESC was expected.
30467
30468 Returns the token consumed, if the token had the appropriate type.
30469 Otherwise, returns NULL. */
30470
30471 static cp_token *
30472 cp_parser_require_keyword (cp_parser* parser,
30473 enum rid keyword,
30474 required_token token_desc)
30475 {
30476 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
30477
30478 if (token && token->keyword != keyword)
30479 {
30480 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
30481 UNKNOWN_LOCATION);
30482 return NULL;
30483 }
30484
30485 return token;
30486 }
30487
30488 /* Returns TRUE iff TOKEN is a token that can begin the body of a
30489 function-definition. */
30490
30491 static bool
30492 cp_parser_token_starts_function_definition_p (cp_token* token)
30493 {
30494 return (/* An ordinary function-body begins with an `{'. */
30495 token->type == CPP_OPEN_BRACE
30496 /* A ctor-initializer begins with a `:'. */
30497 || token->type == CPP_COLON
30498 /* A function-try-block begins with `try'. */
30499 || token->keyword == RID_TRY
30500 /* A function-transaction-block begins with `__transaction_atomic'
30501 or `__transaction_relaxed'. */
30502 || token->keyword == RID_TRANSACTION_ATOMIC
30503 || token->keyword == RID_TRANSACTION_RELAXED
30504 /* The named return value extension begins with `return'. */
30505 || token->keyword == RID_RETURN);
30506 }
30507
30508 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
30509 definition. */
30510
30511 static bool
30512 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
30513 {
30514 cp_token *token;
30515
30516 token = cp_lexer_peek_token (parser->lexer);
30517 return (token->type == CPP_OPEN_BRACE
30518 || (token->type == CPP_COLON
30519 && !parser->colon_doesnt_start_class_def_p));
30520 }
30521
30522 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
30523 C++0x) ending a template-argument. */
30524
30525 static bool
30526 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
30527 {
30528 cp_token *token;
30529
30530 token = cp_lexer_peek_token (parser->lexer);
30531 return (token->type == CPP_COMMA
30532 || token->type == CPP_GREATER
30533 || token->type == CPP_ELLIPSIS
30534 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
30535 }
30536
30537 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
30538 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
30539
30540 static bool
30541 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
30542 size_t n)
30543 {
30544 cp_token *token;
30545
30546 token = cp_lexer_peek_nth_token (parser->lexer, n);
30547 if (token->type == CPP_LESS)
30548 return true;
30549 /* Check for the sequence `<::' in the original code. It would be lexed as
30550 `[:', where `[' is a digraph, and there is no whitespace before
30551 `:'. */
30552 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
30553 {
30554 cp_token *token2;
30555 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
30556 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
30557 return true;
30558 }
30559 return false;
30560 }
30561
30562 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
30563 or none_type otherwise. */
30564
30565 static enum tag_types
30566 cp_parser_token_is_class_key (cp_token* token)
30567 {
30568 switch (token->keyword)
30569 {
30570 case RID_CLASS:
30571 return class_type;
30572 case RID_STRUCT:
30573 return record_type;
30574 case RID_UNION:
30575 return union_type;
30576
30577 default:
30578 return none_type;
30579 }
30580 }
30581
30582 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
30583 or none_type otherwise or if the token is null. */
30584
30585 static enum tag_types
30586 cp_parser_token_is_type_parameter_key (cp_token* token)
30587 {
30588 if (!token)
30589 return none_type;
30590
30591 switch (token->keyword)
30592 {
30593 case RID_CLASS:
30594 return class_type;
30595 case RID_TYPENAME:
30596 return typename_type;
30597
30598 default:
30599 return none_type;
30600 }
30601 }
30602
30603 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
30604
30605 static void
30606 cp_parser_check_class_key (enum tag_types class_key, tree type)
30607 {
30608 if (type == error_mark_node)
30609 return;
30610 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
30611 {
30612 if (permerror (input_location, "%qs tag used in naming %q#T",
30613 class_key == union_type ? "union"
30614 : class_key == record_type ? "struct" : "class",
30615 type))
30616 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
30617 "%q#T was previously declared here", type);
30618 }
30619 }
30620
30621 /* Issue an error message if DECL is redeclared with different
30622 access than its original declaration [class.access.spec/3].
30623 This applies to nested classes, nested class templates and
30624 enumerations [class.mem/1]. */
30625
30626 static void
30627 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
30628 {
30629 if (!decl
30630 || (!CLASS_TYPE_P (TREE_TYPE (decl))
30631 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
30632 return;
30633
30634 if ((TREE_PRIVATE (decl)
30635 != (current_access_specifier == access_private_node))
30636 || (TREE_PROTECTED (decl)
30637 != (current_access_specifier == access_protected_node)))
30638 error_at (location, "%qD redeclared with different access", decl);
30639 }
30640
30641 /* Look for the `template' keyword, as a syntactic disambiguator.
30642 Return TRUE iff it is present, in which case it will be
30643 consumed. */
30644
30645 static bool
30646 cp_parser_optional_template_keyword (cp_parser *parser)
30647 {
30648 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
30649 {
30650 /* In C++98 the `template' keyword can only be used within templates;
30651 outside templates the parser can always figure out what is a
30652 template and what is not. In C++11, per the resolution of DR 468,
30653 `template' is allowed in cases where it is not strictly necessary. */
30654 if (!processing_template_decl
30655 && pedantic && cxx_dialect == cxx98)
30656 {
30657 cp_token *token = cp_lexer_peek_token (parser->lexer);
30658 pedwarn (token->location, OPT_Wpedantic,
30659 "in C++98 %<template%> (as a disambiguator) is only "
30660 "allowed within templates");
30661 /* If this part of the token stream is rescanned, the same
30662 error message would be generated. So, we purge the token
30663 from the stream. */
30664 cp_lexer_purge_token (parser->lexer);
30665 return false;
30666 }
30667 else
30668 {
30669 /* Consume the `template' keyword. */
30670 cp_lexer_consume_token (parser->lexer);
30671 return true;
30672 }
30673 }
30674 return false;
30675 }
30676
30677 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
30678 set PARSER->SCOPE, and perform other related actions. */
30679
30680 static void
30681 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
30682 {
30683 struct tree_check *check_value;
30684
30685 /* Get the stored value. */
30686 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
30687 /* Set the scope from the stored value. */
30688 parser->scope = saved_checks_value (check_value);
30689 parser->qualifying_scope = check_value->qualifying_scope;
30690 parser->object_scope = NULL_TREE;
30691 }
30692
30693 /* Consume tokens up through a non-nested END token. Returns TRUE if we
30694 encounter the end of a block before what we were looking for. */
30695
30696 static bool
30697 cp_parser_cache_group (cp_parser *parser,
30698 enum cpp_ttype end,
30699 unsigned depth)
30700 {
30701 while (true)
30702 {
30703 cp_token *token = cp_lexer_peek_token (parser->lexer);
30704
30705 /* Abort a parenthesized expression if we encounter a semicolon. */
30706 if ((end == CPP_CLOSE_PAREN || depth == 0)
30707 && token->type == CPP_SEMICOLON)
30708 return true;
30709 /* If we've reached the end of the file, stop. */
30710 if (token->type == CPP_EOF
30711 || (end != CPP_PRAGMA_EOL
30712 && token->type == CPP_PRAGMA_EOL))
30713 return true;
30714 if (token->type == CPP_CLOSE_BRACE && depth == 0)
30715 /* We've hit the end of an enclosing block, so there's been some
30716 kind of syntax error. */
30717 return true;
30718
30719 /* Consume the token. */
30720 cp_lexer_consume_token (parser->lexer);
30721 /* See if it starts a new group. */
30722 if (token->type == CPP_OPEN_BRACE)
30723 {
30724 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
30725 /* In theory this should probably check end == '}', but
30726 cp_parser_save_member_function_body needs it to exit
30727 after either '}' or ')' when called with ')'. */
30728 if (depth == 0)
30729 return false;
30730 }
30731 else if (token->type == CPP_OPEN_PAREN)
30732 {
30733 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
30734 if (depth == 0 && end == CPP_CLOSE_PAREN)
30735 return false;
30736 }
30737 else if (token->type == CPP_PRAGMA)
30738 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
30739 else if (token->type == end)
30740 return false;
30741 }
30742 }
30743
30744 /* Like above, for caching a default argument or NSDMI. Both of these are
30745 terminated by a non-nested comma, but it can be unclear whether or not a
30746 comma is nested in a template argument list unless we do more parsing.
30747 In order to handle this ambiguity, when we encounter a ',' after a '<'
30748 we try to parse what follows as a parameter-declaration-list (in the
30749 case of a default argument) or a member-declarator (in the case of an
30750 NSDMI). If that succeeds, then we stop caching. */
30751
30752 static tree
30753 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
30754 {
30755 unsigned depth = 0;
30756 int maybe_template_id = 0;
30757 cp_token *first_token;
30758 cp_token *token;
30759 tree default_argument;
30760
30761 /* Add tokens until we have processed the entire default
30762 argument. We add the range [first_token, token). */
30763 first_token = cp_lexer_peek_token (parser->lexer);
30764 if (first_token->type == CPP_OPEN_BRACE)
30765 {
30766 /* For list-initialization, this is straightforward. */
30767 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
30768 token = cp_lexer_peek_token (parser->lexer);
30769 }
30770 else while (true)
30771 {
30772 bool done = false;
30773
30774 /* Peek at the next token. */
30775 token = cp_lexer_peek_token (parser->lexer);
30776 /* What we do depends on what token we have. */
30777 switch (token->type)
30778 {
30779 /* In valid code, a default argument must be
30780 immediately followed by a `,' `)', or `...'. */
30781 case CPP_COMMA:
30782 if (depth == 0 && maybe_template_id)
30783 {
30784 /* If we've seen a '<', we might be in a
30785 template-argument-list. Until Core issue 325 is
30786 resolved, we don't know how this situation ought
30787 to be handled, so try to DTRT. We check whether
30788 what comes after the comma is a valid parameter
30789 declaration list. If it is, then the comma ends
30790 the default argument; otherwise the default
30791 argument continues. */
30792 bool error = false;
30793 cp_token *peek;
30794
30795 /* Set ITALP so cp_parser_parameter_declaration_list
30796 doesn't decide to commit to this parse. */
30797 bool saved_italp = parser->in_template_argument_list_p;
30798 parser->in_template_argument_list_p = true;
30799
30800 cp_parser_parse_tentatively (parser);
30801
30802 if (nsdmi)
30803 {
30804 /* Parse declarators until we reach a non-comma or
30805 somthing that cannot be an initializer.
30806 Just checking whether we're looking at a single
30807 declarator is insufficient. Consider:
30808 int var = tuple<T,U>::x;
30809 The template parameter 'U' looks exactly like a
30810 declarator. */
30811 do
30812 {
30813 int ctor_dtor_or_conv_p;
30814 cp_lexer_consume_token (parser->lexer);
30815 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30816 CP_PARSER_FLAGS_NONE,
30817 &ctor_dtor_or_conv_p,
30818 /*parenthesized_p=*/NULL,
30819 /*member_p=*/true,
30820 /*friend_p=*/false,
30821 /*static_p=*/false);
30822 peek = cp_lexer_peek_token (parser->lexer);
30823 if (cp_parser_error_occurred (parser))
30824 break;
30825 }
30826 while (peek->type == CPP_COMMA);
30827 /* If we met an '=' or ';' then the original comma
30828 was the end of the NSDMI. Otherwise assume
30829 we're still in the NSDMI. */
30830 error = (peek->type != CPP_EQ
30831 && peek->type != CPP_SEMICOLON);
30832 }
30833 else
30834 {
30835 cp_lexer_consume_token (parser->lexer);
30836 begin_scope (sk_function_parms, NULL_TREE);
30837 tree t = cp_parser_parameter_declaration_list
30838 (parser, CP_PARSER_FLAGS_NONE);
30839 if (t == error_mark_node)
30840 error = true;
30841 pop_bindings_and_leave_scope ();
30842 }
30843 if (!cp_parser_error_occurred (parser) && !error)
30844 done = true;
30845 cp_parser_abort_tentative_parse (parser);
30846
30847 parser->in_template_argument_list_p = saved_italp;
30848 break;
30849 }
30850 /* FALLTHRU */
30851 case CPP_CLOSE_PAREN:
30852 case CPP_ELLIPSIS:
30853 /* If we run into a non-nested `;', `}', or `]',
30854 then the code is invalid -- but the default
30855 argument is certainly over. */
30856 case CPP_SEMICOLON:
30857 case CPP_CLOSE_BRACE:
30858 case CPP_CLOSE_SQUARE:
30859 if (depth == 0
30860 /* Handle correctly int n = sizeof ... ( p ); */
30861 && token->type != CPP_ELLIPSIS)
30862 done = true;
30863 /* Update DEPTH, if necessary. */
30864 else if (token->type == CPP_CLOSE_PAREN
30865 || token->type == CPP_CLOSE_BRACE
30866 || token->type == CPP_CLOSE_SQUARE)
30867 --depth;
30868 break;
30869
30870 case CPP_OPEN_PAREN:
30871 case CPP_OPEN_SQUARE:
30872 case CPP_OPEN_BRACE:
30873 ++depth;
30874 break;
30875
30876 case CPP_LESS:
30877 if (depth == 0)
30878 /* This might be the comparison operator, or it might
30879 start a template argument list. */
30880 ++maybe_template_id;
30881 break;
30882
30883 case CPP_RSHIFT:
30884 if (cxx_dialect == cxx98)
30885 break;
30886 /* Fall through for C++0x, which treats the `>>'
30887 operator like two `>' tokens in certain
30888 cases. */
30889 gcc_fallthrough ();
30890
30891 case CPP_GREATER:
30892 if (depth == 0)
30893 {
30894 /* This might be an operator, or it might close a
30895 template argument list. But if a previous '<'
30896 started a template argument list, this will have
30897 closed it, so we can't be in one anymore. */
30898 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
30899 if (maybe_template_id < 0)
30900 maybe_template_id = 0;
30901 }
30902 break;
30903
30904 /* If we run out of tokens, issue an error message. */
30905 case CPP_EOF:
30906 case CPP_PRAGMA_EOL:
30907 error_at (token->location, "file ends in default argument");
30908 return error_mark_node;
30909
30910 case CPP_NAME:
30911 case CPP_SCOPE:
30912 /* In these cases, we should look for template-ids.
30913 For example, if the default argument is
30914 `X<int, double>()', we need to do name lookup to
30915 figure out whether or not `X' is a template; if
30916 so, the `,' does not end the default argument.
30917
30918 That is not yet done. */
30919 break;
30920
30921 default:
30922 break;
30923 }
30924
30925 /* If we've reached the end, stop. */
30926 if (done)
30927 break;
30928
30929 /* Add the token to the token block. */
30930 token = cp_lexer_consume_token (parser->lexer);
30931 }
30932
30933 /* Create a DEFERRED_PARSE to represent the unparsed default
30934 argument. */
30935 default_argument = make_node (DEFERRED_PARSE);
30936 DEFPARSE_TOKENS (default_argument)
30937 = cp_token_cache_new (first_token, token);
30938 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
30939
30940 return default_argument;
30941 }
30942
30943 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
30944
30945 location_t
30946 defparse_location (tree default_argument)
30947 {
30948 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
30949 location_t start = tokens->first->location;
30950 location_t end = tokens->last->location;
30951 return make_location (start, start, end);
30952 }
30953
30954 /* Begin parsing tentatively. We always save tokens while parsing
30955 tentatively so that if the tentative parsing fails we can restore the
30956 tokens. */
30957
30958 static void
30959 cp_parser_parse_tentatively (cp_parser* parser)
30960 {
30961 /* Enter a new parsing context. */
30962 parser->context = cp_parser_context_new (parser->context);
30963 /* Begin saving tokens. */
30964 cp_lexer_save_tokens (parser->lexer);
30965 /* In order to avoid repetitive access control error messages,
30966 access checks are queued up until we are no longer parsing
30967 tentatively. */
30968 push_deferring_access_checks (dk_deferred);
30969 }
30970
30971 /* Commit to the currently active tentative parse. */
30972
30973 static void
30974 cp_parser_commit_to_tentative_parse (cp_parser* parser)
30975 {
30976 cp_parser_context *context;
30977 cp_lexer *lexer;
30978
30979 /* Mark all of the levels as committed. */
30980 lexer = parser->lexer;
30981 for (context = parser->context; context->next; context = context->next)
30982 {
30983 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
30984 break;
30985 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
30986 while (!cp_lexer_saving_tokens (lexer))
30987 lexer = lexer->next;
30988 cp_lexer_commit_tokens (lexer);
30989 }
30990 }
30991
30992 /* Commit to the topmost currently active tentative parse.
30993
30994 Note that this function shouldn't be called when there are
30995 irreversible side-effects while in a tentative state. For
30996 example, we shouldn't create a permanent entry in the symbol
30997 table, or issue an error message that might not apply if the
30998 tentative parse is aborted. */
30999
31000 static void
31001 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
31002 {
31003 cp_parser_context *context = parser->context;
31004 cp_lexer *lexer = parser->lexer;
31005
31006 if (context)
31007 {
31008 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
31009 return;
31010 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
31011
31012 while (!cp_lexer_saving_tokens (lexer))
31013 lexer = lexer->next;
31014 cp_lexer_commit_tokens (lexer);
31015 }
31016 }
31017
31018 /* Abort the currently active tentative parse. All consumed tokens
31019 will be rolled back, and no diagnostics will be issued. */
31020
31021 static void
31022 cp_parser_abort_tentative_parse (cp_parser* parser)
31023 {
31024 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
31025 || errorcount > 0);
31026 cp_parser_simulate_error (parser);
31027 /* Now, pretend that we want to see if the construct was
31028 successfully parsed. */
31029 cp_parser_parse_definitely (parser);
31030 }
31031
31032 /* Stop parsing tentatively. If a parse error has occurred, restore the
31033 token stream. Otherwise, commit to the tokens we have consumed.
31034 Returns true if no error occurred; false otherwise. */
31035
31036 static bool
31037 cp_parser_parse_definitely (cp_parser* parser)
31038 {
31039 bool error_occurred;
31040 cp_parser_context *context;
31041
31042 /* Remember whether or not an error occurred, since we are about to
31043 destroy that information. */
31044 error_occurred = cp_parser_error_occurred (parser);
31045 /* Remove the topmost context from the stack. */
31046 context = parser->context;
31047 parser->context = context->next;
31048 /* If no parse errors occurred, commit to the tentative parse. */
31049 if (!error_occurred)
31050 {
31051 /* Commit to the tokens read tentatively, unless that was
31052 already done. */
31053 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
31054 cp_lexer_commit_tokens (parser->lexer);
31055
31056 pop_to_parent_deferring_access_checks ();
31057 }
31058 /* Otherwise, if errors occurred, roll back our state so that things
31059 are just as they were before we began the tentative parse. */
31060 else
31061 {
31062 cp_lexer_rollback_tokens (parser->lexer);
31063 pop_deferring_access_checks ();
31064 }
31065 /* Add the context to the front of the free list. */
31066 context->next = cp_parser_context_free_list;
31067 cp_parser_context_free_list = context;
31068
31069 return !error_occurred;
31070 }
31071
31072 /* Returns true if we are parsing tentatively and are not committed to
31073 this tentative parse. */
31074
31075 static bool
31076 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
31077 {
31078 return (cp_parser_parsing_tentatively (parser)
31079 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
31080 }
31081
31082 /* Returns nonzero iff an error has occurred during the most recent
31083 tentative parse. */
31084
31085 static bool
31086 cp_parser_error_occurred (cp_parser* parser)
31087 {
31088 return (cp_parser_parsing_tentatively (parser)
31089 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
31090 }
31091
31092 /* Returns nonzero if GNU extensions are allowed. */
31093
31094 static bool
31095 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
31096 {
31097 return parser->allow_gnu_extensions_p;
31098 }
31099 \f
31100 /* Objective-C++ Productions */
31101
31102
31103 /* Parse an Objective-C expression, which feeds into a primary-expression
31104 above.
31105
31106 objc-expression:
31107 objc-message-expression
31108 objc-string-literal
31109 objc-encode-expression
31110 objc-protocol-expression
31111 objc-selector-expression
31112
31113 Returns a tree representation of the expression. */
31114
31115 static cp_expr
31116 cp_parser_objc_expression (cp_parser* parser)
31117 {
31118 /* Try to figure out what kind of declaration is present. */
31119 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31120
31121 switch (kwd->type)
31122 {
31123 case CPP_OPEN_SQUARE:
31124 return cp_parser_objc_message_expression (parser);
31125
31126 case CPP_OBJC_STRING:
31127 kwd = cp_lexer_consume_token (parser->lexer);
31128 return objc_build_string_object (kwd->u.value);
31129
31130 case CPP_KEYWORD:
31131 switch (kwd->keyword)
31132 {
31133 case RID_AT_ENCODE:
31134 return cp_parser_objc_encode_expression (parser);
31135
31136 case RID_AT_PROTOCOL:
31137 return cp_parser_objc_protocol_expression (parser);
31138
31139 case RID_AT_SELECTOR:
31140 return cp_parser_objc_selector_expression (parser);
31141
31142 default:
31143 break;
31144 }
31145 /* FALLTHRU */
31146 default:
31147 error_at (kwd->location,
31148 "misplaced %<@%D%> Objective-C++ construct",
31149 kwd->u.value);
31150 cp_parser_skip_to_end_of_block_or_statement (parser);
31151 }
31152
31153 return error_mark_node;
31154 }
31155
31156 /* Parse an Objective-C message expression.
31157
31158 objc-message-expression:
31159 [ objc-message-receiver objc-message-args ]
31160
31161 Returns a representation of an Objective-C message. */
31162
31163 static tree
31164 cp_parser_objc_message_expression (cp_parser* parser)
31165 {
31166 tree receiver, messageargs;
31167
31168 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
31169 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
31170 receiver = cp_parser_objc_message_receiver (parser);
31171 messageargs = cp_parser_objc_message_args (parser);
31172 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
31173 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31174
31175 tree result = objc_build_message_expr (receiver, messageargs);
31176
31177 /* Construct a location e.g.
31178 [self func1:5]
31179 ^~~~~~~~~~~~~~
31180 ranging from the '[' to the ']', with the caret at the start. */
31181 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
31182 protected_set_expr_location (result, combined_loc);
31183
31184 return result;
31185 }
31186
31187 /* Parse an objc-message-receiver.
31188
31189 objc-message-receiver:
31190 expression
31191 simple-type-specifier
31192
31193 Returns a representation of the type or expression. */
31194
31195 static tree
31196 cp_parser_objc_message_receiver (cp_parser* parser)
31197 {
31198 tree rcv;
31199
31200 /* An Objective-C message receiver may be either (1) a type
31201 or (2) an expression. */
31202 cp_parser_parse_tentatively (parser);
31203 rcv = cp_parser_expression (parser);
31204
31205 /* If that worked out, fine. */
31206 if (cp_parser_parse_definitely (parser))
31207 return rcv;
31208
31209 cp_parser_parse_tentatively (parser);
31210 rcv = cp_parser_simple_type_specifier (parser,
31211 /*decl_specs=*/NULL,
31212 CP_PARSER_FLAGS_NONE);
31213
31214 if (cp_parser_parse_definitely (parser))
31215 return objc_get_class_reference (rcv);
31216
31217 cp_parser_error (parser, "objective-c++ message receiver expected");
31218 return error_mark_node;
31219 }
31220
31221 /* Parse the arguments and selectors comprising an Objective-C message.
31222
31223 objc-message-args:
31224 objc-selector
31225 objc-selector-args
31226 objc-selector-args , objc-comma-args
31227
31228 objc-selector-args:
31229 objc-selector [opt] : assignment-expression
31230 objc-selector-args objc-selector [opt] : assignment-expression
31231
31232 objc-comma-args:
31233 assignment-expression
31234 objc-comma-args , assignment-expression
31235
31236 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
31237 selector arguments and TREE_VALUE containing a list of comma
31238 arguments. */
31239
31240 static tree
31241 cp_parser_objc_message_args (cp_parser* parser)
31242 {
31243 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
31244 bool maybe_unary_selector_p = true;
31245 cp_token *token = cp_lexer_peek_token (parser->lexer);
31246
31247 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
31248 {
31249 tree selector = NULL_TREE, arg;
31250
31251 if (token->type != CPP_COLON)
31252 selector = cp_parser_objc_selector (parser);
31253
31254 /* Detect if we have a unary selector. */
31255 if (maybe_unary_selector_p
31256 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31257 return build_tree_list (selector, NULL_TREE);
31258
31259 maybe_unary_selector_p = false;
31260 cp_parser_require (parser, CPP_COLON, RT_COLON);
31261 arg = cp_parser_assignment_expression (parser);
31262
31263 sel_args
31264 = chainon (sel_args,
31265 build_tree_list (selector, arg));
31266
31267 token = cp_lexer_peek_token (parser->lexer);
31268 }
31269
31270 /* Handle non-selector arguments, if any. */
31271 while (token->type == CPP_COMMA)
31272 {
31273 tree arg;
31274
31275 cp_lexer_consume_token (parser->lexer);
31276 arg = cp_parser_assignment_expression (parser);
31277
31278 addl_args
31279 = chainon (addl_args,
31280 build_tree_list (NULL_TREE, arg));
31281
31282 token = cp_lexer_peek_token (parser->lexer);
31283 }
31284
31285 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
31286 {
31287 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
31288 return build_tree_list (error_mark_node, error_mark_node);
31289 }
31290
31291 return build_tree_list (sel_args, addl_args);
31292 }
31293
31294 /* Parse an Objective-C encode expression.
31295
31296 objc-encode-expression:
31297 @encode objc-typename
31298
31299 Returns an encoded representation of the type argument. */
31300
31301 static cp_expr
31302 cp_parser_objc_encode_expression (cp_parser* parser)
31303 {
31304 tree type;
31305 cp_token *token;
31306 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
31307
31308 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
31309 matching_parens parens;
31310 parens.require_open (parser);
31311 token = cp_lexer_peek_token (parser->lexer);
31312 type = complete_type (cp_parser_type_id (parser));
31313 parens.require_close (parser);
31314
31315 if (!type)
31316 {
31317 error_at (token->location,
31318 "%<@encode%> must specify a type as an argument");
31319 return error_mark_node;
31320 }
31321
31322 /* This happens if we find @encode(T) (where T is a template
31323 typename or something dependent on a template typename) when
31324 parsing a template. In that case, we can't compile it
31325 immediately, but we rather create an AT_ENCODE_EXPR which will
31326 need to be instantiated when the template is used.
31327 */
31328 if (dependent_type_p (type))
31329 {
31330 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
31331 TREE_READONLY (value) = 1;
31332 return value;
31333 }
31334
31335
31336 /* Build a location of the form:
31337 @encode(int)
31338 ^~~~~~~~~~~~
31339 with caret==start at the @ token, finishing at the close paren. */
31340 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
31341
31342 return cp_expr (objc_build_encode_expr (type), combined_loc);
31343 }
31344
31345 /* Parse an Objective-C @defs expression. */
31346
31347 static tree
31348 cp_parser_objc_defs_expression (cp_parser *parser)
31349 {
31350 tree name;
31351
31352 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
31353 matching_parens parens;
31354 parens.require_open (parser);
31355 name = cp_parser_identifier (parser);
31356 parens.require_close (parser);
31357
31358 return objc_get_class_ivars (name);
31359 }
31360
31361 /* Parse an Objective-C protocol expression.
31362
31363 objc-protocol-expression:
31364 @protocol ( identifier )
31365
31366 Returns a representation of the protocol expression. */
31367
31368 static tree
31369 cp_parser_objc_protocol_expression (cp_parser* parser)
31370 {
31371 tree proto;
31372 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
31373
31374 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
31375 matching_parens parens;
31376 parens.require_open (parser);
31377 proto = cp_parser_identifier (parser);
31378 parens.require_close (parser);
31379
31380 /* Build a location of the form:
31381 @protocol(prot)
31382 ^~~~~~~~~~~~~~~
31383 with caret==start at the @ token, finishing at the close paren. */
31384 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
31385 tree result = objc_build_protocol_expr (proto);
31386 protected_set_expr_location (result, combined_loc);
31387 return result;
31388 }
31389
31390 /* Parse an Objective-C selector expression.
31391
31392 objc-selector-expression:
31393 @selector ( objc-method-signature )
31394
31395 objc-method-signature:
31396 objc-selector
31397 objc-selector-seq
31398
31399 objc-selector-seq:
31400 objc-selector :
31401 objc-selector-seq objc-selector :
31402
31403 Returns a representation of the method selector. */
31404
31405 static tree
31406 cp_parser_objc_selector_expression (cp_parser* parser)
31407 {
31408 tree sel_seq = NULL_TREE;
31409 bool maybe_unary_selector_p = true;
31410 cp_token *token;
31411 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31412
31413 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
31414 matching_parens parens;
31415 parens.require_open (parser);
31416 token = cp_lexer_peek_token (parser->lexer);
31417
31418 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
31419 || token->type == CPP_SCOPE)
31420 {
31421 tree selector = NULL_TREE;
31422
31423 if (token->type != CPP_COLON
31424 || token->type == CPP_SCOPE)
31425 selector = cp_parser_objc_selector (parser);
31426
31427 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
31428 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
31429 {
31430 /* Detect if we have a unary selector. */
31431 if (maybe_unary_selector_p)
31432 {
31433 sel_seq = selector;
31434 goto finish_selector;
31435 }
31436 else
31437 {
31438 cp_parser_error (parser, "expected %<:%>");
31439 }
31440 }
31441 maybe_unary_selector_p = false;
31442 token = cp_lexer_consume_token (parser->lexer);
31443
31444 if (token->type == CPP_SCOPE)
31445 {
31446 sel_seq
31447 = chainon (sel_seq,
31448 build_tree_list (selector, NULL_TREE));
31449 sel_seq
31450 = chainon (sel_seq,
31451 build_tree_list (NULL_TREE, NULL_TREE));
31452 }
31453 else
31454 sel_seq
31455 = chainon (sel_seq,
31456 build_tree_list (selector, NULL_TREE));
31457
31458 token = cp_lexer_peek_token (parser->lexer);
31459 }
31460
31461 finish_selector:
31462 parens.require_close (parser);
31463
31464
31465 /* Build a location of the form:
31466 @selector(func)
31467 ^~~~~~~~~~~~~~~
31468 with caret==start at the @ token, finishing at the close paren. */
31469 location_t combined_loc = make_location (loc, loc, parser->lexer);
31470 tree result = objc_build_selector_expr (combined_loc, sel_seq);
31471 /* TODO: objc_build_selector_expr doesn't always honor the location. */
31472 protected_set_expr_location (result, combined_loc);
31473 return result;
31474 }
31475
31476 /* Parse a list of identifiers.
31477
31478 objc-identifier-list:
31479 identifier
31480 objc-identifier-list , identifier
31481
31482 Returns a TREE_LIST of identifier nodes. */
31483
31484 static tree
31485 cp_parser_objc_identifier_list (cp_parser* parser)
31486 {
31487 tree identifier;
31488 tree list;
31489 cp_token *sep;
31490
31491 identifier = cp_parser_identifier (parser);
31492 if (identifier == error_mark_node)
31493 return error_mark_node;
31494
31495 list = build_tree_list (NULL_TREE, identifier);
31496 sep = cp_lexer_peek_token (parser->lexer);
31497
31498 while (sep->type == CPP_COMMA)
31499 {
31500 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31501 identifier = cp_parser_identifier (parser);
31502 if (identifier == error_mark_node)
31503 return list;
31504
31505 list = chainon (list, build_tree_list (NULL_TREE,
31506 identifier));
31507 sep = cp_lexer_peek_token (parser->lexer);
31508 }
31509
31510 return list;
31511 }
31512
31513 /* Parse an Objective-C alias declaration.
31514
31515 objc-alias-declaration:
31516 @compatibility_alias identifier identifier ;
31517
31518 This function registers the alias mapping with the Objective-C front end.
31519 It returns nothing. */
31520
31521 static void
31522 cp_parser_objc_alias_declaration (cp_parser* parser)
31523 {
31524 tree alias, orig;
31525
31526 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
31527 alias = cp_parser_identifier (parser);
31528 orig = cp_parser_identifier (parser);
31529 objc_declare_alias (alias, orig);
31530 cp_parser_consume_semicolon_at_end_of_statement (parser);
31531 }
31532
31533 /* Parse an Objective-C class forward-declaration.
31534
31535 objc-class-declaration:
31536 @class objc-identifier-list ;
31537
31538 The function registers the forward declarations with the Objective-C
31539 front end. It returns nothing. */
31540
31541 static void
31542 cp_parser_objc_class_declaration (cp_parser* parser)
31543 {
31544 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
31545 while (true)
31546 {
31547 tree id;
31548
31549 id = cp_parser_identifier (parser);
31550 if (id == error_mark_node)
31551 break;
31552
31553 objc_declare_class (id);
31554
31555 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31556 cp_lexer_consume_token (parser->lexer);
31557 else
31558 break;
31559 }
31560 cp_parser_consume_semicolon_at_end_of_statement (parser);
31561 }
31562
31563 /* Parse a list of Objective-C protocol references.
31564
31565 objc-protocol-refs-opt:
31566 objc-protocol-refs [opt]
31567
31568 objc-protocol-refs:
31569 < objc-identifier-list >
31570
31571 Returns a TREE_LIST of identifiers, if any. */
31572
31573 static tree
31574 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
31575 {
31576 tree protorefs = NULL_TREE;
31577
31578 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
31579 {
31580 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
31581 protorefs = cp_parser_objc_identifier_list (parser);
31582 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
31583 }
31584
31585 return protorefs;
31586 }
31587
31588 /* Parse a Objective-C visibility specification. */
31589
31590 static void
31591 cp_parser_objc_visibility_spec (cp_parser* parser)
31592 {
31593 cp_token *vis = cp_lexer_peek_token (parser->lexer);
31594
31595 switch (vis->keyword)
31596 {
31597 case RID_AT_PRIVATE:
31598 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
31599 break;
31600 case RID_AT_PROTECTED:
31601 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
31602 break;
31603 case RID_AT_PUBLIC:
31604 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
31605 break;
31606 case RID_AT_PACKAGE:
31607 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
31608 break;
31609 default:
31610 return;
31611 }
31612
31613 /* Eat '@private'/'@protected'/'@public'. */
31614 cp_lexer_consume_token (parser->lexer);
31615 }
31616
31617 /* Parse an Objective-C method type. Return 'true' if it is a class
31618 (+) method, and 'false' if it is an instance (-) method. */
31619
31620 static inline bool
31621 cp_parser_objc_method_type (cp_parser* parser)
31622 {
31623 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
31624 return true;
31625 else
31626 return false;
31627 }
31628
31629 /* Parse an Objective-C protocol qualifier. */
31630
31631 static tree
31632 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
31633 {
31634 tree quals = NULL_TREE, node;
31635 cp_token *token = cp_lexer_peek_token (parser->lexer);
31636
31637 node = token->u.value;
31638
31639 while (node && identifier_p (node)
31640 && (node == ridpointers [(int) RID_IN]
31641 || node == ridpointers [(int) RID_OUT]
31642 || node == ridpointers [(int) RID_INOUT]
31643 || node == ridpointers [(int) RID_BYCOPY]
31644 || node == ridpointers [(int) RID_BYREF]
31645 || node == ridpointers [(int) RID_ONEWAY]))
31646 {
31647 quals = tree_cons (NULL_TREE, node, quals);
31648 cp_lexer_consume_token (parser->lexer);
31649 token = cp_lexer_peek_token (parser->lexer);
31650 node = token->u.value;
31651 }
31652
31653 return quals;
31654 }
31655
31656 /* Parse an Objective-C typename. */
31657
31658 static tree
31659 cp_parser_objc_typename (cp_parser* parser)
31660 {
31661 tree type_name = NULL_TREE;
31662
31663 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31664 {
31665 tree proto_quals, cp_type = NULL_TREE;
31666
31667 matching_parens parens;
31668 parens.consume_open (parser); /* Eat '('. */
31669 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
31670
31671 /* An ObjC type name may consist of just protocol qualifiers, in which
31672 case the type shall default to 'id'. */
31673 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
31674 {
31675 cp_type = cp_parser_type_id (parser);
31676
31677 /* If the type could not be parsed, an error has already
31678 been produced. For error recovery, behave as if it had
31679 not been specified, which will use the default type
31680 'id'. */
31681 if (cp_type == error_mark_node)
31682 {
31683 cp_type = NULL_TREE;
31684 /* We need to skip to the closing parenthesis as
31685 cp_parser_type_id() does not seem to do it for
31686 us. */
31687 cp_parser_skip_to_closing_parenthesis (parser,
31688 /*recovering=*/true,
31689 /*or_comma=*/false,
31690 /*consume_paren=*/false);
31691 }
31692 }
31693
31694 parens.require_close (parser);
31695 type_name = build_tree_list (proto_quals, cp_type);
31696 }
31697
31698 return type_name;
31699 }
31700
31701 /* Check to see if TYPE refers to an Objective-C selector name. */
31702
31703 static bool
31704 cp_parser_objc_selector_p (enum cpp_ttype type)
31705 {
31706 return (type == CPP_NAME || type == CPP_KEYWORD
31707 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
31708 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
31709 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
31710 || type == CPP_XOR || type == CPP_XOR_EQ);
31711 }
31712
31713 /* Parse an Objective-C selector. */
31714
31715 static tree
31716 cp_parser_objc_selector (cp_parser* parser)
31717 {
31718 cp_token *token = cp_lexer_consume_token (parser->lexer);
31719
31720 if (!cp_parser_objc_selector_p (token->type))
31721 {
31722 error_at (token->location, "invalid Objective-C++ selector name");
31723 return error_mark_node;
31724 }
31725
31726 /* C++ operator names are allowed to appear in ObjC selectors. */
31727 switch (token->type)
31728 {
31729 case CPP_AND_AND: return get_identifier ("and");
31730 case CPP_AND_EQ: return get_identifier ("and_eq");
31731 case CPP_AND: return get_identifier ("bitand");
31732 case CPP_OR: return get_identifier ("bitor");
31733 case CPP_COMPL: return get_identifier ("compl");
31734 case CPP_NOT: return get_identifier ("not");
31735 case CPP_NOT_EQ: return get_identifier ("not_eq");
31736 case CPP_OR_OR: return get_identifier ("or");
31737 case CPP_OR_EQ: return get_identifier ("or_eq");
31738 case CPP_XOR: return get_identifier ("xor");
31739 case CPP_XOR_EQ: return get_identifier ("xor_eq");
31740 default: return token->u.value;
31741 }
31742 }
31743
31744 /* Parse an Objective-C params list. */
31745
31746 static tree
31747 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
31748 {
31749 tree params = NULL_TREE;
31750 bool maybe_unary_selector_p = true;
31751 cp_token *token = cp_lexer_peek_token (parser->lexer);
31752
31753 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
31754 {
31755 tree selector = NULL_TREE, type_name, identifier;
31756 tree parm_attr = NULL_TREE;
31757
31758 if (token->keyword == RID_ATTRIBUTE)
31759 break;
31760
31761 if (token->type != CPP_COLON)
31762 selector = cp_parser_objc_selector (parser);
31763
31764 /* Detect if we have a unary selector. */
31765 if (maybe_unary_selector_p
31766 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31767 {
31768 params = selector; /* Might be followed by attributes. */
31769 break;
31770 }
31771
31772 maybe_unary_selector_p = false;
31773 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31774 {
31775 /* Something went quite wrong. There should be a colon
31776 here, but there is not. Stop parsing parameters. */
31777 break;
31778 }
31779 type_name = cp_parser_objc_typename (parser);
31780 /* New ObjC allows attributes on parameters too. */
31781 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31782 parm_attr = cp_parser_attributes_opt (parser);
31783 identifier = cp_parser_identifier (parser);
31784
31785 params
31786 = chainon (params,
31787 objc_build_keyword_decl (selector,
31788 type_name,
31789 identifier,
31790 parm_attr));
31791
31792 token = cp_lexer_peek_token (parser->lexer);
31793 }
31794
31795 if (params == NULL_TREE)
31796 {
31797 cp_parser_error (parser, "objective-c++ method declaration is expected");
31798 return error_mark_node;
31799 }
31800
31801 /* We allow tail attributes for the method. */
31802 if (token->keyword == RID_ATTRIBUTE)
31803 {
31804 *attributes = cp_parser_attributes_opt (parser);
31805 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
31806 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31807 return params;
31808 cp_parser_error (parser,
31809 "method attributes must be specified at the end");
31810 return error_mark_node;
31811 }
31812
31813 if (params == NULL_TREE)
31814 {
31815 cp_parser_error (parser, "objective-c++ method declaration is expected");
31816 return error_mark_node;
31817 }
31818 return params;
31819 }
31820
31821 /* Parse the non-keyword Objective-C params. */
31822
31823 static tree
31824 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
31825 tree* attributes)
31826 {
31827 tree params = make_node (TREE_LIST);
31828 cp_token *token = cp_lexer_peek_token (parser->lexer);
31829 *ellipsisp = false; /* Initially, assume no ellipsis. */
31830
31831 while (token->type == CPP_COMMA)
31832 {
31833 cp_parameter_declarator *parmdecl;
31834 tree parm;
31835
31836 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31837 token = cp_lexer_peek_token (parser->lexer);
31838
31839 if (token->type == CPP_ELLIPSIS)
31840 {
31841 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
31842 *ellipsisp = true;
31843 token = cp_lexer_peek_token (parser->lexer);
31844 break;
31845 }
31846
31847 /* TODO: parse attributes for tail parameters. */
31848 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31849 false, NULL);
31850 parm = grokdeclarator (parmdecl->declarator,
31851 &parmdecl->decl_specifiers,
31852 PARM, /*initialized=*/0,
31853 /*attrlist=*/NULL);
31854
31855 chainon (params, build_tree_list (NULL_TREE, parm));
31856 token = cp_lexer_peek_token (parser->lexer);
31857 }
31858
31859 /* We allow tail attributes for the method. */
31860 if (token->keyword == RID_ATTRIBUTE)
31861 {
31862 if (*attributes == NULL_TREE)
31863 {
31864 *attributes = cp_parser_attributes_opt (parser);
31865 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
31866 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31867 return params;
31868 }
31869 else
31870 /* We have an error, but parse the attributes, so that we can
31871 carry on. */
31872 *attributes = cp_parser_attributes_opt (parser);
31873
31874 cp_parser_error (parser,
31875 "method attributes must be specified at the end");
31876 return error_mark_node;
31877 }
31878
31879 return params;
31880 }
31881
31882 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
31883
31884 static void
31885 cp_parser_objc_interstitial_code (cp_parser* parser)
31886 {
31887 cp_token *token = cp_lexer_peek_token (parser->lexer);
31888
31889 /* If the next token is `extern' and the following token is a string
31890 literal, then we have a linkage specification. */
31891 if (token->keyword == RID_EXTERN
31892 && cp_parser_is_pure_string_literal
31893 (cp_lexer_peek_nth_token (parser->lexer, 2)))
31894 cp_parser_linkage_specification (parser);
31895 /* Handle #pragma, if any. */
31896 else if (token->type == CPP_PRAGMA)
31897 cp_parser_pragma (parser, pragma_objc_icode, NULL);
31898 /* Allow stray semicolons. */
31899 else if (token->type == CPP_SEMICOLON)
31900 cp_lexer_consume_token (parser->lexer);
31901 /* Mark methods as optional or required, when building protocols. */
31902 else if (token->keyword == RID_AT_OPTIONAL)
31903 {
31904 cp_lexer_consume_token (parser->lexer);
31905 objc_set_method_opt (true);
31906 }
31907 else if (token->keyword == RID_AT_REQUIRED)
31908 {
31909 cp_lexer_consume_token (parser->lexer);
31910 objc_set_method_opt (false);
31911 }
31912 else if (token->keyword == RID_NAMESPACE)
31913 cp_parser_namespace_definition (parser);
31914 /* Other stray characters must generate errors. */
31915 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
31916 {
31917 cp_lexer_consume_token (parser->lexer);
31918 error ("stray %qs between Objective-C++ methods",
31919 token->type == CPP_OPEN_BRACE ? "{" : "}");
31920 }
31921 /* Finally, try to parse a block-declaration, or a function-definition. */
31922 else
31923 cp_parser_block_declaration (parser, /*statement_p=*/false);
31924 }
31925
31926 /* Parse a method signature. */
31927
31928 static tree
31929 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
31930 {
31931 tree rettype, kwdparms, optparms;
31932 bool ellipsis = false;
31933 bool is_class_method;
31934
31935 is_class_method = cp_parser_objc_method_type (parser);
31936 rettype = cp_parser_objc_typename (parser);
31937 *attributes = NULL_TREE;
31938 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
31939 if (kwdparms == error_mark_node)
31940 return error_mark_node;
31941 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
31942 if (optparms == error_mark_node)
31943 return error_mark_node;
31944
31945 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
31946 }
31947
31948 static bool
31949 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
31950 {
31951 tree tattr;
31952 cp_lexer_save_tokens (parser->lexer);
31953 tattr = cp_parser_attributes_opt (parser);
31954 gcc_assert (tattr) ;
31955
31956 /* If the attributes are followed by a method introducer, this is not allowed.
31957 Dump the attributes and flag the situation. */
31958 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
31959 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31960 return true;
31961
31962 /* Otherwise, the attributes introduce some interstitial code, possibly so
31963 rewind to allow that check. */
31964 cp_lexer_rollback_tokens (parser->lexer);
31965 return false;
31966 }
31967
31968 /* Parse an Objective-C method prototype list. */
31969
31970 static void
31971 cp_parser_objc_method_prototype_list (cp_parser* parser)
31972 {
31973 cp_token *token = cp_lexer_peek_token (parser->lexer);
31974
31975 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31976 {
31977 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31978 {
31979 tree attributes, sig;
31980 bool is_class_method;
31981 if (token->type == CPP_PLUS)
31982 is_class_method = true;
31983 else
31984 is_class_method = false;
31985 sig = cp_parser_objc_method_signature (parser, &attributes);
31986 if (sig == error_mark_node)
31987 {
31988 cp_parser_skip_to_end_of_block_or_statement (parser);
31989 token = cp_lexer_peek_token (parser->lexer);
31990 continue;
31991 }
31992 objc_add_method_declaration (is_class_method, sig, attributes);
31993 cp_parser_consume_semicolon_at_end_of_statement (parser);
31994 }
31995 else if (token->keyword == RID_AT_PROPERTY)
31996 cp_parser_objc_at_property_declaration (parser);
31997 else if (token->keyword == RID_ATTRIBUTE
31998 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31999 warning_at (cp_lexer_peek_token (parser->lexer)->location,
32000 OPT_Wattributes,
32001 "prefix attributes are ignored for methods");
32002 else
32003 /* Allow for interspersed non-ObjC++ code. */
32004 cp_parser_objc_interstitial_code (parser);
32005
32006 token = cp_lexer_peek_token (parser->lexer);
32007 }
32008
32009 if (token->type != CPP_EOF)
32010 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
32011 else
32012 cp_parser_error (parser, "expected %<@end%>");
32013
32014 objc_finish_interface ();
32015 }
32016
32017 /* Parse an Objective-C method definition list. */
32018
32019 static void
32020 cp_parser_objc_method_definition_list (cp_parser* parser)
32021 {
32022 cp_token *token = cp_lexer_peek_token (parser->lexer);
32023
32024 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
32025 {
32026 tree meth;
32027
32028 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
32029 {
32030 cp_token *ptk;
32031 tree sig, attribute;
32032 bool is_class_method;
32033 if (token->type == CPP_PLUS)
32034 is_class_method = true;
32035 else
32036 is_class_method = false;
32037 push_deferring_access_checks (dk_deferred);
32038 sig = cp_parser_objc_method_signature (parser, &attribute);
32039 if (sig == error_mark_node)
32040 {
32041 cp_parser_skip_to_end_of_block_or_statement (parser);
32042 token = cp_lexer_peek_token (parser->lexer);
32043 continue;
32044 }
32045 objc_start_method_definition (is_class_method, sig, attribute,
32046 NULL_TREE);
32047
32048 /* For historical reasons, we accept an optional semicolon. */
32049 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32050 cp_lexer_consume_token (parser->lexer);
32051
32052 ptk = cp_lexer_peek_token (parser->lexer);
32053 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
32054 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
32055 {
32056 perform_deferred_access_checks (tf_warning_or_error);
32057 stop_deferring_access_checks ();
32058 meth = cp_parser_function_definition_after_declarator (parser,
32059 false);
32060 pop_deferring_access_checks ();
32061 objc_finish_method_definition (meth);
32062 }
32063 }
32064 /* The following case will be removed once @synthesize is
32065 completely implemented. */
32066 else if (token->keyword == RID_AT_PROPERTY)
32067 cp_parser_objc_at_property_declaration (parser);
32068 else if (token->keyword == RID_AT_SYNTHESIZE)
32069 cp_parser_objc_at_synthesize_declaration (parser);
32070 else if (token->keyword == RID_AT_DYNAMIC)
32071 cp_parser_objc_at_dynamic_declaration (parser);
32072 else if (token->keyword == RID_ATTRIBUTE
32073 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
32074 warning_at (token->location, OPT_Wattributes,
32075 "prefix attributes are ignored for methods");
32076 else
32077 /* Allow for interspersed non-ObjC++ code. */
32078 cp_parser_objc_interstitial_code (parser);
32079
32080 token = cp_lexer_peek_token (parser->lexer);
32081 }
32082
32083 if (token->type != CPP_EOF)
32084 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
32085 else
32086 cp_parser_error (parser, "expected %<@end%>");
32087
32088 objc_finish_implementation ();
32089 }
32090
32091 /* Parse Objective-C ivars. */
32092
32093 static void
32094 cp_parser_objc_class_ivars (cp_parser* parser)
32095 {
32096 cp_token *token = cp_lexer_peek_token (parser->lexer);
32097
32098 if (token->type != CPP_OPEN_BRACE)
32099 return; /* No ivars specified. */
32100
32101 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
32102 token = cp_lexer_peek_token (parser->lexer);
32103
32104 while (token->type != CPP_CLOSE_BRACE
32105 && token->keyword != RID_AT_END && token->type != CPP_EOF)
32106 {
32107 cp_decl_specifier_seq declspecs;
32108 int decl_class_or_enum_p;
32109 tree prefix_attributes;
32110
32111 cp_parser_objc_visibility_spec (parser);
32112
32113 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
32114 break;
32115
32116 cp_parser_decl_specifier_seq (parser,
32117 CP_PARSER_FLAGS_OPTIONAL,
32118 &declspecs,
32119 &decl_class_or_enum_p);
32120
32121 /* auto, register, static, extern, mutable. */
32122 if (declspecs.storage_class != sc_none)
32123 {
32124 cp_parser_error (parser, "invalid type for instance variable");
32125 declspecs.storage_class = sc_none;
32126 }
32127
32128 /* thread_local. */
32129 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
32130 {
32131 cp_parser_error (parser, "invalid type for instance variable");
32132 declspecs.locations[ds_thread] = 0;
32133 }
32134
32135 /* typedef. */
32136 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
32137 {
32138 cp_parser_error (parser, "invalid type for instance variable");
32139 declspecs.locations[ds_typedef] = 0;
32140 }
32141
32142 prefix_attributes = declspecs.attributes;
32143 declspecs.attributes = NULL_TREE;
32144
32145 /* Keep going until we hit the `;' at the end of the
32146 declaration. */
32147 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
32148 {
32149 tree width = NULL_TREE, attributes, first_attribute, decl;
32150 cp_declarator *declarator = NULL;
32151 int ctor_dtor_or_conv_p;
32152
32153 /* Check for a (possibly unnamed) bitfield declaration. */
32154 token = cp_lexer_peek_token (parser->lexer);
32155 if (token->type == CPP_COLON)
32156 goto eat_colon;
32157
32158 if (token->type == CPP_NAME
32159 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
32160 == CPP_COLON))
32161 {
32162 /* Get the name of the bitfield. */
32163 declarator = make_id_declarator (NULL_TREE,
32164 cp_parser_identifier (parser),
32165 sfk_none, token->location);
32166
32167 eat_colon:
32168 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
32169 /* Get the width of the bitfield. */
32170 width
32171 = cp_parser_constant_expression (parser);
32172 }
32173 else
32174 {
32175 /* Parse the declarator. */
32176 declarator
32177 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
32178 CP_PARSER_FLAGS_NONE,
32179 &ctor_dtor_or_conv_p,
32180 /*parenthesized_p=*/NULL,
32181 /*member_p=*/false,
32182 /*friend_p=*/false,
32183 /*static_p=*/false);
32184 }
32185
32186 /* Look for attributes that apply to the ivar. */
32187 attributes = cp_parser_attributes_opt (parser);
32188 /* Remember which attributes are prefix attributes and
32189 which are not. */
32190 first_attribute = attributes;
32191 /* Combine the attributes. */
32192 attributes = attr_chainon (prefix_attributes, attributes);
32193
32194 if (width)
32195 /* Create the bitfield declaration. */
32196 decl = grokbitfield (declarator, &declspecs,
32197 width, NULL_TREE, attributes);
32198 else
32199 decl = grokfield (declarator, &declspecs,
32200 NULL_TREE, /*init_const_expr_p=*/false,
32201 NULL_TREE, attributes);
32202
32203 /* Add the instance variable. */
32204 if (decl != error_mark_node && decl != NULL_TREE)
32205 objc_add_instance_variable (decl);
32206
32207 /* Reset PREFIX_ATTRIBUTES. */
32208 if (attributes != error_mark_node)
32209 {
32210 while (attributes && TREE_CHAIN (attributes) != first_attribute)
32211 attributes = TREE_CHAIN (attributes);
32212 if (attributes)
32213 TREE_CHAIN (attributes) = NULL_TREE;
32214 }
32215
32216 token = cp_lexer_peek_token (parser->lexer);
32217
32218 if (token->type == CPP_COMMA)
32219 {
32220 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
32221 continue;
32222 }
32223 break;
32224 }
32225
32226 cp_parser_consume_semicolon_at_end_of_statement (parser);
32227 token = cp_lexer_peek_token (parser->lexer);
32228 }
32229
32230 if (token->keyword == RID_AT_END)
32231 cp_parser_error (parser, "expected %<}%>");
32232
32233 /* Do not consume the RID_AT_END, so it will be read again as terminating
32234 the @interface of @implementation. */
32235 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
32236 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
32237
32238 /* For historical reasons, we accept an optional semicolon. */
32239 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32240 cp_lexer_consume_token (parser->lexer);
32241 }
32242
32243 /* Parse an Objective-C protocol declaration. */
32244
32245 static void
32246 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
32247 {
32248 tree proto, protorefs;
32249 cp_token *tok;
32250
32251 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
32252 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32253 {
32254 tok = cp_lexer_peek_token (parser->lexer);
32255 error_at (tok->location, "identifier expected after %<@protocol%>");
32256 cp_parser_consume_semicolon_at_end_of_statement (parser);
32257 return;
32258 }
32259
32260 /* See if we have a forward declaration or a definition. */
32261 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
32262
32263 /* Try a forward declaration first. */
32264 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
32265 {
32266 while (true)
32267 {
32268 tree id;
32269
32270 id = cp_parser_identifier (parser);
32271 if (id == error_mark_node)
32272 break;
32273
32274 objc_declare_protocol (id, attributes);
32275
32276 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32277 cp_lexer_consume_token (parser->lexer);
32278 else
32279 break;
32280 }
32281 cp_parser_consume_semicolon_at_end_of_statement (parser);
32282 }
32283
32284 /* Ok, we got a full-fledged definition (or at least should). */
32285 else
32286 {
32287 proto = cp_parser_identifier (parser);
32288 protorefs = cp_parser_objc_protocol_refs_opt (parser);
32289 objc_start_protocol (proto, protorefs, attributes);
32290 cp_parser_objc_method_prototype_list (parser);
32291 }
32292 }
32293
32294 /* Parse an Objective-C superclass or category. */
32295
32296 static void
32297 cp_parser_objc_superclass_or_category (cp_parser *parser,
32298 bool iface_p,
32299 tree *super,
32300 tree *categ, bool *is_class_extension)
32301 {
32302 cp_token *next = cp_lexer_peek_token (parser->lexer);
32303
32304 *super = *categ = NULL_TREE;
32305 *is_class_extension = false;
32306 if (next->type == CPP_COLON)
32307 {
32308 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
32309 *super = cp_parser_identifier (parser);
32310 }
32311 else if (next->type == CPP_OPEN_PAREN)
32312 {
32313 matching_parens parens;
32314 parens.consume_open (parser); /* Eat '('. */
32315
32316 /* If there is no category name, and this is an @interface, we
32317 have a class extension. */
32318 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32319 {
32320 *categ = NULL_TREE;
32321 *is_class_extension = true;
32322 }
32323 else
32324 *categ = cp_parser_identifier (parser);
32325
32326 parens.require_close (parser);
32327 }
32328 }
32329
32330 /* Parse an Objective-C class interface. */
32331
32332 static void
32333 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
32334 {
32335 tree name, super, categ, protos;
32336 bool is_class_extension;
32337
32338 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
32339 name = cp_parser_identifier (parser);
32340 if (name == error_mark_node)
32341 {
32342 /* It's hard to recover because even if valid @interface stuff
32343 is to follow, we can't compile it (or validate it) if we
32344 don't even know which class it refers to. Let's assume this
32345 was a stray '@interface' token in the stream and skip it.
32346 */
32347 return;
32348 }
32349 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
32350 &is_class_extension);
32351 protos = cp_parser_objc_protocol_refs_opt (parser);
32352
32353 /* We have either a class or a category on our hands. */
32354 if (categ || is_class_extension)
32355 objc_start_category_interface (name, categ, protos, attributes);
32356 else
32357 {
32358 objc_start_class_interface (name, super, protos, attributes);
32359 /* Handle instance variable declarations, if any. */
32360 cp_parser_objc_class_ivars (parser);
32361 objc_continue_interface ();
32362 }
32363
32364 cp_parser_objc_method_prototype_list (parser);
32365 }
32366
32367 /* Parse an Objective-C class implementation. */
32368
32369 static void
32370 cp_parser_objc_class_implementation (cp_parser* parser)
32371 {
32372 tree name, super, categ;
32373 bool is_class_extension;
32374
32375 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
32376 name = cp_parser_identifier (parser);
32377 if (name == error_mark_node)
32378 {
32379 /* It's hard to recover because even if valid @implementation
32380 stuff is to follow, we can't compile it (or validate it) if
32381 we don't even know which class it refers to. Let's assume
32382 this was a stray '@implementation' token in the stream and
32383 skip it.
32384 */
32385 return;
32386 }
32387 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
32388 &is_class_extension);
32389
32390 /* We have either a class or a category on our hands. */
32391 if (categ)
32392 objc_start_category_implementation (name, categ);
32393 else
32394 {
32395 objc_start_class_implementation (name, super);
32396 /* Handle instance variable declarations, if any. */
32397 cp_parser_objc_class_ivars (parser);
32398 objc_continue_implementation ();
32399 }
32400
32401 cp_parser_objc_method_definition_list (parser);
32402 }
32403
32404 /* Consume the @end token and finish off the implementation. */
32405
32406 static void
32407 cp_parser_objc_end_implementation (cp_parser* parser)
32408 {
32409 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
32410 objc_finish_implementation ();
32411 }
32412
32413 /* Parse an Objective-C declaration. */
32414
32415 static void
32416 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
32417 {
32418 /* Try to figure out what kind of declaration is present. */
32419 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
32420
32421 if (attributes)
32422 switch (kwd->keyword)
32423 {
32424 case RID_AT_ALIAS:
32425 case RID_AT_CLASS:
32426 case RID_AT_END:
32427 error_at (kwd->location, "attributes may not be specified before"
32428 " the %<@%D%> Objective-C++ keyword",
32429 kwd->u.value);
32430 attributes = NULL;
32431 break;
32432 case RID_AT_IMPLEMENTATION:
32433 warning_at (kwd->location, OPT_Wattributes,
32434 "prefix attributes are ignored before %<@%D%>",
32435 kwd->u.value);
32436 attributes = NULL;
32437 default:
32438 break;
32439 }
32440
32441 switch (kwd->keyword)
32442 {
32443 case RID_AT_ALIAS:
32444 cp_parser_objc_alias_declaration (parser);
32445 break;
32446 case RID_AT_CLASS:
32447 cp_parser_objc_class_declaration (parser);
32448 break;
32449 case RID_AT_PROTOCOL:
32450 cp_parser_objc_protocol_declaration (parser, attributes);
32451 break;
32452 case RID_AT_INTERFACE:
32453 cp_parser_objc_class_interface (parser, attributes);
32454 break;
32455 case RID_AT_IMPLEMENTATION:
32456 cp_parser_objc_class_implementation (parser);
32457 break;
32458 case RID_AT_END:
32459 cp_parser_objc_end_implementation (parser);
32460 break;
32461 default:
32462 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
32463 kwd->u.value);
32464 cp_parser_skip_to_end_of_block_or_statement (parser);
32465 }
32466 }
32467
32468 /* Parse an Objective-C try-catch-finally statement.
32469
32470 objc-try-catch-finally-stmt:
32471 @try compound-statement objc-catch-clause-seq [opt]
32472 objc-finally-clause [opt]
32473
32474 objc-catch-clause-seq:
32475 objc-catch-clause objc-catch-clause-seq [opt]
32476
32477 objc-catch-clause:
32478 @catch ( objc-exception-declaration ) compound-statement
32479
32480 objc-finally-clause:
32481 @finally compound-statement
32482
32483 objc-exception-declaration:
32484 parameter-declaration
32485 '...'
32486
32487 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
32488
32489 Returns NULL_TREE.
32490
32491 PS: This function is identical to c_parser_objc_try_catch_finally_statement
32492 for C. Keep them in sync. */
32493
32494 static tree
32495 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
32496 {
32497 location_t location;
32498 tree stmt;
32499
32500 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
32501 location = cp_lexer_peek_token (parser->lexer)->location;
32502 objc_maybe_warn_exceptions (location);
32503 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
32504 node, lest it get absorbed into the surrounding block. */
32505 stmt = push_stmt_list ();
32506 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
32507 objc_begin_try_stmt (location, pop_stmt_list (stmt));
32508
32509 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
32510 {
32511 cp_parameter_declarator *parm;
32512 tree parameter_declaration = error_mark_node;
32513 bool seen_open_paren = false;
32514 matching_parens parens;
32515
32516 cp_lexer_consume_token (parser->lexer);
32517 if (parens.require_open (parser))
32518 seen_open_paren = true;
32519 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
32520 {
32521 /* We have "@catch (...)" (where the '...' are literally
32522 what is in the code). Skip the '...'.
32523 parameter_declaration is set to NULL_TREE, and
32524 objc_being_catch_clauses() knows that that means
32525 '...'. */
32526 cp_lexer_consume_token (parser->lexer);
32527 parameter_declaration = NULL_TREE;
32528 }
32529 else
32530 {
32531 /* We have "@catch (NSException *exception)" or something
32532 like that. Parse the parameter declaration. */
32533 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
32534 false, NULL);
32535 if (parm == NULL)
32536 parameter_declaration = error_mark_node;
32537 else
32538 parameter_declaration = grokdeclarator (parm->declarator,
32539 &parm->decl_specifiers,
32540 PARM, /*initialized=*/0,
32541 /*attrlist=*/NULL);
32542 }
32543 if (seen_open_paren)
32544 parens.require_close (parser);
32545 else
32546 {
32547 /* If there was no open parenthesis, we are recovering from
32548 an error, and we are trying to figure out what mistake
32549 the user has made. */
32550
32551 /* If there is an immediate closing parenthesis, the user
32552 probably forgot the opening one (ie, they typed "@catch
32553 NSException *e)". Parse the closing parenthesis and keep
32554 going. */
32555 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32556 cp_lexer_consume_token (parser->lexer);
32557
32558 /* If these is no immediate closing parenthesis, the user
32559 probably doesn't know that parenthesis are required at
32560 all (ie, they typed "@catch NSException *e"). So, just
32561 forget about the closing parenthesis and keep going. */
32562 }
32563 objc_begin_catch_clause (parameter_declaration);
32564 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
32565 objc_finish_catch_clause ();
32566 }
32567 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
32568 {
32569 cp_lexer_consume_token (parser->lexer);
32570 location = cp_lexer_peek_token (parser->lexer)->location;
32571 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
32572 node, lest it get absorbed into the surrounding block. */
32573 stmt = push_stmt_list ();
32574 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
32575 objc_build_finally_clause (location, pop_stmt_list (stmt));
32576 }
32577
32578 return objc_finish_try_stmt ();
32579 }
32580
32581 /* Parse an Objective-C synchronized statement.
32582
32583 objc-synchronized-stmt:
32584 @synchronized ( expression ) compound-statement
32585
32586 Returns NULL_TREE. */
32587
32588 static tree
32589 cp_parser_objc_synchronized_statement (cp_parser *parser)
32590 {
32591 location_t location;
32592 tree lock, stmt;
32593
32594 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
32595
32596 location = cp_lexer_peek_token (parser->lexer)->location;
32597 objc_maybe_warn_exceptions (location);
32598 matching_parens parens;
32599 parens.require_open (parser);
32600 lock = cp_parser_expression (parser);
32601 parens.require_close (parser);
32602
32603 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
32604 node, lest it get absorbed into the surrounding block. */
32605 stmt = push_stmt_list ();
32606 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
32607
32608 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
32609 }
32610
32611 /* Parse an Objective-C throw statement.
32612
32613 objc-throw-stmt:
32614 @throw assignment-expression [opt] ;
32615
32616 Returns a constructed '@throw' statement. */
32617
32618 static tree
32619 cp_parser_objc_throw_statement (cp_parser *parser)
32620 {
32621 tree expr = NULL_TREE;
32622 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32623
32624 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
32625
32626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
32627 expr = cp_parser_expression (parser);
32628
32629 cp_parser_consume_semicolon_at_end_of_statement (parser);
32630
32631 return objc_build_throw_stmt (loc, expr);
32632 }
32633
32634 /* Parse an Objective-C statement. */
32635
32636 static tree
32637 cp_parser_objc_statement (cp_parser * parser)
32638 {
32639 /* Try to figure out what kind of declaration is present. */
32640 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
32641
32642 switch (kwd->keyword)
32643 {
32644 case RID_AT_TRY:
32645 return cp_parser_objc_try_catch_finally_statement (parser);
32646 case RID_AT_SYNCHRONIZED:
32647 return cp_parser_objc_synchronized_statement (parser);
32648 case RID_AT_THROW:
32649 return cp_parser_objc_throw_statement (parser);
32650 default:
32651 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
32652 kwd->u.value);
32653 cp_parser_skip_to_end_of_block_or_statement (parser);
32654 }
32655
32656 return error_mark_node;
32657 }
32658
32659 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
32660 look ahead to see if an objc keyword follows the attributes. This
32661 is to detect the use of prefix attributes on ObjC @interface and
32662 @protocol. */
32663
32664 static bool
32665 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
32666 {
32667 cp_lexer_save_tokens (parser->lexer);
32668 *attrib = cp_parser_attributes_opt (parser);
32669 gcc_assert (*attrib);
32670 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
32671 {
32672 cp_lexer_commit_tokens (parser->lexer);
32673 return true;
32674 }
32675 cp_lexer_rollback_tokens (parser->lexer);
32676 return false;
32677 }
32678
32679 /* This routine is a minimal replacement for
32680 c_parser_struct_declaration () used when parsing the list of
32681 types/names or ObjC++ properties. For example, when parsing the
32682 code
32683
32684 @property (readonly) int a, b, c;
32685
32686 this function is responsible for parsing "int a, int b, int c" and
32687 returning the declarations as CHAIN of DECLs.
32688
32689 TODO: Share this code with cp_parser_objc_class_ivars. It's very
32690 similar parsing. */
32691 static tree
32692 cp_parser_objc_struct_declaration (cp_parser *parser)
32693 {
32694 tree decls = NULL_TREE;
32695 cp_decl_specifier_seq declspecs;
32696 int decl_class_or_enum_p;
32697 tree prefix_attributes;
32698
32699 cp_parser_decl_specifier_seq (parser,
32700 CP_PARSER_FLAGS_NONE,
32701 &declspecs,
32702 &decl_class_or_enum_p);
32703
32704 if (declspecs.type == error_mark_node)
32705 return error_mark_node;
32706
32707 /* auto, register, static, extern, mutable. */
32708 if (declspecs.storage_class != sc_none)
32709 {
32710 cp_parser_error (parser, "invalid type for property");
32711 declspecs.storage_class = sc_none;
32712 }
32713
32714 /* thread_local. */
32715 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
32716 {
32717 cp_parser_error (parser, "invalid type for property");
32718 declspecs.locations[ds_thread] = 0;
32719 }
32720
32721 /* typedef. */
32722 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
32723 {
32724 cp_parser_error (parser, "invalid type for property");
32725 declspecs.locations[ds_typedef] = 0;
32726 }
32727
32728 prefix_attributes = declspecs.attributes;
32729 declspecs.attributes = NULL_TREE;
32730
32731 /* Keep going until we hit the `;' at the end of the declaration. */
32732 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
32733 {
32734 tree attributes, first_attribute, decl;
32735 cp_declarator *declarator;
32736 cp_token *token;
32737
32738 /* Parse the declarator. */
32739 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
32740 CP_PARSER_FLAGS_NONE,
32741 NULL, NULL, false, false, false);
32742
32743 /* Look for attributes that apply to the ivar. */
32744 attributes = cp_parser_attributes_opt (parser);
32745 /* Remember which attributes are prefix attributes and
32746 which are not. */
32747 first_attribute = attributes;
32748 /* Combine the attributes. */
32749 attributes = attr_chainon (prefix_attributes, attributes);
32750
32751 decl = grokfield (declarator, &declspecs,
32752 NULL_TREE, /*init_const_expr_p=*/false,
32753 NULL_TREE, attributes);
32754
32755 if (decl == error_mark_node || decl == NULL_TREE)
32756 return error_mark_node;
32757
32758 /* Reset PREFIX_ATTRIBUTES. */
32759 if (attributes != error_mark_node)
32760 {
32761 while (attributes && TREE_CHAIN (attributes) != first_attribute)
32762 attributes = TREE_CHAIN (attributes);
32763 if (attributes)
32764 TREE_CHAIN (attributes) = NULL_TREE;
32765 }
32766
32767 DECL_CHAIN (decl) = decls;
32768 decls = decl;
32769
32770 token = cp_lexer_peek_token (parser->lexer);
32771 if (token->type == CPP_COMMA)
32772 {
32773 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
32774 continue;
32775 }
32776 else
32777 break;
32778 }
32779 return decls;
32780 }
32781
32782 /* Parse an Objective-C @property declaration. The syntax is:
32783
32784 objc-property-declaration:
32785 '@property' objc-property-attributes[opt] struct-declaration ;
32786
32787 objc-property-attributes:
32788 '(' objc-property-attribute-list ')'
32789
32790 objc-property-attribute-list:
32791 objc-property-attribute
32792 objc-property-attribute-list, objc-property-attribute
32793
32794 objc-property-attribute
32795 'getter' = identifier
32796 'setter' = identifier
32797 'readonly'
32798 'readwrite'
32799 'assign'
32800 'retain'
32801 'copy'
32802 'nonatomic'
32803
32804 For example:
32805 @property NSString *name;
32806 @property (readonly) id object;
32807 @property (retain, nonatomic, getter=getTheName) id name;
32808 @property int a, b, c;
32809
32810 PS: This function is identical to
32811 c_parser_objc_at_property_declaration for C. Keep them in sync. */
32812 static void
32813 cp_parser_objc_at_property_declaration (cp_parser *parser)
32814 {
32815 /* The following variables hold the attributes of the properties as
32816 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
32817 seen. When we see an attribute, we set them to 'true' (if they
32818 are boolean properties) or to the identifier (if they have an
32819 argument, ie, for getter and setter). Note that here we only
32820 parse the list of attributes, check the syntax and accumulate the
32821 attributes that we find. objc_add_property_declaration() will
32822 then process the information. */
32823 bool property_assign = false;
32824 bool property_copy = false;
32825 tree property_getter_ident = NULL_TREE;
32826 bool property_nonatomic = false;
32827 bool property_readonly = false;
32828 bool property_readwrite = false;
32829 bool property_retain = false;
32830 tree property_setter_ident = NULL_TREE;
32831
32832 /* 'properties' is the list of properties that we read. Usually a
32833 single one, but maybe more (eg, in "@property int a, b, c;" there
32834 are three). */
32835 tree properties;
32836 location_t loc;
32837
32838 loc = cp_lexer_peek_token (parser->lexer)->location;
32839
32840 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
32841
32842 /* Parse the optional attribute list... */
32843 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32844 {
32845 /* Eat the '('. */
32846 matching_parens parens;
32847 parens.consume_open (parser);
32848
32849 while (true)
32850 {
32851 bool syntax_error = false;
32852 cp_token *token = cp_lexer_peek_token (parser->lexer);
32853 enum rid keyword;
32854
32855 if (token->type != CPP_NAME)
32856 {
32857 cp_parser_error (parser, "expected identifier");
32858 break;
32859 }
32860 keyword = C_RID_CODE (token->u.value);
32861 cp_lexer_consume_token (parser->lexer);
32862 switch (keyword)
32863 {
32864 case RID_ASSIGN: property_assign = true; break;
32865 case RID_COPY: property_copy = true; break;
32866 case RID_NONATOMIC: property_nonatomic = true; break;
32867 case RID_READONLY: property_readonly = true; break;
32868 case RID_READWRITE: property_readwrite = true; break;
32869 case RID_RETAIN: property_retain = true; break;
32870
32871 case RID_GETTER:
32872 case RID_SETTER:
32873 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
32874 {
32875 if (keyword == RID_GETTER)
32876 cp_parser_error (parser,
32877 "missing %<=%> (after %<getter%> attribute)");
32878 else
32879 cp_parser_error (parser,
32880 "missing %<=%> (after %<setter%> attribute)");
32881 syntax_error = true;
32882 break;
32883 }
32884 cp_lexer_consume_token (parser->lexer); /* eat the = */
32885 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
32886 {
32887 cp_parser_error (parser, "expected identifier");
32888 syntax_error = true;
32889 break;
32890 }
32891 if (keyword == RID_SETTER)
32892 {
32893 if (property_setter_ident != NULL_TREE)
32894 {
32895 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
32896 cp_lexer_consume_token (parser->lexer);
32897 }
32898 else
32899 property_setter_ident = cp_parser_objc_selector (parser);
32900 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
32901 cp_parser_error (parser, "setter name must terminate with %<:%>");
32902 else
32903 cp_lexer_consume_token (parser->lexer);
32904 }
32905 else
32906 {
32907 if (property_getter_ident != NULL_TREE)
32908 {
32909 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
32910 cp_lexer_consume_token (parser->lexer);
32911 }
32912 else
32913 property_getter_ident = cp_parser_objc_selector (parser);
32914 }
32915 break;
32916 default:
32917 cp_parser_error (parser, "unknown property attribute");
32918 syntax_error = true;
32919 break;
32920 }
32921
32922 if (syntax_error)
32923 break;
32924
32925 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32926 cp_lexer_consume_token (parser->lexer);
32927 else
32928 break;
32929 }
32930
32931 /* FIXME: "@property (setter, assign);" will generate a spurious
32932 "error: expected ‘)’ before ‘,’ token". This is because
32933 cp_parser_require, unlike the C counterpart, will produce an
32934 error even if we are in error recovery. */
32935 if (!parens.require_close (parser))
32936 {
32937 cp_parser_skip_to_closing_parenthesis (parser,
32938 /*recovering=*/true,
32939 /*or_comma=*/false,
32940 /*consume_paren=*/true);
32941 }
32942 }
32943
32944 /* ... and the property declaration(s). */
32945 properties = cp_parser_objc_struct_declaration (parser);
32946
32947 if (properties == error_mark_node)
32948 {
32949 cp_parser_skip_to_end_of_statement (parser);
32950 /* If the next token is now a `;', consume it. */
32951 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32952 cp_lexer_consume_token (parser->lexer);
32953 return;
32954 }
32955
32956 if (properties == NULL_TREE)
32957 cp_parser_error (parser, "expected identifier");
32958 else
32959 {
32960 /* Comma-separated properties are chained together in
32961 reverse order; add them one by one. */
32962 properties = nreverse (properties);
32963
32964 for (; properties; properties = TREE_CHAIN (properties))
32965 objc_add_property_declaration (loc, copy_node (properties),
32966 property_readonly, property_readwrite,
32967 property_assign, property_retain,
32968 property_copy, property_nonatomic,
32969 property_getter_ident, property_setter_ident);
32970 }
32971
32972 cp_parser_consume_semicolon_at_end_of_statement (parser);
32973 }
32974
32975 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
32976
32977 objc-synthesize-declaration:
32978 @synthesize objc-synthesize-identifier-list ;
32979
32980 objc-synthesize-identifier-list:
32981 objc-synthesize-identifier
32982 objc-synthesize-identifier-list, objc-synthesize-identifier
32983
32984 objc-synthesize-identifier
32985 identifier
32986 identifier = identifier
32987
32988 For example:
32989 @synthesize MyProperty;
32990 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
32991
32992 PS: This function is identical to c_parser_objc_at_synthesize_declaration
32993 for C. Keep them in sync.
32994 */
32995 static void
32996 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
32997 {
32998 tree list = NULL_TREE;
32999 location_t loc;
33000 loc = cp_lexer_peek_token (parser->lexer)->location;
33001
33002 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
33003 while (true)
33004 {
33005 tree property, ivar;
33006 property = cp_parser_identifier (parser);
33007 if (property == error_mark_node)
33008 {
33009 cp_parser_consume_semicolon_at_end_of_statement (parser);
33010 return;
33011 }
33012 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
33013 {
33014 cp_lexer_consume_token (parser->lexer);
33015 ivar = cp_parser_identifier (parser);
33016 if (ivar == error_mark_node)
33017 {
33018 cp_parser_consume_semicolon_at_end_of_statement (parser);
33019 return;
33020 }
33021 }
33022 else
33023 ivar = NULL_TREE;
33024 list = chainon (list, build_tree_list (ivar, property));
33025 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33026 cp_lexer_consume_token (parser->lexer);
33027 else
33028 break;
33029 }
33030 cp_parser_consume_semicolon_at_end_of_statement (parser);
33031 objc_add_synthesize_declaration (loc, list);
33032 }
33033
33034 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
33035
33036 objc-dynamic-declaration:
33037 @dynamic identifier-list ;
33038
33039 For example:
33040 @dynamic MyProperty;
33041 @dynamic MyProperty, AnotherProperty;
33042
33043 PS: This function is identical to c_parser_objc_at_dynamic_declaration
33044 for C. Keep them in sync.
33045 */
33046 static void
33047 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
33048 {
33049 tree list = NULL_TREE;
33050 location_t loc;
33051 loc = cp_lexer_peek_token (parser->lexer)->location;
33052
33053 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
33054 while (true)
33055 {
33056 tree property;
33057 property = cp_parser_identifier (parser);
33058 if (property == error_mark_node)
33059 {
33060 cp_parser_consume_semicolon_at_end_of_statement (parser);
33061 return;
33062 }
33063 list = chainon (list, build_tree_list (NULL, property));
33064 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33065 cp_lexer_consume_token (parser->lexer);
33066 else
33067 break;
33068 }
33069 cp_parser_consume_semicolon_at_end_of_statement (parser);
33070 objc_add_dynamic_declaration (loc, list);
33071 }
33072
33073 \f
33074 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
33075
33076 /* Returns name of the next clause.
33077 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
33078 the token is not consumed. Otherwise appropriate pragma_omp_clause is
33079 returned and the token is consumed. */
33080
33081 static pragma_omp_clause
33082 cp_parser_omp_clause_name (cp_parser *parser)
33083 {
33084 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
33085
33086 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33087 result = PRAGMA_OACC_CLAUSE_AUTO;
33088 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
33089 result = PRAGMA_OMP_CLAUSE_IF;
33090 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
33091 result = PRAGMA_OMP_CLAUSE_DEFAULT;
33092 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
33093 result = PRAGMA_OACC_CLAUSE_DELETE;
33094 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
33095 result = PRAGMA_OMP_CLAUSE_PRIVATE;
33096 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33097 result = PRAGMA_OMP_CLAUSE_FOR;
33098 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33099 {
33100 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33101 const char *p = IDENTIFIER_POINTER (id);
33102
33103 switch (p[0])
33104 {
33105 case 'a':
33106 if (!strcmp ("aligned", p))
33107 result = PRAGMA_OMP_CLAUSE_ALIGNED;
33108 else if (!strcmp ("async", p))
33109 result = PRAGMA_OACC_CLAUSE_ASYNC;
33110 break;
33111 case 'b':
33112 if (!strcmp ("bind", p))
33113 result = PRAGMA_OMP_CLAUSE_BIND;
33114 break;
33115 case 'c':
33116 if (!strcmp ("collapse", p))
33117 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
33118 else if (!strcmp ("copy", p))
33119 result = PRAGMA_OACC_CLAUSE_COPY;
33120 else if (!strcmp ("copyin", p))
33121 result = PRAGMA_OMP_CLAUSE_COPYIN;
33122 else if (!strcmp ("copyout", p))
33123 result = PRAGMA_OACC_CLAUSE_COPYOUT;
33124 else if (!strcmp ("copyprivate", p))
33125 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
33126 else if (!strcmp ("create", p))
33127 result = PRAGMA_OACC_CLAUSE_CREATE;
33128 break;
33129 case 'd':
33130 if (!strcmp ("defaultmap", p))
33131 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
33132 else if (!strcmp ("depend", p))
33133 result = PRAGMA_OMP_CLAUSE_DEPEND;
33134 else if (!strcmp ("device", p))
33135 result = PRAGMA_OMP_CLAUSE_DEVICE;
33136 else if (!strcmp ("deviceptr", p))
33137 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
33138 else if (!strcmp ("device_resident", p))
33139 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
33140 else if (!strcmp ("device_type", p))
33141 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
33142 else if (!strcmp ("dist_schedule", p))
33143 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
33144 break;
33145 case 'f':
33146 if (!strcmp ("final", p))
33147 result = PRAGMA_OMP_CLAUSE_FINAL;
33148 else if (!strcmp ("finalize", p))
33149 result = PRAGMA_OACC_CLAUSE_FINALIZE;
33150 else if (!strcmp ("firstprivate", p))
33151 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
33152 else if (!strcmp ("from", p))
33153 result = PRAGMA_OMP_CLAUSE_FROM;
33154 break;
33155 case 'g':
33156 if (!strcmp ("gang", p))
33157 result = PRAGMA_OACC_CLAUSE_GANG;
33158 else if (!strcmp ("grainsize", p))
33159 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
33160 break;
33161 case 'h':
33162 if (!strcmp ("hint", p))
33163 result = PRAGMA_OMP_CLAUSE_HINT;
33164 else if (!strcmp ("host", p))
33165 result = PRAGMA_OACC_CLAUSE_HOST;
33166 break;
33167 case 'i':
33168 if (!strcmp ("if_present", p))
33169 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
33170 else if (!strcmp ("in_reduction", p))
33171 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
33172 else if (!strcmp ("inbranch", p))
33173 result = PRAGMA_OMP_CLAUSE_INBRANCH;
33174 else if (!strcmp ("independent", p))
33175 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
33176 else if (!strcmp ("is_device_ptr", p))
33177 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
33178 break;
33179 case 'l':
33180 if (!strcmp ("lastprivate", p))
33181 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
33182 else if (!strcmp ("linear", p))
33183 result = PRAGMA_OMP_CLAUSE_LINEAR;
33184 else if (!strcmp ("link", p))
33185 result = PRAGMA_OMP_CLAUSE_LINK;
33186 break;
33187 case 'm':
33188 if (!strcmp ("map", p))
33189 result = PRAGMA_OMP_CLAUSE_MAP;
33190 else if (!strcmp ("mergeable", p))
33191 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
33192 break;
33193 case 'n':
33194 if (!strcmp ("nogroup", p))
33195 result = PRAGMA_OMP_CLAUSE_NOGROUP;
33196 else if (!strcmp ("nontemporal", p))
33197 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
33198 else if (!strcmp ("notinbranch", p))
33199 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
33200 else if (!strcmp ("nowait", p))
33201 result = PRAGMA_OMP_CLAUSE_NOWAIT;
33202 else if (!strcmp ("num_gangs", p))
33203 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
33204 else if (!strcmp ("num_tasks", p))
33205 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
33206 else if (!strcmp ("num_teams", p))
33207 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
33208 else if (!strcmp ("num_threads", p))
33209 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
33210 else if (!strcmp ("num_workers", p))
33211 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
33212 break;
33213 case 'o':
33214 if (!strcmp ("ordered", p))
33215 result = PRAGMA_OMP_CLAUSE_ORDERED;
33216 else if (!strcmp ("order", p))
33217 result = PRAGMA_OMP_CLAUSE_ORDER;
33218 break;
33219 case 'p':
33220 if (!strcmp ("parallel", p))
33221 result = PRAGMA_OMP_CLAUSE_PARALLEL;
33222 else if (!strcmp ("present", p))
33223 result = PRAGMA_OACC_CLAUSE_PRESENT;
33224 else if (!strcmp ("present_or_copy", p)
33225 || !strcmp ("pcopy", p))
33226 result = PRAGMA_OACC_CLAUSE_COPY;
33227 else if (!strcmp ("present_or_copyin", p)
33228 || !strcmp ("pcopyin", p))
33229 result = PRAGMA_OACC_CLAUSE_COPYIN;
33230 else if (!strcmp ("present_or_copyout", p)
33231 || !strcmp ("pcopyout", p))
33232 result = PRAGMA_OACC_CLAUSE_COPYOUT;
33233 else if (!strcmp ("present_or_create", p)
33234 || !strcmp ("pcreate", p))
33235 result = PRAGMA_OACC_CLAUSE_CREATE;
33236 else if (!strcmp ("priority", p))
33237 result = PRAGMA_OMP_CLAUSE_PRIORITY;
33238 else if (!strcmp ("proc_bind", p))
33239 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
33240 break;
33241 case 'r':
33242 if (!strcmp ("reduction", p))
33243 result = PRAGMA_OMP_CLAUSE_REDUCTION;
33244 break;
33245 case 's':
33246 if (!strcmp ("safelen", p))
33247 result = PRAGMA_OMP_CLAUSE_SAFELEN;
33248 else if (!strcmp ("schedule", p))
33249 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
33250 else if (!strcmp ("sections", p))
33251 result = PRAGMA_OMP_CLAUSE_SECTIONS;
33252 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
33253 result = PRAGMA_OACC_CLAUSE_HOST;
33254 else if (!strcmp ("seq", p))
33255 result = PRAGMA_OACC_CLAUSE_SEQ;
33256 else if (!strcmp ("shared", p))
33257 result = PRAGMA_OMP_CLAUSE_SHARED;
33258 else if (!strcmp ("simd", p))
33259 result = PRAGMA_OMP_CLAUSE_SIMD;
33260 else if (!strcmp ("simdlen", p))
33261 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
33262 break;
33263 case 't':
33264 if (!strcmp ("task_reduction", p))
33265 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
33266 else if (!strcmp ("taskgroup", p))
33267 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
33268 else if (!strcmp ("thread_limit", p))
33269 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
33270 else if (!strcmp ("threads", p))
33271 result = PRAGMA_OMP_CLAUSE_THREADS;
33272 else if (!strcmp ("tile", p))
33273 result = PRAGMA_OACC_CLAUSE_TILE;
33274 else if (!strcmp ("to", p))
33275 result = PRAGMA_OMP_CLAUSE_TO;
33276 break;
33277 case 'u':
33278 if (!strcmp ("uniform", p))
33279 result = PRAGMA_OMP_CLAUSE_UNIFORM;
33280 else if (!strcmp ("untied", p))
33281 result = PRAGMA_OMP_CLAUSE_UNTIED;
33282 else if (!strcmp ("use_device", p))
33283 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
33284 else if (!strcmp ("use_device_addr", p))
33285 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
33286 else if (!strcmp ("use_device_ptr", p))
33287 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
33288 break;
33289 case 'v':
33290 if (!strcmp ("vector", p))
33291 result = PRAGMA_OACC_CLAUSE_VECTOR;
33292 else if (!strcmp ("vector_length", p))
33293 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
33294 break;
33295 case 'w':
33296 if (!strcmp ("wait", p))
33297 result = PRAGMA_OACC_CLAUSE_WAIT;
33298 else if (!strcmp ("worker", p))
33299 result = PRAGMA_OACC_CLAUSE_WORKER;
33300 break;
33301 }
33302 }
33303
33304 if (result != PRAGMA_OMP_CLAUSE_NONE)
33305 cp_lexer_consume_token (parser->lexer);
33306
33307 return result;
33308 }
33309
33310 /* Validate that a clause of the given type does not already exist. */
33311
33312 static void
33313 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
33314 const char *name, location_t location)
33315 {
33316 if (omp_find_clause (clauses, code))
33317 error_at (location, "too many %qs clauses", name);
33318 }
33319
33320 /* OpenMP 2.5:
33321 variable-list:
33322 identifier
33323 variable-list , identifier
33324
33325 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
33326 colon). An opening parenthesis will have been consumed by the caller.
33327
33328 If KIND is nonzero, create the appropriate node and install the decl
33329 in OMP_CLAUSE_DECL and add the node to the head of the list.
33330
33331 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
33332 return the list created.
33333
33334 COLON can be NULL if only closing parenthesis should end the list,
33335 or pointer to bool which will receive false if the list is terminated
33336 by closing parenthesis or true if the list is terminated by colon. */
33337
33338 static tree
33339 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
33340 tree list, bool *colon)
33341 {
33342 cp_token *token;
33343 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33344 if (colon)
33345 {
33346 parser->colon_corrects_to_scope_p = false;
33347 *colon = false;
33348 }
33349 while (1)
33350 {
33351 tree name, decl;
33352
33353 if (kind == OMP_CLAUSE_DEPEND)
33354 cp_parser_parse_tentatively (parser);
33355 token = cp_lexer_peek_token (parser->lexer);
33356 if (kind != 0
33357 && current_class_ptr
33358 && cp_parser_is_keyword (token, RID_THIS))
33359 {
33360 decl = finish_this_expr ();
33361 if (TREE_CODE (decl) == NON_LVALUE_EXPR
33362 || CONVERT_EXPR_P (decl))
33363 decl = TREE_OPERAND (decl, 0);
33364 cp_lexer_consume_token (parser->lexer);
33365 }
33366 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
33367 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
33368 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
33369 {
33370 cp_id_kind idk;
33371 decl = cp_parser_primary_expression (parser, false, false, false,
33372 &idk);
33373 }
33374 else
33375 {
33376 name = cp_parser_id_expression (parser, /*template_p=*/false,
33377 /*check_dependency_p=*/true,
33378 /*template_p=*/NULL,
33379 /*declarator_p=*/false,
33380 /*optional_p=*/false);
33381 if (name == error_mark_node)
33382 {
33383 if (kind == OMP_CLAUSE_DEPEND
33384 && cp_parser_simulate_error (parser))
33385 goto depend_lvalue;
33386 goto skip_comma;
33387 }
33388
33389 if (identifier_p (name))
33390 decl = cp_parser_lookup_name_simple (parser, name, token->location);
33391 else
33392 decl = name;
33393 if (decl == error_mark_node)
33394 {
33395 if (kind == OMP_CLAUSE_DEPEND
33396 && cp_parser_simulate_error (parser))
33397 goto depend_lvalue;
33398 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
33399 token->location);
33400 }
33401 }
33402 if (decl == error_mark_node)
33403 ;
33404 else if (kind != 0)
33405 {
33406 switch (kind)
33407 {
33408 case OMP_CLAUSE__CACHE_:
33409 /* The OpenACC cache directive explicitly only allows "array
33410 elements or subarrays". */
33411 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
33412 {
33413 error_at (token->location, "expected %<[%>");
33414 decl = error_mark_node;
33415 break;
33416 }
33417 /* FALLTHROUGH. */
33418 case OMP_CLAUSE_MAP:
33419 case OMP_CLAUSE_FROM:
33420 case OMP_CLAUSE_TO:
33421 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
33422 {
33423 location_t loc
33424 = cp_lexer_peek_token (parser->lexer)->location;
33425 cp_id_kind idk = CP_ID_KIND_NONE;
33426 cp_lexer_consume_token (parser->lexer);
33427 decl = convert_from_reference (decl);
33428 decl
33429 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
33430 decl, false,
33431 &idk, loc);
33432 }
33433 /* FALLTHROUGH. */
33434 case OMP_CLAUSE_DEPEND:
33435 case OMP_CLAUSE_REDUCTION:
33436 case OMP_CLAUSE_IN_REDUCTION:
33437 case OMP_CLAUSE_TASK_REDUCTION:
33438 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
33439 {
33440 tree low_bound = NULL_TREE, length = NULL_TREE;
33441
33442 parser->colon_corrects_to_scope_p = false;
33443 cp_lexer_consume_token (parser->lexer);
33444 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
33445 low_bound = cp_parser_expression (parser);
33446 if (!colon)
33447 parser->colon_corrects_to_scope_p
33448 = saved_colon_corrects_to_scope_p;
33449 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
33450 length = integer_one_node;
33451 else
33452 {
33453 /* Look for `:'. */
33454 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33455 {
33456 if (kind == OMP_CLAUSE_DEPEND
33457 && cp_parser_simulate_error (parser))
33458 goto depend_lvalue;
33459 goto skip_comma;
33460 }
33461 if (kind == OMP_CLAUSE_DEPEND)
33462 cp_parser_commit_to_tentative_parse (parser);
33463 if (!cp_lexer_next_token_is (parser->lexer,
33464 CPP_CLOSE_SQUARE))
33465 length = cp_parser_expression (parser);
33466 }
33467 /* Look for the closing `]'. */
33468 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
33469 RT_CLOSE_SQUARE))
33470 {
33471 if (kind == OMP_CLAUSE_DEPEND
33472 && cp_parser_simulate_error (parser))
33473 goto depend_lvalue;
33474 goto skip_comma;
33475 }
33476
33477 decl = tree_cons (low_bound, length, decl);
33478 }
33479 break;
33480 default:
33481 break;
33482 }
33483
33484 if (kind == OMP_CLAUSE_DEPEND)
33485 {
33486 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
33487 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
33488 && cp_parser_simulate_error (parser))
33489 {
33490 depend_lvalue:
33491 cp_parser_abort_tentative_parse (parser);
33492 decl = cp_parser_assignment_expression (parser, NULL,
33493 false, false);
33494 }
33495 else
33496 cp_parser_parse_definitely (parser);
33497 }
33498
33499 tree u = build_omp_clause (token->location, kind);
33500 OMP_CLAUSE_DECL (u) = decl;
33501 OMP_CLAUSE_CHAIN (u) = list;
33502 list = u;
33503 }
33504 else
33505 list = tree_cons (decl, NULL_TREE, list);
33506
33507 get_comma:
33508 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33509 break;
33510 cp_lexer_consume_token (parser->lexer);
33511 }
33512
33513 if (colon)
33514 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33515
33516 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
33517 {
33518 *colon = true;
33519 cp_parser_require (parser, CPP_COLON, RT_COLON);
33520 return list;
33521 }
33522
33523 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33524 {
33525 int ending;
33526
33527 /* Try to resync to an unnested comma. Copied from
33528 cp_parser_parenthesized_expression_list. */
33529 skip_comma:
33530 if (colon)
33531 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33532 ending = cp_parser_skip_to_closing_parenthesis (parser,
33533 /*recovering=*/true,
33534 /*or_comma=*/true,
33535 /*consume_paren=*/true);
33536 if (ending < 0)
33537 goto get_comma;
33538 }
33539
33540 return list;
33541 }
33542
33543 /* Similarly, but expect leading and trailing parenthesis. This is a very
33544 common case for omp clauses. */
33545
33546 static tree
33547 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
33548 {
33549 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33550 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
33551 return list;
33552 }
33553
33554 /* OpenACC 2.0:
33555 copy ( variable-list )
33556 copyin ( variable-list )
33557 copyout ( variable-list )
33558 create ( variable-list )
33559 delete ( variable-list )
33560 present ( variable-list ) */
33561
33562 static tree
33563 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
33564 tree list)
33565 {
33566 enum gomp_map_kind kind;
33567 switch (c_kind)
33568 {
33569 case PRAGMA_OACC_CLAUSE_COPY:
33570 kind = GOMP_MAP_TOFROM;
33571 break;
33572 case PRAGMA_OACC_CLAUSE_COPYIN:
33573 kind = GOMP_MAP_TO;
33574 break;
33575 case PRAGMA_OACC_CLAUSE_COPYOUT:
33576 kind = GOMP_MAP_FROM;
33577 break;
33578 case PRAGMA_OACC_CLAUSE_CREATE:
33579 kind = GOMP_MAP_ALLOC;
33580 break;
33581 case PRAGMA_OACC_CLAUSE_DELETE:
33582 kind = GOMP_MAP_RELEASE;
33583 break;
33584 case PRAGMA_OACC_CLAUSE_DEVICE:
33585 kind = GOMP_MAP_FORCE_TO;
33586 break;
33587 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33588 kind = GOMP_MAP_DEVICE_RESIDENT;
33589 break;
33590 case PRAGMA_OACC_CLAUSE_HOST:
33591 kind = GOMP_MAP_FORCE_FROM;
33592 break;
33593 case PRAGMA_OACC_CLAUSE_LINK:
33594 kind = GOMP_MAP_LINK;
33595 break;
33596 case PRAGMA_OACC_CLAUSE_PRESENT:
33597 kind = GOMP_MAP_FORCE_PRESENT;
33598 break;
33599 default:
33600 gcc_unreachable ();
33601 }
33602 tree nl, c;
33603 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
33604
33605 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
33606 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33607
33608 return nl;
33609 }
33610
33611 /* OpenACC 2.0:
33612 deviceptr ( variable-list ) */
33613
33614 static tree
33615 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
33616 {
33617 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33618 tree vars, t;
33619
33620 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
33621 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
33622 variable-list must only allow for pointer variables. */
33623 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33624 for (t = vars; t; t = TREE_CHAIN (t))
33625 {
33626 tree v = TREE_PURPOSE (t);
33627 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
33628 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
33629 OMP_CLAUSE_DECL (u) = v;
33630 OMP_CLAUSE_CHAIN (u) = list;
33631 list = u;
33632 }
33633
33634 return list;
33635 }
33636
33637 /* OpenACC 2.5:
33638 auto
33639 finalize
33640 independent
33641 nohost
33642 seq */
33643
33644 static tree
33645 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
33646 tree list)
33647 {
33648 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
33649
33650 tree c = build_omp_clause (loc, code);
33651 OMP_CLAUSE_CHAIN (c) = list;
33652
33653 return c;
33654 }
33655
33656 /* OpenACC:
33657 num_gangs ( expression )
33658 num_workers ( expression )
33659 vector_length ( expression ) */
33660
33661 static tree
33662 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
33663 const char *str, tree list)
33664 {
33665 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33666
33667 matching_parens parens;
33668 if (!parens.require_open (parser))
33669 return list;
33670
33671 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
33672
33673 if (t == error_mark_node
33674 || !parens.require_close (parser))
33675 {
33676 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33677 /*or_comma=*/false,
33678 /*consume_paren=*/true);
33679 return list;
33680 }
33681
33682 check_no_duplicate_clause (list, code, str, loc);
33683
33684 tree c = build_omp_clause (loc, code);
33685 OMP_CLAUSE_OPERAND (c, 0) = t;
33686 OMP_CLAUSE_CHAIN (c) = list;
33687 return c;
33688 }
33689
33690 /* OpenACC:
33691
33692 gang [( gang-arg-list )]
33693 worker [( [num:] int-expr )]
33694 vector [( [length:] int-expr )]
33695
33696 where gang-arg is one of:
33697
33698 [num:] int-expr
33699 static: size-expr
33700
33701 and size-expr may be:
33702
33703 *
33704 int-expr
33705 */
33706
33707 static tree
33708 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
33709 omp_clause_code kind,
33710 const char *str, tree list)
33711 {
33712 const char *id = "num";
33713 cp_lexer *lexer = parser->lexer;
33714 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
33715
33716 if (kind == OMP_CLAUSE_VECTOR)
33717 id = "length";
33718
33719 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
33720 {
33721 matching_parens parens;
33722 parens.consume_open (parser);
33723
33724 do
33725 {
33726 cp_token *next = cp_lexer_peek_token (lexer);
33727 int idx = 0;
33728
33729 /* Gang static argument. */
33730 if (kind == OMP_CLAUSE_GANG
33731 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
33732 {
33733 cp_lexer_consume_token (lexer);
33734
33735 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33736 goto cleanup_error;
33737
33738 idx = 1;
33739 if (ops[idx] != NULL)
33740 {
33741 cp_parser_error (parser, "too many %<static%> arguments");
33742 goto cleanup_error;
33743 }
33744
33745 /* Check for the '*' argument. */
33746 if (cp_lexer_next_token_is (lexer, CPP_MULT)
33747 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
33748 || cp_lexer_nth_token_is (parser->lexer, 2,
33749 CPP_CLOSE_PAREN)))
33750 {
33751 cp_lexer_consume_token (lexer);
33752 ops[idx] = integer_minus_one_node;
33753
33754 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
33755 {
33756 cp_lexer_consume_token (lexer);
33757 continue;
33758 }
33759 else break;
33760 }
33761 }
33762 /* Worker num: argument and vector length: arguments. */
33763 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
33764 && id_equal (next->u.value, id)
33765 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
33766 {
33767 cp_lexer_consume_token (lexer); /* id */
33768 cp_lexer_consume_token (lexer); /* ':' */
33769 }
33770
33771 /* Now collect the actual argument. */
33772 if (ops[idx] != NULL_TREE)
33773 {
33774 cp_parser_error (parser, "unexpected argument");
33775 goto cleanup_error;
33776 }
33777
33778 tree expr = cp_parser_assignment_expression (parser, NULL, false,
33779 false);
33780 if (expr == error_mark_node)
33781 goto cleanup_error;
33782
33783 mark_exp_read (expr);
33784 ops[idx] = expr;
33785
33786 if (kind == OMP_CLAUSE_GANG
33787 && cp_lexer_next_token_is (lexer, CPP_COMMA))
33788 {
33789 cp_lexer_consume_token (lexer);
33790 continue;
33791 }
33792 break;
33793 }
33794 while (1);
33795
33796 if (!parens.require_close (parser))
33797 goto cleanup_error;
33798 }
33799
33800 check_no_duplicate_clause (list, kind, str, loc);
33801
33802 c = build_omp_clause (loc, kind);
33803
33804 if (ops[1])
33805 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
33806
33807 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
33808 OMP_CLAUSE_CHAIN (c) = list;
33809
33810 return c;
33811
33812 cleanup_error:
33813 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33814 return list;
33815 }
33816
33817 /* OpenACC 2.0:
33818 tile ( size-expr-list ) */
33819
33820 static tree
33821 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
33822 {
33823 tree c, expr = error_mark_node;
33824 tree tile = NULL_TREE;
33825
33826 /* Collapse and tile are mutually exclusive. (The spec doesn't say
33827 so, but the spec authors never considered such a case and have
33828 differing opinions on what it might mean, including 'not
33829 allowed'.) */
33830 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
33831 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
33832 clause_loc);
33833
33834 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33835 return list;
33836
33837 do
33838 {
33839 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
33840 return list;
33841
33842 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
33843 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
33844 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
33845 {
33846 cp_lexer_consume_token (parser->lexer);
33847 expr = integer_zero_node;
33848 }
33849 else
33850 expr = cp_parser_constant_expression (parser);
33851
33852 tile = tree_cons (NULL_TREE, expr, tile);
33853 }
33854 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
33855
33856 /* Consume the trailing ')'. */
33857 cp_lexer_consume_token (parser->lexer);
33858
33859 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
33860 tile = nreverse (tile);
33861 OMP_CLAUSE_TILE_LIST (c) = tile;
33862 OMP_CLAUSE_CHAIN (c) = list;
33863 return c;
33864 }
33865
33866 /* OpenACC 2.0
33867 Parse wait clause or directive parameters. */
33868
33869 static tree
33870 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
33871 {
33872 vec<tree, va_gc> *args;
33873 tree t, args_tree;
33874
33875 args = cp_parser_parenthesized_expression_list (parser, non_attr,
33876 /*cast_p=*/false,
33877 /*allow_expansion_p=*/true,
33878 /*non_constant_p=*/NULL);
33879
33880 if (args == NULL || args->length () == 0)
33881 {
33882 if (args != NULL)
33883 {
33884 cp_parser_error (parser, "expected integer expression list");
33885 release_tree_vector (args);
33886 }
33887 return list;
33888 }
33889
33890 args_tree = build_tree_list_vec (args);
33891
33892 release_tree_vector (args);
33893
33894 for (t = args_tree; t; t = TREE_CHAIN (t))
33895 {
33896 tree targ = TREE_VALUE (t);
33897
33898 if (targ != error_mark_node)
33899 {
33900 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
33901 error ("%<wait%> expression must be integral");
33902 else
33903 {
33904 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
33905
33906 targ = mark_rvalue_use (targ);
33907 OMP_CLAUSE_DECL (c) = targ;
33908 OMP_CLAUSE_CHAIN (c) = list;
33909 list = c;
33910 }
33911 }
33912 }
33913
33914 return list;
33915 }
33916
33917 /* OpenACC:
33918 wait [( int-expr-list )] */
33919
33920 static tree
33921 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
33922 {
33923 location_t location = cp_lexer_peek_token (parser->lexer)->location;
33924
33925 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33926 list = cp_parser_oacc_wait_list (parser, location, list);
33927 else
33928 {
33929 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
33930
33931 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33932 OMP_CLAUSE_CHAIN (c) = list;
33933 list = c;
33934 }
33935
33936 return list;
33937 }
33938
33939 /* OpenMP 3.0:
33940 collapse ( constant-expression ) */
33941
33942 static tree
33943 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
33944 {
33945 tree c, num;
33946 location_t loc;
33947 HOST_WIDE_INT n;
33948
33949 loc = cp_lexer_peek_token (parser->lexer)->location;
33950 matching_parens parens;
33951 if (!parens.require_open (parser))
33952 return list;
33953
33954 num = cp_parser_constant_expression (parser);
33955
33956 if (!parens.require_close (parser))
33957 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33958 /*or_comma=*/false,
33959 /*consume_paren=*/true);
33960
33961 if (num == error_mark_node)
33962 return list;
33963 num = fold_non_dependent_expr (num);
33964 if (!tree_fits_shwi_p (num)
33965 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33966 || (n = tree_to_shwi (num)) <= 0
33967 || (int) n != n)
33968 {
33969 error_at (loc, "collapse argument needs positive constant integer expression");
33970 return list;
33971 }
33972
33973 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
33974 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
33975 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
33976 OMP_CLAUSE_CHAIN (c) = list;
33977 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
33978
33979 return c;
33980 }
33981
33982 /* OpenMP 2.5:
33983 default ( none | shared )
33984
33985 OpenACC:
33986 default ( none | present ) */
33987
33988 static tree
33989 cp_parser_omp_clause_default (cp_parser *parser, tree list,
33990 location_t location, bool is_oacc)
33991 {
33992 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
33993 tree c;
33994
33995 matching_parens parens;
33996 if (!parens.require_open (parser))
33997 return list;
33998 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33999 {
34000 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34001 const char *p = IDENTIFIER_POINTER (id);
34002
34003 switch (p[0])
34004 {
34005 case 'n':
34006 if (strcmp ("none", p) != 0)
34007 goto invalid_kind;
34008 kind = OMP_CLAUSE_DEFAULT_NONE;
34009 break;
34010
34011 case 'p':
34012 if (strcmp ("present", p) != 0 || !is_oacc)
34013 goto invalid_kind;
34014 kind = OMP_CLAUSE_DEFAULT_PRESENT;
34015 break;
34016
34017 case 's':
34018 if (strcmp ("shared", p) != 0 || is_oacc)
34019 goto invalid_kind;
34020 kind = OMP_CLAUSE_DEFAULT_SHARED;
34021 break;
34022
34023 default:
34024 goto invalid_kind;
34025 }
34026
34027 cp_lexer_consume_token (parser->lexer);
34028 }
34029 else
34030 {
34031 invalid_kind:
34032 if (is_oacc)
34033 cp_parser_error (parser, "expected %<none%> or %<present%>");
34034 else
34035 cp_parser_error (parser, "expected %<none%> or %<shared%>");
34036 }
34037
34038 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
34039 || !parens.require_close (parser))
34040 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34041 /*or_comma=*/false,
34042 /*consume_paren=*/true);
34043
34044 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
34045 return list;
34046
34047 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
34048 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
34049 OMP_CLAUSE_CHAIN (c) = list;
34050 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
34051
34052 return c;
34053 }
34054
34055 /* OpenMP 3.1:
34056 final ( expression ) */
34057
34058 static tree
34059 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
34060 {
34061 tree t, c;
34062
34063 matching_parens parens;
34064 if (!parens.require_open (parser))
34065 return list;
34066
34067 t = cp_parser_assignment_expression (parser);
34068
34069 if (t == error_mark_node
34070 || !parens.require_close (parser))
34071 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34072 /*or_comma=*/false,
34073 /*consume_paren=*/true);
34074
34075 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
34076
34077 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
34078 OMP_CLAUSE_FINAL_EXPR (c) = t;
34079 OMP_CLAUSE_CHAIN (c) = list;
34080
34081 return c;
34082 }
34083
34084 /* OpenMP 2.5:
34085 if ( expression )
34086
34087 OpenMP 4.5:
34088 if ( directive-name-modifier : expression )
34089
34090 directive-name-modifier:
34091 parallel | task | taskloop | target data | target | target update
34092 | target enter data | target exit data
34093
34094 OpenMP 5.0:
34095 directive-name-modifier:
34096 ... | simd | cancel */
34097
34098 static tree
34099 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
34100 bool is_omp)
34101 {
34102 tree t, c;
34103 enum tree_code if_modifier = ERROR_MARK;
34104
34105 matching_parens parens;
34106 if (!parens.require_open (parser))
34107 return list;
34108
34109 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34110 {
34111 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34112 const char *p = IDENTIFIER_POINTER (id);
34113 int n = 2;
34114
34115 if (strcmp ("cancel", p) == 0)
34116 if_modifier = VOID_CST;
34117 else if (strcmp ("parallel", p) == 0)
34118 if_modifier = OMP_PARALLEL;
34119 else if (strcmp ("simd", p) == 0)
34120 if_modifier = OMP_SIMD;
34121 else if (strcmp ("task", p) == 0)
34122 if_modifier = OMP_TASK;
34123 else if (strcmp ("taskloop", p) == 0)
34124 if_modifier = OMP_TASKLOOP;
34125 else if (strcmp ("target", p) == 0)
34126 {
34127 if_modifier = OMP_TARGET;
34128 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
34129 {
34130 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
34131 p = IDENTIFIER_POINTER (id);
34132 if (strcmp ("data", p) == 0)
34133 if_modifier = OMP_TARGET_DATA;
34134 else if (strcmp ("update", p) == 0)
34135 if_modifier = OMP_TARGET_UPDATE;
34136 else if (strcmp ("enter", p) == 0)
34137 if_modifier = OMP_TARGET_ENTER_DATA;
34138 else if (strcmp ("exit", p) == 0)
34139 if_modifier = OMP_TARGET_EXIT_DATA;
34140 if (if_modifier != OMP_TARGET)
34141 n = 3;
34142 else
34143 {
34144 location_t loc
34145 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
34146 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
34147 "or %<exit%>");
34148 if_modifier = ERROR_MARK;
34149 }
34150 if (if_modifier == OMP_TARGET_ENTER_DATA
34151 || if_modifier == OMP_TARGET_EXIT_DATA)
34152 {
34153 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
34154 {
34155 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
34156 p = IDENTIFIER_POINTER (id);
34157 if (strcmp ("data", p) == 0)
34158 n = 4;
34159 }
34160 if (n != 4)
34161 {
34162 location_t loc
34163 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
34164 error_at (loc, "expected %<data%>");
34165 if_modifier = ERROR_MARK;
34166 }
34167 }
34168 }
34169 }
34170 if (if_modifier != ERROR_MARK)
34171 {
34172 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
34173 {
34174 while (n-- > 0)
34175 cp_lexer_consume_token (parser->lexer);
34176 }
34177 else
34178 {
34179 if (n > 2)
34180 {
34181 location_t loc
34182 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
34183 error_at (loc, "expected %<:%>");
34184 }
34185 if_modifier = ERROR_MARK;
34186 }
34187 }
34188 }
34189
34190 t = cp_parser_assignment_expression (parser);
34191
34192 if (t == error_mark_node
34193 || !parens.require_close (parser))
34194 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34195 /*or_comma=*/false,
34196 /*consume_paren=*/true);
34197
34198 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
34199 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
34200 {
34201 if (if_modifier != ERROR_MARK
34202 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
34203 {
34204 const char *p = NULL;
34205 switch (if_modifier)
34206 {
34207 case VOID_CST: p = "cancel"; break;
34208 case OMP_PARALLEL: p = "parallel"; break;
34209 case OMP_SIMD: p = "simd"; break;
34210 case OMP_TASK: p = "task"; break;
34211 case OMP_TASKLOOP: p = "taskloop"; break;
34212 case OMP_TARGET_DATA: p = "target data"; break;
34213 case OMP_TARGET: p = "target"; break;
34214 case OMP_TARGET_UPDATE: p = "target update"; break;
34215 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
34216 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
34217 default: gcc_unreachable ();
34218 }
34219 error_at (location, "too many %<if%> clauses with %qs modifier",
34220 p);
34221 return list;
34222 }
34223 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
34224 {
34225 if (!is_omp)
34226 error_at (location, "too many %<if%> clauses");
34227 else
34228 error_at (location, "too many %<if%> clauses without modifier");
34229 return list;
34230 }
34231 else if (if_modifier == ERROR_MARK
34232 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
34233 {
34234 error_at (location, "if any %<if%> clause has modifier, then all "
34235 "%<if%> clauses have to use modifier");
34236 return list;
34237 }
34238 }
34239
34240 c = build_omp_clause (location, OMP_CLAUSE_IF);
34241 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
34242 OMP_CLAUSE_IF_EXPR (c) = t;
34243 OMP_CLAUSE_CHAIN (c) = list;
34244
34245 return c;
34246 }
34247
34248 /* OpenMP 3.1:
34249 mergeable */
34250
34251 static tree
34252 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
34253 tree list, location_t location)
34254 {
34255 tree c;
34256
34257 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
34258 location);
34259
34260 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
34261 OMP_CLAUSE_CHAIN (c) = list;
34262 return c;
34263 }
34264
34265 /* OpenMP 2.5:
34266 nowait */
34267
34268 static tree
34269 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
34270 tree list, location_t location)
34271 {
34272 tree c;
34273
34274 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
34275
34276 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
34277 OMP_CLAUSE_CHAIN (c) = list;
34278 return c;
34279 }
34280
34281 /* OpenMP 2.5:
34282 num_threads ( expression ) */
34283
34284 static tree
34285 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
34286 location_t location)
34287 {
34288 tree t, c;
34289
34290 matching_parens parens;
34291 if (!parens.require_open (parser))
34292 return list;
34293
34294 t = cp_parser_assignment_expression (parser);
34295
34296 if (t == error_mark_node
34297 || !parens.require_close (parser))
34298 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34299 /*or_comma=*/false,
34300 /*consume_paren=*/true);
34301
34302 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
34303 "num_threads", location);
34304
34305 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
34306 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
34307 OMP_CLAUSE_CHAIN (c) = list;
34308
34309 return c;
34310 }
34311
34312 /* OpenMP 4.5:
34313 num_tasks ( expression ) */
34314
34315 static tree
34316 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
34317 location_t location)
34318 {
34319 tree t, c;
34320
34321 matching_parens parens;
34322 if (!parens.require_open (parser))
34323 return list;
34324
34325 t = cp_parser_assignment_expression (parser);
34326
34327 if (t == error_mark_node
34328 || !parens.require_close (parser))
34329 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34330 /*or_comma=*/false,
34331 /*consume_paren=*/true);
34332
34333 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
34334 "num_tasks", location);
34335
34336 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
34337 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
34338 OMP_CLAUSE_CHAIN (c) = list;
34339
34340 return c;
34341 }
34342
34343 /* OpenMP 4.5:
34344 grainsize ( expression ) */
34345
34346 static tree
34347 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
34348 location_t location)
34349 {
34350 tree t, c;
34351
34352 matching_parens parens;
34353 if (!parens.require_open (parser))
34354 return list;
34355
34356 t = cp_parser_assignment_expression (parser);
34357
34358 if (t == error_mark_node
34359 || !parens.require_close (parser))
34360 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34361 /*or_comma=*/false,
34362 /*consume_paren=*/true);
34363
34364 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
34365 "grainsize", location);
34366
34367 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
34368 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
34369 OMP_CLAUSE_CHAIN (c) = list;
34370
34371 return c;
34372 }
34373
34374 /* OpenMP 4.5:
34375 priority ( expression ) */
34376
34377 static tree
34378 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
34379 location_t location)
34380 {
34381 tree t, c;
34382
34383 matching_parens parens;
34384 if (!parens.require_open (parser))
34385 return list;
34386
34387 t = cp_parser_assignment_expression (parser);
34388
34389 if (t == error_mark_node
34390 || !parens.require_close (parser))
34391 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34392 /*or_comma=*/false,
34393 /*consume_paren=*/true);
34394
34395 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
34396 "priority", location);
34397
34398 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
34399 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
34400 OMP_CLAUSE_CHAIN (c) = list;
34401
34402 return c;
34403 }
34404
34405 /* OpenMP 4.5:
34406 hint ( expression ) */
34407
34408 static tree
34409 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
34410 {
34411 tree t, c;
34412
34413 matching_parens parens;
34414 if (!parens.require_open (parser))
34415 return list;
34416
34417 t = cp_parser_assignment_expression (parser);
34418
34419 if (t == error_mark_node
34420 || !parens.require_close (parser))
34421 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34422 /*or_comma=*/false,
34423 /*consume_paren=*/true);
34424
34425 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
34426
34427 c = build_omp_clause (location, OMP_CLAUSE_HINT);
34428 OMP_CLAUSE_HINT_EXPR (c) = t;
34429 OMP_CLAUSE_CHAIN (c) = list;
34430
34431 return c;
34432 }
34433
34434 /* OpenMP 4.5:
34435 defaultmap ( tofrom : scalar )
34436
34437 OpenMP 5.0:
34438 defaultmap ( implicit-behavior [ : variable-category ] ) */
34439
34440 static tree
34441 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
34442 location_t location)
34443 {
34444 tree c, id;
34445 const char *p;
34446 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
34447 enum omp_clause_defaultmap_kind category
34448 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
34449
34450 matching_parens parens;
34451 if (!parens.require_open (parser))
34452 return list;
34453
34454 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
34455 p = "default";
34456 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34457 {
34458 invalid_behavior:
34459 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
34460 "%<tofrom%>, %<firstprivate%>, %<none%> "
34461 "or %<default%>");
34462 goto out_err;
34463 }
34464 else
34465 {
34466 id = cp_lexer_peek_token (parser->lexer)->u.value;
34467 p = IDENTIFIER_POINTER (id);
34468 }
34469
34470 switch (p[0])
34471 {
34472 case 'a':
34473 if (strcmp ("alloc", p) == 0)
34474 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
34475 else
34476 goto invalid_behavior;
34477 break;
34478
34479 case 'd':
34480 if (strcmp ("default", p) == 0)
34481 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
34482 else
34483 goto invalid_behavior;
34484 break;
34485
34486 case 'f':
34487 if (strcmp ("firstprivate", p) == 0)
34488 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
34489 else if (strcmp ("from", p) == 0)
34490 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
34491 else
34492 goto invalid_behavior;
34493 break;
34494
34495 case 'n':
34496 if (strcmp ("none", p) == 0)
34497 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
34498 else
34499 goto invalid_behavior;
34500 break;
34501
34502 case 't':
34503 if (strcmp ("tofrom", p) == 0)
34504 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
34505 else if (strcmp ("to", p) == 0)
34506 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
34507 else
34508 goto invalid_behavior;
34509 break;
34510
34511 default:
34512 goto invalid_behavior;
34513 }
34514 cp_lexer_consume_token (parser->lexer);
34515
34516 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34517 {
34518 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34519 goto out_err;
34520
34521 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34522 {
34523 invalid_category:
34524 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
34525 "%<pointer%>");
34526 goto out_err;
34527 }
34528 id = cp_lexer_peek_token (parser->lexer)->u.value;
34529 p = IDENTIFIER_POINTER (id);
34530
34531 switch (p[0])
34532 {
34533 case 'a':
34534 if (strcmp ("aggregate", p) == 0)
34535 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
34536 else
34537 goto invalid_category;
34538 break;
34539
34540 case 'p':
34541 if (strcmp ("pointer", p) == 0)
34542 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
34543 else
34544 goto invalid_category;
34545 break;
34546
34547 case 's':
34548 if (strcmp ("scalar", p) == 0)
34549 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
34550 else
34551 goto invalid_category;
34552 break;
34553
34554 default:
34555 goto invalid_category;
34556 }
34557
34558 cp_lexer_consume_token (parser->lexer);
34559 }
34560 if (!parens.require_close (parser))
34561 goto out_err;
34562
34563 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
34564 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
34565 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
34566 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
34567 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
34568 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
34569 {
34570 enum omp_clause_defaultmap_kind cat = category;
34571 location_t loc = OMP_CLAUSE_LOCATION (c);
34572 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
34573 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
34574 p = NULL;
34575 switch (cat)
34576 {
34577 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
34578 p = NULL;
34579 break;
34580 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
34581 p = "aggregate";
34582 break;
34583 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
34584 p = "pointer";
34585 break;
34586 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
34587 p = "scalar";
34588 break;
34589 default:
34590 gcc_unreachable ();
34591 }
34592 if (p)
34593 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
34594 p);
34595 else
34596 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
34597 "category");
34598 break;
34599 }
34600
34601 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
34602 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
34603 OMP_CLAUSE_CHAIN (c) = list;
34604 return c;
34605
34606 out_err:
34607 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34608 /*or_comma=*/false,
34609 /*consume_paren=*/true);
34610 return list;
34611 }
34612
34613 /* OpenMP 5.0:
34614 order ( concurrent ) */
34615
34616 static tree
34617 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
34618 {
34619 tree c, id;
34620 const char *p;
34621
34622 matching_parens parens;
34623 if (!parens.require_open (parser))
34624 return list;
34625
34626 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34627 {
34628 cp_parser_error (parser, "expected %<concurrent%>");
34629 goto out_err;
34630 }
34631 else
34632 {
34633 id = cp_lexer_peek_token (parser->lexer)->u.value;
34634 p = IDENTIFIER_POINTER (id);
34635 }
34636 if (strcmp (p, "concurrent") != 0)
34637 {
34638 cp_parser_error (parser, "expected %<concurrent%>");
34639 goto out_err;
34640 }
34641 cp_lexer_consume_token (parser->lexer);
34642 if (!parens.require_close (parser))
34643 goto out_err;
34644
34645 /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location); */
34646 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
34647 OMP_CLAUSE_CHAIN (c) = list;
34648 return c;
34649
34650 out_err:
34651 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34652 /*or_comma=*/false,
34653 /*consume_paren=*/true);
34654 return list;
34655 }
34656
34657 /* OpenMP 5.0:
34658 bind ( teams | parallel | thread ) */
34659
34660 static tree
34661 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
34662 location_t location)
34663 {
34664 tree c;
34665 const char *p;
34666 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
34667
34668 matching_parens parens;
34669 if (!parens.require_open (parser))
34670 return list;
34671
34672 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34673 {
34674 invalid:
34675 cp_parser_error (parser,
34676 "expected %<teams%>, %<parallel%> or %<thread%>");
34677 goto out_err;
34678 }
34679 else
34680 {
34681 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34682 p = IDENTIFIER_POINTER (id);
34683 }
34684 if (strcmp (p, "teams") == 0)
34685 kind = OMP_CLAUSE_BIND_TEAMS;
34686 else if (strcmp (p, "parallel") == 0)
34687 kind = OMP_CLAUSE_BIND_PARALLEL;
34688 else if (strcmp (p, "thread") != 0)
34689 goto invalid;
34690 cp_lexer_consume_token (parser->lexer);
34691 if (!parens.require_close (parser))
34692 goto out_err;
34693
34694 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
34695 c = build_omp_clause (location, OMP_CLAUSE_BIND);
34696 OMP_CLAUSE_BIND_KIND (c) = kind;
34697 OMP_CLAUSE_CHAIN (c) = list;
34698 return c;
34699
34700 out_err:
34701 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34702 /*or_comma=*/false,
34703 /*consume_paren=*/true);
34704 return list;
34705 }
34706
34707 /* OpenMP 2.5:
34708 ordered
34709
34710 OpenMP 4.5:
34711 ordered ( constant-expression ) */
34712
34713 static tree
34714 cp_parser_omp_clause_ordered (cp_parser *parser,
34715 tree list, location_t location)
34716 {
34717 tree c, num = NULL_TREE;
34718 HOST_WIDE_INT n;
34719
34720 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
34721 "ordered", location);
34722
34723 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34724 {
34725 matching_parens parens;
34726 parens.consume_open (parser);
34727
34728 num = cp_parser_constant_expression (parser);
34729
34730 if (!parens.require_close (parser))
34731 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34732 /*or_comma=*/false,
34733 /*consume_paren=*/true);
34734
34735 if (num == error_mark_node)
34736 return list;
34737 num = fold_non_dependent_expr (num);
34738 if (!tree_fits_shwi_p (num)
34739 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
34740 || (n = tree_to_shwi (num)) <= 0
34741 || (int) n != n)
34742 {
34743 error_at (location,
34744 "ordered argument needs positive constant integer "
34745 "expression");
34746 return list;
34747 }
34748 }
34749
34750 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
34751 OMP_CLAUSE_ORDERED_EXPR (c) = num;
34752 OMP_CLAUSE_CHAIN (c) = list;
34753 return c;
34754 }
34755
34756 /* OpenMP 2.5:
34757 reduction ( reduction-operator : variable-list )
34758
34759 reduction-operator:
34760 One of: + * - & ^ | && ||
34761
34762 OpenMP 3.1:
34763
34764 reduction-operator:
34765 One of: + * - & ^ | && || min max
34766
34767 OpenMP 4.0:
34768
34769 reduction-operator:
34770 One of: + * - & ^ | && ||
34771 id-expression
34772
34773 OpenMP 5.0:
34774 reduction ( reduction-modifier, reduction-operator : variable-list )
34775 in_reduction ( reduction-operator : variable-list )
34776 task_reduction ( reduction-operator : variable-list ) */
34777
34778 static tree
34779 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
34780 bool is_omp, tree list)
34781 {
34782 enum tree_code code = ERROR_MARK;
34783 tree nlist, c, id = NULL_TREE;
34784 bool task = false;
34785 bool inscan = false;
34786
34787 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34788 return list;
34789
34790 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
34791 {
34792 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
34793 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
34794 {
34795 cp_lexer_consume_token (parser->lexer);
34796 cp_lexer_consume_token (parser->lexer);
34797 }
34798 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34799 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
34800 {
34801 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34802 const char *p = IDENTIFIER_POINTER (id);
34803 if (strcmp (p, "task") == 0)
34804 task = true;
34805 else if (strcmp (p, "inscan") == 0)
34806 inscan = true;
34807 if (task || inscan)
34808 {
34809 cp_lexer_consume_token (parser->lexer);
34810 cp_lexer_consume_token (parser->lexer);
34811 }
34812 }
34813 }
34814
34815 switch (cp_lexer_peek_token (parser->lexer)->type)
34816 {
34817 case CPP_PLUS: code = PLUS_EXPR; break;
34818 case CPP_MULT: code = MULT_EXPR; break;
34819 case CPP_MINUS: code = MINUS_EXPR; break;
34820 case CPP_AND: code = BIT_AND_EXPR; break;
34821 case CPP_XOR: code = BIT_XOR_EXPR; break;
34822 case CPP_OR: code = BIT_IOR_EXPR; break;
34823 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
34824 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
34825 default: break;
34826 }
34827
34828 if (code != ERROR_MARK)
34829 cp_lexer_consume_token (parser->lexer);
34830 else
34831 {
34832 bool saved_colon_corrects_to_scope_p;
34833 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
34834 parser->colon_corrects_to_scope_p = false;
34835 id = cp_parser_id_expression (parser, /*template_p=*/false,
34836 /*check_dependency_p=*/true,
34837 /*template_p=*/NULL,
34838 /*declarator_p=*/false,
34839 /*optional_p=*/false);
34840 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34841 if (identifier_p (id))
34842 {
34843 const char *p = IDENTIFIER_POINTER (id);
34844
34845 if (strcmp (p, "min") == 0)
34846 code = MIN_EXPR;
34847 else if (strcmp (p, "max") == 0)
34848 code = MAX_EXPR;
34849 else if (id == ovl_op_identifier (false, PLUS_EXPR))
34850 code = PLUS_EXPR;
34851 else if (id == ovl_op_identifier (false, MULT_EXPR))
34852 code = MULT_EXPR;
34853 else if (id == ovl_op_identifier (false, MINUS_EXPR))
34854 code = MINUS_EXPR;
34855 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
34856 code = BIT_AND_EXPR;
34857 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
34858 code = BIT_IOR_EXPR;
34859 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
34860 code = BIT_XOR_EXPR;
34861 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
34862 code = TRUTH_ANDIF_EXPR;
34863 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
34864 code = TRUTH_ORIF_EXPR;
34865 id = omp_reduction_id (code, id, NULL_TREE);
34866 tree scope = parser->scope;
34867 if (scope)
34868 id = build_qualified_name (NULL_TREE, scope, id, false);
34869 parser->scope = NULL_TREE;
34870 parser->qualifying_scope = NULL_TREE;
34871 parser->object_scope = NULL_TREE;
34872 }
34873 else
34874 {
34875 error ("invalid reduction-identifier");
34876 resync_fail:
34877 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34878 /*or_comma=*/false,
34879 /*consume_paren=*/true);
34880 return list;
34881 }
34882 }
34883
34884 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34885 goto resync_fail;
34886
34887 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
34888 NULL);
34889 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34890 {
34891 OMP_CLAUSE_REDUCTION_CODE (c) = code;
34892 if (task)
34893 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
34894 else if (inscan)
34895 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
34896 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
34897 }
34898
34899 return nlist;
34900 }
34901
34902 /* OpenMP 2.5:
34903 schedule ( schedule-kind )
34904 schedule ( schedule-kind , expression )
34905
34906 schedule-kind:
34907 static | dynamic | guided | runtime | auto
34908
34909 OpenMP 4.5:
34910 schedule ( schedule-modifier : schedule-kind )
34911 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
34912
34913 schedule-modifier:
34914 simd
34915 monotonic
34916 nonmonotonic */
34917
34918 static tree
34919 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
34920 {
34921 tree c, t;
34922 int modifiers = 0, nmodifiers = 0;
34923
34924 matching_parens parens;
34925 if (!parens.require_open (parser))
34926 return list;
34927
34928 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
34929
34930 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34931 {
34932 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34933 const char *p = IDENTIFIER_POINTER (id);
34934 if (strcmp ("simd", p) == 0)
34935 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
34936 else if (strcmp ("monotonic", p) == 0)
34937 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
34938 else if (strcmp ("nonmonotonic", p) == 0)
34939 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
34940 else
34941 break;
34942 cp_lexer_consume_token (parser->lexer);
34943 if (nmodifiers++ == 0
34944 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34945 cp_lexer_consume_token (parser->lexer);
34946 else
34947 {
34948 cp_parser_require (parser, CPP_COLON, RT_COLON);
34949 break;
34950 }
34951 }
34952
34953 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34954 {
34955 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34956 const char *p = IDENTIFIER_POINTER (id);
34957
34958 switch (p[0])
34959 {
34960 case 'd':
34961 if (strcmp ("dynamic", p) != 0)
34962 goto invalid_kind;
34963 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
34964 break;
34965
34966 case 'g':
34967 if (strcmp ("guided", p) != 0)
34968 goto invalid_kind;
34969 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
34970 break;
34971
34972 case 'r':
34973 if (strcmp ("runtime", p) != 0)
34974 goto invalid_kind;
34975 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
34976 break;
34977
34978 default:
34979 goto invalid_kind;
34980 }
34981 }
34982 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34983 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
34984 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
34985 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
34986 else
34987 goto invalid_kind;
34988 cp_lexer_consume_token (parser->lexer);
34989
34990 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
34991 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
34992 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
34993 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
34994 {
34995 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
34996 "specified");
34997 modifiers = 0;
34998 }
34999
35000 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35001 {
35002 cp_token *token;
35003 cp_lexer_consume_token (parser->lexer);
35004
35005 token = cp_lexer_peek_token (parser->lexer);
35006 t = cp_parser_assignment_expression (parser);
35007
35008 if (t == error_mark_node)
35009 goto resync_fail;
35010 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
35011 error_at (token->location, "schedule %<runtime%> does not take "
35012 "a %<chunk_size%> parameter");
35013 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
35014 error_at (token->location, "schedule %<auto%> does not take "
35015 "a %<chunk_size%> parameter");
35016 else
35017 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
35018
35019 if (!parens.require_close (parser))
35020 goto resync_fail;
35021 }
35022 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
35023 goto resync_fail;
35024
35025 OMP_CLAUSE_SCHEDULE_KIND (c)
35026 = (enum omp_clause_schedule_kind)
35027 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
35028
35029 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
35030 OMP_CLAUSE_CHAIN (c) = list;
35031 return c;
35032
35033 invalid_kind:
35034 cp_parser_error (parser, "invalid schedule kind");
35035 resync_fail:
35036 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35037 /*or_comma=*/false,
35038 /*consume_paren=*/true);
35039 return list;
35040 }
35041
35042 /* OpenMP 3.0:
35043 untied */
35044
35045 static tree
35046 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
35047 tree list, location_t location)
35048 {
35049 tree c;
35050
35051 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
35052
35053 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
35054 OMP_CLAUSE_CHAIN (c) = list;
35055 return c;
35056 }
35057
35058 /* OpenMP 4.0:
35059 inbranch
35060 notinbranch */
35061
35062 static tree
35063 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
35064 tree list, location_t location)
35065 {
35066 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
35067 tree c = build_omp_clause (location, code);
35068 OMP_CLAUSE_CHAIN (c) = list;
35069 return c;
35070 }
35071
35072 /* OpenMP 4.0:
35073 parallel
35074 for
35075 sections
35076 taskgroup */
35077
35078 static tree
35079 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
35080 enum omp_clause_code code,
35081 tree list, location_t location)
35082 {
35083 tree c = build_omp_clause (location, code);
35084 OMP_CLAUSE_CHAIN (c) = list;
35085 return c;
35086 }
35087
35088 /* OpenMP 4.5:
35089 nogroup */
35090
35091 static tree
35092 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
35093 tree list, location_t location)
35094 {
35095 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
35096 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
35097 OMP_CLAUSE_CHAIN (c) = list;
35098 return c;
35099 }
35100
35101 /* OpenMP 4.5:
35102 simd
35103 threads */
35104
35105 static tree
35106 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
35107 enum omp_clause_code code,
35108 tree list, location_t location)
35109 {
35110 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
35111 tree c = build_omp_clause (location, code);
35112 OMP_CLAUSE_CHAIN (c) = list;
35113 return c;
35114 }
35115
35116 /* OpenMP 4.0:
35117 num_teams ( expression ) */
35118
35119 static tree
35120 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
35121 location_t location)
35122 {
35123 tree t, c;
35124
35125 matching_parens parens;
35126 if (!parens.require_open (parser))
35127 return list;
35128
35129 t = cp_parser_assignment_expression (parser);
35130
35131 if (t == error_mark_node
35132 || !parens.require_close (parser))
35133 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35134 /*or_comma=*/false,
35135 /*consume_paren=*/true);
35136
35137 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
35138 "num_teams", location);
35139
35140 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
35141 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
35142 OMP_CLAUSE_CHAIN (c) = list;
35143
35144 return c;
35145 }
35146
35147 /* OpenMP 4.0:
35148 thread_limit ( expression ) */
35149
35150 static tree
35151 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
35152 location_t location)
35153 {
35154 tree t, c;
35155
35156 matching_parens parens;
35157 if (!parens.require_open (parser))
35158 return list;
35159
35160 t = cp_parser_assignment_expression (parser);
35161
35162 if (t == error_mark_node
35163 || !parens.require_close (parser))
35164 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35165 /*or_comma=*/false,
35166 /*consume_paren=*/true);
35167
35168 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
35169 "thread_limit", location);
35170
35171 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
35172 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
35173 OMP_CLAUSE_CHAIN (c) = list;
35174
35175 return c;
35176 }
35177
35178 /* OpenMP 4.0:
35179 aligned ( variable-list )
35180 aligned ( variable-list : constant-expression ) */
35181
35182 static tree
35183 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
35184 {
35185 tree nlist, c, alignment = NULL_TREE;
35186 bool colon;
35187
35188 matching_parens parens;
35189 if (!parens.require_open (parser))
35190 return list;
35191
35192 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
35193 &colon);
35194
35195 if (colon)
35196 {
35197 alignment = cp_parser_constant_expression (parser);
35198
35199 if (!parens.require_close (parser))
35200 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35201 /*or_comma=*/false,
35202 /*consume_paren=*/true);
35203
35204 if (alignment == error_mark_node)
35205 alignment = NULL_TREE;
35206 }
35207
35208 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
35209 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
35210
35211 return nlist;
35212 }
35213
35214 /* OpenMP 2.5:
35215 lastprivate ( variable-list )
35216
35217 OpenMP 5.0:
35218 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
35219
35220 static tree
35221 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
35222 {
35223 bool conditional = false;
35224
35225 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35226 return list;
35227
35228 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
35229 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
35230 {
35231 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35232 const char *p = IDENTIFIER_POINTER (id);
35233
35234 if (strcmp ("conditional", p) == 0)
35235 {
35236 conditional = true;
35237 cp_lexer_consume_token (parser->lexer);
35238 cp_lexer_consume_token (parser->lexer);
35239 }
35240 }
35241
35242 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
35243 list, NULL);
35244
35245 if (conditional)
35246 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
35247 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
35248 return nlist;
35249 }
35250
35251 /* OpenMP 4.0:
35252 linear ( variable-list )
35253 linear ( variable-list : expression )
35254
35255 OpenMP 4.5:
35256 linear ( modifier ( variable-list ) )
35257 linear ( modifier ( variable-list ) : expression ) */
35258
35259 static tree
35260 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
35261 bool declare_simd)
35262 {
35263 tree nlist, c, step = integer_one_node;
35264 bool colon;
35265 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
35266
35267 matching_parens parens;
35268 if (!parens.require_open (parser))
35269 return list;
35270
35271 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35272 {
35273 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35274 const char *p = IDENTIFIER_POINTER (id);
35275
35276 if (strcmp ("ref", p) == 0)
35277 kind = OMP_CLAUSE_LINEAR_REF;
35278 else if (strcmp ("val", p) == 0)
35279 kind = OMP_CLAUSE_LINEAR_VAL;
35280 else if (strcmp ("uval", p) == 0)
35281 kind = OMP_CLAUSE_LINEAR_UVAL;
35282 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
35283 cp_lexer_consume_token (parser->lexer);
35284 else
35285 kind = OMP_CLAUSE_LINEAR_DEFAULT;
35286 }
35287
35288 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
35289 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
35290 &colon);
35291 else
35292 {
35293 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
35294 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
35295 if (colon)
35296 cp_parser_require (parser, CPP_COLON, RT_COLON);
35297 else if (!parens.require_close (parser))
35298 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35299 /*or_comma=*/false,
35300 /*consume_paren=*/true);
35301 }
35302
35303 if (colon)
35304 {
35305 step = NULL_TREE;
35306 if (declare_simd
35307 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
35308 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
35309 {
35310 cp_token *token = cp_lexer_peek_token (parser->lexer);
35311 cp_parser_parse_tentatively (parser);
35312 step = cp_parser_id_expression (parser, /*template_p=*/false,
35313 /*check_dependency_p=*/true,
35314 /*template_p=*/NULL,
35315 /*declarator_p=*/false,
35316 /*optional_p=*/false);
35317 if (step != error_mark_node)
35318 step = cp_parser_lookup_name_simple (parser, step, token->location);
35319 if (step == error_mark_node)
35320 {
35321 step = NULL_TREE;
35322 cp_parser_abort_tentative_parse (parser);
35323 }
35324 else if (!cp_parser_parse_definitely (parser))
35325 step = NULL_TREE;
35326 }
35327 if (!step)
35328 step = cp_parser_assignment_expression (parser);
35329
35330 if (!parens.require_close (parser))
35331 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35332 /*or_comma=*/false,
35333 /*consume_paren=*/true);
35334
35335 if (step == error_mark_node)
35336 return list;
35337 }
35338
35339 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
35340 {
35341 OMP_CLAUSE_LINEAR_STEP (c) = step;
35342 OMP_CLAUSE_LINEAR_KIND (c) = kind;
35343 }
35344
35345 return nlist;
35346 }
35347
35348 /* OpenMP 4.0:
35349 safelen ( constant-expression ) */
35350
35351 static tree
35352 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
35353 location_t location)
35354 {
35355 tree t, c;
35356
35357 matching_parens parens;
35358 if (!parens.require_open (parser))
35359 return list;
35360
35361 t = cp_parser_constant_expression (parser);
35362
35363 if (t == error_mark_node
35364 || !parens.require_close (parser))
35365 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35366 /*or_comma=*/false,
35367 /*consume_paren=*/true);
35368
35369 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
35370
35371 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
35372 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
35373 OMP_CLAUSE_CHAIN (c) = list;
35374
35375 return c;
35376 }
35377
35378 /* OpenMP 4.0:
35379 simdlen ( constant-expression ) */
35380
35381 static tree
35382 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
35383 location_t location)
35384 {
35385 tree t, c;
35386
35387 matching_parens parens;
35388 if (!parens.require_open (parser))
35389 return list;
35390
35391 t = cp_parser_constant_expression (parser);
35392
35393 if (t == error_mark_node
35394 || !parens.require_close (parser))
35395 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35396 /*or_comma=*/false,
35397 /*consume_paren=*/true);
35398
35399 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
35400
35401 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
35402 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
35403 OMP_CLAUSE_CHAIN (c) = list;
35404
35405 return c;
35406 }
35407
35408 /* OpenMP 4.5:
35409 vec:
35410 identifier [+/- integer]
35411 vec , identifier [+/- integer]
35412 */
35413
35414 static tree
35415 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
35416 tree list)
35417 {
35418 tree vec = NULL;
35419
35420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35421 {
35422 cp_parser_error (parser, "expected identifier");
35423 return list;
35424 }
35425
35426 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35427 {
35428 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
35429 tree t, identifier = cp_parser_identifier (parser);
35430 tree addend = NULL;
35431
35432 if (identifier == error_mark_node)
35433 t = error_mark_node;
35434 else
35435 {
35436 t = cp_parser_lookup_name_simple
35437 (parser, identifier,
35438 cp_lexer_peek_token (parser->lexer)->location);
35439 if (t == error_mark_node)
35440 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
35441 id_loc);
35442 }
35443
35444 bool neg = false;
35445 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
35446 neg = true;
35447 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
35448 {
35449 addend = integer_zero_node;
35450 goto add_to_vector;
35451 }
35452 cp_lexer_consume_token (parser->lexer);
35453
35454 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
35455 {
35456 cp_parser_error (parser, "expected integer");
35457 return list;
35458 }
35459
35460 addend = cp_lexer_peek_token (parser->lexer)->u.value;
35461 if (TREE_CODE (addend) != INTEGER_CST)
35462 {
35463 cp_parser_error (parser, "expected integer");
35464 return list;
35465 }
35466 cp_lexer_consume_token (parser->lexer);
35467
35468 add_to_vector:
35469 if (t != error_mark_node)
35470 {
35471 vec = tree_cons (addend, t, vec);
35472 if (neg)
35473 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
35474 }
35475
35476 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
35477 break;
35478
35479 cp_lexer_consume_token (parser->lexer);
35480 }
35481
35482 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
35483 {
35484 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
35485 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
35486 OMP_CLAUSE_DECL (u) = nreverse (vec);
35487 OMP_CLAUSE_CHAIN (u) = list;
35488 return u;
35489 }
35490 return list;
35491 }
35492
35493 /* OpenMP 5.0:
35494 iterators ( iterators-definition )
35495
35496 iterators-definition:
35497 iterator-specifier
35498 iterator-specifier , iterators-definition
35499
35500 iterator-specifier:
35501 identifier = range-specification
35502 iterator-type identifier = range-specification
35503
35504 range-specification:
35505 begin : end
35506 begin : end : step */
35507
35508 static tree
35509 cp_parser_omp_iterators (cp_parser *parser)
35510 {
35511 tree ret = NULL_TREE, *last = &ret;
35512 cp_lexer_consume_token (parser->lexer);
35513
35514 matching_parens parens;
35515 if (!parens.require_open (parser))
35516 return error_mark_node;
35517
35518 bool saved_colon_corrects_to_scope_p
35519 = parser->colon_corrects_to_scope_p;
35520 bool saved_colon_doesnt_start_class_def_p
35521 = parser->colon_doesnt_start_class_def_p;
35522
35523 do
35524 {
35525 tree iter_type;
35526 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
35527 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
35528 iter_type = integer_type_node;
35529 else
35530 {
35531 const char *saved_message
35532 = parser->type_definition_forbidden_message;
35533 parser->type_definition_forbidden_message
35534 = G_("types may not be defined in iterator type");
35535
35536 iter_type = cp_parser_type_id (parser);
35537
35538 parser->type_definition_forbidden_message = saved_message;
35539 }
35540
35541 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35542 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35543 {
35544 cp_parser_error (parser, "expected identifier");
35545 break;
35546 }
35547
35548 tree id = cp_parser_identifier (parser);
35549 if (id == error_mark_node)
35550 break;
35551
35552 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35553 break;
35554
35555 parser->colon_corrects_to_scope_p = false;
35556 parser->colon_doesnt_start_class_def_p = true;
35557 tree begin = cp_parser_assignment_expression (parser);
35558
35559 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35560 break;
35561
35562 tree end = cp_parser_assignment_expression (parser);
35563
35564 tree step = integer_one_node;
35565 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
35566 {
35567 cp_lexer_consume_token (parser->lexer);
35568 step = cp_parser_assignment_expression (parser);
35569 }
35570
35571 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
35572 DECL_ARTIFICIAL (iter_var) = 1;
35573 DECL_CONTEXT (iter_var) = current_function_decl;
35574 pushdecl (iter_var);
35575
35576 *last = make_tree_vec (6);
35577 TREE_VEC_ELT (*last, 0) = iter_var;
35578 TREE_VEC_ELT (*last, 1) = begin;
35579 TREE_VEC_ELT (*last, 2) = end;
35580 TREE_VEC_ELT (*last, 3) = step;
35581 last = &TREE_CHAIN (*last);
35582
35583 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35584 {
35585 cp_lexer_consume_token (parser->lexer);
35586 continue;
35587 }
35588 break;
35589 }
35590 while (1);
35591
35592 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
35593 parser->colon_doesnt_start_class_def_p
35594 = saved_colon_doesnt_start_class_def_p;
35595
35596 if (!parens.require_close (parser))
35597 cp_parser_skip_to_closing_parenthesis (parser,
35598 /*recovering=*/true,
35599 /*or_comma=*/false,
35600 /*consume_paren=*/true);
35601
35602 return ret ? ret : error_mark_node;
35603 }
35604
35605 /* OpenMP 4.0:
35606 depend ( depend-kind : variable-list )
35607
35608 depend-kind:
35609 in | out | inout
35610
35611 OpenMP 4.5:
35612 depend ( source )
35613
35614 depend ( sink : vec )
35615
35616 OpenMP 5.0:
35617 depend ( depend-modifier , depend-kind: variable-list )
35618
35619 depend-kind:
35620 in | out | inout | mutexinoutset | depobj
35621
35622 depend-modifier:
35623 iterator ( iterators-definition ) */
35624
35625 static tree
35626 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
35627 {
35628 tree nlist, c, iterators = NULL_TREE;
35629 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
35630
35631 matching_parens parens;
35632 if (!parens.require_open (parser))
35633 return list;
35634
35635 do
35636 {
35637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35638 goto invalid_kind;
35639
35640 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35641 const char *p = IDENTIFIER_POINTER (id);
35642
35643 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
35644 {
35645 begin_scope (sk_omp, NULL);
35646 iterators = cp_parser_omp_iterators (parser);
35647 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
35648 continue;
35649 }
35650 if (strcmp ("in", p) == 0)
35651 kind = OMP_CLAUSE_DEPEND_IN;
35652 else if (strcmp ("inout", p) == 0)
35653 kind = OMP_CLAUSE_DEPEND_INOUT;
35654 else if (strcmp ("mutexinoutset", p) == 0)
35655 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
35656 else if (strcmp ("out", p) == 0)
35657 kind = OMP_CLAUSE_DEPEND_OUT;
35658 else if (strcmp ("depobj", p) == 0)
35659 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
35660 else if (strcmp ("sink", p) == 0)
35661 kind = OMP_CLAUSE_DEPEND_SINK;
35662 else if (strcmp ("source", p) == 0)
35663 kind = OMP_CLAUSE_DEPEND_SOURCE;
35664 else
35665 goto invalid_kind;
35666 break;
35667 }
35668 while (1);
35669
35670 cp_lexer_consume_token (parser->lexer);
35671
35672 if (iterators
35673 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
35674 {
35675 poplevel (0, 1, 0);
35676 error_at (loc, "%<iterator%> modifier incompatible with %qs",
35677 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
35678 iterators = NULL_TREE;
35679 }
35680
35681 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
35682 {
35683 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
35684 OMP_CLAUSE_DEPEND_KIND (c) = kind;
35685 OMP_CLAUSE_DECL (c) = NULL_TREE;
35686 OMP_CLAUSE_CHAIN (c) = list;
35687 if (!parens.require_close (parser))
35688 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35689 /*or_comma=*/false,
35690 /*consume_paren=*/true);
35691 return c;
35692 }
35693
35694 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35695 goto resync_fail;
35696
35697 if (kind == OMP_CLAUSE_DEPEND_SINK)
35698 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
35699 else
35700 {
35701 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
35702 list, NULL);
35703
35704 if (iterators)
35705 {
35706 tree block = poplevel (1, 1, 0);
35707 if (iterators == error_mark_node)
35708 iterators = NULL_TREE;
35709 else
35710 TREE_VEC_ELT (iterators, 5) = block;
35711 }
35712
35713 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
35714 {
35715 OMP_CLAUSE_DEPEND_KIND (c) = kind;
35716 if (iterators)
35717 OMP_CLAUSE_DECL (c)
35718 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
35719 }
35720 }
35721 return nlist;
35722
35723 invalid_kind:
35724 cp_parser_error (parser, "invalid depend kind");
35725 resync_fail:
35726 if (iterators)
35727 poplevel (0, 1, 0);
35728 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35729 /*or_comma=*/false,
35730 /*consume_paren=*/true);
35731 return list;
35732 }
35733
35734 /* OpenMP 4.0:
35735 map ( map-kind : variable-list )
35736 map ( variable-list )
35737
35738 map-kind:
35739 alloc | to | from | tofrom
35740
35741 OpenMP 4.5:
35742 map-kind:
35743 alloc | to | from | tofrom | release | delete
35744
35745 map ( always [,] map-kind: variable-list ) */
35746
35747 static tree
35748 cp_parser_omp_clause_map (cp_parser *parser, tree list)
35749 {
35750 tree nlist, c;
35751 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
35752 bool always = false;
35753
35754 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35755 return list;
35756
35757 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35758 {
35759 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35760 const char *p = IDENTIFIER_POINTER (id);
35761
35762 if (strcmp ("always", p) == 0)
35763 {
35764 int nth = 2;
35765 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
35766 nth++;
35767 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
35768 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
35769 == RID_DELETE))
35770 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
35771 == CPP_COLON))
35772 {
35773 always = true;
35774 cp_lexer_consume_token (parser->lexer);
35775 if (nth == 3)
35776 cp_lexer_consume_token (parser->lexer);
35777 }
35778 }
35779 }
35780
35781 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
35782 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
35783 {
35784 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35785 const char *p = IDENTIFIER_POINTER (id);
35786
35787 if (strcmp ("alloc", p) == 0)
35788 kind = GOMP_MAP_ALLOC;
35789 else if (strcmp ("to", p) == 0)
35790 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
35791 else if (strcmp ("from", p) == 0)
35792 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
35793 else if (strcmp ("tofrom", p) == 0)
35794 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
35795 else if (strcmp ("release", p) == 0)
35796 kind = GOMP_MAP_RELEASE;
35797 else
35798 {
35799 cp_parser_error (parser, "invalid map kind");
35800 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35801 /*or_comma=*/false,
35802 /*consume_paren=*/true);
35803 return list;
35804 }
35805 cp_lexer_consume_token (parser->lexer);
35806 cp_lexer_consume_token (parser->lexer);
35807 }
35808 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
35809 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
35810 {
35811 kind = GOMP_MAP_DELETE;
35812 cp_lexer_consume_token (parser->lexer);
35813 cp_lexer_consume_token (parser->lexer);
35814 }
35815
35816 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
35817 NULL);
35818
35819 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
35820 OMP_CLAUSE_SET_MAP_KIND (c, kind);
35821
35822 return nlist;
35823 }
35824
35825 /* OpenMP 4.0:
35826 device ( expression ) */
35827
35828 static tree
35829 cp_parser_omp_clause_device (cp_parser *parser, tree list,
35830 location_t location)
35831 {
35832 tree t, c;
35833
35834 matching_parens parens;
35835 if (!parens.require_open (parser))
35836 return list;
35837
35838 t = cp_parser_assignment_expression (parser);
35839
35840 if (t == error_mark_node
35841 || !parens.require_close (parser))
35842 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35843 /*or_comma=*/false,
35844 /*consume_paren=*/true);
35845
35846 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
35847 "device", location);
35848
35849 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
35850 OMP_CLAUSE_DEVICE_ID (c) = t;
35851 OMP_CLAUSE_CHAIN (c) = list;
35852
35853 return c;
35854 }
35855
35856 /* OpenMP 4.0:
35857 dist_schedule ( static )
35858 dist_schedule ( static , expression ) */
35859
35860 static tree
35861 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
35862 location_t location)
35863 {
35864 tree c, t;
35865
35866 matching_parens parens;
35867 if (!parens.require_open (parser))
35868 return list;
35869
35870 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
35871
35872 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
35873 goto invalid_kind;
35874 cp_lexer_consume_token (parser->lexer);
35875
35876 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35877 {
35878 cp_lexer_consume_token (parser->lexer);
35879
35880 t = cp_parser_assignment_expression (parser);
35881
35882 if (t == error_mark_node)
35883 goto resync_fail;
35884 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
35885
35886 if (!parens.require_close (parser))
35887 goto resync_fail;
35888 }
35889 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
35890 goto resync_fail;
35891
35892 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
35893 "dist_schedule", location); */
35894 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
35895 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
35896 OMP_CLAUSE_CHAIN (c) = list;
35897 return c;
35898
35899 invalid_kind:
35900 cp_parser_error (parser, "invalid dist_schedule kind");
35901 resync_fail:
35902 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35903 /*or_comma=*/false,
35904 /*consume_paren=*/true);
35905 return list;
35906 }
35907
35908 /* OpenMP 4.0:
35909 proc_bind ( proc-bind-kind )
35910
35911 proc-bind-kind:
35912 master | close | spread */
35913
35914 static tree
35915 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
35916 location_t location)
35917 {
35918 tree c;
35919 enum omp_clause_proc_bind_kind kind;
35920
35921 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35922 return list;
35923
35924 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35925 {
35926 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35927 const char *p = IDENTIFIER_POINTER (id);
35928
35929 if (strcmp ("master", p) == 0)
35930 kind = OMP_CLAUSE_PROC_BIND_MASTER;
35931 else if (strcmp ("close", p) == 0)
35932 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
35933 else if (strcmp ("spread", p) == 0)
35934 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
35935 else
35936 goto invalid_kind;
35937 }
35938 else
35939 goto invalid_kind;
35940
35941 cp_lexer_consume_token (parser->lexer);
35942 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
35943 goto resync_fail;
35944
35945 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
35946 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
35947 location);
35948 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
35949 OMP_CLAUSE_CHAIN (c) = list;
35950 return c;
35951
35952 invalid_kind:
35953 cp_parser_error (parser, "invalid depend kind");
35954 resync_fail:
35955 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35956 /*or_comma=*/false,
35957 /*consume_paren=*/true);
35958 return list;
35959 }
35960
35961 /* OpenMP 5.0:
35962 device_type ( host | nohost | any ) */
35963
35964 static tree
35965 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
35966 location_t location)
35967 {
35968 tree c;
35969 enum omp_clause_device_type_kind kind;
35970
35971 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35972 return list;
35973
35974 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35975 {
35976 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35977 const char *p = IDENTIFIER_POINTER (id);
35978
35979 if (strcmp ("host", p) == 0)
35980 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
35981 else if (strcmp ("nohost", p) == 0)
35982 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
35983 else if (strcmp ("any", p) == 0)
35984 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
35985 else
35986 goto invalid_kind;
35987 }
35988 else
35989 goto invalid_kind;
35990
35991 cp_lexer_consume_token (parser->lexer);
35992 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
35993 goto resync_fail;
35994
35995 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
35996 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
35997 location); */
35998 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
35999 OMP_CLAUSE_CHAIN (c) = list;
36000 return c;
36001
36002 invalid_kind:
36003 cp_parser_error (parser, "invalid depend kind");
36004 resync_fail:
36005 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36006 /*or_comma=*/false,
36007 /*consume_paren=*/true);
36008 return list;
36009 }
36010
36011 /* OpenACC:
36012 async [( int-expr )] */
36013
36014 static tree
36015 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
36016 {
36017 tree c, t;
36018 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36019
36020 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
36021
36022 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36023 {
36024 matching_parens parens;
36025 parens.consume_open (parser);
36026
36027 t = cp_parser_expression (parser);
36028 if (t == error_mark_node
36029 || !parens.require_close (parser))
36030 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36031 /*or_comma=*/false,
36032 /*consume_paren=*/true);
36033 }
36034
36035 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
36036
36037 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
36038 OMP_CLAUSE_ASYNC_EXPR (c) = t;
36039 OMP_CLAUSE_CHAIN (c) = list;
36040 list = c;
36041
36042 return list;
36043 }
36044
36045 /* Parse all OpenACC clauses. The set clauses allowed by the directive
36046 is a bitmask in MASK. Return the list of clauses found. */
36047
36048 static tree
36049 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
36050 const char *where, cp_token *pragma_tok,
36051 bool finish_p = true)
36052 {
36053 tree clauses = NULL;
36054 bool first = true;
36055
36056 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36057 {
36058 location_t here;
36059 pragma_omp_clause c_kind;
36060 omp_clause_code code;
36061 const char *c_name;
36062 tree prev = clauses;
36063
36064 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36065 cp_lexer_consume_token (parser->lexer);
36066
36067 here = cp_lexer_peek_token (parser->lexer)->location;
36068 c_kind = cp_parser_omp_clause_name (parser);
36069
36070 switch (c_kind)
36071 {
36072 case PRAGMA_OACC_CLAUSE_ASYNC:
36073 clauses = cp_parser_oacc_clause_async (parser, clauses);
36074 c_name = "async";
36075 break;
36076 case PRAGMA_OACC_CLAUSE_AUTO:
36077 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
36078 clauses);
36079 c_name = "auto";
36080 break;
36081 case PRAGMA_OACC_CLAUSE_COLLAPSE:
36082 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
36083 c_name = "collapse";
36084 break;
36085 case PRAGMA_OACC_CLAUSE_COPY:
36086 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36087 c_name = "copy";
36088 break;
36089 case PRAGMA_OACC_CLAUSE_COPYIN:
36090 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36091 c_name = "copyin";
36092 break;
36093 case PRAGMA_OACC_CLAUSE_COPYOUT:
36094 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36095 c_name = "copyout";
36096 break;
36097 case PRAGMA_OACC_CLAUSE_CREATE:
36098 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36099 c_name = "create";
36100 break;
36101 case PRAGMA_OACC_CLAUSE_DELETE:
36102 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36103 c_name = "delete";
36104 break;
36105 case PRAGMA_OMP_CLAUSE_DEFAULT:
36106 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
36107 c_name = "default";
36108 break;
36109 case PRAGMA_OACC_CLAUSE_DEVICE:
36110 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36111 c_name = "device";
36112 break;
36113 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
36114 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
36115 c_name = "deviceptr";
36116 break;
36117 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
36118 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36119 c_name = "device_resident";
36120 break;
36121 case PRAGMA_OACC_CLAUSE_FINALIZE:
36122 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
36123 clauses);
36124 c_name = "finalize";
36125 break;
36126 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
36127 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
36128 clauses);
36129 c_name = "firstprivate";
36130 break;
36131 case PRAGMA_OACC_CLAUSE_GANG:
36132 c_name = "gang";
36133 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
36134 c_name, clauses);
36135 break;
36136 case PRAGMA_OACC_CLAUSE_HOST:
36137 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36138 c_name = "host";
36139 break;
36140 case PRAGMA_OACC_CLAUSE_IF:
36141 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
36142 c_name = "if";
36143 break;
36144 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
36145 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
36146 clauses);
36147 c_name = "if_present";
36148 break;
36149 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
36150 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
36151 clauses);
36152 c_name = "independent";
36153 break;
36154 case PRAGMA_OACC_CLAUSE_LINK:
36155 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36156 c_name = "link";
36157 break;
36158 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
36159 code = OMP_CLAUSE_NUM_GANGS;
36160 c_name = "num_gangs";
36161 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
36162 clauses);
36163 break;
36164 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
36165 c_name = "num_workers";
36166 code = OMP_CLAUSE_NUM_WORKERS;
36167 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
36168 clauses);
36169 break;
36170 case PRAGMA_OACC_CLAUSE_PRESENT:
36171 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36172 c_name = "present";
36173 break;
36174 case PRAGMA_OACC_CLAUSE_PRIVATE:
36175 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
36176 clauses);
36177 c_name = "private";
36178 break;
36179 case PRAGMA_OACC_CLAUSE_REDUCTION:
36180 clauses
36181 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
36182 false, clauses);
36183 c_name = "reduction";
36184 break;
36185 case PRAGMA_OACC_CLAUSE_SEQ:
36186 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
36187 clauses);
36188 c_name = "seq";
36189 break;
36190 case PRAGMA_OACC_CLAUSE_TILE:
36191 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
36192 c_name = "tile";
36193 break;
36194 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
36195 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
36196 clauses);
36197 c_name = "use_device";
36198 break;
36199 case PRAGMA_OACC_CLAUSE_VECTOR:
36200 c_name = "vector";
36201 clauses = cp_parser_oacc_shape_clause (parser, here,
36202 OMP_CLAUSE_VECTOR,
36203 c_name, clauses);
36204 break;
36205 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
36206 c_name = "vector_length";
36207 code = OMP_CLAUSE_VECTOR_LENGTH;
36208 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
36209 clauses);
36210 break;
36211 case PRAGMA_OACC_CLAUSE_WAIT:
36212 clauses = cp_parser_oacc_clause_wait (parser, clauses);
36213 c_name = "wait";
36214 break;
36215 case PRAGMA_OACC_CLAUSE_WORKER:
36216 c_name = "worker";
36217 clauses = cp_parser_oacc_shape_clause (parser, here,
36218 OMP_CLAUSE_WORKER,
36219 c_name, clauses);
36220 break;
36221 default:
36222 cp_parser_error (parser, "expected %<#pragma acc%> clause");
36223 goto saw_error;
36224 }
36225
36226 first = false;
36227
36228 if (((mask >> c_kind) & 1) == 0)
36229 {
36230 /* Remove the invalid clause(s) from the list to avoid
36231 confusing the rest of the compiler. */
36232 clauses = prev;
36233 error_at (here, "%qs is not valid for %qs", c_name, where);
36234 }
36235 }
36236
36237 saw_error:
36238 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36239
36240 if (finish_p)
36241 return finish_omp_clauses (clauses, C_ORT_ACC);
36242
36243 return clauses;
36244 }
36245
36246 /* Parse all OpenMP clauses. The set clauses allowed by the directive
36247 is a bitmask in MASK. Return the list of clauses found.
36248 FINISH_P set if finish_omp_clauses should be called.
36249 NESTED non-zero if clauses should be terminated by closing paren instead
36250 of end of pragma. If it is 2, additionally commas are required in between
36251 the clauses. */
36252
36253 static tree
36254 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
36255 const char *where, cp_token *pragma_tok,
36256 bool finish_p = true, int nested = 0)
36257 {
36258 tree clauses = NULL;
36259 bool first = true;
36260 cp_token *token = NULL;
36261
36262 /* Don't create location wrapper nodes within OpenMP clauses. */
36263 auto_suppress_location_wrappers sentinel;
36264
36265 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36266 {
36267 pragma_omp_clause c_kind;
36268 const char *c_name;
36269 tree prev = clauses;
36270
36271 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36272 break;
36273
36274 if (!first)
36275 {
36276 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36277 cp_lexer_consume_token (parser->lexer);
36278 else if (nested == 2)
36279 error_at (cp_lexer_peek_token (parser->lexer)->location,
36280 "clauses in %<simd%> trait should be separated "
36281 "by %<,%>");
36282 }
36283
36284 token = cp_lexer_peek_token (parser->lexer);
36285 c_kind = cp_parser_omp_clause_name (parser);
36286
36287 switch (c_kind)
36288 {
36289 case PRAGMA_OMP_CLAUSE_BIND:
36290 clauses = cp_parser_omp_clause_bind (parser, clauses,
36291 token->location);
36292 c_name = "bind";
36293 break;
36294 case PRAGMA_OMP_CLAUSE_COLLAPSE:
36295 clauses = cp_parser_omp_clause_collapse (parser, clauses,
36296 token->location);
36297 c_name = "collapse";
36298 break;
36299 case PRAGMA_OMP_CLAUSE_COPYIN:
36300 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
36301 c_name = "copyin";
36302 break;
36303 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
36304 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
36305 clauses);
36306 c_name = "copyprivate";
36307 break;
36308 case PRAGMA_OMP_CLAUSE_DEFAULT:
36309 clauses = cp_parser_omp_clause_default (parser, clauses,
36310 token->location, false);
36311 c_name = "default";
36312 break;
36313 case PRAGMA_OMP_CLAUSE_FINAL:
36314 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
36315 c_name = "final";
36316 break;
36317 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
36318 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
36319 clauses);
36320 c_name = "firstprivate";
36321 break;
36322 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
36323 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
36324 token->location);
36325 c_name = "grainsize";
36326 break;
36327 case PRAGMA_OMP_CLAUSE_HINT:
36328 clauses = cp_parser_omp_clause_hint (parser, clauses,
36329 token->location);
36330 c_name = "hint";
36331 break;
36332 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
36333 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
36334 token->location);
36335 c_name = "defaultmap";
36336 break;
36337 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
36338 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
36339 clauses);
36340 c_name = "use_device_ptr";
36341 break;
36342 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
36343 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
36344 clauses);
36345 c_name = "use_device_addr";
36346 break;
36347 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
36348 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
36349 clauses);
36350 c_name = "is_device_ptr";
36351 break;
36352 case PRAGMA_OMP_CLAUSE_IF:
36353 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
36354 true);
36355 c_name = "if";
36356 break;
36357 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
36358 clauses
36359 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
36360 true, clauses);
36361 c_name = "in_reduction";
36362 break;
36363 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
36364 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
36365 c_name = "lastprivate";
36366 break;
36367 case PRAGMA_OMP_CLAUSE_MERGEABLE:
36368 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
36369 token->location);
36370 c_name = "mergeable";
36371 break;
36372 case PRAGMA_OMP_CLAUSE_NOWAIT:
36373 clauses = cp_parser_omp_clause_nowait (parser, clauses,
36374 token->location);
36375 c_name = "nowait";
36376 break;
36377 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
36378 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
36379 token->location);
36380 c_name = "num_tasks";
36381 break;
36382 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
36383 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
36384 token->location);
36385 c_name = "num_threads";
36386 break;
36387 case PRAGMA_OMP_CLAUSE_ORDER:
36388 clauses = cp_parser_omp_clause_order (parser, clauses,
36389 token->location);
36390 c_name = "order";
36391 break;
36392 case PRAGMA_OMP_CLAUSE_ORDERED:
36393 clauses = cp_parser_omp_clause_ordered (parser, clauses,
36394 token->location);
36395 c_name = "ordered";
36396 break;
36397 case PRAGMA_OMP_CLAUSE_PRIORITY:
36398 clauses = cp_parser_omp_clause_priority (parser, clauses,
36399 token->location);
36400 c_name = "priority";
36401 break;
36402 case PRAGMA_OMP_CLAUSE_PRIVATE:
36403 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
36404 clauses);
36405 c_name = "private";
36406 break;
36407 case PRAGMA_OMP_CLAUSE_REDUCTION:
36408 clauses
36409 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
36410 true, clauses);
36411 c_name = "reduction";
36412 break;
36413 case PRAGMA_OMP_CLAUSE_SCHEDULE:
36414 clauses = cp_parser_omp_clause_schedule (parser, clauses,
36415 token->location);
36416 c_name = "schedule";
36417 break;
36418 case PRAGMA_OMP_CLAUSE_SHARED:
36419 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
36420 clauses);
36421 c_name = "shared";
36422 break;
36423 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
36424 clauses
36425 = cp_parser_omp_clause_reduction (parser,
36426 OMP_CLAUSE_TASK_REDUCTION,
36427 true, clauses);
36428 c_name = "task_reduction";
36429 break;
36430 case PRAGMA_OMP_CLAUSE_UNTIED:
36431 clauses = cp_parser_omp_clause_untied (parser, clauses,
36432 token->location);
36433 c_name = "untied";
36434 break;
36435 case PRAGMA_OMP_CLAUSE_INBRANCH:
36436 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
36437 clauses, token->location);
36438 c_name = "inbranch";
36439 break;
36440 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
36441 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
36442 clauses);
36443 c_name = "nontemporal";
36444 break;
36445 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
36446 clauses = cp_parser_omp_clause_branch (parser,
36447 OMP_CLAUSE_NOTINBRANCH,
36448 clauses, token->location);
36449 c_name = "notinbranch";
36450 break;
36451 case PRAGMA_OMP_CLAUSE_PARALLEL:
36452 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
36453 clauses, token->location);
36454 c_name = "parallel";
36455 if (!first)
36456 {
36457 clause_not_first:
36458 error_at (token->location, "%qs must be the first clause of %qs",
36459 c_name, where);
36460 clauses = prev;
36461 }
36462 break;
36463 case PRAGMA_OMP_CLAUSE_FOR:
36464 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
36465 clauses, token->location);
36466 c_name = "for";
36467 if (!first)
36468 goto clause_not_first;
36469 break;
36470 case PRAGMA_OMP_CLAUSE_SECTIONS:
36471 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
36472 clauses, token->location);
36473 c_name = "sections";
36474 if (!first)
36475 goto clause_not_first;
36476 break;
36477 case PRAGMA_OMP_CLAUSE_TASKGROUP:
36478 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
36479 clauses, token->location);
36480 c_name = "taskgroup";
36481 if (!first)
36482 goto clause_not_first;
36483 break;
36484 case PRAGMA_OMP_CLAUSE_LINK:
36485 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
36486 c_name = "to";
36487 break;
36488 case PRAGMA_OMP_CLAUSE_TO:
36489 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
36490 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36491 clauses);
36492 else
36493 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
36494 c_name = "to";
36495 break;
36496 case PRAGMA_OMP_CLAUSE_FROM:
36497 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
36498 c_name = "from";
36499 break;
36500 case PRAGMA_OMP_CLAUSE_UNIFORM:
36501 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
36502 clauses);
36503 c_name = "uniform";
36504 break;
36505 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
36506 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
36507 token->location);
36508 c_name = "num_teams";
36509 break;
36510 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
36511 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
36512 token->location);
36513 c_name = "thread_limit";
36514 break;
36515 case PRAGMA_OMP_CLAUSE_ALIGNED:
36516 clauses = cp_parser_omp_clause_aligned (parser, clauses);
36517 c_name = "aligned";
36518 break;
36519 case PRAGMA_OMP_CLAUSE_LINEAR:
36520 {
36521 bool declare_simd = false;
36522 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
36523 declare_simd = true;
36524 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
36525 }
36526 c_name = "linear";
36527 break;
36528 case PRAGMA_OMP_CLAUSE_DEPEND:
36529 clauses = cp_parser_omp_clause_depend (parser, clauses,
36530 token->location);
36531 c_name = "depend";
36532 break;
36533 case PRAGMA_OMP_CLAUSE_MAP:
36534 clauses = cp_parser_omp_clause_map (parser, clauses);
36535 c_name = "map";
36536 break;
36537 case PRAGMA_OMP_CLAUSE_DEVICE:
36538 clauses = cp_parser_omp_clause_device (parser, clauses,
36539 token->location);
36540 c_name = "device";
36541 break;
36542 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
36543 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
36544 token->location);
36545 c_name = "dist_schedule";
36546 break;
36547 case PRAGMA_OMP_CLAUSE_PROC_BIND:
36548 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
36549 token->location);
36550 c_name = "proc_bind";
36551 break;
36552 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
36553 clauses = cp_parser_omp_clause_device_type (parser, clauses,
36554 token->location);
36555 c_name = "device_type";
36556 break;
36557 case PRAGMA_OMP_CLAUSE_SAFELEN:
36558 clauses = cp_parser_omp_clause_safelen (parser, clauses,
36559 token->location);
36560 c_name = "safelen";
36561 break;
36562 case PRAGMA_OMP_CLAUSE_SIMDLEN:
36563 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
36564 token->location);
36565 c_name = "simdlen";
36566 break;
36567 case PRAGMA_OMP_CLAUSE_NOGROUP:
36568 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
36569 token->location);
36570 c_name = "nogroup";
36571 break;
36572 case PRAGMA_OMP_CLAUSE_THREADS:
36573 clauses
36574 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
36575 clauses, token->location);
36576 c_name = "threads";
36577 break;
36578 case PRAGMA_OMP_CLAUSE_SIMD:
36579 clauses
36580 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
36581 clauses, token->location);
36582 c_name = "simd";
36583 break;
36584 default:
36585 cp_parser_error (parser, "expected %<#pragma omp%> clause");
36586 goto saw_error;
36587 }
36588
36589 first = false;
36590
36591 if (((mask >> c_kind) & 1) == 0)
36592 {
36593 /* Remove the invalid clause(s) from the list to avoid
36594 confusing the rest of the compiler. */
36595 clauses = prev;
36596 error_at (token->location, "%qs is not valid for %qs", c_name, where);
36597 }
36598 }
36599 saw_error:
36600 if (!nested)
36601 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36602 if (finish_p)
36603 {
36604 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
36605 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
36606 else
36607 return finish_omp_clauses (clauses, C_ORT_OMP);
36608 }
36609 return clauses;
36610 }
36611
36612 /* OpenMP 2.5:
36613 structured-block:
36614 statement
36615
36616 In practice, we're also interested in adding the statement to an
36617 outer node. So it is convenient if we work around the fact that
36618 cp_parser_statement calls add_stmt. */
36619
36620 static unsigned
36621 cp_parser_begin_omp_structured_block (cp_parser *parser)
36622 {
36623 unsigned save = parser->in_statement;
36624
36625 /* Only move the values to IN_OMP_BLOCK if they weren't false.
36626 This preserves the "not within loop or switch" style error messages
36627 for nonsense cases like
36628 void foo() {
36629 #pragma omp single
36630 break;
36631 }
36632 */
36633 if (parser->in_statement)
36634 parser->in_statement = IN_OMP_BLOCK;
36635
36636 return save;
36637 }
36638
36639 static void
36640 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
36641 {
36642 parser->in_statement = save;
36643 }
36644
36645 static tree
36646 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
36647 {
36648 tree stmt = begin_omp_structured_block ();
36649 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36650
36651 cp_parser_statement (parser, NULL_TREE, false, if_p);
36652
36653 cp_parser_end_omp_structured_block (parser, save);
36654 return finish_omp_structured_block (stmt);
36655 }
36656
36657 /* OpenMP 2.5:
36658 # pragma omp atomic new-line
36659 expression-stmt
36660
36661 expression-stmt:
36662 x binop= expr | x++ | ++x | x-- | --x
36663 binop:
36664 +, *, -, /, &, ^, |, <<, >>
36665
36666 where x is an lvalue expression with scalar type.
36667
36668 OpenMP 3.1:
36669 # pragma omp atomic new-line
36670 update-stmt
36671
36672 # pragma omp atomic read new-line
36673 read-stmt
36674
36675 # pragma omp atomic write new-line
36676 write-stmt
36677
36678 # pragma omp atomic update new-line
36679 update-stmt
36680
36681 # pragma omp atomic capture new-line
36682 capture-stmt
36683
36684 # pragma omp atomic capture new-line
36685 capture-block
36686
36687 read-stmt:
36688 v = x
36689 write-stmt:
36690 x = expr
36691 update-stmt:
36692 expression-stmt | x = x binop expr
36693 capture-stmt:
36694 v = expression-stmt
36695 capture-block:
36696 { v = x; update-stmt; } | { update-stmt; v = x; }
36697
36698 OpenMP 4.0:
36699 update-stmt:
36700 expression-stmt | x = x binop expr | x = expr binop x
36701 capture-stmt:
36702 v = update-stmt
36703 capture-block:
36704 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
36705
36706 where x and v are lvalue expressions with scalar type. */
36707
36708 static void
36709 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
36710 {
36711 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
36712 tree rhs1 = NULL_TREE, orig_lhs;
36713 location_t loc = pragma_tok->location;
36714 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
36715 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
36716 bool structured_block = false;
36717 bool first = true;
36718 tree clauses = NULL_TREE;
36719
36720 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36721 {
36722 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36723 cp_lexer_consume_token (parser->lexer);
36724
36725 first = false;
36726
36727 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36728 {
36729 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36730 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
36731 const char *p = IDENTIFIER_POINTER (id);
36732 enum tree_code new_code = ERROR_MARK;
36733 enum omp_memory_order new_memory_order
36734 = OMP_MEMORY_ORDER_UNSPECIFIED;
36735
36736 if (!strcmp (p, "read"))
36737 new_code = OMP_ATOMIC_READ;
36738 else if (!strcmp (p, "write"))
36739 new_code = NOP_EXPR;
36740 else if (!strcmp (p, "update"))
36741 new_code = OMP_ATOMIC;
36742 else if (!strcmp (p, "capture"))
36743 new_code = OMP_ATOMIC_CAPTURE_NEW;
36744 else if (!strcmp (p, "seq_cst"))
36745 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36746 else if (!strcmp (p, "acq_rel"))
36747 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
36748 else if (!strcmp (p, "release"))
36749 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
36750 else if (!strcmp (p, "acquire"))
36751 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
36752 else if (!strcmp (p, "relaxed"))
36753 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
36754 else if (!strcmp (p, "hint"))
36755 {
36756 cp_lexer_consume_token (parser->lexer);
36757 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
36758 continue;
36759 }
36760 else
36761 {
36762 p = NULL;
36763 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
36764 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
36765 "%<release%>, %<relaxed%> or %<hint%> clause");
36766 }
36767 if (p)
36768 {
36769 if (new_code != ERROR_MARK)
36770 {
36771 if (code != ERROR_MARK)
36772 error_at (cloc, "too many atomic clauses");
36773 else
36774 code = new_code;
36775 }
36776 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
36777 {
36778 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
36779 error_at (cloc, "too many memory order clauses");
36780 else
36781 memory_order = new_memory_order;
36782 }
36783 cp_lexer_consume_token (parser->lexer);
36784 continue;
36785 }
36786 }
36787 break;
36788 }
36789 cp_parser_require_pragma_eol (parser, pragma_tok);
36790
36791 if (code == ERROR_MARK)
36792 code = OMP_ATOMIC;
36793 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
36794 {
36795 omp_requires_mask
36796 = (enum omp_requires) (omp_requires_mask
36797 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
36798 switch ((enum omp_memory_order)
36799 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
36800 {
36801 case OMP_MEMORY_ORDER_UNSPECIFIED:
36802 case OMP_MEMORY_ORDER_RELAXED:
36803 memory_order = OMP_MEMORY_ORDER_RELAXED;
36804 break;
36805 case OMP_MEMORY_ORDER_SEQ_CST:
36806 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36807 break;
36808 case OMP_MEMORY_ORDER_ACQ_REL:
36809 switch (code)
36810 {
36811 case OMP_ATOMIC_READ:
36812 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
36813 break;
36814 case NOP_EXPR: /* atomic write */
36815 case OMP_ATOMIC:
36816 memory_order = OMP_MEMORY_ORDER_RELEASE;
36817 break;
36818 default:
36819 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
36820 break;
36821 }
36822 break;
36823 default:
36824 gcc_unreachable ();
36825 }
36826 }
36827 else
36828 switch (code)
36829 {
36830 case OMP_ATOMIC_READ:
36831 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
36832 || memory_order == OMP_MEMORY_ORDER_RELEASE)
36833 {
36834 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
36835 "%<acq_rel%> or %<release%> clauses");
36836 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36837 }
36838 break;
36839 case NOP_EXPR: /* atomic write */
36840 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
36841 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
36842 {
36843 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
36844 "%<acq_rel%> or %<acquire%> clauses");
36845 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36846 }
36847 break;
36848 case OMP_ATOMIC:
36849 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
36850 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
36851 {
36852 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
36853 "%<acq_rel%> or %<acquire%> clauses");
36854 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36855 }
36856 break;
36857 default:
36858 break;
36859 }
36860
36861 switch (code)
36862 {
36863 case OMP_ATOMIC_READ:
36864 case NOP_EXPR: /* atomic write */
36865 v = cp_parser_unary_expression (parser);
36866 if (v == error_mark_node)
36867 goto saw_error;
36868 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36869 goto saw_error;
36870 if (code == NOP_EXPR)
36871 lhs = cp_parser_expression (parser);
36872 else
36873 lhs = cp_parser_unary_expression (parser);
36874 if (lhs == error_mark_node)
36875 goto saw_error;
36876 if (code == NOP_EXPR)
36877 {
36878 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
36879 opcode. */
36880 code = OMP_ATOMIC;
36881 rhs = lhs;
36882 lhs = v;
36883 v = NULL_TREE;
36884 }
36885 goto done;
36886 case OMP_ATOMIC_CAPTURE_NEW:
36887 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36888 {
36889 cp_lexer_consume_token (parser->lexer);
36890 structured_block = true;
36891 }
36892 else
36893 {
36894 v = cp_parser_unary_expression (parser);
36895 if (v == error_mark_node)
36896 goto saw_error;
36897 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36898 goto saw_error;
36899 }
36900 default:
36901 break;
36902 }
36903
36904 restart:
36905 lhs = cp_parser_unary_expression (parser);
36906 orig_lhs = lhs;
36907 switch (TREE_CODE (lhs))
36908 {
36909 case ERROR_MARK:
36910 goto saw_error;
36911
36912 case POSTINCREMENT_EXPR:
36913 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
36914 code = OMP_ATOMIC_CAPTURE_OLD;
36915 /* FALLTHROUGH */
36916 case PREINCREMENT_EXPR:
36917 lhs = TREE_OPERAND (lhs, 0);
36918 opcode = PLUS_EXPR;
36919 rhs = integer_one_node;
36920 break;
36921
36922 case POSTDECREMENT_EXPR:
36923 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
36924 code = OMP_ATOMIC_CAPTURE_OLD;
36925 /* FALLTHROUGH */
36926 case PREDECREMENT_EXPR:
36927 lhs = TREE_OPERAND (lhs, 0);
36928 opcode = MINUS_EXPR;
36929 rhs = integer_one_node;
36930 break;
36931
36932 case COMPOUND_EXPR:
36933 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
36934 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
36935 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
36936 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
36937 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
36938 (TREE_OPERAND (lhs, 1), 0), 0)))
36939 == BOOLEAN_TYPE)
36940 /* Undo effects of boolean_increment for post {in,de}crement. */
36941 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
36942 /* FALLTHRU */
36943 case MODIFY_EXPR:
36944 if (TREE_CODE (lhs) == MODIFY_EXPR
36945 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
36946 {
36947 /* Undo effects of boolean_increment. */
36948 if (integer_onep (TREE_OPERAND (lhs, 1)))
36949 {
36950 /* This is pre or post increment. */
36951 rhs = TREE_OPERAND (lhs, 1);
36952 lhs = TREE_OPERAND (lhs, 0);
36953 opcode = NOP_EXPR;
36954 if (code == OMP_ATOMIC_CAPTURE_NEW
36955 && !structured_block
36956 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
36957 code = OMP_ATOMIC_CAPTURE_OLD;
36958 break;
36959 }
36960 }
36961 /* FALLTHRU */
36962 default:
36963 switch (cp_lexer_peek_token (parser->lexer)->type)
36964 {
36965 case CPP_MULT_EQ:
36966 opcode = MULT_EXPR;
36967 break;
36968 case CPP_DIV_EQ:
36969 opcode = TRUNC_DIV_EXPR;
36970 break;
36971 case CPP_PLUS_EQ:
36972 opcode = PLUS_EXPR;
36973 break;
36974 case CPP_MINUS_EQ:
36975 opcode = MINUS_EXPR;
36976 break;
36977 case CPP_LSHIFT_EQ:
36978 opcode = LSHIFT_EXPR;
36979 break;
36980 case CPP_RSHIFT_EQ:
36981 opcode = RSHIFT_EXPR;
36982 break;
36983 case CPP_AND_EQ:
36984 opcode = BIT_AND_EXPR;
36985 break;
36986 case CPP_OR_EQ:
36987 opcode = BIT_IOR_EXPR;
36988 break;
36989 case CPP_XOR_EQ:
36990 opcode = BIT_XOR_EXPR;
36991 break;
36992 case CPP_EQ:
36993 enum cp_parser_prec oprec;
36994 cp_token *token;
36995 cp_lexer_consume_token (parser->lexer);
36996 cp_parser_parse_tentatively (parser);
36997 rhs1 = cp_parser_simple_cast_expression (parser);
36998 if (rhs1 == error_mark_node)
36999 {
37000 cp_parser_abort_tentative_parse (parser);
37001 cp_parser_simple_cast_expression (parser);
37002 goto saw_error;
37003 }
37004 token = cp_lexer_peek_token (parser->lexer);
37005 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
37006 {
37007 cp_parser_abort_tentative_parse (parser);
37008 cp_parser_parse_tentatively (parser);
37009 rhs = cp_parser_binary_expression (parser, false, true,
37010 PREC_NOT_OPERATOR, NULL);
37011 if (rhs == error_mark_node)
37012 {
37013 cp_parser_abort_tentative_parse (parser);
37014 cp_parser_binary_expression (parser, false, true,
37015 PREC_NOT_OPERATOR, NULL);
37016 goto saw_error;
37017 }
37018 switch (TREE_CODE (rhs))
37019 {
37020 case MULT_EXPR:
37021 case TRUNC_DIV_EXPR:
37022 case RDIV_EXPR:
37023 case PLUS_EXPR:
37024 case MINUS_EXPR:
37025 case LSHIFT_EXPR:
37026 case RSHIFT_EXPR:
37027 case BIT_AND_EXPR:
37028 case BIT_IOR_EXPR:
37029 case BIT_XOR_EXPR:
37030 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
37031 {
37032 if (cp_parser_parse_definitely (parser))
37033 {
37034 opcode = TREE_CODE (rhs);
37035 rhs1 = TREE_OPERAND (rhs, 0);
37036 rhs = TREE_OPERAND (rhs, 1);
37037 goto stmt_done;
37038 }
37039 else
37040 goto saw_error;
37041 }
37042 break;
37043 default:
37044 break;
37045 }
37046 cp_parser_abort_tentative_parse (parser);
37047 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
37048 {
37049 rhs = cp_parser_expression (parser);
37050 if (rhs == error_mark_node)
37051 goto saw_error;
37052 opcode = NOP_EXPR;
37053 rhs1 = NULL_TREE;
37054 goto stmt_done;
37055 }
37056 cp_parser_error (parser,
37057 "invalid form of %<#pragma omp atomic%>");
37058 goto saw_error;
37059 }
37060 if (!cp_parser_parse_definitely (parser))
37061 goto saw_error;
37062 switch (token->type)
37063 {
37064 case CPP_SEMICOLON:
37065 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
37066 {
37067 code = OMP_ATOMIC_CAPTURE_OLD;
37068 v = lhs;
37069 lhs = NULL_TREE;
37070 lhs1 = rhs1;
37071 rhs1 = NULL_TREE;
37072 cp_lexer_consume_token (parser->lexer);
37073 goto restart;
37074 }
37075 else if (structured_block)
37076 {
37077 opcode = NOP_EXPR;
37078 rhs = rhs1;
37079 rhs1 = NULL_TREE;
37080 goto stmt_done;
37081 }
37082 cp_parser_error (parser,
37083 "invalid form of %<#pragma omp atomic%>");
37084 goto saw_error;
37085 case CPP_MULT:
37086 opcode = MULT_EXPR;
37087 break;
37088 case CPP_DIV:
37089 opcode = TRUNC_DIV_EXPR;
37090 break;
37091 case CPP_PLUS:
37092 opcode = PLUS_EXPR;
37093 break;
37094 case CPP_MINUS:
37095 opcode = MINUS_EXPR;
37096 break;
37097 case CPP_LSHIFT:
37098 opcode = LSHIFT_EXPR;
37099 break;
37100 case CPP_RSHIFT:
37101 opcode = RSHIFT_EXPR;
37102 break;
37103 case CPP_AND:
37104 opcode = BIT_AND_EXPR;
37105 break;
37106 case CPP_OR:
37107 opcode = BIT_IOR_EXPR;
37108 break;
37109 case CPP_XOR:
37110 opcode = BIT_XOR_EXPR;
37111 break;
37112 default:
37113 cp_parser_error (parser,
37114 "invalid operator for %<#pragma omp atomic%>");
37115 goto saw_error;
37116 }
37117 oprec = TOKEN_PRECEDENCE (token);
37118 gcc_assert (oprec != PREC_NOT_OPERATOR);
37119 if (commutative_tree_code (opcode))
37120 oprec = (enum cp_parser_prec) (oprec - 1);
37121 cp_lexer_consume_token (parser->lexer);
37122 rhs = cp_parser_binary_expression (parser, false, false,
37123 oprec, NULL);
37124 if (rhs == error_mark_node)
37125 goto saw_error;
37126 goto stmt_done;
37127 /* FALLTHROUGH */
37128 default:
37129 cp_parser_error (parser,
37130 "invalid operator for %<#pragma omp atomic%>");
37131 goto saw_error;
37132 }
37133 cp_lexer_consume_token (parser->lexer);
37134
37135 rhs = cp_parser_expression (parser);
37136 if (rhs == error_mark_node)
37137 goto saw_error;
37138 break;
37139 }
37140 stmt_done:
37141 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
37142 {
37143 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
37144 goto saw_error;
37145 v = cp_parser_unary_expression (parser);
37146 if (v == error_mark_node)
37147 goto saw_error;
37148 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
37149 goto saw_error;
37150 lhs1 = cp_parser_unary_expression (parser);
37151 if (lhs1 == error_mark_node)
37152 goto saw_error;
37153 }
37154 if (structured_block)
37155 {
37156 cp_parser_consume_semicolon_at_end_of_statement (parser);
37157 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
37158 }
37159 done:
37160 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37161 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
37162 rhs1, clauses, memory_order);
37163 if (!structured_block)
37164 cp_parser_consume_semicolon_at_end_of_statement (parser);
37165 return;
37166
37167 saw_error:
37168 cp_parser_skip_to_end_of_block_or_statement (parser);
37169 if (structured_block)
37170 {
37171 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
37172 cp_lexer_consume_token (parser->lexer);
37173 else if (code == OMP_ATOMIC_CAPTURE_NEW)
37174 {
37175 cp_parser_skip_to_end_of_block_or_statement (parser);
37176 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
37177 cp_lexer_consume_token (parser->lexer);
37178 }
37179 }
37180 }
37181
37182
37183 /* OpenMP 2.5:
37184 # pragma omp barrier new-line */
37185
37186 static void
37187 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
37188 {
37189 cp_parser_require_pragma_eol (parser, pragma_tok);
37190 finish_omp_barrier ();
37191 }
37192
37193 /* OpenMP 2.5:
37194 # pragma omp critical [(name)] new-line
37195 structured-block
37196
37197 OpenMP 4.5:
37198 # pragma omp critical [(name) [hint(expression)]] new-line
37199 structured-block */
37200
37201 #define OMP_CRITICAL_CLAUSE_MASK \
37202 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
37203
37204 static tree
37205 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37206 {
37207 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
37208
37209 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37210 {
37211 matching_parens parens;
37212 parens.consume_open (parser);
37213
37214 name = cp_parser_identifier (parser);
37215
37216 if (name == error_mark_node
37217 || !parens.require_close (parser))
37218 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37219 /*or_comma=*/false,
37220 /*consume_paren=*/true);
37221 if (name == error_mark_node)
37222 name = NULL;
37223
37224 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
37225 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
37226 cp_lexer_consume_token (parser->lexer);
37227
37228 clauses = cp_parser_omp_all_clauses (parser,
37229 OMP_CRITICAL_CLAUSE_MASK,
37230 "#pragma omp critical", pragma_tok);
37231 }
37232 else
37233 cp_parser_require_pragma_eol (parser, pragma_tok);
37234
37235 stmt = cp_parser_omp_structured_block (parser, if_p);
37236 return c_finish_omp_critical (input_location, stmt, name, clauses);
37237 }
37238
37239 /* OpenMP 5.0:
37240 # pragma omp depobj ( depobj ) depobj-clause new-line
37241
37242 depobj-clause:
37243 depend (dependence-type : locator)
37244 destroy
37245 update (dependence-type)
37246
37247 dependence-type:
37248 in
37249 out
37250 inout
37251 mutexinout */
37252
37253 static void
37254 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
37255 {
37256 location_t loc = pragma_tok->location;
37257 matching_parens parens;
37258 if (!parens.require_open (parser))
37259 {
37260 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37261 return;
37262 }
37263
37264 tree depobj = cp_parser_assignment_expression (parser);
37265
37266 if (!parens.require_close (parser))
37267 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37268 /*or_comma=*/false,
37269 /*consume_paren=*/true);
37270
37271 tree clause = NULL_TREE;
37272 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
37273 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
37274 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37275 {
37276 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37277 const char *p = IDENTIFIER_POINTER (id);
37278
37279 cp_lexer_consume_token (parser->lexer);
37280 if (!strcmp ("depend", p))
37281 {
37282 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
37283 if (clause)
37284 clause = finish_omp_clauses (clause, C_ORT_OMP);
37285 if (!clause)
37286 clause = error_mark_node;
37287 }
37288 else if (!strcmp ("destroy", p))
37289 kind = OMP_CLAUSE_DEPEND_LAST;
37290 else if (!strcmp ("update", p))
37291 {
37292 matching_parens c_parens;
37293 if (c_parens.require_open (parser))
37294 {
37295 location_t c2_loc
37296 = cp_lexer_peek_token (parser->lexer)->location;
37297 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37298 {
37299 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
37300 const char *p2 = IDENTIFIER_POINTER (id2);
37301
37302 cp_lexer_consume_token (parser->lexer);
37303 if (!strcmp ("in", p2))
37304 kind = OMP_CLAUSE_DEPEND_IN;
37305 else if (!strcmp ("out", p2))
37306 kind = OMP_CLAUSE_DEPEND_OUT;
37307 else if (!strcmp ("inout", p2))
37308 kind = OMP_CLAUSE_DEPEND_INOUT;
37309 else if (!strcmp ("mutexinoutset", p2))
37310 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
37311 }
37312 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
37313 {
37314 clause = error_mark_node;
37315 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
37316 "%<mutexinoutset%>");
37317 }
37318 if (!c_parens.require_close (parser))
37319 cp_parser_skip_to_closing_parenthesis (parser,
37320 /*recovering=*/true,
37321 /*or_comma=*/false,
37322 /*consume_paren=*/true);
37323 }
37324 else
37325 clause = error_mark_node;
37326 }
37327 }
37328 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
37329 {
37330 clause = error_mark_node;
37331 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
37332 }
37333 cp_parser_require_pragma_eol (parser, pragma_tok);
37334
37335 finish_omp_depobj (loc, depobj, kind, clause);
37336 }
37337
37338
37339 /* OpenMP 2.5:
37340 # pragma omp flush flush-vars[opt] new-line
37341
37342 flush-vars:
37343 ( variable-list )
37344
37345 OpenMP 5.0:
37346 # pragma omp flush memory-order-clause new-line */
37347
37348 static void
37349 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
37350 {
37351 enum memmodel mo = MEMMODEL_LAST;
37352 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37353 {
37354 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37355 const char *p = IDENTIFIER_POINTER (id);
37356 if (!strcmp (p, "acq_rel"))
37357 mo = MEMMODEL_ACQ_REL;
37358 else if (!strcmp (p, "release"))
37359 mo = MEMMODEL_RELEASE;
37360 else if (!strcmp (p, "acquire"))
37361 mo = MEMMODEL_ACQUIRE;
37362 else
37363 error_at (cp_lexer_peek_token (parser->lexer)->location,
37364 "expected %<acq_rel%>, %<release%> or %<acquire%>");
37365 cp_lexer_consume_token (parser->lexer);
37366 }
37367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37368 {
37369 if (mo != MEMMODEL_LAST)
37370 error_at (cp_lexer_peek_token (parser->lexer)->location,
37371 "%<flush%> list specified together with memory order "
37372 "clause");
37373 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37374 }
37375 cp_parser_require_pragma_eol (parser, pragma_tok);
37376
37377 finish_omp_flush (mo);
37378 }
37379
37380 /* Helper function, to parse omp for increment expression. */
37381
37382 static tree
37383 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
37384 {
37385 tree cond = cp_parser_binary_expression (parser, false, true,
37386 PREC_NOT_OPERATOR, NULL);
37387 if (cond == error_mark_node
37388 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37389 {
37390 cp_parser_skip_to_end_of_statement (parser);
37391 return error_mark_node;
37392 }
37393
37394 switch (TREE_CODE (cond))
37395 {
37396 case GT_EXPR:
37397 case GE_EXPR:
37398 case LT_EXPR:
37399 case LE_EXPR:
37400 break;
37401 case NE_EXPR:
37402 if (code != OACC_LOOP)
37403 break;
37404 gcc_fallthrough ();
37405 default:
37406 return error_mark_node;
37407 }
37408
37409 /* If decl is an iterator, preserve LHS and RHS of the relational
37410 expr until finish_omp_for. */
37411 if (decl
37412 && (type_dependent_expression_p (decl)
37413 || CLASS_TYPE_P (TREE_TYPE (decl))))
37414 return cond;
37415
37416 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
37417 TREE_CODE (cond),
37418 TREE_OPERAND (cond, 0), ERROR_MARK,
37419 TREE_OPERAND (cond, 1), ERROR_MARK,
37420 /*overload=*/NULL, tf_warning_or_error);
37421 }
37422
37423 /* Helper function, to parse omp for increment expression. */
37424
37425 static tree
37426 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
37427 {
37428 cp_token *token = cp_lexer_peek_token (parser->lexer);
37429 enum tree_code op;
37430 tree lhs, rhs;
37431 cp_id_kind idk;
37432 bool decl_first;
37433
37434 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
37435 {
37436 op = (token->type == CPP_PLUS_PLUS
37437 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
37438 cp_lexer_consume_token (parser->lexer);
37439 lhs = cp_parser_simple_cast_expression (parser);
37440 if (lhs != decl
37441 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
37442 return error_mark_node;
37443 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
37444 }
37445
37446 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
37447 if (lhs != decl
37448 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
37449 return error_mark_node;
37450
37451 token = cp_lexer_peek_token (parser->lexer);
37452 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
37453 {
37454 op = (token->type == CPP_PLUS_PLUS
37455 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
37456 cp_lexer_consume_token (parser->lexer);
37457 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
37458 }
37459
37460 op = cp_parser_assignment_operator_opt (parser);
37461 if (op == ERROR_MARK)
37462 return error_mark_node;
37463
37464 if (op != NOP_EXPR)
37465 {
37466 rhs = cp_parser_assignment_expression (parser);
37467 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
37468 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
37469 }
37470
37471 lhs = cp_parser_binary_expression (parser, false, false,
37472 PREC_ADDITIVE_EXPRESSION, NULL);
37473 token = cp_lexer_peek_token (parser->lexer);
37474 decl_first = (lhs == decl
37475 || (processing_template_decl && cp_tree_equal (lhs, decl)));
37476 if (decl_first)
37477 lhs = NULL_TREE;
37478 if (token->type != CPP_PLUS
37479 && token->type != CPP_MINUS)
37480 return error_mark_node;
37481
37482 do
37483 {
37484 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
37485 cp_lexer_consume_token (parser->lexer);
37486 rhs = cp_parser_binary_expression (parser, false, false,
37487 PREC_ADDITIVE_EXPRESSION, NULL);
37488 token = cp_lexer_peek_token (parser->lexer);
37489 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
37490 {
37491 if (lhs == NULL_TREE)
37492 {
37493 if (op == PLUS_EXPR)
37494 lhs = rhs;
37495 else
37496 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
37497 tf_warning_or_error);
37498 }
37499 else
37500 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
37501 ERROR_MARK, NULL, tf_warning_or_error);
37502 }
37503 }
37504 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
37505
37506 if (!decl_first)
37507 {
37508 if ((rhs != decl
37509 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
37510 || op == MINUS_EXPR)
37511 return error_mark_node;
37512 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
37513 }
37514 else
37515 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
37516
37517 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
37518 }
37519
37520 /* Parse the initialization statement of an OpenMP for loop.
37521
37522 Return true if the resulting construct should have an
37523 OMP_CLAUSE_PRIVATE added to it. */
37524
37525 static tree
37526 cp_parser_omp_for_loop_init (cp_parser *parser,
37527 tree &this_pre_body,
37528 releasing_vec &for_block,
37529 tree &init,
37530 tree &orig_init,
37531 tree &decl,
37532 tree &real_decl)
37533 {
37534 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37535 return NULL_TREE;
37536
37537 tree add_private_clause = NULL_TREE;
37538
37539 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
37540
37541 init-expr:
37542 var = lb
37543 integer-type var = lb
37544 random-access-iterator-type var = lb
37545 pointer-type var = lb
37546 */
37547 cp_decl_specifier_seq type_specifiers;
37548
37549 /* First, try to parse as an initialized declaration. See
37550 cp_parser_condition, from whence the bulk of this is copied. */
37551
37552 cp_parser_parse_tentatively (parser);
37553 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
37554 /*is_declaration=*/true,
37555 /*is_trailing_return=*/false,
37556 &type_specifiers);
37557 if (cp_parser_parse_definitely (parser))
37558 {
37559 /* If parsing a type specifier seq succeeded, then this
37560 MUST be a initialized declaration. */
37561 tree asm_specification, attributes;
37562 cp_declarator *declarator;
37563
37564 declarator = cp_parser_declarator (parser,
37565 CP_PARSER_DECLARATOR_NAMED,
37566 CP_PARSER_FLAGS_NONE,
37567 /*ctor_dtor_or_conv_p=*/NULL,
37568 /*parenthesized_p=*/NULL,
37569 /*member_p=*/false,
37570 /*friend_p=*/false,
37571 /*static_p=*/false);
37572 attributes = cp_parser_attributes_opt (parser);
37573 asm_specification = cp_parser_asm_specification_opt (parser);
37574
37575 if (declarator == cp_error_declarator)
37576 cp_parser_skip_to_end_of_statement (parser);
37577
37578 else
37579 {
37580 tree pushed_scope, auto_node;
37581
37582 decl = start_decl (declarator, &type_specifiers,
37583 SD_INITIALIZED, attributes,
37584 /*prefix_attributes=*/NULL_TREE,
37585 &pushed_scope);
37586
37587 auto_node = type_uses_auto (TREE_TYPE (decl));
37588 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
37589 {
37590 if (cp_lexer_next_token_is (parser->lexer,
37591 CPP_OPEN_PAREN))
37592 error ("parenthesized initialization is not allowed in "
37593 "OpenMP %<for%> loop");
37594 else
37595 /* Trigger an error. */
37596 cp_parser_require (parser, CPP_EQ, RT_EQ);
37597
37598 init = error_mark_node;
37599 cp_parser_skip_to_end_of_statement (parser);
37600 }
37601 else if (CLASS_TYPE_P (TREE_TYPE (decl))
37602 || type_dependent_expression_p (decl)
37603 || auto_node)
37604 {
37605 bool is_direct_init, is_non_constant_init;
37606
37607 init = cp_parser_initializer (parser,
37608 &is_direct_init,
37609 &is_non_constant_init);
37610
37611 if (auto_node)
37612 {
37613 TREE_TYPE (decl)
37614 = do_auto_deduction (TREE_TYPE (decl), init,
37615 auto_node);
37616
37617 if (!CLASS_TYPE_P (TREE_TYPE (decl))
37618 && !type_dependent_expression_p (decl))
37619 goto non_class;
37620 }
37621
37622 cp_finish_decl (decl, init, !is_non_constant_init,
37623 asm_specification,
37624 LOOKUP_ONLYCONVERTING);
37625 orig_init = init;
37626 if (CLASS_TYPE_P (TREE_TYPE (decl)))
37627 {
37628 vec_safe_push (for_block, this_pre_body);
37629 init = NULL_TREE;
37630 }
37631 else
37632 {
37633 init = pop_stmt_list (this_pre_body);
37634 if (init && TREE_CODE (init) == STATEMENT_LIST)
37635 {
37636 tree_stmt_iterator i = tsi_start (init);
37637 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
37638 while (!tsi_end_p (i))
37639 {
37640 tree t = tsi_stmt (i);
37641 if (TREE_CODE (t) == DECL_EXPR
37642 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
37643 {
37644 tsi_delink (&i);
37645 vec_safe_push (for_block, t);
37646 continue;
37647 }
37648 break;
37649 }
37650 if (tsi_one_before_end_p (i))
37651 {
37652 tree t = tsi_stmt (i);
37653 tsi_delink (&i);
37654 free_stmt_list (init);
37655 init = t;
37656 }
37657 }
37658 }
37659 this_pre_body = NULL_TREE;
37660 }
37661 else
37662 {
37663 /* Consume '='. */
37664 cp_lexer_consume_token (parser->lexer);
37665 init = cp_parser_assignment_expression (parser);
37666
37667 non_class:
37668 if (TYPE_REF_P (TREE_TYPE (decl)))
37669 init = error_mark_node;
37670 else
37671 cp_finish_decl (decl, NULL_TREE,
37672 /*init_const_expr_p=*/false,
37673 asm_specification,
37674 LOOKUP_ONLYCONVERTING);
37675 }
37676
37677 if (pushed_scope)
37678 pop_scope (pushed_scope);
37679 }
37680 }
37681 else
37682 {
37683 cp_id_kind idk;
37684 /* If parsing a type specifier sequence failed, then
37685 this MUST be a simple expression. */
37686 cp_parser_parse_tentatively (parser);
37687 decl = cp_parser_primary_expression (parser, false, false,
37688 false, &idk);
37689 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
37690 if (!cp_parser_error_occurred (parser)
37691 && decl
37692 && (TREE_CODE (decl) == COMPONENT_REF
37693 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
37694 {
37695 cp_parser_abort_tentative_parse (parser);
37696 cp_parser_parse_tentatively (parser);
37697 cp_token *token = cp_lexer_peek_token (parser->lexer);
37698 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
37699 /*check_dependency_p=*/true,
37700 /*template_p=*/NULL,
37701 /*declarator_p=*/false,
37702 /*optional_p=*/false);
37703 if (name != error_mark_node
37704 && last_tok == cp_lexer_peek_token (parser->lexer))
37705 {
37706 decl = cp_parser_lookup_name_simple (parser, name,
37707 token->location);
37708 if (TREE_CODE (decl) == FIELD_DECL)
37709 add_private_clause = omp_privatize_field (decl, false);
37710 }
37711 cp_parser_abort_tentative_parse (parser);
37712 cp_parser_parse_tentatively (parser);
37713 decl = cp_parser_primary_expression (parser, false, false,
37714 false, &idk);
37715 }
37716 if (!cp_parser_error_occurred (parser)
37717 && decl
37718 && DECL_P (decl)
37719 && CLASS_TYPE_P (TREE_TYPE (decl)))
37720 {
37721 tree rhs;
37722
37723 cp_parser_parse_definitely (parser);
37724 cp_parser_require (parser, CPP_EQ, RT_EQ);
37725 rhs = cp_parser_assignment_expression (parser);
37726 orig_init = rhs;
37727 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
37728 decl, NOP_EXPR,
37729 rhs,
37730 tf_warning_or_error));
37731 if (!add_private_clause)
37732 add_private_clause = decl;
37733 }
37734 else
37735 {
37736 decl = NULL;
37737 cp_parser_abort_tentative_parse (parser);
37738 init = cp_parser_expression (parser);
37739 if (init)
37740 {
37741 if (TREE_CODE (init) == MODIFY_EXPR
37742 || TREE_CODE (init) == MODOP_EXPR)
37743 real_decl = TREE_OPERAND (init, 0);
37744 }
37745 }
37746 }
37747 return add_private_clause;
37748 }
37749
37750 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
37751
37752 void
37753 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
37754 tree &decl, tree &orig_decl, tree &init,
37755 tree &orig_init, tree &cond, tree &incr)
37756 {
37757 tree begin, end, range_temp_decl = NULL_TREE;
37758 tree iter_type, begin_expr, end_expr;
37759
37760 if (processing_template_decl)
37761 {
37762 if (check_for_bare_parameter_packs (init))
37763 init = error_mark_node;
37764 if (!type_dependent_expression_p (init)
37765 /* do_auto_deduction doesn't mess with template init-lists. */
37766 && !BRACE_ENCLOSED_INITIALIZER_P (init))
37767 {
37768 tree d = decl;
37769 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
37770 {
37771 tree v = DECL_VALUE_EXPR (decl);
37772 if (TREE_CODE (v) == ARRAY_REF
37773 && VAR_P (TREE_OPERAND (v, 0))
37774 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
37775 d = TREE_OPERAND (v, 0);
37776 }
37777 do_range_for_auto_deduction (d, init);
37778 }
37779 cond = global_namespace;
37780 incr = NULL_TREE;
37781 orig_init = init;
37782 if (this_pre_body)
37783 this_pre_body = pop_stmt_list (this_pre_body);
37784 return;
37785 }
37786
37787 init = mark_lvalue_use (init);
37788
37789 if (decl == error_mark_node || init == error_mark_node)
37790 /* If an error happened previously do nothing or else a lot of
37791 unhelpful errors would be issued. */
37792 begin_expr = end_expr = iter_type = error_mark_node;
37793 else
37794 {
37795 tree range_temp;
37796
37797 if (VAR_P (init)
37798 && array_of_runtime_bound_p (TREE_TYPE (init)))
37799 /* Can't bind a reference to an array of runtime bound. */
37800 range_temp = init;
37801 else
37802 {
37803 range_temp = build_range_temp (init);
37804 DECL_NAME (range_temp) = NULL_TREE;
37805 pushdecl (range_temp);
37806 cp_finish_decl (range_temp, init,
37807 /*is_constant_init*/false, NULL_TREE,
37808 LOOKUP_ONLYCONVERTING);
37809 range_temp_decl = range_temp;
37810 range_temp = convert_from_reference (range_temp);
37811 }
37812 iter_type = cp_parser_perform_range_for_lookup (range_temp,
37813 &begin_expr, &end_expr);
37814 }
37815
37816 tree end_iter_type = iter_type;
37817 if (cxx_dialect >= cxx17)
37818 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
37819 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
37820 TREE_USED (end) = 1;
37821 DECL_ARTIFICIAL (end) = 1;
37822 pushdecl (end);
37823 cp_finish_decl (end, end_expr,
37824 /*is_constant_init*/false, NULL_TREE,
37825 LOOKUP_ONLYCONVERTING);
37826
37827 /* The new for initialization statement. */
37828 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
37829 TREE_USED (begin) = 1;
37830 DECL_ARTIFICIAL (begin) = 1;
37831 pushdecl (begin);
37832 orig_init = init;
37833 if (CLASS_TYPE_P (iter_type))
37834 init = NULL_TREE;
37835 else
37836 {
37837 init = begin_expr;
37838 begin_expr = NULL_TREE;
37839 }
37840 cp_finish_decl (begin, begin_expr,
37841 /*is_constant_init*/false, NULL_TREE,
37842 LOOKUP_ONLYCONVERTING);
37843
37844 /* The new for condition. */
37845 if (CLASS_TYPE_P (iter_type))
37846 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
37847 else
37848 cond = build_x_binary_op (input_location, NE_EXPR,
37849 begin, ERROR_MARK,
37850 end, ERROR_MARK,
37851 NULL, tf_warning_or_error);
37852
37853 /* The new increment expression. */
37854 if (CLASS_TYPE_P (iter_type))
37855 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
37856 else
37857 incr = finish_unary_op_expr (input_location,
37858 PREINCREMENT_EXPR, begin,
37859 tf_warning_or_error);
37860
37861 orig_decl = decl;
37862 decl = begin;
37863 if (for_block)
37864 {
37865 vec_safe_push (for_block, this_pre_body);
37866 this_pre_body = NULL_TREE;
37867 }
37868
37869 tree decomp_first_name = NULL_TREE;
37870 unsigned decomp_cnt = 0;
37871 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
37872 {
37873 tree v = DECL_VALUE_EXPR (orig_decl);
37874 if (TREE_CODE (v) == ARRAY_REF
37875 && VAR_P (TREE_OPERAND (v, 0))
37876 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
37877 {
37878 tree d = orig_decl;
37879 orig_decl = TREE_OPERAND (v, 0);
37880 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
37881 decomp_first_name = d;
37882 }
37883 }
37884
37885 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
37886 if (auto_node)
37887 {
37888 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
37889 tf_none);
37890 if (!error_operand_p (t))
37891 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
37892 t, auto_node);
37893 }
37894
37895 tree v = make_tree_vec (decomp_cnt + 3);
37896 TREE_VEC_ELT (v, 0) = range_temp_decl;
37897 TREE_VEC_ELT (v, 1) = end;
37898 TREE_VEC_ELT (v, 2) = orig_decl;
37899 for (unsigned i = 0; i < decomp_cnt; i++)
37900 {
37901 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
37902 decomp_first_name = DECL_CHAIN (decomp_first_name);
37903 }
37904 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
37905 }
37906
37907 /* Helper for cp_parser_omp_for_loop, finalize part of range for
37908 inside of the collapsed body. */
37909
37910 void
37911 cp_finish_omp_range_for (tree orig, tree begin)
37912 {
37913 gcc_assert (TREE_CODE (orig) == TREE_LIST
37914 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
37915 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
37916 tree decomp_first_name = NULL_TREE;
37917 unsigned int decomp_cnt = 0;
37918
37919 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
37920 {
37921 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
37922 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
37923 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
37924 }
37925
37926 /* The declaration is initialized with *__begin inside the loop body. */
37927 cp_finish_decl (decl,
37928 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
37929 tf_warning_or_error),
37930 /*is_constant_init*/false, NULL_TREE,
37931 LOOKUP_ONLYCONVERTING);
37932 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
37933 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
37934 }
37935
37936 /* OpenMP 5.0:
37937
37938 scan-loop-body:
37939 { structured-block scan-directive structured-block } */
37940
37941 static void
37942 cp_parser_omp_scan_loop_body (cp_parser *parser)
37943 {
37944 tree substmt, clauses = NULL_TREE;
37945
37946 matching_braces braces;
37947 if (!braces.require_open (parser))
37948 return;
37949
37950 substmt = cp_parser_omp_structured_block (parser, NULL);
37951 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
37952 add_stmt (substmt);
37953
37954 cp_token *tok = cp_lexer_peek_token (parser->lexer);
37955 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
37956 {
37957 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
37958
37959 cp_lexer_consume_token (parser->lexer);
37960
37961 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37962 {
37963 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37964 const char *p = IDENTIFIER_POINTER (id);
37965 if (strcmp (p, "inclusive") == 0)
37966 clause = OMP_CLAUSE_INCLUSIVE;
37967 else if (strcmp (p, "exclusive") == 0)
37968 clause = OMP_CLAUSE_EXCLUSIVE;
37969 }
37970 if (clause != OMP_CLAUSE_ERROR)
37971 {
37972 cp_lexer_consume_token (parser->lexer);
37973 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
37974 }
37975 else
37976 cp_parser_error (parser, "expected %<inclusive%> or "
37977 "%<exclusive%> clause");
37978
37979 cp_parser_require_pragma_eol (parser, tok);
37980 }
37981 else
37982 error ("expected %<#pragma omp scan%>");
37983
37984 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37985 substmt = cp_parser_omp_structured_block (parser, NULL);
37986 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
37987 clauses);
37988 add_stmt (substmt);
37989
37990 braces.require_close (parser);
37991 }
37992
37993 /* Parse the restricted form of the for statement allowed by OpenMP. */
37994
37995 static tree
37996 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
37997 tree *cclauses, bool *if_p)
37998 {
37999 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
38000 tree orig_decl;
38001 tree real_decl, initv, condv, incrv, declv, orig_declv;
38002 tree this_pre_body, cl, ordered_cl = NULL_TREE;
38003 location_t loc_first;
38004 bool collapse_err = false;
38005 int i, collapse = 1, ordered = 0, count, nbraces = 0;
38006 releasing_vec for_block;
38007 auto_vec<tree, 4> orig_inits;
38008 bool tiling = false;
38009 bool inscan = false;
38010
38011 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
38012 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
38013 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
38014 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
38015 {
38016 tiling = true;
38017 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
38018 }
38019 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
38020 && OMP_CLAUSE_ORDERED_EXPR (cl))
38021 {
38022 ordered_cl = cl;
38023 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
38024 }
38025 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
38026 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
38027 && (code == OMP_SIMD || code == OMP_FOR))
38028 inscan = true;
38029
38030 if (ordered && ordered < collapse)
38031 {
38032 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
38033 "%<ordered%> clause parameter is less than %<collapse%>");
38034 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
38035 = build_int_cst (NULL_TREE, collapse);
38036 ordered = collapse;
38037 }
38038 if (ordered)
38039 {
38040 for (tree *pc = &clauses; *pc; )
38041 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
38042 {
38043 error_at (OMP_CLAUSE_LOCATION (*pc),
38044 "%<linear%> clause may not be specified together "
38045 "with %<ordered%> clause with a parameter");
38046 *pc = OMP_CLAUSE_CHAIN (*pc);
38047 }
38048 else
38049 pc = &OMP_CLAUSE_CHAIN (*pc);
38050 }
38051
38052 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
38053 count = ordered ? ordered : collapse;
38054
38055 declv = make_tree_vec (count);
38056 initv = make_tree_vec (count);
38057 condv = make_tree_vec (count);
38058 incrv = make_tree_vec (count);
38059 orig_declv = NULL_TREE;
38060
38061 loc_first = cp_lexer_peek_token (parser->lexer)->location;
38062
38063 for (i = 0; i < count; i++)
38064 {
38065 int bracecount = 0;
38066 tree add_private_clause = NULL_TREE;
38067 location_t loc;
38068
38069 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
38070 {
38071 if (!collapse_err)
38072 cp_parser_error (parser, "for statement expected");
38073 return NULL;
38074 }
38075 loc = cp_lexer_consume_token (parser->lexer)->location;
38076
38077 /* Don't create location wrapper nodes within an OpenMP "for"
38078 statement. */
38079 auto_suppress_location_wrappers sentinel;
38080
38081 matching_parens parens;
38082 if (!parens.require_open (parser))
38083 return NULL;
38084
38085 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
38086 this_pre_body = push_stmt_list ();
38087
38088 if (code != OACC_LOOP && cxx_dialect >= cxx11)
38089 {
38090 /* Save tokens so that we can put them back. */
38091 cp_lexer_save_tokens (parser->lexer);
38092
38093 /* Look for ':' that is not nested in () or {}. */
38094 bool is_range_for
38095 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
38096 /*recovering=*/false,
38097 CPP_COLON,
38098 /*consume_paren=*/
38099 false) == -1);
38100
38101 /* Roll back the tokens we skipped. */
38102 cp_lexer_rollback_tokens (parser->lexer);
38103
38104 if (is_range_for)
38105 {
38106 bool saved_colon_corrects_to_scope_p
38107 = parser->colon_corrects_to_scope_p;
38108
38109 /* A colon is used in range-based for. */
38110 parser->colon_corrects_to_scope_p = false;
38111
38112 /* Parse the declaration. */
38113 cp_parser_simple_declaration (parser,
38114 /*function_definition_allowed_p=*/
38115 false, &decl);
38116 parser->colon_corrects_to_scope_p
38117 = saved_colon_corrects_to_scope_p;
38118
38119 cp_parser_require (parser, CPP_COLON, RT_COLON);
38120
38121 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
38122 false, 0, true);
38123
38124 cp_convert_omp_range_for (this_pre_body, for_block, decl,
38125 orig_decl, init, orig_init,
38126 cond, incr);
38127 if (this_pre_body)
38128 {
38129 if (pre_body)
38130 {
38131 tree t = pre_body;
38132 pre_body = push_stmt_list ();
38133 add_stmt (t);
38134 add_stmt (this_pre_body);
38135 pre_body = pop_stmt_list (pre_body);
38136 }
38137 else
38138 pre_body = this_pre_body;
38139 }
38140
38141 if (ordered_cl)
38142 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
38143 "%<ordered%> clause with parameter on "
38144 "range-based %<for%> loop");
38145
38146 goto parse_close_paren;
38147 }
38148 }
38149
38150 add_private_clause
38151 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
38152 init, orig_init, decl, real_decl);
38153
38154 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38155 if (this_pre_body)
38156 {
38157 this_pre_body = pop_stmt_list (this_pre_body);
38158 if (pre_body)
38159 {
38160 tree t = pre_body;
38161 pre_body = push_stmt_list ();
38162 add_stmt (t);
38163 add_stmt (this_pre_body);
38164 pre_body = pop_stmt_list (pre_body);
38165 }
38166 else
38167 pre_body = this_pre_body;
38168 }
38169
38170 if (decl)
38171 real_decl = decl;
38172 if (cclauses != NULL
38173 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
38174 && real_decl != NULL_TREE
38175 && code != OMP_LOOP)
38176 {
38177 tree *c;
38178 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
38179 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
38180 && OMP_CLAUSE_DECL (*c) == real_decl)
38181 {
38182 error_at (loc, "iteration variable %qD"
38183 " should not be firstprivate", real_decl);
38184 *c = OMP_CLAUSE_CHAIN (*c);
38185 }
38186 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
38187 && OMP_CLAUSE_DECL (*c) == real_decl)
38188 {
38189 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
38190 tree l = *c;
38191 *c = OMP_CLAUSE_CHAIN (*c);
38192 if (code == OMP_SIMD)
38193 {
38194 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
38195 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
38196 }
38197 else
38198 {
38199 OMP_CLAUSE_CHAIN (l) = clauses;
38200 clauses = l;
38201 }
38202 add_private_clause = NULL_TREE;
38203 }
38204 else
38205 {
38206 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
38207 && OMP_CLAUSE_DECL (*c) == real_decl)
38208 add_private_clause = NULL_TREE;
38209 c = &OMP_CLAUSE_CHAIN (*c);
38210 }
38211 }
38212
38213 if (add_private_clause)
38214 {
38215 tree c;
38216 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
38217 {
38218 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
38219 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
38220 && OMP_CLAUSE_DECL (c) == decl)
38221 break;
38222 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
38223 && OMP_CLAUSE_DECL (c) == decl)
38224 error_at (loc, "iteration variable %qD "
38225 "should not be firstprivate",
38226 decl);
38227 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
38228 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
38229 && OMP_CLAUSE_DECL (c) == decl)
38230 error_at (loc, "iteration variable %qD should not be reduction",
38231 decl);
38232 }
38233 if (c == NULL)
38234 {
38235 if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
38236 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
38237 else if (code != OMP_SIMD)
38238 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
38239 else
38240 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38241 OMP_CLAUSE_DECL (c) = add_private_clause;
38242 c = finish_omp_clauses (c, C_ORT_OMP);
38243 if (c)
38244 {
38245 OMP_CLAUSE_CHAIN (c) = clauses;
38246 clauses = c;
38247 /* For linear, signal that we need to fill up
38248 the so far unknown linear step. */
38249 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
38250 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
38251 }
38252 }
38253 }
38254
38255 cond = NULL;
38256 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
38257 cond = cp_parser_omp_for_cond (parser, decl, code);
38258 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38259
38260 incr = NULL;
38261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
38262 {
38263 /* If decl is an iterator, preserve the operator on decl
38264 until finish_omp_for. */
38265 if (real_decl
38266 && ((processing_template_decl
38267 && (TREE_TYPE (real_decl) == NULL_TREE
38268 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
38269 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
38270 incr = cp_parser_omp_for_incr (parser, real_decl);
38271 else
38272 incr = cp_parser_expression (parser);
38273 if (!EXPR_HAS_LOCATION (incr))
38274 protected_set_expr_location (incr, input_location);
38275 }
38276
38277 parse_close_paren:
38278 if (!parens.require_close (parser))
38279 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38280 /*or_comma=*/false,
38281 /*consume_paren=*/true);
38282
38283 TREE_VEC_ELT (declv, i) = decl;
38284 TREE_VEC_ELT (initv, i) = init;
38285 TREE_VEC_ELT (condv, i) = cond;
38286 TREE_VEC_ELT (incrv, i) = incr;
38287 if (orig_init)
38288 {
38289 orig_inits.safe_grow_cleared (i + 1);
38290 orig_inits[i] = orig_init;
38291 }
38292 if (orig_decl)
38293 {
38294 if (!orig_declv)
38295 orig_declv = copy_node (declv);
38296 TREE_VEC_ELT (orig_declv, i) = orig_decl;
38297 }
38298 else if (orig_declv)
38299 TREE_VEC_ELT (orig_declv, i) = decl;
38300
38301 if (i == count - 1)
38302 break;
38303
38304 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
38305 in between the collapsed for loops to be still considered perfectly
38306 nested. Hopefully the final version clarifies this.
38307 For now handle (multiple) {'s and empty statements. */
38308 cp_parser_parse_tentatively (parser);
38309 for (;;)
38310 {
38311 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
38312 break;
38313 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
38314 {
38315 cp_lexer_consume_token (parser->lexer);
38316 bracecount++;
38317 }
38318 else if (bracecount
38319 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
38320 cp_lexer_consume_token (parser->lexer);
38321 else
38322 {
38323 loc = cp_lexer_peek_token (parser->lexer)->location;
38324 error_at (loc, "not enough for loops to collapse");
38325 collapse_err = true;
38326 cp_parser_abort_tentative_parse (parser);
38327 declv = NULL_TREE;
38328 break;
38329 }
38330 }
38331
38332 if (declv)
38333 {
38334 cp_parser_parse_definitely (parser);
38335 nbraces += bracecount;
38336 }
38337 }
38338
38339 if (nbraces)
38340 if_p = NULL;
38341
38342 /* Note that we saved the original contents of this flag when we entered
38343 the structured block, and so we don't need to re-save it here. */
38344 parser->in_statement = IN_OMP_FOR;
38345
38346 /* Note that the grammar doesn't call for a structured block here,
38347 though the loop as a whole is a structured block. */
38348 if (orig_declv)
38349 {
38350 body = begin_omp_structured_block ();
38351 for (i = 0; i < count; i++)
38352 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
38353 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
38354 TREE_VEC_ELT (declv, i));
38355 }
38356 else
38357 body = push_stmt_list ();
38358 if (inscan)
38359 cp_parser_omp_scan_loop_body (parser);
38360 else
38361 cp_parser_statement (parser, NULL_TREE, false, if_p);
38362 if (orig_declv)
38363 body = finish_omp_structured_block (body);
38364 else
38365 body = pop_stmt_list (body);
38366
38367 if (declv == NULL_TREE)
38368 ret = NULL_TREE;
38369 else
38370 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
38371 incrv, body, pre_body, &orig_inits, clauses);
38372
38373 while (nbraces)
38374 {
38375 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
38376 {
38377 cp_lexer_consume_token (parser->lexer);
38378 nbraces--;
38379 }
38380 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
38381 cp_lexer_consume_token (parser->lexer);
38382 else
38383 {
38384 if (!collapse_err)
38385 {
38386 error_at (cp_lexer_peek_token (parser->lexer)->location,
38387 "collapsed loops not perfectly nested");
38388 }
38389 collapse_err = true;
38390 cp_parser_statement_seq_opt (parser, NULL);
38391 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
38392 break;
38393 }
38394 }
38395
38396 while (!for_block->is_empty ())
38397 {
38398 tree t = for_block->pop ();
38399 if (TREE_CODE (t) == STATEMENT_LIST)
38400 add_stmt (pop_stmt_list (t));
38401 else
38402 add_stmt (t);
38403 }
38404
38405 return ret;
38406 }
38407
38408 /* Helper function for OpenMP parsing, split clauses and call
38409 finish_omp_clauses on each of the set of clauses afterwards. */
38410
38411 static void
38412 cp_omp_split_clauses (location_t loc, enum tree_code code,
38413 omp_clause_mask mask, tree clauses, tree *cclauses)
38414 {
38415 int i;
38416 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
38417 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
38418 if (cclauses[i])
38419 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
38420 }
38421
38422 /* OpenMP 5.0:
38423 #pragma omp loop loop-clause[optseq] new-line
38424 for-loop */
38425
38426 #define OMP_LOOP_CLAUSE_MASK \
38427 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
38431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
38432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
38433
38434 static tree
38435 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
38436 char *p_name, omp_clause_mask mask, tree *cclauses,
38437 bool *if_p)
38438 {
38439 tree clauses, sb, ret;
38440 unsigned int save;
38441 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38442
38443 strcat (p_name, " loop");
38444 mask |= OMP_LOOP_CLAUSE_MASK;
38445
38446 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38447 cclauses == NULL);
38448 if (cclauses)
38449 {
38450 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
38451 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
38452 }
38453
38454 keep_next_level (true);
38455 sb = begin_omp_structured_block ();
38456 save = cp_parser_begin_omp_structured_block (parser);
38457
38458 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
38459
38460 cp_parser_end_omp_structured_block (parser, save);
38461 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
38462
38463 return ret;
38464 }
38465
38466 /* OpenMP 4.0:
38467 #pragma omp simd simd-clause[optseq] new-line
38468 for-loop */
38469
38470 #define OMP_SIMD_CLAUSE_MASK \
38471 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
38472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
38479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
38481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
38482
38483 static tree
38484 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
38485 char *p_name, omp_clause_mask mask, tree *cclauses,
38486 bool *if_p)
38487 {
38488 tree clauses, sb, ret;
38489 unsigned int save;
38490 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38491
38492 strcat (p_name, " simd");
38493 mask |= OMP_SIMD_CLAUSE_MASK;
38494
38495 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38496 cclauses == NULL);
38497 if (cclauses)
38498 {
38499 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
38500 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
38501 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
38502 OMP_CLAUSE_ORDERED);
38503 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
38504 {
38505 error_at (OMP_CLAUSE_LOCATION (c),
38506 "%<ordered%> clause with parameter may not be specified "
38507 "on %qs construct", p_name);
38508 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
38509 }
38510 }
38511
38512 keep_next_level (true);
38513 sb = begin_omp_structured_block ();
38514 save = cp_parser_begin_omp_structured_block (parser);
38515
38516 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
38517
38518 cp_parser_end_omp_structured_block (parser, save);
38519 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
38520
38521 return ret;
38522 }
38523
38524 /* OpenMP 2.5:
38525 #pragma omp for for-clause[optseq] new-line
38526 for-loop
38527
38528 OpenMP 4.0:
38529 #pragma omp for simd for-simd-clause[optseq] new-line
38530 for-loop */
38531
38532 #define OMP_FOR_CLAUSE_MASK \
38533 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
38539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
38540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
38542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
38543
38544 static tree
38545 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
38546 char *p_name, omp_clause_mask mask, tree *cclauses,
38547 bool *if_p)
38548 {
38549 tree clauses, sb, ret;
38550 unsigned int save;
38551 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38552
38553 strcat (p_name, " for");
38554 mask |= OMP_FOR_CLAUSE_MASK;
38555 /* parallel for{, simd} disallows nowait clause, but for
38556 target {teams distribute ,}parallel for{, simd} it should be accepted. */
38557 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
38558 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
38559 /* Composite distribute parallel for{, simd} disallows ordered clause. */
38560 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
38561 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
38562
38563 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38564 {
38565 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38566 const char *p = IDENTIFIER_POINTER (id);
38567
38568 if (strcmp (p, "simd") == 0)
38569 {
38570 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38571 if (cclauses == NULL)
38572 cclauses = cclauses_buf;
38573
38574 cp_lexer_consume_token (parser->lexer);
38575 if (!flag_openmp) /* flag_openmp_simd */
38576 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38577 cclauses, if_p);
38578 sb = begin_omp_structured_block ();
38579 save = cp_parser_begin_omp_structured_block (parser);
38580 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38581 cclauses, if_p);
38582 cp_parser_end_omp_structured_block (parser, save);
38583 tree body = finish_omp_structured_block (sb);
38584 if (ret == NULL)
38585 return ret;
38586 ret = make_node (OMP_FOR);
38587 TREE_TYPE (ret) = void_type_node;
38588 OMP_FOR_BODY (ret) = body;
38589 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
38590 SET_EXPR_LOCATION (ret, loc);
38591 add_stmt (ret);
38592 return ret;
38593 }
38594 }
38595 if (!flag_openmp) /* flag_openmp_simd */
38596 {
38597 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38598 return NULL_TREE;
38599 }
38600
38601 /* Composite distribute parallel for disallows linear clause. */
38602 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
38603 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
38604
38605 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38606 cclauses == NULL);
38607 if (cclauses)
38608 {
38609 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
38610 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
38611 }
38612
38613 keep_next_level (true);
38614 sb = begin_omp_structured_block ();
38615 save = cp_parser_begin_omp_structured_block (parser);
38616
38617 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
38618
38619 cp_parser_end_omp_structured_block (parser, save);
38620 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
38621
38622 return ret;
38623 }
38624
38625 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
38626 omp_clause_mask, tree *, bool *);
38627
38628 /* OpenMP 2.5:
38629 # pragma omp master new-line
38630 structured-block */
38631
38632 static tree
38633 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
38634 char *p_name, omp_clause_mask mask, tree *cclauses,
38635 bool *if_p)
38636 {
38637 tree clauses, sb, ret;
38638 unsigned int save;
38639 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38640
38641 strcat (p_name, " master");
38642
38643 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38644 {
38645 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38646 const char *p = IDENTIFIER_POINTER (id);
38647
38648 if (strcmp (p, "taskloop") == 0)
38649 {
38650 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38651 if (cclauses == NULL)
38652 cclauses = cclauses_buf;
38653
38654 cp_lexer_consume_token (parser->lexer);
38655 if (!flag_openmp) /* flag_openmp_simd */
38656 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
38657 cclauses, if_p);
38658 sb = begin_omp_structured_block ();
38659 save = cp_parser_begin_omp_structured_block (parser);
38660 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
38661 cclauses, if_p);
38662 cp_parser_end_omp_structured_block (parser, save);
38663 tree body = finish_omp_structured_block (sb);
38664 if (ret == NULL)
38665 return ret;
38666 return c_finish_omp_master (loc, body);
38667 }
38668 }
38669 if (!flag_openmp) /* flag_openmp_simd */
38670 {
38671 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38672 return NULL_TREE;
38673 }
38674
38675 if (cclauses)
38676 {
38677 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38678 false);
38679 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
38680 }
38681 else
38682 cp_parser_require_pragma_eol (parser, pragma_tok);
38683
38684 return c_finish_omp_master (loc,
38685 cp_parser_omp_structured_block (parser, if_p));
38686 }
38687
38688 /* OpenMP 2.5:
38689 # pragma omp ordered new-line
38690 structured-block
38691
38692 OpenMP 4.5:
38693 # pragma omp ordered ordered-clauses new-line
38694 structured-block */
38695
38696 #define OMP_ORDERED_CLAUSE_MASK \
38697 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
38698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
38699
38700 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
38701 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
38702
38703 static bool
38704 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
38705 enum pragma_context context, bool *if_p)
38706 {
38707 location_t loc = pragma_tok->location;
38708
38709 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38710 {
38711 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38712 const char *p = IDENTIFIER_POINTER (id);
38713
38714 if (strcmp (p, "depend") == 0)
38715 {
38716 if (!flag_openmp) /* flag_openmp_simd */
38717 {
38718 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38719 return false;
38720 }
38721 if (context == pragma_stmt)
38722 {
38723 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
38724 "%<depend%> clause may only be used in compound "
38725 "statements");
38726 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38727 return false;
38728 }
38729 tree clauses
38730 = cp_parser_omp_all_clauses (parser,
38731 OMP_ORDERED_DEPEND_CLAUSE_MASK,
38732 "#pragma omp ordered", pragma_tok);
38733 c_finish_omp_ordered (loc, clauses, NULL_TREE);
38734 return false;
38735 }
38736 }
38737
38738 tree clauses
38739 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
38740 "#pragma omp ordered", pragma_tok);
38741
38742 if (!flag_openmp /* flag_openmp_simd */
38743 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
38744 return false;
38745
38746 c_finish_omp_ordered (loc, clauses,
38747 cp_parser_omp_structured_block (parser, if_p));
38748 return true;
38749 }
38750
38751 /* OpenMP 2.5:
38752
38753 section-scope:
38754 { section-sequence }
38755
38756 section-sequence:
38757 section-directive[opt] structured-block
38758 section-sequence section-directive structured-block */
38759
38760 static tree
38761 cp_parser_omp_sections_scope (cp_parser *parser)
38762 {
38763 tree stmt, substmt;
38764 bool error_suppress = false;
38765 cp_token *tok;
38766
38767 matching_braces braces;
38768 if (!braces.require_open (parser))
38769 return NULL_TREE;
38770
38771 stmt = push_stmt_list ();
38772
38773 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
38774 != PRAGMA_OMP_SECTION)
38775 {
38776 substmt = cp_parser_omp_structured_block (parser, NULL);
38777 substmt = build1 (OMP_SECTION, void_type_node, substmt);
38778 add_stmt (substmt);
38779 }
38780
38781 while (1)
38782 {
38783 tok = cp_lexer_peek_token (parser->lexer);
38784 if (tok->type == CPP_CLOSE_BRACE)
38785 break;
38786 if (tok->type == CPP_EOF)
38787 break;
38788
38789 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
38790 {
38791 cp_lexer_consume_token (parser->lexer);
38792 cp_parser_require_pragma_eol (parser, tok);
38793 error_suppress = false;
38794 }
38795 else if (!error_suppress)
38796 {
38797 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
38798 error_suppress = true;
38799 }
38800
38801 substmt = cp_parser_omp_structured_block (parser, NULL);
38802 substmt = build1 (OMP_SECTION, void_type_node, substmt);
38803 add_stmt (substmt);
38804 }
38805 braces.require_close (parser);
38806
38807 substmt = pop_stmt_list (stmt);
38808
38809 stmt = make_node (OMP_SECTIONS);
38810 TREE_TYPE (stmt) = void_type_node;
38811 OMP_SECTIONS_BODY (stmt) = substmt;
38812
38813 add_stmt (stmt);
38814 return stmt;
38815 }
38816
38817 /* OpenMP 2.5:
38818 # pragma omp sections sections-clause[optseq] newline
38819 sections-scope */
38820
38821 #define OMP_SECTIONS_CLAUSE_MASK \
38822 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38827
38828 static tree
38829 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
38830 char *p_name, omp_clause_mask mask, tree *cclauses)
38831 {
38832 tree clauses, ret;
38833 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38834
38835 strcat (p_name, " sections");
38836 mask |= OMP_SECTIONS_CLAUSE_MASK;
38837 if (cclauses)
38838 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
38839
38840 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38841 cclauses == NULL);
38842 if (cclauses)
38843 {
38844 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
38845 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
38846 }
38847
38848 ret = cp_parser_omp_sections_scope (parser);
38849 if (ret)
38850 OMP_SECTIONS_CLAUSES (ret) = clauses;
38851
38852 return ret;
38853 }
38854
38855 /* OpenMP 2.5:
38856 # pragma omp parallel parallel-clause[optseq] new-line
38857 structured-block
38858 # pragma omp parallel for parallel-for-clause[optseq] new-line
38859 structured-block
38860 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
38861 structured-block
38862
38863 OpenMP 4.0:
38864 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
38865 structured-block */
38866
38867 #define OMP_PARALLEL_CLAUSE_MASK \
38868 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
38872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
38874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
38876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
38877
38878 static tree
38879 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
38880 char *p_name, omp_clause_mask mask, tree *cclauses,
38881 bool *if_p)
38882 {
38883 tree stmt, clauses, block;
38884 unsigned int save;
38885 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38886
38887 strcat (p_name, " parallel");
38888 mask |= OMP_PARALLEL_CLAUSE_MASK;
38889 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
38890 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
38891 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
38892 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
38893
38894 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
38895 {
38896 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38897 if (cclauses == NULL)
38898 cclauses = cclauses_buf;
38899
38900 cp_lexer_consume_token (parser->lexer);
38901 if (!flag_openmp) /* flag_openmp_simd */
38902 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
38903 if_p);
38904 block = begin_omp_parallel ();
38905 save = cp_parser_begin_omp_structured_block (parser);
38906 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
38907 if_p);
38908 cp_parser_end_omp_structured_block (parser, save);
38909 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
38910 block);
38911 if (ret == NULL_TREE)
38912 return ret;
38913 OMP_PARALLEL_COMBINED (stmt) = 1;
38914 return stmt;
38915 }
38916 /* When combined with distribute, parallel has to be followed by for.
38917 #pragma omp target parallel is allowed though. */
38918 else if (cclauses
38919 && (mask & (OMP_CLAUSE_MASK_1
38920 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
38921 {
38922 error_at (loc, "expected %<for%> after %qs", p_name);
38923 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38924 return NULL_TREE;
38925 }
38926 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38927 {
38928 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38929 const char *p = IDENTIFIER_POINTER (id);
38930 if (cclauses == NULL && strcmp (p, "master") == 0)
38931 {
38932 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38933 cclauses = cclauses_buf;
38934
38935 cp_lexer_consume_token (parser->lexer);
38936 block = begin_omp_parallel ();
38937 save = cp_parser_begin_omp_structured_block (parser);
38938 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
38939 cclauses, if_p);
38940 cp_parser_end_omp_structured_block (parser, save);
38941 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
38942 block);
38943 OMP_PARALLEL_COMBINED (stmt) = 1;
38944 if (ret == NULL_TREE)
38945 return ret;
38946 return stmt;
38947 }
38948 else if (strcmp (p, "loop") == 0)
38949 {
38950 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38951 if (cclauses == NULL)
38952 cclauses = cclauses_buf;
38953
38954 cp_lexer_consume_token (parser->lexer);
38955 if (!flag_openmp) /* flag_openmp_simd */
38956 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
38957 cclauses, if_p);
38958 block = begin_omp_parallel ();
38959 save = cp_parser_begin_omp_structured_block (parser);
38960 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
38961 cclauses, if_p);
38962 cp_parser_end_omp_structured_block (parser, save);
38963 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
38964 block);
38965 if (ret == NULL_TREE)
38966 return ret;
38967 OMP_PARALLEL_COMBINED (stmt) = 1;
38968 return stmt;
38969 }
38970 else if (!flag_openmp) /* flag_openmp_simd */
38971 {
38972 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38973 return NULL_TREE;
38974 }
38975 else if (cclauses == NULL && strcmp (p, "sections") == 0)
38976 {
38977 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38978 cclauses = cclauses_buf;
38979
38980 cp_lexer_consume_token (parser->lexer);
38981 block = begin_omp_parallel ();
38982 save = cp_parser_begin_omp_structured_block (parser);
38983 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
38984 cp_parser_end_omp_structured_block (parser, save);
38985 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
38986 block);
38987 OMP_PARALLEL_COMBINED (stmt) = 1;
38988 return stmt;
38989 }
38990 }
38991 else if (!flag_openmp) /* flag_openmp_simd */
38992 {
38993 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38994 return NULL_TREE;
38995 }
38996
38997 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38998 cclauses == NULL);
38999 if (cclauses)
39000 {
39001 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
39002 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
39003 }
39004
39005 block = begin_omp_parallel ();
39006 save = cp_parser_begin_omp_structured_block (parser);
39007 cp_parser_statement (parser, NULL_TREE, false, if_p);
39008 cp_parser_end_omp_structured_block (parser, save);
39009 stmt = finish_omp_parallel (clauses, block);
39010 return stmt;
39011 }
39012
39013 /* OpenMP 2.5:
39014 # pragma omp single single-clause[optseq] new-line
39015 structured-block */
39016
39017 #define OMP_SINGLE_CLAUSE_MASK \
39018 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
39021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39022
39023 static tree
39024 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39025 {
39026 tree stmt = make_node (OMP_SINGLE);
39027 TREE_TYPE (stmt) = void_type_node;
39028 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39029
39030 OMP_SINGLE_CLAUSES (stmt)
39031 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
39032 "#pragma omp single", pragma_tok);
39033 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
39034
39035 return add_stmt (stmt);
39036 }
39037
39038 /* OpenMP 3.0:
39039 # pragma omp task task-clause[optseq] new-line
39040 structured-block */
39041
39042 #define OMP_TASK_CLAUSE_MASK \
39043 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
39052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39054
39055 static tree
39056 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39057 {
39058 tree clauses, block;
39059 unsigned int save;
39060
39061 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
39062 "#pragma omp task", pragma_tok);
39063 block = begin_omp_task ();
39064 save = cp_parser_begin_omp_structured_block (parser);
39065 cp_parser_statement (parser, NULL_TREE, false, if_p);
39066 cp_parser_end_omp_structured_block (parser, save);
39067 return finish_omp_task (clauses, block);
39068 }
39069
39070 /* OpenMP 3.0:
39071 # pragma omp taskwait new-line
39072
39073 OpenMP 5.0:
39074 # pragma omp taskwait taskwait-clause[opt] new-line */
39075
39076 #define OMP_TASKWAIT_CLAUSE_MASK \
39077 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
39078
39079 static void
39080 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
39081 {
39082 tree clauses
39083 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
39084 "#pragma omp taskwait", pragma_tok);
39085
39086 if (clauses)
39087 {
39088 tree stmt = make_node (OMP_TASK);
39089 TREE_TYPE (stmt) = void_node;
39090 OMP_TASK_CLAUSES (stmt) = clauses;
39091 OMP_TASK_BODY (stmt) = NULL_TREE;
39092 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39093 add_stmt (stmt);
39094 }
39095 else
39096 finish_omp_taskwait ();
39097 }
39098
39099 /* OpenMP 3.1:
39100 # pragma omp taskyield new-line */
39101
39102 static void
39103 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
39104 {
39105 cp_parser_require_pragma_eol (parser, pragma_tok);
39106 finish_omp_taskyield ();
39107 }
39108
39109 /* OpenMP 4.0:
39110 # pragma omp taskgroup new-line
39111 structured-block
39112
39113 OpenMP 5.0:
39114 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
39115
39116 #define OMP_TASKGROUP_CLAUSE_MASK \
39117 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
39118
39119 static tree
39120 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39121 {
39122 tree clauses
39123 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
39124 "#pragma omp taskgroup", pragma_tok);
39125 return c_finish_omp_taskgroup (input_location,
39126 cp_parser_omp_structured_block (parser,
39127 if_p),
39128 clauses);
39129 }
39130
39131
39132 /* OpenMP 2.5:
39133 # pragma omp threadprivate (variable-list) */
39134
39135 static void
39136 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
39137 {
39138 tree vars;
39139
39140 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
39141 cp_parser_require_pragma_eol (parser, pragma_tok);
39142
39143 finish_omp_threadprivate (vars);
39144 }
39145
39146 /* OpenMP 4.0:
39147 # pragma omp cancel cancel-clause[optseq] new-line */
39148
39149 #define OMP_CANCEL_CLAUSE_MASK \
39150 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
39151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
39152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
39153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
39154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
39155
39156 static void
39157 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
39158 {
39159 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
39160 "#pragma omp cancel", pragma_tok);
39161 finish_omp_cancel (clauses);
39162 }
39163
39164 /* OpenMP 4.0:
39165 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
39166
39167 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
39168 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
39169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
39170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
39171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
39172
39173 static void
39174 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
39175 enum pragma_context context)
39176 {
39177 tree clauses;
39178 bool point_seen = false;
39179
39180 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39181 {
39182 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39183 const char *p = IDENTIFIER_POINTER (id);
39184
39185 if (strcmp (p, "point") == 0)
39186 {
39187 cp_lexer_consume_token (parser->lexer);
39188 point_seen = true;
39189 }
39190 }
39191 if (!point_seen)
39192 {
39193 cp_parser_error (parser, "expected %<point%>");
39194 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39195 return;
39196 }
39197
39198 if (context != pragma_compound)
39199 {
39200 if (context == pragma_stmt)
39201 error_at (pragma_tok->location,
39202 "%<#pragma %s%> may only be used in compound statements",
39203 "omp cancellation point");
39204 else
39205 cp_parser_error (parser, "expected declaration specifiers");
39206 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39207 return;
39208 }
39209
39210 clauses = cp_parser_omp_all_clauses (parser,
39211 OMP_CANCELLATION_POINT_CLAUSE_MASK,
39212 "#pragma omp cancellation point",
39213 pragma_tok);
39214 finish_omp_cancellation_point (clauses);
39215 }
39216
39217 /* OpenMP 4.0:
39218 #pragma omp distribute distribute-clause[optseq] new-line
39219 for-loop */
39220
39221 #define OMP_DISTRIBUTE_CLAUSE_MASK \
39222 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
39226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
39227
39228 static tree
39229 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
39230 char *p_name, omp_clause_mask mask, tree *cclauses,
39231 bool *if_p)
39232 {
39233 tree clauses, sb, ret;
39234 unsigned int save;
39235 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39236
39237 strcat (p_name, " distribute");
39238 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
39239
39240 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39241 {
39242 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39243 const char *p = IDENTIFIER_POINTER (id);
39244 bool simd = false;
39245 bool parallel = false;
39246
39247 if (strcmp (p, "simd") == 0)
39248 simd = true;
39249 else
39250 parallel = strcmp (p, "parallel") == 0;
39251 if (parallel || simd)
39252 {
39253 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39254 if (cclauses == NULL)
39255 cclauses = cclauses_buf;
39256 cp_lexer_consume_token (parser->lexer);
39257 if (!flag_openmp) /* flag_openmp_simd */
39258 {
39259 if (simd)
39260 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39261 cclauses, if_p);
39262 else
39263 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
39264 cclauses, if_p);
39265 }
39266 sb = begin_omp_structured_block ();
39267 save = cp_parser_begin_omp_structured_block (parser);
39268 if (simd)
39269 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39270 cclauses, if_p);
39271 else
39272 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
39273 cclauses, if_p);
39274 cp_parser_end_omp_structured_block (parser, save);
39275 tree body = finish_omp_structured_block (sb);
39276 if (ret == NULL)
39277 return ret;
39278 ret = make_node (OMP_DISTRIBUTE);
39279 TREE_TYPE (ret) = void_type_node;
39280 OMP_FOR_BODY (ret) = body;
39281 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
39282 SET_EXPR_LOCATION (ret, loc);
39283 add_stmt (ret);
39284 return ret;
39285 }
39286 }
39287 if (!flag_openmp) /* flag_openmp_simd */
39288 {
39289 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39290 return NULL_TREE;
39291 }
39292
39293 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39294 cclauses == NULL);
39295 if (cclauses)
39296 {
39297 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
39298 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
39299 }
39300
39301 keep_next_level (true);
39302 sb = begin_omp_structured_block ();
39303 save = cp_parser_begin_omp_structured_block (parser);
39304
39305 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
39306
39307 cp_parser_end_omp_structured_block (parser, save);
39308 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39309
39310 return ret;
39311 }
39312
39313 /* OpenMP 4.0:
39314 # pragma omp teams teams-clause[optseq] new-line
39315 structured-block */
39316
39317 #define OMP_TEAMS_CLAUSE_MASK \
39318 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
39323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
39324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
39325
39326 static tree
39327 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
39328 char *p_name, omp_clause_mask mask, tree *cclauses,
39329 bool *if_p)
39330 {
39331 tree clauses, sb, ret;
39332 unsigned int save;
39333 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39334
39335 strcat (p_name, " teams");
39336 mask |= OMP_TEAMS_CLAUSE_MASK;
39337
39338 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39339 {
39340 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39341 const char *p = IDENTIFIER_POINTER (id);
39342 if (strcmp (p, "distribute") == 0)
39343 {
39344 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39345 if (cclauses == NULL)
39346 cclauses = cclauses_buf;
39347
39348 cp_lexer_consume_token (parser->lexer);
39349 if (!flag_openmp) /* flag_openmp_simd */
39350 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
39351 cclauses, if_p);
39352 keep_next_level (true);
39353 sb = begin_omp_structured_block ();
39354 save = cp_parser_begin_omp_structured_block (parser);
39355 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
39356 cclauses, if_p);
39357 cp_parser_end_omp_structured_block (parser, save);
39358 tree body = finish_omp_structured_block (sb);
39359 if (ret == NULL)
39360 return ret;
39361 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
39362 ret = make_node (OMP_TEAMS);
39363 TREE_TYPE (ret) = void_type_node;
39364 OMP_TEAMS_CLAUSES (ret) = clauses;
39365 OMP_TEAMS_BODY (ret) = body;
39366 OMP_TEAMS_COMBINED (ret) = 1;
39367 SET_EXPR_LOCATION (ret, loc);
39368 return add_stmt (ret);
39369 }
39370 else if (strcmp (p, "loop") == 0)
39371 {
39372 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39373 if (cclauses == NULL)
39374 cclauses = cclauses_buf;
39375
39376 cp_lexer_consume_token (parser->lexer);
39377 if (!flag_openmp) /* flag_openmp_simd */
39378 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
39379 cclauses, if_p);
39380 keep_next_level (true);
39381 sb = begin_omp_structured_block ();
39382 save = cp_parser_begin_omp_structured_block (parser);
39383 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
39384 cclauses, if_p);
39385 cp_parser_end_omp_structured_block (parser, save);
39386 tree body = finish_omp_structured_block (sb);
39387 if (ret == NULL)
39388 return ret;
39389 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
39390 ret = make_node (OMP_TEAMS);
39391 TREE_TYPE (ret) = void_type_node;
39392 OMP_TEAMS_CLAUSES (ret) = clauses;
39393 OMP_TEAMS_BODY (ret) = body;
39394 OMP_TEAMS_COMBINED (ret) = 1;
39395 SET_EXPR_LOCATION (ret, loc);
39396 return add_stmt (ret);
39397 }
39398 }
39399 if (!flag_openmp) /* flag_openmp_simd */
39400 {
39401 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39402 return NULL_TREE;
39403 }
39404
39405 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39406 cclauses == NULL);
39407 if (cclauses)
39408 {
39409 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
39410 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
39411 }
39412
39413 tree stmt = make_node (OMP_TEAMS);
39414 TREE_TYPE (stmt) = void_type_node;
39415 OMP_TEAMS_CLAUSES (stmt) = clauses;
39416 keep_next_level (true);
39417 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
39418 SET_EXPR_LOCATION (stmt, loc);
39419
39420 return add_stmt (stmt);
39421 }
39422
39423 /* OpenMP 4.0:
39424 # pragma omp target data target-data-clause[optseq] new-line
39425 structured-block */
39426
39427 #define OMP_TARGET_DATA_CLAUSE_MASK \
39428 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
39429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
39430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
39432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
39433
39434 static tree
39435 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39436 {
39437 tree clauses
39438 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
39439 "#pragma omp target data", pragma_tok);
39440 int map_seen = 0;
39441 for (tree *pc = &clauses; *pc;)
39442 {
39443 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
39444 switch (OMP_CLAUSE_MAP_KIND (*pc))
39445 {
39446 case GOMP_MAP_TO:
39447 case GOMP_MAP_ALWAYS_TO:
39448 case GOMP_MAP_FROM:
39449 case GOMP_MAP_ALWAYS_FROM:
39450 case GOMP_MAP_TOFROM:
39451 case GOMP_MAP_ALWAYS_TOFROM:
39452 case GOMP_MAP_ALLOC:
39453 map_seen = 3;
39454 break;
39455 case GOMP_MAP_FIRSTPRIVATE_POINTER:
39456 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
39457 case GOMP_MAP_ALWAYS_POINTER:
39458 break;
39459 default:
39460 map_seen |= 1;
39461 error_at (OMP_CLAUSE_LOCATION (*pc),
39462 "%<#pragma omp target data%> with map-type other "
39463 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
39464 "on %<map%> clause");
39465 *pc = OMP_CLAUSE_CHAIN (*pc);
39466 continue;
39467 }
39468 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
39469 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
39470 map_seen = 3;
39471 pc = &OMP_CLAUSE_CHAIN (*pc);
39472 }
39473
39474 if (map_seen != 3)
39475 {
39476 if (map_seen == 0)
39477 error_at (pragma_tok->location,
39478 "%<#pragma omp target data%> must contain at least "
39479 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
39480 "clause");
39481 return NULL_TREE;
39482 }
39483
39484 tree stmt = make_node (OMP_TARGET_DATA);
39485 TREE_TYPE (stmt) = void_type_node;
39486 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
39487
39488 keep_next_level (true);
39489 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
39490
39491 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39492 return add_stmt (stmt);
39493 }
39494
39495 /* OpenMP 4.5:
39496 # pragma omp target enter data target-enter-data-clause[optseq] new-line
39497 structured-block */
39498
39499 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
39500 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
39501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
39502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
39504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39505
39506 static tree
39507 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
39508 enum pragma_context context)
39509 {
39510 bool data_seen = false;
39511 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39512 {
39513 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39514 const char *p = IDENTIFIER_POINTER (id);
39515
39516 if (strcmp (p, "data") == 0)
39517 {
39518 cp_lexer_consume_token (parser->lexer);
39519 data_seen = true;
39520 }
39521 }
39522 if (!data_seen)
39523 {
39524 cp_parser_error (parser, "expected %<data%>");
39525 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39526 return NULL_TREE;
39527 }
39528
39529 if (context == pragma_stmt)
39530 {
39531 error_at (pragma_tok->location,
39532 "%<#pragma %s%> may only be used in compound statements",
39533 "omp target enter data");
39534 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39535 return NULL_TREE;
39536 }
39537
39538 tree clauses
39539 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
39540 "#pragma omp target enter data", pragma_tok);
39541 int map_seen = 0;
39542 for (tree *pc = &clauses; *pc;)
39543 {
39544 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
39545 switch (OMP_CLAUSE_MAP_KIND (*pc))
39546 {
39547 case GOMP_MAP_TO:
39548 case GOMP_MAP_ALWAYS_TO:
39549 case GOMP_MAP_ALLOC:
39550 map_seen = 3;
39551 break;
39552 case GOMP_MAP_FIRSTPRIVATE_POINTER:
39553 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
39554 case GOMP_MAP_ALWAYS_POINTER:
39555 break;
39556 default:
39557 map_seen |= 1;
39558 error_at (OMP_CLAUSE_LOCATION (*pc),
39559 "%<#pragma omp target enter data%> with map-type other "
39560 "than %<to%> or %<alloc%> on %<map%> clause");
39561 *pc = OMP_CLAUSE_CHAIN (*pc);
39562 continue;
39563 }
39564 pc = &OMP_CLAUSE_CHAIN (*pc);
39565 }
39566
39567 if (map_seen != 3)
39568 {
39569 if (map_seen == 0)
39570 error_at (pragma_tok->location,
39571 "%<#pragma omp target enter data%> must contain at least "
39572 "one %<map%> clause");
39573 return NULL_TREE;
39574 }
39575
39576 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
39577 TREE_TYPE (stmt) = void_type_node;
39578 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
39579 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39580 return add_stmt (stmt);
39581 }
39582
39583 /* OpenMP 4.5:
39584 # pragma omp target exit data target-enter-data-clause[optseq] new-line
39585 structured-block */
39586
39587 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
39588 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
39589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
39590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
39592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39593
39594 static tree
39595 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
39596 enum pragma_context context)
39597 {
39598 bool data_seen = false;
39599 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39600 {
39601 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39602 const char *p = IDENTIFIER_POINTER (id);
39603
39604 if (strcmp (p, "data") == 0)
39605 {
39606 cp_lexer_consume_token (parser->lexer);
39607 data_seen = true;
39608 }
39609 }
39610 if (!data_seen)
39611 {
39612 cp_parser_error (parser, "expected %<data%>");
39613 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39614 return NULL_TREE;
39615 }
39616
39617 if (context == pragma_stmt)
39618 {
39619 error_at (pragma_tok->location,
39620 "%<#pragma %s%> may only be used in compound statements",
39621 "omp target exit data");
39622 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39623 return NULL_TREE;
39624 }
39625
39626 tree clauses
39627 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
39628 "#pragma omp target exit data", pragma_tok);
39629 int map_seen = 0;
39630 for (tree *pc = &clauses; *pc;)
39631 {
39632 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
39633 switch (OMP_CLAUSE_MAP_KIND (*pc))
39634 {
39635 case GOMP_MAP_FROM:
39636 case GOMP_MAP_ALWAYS_FROM:
39637 case GOMP_MAP_RELEASE:
39638 case GOMP_MAP_DELETE:
39639 map_seen = 3;
39640 break;
39641 case GOMP_MAP_FIRSTPRIVATE_POINTER:
39642 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
39643 case GOMP_MAP_ALWAYS_POINTER:
39644 break;
39645 default:
39646 map_seen |= 1;
39647 error_at (OMP_CLAUSE_LOCATION (*pc),
39648 "%<#pragma omp target exit data%> with map-type other "
39649 "than %<from%>, %<release%> or %<delete%> on %<map%>"
39650 " clause");
39651 *pc = OMP_CLAUSE_CHAIN (*pc);
39652 continue;
39653 }
39654 pc = &OMP_CLAUSE_CHAIN (*pc);
39655 }
39656
39657 if (map_seen != 3)
39658 {
39659 if (map_seen == 0)
39660 error_at (pragma_tok->location,
39661 "%<#pragma omp target exit data%> must contain at least "
39662 "one %<map%> clause");
39663 return NULL_TREE;
39664 }
39665
39666 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
39667 TREE_TYPE (stmt) = void_type_node;
39668 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
39669 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39670 return add_stmt (stmt);
39671 }
39672
39673 /* OpenMP 4.0:
39674 # pragma omp target update target-update-clause[optseq] new-line */
39675
39676 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
39677 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
39678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
39680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
39682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39683
39684 static bool
39685 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
39686 enum pragma_context context)
39687 {
39688 if (context == pragma_stmt)
39689 {
39690 error_at (pragma_tok->location,
39691 "%<#pragma %s%> may only be used in compound statements",
39692 "omp target update");
39693 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39694 return false;
39695 }
39696
39697 tree clauses
39698 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
39699 "#pragma omp target update", pragma_tok);
39700 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
39701 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
39702 {
39703 error_at (pragma_tok->location,
39704 "%<#pragma omp target update%> must contain at least one "
39705 "%<from%> or %<to%> clauses");
39706 return false;
39707 }
39708
39709 tree stmt = make_node (OMP_TARGET_UPDATE);
39710 TREE_TYPE (stmt) = void_type_node;
39711 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
39712 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39713 add_stmt (stmt);
39714 return false;
39715 }
39716
39717 /* OpenMP 4.0:
39718 # pragma omp target target-clause[optseq] new-line
39719 structured-block */
39720
39721 #define OMP_TARGET_CLAUSE_MASK \
39722 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
39723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
39724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
39726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
39727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
39730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
39731
39732 static bool
39733 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
39734 enum pragma_context context, bool *if_p)
39735 {
39736 tree *pc = NULL, stmt;
39737
39738 if (flag_openmp)
39739 omp_requires_mask
39740 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
39741
39742 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39743 {
39744 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39745 const char *p = IDENTIFIER_POINTER (id);
39746 enum tree_code ccode = ERROR_MARK;
39747
39748 if (strcmp (p, "teams") == 0)
39749 ccode = OMP_TEAMS;
39750 else if (strcmp (p, "parallel") == 0)
39751 ccode = OMP_PARALLEL;
39752 else if (strcmp (p, "simd") == 0)
39753 ccode = OMP_SIMD;
39754 if (ccode != ERROR_MARK)
39755 {
39756 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
39757 char p_name[sizeof ("#pragma omp target teams distribute "
39758 "parallel for simd")];
39759
39760 cp_lexer_consume_token (parser->lexer);
39761 strcpy (p_name, "#pragma omp target");
39762 if (!flag_openmp) /* flag_openmp_simd */
39763 {
39764 tree stmt;
39765 switch (ccode)
39766 {
39767 case OMP_TEAMS:
39768 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
39769 OMP_TARGET_CLAUSE_MASK,
39770 cclauses, if_p);
39771 break;
39772 case OMP_PARALLEL:
39773 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
39774 OMP_TARGET_CLAUSE_MASK,
39775 cclauses, if_p);
39776 break;
39777 case OMP_SIMD:
39778 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
39779 OMP_TARGET_CLAUSE_MASK,
39780 cclauses, if_p);
39781 break;
39782 default:
39783 gcc_unreachable ();
39784 }
39785 return stmt != NULL_TREE;
39786 }
39787 keep_next_level (true);
39788 tree sb = begin_omp_structured_block (), ret;
39789 unsigned save = cp_parser_begin_omp_structured_block (parser);
39790 switch (ccode)
39791 {
39792 case OMP_TEAMS:
39793 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
39794 OMP_TARGET_CLAUSE_MASK, cclauses,
39795 if_p);
39796 break;
39797 case OMP_PARALLEL:
39798 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
39799 OMP_TARGET_CLAUSE_MASK, cclauses,
39800 if_p);
39801 break;
39802 case OMP_SIMD:
39803 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
39804 OMP_TARGET_CLAUSE_MASK, cclauses,
39805 if_p);
39806 break;
39807 default:
39808 gcc_unreachable ();
39809 }
39810 cp_parser_end_omp_structured_block (parser, save);
39811 tree body = finish_omp_structured_block (sb);
39812 if (ret == NULL_TREE)
39813 return false;
39814 if (ccode == OMP_TEAMS && !processing_template_decl)
39815 {
39816 /* For combined target teams, ensure the num_teams and
39817 thread_limit clause expressions are evaluated on the host,
39818 before entering the target construct. */
39819 tree c;
39820 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
39821 c; c = OMP_CLAUSE_CHAIN (c))
39822 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
39823 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
39824 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
39825 {
39826 tree expr = OMP_CLAUSE_OPERAND (c, 0);
39827 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
39828 if (expr == error_mark_node)
39829 continue;
39830 tree tmp = TARGET_EXPR_SLOT (expr);
39831 add_stmt (expr);
39832 OMP_CLAUSE_OPERAND (c, 0) = expr;
39833 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
39834 OMP_CLAUSE_FIRSTPRIVATE);
39835 OMP_CLAUSE_DECL (tc) = tmp;
39836 OMP_CLAUSE_CHAIN (tc)
39837 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
39838 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
39839 }
39840 }
39841 tree stmt = make_node (OMP_TARGET);
39842 TREE_TYPE (stmt) = void_type_node;
39843 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
39844 OMP_TARGET_BODY (stmt) = body;
39845 OMP_TARGET_COMBINED (stmt) = 1;
39846 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39847 add_stmt (stmt);
39848 pc = &OMP_TARGET_CLAUSES (stmt);
39849 goto check_clauses;
39850 }
39851 else if (!flag_openmp) /* flag_openmp_simd */
39852 {
39853 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39854 return false;
39855 }
39856 else if (strcmp (p, "data") == 0)
39857 {
39858 cp_lexer_consume_token (parser->lexer);
39859 cp_parser_omp_target_data (parser, pragma_tok, if_p);
39860 return true;
39861 }
39862 else if (strcmp (p, "enter") == 0)
39863 {
39864 cp_lexer_consume_token (parser->lexer);
39865 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
39866 return false;
39867 }
39868 else if (strcmp (p, "exit") == 0)
39869 {
39870 cp_lexer_consume_token (parser->lexer);
39871 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
39872 return false;
39873 }
39874 else if (strcmp (p, "update") == 0)
39875 {
39876 cp_lexer_consume_token (parser->lexer);
39877 return cp_parser_omp_target_update (parser, pragma_tok, context);
39878 }
39879 }
39880 if (!flag_openmp) /* flag_openmp_simd */
39881 {
39882 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39883 return false;
39884 }
39885
39886 stmt = make_node (OMP_TARGET);
39887 TREE_TYPE (stmt) = void_type_node;
39888
39889 OMP_TARGET_CLAUSES (stmt)
39890 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
39891 "#pragma omp target", pragma_tok);
39892 pc = &OMP_TARGET_CLAUSES (stmt);
39893 keep_next_level (true);
39894 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
39895
39896 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39897 add_stmt (stmt);
39898
39899 check_clauses:
39900 while (*pc)
39901 {
39902 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
39903 switch (OMP_CLAUSE_MAP_KIND (*pc))
39904 {
39905 case GOMP_MAP_TO:
39906 case GOMP_MAP_ALWAYS_TO:
39907 case GOMP_MAP_FROM:
39908 case GOMP_MAP_ALWAYS_FROM:
39909 case GOMP_MAP_TOFROM:
39910 case GOMP_MAP_ALWAYS_TOFROM:
39911 case GOMP_MAP_ALLOC:
39912 case GOMP_MAP_FIRSTPRIVATE_POINTER:
39913 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
39914 case GOMP_MAP_ALWAYS_POINTER:
39915 break;
39916 default:
39917 error_at (OMP_CLAUSE_LOCATION (*pc),
39918 "%<#pragma omp target%> with map-type other "
39919 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
39920 "on %<map%> clause");
39921 *pc = OMP_CLAUSE_CHAIN (*pc);
39922 continue;
39923 }
39924 pc = &OMP_CLAUSE_CHAIN (*pc);
39925 }
39926 return true;
39927 }
39928
39929 /* OpenACC 2.0:
39930 # pragma acc cache (variable-list) new-line
39931 */
39932
39933 static tree
39934 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
39935 {
39936 tree stmt, clauses;
39937
39938 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
39939 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
39940
39941 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
39942
39943 stmt = make_node (OACC_CACHE);
39944 TREE_TYPE (stmt) = void_type_node;
39945 OACC_CACHE_CLAUSES (stmt) = clauses;
39946 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39947 add_stmt (stmt);
39948
39949 return stmt;
39950 }
39951
39952 /* OpenACC 2.0:
39953 # pragma acc data oacc-data-clause[optseq] new-line
39954 structured-block */
39955
39956 #define OACC_DATA_CLAUSE_MASK \
39957 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
39964
39965 static tree
39966 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39967 {
39968 tree stmt, clauses, block;
39969 unsigned int save;
39970
39971 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
39972 "#pragma acc data", pragma_tok);
39973
39974 block = begin_omp_parallel ();
39975 save = cp_parser_begin_omp_structured_block (parser);
39976 cp_parser_statement (parser, NULL_TREE, false, if_p);
39977 cp_parser_end_omp_structured_block (parser, save);
39978 stmt = finish_oacc_data (clauses, block);
39979 return stmt;
39980 }
39981
39982 /* OpenACC 2.0:
39983 # pragma acc host_data <clauses> new-line
39984 structured-block */
39985
39986 #define OACC_HOST_DATA_CLAUSE_MASK \
39987 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
39988
39989 static tree
39990 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39991 {
39992 tree stmt, clauses, block;
39993 unsigned int save;
39994
39995 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
39996 "#pragma acc host_data", pragma_tok);
39997
39998 block = begin_omp_parallel ();
39999 save = cp_parser_begin_omp_structured_block (parser);
40000 cp_parser_statement (parser, NULL_TREE, false, if_p);
40001 cp_parser_end_omp_structured_block (parser, save);
40002 stmt = finish_oacc_host_data (clauses, block);
40003 return stmt;
40004 }
40005
40006 /* OpenACC 2.0:
40007 # pragma acc declare oacc-data-clause[optseq] new-line
40008 */
40009
40010 #define OACC_DECLARE_CLAUSE_MASK \
40011 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
40017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
40018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
40019
40020 static tree
40021 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
40022 {
40023 tree clauses, stmt;
40024 bool error = false;
40025
40026 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
40027 "#pragma acc declare", pragma_tok, true);
40028
40029
40030 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
40031 {
40032 error_at (pragma_tok->location,
40033 "no valid clauses specified in %<#pragma acc declare%>");
40034 return NULL_TREE;
40035 }
40036
40037 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
40038 {
40039 location_t loc = OMP_CLAUSE_LOCATION (t);
40040 tree decl = OMP_CLAUSE_DECL (t);
40041 if (!DECL_P (decl))
40042 {
40043 error_at (loc, "array section in %<#pragma acc declare%>");
40044 error = true;
40045 continue;
40046 }
40047 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
40048 switch (OMP_CLAUSE_MAP_KIND (t))
40049 {
40050 case GOMP_MAP_FIRSTPRIVATE_POINTER:
40051 case GOMP_MAP_ALLOC:
40052 case GOMP_MAP_TO:
40053 case GOMP_MAP_FORCE_DEVICEPTR:
40054 case GOMP_MAP_DEVICE_RESIDENT:
40055 break;
40056
40057 case GOMP_MAP_LINK:
40058 if (!global_bindings_p ()
40059 && (TREE_STATIC (decl)
40060 || !DECL_EXTERNAL (decl)))
40061 {
40062 error_at (loc,
40063 "%qD must be a global variable in "
40064 "%<#pragma acc declare link%>",
40065 decl);
40066 error = true;
40067 continue;
40068 }
40069 break;
40070
40071 default:
40072 if (global_bindings_p ())
40073 {
40074 error_at (loc, "invalid OpenACC clause at file scope");
40075 error = true;
40076 continue;
40077 }
40078 if (DECL_EXTERNAL (decl))
40079 {
40080 error_at (loc,
40081 "invalid use of %<extern%> variable %qD "
40082 "in %<#pragma acc declare%>", decl);
40083 error = true;
40084 continue;
40085 }
40086 else if (TREE_PUBLIC (decl))
40087 {
40088 error_at (loc,
40089 "invalid use of %<global%> variable %qD "
40090 "in %<#pragma acc declare%>", decl);
40091 error = true;
40092 continue;
40093 }
40094 break;
40095 }
40096
40097 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
40098 || lookup_attribute ("omp declare target link",
40099 DECL_ATTRIBUTES (decl)))
40100 {
40101 error_at (loc, "variable %qD used more than once with "
40102 "%<#pragma acc declare%>", decl);
40103 error = true;
40104 continue;
40105 }
40106
40107 if (!error)
40108 {
40109 tree id;
40110
40111 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
40112 id = get_identifier ("omp declare target link");
40113 else
40114 id = get_identifier ("omp declare target");
40115
40116 DECL_ATTRIBUTES (decl)
40117 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
40118 if (global_bindings_p ())
40119 {
40120 symtab_node *node = symtab_node::get (decl);
40121 if (node != NULL)
40122 {
40123 node->offloadable = 1;
40124 if (ENABLE_OFFLOADING)
40125 {
40126 g->have_offload = true;
40127 if (is_a <varpool_node *> (node))
40128 vec_safe_push (offload_vars, decl);
40129 }
40130 }
40131 }
40132 }
40133 }
40134
40135 if (error || global_bindings_p ())
40136 return NULL_TREE;
40137
40138 stmt = make_node (OACC_DECLARE);
40139 TREE_TYPE (stmt) = void_type_node;
40140 OACC_DECLARE_CLAUSES (stmt) = clauses;
40141 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40142
40143 add_stmt (stmt);
40144
40145 return NULL_TREE;
40146 }
40147
40148 /* OpenACC 2.0:
40149 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
40150
40151 or
40152
40153 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
40154
40155 LOC is the location of the #pragma token.
40156 */
40157
40158 #define OACC_ENTER_DATA_CLAUSE_MASK \
40159 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
40164
40165 #define OACC_EXIT_DATA_CLAUSE_MASK \
40166 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
40170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
40171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
40172
40173 static tree
40174 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
40175 bool enter)
40176 {
40177 location_t loc = pragma_tok->location;
40178 tree stmt, clauses;
40179 const char *p = "";
40180
40181 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40182 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
40183
40184 if (strcmp (p, "data") != 0)
40185 {
40186 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
40187 enter ? "enter" : "exit");
40188 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40189 return NULL_TREE;
40190 }
40191
40192 cp_lexer_consume_token (parser->lexer);
40193
40194 if (enter)
40195 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
40196 "#pragma acc enter data", pragma_tok);
40197 else
40198 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
40199 "#pragma acc exit data", pragma_tok);
40200
40201 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
40202 {
40203 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
40204 enter ? "enter" : "exit");
40205 return NULL_TREE;
40206 }
40207
40208 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
40209 TREE_TYPE (stmt) = void_type_node;
40210 OMP_STANDALONE_CLAUSES (stmt) = clauses;
40211 SET_EXPR_LOCATION (stmt, loc);
40212 add_stmt (stmt);
40213 return stmt;
40214 }
40215
40216 /* OpenACC 2.0:
40217 # pragma acc loop oacc-loop-clause[optseq] new-line
40218 structured-block */
40219
40220 #define OACC_LOOP_CLAUSE_MASK \
40221 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
40222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
40223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
40224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
40225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
40226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
40227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
40228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
40229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
40230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
40231
40232 static tree
40233 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
40234 omp_clause_mask mask, tree *cclauses, bool *if_p)
40235 {
40236 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
40237
40238 strcat (p_name, " loop");
40239 mask |= OACC_LOOP_CLAUSE_MASK;
40240
40241 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
40242 cclauses == NULL);
40243 if (cclauses)
40244 {
40245 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
40246 if (*cclauses)
40247 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
40248 if (clauses)
40249 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
40250 }
40251
40252 tree block = begin_omp_structured_block ();
40253 int save = cp_parser_begin_omp_structured_block (parser);
40254 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
40255 cp_parser_end_omp_structured_block (parser, save);
40256 add_stmt (finish_omp_structured_block (block));
40257
40258 return stmt;
40259 }
40260
40261 /* OpenACC 2.0:
40262 # pragma acc kernels oacc-kernels-clause[optseq] new-line
40263 structured-block
40264
40265 or
40266
40267 # pragma acc parallel oacc-parallel-clause[optseq] new-line
40268 structured-block
40269
40270 OpenACC 2.6:
40271
40272 # pragma acc serial oacc-serial-clause[optseq] new-line
40273 */
40274
40275 #define OACC_KERNELS_CLAUSE_MASK \
40276 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
40282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
40285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
40286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
40287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
40288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
40289
40290 #define OACC_PARALLEL_CLAUSE_MASK \
40291 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
40297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
40299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
40301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
40302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
40303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
40304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
40305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
40306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
40307
40308 #define OACC_SERIAL_CLAUSE_MASK \
40309 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
40315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
40318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
40319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
40320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
40321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
40322
40323 static tree
40324 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
40325 char *p_name, bool *if_p)
40326 {
40327 omp_clause_mask mask;
40328 enum tree_code code;
40329 switch (cp_parser_pragma_kind (pragma_tok))
40330 {
40331 case PRAGMA_OACC_KERNELS:
40332 strcat (p_name, " kernels");
40333 mask = OACC_KERNELS_CLAUSE_MASK;
40334 code = OACC_KERNELS;
40335 break;
40336 case PRAGMA_OACC_PARALLEL:
40337 strcat (p_name, " parallel");
40338 mask = OACC_PARALLEL_CLAUSE_MASK;
40339 code = OACC_PARALLEL;
40340 break;
40341 case PRAGMA_OACC_SERIAL:
40342 strcat (p_name, " serial");
40343 mask = OACC_SERIAL_CLAUSE_MASK;
40344 code = OACC_SERIAL;
40345 break;
40346 default:
40347 gcc_unreachable ();
40348 }
40349
40350 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40351 {
40352 const char *p
40353 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
40354 if (strcmp (p, "loop") == 0)
40355 {
40356 cp_lexer_consume_token (parser->lexer);
40357 tree block = begin_omp_parallel ();
40358 tree clauses;
40359 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
40360 &clauses, if_p);
40361 protected_set_expr_location (stmt, pragma_tok->location);
40362 return finish_omp_construct (code, block, clauses);
40363 }
40364 }
40365
40366 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
40367
40368 tree block = begin_omp_parallel ();
40369 unsigned int save = cp_parser_begin_omp_structured_block (parser);
40370 cp_parser_statement (parser, NULL_TREE, false, if_p);
40371 cp_parser_end_omp_structured_block (parser, save);
40372 return finish_omp_construct (code, block, clauses);
40373 }
40374
40375 /* OpenACC 2.0:
40376 # pragma acc update oacc-update-clause[optseq] new-line
40377 */
40378
40379 #define OACC_UPDATE_CLAUSE_MASK \
40380 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
40382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
40383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
40385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
40386
40387 static tree
40388 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
40389 {
40390 tree stmt, clauses;
40391
40392 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
40393 "#pragma acc update", pragma_tok);
40394
40395 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
40396 {
40397 error_at (pragma_tok->location,
40398 "%<#pragma acc update%> must contain at least one "
40399 "%<device%> or %<host%> or %<self%> clause");
40400 return NULL_TREE;
40401 }
40402
40403 stmt = make_node (OACC_UPDATE);
40404 TREE_TYPE (stmt) = void_type_node;
40405 OACC_UPDATE_CLAUSES (stmt) = clauses;
40406 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40407 add_stmt (stmt);
40408 return stmt;
40409 }
40410
40411 /* OpenACC 2.0:
40412 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
40413
40414 LOC is the location of the #pragma token.
40415 */
40416
40417 #define OACC_WAIT_CLAUSE_MASK \
40418 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
40419
40420 static tree
40421 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
40422 {
40423 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
40424 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40425
40426 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40427 list = cp_parser_oacc_wait_list (parser, loc, list);
40428
40429 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
40430 "#pragma acc wait", pragma_tok);
40431
40432 stmt = c_finish_oacc_wait (loc, list, clauses);
40433 stmt = finish_expr_stmt (stmt);
40434
40435 return stmt;
40436 }
40437
40438 /* OpenMP 4.0:
40439 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
40440
40441 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
40442 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
40443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
40444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
40445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
40446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
40447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
40448
40449 static void
40450 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
40451 enum pragma_context context,
40452 bool variant_p)
40453 {
40454 bool first_p = parser->omp_declare_simd == NULL;
40455 cp_omp_declare_simd_data data;
40456 if (first_p)
40457 {
40458 data.error_seen = false;
40459 data.fndecl_seen = false;
40460 data.variant_p = variant_p;
40461 data.tokens = vNULL;
40462 data.clauses = NULL_TREE;
40463 /* It is safe to take the address of a local variable; it will only be
40464 used while this scope is live. */
40465 parser->omp_declare_simd = &data;
40466 }
40467 else if (parser->omp_declare_simd->variant_p != variant_p)
40468 {
40469 error_at (pragma_tok->location,
40470 "%<#pragma omp declare %s%> followed by "
40471 "%<#pragma omp declare %s%>",
40472 parser->omp_declare_simd->variant_p ? "variant" : "simd",
40473 parser->omp_declare_simd->variant_p ? "simd" : "variant");
40474 parser->omp_declare_simd->error_seen = true;
40475 }
40476
40477 /* Store away all pragma tokens. */
40478 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
40479 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
40480 cp_lexer_consume_token (parser->lexer);
40481 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40482 parser->omp_declare_simd->error_seen = true;
40483 cp_parser_require_pragma_eol (parser, pragma_tok);
40484 struct cp_token_cache *cp
40485 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
40486 parser->omp_declare_simd->tokens.safe_push (cp);
40487
40488 if (first_p)
40489 {
40490 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
40491 cp_parser_pragma (parser, context, NULL);
40492 switch (context)
40493 {
40494 case pragma_external:
40495 cp_parser_declaration (parser);
40496 break;
40497 case pragma_member:
40498 cp_parser_member_declaration (parser);
40499 break;
40500 case pragma_objc_icode:
40501 cp_parser_block_declaration (parser, /*statement_p=*/false);
40502 break;
40503 default:
40504 cp_parser_declaration_statement (parser);
40505 break;
40506 }
40507 if (parser->omp_declare_simd
40508 && !parser->omp_declare_simd->error_seen
40509 && !parser->omp_declare_simd->fndecl_seen)
40510 error_at (pragma_tok->location,
40511 "%<#pragma omp declare %s%> not immediately followed by "
40512 "function declaration or definition",
40513 parser->omp_declare_simd->variant_p ? "variant" : "simd");
40514 data.tokens.release ();
40515 parser->omp_declare_simd = NULL;
40516 }
40517 }
40518
40519 static const char *const omp_construct_selectors[] = {
40520 "simd", "target", "teams", "parallel", "for", NULL };
40521 static const char *const omp_device_selectors[] = {
40522 "kind", "isa", "arch", NULL };
40523 static const char *const omp_implementation_selectors[] = {
40524 "vendor", "extension", "atomic_default_mem_order", "unified_address",
40525 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
40526 static const char *const omp_user_selectors[] = {
40527 "condition", NULL };
40528
40529 /* OpenMP 5.0:
40530
40531 trait-selector:
40532 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
40533
40534 trait-score:
40535 score(score-expression) */
40536
40537 static tree
40538 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
40539 {
40540 tree ret = NULL_TREE;
40541 do
40542 {
40543 tree selector;
40544 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
40545 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40546 selector = cp_lexer_peek_token (parser->lexer)->u.value;
40547 else
40548 {
40549 cp_parser_error (parser, "expected trait selector name");
40550 return error_mark_node;
40551 }
40552
40553 tree properties = NULL_TREE;
40554 const char *const *selectors = NULL;
40555 bool allow_score = true;
40556 bool allow_user = false;
40557 int property_limit = 0;
40558 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
40559 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
40560 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
40561 switch (IDENTIFIER_POINTER (set)[0])
40562 {
40563 case 'c': /* construct */
40564 selectors = omp_construct_selectors;
40565 allow_score = false;
40566 property_limit = 1;
40567 property_kind = CTX_PROPERTY_SIMD;
40568 break;
40569 case 'd': /* device */
40570 selectors = omp_device_selectors;
40571 allow_score = false;
40572 allow_user = true;
40573 property_limit = 3;
40574 property_kind = CTX_PROPERTY_NAME_LIST;
40575 break;
40576 case 'i': /* implementation */
40577 selectors = omp_implementation_selectors;
40578 allow_user = true;
40579 property_limit = 3;
40580 property_kind = CTX_PROPERTY_NAME_LIST;
40581 break;
40582 case 'u': /* user */
40583 selectors = omp_user_selectors;
40584 property_limit = 1;
40585 property_kind = CTX_PROPERTY_EXPR;
40586 break;
40587 default:
40588 gcc_unreachable ();
40589 }
40590 for (int i = 0; ; i++)
40591 {
40592 if (selectors[i] == NULL)
40593 {
40594 if (allow_user)
40595 {
40596 property_kind = CTX_PROPERTY_USER;
40597 break;
40598 }
40599 else
40600 {
40601 error ("selector %qs not allowed for context selector "
40602 "set %qs", IDENTIFIER_POINTER (selector),
40603 IDENTIFIER_POINTER (set));
40604 cp_lexer_consume_token (parser->lexer);
40605 return error_mark_node;
40606 }
40607 }
40608 if (i == property_limit)
40609 property_kind = CTX_PROPERTY_NONE;
40610 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
40611 break;
40612 }
40613 if (property_kind == CTX_PROPERTY_NAME_LIST
40614 && IDENTIFIER_POINTER (set)[0] == 'i'
40615 && strcmp (IDENTIFIER_POINTER (selector),
40616 "atomic_default_mem_order") == 0)
40617 property_kind = CTX_PROPERTY_ID;
40618
40619 cp_lexer_consume_token (parser->lexer);
40620
40621 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
40622 {
40623 if (property_kind == CTX_PROPERTY_NONE)
40624 {
40625 error ("selector %qs does not accept any properties",
40626 IDENTIFIER_POINTER (selector));
40627 return error_mark_node;
40628 }
40629
40630 matching_parens parens;
40631 parens.consume_open (parser);
40632
40633 cp_token *token = cp_lexer_peek_token (parser->lexer);
40634 if (allow_score
40635 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40636 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
40637 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40638 {
40639 cp_lexer_save_tokens (parser->lexer);
40640 cp_lexer_consume_token (parser->lexer);
40641 cp_lexer_consume_token (parser->lexer);
40642 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
40643 true)
40644 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40645 {
40646 cp_lexer_rollback_tokens (parser->lexer);
40647 cp_lexer_consume_token (parser->lexer);
40648
40649 matching_parens parens2;
40650 parens2.require_open (parser);
40651 tree score = cp_parser_constant_expression (parser);
40652 if (!parens2.require_close (parser))
40653 cp_parser_skip_to_closing_parenthesis (parser, true,
40654 false, true);
40655 cp_parser_require (parser, CPP_COLON, RT_COLON);
40656 if (score != error_mark_node)
40657 {
40658 score = fold_non_dependent_expr (score);
40659 if (value_dependent_expression_p (score))
40660 properties = tree_cons (get_identifier (" score"),
40661 score, properties);
40662 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
40663 || TREE_CODE (score) != INTEGER_CST)
40664 error_at (token->location, "score argument must be "
40665 "constant integer expression");
40666 else if (tree_int_cst_sgn (score) < 0)
40667 error_at (token->location, "score argument must be "
40668 "non-negative");
40669 else
40670 properties = tree_cons (get_identifier (" score"),
40671 score, properties);
40672 }
40673 }
40674 else
40675 cp_lexer_rollback_tokens (parser->lexer);
40676
40677 token = cp_lexer_peek_token (parser->lexer);
40678 }
40679
40680 switch (property_kind)
40681 {
40682 tree t;
40683 case CTX_PROPERTY_USER:
40684 do
40685 {
40686 t = cp_parser_constant_expression (parser);
40687 if (t != error_mark_node)
40688 {
40689 t = fold_non_dependent_expr (t);
40690 if (TREE_CODE (t) == STRING_CST)
40691 properties = tree_cons (NULL_TREE, t, properties);
40692 else if (!value_dependent_expression_p (t)
40693 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
40694 || !tree_fits_shwi_p (t)))
40695 error_at (token->location, "property must be "
40696 "constant integer expression or string "
40697 "literal");
40698 else
40699 properties = tree_cons (NULL_TREE, t, properties);
40700 }
40701 else
40702 return error_mark_node;
40703
40704 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40705 cp_lexer_consume_token (parser->lexer);
40706 else
40707 break;
40708 }
40709 while (1);
40710 break;
40711 case CTX_PROPERTY_ID:
40712 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
40713 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40714 {
40715 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
40716 cp_lexer_consume_token (parser->lexer);
40717 properties = tree_cons (prop, NULL_TREE, properties);
40718 }
40719 else
40720 {
40721 cp_parser_error (parser, "expected identifier");
40722 return error_mark_node;
40723 }
40724 break;
40725 case CTX_PROPERTY_NAME_LIST:
40726 do
40727 {
40728 tree prop = NULL_TREE, value = NULL_TREE;
40729 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
40730 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40731 {
40732 prop = cp_lexer_peek_token (parser->lexer)->u.value;
40733 cp_lexer_consume_token (parser->lexer);
40734 }
40735 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
40736 value = cp_parser_string_literal (parser, false, false);
40737 else
40738 {
40739 cp_parser_error (parser, "expected identifier or "
40740 "string literal");
40741 return error_mark_node;
40742 }
40743
40744 properties = tree_cons (prop, value, properties);
40745
40746 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40747 cp_lexer_consume_token (parser->lexer);
40748 else
40749 break;
40750 }
40751 while (1);
40752 break;
40753 case CTX_PROPERTY_EXPR:
40754 t = cp_parser_constant_expression (parser);
40755 if (t != error_mark_node)
40756 {
40757 t = fold_non_dependent_expr (t);
40758 if (!value_dependent_expression_p (t)
40759 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
40760 || !tree_fits_shwi_p (t)))
40761 error_at (token->location, "property must be "
40762 "constant integer expression");
40763 else
40764 properties = tree_cons (NULL_TREE, t, properties);
40765 }
40766 else
40767 return error_mark_node;
40768 break;
40769 case CTX_PROPERTY_SIMD:
40770 if (!has_parms_p)
40771 {
40772 error_at (token->location, "properties for %<simd%> "
40773 "selector may not be specified in "
40774 "%<metadirective%>");
40775 return error_mark_node;
40776 }
40777 properties
40778 = cp_parser_omp_all_clauses (parser,
40779 OMP_DECLARE_SIMD_CLAUSE_MASK,
40780 "simd", NULL, true, 2);
40781 break;
40782 default:
40783 gcc_unreachable ();
40784 }
40785
40786 if (!parens.require_close (parser))
40787 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
40788
40789 properties = nreverse (properties);
40790 }
40791 else if (property_kind == CTX_PROPERTY_NAME_LIST
40792 || property_kind == CTX_PROPERTY_ID
40793 || property_kind == CTX_PROPERTY_EXPR)
40794 {
40795 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
40796 return error_mark_node;
40797 }
40798
40799 ret = tree_cons (selector, properties, ret);
40800
40801 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40802 cp_lexer_consume_token (parser->lexer);
40803 else
40804 break;
40805 }
40806 while (1);
40807
40808 return nreverse (ret);
40809 }
40810
40811 /* OpenMP 5.0:
40812
40813 trait-set-selector[,trait-set-selector[,...]]
40814
40815 trait-set-selector:
40816 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
40817
40818 trait-set-selector-name:
40819 constructor
40820 device
40821 implementation
40822 user */
40823
40824 static tree
40825 cp_parser_omp_context_selector_specification (cp_parser *parser,
40826 bool has_parms_p)
40827 {
40828 tree ret = NULL_TREE;
40829 do
40830 {
40831 const char *setp = "";
40832 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40833 setp
40834 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
40835 switch (setp[0])
40836 {
40837 case 'c':
40838 if (strcmp (setp, "construct") == 0)
40839 setp = NULL;
40840 break;
40841 case 'd':
40842 if (strcmp (setp, "device") == 0)
40843 setp = NULL;
40844 break;
40845 case 'i':
40846 if (strcmp (setp, "implementation") == 0)
40847 setp = NULL;
40848 break;
40849 case 'u':
40850 if (strcmp (setp, "user") == 0)
40851 setp = NULL;
40852 break;
40853 default:
40854 break;
40855 }
40856 if (setp)
40857 {
40858 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
40859 "%<implementation%> or %<user%>");
40860 return error_mark_node;
40861 }
40862
40863 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
40864 cp_lexer_consume_token (parser->lexer);
40865
40866 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40867 return error_mark_node;
40868
40869 matching_braces braces;
40870 if (!braces.require_open (parser))
40871 return error_mark_node;
40872
40873 tree selectors
40874 = cp_parser_omp_context_selector (parser, set, has_parms_p);
40875 if (selectors == error_mark_node)
40876 {
40877 cp_parser_skip_to_closing_brace (parser);
40878 ret = error_mark_node;
40879 }
40880 else if (ret != error_mark_node)
40881 ret = tree_cons (set, selectors, ret);
40882
40883 braces.require_close (parser);
40884
40885 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40886 cp_lexer_consume_token (parser->lexer);
40887 else
40888 break;
40889 }
40890 while (1);
40891
40892 if (ret == error_mark_node)
40893 return ret;
40894 return nreverse (ret);
40895 }
40896
40897 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
40898 that into "omp declare variant base" attribute. */
40899
40900 static tree
40901 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
40902 tree attrs)
40903 {
40904 matching_parens parens;
40905 if (!parens.require_open (parser))
40906 {
40907 fail:
40908 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40909 return attrs;
40910 }
40911
40912 bool template_p;
40913 cp_id_kind idk = CP_ID_KIND_NONE;
40914 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
40915 cp_expr varid
40916 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
40917 /*check_dependency_p=*/true,
40918 /*template_p=*/&template_p,
40919 /*declarator_p=*/false,
40920 /*optional_p=*/false);
40921 parens.require_close (parser);
40922
40923 tree variant;
40924 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
40925 || TREE_CODE (varid) == TYPE_DECL
40926 || varid == error_mark_node)
40927 variant = varid;
40928 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
40929 variant = NULL_TREE;
40930 else
40931 {
40932 tree ambiguous_decls;
40933 variant = cp_parser_lookup_name (parser, varid, none_type,
40934 template_p, /*is_namespace=*/false,
40935 /*check_dependency=*/true,
40936 &ambiguous_decls,
40937 varid.get_location ());
40938 if (ambiguous_decls)
40939 variant = NULL_TREE;
40940 }
40941 if (variant == NULL_TREE)
40942 variant = error_mark_node;
40943 else if (TREE_CODE (variant) != SCOPE_REF)
40944 {
40945 const char *error_msg;
40946 variant
40947 = finish_id_expression (varid, variant, parser->scope,
40948 &idk, false, true,
40949 &parser->non_integral_constant_expression_p,
40950 template_p, true, false, false, &error_msg,
40951 varid.get_location ());
40952 if (error_msg)
40953 cp_parser_error (parser, error_msg);
40954 }
40955 location_t caret_loc = get_pure_location (varid.get_location ());
40956 location_t start_loc = get_start (varid_token->location);
40957 location_t finish_loc = get_finish (varid.get_location ());
40958 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
40959
40960 const char *clause = "";
40961 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
40962 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40963 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
40964 if (strcmp (clause, "match"))
40965 {
40966 cp_parser_error (parser, "expected %<match%>");
40967 goto fail;
40968 }
40969
40970 cp_lexer_consume_token (parser->lexer);
40971
40972 if (!parens.require_open (parser))
40973 goto fail;
40974
40975 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
40976 if (ctx == error_mark_node)
40977 goto fail;
40978 ctx = c_omp_check_context_selector (match_loc, ctx);
40979 if (ctx != error_mark_node && variant != error_mark_node)
40980 {
40981 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
40982 match_loc);
40983 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
40984 loc_node = tree_cons (match_loc_node,
40985 build_int_cst (integer_type_node, idk),
40986 build_tree_list (loc_node, integer_zero_node));
40987 attrs = tree_cons (get_identifier ("omp declare variant base"),
40988 tree_cons (variant, ctx, loc_node), attrs);
40989 if (processing_template_decl)
40990 ATTR_IS_DEPENDENT (attrs) = 1;
40991 }
40992
40993 parens.require_close (parser);
40994 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40995 return attrs;
40996 }
40997
40998
40999 /* Finalize #pragma omp declare simd clauses after direct declarator has
41000 been parsed, and put that into "omp declare simd" attribute. */
41001
41002 static tree
41003 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
41004 {
41005 struct cp_token_cache *ce;
41006 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
41007 int i;
41008
41009 if (!data->error_seen && data->fndecl_seen)
41010 {
41011 error ("%<#pragma omp declare %s%> not immediately followed by "
41012 "a single function declaration or definition",
41013 data->variant_p ? "variant" : "simd");
41014 data->error_seen = true;
41015 }
41016 if (data->error_seen)
41017 return attrs;
41018
41019 FOR_EACH_VEC_ELT (data->tokens, i, ce)
41020 {
41021 tree c, cl;
41022
41023 cp_parser_push_lexer_for_tokens (parser, ce);
41024 parser->lexer->in_pragma = true;
41025 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
41026 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
41027 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41028 const char *kind = IDENTIFIER_POINTER (id);
41029 cp_lexer_consume_token (parser->lexer);
41030 if (strcmp (kind, "simd") == 0)
41031 {
41032 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
41033 "#pragma omp declare simd",
41034 pragma_tok);
41035 if (cl)
41036 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
41037 c = build_tree_list (get_identifier ("omp declare simd"), cl);
41038 TREE_CHAIN (c) = attrs;
41039 if (processing_template_decl)
41040 ATTR_IS_DEPENDENT (c) = 1;
41041 attrs = c;
41042 }
41043 else
41044 {
41045 gcc_assert (strcmp (kind, "variant") == 0);
41046 attrs = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
41047 }
41048 cp_parser_pop_lexer (parser);
41049 }
41050
41051 data->fndecl_seen = true;
41052 return attrs;
41053 }
41054
41055
41056 /* OpenMP 4.0:
41057 # pragma omp declare target new-line
41058 declarations and definitions
41059 # pragma omp end declare target new-line
41060
41061 OpenMP 4.5:
41062 # pragma omp declare target ( extended-list ) new-line
41063
41064 # pragma omp declare target declare-target-clauses[seq] new-line */
41065
41066 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
41067 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
41068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
41069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
41070
41071 static void
41072 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
41073 {
41074 tree clauses = NULL_TREE;
41075 int device_type = 0;
41076 bool only_device_type = true;
41077 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41078 clauses
41079 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
41080 "#pragma omp declare target", pragma_tok);
41081 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41082 {
41083 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
41084 clauses);
41085 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
41086 cp_parser_require_pragma_eol (parser, pragma_tok);
41087 }
41088 else
41089 {
41090 cp_parser_require_pragma_eol (parser, pragma_tok);
41091 scope_chain->omp_declare_target_attribute++;
41092 return;
41093 }
41094 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
41095 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
41096 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
41097 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
41098 {
41099 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
41100 continue;
41101 tree t = OMP_CLAUSE_DECL (c), id;
41102 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
41103 tree at2 = lookup_attribute ("omp declare target link",
41104 DECL_ATTRIBUTES (t));
41105 only_device_type = false;
41106 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
41107 {
41108 id = get_identifier ("omp declare target link");
41109 std::swap (at1, at2);
41110 }
41111 else
41112 id = get_identifier ("omp declare target");
41113 if (at2)
41114 {
41115 error_at (OMP_CLAUSE_LOCATION (c),
41116 "%qD specified both in declare target %<link%> and %<to%>"
41117 " clauses", t);
41118 continue;
41119 }
41120 if (!at1)
41121 {
41122 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
41123 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
41124 continue;
41125
41126 symtab_node *node = symtab_node::get (t);
41127 if (node != NULL)
41128 {
41129 node->offloadable = 1;
41130 if (ENABLE_OFFLOADING)
41131 {
41132 g->have_offload = true;
41133 if (is_a <varpool_node *> (node))
41134 vec_safe_push (offload_vars, t);
41135 }
41136 }
41137 }
41138 if (TREE_CODE (t) != FUNCTION_DECL)
41139 continue;
41140 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
41141 {
41142 tree at3 = lookup_attribute ("omp declare target host",
41143 DECL_ATTRIBUTES (t));
41144 if (at3 == NULL_TREE)
41145 {
41146 id = get_identifier ("omp declare target host");
41147 DECL_ATTRIBUTES (t)
41148 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
41149 }
41150 }
41151 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
41152 {
41153 tree at3 = lookup_attribute ("omp declare target nohost",
41154 DECL_ATTRIBUTES (t));
41155 if (at3 == NULL_TREE)
41156 {
41157 id = get_identifier ("omp declare target nohost");
41158 DECL_ATTRIBUTES (t)
41159 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
41160 }
41161 }
41162 }
41163 if (device_type && only_device_type)
41164 warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
41165 "directive with only %<device_type%> clauses ignored");
41166 }
41167
41168 static void
41169 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
41170 {
41171 const char *p = "";
41172 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41173 {
41174 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41175 p = IDENTIFIER_POINTER (id);
41176 }
41177 if (strcmp (p, "declare") == 0)
41178 {
41179 cp_lexer_consume_token (parser->lexer);
41180 p = "";
41181 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41182 {
41183 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41184 p = IDENTIFIER_POINTER (id);
41185 }
41186 if (strcmp (p, "target") == 0)
41187 cp_lexer_consume_token (parser->lexer);
41188 else
41189 {
41190 cp_parser_error (parser, "expected %<target%>");
41191 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41192 return;
41193 }
41194 }
41195 else
41196 {
41197 cp_parser_error (parser, "expected %<declare%>");
41198 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41199 return;
41200 }
41201 cp_parser_require_pragma_eol (parser, pragma_tok);
41202 if (!scope_chain->omp_declare_target_attribute)
41203 error_at (pragma_tok->location,
41204 "%<#pragma omp end declare target%> without corresponding "
41205 "%<#pragma omp declare target%>");
41206 else
41207 scope_chain->omp_declare_target_attribute--;
41208 }
41209
41210 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
41211 expression and optional initializer clause of
41212 #pragma omp declare reduction. We store the expression(s) as
41213 either 3, 6 or 7 special statements inside of the artificial function's
41214 body. The first two statements are DECL_EXPRs for the artificial
41215 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
41216 expression that uses those variables.
41217 If there was any INITIALIZER clause, this is followed by further statements,
41218 the fourth and fifth statements are DECL_EXPRs for the artificial
41219 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
41220 constructor variant (first token after open paren is not omp_priv),
41221 then the sixth statement is a statement with the function call expression
41222 that uses the OMP_PRIV and optionally OMP_ORIG variable.
41223 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
41224 to initialize the OMP_PRIV artificial variable and there is seventh
41225 statement, a DECL_EXPR of the OMP_PRIV statement again. */
41226
41227 static bool
41228 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
41229 {
41230 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
41231 gcc_assert (TYPE_REF_P (type));
41232 type = TREE_TYPE (type);
41233 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
41234 DECL_ARTIFICIAL (omp_out) = 1;
41235 pushdecl (omp_out);
41236 add_decl_expr (omp_out);
41237 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
41238 DECL_ARTIFICIAL (omp_in) = 1;
41239 pushdecl (omp_in);
41240 add_decl_expr (omp_in);
41241 tree combiner;
41242 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
41243
41244 keep_next_level (true);
41245 tree block = begin_omp_structured_block ();
41246 combiner = cp_parser_expression (parser);
41247 finish_expr_stmt (combiner);
41248 block = finish_omp_structured_block (block);
41249 if (processing_template_decl)
41250 block = build_stmt (input_location, EXPR_STMT, block);
41251 add_stmt (block);
41252
41253 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
41254 return false;
41255
41256 const char *p = "";
41257 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41258 {
41259 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41260 p = IDENTIFIER_POINTER (id);
41261 }
41262
41263 if (strcmp (p, "initializer") == 0)
41264 {
41265 cp_lexer_consume_token (parser->lexer);
41266 matching_parens parens;
41267 if (!parens.require_open (parser))
41268 return false;
41269
41270 p = "";
41271 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41272 {
41273 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41274 p = IDENTIFIER_POINTER (id);
41275 }
41276
41277 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
41278 DECL_ARTIFICIAL (omp_priv) = 1;
41279 pushdecl (omp_priv);
41280 add_decl_expr (omp_priv);
41281 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
41282 DECL_ARTIFICIAL (omp_orig) = 1;
41283 pushdecl (omp_orig);
41284 add_decl_expr (omp_orig);
41285
41286 keep_next_level (true);
41287 block = begin_omp_structured_block ();
41288
41289 bool ctor = false;
41290 if (strcmp (p, "omp_priv") == 0)
41291 {
41292 bool is_direct_init, is_non_constant_init;
41293 ctor = true;
41294 cp_lexer_consume_token (parser->lexer);
41295 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
41296 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
41297 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
41298 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
41299 == CPP_CLOSE_PAREN
41300 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
41301 == CPP_CLOSE_PAREN))
41302 {
41303 finish_omp_structured_block (block);
41304 error ("invalid initializer clause");
41305 return false;
41306 }
41307 initializer = cp_parser_initializer (parser, &is_direct_init,
41308 &is_non_constant_init);
41309 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
41310 NULL_TREE, LOOKUP_ONLYCONVERTING);
41311 }
41312 else
41313 {
41314 cp_parser_parse_tentatively (parser);
41315 /* Don't create location wrapper nodes here. */
41316 auto_suppress_location_wrappers sentinel;
41317 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
41318 /*check_dependency_p=*/true,
41319 /*template_p=*/NULL,
41320 /*declarator_p=*/false,
41321 /*optional_p=*/false);
41322 vec<tree, va_gc> *args;
41323 if (fn_name == error_mark_node
41324 || cp_parser_error_occurred (parser)
41325 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
41326 || ((args = cp_parser_parenthesized_expression_list
41327 (parser, non_attr, /*cast_p=*/false,
41328 /*allow_expansion_p=*/true,
41329 /*non_constant_p=*/NULL)),
41330 cp_parser_error_occurred (parser)))
41331 {
41332 finish_omp_structured_block (block);
41333 cp_parser_abort_tentative_parse (parser);
41334 cp_parser_error (parser, "expected id-expression (arguments)");
41335 return false;
41336 }
41337 unsigned int i;
41338 tree arg;
41339 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
41340 if (arg == omp_priv
41341 || (TREE_CODE (arg) == ADDR_EXPR
41342 && TREE_OPERAND (arg, 0) == omp_priv))
41343 break;
41344 cp_parser_abort_tentative_parse (parser);
41345 if (arg == NULL_TREE)
41346 error ("one of the initializer call arguments should be %<omp_priv%>"
41347 " or %<&omp_priv%>");
41348 initializer = cp_parser_postfix_expression (parser, false, false, false,
41349 false, NULL);
41350 finish_expr_stmt (initializer);
41351 }
41352
41353 block = finish_omp_structured_block (block);
41354 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
41355 if (processing_template_decl)
41356 block = build_stmt (input_location, EXPR_STMT, block);
41357 add_stmt (block);
41358
41359 if (ctor)
41360 add_decl_expr (omp_orig);
41361
41362 if (!parens.require_close (parser))
41363 return false;
41364 }
41365
41366 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
41367 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
41368 UNKNOWN_LOCATION);
41369
41370 return true;
41371 }
41372
41373 /* OpenMP 4.0
41374 #pragma omp declare reduction (reduction-id : typename-list : expression) \
41375 initializer-clause[opt] new-line
41376
41377 initializer-clause:
41378 initializer (omp_priv initializer)
41379 initializer (function-name (argument-list)) */
41380
41381 static void
41382 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
41383 enum pragma_context)
41384 {
41385 auto_vec<tree> types;
41386 enum tree_code reduc_code = ERROR_MARK;
41387 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
41388 unsigned int i;
41389 cp_token *first_token;
41390 cp_token_cache *cp;
41391 int errs;
41392 void *p;
41393
41394 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
41395 p = obstack_alloc (&declarator_obstack, 0);
41396
41397 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41398 goto fail;
41399
41400 switch (cp_lexer_peek_token (parser->lexer)->type)
41401 {
41402 case CPP_PLUS:
41403 reduc_code = PLUS_EXPR;
41404 break;
41405 case CPP_MULT:
41406 reduc_code = MULT_EXPR;
41407 break;
41408 case CPP_MINUS:
41409 reduc_code = MINUS_EXPR;
41410 break;
41411 case CPP_AND:
41412 reduc_code = BIT_AND_EXPR;
41413 break;
41414 case CPP_XOR:
41415 reduc_code = BIT_XOR_EXPR;
41416 break;
41417 case CPP_OR:
41418 reduc_code = BIT_IOR_EXPR;
41419 break;
41420 case CPP_AND_AND:
41421 reduc_code = TRUTH_ANDIF_EXPR;
41422 break;
41423 case CPP_OR_OR:
41424 reduc_code = TRUTH_ORIF_EXPR;
41425 break;
41426 case CPP_NAME:
41427 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
41428 break;
41429 default:
41430 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
41431 "%<|%>, %<&&%>, %<||%> or identifier");
41432 goto fail;
41433 }
41434
41435 if (reduc_code != ERROR_MARK)
41436 cp_lexer_consume_token (parser->lexer);
41437
41438 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
41439 if (reduc_id == error_mark_node)
41440 goto fail;
41441
41442 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41443 goto fail;
41444
41445 /* Types may not be defined in declare reduction type list. */
41446 const char *saved_message;
41447 saved_message = parser->type_definition_forbidden_message;
41448 parser->type_definition_forbidden_message
41449 = G_("types may not be defined in declare reduction type list");
41450 bool saved_colon_corrects_to_scope_p;
41451 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
41452 parser->colon_corrects_to_scope_p = false;
41453 bool saved_colon_doesnt_start_class_def_p;
41454 saved_colon_doesnt_start_class_def_p
41455 = parser->colon_doesnt_start_class_def_p;
41456 parser->colon_doesnt_start_class_def_p = true;
41457
41458 while (true)
41459 {
41460 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41461 type = cp_parser_type_id (parser);
41462 if (type == error_mark_node)
41463 ;
41464 else if (ARITHMETIC_TYPE_P (type)
41465 && (orig_reduc_id == NULL_TREE
41466 || (TREE_CODE (type) != COMPLEX_TYPE
41467 && (id_equal (orig_reduc_id, "min")
41468 || id_equal (orig_reduc_id, "max")))))
41469 error_at (loc, "predeclared arithmetic type %qT in "
41470 "%<#pragma omp declare reduction%>", type);
41471 else if (FUNC_OR_METHOD_TYPE_P (type)
41472 || TREE_CODE (type) == ARRAY_TYPE)
41473 error_at (loc, "function or array type %qT in "
41474 "%<#pragma omp declare reduction%>", type);
41475 else if (TYPE_REF_P (type))
41476 error_at (loc, "reference type %qT in "
41477 "%<#pragma omp declare reduction%>", type);
41478 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
41479 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
41480 "type %qT in %<#pragma omp declare reduction%>", type);
41481 else
41482 types.safe_push (type);
41483
41484 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41485 cp_lexer_consume_token (parser->lexer);
41486 else
41487 break;
41488 }
41489
41490 /* Restore the saved message. */
41491 parser->type_definition_forbidden_message = saved_message;
41492 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
41493 parser->colon_doesnt_start_class_def_p
41494 = saved_colon_doesnt_start_class_def_p;
41495
41496 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
41497 || types.is_empty ())
41498 {
41499 fail:
41500 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41501 goto done;
41502 }
41503
41504 first_token = cp_lexer_peek_token (parser->lexer);
41505 cp = NULL;
41506 errs = errorcount;
41507 FOR_EACH_VEC_ELT (types, i, type)
41508 {
41509 tree fntype
41510 = build_function_type_list (void_type_node,
41511 cp_build_reference_type (type, false),
41512 NULL_TREE);
41513 tree this_reduc_id = reduc_id;
41514 if (!dependent_type_p (type))
41515 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
41516 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
41517 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
41518 DECL_ARTIFICIAL (fndecl) = 1;
41519 DECL_EXTERNAL (fndecl) = 1;
41520 DECL_DECLARED_INLINE_P (fndecl) = 1;
41521 DECL_IGNORED_P (fndecl) = 1;
41522 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
41523 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
41524 DECL_ATTRIBUTES (fndecl)
41525 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
41526 DECL_ATTRIBUTES (fndecl));
41527 if (processing_template_decl)
41528 fndecl = push_template_decl (fndecl);
41529 bool block_scope = false;
41530 tree block = NULL_TREE;
41531 if (current_function_decl)
41532 {
41533 block_scope = true;
41534 DECL_CONTEXT (fndecl) = global_namespace;
41535 if (!processing_template_decl)
41536 pushdecl (fndecl);
41537 }
41538 else if (current_class_type)
41539 {
41540 if (cp == NULL)
41541 {
41542 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
41543 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
41544 cp_lexer_consume_token (parser->lexer);
41545 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41546 goto fail;
41547 cp = cp_token_cache_new (first_token,
41548 cp_lexer_peek_nth_token (parser->lexer,
41549 2));
41550 }
41551 DECL_STATIC_FUNCTION_P (fndecl) = 1;
41552 finish_member_declaration (fndecl);
41553 DECL_PENDING_INLINE_INFO (fndecl) = cp;
41554 DECL_PENDING_INLINE_P (fndecl) = 1;
41555 vec_safe_push (unparsed_funs_with_definitions, fndecl);
41556 continue;
41557 }
41558 else
41559 {
41560 DECL_CONTEXT (fndecl) = current_namespace;
41561 pushdecl (fndecl);
41562 }
41563 if (!block_scope)
41564 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
41565 else
41566 block = begin_omp_structured_block ();
41567 if (cp)
41568 {
41569 cp_parser_push_lexer_for_tokens (parser, cp);
41570 parser->lexer->in_pragma = true;
41571 }
41572 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
41573 {
41574 if (!block_scope)
41575 finish_function (/*inline_p=*/false);
41576 else
41577 DECL_CONTEXT (fndecl) = current_function_decl;
41578 if (cp)
41579 cp_parser_pop_lexer (parser);
41580 goto fail;
41581 }
41582 if (cp)
41583 cp_parser_pop_lexer (parser);
41584 if (!block_scope)
41585 finish_function (/*inline_p=*/false);
41586 else
41587 {
41588 DECL_CONTEXT (fndecl) = current_function_decl;
41589 block = finish_omp_structured_block (block);
41590 if (TREE_CODE (block) == BIND_EXPR)
41591 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
41592 else if (TREE_CODE (block) == STATEMENT_LIST)
41593 DECL_SAVED_TREE (fndecl) = block;
41594 if (processing_template_decl)
41595 add_decl_expr (fndecl);
41596 }
41597 cp_check_omp_declare_reduction (fndecl);
41598 if (cp == NULL && types.length () > 1)
41599 cp = cp_token_cache_new (first_token,
41600 cp_lexer_peek_nth_token (parser->lexer, 2));
41601 if (errs != errorcount)
41602 break;
41603 }
41604
41605 cp_parser_require_pragma_eol (parser, pragma_tok);
41606
41607 done:
41608 /* Free any declarators allocated. */
41609 obstack_free (&declarator_obstack, p);
41610 }
41611
41612 /* OpenMP 4.0
41613 #pragma omp declare simd declare-simd-clauses[optseq] new-line
41614 #pragma omp declare reduction (reduction-id : typename-list : expression) \
41615 initializer-clause[opt] new-line
41616 #pragma omp declare target new-line
41617
41618 OpenMP 5.0
41619 #pragma omp declare variant (identifier) match (context-selector) */
41620
41621 static bool
41622 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
41623 enum pragma_context context)
41624 {
41625 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41626 {
41627 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41628 const char *p = IDENTIFIER_POINTER (id);
41629
41630 if (strcmp (p, "simd") == 0)
41631 {
41632 cp_lexer_consume_token (parser->lexer);
41633 cp_parser_omp_declare_simd (parser, pragma_tok,
41634 context, false);
41635 return true;
41636 }
41637 if (flag_openmp && strcmp (p, "variant") == 0)
41638 {
41639 cp_lexer_consume_token (parser->lexer);
41640 cp_parser_omp_declare_simd (parser, pragma_tok,
41641 context, true);
41642 return true;
41643 }
41644 cp_ensure_no_omp_declare_simd (parser);
41645 if (strcmp (p, "reduction") == 0)
41646 {
41647 cp_lexer_consume_token (parser->lexer);
41648 cp_parser_omp_declare_reduction (parser, pragma_tok,
41649 context);
41650 return false;
41651 }
41652 if (!flag_openmp) /* flag_openmp_simd */
41653 {
41654 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41655 return false;
41656 }
41657 if (strcmp (p, "target") == 0)
41658 {
41659 cp_lexer_consume_token (parser->lexer);
41660 cp_parser_omp_declare_target (parser, pragma_tok);
41661 return false;
41662 }
41663 }
41664 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
41665 "%<target%> or %<variant%>");
41666 cp_parser_require_pragma_eol (parser, pragma_tok);
41667 return false;
41668 }
41669
41670 /* OpenMP 5.0
41671 #pragma omp requires clauses[optseq] new-line */
41672
41673 static bool
41674 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
41675 {
41676 bool first = true;
41677 enum omp_requires new_req = (enum omp_requires) 0;
41678
41679 location_t loc = pragma_tok->location;
41680 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41681 {
41682 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41683 cp_lexer_consume_token (parser->lexer);
41684
41685 first = false;
41686
41687 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41688 {
41689 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41690 const char *p = IDENTIFIER_POINTER (id);
41691 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
41692 enum omp_requires this_req = (enum omp_requires) 0;
41693
41694 if (!strcmp (p, "unified_address"))
41695 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
41696 else if (!strcmp (p, "unified_shared_memory"))
41697 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
41698 else if (!strcmp (p, "dynamic_allocators"))
41699 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
41700 else if (!strcmp (p, "reverse_offload"))
41701 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
41702 else if (!strcmp (p, "atomic_default_mem_order"))
41703 {
41704 cp_lexer_consume_token (parser->lexer);
41705
41706 matching_parens parens;
41707 if (parens.require_open (parser))
41708 {
41709 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41710 {
41711 id = cp_lexer_peek_token (parser->lexer)->u.value;
41712 p = IDENTIFIER_POINTER (id);
41713
41714 if (!strcmp (p, "seq_cst"))
41715 this_req
41716 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
41717 else if (!strcmp (p, "relaxed"))
41718 this_req
41719 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
41720 else if (!strcmp (p, "acq_rel"))
41721 this_req
41722 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
41723 }
41724 if (this_req == 0)
41725 {
41726 error_at (cp_lexer_peek_token (parser->lexer)->location,
41727 "expected %<seq_cst%>, %<relaxed%> or "
41728 "%<acq_rel%>");
41729 if (cp_lexer_nth_token_is (parser->lexer, 2,
41730 CPP_CLOSE_PAREN))
41731 cp_lexer_consume_token (parser->lexer);
41732 }
41733 else
41734 cp_lexer_consume_token (parser->lexer);
41735
41736 if (!parens.require_close (parser))
41737 cp_parser_skip_to_closing_parenthesis (parser,
41738 /*recovering=*/true,
41739 /*or_comma=*/false,
41740 /*consume_paren=*/
41741 true);
41742
41743 if (this_req == 0)
41744 {
41745 cp_parser_require_pragma_eol (parser, pragma_tok);
41746 return false;
41747 }
41748 }
41749 p = NULL;
41750 }
41751 else
41752 {
41753 error_at (cloc, "expected %<unified_address%>, "
41754 "%<unified_shared_memory%>, "
41755 "%<dynamic_allocators%>, "
41756 "%<reverse_offload%> "
41757 "or %<atomic_default_mem_order%> clause");
41758 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41759 return false;
41760 }
41761 if (p)
41762 sorry_at (cloc, "%qs clause on %<requires%> directive not "
41763 "supported yet", p);
41764 if (p)
41765 cp_lexer_consume_token (parser->lexer);
41766 if (this_req)
41767 {
41768 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
41769 {
41770 if ((this_req & new_req) != 0)
41771 error_at (cloc, "too many %qs clauses", p);
41772 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
41773 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
41774 error_at (cloc, "%qs clause used lexically after first "
41775 "target construct or offloading API", p);
41776 }
41777 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
41778 {
41779 error_at (cloc, "too many %qs clauses",
41780 "atomic_default_mem_order");
41781 this_req = (enum omp_requires) 0;
41782 }
41783 else if ((omp_requires_mask
41784 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
41785 {
41786 error_at (cloc, "more than one %<atomic_default_mem_order%>"
41787 " clause in a single compilation unit");
41788 this_req
41789 = (enum omp_requires)
41790 (omp_requires_mask
41791 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
41792 }
41793 else if ((omp_requires_mask
41794 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
41795 error_at (cloc, "%<atomic_default_mem_order%> clause used "
41796 "lexically after first %<atomic%> construct "
41797 "without memory order clause");
41798 new_req = (enum omp_requires) (new_req | this_req);
41799 omp_requires_mask
41800 = (enum omp_requires) (omp_requires_mask | this_req);
41801 continue;
41802 }
41803 }
41804 break;
41805 }
41806 cp_parser_require_pragma_eol (parser, pragma_tok);
41807
41808 if (new_req == 0)
41809 error_at (loc, "%<pragma omp requires%> requires at least one clause");
41810 return false;
41811 }
41812
41813
41814 /* OpenMP 4.5:
41815 #pragma omp taskloop taskloop-clause[optseq] new-line
41816 for-loop
41817
41818 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
41819 for-loop */
41820
41821 #define OMP_TASKLOOP_CLAUSE_MASK \
41822 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
41823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
41826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
41827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
41828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
41829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
41830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
41831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
41833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
41834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
41835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
41836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
41837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
41838
41839 static tree
41840 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
41841 char *p_name, omp_clause_mask mask, tree *cclauses,
41842 bool *if_p)
41843 {
41844 tree clauses, sb, ret;
41845 unsigned int save;
41846 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41847
41848 strcat (p_name, " taskloop");
41849 mask |= OMP_TASKLOOP_CLAUSE_MASK;
41850 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
41851 clause. */
41852 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
41853 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
41854
41855 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41856 {
41857 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41858 const char *p = IDENTIFIER_POINTER (id);
41859
41860 if (strcmp (p, "simd") == 0)
41861 {
41862 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
41863 if (cclauses == NULL)
41864 cclauses = cclauses_buf;
41865
41866 cp_lexer_consume_token (parser->lexer);
41867 if (!flag_openmp) /* flag_openmp_simd */
41868 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
41869 cclauses, if_p);
41870 sb = begin_omp_structured_block ();
41871 save = cp_parser_begin_omp_structured_block (parser);
41872 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
41873 cclauses, if_p);
41874 cp_parser_end_omp_structured_block (parser, save);
41875 tree body = finish_omp_structured_block (sb);
41876 if (ret == NULL)
41877 return ret;
41878 ret = make_node (OMP_TASKLOOP);
41879 TREE_TYPE (ret) = void_type_node;
41880 OMP_FOR_BODY (ret) = body;
41881 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
41882 SET_EXPR_LOCATION (ret, loc);
41883 add_stmt (ret);
41884 return ret;
41885 }
41886 }
41887 if (!flag_openmp) /* flag_openmp_simd */
41888 {
41889 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41890 return NULL_TREE;
41891 }
41892
41893 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
41894 cclauses == NULL);
41895 if (cclauses)
41896 {
41897 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
41898 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
41899 }
41900
41901 keep_next_level (true);
41902 sb = begin_omp_structured_block ();
41903 save = cp_parser_begin_omp_structured_block (parser);
41904
41905 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
41906 if_p);
41907
41908 cp_parser_end_omp_structured_block (parser, save);
41909 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
41910
41911 return ret;
41912 }
41913
41914
41915 /* OpenACC 2.0:
41916 # pragma acc routine oacc-routine-clause[optseq] new-line
41917 function-definition
41918
41919 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
41920 */
41921
41922 #define OACC_ROUTINE_CLAUSE_MASK \
41923 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
41924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
41925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
41926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
41927
41928
41929 /* Parse the OpenACC routine pragma. This has an optional '( name )'
41930 component, which must resolve to a declared namespace-scope
41931 function. The clauses are either processed directly (for a named
41932 function), or defered until the immediatley following declaration
41933 is parsed. */
41934
41935 static void
41936 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
41937 enum pragma_context context)
41938 {
41939 gcc_checking_assert (context == pragma_external);
41940 /* The checking for "another pragma following this one" in the "no optional
41941 '( name )'" case makes sure that we dont re-enter. */
41942 gcc_checking_assert (parser->oacc_routine == NULL);
41943
41944 cp_oacc_routine_data data;
41945 data.error_seen = false;
41946 data.fndecl_seen = false;
41947 data.tokens = vNULL;
41948 data.clauses = NULL_TREE;
41949 data.loc = pragma_tok->location;
41950 /* It is safe to take the address of a local variable; it will only be
41951 used while this scope is live. */
41952 parser->oacc_routine = &data;
41953
41954 /* Look for optional '( name )'. */
41955 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41956 {
41957 matching_parens parens;
41958 parens.consume_open (parser); /* '(' */
41959
41960 /* We parse the name as an id-expression. If it resolves to
41961 anything other than a non-overloaded function at namespace
41962 scope, it's an error. */
41963 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
41964 tree name = cp_parser_id_expression (parser,
41965 /*template_keyword_p=*/false,
41966 /*check_dependency_p=*/false,
41967 /*template_p=*/NULL,
41968 /*declarator_p=*/false,
41969 /*optional_p=*/false);
41970 tree decl = (identifier_p (name)
41971 ? cp_parser_lookup_name_simple (parser, name, name_loc)
41972 : name);
41973 if (name != error_mark_node && decl == error_mark_node)
41974 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
41975
41976 if (decl == error_mark_node
41977 || !parens.require_close (parser))
41978 {
41979 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41980 parser->oacc_routine = NULL;
41981 return;
41982 }
41983
41984 data.clauses
41985 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
41986 "#pragma acc routine",
41987 cp_lexer_peek_token (parser->lexer));
41988 /* The clauses are in reverse order; fix that to make later diagnostic
41989 emission easier. */
41990 data.clauses = nreverse (data.clauses);
41991
41992 if (decl && is_overloaded_fn (decl)
41993 && (TREE_CODE (decl) != FUNCTION_DECL
41994 || DECL_FUNCTION_TEMPLATE_P (decl)))
41995 {
41996 error_at (name_loc,
41997 "%<#pragma acc routine%> names a set of overloads");
41998 parser->oacc_routine = NULL;
41999 return;
42000 }
42001
42002 /* Perhaps we should use the same rule as declarations in different
42003 namespaces? */
42004 if (!DECL_NAMESPACE_SCOPE_P (decl))
42005 {
42006 error_at (name_loc,
42007 "%qD does not refer to a namespace scope function", decl);
42008 parser->oacc_routine = NULL;
42009 return;
42010 }
42011
42012 if (TREE_CODE (decl) != FUNCTION_DECL)
42013 {
42014 error_at (name_loc, "%qD does not refer to a function", decl);
42015 parser->oacc_routine = NULL;
42016 return;
42017 }
42018
42019 cp_finalize_oacc_routine (parser, decl, false);
42020 parser->oacc_routine = NULL;
42021 }
42022 else /* No optional '( name )'. */
42023 {
42024 /* Store away all pragma tokens. */
42025 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
42026 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
42027 cp_lexer_consume_token (parser->lexer);
42028 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42029 parser->oacc_routine->error_seen = true;
42030 cp_parser_require_pragma_eol (parser, pragma_tok);
42031 struct cp_token_cache *cp
42032 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
42033 parser->oacc_routine->tokens.safe_push (cp);
42034
42035 /* Emit a helpful diagnostic if there's another pragma following this
42036 one. */
42037 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
42038 {
42039 cp_ensure_no_oacc_routine (parser);
42040 data.tokens.release ();
42041 /* ..., and then just keep going. */
42042 return;
42043 }
42044
42045 /* We only have to consider the pragma_external case here. */
42046 cp_parser_declaration (parser);
42047 if (parser->oacc_routine
42048 && !parser->oacc_routine->fndecl_seen)
42049 cp_ensure_no_oacc_routine (parser);
42050 else
42051 parser->oacc_routine = NULL;
42052 data.tokens.release ();
42053 }
42054 }
42055
42056 /* Finalize #pragma acc routine clauses after direct declarator has
42057 been parsed. */
42058
42059 static tree
42060 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
42061 {
42062 struct cp_token_cache *ce;
42063 cp_oacc_routine_data *data = parser->oacc_routine;
42064
42065 if (!data->error_seen && data->fndecl_seen)
42066 {
42067 error_at (data->loc,
42068 "%<#pragma acc routine%> not immediately followed by "
42069 "a single function declaration or definition");
42070 data->error_seen = true;
42071 }
42072 if (data->error_seen)
42073 return attrs;
42074
42075 gcc_checking_assert (data->tokens.length () == 1);
42076 ce = data->tokens[0];
42077
42078 cp_parser_push_lexer_for_tokens (parser, ce);
42079 parser->lexer->in_pragma = true;
42080 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
42081
42082 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
42083 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
42084 parser->oacc_routine->clauses
42085 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
42086 "#pragma acc routine", pragma_tok);
42087 /* The clauses are in reverse order; fix that to make later diagnostic
42088 emission easier. */
42089 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
42090 cp_parser_pop_lexer (parser);
42091 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
42092 fndecl_seen. */
42093
42094 return attrs;
42095 }
42096
42097 /* Apply any saved OpenACC routine clauses to a just-parsed
42098 declaration. */
42099
42100 static void
42101 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
42102 {
42103 if (__builtin_expect (parser->oacc_routine != NULL, 0))
42104 {
42105 /* Keep going if we're in error reporting mode. */
42106 if (parser->oacc_routine->error_seen
42107 || fndecl == error_mark_node)
42108 return;
42109
42110 if (parser->oacc_routine->fndecl_seen)
42111 {
42112 error_at (parser->oacc_routine->loc,
42113 "%<#pragma acc routine%> not immediately followed by"
42114 " a single function declaration or definition");
42115 parser->oacc_routine = NULL;
42116 return;
42117 }
42118 if (TREE_CODE (fndecl) != FUNCTION_DECL)
42119 {
42120 cp_ensure_no_oacc_routine (parser);
42121 return;
42122 }
42123
42124 int compatible
42125 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
42126 parser->oacc_routine->loc,
42127 "#pragma acc routine");
42128 if (compatible < 0)
42129 {
42130 parser->oacc_routine = NULL;
42131 return;
42132 }
42133 if (compatible > 0)
42134 {
42135 }
42136 else
42137 {
42138 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
42139 {
42140 error_at (parser->oacc_routine->loc,
42141 TREE_USED (fndecl)
42142 ? G_("%<#pragma acc routine%> must be applied before"
42143 " use")
42144 : G_("%<#pragma acc routine%> must be applied before"
42145 " definition"));
42146 parser->oacc_routine = NULL;
42147 return;
42148 }
42149
42150 /* Set the routine's level of parallelism. */
42151 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
42152 oacc_replace_fn_attrib (fndecl, dims);
42153
42154 /* Add an "omp declare target" attribute. */
42155 DECL_ATTRIBUTES (fndecl)
42156 = tree_cons (get_identifier ("omp declare target"),
42157 parser->oacc_routine->clauses,
42158 DECL_ATTRIBUTES (fndecl));
42159 }
42160
42161 /* Don't unset parser->oacc_routine here: we may still need it to
42162 diagnose wrong usage. But, remember that we've used this "#pragma acc
42163 routine". */
42164 parser->oacc_routine->fndecl_seen = true;
42165 }
42166 }
42167
42168 /* Main entry point to OpenMP statement pragmas. */
42169
42170 static void
42171 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
42172 {
42173 tree stmt;
42174 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
42175 omp_clause_mask mask (0);
42176
42177 switch (cp_parser_pragma_kind (pragma_tok))
42178 {
42179 case PRAGMA_OACC_ATOMIC:
42180 cp_parser_omp_atomic (parser, pragma_tok);
42181 return;
42182 case PRAGMA_OACC_CACHE:
42183 stmt = cp_parser_oacc_cache (parser, pragma_tok);
42184 break;
42185 case PRAGMA_OACC_DATA:
42186 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
42187 break;
42188 case PRAGMA_OACC_ENTER_DATA:
42189 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
42190 break;
42191 case PRAGMA_OACC_EXIT_DATA:
42192 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
42193 break;
42194 case PRAGMA_OACC_HOST_DATA:
42195 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
42196 break;
42197 case PRAGMA_OACC_KERNELS:
42198 case PRAGMA_OACC_PARALLEL:
42199 case PRAGMA_OACC_SERIAL:
42200 strcpy (p_name, "#pragma acc");
42201 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
42202 break;
42203 case PRAGMA_OACC_LOOP:
42204 strcpy (p_name, "#pragma acc");
42205 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
42206 if_p);
42207 break;
42208 case PRAGMA_OACC_UPDATE:
42209 stmt = cp_parser_oacc_update (parser, pragma_tok);
42210 break;
42211 case PRAGMA_OACC_WAIT:
42212 stmt = cp_parser_oacc_wait (parser, pragma_tok);
42213 break;
42214 case PRAGMA_OMP_ATOMIC:
42215 cp_parser_omp_atomic (parser, pragma_tok);
42216 return;
42217 case PRAGMA_OMP_CRITICAL:
42218 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
42219 break;
42220 case PRAGMA_OMP_DISTRIBUTE:
42221 strcpy (p_name, "#pragma omp");
42222 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
42223 if_p);
42224 break;
42225 case PRAGMA_OMP_FOR:
42226 strcpy (p_name, "#pragma omp");
42227 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
42228 if_p);
42229 break;
42230 case PRAGMA_OMP_LOOP:
42231 strcpy (p_name, "#pragma omp");
42232 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
42233 if_p);
42234 break;
42235 case PRAGMA_OMP_MASTER:
42236 strcpy (p_name, "#pragma omp");
42237 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
42238 if_p);
42239 break;
42240 case PRAGMA_OMP_PARALLEL:
42241 strcpy (p_name, "#pragma omp");
42242 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
42243 if_p);
42244 break;
42245 case PRAGMA_OMP_SECTIONS:
42246 strcpy (p_name, "#pragma omp");
42247 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
42248 break;
42249 case PRAGMA_OMP_SIMD:
42250 strcpy (p_name, "#pragma omp");
42251 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
42252 if_p);
42253 break;
42254 case PRAGMA_OMP_SINGLE:
42255 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
42256 break;
42257 case PRAGMA_OMP_TASK:
42258 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
42259 break;
42260 case PRAGMA_OMP_TASKGROUP:
42261 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
42262 break;
42263 case PRAGMA_OMP_TASKLOOP:
42264 strcpy (p_name, "#pragma omp");
42265 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
42266 if_p);
42267 break;
42268 case PRAGMA_OMP_TEAMS:
42269 strcpy (p_name, "#pragma omp");
42270 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
42271 if_p);
42272 break;
42273 default:
42274 gcc_unreachable ();
42275 }
42276
42277 protected_set_expr_location (stmt, pragma_tok->location);
42278 }
42279 \f
42280 /* Transactional Memory parsing routines. */
42281
42282 /* Parse a transaction attribute.
42283
42284 txn-attribute:
42285 attribute
42286 [ [ identifier ] ]
42287
42288 We use this instead of cp_parser_attributes_opt for transactions to avoid
42289 the pedwarn in C++98 mode. */
42290
42291 static tree
42292 cp_parser_txn_attribute_opt (cp_parser *parser)
42293 {
42294 cp_token *token;
42295 tree attr_name, attr = NULL;
42296
42297 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
42298 return cp_parser_attributes_opt (parser);
42299
42300 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
42301 return NULL_TREE;
42302 cp_lexer_consume_token (parser->lexer);
42303 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
42304 goto error1;
42305
42306 token = cp_lexer_peek_token (parser->lexer);
42307 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
42308 {
42309 token = cp_lexer_consume_token (parser->lexer);
42310
42311 attr_name = (token->type == CPP_KEYWORD
42312 /* For keywords, use the canonical spelling,
42313 not the parsed identifier. */
42314 ? ridpointers[(int) token->keyword]
42315 : token->u.value);
42316 attr = build_tree_list (attr_name, NULL_TREE);
42317 }
42318 else
42319 cp_parser_error (parser, "expected identifier");
42320
42321 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
42322 error1:
42323 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
42324 return attr;
42325 }
42326
42327 /* Parse a __transaction_atomic or __transaction_relaxed statement.
42328
42329 transaction-statement:
42330 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
42331 compound-statement
42332 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
42333 */
42334
42335 static tree
42336 cp_parser_transaction (cp_parser *parser, cp_token *token)
42337 {
42338 unsigned char old_in = parser->in_transaction;
42339 unsigned char this_in = 1, new_in;
42340 enum rid keyword = token->keyword;
42341 tree stmt, attrs, noex;
42342
42343 cp_lexer_consume_token (parser->lexer);
42344
42345 if (keyword == RID_TRANSACTION_RELAXED
42346 || keyword == RID_SYNCHRONIZED)
42347 this_in |= TM_STMT_ATTR_RELAXED;
42348 else
42349 {
42350 attrs = cp_parser_txn_attribute_opt (parser);
42351 if (attrs)
42352 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
42353 }
42354
42355 /* Parse a noexcept specification. */
42356 if (keyword == RID_ATOMIC_NOEXCEPT)
42357 noex = boolean_true_node;
42358 else if (keyword == RID_ATOMIC_CANCEL)
42359 {
42360 /* cancel-and-throw is unimplemented. */
42361 sorry ("%<atomic_cancel%>");
42362 noex = NULL_TREE;
42363 }
42364 else
42365 noex = cp_parser_noexcept_specification_opt (parser,
42366 CP_PARSER_FLAGS_NONE,
42367 /*require_constexpr=*/true,
42368 /*consumed_expr=*/NULL,
42369 /*return_cond=*/true);
42370
42371 /* Keep track if we're in the lexical scope of an outer transaction. */
42372 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
42373
42374 stmt = begin_transaction_stmt (token->location, NULL, this_in);
42375
42376 parser->in_transaction = new_in;
42377 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
42378 parser->in_transaction = old_in;
42379
42380 finish_transaction_stmt (stmt, NULL, this_in, noex);
42381
42382 return stmt;
42383 }
42384
42385 /* Parse a __transaction_atomic or __transaction_relaxed expression.
42386
42387 transaction-expression:
42388 __transaction_atomic txn-noexcept-spec[opt] ( expression )
42389 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
42390 */
42391
42392 static tree
42393 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
42394 {
42395 unsigned char old_in = parser->in_transaction;
42396 unsigned char this_in = 1;
42397 cp_token *token;
42398 tree expr, noex;
42399 bool noex_expr;
42400 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42401
42402 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
42403 || keyword == RID_TRANSACTION_RELAXED);
42404
42405 if (!flag_tm)
42406 error_at (loc,
42407 keyword == RID_TRANSACTION_RELAXED
42408 ? G_("%<__transaction_relaxed%> without transactional memory "
42409 "support enabled")
42410 : G_("%<__transaction_atomic%> without transactional memory "
42411 "support enabled"));
42412
42413 token = cp_parser_require_keyword (parser, keyword,
42414 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
42415 : RT_TRANSACTION_RELAXED));
42416 gcc_assert (token != NULL);
42417
42418 if (keyword == RID_TRANSACTION_RELAXED)
42419 this_in |= TM_STMT_ATTR_RELAXED;
42420
42421 /* Set this early. This might mean that we allow transaction_cancel in
42422 an expression that we find out later actually has to be a constexpr.
42423 However, we expect that cxx_constant_value will be able to deal with
42424 this; also, if the noexcept has no constexpr, then what we parse next
42425 really is a transaction's body. */
42426 parser->in_transaction = this_in;
42427
42428 /* Parse a noexcept specification. */
42429 noex = cp_parser_noexcept_specification_opt (parser,
42430 CP_PARSER_FLAGS_NONE,
42431 /*require_constexpr=*/false,
42432 &noex_expr,
42433 /*return_cond=*/true);
42434
42435 if (!noex || !noex_expr
42436 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
42437 {
42438 matching_parens parens;
42439 parens.require_open (parser);
42440
42441 expr = cp_parser_expression (parser);
42442 expr = finish_parenthesized_expr (expr);
42443
42444 parens.require_close (parser);
42445 }
42446 else
42447 {
42448 /* The only expression that is available got parsed for the noexcept
42449 already. noexcept is true then. */
42450 expr = noex;
42451 noex = boolean_true_node;
42452 }
42453
42454 expr = build_transaction_expr (token->location, expr, this_in, noex);
42455 parser->in_transaction = old_in;
42456
42457 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
42458 return error_mark_node;
42459
42460 return (flag_tm ? expr : error_mark_node);
42461 }
42462
42463 /* Parse a function-transaction-block.
42464
42465 function-transaction-block:
42466 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
42467 function-body
42468 __transaction_atomic txn-attribute[opt] function-try-block
42469 __transaction_relaxed ctor-initializer[opt] function-body
42470 __transaction_relaxed function-try-block
42471 */
42472
42473 static void
42474 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
42475 {
42476 unsigned char old_in = parser->in_transaction;
42477 unsigned char new_in = 1;
42478 tree compound_stmt, stmt, attrs;
42479 cp_token *token;
42480
42481 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
42482 || keyword == RID_TRANSACTION_RELAXED);
42483 token = cp_parser_require_keyword (parser, keyword,
42484 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
42485 : RT_TRANSACTION_RELAXED));
42486 gcc_assert (token != NULL);
42487
42488 if (keyword == RID_TRANSACTION_RELAXED)
42489 new_in |= TM_STMT_ATTR_RELAXED;
42490 else
42491 {
42492 attrs = cp_parser_txn_attribute_opt (parser);
42493 if (attrs)
42494 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
42495 }
42496
42497 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
42498
42499 parser->in_transaction = new_in;
42500
42501 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
42502 cp_parser_function_try_block (parser);
42503 else
42504 cp_parser_ctor_initializer_opt_and_function_body
42505 (parser, /*in_function_try_block=*/false);
42506
42507 parser->in_transaction = old_in;
42508
42509 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
42510 }
42511
42512 /* Parse a __transaction_cancel statement.
42513
42514 cancel-statement:
42515 __transaction_cancel txn-attribute[opt] ;
42516 __transaction_cancel txn-attribute[opt] throw-expression ;
42517
42518 ??? Cancel and throw is not yet implemented. */
42519
42520 static tree
42521 cp_parser_transaction_cancel (cp_parser *parser)
42522 {
42523 cp_token *token;
42524 bool is_outer = false;
42525 tree stmt, attrs;
42526
42527 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
42528 RT_TRANSACTION_CANCEL);
42529 gcc_assert (token != NULL);
42530
42531 attrs = cp_parser_txn_attribute_opt (parser);
42532 if (attrs)
42533 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
42534
42535 /* ??? Parse cancel-and-throw here. */
42536
42537 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
42538
42539 if (!flag_tm)
42540 {
42541 error_at (token->location, "%<__transaction_cancel%> without "
42542 "transactional memory support enabled");
42543 return error_mark_node;
42544 }
42545 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
42546 {
42547 error_at (token->location, "%<__transaction_cancel%> within a "
42548 "%<__transaction_relaxed%>");
42549 return error_mark_node;
42550 }
42551 else if (is_outer)
42552 {
42553 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
42554 && !is_tm_may_cancel_outer (current_function_decl))
42555 {
42556 error_at (token->location, "outer %<__transaction_cancel%> not "
42557 "within outer %<__transaction_atomic%>");
42558 error_at (token->location,
42559 " or a %<transaction_may_cancel_outer%> function");
42560 return error_mark_node;
42561 }
42562 }
42563 else if (parser->in_transaction == 0)
42564 {
42565 error_at (token->location, "%<__transaction_cancel%> not within "
42566 "%<__transaction_atomic%>");
42567 return error_mark_node;
42568 }
42569
42570 stmt = build_tm_abort_call (token->location, is_outer);
42571 add_stmt (stmt);
42572
42573 return stmt;
42574 }
42575 \f
42576 /* The parser. */
42577
42578 static GTY (()) cp_parser *the_parser;
42579
42580 \f
42581 /* Special handling for the first token or line in the file. The first
42582 thing in the file might be #pragma GCC pch_preprocess, which loads a
42583 PCH file, which is a GC collection point. So we need to handle this
42584 first pragma without benefit of an existing lexer structure.
42585
42586 Always returns one token to the caller in *FIRST_TOKEN. This is
42587 either the true first token of the file, or the first token after
42588 the initial pragma. */
42589
42590 static void
42591 cp_parser_initial_pragma (cp_token *first_token)
42592 {
42593 tree name = NULL;
42594
42595 cp_lexer_get_preprocessor_token (NULL, first_token);
42596 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
42597 {
42598 c_common_no_more_pch ();
42599 return;
42600 }
42601
42602 cp_lexer_get_preprocessor_token (NULL, first_token);
42603 if (first_token->type == CPP_STRING)
42604 {
42605 name = first_token->u.value;
42606
42607 cp_lexer_get_preprocessor_token (NULL, first_token);
42608 if (first_token->type != CPP_PRAGMA_EOL)
42609 error_at (first_token->location,
42610 "junk at end of %<#pragma GCC pch_preprocess%>");
42611 }
42612 else
42613 error_at (first_token->location, "expected string literal");
42614
42615 /* Skip to the end of the pragma. */
42616 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
42617 cp_lexer_get_preprocessor_token (NULL, first_token);
42618
42619 /* Now actually load the PCH file. */
42620 if (name)
42621 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
42622
42623 /* Read one more token to return to our caller. We have to do this
42624 after reading the PCH file in, since its pointers have to be
42625 live. */
42626 cp_lexer_get_preprocessor_token (NULL, first_token);
42627 }
42628
42629 /* Parse a pragma GCC ivdep. */
42630
42631 static bool
42632 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
42633 {
42634 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42635 return true;
42636 }
42637
42638 /* Parse a pragma GCC unroll. */
42639
42640 static unsigned short
42641 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
42642 {
42643 location_t location = cp_lexer_peek_token (parser->lexer)->location;
42644 tree expr = cp_parser_constant_expression (parser);
42645 unsigned short unroll;
42646 expr = maybe_constant_value (expr);
42647 HOST_WIDE_INT lunroll = 0;
42648 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
42649 || TREE_CODE (expr) != INTEGER_CST
42650 || (lunroll = tree_to_shwi (expr)) < 0
42651 || lunroll >= USHRT_MAX)
42652 {
42653 error_at (location, "%<#pragma GCC unroll%> requires an"
42654 " assignment-expression that evaluates to a non-negative"
42655 " integral constant less than %u", USHRT_MAX);
42656 unroll = 0;
42657 }
42658 else
42659 {
42660 unroll = (unsigned short)lunroll;
42661 if (unroll == 0)
42662 unroll = 1;
42663 }
42664 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42665 return unroll;
42666 }
42667
42668 /* Normal parsing of a pragma token. Here we can (and must) use the
42669 regular lexer. */
42670
42671 static bool
42672 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
42673 {
42674 cp_token *pragma_tok;
42675 unsigned int id;
42676 tree stmt;
42677 bool ret;
42678
42679 pragma_tok = cp_lexer_consume_token (parser->lexer);
42680 gcc_assert (pragma_tok->type == CPP_PRAGMA);
42681 parser->lexer->in_pragma = true;
42682
42683 id = cp_parser_pragma_kind (pragma_tok);
42684 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
42685 cp_ensure_no_omp_declare_simd (parser);
42686 switch (id)
42687 {
42688 case PRAGMA_GCC_PCH_PREPROCESS:
42689 error_at (pragma_tok->location,
42690 "%<#pragma GCC pch_preprocess%> must be first");
42691 break;
42692
42693 case PRAGMA_OMP_BARRIER:
42694 switch (context)
42695 {
42696 case pragma_compound:
42697 cp_parser_omp_barrier (parser, pragma_tok);
42698 return false;
42699 case pragma_stmt:
42700 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
42701 "used in compound statements", "omp barrier");
42702 break;
42703 default:
42704 goto bad_stmt;
42705 }
42706 break;
42707
42708 case PRAGMA_OMP_DEPOBJ:
42709 switch (context)
42710 {
42711 case pragma_compound:
42712 cp_parser_omp_depobj (parser, pragma_tok);
42713 return false;
42714 case pragma_stmt:
42715 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
42716 "used in compound statements", "omp depobj");
42717 break;
42718 default:
42719 goto bad_stmt;
42720 }
42721 break;
42722
42723 case PRAGMA_OMP_FLUSH:
42724 switch (context)
42725 {
42726 case pragma_compound:
42727 cp_parser_omp_flush (parser, pragma_tok);
42728 return false;
42729 case pragma_stmt:
42730 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
42731 "used in compound statements", "omp flush");
42732 break;
42733 default:
42734 goto bad_stmt;
42735 }
42736 break;
42737
42738 case PRAGMA_OMP_TASKWAIT:
42739 switch (context)
42740 {
42741 case pragma_compound:
42742 cp_parser_omp_taskwait (parser, pragma_tok);
42743 return false;
42744 case pragma_stmt:
42745 error_at (pragma_tok->location,
42746 "%<#pragma %s%> may only be used in compound statements",
42747 "omp taskwait");
42748 break;
42749 default:
42750 goto bad_stmt;
42751 }
42752 break;
42753
42754 case PRAGMA_OMP_TASKYIELD:
42755 switch (context)
42756 {
42757 case pragma_compound:
42758 cp_parser_omp_taskyield (parser, pragma_tok);
42759 return false;
42760 case pragma_stmt:
42761 error_at (pragma_tok->location,
42762 "%<#pragma %s%> may only be used in compound statements",
42763 "omp taskyield");
42764 break;
42765 default:
42766 goto bad_stmt;
42767 }
42768 break;
42769
42770 case PRAGMA_OMP_CANCEL:
42771 switch (context)
42772 {
42773 case pragma_compound:
42774 cp_parser_omp_cancel (parser, pragma_tok);
42775 return false;
42776 case pragma_stmt:
42777 error_at (pragma_tok->location,
42778 "%<#pragma %s%> may only be used in compound statements",
42779 "omp cancel");
42780 break;
42781 default:
42782 goto bad_stmt;
42783 }
42784 break;
42785
42786 case PRAGMA_OMP_CANCELLATION_POINT:
42787 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
42788 return false;
42789
42790 case PRAGMA_OMP_THREADPRIVATE:
42791 cp_parser_omp_threadprivate (parser, pragma_tok);
42792 return false;
42793
42794 case PRAGMA_OMP_DECLARE:
42795 return cp_parser_omp_declare (parser, pragma_tok, context);
42796
42797 case PRAGMA_OACC_DECLARE:
42798 cp_parser_oacc_declare (parser, pragma_tok);
42799 return false;
42800
42801 case PRAGMA_OACC_ENTER_DATA:
42802 if (context == pragma_stmt)
42803 {
42804 error_at (pragma_tok->location,
42805 "%<#pragma %s%> may only be used in compound statements",
42806 "acc enter data");
42807 break;
42808 }
42809 else if (context != pragma_compound)
42810 goto bad_stmt;
42811 cp_parser_omp_construct (parser, pragma_tok, if_p);
42812 return true;
42813
42814 case PRAGMA_OACC_EXIT_DATA:
42815 if (context == pragma_stmt)
42816 {
42817 error_at (pragma_tok->location,
42818 "%<#pragma %s%> may only be used in compound statements",
42819 "acc exit data");
42820 break;
42821 }
42822 else if (context != pragma_compound)
42823 goto bad_stmt;
42824 cp_parser_omp_construct (parser, pragma_tok, if_p);
42825 return true;
42826
42827 case PRAGMA_OACC_ROUTINE:
42828 if (context != pragma_external)
42829 {
42830 error_at (pragma_tok->location,
42831 "%<#pragma acc routine%> must be at file scope");
42832 break;
42833 }
42834 cp_parser_oacc_routine (parser, pragma_tok, context);
42835 return false;
42836
42837 case PRAGMA_OACC_UPDATE:
42838 if (context == pragma_stmt)
42839 {
42840 error_at (pragma_tok->location,
42841 "%<#pragma %s%> may only be used in compound statements",
42842 "acc update");
42843 break;
42844 }
42845 else if (context != pragma_compound)
42846 goto bad_stmt;
42847 cp_parser_omp_construct (parser, pragma_tok, if_p);
42848 return true;
42849
42850 case PRAGMA_OACC_WAIT:
42851 if (context == pragma_stmt)
42852 {
42853 error_at (pragma_tok->location,
42854 "%<#pragma %s%> may only be used in compound statements",
42855 "acc wait");
42856 break;
42857 }
42858 else if (context != pragma_compound)
42859 goto bad_stmt;
42860 cp_parser_omp_construct (parser, pragma_tok, if_p);
42861 return true;
42862
42863 case PRAGMA_OACC_ATOMIC:
42864 case PRAGMA_OACC_CACHE:
42865 case PRAGMA_OACC_DATA:
42866 case PRAGMA_OACC_HOST_DATA:
42867 case PRAGMA_OACC_KERNELS:
42868 case PRAGMA_OACC_LOOP:
42869 case PRAGMA_OACC_PARALLEL:
42870 case PRAGMA_OACC_SERIAL:
42871 case PRAGMA_OMP_ATOMIC:
42872 case PRAGMA_OMP_CRITICAL:
42873 case PRAGMA_OMP_DISTRIBUTE:
42874 case PRAGMA_OMP_FOR:
42875 case PRAGMA_OMP_LOOP:
42876 case PRAGMA_OMP_MASTER:
42877 case PRAGMA_OMP_PARALLEL:
42878 case PRAGMA_OMP_SECTIONS:
42879 case PRAGMA_OMP_SIMD:
42880 case PRAGMA_OMP_SINGLE:
42881 case PRAGMA_OMP_TASK:
42882 case PRAGMA_OMP_TASKGROUP:
42883 case PRAGMA_OMP_TASKLOOP:
42884 case PRAGMA_OMP_TEAMS:
42885 if (context != pragma_stmt && context != pragma_compound)
42886 goto bad_stmt;
42887 stmt = push_omp_privatization_clauses (false);
42888 cp_parser_omp_construct (parser, pragma_tok, if_p);
42889 pop_omp_privatization_clauses (stmt);
42890 return true;
42891
42892 case PRAGMA_OMP_REQUIRES:
42893 return cp_parser_omp_requires (parser, pragma_tok);
42894
42895 case PRAGMA_OMP_ORDERED:
42896 if (context != pragma_stmt && context != pragma_compound)
42897 goto bad_stmt;
42898 stmt = push_omp_privatization_clauses (false);
42899 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
42900 pop_omp_privatization_clauses (stmt);
42901 return ret;
42902
42903 case PRAGMA_OMP_TARGET:
42904 if (context != pragma_stmt && context != pragma_compound)
42905 goto bad_stmt;
42906 stmt = push_omp_privatization_clauses (false);
42907 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
42908 pop_omp_privatization_clauses (stmt);
42909 return ret;
42910
42911 case PRAGMA_OMP_END_DECLARE_TARGET:
42912 cp_parser_omp_end_declare_target (parser, pragma_tok);
42913 return false;
42914
42915 case PRAGMA_OMP_SCAN:
42916 error_at (pragma_tok->location,
42917 "%<#pragma omp scan%> may only be used in "
42918 "a loop construct with %<inscan%> %<reduction%> clause");
42919 break;
42920
42921 case PRAGMA_OMP_SECTION:
42922 error_at (pragma_tok->location,
42923 "%<#pragma omp section%> may only be used in "
42924 "%<#pragma omp sections%> construct");
42925 break;
42926
42927 case PRAGMA_IVDEP:
42928 {
42929 if (context == pragma_external)
42930 {
42931 error_at (pragma_tok->location,
42932 "%<#pragma GCC ivdep%> must be inside a function");
42933 break;
42934 }
42935 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
42936 unsigned short unroll;
42937 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
42938 if (tok->type == CPP_PRAGMA
42939 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
42940 {
42941 tok = cp_lexer_consume_token (parser->lexer);
42942 unroll = cp_parser_pragma_unroll (parser, tok);
42943 tok = cp_lexer_peek_token (the_parser->lexer);
42944 }
42945 else
42946 unroll = 0;
42947 if (tok->type != CPP_KEYWORD
42948 || (tok->keyword != RID_FOR
42949 && tok->keyword != RID_WHILE
42950 && tok->keyword != RID_DO))
42951 {
42952 cp_parser_error (parser, "for, while or do statement expected");
42953 return false;
42954 }
42955 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
42956 return true;
42957 }
42958
42959 case PRAGMA_UNROLL:
42960 {
42961 if (context == pragma_external)
42962 {
42963 error_at (pragma_tok->location,
42964 "%<#pragma GCC unroll%> must be inside a function");
42965 break;
42966 }
42967 const unsigned short unroll
42968 = cp_parser_pragma_unroll (parser, pragma_tok);
42969 bool ivdep;
42970 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
42971 if (tok->type == CPP_PRAGMA
42972 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
42973 {
42974 tok = cp_lexer_consume_token (parser->lexer);
42975 ivdep = cp_parser_pragma_ivdep (parser, tok);
42976 tok = cp_lexer_peek_token (the_parser->lexer);
42977 }
42978 else
42979 ivdep = false;
42980 if (tok->type != CPP_KEYWORD
42981 || (tok->keyword != RID_FOR
42982 && tok->keyword != RID_WHILE
42983 && tok->keyword != RID_DO))
42984 {
42985 cp_parser_error (parser, "for, while or do statement expected");
42986 return false;
42987 }
42988 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
42989 return true;
42990 }
42991
42992 default:
42993 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
42994 c_invoke_pragma_handler (id);
42995 break;
42996
42997 bad_stmt:
42998 cp_parser_error (parser, "expected declaration specifiers");
42999 break;
43000 }
43001
43002 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43003 return false;
43004 }
43005
43006 /* The interface the pragma parsers have to the lexer. */
43007
43008 enum cpp_ttype
43009 pragma_lex (tree *value, location_t *loc)
43010 {
43011 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
43012 enum cpp_ttype ret = tok->type;
43013
43014 *value = tok->u.value;
43015 if (loc)
43016 *loc = tok->location;
43017
43018 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
43019 ret = CPP_EOF;
43020 else if (ret == CPP_STRING)
43021 *value = cp_parser_string_literal (the_parser, false, false);
43022 else
43023 {
43024 if (ret == CPP_KEYWORD)
43025 ret = CPP_NAME;
43026 cp_lexer_consume_token (the_parser->lexer);
43027 }
43028
43029 return ret;
43030 }
43031
43032 \f
43033 /* External interface. */
43034
43035 /* Parse one entire translation unit. */
43036
43037 void
43038 c_parse_file (void)
43039 {
43040 static bool already_called = false;
43041
43042 if (already_called)
43043 fatal_error (input_location,
43044 "inter-module optimizations not implemented for C++");
43045 already_called = true;
43046
43047 the_parser = cp_parser_new ();
43048 push_deferring_access_checks (flag_access_control
43049 ? dk_no_deferred : dk_no_check);
43050 cp_parser_translation_unit (the_parser);
43051 the_parser = NULL;
43052
43053 finish_translation_unit ();
43054 }
43055
43056 /* Create an identifier for a generic parameter type (a synthesized
43057 template parameter implied by `auto' or a concept identifier). */
43058
43059 static GTY(()) int generic_parm_count;
43060 static tree
43061 make_generic_type_name ()
43062 {
43063 char buf[32];
43064 sprintf (buf, "auto:%d", ++generic_parm_count);
43065 return get_identifier (buf);
43066 }
43067
43068 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
43069 (creating a new template parameter list if necessary). Returns the newly
43070 created template type parm. */
43071
43072 static tree
43073 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
43074 {
43075 /* A requires-clause is not a function and cannot have placeholders. */
43076 if (current_binding_level->kind == sk_block)
43077 {
43078 error ("placeholder type not allowed in this context");
43079 return error_mark_node;
43080 }
43081
43082 gcc_assert (current_binding_level->kind == sk_function_parms);
43083
43084 /* We are either continuing a function template that already contains implicit
43085 template parameters, creating a new fully-implicit function template, or
43086 extending an existing explicit function template with implicit template
43087 parameters. */
43088
43089 cp_binding_level *const entry_scope = current_binding_level;
43090
43091 bool become_template = false;
43092 cp_binding_level *parent_scope = 0;
43093
43094 if (parser->implicit_template_scope)
43095 {
43096 gcc_assert (parser->implicit_template_parms);
43097
43098 current_binding_level = parser->implicit_template_scope;
43099 }
43100 else
43101 {
43102 /* Roll back to the existing template parameter scope (in the case of
43103 extending an explicit function template) or introduce a new template
43104 parameter scope ahead of the function parameter scope (or class scope
43105 in the case of out-of-line member definitions). The function scope is
43106 added back after template parameter synthesis below. */
43107
43108 cp_binding_level *scope = entry_scope;
43109
43110 while (scope->kind == sk_function_parms)
43111 {
43112 parent_scope = scope;
43113 scope = scope->level_chain;
43114 }
43115 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
43116 {
43117 /* If not defining a class, then any class scope is a scope level in
43118 an out-of-line member definition. In this case simply wind back
43119 beyond the first such scope to inject the template parameter list.
43120 Otherwise wind back to the class being defined. The latter can
43121 occur in class member friend declarations such as:
43122
43123 class A {
43124 void foo (auto);
43125 };
43126 class B {
43127 friend void A::foo (auto);
43128 };
43129
43130 The template parameter list synthesized for the friend declaration
43131 must be injected in the scope of 'B'. This can also occur in
43132 erroneous cases such as:
43133
43134 struct A {
43135 struct B {
43136 void foo (auto);
43137 };
43138 void B::foo (auto) {}
43139 };
43140
43141 Here the attempted definition of 'B::foo' within 'A' is ill-formed
43142 but, nevertheless, the template parameter list synthesized for the
43143 declarator should be injected into the scope of 'A' as if the
43144 ill-formed template was specified explicitly. */
43145
43146 while (scope->kind == sk_class && !scope->defining_class_p)
43147 {
43148 parent_scope = scope;
43149 scope = scope->level_chain;
43150 }
43151 }
43152
43153 current_binding_level = scope;
43154
43155 if (scope->kind != sk_template_parms
43156 || !function_being_declared_is_template_p (parser))
43157 {
43158 /* Introduce a new template parameter list for implicit template
43159 parameters. */
43160
43161 become_template = true;
43162
43163 parser->implicit_template_scope
43164 = begin_scope (sk_template_parms, NULL);
43165
43166 ++processing_template_decl;
43167
43168 parser->fully_implicit_function_template_p = true;
43169 ++parser->num_template_parameter_lists;
43170 }
43171 else
43172 {
43173 /* Synthesize implicit template parameters at the end of the explicit
43174 template parameter list. */
43175
43176 gcc_assert (current_template_parms);
43177
43178 parser->implicit_template_scope = scope;
43179
43180 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
43181 parser->implicit_template_parms
43182 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
43183 }
43184 }
43185
43186 /* Synthesize a new template parameter and track the current template
43187 parameter chain with implicit_template_parms. */
43188
43189 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
43190 tree synth_id = make_generic_type_name ();
43191 tree synth_tmpl_parm;
43192 bool non_type = false;
43193
43194 /* Synthesize the type template parameter. */
43195 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
43196 synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
43197
43198 /* Attach the constraint to the parm before processing. */
43199 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
43200 TREE_TYPE (node) = constr;
43201 tree new_parm
43202 = process_template_parm (parser->implicit_template_parms,
43203 input_location,
43204 node,
43205 /*non_type=*/non_type,
43206 /*param_pack=*/false);
43207
43208 /* Mark the synthetic declaration "virtual". This is used when
43209 comparing template-heads to determine if whether an abbreviated
43210 function template is equivalent to an explicit template.
43211
43212 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
43213 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
43214
43215 // Chain the new parameter to the list of implicit parameters.
43216 if (parser->implicit_template_parms)
43217 parser->implicit_template_parms
43218 = TREE_CHAIN (parser->implicit_template_parms);
43219 else
43220 parser->implicit_template_parms = new_parm;
43221
43222 tree new_decl = get_local_decls ();
43223 if (non_type)
43224 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
43225 new_decl = DECL_INITIAL (new_decl);
43226
43227 /* If creating a fully implicit function template, start the new implicit
43228 template parameter list with this synthesized type, otherwise grow the
43229 current template parameter list. */
43230
43231 if (become_template)
43232 {
43233 parent_scope->level_chain = current_binding_level;
43234
43235 tree new_parms = make_tree_vec (1);
43236 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
43237 current_template_parms = tree_cons (size_int (processing_template_decl),
43238 new_parms, current_template_parms);
43239 }
43240 else
43241 {
43242 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
43243 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
43244 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
43245 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
43246 }
43247
43248 /* If the new parameter was constrained, we need to add that to the
43249 constraints in the template parameter list. */
43250 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
43251 {
43252 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
43253 reqs = combine_constraint_expressions (reqs, req);
43254 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
43255 }
43256
43257 current_binding_level = entry_scope;
43258
43259 return new_decl;
43260 }
43261
43262 /* Finish the declaration of a fully implicit function template. Such a
43263 template has no explicit template parameter list so has not been through the
43264 normal template head and tail processing. synthesize_implicit_template_parm
43265 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
43266 provided if the declaration is a class member such that its template
43267 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
43268 form is returned. Otherwise NULL_TREE is returned. */
43269
43270 static tree
43271 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
43272 {
43273 gcc_assert (parser->fully_implicit_function_template_p);
43274
43275 if (member_decl_opt && member_decl_opt != error_mark_node
43276 && DECL_VIRTUAL_P (member_decl_opt))
43277 {
43278 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
43279 "implicit templates may not be %<virtual%>");
43280 DECL_VIRTUAL_P (member_decl_opt) = false;
43281 }
43282
43283 if (member_decl_opt)
43284 member_decl_opt = finish_member_template_decl (member_decl_opt);
43285 end_template_decl ();
43286
43287 parser->fully_implicit_function_template_p = false;
43288 parser->implicit_template_parms = 0;
43289 parser->implicit_template_scope = 0;
43290 --parser->num_template_parameter_lists;
43291
43292 return member_decl_opt;
43293 }
43294
43295 /* Like finish_fully_implicit_template, but to be used in error
43296 recovery, rearranging scopes so that we restore the state we had
43297 before synthesize_implicit_template_parm inserted the implement
43298 template parms scope. */
43299
43300 static void
43301 abort_fully_implicit_template (cp_parser *parser)
43302 {
43303 cp_binding_level *return_to_scope = current_binding_level;
43304
43305 if (parser->implicit_template_scope
43306 && return_to_scope != parser->implicit_template_scope)
43307 {
43308 cp_binding_level *child = return_to_scope;
43309 for (cp_binding_level *scope = child->level_chain;
43310 scope != parser->implicit_template_scope;
43311 scope = child->level_chain)
43312 child = scope;
43313 child->level_chain = parser->implicit_template_scope->level_chain;
43314 parser->implicit_template_scope->level_chain = return_to_scope;
43315 current_binding_level = parser->implicit_template_scope;
43316 }
43317 else
43318 return_to_scope = return_to_scope->level_chain;
43319
43320 finish_fully_implicit_template (parser, NULL);
43321
43322 gcc_assert (current_binding_level == return_to_scope);
43323 }
43324
43325 /* Helper function for diagnostics that have complained about things
43326 being used with 'extern "C"' linkage.
43327
43328 Attempt to issue a note showing where the 'extern "C"' linkage began. */
43329
43330 void
43331 maybe_show_extern_c_location (void)
43332 {
43333 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
43334 inform (the_parser->innermost_linkage_specification_location,
43335 "%<extern \"C\"%> linkage started here");
43336 }
43337
43338 #include "gt-cp-parser.h"