]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
ed2441644f1a314c1969928325e17340f91be06b
[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 cast = build_functional_cast (type, expression_list,
29272 tf_warning_or_error);
29273 /* [expr.const]/1: In an integral constant expression "only type
29274 conversions to integral or enumeration type can be used". */
29275 if (TREE_CODE (type) == TYPE_DECL)
29276 type = TREE_TYPE (type);
29277 if (cast != error_mark_node
29278 && !cast_valid_in_integral_constant_expression_p (type)
29279 && cp_parser_non_integral_constant_expression (parser,
29280 NIC_CONSTRUCTOR))
29281 return error_mark_node;
29282
29283 /* Create a location of the form:
29284 float(i)
29285 ^~~~~~~~
29286 with caret == start at the start of the type name,
29287 finishing at the closing paren. */
29288 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
29289 cast.set_location (combined_loc);
29290 return cast;
29291 }
29292
29293 /* Save the tokens that make up the body of a member function defined
29294 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
29295 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
29296 specifiers applied to the declaration. Returns the FUNCTION_DECL
29297 for the member function. */
29298
29299 static tree
29300 cp_parser_save_member_function_body (cp_parser* parser,
29301 cp_decl_specifier_seq *decl_specifiers,
29302 cp_declarator *declarator,
29303 tree attributes)
29304 {
29305 cp_token *first;
29306 cp_token *last;
29307 tree fn;
29308 bool function_try_block = false;
29309
29310 /* Create the FUNCTION_DECL. */
29311 fn = grokmethod (decl_specifiers, declarator, attributes);
29312 cp_finalize_omp_declare_simd (parser, fn);
29313 cp_finalize_oacc_routine (parser, fn, true);
29314 /* If something went badly wrong, bail out now. */
29315 if (fn == error_mark_node)
29316 {
29317 /* If there's a function-body, skip it. */
29318 if (cp_parser_token_starts_function_definition_p
29319 (cp_lexer_peek_token (parser->lexer)))
29320 cp_parser_skip_to_end_of_block_or_statement (parser);
29321 return error_mark_node;
29322 }
29323
29324 /* Remember it, if there are default args to post process. */
29325 cp_parser_save_default_args (parser, fn);
29326
29327 /* Save away the tokens that make up the body of the
29328 function. */
29329 first = parser->lexer->next_token;
29330
29331 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
29332 cp_lexer_consume_token (parser->lexer);
29333 else if (cp_lexer_next_token_is_keyword (parser->lexer,
29334 RID_TRANSACTION_ATOMIC))
29335 {
29336 cp_lexer_consume_token (parser->lexer);
29337 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
29338 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
29339 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
29340 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
29341 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
29342 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
29343 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
29344 {
29345 cp_lexer_consume_token (parser->lexer);
29346 cp_lexer_consume_token (parser->lexer);
29347 cp_lexer_consume_token (parser->lexer);
29348 cp_lexer_consume_token (parser->lexer);
29349 cp_lexer_consume_token (parser->lexer);
29350 }
29351 else
29352 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
29353 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
29354 {
29355 cp_lexer_consume_token (parser->lexer);
29356 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
29357 break;
29358 }
29359 }
29360
29361 /* Handle function try blocks. */
29362 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
29363 {
29364 cp_lexer_consume_token (parser->lexer);
29365 function_try_block = true;
29366 }
29367 /* We can have braced-init-list mem-initializers before the fn body. */
29368 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29369 {
29370 cp_lexer_consume_token (parser->lexer);
29371 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
29372 {
29373 /* cache_group will stop after an un-nested { } pair, too. */
29374 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
29375 break;
29376
29377 /* variadic mem-inits have ... after the ')'. */
29378 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29379 cp_lexer_consume_token (parser->lexer);
29380 }
29381 }
29382 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29383 /* Handle function try blocks. */
29384 if (function_try_block)
29385 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
29386 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29387 last = parser->lexer->next_token;
29388
29389 /* Save away the inline definition; we will process it when the
29390 class is complete. */
29391 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
29392 DECL_PENDING_INLINE_P (fn) = 1;
29393
29394 /* We need to know that this was defined in the class, so that
29395 friend templates are handled correctly. */
29396 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
29397
29398 /* Add FN to the queue of functions to be parsed later. */
29399 vec_safe_push (unparsed_funs_with_definitions, fn);
29400
29401 return fn;
29402 }
29403
29404 /* Save the tokens that make up the in-class initializer for a non-static
29405 data member. Returns a DEFERRED_PARSE. */
29406
29407 static tree
29408 cp_parser_save_nsdmi (cp_parser* parser)
29409 {
29410 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
29411 }
29412
29413 /* Parse a template-argument-list, as well as the trailing ">" (but
29414 not the opening "<"). See cp_parser_template_argument_list for the
29415 return value. */
29416
29417 static tree
29418 cp_parser_enclosed_template_argument_list (cp_parser* parser)
29419 {
29420 tree arguments;
29421 tree saved_scope;
29422 tree saved_qualifying_scope;
29423 tree saved_object_scope;
29424 bool saved_greater_than_is_operator_p;
29425
29426 /* [temp.names]
29427
29428 When parsing a template-id, the first non-nested `>' is taken as
29429 the end of the template-argument-list rather than a greater-than
29430 operator. */
29431 saved_greater_than_is_operator_p
29432 = parser->greater_than_is_operator_p;
29433 parser->greater_than_is_operator_p = false;
29434 /* Parsing the argument list may modify SCOPE, so we save it
29435 here. */
29436 saved_scope = parser->scope;
29437 saved_qualifying_scope = parser->qualifying_scope;
29438 saved_object_scope = parser->object_scope;
29439 /* We need to evaluate the template arguments, even though this
29440 template-id may be nested within a "sizeof". */
29441 cp_evaluated ev;
29442 /* Parse the template-argument-list itself. */
29443 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
29444 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
29445 arguments = NULL_TREE;
29446 else
29447 arguments = cp_parser_template_argument_list (parser);
29448 /* Look for the `>' that ends the template-argument-list. If we find
29449 a '>>' instead, it's probably just a typo. */
29450 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
29451 {
29452 if (cxx_dialect != cxx98)
29453 {
29454 /* In C++0x, a `>>' in a template argument list or cast
29455 expression is considered to be two separate `>'
29456 tokens. So, change the current token to a `>', but don't
29457 consume it: it will be consumed later when the outer
29458 template argument list (or cast expression) is parsed.
29459 Note that this replacement of `>' for `>>' is necessary
29460 even if we are parsing tentatively: in the tentative
29461 case, after calling
29462 cp_parser_enclosed_template_argument_list we will always
29463 throw away all of the template arguments and the first
29464 closing `>', either because the template argument list
29465 was erroneous or because we are replacing those tokens
29466 with a CPP_TEMPLATE_ID token. The second `>' (which will
29467 not have been thrown away) is needed either to close an
29468 outer template argument list or to complete a new-style
29469 cast. */
29470 cp_token *token = cp_lexer_peek_token (parser->lexer);
29471 token->type = CPP_GREATER;
29472 }
29473 else if (!saved_greater_than_is_operator_p)
29474 {
29475 /* If we're in a nested template argument list, the '>>' has
29476 to be a typo for '> >'. We emit the error message, but we
29477 continue parsing and we push a '>' as next token, so that
29478 the argument list will be parsed correctly. Note that the
29479 global source location is still on the token before the
29480 '>>', so we need to say explicitly where we want it. */
29481 cp_token *token = cp_lexer_peek_token (parser->lexer);
29482 gcc_rich_location richloc (token->location);
29483 richloc.add_fixit_replace ("> >");
29484 error_at (&richloc, "%<>>%> should be %<> >%> "
29485 "within a nested template argument list");
29486
29487 token->type = CPP_GREATER;
29488 }
29489 else
29490 {
29491 /* If this is not a nested template argument list, the '>>'
29492 is a typo for '>'. Emit an error message and continue.
29493 Same deal about the token location, but here we can get it
29494 right by consuming the '>>' before issuing the diagnostic. */
29495 cp_token *token = cp_lexer_consume_token (parser->lexer);
29496 error_at (token->location,
29497 "spurious %<>>%>, use %<>%> to terminate "
29498 "a template argument list");
29499 }
29500 }
29501 else
29502 cp_parser_skip_to_end_of_template_parameter_list (parser);
29503 /* The `>' token might be a greater-than operator again now. */
29504 parser->greater_than_is_operator_p
29505 = saved_greater_than_is_operator_p;
29506 /* Restore the SAVED_SCOPE. */
29507 parser->scope = saved_scope;
29508 parser->qualifying_scope = saved_qualifying_scope;
29509 parser->object_scope = saved_object_scope;
29510
29511 return arguments;
29512 }
29513
29514 /* MEMBER_FUNCTION is a member function, or a friend. If default
29515 arguments, or the body of the function have not yet been parsed,
29516 parse them now. */
29517
29518 static void
29519 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
29520 {
29521 timevar_push (TV_PARSE_INMETH);
29522 /* If this member is a template, get the underlying
29523 FUNCTION_DECL. */
29524 if (DECL_FUNCTION_TEMPLATE_P (member_function))
29525 member_function = DECL_TEMPLATE_RESULT (member_function);
29526
29527 /* There should not be any class definitions in progress at this
29528 point; the bodies of members are only parsed outside of all class
29529 definitions. */
29530 gcc_assert (parser->num_classes_being_defined == 0);
29531 /* While we're parsing the member functions we might encounter more
29532 classes. We want to handle them right away, but we don't want
29533 them getting mixed up with functions that are currently in the
29534 queue. */
29535 push_unparsed_function_queues (parser);
29536
29537 /* Make sure that any template parameters are in scope. */
29538 maybe_begin_member_template_processing (member_function);
29539
29540 /* If the body of the function has not yet been parsed, parse it
29541 now. */
29542 if (DECL_PENDING_INLINE_P (member_function))
29543 {
29544 tree function_scope;
29545 cp_token_cache *tokens;
29546
29547 /* The function is no longer pending; we are processing it. */
29548 tokens = DECL_PENDING_INLINE_INFO (member_function);
29549 DECL_PENDING_INLINE_INFO (member_function) = NULL;
29550 DECL_PENDING_INLINE_P (member_function) = 0;
29551
29552 /* If this is a local class, enter the scope of the containing
29553 function. */
29554 function_scope = current_function_decl;
29555 if (function_scope)
29556 push_function_context ();
29557
29558 /* Push the body of the function onto the lexer stack. */
29559 cp_parser_push_lexer_for_tokens (parser, tokens);
29560
29561 /* Let the front end know that we going to be defining this
29562 function. */
29563 start_preparsed_function (member_function, NULL_TREE,
29564 SF_PRE_PARSED | SF_INCLASS_INLINE);
29565
29566 /* Don't do access checking if it is a templated function. */
29567 if (processing_template_decl)
29568 push_deferring_access_checks (dk_no_check);
29569
29570 /* #pragma omp declare reduction needs special parsing. */
29571 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
29572 {
29573 parser->lexer->in_pragma = true;
29574 cp_parser_omp_declare_reduction_exprs (member_function, parser);
29575 finish_function (/*inline_p=*/true);
29576 cp_check_omp_declare_reduction (member_function);
29577 }
29578 else
29579 /* Now, parse the body of the function. */
29580 cp_parser_function_definition_after_declarator (parser,
29581 /*inline_p=*/true);
29582
29583 if (processing_template_decl)
29584 pop_deferring_access_checks ();
29585
29586 /* Leave the scope of the containing function. */
29587 if (function_scope)
29588 pop_function_context ();
29589 cp_parser_pop_lexer (parser);
29590 }
29591
29592 /* Remove any template parameters from the symbol table. */
29593 maybe_end_member_template_processing ();
29594
29595 /* Restore the queue. */
29596 pop_unparsed_function_queues (parser);
29597 timevar_pop (TV_PARSE_INMETH);
29598 }
29599
29600 /* If DECL contains any default args, remember it on the unparsed
29601 functions queue. */
29602
29603 static void
29604 cp_parser_save_default_args (cp_parser* parser, tree decl)
29605 {
29606 tree probe;
29607
29608 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
29609 probe;
29610 probe = TREE_CHAIN (probe))
29611 if (TREE_PURPOSE (probe))
29612 {
29613 cp_default_arg_entry entry = {current_class_type, decl};
29614 vec_safe_push (unparsed_funs_with_default_args, entry);
29615 break;
29616 }
29617
29618 /* Remember if there is a noexcept-specifier to post process. */
29619 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
29620 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
29621 vec_safe_push (unparsed_noexcepts, decl);
29622 }
29623
29624 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
29625 which is either a FIELD_DECL or PARM_DECL. Parse it and return
29626 the result. For a PARM_DECL, PARMTYPE is the corresponding type
29627 from the parameter-type-list. */
29628
29629 static tree
29630 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
29631 tree default_arg, tree parmtype)
29632 {
29633 cp_token_cache *tokens;
29634 tree parsed_arg;
29635 bool dummy;
29636
29637 if (default_arg == error_mark_node)
29638 return error_mark_node;
29639
29640 /* Push the saved tokens for the default argument onto the parser's
29641 lexer stack. */
29642 tokens = DEFPARSE_TOKENS (default_arg);
29643 cp_parser_push_lexer_for_tokens (parser, tokens);
29644
29645 start_lambda_scope (decl);
29646
29647 /* Parse the default argument. */
29648 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
29649 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
29650 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
29651
29652 finish_lambda_scope ();
29653
29654 if (parsed_arg == error_mark_node)
29655 cp_parser_skip_to_end_of_statement (parser);
29656
29657 if (!processing_template_decl)
29658 {
29659 /* In a non-template class, check conversions now. In a template,
29660 we'll wait and instantiate these as needed. */
29661 if (TREE_CODE (decl) == PARM_DECL)
29662 parsed_arg = check_default_argument (parmtype, parsed_arg,
29663 tf_warning_or_error);
29664 else if (maybe_reject_flexarray_init (decl, parsed_arg))
29665 parsed_arg = error_mark_node;
29666 else
29667 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
29668 }
29669
29670 /* If the token stream has not been completely used up, then
29671 there was extra junk after the end of the default
29672 argument. */
29673 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29674 {
29675 if (TREE_CODE (decl) == PARM_DECL)
29676 cp_parser_error (parser, "expected %<,%>");
29677 else
29678 cp_parser_error (parser, "expected %<;%>");
29679 }
29680
29681 /* Revert to the main lexer. */
29682 cp_parser_pop_lexer (parser);
29683
29684 return parsed_arg;
29685 }
29686
29687 /* FIELD is a non-static data member with an initializer which we saved for
29688 later; parse it now. */
29689
29690 static void
29691 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
29692 {
29693 tree def;
29694
29695 maybe_begin_member_template_processing (field);
29696
29697 push_unparsed_function_queues (parser);
29698 def = cp_parser_late_parse_one_default_arg (parser, field,
29699 DECL_INITIAL (field),
29700 NULL_TREE);
29701 pop_unparsed_function_queues (parser);
29702
29703 maybe_end_member_template_processing ();
29704
29705 DECL_INITIAL (field) = def;
29706 }
29707
29708 /* FN is a FUNCTION_DECL which may contains a parameter with an
29709 unparsed DEFERRED_PARSE. Parse the default args now. This function
29710 assumes that the current scope is the scope in which the default
29711 argument should be processed. */
29712
29713 static void
29714 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
29715 {
29716 unsigned char saved_local_variables_forbidden_p;
29717 tree parm, parmdecl;
29718
29719 /* While we're parsing the default args, we might (due to the
29720 statement expression extension) encounter more classes. We want
29721 to handle them right away, but we don't want them getting mixed
29722 up with default args that are currently in the queue. */
29723 push_unparsed_function_queues (parser);
29724
29725 /* Local variable names (and the `this' keyword) may not appear
29726 in a default argument. */
29727 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
29728 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
29729
29730 push_defarg_context (fn);
29731
29732 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
29733 parmdecl = DECL_ARGUMENTS (fn);
29734 parm && parm != void_list_node;
29735 parm = TREE_CHAIN (parm),
29736 parmdecl = DECL_CHAIN (parmdecl))
29737 {
29738 tree default_arg = TREE_PURPOSE (parm);
29739 tree parsed_arg;
29740 vec<tree, va_gc> *insts;
29741 tree copy;
29742 unsigned ix;
29743
29744 if (!default_arg)
29745 continue;
29746
29747 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
29748 /* This can happen for a friend declaration for a function
29749 already declared with default arguments. */
29750 continue;
29751
29752 parsed_arg
29753 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
29754 default_arg,
29755 TREE_VALUE (parm));
29756 TREE_PURPOSE (parm) = parsed_arg;
29757
29758 /* Update any instantiations we've already created. */
29759 for (insts = DEFPARSE_INSTANTIATIONS (default_arg), ix = 0;
29760 vec_safe_iterate (insts, ix, &copy); ix++)
29761 TREE_PURPOSE (copy) = parsed_arg;
29762 }
29763
29764 pop_defarg_context ();
29765
29766 /* Make sure no default arg is missing. */
29767 check_default_args (fn);
29768
29769 /* Restore the state of local_variables_forbidden_p. */
29770 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
29771
29772 /* Restore the queue. */
29773 pop_unparsed_function_queues (parser);
29774 }
29775
29776 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
29777
29778 sizeof ... ( identifier )
29779
29780 where the 'sizeof' token has already been consumed. */
29781
29782 static tree
29783 cp_parser_sizeof_pack (cp_parser *parser)
29784 {
29785 /* Consume the `...'. */
29786 cp_lexer_consume_token (parser->lexer);
29787 maybe_warn_variadic_templates ();
29788
29789 matching_parens parens;
29790 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
29791 if (paren)
29792 parens.consume_open (parser);
29793 else
29794 permerror (cp_lexer_peek_token (parser->lexer)->location,
29795 "%<sizeof...%> argument must be surrounded by parentheses");
29796
29797 cp_token *token = cp_lexer_peek_token (parser->lexer);
29798 tree name = cp_parser_identifier (parser);
29799 if (name == error_mark_node)
29800 return error_mark_node;
29801 /* The name is not qualified. */
29802 parser->scope = NULL_TREE;
29803 parser->qualifying_scope = NULL_TREE;
29804 parser->object_scope = NULL_TREE;
29805 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
29806 if (expr == error_mark_node)
29807 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
29808 token->location);
29809 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
29810 expr = TREE_TYPE (expr);
29811 else if (TREE_CODE (expr) == CONST_DECL)
29812 expr = DECL_INITIAL (expr);
29813 expr = make_pack_expansion (expr);
29814 PACK_EXPANSION_SIZEOF_P (expr) = true;
29815
29816 if (paren)
29817 parens.require_close (parser);
29818
29819 return expr;
29820 }
29821
29822 /* Parse the operand of `sizeof' (or a similar operator). Returns
29823 either a TYPE or an expression, depending on the form of the
29824 input. The KEYWORD indicates which kind of expression we have
29825 encountered. */
29826
29827 static tree
29828 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
29829 {
29830 tree expr = NULL_TREE;
29831 const char *saved_message;
29832 const char *saved_message_arg;
29833 bool saved_integral_constant_expression_p;
29834 bool saved_non_integral_constant_expression_p;
29835
29836 /* If it's a `...', then we are computing the length of a parameter
29837 pack. */
29838 if (keyword == RID_SIZEOF
29839 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29840 return cp_parser_sizeof_pack (parser);
29841
29842 /* Types cannot be defined in a `sizeof' expression. Save away the
29843 old message. */
29844 saved_message = parser->type_definition_forbidden_message;
29845 saved_message_arg = parser->type_definition_forbidden_message_arg;
29846 parser->type_definition_forbidden_message
29847 = G_("types may not be defined in %qs expressions");
29848 parser->type_definition_forbidden_message_arg
29849 = IDENTIFIER_POINTER (ridpointers[keyword]);
29850
29851 /* The restrictions on constant-expressions do not apply inside
29852 sizeof expressions. */
29853 saved_integral_constant_expression_p
29854 = parser->integral_constant_expression_p;
29855 saved_non_integral_constant_expression_p
29856 = parser->non_integral_constant_expression_p;
29857 parser->integral_constant_expression_p = false;
29858
29859 /* Do not actually evaluate the expression. */
29860 ++cp_unevaluated_operand;
29861 ++c_inhibit_evaluation_warnings;
29862 /* If it's a `(', then we might be looking at the type-id
29863 construction. */
29864 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29865 {
29866 tree type = NULL_TREE;
29867
29868 tentative_firewall firewall (parser);
29869
29870 /* We can't be sure yet whether we're looking at a type-id or an
29871 expression. */
29872 cp_parser_parse_tentatively (parser);
29873
29874 matching_parens parens;
29875 parens.consume_open (parser);
29876
29877 /* Note: as a GNU Extension, compound literals are considered
29878 postfix-expressions as they are in C99, so they are valid
29879 arguments to sizeof. See comment in cp_parser_cast_expression
29880 for details. */
29881 if (cp_parser_compound_literal_p (parser))
29882 cp_parser_simulate_error (parser);
29883 else
29884 {
29885 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
29886 parser->in_type_id_in_expr_p = true;
29887 /* Look for the type-id. */
29888 type = cp_parser_type_id (parser);
29889 /* Look for the closing `)'. */
29890 parens.require_close (parser);
29891 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
29892 }
29893
29894 /* If all went well, then we're done. */
29895 if (cp_parser_parse_definitely (parser))
29896 expr = type;
29897 else
29898 {
29899 /* Commit to the tentative_firewall so we get syntax errors. */
29900 cp_parser_commit_to_tentative_parse (parser);
29901
29902 expr = cp_parser_unary_expression (parser);
29903 }
29904 }
29905 else
29906 expr = cp_parser_unary_expression (parser);
29907
29908 /* Go back to evaluating expressions. */
29909 --cp_unevaluated_operand;
29910 --c_inhibit_evaluation_warnings;
29911
29912 /* And restore the old one. */
29913 parser->type_definition_forbidden_message = saved_message;
29914 parser->type_definition_forbidden_message_arg = saved_message_arg;
29915 parser->integral_constant_expression_p
29916 = saved_integral_constant_expression_p;
29917 parser->non_integral_constant_expression_p
29918 = saved_non_integral_constant_expression_p;
29919
29920 return expr;
29921 }
29922
29923 /* If the current declaration has no declarator, return true. */
29924
29925 static bool
29926 cp_parser_declares_only_class_p (cp_parser *parser)
29927 {
29928 /* If the next token is a `;' or a `,' then there is no
29929 declarator. */
29930 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29931 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
29932 }
29933
29934 /* Update the DECL_SPECS to reflect the storage class indicated by
29935 KEYWORD. */
29936
29937 static void
29938 cp_parser_set_storage_class (cp_parser *parser,
29939 cp_decl_specifier_seq *decl_specs,
29940 enum rid keyword,
29941 cp_token *token)
29942 {
29943 cp_storage_class storage_class;
29944
29945 if (parser->in_unbraced_linkage_specification_p)
29946 {
29947 error_at (token->location, "invalid use of %qD in linkage specification",
29948 ridpointers[keyword]);
29949 return;
29950 }
29951 else if (decl_specs->storage_class != sc_none)
29952 {
29953 decl_specs->conflicting_specifiers_p = true;
29954 return;
29955 }
29956
29957 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
29958 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
29959 && decl_specs->gnu_thread_keyword_p)
29960 {
29961 pedwarn (decl_specs->locations[ds_thread], 0,
29962 "%<__thread%> before %qD", ridpointers[keyword]);
29963 }
29964
29965 switch (keyword)
29966 {
29967 case RID_AUTO:
29968 storage_class = sc_auto;
29969 break;
29970 case RID_REGISTER:
29971 storage_class = sc_register;
29972 break;
29973 case RID_STATIC:
29974 storage_class = sc_static;
29975 break;
29976 case RID_EXTERN:
29977 storage_class = sc_extern;
29978 break;
29979 case RID_MUTABLE:
29980 storage_class = sc_mutable;
29981 break;
29982 default:
29983 gcc_unreachable ();
29984 }
29985 decl_specs->storage_class = storage_class;
29986 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
29987
29988 /* A storage class specifier cannot be applied alongside a typedef
29989 specifier. If there is a typedef specifier present then set
29990 conflicting_specifiers_p which will trigger an error later
29991 on in grokdeclarator. */
29992 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
29993 decl_specs->conflicting_specifiers_p = true;
29994 }
29995
29996 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
29997 is true, the type is a class or enum definition. */
29998
29999 static void
30000 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
30001 tree type_spec,
30002 cp_token *token,
30003 bool type_definition_p)
30004 {
30005 decl_specs->any_specifiers_p = true;
30006
30007 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
30008 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
30009 this is what happened. In system headers, we ignore these
30010 declarations so that G++ can work with system headers that are not
30011 C++-safe. */
30012 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
30013 && !type_definition_p
30014 && (type_spec == boolean_type_node
30015 || type_spec == char8_type_node
30016 || type_spec == char16_type_node
30017 || type_spec == char32_type_node
30018 || type_spec == wchar_type_node)
30019 && (decl_specs->type
30020 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
30021 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
30022 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
30023 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
30024 {
30025 decl_specs->redefined_builtin_type = type_spec;
30026 set_and_check_decl_spec_loc (decl_specs,
30027 ds_redefined_builtin_type_spec,
30028 token);
30029 if (!decl_specs->type)
30030 {
30031 decl_specs->type = type_spec;
30032 decl_specs->type_definition_p = false;
30033 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
30034 }
30035 }
30036 else if (decl_specs->type)
30037 decl_specs->multiple_types_p = true;
30038 else
30039 {
30040 decl_specs->type = type_spec;
30041 decl_specs->type_definition_p = type_definition_p;
30042 decl_specs->redefined_builtin_type = NULL_TREE;
30043 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
30044 }
30045 }
30046
30047 /* True iff TOKEN is the GNU keyword __thread. */
30048
30049 static bool
30050 token_is__thread (cp_token *token)
30051 {
30052 gcc_assert (token->keyword == RID_THREAD);
30053 return id_equal (token->u.value, "__thread");
30054 }
30055
30056 /* Set the location for a declarator specifier and check if it is
30057 duplicated.
30058
30059 DECL_SPECS is the sequence of declarator specifiers onto which to
30060 set the location.
30061
30062 DS is the single declarator specifier to set which location is to
30063 be set onto the existing sequence of declarators.
30064
30065 LOCATION is the location for the declarator specifier to
30066 consider. */
30067
30068 static void
30069 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
30070 cp_decl_spec ds, cp_token *token)
30071 {
30072 gcc_assert (ds < ds_last);
30073
30074 if (decl_specs == NULL)
30075 return;
30076
30077 location_t location = token->location;
30078
30079 if (decl_specs->locations[ds] == 0)
30080 {
30081 decl_specs->locations[ds] = location;
30082 if (ds == ds_thread)
30083 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
30084 }
30085 else
30086 {
30087 if (ds == ds_long)
30088 {
30089 if (decl_specs->locations[ds_long_long] != 0)
30090 error_at (location,
30091 "%<long long long%> is too long for GCC");
30092 else
30093 {
30094 decl_specs->locations[ds_long_long] = location;
30095 pedwarn_cxx98 (location,
30096 OPT_Wlong_long,
30097 "ISO C++ 1998 does not support %<long long%>");
30098 }
30099 }
30100 else if (ds == ds_thread)
30101 {
30102 bool gnu = token_is__thread (token);
30103 gcc_rich_location richloc (location);
30104 if (gnu != decl_specs->gnu_thread_keyword_p)
30105 {
30106 richloc.add_range (decl_specs->locations[ds_thread]);
30107 error_at (&richloc,
30108 "both %<__thread%> and %<thread_local%> specified");
30109 }
30110 else
30111 {
30112 richloc.add_fixit_remove ();
30113 error_at (&richloc, "duplicate %qD", token->u.value);
30114 }
30115 }
30116 else
30117 {
30118 static const char *const decl_spec_names[] = {
30119 "signed",
30120 "unsigned",
30121 "short",
30122 "long",
30123 "const",
30124 "volatile",
30125 "restrict",
30126 "inline",
30127 "virtual",
30128 "explicit",
30129 "friend",
30130 "typedef",
30131 "using",
30132 "constexpr",
30133 "__complex",
30134 "constinit",
30135 "consteval"
30136 };
30137 gcc_rich_location richloc (location);
30138 richloc.add_fixit_remove ();
30139 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
30140 }
30141 }
30142 }
30143
30144 /* Return true iff the declarator specifier DS is present in the
30145 sequence of declarator specifiers DECL_SPECS. */
30146
30147 bool
30148 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
30149 cp_decl_spec ds)
30150 {
30151 gcc_assert (ds < ds_last);
30152
30153 if (decl_specs == NULL)
30154 return false;
30155
30156 return decl_specs->locations[ds] != 0;
30157 }
30158
30159 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
30160 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
30161
30162 static bool
30163 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
30164 {
30165 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
30166 }
30167
30168 /* Issue an error message indicating that TOKEN_DESC was expected.
30169 If KEYWORD is true, it indicated this function is called by
30170 cp_parser_require_keword and the required token can only be
30171 a indicated keyword.
30172
30173 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
30174 within any error as the location of an "opening" token matching
30175 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
30176 RT_CLOSE_PAREN). */
30177
30178 static void
30179 cp_parser_required_error (cp_parser *parser,
30180 required_token token_desc,
30181 bool keyword,
30182 location_t matching_location)
30183 {
30184 if (cp_parser_simulate_error (parser))
30185 return;
30186
30187 const char *gmsgid = NULL;
30188 switch (token_desc)
30189 {
30190 case RT_NEW:
30191 gmsgid = G_("expected %<new%>");
30192 break;
30193 case RT_DELETE:
30194 gmsgid = G_("expected %<delete%>");
30195 break;
30196 case RT_RETURN:
30197 gmsgid = G_("expected %<return%>");
30198 break;
30199 case RT_WHILE:
30200 gmsgid = G_("expected %<while%>");
30201 break;
30202 case RT_EXTERN:
30203 gmsgid = G_("expected %<extern%>");
30204 break;
30205 case RT_STATIC_ASSERT:
30206 gmsgid = G_("expected %<static_assert%>");
30207 break;
30208 case RT_DECLTYPE:
30209 gmsgid = G_("expected %<decltype%>");
30210 break;
30211 case RT_OPERATOR:
30212 gmsgid = G_("expected %<operator%>");
30213 break;
30214 case RT_CLASS:
30215 gmsgid = G_("expected %<class%>");
30216 break;
30217 case RT_TEMPLATE:
30218 gmsgid = G_("expected %<template%>");
30219 break;
30220 case RT_NAMESPACE:
30221 gmsgid = G_("expected %<namespace%>");
30222 break;
30223 case RT_USING:
30224 gmsgid = G_("expected %<using%>");
30225 break;
30226 case RT_ASM:
30227 gmsgid = G_("expected %<asm%>");
30228 break;
30229 case RT_TRY:
30230 gmsgid = G_("expected %<try%>");
30231 break;
30232 case RT_CATCH:
30233 gmsgid = G_("expected %<catch%>");
30234 break;
30235 case RT_THROW:
30236 gmsgid = G_("expected %<throw%>");
30237 break;
30238 case RT_AUTO:
30239 gmsgid = G_("expected %<auto%>");
30240 break;
30241 case RT_LABEL:
30242 gmsgid = G_("expected %<__label__%>");
30243 break;
30244 case RT_AT_TRY:
30245 gmsgid = G_("expected %<@try%>");
30246 break;
30247 case RT_AT_SYNCHRONIZED:
30248 gmsgid = G_("expected %<@synchronized%>");
30249 break;
30250 case RT_AT_THROW:
30251 gmsgid = G_("expected %<@throw%>");
30252 break;
30253 case RT_TRANSACTION_ATOMIC:
30254 gmsgid = G_("expected %<__transaction_atomic%>");
30255 break;
30256 case RT_TRANSACTION_RELAXED:
30257 gmsgid = G_("expected %<__transaction_relaxed%>");
30258 break;
30259 default:
30260 break;
30261 }
30262
30263 if (!gmsgid && !keyword)
30264 {
30265 switch (token_desc)
30266 {
30267 case RT_SEMICOLON:
30268 gmsgid = G_("expected %<;%>");
30269 break;
30270 case RT_OPEN_PAREN:
30271 gmsgid = G_("expected %<(%>");
30272 break;
30273 case RT_CLOSE_BRACE:
30274 gmsgid = G_("expected %<}%>");
30275 break;
30276 case RT_OPEN_BRACE:
30277 gmsgid = G_("expected %<{%>");
30278 break;
30279 case RT_CLOSE_SQUARE:
30280 gmsgid = G_("expected %<]%>");
30281 break;
30282 case RT_OPEN_SQUARE:
30283 gmsgid = G_("expected %<[%>");
30284 break;
30285 case RT_COMMA:
30286 gmsgid = G_("expected %<,%>");
30287 break;
30288 case RT_SCOPE:
30289 gmsgid = G_("expected %<::%>");
30290 break;
30291 case RT_LESS:
30292 gmsgid = G_("expected %<<%>");
30293 break;
30294 case RT_GREATER:
30295 gmsgid = G_("expected %<>%>");
30296 break;
30297 case RT_EQ:
30298 gmsgid = G_("expected %<=%>");
30299 break;
30300 case RT_ELLIPSIS:
30301 gmsgid = G_("expected %<...%>");
30302 break;
30303 case RT_MULT:
30304 gmsgid = G_("expected %<*%>");
30305 break;
30306 case RT_COMPL:
30307 gmsgid = G_("expected %<~%>");
30308 break;
30309 case RT_COLON:
30310 gmsgid = G_("expected %<:%>");
30311 break;
30312 case RT_COLON_SCOPE:
30313 gmsgid = G_("expected %<:%> or %<::%>");
30314 break;
30315 case RT_CLOSE_PAREN:
30316 gmsgid = G_("expected %<)%>");
30317 break;
30318 case RT_COMMA_CLOSE_PAREN:
30319 gmsgid = G_("expected %<,%> or %<)%>");
30320 break;
30321 case RT_PRAGMA_EOL:
30322 gmsgid = G_("expected end of line");
30323 break;
30324 case RT_NAME:
30325 gmsgid = G_("expected identifier");
30326 break;
30327 case RT_SELECT:
30328 gmsgid = G_("expected selection-statement");
30329 break;
30330 case RT_ITERATION:
30331 gmsgid = G_("expected iteration-statement");
30332 break;
30333 case RT_JUMP:
30334 gmsgid = G_("expected jump-statement");
30335 break;
30336 case RT_CLASS_KEY:
30337 gmsgid = G_("expected class-key");
30338 break;
30339 case RT_CLASS_TYPENAME_TEMPLATE:
30340 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
30341 break;
30342 default:
30343 gcc_unreachable ();
30344 }
30345 }
30346
30347 if (gmsgid)
30348 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
30349 }
30350
30351
30352 /* If the next token is of the indicated TYPE, consume it. Otherwise,
30353 issue an error message indicating that TOKEN_DESC was expected.
30354
30355 Returns the token consumed, if the token had the appropriate type.
30356 Otherwise, returns NULL.
30357
30358 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
30359 within any error as the location of an "opening" token matching
30360 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
30361 RT_CLOSE_PAREN). */
30362
30363 static cp_token *
30364 cp_parser_require (cp_parser* parser,
30365 enum cpp_ttype type,
30366 required_token token_desc,
30367 location_t matching_location)
30368 {
30369 if (cp_lexer_next_token_is (parser->lexer, type))
30370 return cp_lexer_consume_token (parser->lexer);
30371 else
30372 {
30373 /* Output the MESSAGE -- unless we're parsing tentatively. */
30374 if (!cp_parser_simulate_error (parser))
30375 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
30376 matching_location);
30377 return NULL;
30378 }
30379 }
30380
30381 /* An error message is produced if the next token is not '>'.
30382 All further tokens are skipped until the desired token is
30383 found or '{', '}', ';' or an unbalanced ')' or ']'. */
30384
30385 static void
30386 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
30387 {
30388 /* Current level of '< ... >'. */
30389 unsigned level = 0;
30390 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
30391 unsigned nesting_depth = 0;
30392
30393 /* Are we ready, yet? If not, issue error message. */
30394 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
30395 return;
30396
30397 /* Skip tokens until the desired token is found. */
30398 while (true)
30399 {
30400 /* Peek at the next token. */
30401 switch (cp_lexer_peek_token (parser->lexer)->type)
30402 {
30403 case CPP_LESS:
30404 if (!nesting_depth)
30405 ++level;
30406 break;
30407
30408 case CPP_RSHIFT:
30409 if (cxx_dialect == cxx98)
30410 /* C++0x views the `>>' operator as two `>' tokens, but
30411 C++98 does not. */
30412 break;
30413 else if (!nesting_depth && level-- == 0)
30414 {
30415 /* We've hit a `>>' where the first `>' closes the
30416 template argument list, and the second `>' is
30417 spurious. Just consume the `>>' and stop; we've
30418 already produced at least one error. */
30419 cp_lexer_consume_token (parser->lexer);
30420 return;
30421 }
30422 /* Fall through for C++0x, so we handle the second `>' in
30423 the `>>'. */
30424 gcc_fallthrough ();
30425
30426 case CPP_GREATER:
30427 if (!nesting_depth && level-- == 0)
30428 {
30429 /* We've reached the token we want, consume it and stop. */
30430 cp_lexer_consume_token (parser->lexer);
30431 return;
30432 }
30433 break;
30434
30435 case CPP_OPEN_PAREN:
30436 case CPP_OPEN_SQUARE:
30437 ++nesting_depth;
30438 break;
30439
30440 case CPP_CLOSE_PAREN:
30441 case CPP_CLOSE_SQUARE:
30442 if (nesting_depth-- == 0)
30443 return;
30444 break;
30445
30446 case CPP_EOF:
30447 case CPP_PRAGMA_EOL:
30448 case CPP_SEMICOLON:
30449 case CPP_OPEN_BRACE:
30450 case CPP_CLOSE_BRACE:
30451 /* The '>' was probably forgotten, don't look further. */
30452 return;
30453
30454 default:
30455 break;
30456 }
30457
30458 /* Consume this token. */
30459 cp_lexer_consume_token (parser->lexer);
30460 }
30461 }
30462
30463 /* If the next token is the indicated keyword, consume it. Otherwise,
30464 issue an error message indicating that TOKEN_DESC was expected.
30465
30466 Returns the token consumed, if the token had the appropriate type.
30467 Otherwise, returns NULL. */
30468
30469 static cp_token *
30470 cp_parser_require_keyword (cp_parser* parser,
30471 enum rid keyword,
30472 required_token token_desc)
30473 {
30474 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
30475
30476 if (token && token->keyword != keyword)
30477 {
30478 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
30479 UNKNOWN_LOCATION);
30480 return NULL;
30481 }
30482
30483 return token;
30484 }
30485
30486 /* Returns TRUE iff TOKEN is a token that can begin the body of a
30487 function-definition. */
30488
30489 static bool
30490 cp_parser_token_starts_function_definition_p (cp_token* token)
30491 {
30492 return (/* An ordinary function-body begins with an `{'. */
30493 token->type == CPP_OPEN_BRACE
30494 /* A ctor-initializer begins with a `:'. */
30495 || token->type == CPP_COLON
30496 /* A function-try-block begins with `try'. */
30497 || token->keyword == RID_TRY
30498 /* A function-transaction-block begins with `__transaction_atomic'
30499 or `__transaction_relaxed'. */
30500 || token->keyword == RID_TRANSACTION_ATOMIC
30501 || token->keyword == RID_TRANSACTION_RELAXED
30502 /* The named return value extension begins with `return'. */
30503 || token->keyword == RID_RETURN);
30504 }
30505
30506 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
30507 definition. */
30508
30509 static bool
30510 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
30511 {
30512 cp_token *token;
30513
30514 token = cp_lexer_peek_token (parser->lexer);
30515 return (token->type == CPP_OPEN_BRACE
30516 || (token->type == CPP_COLON
30517 && !parser->colon_doesnt_start_class_def_p));
30518 }
30519
30520 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
30521 C++0x) ending a template-argument. */
30522
30523 static bool
30524 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
30525 {
30526 cp_token *token;
30527
30528 token = cp_lexer_peek_token (parser->lexer);
30529 return (token->type == CPP_COMMA
30530 || token->type == CPP_GREATER
30531 || token->type == CPP_ELLIPSIS
30532 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
30533 }
30534
30535 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
30536 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
30537
30538 static bool
30539 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
30540 size_t n)
30541 {
30542 cp_token *token;
30543
30544 token = cp_lexer_peek_nth_token (parser->lexer, n);
30545 if (token->type == CPP_LESS)
30546 return true;
30547 /* Check for the sequence `<::' in the original code. It would be lexed as
30548 `[:', where `[' is a digraph, and there is no whitespace before
30549 `:'. */
30550 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
30551 {
30552 cp_token *token2;
30553 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
30554 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
30555 return true;
30556 }
30557 return false;
30558 }
30559
30560 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
30561 or none_type otherwise. */
30562
30563 static enum tag_types
30564 cp_parser_token_is_class_key (cp_token* token)
30565 {
30566 switch (token->keyword)
30567 {
30568 case RID_CLASS:
30569 return class_type;
30570 case RID_STRUCT:
30571 return record_type;
30572 case RID_UNION:
30573 return union_type;
30574
30575 default:
30576 return none_type;
30577 }
30578 }
30579
30580 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
30581 or none_type otherwise or if the token is null. */
30582
30583 static enum tag_types
30584 cp_parser_token_is_type_parameter_key (cp_token* token)
30585 {
30586 if (!token)
30587 return none_type;
30588
30589 switch (token->keyword)
30590 {
30591 case RID_CLASS:
30592 return class_type;
30593 case RID_TYPENAME:
30594 return typename_type;
30595
30596 default:
30597 return none_type;
30598 }
30599 }
30600
30601 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
30602
30603 static void
30604 cp_parser_check_class_key (enum tag_types class_key, tree type)
30605 {
30606 if (type == error_mark_node)
30607 return;
30608 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
30609 {
30610 if (permerror (input_location, "%qs tag used in naming %q#T",
30611 class_key == union_type ? "union"
30612 : class_key == record_type ? "struct" : "class",
30613 type))
30614 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
30615 "%q#T was previously declared here", type);
30616 }
30617 }
30618
30619 /* Issue an error message if DECL is redeclared with different
30620 access than its original declaration [class.access.spec/3].
30621 This applies to nested classes, nested class templates and
30622 enumerations [class.mem/1]. */
30623
30624 static void
30625 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
30626 {
30627 if (!decl
30628 || (!CLASS_TYPE_P (TREE_TYPE (decl))
30629 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
30630 return;
30631
30632 if ((TREE_PRIVATE (decl)
30633 != (current_access_specifier == access_private_node))
30634 || (TREE_PROTECTED (decl)
30635 != (current_access_specifier == access_protected_node)))
30636 error_at (location, "%qD redeclared with different access", decl);
30637 }
30638
30639 /* Look for the `template' keyword, as a syntactic disambiguator.
30640 Return TRUE iff it is present, in which case it will be
30641 consumed. */
30642
30643 static bool
30644 cp_parser_optional_template_keyword (cp_parser *parser)
30645 {
30646 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
30647 {
30648 /* In C++98 the `template' keyword can only be used within templates;
30649 outside templates the parser can always figure out what is a
30650 template and what is not. In C++11, per the resolution of DR 468,
30651 `template' is allowed in cases where it is not strictly necessary. */
30652 if (!processing_template_decl
30653 && pedantic && cxx_dialect == cxx98)
30654 {
30655 cp_token *token = cp_lexer_peek_token (parser->lexer);
30656 pedwarn (token->location, OPT_Wpedantic,
30657 "in C++98 %<template%> (as a disambiguator) is only "
30658 "allowed within templates");
30659 /* If this part of the token stream is rescanned, the same
30660 error message would be generated. So, we purge the token
30661 from the stream. */
30662 cp_lexer_purge_token (parser->lexer);
30663 return false;
30664 }
30665 else
30666 {
30667 /* Consume the `template' keyword. */
30668 cp_lexer_consume_token (parser->lexer);
30669 return true;
30670 }
30671 }
30672 return false;
30673 }
30674
30675 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
30676 set PARSER->SCOPE, and perform other related actions. */
30677
30678 static void
30679 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
30680 {
30681 struct tree_check *check_value;
30682
30683 /* Get the stored value. */
30684 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
30685 /* Set the scope from the stored value. */
30686 parser->scope = saved_checks_value (check_value);
30687 parser->qualifying_scope = check_value->qualifying_scope;
30688 parser->object_scope = NULL_TREE;
30689 }
30690
30691 /* Consume tokens up through a non-nested END token. Returns TRUE if we
30692 encounter the end of a block before what we were looking for. */
30693
30694 static bool
30695 cp_parser_cache_group (cp_parser *parser,
30696 enum cpp_ttype end,
30697 unsigned depth)
30698 {
30699 while (true)
30700 {
30701 cp_token *token = cp_lexer_peek_token (parser->lexer);
30702
30703 /* Abort a parenthesized expression if we encounter a semicolon. */
30704 if ((end == CPP_CLOSE_PAREN || depth == 0)
30705 && token->type == CPP_SEMICOLON)
30706 return true;
30707 /* If we've reached the end of the file, stop. */
30708 if (token->type == CPP_EOF
30709 || (end != CPP_PRAGMA_EOL
30710 && token->type == CPP_PRAGMA_EOL))
30711 return true;
30712 if (token->type == CPP_CLOSE_BRACE && depth == 0)
30713 /* We've hit the end of an enclosing block, so there's been some
30714 kind of syntax error. */
30715 return true;
30716
30717 /* Consume the token. */
30718 cp_lexer_consume_token (parser->lexer);
30719 /* See if it starts a new group. */
30720 if (token->type == CPP_OPEN_BRACE)
30721 {
30722 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
30723 /* In theory this should probably check end == '}', but
30724 cp_parser_save_member_function_body needs it to exit
30725 after either '}' or ')' when called with ')'. */
30726 if (depth == 0)
30727 return false;
30728 }
30729 else if (token->type == CPP_OPEN_PAREN)
30730 {
30731 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
30732 if (depth == 0 && end == CPP_CLOSE_PAREN)
30733 return false;
30734 }
30735 else if (token->type == CPP_PRAGMA)
30736 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
30737 else if (token->type == end)
30738 return false;
30739 }
30740 }
30741
30742 /* Like above, for caching a default argument or NSDMI. Both of these are
30743 terminated by a non-nested comma, but it can be unclear whether or not a
30744 comma is nested in a template argument list unless we do more parsing.
30745 In order to handle this ambiguity, when we encounter a ',' after a '<'
30746 we try to parse what follows as a parameter-declaration-list (in the
30747 case of a default argument) or a member-declarator (in the case of an
30748 NSDMI). If that succeeds, then we stop caching. */
30749
30750 static tree
30751 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
30752 {
30753 unsigned depth = 0;
30754 int maybe_template_id = 0;
30755 cp_token *first_token;
30756 cp_token *token;
30757 tree default_argument;
30758
30759 /* Add tokens until we have processed the entire default
30760 argument. We add the range [first_token, token). */
30761 first_token = cp_lexer_peek_token (parser->lexer);
30762 if (first_token->type == CPP_OPEN_BRACE)
30763 {
30764 /* For list-initialization, this is straightforward. */
30765 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
30766 token = cp_lexer_peek_token (parser->lexer);
30767 }
30768 else while (true)
30769 {
30770 bool done = false;
30771
30772 /* Peek at the next token. */
30773 token = cp_lexer_peek_token (parser->lexer);
30774 /* What we do depends on what token we have. */
30775 switch (token->type)
30776 {
30777 /* In valid code, a default argument must be
30778 immediately followed by a `,' `)', or `...'. */
30779 case CPP_COMMA:
30780 if (depth == 0 && maybe_template_id)
30781 {
30782 /* If we've seen a '<', we might be in a
30783 template-argument-list. Until Core issue 325 is
30784 resolved, we don't know how this situation ought
30785 to be handled, so try to DTRT. We check whether
30786 what comes after the comma is a valid parameter
30787 declaration list. If it is, then the comma ends
30788 the default argument; otherwise the default
30789 argument continues. */
30790 bool error = false;
30791 cp_token *peek;
30792
30793 /* Set ITALP so cp_parser_parameter_declaration_list
30794 doesn't decide to commit to this parse. */
30795 bool saved_italp = parser->in_template_argument_list_p;
30796 parser->in_template_argument_list_p = true;
30797
30798 cp_parser_parse_tentatively (parser);
30799
30800 if (nsdmi)
30801 {
30802 /* Parse declarators until we reach a non-comma or
30803 somthing that cannot be an initializer.
30804 Just checking whether we're looking at a single
30805 declarator is insufficient. Consider:
30806 int var = tuple<T,U>::x;
30807 The template parameter 'U' looks exactly like a
30808 declarator. */
30809 do
30810 {
30811 int ctor_dtor_or_conv_p;
30812 cp_lexer_consume_token (parser->lexer);
30813 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30814 CP_PARSER_FLAGS_NONE,
30815 &ctor_dtor_or_conv_p,
30816 /*parenthesized_p=*/NULL,
30817 /*member_p=*/true,
30818 /*friend_p=*/false,
30819 /*static_p=*/false);
30820 peek = cp_lexer_peek_token (parser->lexer);
30821 if (cp_parser_error_occurred (parser))
30822 break;
30823 }
30824 while (peek->type == CPP_COMMA);
30825 /* If we met an '=' or ';' then the original comma
30826 was the end of the NSDMI. Otherwise assume
30827 we're still in the NSDMI. */
30828 error = (peek->type != CPP_EQ
30829 && peek->type != CPP_SEMICOLON);
30830 }
30831 else
30832 {
30833 cp_lexer_consume_token (parser->lexer);
30834 begin_scope (sk_function_parms, NULL_TREE);
30835 tree t = cp_parser_parameter_declaration_list
30836 (parser, CP_PARSER_FLAGS_NONE);
30837 if (t == error_mark_node)
30838 error = true;
30839 pop_bindings_and_leave_scope ();
30840 }
30841 if (!cp_parser_error_occurred (parser) && !error)
30842 done = true;
30843 cp_parser_abort_tentative_parse (parser);
30844
30845 parser->in_template_argument_list_p = saved_italp;
30846 break;
30847 }
30848 /* FALLTHRU */
30849 case CPP_CLOSE_PAREN:
30850 case CPP_ELLIPSIS:
30851 /* If we run into a non-nested `;', `}', or `]',
30852 then the code is invalid -- but the default
30853 argument is certainly over. */
30854 case CPP_SEMICOLON:
30855 case CPP_CLOSE_BRACE:
30856 case CPP_CLOSE_SQUARE:
30857 if (depth == 0
30858 /* Handle correctly int n = sizeof ... ( p ); */
30859 && token->type != CPP_ELLIPSIS)
30860 done = true;
30861 /* Update DEPTH, if necessary. */
30862 else if (token->type == CPP_CLOSE_PAREN
30863 || token->type == CPP_CLOSE_BRACE
30864 || token->type == CPP_CLOSE_SQUARE)
30865 --depth;
30866 break;
30867
30868 case CPP_OPEN_PAREN:
30869 case CPP_OPEN_SQUARE:
30870 case CPP_OPEN_BRACE:
30871 ++depth;
30872 break;
30873
30874 case CPP_LESS:
30875 if (depth == 0)
30876 /* This might be the comparison operator, or it might
30877 start a template argument list. */
30878 ++maybe_template_id;
30879 break;
30880
30881 case CPP_RSHIFT:
30882 if (cxx_dialect == cxx98)
30883 break;
30884 /* Fall through for C++0x, which treats the `>>'
30885 operator like two `>' tokens in certain
30886 cases. */
30887 gcc_fallthrough ();
30888
30889 case CPP_GREATER:
30890 if (depth == 0)
30891 {
30892 /* This might be an operator, or it might close a
30893 template argument list. But if a previous '<'
30894 started a template argument list, this will have
30895 closed it, so we can't be in one anymore. */
30896 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
30897 if (maybe_template_id < 0)
30898 maybe_template_id = 0;
30899 }
30900 break;
30901
30902 /* If we run out of tokens, issue an error message. */
30903 case CPP_EOF:
30904 case CPP_PRAGMA_EOL:
30905 error_at (token->location, "file ends in default argument");
30906 return error_mark_node;
30907
30908 case CPP_NAME:
30909 case CPP_SCOPE:
30910 /* In these cases, we should look for template-ids.
30911 For example, if the default argument is
30912 `X<int, double>()', we need to do name lookup to
30913 figure out whether or not `X' is a template; if
30914 so, the `,' does not end the default argument.
30915
30916 That is not yet done. */
30917 break;
30918
30919 default:
30920 break;
30921 }
30922
30923 /* If we've reached the end, stop. */
30924 if (done)
30925 break;
30926
30927 /* Add the token to the token block. */
30928 token = cp_lexer_consume_token (parser->lexer);
30929 }
30930
30931 /* Create a DEFERRED_PARSE to represent the unparsed default
30932 argument. */
30933 default_argument = make_node (DEFERRED_PARSE);
30934 DEFPARSE_TOKENS (default_argument)
30935 = cp_token_cache_new (first_token, token);
30936 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
30937
30938 return default_argument;
30939 }
30940
30941 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
30942
30943 location_t
30944 defparse_location (tree default_argument)
30945 {
30946 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
30947 location_t start = tokens->first->location;
30948 location_t end = tokens->last->location;
30949 return make_location (start, start, end);
30950 }
30951
30952 /* Begin parsing tentatively. We always save tokens while parsing
30953 tentatively so that if the tentative parsing fails we can restore the
30954 tokens. */
30955
30956 static void
30957 cp_parser_parse_tentatively (cp_parser* parser)
30958 {
30959 /* Enter a new parsing context. */
30960 parser->context = cp_parser_context_new (parser->context);
30961 /* Begin saving tokens. */
30962 cp_lexer_save_tokens (parser->lexer);
30963 /* In order to avoid repetitive access control error messages,
30964 access checks are queued up until we are no longer parsing
30965 tentatively. */
30966 push_deferring_access_checks (dk_deferred);
30967 }
30968
30969 /* Commit to the currently active tentative parse. */
30970
30971 static void
30972 cp_parser_commit_to_tentative_parse (cp_parser* parser)
30973 {
30974 cp_parser_context *context;
30975 cp_lexer *lexer;
30976
30977 /* Mark all of the levels as committed. */
30978 lexer = parser->lexer;
30979 for (context = parser->context; context->next; context = context->next)
30980 {
30981 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
30982 break;
30983 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
30984 while (!cp_lexer_saving_tokens (lexer))
30985 lexer = lexer->next;
30986 cp_lexer_commit_tokens (lexer);
30987 }
30988 }
30989
30990 /* Commit to the topmost currently active tentative parse.
30991
30992 Note that this function shouldn't be called when there are
30993 irreversible side-effects while in a tentative state. For
30994 example, we shouldn't create a permanent entry in the symbol
30995 table, or issue an error message that might not apply if the
30996 tentative parse is aborted. */
30997
30998 static void
30999 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
31000 {
31001 cp_parser_context *context = parser->context;
31002 cp_lexer *lexer = parser->lexer;
31003
31004 if (context)
31005 {
31006 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
31007 return;
31008 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
31009
31010 while (!cp_lexer_saving_tokens (lexer))
31011 lexer = lexer->next;
31012 cp_lexer_commit_tokens (lexer);
31013 }
31014 }
31015
31016 /* Abort the currently active tentative parse. All consumed tokens
31017 will be rolled back, and no diagnostics will be issued. */
31018
31019 static void
31020 cp_parser_abort_tentative_parse (cp_parser* parser)
31021 {
31022 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
31023 || errorcount > 0);
31024 cp_parser_simulate_error (parser);
31025 /* Now, pretend that we want to see if the construct was
31026 successfully parsed. */
31027 cp_parser_parse_definitely (parser);
31028 }
31029
31030 /* Stop parsing tentatively. If a parse error has occurred, restore the
31031 token stream. Otherwise, commit to the tokens we have consumed.
31032 Returns true if no error occurred; false otherwise. */
31033
31034 static bool
31035 cp_parser_parse_definitely (cp_parser* parser)
31036 {
31037 bool error_occurred;
31038 cp_parser_context *context;
31039
31040 /* Remember whether or not an error occurred, since we are about to
31041 destroy that information. */
31042 error_occurred = cp_parser_error_occurred (parser);
31043 /* Remove the topmost context from the stack. */
31044 context = parser->context;
31045 parser->context = context->next;
31046 /* If no parse errors occurred, commit to the tentative parse. */
31047 if (!error_occurred)
31048 {
31049 /* Commit to the tokens read tentatively, unless that was
31050 already done. */
31051 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
31052 cp_lexer_commit_tokens (parser->lexer);
31053
31054 pop_to_parent_deferring_access_checks ();
31055 }
31056 /* Otherwise, if errors occurred, roll back our state so that things
31057 are just as they were before we began the tentative parse. */
31058 else
31059 {
31060 cp_lexer_rollback_tokens (parser->lexer);
31061 pop_deferring_access_checks ();
31062 }
31063 /* Add the context to the front of the free list. */
31064 context->next = cp_parser_context_free_list;
31065 cp_parser_context_free_list = context;
31066
31067 return !error_occurred;
31068 }
31069
31070 /* Returns true if we are parsing tentatively and are not committed to
31071 this tentative parse. */
31072
31073 static bool
31074 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
31075 {
31076 return (cp_parser_parsing_tentatively (parser)
31077 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
31078 }
31079
31080 /* Returns nonzero iff an error has occurred during the most recent
31081 tentative parse. */
31082
31083 static bool
31084 cp_parser_error_occurred (cp_parser* parser)
31085 {
31086 return (cp_parser_parsing_tentatively (parser)
31087 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
31088 }
31089
31090 /* Returns nonzero if GNU extensions are allowed. */
31091
31092 static bool
31093 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
31094 {
31095 return parser->allow_gnu_extensions_p;
31096 }
31097 \f
31098 /* Objective-C++ Productions */
31099
31100
31101 /* Parse an Objective-C expression, which feeds into a primary-expression
31102 above.
31103
31104 objc-expression:
31105 objc-message-expression
31106 objc-string-literal
31107 objc-encode-expression
31108 objc-protocol-expression
31109 objc-selector-expression
31110
31111 Returns a tree representation of the expression. */
31112
31113 static cp_expr
31114 cp_parser_objc_expression (cp_parser* parser)
31115 {
31116 /* Try to figure out what kind of declaration is present. */
31117 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31118
31119 switch (kwd->type)
31120 {
31121 case CPP_OPEN_SQUARE:
31122 return cp_parser_objc_message_expression (parser);
31123
31124 case CPP_OBJC_STRING:
31125 kwd = cp_lexer_consume_token (parser->lexer);
31126 return objc_build_string_object (kwd->u.value);
31127
31128 case CPP_KEYWORD:
31129 switch (kwd->keyword)
31130 {
31131 case RID_AT_ENCODE:
31132 return cp_parser_objc_encode_expression (parser);
31133
31134 case RID_AT_PROTOCOL:
31135 return cp_parser_objc_protocol_expression (parser);
31136
31137 case RID_AT_SELECTOR:
31138 return cp_parser_objc_selector_expression (parser);
31139
31140 default:
31141 break;
31142 }
31143 /* FALLTHRU */
31144 default:
31145 error_at (kwd->location,
31146 "misplaced %<@%D%> Objective-C++ construct",
31147 kwd->u.value);
31148 cp_parser_skip_to_end_of_block_or_statement (parser);
31149 }
31150
31151 return error_mark_node;
31152 }
31153
31154 /* Parse an Objective-C message expression.
31155
31156 objc-message-expression:
31157 [ objc-message-receiver objc-message-args ]
31158
31159 Returns a representation of an Objective-C message. */
31160
31161 static tree
31162 cp_parser_objc_message_expression (cp_parser* parser)
31163 {
31164 tree receiver, messageargs;
31165
31166 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
31167 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
31168 receiver = cp_parser_objc_message_receiver (parser);
31169 messageargs = cp_parser_objc_message_args (parser);
31170 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
31171 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31172
31173 tree result = objc_build_message_expr (receiver, messageargs);
31174
31175 /* Construct a location e.g.
31176 [self func1:5]
31177 ^~~~~~~~~~~~~~
31178 ranging from the '[' to the ']', with the caret at the start. */
31179 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
31180 protected_set_expr_location (result, combined_loc);
31181
31182 return result;
31183 }
31184
31185 /* Parse an objc-message-receiver.
31186
31187 objc-message-receiver:
31188 expression
31189 simple-type-specifier
31190
31191 Returns a representation of the type or expression. */
31192
31193 static tree
31194 cp_parser_objc_message_receiver (cp_parser* parser)
31195 {
31196 tree rcv;
31197
31198 /* An Objective-C message receiver may be either (1) a type
31199 or (2) an expression. */
31200 cp_parser_parse_tentatively (parser);
31201 rcv = cp_parser_expression (parser);
31202
31203 /* If that worked out, fine. */
31204 if (cp_parser_parse_definitely (parser))
31205 return rcv;
31206
31207 cp_parser_parse_tentatively (parser);
31208 rcv = cp_parser_simple_type_specifier (parser,
31209 /*decl_specs=*/NULL,
31210 CP_PARSER_FLAGS_NONE);
31211
31212 if (cp_parser_parse_definitely (parser))
31213 return objc_get_class_reference (rcv);
31214
31215 cp_parser_error (parser, "objective-c++ message receiver expected");
31216 return error_mark_node;
31217 }
31218
31219 /* Parse the arguments and selectors comprising an Objective-C message.
31220
31221 objc-message-args:
31222 objc-selector
31223 objc-selector-args
31224 objc-selector-args , objc-comma-args
31225
31226 objc-selector-args:
31227 objc-selector [opt] : assignment-expression
31228 objc-selector-args objc-selector [opt] : assignment-expression
31229
31230 objc-comma-args:
31231 assignment-expression
31232 objc-comma-args , assignment-expression
31233
31234 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
31235 selector arguments and TREE_VALUE containing a list of comma
31236 arguments. */
31237
31238 static tree
31239 cp_parser_objc_message_args (cp_parser* parser)
31240 {
31241 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
31242 bool maybe_unary_selector_p = true;
31243 cp_token *token = cp_lexer_peek_token (parser->lexer);
31244
31245 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
31246 {
31247 tree selector = NULL_TREE, arg;
31248
31249 if (token->type != CPP_COLON)
31250 selector = cp_parser_objc_selector (parser);
31251
31252 /* Detect if we have a unary selector. */
31253 if (maybe_unary_selector_p
31254 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31255 return build_tree_list (selector, NULL_TREE);
31256
31257 maybe_unary_selector_p = false;
31258 cp_parser_require (parser, CPP_COLON, RT_COLON);
31259 arg = cp_parser_assignment_expression (parser);
31260
31261 sel_args
31262 = chainon (sel_args,
31263 build_tree_list (selector, arg));
31264
31265 token = cp_lexer_peek_token (parser->lexer);
31266 }
31267
31268 /* Handle non-selector arguments, if any. */
31269 while (token->type == CPP_COMMA)
31270 {
31271 tree arg;
31272
31273 cp_lexer_consume_token (parser->lexer);
31274 arg = cp_parser_assignment_expression (parser);
31275
31276 addl_args
31277 = chainon (addl_args,
31278 build_tree_list (NULL_TREE, arg));
31279
31280 token = cp_lexer_peek_token (parser->lexer);
31281 }
31282
31283 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
31284 {
31285 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
31286 return build_tree_list (error_mark_node, error_mark_node);
31287 }
31288
31289 return build_tree_list (sel_args, addl_args);
31290 }
31291
31292 /* Parse an Objective-C encode expression.
31293
31294 objc-encode-expression:
31295 @encode objc-typename
31296
31297 Returns an encoded representation of the type argument. */
31298
31299 static cp_expr
31300 cp_parser_objc_encode_expression (cp_parser* parser)
31301 {
31302 tree type;
31303 cp_token *token;
31304 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
31305
31306 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
31307 matching_parens parens;
31308 parens.require_open (parser);
31309 token = cp_lexer_peek_token (parser->lexer);
31310 type = complete_type (cp_parser_type_id (parser));
31311 parens.require_close (parser);
31312
31313 if (!type)
31314 {
31315 error_at (token->location,
31316 "%<@encode%> must specify a type as an argument");
31317 return error_mark_node;
31318 }
31319
31320 /* This happens if we find @encode(T) (where T is a template
31321 typename or something dependent on a template typename) when
31322 parsing a template. In that case, we can't compile it
31323 immediately, but we rather create an AT_ENCODE_EXPR which will
31324 need to be instantiated when the template is used.
31325 */
31326 if (dependent_type_p (type))
31327 {
31328 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
31329 TREE_READONLY (value) = 1;
31330 return value;
31331 }
31332
31333
31334 /* Build a location of the form:
31335 @encode(int)
31336 ^~~~~~~~~~~~
31337 with caret==start at the @ token, finishing at the close paren. */
31338 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
31339
31340 return cp_expr (objc_build_encode_expr (type), combined_loc);
31341 }
31342
31343 /* Parse an Objective-C @defs expression. */
31344
31345 static tree
31346 cp_parser_objc_defs_expression (cp_parser *parser)
31347 {
31348 tree name;
31349
31350 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
31351 matching_parens parens;
31352 parens.require_open (parser);
31353 name = cp_parser_identifier (parser);
31354 parens.require_close (parser);
31355
31356 return objc_get_class_ivars (name);
31357 }
31358
31359 /* Parse an Objective-C protocol expression.
31360
31361 objc-protocol-expression:
31362 @protocol ( identifier )
31363
31364 Returns a representation of the protocol expression. */
31365
31366 static tree
31367 cp_parser_objc_protocol_expression (cp_parser* parser)
31368 {
31369 tree proto;
31370 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
31371
31372 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
31373 matching_parens parens;
31374 parens.require_open (parser);
31375 proto = cp_parser_identifier (parser);
31376 parens.require_close (parser);
31377
31378 /* Build a location of the form:
31379 @protocol(prot)
31380 ^~~~~~~~~~~~~~~
31381 with caret==start at the @ token, finishing at the close paren. */
31382 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
31383 tree result = objc_build_protocol_expr (proto);
31384 protected_set_expr_location (result, combined_loc);
31385 return result;
31386 }
31387
31388 /* Parse an Objective-C selector expression.
31389
31390 objc-selector-expression:
31391 @selector ( objc-method-signature )
31392
31393 objc-method-signature:
31394 objc-selector
31395 objc-selector-seq
31396
31397 objc-selector-seq:
31398 objc-selector :
31399 objc-selector-seq objc-selector :
31400
31401 Returns a representation of the method selector. */
31402
31403 static tree
31404 cp_parser_objc_selector_expression (cp_parser* parser)
31405 {
31406 tree sel_seq = NULL_TREE;
31407 bool maybe_unary_selector_p = true;
31408 cp_token *token;
31409 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31410
31411 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
31412 matching_parens parens;
31413 parens.require_open (parser);
31414 token = cp_lexer_peek_token (parser->lexer);
31415
31416 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
31417 || token->type == CPP_SCOPE)
31418 {
31419 tree selector = NULL_TREE;
31420
31421 if (token->type != CPP_COLON
31422 || token->type == CPP_SCOPE)
31423 selector = cp_parser_objc_selector (parser);
31424
31425 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
31426 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
31427 {
31428 /* Detect if we have a unary selector. */
31429 if (maybe_unary_selector_p)
31430 {
31431 sel_seq = selector;
31432 goto finish_selector;
31433 }
31434 else
31435 {
31436 cp_parser_error (parser, "expected %<:%>");
31437 }
31438 }
31439 maybe_unary_selector_p = false;
31440 token = cp_lexer_consume_token (parser->lexer);
31441
31442 if (token->type == CPP_SCOPE)
31443 {
31444 sel_seq
31445 = chainon (sel_seq,
31446 build_tree_list (selector, NULL_TREE));
31447 sel_seq
31448 = chainon (sel_seq,
31449 build_tree_list (NULL_TREE, NULL_TREE));
31450 }
31451 else
31452 sel_seq
31453 = chainon (sel_seq,
31454 build_tree_list (selector, NULL_TREE));
31455
31456 token = cp_lexer_peek_token (parser->lexer);
31457 }
31458
31459 finish_selector:
31460 parens.require_close (parser);
31461
31462
31463 /* Build a location of the form:
31464 @selector(func)
31465 ^~~~~~~~~~~~~~~
31466 with caret==start at the @ token, finishing at the close paren. */
31467 location_t combined_loc = make_location (loc, loc, parser->lexer);
31468 tree result = objc_build_selector_expr (combined_loc, sel_seq);
31469 /* TODO: objc_build_selector_expr doesn't always honor the location. */
31470 protected_set_expr_location (result, combined_loc);
31471 return result;
31472 }
31473
31474 /* Parse a list of identifiers.
31475
31476 objc-identifier-list:
31477 identifier
31478 objc-identifier-list , identifier
31479
31480 Returns a TREE_LIST of identifier nodes. */
31481
31482 static tree
31483 cp_parser_objc_identifier_list (cp_parser* parser)
31484 {
31485 tree identifier;
31486 tree list;
31487 cp_token *sep;
31488
31489 identifier = cp_parser_identifier (parser);
31490 if (identifier == error_mark_node)
31491 return error_mark_node;
31492
31493 list = build_tree_list (NULL_TREE, identifier);
31494 sep = cp_lexer_peek_token (parser->lexer);
31495
31496 while (sep->type == CPP_COMMA)
31497 {
31498 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31499 identifier = cp_parser_identifier (parser);
31500 if (identifier == error_mark_node)
31501 return list;
31502
31503 list = chainon (list, build_tree_list (NULL_TREE,
31504 identifier));
31505 sep = cp_lexer_peek_token (parser->lexer);
31506 }
31507
31508 return list;
31509 }
31510
31511 /* Parse an Objective-C alias declaration.
31512
31513 objc-alias-declaration:
31514 @compatibility_alias identifier identifier ;
31515
31516 This function registers the alias mapping with the Objective-C front end.
31517 It returns nothing. */
31518
31519 static void
31520 cp_parser_objc_alias_declaration (cp_parser* parser)
31521 {
31522 tree alias, orig;
31523
31524 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
31525 alias = cp_parser_identifier (parser);
31526 orig = cp_parser_identifier (parser);
31527 objc_declare_alias (alias, orig);
31528 cp_parser_consume_semicolon_at_end_of_statement (parser);
31529 }
31530
31531 /* Parse an Objective-C class forward-declaration.
31532
31533 objc-class-declaration:
31534 @class objc-identifier-list ;
31535
31536 The function registers the forward declarations with the Objective-C
31537 front end. It returns nothing. */
31538
31539 static void
31540 cp_parser_objc_class_declaration (cp_parser* parser)
31541 {
31542 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
31543 while (true)
31544 {
31545 tree id;
31546
31547 id = cp_parser_identifier (parser);
31548 if (id == error_mark_node)
31549 break;
31550
31551 objc_declare_class (id);
31552
31553 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31554 cp_lexer_consume_token (parser->lexer);
31555 else
31556 break;
31557 }
31558 cp_parser_consume_semicolon_at_end_of_statement (parser);
31559 }
31560
31561 /* Parse a list of Objective-C protocol references.
31562
31563 objc-protocol-refs-opt:
31564 objc-protocol-refs [opt]
31565
31566 objc-protocol-refs:
31567 < objc-identifier-list >
31568
31569 Returns a TREE_LIST of identifiers, if any. */
31570
31571 static tree
31572 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
31573 {
31574 tree protorefs = NULL_TREE;
31575
31576 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
31577 {
31578 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
31579 protorefs = cp_parser_objc_identifier_list (parser);
31580 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
31581 }
31582
31583 return protorefs;
31584 }
31585
31586 /* Parse a Objective-C visibility specification. */
31587
31588 static void
31589 cp_parser_objc_visibility_spec (cp_parser* parser)
31590 {
31591 cp_token *vis = cp_lexer_peek_token (parser->lexer);
31592
31593 switch (vis->keyword)
31594 {
31595 case RID_AT_PRIVATE:
31596 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
31597 break;
31598 case RID_AT_PROTECTED:
31599 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
31600 break;
31601 case RID_AT_PUBLIC:
31602 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
31603 break;
31604 case RID_AT_PACKAGE:
31605 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
31606 break;
31607 default:
31608 return;
31609 }
31610
31611 /* Eat '@private'/'@protected'/'@public'. */
31612 cp_lexer_consume_token (parser->lexer);
31613 }
31614
31615 /* Parse an Objective-C method type. Return 'true' if it is a class
31616 (+) method, and 'false' if it is an instance (-) method. */
31617
31618 static inline bool
31619 cp_parser_objc_method_type (cp_parser* parser)
31620 {
31621 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
31622 return true;
31623 else
31624 return false;
31625 }
31626
31627 /* Parse an Objective-C protocol qualifier. */
31628
31629 static tree
31630 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
31631 {
31632 tree quals = NULL_TREE, node;
31633 cp_token *token = cp_lexer_peek_token (parser->lexer);
31634
31635 node = token->u.value;
31636
31637 while (node && identifier_p (node)
31638 && (node == ridpointers [(int) RID_IN]
31639 || node == ridpointers [(int) RID_OUT]
31640 || node == ridpointers [(int) RID_INOUT]
31641 || node == ridpointers [(int) RID_BYCOPY]
31642 || node == ridpointers [(int) RID_BYREF]
31643 || node == ridpointers [(int) RID_ONEWAY]))
31644 {
31645 quals = tree_cons (NULL_TREE, node, quals);
31646 cp_lexer_consume_token (parser->lexer);
31647 token = cp_lexer_peek_token (parser->lexer);
31648 node = token->u.value;
31649 }
31650
31651 return quals;
31652 }
31653
31654 /* Parse an Objective-C typename. */
31655
31656 static tree
31657 cp_parser_objc_typename (cp_parser* parser)
31658 {
31659 tree type_name = NULL_TREE;
31660
31661 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31662 {
31663 tree proto_quals, cp_type = NULL_TREE;
31664
31665 matching_parens parens;
31666 parens.consume_open (parser); /* Eat '('. */
31667 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
31668
31669 /* An ObjC type name may consist of just protocol qualifiers, in which
31670 case the type shall default to 'id'. */
31671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
31672 {
31673 cp_type = cp_parser_type_id (parser);
31674
31675 /* If the type could not be parsed, an error has already
31676 been produced. For error recovery, behave as if it had
31677 not been specified, which will use the default type
31678 'id'. */
31679 if (cp_type == error_mark_node)
31680 {
31681 cp_type = NULL_TREE;
31682 /* We need to skip to the closing parenthesis as
31683 cp_parser_type_id() does not seem to do it for
31684 us. */
31685 cp_parser_skip_to_closing_parenthesis (parser,
31686 /*recovering=*/true,
31687 /*or_comma=*/false,
31688 /*consume_paren=*/false);
31689 }
31690 }
31691
31692 parens.require_close (parser);
31693 type_name = build_tree_list (proto_quals, cp_type);
31694 }
31695
31696 return type_name;
31697 }
31698
31699 /* Check to see if TYPE refers to an Objective-C selector name. */
31700
31701 static bool
31702 cp_parser_objc_selector_p (enum cpp_ttype type)
31703 {
31704 return (type == CPP_NAME || type == CPP_KEYWORD
31705 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
31706 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
31707 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
31708 || type == CPP_XOR || type == CPP_XOR_EQ);
31709 }
31710
31711 /* Parse an Objective-C selector. */
31712
31713 static tree
31714 cp_parser_objc_selector (cp_parser* parser)
31715 {
31716 cp_token *token = cp_lexer_consume_token (parser->lexer);
31717
31718 if (!cp_parser_objc_selector_p (token->type))
31719 {
31720 error_at (token->location, "invalid Objective-C++ selector name");
31721 return error_mark_node;
31722 }
31723
31724 /* C++ operator names are allowed to appear in ObjC selectors. */
31725 switch (token->type)
31726 {
31727 case CPP_AND_AND: return get_identifier ("and");
31728 case CPP_AND_EQ: return get_identifier ("and_eq");
31729 case CPP_AND: return get_identifier ("bitand");
31730 case CPP_OR: return get_identifier ("bitor");
31731 case CPP_COMPL: return get_identifier ("compl");
31732 case CPP_NOT: return get_identifier ("not");
31733 case CPP_NOT_EQ: return get_identifier ("not_eq");
31734 case CPP_OR_OR: return get_identifier ("or");
31735 case CPP_OR_EQ: return get_identifier ("or_eq");
31736 case CPP_XOR: return get_identifier ("xor");
31737 case CPP_XOR_EQ: return get_identifier ("xor_eq");
31738 default: return token->u.value;
31739 }
31740 }
31741
31742 /* Parse an Objective-C params list. */
31743
31744 static tree
31745 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
31746 {
31747 tree params = NULL_TREE;
31748 bool maybe_unary_selector_p = true;
31749 cp_token *token = cp_lexer_peek_token (parser->lexer);
31750
31751 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
31752 {
31753 tree selector = NULL_TREE, type_name, identifier;
31754 tree parm_attr = NULL_TREE;
31755
31756 if (token->keyword == RID_ATTRIBUTE)
31757 break;
31758
31759 if (token->type != CPP_COLON)
31760 selector = cp_parser_objc_selector (parser);
31761
31762 /* Detect if we have a unary selector. */
31763 if (maybe_unary_selector_p
31764 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31765 {
31766 params = selector; /* Might be followed by attributes. */
31767 break;
31768 }
31769
31770 maybe_unary_selector_p = false;
31771 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31772 {
31773 /* Something went quite wrong. There should be a colon
31774 here, but there is not. Stop parsing parameters. */
31775 break;
31776 }
31777 type_name = cp_parser_objc_typename (parser);
31778 /* New ObjC allows attributes on parameters too. */
31779 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31780 parm_attr = cp_parser_attributes_opt (parser);
31781 identifier = cp_parser_identifier (parser);
31782
31783 params
31784 = chainon (params,
31785 objc_build_keyword_decl (selector,
31786 type_name,
31787 identifier,
31788 parm_attr));
31789
31790 token = cp_lexer_peek_token (parser->lexer);
31791 }
31792
31793 if (params == NULL_TREE)
31794 {
31795 cp_parser_error (parser, "objective-c++ method declaration is expected");
31796 return error_mark_node;
31797 }
31798
31799 /* We allow tail attributes for the method. */
31800 if (token->keyword == RID_ATTRIBUTE)
31801 {
31802 *attributes = cp_parser_attributes_opt (parser);
31803 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
31804 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31805 return params;
31806 cp_parser_error (parser,
31807 "method attributes must be specified at the end");
31808 return error_mark_node;
31809 }
31810
31811 if (params == NULL_TREE)
31812 {
31813 cp_parser_error (parser, "objective-c++ method declaration is expected");
31814 return error_mark_node;
31815 }
31816 return params;
31817 }
31818
31819 /* Parse the non-keyword Objective-C params. */
31820
31821 static tree
31822 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
31823 tree* attributes)
31824 {
31825 tree params = make_node (TREE_LIST);
31826 cp_token *token = cp_lexer_peek_token (parser->lexer);
31827 *ellipsisp = false; /* Initially, assume no ellipsis. */
31828
31829 while (token->type == CPP_COMMA)
31830 {
31831 cp_parameter_declarator *parmdecl;
31832 tree parm;
31833
31834 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31835 token = cp_lexer_peek_token (parser->lexer);
31836
31837 if (token->type == CPP_ELLIPSIS)
31838 {
31839 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
31840 *ellipsisp = true;
31841 token = cp_lexer_peek_token (parser->lexer);
31842 break;
31843 }
31844
31845 /* TODO: parse attributes for tail parameters. */
31846 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31847 false, NULL);
31848 parm = grokdeclarator (parmdecl->declarator,
31849 &parmdecl->decl_specifiers,
31850 PARM, /*initialized=*/0,
31851 /*attrlist=*/NULL);
31852
31853 chainon (params, build_tree_list (NULL_TREE, parm));
31854 token = cp_lexer_peek_token (parser->lexer);
31855 }
31856
31857 /* We allow tail attributes for the method. */
31858 if (token->keyword == RID_ATTRIBUTE)
31859 {
31860 if (*attributes == NULL_TREE)
31861 {
31862 *attributes = cp_parser_attributes_opt (parser);
31863 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
31864 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31865 return params;
31866 }
31867 else
31868 /* We have an error, but parse the attributes, so that we can
31869 carry on. */
31870 *attributes = cp_parser_attributes_opt (parser);
31871
31872 cp_parser_error (parser,
31873 "method attributes must be specified at the end");
31874 return error_mark_node;
31875 }
31876
31877 return params;
31878 }
31879
31880 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
31881
31882 static void
31883 cp_parser_objc_interstitial_code (cp_parser* parser)
31884 {
31885 cp_token *token = cp_lexer_peek_token (parser->lexer);
31886
31887 /* If the next token is `extern' and the following token is a string
31888 literal, then we have a linkage specification. */
31889 if (token->keyword == RID_EXTERN
31890 && cp_parser_is_pure_string_literal
31891 (cp_lexer_peek_nth_token (parser->lexer, 2)))
31892 cp_parser_linkage_specification (parser);
31893 /* Handle #pragma, if any. */
31894 else if (token->type == CPP_PRAGMA)
31895 cp_parser_pragma (parser, pragma_objc_icode, NULL);
31896 /* Allow stray semicolons. */
31897 else if (token->type == CPP_SEMICOLON)
31898 cp_lexer_consume_token (parser->lexer);
31899 /* Mark methods as optional or required, when building protocols. */
31900 else if (token->keyword == RID_AT_OPTIONAL)
31901 {
31902 cp_lexer_consume_token (parser->lexer);
31903 objc_set_method_opt (true);
31904 }
31905 else if (token->keyword == RID_AT_REQUIRED)
31906 {
31907 cp_lexer_consume_token (parser->lexer);
31908 objc_set_method_opt (false);
31909 }
31910 else if (token->keyword == RID_NAMESPACE)
31911 cp_parser_namespace_definition (parser);
31912 /* Other stray characters must generate errors. */
31913 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
31914 {
31915 cp_lexer_consume_token (parser->lexer);
31916 error ("stray %qs between Objective-C++ methods",
31917 token->type == CPP_OPEN_BRACE ? "{" : "}");
31918 }
31919 /* Finally, try to parse a block-declaration, or a function-definition. */
31920 else
31921 cp_parser_block_declaration (parser, /*statement_p=*/false);
31922 }
31923
31924 /* Parse a method signature. */
31925
31926 static tree
31927 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
31928 {
31929 tree rettype, kwdparms, optparms;
31930 bool ellipsis = false;
31931 bool is_class_method;
31932
31933 is_class_method = cp_parser_objc_method_type (parser);
31934 rettype = cp_parser_objc_typename (parser);
31935 *attributes = NULL_TREE;
31936 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
31937 if (kwdparms == error_mark_node)
31938 return error_mark_node;
31939 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
31940 if (optparms == error_mark_node)
31941 return error_mark_node;
31942
31943 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
31944 }
31945
31946 static bool
31947 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
31948 {
31949 tree tattr;
31950 cp_lexer_save_tokens (parser->lexer);
31951 tattr = cp_parser_attributes_opt (parser);
31952 gcc_assert (tattr) ;
31953
31954 /* If the attributes are followed by a method introducer, this is not allowed.
31955 Dump the attributes and flag the situation. */
31956 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
31957 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31958 return true;
31959
31960 /* Otherwise, the attributes introduce some interstitial code, possibly so
31961 rewind to allow that check. */
31962 cp_lexer_rollback_tokens (parser->lexer);
31963 return false;
31964 }
31965
31966 /* Parse an Objective-C method prototype list. */
31967
31968 static void
31969 cp_parser_objc_method_prototype_list (cp_parser* parser)
31970 {
31971 cp_token *token = cp_lexer_peek_token (parser->lexer);
31972
31973 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31974 {
31975 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31976 {
31977 tree attributes, sig;
31978 bool is_class_method;
31979 if (token->type == CPP_PLUS)
31980 is_class_method = true;
31981 else
31982 is_class_method = false;
31983 sig = cp_parser_objc_method_signature (parser, &attributes);
31984 if (sig == error_mark_node)
31985 {
31986 cp_parser_skip_to_end_of_block_or_statement (parser);
31987 token = cp_lexer_peek_token (parser->lexer);
31988 continue;
31989 }
31990 objc_add_method_declaration (is_class_method, sig, attributes);
31991 cp_parser_consume_semicolon_at_end_of_statement (parser);
31992 }
31993 else if (token->keyword == RID_AT_PROPERTY)
31994 cp_parser_objc_at_property_declaration (parser);
31995 else if (token->keyword == RID_ATTRIBUTE
31996 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31997 warning_at (cp_lexer_peek_token (parser->lexer)->location,
31998 OPT_Wattributes,
31999 "prefix attributes are ignored for methods");
32000 else
32001 /* Allow for interspersed non-ObjC++ code. */
32002 cp_parser_objc_interstitial_code (parser);
32003
32004 token = cp_lexer_peek_token (parser->lexer);
32005 }
32006
32007 if (token->type != CPP_EOF)
32008 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
32009 else
32010 cp_parser_error (parser, "expected %<@end%>");
32011
32012 objc_finish_interface ();
32013 }
32014
32015 /* Parse an Objective-C method definition list. */
32016
32017 static void
32018 cp_parser_objc_method_definition_list (cp_parser* parser)
32019 {
32020 cp_token *token = cp_lexer_peek_token (parser->lexer);
32021
32022 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
32023 {
32024 tree meth;
32025
32026 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
32027 {
32028 cp_token *ptk;
32029 tree sig, attribute;
32030 bool is_class_method;
32031 if (token->type == CPP_PLUS)
32032 is_class_method = true;
32033 else
32034 is_class_method = false;
32035 push_deferring_access_checks (dk_deferred);
32036 sig = cp_parser_objc_method_signature (parser, &attribute);
32037 if (sig == error_mark_node)
32038 {
32039 cp_parser_skip_to_end_of_block_or_statement (parser);
32040 token = cp_lexer_peek_token (parser->lexer);
32041 continue;
32042 }
32043 objc_start_method_definition (is_class_method, sig, attribute,
32044 NULL_TREE);
32045
32046 /* For historical reasons, we accept an optional semicolon. */
32047 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32048 cp_lexer_consume_token (parser->lexer);
32049
32050 ptk = cp_lexer_peek_token (parser->lexer);
32051 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
32052 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
32053 {
32054 perform_deferred_access_checks (tf_warning_or_error);
32055 stop_deferring_access_checks ();
32056 meth = cp_parser_function_definition_after_declarator (parser,
32057 false);
32058 pop_deferring_access_checks ();
32059 objc_finish_method_definition (meth);
32060 }
32061 }
32062 /* The following case will be removed once @synthesize is
32063 completely implemented. */
32064 else if (token->keyword == RID_AT_PROPERTY)
32065 cp_parser_objc_at_property_declaration (parser);
32066 else if (token->keyword == RID_AT_SYNTHESIZE)
32067 cp_parser_objc_at_synthesize_declaration (parser);
32068 else if (token->keyword == RID_AT_DYNAMIC)
32069 cp_parser_objc_at_dynamic_declaration (parser);
32070 else if (token->keyword == RID_ATTRIBUTE
32071 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
32072 warning_at (token->location, OPT_Wattributes,
32073 "prefix attributes are ignored for methods");
32074 else
32075 /* Allow for interspersed non-ObjC++ code. */
32076 cp_parser_objc_interstitial_code (parser);
32077
32078 token = cp_lexer_peek_token (parser->lexer);
32079 }
32080
32081 if (token->type != CPP_EOF)
32082 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
32083 else
32084 cp_parser_error (parser, "expected %<@end%>");
32085
32086 objc_finish_implementation ();
32087 }
32088
32089 /* Parse Objective-C ivars. */
32090
32091 static void
32092 cp_parser_objc_class_ivars (cp_parser* parser)
32093 {
32094 cp_token *token = cp_lexer_peek_token (parser->lexer);
32095
32096 if (token->type != CPP_OPEN_BRACE)
32097 return; /* No ivars specified. */
32098
32099 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
32100 token = cp_lexer_peek_token (parser->lexer);
32101
32102 while (token->type != CPP_CLOSE_BRACE
32103 && token->keyword != RID_AT_END && token->type != CPP_EOF)
32104 {
32105 cp_decl_specifier_seq declspecs;
32106 int decl_class_or_enum_p;
32107 tree prefix_attributes;
32108
32109 cp_parser_objc_visibility_spec (parser);
32110
32111 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
32112 break;
32113
32114 cp_parser_decl_specifier_seq (parser,
32115 CP_PARSER_FLAGS_OPTIONAL,
32116 &declspecs,
32117 &decl_class_or_enum_p);
32118
32119 /* auto, register, static, extern, mutable. */
32120 if (declspecs.storage_class != sc_none)
32121 {
32122 cp_parser_error (parser, "invalid type for instance variable");
32123 declspecs.storage_class = sc_none;
32124 }
32125
32126 /* thread_local. */
32127 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
32128 {
32129 cp_parser_error (parser, "invalid type for instance variable");
32130 declspecs.locations[ds_thread] = 0;
32131 }
32132
32133 /* typedef. */
32134 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
32135 {
32136 cp_parser_error (parser, "invalid type for instance variable");
32137 declspecs.locations[ds_typedef] = 0;
32138 }
32139
32140 prefix_attributes = declspecs.attributes;
32141 declspecs.attributes = NULL_TREE;
32142
32143 /* Keep going until we hit the `;' at the end of the
32144 declaration. */
32145 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
32146 {
32147 tree width = NULL_TREE, attributes, first_attribute, decl;
32148 cp_declarator *declarator = NULL;
32149 int ctor_dtor_or_conv_p;
32150
32151 /* Check for a (possibly unnamed) bitfield declaration. */
32152 token = cp_lexer_peek_token (parser->lexer);
32153 if (token->type == CPP_COLON)
32154 goto eat_colon;
32155
32156 if (token->type == CPP_NAME
32157 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
32158 == CPP_COLON))
32159 {
32160 /* Get the name of the bitfield. */
32161 declarator = make_id_declarator (NULL_TREE,
32162 cp_parser_identifier (parser),
32163 sfk_none, token->location);
32164
32165 eat_colon:
32166 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
32167 /* Get the width of the bitfield. */
32168 width
32169 = cp_parser_constant_expression (parser);
32170 }
32171 else
32172 {
32173 /* Parse the declarator. */
32174 declarator
32175 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
32176 CP_PARSER_FLAGS_NONE,
32177 &ctor_dtor_or_conv_p,
32178 /*parenthesized_p=*/NULL,
32179 /*member_p=*/false,
32180 /*friend_p=*/false,
32181 /*static_p=*/false);
32182 }
32183
32184 /* Look for attributes that apply to the ivar. */
32185 attributes = cp_parser_attributes_opt (parser);
32186 /* Remember which attributes are prefix attributes and
32187 which are not. */
32188 first_attribute = attributes;
32189 /* Combine the attributes. */
32190 attributes = attr_chainon (prefix_attributes, attributes);
32191
32192 if (width)
32193 /* Create the bitfield declaration. */
32194 decl = grokbitfield (declarator, &declspecs,
32195 width, NULL_TREE, attributes);
32196 else
32197 decl = grokfield (declarator, &declspecs,
32198 NULL_TREE, /*init_const_expr_p=*/false,
32199 NULL_TREE, attributes);
32200
32201 /* Add the instance variable. */
32202 if (decl != error_mark_node && decl != NULL_TREE)
32203 objc_add_instance_variable (decl);
32204
32205 /* Reset PREFIX_ATTRIBUTES. */
32206 if (attributes != error_mark_node)
32207 {
32208 while (attributes && TREE_CHAIN (attributes) != first_attribute)
32209 attributes = TREE_CHAIN (attributes);
32210 if (attributes)
32211 TREE_CHAIN (attributes) = NULL_TREE;
32212 }
32213
32214 token = cp_lexer_peek_token (parser->lexer);
32215
32216 if (token->type == CPP_COMMA)
32217 {
32218 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
32219 continue;
32220 }
32221 break;
32222 }
32223
32224 cp_parser_consume_semicolon_at_end_of_statement (parser);
32225 token = cp_lexer_peek_token (parser->lexer);
32226 }
32227
32228 if (token->keyword == RID_AT_END)
32229 cp_parser_error (parser, "expected %<}%>");
32230
32231 /* Do not consume the RID_AT_END, so it will be read again as terminating
32232 the @interface of @implementation. */
32233 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
32234 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
32235
32236 /* For historical reasons, we accept an optional semicolon. */
32237 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32238 cp_lexer_consume_token (parser->lexer);
32239 }
32240
32241 /* Parse an Objective-C protocol declaration. */
32242
32243 static void
32244 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
32245 {
32246 tree proto, protorefs;
32247 cp_token *tok;
32248
32249 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
32250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32251 {
32252 tok = cp_lexer_peek_token (parser->lexer);
32253 error_at (tok->location, "identifier expected after %<@protocol%>");
32254 cp_parser_consume_semicolon_at_end_of_statement (parser);
32255 return;
32256 }
32257
32258 /* See if we have a forward declaration or a definition. */
32259 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
32260
32261 /* Try a forward declaration first. */
32262 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
32263 {
32264 while (true)
32265 {
32266 tree id;
32267
32268 id = cp_parser_identifier (parser);
32269 if (id == error_mark_node)
32270 break;
32271
32272 objc_declare_protocol (id, attributes);
32273
32274 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32275 cp_lexer_consume_token (parser->lexer);
32276 else
32277 break;
32278 }
32279 cp_parser_consume_semicolon_at_end_of_statement (parser);
32280 }
32281
32282 /* Ok, we got a full-fledged definition (or at least should). */
32283 else
32284 {
32285 proto = cp_parser_identifier (parser);
32286 protorefs = cp_parser_objc_protocol_refs_opt (parser);
32287 objc_start_protocol (proto, protorefs, attributes);
32288 cp_parser_objc_method_prototype_list (parser);
32289 }
32290 }
32291
32292 /* Parse an Objective-C superclass or category. */
32293
32294 static void
32295 cp_parser_objc_superclass_or_category (cp_parser *parser,
32296 bool iface_p,
32297 tree *super,
32298 tree *categ, bool *is_class_extension)
32299 {
32300 cp_token *next = cp_lexer_peek_token (parser->lexer);
32301
32302 *super = *categ = NULL_TREE;
32303 *is_class_extension = false;
32304 if (next->type == CPP_COLON)
32305 {
32306 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
32307 *super = cp_parser_identifier (parser);
32308 }
32309 else if (next->type == CPP_OPEN_PAREN)
32310 {
32311 matching_parens parens;
32312 parens.consume_open (parser); /* Eat '('. */
32313
32314 /* If there is no category name, and this is an @interface, we
32315 have a class extension. */
32316 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32317 {
32318 *categ = NULL_TREE;
32319 *is_class_extension = true;
32320 }
32321 else
32322 *categ = cp_parser_identifier (parser);
32323
32324 parens.require_close (parser);
32325 }
32326 }
32327
32328 /* Parse an Objective-C class interface. */
32329
32330 static void
32331 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
32332 {
32333 tree name, super, categ, protos;
32334 bool is_class_extension;
32335
32336 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
32337 name = cp_parser_identifier (parser);
32338 if (name == error_mark_node)
32339 {
32340 /* It's hard to recover because even if valid @interface stuff
32341 is to follow, we can't compile it (or validate it) if we
32342 don't even know which class it refers to. Let's assume this
32343 was a stray '@interface' token in the stream and skip it.
32344 */
32345 return;
32346 }
32347 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
32348 &is_class_extension);
32349 protos = cp_parser_objc_protocol_refs_opt (parser);
32350
32351 /* We have either a class or a category on our hands. */
32352 if (categ || is_class_extension)
32353 objc_start_category_interface (name, categ, protos, attributes);
32354 else
32355 {
32356 objc_start_class_interface (name, super, protos, attributes);
32357 /* Handle instance variable declarations, if any. */
32358 cp_parser_objc_class_ivars (parser);
32359 objc_continue_interface ();
32360 }
32361
32362 cp_parser_objc_method_prototype_list (parser);
32363 }
32364
32365 /* Parse an Objective-C class implementation. */
32366
32367 static void
32368 cp_parser_objc_class_implementation (cp_parser* parser)
32369 {
32370 tree name, super, categ;
32371 bool is_class_extension;
32372
32373 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
32374 name = cp_parser_identifier (parser);
32375 if (name == error_mark_node)
32376 {
32377 /* It's hard to recover because even if valid @implementation
32378 stuff is to follow, we can't compile it (or validate it) if
32379 we don't even know which class it refers to. Let's assume
32380 this was a stray '@implementation' token in the stream and
32381 skip it.
32382 */
32383 return;
32384 }
32385 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
32386 &is_class_extension);
32387
32388 /* We have either a class or a category on our hands. */
32389 if (categ)
32390 objc_start_category_implementation (name, categ);
32391 else
32392 {
32393 objc_start_class_implementation (name, super);
32394 /* Handle instance variable declarations, if any. */
32395 cp_parser_objc_class_ivars (parser);
32396 objc_continue_implementation ();
32397 }
32398
32399 cp_parser_objc_method_definition_list (parser);
32400 }
32401
32402 /* Consume the @end token and finish off the implementation. */
32403
32404 static void
32405 cp_parser_objc_end_implementation (cp_parser* parser)
32406 {
32407 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
32408 objc_finish_implementation ();
32409 }
32410
32411 /* Parse an Objective-C declaration. */
32412
32413 static void
32414 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
32415 {
32416 /* Try to figure out what kind of declaration is present. */
32417 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
32418
32419 if (attributes)
32420 switch (kwd->keyword)
32421 {
32422 case RID_AT_ALIAS:
32423 case RID_AT_CLASS:
32424 case RID_AT_END:
32425 error_at (kwd->location, "attributes may not be specified before"
32426 " the %<@%D%> Objective-C++ keyword",
32427 kwd->u.value);
32428 attributes = NULL;
32429 break;
32430 case RID_AT_IMPLEMENTATION:
32431 warning_at (kwd->location, OPT_Wattributes,
32432 "prefix attributes are ignored before %<@%D%>",
32433 kwd->u.value);
32434 attributes = NULL;
32435 default:
32436 break;
32437 }
32438
32439 switch (kwd->keyword)
32440 {
32441 case RID_AT_ALIAS:
32442 cp_parser_objc_alias_declaration (parser);
32443 break;
32444 case RID_AT_CLASS:
32445 cp_parser_objc_class_declaration (parser);
32446 break;
32447 case RID_AT_PROTOCOL:
32448 cp_parser_objc_protocol_declaration (parser, attributes);
32449 break;
32450 case RID_AT_INTERFACE:
32451 cp_parser_objc_class_interface (parser, attributes);
32452 break;
32453 case RID_AT_IMPLEMENTATION:
32454 cp_parser_objc_class_implementation (parser);
32455 break;
32456 case RID_AT_END:
32457 cp_parser_objc_end_implementation (parser);
32458 break;
32459 default:
32460 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
32461 kwd->u.value);
32462 cp_parser_skip_to_end_of_block_or_statement (parser);
32463 }
32464 }
32465
32466 /* Parse an Objective-C try-catch-finally statement.
32467
32468 objc-try-catch-finally-stmt:
32469 @try compound-statement objc-catch-clause-seq [opt]
32470 objc-finally-clause [opt]
32471
32472 objc-catch-clause-seq:
32473 objc-catch-clause objc-catch-clause-seq [opt]
32474
32475 objc-catch-clause:
32476 @catch ( objc-exception-declaration ) compound-statement
32477
32478 objc-finally-clause:
32479 @finally compound-statement
32480
32481 objc-exception-declaration:
32482 parameter-declaration
32483 '...'
32484
32485 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
32486
32487 Returns NULL_TREE.
32488
32489 PS: This function is identical to c_parser_objc_try_catch_finally_statement
32490 for C. Keep them in sync. */
32491
32492 static tree
32493 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
32494 {
32495 location_t location;
32496 tree stmt;
32497
32498 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
32499 location = cp_lexer_peek_token (parser->lexer)->location;
32500 objc_maybe_warn_exceptions (location);
32501 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
32502 node, lest it get absorbed into the surrounding block. */
32503 stmt = push_stmt_list ();
32504 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
32505 objc_begin_try_stmt (location, pop_stmt_list (stmt));
32506
32507 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
32508 {
32509 cp_parameter_declarator *parm;
32510 tree parameter_declaration = error_mark_node;
32511 bool seen_open_paren = false;
32512 matching_parens parens;
32513
32514 cp_lexer_consume_token (parser->lexer);
32515 if (parens.require_open (parser))
32516 seen_open_paren = true;
32517 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
32518 {
32519 /* We have "@catch (...)" (where the '...' are literally
32520 what is in the code). Skip the '...'.
32521 parameter_declaration is set to NULL_TREE, and
32522 objc_being_catch_clauses() knows that that means
32523 '...'. */
32524 cp_lexer_consume_token (parser->lexer);
32525 parameter_declaration = NULL_TREE;
32526 }
32527 else
32528 {
32529 /* We have "@catch (NSException *exception)" or something
32530 like that. Parse the parameter declaration. */
32531 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
32532 false, NULL);
32533 if (parm == NULL)
32534 parameter_declaration = error_mark_node;
32535 else
32536 parameter_declaration = grokdeclarator (parm->declarator,
32537 &parm->decl_specifiers,
32538 PARM, /*initialized=*/0,
32539 /*attrlist=*/NULL);
32540 }
32541 if (seen_open_paren)
32542 parens.require_close (parser);
32543 else
32544 {
32545 /* If there was no open parenthesis, we are recovering from
32546 an error, and we are trying to figure out what mistake
32547 the user has made. */
32548
32549 /* If there is an immediate closing parenthesis, the user
32550 probably forgot the opening one (ie, they typed "@catch
32551 NSException *e)". Parse the closing parenthesis and keep
32552 going. */
32553 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32554 cp_lexer_consume_token (parser->lexer);
32555
32556 /* If these is no immediate closing parenthesis, the user
32557 probably doesn't know that parenthesis are required at
32558 all (ie, they typed "@catch NSException *e"). So, just
32559 forget about the closing parenthesis and keep going. */
32560 }
32561 objc_begin_catch_clause (parameter_declaration);
32562 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
32563 objc_finish_catch_clause ();
32564 }
32565 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
32566 {
32567 cp_lexer_consume_token (parser->lexer);
32568 location = cp_lexer_peek_token (parser->lexer)->location;
32569 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
32570 node, lest it get absorbed into the surrounding block. */
32571 stmt = push_stmt_list ();
32572 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
32573 objc_build_finally_clause (location, pop_stmt_list (stmt));
32574 }
32575
32576 return objc_finish_try_stmt ();
32577 }
32578
32579 /* Parse an Objective-C synchronized statement.
32580
32581 objc-synchronized-stmt:
32582 @synchronized ( expression ) compound-statement
32583
32584 Returns NULL_TREE. */
32585
32586 static tree
32587 cp_parser_objc_synchronized_statement (cp_parser *parser)
32588 {
32589 location_t location;
32590 tree lock, stmt;
32591
32592 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
32593
32594 location = cp_lexer_peek_token (parser->lexer)->location;
32595 objc_maybe_warn_exceptions (location);
32596 matching_parens parens;
32597 parens.require_open (parser);
32598 lock = cp_parser_expression (parser);
32599 parens.require_close (parser);
32600
32601 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
32602 node, lest it get absorbed into the surrounding block. */
32603 stmt = push_stmt_list ();
32604 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
32605
32606 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
32607 }
32608
32609 /* Parse an Objective-C throw statement.
32610
32611 objc-throw-stmt:
32612 @throw assignment-expression [opt] ;
32613
32614 Returns a constructed '@throw' statement. */
32615
32616 static tree
32617 cp_parser_objc_throw_statement (cp_parser *parser)
32618 {
32619 tree expr = NULL_TREE;
32620 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32621
32622 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
32623
32624 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
32625 expr = cp_parser_expression (parser);
32626
32627 cp_parser_consume_semicolon_at_end_of_statement (parser);
32628
32629 return objc_build_throw_stmt (loc, expr);
32630 }
32631
32632 /* Parse an Objective-C statement. */
32633
32634 static tree
32635 cp_parser_objc_statement (cp_parser * parser)
32636 {
32637 /* Try to figure out what kind of declaration is present. */
32638 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
32639
32640 switch (kwd->keyword)
32641 {
32642 case RID_AT_TRY:
32643 return cp_parser_objc_try_catch_finally_statement (parser);
32644 case RID_AT_SYNCHRONIZED:
32645 return cp_parser_objc_synchronized_statement (parser);
32646 case RID_AT_THROW:
32647 return cp_parser_objc_throw_statement (parser);
32648 default:
32649 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
32650 kwd->u.value);
32651 cp_parser_skip_to_end_of_block_or_statement (parser);
32652 }
32653
32654 return error_mark_node;
32655 }
32656
32657 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
32658 look ahead to see if an objc keyword follows the attributes. This
32659 is to detect the use of prefix attributes on ObjC @interface and
32660 @protocol. */
32661
32662 static bool
32663 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
32664 {
32665 cp_lexer_save_tokens (parser->lexer);
32666 *attrib = cp_parser_attributes_opt (parser);
32667 gcc_assert (*attrib);
32668 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
32669 {
32670 cp_lexer_commit_tokens (parser->lexer);
32671 return true;
32672 }
32673 cp_lexer_rollback_tokens (parser->lexer);
32674 return false;
32675 }
32676
32677 /* This routine is a minimal replacement for
32678 c_parser_struct_declaration () used when parsing the list of
32679 types/names or ObjC++ properties. For example, when parsing the
32680 code
32681
32682 @property (readonly) int a, b, c;
32683
32684 this function is responsible for parsing "int a, int b, int c" and
32685 returning the declarations as CHAIN of DECLs.
32686
32687 TODO: Share this code with cp_parser_objc_class_ivars. It's very
32688 similar parsing. */
32689 static tree
32690 cp_parser_objc_struct_declaration (cp_parser *parser)
32691 {
32692 tree decls = NULL_TREE;
32693 cp_decl_specifier_seq declspecs;
32694 int decl_class_or_enum_p;
32695 tree prefix_attributes;
32696
32697 cp_parser_decl_specifier_seq (parser,
32698 CP_PARSER_FLAGS_NONE,
32699 &declspecs,
32700 &decl_class_or_enum_p);
32701
32702 if (declspecs.type == error_mark_node)
32703 return error_mark_node;
32704
32705 /* auto, register, static, extern, mutable. */
32706 if (declspecs.storage_class != sc_none)
32707 {
32708 cp_parser_error (parser, "invalid type for property");
32709 declspecs.storage_class = sc_none;
32710 }
32711
32712 /* thread_local. */
32713 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
32714 {
32715 cp_parser_error (parser, "invalid type for property");
32716 declspecs.locations[ds_thread] = 0;
32717 }
32718
32719 /* typedef. */
32720 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
32721 {
32722 cp_parser_error (parser, "invalid type for property");
32723 declspecs.locations[ds_typedef] = 0;
32724 }
32725
32726 prefix_attributes = declspecs.attributes;
32727 declspecs.attributes = NULL_TREE;
32728
32729 /* Keep going until we hit the `;' at the end of the declaration. */
32730 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
32731 {
32732 tree attributes, first_attribute, decl;
32733 cp_declarator *declarator;
32734 cp_token *token;
32735
32736 /* Parse the declarator. */
32737 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
32738 CP_PARSER_FLAGS_NONE,
32739 NULL, NULL, false, false, false);
32740
32741 /* Look for attributes that apply to the ivar. */
32742 attributes = cp_parser_attributes_opt (parser);
32743 /* Remember which attributes are prefix attributes and
32744 which are not. */
32745 first_attribute = attributes;
32746 /* Combine the attributes. */
32747 attributes = attr_chainon (prefix_attributes, attributes);
32748
32749 decl = grokfield (declarator, &declspecs,
32750 NULL_TREE, /*init_const_expr_p=*/false,
32751 NULL_TREE, attributes);
32752
32753 if (decl == error_mark_node || decl == NULL_TREE)
32754 return error_mark_node;
32755
32756 /* Reset PREFIX_ATTRIBUTES. */
32757 if (attributes != error_mark_node)
32758 {
32759 while (attributes && TREE_CHAIN (attributes) != first_attribute)
32760 attributes = TREE_CHAIN (attributes);
32761 if (attributes)
32762 TREE_CHAIN (attributes) = NULL_TREE;
32763 }
32764
32765 DECL_CHAIN (decl) = decls;
32766 decls = decl;
32767
32768 token = cp_lexer_peek_token (parser->lexer);
32769 if (token->type == CPP_COMMA)
32770 {
32771 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
32772 continue;
32773 }
32774 else
32775 break;
32776 }
32777 return decls;
32778 }
32779
32780 /* Parse an Objective-C @property declaration. The syntax is:
32781
32782 objc-property-declaration:
32783 '@property' objc-property-attributes[opt] struct-declaration ;
32784
32785 objc-property-attributes:
32786 '(' objc-property-attribute-list ')'
32787
32788 objc-property-attribute-list:
32789 objc-property-attribute
32790 objc-property-attribute-list, objc-property-attribute
32791
32792 objc-property-attribute
32793 'getter' = identifier
32794 'setter' = identifier
32795 'readonly'
32796 'readwrite'
32797 'assign'
32798 'retain'
32799 'copy'
32800 'nonatomic'
32801
32802 For example:
32803 @property NSString *name;
32804 @property (readonly) id object;
32805 @property (retain, nonatomic, getter=getTheName) id name;
32806 @property int a, b, c;
32807
32808 PS: This function is identical to
32809 c_parser_objc_at_property_declaration for C. Keep them in sync. */
32810 static void
32811 cp_parser_objc_at_property_declaration (cp_parser *parser)
32812 {
32813 /* The following variables hold the attributes of the properties as
32814 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
32815 seen. When we see an attribute, we set them to 'true' (if they
32816 are boolean properties) or to the identifier (if they have an
32817 argument, ie, for getter and setter). Note that here we only
32818 parse the list of attributes, check the syntax and accumulate the
32819 attributes that we find. objc_add_property_declaration() will
32820 then process the information. */
32821 bool property_assign = false;
32822 bool property_copy = false;
32823 tree property_getter_ident = NULL_TREE;
32824 bool property_nonatomic = false;
32825 bool property_readonly = false;
32826 bool property_readwrite = false;
32827 bool property_retain = false;
32828 tree property_setter_ident = NULL_TREE;
32829
32830 /* 'properties' is the list of properties that we read. Usually a
32831 single one, but maybe more (eg, in "@property int a, b, c;" there
32832 are three). */
32833 tree properties;
32834 location_t loc;
32835
32836 loc = cp_lexer_peek_token (parser->lexer)->location;
32837
32838 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
32839
32840 /* Parse the optional attribute list... */
32841 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32842 {
32843 /* Eat the '('. */
32844 matching_parens parens;
32845 parens.consume_open (parser);
32846
32847 while (true)
32848 {
32849 bool syntax_error = false;
32850 cp_token *token = cp_lexer_peek_token (parser->lexer);
32851 enum rid keyword;
32852
32853 if (token->type != CPP_NAME)
32854 {
32855 cp_parser_error (parser, "expected identifier");
32856 break;
32857 }
32858 keyword = C_RID_CODE (token->u.value);
32859 cp_lexer_consume_token (parser->lexer);
32860 switch (keyword)
32861 {
32862 case RID_ASSIGN: property_assign = true; break;
32863 case RID_COPY: property_copy = true; break;
32864 case RID_NONATOMIC: property_nonatomic = true; break;
32865 case RID_READONLY: property_readonly = true; break;
32866 case RID_READWRITE: property_readwrite = true; break;
32867 case RID_RETAIN: property_retain = true; break;
32868
32869 case RID_GETTER:
32870 case RID_SETTER:
32871 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
32872 {
32873 if (keyword == RID_GETTER)
32874 cp_parser_error (parser,
32875 "missing %<=%> (after %<getter%> attribute)");
32876 else
32877 cp_parser_error (parser,
32878 "missing %<=%> (after %<setter%> attribute)");
32879 syntax_error = true;
32880 break;
32881 }
32882 cp_lexer_consume_token (parser->lexer); /* eat the = */
32883 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
32884 {
32885 cp_parser_error (parser, "expected identifier");
32886 syntax_error = true;
32887 break;
32888 }
32889 if (keyword == RID_SETTER)
32890 {
32891 if (property_setter_ident != NULL_TREE)
32892 {
32893 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
32894 cp_lexer_consume_token (parser->lexer);
32895 }
32896 else
32897 property_setter_ident = cp_parser_objc_selector (parser);
32898 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
32899 cp_parser_error (parser, "setter name must terminate with %<:%>");
32900 else
32901 cp_lexer_consume_token (parser->lexer);
32902 }
32903 else
32904 {
32905 if (property_getter_ident != NULL_TREE)
32906 {
32907 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
32908 cp_lexer_consume_token (parser->lexer);
32909 }
32910 else
32911 property_getter_ident = cp_parser_objc_selector (parser);
32912 }
32913 break;
32914 default:
32915 cp_parser_error (parser, "unknown property attribute");
32916 syntax_error = true;
32917 break;
32918 }
32919
32920 if (syntax_error)
32921 break;
32922
32923 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32924 cp_lexer_consume_token (parser->lexer);
32925 else
32926 break;
32927 }
32928
32929 /* FIXME: "@property (setter, assign);" will generate a spurious
32930 "error: expected ‘)’ before ‘,’ token". This is because
32931 cp_parser_require, unlike the C counterpart, will produce an
32932 error even if we are in error recovery. */
32933 if (!parens.require_close (parser))
32934 {
32935 cp_parser_skip_to_closing_parenthesis (parser,
32936 /*recovering=*/true,
32937 /*or_comma=*/false,
32938 /*consume_paren=*/true);
32939 }
32940 }
32941
32942 /* ... and the property declaration(s). */
32943 properties = cp_parser_objc_struct_declaration (parser);
32944
32945 if (properties == error_mark_node)
32946 {
32947 cp_parser_skip_to_end_of_statement (parser);
32948 /* If the next token is now a `;', consume it. */
32949 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32950 cp_lexer_consume_token (parser->lexer);
32951 return;
32952 }
32953
32954 if (properties == NULL_TREE)
32955 cp_parser_error (parser, "expected identifier");
32956 else
32957 {
32958 /* Comma-separated properties are chained together in
32959 reverse order; add them one by one. */
32960 properties = nreverse (properties);
32961
32962 for (; properties; properties = TREE_CHAIN (properties))
32963 objc_add_property_declaration (loc, copy_node (properties),
32964 property_readonly, property_readwrite,
32965 property_assign, property_retain,
32966 property_copy, property_nonatomic,
32967 property_getter_ident, property_setter_ident);
32968 }
32969
32970 cp_parser_consume_semicolon_at_end_of_statement (parser);
32971 }
32972
32973 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
32974
32975 objc-synthesize-declaration:
32976 @synthesize objc-synthesize-identifier-list ;
32977
32978 objc-synthesize-identifier-list:
32979 objc-synthesize-identifier
32980 objc-synthesize-identifier-list, objc-synthesize-identifier
32981
32982 objc-synthesize-identifier
32983 identifier
32984 identifier = identifier
32985
32986 For example:
32987 @synthesize MyProperty;
32988 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
32989
32990 PS: This function is identical to c_parser_objc_at_synthesize_declaration
32991 for C. Keep them in sync.
32992 */
32993 static void
32994 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
32995 {
32996 tree list = NULL_TREE;
32997 location_t loc;
32998 loc = cp_lexer_peek_token (parser->lexer)->location;
32999
33000 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
33001 while (true)
33002 {
33003 tree property, ivar;
33004 property = cp_parser_identifier (parser);
33005 if (property == error_mark_node)
33006 {
33007 cp_parser_consume_semicolon_at_end_of_statement (parser);
33008 return;
33009 }
33010 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
33011 {
33012 cp_lexer_consume_token (parser->lexer);
33013 ivar = cp_parser_identifier (parser);
33014 if (ivar == error_mark_node)
33015 {
33016 cp_parser_consume_semicolon_at_end_of_statement (parser);
33017 return;
33018 }
33019 }
33020 else
33021 ivar = NULL_TREE;
33022 list = chainon (list, build_tree_list (ivar, property));
33023 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33024 cp_lexer_consume_token (parser->lexer);
33025 else
33026 break;
33027 }
33028 cp_parser_consume_semicolon_at_end_of_statement (parser);
33029 objc_add_synthesize_declaration (loc, list);
33030 }
33031
33032 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
33033
33034 objc-dynamic-declaration:
33035 @dynamic identifier-list ;
33036
33037 For example:
33038 @dynamic MyProperty;
33039 @dynamic MyProperty, AnotherProperty;
33040
33041 PS: This function is identical to c_parser_objc_at_dynamic_declaration
33042 for C. Keep them in sync.
33043 */
33044 static void
33045 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
33046 {
33047 tree list = NULL_TREE;
33048 location_t loc;
33049 loc = cp_lexer_peek_token (parser->lexer)->location;
33050
33051 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
33052 while (true)
33053 {
33054 tree property;
33055 property = cp_parser_identifier (parser);
33056 if (property == error_mark_node)
33057 {
33058 cp_parser_consume_semicolon_at_end_of_statement (parser);
33059 return;
33060 }
33061 list = chainon (list, build_tree_list (NULL, property));
33062 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33063 cp_lexer_consume_token (parser->lexer);
33064 else
33065 break;
33066 }
33067 cp_parser_consume_semicolon_at_end_of_statement (parser);
33068 objc_add_dynamic_declaration (loc, list);
33069 }
33070
33071 \f
33072 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
33073
33074 /* Returns name of the next clause.
33075 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
33076 the token is not consumed. Otherwise appropriate pragma_omp_clause is
33077 returned and the token is consumed. */
33078
33079 static pragma_omp_clause
33080 cp_parser_omp_clause_name (cp_parser *parser)
33081 {
33082 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
33083
33084 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33085 result = PRAGMA_OACC_CLAUSE_AUTO;
33086 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
33087 result = PRAGMA_OMP_CLAUSE_IF;
33088 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
33089 result = PRAGMA_OMP_CLAUSE_DEFAULT;
33090 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
33091 result = PRAGMA_OACC_CLAUSE_DELETE;
33092 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
33093 result = PRAGMA_OMP_CLAUSE_PRIVATE;
33094 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33095 result = PRAGMA_OMP_CLAUSE_FOR;
33096 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33097 {
33098 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33099 const char *p = IDENTIFIER_POINTER (id);
33100
33101 switch (p[0])
33102 {
33103 case 'a':
33104 if (!strcmp ("aligned", p))
33105 result = PRAGMA_OMP_CLAUSE_ALIGNED;
33106 else if (!strcmp ("async", p))
33107 result = PRAGMA_OACC_CLAUSE_ASYNC;
33108 break;
33109 case 'b':
33110 if (!strcmp ("bind", p))
33111 result = PRAGMA_OMP_CLAUSE_BIND;
33112 break;
33113 case 'c':
33114 if (!strcmp ("collapse", p))
33115 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
33116 else if (!strcmp ("copy", p))
33117 result = PRAGMA_OACC_CLAUSE_COPY;
33118 else if (!strcmp ("copyin", p))
33119 result = PRAGMA_OMP_CLAUSE_COPYIN;
33120 else if (!strcmp ("copyout", p))
33121 result = PRAGMA_OACC_CLAUSE_COPYOUT;
33122 else if (!strcmp ("copyprivate", p))
33123 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
33124 else if (!strcmp ("create", p))
33125 result = PRAGMA_OACC_CLAUSE_CREATE;
33126 break;
33127 case 'd':
33128 if (!strcmp ("defaultmap", p))
33129 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
33130 else if (!strcmp ("depend", p))
33131 result = PRAGMA_OMP_CLAUSE_DEPEND;
33132 else if (!strcmp ("device", p))
33133 result = PRAGMA_OMP_CLAUSE_DEVICE;
33134 else if (!strcmp ("deviceptr", p))
33135 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
33136 else if (!strcmp ("device_resident", p))
33137 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
33138 else if (!strcmp ("device_type", p))
33139 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
33140 else if (!strcmp ("dist_schedule", p))
33141 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
33142 break;
33143 case 'f':
33144 if (!strcmp ("final", p))
33145 result = PRAGMA_OMP_CLAUSE_FINAL;
33146 else if (!strcmp ("finalize", p))
33147 result = PRAGMA_OACC_CLAUSE_FINALIZE;
33148 else if (!strcmp ("firstprivate", p))
33149 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
33150 else if (!strcmp ("from", p))
33151 result = PRAGMA_OMP_CLAUSE_FROM;
33152 break;
33153 case 'g':
33154 if (!strcmp ("gang", p))
33155 result = PRAGMA_OACC_CLAUSE_GANG;
33156 else if (!strcmp ("grainsize", p))
33157 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
33158 break;
33159 case 'h':
33160 if (!strcmp ("hint", p))
33161 result = PRAGMA_OMP_CLAUSE_HINT;
33162 else if (!strcmp ("host", p))
33163 result = PRAGMA_OACC_CLAUSE_HOST;
33164 break;
33165 case 'i':
33166 if (!strcmp ("if_present", p))
33167 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
33168 else if (!strcmp ("in_reduction", p))
33169 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
33170 else if (!strcmp ("inbranch", p))
33171 result = PRAGMA_OMP_CLAUSE_INBRANCH;
33172 else if (!strcmp ("independent", p))
33173 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
33174 else if (!strcmp ("is_device_ptr", p))
33175 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
33176 break;
33177 case 'l':
33178 if (!strcmp ("lastprivate", p))
33179 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
33180 else if (!strcmp ("linear", p))
33181 result = PRAGMA_OMP_CLAUSE_LINEAR;
33182 else if (!strcmp ("link", p))
33183 result = PRAGMA_OMP_CLAUSE_LINK;
33184 break;
33185 case 'm':
33186 if (!strcmp ("map", p))
33187 result = PRAGMA_OMP_CLAUSE_MAP;
33188 else if (!strcmp ("mergeable", p))
33189 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
33190 break;
33191 case 'n':
33192 if (!strcmp ("nogroup", p))
33193 result = PRAGMA_OMP_CLAUSE_NOGROUP;
33194 else if (!strcmp ("nontemporal", p))
33195 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
33196 else if (!strcmp ("notinbranch", p))
33197 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
33198 else if (!strcmp ("nowait", p))
33199 result = PRAGMA_OMP_CLAUSE_NOWAIT;
33200 else if (!strcmp ("num_gangs", p))
33201 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
33202 else if (!strcmp ("num_tasks", p))
33203 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
33204 else if (!strcmp ("num_teams", p))
33205 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
33206 else if (!strcmp ("num_threads", p))
33207 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
33208 else if (!strcmp ("num_workers", p))
33209 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
33210 break;
33211 case 'o':
33212 if (!strcmp ("ordered", p))
33213 result = PRAGMA_OMP_CLAUSE_ORDERED;
33214 else if (!strcmp ("order", p))
33215 result = PRAGMA_OMP_CLAUSE_ORDER;
33216 break;
33217 case 'p':
33218 if (!strcmp ("parallel", p))
33219 result = PRAGMA_OMP_CLAUSE_PARALLEL;
33220 else if (!strcmp ("present", p))
33221 result = PRAGMA_OACC_CLAUSE_PRESENT;
33222 else if (!strcmp ("present_or_copy", p)
33223 || !strcmp ("pcopy", p))
33224 result = PRAGMA_OACC_CLAUSE_COPY;
33225 else if (!strcmp ("present_or_copyin", p)
33226 || !strcmp ("pcopyin", p))
33227 result = PRAGMA_OACC_CLAUSE_COPYIN;
33228 else if (!strcmp ("present_or_copyout", p)
33229 || !strcmp ("pcopyout", p))
33230 result = PRAGMA_OACC_CLAUSE_COPYOUT;
33231 else if (!strcmp ("present_or_create", p)
33232 || !strcmp ("pcreate", p))
33233 result = PRAGMA_OACC_CLAUSE_CREATE;
33234 else if (!strcmp ("priority", p))
33235 result = PRAGMA_OMP_CLAUSE_PRIORITY;
33236 else if (!strcmp ("proc_bind", p))
33237 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
33238 break;
33239 case 'r':
33240 if (!strcmp ("reduction", p))
33241 result = PRAGMA_OMP_CLAUSE_REDUCTION;
33242 break;
33243 case 's':
33244 if (!strcmp ("safelen", p))
33245 result = PRAGMA_OMP_CLAUSE_SAFELEN;
33246 else if (!strcmp ("schedule", p))
33247 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
33248 else if (!strcmp ("sections", p))
33249 result = PRAGMA_OMP_CLAUSE_SECTIONS;
33250 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
33251 result = PRAGMA_OACC_CLAUSE_HOST;
33252 else if (!strcmp ("seq", p))
33253 result = PRAGMA_OACC_CLAUSE_SEQ;
33254 else if (!strcmp ("shared", p))
33255 result = PRAGMA_OMP_CLAUSE_SHARED;
33256 else if (!strcmp ("simd", p))
33257 result = PRAGMA_OMP_CLAUSE_SIMD;
33258 else if (!strcmp ("simdlen", p))
33259 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
33260 break;
33261 case 't':
33262 if (!strcmp ("task_reduction", p))
33263 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
33264 else if (!strcmp ("taskgroup", p))
33265 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
33266 else if (!strcmp ("thread_limit", p))
33267 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
33268 else if (!strcmp ("threads", p))
33269 result = PRAGMA_OMP_CLAUSE_THREADS;
33270 else if (!strcmp ("tile", p))
33271 result = PRAGMA_OACC_CLAUSE_TILE;
33272 else if (!strcmp ("to", p))
33273 result = PRAGMA_OMP_CLAUSE_TO;
33274 break;
33275 case 'u':
33276 if (!strcmp ("uniform", p))
33277 result = PRAGMA_OMP_CLAUSE_UNIFORM;
33278 else if (!strcmp ("untied", p))
33279 result = PRAGMA_OMP_CLAUSE_UNTIED;
33280 else if (!strcmp ("use_device", p))
33281 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
33282 else if (!strcmp ("use_device_addr", p))
33283 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
33284 else if (!strcmp ("use_device_ptr", p))
33285 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
33286 break;
33287 case 'v':
33288 if (!strcmp ("vector", p))
33289 result = PRAGMA_OACC_CLAUSE_VECTOR;
33290 else if (!strcmp ("vector_length", p))
33291 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
33292 break;
33293 case 'w':
33294 if (!strcmp ("wait", p))
33295 result = PRAGMA_OACC_CLAUSE_WAIT;
33296 else if (!strcmp ("worker", p))
33297 result = PRAGMA_OACC_CLAUSE_WORKER;
33298 break;
33299 }
33300 }
33301
33302 if (result != PRAGMA_OMP_CLAUSE_NONE)
33303 cp_lexer_consume_token (parser->lexer);
33304
33305 return result;
33306 }
33307
33308 /* Validate that a clause of the given type does not already exist. */
33309
33310 static void
33311 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
33312 const char *name, location_t location)
33313 {
33314 if (omp_find_clause (clauses, code))
33315 error_at (location, "too many %qs clauses", name);
33316 }
33317
33318 /* OpenMP 2.5:
33319 variable-list:
33320 identifier
33321 variable-list , identifier
33322
33323 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
33324 colon). An opening parenthesis will have been consumed by the caller.
33325
33326 If KIND is nonzero, create the appropriate node and install the decl
33327 in OMP_CLAUSE_DECL and add the node to the head of the list.
33328
33329 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
33330 return the list created.
33331
33332 COLON can be NULL if only closing parenthesis should end the list,
33333 or pointer to bool which will receive false if the list is terminated
33334 by closing parenthesis or true if the list is terminated by colon. */
33335
33336 static tree
33337 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
33338 tree list, bool *colon)
33339 {
33340 cp_token *token;
33341 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33342 if (colon)
33343 {
33344 parser->colon_corrects_to_scope_p = false;
33345 *colon = false;
33346 }
33347 while (1)
33348 {
33349 tree name, decl;
33350
33351 if (kind == OMP_CLAUSE_DEPEND)
33352 cp_parser_parse_tentatively (parser);
33353 token = cp_lexer_peek_token (parser->lexer);
33354 if (kind != 0
33355 && current_class_ptr
33356 && cp_parser_is_keyword (token, RID_THIS))
33357 {
33358 decl = finish_this_expr ();
33359 if (TREE_CODE (decl) == NON_LVALUE_EXPR
33360 || CONVERT_EXPR_P (decl))
33361 decl = TREE_OPERAND (decl, 0);
33362 cp_lexer_consume_token (parser->lexer);
33363 }
33364 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
33365 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
33366 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
33367 {
33368 cp_id_kind idk;
33369 decl = cp_parser_primary_expression (parser, false, false, false,
33370 &idk);
33371 }
33372 else
33373 {
33374 name = cp_parser_id_expression (parser, /*template_p=*/false,
33375 /*check_dependency_p=*/true,
33376 /*template_p=*/NULL,
33377 /*declarator_p=*/false,
33378 /*optional_p=*/false);
33379 if (name == error_mark_node)
33380 {
33381 if (kind == OMP_CLAUSE_DEPEND
33382 && cp_parser_simulate_error (parser))
33383 goto depend_lvalue;
33384 goto skip_comma;
33385 }
33386
33387 if (identifier_p (name))
33388 decl = cp_parser_lookup_name_simple (parser, name, token->location);
33389 else
33390 decl = name;
33391 if (decl == error_mark_node)
33392 {
33393 if (kind == OMP_CLAUSE_DEPEND
33394 && cp_parser_simulate_error (parser))
33395 goto depend_lvalue;
33396 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
33397 token->location);
33398 }
33399 }
33400 if (decl == error_mark_node)
33401 ;
33402 else if (kind != 0)
33403 {
33404 switch (kind)
33405 {
33406 case OMP_CLAUSE__CACHE_:
33407 /* The OpenACC cache directive explicitly only allows "array
33408 elements or subarrays". */
33409 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
33410 {
33411 error_at (token->location, "expected %<[%>");
33412 decl = error_mark_node;
33413 break;
33414 }
33415 /* FALLTHROUGH. */
33416 case OMP_CLAUSE_MAP:
33417 case OMP_CLAUSE_FROM:
33418 case OMP_CLAUSE_TO:
33419 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
33420 {
33421 location_t loc
33422 = cp_lexer_peek_token (parser->lexer)->location;
33423 cp_id_kind idk = CP_ID_KIND_NONE;
33424 cp_lexer_consume_token (parser->lexer);
33425 decl = convert_from_reference (decl);
33426 decl
33427 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
33428 decl, false,
33429 &idk, loc);
33430 }
33431 /* FALLTHROUGH. */
33432 case OMP_CLAUSE_DEPEND:
33433 case OMP_CLAUSE_REDUCTION:
33434 case OMP_CLAUSE_IN_REDUCTION:
33435 case OMP_CLAUSE_TASK_REDUCTION:
33436 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
33437 {
33438 tree low_bound = NULL_TREE, length = NULL_TREE;
33439
33440 parser->colon_corrects_to_scope_p = false;
33441 cp_lexer_consume_token (parser->lexer);
33442 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
33443 low_bound = cp_parser_expression (parser);
33444 if (!colon)
33445 parser->colon_corrects_to_scope_p
33446 = saved_colon_corrects_to_scope_p;
33447 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
33448 length = integer_one_node;
33449 else
33450 {
33451 /* Look for `:'. */
33452 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33453 {
33454 if (kind == OMP_CLAUSE_DEPEND
33455 && cp_parser_simulate_error (parser))
33456 goto depend_lvalue;
33457 goto skip_comma;
33458 }
33459 if (kind == OMP_CLAUSE_DEPEND)
33460 cp_parser_commit_to_tentative_parse (parser);
33461 if (!cp_lexer_next_token_is (parser->lexer,
33462 CPP_CLOSE_SQUARE))
33463 length = cp_parser_expression (parser);
33464 }
33465 /* Look for the closing `]'. */
33466 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
33467 RT_CLOSE_SQUARE))
33468 {
33469 if (kind == OMP_CLAUSE_DEPEND
33470 && cp_parser_simulate_error (parser))
33471 goto depend_lvalue;
33472 goto skip_comma;
33473 }
33474
33475 decl = tree_cons (low_bound, length, decl);
33476 }
33477 break;
33478 default:
33479 break;
33480 }
33481
33482 if (kind == OMP_CLAUSE_DEPEND)
33483 {
33484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
33485 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
33486 && cp_parser_simulate_error (parser))
33487 {
33488 depend_lvalue:
33489 cp_parser_abort_tentative_parse (parser);
33490 decl = cp_parser_assignment_expression (parser, NULL,
33491 false, false);
33492 }
33493 else
33494 cp_parser_parse_definitely (parser);
33495 }
33496
33497 tree u = build_omp_clause (token->location, kind);
33498 OMP_CLAUSE_DECL (u) = decl;
33499 OMP_CLAUSE_CHAIN (u) = list;
33500 list = u;
33501 }
33502 else
33503 list = tree_cons (decl, NULL_TREE, list);
33504
33505 get_comma:
33506 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33507 break;
33508 cp_lexer_consume_token (parser->lexer);
33509 }
33510
33511 if (colon)
33512 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33513
33514 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
33515 {
33516 *colon = true;
33517 cp_parser_require (parser, CPP_COLON, RT_COLON);
33518 return list;
33519 }
33520
33521 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33522 {
33523 int ending;
33524
33525 /* Try to resync to an unnested comma. Copied from
33526 cp_parser_parenthesized_expression_list. */
33527 skip_comma:
33528 if (colon)
33529 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33530 ending = cp_parser_skip_to_closing_parenthesis (parser,
33531 /*recovering=*/true,
33532 /*or_comma=*/true,
33533 /*consume_paren=*/true);
33534 if (ending < 0)
33535 goto get_comma;
33536 }
33537
33538 return list;
33539 }
33540
33541 /* Similarly, but expect leading and trailing parenthesis. This is a very
33542 common case for omp clauses. */
33543
33544 static tree
33545 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
33546 {
33547 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33548 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
33549 return list;
33550 }
33551
33552 /* OpenACC 2.0:
33553 copy ( variable-list )
33554 copyin ( variable-list )
33555 copyout ( variable-list )
33556 create ( variable-list )
33557 delete ( variable-list )
33558 present ( variable-list ) */
33559
33560 static tree
33561 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
33562 tree list)
33563 {
33564 enum gomp_map_kind kind;
33565 switch (c_kind)
33566 {
33567 case PRAGMA_OACC_CLAUSE_COPY:
33568 kind = GOMP_MAP_TOFROM;
33569 break;
33570 case PRAGMA_OACC_CLAUSE_COPYIN:
33571 kind = GOMP_MAP_TO;
33572 break;
33573 case PRAGMA_OACC_CLAUSE_COPYOUT:
33574 kind = GOMP_MAP_FROM;
33575 break;
33576 case PRAGMA_OACC_CLAUSE_CREATE:
33577 kind = GOMP_MAP_ALLOC;
33578 break;
33579 case PRAGMA_OACC_CLAUSE_DELETE:
33580 kind = GOMP_MAP_RELEASE;
33581 break;
33582 case PRAGMA_OACC_CLAUSE_DEVICE:
33583 kind = GOMP_MAP_FORCE_TO;
33584 break;
33585 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33586 kind = GOMP_MAP_DEVICE_RESIDENT;
33587 break;
33588 case PRAGMA_OACC_CLAUSE_HOST:
33589 kind = GOMP_MAP_FORCE_FROM;
33590 break;
33591 case PRAGMA_OACC_CLAUSE_LINK:
33592 kind = GOMP_MAP_LINK;
33593 break;
33594 case PRAGMA_OACC_CLAUSE_PRESENT:
33595 kind = GOMP_MAP_FORCE_PRESENT;
33596 break;
33597 default:
33598 gcc_unreachable ();
33599 }
33600 tree nl, c;
33601 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
33602
33603 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
33604 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33605
33606 return nl;
33607 }
33608
33609 /* OpenACC 2.0:
33610 deviceptr ( variable-list ) */
33611
33612 static tree
33613 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
33614 {
33615 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33616 tree vars, t;
33617
33618 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
33619 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
33620 variable-list must only allow for pointer variables. */
33621 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33622 for (t = vars; t; t = TREE_CHAIN (t))
33623 {
33624 tree v = TREE_PURPOSE (t);
33625 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
33626 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
33627 OMP_CLAUSE_DECL (u) = v;
33628 OMP_CLAUSE_CHAIN (u) = list;
33629 list = u;
33630 }
33631
33632 return list;
33633 }
33634
33635 /* OpenACC 2.5:
33636 auto
33637 finalize
33638 independent
33639 nohost
33640 seq */
33641
33642 static tree
33643 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
33644 tree list)
33645 {
33646 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
33647
33648 tree c = build_omp_clause (loc, code);
33649 OMP_CLAUSE_CHAIN (c) = list;
33650
33651 return c;
33652 }
33653
33654 /* OpenACC:
33655 num_gangs ( expression )
33656 num_workers ( expression )
33657 vector_length ( expression ) */
33658
33659 static tree
33660 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
33661 const char *str, tree list)
33662 {
33663 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33664
33665 matching_parens parens;
33666 if (!parens.require_open (parser))
33667 return list;
33668
33669 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
33670
33671 if (t == error_mark_node
33672 || !parens.require_close (parser))
33673 {
33674 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33675 /*or_comma=*/false,
33676 /*consume_paren=*/true);
33677 return list;
33678 }
33679
33680 check_no_duplicate_clause (list, code, str, loc);
33681
33682 tree c = build_omp_clause (loc, code);
33683 OMP_CLAUSE_OPERAND (c, 0) = t;
33684 OMP_CLAUSE_CHAIN (c) = list;
33685 return c;
33686 }
33687
33688 /* OpenACC:
33689
33690 gang [( gang-arg-list )]
33691 worker [( [num:] int-expr )]
33692 vector [( [length:] int-expr )]
33693
33694 where gang-arg is one of:
33695
33696 [num:] int-expr
33697 static: size-expr
33698
33699 and size-expr may be:
33700
33701 *
33702 int-expr
33703 */
33704
33705 static tree
33706 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
33707 omp_clause_code kind,
33708 const char *str, tree list)
33709 {
33710 const char *id = "num";
33711 cp_lexer *lexer = parser->lexer;
33712 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
33713
33714 if (kind == OMP_CLAUSE_VECTOR)
33715 id = "length";
33716
33717 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
33718 {
33719 matching_parens parens;
33720 parens.consume_open (parser);
33721
33722 do
33723 {
33724 cp_token *next = cp_lexer_peek_token (lexer);
33725 int idx = 0;
33726
33727 /* Gang static argument. */
33728 if (kind == OMP_CLAUSE_GANG
33729 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
33730 {
33731 cp_lexer_consume_token (lexer);
33732
33733 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33734 goto cleanup_error;
33735
33736 idx = 1;
33737 if (ops[idx] != NULL)
33738 {
33739 cp_parser_error (parser, "too many %<static%> arguments");
33740 goto cleanup_error;
33741 }
33742
33743 /* Check for the '*' argument. */
33744 if (cp_lexer_next_token_is (lexer, CPP_MULT)
33745 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
33746 || cp_lexer_nth_token_is (parser->lexer, 2,
33747 CPP_CLOSE_PAREN)))
33748 {
33749 cp_lexer_consume_token (lexer);
33750 ops[idx] = integer_minus_one_node;
33751
33752 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
33753 {
33754 cp_lexer_consume_token (lexer);
33755 continue;
33756 }
33757 else break;
33758 }
33759 }
33760 /* Worker num: argument and vector length: arguments. */
33761 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
33762 && id_equal (next->u.value, id)
33763 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
33764 {
33765 cp_lexer_consume_token (lexer); /* id */
33766 cp_lexer_consume_token (lexer); /* ':' */
33767 }
33768
33769 /* Now collect the actual argument. */
33770 if (ops[idx] != NULL_TREE)
33771 {
33772 cp_parser_error (parser, "unexpected argument");
33773 goto cleanup_error;
33774 }
33775
33776 tree expr = cp_parser_assignment_expression (parser, NULL, false,
33777 false);
33778 if (expr == error_mark_node)
33779 goto cleanup_error;
33780
33781 mark_exp_read (expr);
33782 ops[idx] = expr;
33783
33784 if (kind == OMP_CLAUSE_GANG
33785 && cp_lexer_next_token_is (lexer, CPP_COMMA))
33786 {
33787 cp_lexer_consume_token (lexer);
33788 continue;
33789 }
33790 break;
33791 }
33792 while (1);
33793
33794 if (!parens.require_close (parser))
33795 goto cleanup_error;
33796 }
33797
33798 check_no_duplicate_clause (list, kind, str, loc);
33799
33800 c = build_omp_clause (loc, kind);
33801
33802 if (ops[1])
33803 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
33804
33805 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
33806 OMP_CLAUSE_CHAIN (c) = list;
33807
33808 return c;
33809
33810 cleanup_error:
33811 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33812 return list;
33813 }
33814
33815 /* OpenACC 2.0:
33816 tile ( size-expr-list ) */
33817
33818 static tree
33819 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
33820 {
33821 tree c, expr = error_mark_node;
33822 tree tile = NULL_TREE;
33823
33824 /* Collapse and tile are mutually exclusive. (The spec doesn't say
33825 so, but the spec authors never considered such a case and have
33826 differing opinions on what it might mean, including 'not
33827 allowed'.) */
33828 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
33829 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
33830 clause_loc);
33831
33832 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33833 return list;
33834
33835 do
33836 {
33837 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
33838 return list;
33839
33840 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
33841 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
33842 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
33843 {
33844 cp_lexer_consume_token (parser->lexer);
33845 expr = integer_zero_node;
33846 }
33847 else
33848 expr = cp_parser_constant_expression (parser);
33849
33850 tile = tree_cons (NULL_TREE, expr, tile);
33851 }
33852 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
33853
33854 /* Consume the trailing ')'. */
33855 cp_lexer_consume_token (parser->lexer);
33856
33857 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
33858 tile = nreverse (tile);
33859 OMP_CLAUSE_TILE_LIST (c) = tile;
33860 OMP_CLAUSE_CHAIN (c) = list;
33861 return c;
33862 }
33863
33864 /* OpenACC 2.0
33865 Parse wait clause or directive parameters. */
33866
33867 static tree
33868 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
33869 {
33870 vec<tree, va_gc> *args;
33871 tree t, args_tree;
33872
33873 args = cp_parser_parenthesized_expression_list (parser, non_attr,
33874 /*cast_p=*/false,
33875 /*allow_expansion_p=*/true,
33876 /*non_constant_p=*/NULL);
33877
33878 if (args == NULL || args->length () == 0)
33879 {
33880 if (args != NULL)
33881 {
33882 cp_parser_error (parser, "expected integer expression list");
33883 release_tree_vector (args);
33884 }
33885 return list;
33886 }
33887
33888 args_tree = build_tree_list_vec (args);
33889
33890 release_tree_vector (args);
33891
33892 for (t = args_tree; t; t = TREE_CHAIN (t))
33893 {
33894 tree targ = TREE_VALUE (t);
33895
33896 if (targ != error_mark_node)
33897 {
33898 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
33899 error ("%<wait%> expression must be integral");
33900 else
33901 {
33902 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
33903
33904 targ = mark_rvalue_use (targ);
33905 OMP_CLAUSE_DECL (c) = targ;
33906 OMP_CLAUSE_CHAIN (c) = list;
33907 list = c;
33908 }
33909 }
33910 }
33911
33912 return list;
33913 }
33914
33915 /* OpenACC:
33916 wait [( int-expr-list )] */
33917
33918 static tree
33919 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
33920 {
33921 location_t location = cp_lexer_peek_token (parser->lexer)->location;
33922
33923 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33924 list = cp_parser_oacc_wait_list (parser, location, list);
33925 else
33926 {
33927 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
33928
33929 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33930 OMP_CLAUSE_CHAIN (c) = list;
33931 list = c;
33932 }
33933
33934 return list;
33935 }
33936
33937 /* OpenMP 3.0:
33938 collapse ( constant-expression ) */
33939
33940 static tree
33941 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
33942 {
33943 tree c, num;
33944 location_t loc;
33945 HOST_WIDE_INT n;
33946
33947 loc = cp_lexer_peek_token (parser->lexer)->location;
33948 matching_parens parens;
33949 if (!parens.require_open (parser))
33950 return list;
33951
33952 num = cp_parser_constant_expression (parser);
33953
33954 if (!parens.require_close (parser))
33955 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33956 /*or_comma=*/false,
33957 /*consume_paren=*/true);
33958
33959 if (num == error_mark_node)
33960 return list;
33961 num = fold_non_dependent_expr (num);
33962 if (!tree_fits_shwi_p (num)
33963 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33964 || (n = tree_to_shwi (num)) <= 0
33965 || (int) n != n)
33966 {
33967 error_at (loc, "collapse argument needs positive constant integer expression");
33968 return list;
33969 }
33970
33971 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
33972 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
33973 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
33974 OMP_CLAUSE_CHAIN (c) = list;
33975 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
33976
33977 return c;
33978 }
33979
33980 /* OpenMP 2.5:
33981 default ( none | shared )
33982
33983 OpenACC:
33984 default ( none | present ) */
33985
33986 static tree
33987 cp_parser_omp_clause_default (cp_parser *parser, tree list,
33988 location_t location, bool is_oacc)
33989 {
33990 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
33991 tree c;
33992
33993 matching_parens parens;
33994 if (!parens.require_open (parser))
33995 return list;
33996 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33997 {
33998 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33999 const char *p = IDENTIFIER_POINTER (id);
34000
34001 switch (p[0])
34002 {
34003 case 'n':
34004 if (strcmp ("none", p) != 0)
34005 goto invalid_kind;
34006 kind = OMP_CLAUSE_DEFAULT_NONE;
34007 break;
34008
34009 case 'p':
34010 if (strcmp ("present", p) != 0 || !is_oacc)
34011 goto invalid_kind;
34012 kind = OMP_CLAUSE_DEFAULT_PRESENT;
34013 break;
34014
34015 case 's':
34016 if (strcmp ("shared", p) != 0 || is_oacc)
34017 goto invalid_kind;
34018 kind = OMP_CLAUSE_DEFAULT_SHARED;
34019 break;
34020
34021 default:
34022 goto invalid_kind;
34023 }
34024
34025 cp_lexer_consume_token (parser->lexer);
34026 }
34027 else
34028 {
34029 invalid_kind:
34030 if (is_oacc)
34031 cp_parser_error (parser, "expected %<none%> or %<present%>");
34032 else
34033 cp_parser_error (parser, "expected %<none%> or %<shared%>");
34034 }
34035
34036 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
34037 || !parens.require_close (parser))
34038 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34039 /*or_comma=*/false,
34040 /*consume_paren=*/true);
34041
34042 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
34043 return list;
34044
34045 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
34046 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
34047 OMP_CLAUSE_CHAIN (c) = list;
34048 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
34049
34050 return c;
34051 }
34052
34053 /* OpenMP 3.1:
34054 final ( expression ) */
34055
34056 static tree
34057 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
34058 {
34059 tree t, c;
34060
34061 matching_parens parens;
34062 if (!parens.require_open (parser))
34063 return list;
34064
34065 t = cp_parser_assignment_expression (parser);
34066
34067 if (t == error_mark_node
34068 || !parens.require_close (parser))
34069 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34070 /*or_comma=*/false,
34071 /*consume_paren=*/true);
34072
34073 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
34074
34075 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
34076 OMP_CLAUSE_FINAL_EXPR (c) = t;
34077 OMP_CLAUSE_CHAIN (c) = list;
34078
34079 return c;
34080 }
34081
34082 /* OpenMP 2.5:
34083 if ( expression )
34084
34085 OpenMP 4.5:
34086 if ( directive-name-modifier : expression )
34087
34088 directive-name-modifier:
34089 parallel | task | taskloop | target data | target | target update
34090 | target enter data | target exit data
34091
34092 OpenMP 5.0:
34093 directive-name-modifier:
34094 ... | simd | cancel */
34095
34096 static tree
34097 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
34098 bool is_omp)
34099 {
34100 tree t, c;
34101 enum tree_code if_modifier = ERROR_MARK;
34102
34103 matching_parens parens;
34104 if (!parens.require_open (parser))
34105 return list;
34106
34107 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34108 {
34109 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34110 const char *p = IDENTIFIER_POINTER (id);
34111 int n = 2;
34112
34113 if (strcmp ("cancel", p) == 0)
34114 if_modifier = VOID_CST;
34115 else if (strcmp ("parallel", p) == 0)
34116 if_modifier = OMP_PARALLEL;
34117 else if (strcmp ("simd", p) == 0)
34118 if_modifier = OMP_SIMD;
34119 else if (strcmp ("task", p) == 0)
34120 if_modifier = OMP_TASK;
34121 else if (strcmp ("taskloop", p) == 0)
34122 if_modifier = OMP_TASKLOOP;
34123 else if (strcmp ("target", p) == 0)
34124 {
34125 if_modifier = OMP_TARGET;
34126 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
34127 {
34128 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
34129 p = IDENTIFIER_POINTER (id);
34130 if (strcmp ("data", p) == 0)
34131 if_modifier = OMP_TARGET_DATA;
34132 else if (strcmp ("update", p) == 0)
34133 if_modifier = OMP_TARGET_UPDATE;
34134 else if (strcmp ("enter", p) == 0)
34135 if_modifier = OMP_TARGET_ENTER_DATA;
34136 else if (strcmp ("exit", p) == 0)
34137 if_modifier = OMP_TARGET_EXIT_DATA;
34138 if (if_modifier != OMP_TARGET)
34139 n = 3;
34140 else
34141 {
34142 location_t loc
34143 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
34144 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
34145 "or %<exit%>");
34146 if_modifier = ERROR_MARK;
34147 }
34148 if (if_modifier == OMP_TARGET_ENTER_DATA
34149 || if_modifier == OMP_TARGET_EXIT_DATA)
34150 {
34151 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
34152 {
34153 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
34154 p = IDENTIFIER_POINTER (id);
34155 if (strcmp ("data", p) == 0)
34156 n = 4;
34157 }
34158 if (n != 4)
34159 {
34160 location_t loc
34161 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
34162 error_at (loc, "expected %<data%>");
34163 if_modifier = ERROR_MARK;
34164 }
34165 }
34166 }
34167 }
34168 if (if_modifier != ERROR_MARK)
34169 {
34170 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
34171 {
34172 while (n-- > 0)
34173 cp_lexer_consume_token (parser->lexer);
34174 }
34175 else
34176 {
34177 if (n > 2)
34178 {
34179 location_t loc
34180 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
34181 error_at (loc, "expected %<:%>");
34182 }
34183 if_modifier = ERROR_MARK;
34184 }
34185 }
34186 }
34187
34188 t = cp_parser_assignment_expression (parser);
34189
34190 if (t == error_mark_node
34191 || !parens.require_close (parser))
34192 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34193 /*or_comma=*/false,
34194 /*consume_paren=*/true);
34195
34196 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
34197 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
34198 {
34199 if (if_modifier != ERROR_MARK
34200 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
34201 {
34202 const char *p = NULL;
34203 switch (if_modifier)
34204 {
34205 case VOID_CST: p = "cancel"; break;
34206 case OMP_PARALLEL: p = "parallel"; break;
34207 case OMP_SIMD: p = "simd"; break;
34208 case OMP_TASK: p = "task"; break;
34209 case OMP_TASKLOOP: p = "taskloop"; break;
34210 case OMP_TARGET_DATA: p = "target data"; break;
34211 case OMP_TARGET: p = "target"; break;
34212 case OMP_TARGET_UPDATE: p = "target update"; break;
34213 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
34214 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
34215 default: gcc_unreachable ();
34216 }
34217 error_at (location, "too many %<if%> clauses with %qs modifier",
34218 p);
34219 return list;
34220 }
34221 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
34222 {
34223 if (!is_omp)
34224 error_at (location, "too many %<if%> clauses");
34225 else
34226 error_at (location, "too many %<if%> clauses without modifier");
34227 return list;
34228 }
34229 else if (if_modifier == ERROR_MARK
34230 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
34231 {
34232 error_at (location, "if any %<if%> clause has modifier, then all "
34233 "%<if%> clauses have to use modifier");
34234 return list;
34235 }
34236 }
34237
34238 c = build_omp_clause (location, OMP_CLAUSE_IF);
34239 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
34240 OMP_CLAUSE_IF_EXPR (c) = t;
34241 OMP_CLAUSE_CHAIN (c) = list;
34242
34243 return c;
34244 }
34245
34246 /* OpenMP 3.1:
34247 mergeable */
34248
34249 static tree
34250 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
34251 tree list, location_t location)
34252 {
34253 tree c;
34254
34255 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
34256 location);
34257
34258 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
34259 OMP_CLAUSE_CHAIN (c) = list;
34260 return c;
34261 }
34262
34263 /* OpenMP 2.5:
34264 nowait */
34265
34266 static tree
34267 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
34268 tree list, location_t location)
34269 {
34270 tree c;
34271
34272 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
34273
34274 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
34275 OMP_CLAUSE_CHAIN (c) = list;
34276 return c;
34277 }
34278
34279 /* OpenMP 2.5:
34280 num_threads ( expression ) */
34281
34282 static tree
34283 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
34284 location_t location)
34285 {
34286 tree t, c;
34287
34288 matching_parens parens;
34289 if (!parens.require_open (parser))
34290 return list;
34291
34292 t = cp_parser_assignment_expression (parser);
34293
34294 if (t == error_mark_node
34295 || !parens.require_close (parser))
34296 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34297 /*or_comma=*/false,
34298 /*consume_paren=*/true);
34299
34300 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
34301 "num_threads", location);
34302
34303 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
34304 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
34305 OMP_CLAUSE_CHAIN (c) = list;
34306
34307 return c;
34308 }
34309
34310 /* OpenMP 4.5:
34311 num_tasks ( expression ) */
34312
34313 static tree
34314 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
34315 location_t location)
34316 {
34317 tree t, c;
34318
34319 matching_parens parens;
34320 if (!parens.require_open (parser))
34321 return list;
34322
34323 t = cp_parser_assignment_expression (parser);
34324
34325 if (t == error_mark_node
34326 || !parens.require_close (parser))
34327 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34328 /*or_comma=*/false,
34329 /*consume_paren=*/true);
34330
34331 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
34332 "num_tasks", location);
34333
34334 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
34335 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
34336 OMP_CLAUSE_CHAIN (c) = list;
34337
34338 return c;
34339 }
34340
34341 /* OpenMP 4.5:
34342 grainsize ( expression ) */
34343
34344 static tree
34345 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
34346 location_t location)
34347 {
34348 tree t, c;
34349
34350 matching_parens parens;
34351 if (!parens.require_open (parser))
34352 return list;
34353
34354 t = cp_parser_assignment_expression (parser);
34355
34356 if (t == error_mark_node
34357 || !parens.require_close (parser))
34358 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34359 /*or_comma=*/false,
34360 /*consume_paren=*/true);
34361
34362 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
34363 "grainsize", location);
34364
34365 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
34366 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
34367 OMP_CLAUSE_CHAIN (c) = list;
34368
34369 return c;
34370 }
34371
34372 /* OpenMP 4.5:
34373 priority ( expression ) */
34374
34375 static tree
34376 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
34377 location_t location)
34378 {
34379 tree t, c;
34380
34381 matching_parens parens;
34382 if (!parens.require_open (parser))
34383 return list;
34384
34385 t = cp_parser_assignment_expression (parser);
34386
34387 if (t == error_mark_node
34388 || !parens.require_close (parser))
34389 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34390 /*or_comma=*/false,
34391 /*consume_paren=*/true);
34392
34393 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
34394 "priority", location);
34395
34396 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
34397 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
34398 OMP_CLAUSE_CHAIN (c) = list;
34399
34400 return c;
34401 }
34402
34403 /* OpenMP 4.5:
34404 hint ( expression ) */
34405
34406 static tree
34407 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
34408 {
34409 tree t, c;
34410
34411 matching_parens parens;
34412 if (!parens.require_open (parser))
34413 return list;
34414
34415 t = cp_parser_assignment_expression (parser);
34416
34417 if (t == error_mark_node
34418 || !parens.require_close (parser))
34419 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34420 /*or_comma=*/false,
34421 /*consume_paren=*/true);
34422
34423 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
34424
34425 c = build_omp_clause (location, OMP_CLAUSE_HINT);
34426 OMP_CLAUSE_HINT_EXPR (c) = t;
34427 OMP_CLAUSE_CHAIN (c) = list;
34428
34429 return c;
34430 }
34431
34432 /* OpenMP 4.5:
34433 defaultmap ( tofrom : scalar )
34434
34435 OpenMP 5.0:
34436 defaultmap ( implicit-behavior [ : variable-category ] ) */
34437
34438 static tree
34439 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
34440 location_t location)
34441 {
34442 tree c, id;
34443 const char *p;
34444 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
34445 enum omp_clause_defaultmap_kind category
34446 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
34447
34448 matching_parens parens;
34449 if (!parens.require_open (parser))
34450 return list;
34451
34452 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
34453 p = "default";
34454 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34455 {
34456 invalid_behavior:
34457 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
34458 "%<tofrom%>, %<firstprivate%>, %<none%> "
34459 "or %<default%>");
34460 goto out_err;
34461 }
34462 else
34463 {
34464 id = cp_lexer_peek_token (parser->lexer)->u.value;
34465 p = IDENTIFIER_POINTER (id);
34466 }
34467
34468 switch (p[0])
34469 {
34470 case 'a':
34471 if (strcmp ("alloc", p) == 0)
34472 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
34473 else
34474 goto invalid_behavior;
34475 break;
34476
34477 case 'd':
34478 if (strcmp ("default", p) == 0)
34479 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
34480 else
34481 goto invalid_behavior;
34482 break;
34483
34484 case 'f':
34485 if (strcmp ("firstprivate", p) == 0)
34486 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
34487 else if (strcmp ("from", p) == 0)
34488 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
34489 else
34490 goto invalid_behavior;
34491 break;
34492
34493 case 'n':
34494 if (strcmp ("none", p) == 0)
34495 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
34496 else
34497 goto invalid_behavior;
34498 break;
34499
34500 case 't':
34501 if (strcmp ("tofrom", p) == 0)
34502 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
34503 else if (strcmp ("to", p) == 0)
34504 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
34505 else
34506 goto invalid_behavior;
34507 break;
34508
34509 default:
34510 goto invalid_behavior;
34511 }
34512 cp_lexer_consume_token (parser->lexer);
34513
34514 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34515 {
34516 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34517 goto out_err;
34518
34519 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34520 {
34521 invalid_category:
34522 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
34523 "%<pointer%>");
34524 goto out_err;
34525 }
34526 id = cp_lexer_peek_token (parser->lexer)->u.value;
34527 p = IDENTIFIER_POINTER (id);
34528
34529 switch (p[0])
34530 {
34531 case 'a':
34532 if (strcmp ("aggregate", p) == 0)
34533 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
34534 else
34535 goto invalid_category;
34536 break;
34537
34538 case 'p':
34539 if (strcmp ("pointer", p) == 0)
34540 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
34541 else
34542 goto invalid_category;
34543 break;
34544
34545 case 's':
34546 if (strcmp ("scalar", p) == 0)
34547 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
34548 else
34549 goto invalid_category;
34550 break;
34551
34552 default:
34553 goto invalid_category;
34554 }
34555
34556 cp_lexer_consume_token (parser->lexer);
34557 }
34558 if (!parens.require_close (parser))
34559 goto out_err;
34560
34561 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
34562 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
34563 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
34564 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
34565 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
34566 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
34567 {
34568 enum omp_clause_defaultmap_kind cat = category;
34569 location_t loc = OMP_CLAUSE_LOCATION (c);
34570 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
34571 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
34572 p = NULL;
34573 switch (cat)
34574 {
34575 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
34576 p = NULL;
34577 break;
34578 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
34579 p = "aggregate";
34580 break;
34581 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
34582 p = "pointer";
34583 break;
34584 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
34585 p = "scalar";
34586 break;
34587 default:
34588 gcc_unreachable ();
34589 }
34590 if (p)
34591 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
34592 p);
34593 else
34594 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
34595 "category");
34596 break;
34597 }
34598
34599 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
34600 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
34601 OMP_CLAUSE_CHAIN (c) = list;
34602 return c;
34603
34604 out_err:
34605 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34606 /*or_comma=*/false,
34607 /*consume_paren=*/true);
34608 return list;
34609 }
34610
34611 /* OpenMP 5.0:
34612 order ( concurrent ) */
34613
34614 static tree
34615 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
34616 {
34617 tree c, id;
34618 const char *p;
34619
34620 matching_parens parens;
34621 if (!parens.require_open (parser))
34622 return list;
34623
34624 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34625 {
34626 cp_parser_error (parser, "expected %<concurrent%>");
34627 goto out_err;
34628 }
34629 else
34630 {
34631 id = cp_lexer_peek_token (parser->lexer)->u.value;
34632 p = IDENTIFIER_POINTER (id);
34633 }
34634 if (strcmp (p, "concurrent") != 0)
34635 {
34636 cp_parser_error (parser, "expected %<concurrent%>");
34637 goto out_err;
34638 }
34639 cp_lexer_consume_token (parser->lexer);
34640 if (!parens.require_close (parser))
34641 goto out_err;
34642
34643 /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location); */
34644 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
34645 OMP_CLAUSE_CHAIN (c) = list;
34646 return c;
34647
34648 out_err:
34649 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34650 /*or_comma=*/false,
34651 /*consume_paren=*/true);
34652 return list;
34653 }
34654
34655 /* OpenMP 5.0:
34656 bind ( teams | parallel | thread ) */
34657
34658 static tree
34659 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
34660 location_t location)
34661 {
34662 tree c;
34663 const char *p;
34664 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
34665
34666 matching_parens parens;
34667 if (!parens.require_open (parser))
34668 return list;
34669
34670 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34671 {
34672 invalid:
34673 cp_parser_error (parser,
34674 "expected %<teams%>, %<parallel%> or %<thread%>");
34675 goto out_err;
34676 }
34677 else
34678 {
34679 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34680 p = IDENTIFIER_POINTER (id);
34681 }
34682 if (strcmp (p, "teams") == 0)
34683 kind = OMP_CLAUSE_BIND_TEAMS;
34684 else if (strcmp (p, "parallel") == 0)
34685 kind = OMP_CLAUSE_BIND_PARALLEL;
34686 else if (strcmp (p, "thread") != 0)
34687 goto invalid;
34688 cp_lexer_consume_token (parser->lexer);
34689 if (!parens.require_close (parser))
34690 goto out_err;
34691
34692 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
34693 c = build_omp_clause (location, OMP_CLAUSE_BIND);
34694 OMP_CLAUSE_BIND_KIND (c) = kind;
34695 OMP_CLAUSE_CHAIN (c) = list;
34696 return c;
34697
34698 out_err:
34699 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34700 /*or_comma=*/false,
34701 /*consume_paren=*/true);
34702 return list;
34703 }
34704
34705 /* OpenMP 2.5:
34706 ordered
34707
34708 OpenMP 4.5:
34709 ordered ( constant-expression ) */
34710
34711 static tree
34712 cp_parser_omp_clause_ordered (cp_parser *parser,
34713 tree list, location_t location)
34714 {
34715 tree c, num = NULL_TREE;
34716 HOST_WIDE_INT n;
34717
34718 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
34719 "ordered", location);
34720
34721 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34722 {
34723 matching_parens parens;
34724 parens.consume_open (parser);
34725
34726 num = cp_parser_constant_expression (parser);
34727
34728 if (!parens.require_close (parser))
34729 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34730 /*or_comma=*/false,
34731 /*consume_paren=*/true);
34732
34733 if (num == error_mark_node)
34734 return list;
34735 num = fold_non_dependent_expr (num);
34736 if (!tree_fits_shwi_p (num)
34737 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
34738 || (n = tree_to_shwi (num)) <= 0
34739 || (int) n != n)
34740 {
34741 error_at (location,
34742 "ordered argument needs positive constant integer "
34743 "expression");
34744 return list;
34745 }
34746 }
34747
34748 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
34749 OMP_CLAUSE_ORDERED_EXPR (c) = num;
34750 OMP_CLAUSE_CHAIN (c) = list;
34751 return c;
34752 }
34753
34754 /* OpenMP 2.5:
34755 reduction ( reduction-operator : variable-list )
34756
34757 reduction-operator:
34758 One of: + * - & ^ | && ||
34759
34760 OpenMP 3.1:
34761
34762 reduction-operator:
34763 One of: + * - & ^ | && || min max
34764
34765 OpenMP 4.0:
34766
34767 reduction-operator:
34768 One of: + * - & ^ | && ||
34769 id-expression
34770
34771 OpenMP 5.0:
34772 reduction ( reduction-modifier, reduction-operator : variable-list )
34773 in_reduction ( reduction-operator : variable-list )
34774 task_reduction ( reduction-operator : variable-list ) */
34775
34776 static tree
34777 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
34778 bool is_omp, tree list)
34779 {
34780 enum tree_code code = ERROR_MARK;
34781 tree nlist, c, id = NULL_TREE;
34782 bool task = false;
34783 bool inscan = false;
34784
34785 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34786 return list;
34787
34788 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
34789 {
34790 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
34791 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
34792 {
34793 cp_lexer_consume_token (parser->lexer);
34794 cp_lexer_consume_token (parser->lexer);
34795 }
34796 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34797 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
34798 {
34799 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34800 const char *p = IDENTIFIER_POINTER (id);
34801 if (strcmp (p, "task") == 0)
34802 task = true;
34803 else if (strcmp (p, "inscan") == 0)
34804 inscan = true;
34805 if (task || inscan)
34806 {
34807 cp_lexer_consume_token (parser->lexer);
34808 cp_lexer_consume_token (parser->lexer);
34809 }
34810 }
34811 }
34812
34813 switch (cp_lexer_peek_token (parser->lexer)->type)
34814 {
34815 case CPP_PLUS: code = PLUS_EXPR; break;
34816 case CPP_MULT: code = MULT_EXPR; break;
34817 case CPP_MINUS: code = MINUS_EXPR; break;
34818 case CPP_AND: code = BIT_AND_EXPR; break;
34819 case CPP_XOR: code = BIT_XOR_EXPR; break;
34820 case CPP_OR: code = BIT_IOR_EXPR; break;
34821 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
34822 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
34823 default: break;
34824 }
34825
34826 if (code != ERROR_MARK)
34827 cp_lexer_consume_token (parser->lexer);
34828 else
34829 {
34830 bool saved_colon_corrects_to_scope_p;
34831 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
34832 parser->colon_corrects_to_scope_p = false;
34833 id = cp_parser_id_expression (parser, /*template_p=*/false,
34834 /*check_dependency_p=*/true,
34835 /*template_p=*/NULL,
34836 /*declarator_p=*/false,
34837 /*optional_p=*/false);
34838 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34839 if (identifier_p (id))
34840 {
34841 const char *p = IDENTIFIER_POINTER (id);
34842
34843 if (strcmp (p, "min") == 0)
34844 code = MIN_EXPR;
34845 else if (strcmp (p, "max") == 0)
34846 code = MAX_EXPR;
34847 else if (id == ovl_op_identifier (false, PLUS_EXPR))
34848 code = PLUS_EXPR;
34849 else if (id == ovl_op_identifier (false, MULT_EXPR))
34850 code = MULT_EXPR;
34851 else if (id == ovl_op_identifier (false, MINUS_EXPR))
34852 code = MINUS_EXPR;
34853 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
34854 code = BIT_AND_EXPR;
34855 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
34856 code = BIT_IOR_EXPR;
34857 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
34858 code = BIT_XOR_EXPR;
34859 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
34860 code = TRUTH_ANDIF_EXPR;
34861 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
34862 code = TRUTH_ORIF_EXPR;
34863 id = omp_reduction_id (code, id, NULL_TREE);
34864 tree scope = parser->scope;
34865 if (scope)
34866 id = build_qualified_name (NULL_TREE, scope, id, false);
34867 parser->scope = NULL_TREE;
34868 parser->qualifying_scope = NULL_TREE;
34869 parser->object_scope = NULL_TREE;
34870 }
34871 else
34872 {
34873 error ("invalid reduction-identifier");
34874 resync_fail:
34875 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34876 /*or_comma=*/false,
34877 /*consume_paren=*/true);
34878 return list;
34879 }
34880 }
34881
34882 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34883 goto resync_fail;
34884
34885 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
34886 NULL);
34887 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34888 {
34889 OMP_CLAUSE_REDUCTION_CODE (c) = code;
34890 if (task)
34891 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
34892 else if (inscan)
34893 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
34894 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
34895 }
34896
34897 return nlist;
34898 }
34899
34900 /* OpenMP 2.5:
34901 schedule ( schedule-kind )
34902 schedule ( schedule-kind , expression )
34903
34904 schedule-kind:
34905 static | dynamic | guided | runtime | auto
34906
34907 OpenMP 4.5:
34908 schedule ( schedule-modifier : schedule-kind )
34909 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
34910
34911 schedule-modifier:
34912 simd
34913 monotonic
34914 nonmonotonic */
34915
34916 static tree
34917 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
34918 {
34919 tree c, t;
34920 int modifiers = 0, nmodifiers = 0;
34921
34922 matching_parens parens;
34923 if (!parens.require_open (parser))
34924 return list;
34925
34926 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
34927
34928 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34929 {
34930 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34931 const char *p = IDENTIFIER_POINTER (id);
34932 if (strcmp ("simd", p) == 0)
34933 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
34934 else if (strcmp ("monotonic", p) == 0)
34935 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
34936 else if (strcmp ("nonmonotonic", p) == 0)
34937 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
34938 else
34939 break;
34940 cp_lexer_consume_token (parser->lexer);
34941 if (nmodifiers++ == 0
34942 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34943 cp_lexer_consume_token (parser->lexer);
34944 else
34945 {
34946 cp_parser_require (parser, CPP_COLON, RT_COLON);
34947 break;
34948 }
34949 }
34950
34951 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34952 {
34953 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34954 const char *p = IDENTIFIER_POINTER (id);
34955
34956 switch (p[0])
34957 {
34958 case 'd':
34959 if (strcmp ("dynamic", p) != 0)
34960 goto invalid_kind;
34961 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
34962 break;
34963
34964 case 'g':
34965 if (strcmp ("guided", p) != 0)
34966 goto invalid_kind;
34967 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
34968 break;
34969
34970 case 'r':
34971 if (strcmp ("runtime", p) != 0)
34972 goto invalid_kind;
34973 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
34974 break;
34975
34976 default:
34977 goto invalid_kind;
34978 }
34979 }
34980 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34981 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
34982 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
34983 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
34984 else
34985 goto invalid_kind;
34986 cp_lexer_consume_token (parser->lexer);
34987
34988 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
34989 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
34990 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
34991 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
34992 {
34993 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
34994 "specified");
34995 modifiers = 0;
34996 }
34997
34998 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34999 {
35000 cp_token *token;
35001 cp_lexer_consume_token (parser->lexer);
35002
35003 token = cp_lexer_peek_token (parser->lexer);
35004 t = cp_parser_assignment_expression (parser);
35005
35006 if (t == error_mark_node)
35007 goto resync_fail;
35008 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
35009 error_at (token->location, "schedule %<runtime%> does not take "
35010 "a %<chunk_size%> parameter");
35011 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
35012 error_at (token->location, "schedule %<auto%> does not take "
35013 "a %<chunk_size%> parameter");
35014 else
35015 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
35016
35017 if (!parens.require_close (parser))
35018 goto resync_fail;
35019 }
35020 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
35021 goto resync_fail;
35022
35023 OMP_CLAUSE_SCHEDULE_KIND (c)
35024 = (enum omp_clause_schedule_kind)
35025 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
35026
35027 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
35028 OMP_CLAUSE_CHAIN (c) = list;
35029 return c;
35030
35031 invalid_kind:
35032 cp_parser_error (parser, "invalid schedule kind");
35033 resync_fail:
35034 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35035 /*or_comma=*/false,
35036 /*consume_paren=*/true);
35037 return list;
35038 }
35039
35040 /* OpenMP 3.0:
35041 untied */
35042
35043 static tree
35044 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
35045 tree list, location_t location)
35046 {
35047 tree c;
35048
35049 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
35050
35051 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
35052 OMP_CLAUSE_CHAIN (c) = list;
35053 return c;
35054 }
35055
35056 /* OpenMP 4.0:
35057 inbranch
35058 notinbranch */
35059
35060 static tree
35061 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
35062 tree list, location_t location)
35063 {
35064 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
35065 tree c = build_omp_clause (location, code);
35066 OMP_CLAUSE_CHAIN (c) = list;
35067 return c;
35068 }
35069
35070 /* OpenMP 4.0:
35071 parallel
35072 for
35073 sections
35074 taskgroup */
35075
35076 static tree
35077 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
35078 enum omp_clause_code code,
35079 tree list, location_t location)
35080 {
35081 tree c = build_omp_clause (location, code);
35082 OMP_CLAUSE_CHAIN (c) = list;
35083 return c;
35084 }
35085
35086 /* OpenMP 4.5:
35087 nogroup */
35088
35089 static tree
35090 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
35091 tree list, location_t location)
35092 {
35093 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
35094 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
35095 OMP_CLAUSE_CHAIN (c) = list;
35096 return c;
35097 }
35098
35099 /* OpenMP 4.5:
35100 simd
35101 threads */
35102
35103 static tree
35104 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
35105 enum omp_clause_code code,
35106 tree list, location_t location)
35107 {
35108 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
35109 tree c = build_omp_clause (location, code);
35110 OMP_CLAUSE_CHAIN (c) = list;
35111 return c;
35112 }
35113
35114 /* OpenMP 4.0:
35115 num_teams ( expression ) */
35116
35117 static tree
35118 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
35119 location_t location)
35120 {
35121 tree t, c;
35122
35123 matching_parens parens;
35124 if (!parens.require_open (parser))
35125 return list;
35126
35127 t = cp_parser_assignment_expression (parser);
35128
35129 if (t == error_mark_node
35130 || !parens.require_close (parser))
35131 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35132 /*or_comma=*/false,
35133 /*consume_paren=*/true);
35134
35135 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
35136 "num_teams", location);
35137
35138 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
35139 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
35140 OMP_CLAUSE_CHAIN (c) = list;
35141
35142 return c;
35143 }
35144
35145 /* OpenMP 4.0:
35146 thread_limit ( expression ) */
35147
35148 static tree
35149 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
35150 location_t location)
35151 {
35152 tree t, c;
35153
35154 matching_parens parens;
35155 if (!parens.require_open (parser))
35156 return list;
35157
35158 t = cp_parser_assignment_expression (parser);
35159
35160 if (t == error_mark_node
35161 || !parens.require_close (parser))
35162 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35163 /*or_comma=*/false,
35164 /*consume_paren=*/true);
35165
35166 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
35167 "thread_limit", location);
35168
35169 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
35170 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
35171 OMP_CLAUSE_CHAIN (c) = list;
35172
35173 return c;
35174 }
35175
35176 /* OpenMP 4.0:
35177 aligned ( variable-list )
35178 aligned ( variable-list : constant-expression ) */
35179
35180 static tree
35181 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
35182 {
35183 tree nlist, c, alignment = NULL_TREE;
35184 bool colon;
35185
35186 matching_parens parens;
35187 if (!parens.require_open (parser))
35188 return list;
35189
35190 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
35191 &colon);
35192
35193 if (colon)
35194 {
35195 alignment = cp_parser_constant_expression (parser);
35196
35197 if (!parens.require_close (parser))
35198 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35199 /*or_comma=*/false,
35200 /*consume_paren=*/true);
35201
35202 if (alignment == error_mark_node)
35203 alignment = NULL_TREE;
35204 }
35205
35206 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
35207 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
35208
35209 return nlist;
35210 }
35211
35212 /* OpenMP 2.5:
35213 lastprivate ( variable-list )
35214
35215 OpenMP 5.0:
35216 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
35217
35218 static tree
35219 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
35220 {
35221 bool conditional = false;
35222
35223 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35224 return list;
35225
35226 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
35227 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
35228 {
35229 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35230 const char *p = IDENTIFIER_POINTER (id);
35231
35232 if (strcmp ("conditional", p) == 0)
35233 {
35234 conditional = true;
35235 cp_lexer_consume_token (parser->lexer);
35236 cp_lexer_consume_token (parser->lexer);
35237 }
35238 }
35239
35240 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
35241 list, NULL);
35242
35243 if (conditional)
35244 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
35245 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
35246 return nlist;
35247 }
35248
35249 /* OpenMP 4.0:
35250 linear ( variable-list )
35251 linear ( variable-list : expression )
35252
35253 OpenMP 4.5:
35254 linear ( modifier ( variable-list ) )
35255 linear ( modifier ( variable-list ) : expression ) */
35256
35257 static tree
35258 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
35259 bool declare_simd)
35260 {
35261 tree nlist, c, step = integer_one_node;
35262 bool colon;
35263 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
35264
35265 matching_parens parens;
35266 if (!parens.require_open (parser))
35267 return list;
35268
35269 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35270 {
35271 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35272 const char *p = IDENTIFIER_POINTER (id);
35273
35274 if (strcmp ("ref", p) == 0)
35275 kind = OMP_CLAUSE_LINEAR_REF;
35276 else if (strcmp ("val", p) == 0)
35277 kind = OMP_CLAUSE_LINEAR_VAL;
35278 else if (strcmp ("uval", p) == 0)
35279 kind = OMP_CLAUSE_LINEAR_UVAL;
35280 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
35281 cp_lexer_consume_token (parser->lexer);
35282 else
35283 kind = OMP_CLAUSE_LINEAR_DEFAULT;
35284 }
35285
35286 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
35287 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
35288 &colon);
35289 else
35290 {
35291 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
35292 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
35293 if (colon)
35294 cp_parser_require (parser, CPP_COLON, RT_COLON);
35295 else if (!parens.require_close (parser))
35296 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35297 /*or_comma=*/false,
35298 /*consume_paren=*/true);
35299 }
35300
35301 if (colon)
35302 {
35303 step = NULL_TREE;
35304 if (declare_simd
35305 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
35306 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
35307 {
35308 cp_token *token = cp_lexer_peek_token (parser->lexer);
35309 cp_parser_parse_tentatively (parser);
35310 step = cp_parser_id_expression (parser, /*template_p=*/false,
35311 /*check_dependency_p=*/true,
35312 /*template_p=*/NULL,
35313 /*declarator_p=*/false,
35314 /*optional_p=*/false);
35315 if (step != error_mark_node)
35316 step = cp_parser_lookup_name_simple (parser, step, token->location);
35317 if (step == error_mark_node)
35318 {
35319 step = NULL_TREE;
35320 cp_parser_abort_tentative_parse (parser);
35321 }
35322 else if (!cp_parser_parse_definitely (parser))
35323 step = NULL_TREE;
35324 }
35325 if (!step)
35326 step = cp_parser_assignment_expression (parser);
35327
35328 if (!parens.require_close (parser))
35329 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35330 /*or_comma=*/false,
35331 /*consume_paren=*/true);
35332
35333 if (step == error_mark_node)
35334 return list;
35335 }
35336
35337 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
35338 {
35339 OMP_CLAUSE_LINEAR_STEP (c) = step;
35340 OMP_CLAUSE_LINEAR_KIND (c) = kind;
35341 }
35342
35343 return nlist;
35344 }
35345
35346 /* OpenMP 4.0:
35347 safelen ( constant-expression ) */
35348
35349 static tree
35350 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
35351 location_t location)
35352 {
35353 tree t, c;
35354
35355 matching_parens parens;
35356 if (!parens.require_open (parser))
35357 return list;
35358
35359 t = cp_parser_constant_expression (parser);
35360
35361 if (t == error_mark_node
35362 || !parens.require_close (parser))
35363 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35364 /*or_comma=*/false,
35365 /*consume_paren=*/true);
35366
35367 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
35368
35369 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
35370 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
35371 OMP_CLAUSE_CHAIN (c) = list;
35372
35373 return c;
35374 }
35375
35376 /* OpenMP 4.0:
35377 simdlen ( constant-expression ) */
35378
35379 static tree
35380 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
35381 location_t location)
35382 {
35383 tree t, c;
35384
35385 matching_parens parens;
35386 if (!parens.require_open (parser))
35387 return list;
35388
35389 t = cp_parser_constant_expression (parser);
35390
35391 if (t == error_mark_node
35392 || !parens.require_close (parser))
35393 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35394 /*or_comma=*/false,
35395 /*consume_paren=*/true);
35396
35397 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
35398
35399 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
35400 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
35401 OMP_CLAUSE_CHAIN (c) = list;
35402
35403 return c;
35404 }
35405
35406 /* OpenMP 4.5:
35407 vec:
35408 identifier [+/- integer]
35409 vec , identifier [+/- integer]
35410 */
35411
35412 static tree
35413 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
35414 tree list)
35415 {
35416 tree vec = NULL;
35417
35418 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35419 {
35420 cp_parser_error (parser, "expected identifier");
35421 return list;
35422 }
35423
35424 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35425 {
35426 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
35427 tree t, identifier = cp_parser_identifier (parser);
35428 tree addend = NULL;
35429
35430 if (identifier == error_mark_node)
35431 t = error_mark_node;
35432 else
35433 {
35434 t = cp_parser_lookup_name_simple
35435 (parser, identifier,
35436 cp_lexer_peek_token (parser->lexer)->location);
35437 if (t == error_mark_node)
35438 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
35439 id_loc);
35440 }
35441
35442 bool neg = false;
35443 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
35444 neg = true;
35445 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
35446 {
35447 addend = integer_zero_node;
35448 goto add_to_vector;
35449 }
35450 cp_lexer_consume_token (parser->lexer);
35451
35452 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
35453 {
35454 cp_parser_error (parser, "expected integer");
35455 return list;
35456 }
35457
35458 addend = cp_lexer_peek_token (parser->lexer)->u.value;
35459 if (TREE_CODE (addend) != INTEGER_CST)
35460 {
35461 cp_parser_error (parser, "expected integer");
35462 return list;
35463 }
35464 cp_lexer_consume_token (parser->lexer);
35465
35466 add_to_vector:
35467 if (t != error_mark_node)
35468 {
35469 vec = tree_cons (addend, t, vec);
35470 if (neg)
35471 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
35472 }
35473
35474 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
35475 break;
35476
35477 cp_lexer_consume_token (parser->lexer);
35478 }
35479
35480 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
35481 {
35482 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
35483 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
35484 OMP_CLAUSE_DECL (u) = nreverse (vec);
35485 OMP_CLAUSE_CHAIN (u) = list;
35486 return u;
35487 }
35488 return list;
35489 }
35490
35491 /* OpenMP 5.0:
35492 iterators ( iterators-definition )
35493
35494 iterators-definition:
35495 iterator-specifier
35496 iterator-specifier , iterators-definition
35497
35498 iterator-specifier:
35499 identifier = range-specification
35500 iterator-type identifier = range-specification
35501
35502 range-specification:
35503 begin : end
35504 begin : end : step */
35505
35506 static tree
35507 cp_parser_omp_iterators (cp_parser *parser)
35508 {
35509 tree ret = NULL_TREE, *last = &ret;
35510 cp_lexer_consume_token (parser->lexer);
35511
35512 matching_parens parens;
35513 if (!parens.require_open (parser))
35514 return error_mark_node;
35515
35516 bool saved_colon_corrects_to_scope_p
35517 = parser->colon_corrects_to_scope_p;
35518 bool saved_colon_doesnt_start_class_def_p
35519 = parser->colon_doesnt_start_class_def_p;
35520
35521 do
35522 {
35523 tree iter_type;
35524 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
35525 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
35526 iter_type = integer_type_node;
35527 else
35528 {
35529 const char *saved_message
35530 = parser->type_definition_forbidden_message;
35531 parser->type_definition_forbidden_message
35532 = G_("types may not be defined in iterator type");
35533
35534 iter_type = cp_parser_type_id (parser);
35535
35536 parser->type_definition_forbidden_message = saved_message;
35537 }
35538
35539 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35540 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35541 {
35542 cp_parser_error (parser, "expected identifier");
35543 break;
35544 }
35545
35546 tree id = cp_parser_identifier (parser);
35547 if (id == error_mark_node)
35548 break;
35549
35550 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35551 break;
35552
35553 parser->colon_corrects_to_scope_p = false;
35554 parser->colon_doesnt_start_class_def_p = true;
35555 tree begin = cp_parser_assignment_expression (parser);
35556
35557 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35558 break;
35559
35560 tree end = cp_parser_assignment_expression (parser);
35561
35562 tree step = integer_one_node;
35563 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
35564 {
35565 cp_lexer_consume_token (parser->lexer);
35566 step = cp_parser_assignment_expression (parser);
35567 }
35568
35569 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
35570 DECL_ARTIFICIAL (iter_var) = 1;
35571 DECL_CONTEXT (iter_var) = current_function_decl;
35572 pushdecl (iter_var);
35573
35574 *last = make_tree_vec (6);
35575 TREE_VEC_ELT (*last, 0) = iter_var;
35576 TREE_VEC_ELT (*last, 1) = begin;
35577 TREE_VEC_ELT (*last, 2) = end;
35578 TREE_VEC_ELT (*last, 3) = step;
35579 last = &TREE_CHAIN (*last);
35580
35581 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35582 {
35583 cp_lexer_consume_token (parser->lexer);
35584 continue;
35585 }
35586 break;
35587 }
35588 while (1);
35589
35590 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
35591 parser->colon_doesnt_start_class_def_p
35592 = saved_colon_doesnt_start_class_def_p;
35593
35594 if (!parens.require_close (parser))
35595 cp_parser_skip_to_closing_parenthesis (parser,
35596 /*recovering=*/true,
35597 /*or_comma=*/false,
35598 /*consume_paren=*/true);
35599
35600 return ret ? ret : error_mark_node;
35601 }
35602
35603 /* OpenMP 4.0:
35604 depend ( depend-kind : variable-list )
35605
35606 depend-kind:
35607 in | out | inout
35608
35609 OpenMP 4.5:
35610 depend ( source )
35611
35612 depend ( sink : vec )
35613
35614 OpenMP 5.0:
35615 depend ( depend-modifier , depend-kind: variable-list )
35616
35617 depend-kind:
35618 in | out | inout | mutexinoutset | depobj
35619
35620 depend-modifier:
35621 iterator ( iterators-definition ) */
35622
35623 static tree
35624 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
35625 {
35626 tree nlist, c, iterators = NULL_TREE;
35627 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
35628
35629 matching_parens parens;
35630 if (!parens.require_open (parser))
35631 return list;
35632
35633 do
35634 {
35635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35636 goto invalid_kind;
35637
35638 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35639 const char *p = IDENTIFIER_POINTER (id);
35640
35641 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
35642 {
35643 begin_scope (sk_omp, NULL);
35644 iterators = cp_parser_omp_iterators (parser);
35645 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
35646 continue;
35647 }
35648 if (strcmp ("in", p) == 0)
35649 kind = OMP_CLAUSE_DEPEND_IN;
35650 else if (strcmp ("inout", p) == 0)
35651 kind = OMP_CLAUSE_DEPEND_INOUT;
35652 else if (strcmp ("mutexinoutset", p) == 0)
35653 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
35654 else if (strcmp ("out", p) == 0)
35655 kind = OMP_CLAUSE_DEPEND_OUT;
35656 else if (strcmp ("depobj", p) == 0)
35657 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
35658 else if (strcmp ("sink", p) == 0)
35659 kind = OMP_CLAUSE_DEPEND_SINK;
35660 else if (strcmp ("source", p) == 0)
35661 kind = OMP_CLAUSE_DEPEND_SOURCE;
35662 else
35663 goto invalid_kind;
35664 break;
35665 }
35666 while (1);
35667
35668 cp_lexer_consume_token (parser->lexer);
35669
35670 if (iterators
35671 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
35672 {
35673 poplevel (0, 1, 0);
35674 error_at (loc, "%<iterator%> modifier incompatible with %qs",
35675 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
35676 iterators = NULL_TREE;
35677 }
35678
35679 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
35680 {
35681 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
35682 OMP_CLAUSE_DEPEND_KIND (c) = kind;
35683 OMP_CLAUSE_DECL (c) = NULL_TREE;
35684 OMP_CLAUSE_CHAIN (c) = list;
35685 if (!parens.require_close (parser))
35686 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35687 /*or_comma=*/false,
35688 /*consume_paren=*/true);
35689 return c;
35690 }
35691
35692 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35693 goto resync_fail;
35694
35695 if (kind == OMP_CLAUSE_DEPEND_SINK)
35696 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
35697 else
35698 {
35699 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
35700 list, NULL);
35701
35702 if (iterators)
35703 {
35704 tree block = poplevel (1, 1, 0);
35705 if (iterators == error_mark_node)
35706 iterators = NULL_TREE;
35707 else
35708 TREE_VEC_ELT (iterators, 5) = block;
35709 }
35710
35711 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
35712 {
35713 OMP_CLAUSE_DEPEND_KIND (c) = kind;
35714 if (iterators)
35715 OMP_CLAUSE_DECL (c)
35716 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
35717 }
35718 }
35719 return nlist;
35720
35721 invalid_kind:
35722 cp_parser_error (parser, "invalid depend kind");
35723 resync_fail:
35724 if (iterators)
35725 poplevel (0, 1, 0);
35726 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35727 /*or_comma=*/false,
35728 /*consume_paren=*/true);
35729 return list;
35730 }
35731
35732 /* OpenMP 4.0:
35733 map ( map-kind : variable-list )
35734 map ( variable-list )
35735
35736 map-kind:
35737 alloc | to | from | tofrom
35738
35739 OpenMP 4.5:
35740 map-kind:
35741 alloc | to | from | tofrom | release | delete
35742
35743 map ( always [,] map-kind: variable-list ) */
35744
35745 static tree
35746 cp_parser_omp_clause_map (cp_parser *parser, tree list)
35747 {
35748 tree nlist, c;
35749 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
35750 bool always = false;
35751
35752 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35753 return list;
35754
35755 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35756 {
35757 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35758 const char *p = IDENTIFIER_POINTER (id);
35759
35760 if (strcmp ("always", p) == 0)
35761 {
35762 int nth = 2;
35763 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
35764 nth++;
35765 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
35766 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
35767 == RID_DELETE))
35768 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
35769 == CPP_COLON))
35770 {
35771 always = true;
35772 cp_lexer_consume_token (parser->lexer);
35773 if (nth == 3)
35774 cp_lexer_consume_token (parser->lexer);
35775 }
35776 }
35777 }
35778
35779 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
35780 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
35781 {
35782 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35783 const char *p = IDENTIFIER_POINTER (id);
35784
35785 if (strcmp ("alloc", p) == 0)
35786 kind = GOMP_MAP_ALLOC;
35787 else if (strcmp ("to", p) == 0)
35788 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
35789 else if (strcmp ("from", p) == 0)
35790 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
35791 else if (strcmp ("tofrom", p) == 0)
35792 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
35793 else if (strcmp ("release", p) == 0)
35794 kind = GOMP_MAP_RELEASE;
35795 else
35796 {
35797 cp_parser_error (parser, "invalid map kind");
35798 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35799 /*or_comma=*/false,
35800 /*consume_paren=*/true);
35801 return list;
35802 }
35803 cp_lexer_consume_token (parser->lexer);
35804 cp_lexer_consume_token (parser->lexer);
35805 }
35806 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
35807 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
35808 {
35809 kind = GOMP_MAP_DELETE;
35810 cp_lexer_consume_token (parser->lexer);
35811 cp_lexer_consume_token (parser->lexer);
35812 }
35813
35814 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
35815 NULL);
35816
35817 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
35818 OMP_CLAUSE_SET_MAP_KIND (c, kind);
35819
35820 return nlist;
35821 }
35822
35823 /* OpenMP 4.0:
35824 device ( expression ) */
35825
35826 static tree
35827 cp_parser_omp_clause_device (cp_parser *parser, tree list,
35828 location_t location)
35829 {
35830 tree t, c;
35831
35832 matching_parens parens;
35833 if (!parens.require_open (parser))
35834 return list;
35835
35836 t = cp_parser_assignment_expression (parser);
35837
35838 if (t == error_mark_node
35839 || !parens.require_close (parser))
35840 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35841 /*or_comma=*/false,
35842 /*consume_paren=*/true);
35843
35844 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
35845 "device", location);
35846
35847 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
35848 OMP_CLAUSE_DEVICE_ID (c) = t;
35849 OMP_CLAUSE_CHAIN (c) = list;
35850
35851 return c;
35852 }
35853
35854 /* OpenMP 4.0:
35855 dist_schedule ( static )
35856 dist_schedule ( static , expression ) */
35857
35858 static tree
35859 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
35860 location_t location)
35861 {
35862 tree c, t;
35863
35864 matching_parens parens;
35865 if (!parens.require_open (parser))
35866 return list;
35867
35868 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
35869
35870 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
35871 goto invalid_kind;
35872 cp_lexer_consume_token (parser->lexer);
35873
35874 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35875 {
35876 cp_lexer_consume_token (parser->lexer);
35877
35878 t = cp_parser_assignment_expression (parser);
35879
35880 if (t == error_mark_node)
35881 goto resync_fail;
35882 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
35883
35884 if (!parens.require_close (parser))
35885 goto resync_fail;
35886 }
35887 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
35888 goto resync_fail;
35889
35890 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
35891 "dist_schedule", location); */
35892 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
35893 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
35894 OMP_CLAUSE_CHAIN (c) = list;
35895 return c;
35896
35897 invalid_kind:
35898 cp_parser_error (parser, "invalid dist_schedule kind");
35899 resync_fail:
35900 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35901 /*or_comma=*/false,
35902 /*consume_paren=*/true);
35903 return list;
35904 }
35905
35906 /* OpenMP 4.0:
35907 proc_bind ( proc-bind-kind )
35908
35909 proc-bind-kind:
35910 master | close | spread */
35911
35912 static tree
35913 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
35914 location_t location)
35915 {
35916 tree c;
35917 enum omp_clause_proc_bind_kind kind;
35918
35919 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35920 return list;
35921
35922 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35923 {
35924 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35925 const char *p = IDENTIFIER_POINTER (id);
35926
35927 if (strcmp ("master", p) == 0)
35928 kind = OMP_CLAUSE_PROC_BIND_MASTER;
35929 else if (strcmp ("close", p) == 0)
35930 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
35931 else if (strcmp ("spread", p) == 0)
35932 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
35933 else
35934 goto invalid_kind;
35935 }
35936 else
35937 goto invalid_kind;
35938
35939 cp_lexer_consume_token (parser->lexer);
35940 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
35941 goto resync_fail;
35942
35943 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
35944 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
35945 location);
35946 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
35947 OMP_CLAUSE_CHAIN (c) = list;
35948 return c;
35949
35950 invalid_kind:
35951 cp_parser_error (parser, "invalid depend kind");
35952 resync_fail:
35953 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35954 /*or_comma=*/false,
35955 /*consume_paren=*/true);
35956 return list;
35957 }
35958
35959 /* OpenMP 5.0:
35960 device_type ( host | nohost | any ) */
35961
35962 static tree
35963 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
35964 location_t location)
35965 {
35966 tree c;
35967 enum omp_clause_device_type_kind kind;
35968
35969 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35970 return list;
35971
35972 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35973 {
35974 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35975 const char *p = IDENTIFIER_POINTER (id);
35976
35977 if (strcmp ("host", p) == 0)
35978 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
35979 else if (strcmp ("nohost", p) == 0)
35980 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
35981 else if (strcmp ("any", p) == 0)
35982 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
35983 else
35984 goto invalid_kind;
35985 }
35986 else
35987 goto invalid_kind;
35988
35989 cp_lexer_consume_token (parser->lexer);
35990 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
35991 goto resync_fail;
35992
35993 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
35994 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
35995 location); */
35996 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
35997 OMP_CLAUSE_CHAIN (c) = list;
35998 return c;
35999
36000 invalid_kind:
36001 cp_parser_error (parser, "invalid depend kind");
36002 resync_fail:
36003 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36004 /*or_comma=*/false,
36005 /*consume_paren=*/true);
36006 return list;
36007 }
36008
36009 /* OpenACC:
36010 async [( int-expr )] */
36011
36012 static tree
36013 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
36014 {
36015 tree c, t;
36016 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36017
36018 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
36019
36020 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36021 {
36022 matching_parens parens;
36023 parens.consume_open (parser);
36024
36025 t = cp_parser_expression (parser);
36026 if (t == error_mark_node
36027 || !parens.require_close (parser))
36028 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36029 /*or_comma=*/false,
36030 /*consume_paren=*/true);
36031 }
36032
36033 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
36034
36035 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
36036 OMP_CLAUSE_ASYNC_EXPR (c) = t;
36037 OMP_CLAUSE_CHAIN (c) = list;
36038 list = c;
36039
36040 return list;
36041 }
36042
36043 /* Parse all OpenACC clauses. The set clauses allowed by the directive
36044 is a bitmask in MASK. Return the list of clauses found. */
36045
36046 static tree
36047 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
36048 const char *where, cp_token *pragma_tok,
36049 bool finish_p = true)
36050 {
36051 tree clauses = NULL;
36052 bool first = true;
36053
36054 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36055 {
36056 location_t here;
36057 pragma_omp_clause c_kind;
36058 omp_clause_code code;
36059 const char *c_name;
36060 tree prev = clauses;
36061
36062 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36063 cp_lexer_consume_token (parser->lexer);
36064
36065 here = cp_lexer_peek_token (parser->lexer)->location;
36066 c_kind = cp_parser_omp_clause_name (parser);
36067
36068 switch (c_kind)
36069 {
36070 case PRAGMA_OACC_CLAUSE_ASYNC:
36071 clauses = cp_parser_oacc_clause_async (parser, clauses);
36072 c_name = "async";
36073 break;
36074 case PRAGMA_OACC_CLAUSE_AUTO:
36075 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
36076 clauses);
36077 c_name = "auto";
36078 break;
36079 case PRAGMA_OACC_CLAUSE_COLLAPSE:
36080 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
36081 c_name = "collapse";
36082 break;
36083 case PRAGMA_OACC_CLAUSE_COPY:
36084 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36085 c_name = "copy";
36086 break;
36087 case PRAGMA_OACC_CLAUSE_COPYIN:
36088 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36089 c_name = "copyin";
36090 break;
36091 case PRAGMA_OACC_CLAUSE_COPYOUT:
36092 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36093 c_name = "copyout";
36094 break;
36095 case PRAGMA_OACC_CLAUSE_CREATE:
36096 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36097 c_name = "create";
36098 break;
36099 case PRAGMA_OACC_CLAUSE_DELETE:
36100 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36101 c_name = "delete";
36102 break;
36103 case PRAGMA_OMP_CLAUSE_DEFAULT:
36104 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
36105 c_name = "default";
36106 break;
36107 case PRAGMA_OACC_CLAUSE_DEVICE:
36108 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36109 c_name = "device";
36110 break;
36111 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
36112 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
36113 c_name = "deviceptr";
36114 break;
36115 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
36116 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36117 c_name = "device_resident";
36118 break;
36119 case PRAGMA_OACC_CLAUSE_FINALIZE:
36120 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
36121 clauses);
36122 c_name = "finalize";
36123 break;
36124 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
36125 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
36126 clauses);
36127 c_name = "firstprivate";
36128 break;
36129 case PRAGMA_OACC_CLAUSE_GANG:
36130 c_name = "gang";
36131 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
36132 c_name, clauses);
36133 break;
36134 case PRAGMA_OACC_CLAUSE_HOST:
36135 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36136 c_name = "host";
36137 break;
36138 case PRAGMA_OACC_CLAUSE_IF:
36139 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
36140 c_name = "if";
36141 break;
36142 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
36143 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
36144 clauses);
36145 c_name = "if_present";
36146 break;
36147 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
36148 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
36149 clauses);
36150 c_name = "independent";
36151 break;
36152 case PRAGMA_OACC_CLAUSE_LINK:
36153 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36154 c_name = "link";
36155 break;
36156 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
36157 code = OMP_CLAUSE_NUM_GANGS;
36158 c_name = "num_gangs";
36159 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
36160 clauses);
36161 break;
36162 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
36163 c_name = "num_workers";
36164 code = OMP_CLAUSE_NUM_WORKERS;
36165 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
36166 clauses);
36167 break;
36168 case PRAGMA_OACC_CLAUSE_PRESENT:
36169 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
36170 c_name = "present";
36171 break;
36172 case PRAGMA_OACC_CLAUSE_PRIVATE:
36173 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
36174 clauses);
36175 c_name = "private";
36176 break;
36177 case PRAGMA_OACC_CLAUSE_REDUCTION:
36178 clauses
36179 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
36180 false, clauses);
36181 c_name = "reduction";
36182 break;
36183 case PRAGMA_OACC_CLAUSE_SEQ:
36184 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
36185 clauses);
36186 c_name = "seq";
36187 break;
36188 case PRAGMA_OACC_CLAUSE_TILE:
36189 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
36190 c_name = "tile";
36191 break;
36192 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
36193 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
36194 clauses);
36195 c_name = "use_device";
36196 break;
36197 case PRAGMA_OACC_CLAUSE_VECTOR:
36198 c_name = "vector";
36199 clauses = cp_parser_oacc_shape_clause (parser, here,
36200 OMP_CLAUSE_VECTOR,
36201 c_name, clauses);
36202 break;
36203 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
36204 c_name = "vector_length";
36205 code = OMP_CLAUSE_VECTOR_LENGTH;
36206 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
36207 clauses);
36208 break;
36209 case PRAGMA_OACC_CLAUSE_WAIT:
36210 clauses = cp_parser_oacc_clause_wait (parser, clauses);
36211 c_name = "wait";
36212 break;
36213 case PRAGMA_OACC_CLAUSE_WORKER:
36214 c_name = "worker";
36215 clauses = cp_parser_oacc_shape_clause (parser, here,
36216 OMP_CLAUSE_WORKER,
36217 c_name, clauses);
36218 break;
36219 default:
36220 cp_parser_error (parser, "expected %<#pragma acc%> clause");
36221 goto saw_error;
36222 }
36223
36224 first = false;
36225
36226 if (((mask >> c_kind) & 1) == 0)
36227 {
36228 /* Remove the invalid clause(s) from the list to avoid
36229 confusing the rest of the compiler. */
36230 clauses = prev;
36231 error_at (here, "%qs is not valid for %qs", c_name, where);
36232 }
36233 }
36234
36235 saw_error:
36236 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36237
36238 if (finish_p)
36239 return finish_omp_clauses (clauses, C_ORT_ACC);
36240
36241 return clauses;
36242 }
36243
36244 /* Parse all OpenMP clauses. The set clauses allowed by the directive
36245 is a bitmask in MASK. Return the list of clauses found.
36246 FINISH_P set if finish_omp_clauses should be called.
36247 NESTED non-zero if clauses should be terminated by closing paren instead
36248 of end of pragma. If it is 2, additionally commas are required in between
36249 the clauses. */
36250
36251 static tree
36252 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
36253 const char *where, cp_token *pragma_tok,
36254 bool finish_p = true, int nested = 0)
36255 {
36256 tree clauses = NULL;
36257 bool first = true;
36258 cp_token *token = NULL;
36259
36260 /* Don't create location wrapper nodes within OpenMP clauses. */
36261 auto_suppress_location_wrappers sentinel;
36262
36263 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36264 {
36265 pragma_omp_clause c_kind;
36266 const char *c_name;
36267 tree prev = clauses;
36268
36269 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36270 break;
36271
36272 if (!first)
36273 {
36274 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36275 cp_lexer_consume_token (parser->lexer);
36276 else if (nested == 2)
36277 error_at (cp_lexer_peek_token (parser->lexer)->location,
36278 "clauses in %<simd%> trait should be separated "
36279 "by %<,%>");
36280 }
36281
36282 token = cp_lexer_peek_token (parser->lexer);
36283 c_kind = cp_parser_omp_clause_name (parser);
36284
36285 switch (c_kind)
36286 {
36287 case PRAGMA_OMP_CLAUSE_BIND:
36288 clauses = cp_parser_omp_clause_bind (parser, clauses,
36289 token->location);
36290 c_name = "bind";
36291 break;
36292 case PRAGMA_OMP_CLAUSE_COLLAPSE:
36293 clauses = cp_parser_omp_clause_collapse (parser, clauses,
36294 token->location);
36295 c_name = "collapse";
36296 break;
36297 case PRAGMA_OMP_CLAUSE_COPYIN:
36298 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
36299 c_name = "copyin";
36300 break;
36301 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
36302 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
36303 clauses);
36304 c_name = "copyprivate";
36305 break;
36306 case PRAGMA_OMP_CLAUSE_DEFAULT:
36307 clauses = cp_parser_omp_clause_default (parser, clauses,
36308 token->location, false);
36309 c_name = "default";
36310 break;
36311 case PRAGMA_OMP_CLAUSE_FINAL:
36312 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
36313 c_name = "final";
36314 break;
36315 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
36316 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
36317 clauses);
36318 c_name = "firstprivate";
36319 break;
36320 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
36321 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
36322 token->location);
36323 c_name = "grainsize";
36324 break;
36325 case PRAGMA_OMP_CLAUSE_HINT:
36326 clauses = cp_parser_omp_clause_hint (parser, clauses,
36327 token->location);
36328 c_name = "hint";
36329 break;
36330 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
36331 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
36332 token->location);
36333 c_name = "defaultmap";
36334 break;
36335 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
36336 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
36337 clauses);
36338 c_name = "use_device_ptr";
36339 break;
36340 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
36341 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
36342 clauses);
36343 c_name = "use_device_addr";
36344 break;
36345 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
36346 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
36347 clauses);
36348 c_name = "is_device_ptr";
36349 break;
36350 case PRAGMA_OMP_CLAUSE_IF:
36351 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
36352 true);
36353 c_name = "if";
36354 break;
36355 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
36356 clauses
36357 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
36358 true, clauses);
36359 c_name = "in_reduction";
36360 break;
36361 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
36362 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
36363 c_name = "lastprivate";
36364 break;
36365 case PRAGMA_OMP_CLAUSE_MERGEABLE:
36366 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
36367 token->location);
36368 c_name = "mergeable";
36369 break;
36370 case PRAGMA_OMP_CLAUSE_NOWAIT:
36371 clauses = cp_parser_omp_clause_nowait (parser, clauses,
36372 token->location);
36373 c_name = "nowait";
36374 break;
36375 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
36376 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
36377 token->location);
36378 c_name = "num_tasks";
36379 break;
36380 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
36381 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
36382 token->location);
36383 c_name = "num_threads";
36384 break;
36385 case PRAGMA_OMP_CLAUSE_ORDER:
36386 clauses = cp_parser_omp_clause_order (parser, clauses,
36387 token->location);
36388 c_name = "order";
36389 break;
36390 case PRAGMA_OMP_CLAUSE_ORDERED:
36391 clauses = cp_parser_omp_clause_ordered (parser, clauses,
36392 token->location);
36393 c_name = "ordered";
36394 break;
36395 case PRAGMA_OMP_CLAUSE_PRIORITY:
36396 clauses = cp_parser_omp_clause_priority (parser, clauses,
36397 token->location);
36398 c_name = "priority";
36399 break;
36400 case PRAGMA_OMP_CLAUSE_PRIVATE:
36401 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
36402 clauses);
36403 c_name = "private";
36404 break;
36405 case PRAGMA_OMP_CLAUSE_REDUCTION:
36406 clauses
36407 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
36408 true, clauses);
36409 c_name = "reduction";
36410 break;
36411 case PRAGMA_OMP_CLAUSE_SCHEDULE:
36412 clauses = cp_parser_omp_clause_schedule (parser, clauses,
36413 token->location);
36414 c_name = "schedule";
36415 break;
36416 case PRAGMA_OMP_CLAUSE_SHARED:
36417 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
36418 clauses);
36419 c_name = "shared";
36420 break;
36421 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
36422 clauses
36423 = cp_parser_omp_clause_reduction (parser,
36424 OMP_CLAUSE_TASK_REDUCTION,
36425 true, clauses);
36426 c_name = "task_reduction";
36427 break;
36428 case PRAGMA_OMP_CLAUSE_UNTIED:
36429 clauses = cp_parser_omp_clause_untied (parser, clauses,
36430 token->location);
36431 c_name = "untied";
36432 break;
36433 case PRAGMA_OMP_CLAUSE_INBRANCH:
36434 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
36435 clauses, token->location);
36436 c_name = "inbranch";
36437 break;
36438 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
36439 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
36440 clauses);
36441 c_name = "nontemporal";
36442 break;
36443 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
36444 clauses = cp_parser_omp_clause_branch (parser,
36445 OMP_CLAUSE_NOTINBRANCH,
36446 clauses, token->location);
36447 c_name = "notinbranch";
36448 break;
36449 case PRAGMA_OMP_CLAUSE_PARALLEL:
36450 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
36451 clauses, token->location);
36452 c_name = "parallel";
36453 if (!first)
36454 {
36455 clause_not_first:
36456 error_at (token->location, "%qs must be the first clause of %qs",
36457 c_name, where);
36458 clauses = prev;
36459 }
36460 break;
36461 case PRAGMA_OMP_CLAUSE_FOR:
36462 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
36463 clauses, token->location);
36464 c_name = "for";
36465 if (!first)
36466 goto clause_not_first;
36467 break;
36468 case PRAGMA_OMP_CLAUSE_SECTIONS:
36469 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
36470 clauses, token->location);
36471 c_name = "sections";
36472 if (!first)
36473 goto clause_not_first;
36474 break;
36475 case PRAGMA_OMP_CLAUSE_TASKGROUP:
36476 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
36477 clauses, token->location);
36478 c_name = "taskgroup";
36479 if (!first)
36480 goto clause_not_first;
36481 break;
36482 case PRAGMA_OMP_CLAUSE_LINK:
36483 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
36484 c_name = "to";
36485 break;
36486 case PRAGMA_OMP_CLAUSE_TO:
36487 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
36488 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36489 clauses);
36490 else
36491 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
36492 c_name = "to";
36493 break;
36494 case PRAGMA_OMP_CLAUSE_FROM:
36495 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
36496 c_name = "from";
36497 break;
36498 case PRAGMA_OMP_CLAUSE_UNIFORM:
36499 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
36500 clauses);
36501 c_name = "uniform";
36502 break;
36503 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
36504 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
36505 token->location);
36506 c_name = "num_teams";
36507 break;
36508 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
36509 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
36510 token->location);
36511 c_name = "thread_limit";
36512 break;
36513 case PRAGMA_OMP_CLAUSE_ALIGNED:
36514 clauses = cp_parser_omp_clause_aligned (parser, clauses);
36515 c_name = "aligned";
36516 break;
36517 case PRAGMA_OMP_CLAUSE_LINEAR:
36518 {
36519 bool declare_simd = false;
36520 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
36521 declare_simd = true;
36522 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
36523 }
36524 c_name = "linear";
36525 break;
36526 case PRAGMA_OMP_CLAUSE_DEPEND:
36527 clauses = cp_parser_omp_clause_depend (parser, clauses,
36528 token->location);
36529 c_name = "depend";
36530 break;
36531 case PRAGMA_OMP_CLAUSE_MAP:
36532 clauses = cp_parser_omp_clause_map (parser, clauses);
36533 c_name = "map";
36534 break;
36535 case PRAGMA_OMP_CLAUSE_DEVICE:
36536 clauses = cp_parser_omp_clause_device (parser, clauses,
36537 token->location);
36538 c_name = "device";
36539 break;
36540 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
36541 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
36542 token->location);
36543 c_name = "dist_schedule";
36544 break;
36545 case PRAGMA_OMP_CLAUSE_PROC_BIND:
36546 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
36547 token->location);
36548 c_name = "proc_bind";
36549 break;
36550 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
36551 clauses = cp_parser_omp_clause_device_type (parser, clauses,
36552 token->location);
36553 c_name = "device_type";
36554 break;
36555 case PRAGMA_OMP_CLAUSE_SAFELEN:
36556 clauses = cp_parser_omp_clause_safelen (parser, clauses,
36557 token->location);
36558 c_name = "safelen";
36559 break;
36560 case PRAGMA_OMP_CLAUSE_SIMDLEN:
36561 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
36562 token->location);
36563 c_name = "simdlen";
36564 break;
36565 case PRAGMA_OMP_CLAUSE_NOGROUP:
36566 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
36567 token->location);
36568 c_name = "nogroup";
36569 break;
36570 case PRAGMA_OMP_CLAUSE_THREADS:
36571 clauses
36572 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
36573 clauses, token->location);
36574 c_name = "threads";
36575 break;
36576 case PRAGMA_OMP_CLAUSE_SIMD:
36577 clauses
36578 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
36579 clauses, token->location);
36580 c_name = "simd";
36581 break;
36582 default:
36583 cp_parser_error (parser, "expected %<#pragma omp%> clause");
36584 goto saw_error;
36585 }
36586
36587 first = false;
36588
36589 if (((mask >> c_kind) & 1) == 0)
36590 {
36591 /* Remove the invalid clause(s) from the list to avoid
36592 confusing the rest of the compiler. */
36593 clauses = prev;
36594 error_at (token->location, "%qs is not valid for %qs", c_name, where);
36595 }
36596 }
36597 saw_error:
36598 if (!nested)
36599 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36600 if (finish_p)
36601 {
36602 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
36603 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
36604 else
36605 return finish_omp_clauses (clauses, C_ORT_OMP);
36606 }
36607 return clauses;
36608 }
36609
36610 /* OpenMP 2.5:
36611 structured-block:
36612 statement
36613
36614 In practice, we're also interested in adding the statement to an
36615 outer node. So it is convenient if we work around the fact that
36616 cp_parser_statement calls add_stmt. */
36617
36618 static unsigned
36619 cp_parser_begin_omp_structured_block (cp_parser *parser)
36620 {
36621 unsigned save = parser->in_statement;
36622
36623 /* Only move the values to IN_OMP_BLOCK if they weren't false.
36624 This preserves the "not within loop or switch" style error messages
36625 for nonsense cases like
36626 void foo() {
36627 #pragma omp single
36628 break;
36629 }
36630 */
36631 if (parser->in_statement)
36632 parser->in_statement = IN_OMP_BLOCK;
36633
36634 return save;
36635 }
36636
36637 static void
36638 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
36639 {
36640 parser->in_statement = save;
36641 }
36642
36643 static tree
36644 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
36645 {
36646 tree stmt = begin_omp_structured_block ();
36647 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36648
36649 cp_parser_statement (parser, NULL_TREE, false, if_p);
36650
36651 cp_parser_end_omp_structured_block (parser, save);
36652 return finish_omp_structured_block (stmt);
36653 }
36654
36655 /* OpenMP 2.5:
36656 # pragma omp atomic new-line
36657 expression-stmt
36658
36659 expression-stmt:
36660 x binop= expr | x++ | ++x | x-- | --x
36661 binop:
36662 +, *, -, /, &, ^, |, <<, >>
36663
36664 where x is an lvalue expression with scalar type.
36665
36666 OpenMP 3.1:
36667 # pragma omp atomic new-line
36668 update-stmt
36669
36670 # pragma omp atomic read new-line
36671 read-stmt
36672
36673 # pragma omp atomic write new-line
36674 write-stmt
36675
36676 # pragma omp atomic update new-line
36677 update-stmt
36678
36679 # pragma omp atomic capture new-line
36680 capture-stmt
36681
36682 # pragma omp atomic capture new-line
36683 capture-block
36684
36685 read-stmt:
36686 v = x
36687 write-stmt:
36688 x = expr
36689 update-stmt:
36690 expression-stmt | x = x binop expr
36691 capture-stmt:
36692 v = expression-stmt
36693 capture-block:
36694 { v = x; update-stmt; } | { update-stmt; v = x; }
36695
36696 OpenMP 4.0:
36697 update-stmt:
36698 expression-stmt | x = x binop expr | x = expr binop x
36699 capture-stmt:
36700 v = update-stmt
36701 capture-block:
36702 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
36703
36704 where x and v are lvalue expressions with scalar type. */
36705
36706 static void
36707 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
36708 {
36709 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
36710 tree rhs1 = NULL_TREE, orig_lhs;
36711 location_t loc = pragma_tok->location;
36712 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
36713 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
36714 bool structured_block = false;
36715 bool first = true;
36716 tree clauses = NULL_TREE;
36717
36718 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36719 {
36720 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36721 cp_lexer_consume_token (parser->lexer);
36722
36723 first = false;
36724
36725 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36726 {
36727 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36728 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
36729 const char *p = IDENTIFIER_POINTER (id);
36730 enum tree_code new_code = ERROR_MARK;
36731 enum omp_memory_order new_memory_order
36732 = OMP_MEMORY_ORDER_UNSPECIFIED;
36733
36734 if (!strcmp (p, "read"))
36735 new_code = OMP_ATOMIC_READ;
36736 else if (!strcmp (p, "write"))
36737 new_code = NOP_EXPR;
36738 else if (!strcmp (p, "update"))
36739 new_code = OMP_ATOMIC;
36740 else if (!strcmp (p, "capture"))
36741 new_code = OMP_ATOMIC_CAPTURE_NEW;
36742 else if (!strcmp (p, "seq_cst"))
36743 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36744 else if (!strcmp (p, "acq_rel"))
36745 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
36746 else if (!strcmp (p, "release"))
36747 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
36748 else if (!strcmp (p, "acquire"))
36749 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
36750 else if (!strcmp (p, "relaxed"))
36751 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
36752 else if (!strcmp (p, "hint"))
36753 {
36754 cp_lexer_consume_token (parser->lexer);
36755 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
36756 continue;
36757 }
36758 else
36759 {
36760 p = NULL;
36761 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
36762 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
36763 "%<release%>, %<relaxed%> or %<hint%> clause");
36764 }
36765 if (p)
36766 {
36767 if (new_code != ERROR_MARK)
36768 {
36769 if (code != ERROR_MARK)
36770 error_at (cloc, "too many atomic clauses");
36771 else
36772 code = new_code;
36773 }
36774 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
36775 {
36776 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
36777 error_at (cloc, "too many memory order clauses");
36778 else
36779 memory_order = new_memory_order;
36780 }
36781 cp_lexer_consume_token (parser->lexer);
36782 continue;
36783 }
36784 }
36785 break;
36786 }
36787 cp_parser_require_pragma_eol (parser, pragma_tok);
36788
36789 if (code == ERROR_MARK)
36790 code = OMP_ATOMIC;
36791 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
36792 {
36793 omp_requires_mask
36794 = (enum omp_requires) (omp_requires_mask
36795 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
36796 switch ((enum omp_memory_order)
36797 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
36798 {
36799 case OMP_MEMORY_ORDER_UNSPECIFIED:
36800 case OMP_MEMORY_ORDER_RELAXED:
36801 memory_order = OMP_MEMORY_ORDER_RELAXED;
36802 break;
36803 case OMP_MEMORY_ORDER_SEQ_CST:
36804 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36805 break;
36806 case OMP_MEMORY_ORDER_ACQ_REL:
36807 switch (code)
36808 {
36809 case OMP_ATOMIC_READ:
36810 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
36811 break;
36812 case NOP_EXPR: /* atomic write */
36813 case OMP_ATOMIC:
36814 memory_order = OMP_MEMORY_ORDER_RELEASE;
36815 break;
36816 default:
36817 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
36818 break;
36819 }
36820 break;
36821 default:
36822 gcc_unreachable ();
36823 }
36824 }
36825 else
36826 switch (code)
36827 {
36828 case OMP_ATOMIC_READ:
36829 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
36830 || memory_order == OMP_MEMORY_ORDER_RELEASE)
36831 {
36832 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
36833 "%<acq_rel%> or %<release%> clauses");
36834 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36835 }
36836 break;
36837 case NOP_EXPR: /* atomic write */
36838 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
36839 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
36840 {
36841 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
36842 "%<acq_rel%> or %<acquire%> clauses");
36843 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36844 }
36845 break;
36846 case OMP_ATOMIC:
36847 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
36848 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
36849 {
36850 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
36851 "%<acq_rel%> or %<acquire%> clauses");
36852 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36853 }
36854 break;
36855 default:
36856 break;
36857 }
36858
36859 switch (code)
36860 {
36861 case OMP_ATOMIC_READ:
36862 case NOP_EXPR: /* atomic write */
36863 v = cp_parser_unary_expression (parser);
36864 if (v == error_mark_node)
36865 goto saw_error;
36866 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36867 goto saw_error;
36868 if (code == NOP_EXPR)
36869 lhs = cp_parser_expression (parser);
36870 else
36871 lhs = cp_parser_unary_expression (parser);
36872 if (lhs == error_mark_node)
36873 goto saw_error;
36874 if (code == NOP_EXPR)
36875 {
36876 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
36877 opcode. */
36878 code = OMP_ATOMIC;
36879 rhs = lhs;
36880 lhs = v;
36881 v = NULL_TREE;
36882 }
36883 goto done;
36884 case OMP_ATOMIC_CAPTURE_NEW:
36885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36886 {
36887 cp_lexer_consume_token (parser->lexer);
36888 structured_block = true;
36889 }
36890 else
36891 {
36892 v = cp_parser_unary_expression (parser);
36893 if (v == error_mark_node)
36894 goto saw_error;
36895 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36896 goto saw_error;
36897 }
36898 default:
36899 break;
36900 }
36901
36902 restart:
36903 lhs = cp_parser_unary_expression (parser);
36904 orig_lhs = lhs;
36905 switch (TREE_CODE (lhs))
36906 {
36907 case ERROR_MARK:
36908 goto saw_error;
36909
36910 case POSTINCREMENT_EXPR:
36911 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
36912 code = OMP_ATOMIC_CAPTURE_OLD;
36913 /* FALLTHROUGH */
36914 case PREINCREMENT_EXPR:
36915 lhs = TREE_OPERAND (lhs, 0);
36916 opcode = PLUS_EXPR;
36917 rhs = integer_one_node;
36918 break;
36919
36920 case POSTDECREMENT_EXPR:
36921 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
36922 code = OMP_ATOMIC_CAPTURE_OLD;
36923 /* FALLTHROUGH */
36924 case PREDECREMENT_EXPR:
36925 lhs = TREE_OPERAND (lhs, 0);
36926 opcode = MINUS_EXPR;
36927 rhs = integer_one_node;
36928 break;
36929
36930 case COMPOUND_EXPR:
36931 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
36932 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
36933 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
36934 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
36935 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
36936 (TREE_OPERAND (lhs, 1), 0), 0)))
36937 == BOOLEAN_TYPE)
36938 /* Undo effects of boolean_increment for post {in,de}crement. */
36939 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
36940 /* FALLTHRU */
36941 case MODIFY_EXPR:
36942 if (TREE_CODE (lhs) == MODIFY_EXPR
36943 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
36944 {
36945 /* Undo effects of boolean_increment. */
36946 if (integer_onep (TREE_OPERAND (lhs, 1)))
36947 {
36948 /* This is pre or post increment. */
36949 rhs = TREE_OPERAND (lhs, 1);
36950 lhs = TREE_OPERAND (lhs, 0);
36951 opcode = NOP_EXPR;
36952 if (code == OMP_ATOMIC_CAPTURE_NEW
36953 && !structured_block
36954 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
36955 code = OMP_ATOMIC_CAPTURE_OLD;
36956 break;
36957 }
36958 }
36959 /* FALLTHRU */
36960 default:
36961 switch (cp_lexer_peek_token (parser->lexer)->type)
36962 {
36963 case CPP_MULT_EQ:
36964 opcode = MULT_EXPR;
36965 break;
36966 case CPP_DIV_EQ:
36967 opcode = TRUNC_DIV_EXPR;
36968 break;
36969 case CPP_PLUS_EQ:
36970 opcode = PLUS_EXPR;
36971 break;
36972 case CPP_MINUS_EQ:
36973 opcode = MINUS_EXPR;
36974 break;
36975 case CPP_LSHIFT_EQ:
36976 opcode = LSHIFT_EXPR;
36977 break;
36978 case CPP_RSHIFT_EQ:
36979 opcode = RSHIFT_EXPR;
36980 break;
36981 case CPP_AND_EQ:
36982 opcode = BIT_AND_EXPR;
36983 break;
36984 case CPP_OR_EQ:
36985 opcode = BIT_IOR_EXPR;
36986 break;
36987 case CPP_XOR_EQ:
36988 opcode = BIT_XOR_EXPR;
36989 break;
36990 case CPP_EQ:
36991 enum cp_parser_prec oprec;
36992 cp_token *token;
36993 cp_lexer_consume_token (parser->lexer);
36994 cp_parser_parse_tentatively (parser);
36995 rhs1 = cp_parser_simple_cast_expression (parser);
36996 if (rhs1 == error_mark_node)
36997 {
36998 cp_parser_abort_tentative_parse (parser);
36999 cp_parser_simple_cast_expression (parser);
37000 goto saw_error;
37001 }
37002 token = cp_lexer_peek_token (parser->lexer);
37003 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
37004 {
37005 cp_parser_abort_tentative_parse (parser);
37006 cp_parser_parse_tentatively (parser);
37007 rhs = cp_parser_binary_expression (parser, false, true,
37008 PREC_NOT_OPERATOR, NULL);
37009 if (rhs == error_mark_node)
37010 {
37011 cp_parser_abort_tentative_parse (parser);
37012 cp_parser_binary_expression (parser, false, true,
37013 PREC_NOT_OPERATOR, NULL);
37014 goto saw_error;
37015 }
37016 switch (TREE_CODE (rhs))
37017 {
37018 case MULT_EXPR:
37019 case TRUNC_DIV_EXPR:
37020 case RDIV_EXPR:
37021 case PLUS_EXPR:
37022 case MINUS_EXPR:
37023 case LSHIFT_EXPR:
37024 case RSHIFT_EXPR:
37025 case BIT_AND_EXPR:
37026 case BIT_IOR_EXPR:
37027 case BIT_XOR_EXPR:
37028 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
37029 {
37030 if (cp_parser_parse_definitely (parser))
37031 {
37032 opcode = TREE_CODE (rhs);
37033 rhs1 = TREE_OPERAND (rhs, 0);
37034 rhs = TREE_OPERAND (rhs, 1);
37035 goto stmt_done;
37036 }
37037 else
37038 goto saw_error;
37039 }
37040 break;
37041 default:
37042 break;
37043 }
37044 cp_parser_abort_tentative_parse (parser);
37045 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
37046 {
37047 rhs = cp_parser_expression (parser);
37048 if (rhs == error_mark_node)
37049 goto saw_error;
37050 opcode = NOP_EXPR;
37051 rhs1 = NULL_TREE;
37052 goto stmt_done;
37053 }
37054 cp_parser_error (parser,
37055 "invalid form of %<#pragma omp atomic%>");
37056 goto saw_error;
37057 }
37058 if (!cp_parser_parse_definitely (parser))
37059 goto saw_error;
37060 switch (token->type)
37061 {
37062 case CPP_SEMICOLON:
37063 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
37064 {
37065 code = OMP_ATOMIC_CAPTURE_OLD;
37066 v = lhs;
37067 lhs = NULL_TREE;
37068 lhs1 = rhs1;
37069 rhs1 = NULL_TREE;
37070 cp_lexer_consume_token (parser->lexer);
37071 goto restart;
37072 }
37073 else if (structured_block)
37074 {
37075 opcode = NOP_EXPR;
37076 rhs = rhs1;
37077 rhs1 = NULL_TREE;
37078 goto stmt_done;
37079 }
37080 cp_parser_error (parser,
37081 "invalid form of %<#pragma omp atomic%>");
37082 goto saw_error;
37083 case CPP_MULT:
37084 opcode = MULT_EXPR;
37085 break;
37086 case CPP_DIV:
37087 opcode = TRUNC_DIV_EXPR;
37088 break;
37089 case CPP_PLUS:
37090 opcode = PLUS_EXPR;
37091 break;
37092 case CPP_MINUS:
37093 opcode = MINUS_EXPR;
37094 break;
37095 case CPP_LSHIFT:
37096 opcode = LSHIFT_EXPR;
37097 break;
37098 case CPP_RSHIFT:
37099 opcode = RSHIFT_EXPR;
37100 break;
37101 case CPP_AND:
37102 opcode = BIT_AND_EXPR;
37103 break;
37104 case CPP_OR:
37105 opcode = BIT_IOR_EXPR;
37106 break;
37107 case CPP_XOR:
37108 opcode = BIT_XOR_EXPR;
37109 break;
37110 default:
37111 cp_parser_error (parser,
37112 "invalid operator for %<#pragma omp atomic%>");
37113 goto saw_error;
37114 }
37115 oprec = TOKEN_PRECEDENCE (token);
37116 gcc_assert (oprec != PREC_NOT_OPERATOR);
37117 if (commutative_tree_code (opcode))
37118 oprec = (enum cp_parser_prec) (oprec - 1);
37119 cp_lexer_consume_token (parser->lexer);
37120 rhs = cp_parser_binary_expression (parser, false, false,
37121 oprec, NULL);
37122 if (rhs == error_mark_node)
37123 goto saw_error;
37124 goto stmt_done;
37125 /* FALLTHROUGH */
37126 default:
37127 cp_parser_error (parser,
37128 "invalid operator for %<#pragma omp atomic%>");
37129 goto saw_error;
37130 }
37131 cp_lexer_consume_token (parser->lexer);
37132
37133 rhs = cp_parser_expression (parser);
37134 if (rhs == error_mark_node)
37135 goto saw_error;
37136 break;
37137 }
37138 stmt_done:
37139 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
37140 {
37141 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
37142 goto saw_error;
37143 v = cp_parser_unary_expression (parser);
37144 if (v == error_mark_node)
37145 goto saw_error;
37146 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
37147 goto saw_error;
37148 lhs1 = cp_parser_unary_expression (parser);
37149 if (lhs1 == error_mark_node)
37150 goto saw_error;
37151 }
37152 if (structured_block)
37153 {
37154 cp_parser_consume_semicolon_at_end_of_statement (parser);
37155 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
37156 }
37157 done:
37158 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37159 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
37160 rhs1, clauses, memory_order);
37161 if (!structured_block)
37162 cp_parser_consume_semicolon_at_end_of_statement (parser);
37163 return;
37164
37165 saw_error:
37166 cp_parser_skip_to_end_of_block_or_statement (parser);
37167 if (structured_block)
37168 {
37169 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
37170 cp_lexer_consume_token (parser->lexer);
37171 else if (code == OMP_ATOMIC_CAPTURE_NEW)
37172 {
37173 cp_parser_skip_to_end_of_block_or_statement (parser);
37174 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
37175 cp_lexer_consume_token (parser->lexer);
37176 }
37177 }
37178 }
37179
37180
37181 /* OpenMP 2.5:
37182 # pragma omp barrier new-line */
37183
37184 static void
37185 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
37186 {
37187 cp_parser_require_pragma_eol (parser, pragma_tok);
37188 finish_omp_barrier ();
37189 }
37190
37191 /* OpenMP 2.5:
37192 # pragma omp critical [(name)] new-line
37193 structured-block
37194
37195 OpenMP 4.5:
37196 # pragma omp critical [(name) [hint(expression)]] new-line
37197 structured-block */
37198
37199 #define OMP_CRITICAL_CLAUSE_MASK \
37200 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
37201
37202 static tree
37203 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37204 {
37205 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
37206
37207 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37208 {
37209 matching_parens parens;
37210 parens.consume_open (parser);
37211
37212 name = cp_parser_identifier (parser);
37213
37214 if (name == error_mark_node
37215 || !parens.require_close (parser))
37216 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37217 /*or_comma=*/false,
37218 /*consume_paren=*/true);
37219 if (name == error_mark_node)
37220 name = NULL;
37221
37222 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
37223 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
37224 cp_lexer_consume_token (parser->lexer);
37225
37226 clauses = cp_parser_omp_all_clauses (parser,
37227 OMP_CRITICAL_CLAUSE_MASK,
37228 "#pragma omp critical", pragma_tok);
37229 }
37230 else
37231 cp_parser_require_pragma_eol (parser, pragma_tok);
37232
37233 stmt = cp_parser_omp_structured_block (parser, if_p);
37234 return c_finish_omp_critical (input_location, stmt, name, clauses);
37235 }
37236
37237 /* OpenMP 5.0:
37238 # pragma omp depobj ( depobj ) depobj-clause new-line
37239
37240 depobj-clause:
37241 depend (dependence-type : locator)
37242 destroy
37243 update (dependence-type)
37244
37245 dependence-type:
37246 in
37247 out
37248 inout
37249 mutexinout */
37250
37251 static void
37252 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
37253 {
37254 location_t loc = pragma_tok->location;
37255 matching_parens parens;
37256 if (!parens.require_open (parser))
37257 {
37258 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37259 return;
37260 }
37261
37262 tree depobj = cp_parser_assignment_expression (parser);
37263
37264 if (!parens.require_close (parser))
37265 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37266 /*or_comma=*/false,
37267 /*consume_paren=*/true);
37268
37269 tree clause = NULL_TREE;
37270 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
37271 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
37272 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37273 {
37274 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37275 const char *p = IDENTIFIER_POINTER (id);
37276
37277 cp_lexer_consume_token (parser->lexer);
37278 if (!strcmp ("depend", p))
37279 {
37280 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
37281 if (clause)
37282 clause = finish_omp_clauses (clause, C_ORT_OMP);
37283 if (!clause)
37284 clause = error_mark_node;
37285 }
37286 else if (!strcmp ("destroy", p))
37287 kind = OMP_CLAUSE_DEPEND_LAST;
37288 else if (!strcmp ("update", p))
37289 {
37290 matching_parens c_parens;
37291 if (c_parens.require_open (parser))
37292 {
37293 location_t c2_loc
37294 = cp_lexer_peek_token (parser->lexer)->location;
37295 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37296 {
37297 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
37298 const char *p2 = IDENTIFIER_POINTER (id2);
37299
37300 cp_lexer_consume_token (parser->lexer);
37301 if (!strcmp ("in", p2))
37302 kind = OMP_CLAUSE_DEPEND_IN;
37303 else if (!strcmp ("out", p2))
37304 kind = OMP_CLAUSE_DEPEND_OUT;
37305 else if (!strcmp ("inout", p2))
37306 kind = OMP_CLAUSE_DEPEND_INOUT;
37307 else if (!strcmp ("mutexinoutset", p2))
37308 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
37309 }
37310 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
37311 {
37312 clause = error_mark_node;
37313 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
37314 "%<mutexinoutset%>");
37315 }
37316 if (!c_parens.require_close (parser))
37317 cp_parser_skip_to_closing_parenthesis (parser,
37318 /*recovering=*/true,
37319 /*or_comma=*/false,
37320 /*consume_paren=*/true);
37321 }
37322 else
37323 clause = error_mark_node;
37324 }
37325 }
37326 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
37327 {
37328 clause = error_mark_node;
37329 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
37330 }
37331 cp_parser_require_pragma_eol (parser, pragma_tok);
37332
37333 finish_omp_depobj (loc, depobj, kind, clause);
37334 }
37335
37336
37337 /* OpenMP 2.5:
37338 # pragma omp flush flush-vars[opt] new-line
37339
37340 flush-vars:
37341 ( variable-list )
37342
37343 OpenMP 5.0:
37344 # pragma omp flush memory-order-clause new-line */
37345
37346 static void
37347 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
37348 {
37349 enum memmodel mo = MEMMODEL_LAST;
37350 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37351 {
37352 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37353 const char *p = IDENTIFIER_POINTER (id);
37354 if (!strcmp (p, "acq_rel"))
37355 mo = MEMMODEL_ACQ_REL;
37356 else if (!strcmp (p, "release"))
37357 mo = MEMMODEL_RELEASE;
37358 else if (!strcmp (p, "acquire"))
37359 mo = MEMMODEL_ACQUIRE;
37360 else
37361 error_at (cp_lexer_peek_token (parser->lexer)->location,
37362 "expected %<acq_rel%>, %<release%> or %<acquire%>");
37363 cp_lexer_consume_token (parser->lexer);
37364 }
37365 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37366 {
37367 if (mo != MEMMODEL_LAST)
37368 error_at (cp_lexer_peek_token (parser->lexer)->location,
37369 "%<flush%> list specified together with memory order "
37370 "clause");
37371 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37372 }
37373 cp_parser_require_pragma_eol (parser, pragma_tok);
37374
37375 finish_omp_flush (mo);
37376 }
37377
37378 /* Helper function, to parse omp for increment expression. */
37379
37380 static tree
37381 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
37382 {
37383 tree cond = cp_parser_binary_expression (parser, false, true,
37384 PREC_NOT_OPERATOR, NULL);
37385 if (cond == error_mark_node
37386 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37387 {
37388 cp_parser_skip_to_end_of_statement (parser);
37389 return error_mark_node;
37390 }
37391
37392 switch (TREE_CODE (cond))
37393 {
37394 case GT_EXPR:
37395 case GE_EXPR:
37396 case LT_EXPR:
37397 case LE_EXPR:
37398 break;
37399 case NE_EXPR:
37400 if (code != OACC_LOOP)
37401 break;
37402 gcc_fallthrough ();
37403 default:
37404 return error_mark_node;
37405 }
37406
37407 /* If decl is an iterator, preserve LHS and RHS of the relational
37408 expr until finish_omp_for. */
37409 if (decl
37410 && (type_dependent_expression_p (decl)
37411 || CLASS_TYPE_P (TREE_TYPE (decl))))
37412 return cond;
37413
37414 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
37415 TREE_CODE (cond),
37416 TREE_OPERAND (cond, 0), ERROR_MARK,
37417 TREE_OPERAND (cond, 1), ERROR_MARK,
37418 /*overload=*/NULL, tf_warning_or_error);
37419 }
37420
37421 /* Helper function, to parse omp for increment expression. */
37422
37423 static tree
37424 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
37425 {
37426 cp_token *token = cp_lexer_peek_token (parser->lexer);
37427 enum tree_code op;
37428 tree lhs, rhs;
37429 cp_id_kind idk;
37430 bool decl_first;
37431
37432 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
37433 {
37434 op = (token->type == CPP_PLUS_PLUS
37435 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
37436 cp_lexer_consume_token (parser->lexer);
37437 lhs = cp_parser_simple_cast_expression (parser);
37438 if (lhs != decl
37439 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
37440 return error_mark_node;
37441 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
37442 }
37443
37444 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
37445 if (lhs != decl
37446 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
37447 return error_mark_node;
37448
37449 token = cp_lexer_peek_token (parser->lexer);
37450 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
37451 {
37452 op = (token->type == CPP_PLUS_PLUS
37453 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
37454 cp_lexer_consume_token (parser->lexer);
37455 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
37456 }
37457
37458 op = cp_parser_assignment_operator_opt (parser);
37459 if (op == ERROR_MARK)
37460 return error_mark_node;
37461
37462 if (op != NOP_EXPR)
37463 {
37464 rhs = cp_parser_assignment_expression (parser);
37465 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
37466 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
37467 }
37468
37469 lhs = cp_parser_binary_expression (parser, false, false,
37470 PREC_ADDITIVE_EXPRESSION, NULL);
37471 token = cp_lexer_peek_token (parser->lexer);
37472 decl_first = (lhs == decl
37473 || (processing_template_decl && cp_tree_equal (lhs, decl)));
37474 if (decl_first)
37475 lhs = NULL_TREE;
37476 if (token->type != CPP_PLUS
37477 && token->type != CPP_MINUS)
37478 return error_mark_node;
37479
37480 do
37481 {
37482 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
37483 cp_lexer_consume_token (parser->lexer);
37484 rhs = cp_parser_binary_expression (parser, false, false,
37485 PREC_ADDITIVE_EXPRESSION, NULL);
37486 token = cp_lexer_peek_token (parser->lexer);
37487 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
37488 {
37489 if (lhs == NULL_TREE)
37490 {
37491 if (op == PLUS_EXPR)
37492 lhs = rhs;
37493 else
37494 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
37495 tf_warning_or_error);
37496 }
37497 else
37498 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
37499 ERROR_MARK, NULL, tf_warning_or_error);
37500 }
37501 }
37502 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
37503
37504 if (!decl_first)
37505 {
37506 if ((rhs != decl
37507 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
37508 || op == MINUS_EXPR)
37509 return error_mark_node;
37510 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
37511 }
37512 else
37513 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
37514
37515 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
37516 }
37517
37518 /* Parse the initialization statement of an OpenMP for loop.
37519
37520 Return true if the resulting construct should have an
37521 OMP_CLAUSE_PRIVATE added to it. */
37522
37523 static tree
37524 cp_parser_omp_for_loop_init (cp_parser *parser,
37525 tree &this_pre_body,
37526 releasing_vec &for_block,
37527 tree &init,
37528 tree &orig_init,
37529 tree &decl,
37530 tree &real_decl)
37531 {
37532 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37533 return NULL_TREE;
37534
37535 tree add_private_clause = NULL_TREE;
37536
37537 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
37538
37539 init-expr:
37540 var = lb
37541 integer-type var = lb
37542 random-access-iterator-type var = lb
37543 pointer-type var = lb
37544 */
37545 cp_decl_specifier_seq type_specifiers;
37546
37547 /* First, try to parse as an initialized declaration. See
37548 cp_parser_condition, from whence the bulk of this is copied. */
37549
37550 cp_parser_parse_tentatively (parser);
37551 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
37552 /*is_declaration=*/true,
37553 /*is_trailing_return=*/false,
37554 &type_specifiers);
37555 if (cp_parser_parse_definitely (parser))
37556 {
37557 /* If parsing a type specifier seq succeeded, then this
37558 MUST be a initialized declaration. */
37559 tree asm_specification, attributes;
37560 cp_declarator *declarator;
37561
37562 declarator = cp_parser_declarator (parser,
37563 CP_PARSER_DECLARATOR_NAMED,
37564 CP_PARSER_FLAGS_NONE,
37565 /*ctor_dtor_or_conv_p=*/NULL,
37566 /*parenthesized_p=*/NULL,
37567 /*member_p=*/false,
37568 /*friend_p=*/false,
37569 /*static_p=*/false);
37570 attributes = cp_parser_attributes_opt (parser);
37571 asm_specification = cp_parser_asm_specification_opt (parser);
37572
37573 if (declarator == cp_error_declarator)
37574 cp_parser_skip_to_end_of_statement (parser);
37575
37576 else
37577 {
37578 tree pushed_scope, auto_node;
37579
37580 decl = start_decl (declarator, &type_specifiers,
37581 SD_INITIALIZED, attributes,
37582 /*prefix_attributes=*/NULL_TREE,
37583 &pushed_scope);
37584
37585 auto_node = type_uses_auto (TREE_TYPE (decl));
37586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
37587 {
37588 if (cp_lexer_next_token_is (parser->lexer,
37589 CPP_OPEN_PAREN))
37590 error ("parenthesized initialization is not allowed in "
37591 "OpenMP %<for%> loop");
37592 else
37593 /* Trigger an error. */
37594 cp_parser_require (parser, CPP_EQ, RT_EQ);
37595
37596 init = error_mark_node;
37597 cp_parser_skip_to_end_of_statement (parser);
37598 }
37599 else if (CLASS_TYPE_P (TREE_TYPE (decl))
37600 || type_dependent_expression_p (decl)
37601 || auto_node)
37602 {
37603 bool is_direct_init, is_non_constant_init;
37604
37605 init = cp_parser_initializer (parser,
37606 &is_direct_init,
37607 &is_non_constant_init);
37608
37609 if (auto_node)
37610 {
37611 TREE_TYPE (decl)
37612 = do_auto_deduction (TREE_TYPE (decl), init,
37613 auto_node);
37614
37615 if (!CLASS_TYPE_P (TREE_TYPE (decl))
37616 && !type_dependent_expression_p (decl))
37617 goto non_class;
37618 }
37619
37620 cp_finish_decl (decl, init, !is_non_constant_init,
37621 asm_specification,
37622 LOOKUP_ONLYCONVERTING);
37623 orig_init = init;
37624 if (CLASS_TYPE_P (TREE_TYPE (decl)))
37625 {
37626 vec_safe_push (for_block, this_pre_body);
37627 init = NULL_TREE;
37628 }
37629 else
37630 {
37631 init = pop_stmt_list (this_pre_body);
37632 if (init && TREE_CODE (init) == STATEMENT_LIST)
37633 {
37634 tree_stmt_iterator i = tsi_start (init);
37635 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
37636 while (!tsi_end_p (i))
37637 {
37638 tree t = tsi_stmt (i);
37639 if (TREE_CODE (t) == DECL_EXPR
37640 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
37641 {
37642 tsi_delink (&i);
37643 vec_safe_push (for_block, t);
37644 continue;
37645 }
37646 break;
37647 }
37648 if (tsi_one_before_end_p (i))
37649 {
37650 tree t = tsi_stmt (i);
37651 tsi_delink (&i);
37652 free_stmt_list (init);
37653 init = t;
37654 }
37655 }
37656 }
37657 this_pre_body = NULL_TREE;
37658 }
37659 else
37660 {
37661 /* Consume '='. */
37662 cp_lexer_consume_token (parser->lexer);
37663 init = cp_parser_assignment_expression (parser);
37664
37665 non_class:
37666 if (TYPE_REF_P (TREE_TYPE (decl)))
37667 init = error_mark_node;
37668 else
37669 cp_finish_decl (decl, NULL_TREE,
37670 /*init_const_expr_p=*/false,
37671 asm_specification,
37672 LOOKUP_ONLYCONVERTING);
37673 }
37674
37675 if (pushed_scope)
37676 pop_scope (pushed_scope);
37677 }
37678 }
37679 else
37680 {
37681 cp_id_kind idk;
37682 /* If parsing a type specifier sequence failed, then
37683 this MUST be a simple expression. */
37684 cp_parser_parse_tentatively (parser);
37685 decl = cp_parser_primary_expression (parser, false, false,
37686 false, &idk);
37687 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
37688 if (!cp_parser_error_occurred (parser)
37689 && decl
37690 && (TREE_CODE (decl) == COMPONENT_REF
37691 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
37692 {
37693 cp_parser_abort_tentative_parse (parser);
37694 cp_parser_parse_tentatively (parser);
37695 cp_token *token = cp_lexer_peek_token (parser->lexer);
37696 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
37697 /*check_dependency_p=*/true,
37698 /*template_p=*/NULL,
37699 /*declarator_p=*/false,
37700 /*optional_p=*/false);
37701 if (name != error_mark_node
37702 && last_tok == cp_lexer_peek_token (parser->lexer))
37703 {
37704 decl = cp_parser_lookup_name_simple (parser, name,
37705 token->location);
37706 if (TREE_CODE (decl) == FIELD_DECL)
37707 add_private_clause = omp_privatize_field (decl, false);
37708 }
37709 cp_parser_abort_tentative_parse (parser);
37710 cp_parser_parse_tentatively (parser);
37711 decl = cp_parser_primary_expression (parser, false, false,
37712 false, &idk);
37713 }
37714 if (!cp_parser_error_occurred (parser)
37715 && decl
37716 && DECL_P (decl)
37717 && CLASS_TYPE_P (TREE_TYPE (decl)))
37718 {
37719 tree rhs;
37720
37721 cp_parser_parse_definitely (parser);
37722 cp_parser_require (parser, CPP_EQ, RT_EQ);
37723 rhs = cp_parser_assignment_expression (parser);
37724 orig_init = rhs;
37725 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
37726 decl, NOP_EXPR,
37727 rhs,
37728 tf_warning_or_error));
37729 if (!add_private_clause)
37730 add_private_clause = decl;
37731 }
37732 else
37733 {
37734 decl = NULL;
37735 cp_parser_abort_tentative_parse (parser);
37736 init = cp_parser_expression (parser);
37737 if (init)
37738 {
37739 if (TREE_CODE (init) == MODIFY_EXPR
37740 || TREE_CODE (init) == MODOP_EXPR)
37741 real_decl = TREE_OPERAND (init, 0);
37742 }
37743 }
37744 }
37745 return add_private_clause;
37746 }
37747
37748 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
37749
37750 void
37751 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
37752 tree &decl, tree &orig_decl, tree &init,
37753 tree &orig_init, tree &cond, tree &incr)
37754 {
37755 tree begin, end, range_temp_decl = NULL_TREE;
37756 tree iter_type, begin_expr, end_expr;
37757
37758 if (processing_template_decl)
37759 {
37760 if (check_for_bare_parameter_packs (init))
37761 init = error_mark_node;
37762 if (!type_dependent_expression_p (init)
37763 /* do_auto_deduction doesn't mess with template init-lists. */
37764 && !BRACE_ENCLOSED_INITIALIZER_P (init))
37765 {
37766 tree d = decl;
37767 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
37768 {
37769 tree v = DECL_VALUE_EXPR (decl);
37770 if (TREE_CODE (v) == ARRAY_REF
37771 && VAR_P (TREE_OPERAND (v, 0))
37772 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
37773 d = TREE_OPERAND (v, 0);
37774 }
37775 do_range_for_auto_deduction (d, init);
37776 }
37777 cond = global_namespace;
37778 incr = NULL_TREE;
37779 orig_init = init;
37780 if (this_pre_body)
37781 this_pre_body = pop_stmt_list (this_pre_body);
37782 return;
37783 }
37784
37785 init = mark_lvalue_use (init);
37786
37787 if (decl == error_mark_node || init == error_mark_node)
37788 /* If an error happened previously do nothing or else a lot of
37789 unhelpful errors would be issued. */
37790 begin_expr = end_expr = iter_type = error_mark_node;
37791 else
37792 {
37793 tree range_temp;
37794
37795 if (VAR_P (init)
37796 && array_of_runtime_bound_p (TREE_TYPE (init)))
37797 /* Can't bind a reference to an array of runtime bound. */
37798 range_temp = init;
37799 else
37800 {
37801 range_temp = build_range_temp (init);
37802 DECL_NAME (range_temp) = NULL_TREE;
37803 pushdecl (range_temp);
37804 cp_finish_decl (range_temp, init,
37805 /*is_constant_init*/false, NULL_TREE,
37806 LOOKUP_ONLYCONVERTING);
37807 range_temp_decl = range_temp;
37808 range_temp = convert_from_reference (range_temp);
37809 }
37810 iter_type = cp_parser_perform_range_for_lookup (range_temp,
37811 &begin_expr, &end_expr);
37812 }
37813
37814 tree end_iter_type = iter_type;
37815 if (cxx_dialect >= cxx17)
37816 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
37817 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
37818 TREE_USED (end) = 1;
37819 DECL_ARTIFICIAL (end) = 1;
37820 pushdecl (end);
37821 cp_finish_decl (end, end_expr,
37822 /*is_constant_init*/false, NULL_TREE,
37823 LOOKUP_ONLYCONVERTING);
37824
37825 /* The new for initialization statement. */
37826 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
37827 TREE_USED (begin) = 1;
37828 DECL_ARTIFICIAL (begin) = 1;
37829 pushdecl (begin);
37830 orig_init = init;
37831 if (CLASS_TYPE_P (iter_type))
37832 init = NULL_TREE;
37833 else
37834 {
37835 init = begin_expr;
37836 begin_expr = NULL_TREE;
37837 }
37838 cp_finish_decl (begin, begin_expr,
37839 /*is_constant_init*/false, NULL_TREE,
37840 LOOKUP_ONLYCONVERTING);
37841
37842 /* The new for condition. */
37843 if (CLASS_TYPE_P (iter_type))
37844 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
37845 else
37846 cond = build_x_binary_op (input_location, NE_EXPR,
37847 begin, ERROR_MARK,
37848 end, ERROR_MARK,
37849 NULL, tf_warning_or_error);
37850
37851 /* The new increment expression. */
37852 if (CLASS_TYPE_P (iter_type))
37853 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
37854 else
37855 incr = finish_unary_op_expr (input_location,
37856 PREINCREMENT_EXPR, begin,
37857 tf_warning_or_error);
37858
37859 orig_decl = decl;
37860 decl = begin;
37861 if (for_block)
37862 {
37863 vec_safe_push (for_block, this_pre_body);
37864 this_pre_body = NULL_TREE;
37865 }
37866
37867 tree decomp_first_name = NULL_TREE;
37868 unsigned decomp_cnt = 0;
37869 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
37870 {
37871 tree v = DECL_VALUE_EXPR (orig_decl);
37872 if (TREE_CODE (v) == ARRAY_REF
37873 && VAR_P (TREE_OPERAND (v, 0))
37874 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
37875 {
37876 tree d = orig_decl;
37877 orig_decl = TREE_OPERAND (v, 0);
37878 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
37879 decomp_first_name = d;
37880 }
37881 }
37882
37883 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
37884 if (auto_node)
37885 {
37886 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
37887 tf_none);
37888 if (!error_operand_p (t))
37889 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
37890 t, auto_node);
37891 }
37892
37893 tree v = make_tree_vec (decomp_cnt + 3);
37894 TREE_VEC_ELT (v, 0) = range_temp_decl;
37895 TREE_VEC_ELT (v, 1) = end;
37896 TREE_VEC_ELT (v, 2) = orig_decl;
37897 for (unsigned i = 0; i < decomp_cnt; i++)
37898 {
37899 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
37900 decomp_first_name = DECL_CHAIN (decomp_first_name);
37901 }
37902 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
37903 }
37904
37905 /* Helper for cp_parser_omp_for_loop, finalize part of range for
37906 inside of the collapsed body. */
37907
37908 void
37909 cp_finish_omp_range_for (tree orig, tree begin)
37910 {
37911 gcc_assert (TREE_CODE (orig) == TREE_LIST
37912 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
37913 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
37914 tree decomp_first_name = NULL_TREE;
37915 unsigned int decomp_cnt = 0;
37916
37917 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
37918 {
37919 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
37920 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
37921 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
37922 }
37923
37924 /* The declaration is initialized with *__begin inside the loop body. */
37925 cp_finish_decl (decl,
37926 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
37927 tf_warning_or_error),
37928 /*is_constant_init*/false, NULL_TREE,
37929 LOOKUP_ONLYCONVERTING);
37930 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
37931 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
37932 }
37933
37934 /* OpenMP 5.0:
37935
37936 scan-loop-body:
37937 { structured-block scan-directive structured-block } */
37938
37939 static void
37940 cp_parser_omp_scan_loop_body (cp_parser *parser)
37941 {
37942 tree substmt, clauses = NULL_TREE;
37943
37944 matching_braces braces;
37945 if (!braces.require_open (parser))
37946 return;
37947
37948 substmt = cp_parser_omp_structured_block (parser, NULL);
37949 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
37950 add_stmt (substmt);
37951
37952 cp_token *tok = cp_lexer_peek_token (parser->lexer);
37953 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
37954 {
37955 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
37956
37957 cp_lexer_consume_token (parser->lexer);
37958
37959 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37960 {
37961 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37962 const char *p = IDENTIFIER_POINTER (id);
37963 if (strcmp (p, "inclusive") == 0)
37964 clause = OMP_CLAUSE_INCLUSIVE;
37965 else if (strcmp (p, "exclusive") == 0)
37966 clause = OMP_CLAUSE_EXCLUSIVE;
37967 }
37968 if (clause != OMP_CLAUSE_ERROR)
37969 {
37970 cp_lexer_consume_token (parser->lexer);
37971 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
37972 }
37973 else
37974 cp_parser_error (parser, "expected %<inclusive%> or "
37975 "%<exclusive%> clause");
37976
37977 cp_parser_require_pragma_eol (parser, tok);
37978 }
37979 else
37980 error ("expected %<#pragma omp scan%>");
37981
37982 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37983 substmt = cp_parser_omp_structured_block (parser, NULL);
37984 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
37985 clauses);
37986 add_stmt (substmt);
37987
37988 braces.require_close (parser);
37989 }
37990
37991 /* Parse the restricted form of the for statement allowed by OpenMP. */
37992
37993 static tree
37994 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
37995 tree *cclauses, bool *if_p)
37996 {
37997 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
37998 tree orig_decl;
37999 tree real_decl, initv, condv, incrv, declv, orig_declv;
38000 tree this_pre_body, cl, ordered_cl = NULL_TREE;
38001 location_t loc_first;
38002 bool collapse_err = false;
38003 int i, collapse = 1, ordered = 0, count, nbraces = 0;
38004 releasing_vec for_block;
38005 auto_vec<tree, 4> orig_inits;
38006 bool tiling = false;
38007 bool inscan = false;
38008
38009 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
38010 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
38011 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
38012 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
38013 {
38014 tiling = true;
38015 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
38016 }
38017 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
38018 && OMP_CLAUSE_ORDERED_EXPR (cl))
38019 {
38020 ordered_cl = cl;
38021 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
38022 }
38023 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
38024 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
38025 && (code == OMP_SIMD || code == OMP_FOR))
38026 inscan = true;
38027
38028 if (ordered && ordered < collapse)
38029 {
38030 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
38031 "%<ordered%> clause parameter is less than %<collapse%>");
38032 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
38033 = build_int_cst (NULL_TREE, collapse);
38034 ordered = collapse;
38035 }
38036 if (ordered)
38037 {
38038 for (tree *pc = &clauses; *pc; )
38039 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
38040 {
38041 error_at (OMP_CLAUSE_LOCATION (*pc),
38042 "%<linear%> clause may not be specified together "
38043 "with %<ordered%> clause with a parameter");
38044 *pc = OMP_CLAUSE_CHAIN (*pc);
38045 }
38046 else
38047 pc = &OMP_CLAUSE_CHAIN (*pc);
38048 }
38049
38050 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
38051 count = ordered ? ordered : collapse;
38052
38053 declv = make_tree_vec (count);
38054 initv = make_tree_vec (count);
38055 condv = make_tree_vec (count);
38056 incrv = make_tree_vec (count);
38057 orig_declv = NULL_TREE;
38058
38059 loc_first = cp_lexer_peek_token (parser->lexer)->location;
38060
38061 for (i = 0; i < count; i++)
38062 {
38063 int bracecount = 0;
38064 tree add_private_clause = NULL_TREE;
38065 location_t loc;
38066
38067 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
38068 {
38069 if (!collapse_err)
38070 cp_parser_error (parser, "for statement expected");
38071 return NULL;
38072 }
38073 loc = cp_lexer_consume_token (parser->lexer)->location;
38074
38075 /* Don't create location wrapper nodes within an OpenMP "for"
38076 statement. */
38077 auto_suppress_location_wrappers sentinel;
38078
38079 matching_parens parens;
38080 if (!parens.require_open (parser))
38081 return NULL;
38082
38083 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
38084 this_pre_body = push_stmt_list ();
38085
38086 if (code != OACC_LOOP && cxx_dialect >= cxx11)
38087 {
38088 /* Save tokens so that we can put them back. */
38089 cp_lexer_save_tokens (parser->lexer);
38090
38091 /* Look for ':' that is not nested in () or {}. */
38092 bool is_range_for
38093 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
38094 /*recovering=*/false,
38095 CPP_COLON,
38096 /*consume_paren=*/
38097 false) == -1);
38098
38099 /* Roll back the tokens we skipped. */
38100 cp_lexer_rollback_tokens (parser->lexer);
38101
38102 if (is_range_for)
38103 {
38104 bool saved_colon_corrects_to_scope_p
38105 = parser->colon_corrects_to_scope_p;
38106
38107 /* A colon is used in range-based for. */
38108 parser->colon_corrects_to_scope_p = false;
38109
38110 /* Parse the declaration. */
38111 cp_parser_simple_declaration (parser,
38112 /*function_definition_allowed_p=*/
38113 false, &decl);
38114 parser->colon_corrects_to_scope_p
38115 = saved_colon_corrects_to_scope_p;
38116
38117 cp_parser_require (parser, CPP_COLON, RT_COLON);
38118
38119 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
38120 false, 0, true);
38121
38122 cp_convert_omp_range_for (this_pre_body, for_block, decl,
38123 orig_decl, init, orig_init,
38124 cond, incr);
38125 if (this_pre_body)
38126 {
38127 if (pre_body)
38128 {
38129 tree t = pre_body;
38130 pre_body = push_stmt_list ();
38131 add_stmt (t);
38132 add_stmt (this_pre_body);
38133 pre_body = pop_stmt_list (pre_body);
38134 }
38135 else
38136 pre_body = this_pre_body;
38137 }
38138
38139 if (ordered_cl)
38140 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
38141 "%<ordered%> clause with parameter on "
38142 "range-based %<for%> loop");
38143
38144 goto parse_close_paren;
38145 }
38146 }
38147
38148 add_private_clause
38149 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
38150 init, orig_init, decl, real_decl);
38151
38152 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38153 if (this_pre_body)
38154 {
38155 this_pre_body = pop_stmt_list (this_pre_body);
38156 if (pre_body)
38157 {
38158 tree t = pre_body;
38159 pre_body = push_stmt_list ();
38160 add_stmt (t);
38161 add_stmt (this_pre_body);
38162 pre_body = pop_stmt_list (pre_body);
38163 }
38164 else
38165 pre_body = this_pre_body;
38166 }
38167
38168 if (decl)
38169 real_decl = decl;
38170 if (cclauses != NULL
38171 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
38172 && real_decl != NULL_TREE
38173 && code != OMP_LOOP)
38174 {
38175 tree *c;
38176 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
38177 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
38178 && OMP_CLAUSE_DECL (*c) == real_decl)
38179 {
38180 error_at (loc, "iteration variable %qD"
38181 " should not be firstprivate", real_decl);
38182 *c = OMP_CLAUSE_CHAIN (*c);
38183 }
38184 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
38185 && OMP_CLAUSE_DECL (*c) == real_decl)
38186 {
38187 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
38188 tree l = *c;
38189 *c = OMP_CLAUSE_CHAIN (*c);
38190 if (code == OMP_SIMD)
38191 {
38192 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
38193 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
38194 }
38195 else
38196 {
38197 OMP_CLAUSE_CHAIN (l) = clauses;
38198 clauses = l;
38199 }
38200 add_private_clause = NULL_TREE;
38201 }
38202 else
38203 {
38204 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
38205 && OMP_CLAUSE_DECL (*c) == real_decl)
38206 add_private_clause = NULL_TREE;
38207 c = &OMP_CLAUSE_CHAIN (*c);
38208 }
38209 }
38210
38211 if (add_private_clause)
38212 {
38213 tree c;
38214 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
38215 {
38216 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
38217 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
38218 && OMP_CLAUSE_DECL (c) == decl)
38219 break;
38220 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
38221 && OMP_CLAUSE_DECL (c) == decl)
38222 error_at (loc, "iteration variable %qD "
38223 "should not be firstprivate",
38224 decl);
38225 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
38226 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
38227 && OMP_CLAUSE_DECL (c) == decl)
38228 error_at (loc, "iteration variable %qD should not be reduction",
38229 decl);
38230 }
38231 if (c == NULL)
38232 {
38233 if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
38234 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
38235 else if (code != OMP_SIMD)
38236 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
38237 else
38238 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38239 OMP_CLAUSE_DECL (c) = add_private_clause;
38240 c = finish_omp_clauses (c, C_ORT_OMP);
38241 if (c)
38242 {
38243 OMP_CLAUSE_CHAIN (c) = clauses;
38244 clauses = c;
38245 /* For linear, signal that we need to fill up
38246 the so far unknown linear step. */
38247 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
38248 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
38249 }
38250 }
38251 }
38252
38253 cond = NULL;
38254 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
38255 cond = cp_parser_omp_for_cond (parser, decl, code);
38256 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38257
38258 incr = NULL;
38259 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
38260 {
38261 /* If decl is an iterator, preserve the operator on decl
38262 until finish_omp_for. */
38263 if (real_decl
38264 && ((processing_template_decl
38265 && (TREE_TYPE (real_decl) == NULL_TREE
38266 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
38267 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
38268 incr = cp_parser_omp_for_incr (parser, real_decl);
38269 else
38270 incr = cp_parser_expression (parser);
38271 if (!EXPR_HAS_LOCATION (incr))
38272 protected_set_expr_location (incr, input_location);
38273 }
38274
38275 parse_close_paren:
38276 if (!parens.require_close (parser))
38277 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38278 /*or_comma=*/false,
38279 /*consume_paren=*/true);
38280
38281 TREE_VEC_ELT (declv, i) = decl;
38282 TREE_VEC_ELT (initv, i) = init;
38283 TREE_VEC_ELT (condv, i) = cond;
38284 TREE_VEC_ELT (incrv, i) = incr;
38285 if (orig_init)
38286 {
38287 orig_inits.safe_grow_cleared (i + 1);
38288 orig_inits[i] = orig_init;
38289 }
38290 if (orig_decl)
38291 {
38292 if (!orig_declv)
38293 orig_declv = copy_node (declv);
38294 TREE_VEC_ELT (orig_declv, i) = orig_decl;
38295 }
38296 else if (orig_declv)
38297 TREE_VEC_ELT (orig_declv, i) = decl;
38298
38299 if (i == count - 1)
38300 break;
38301
38302 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
38303 in between the collapsed for loops to be still considered perfectly
38304 nested. Hopefully the final version clarifies this.
38305 For now handle (multiple) {'s and empty statements. */
38306 cp_parser_parse_tentatively (parser);
38307 for (;;)
38308 {
38309 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
38310 break;
38311 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
38312 {
38313 cp_lexer_consume_token (parser->lexer);
38314 bracecount++;
38315 }
38316 else if (bracecount
38317 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
38318 cp_lexer_consume_token (parser->lexer);
38319 else
38320 {
38321 loc = cp_lexer_peek_token (parser->lexer)->location;
38322 error_at (loc, "not enough for loops to collapse");
38323 collapse_err = true;
38324 cp_parser_abort_tentative_parse (parser);
38325 declv = NULL_TREE;
38326 break;
38327 }
38328 }
38329
38330 if (declv)
38331 {
38332 cp_parser_parse_definitely (parser);
38333 nbraces += bracecount;
38334 }
38335 }
38336
38337 if (nbraces)
38338 if_p = NULL;
38339
38340 /* Note that we saved the original contents of this flag when we entered
38341 the structured block, and so we don't need to re-save it here. */
38342 parser->in_statement = IN_OMP_FOR;
38343
38344 /* Note that the grammar doesn't call for a structured block here,
38345 though the loop as a whole is a structured block. */
38346 if (orig_declv)
38347 {
38348 body = begin_omp_structured_block ();
38349 for (i = 0; i < count; i++)
38350 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
38351 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
38352 TREE_VEC_ELT (declv, i));
38353 }
38354 else
38355 body = push_stmt_list ();
38356 if (inscan)
38357 cp_parser_omp_scan_loop_body (parser);
38358 else
38359 cp_parser_statement (parser, NULL_TREE, false, if_p);
38360 if (orig_declv)
38361 body = finish_omp_structured_block (body);
38362 else
38363 body = pop_stmt_list (body);
38364
38365 if (declv == NULL_TREE)
38366 ret = NULL_TREE;
38367 else
38368 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
38369 incrv, body, pre_body, &orig_inits, clauses);
38370
38371 while (nbraces)
38372 {
38373 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
38374 {
38375 cp_lexer_consume_token (parser->lexer);
38376 nbraces--;
38377 }
38378 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
38379 cp_lexer_consume_token (parser->lexer);
38380 else
38381 {
38382 if (!collapse_err)
38383 {
38384 error_at (cp_lexer_peek_token (parser->lexer)->location,
38385 "collapsed loops not perfectly nested");
38386 }
38387 collapse_err = true;
38388 cp_parser_statement_seq_opt (parser, NULL);
38389 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
38390 break;
38391 }
38392 }
38393
38394 while (!for_block->is_empty ())
38395 {
38396 tree t = for_block->pop ();
38397 if (TREE_CODE (t) == STATEMENT_LIST)
38398 add_stmt (pop_stmt_list (t));
38399 else
38400 add_stmt (t);
38401 }
38402
38403 return ret;
38404 }
38405
38406 /* Helper function for OpenMP parsing, split clauses and call
38407 finish_omp_clauses on each of the set of clauses afterwards. */
38408
38409 static void
38410 cp_omp_split_clauses (location_t loc, enum tree_code code,
38411 omp_clause_mask mask, tree clauses, tree *cclauses)
38412 {
38413 int i;
38414 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
38415 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
38416 if (cclauses[i])
38417 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
38418 }
38419
38420 /* OpenMP 5.0:
38421 #pragma omp loop loop-clause[optseq] new-line
38422 for-loop */
38423
38424 #define OMP_LOOP_CLAUSE_MASK \
38425 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
38429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
38430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
38431
38432 static tree
38433 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
38434 char *p_name, omp_clause_mask mask, tree *cclauses,
38435 bool *if_p)
38436 {
38437 tree clauses, sb, ret;
38438 unsigned int save;
38439 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38440
38441 strcat (p_name, " loop");
38442 mask |= OMP_LOOP_CLAUSE_MASK;
38443
38444 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38445 cclauses == NULL);
38446 if (cclauses)
38447 {
38448 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
38449 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
38450 }
38451
38452 keep_next_level (true);
38453 sb = begin_omp_structured_block ();
38454 save = cp_parser_begin_omp_structured_block (parser);
38455
38456 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
38457
38458 cp_parser_end_omp_structured_block (parser, save);
38459 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
38460
38461 return ret;
38462 }
38463
38464 /* OpenMP 4.0:
38465 #pragma omp simd simd-clause[optseq] new-line
38466 for-loop */
38467
38468 #define OMP_SIMD_CLAUSE_MASK \
38469 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
38470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
38477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
38479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
38480
38481 static tree
38482 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
38483 char *p_name, omp_clause_mask mask, tree *cclauses,
38484 bool *if_p)
38485 {
38486 tree clauses, sb, ret;
38487 unsigned int save;
38488 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38489
38490 strcat (p_name, " simd");
38491 mask |= OMP_SIMD_CLAUSE_MASK;
38492
38493 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38494 cclauses == NULL);
38495 if (cclauses)
38496 {
38497 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
38498 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
38499 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
38500 OMP_CLAUSE_ORDERED);
38501 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
38502 {
38503 error_at (OMP_CLAUSE_LOCATION (c),
38504 "%<ordered%> clause with parameter may not be specified "
38505 "on %qs construct", p_name);
38506 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
38507 }
38508 }
38509
38510 keep_next_level (true);
38511 sb = begin_omp_structured_block ();
38512 save = cp_parser_begin_omp_structured_block (parser);
38513
38514 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
38515
38516 cp_parser_end_omp_structured_block (parser, save);
38517 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
38518
38519 return ret;
38520 }
38521
38522 /* OpenMP 2.5:
38523 #pragma omp for for-clause[optseq] new-line
38524 for-loop
38525
38526 OpenMP 4.0:
38527 #pragma omp for simd for-simd-clause[optseq] new-line
38528 for-loop */
38529
38530 #define OMP_FOR_CLAUSE_MASK \
38531 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
38537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
38538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
38540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
38541
38542 static tree
38543 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
38544 char *p_name, omp_clause_mask mask, tree *cclauses,
38545 bool *if_p)
38546 {
38547 tree clauses, sb, ret;
38548 unsigned int save;
38549 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38550
38551 strcat (p_name, " for");
38552 mask |= OMP_FOR_CLAUSE_MASK;
38553 /* parallel for{, simd} disallows nowait clause, but for
38554 target {teams distribute ,}parallel for{, simd} it should be accepted. */
38555 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
38556 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
38557 /* Composite distribute parallel for{, simd} disallows ordered clause. */
38558 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
38559 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
38560
38561 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38562 {
38563 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38564 const char *p = IDENTIFIER_POINTER (id);
38565
38566 if (strcmp (p, "simd") == 0)
38567 {
38568 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38569 if (cclauses == NULL)
38570 cclauses = cclauses_buf;
38571
38572 cp_lexer_consume_token (parser->lexer);
38573 if (!flag_openmp) /* flag_openmp_simd */
38574 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38575 cclauses, if_p);
38576 sb = begin_omp_structured_block ();
38577 save = cp_parser_begin_omp_structured_block (parser);
38578 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38579 cclauses, if_p);
38580 cp_parser_end_omp_structured_block (parser, save);
38581 tree body = finish_omp_structured_block (sb);
38582 if (ret == NULL)
38583 return ret;
38584 ret = make_node (OMP_FOR);
38585 TREE_TYPE (ret) = void_type_node;
38586 OMP_FOR_BODY (ret) = body;
38587 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
38588 SET_EXPR_LOCATION (ret, loc);
38589 add_stmt (ret);
38590 return ret;
38591 }
38592 }
38593 if (!flag_openmp) /* flag_openmp_simd */
38594 {
38595 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38596 return NULL_TREE;
38597 }
38598
38599 /* Composite distribute parallel for disallows linear clause. */
38600 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
38601 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
38602
38603 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38604 cclauses == NULL);
38605 if (cclauses)
38606 {
38607 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
38608 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
38609 }
38610
38611 keep_next_level (true);
38612 sb = begin_omp_structured_block ();
38613 save = cp_parser_begin_omp_structured_block (parser);
38614
38615 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
38616
38617 cp_parser_end_omp_structured_block (parser, save);
38618 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
38619
38620 return ret;
38621 }
38622
38623 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
38624 omp_clause_mask, tree *, bool *);
38625
38626 /* OpenMP 2.5:
38627 # pragma omp master new-line
38628 structured-block */
38629
38630 static tree
38631 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
38632 char *p_name, omp_clause_mask mask, tree *cclauses,
38633 bool *if_p)
38634 {
38635 tree clauses, sb, ret;
38636 unsigned int save;
38637 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38638
38639 strcat (p_name, " master");
38640
38641 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38642 {
38643 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38644 const char *p = IDENTIFIER_POINTER (id);
38645
38646 if (strcmp (p, "taskloop") == 0)
38647 {
38648 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38649 if (cclauses == NULL)
38650 cclauses = cclauses_buf;
38651
38652 cp_lexer_consume_token (parser->lexer);
38653 if (!flag_openmp) /* flag_openmp_simd */
38654 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
38655 cclauses, if_p);
38656 sb = begin_omp_structured_block ();
38657 save = cp_parser_begin_omp_structured_block (parser);
38658 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
38659 cclauses, if_p);
38660 cp_parser_end_omp_structured_block (parser, save);
38661 tree body = finish_omp_structured_block (sb);
38662 if (ret == NULL)
38663 return ret;
38664 return c_finish_omp_master (loc, body);
38665 }
38666 }
38667 if (!flag_openmp) /* flag_openmp_simd */
38668 {
38669 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38670 return NULL_TREE;
38671 }
38672
38673 if (cclauses)
38674 {
38675 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38676 false);
38677 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
38678 }
38679 else
38680 cp_parser_require_pragma_eol (parser, pragma_tok);
38681
38682 return c_finish_omp_master (loc,
38683 cp_parser_omp_structured_block (parser, if_p));
38684 }
38685
38686 /* OpenMP 2.5:
38687 # pragma omp ordered new-line
38688 structured-block
38689
38690 OpenMP 4.5:
38691 # pragma omp ordered ordered-clauses new-line
38692 structured-block */
38693
38694 #define OMP_ORDERED_CLAUSE_MASK \
38695 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
38696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
38697
38698 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
38699 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
38700
38701 static bool
38702 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
38703 enum pragma_context context, bool *if_p)
38704 {
38705 location_t loc = pragma_tok->location;
38706
38707 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38708 {
38709 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38710 const char *p = IDENTIFIER_POINTER (id);
38711
38712 if (strcmp (p, "depend") == 0)
38713 {
38714 if (!flag_openmp) /* flag_openmp_simd */
38715 {
38716 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38717 return false;
38718 }
38719 if (context == pragma_stmt)
38720 {
38721 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
38722 "%<depend%> clause may only be used in compound "
38723 "statements");
38724 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38725 return false;
38726 }
38727 tree clauses
38728 = cp_parser_omp_all_clauses (parser,
38729 OMP_ORDERED_DEPEND_CLAUSE_MASK,
38730 "#pragma omp ordered", pragma_tok);
38731 c_finish_omp_ordered (loc, clauses, NULL_TREE);
38732 return false;
38733 }
38734 }
38735
38736 tree clauses
38737 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
38738 "#pragma omp ordered", pragma_tok);
38739
38740 if (!flag_openmp /* flag_openmp_simd */
38741 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
38742 return false;
38743
38744 c_finish_omp_ordered (loc, clauses,
38745 cp_parser_omp_structured_block (parser, if_p));
38746 return true;
38747 }
38748
38749 /* OpenMP 2.5:
38750
38751 section-scope:
38752 { section-sequence }
38753
38754 section-sequence:
38755 section-directive[opt] structured-block
38756 section-sequence section-directive structured-block */
38757
38758 static tree
38759 cp_parser_omp_sections_scope (cp_parser *parser)
38760 {
38761 tree stmt, substmt;
38762 bool error_suppress = false;
38763 cp_token *tok;
38764
38765 matching_braces braces;
38766 if (!braces.require_open (parser))
38767 return NULL_TREE;
38768
38769 stmt = push_stmt_list ();
38770
38771 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
38772 != PRAGMA_OMP_SECTION)
38773 {
38774 substmt = cp_parser_omp_structured_block (parser, NULL);
38775 substmt = build1 (OMP_SECTION, void_type_node, substmt);
38776 add_stmt (substmt);
38777 }
38778
38779 while (1)
38780 {
38781 tok = cp_lexer_peek_token (parser->lexer);
38782 if (tok->type == CPP_CLOSE_BRACE)
38783 break;
38784 if (tok->type == CPP_EOF)
38785 break;
38786
38787 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
38788 {
38789 cp_lexer_consume_token (parser->lexer);
38790 cp_parser_require_pragma_eol (parser, tok);
38791 error_suppress = false;
38792 }
38793 else if (!error_suppress)
38794 {
38795 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
38796 error_suppress = true;
38797 }
38798
38799 substmt = cp_parser_omp_structured_block (parser, NULL);
38800 substmt = build1 (OMP_SECTION, void_type_node, substmt);
38801 add_stmt (substmt);
38802 }
38803 braces.require_close (parser);
38804
38805 substmt = pop_stmt_list (stmt);
38806
38807 stmt = make_node (OMP_SECTIONS);
38808 TREE_TYPE (stmt) = void_type_node;
38809 OMP_SECTIONS_BODY (stmt) = substmt;
38810
38811 add_stmt (stmt);
38812 return stmt;
38813 }
38814
38815 /* OpenMP 2.5:
38816 # pragma omp sections sections-clause[optseq] newline
38817 sections-scope */
38818
38819 #define OMP_SECTIONS_CLAUSE_MASK \
38820 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38825
38826 static tree
38827 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
38828 char *p_name, omp_clause_mask mask, tree *cclauses)
38829 {
38830 tree clauses, ret;
38831 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38832
38833 strcat (p_name, " sections");
38834 mask |= OMP_SECTIONS_CLAUSE_MASK;
38835 if (cclauses)
38836 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
38837
38838 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38839 cclauses == NULL);
38840 if (cclauses)
38841 {
38842 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
38843 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
38844 }
38845
38846 ret = cp_parser_omp_sections_scope (parser);
38847 if (ret)
38848 OMP_SECTIONS_CLAUSES (ret) = clauses;
38849
38850 return ret;
38851 }
38852
38853 /* OpenMP 2.5:
38854 # pragma omp parallel parallel-clause[optseq] new-line
38855 structured-block
38856 # pragma omp parallel for parallel-for-clause[optseq] new-line
38857 structured-block
38858 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
38859 structured-block
38860
38861 OpenMP 4.0:
38862 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
38863 structured-block */
38864
38865 #define OMP_PARALLEL_CLAUSE_MASK \
38866 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
38870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
38872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
38874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
38875
38876 static tree
38877 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
38878 char *p_name, omp_clause_mask mask, tree *cclauses,
38879 bool *if_p)
38880 {
38881 tree stmt, clauses, block;
38882 unsigned int save;
38883 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38884
38885 strcat (p_name, " parallel");
38886 mask |= OMP_PARALLEL_CLAUSE_MASK;
38887 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
38888 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
38889 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
38890 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
38891
38892 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
38893 {
38894 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38895 if (cclauses == NULL)
38896 cclauses = cclauses_buf;
38897
38898 cp_lexer_consume_token (parser->lexer);
38899 if (!flag_openmp) /* flag_openmp_simd */
38900 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
38901 if_p);
38902 block = begin_omp_parallel ();
38903 save = cp_parser_begin_omp_structured_block (parser);
38904 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
38905 if_p);
38906 cp_parser_end_omp_structured_block (parser, save);
38907 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
38908 block);
38909 if (ret == NULL_TREE)
38910 return ret;
38911 OMP_PARALLEL_COMBINED (stmt) = 1;
38912 return stmt;
38913 }
38914 /* When combined with distribute, parallel has to be followed by for.
38915 #pragma omp target parallel is allowed though. */
38916 else if (cclauses
38917 && (mask & (OMP_CLAUSE_MASK_1
38918 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
38919 {
38920 error_at (loc, "expected %<for%> after %qs", p_name);
38921 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38922 return NULL_TREE;
38923 }
38924 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38925 {
38926 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38927 const char *p = IDENTIFIER_POINTER (id);
38928 if (cclauses == NULL && strcmp (p, "master") == 0)
38929 {
38930 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38931 cclauses = cclauses_buf;
38932
38933 cp_lexer_consume_token (parser->lexer);
38934 block = begin_omp_parallel ();
38935 save = cp_parser_begin_omp_structured_block (parser);
38936 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
38937 cclauses, if_p);
38938 cp_parser_end_omp_structured_block (parser, save);
38939 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
38940 block);
38941 OMP_PARALLEL_COMBINED (stmt) = 1;
38942 if (ret == NULL_TREE)
38943 return ret;
38944 return stmt;
38945 }
38946 else if (strcmp (p, "loop") == 0)
38947 {
38948 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38949 if (cclauses == NULL)
38950 cclauses = cclauses_buf;
38951
38952 cp_lexer_consume_token (parser->lexer);
38953 if (!flag_openmp) /* flag_openmp_simd */
38954 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
38955 cclauses, if_p);
38956 block = begin_omp_parallel ();
38957 save = cp_parser_begin_omp_structured_block (parser);
38958 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
38959 cclauses, if_p);
38960 cp_parser_end_omp_structured_block (parser, save);
38961 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
38962 block);
38963 if (ret == NULL_TREE)
38964 return ret;
38965 OMP_PARALLEL_COMBINED (stmt) = 1;
38966 return stmt;
38967 }
38968 else if (!flag_openmp) /* flag_openmp_simd */
38969 {
38970 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38971 return NULL_TREE;
38972 }
38973 else if (cclauses == NULL && strcmp (p, "sections") == 0)
38974 {
38975 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38976 cclauses = cclauses_buf;
38977
38978 cp_lexer_consume_token (parser->lexer);
38979 block = begin_omp_parallel ();
38980 save = cp_parser_begin_omp_structured_block (parser);
38981 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
38982 cp_parser_end_omp_structured_block (parser, save);
38983 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
38984 block);
38985 OMP_PARALLEL_COMBINED (stmt) = 1;
38986 return stmt;
38987 }
38988 }
38989 else if (!flag_openmp) /* flag_openmp_simd */
38990 {
38991 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38992 return NULL_TREE;
38993 }
38994
38995 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38996 cclauses == NULL);
38997 if (cclauses)
38998 {
38999 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
39000 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
39001 }
39002
39003 block = begin_omp_parallel ();
39004 save = cp_parser_begin_omp_structured_block (parser);
39005 cp_parser_statement (parser, NULL_TREE, false, if_p);
39006 cp_parser_end_omp_structured_block (parser, save);
39007 stmt = finish_omp_parallel (clauses, block);
39008 return stmt;
39009 }
39010
39011 /* OpenMP 2.5:
39012 # pragma omp single single-clause[optseq] new-line
39013 structured-block */
39014
39015 #define OMP_SINGLE_CLAUSE_MASK \
39016 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
39019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39020
39021 static tree
39022 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39023 {
39024 tree stmt = make_node (OMP_SINGLE);
39025 TREE_TYPE (stmt) = void_type_node;
39026 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39027
39028 OMP_SINGLE_CLAUSES (stmt)
39029 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
39030 "#pragma omp single", pragma_tok);
39031 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
39032
39033 return add_stmt (stmt);
39034 }
39035
39036 /* OpenMP 3.0:
39037 # pragma omp task task-clause[optseq] new-line
39038 structured-block */
39039
39040 #define OMP_TASK_CLAUSE_MASK \
39041 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
39050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39052
39053 static tree
39054 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39055 {
39056 tree clauses, block;
39057 unsigned int save;
39058
39059 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
39060 "#pragma omp task", pragma_tok);
39061 block = begin_omp_task ();
39062 save = cp_parser_begin_omp_structured_block (parser);
39063 cp_parser_statement (parser, NULL_TREE, false, if_p);
39064 cp_parser_end_omp_structured_block (parser, save);
39065 return finish_omp_task (clauses, block);
39066 }
39067
39068 /* OpenMP 3.0:
39069 # pragma omp taskwait new-line
39070
39071 OpenMP 5.0:
39072 # pragma omp taskwait taskwait-clause[opt] new-line */
39073
39074 #define OMP_TASKWAIT_CLAUSE_MASK \
39075 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
39076
39077 static void
39078 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
39079 {
39080 tree clauses
39081 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
39082 "#pragma omp taskwait", pragma_tok);
39083
39084 if (clauses)
39085 {
39086 tree stmt = make_node (OMP_TASK);
39087 TREE_TYPE (stmt) = void_node;
39088 OMP_TASK_CLAUSES (stmt) = clauses;
39089 OMP_TASK_BODY (stmt) = NULL_TREE;
39090 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39091 add_stmt (stmt);
39092 }
39093 else
39094 finish_omp_taskwait ();
39095 }
39096
39097 /* OpenMP 3.1:
39098 # pragma omp taskyield new-line */
39099
39100 static void
39101 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
39102 {
39103 cp_parser_require_pragma_eol (parser, pragma_tok);
39104 finish_omp_taskyield ();
39105 }
39106
39107 /* OpenMP 4.0:
39108 # pragma omp taskgroup new-line
39109 structured-block
39110
39111 OpenMP 5.0:
39112 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
39113
39114 #define OMP_TASKGROUP_CLAUSE_MASK \
39115 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
39116
39117 static tree
39118 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39119 {
39120 tree clauses
39121 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
39122 "#pragma omp taskgroup", pragma_tok);
39123 return c_finish_omp_taskgroup (input_location,
39124 cp_parser_omp_structured_block (parser,
39125 if_p),
39126 clauses);
39127 }
39128
39129
39130 /* OpenMP 2.5:
39131 # pragma omp threadprivate (variable-list) */
39132
39133 static void
39134 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
39135 {
39136 tree vars;
39137
39138 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
39139 cp_parser_require_pragma_eol (parser, pragma_tok);
39140
39141 finish_omp_threadprivate (vars);
39142 }
39143
39144 /* OpenMP 4.0:
39145 # pragma omp cancel cancel-clause[optseq] new-line */
39146
39147 #define OMP_CANCEL_CLAUSE_MASK \
39148 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
39149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
39150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
39151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
39152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
39153
39154 static void
39155 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
39156 {
39157 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
39158 "#pragma omp cancel", pragma_tok);
39159 finish_omp_cancel (clauses);
39160 }
39161
39162 /* OpenMP 4.0:
39163 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
39164
39165 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
39166 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
39167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
39168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
39169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
39170
39171 static void
39172 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
39173 enum pragma_context context)
39174 {
39175 tree clauses;
39176 bool point_seen = false;
39177
39178 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39179 {
39180 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39181 const char *p = IDENTIFIER_POINTER (id);
39182
39183 if (strcmp (p, "point") == 0)
39184 {
39185 cp_lexer_consume_token (parser->lexer);
39186 point_seen = true;
39187 }
39188 }
39189 if (!point_seen)
39190 {
39191 cp_parser_error (parser, "expected %<point%>");
39192 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39193 return;
39194 }
39195
39196 if (context != pragma_compound)
39197 {
39198 if (context == pragma_stmt)
39199 error_at (pragma_tok->location,
39200 "%<#pragma %s%> may only be used in compound statements",
39201 "omp cancellation point");
39202 else
39203 cp_parser_error (parser, "expected declaration specifiers");
39204 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39205 return;
39206 }
39207
39208 clauses = cp_parser_omp_all_clauses (parser,
39209 OMP_CANCELLATION_POINT_CLAUSE_MASK,
39210 "#pragma omp cancellation point",
39211 pragma_tok);
39212 finish_omp_cancellation_point (clauses);
39213 }
39214
39215 /* OpenMP 4.0:
39216 #pragma omp distribute distribute-clause[optseq] new-line
39217 for-loop */
39218
39219 #define OMP_DISTRIBUTE_CLAUSE_MASK \
39220 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
39224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
39225
39226 static tree
39227 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
39228 char *p_name, omp_clause_mask mask, tree *cclauses,
39229 bool *if_p)
39230 {
39231 tree clauses, sb, ret;
39232 unsigned int save;
39233 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39234
39235 strcat (p_name, " distribute");
39236 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
39237
39238 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39239 {
39240 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39241 const char *p = IDENTIFIER_POINTER (id);
39242 bool simd = false;
39243 bool parallel = false;
39244
39245 if (strcmp (p, "simd") == 0)
39246 simd = true;
39247 else
39248 parallel = strcmp (p, "parallel") == 0;
39249 if (parallel || simd)
39250 {
39251 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39252 if (cclauses == NULL)
39253 cclauses = cclauses_buf;
39254 cp_lexer_consume_token (parser->lexer);
39255 if (!flag_openmp) /* flag_openmp_simd */
39256 {
39257 if (simd)
39258 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39259 cclauses, if_p);
39260 else
39261 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
39262 cclauses, if_p);
39263 }
39264 sb = begin_omp_structured_block ();
39265 save = cp_parser_begin_omp_structured_block (parser);
39266 if (simd)
39267 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39268 cclauses, if_p);
39269 else
39270 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
39271 cclauses, if_p);
39272 cp_parser_end_omp_structured_block (parser, save);
39273 tree body = finish_omp_structured_block (sb);
39274 if (ret == NULL)
39275 return ret;
39276 ret = make_node (OMP_DISTRIBUTE);
39277 TREE_TYPE (ret) = void_type_node;
39278 OMP_FOR_BODY (ret) = body;
39279 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
39280 SET_EXPR_LOCATION (ret, loc);
39281 add_stmt (ret);
39282 return ret;
39283 }
39284 }
39285 if (!flag_openmp) /* flag_openmp_simd */
39286 {
39287 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39288 return NULL_TREE;
39289 }
39290
39291 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39292 cclauses == NULL);
39293 if (cclauses)
39294 {
39295 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
39296 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
39297 }
39298
39299 keep_next_level (true);
39300 sb = begin_omp_structured_block ();
39301 save = cp_parser_begin_omp_structured_block (parser);
39302
39303 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
39304
39305 cp_parser_end_omp_structured_block (parser, save);
39306 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39307
39308 return ret;
39309 }
39310
39311 /* OpenMP 4.0:
39312 # pragma omp teams teams-clause[optseq] new-line
39313 structured-block */
39314
39315 #define OMP_TEAMS_CLAUSE_MASK \
39316 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
39321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
39322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
39323
39324 static tree
39325 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
39326 char *p_name, omp_clause_mask mask, tree *cclauses,
39327 bool *if_p)
39328 {
39329 tree clauses, sb, ret;
39330 unsigned int save;
39331 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39332
39333 strcat (p_name, " teams");
39334 mask |= OMP_TEAMS_CLAUSE_MASK;
39335
39336 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39337 {
39338 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39339 const char *p = IDENTIFIER_POINTER (id);
39340 if (strcmp (p, "distribute") == 0)
39341 {
39342 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39343 if (cclauses == NULL)
39344 cclauses = cclauses_buf;
39345
39346 cp_lexer_consume_token (parser->lexer);
39347 if (!flag_openmp) /* flag_openmp_simd */
39348 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
39349 cclauses, if_p);
39350 keep_next_level (true);
39351 sb = begin_omp_structured_block ();
39352 save = cp_parser_begin_omp_structured_block (parser);
39353 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
39354 cclauses, if_p);
39355 cp_parser_end_omp_structured_block (parser, save);
39356 tree body = finish_omp_structured_block (sb);
39357 if (ret == NULL)
39358 return ret;
39359 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
39360 ret = make_node (OMP_TEAMS);
39361 TREE_TYPE (ret) = void_type_node;
39362 OMP_TEAMS_CLAUSES (ret) = clauses;
39363 OMP_TEAMS_BODY (ret) = body;
39364 OMP_TEAMS_COMBINED (ret) = 1;
39365 SET_EXPR_LOCATION (ret, loc);
39366 return add_stmt (ret);
39367 }
39368 else if (strcmp (p, "loop") == 0)
39369 {
39370 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39371 if (cclauses == NULL)
39372 cclauses = cclauses_buf;
39373
39374 cp_lexer_consume_token (parser->lexer);
39375 if (!flag_openmp) /* flag_openmp_simd */
39376 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
39377 cclauses, if_p);
39378 keep_next_level (true);
39379 sb = begin_omp_structured_block ();
39380 save = cp_parser_begin_omp_structured_block (parser);
39381 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
39382 cclauses, if_p);
39383 cp_parser_end_omp_structured_block (parser, save);
39384 tree body = finish_omp_structured_block (sb);
39385 if (ret == NULL)
39386 return ret;
39387 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
39388 ret = make_node (OMP_TEAMS);
39389 TREE_TYPE (ret) = void_type_node;
39390 OMP_TEAMS_CLAUSES (ret) = clauses;
39391 OMP_TEAMS_BODY (ret) = body;
39392 OMP_TEAMS_COMBINED (ret) = 1;
39393 SET_EXPR_LOCATION (ret, loc);
39394 return add_stmt (ret);
39395 }
39396 }
39397 if (!flag_openmp) /* flag_openmp_simd */
39398 {
39399 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39400 return NULL_TREE;
39401 }
39402
39403 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39404 cclauses == NULL);
39405 if (cclauses)
39406 {
39407 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
39408 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
39409 }
39410
39411 tree stmt = make_node (OMP_TEAMS);
39412 TREE_TYPE (stmt) = void_type_node;
39413 OMP_TEAMS_CLAUSES (stmt) = clauses;
39414 keep_next_level (true);
39415 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
39416 SET_EXPR_LOCATION (stmt, loc);
39417
39418 return add_stmt (stmt);
39419 }
39420
39421 /* OpenMP 4.0:
39422 # pragma omp target data target-data-clause[optseq] new-line
39423 structured-block */
39424
39425 #define OMP_TARGET_DATA_CLAUSE_MASK \
39426 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
39427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
39428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
39430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
39431
39432 static tree
39433 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39434 {
39435 tree clauses
39436 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
39437 "#pragma omp target data", pragma_tok);
39438 int map_seen = 0;
39439 for (tree *pc = &clauses; *pc;)
39440 {
39441 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
39442 switch (OMP_CLAUSE_MAP_KIND (*pc))
39443 {
39444 case GOMP_MAP_TO:
39445 case GOMP_MAP_ALWAYS_TO:
39446 case GOMP_MAP_FROM:
39447 case GOMP_MAP_ALWAYS_FROM:
39448 case GOMP_MAP_TOFROM:
39449 case GOMP_MAP_ALWAYS_TOFROM:
39450 case GOMP_MAP_ALLOC:
39451 map_seen = 3;
39452 break;
39453 case GOMP_MAP_FIRSTPRIVATE_POINTER:
39454 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
39455 case GOMP_MAP_ALWAYS_POINTER:
39456 break;
39457 default:
39458 map_seen |= 1;
39459 error_at (OMP_CLAUSE_LOCATION (*pc),
39460 "%<#pragma omp target data%> with map-type other "
39461 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
39462 "on %<map%> clause");
39463 *pc = OMP_CLAUSE_CHAIN (*pc);
39464 continue;
39465 }
39466 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
39467 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
39468 map_seen = 3;
39469 pc = &OMP_CLAUSE_CHAIN (*pc);
39470 }
39471
39472 if (map_seen != 3)
39473 {
39474 if (map_seen == 0)
39475 error_at (pragma_tok->location,
39476 "%<#pragma omp target data%> must contain at least "
39477 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
39478 "clause");
39479 return NULL_TREE;
39480 }
39481
39482 tree stmt = make_node (OMP_TARGET_DATA);
39483 TREE_TYPE (stmt) = void_type_node;
39484 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
39485
39486 keep_next_level (true);
39487 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
39488
39489 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39490 return add_stmt (stmt);
39491 }
39492
39493 /* OpenMP 4.5:
39494 # pragma omp target enter data target-enter-data-clause[optseq] new-line
39495 structured-block */
39496
39497 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
39498 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
39499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
39500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
39502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39503
39504 static tree
39505 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
39506 enum pragma_context context)
39507 {
39508 bool data_seen = false;
39509 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39510 {
39511 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39512 const char *p = IDENTIFIER_POINTER (id);
39513
39514 if (strcmp (p, "data") == 0)
39515 {
39516 cp_lexer_consume_token (parser->lexer);
39517 data_seen = true;
39518 }
39519 }
39520 if (!data_seen)
39521 {
39522 cp_parser_error (parser, "expected %<data%>");
39523 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39524 return NULL_TREE;
39525 }
39526
39527 if (context == pragma_stmt)
39528 {
39529 error_at (pragma_tok->location,
39530 "%<#pragma %s%> may only be used in compound statements",
39531 "omp target enter data");
39532 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39533 return NULL_TREE;
39534 }
39535
39536 tree clauses
39537 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
39538 "#pragma omp target enter data", pragma_tok);
39539 int map_seen = 0;
39540 for (tree *pc = &clauses; *pc;)
39541 {
39542 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
39543 switch (OMP_CLAUSE_MAP_KIND (*pc))
39544 {
39545 case GOMP_MAP_TO:
39546 case GOMP_MAP_ALWAYS_TO:
39547 case GOMP_MAP_ALLOC:
39548 map_seen = 3;
39549 break;
39550 case GOMP_MAP_FIRSTPRIVATE_POINTER:
39551 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
39552 case GOMP_MAP_ALWAYS_POINTER:
39553 break;
39554 default:
39555 map_seen |= 1;
39556 error_at (OMP_CLAUSE_LOCATION (*pc),
39557 "%<#pragma omp target enter data%> with map-type other "
39558 "than %<to%> or %<alloc%> on %<map%> clause");
39559 *pc = OMP_CLAUSE_CHAIN (*pc);
39560 continue;
39561 }
39562 pc = &OMP_CLAUSE_CHAIN (*pc);
39563 }
39564
39565 if (map_seen != 3)
39566 {
39567 if (map_seen == 0)
39568 error_at (pragma_tok->location,
39569 "%<#pragma omp target enter data%> must contain at least "
39570 "one %<map%> clause");
39571 return NULL_TREE;
39572 }
39573
39574 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
39575 TREE_TYPE (stmt) = void_type_node;
39576 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
39577 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39578 return add_stmt (stmt);
39579 }
39580
39581 /* OpenMP 4.5:
39582 # pragma omp target exit data target-enter-data-clause[optseq] new-line
39583 structured-block */
39584
39585 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
39586 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
39587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
39588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
39590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39591
39592 static tree
39593 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
39594 enum pragma_context context)
39595 {
39596 bool data_seen = false;
39597 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39598 {
39599 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39600 const char *p = IDENTIFIER_POINTER (id);
39601
39602 if (strcmp (p, "data") == 0)
39603 {
39604 cp_lexer_consume_token (parser->lexer);
39605 data_seen = true;
39606 }
39607 }
39608 if (!data_seen)
39609 {
39610 cp_parser_error (parser, "expected %<data%>");
39611 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39612 return NULL_TREE;
39613 }
39614
39615 if (context == pragma_stmt)
39616 {
39617 error_at (pragma_tok->location,
39618 "%<#pragma %s%> may only be used in compound statements",
39619 "omp target exit data");
39620 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39621 return NULL_TREE;
39622 }
39623
39624 tree clauses
39625 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
39626 "#pragma omp target exit data", pragma_tok);
39627 int map_seen = 0;
39628 for (tree *pc = &clauses; *pc;)
39629 {
39630 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
39631 switch (OMP_CLAUSE_MAP_KIND (*pc))
39632 {
39633 case GOMP_MAP_FROM:
39634 case GOMP_MAP_ALWAYS_FROM:
39635 case GOMP_MAP_RELEASE:
39636 case GOMP_MAP_DELETE:
39637 map_seen = 3;
39638 break;
39639 case GOMP_MAP_FIRSTPRIVATE_POINTER:
39640 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
39641 case GOMP_MAP_ALWAYS_POINTER:
39642 break;
39643 default:
39644 map_seen |= 1;
39645 error_at (OMP_CLAUSE_LOCATION (*pc),
39646 "%<#pragma omp target exit data%> with map-type other "
39647 "than %<from%>, %<release%> or %<delete%> on %<map%>"
39648 " clause");
39649 *pc = OMP_CLAUSE_CHAIN (*pc);
39650 continue;
39651 }
39652 pc = &OMP_CLAUSE_CHAIN (*pc);
39653 }
39654
39655 if (map_seen != 3)
39656 {
39657 if (map_seen == 0)
39658 error_at (pragma_tok->location,
39659 "%<#pragma omp target exit data%> must contain at least "
39660 "one %<map%> clause");
39661 return NULL_TREE;
39662 }
39663
39664 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
39665 TREE_TYPE (stmt) = void_type_node;
39666 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
39667 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39668 return add_stmt (stmt);
39669 }
39670
39671 /* OpenMP 4.0:
39672 # pragma omp target update target-update-clause[optseq] new-line */
39673
39674 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
39675 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
39676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
39678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
39680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39681
39682 static bool
39683 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
39684 enum pragma_context context)
39685 {
39686 if (context == pragma_stmt)
39687 {
39688 error_at (pragma_tok->location,
39689 "%<#pragma %s%> may only be used in compound statements",
39690 "omp target update");
39691 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39692 return false;
39693 }
39694
39695 tree clauses
39696 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
39697 "#pragma omp target update", pragma_tok);
39698 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
39699 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
39700 {
39701 error_at (pragma_tok->location,
39702 "%<#pragma omp target update%> must contain at least one "
39703 "%<from%> or %<to%> clauses");
39704 return false;
39705 }
39706
39707 tree stmt = make_node (OMP_TARGET_UPDATE);
39708 TREE_TYPE (stmt) = void_type_node;
39709 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
39710 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39711 add_stmt (stmt);
39712 return false;
39713 }
39714
39715 /* OpenMP 4.0:
39716 # pragma omp target target-clause[optseq] new-line
39717 structured-block */
39718
39719 #define OMP_TARGET_CLAUSE_MASK \
39720 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
39721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
39722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
39724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
39725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
39728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
39729
39730 static bool
39731 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
39732 enum pragma_context context, bool *if_p)
39733 {
39734 tree *pc = NULL, stmt;
39735
39736 if (flag_openmp)
39737 omp_requires_mask
39738 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
39739
39740 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39741 {
39742 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39743 const char *p = IDENTIFIER_POINTER (id);
39744 enum tree_code ccode = ERROR_MARK;
39745
39746 if (strcmp (p, "teams") == 0)
39747 ccode = OMP_TEAMS;
39748 else if (strcmp (p, "parallel") == 0)
39749 ccode = OMP_PARALLEL;
39750 else if (strcmp (p, "simd") == 0)
39751 ccode = OMP_SIMD;
39752 if (ccode != ERROR_MARK)
39753 {
39754 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
39755 char p_name[sizeof ("#pragma omp target teams distribute "
39756 "parallel for simd")];
39757
39758 cp_lexer_consume_token (parser->lexer);
39759 strcpy (p_name, "#pragma omp target");
39760 if (!flag_openmp) /* flag_openmp_simd */
39761 {
39762 tree stmt;
39763 switch (ccode)
39764 {
39765 case OMP_TEAMS:
39766 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
39767 OMP_TARGET_CLAUSE_MASK,
39768 cclauses, if_p);
39769 break;
39770 case OMP_PARALLEL:
39771 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
39772 OMP_TARGET_CLAUSE_MASK,
39773 cclauses, if_p);
39774 break;
39775 case OMP_SIMD:
39776 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
39777 OMP_TARGET_CLAUSE_MASK,
39778 cclauses, if_p);
39779 break;
39780 default:
39781 gcc_unreachable ();
39782 }
39783 return stmt != NULL_TREE;
39784 }
39785 keep_next_level (true);
39786 tree sb = begin_omp_structured_block (), ret;
39787 unsigned save = cp_parser_begin_omp_structured_block (parser);
39788 switch (ccode)
39789 {
39790 case OMP_TEAMS:
39791 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
39792 OMP_TARGET_CLAUSE_MASK, cclauses,
39793 if_p);
39794 break;
39795 case OMP_PARALLEL:
39796 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
39797 OMP_TARGET_CLAUSE_MASK, cclauses,
39798 if_p);
39799 break;
39800 case OMP_SIMD:
39801 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
39802 OMP_TARGET_CLAUSE_MASK, cclauses,
39803 if_p);
39804 break;
39805 default:
39806 gcc_unreachable ();
39807 }
39808 cp_parser_end_omp_structured_block (parser, save);
39809 tree body = finish_omp_structured_block (sb);
39810 if (ret == NULL_TREE)
39811 return false;
39812 if (ccode == OMP_TEAMS && !processing_template_decl)
39813 {
39814 /* For combined target teams, ensure the num_teams and
39815 thread_limit clause expressions are evaluated on the host,
39816 before entering the target construct. */
39817 tree c;
39818 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
39819 c; c = OMP_CLAUSE_CHAIN (c))
39820 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
39821 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
39822 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
39823 {
39824 tree expr = OMP_CLAUSE_OPERAND (c, 0);
39825 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
39826 if (expr == error_mark_node)
39827 continue;
39828 tree tmp = TARGET_EXPR_SLOT (expr);
39829 add_stmt (expr);
39830 OMP_CLAUSE_OPERAND (c, 0) = expr;
39831 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
39832 OMP_CLAUSE_FIRSTPRIVATE);
39833 OMP_CLAUSE_DECL (tc) = tmp;
39834 OMP_CLAUSE_CHAIN (tc)
39835 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
39836 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
39837 }
39838 }
39839 tree stmt = make_node (OMP_TARGET);
39840 TREE_TYPE (stmt) = void_type_node;
39841 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
39842 OMP_TARGET_BODY (stmt) = body;
39843 OMP_TARGET_COMBINED (stmt) = 1;
39844 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39845 add_stmt (stmt);
39846 pc = &OMP_TARGET_CLAUSES (stmt);
39847 goto check_clauses;
39848 }
39849 else if (!flag_openmp) /* flag_openmp_simd */
39850 {
39851 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39852 return false;
39853 }
39854 else if (strcmp (p, "data") == 0)
39855 {
39856 cp_lexer_consume_token (parser->lexer);
39857 cp_parser_omp_target_data (parser, pragma_tok, if_p);
39858 return true;
39859 }
39860 else if (strcmp (p, "enter") == 0)
39861 {
39862 cp_lexer_consume_token (parser->lexer);
39863 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
39864 return false;
39865 }
39866 else if (strcmp (p, "exit") == 0)
39867 {
39868 cp_lexer_consume_token (parser->lexer);
39869 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
39870 return false;
39871 }
39872 else if (strcmp (p, "update") == 0)
39873 {
39874 cp_lexer_consume_token (parser->lexer);
39875 return cp_parser_omp_target_update (parser, pragma_tok, context);
39876 }
39877 }
39878 if (!flag_openmp) /* flag_openmp_simd */
39879 {
39880 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39881 return false;
39882 }
39883
39884 stmt = make_node (OMP_TARGET);
39885 TREE_TYPE (stmt) = void_type_node;
39886
39887 OMP_TARGET_CLAUSES (stmt)
39888 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
39889 "#pragma omp target", pragma_tok);
39890 pc = &OMP_TARGET_CLAUSES (stmt);
39891 keep_next_level (true);
39892 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
39893
39894 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39895 add_stmt (stmt);
39896
39897 check_clauses:
39898 while (*pc)
39899 {
39900 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
39901 switch (OMP_CLAUSE_MAP_KIND (*pc))
39902 {
39903 case GOMP_MAP_TO:
39904 case GOMP_MAP_ALWAYS_TO:
39905 case GOMP_MAP_FROM:
39906 case GOMP_MAP_ALWAYS_FROM:
39907 case GOMP_MAP_TOFROM:
39908 case GOMP_MAP_ALWAYS_TOFROM:
39909 case GOMP_MAP_ALLOC:
39910 case GOMP_MAP_FIRSTPRIVATE_POINTER:
39911 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
39912 case GOMP_MAP_ALWAYS_POINTER:
39913 break;
39914 default:
39915 error_at (OMP_CLAUSE_LOCATION (*pc),
39916 "%<#pragma omp target%> with map-type other "
39917 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
39918 "on %<map%> clause");
39919 *pc = OMP_CLAUSE_CHAIN (*pc);
39920 continue;
39921 }
39922 pc = &OMP_CLAUSE_CHAIN (*pc);
39923 }
39924 return true;
39925 }
39926
39927 /* OpenACC 2.0:
39928 # pragma acc cache (variable-list) new-line
39929 */
39930
39931 static tree
39932 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
39933 {
39934 tree stmt, clauses;
39935
39936 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
39937 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
39938
39939 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
39940
39941 stmt = make_node (OACC_CACHE);
39942 TREE_TYPE (stmt) = void_type_node;
39943 OACC_CACHE_CLAUSES (stmt) = clauses;
39944 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39945 add_stmt (stmt);
39946
39947 return stmt;
39948 }
39949
39950 /* OpenACC 2.0:
39951 # pragma acc data oacc-data-clause[optseq] new-line
39952 structured-block */
39953
39954 #define OACC_DATA_CLAUSE_MASK \
39955 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
39962
39963 static tree
39964 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39965 {
39966 tree stmt, clauses, block;
39967 unsigned int save;
39968
39969 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
39970 "#pragma acc data", pragma_tok);
39971
39972 block = begin_omp_parallel ();
39973 save = cp_parser_begin_omp_structured_block (parser);
39974 cp_parser_statement (parser, NULL_TREE, false, if_p);
39975 cp_parser_end_omp_structured_block (parser, save);
39976 stmt = finish_oacc_data (clauses, block);
39977 return stmt;
39978 }
39979
39980 /* OpenACC 2.0:
39981 # pragma acc host_data <clauses> new-line
39982 structured-block */
39983
39984 #define OACC_HOST_DATA_CLAUSE_MASK \
39985 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
39986
39987 static tree
39988 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39989 {
39990 tree stmt, clauses, block;
39991 unsigned int save;
39992
39993 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
39994 "#pragma acc host_data", pragma_tok);
39995
39996 block = begin_omp_parallel ();
39997 save = cp_parser_begin_omp_structured_block (parser);
39998 cp_parser_statement (parser, NULL_TREE, false, if_p);
39999 cp_parser_end_omp_structured_block (parser, save);
40000 stmt = finish_oacc_host_data (clauses, block);
40001 return stmt;
40002 }
40003
40004 /* OpenACC 2.0:
40005 # pragma acc declare oacc-data-clause[optseq] new-line
40006 */
40007
40008 #define OACC_DECLARE_CLAUSE_MASK \
40009 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
40015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
40016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
40017
40018 static tree
40019 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
40020 {
40021 tree clauses, stmt;
40022 bool error = false;
40023
40024 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
40025 "#pragma acc declare", pragma_tok, true);
40026
40027
40028 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
40029 {
40030 error_at (pragma_tok->location,
40031 "no valid clauses specified in %<#pragma acc declare%>");
40032 return NULL_TREE;
40033 }
40034
40035 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
40036 {
40037 location_t loc = OMP_CLAUSE_LOCATION (t);
40038 tree decl = OMP_CLAUSE_DECL (t);
40039 if (!DECL_P (decl))
40040 {
40041 error_at (loc, "array section in %<#pragma acc declare%>");
40042 error = true;
40043 continue;
40044 }
40045 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
40046 switch (OMP_CLAUSE_MAP_KIND (t))
40047 {
40048 case GOMP_MAP_FIRSTPRIVATE_POINTER:
40049 case GOMP_MAP_ALLOC:
40050 case GOMP_MAP_TO:
40051 case GOMP_MAP_FORCE_DEVICEPTR:
40052 case GOMP_MAP_DEVICE_RESIDENT:
40053 break;
40054
40055 case GOMP_MAP_LINK:
40056 if (!global_bindings_p ()
40057 && (TREE_STATIC (decl)
40058 || !DECL_EXTERNAL (decl)))
40059 {
40060 error_at (loc,
40061 "%qD must be a global variable in "
40062 "%<#pragma acc declare link%>",
40063 decl);
40064 error = true;
40065 continue;
40066 }
40067 break;
40068
40069 default:
40070 if (global_bindings_p ())
40071 {
40072 error_at (loc, "invalid OpenACC clause at file scope");
40073 error = true;
40074 continue;
40075 }
40076 if (DECL_EXTERNAL (decl))
40077 {
40078 error_at (loc,
40079 "invalid use of %<extern%> variable %qD "
40080 "in %<#pragma acc declare%>", decl);
40081 error = true;
40082 continue;
40083 }
40084 else if (TREE_PUBLIC (decl))
40085 {
40086 error_at (loc,
40087 "invalid use of %<global%> variable %qD "
40088 "in %<#pragma acc declare%>", decl);
40089 error = true;
40090 continue;
40091 }
40092 break;
40093 }
40094
40095 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
40096 || lookup_attribute ("omp declare target link",
40097 DECL_ATTRIBUTES (decl)))
40098 {
40099 error_at (loc, "variable %qD used more than once with "
40100 "%<#pragma acc declare%>", decl);
40101 error = true;
40102 continue;
40103 }
40104
40105 if (!error)
40106 {
40107 tree id;
40108
40109 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
40110 id = get_identifier ("omp declare target link");
40111 else
40112 id = get_identifier ("omp declare target");
40113
40114 DECL_ATTRIBUTES (decl)
40115 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
40116 if (global_bindings_p ())
40117 {
40118 symtab_node *node = symtab_node::get (decl);
40119 if (node != NULL)
40120 {
40121 node->offloadable = 1;
40122 if (ENABLE_OFFLOADING)
40123 {
40124 g->have_offload = true;
40125 if (is_a <varpool_node *> (node))
40126 vec_safe_push (offload_vars, decl);
40127 }
40128 }
40129 }
40130 }
40131 }
40132
40133 if (error || global_bindings_p ())
40134 return NULL_TREE;
40135
40136 stmt = make_node (OACC_DECLARE);
40137 TREE_TYPE (stmt) = void_type_node;
40138 OACC_DECLARE_CLAUSES (stmt) = clauses;
40139 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40140
40141 add_stmt (stmt);
40142
40143 return NULL_TREE;
40144 }
40145
40146 /* OpenACC 2.0:
40147 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
40148
40149 or
40150
40151 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
40152
40153 LOC is the location of the #pragma token.
40154 */
40155
40156 #define OACC_ENTER_DATA_CLAUSE_MASK \
40157 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
40162
40163 #define OACC_EXIT_DATA_CLAUSE_MASK \
40164 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
40168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
40169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
40170
40171 static tree
40172 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
40173 bool enter)
40174 {
40175 location_t loc = pragma_tok->location;
40176 tree stmt, clauses;
40177 const char *p = "";
40178
40179 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40180 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
40181
40182 if (strcmp (p, "data") != 0)
40183 {
40184 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
40185 enter ? "enter" : "exit");
40186 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40187 return NULL_TREE;
40188 }
40189
40190 cp_lexer_consume_token (parser->lexer);
40191
40192 if (enter)
40193 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
40194 "#pragma acc enter data", pragma_tok);
40195 else
40196 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
40197 "#pragma acc exit data", pragma_tok);
40198
40199 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
40200 {
40201 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
40202 enter ? "enter" : "exit");
40203 return NULL_TREE;
40204 }
40205
40206 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
40207 TREE_TYPE (stmt) = void_type_node;
40208 OMP_STANDALONE_CLAUSES (stmt) = clauses;
40209 SET_EXPR_LOCATION (stmt, loc);
40210 add_stmt (stmt);
40211 return stmt;
40212 }
40213
40214 /* OpenACC 2.0:
40215 # pragma acc loop oacc-loop-clause[optseq] new-line
40216 structured-block */
40217
40218 #define OACC_LOOP_CLAUSE_MASK \
40219 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
40220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
40221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
40222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
40223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
40224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
40225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
40226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
40227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
40228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
40229
40230 static tree
40231 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
40232 omp_clause_mask mask, tree *cclauses, bool *if_p)
40233 {
40234 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
40235
40236 strcat (p_name, " loop");
40237 mask |= OACC_LOOP_CLAUSE_MASK;
40238
40239 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
40240 cclauses == NULL);
40241 if (cclauses)
40242 {
40243 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
40244 if (*cclauses)
40245 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
40246 if (clauses)
40247 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
40248 }
40249
40250 tree block = begin_omp_structured_block ();
40251 int save = cp_parser_begin_omp_structured_block (parser);
40252 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
40253 cp_parser_end_omp_structured_block (parser, save);
40254 add_stmt (finish_omp_structured_block (block));
40255
40256 return stmt;
40257 }
40258
40259 /* OpenACC 2.0:
40260 # pragma acc kernels oacc-kernels-clause[optseq] new-line
40261 structured-block
40262
40263 or
40264
40265 # pragma acc parallel oacc-parallel-clause[optseq] new-line
40266 structured-block
40267
40268 OpenACC 2.6:
40269
40270 # pragma acc serial oacc-serial-clause[optseq] new-line
40271 */
40272
40273 #define OACC_KERNELS_CLAUSE_MASK \
40274 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
40280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
40283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
40284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
40285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
40286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
40287
40288 #define OACC_PARALLEL_CLAUSE_MASK \
40289 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
40295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
40297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
40299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
40300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
40301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
40302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
40303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
40304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
40305
40306 #define OACC_SERIAL_CLAUSE_MASK \
40307 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
40313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
40316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
40317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
40318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
40319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
40320
40321 static tree
40322 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
40323 char *p_name, bool *if_p)
40324 {
40325 omp_clause_mask mask;
40326 enum tree_code code;
40327 switch (cp_parser_pragma_kind (pragma_tok))
40328 {
40329 case PRAGMA_OACC_KERNELS:
40330 strcat (p_name, " kernels");
40331 mask = OACC_KERNELS_CLAUSE_MASK;
40332 code = OACC_KERNELS;
40333 break;
40334 case PRAGMA_OACC_PARALLEL:
40335 strcat (p_name, " parallel");
40336 mask = OACC_PARALLEL_CLAUSE_MASK;
40337 code = OACC_PARALLEL;
40338 break;
40339 case PRAGMA_OACC_SERIAL:
40340 strcat (p_name, " serial");
40341 mask = OACC_SERIAL_CLAUSE_MASK;
40342 code = OACC_SERIAL;
40343 break;
40344 default:
40345 gcc_unreachable ();
40346 }
40347
40348 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40349 {
40350 const char *p
40351 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
40352 if (strcmp (p, "loop") == 0)
40353 {
40354 cp_lexer_consume_token (parser->lexer);
40355 tree block = begin_omp_parallel ();
40356 tree clauses;
40357 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
40358 &clauses, if_p);
40359 protected_set_expr_location (stmt, pragma_tok->location);
40360 return finish_omp_construct (code, block, clauses);
40361 }
40362 }
40363
40364 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
40365
40366 tree block = begin_omp_parallel ();
40367 unsigned int save = cp_parser_begin_omp_structured_block (parser);
40368 cp_parser_statement (parser, NULL_TREE, false, if_p);
40369 cp_parser_end_omp_structured_block (parser, save);
40370 return finish_omp_construct (code, block, clauses);
40371 }
40372
40373 /* OpenACC 2.0:
40374 # pragma acc update oacc-update-clause[optseq] new-line
40375 */
40376
40377 #define OACC_UPDATE_CLAUSE_MASK \
40378 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
40380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
40381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
40383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
40384
40385 static tree
40386 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
40387 {
40388 tree stmt, clauses;
40389
40390 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
40391 "#pragma acc update", pragma_tok);
40392
40393 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
40394 {
40395 error_at (pragma_tok->location,
40396 "%<#pragma acc update%> must contain at least one "
40397 "%<device%> or %<host%> or %<self%> clause");
40398 return NULL_TREE;
40399 }
40400
40401 stmt = make_node (OACC_UPDATE);
40402 TREE_TYPE (stmt) = void_type_node;
40403 OACC_UPDATE_CLAUSES (stmt) = clauses;
40404 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40405 add_stmt (stmt);
40406 return stmt;
40407 }
40408
40409 /* OpenACC 2.0:
40410 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
40411
40412 LOC is the location of the #pragma token.
40413 */
40414
40415 #define OACC_WAIT_CLAUSE_MASK \
40416 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
40417
40418 static tree
40419 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
40420 {
40421 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
40422 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40423
40424 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40425 list = cp_parser_oacc_wait_list (parser, loc, list);
40426
40427 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
40428 "#pragma acc wait", pragma_tok);
40429
40430 stmt = c_finish_oacc_wait (loc, list, clauses);
40431 stmt = finish_expr_stmt (stmt);
40432
40433 return stmt;
40434 }
40435
40436 /* OpenMP 4.0:
40437 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
40438
40439 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
40440 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
40441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
40442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
40443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
40444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
40445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
40446
40447 static void
40448 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
40449 enum pragma_context context,
40450 bool variant_p)
40451 {
40452 bool first_p = parser->omp_declare_simd == NULL;
40453 cp_omp_declare_simd_data data;
40454 if (first_p)
40455 {
40456 data.error_seen = false;
40457 data.fndecl_seen = false;
40458 data.variant_p = variant_p;
40459 data.tokens = vNULL;
40460 data.clauses = NULL_TREE;
40461 /* It is safe to take the address of a local variable; it will only be
40462 used while this scope is live. */
40463 parser->omp_declare_simd = &data;
40464 }
40465 else if (parser->omp_declare_simd->variant_p != variant_p)
40466 {
40467 error_at (pragma_tok->location,
40468 "%<#pragma omp declare %s%> followed by "
40469 "%<#pragma omp declare %s%>",
40470 parser->omp_declare_simd->variant_p ? "variant" : "simd",
40471 parser->omp_declare_simd->variant_p ? "simd" : "variant");
40472 parser->omp_declare_simd->error_seen = true;
40473 }
40474
40475 /* Store away all pragma tokens. */
40476 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
40477 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
40478 cp_lexer_consume_token (parser->lexer);
40479 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40480 parser->omp_declare_simd->error_seen = true;
40481 cp_parser_require_pragma_eol (parser, pragma_tok);
40482 struct cp_token_cache *cp
40483 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
40484 parser->omp_declare_simd->tokens.safe_push (cp);
40485
40486 if (first_p)
40487 {
40488 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
40489 cp_parser_pragma (parser, context, NULL);
40490 switch (context)
40491 {
40492 case pragma_external:
40493 cp_parser_declaration (parser);
40494 break;
40495 case pragma_member:
40496 cp_parser_member_declaration (parser);
40497 break;
40498 case pragma_objc_icode:
40499 cp_parser_block_declaration (parser, /*statement_p=*/false);
40500 break;
40501 default:
40502 cp_parser_declaration_statement (parser);
40503 break;
40504 }
40505 if (parser->omp_declare_simd
40506 && !parser->omp_declare_simd->error_seen
40507 && !parser->omp_declare_simd->fndecl_seen)
40508 error_at (pragma_tok->location,
40509 "%<#pragma omp declare %s%> not immediately followed by "
40510 "function declaration or definition",
40511 parser->omp_declare_simd->variant_p ? "variant" : "simd");
40512 data.tokens.release ();
40513 parser->omp_declare_simd = NULL;
40514 }
40515 }
40516
40517 static const char *const omp_construct_selectors[] = {
40518 "simd", "target", "teams", "parallel", "for", NULL };
40519 static const char *const omp_device_selectors[] = {
40520 "kind", "isa", "arch", NULL };
40521 static const char *const omp_implementation_selectors[] = {
40522 "vendor", "extension", "atomic_default_mem_order", "unified_address",
40523 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
40524 static const char *const omp_user_selectors[] = {
40525 "condition", NULL };
40526
40527 /* OpenMP 5.0:
40528
40529 trait-selector:
40530 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
40531
40532 trait-score:
40533 score(score-expression) */
40534
40535 static tree
40536 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
40537 {
40538 tree ret = NULL_TREE;
40539 do
40540 {
40541 tree selector;
40542 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
40543 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40544 selector = cp_lexer_peek_token (parser->lexer)->u.value;
40545 else
40546 {
40547 cp_parser_error (parser, "expected trait selector name");
40548 return error_mark_node;
40549 }
40550
40551 tree properties = NULL_TREE;
40552 const char *const *selectors = NULL;
40553 bool allow_score = true;
40554 bool allow_user = false;
40555 int property_limit = 0;
40556 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
40557 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
40558 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
40559 switch (IDENTIFIER_POINTER (set)[0])
40560 {
40561 case 'c': /* construct */
40562 selectors = omp_construct_selectors;
40563 allow_score = false;
40564 property_limit = 1;
40565 property_kind = CTX_PROPERTY_SIMD;
40566 break;
40567 case 'd': /* device */
40568 selectors = omp_device_selectors;
40569 allow_score = false;
40570 allow_user = true;
40571 property_limit = 3;
40572 property_kind = CTX_PROPERTY_NAME_LIST;
40573 break;
40574 case 'i': /* implementation */
40575 selectors = omp_implementation_selectors;
40576 allow_user = true;
40577 property_limit = 3;
40578 property_kind = CTX_PROPERTY_NAME_LIST;
40579 break;
40580 case 'u': /* user */
40581 selectors = omp_user_selectors;
40582 property_limit = 1;
40583 property_kind = CTX_PROPERTY_EXPR;
40584 break;
40585 default:
40586 gcc_unreachable ();
40587 }
40588 for (int i = 0; ; i++)
40589 {
40590 if (selectors[i] == NULL)
40591 {
40592 if (allow_user)
40593 {
40594 property_kind = CTX_PROPERTY_USER;
40595 break;
40596 }
40597 else
40598 {
40599 error ("selector %qs not allowed for context selector "
40600 "set %qs", IDENTIFIER_POINTER (selector),
40601 IDENTIFIER_POINTER (set));
40602 cp_lexer_consume_token (parser->lexer);
40603 return error_mark_node;
40604 }
40605 }
40606 if (i == property_limit)
40607 property_kind = CTX_PROPERTY_NONE;
40608 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
40609 break;
40610 }
40611 if (property_kind == CTX_PROPERTY_NAME_LIST
40612 && IDENTIFIER_POINTER (set)[0] == 'i'
40613 && strcmp (IDENTIFIER_POINTER (selector),
40614 "atomic_default_mem_order") == 0)
40615 property_kind = CTX_PROPERTY_ID;
40616
40617 cp_lexer_consume_token (parser->lexer);
40618
40619 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
40620 {
40621 if (property_kind == CTX_PROPERTY_NONE)
40622 {
40623 error ("selector %qs does not accept any properties",
40624 IDENTIFIER_POINTER (selector));
40625 return error_mark_node;
40626 }
40627
40628 matching_parens parens;
40629 parens.consume_open (parser);
40630
40631 cp_token *token = cp_lexer_peek_token (parser->lexer);
40632 if (allow_score
40633 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40634 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
40635 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40636 {
40637 cp_lexer_save_tokens (parser->lexer);
40638 cp_lexer_consume_token (parser->lexer);
40639 cp_lexer_consume_token (parser->lexer);
40640 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
40641 true)
40642 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40643 {
40644 cp_lexer_rollback_tokens (parser->lexer);
40645 cp_lexer_consume_token (parser->lexer);
40646
40647 matching_parens parens2;
40648 parens2.require_open (parser);
40649 tree score = cp_parser_constant_expression (parser);
40650 if (!parens2.require_close (parser))
40651 cp_parser_skip_to_closing_parenthesis (parser, true,
40652 false, true);
40653 cp_parser_require (parser, CPP_COLON, RT_COLON);
40654 if (score != error_mark_node)
40655 {
40656 score = fold_non_dependent_expr (score);
40657 if (value_dependent_expression_p (score))
40658 properties = tree_cons (get_identifier (" score"),
40659 score, properties);
40660 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
40661 || TREE_CODE (score) != INTEGER_CST)
40662 error_at (token->location, "score argument must be "
40663 "constant integer expression");
40664 else if (tree_int_cst_sgn (score) < 0)
40665 error_at (token->location, "score argument must be "
40666 "non-negative");
40667 else
40668 properties = tree_cons (get_identifier (" score"),
40669 score, properties);
40670 }
40671 }
40672 else
40673 cp_lexer_rollback_tokens (parser->lexer);
40674
40675 token = cp_lexer_peek_token (parser->lexer);
40676 }
40677
40678 switch (property_kind)
40679 {
40680 tree t;
40681 case CTX_PROPERTY_USER:
40682 do
40683 {
40684 t = cp_parser_constant_expression (parser);
40685 if (t != error_mark_node)
40686 {
40687 t = fold_non_dependent_expr (t);
40688 if (TREE_CODE (t) == STRING_CST)
40689 properties = tree_cons (NULL_TREE, t, properties);
40690 else if (!value_dependent_expression_p (t)
40691 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
40692 || !tree_fits_shwi_p (t)))
40693 error_at (token->location, "property must be "
40694 "constant integer expression or string "
40695 "literal");
40696 else
40697 properties = tree_cons (NULL_TREE, t, properties);
40698 }
40699 else
40700 return error_mark_node;
40701
40702 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40703 cp_lexer_consume_token (parser->lexer);
40704 else
40705 break;
40706 }
40707 while (1);
40708 break;
40709 case CTX_PROPERTY_ID:
40710 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
40711 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40712 {
40713 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
40714 cp_lexer_consume_token (parser->lexer);
40715 properties = tree_cons (prop, NULL_TREE, properties);
40716 }
40717 else
40718 {
40719 cp_parser_error (parser, "expected identifier");
40720 return error_mark_node;
40721 }
40722 break;
40723 case CTX_PROPERTY_NAME_LIST:
40724 do
40725 {
40726 tree prop = NULL_TREE, value = NULL_TREE;
40727 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
40728 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40729 {
40730 prop = cp_lexer_peek_token (parser->lexer)->u.value;
40731 cp_lexer_consume_token (parser->lexer);
40732 }
40733 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
40734 value = cp_parser_string_literal (parser, false, false);
40735 else
40736 {
40737 cp_parser_error (parser, "expected identifier or "
40738 "string literal");
40739 return error_mark_node;
40740 }
40741
40742 properties = tree_cons (prop, value, properties);
40743
40744 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40745 cp_lexer_consume_token (parser->lexer);
40746 else
40747 break;
40748 }
40749 while (1);
40750 break;
40751 case CTX_PROPERTY_EXPR:
40752 t = cp_parser_constant_expression (parser);
40753 if (t != error_mark_node)
40754 {
40755 t = fold_non_dependent_expr (t);
40756 if (!value_dependent_expression_p (t)
40757 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
40758 || !tree_fits_shwi_p (t)))
40759 error_at (token->location, "property must be "
40760 "constant integer expression");
40761 else
40762 properties = tree_cons (NULL_TREE, t, properties);
40763 }
40764 else
40765 return error_mark_node;
40766 break;
40767 case CTX_PROPERTY_SIMD:
40768 if (!has_parms_p)
40769 {
40770 error_at (token->location, "properties for %<simd%> "
40771 "selector may not be specified in "
40772 "%<metadirective%>");
40773 return error_mark_node;
40774 }
40775 properties
40776 = cp_parser_omp_all_clauses (parser,
40777 OMP_DECLARE_SIMD_CLAUSE_MASK,
40778 "simd", NULL, true, 2);
40779 break;
40780 default:
40781 gcc_unreachable ();
40782 }
40783
40784 if (!parens.require_close (parser))
40785 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
40786
40787 properties = nreverse (properties);
40788 }
40789 else if (property_kind == CTX_PROPERTY_NAME_LIST
40790 || property_kind == CTX_PROPERTY_ID
40791 || property_kind == CTX_PROPERTY_EXPR)
40792 {
40793 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
40794 return error_mark_node;
40795 }
40796
40797 ret = tree_cons (selector, properties, ret);
40798
40799 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40800 cp_lexer_consume_token (parser->lexer);
40801 else
40802 break;
40803 }
40804 while (1);
40805
40806 return nreverse (ret);
40807 }
40808
40809 /* OpenMP 5.0:
40810
40811 trait-set-selector[,trait-set-selector[,...]]
40812
40813 trait-set-selector:
40814 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
40815
40816 trait-set-selector-name:
40817 constructor
40818 device
40819 implementation
40820 user */
40821
40822 static tree
40823 cp_parser_omp_context_selector_specification (cp_parser *parser,
40824 bool has_parms_p)
40825 {
40826 tree ret = NULL_TREE;
40827 do
40828 {
40829 const char *setp = "";
40830 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40831 setp
40832 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
40833 switch (setp[0])
40834 {
40835 case 'c':
40836 if (strcmp (setp, "construct") == 0)
40837 setp = NULL;
40838 break;
40839 case 'd':
40840 if (strcmp (setp, "device") == 0)
40841 setp = NULL;
40842 break;
40843 case 'i':
40844 if (strcmp (setp, "implementation") == 0)
40845 setp = NULL;
40846 break;
40847 case 'u':
40848 if (strcmp (setp, "user") == 0)
40849 setp = NULL;
40850 break;
40851 default:
40852 break;
40853 }
40854 if (setp)
40855 {
40856 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
40857 "%<implementation%> or %<user%>");
40858 return error_mark_node;
40859 }
40860
40861 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
40862 cp_lexer_consume_token (parser->lexer);
40863
40864 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40865 return error_mark_node;
40866
40867 matching_braces braces;
40868 if (!braces.require_open (parser))
40869 return error_mark_node;
40870
40871 tree selectors
40872 = cp_parser_omp_context_selector (parser, set, has_parms_p);
40873 if (selectors == error_mark_node)
40874 {
40875 cp_parser_skip_to_closing_brace (parser);
40876 ret = error_mark_node;
40877 }
40878 else if (ret != error_mark_node)
40879 ret = tree_cons (set, selectors, ret);
40880
40881 braces.require_close (parser);
40882
40883 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40884 cp_lexer_consume_token (parser->lexer);
40885 else
40886 break;
40887 }
40888 while (1);
40889
40890 if (ret == error_mark_node)
40891 return ret;
40892 return nreverse (ret);
40893 }
40894
40895 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
40896 that into "omp declare variant base" attribute. */
40897
40898 static tree
40899 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
40900 tree attrs)
40901 {
40902 matching_parens parens;
40903 if (!parens.require_open (parser))
40904 {
40905 fail:
40906 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40907 return attrs;
40908 }
40909
40910 bool template_p;
40911 cp_id_kind idk = CP_ID_KIND_NONE;
40912 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
40913 cp_expr varid
40914 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
40915 /*check_dependency_p=*/true,
40916 /*template_p=*/&template_p,
40917 /*declarator_p=*/false,
40918 /*optional_p=*/false);
40919 parens.require_close (parser);
40920
40921 tree variant;
40922 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
40923 || TREE_CODE (varid) == TYPE_DECL
40924 || varid == error_mark_node)
40925 variant = varid;
40926 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
40927 variant = NULL_TREE;
40928 else
40929 {
40930 tree ambiguous_decls;
40931 variant = cp_parser_lookup_name (parser, varid, none_type,
40932 template_p, /*is_namespace=*/false,
40933 /*check_dependency=*/true,
40934 &ambiguous_decls,
40935 varid.get_location ());
40936 if (ambiguous_decls)
40937 variant = NULL_TREE;
40938 }
40939 if (variant == NULL_TREE)
40940 variant = error_mark_node;
40941 else if (TREE_CODE (variant) != SCOPE_REF)
40942 {
40943 const char *error_msg;
40944 variant
40945 = finish_id_expression (varid, variant, parser->scope,
40946 &idk, false, true,
40947 &parser->non_integral_constant_expression_p,
40948 template_p, true, false, false, &error_msg,
40949 varid.get_location ());
40950 if (error_msg)
40951 cp_parser_error (parser, error_msg);
40952 }
40953 location_t caret_loc = get_pure_location (varid.get_location ());
40954 location_t start_loc = get_start (varid_token->location);
40955 location_t finish_loc = get_finish (varid.get_location ());
40956 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
40957
40958 const char *clause = "";
40959 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
40960 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40961 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
40962 if (strcmp (clause, "match"))
40963 {
40964 cp_parser_error (parser, "expected %<match%>");
40965 goto fail;
40966 }
40967
40968 cp_lexer_consume_token (parser->lexer);
40969
40970 if (!parens.require_open (parser))
40971 goto fail;
40972
40973 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
40974 if (ctx == error_mark_node)
40975 goto fail;
40976 ctx = c_omp_check_context_selector (match_loc, ctx);
40977 if (ctx != error_mark_node && variant != error_mark_node)
40978 {
40979 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
40980 match_loc);
40981 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
40982 loc_node = tree_cons (match_loc_node,
40983 build_int_cst (integer_type_node, idk),
40984 build_tree_list (loc_node, integer_zero_node));
40985 attrs = tree_cons (get_identifier ("omp declare variant base"),
40986 tree_cons (variant, ctx, loc_node), attrs);
40987 if (processing_template_decl)
40988 ATTR_IS_DEPENDENT (attrs) = 1;
40989 }
40990
40991 parens.require_close (parser);
40992 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40993 return attrs;
40994 }
40995
40996
40997 /* Finalize #pragma omp declare simd clauses after direct declarator has
40998 been parsed, and put that into "omp declare simd" attribute. */
40999
41000 static tree
41001 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
41002 {
41003 struct cp_token_cache *ce;
41004 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
41005 int i;
41006
41007 if (!data->error_seen && data->fndecl_seen)
41008 {
41009 error ("%<#pragma omp declare %s%> not immediately followed by "
41010 "a single function declaration or definition",
41011 data->variant_p ? "variant" : "simd");
41012 data->error_seen = true;
41013 }
41014 if (data->error_seen)
41015 return attrs;
41016
41017 FOR_EACH_VEC_ELT (data->tokens, i, ce)
41018 {
41019 tree c, cl;
41020
41021 cp_parser_push_lexer_for_tokens (parser, ce);
41022 parser->lexer->in_pragma = true;
41023 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
41024 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
41025 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41026 const char *kind = IDENTIFIER_POINTER (id);
41027 cp_lexer_consume_token (parser->lexer);
41028 if (strcmp (kind, "simd") == 0)
41029 {
41030 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
41031 "#pragma omp declare simd",
41032 pragma_tok);
41033 if (cl)
41034 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
41035 c = build_tree_list (get_identifier ("omp declare simd"), cl);
41036 TREE_CHAIN (c) = attrs;
41037 if (processing_template_decl)
41038 ATTR_IS_DEPENDENT (c) = 1;
41039 attrs = c;
41040 }
41041 else
41042 {
41043 gcc_assert (strcmp (kind, "variant") == 0);
41044 attrs = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
41045 }
41046 cp_parser_pop_lexer (parser);
41047 }
41048
41049 data->fndecl_seen = true;
41050 return attrs;
41051 }
41052
41053
41054 /* OpenMP 4.0:
41055 # pragma omp declare target new-line
41056 declarations and definitions
41057 # pragma omp end declare target new-line
41058
41059 OpenMP 4.5:
41060 # pragma omp declare target ( extended-list ) new-line
41061
41062 # pragma omp declare target declare-target-clauses[seq] new-line */
41063
41064 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
41065 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
41066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
41067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
41068
41069 static void
41070 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
41071 {
41072 tree clauses = NULL_TREE;
41073 int device_type = 0;
41074 bool only_device_type = true;
41075 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41076 clauses
41077 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
41078 "#pragma omp declare target", pragma_tok);
41079 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41080 {
41081 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
41082 clauses);
41083 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
41084 cp_parser_require_pragma_eol (parser, pragma_tok);
41085 }
41086 else
41087 {
41088 cp_parser_require_pragma_eol (parser, pragma_tok);
41089 scope_chain->omp_declare_target_attribute++;
41090 return;
41091 }
41092 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
41093 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
41094 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
41095 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
41096 {
41097 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
41098 continue;
41099 tree t = OMP_CLAUSE_DECL (c), id;
41100 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
41101 tree at2 = lookup_attribute ("omp declare target link",
41102 DECL_ATTRIBUTES (t));
41103 only_device_type = false;
41104 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
41105 {
41106 id = get_identifier ("omp declare target link");
41107 std::swap (at1, at2);
41108 }
41109 else
41110 id = get_identifier ("omp declare target");
41111 if (at2)
41112 {
41113 error_at (OMP_CLAUSE_LOCATION (c),
41114 "%qD specified both in declare target %<link%> and %<to%>"
41115 " clauses", t);
41116 continue;
41117 }
41118 if (!at1)
41119 {
41120 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
41121 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
41122 continue;
41123
41124 symtab_node *node = symtab_node::get (t);
41125 if (node != NULL)
41126 {
41127 node->offloadable = 1;
41128 if (ENABLE_OFFLOADING)
41129 {
41130 g->have_offload = true;
41131 if (is_a <varpool_node *> (node))
41132 vec_safe_push (offload_vars, t);
41133 }
41134 }
41135 }
41136 if (TREE_CODE (t) != FUNCTION_DECL)
41137 continue;
41138 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
41139 {
41140 tree at3 = lookup_attribute ("omp declare target host",
41141 DECL_ATTRIBUTES (t));
41142 if (at3 == NULL_TREE)
41143 {
41144 id = get_identifier ("omp declare target host");
41145 DECL_ATTRIBUTES (t)
41146 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
41147 }
41148 }
41149 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
41150 {
41151 tree at3 = lookup_attribute ("omp declare target nohost",
41152 DECL_ATTRIBUTES (t));
41153 if (at3 == NULL_TREE)
41154 {
41155 id = get_identifier ("omp declare target nohost");
41156 DECL_ATTRIBUTES (t)
41157 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
41158 }
41159 }
41160 }
41161 if (device_type && only_device_type)
41162 warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
41163 "directive with only %<device_type%> clauses ignored");
41164 }
41165
41166 static void
41167 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
41168 {
41169 const char *p = "";
41170 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41171 {
41172 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41173 p = IDENTIFIER_POINTER (id);
41174 }
41175 if (strcmp (p, "declare") == 0)
41176 {
41177 cp_lexer_consume_token (parser->lexer);
41178 p = "";
41179 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41180 {
41181 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41182 p = IDENTIFIER_POINTER (id);
41183 }
41184 if (strcmp (p, "target") == 0)
41185 cp_lexer_consume_token (parser->lexer);
41186 else
41187 {
41188 cp_parser_error (parser, "expected %<target%>");
41189 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41190 return;
41191 }
41192 }
41193 else
41194 {
41195 cp_parser_error (parser, "expected %<declare%>");
41196 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41197 return;
41198 }
41199 cp_parser_require_pragma_eol (parser, pragma_tok);
41200 if (!scope_chain->omp_declare_target_attribute)
41201 error_at (pragma_tok->location,
41202 "%<#pragma omp end declare target%> without corresponding "
41203 "%<#pragma omp declare target%>");
41204 else
41205 scope_chain->omp_declare_target_attribute--;
41206 }
41207
41208 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
41209 expression and optional initializer clause of
41210 #pragma omp declare reduction. We store the expression(s) as
41211 either 3, 6 or 7 special statements inside of the artificial function's
41212 body. The first two statements are DECL_EXPRs for the artificial
41213 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
41214 expression that uses those variables.
41215 If there was any INITIALIZER clause, this is followed by further statements,
41216 the fourth and fifth statements are DECL_EXPRs for the artificial
41217 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
41218 constructor variant (first token after open paren is not omp_priv),
41219 then the sixth statement is a statement with the function call expression
41220 that uses the OMP_PRIV and optionally OMP_ORIG variable.
41221 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
41222 to initialize the OMP_PRIV artificial variable and there is seventh
41223 statement, a DECL_EXPR of the OMP_PRIV statement again. */
41224
41225 static bool
41226 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
41227 {
41228 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
41229 gcc_assert (TYPE_REF_P (type));
41230 type = TREE_TYPE (type);
41231 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
41232 DECL_ARTIFICIAL (omp_out) = 1;
41233 pushdecl (omp_out);
41234 add_decl_expr (omp_out);
41235 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
41236 DECL_ARTIFICIAL (omp_in) = 1;
41237 pushdecl (omp_in);
41238 add_decl_expr (omp_in);
41239 tree combiner;
41240 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
41241
41242 keep_next_level (true);
41243 tree block = begin_omp_structured_block ();
41244 combiner = cp_parser_expression (parser);
41245 finish_expr_stmt (combiner);
41246 block = finish_omp_structured_block (block);
41247 add_stmt (block);
41248
41249 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
41250 return false;
41251
41252 const char *p = "";
41253 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41254 {
41255 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41256 p = IDENTIFIER_POINTER (id);
41257 }
41258
41259 if (strcmp (p, "initializer") == 0)
41260 {
41261 cp_lexer_consume_token (parser->lexer);
41262 matching_parens parens;
41263 if (!parens.require_open (parser))
41264 return false;
41265
41266 p = "";
41267 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41268 {
41269 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41270 p = IDENTIFIER_POINTER (id);
41271 }
41272
41273 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
41274 DECL_ARTIFICIAL (omp_priv) = 1;
41275 pushdecl (omp_priv);
41276 add_decl_expr (omp_priv);
41277 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
41278 DECL_ARTIFICIAL (omp_orig) = 1;
41279 pushdecl (omp_orig);
41280 add_decl_expr (omp_orig);
41281
41282 keep_next_level (true);
41283 block = begin_omp_structured_block ();
41284
41285 bool ctor = false;
41286 if (strcmp (p, "omp_priv") == 0)
41287 {
41288 bool is_direct_init, is_non_constant_init;
41289 ctor = true;
41290 cp_lexer_consume_token (parser->lexer);
41291 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
41292 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
41293 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
41294 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
41295 == CPP_CLOSE_PAREN
41296 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
41297 == CPP_CLOSE_PAREN))
41298 {
41299 finish_omp_structured_block (block);
41300 error ("invalid initializer clause");
41301 return false;
41302 }
41303 initializer = cp_parser_initializer (parser, &is_direct_init,
41304 &is_non_constant_init);
41305 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
41306 NULL_TREE, LOOKUP_ONLYCONVERTING);
41307 }
41308 else
41309 {
41310 cp_parser_parse_tentatively (parser);
41311 /* Don't create location wrapper nodes here. */
41312 auto_suppress_location_wrappers sentinel;
41313 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
41314 /*check_dependency_p=*/true,
41315 /*template_p=*/NULL,
41316 /*declarator_p=*/false,
41317 /*optional_p=*/false);
41318 vec<tree, va_gc> *args;
41319 if (fn_name == error_mark_node
41320 || cp_parser_error_occurred (parser)
41321 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
41322 || ((args = cp_parser_parenthesized_expression_list
41323 (parser, non_attr, /*cast_p=*/false,
41324 /*allow_expansion_p=*/true,
41325 /*non_constant_p=*/NULL)),
41326 cp_parser_error_occurred (parser)))
41327 {
41328 finish_omp_structured_block (block);
41329 cp_parser_abort_tentative_parse (parser);
41330 cp_parser_error (parser, "expected id-expression (arguments)");
41331 return false;
41332 }
41333 unsigned int i;
41334 tree arg;
41335 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
41336 if (arg == omp_priv
41337 || (TREE_CODE (arg) == ADDR_EXPR
41338 && TREE_OPERAND (arg, 0) == omp_priv))
41339 break;
41340 cp_parser_abort_tentative_parse (parser);
41341 if (arg == NULL_TREE)
41342 error ("one of the initializer call arguments should be %<omp_priv%>"
41343 " or %<&omp_priv%>");
41344 initializer = cp_parser_postfix_expression (parser, false, false, false,
41345 false, NULL);
41346 finish_expr_stmt (initializer);
41347 }
41348
41349 block = finish_omp_structured_block (block);
41350 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
41351 add_stmt (block);
41352
41353 if (ctor)
41354 add_decl_expr (omp_orig);
41355
41356 if (!parens.require_close (parser))
41357 return false;
41358 }
41359
41360 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
41361 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
41362 UNKNOWN_LOCATION);
41363
41364 return true;
41365 }
41366
41367 /* OpenMP 4.0
41368 #pragma omp declare reduction (reduction-id : typename-list : expression) \
41369 initializer-clause[opt] new-line
41370
41371 initializer-clause:
41372 initializer (omp_priv initializer)
41373 initializer (function-name (argument-list)) */
41374
41375 static void
41376 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
41377 enum pragma_context)
41378 {
41379 auto_vec<tree> types;
41380 enum tree_code reduc_code = ERROR_MARK;
41381 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
41382 unsigned int i;
41383 cp_token *first_token;
41384 cp_token_cache *cp;
41385 int errs;
41386 void *p;
41387
41388 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
41389 p = obstack_alloc (&declarator_obstack, 0);
41390
41391 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41392 goto fail;
41393
41394 switch (cp_lexer_peek_token (parser->lexer)->type)
41395 {
41396 case CPP_PLUS:
41397 reduc_code = PLUS_EXPR;
41398 break;
41399 case CPP_MULT:
41400 reduc_code = MULT_EXPR;
41401 break;
41402 case CPP_MINUS:
41403 reduc_code = MINUS_EXPR;
41404 break;
41405 case CPP_AND:
41406 reduc_code = BIT_AND_EXPR;
41407 break;
41408 case CPP_XOR:
41409 reduc_code = BIT_XOR_EXPR;
41410 break;
41411 case CPP_OR:
41412 reduc_code = BIT_IOR_EXPR;
41413 break;
41414 case CPP_AND_AND:
41415 reduc_code = TRUTH_ANDIF_EXPR;
41416 break;
41417 case CPP_OR_OR:
41418 reduc_code = TRUTH_ORIF_EXPR;
41419 break;
41420 case CPP_NAME:
41421 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
41422 break;
41423 default:
41424 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
41425 "%<|%>, %<&&%>, %<||%> or identifier");
41426 goto fail;
41427 }
41428
41429 if (reduc_code != ERROR_MARK)
41430 cp_lexer_consume_token (parser->lexer);
41431
41432 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
41433 if (reduc_id == error_mark_node)
41434 goto fail;
41435
41436 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41437 goto fail;
41438
41439 /* Types may not be defined in declare reduction type list. */
41440 const char *saved_message;
41441 saved_message = parser->type_definition_forbidden_message;
41442 parser->type_definition_forbidden_message
41443 = G_("types may not be defined in declare reduction type list");
41444 bool saved_colon_corrects_to_scope_p;
41445 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
41446 parser->colon_corrects_to_scope_p = false;
41447 bool saved_colon_doesnt_start_class_def_p;
41448 saved_colon_doesnt_start_class_def_p
41449 = parser->colon_doesnt_start_class_def_p;
41450 parser->colon_doesnt_start_class_def_p = true;
41451
41452 while (true)
41453 {
41454 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41455 type = cp_parser_type_id (parser);
41456 if (type == error_mark_node)
41457 ;
41458 else if (ARITHMETIC_TYPE_P (type)
41459 && (orig_reduc_id == NULL_TREE
41460 || (TREE_CODE (type) != COMPLEX_TYPE
41461 && (id_equal (orig_reduc_id, "min")
41462 || id_equal (orig_reduc_id, "max")))))
41463 error_at (loc, "predeclared arithmetic type %qT in "
41464 "%<#pragma omp declare reduction%>", type);
41465 else if (FUNC_OR_METHOD_TYPE_P (type)
41466 || TREE_CODE (type) == ARRAY_TYPE)
41467 error_at (loc, "function or array type %qT in "
41468 "%<#pragma omp declare reduction%>", type);
41469 else if (TYPE_REF_P (type))
41470 error_at (loc, "reference type %qT in "
41471 "%<#pragma omp declare reduction%>", type);
41472 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
41473 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
41474 "type %qT in %<#pragma omp declare reduction%>", type);
41475 else
41476 types.safe_push (type);
41477
41478 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41479 cp_lexer_consume_token (parser->lexer);
41480 else
41481 break;
41482 }
41483
41484 /* Restore the saved message. */
41485 parser->type_definition_forbidden_message = saved_message;
41486 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
41487 parser->colon_doesnt_start_class_def_p
41488 = saved_colon_doesnt_start_class_def_p;
41489
41490 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
41491 || types.is_empty ())
41492 {
41493 fail:
41494 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41495 goto done;
41496 }
41497
41498 first_token = cp_lexer_peek_token (parser->lexer);
41499 cp = NULL;
41500 errs = errorcount;
41501 FOR_EACH_VEC_ELT (types, i, type)
41502 {
41503 tree fntype
41504 = build_function_type_list (void_type_node,
41505 cp_build_reference_type (type, false),
41506 NULL_TREE);
41507 tree this_reduc_id = reduc_id;
41508 if (!dependent_type_p (type))
41509 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
41510 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
41511 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
41512 DECL_ARTIFICIAL (fndecl) = 1;
41513 DECL_EXTERNAL (fndecl) = 1;
41514 DECL_DECLARED_INLINE_P (fndecl) = 1;
41515 DECL_IGNORED_P (fndecl) = 1;
41516 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
41517 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
41518 DECL_ATTRIBUTES (fndecl)
41519 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
41520 DECL_ATTRIBUTES (fndecl));
41521 if (processing_template_decl)
41522 fndecl = push_template_decl (fndecl);
41523 bool block_scope = false;
41524 tree block = NULL_TREE;
41525 if (current_function_decl)
41526 {
41527 block_scope = true;
41528 DECL_CONTEXT (fndecl) = global_namespace;
41529 if (!processing_template_decl)
41530 pushdecl (fndecl);
41531 }
41532 else if (current_class_type)
41533 {
41534 if (cp == NULL)
41535 {
41536 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
41537 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
41538 cp_lexer_consume_token (parser->lexer);
41539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41540 goto fail;
41541 cp = cp_token_cache_new (first_token,
41542 cp_lexer_peek_nth_token (parser->lexer,
41543 2));
41544 }
41545 DECL_STATIC_FUNCTION_P (fndecl) = 1;
41546 finish_member_declaration (fndecl);
41547 DECL_PENDING_INLINE_INFO (fndecl) = cp;
41548 DECL_PENDING_INLINE_P (fndecl) = 1;
41549 vec_safe_push (unparsed_funs_with_definitions, fndecl);
41550 continue;
41551 }
41552 else
41553 {
41554 DECL_CONTEXT (fndecl) = current_namespace;
41555 pushdecl (fndecl);
41556 }
41557 if (!block_scope)
41558 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
41559 else
41560 block = begin_omp_structured_block ();
41561 if (cp)
41562 {
41563 cp_parser_push_lexer_for_tokens (parser, cp);
41564 parser->lexer->in_pragma = true;
41565 }
41566 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
41567 {
41568 if (!block_scope)
41569 finish_function (/*inline_p=*/false);
41570 else
41571 DECL_CONTEXT (fndecl) = current_function_decl;
41572 if (cp)
41573 cp_parser_pop_lexer (parser);
41574 goto fail;
41575 }
41576 if (cp)
41577 cp_parser_pop_lexer (parser);
41578 if (!block_scope)
41579 finish_function (/*inline_p=*/false);
41580 else
41581 {
41582 DECL_CONTEXT (fndecl) = current_function_decl;
41583 block = finish_omp_structured_block (block);
41584 if (TREE_CODE (block) == BIND_EXPR)
41585 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
41586 else if (TREE_CODE (block) == STATEMENT_LIST)
41587 DECL_SAVED_TREE (fndecl) = block;
41588 if (processing_template_decl)
41589 add_decl_expr (fndecl);
41590 }
41591 cp_check_omp_declare_reduction (fndecl);
41592 if (cp == NULL && types.length () > 1)
41593 cp = cp_token_cache_new (first_token,
41594 cp_lexer_peek_nth_token (parser->lexer, 2));
41595 if (errs != errorcount)
41596 break;
41597 }
41598
41599 cp_parser_require_pragma_eol (parser, pragma_tok);
41600
41601 done:
41602 /* Free any declarators allocated. */
41603 obstack_free (&declarator_obstack, p);
41604 }
41605
41606 /* OpenMP 4.0
41607 #pragma omp declare simd declare-simd-clauses[optseq] new-line
41608 #pragma omp declare reduction (reduction-id : typename-list : expression) \
41609 initializer-clause[opt] new-line
41610 #pragma omp declare target new-line
41611
41612 OpenMP 5.0
41613 #pragma omp declare variant (identifier) match (context-selector) */
41614
41615 static bool
41616 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
41617 enum pragma_context context)
41618 {
41619 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41620 {
41621 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41622 const char *p = IDENTIFIER_POINTER (id);
41623
41624 if (strcmp (p, "simd") == 0)
41625 {
41626 cp_lexer_consume_token (parser->lexer);
41627 cp_parser_omp_declare_simd (parser, pragma_tok,
41628 context, false);
41629 return true;
41630 }
41631 if (flag_openmp && strcmp (p, "variant") == 0)
41632 {
41633 cp_lexer_consume_token (parser->lexer);
41634 cp_parser_omp_declare_simd (parser, pragma_tok,
41635 context, true);
41636 return true;
41637 }
41638 cp_ensure_no_omp_declare_simd (parser);
41639 if (strcmp (p, "reduction") == 0)
41640 {
41641 cp_lexer_consume_token (parser->lexer);
41642 cp_parser_omp_declare_reduction (parser, pragma_tok,
41643 context);
41644 return false;
41645 }
41646 if (!flag_openmp) /* flag_openmp_simd */
41647 {
41648 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41649 return false;
41650 }
41651 if (strcmp (p, "target") == 0)
41652 {
41653 cp_lexer_consume_token (parser->lexer);
41654 cp_parser_omp_declare_target (parser, pragma_tok);
41655 return false;
41656 }
41657 }
41658 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
41659 "%<target%> or %<variant%>");
41660 cp_parser_require_pragma_eol (parser, pragma_tok);
41661 return false;
41662 }
41663
41664 /* OpenMP 5.0
41665 #pragma omp requires clauses[optseq] new-line */
41666
41667 static bool
41668 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
41669 {
41670 bool first = true;
41671 enum omp_requires new_req = (enum omp_requires) 0;
41672
41673 location_t loc = pragma_tok->location;
41674 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41675 {
41676 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41677 cp_lexer_consume_token (parser->lexer);
41678
41679 first = false;
41680
41681 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41682 {
41683 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41684 const char *p = IDENTIFIER_POINTER (id);
41685 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
41686 enum omp_requires this_req = (enum omp_requires) 0;
41687
41688 if (!strcmp (p, "unified_address"))
41689 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
41690 else if (!strcmp (p, "unified_shared_memory"))
41691 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
41692 else if (!strcmp (p, "dynamic_allocators"))
41693 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
41694 else if (!strcmp (p, "reverse_offload"))
41695 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
41696 else if (!strcmp (p, "atomic_default_mem_order"))
41697 {
41698 cp_lexer_consume_token (parser->lexer);
41699
41700 matching_parens parens;
41701 if (parens.require_open (parser))
41702 {
41703 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41704 {
41705 id = cp_lexer_peek_token (parser->lexer)->u.value;
41706 p = IDENTIFIER_POINTER (id);
41707
41708 if (!strcmp (p, "seq_cst"))
41709 this_req
41710 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
41711 else if (!strcmp (p, "relaxed"))
41712 this_req
41713 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
41714 else if (!strcmp (p, "acq_rel"))
41715 this_req
41716 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
41717 }
41718 if (this_req == 0)
41719 {
41720 error_at (cp_lexer_peek_token (parser->lexer)->location,
41721 "expected %<seq_cst%>, %<relaxed%> or "
41722 "%<acq_rel%>");
41723 if (cp_lexer_nth_token_is (parser->lexer, 2,
41724 CPP_CLOSE_PAREN))
41725 cp_lexer_consume_token (parser->lexer);
41726 }
41727 else
41728 cp_lexer_consume_token (parser->lexer);
41729
41730 if (!parens.require_close (parser))
41731 cp_parser_skip_to_closing_parenthesis (parser,
41732 /*recovering=*/true,
41733 /*or_comma=*/false,
41734 /*consume_paren=*/
41735 true);
41736
41737 if (this_req == 0)
41738 {
41739 cp_parser_require_pragma_eol (parser, pragma_tok);
41740 return false;
41741 }
41742 }
41743 p = NULL;
41744 }
41745 else
41746 {
41747 error_at (cloc, "expected %<unified_address%>, "
41748 "%<unified_shared_memory%>, "
41749 "%<dynamic_allocators%>, "
41750 "%<reverse_offload%> "
41751 "or %<atomic_default_mem_order%> clause");
41752 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41753 return false;
41754 }
41755 if (p)
41756 sorry_at (cloc, "%qs clause on %<requires%> directive not "
41757 "supported yet", p);
41758 if (p)
41759 cp_lexer_consume_token (parser->lexer);
41760 if (this_req)
41761 {
41762 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
41763 {
41764 if ((this_req & new_req) != 0)
41765 error_at (cloc, "too many %qs clauses", p);
41766 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
41767 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
41768 error_at (cloc, "%qs clause used lexically after first "
41769 "target construct or offloading API", p);
41770 }
41771 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
41772 {
41773 error_at (cloc, "too many %qs clauses",
41774 "atomic_default_mem_order");
41775 this_req = (enum omp_requires) 0;
41776 }
41777 else if ((omp_requires_mask
41778 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
41779 {
41780 error_at (cloc, "more than one %<atomic_default_mem_order%>"
41781 " clause in a single compilation unit");
41782 this_req
41783 = (enum omp_requires)
41784 (omp_requires_mask
41785 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
41786 }
41787 else if ((omp_requires_mask
41788 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
41789 error_at (cloc, "%<atomic_default_mem_order%> clause used "
41790 "lexically after first %<atomic%> construct "
41791 "without memory order clause");
41792 new_req = (enum omp_requires) (new_req | this_req);
41793 omp_requires_mask
41794 = (enum omp_requires) (omp_requires_mask | this_req);
41795 continue;
41796 }
41797 }
41798 break;
41799 }
41800 cp_parser_require_pragma_eol (parser, pragma_tok);
41801
41802 if (new_req == 0)
41803 error_at (loc, "%<pragma omp requires%> requires at least one clause");
41804 return false;
41805 }
41806
41807
41808 /* OpenMP 4.5:
41809 #pragma omp taskloop taskloop-clause[optseq] new-line
41810 for-loop
41811
41812 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
41813 for-loop */
41814
41815 #define OMP_TASKLOOP_CLAUSE_MASK \
41816 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
41817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
41820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
41821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
41822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
41823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
41824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
41825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
41827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
41828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
41829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
41830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
41831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
41832
41833 static tree
41834 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
41835 char *p_name, omp_clause_mask mask, tree *cclauses,
41836 bool *if_p)
41837 {
41838 tree clauses, sb, ret;
41839 unsigned int save;
41840 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41841
41842 strcat (p_name, " taskloop");
41843 mask |= OMP_TASKLOOP_CLAUSE_MASK;
41844 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
41845 clause. */
41846 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
41847 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
41848
41849 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41850 {
41851 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41852 const char *p = IDENTIFIER_POINTER (id);
41853
41854 if (strcmp (p, "simd") == 0)
41855 {
41856 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
41857 if (cclauses == NULL)
41858 cclauses = cclauses_buf;
41859
41860 cp_lexer_consume_token (parser->lexer);
41861 if (!flag_openmp) /* flag_openmp_simd */
41862 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
41863 cclauses, if_p);
41864 sb = begin_omp_structured_block ();
41865 save = cp_parser_begin_omp_structured_block (parser);
41866 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
41867 cclauses, if_p);
41868 cp_parser_end_omp_structured_block (parser, save);
41869 tree body = finish_omp_structured_block (sb);
41870 if (ret == NULL)
41871 return ret;
41872 ret = make_node (OMP_TASKLOOP);
41873 TREE_TYPE (ret) = void_type_node;
41874 OMP_FOR_BODY (ret) = body;
41875 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
41876 SET_EXPR_LOCATION (ret, loc);
41877 add_stmt (ret);
41878 return ret;
41879 }
41880 }
41881 if (!flag_openmp) /* flag_openmp_simd */
41882 {
41883 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41884 return NULL_TREE;
41885 }
41886
41887 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
41888 cclauses == NULL);
41889 if (cclauses)
41890 {
41891 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
41892 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
41893 }
41894
41895 keep_next_level (true);
41896 sb = begin_omp_structured_block ();
41897 save = cp_parser_begin_omp_structured_block (parser);
41898
41899 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
41900 if_p);
41901
41902 cp_parser_end_omp_structured_block (parser, save);
41903 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
41904
41905 return ret;
41906 }
41907
41908
41909 /* OpenACC 2.0:
41910 # pragma acc routine oacc-routine-clause[optseq] new-line
41911 function-definition
41912
41913 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
41914 */
41915
41916 #define OACC_ROUTINE_CLAUSE_MASK \
41917 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
41918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
41919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
41920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
41921
41922
41923 /* Parse the OpenACC routine pragma. This has an optional '( name )'
41924 component, which must resolve to a declared namespace-scope
41925 function. The clauses are either processed directly (for a named
41926 function), or defered until the immediatley following declaration
41927 is parsed. */
41928
41929 static void
41930 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
41931 enum pragma_context context)
41932 {
41933 gcc_checking_assert (context == pragma_external);
41934 /* The checking for "another pragma following this one" in the "no optional
41935 '( name )'" case makes sure that we dont re-enter. */
41936 gcc_checking_assert (parser->oacc_routine == NULL);
41937
41938 cp_oacc_routine_data data;
41939 data.error_seen = false;
41940 data.fndecl_seen = false;
41941 data.tokens = vNULL;
41942 data.clauses = NULL_TREE;
41943 data.loc = pragma_tok->location;
41944 /* It is safe to take the address of a local variable; it will only be
41945 used while this scope is live. */
41946 parser->oacc_routine = &data;
41947
41948 /* Look for optional '( name )'. */
41949 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41950 {
41951 matching_parens parens;
41952 parens.consume_open (parser); /* '(' */
41953
41954 /* We parse the name as an id-expression. If it resolves to
41955 anything other than a non-overloaded function at namespace
41956 scope, it's an error. */
41957 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
41958 tree name = cp_parser_id_expression (parser,
41959 /*template_keyword_p=*/false,
41960 /*check_dependency_p=*/false,
41961 /*template_p=*/NULL,
41962 /*declarator_p=*/false,
41963 /*optional_p=*/false);
41964 tree decl = (identifier_p (name)
41965 ? cp_parser_lookup_name_simple (parser, name, name_loc)
41966 : name);
41967 if (name != error_mark_node && decl == error_mark_node)
41968 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
41969
41970 if (decl == error_mark_node
41971 || !parens.require_close (parser))
41972 {
41973 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41974 parser->oacc_routine = NULL;
41975 return;
41976 }
41977
41978 data.clauses
41979 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
41980 "#pragma acc routine",
41981 cp_lexer_peek_token (parser->lexer));
41982 /* The clauses are in reverse order; fix that to make later diagnostic
41983 emission easier. */
41984 data.clauses = nreverse (data.clauses);
41985
41986 if (decl && is_overloaded_fn (decl)
41987 && (TREE_CODE (decl) != FUNCTION_DECL
41988 || DECL_FUNCTION_TEMPLATE_P (decl)))
41989 {
41990 error_at (name_loc,
41991 "%<#pragma acc routine%> names a set of overloads");
41992 parser->oacc_routine = NULL;
41993 return;
41994 }
41995
41996 /* Perhaps we should use the same rule as declarations in different
41997 namespaces? */
41998 if (!DECL_NAMESPACE_SCOPE_P (decl))
41999 {
42000 error_at (name_loc,
42001 "%qD does not refer to a namespace scope function", decl);
42002 parser->oacc_routine = NULL;
42003 return;
42004 }
42005
42006 if (TREE_CODE (decl) != FUNCTION_DECL)
42007 {
42008 error_at (name_loc, "%qD does not refer to a function", decl);
42009 parser->oacc_routine = NULL;
42010 return;
42011 }
42012
42013 cp_finalize_oacc_routine (parser, decl, false);
42014 parser->oacc_routine = NULL;
42015 }
42016 else /* No optional '( name )'. */
42017 {
42018 /* Store away all pragma tokens. */
42019 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
42020 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
42021 cp_lexer_consume_token (parser->lexer);
42022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42023 parser->oacc_routine->error_seen = true;
42024 cp_parser_require_pragma_eol (parser, pragma_tok);
42025 struct cp_token_cache *cp
42026 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
42027 parser->oacc_routine->tokens.safe_push (cp);
42028
42029 /* Emit a helpful diagnostic if there's another pragma following this
42030 one. */
42031 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
42032 {
42033 cp_ensure_no_oacc_routine (parser);
42034 data.tokens.release ();
42035 /* ..., and then just keep going. */
42036 return;
42037 }
42038
42039 /* We only have to consider the pragma_external case here. */
42040 cp_parser_declaration (parser);
42041 if (parser->oacc_routine
42042 && !parser->oacc_routine->fndecl_seen)
42043 cp_ensure_no_oacc_routine (parser);
42044 else
42045 parser->oacc_routine = NULL;
42046 data.tokens.release ();
42047 }
42048 }
42049
42050 /* Finalize #pragma acc routine clauses after direct declarator has
42051 been parsed. */
42052
42053 static tree
42054 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
42055 {
42056 struct cp_token_cache *ce;
42057 cp_oacc_routine_data *data = parser->oacc_routine;
42058
42059 if (!data->error_seen && data->fndecl_seen)
42060 {
42061 error_at (data->loc,
42062 "%<#pragma acc routine%> not immediately followed by "
42063 "a single function declaration or definition");
42064 data->error_seen = true;
42065 }
42066 if (data->error_seen)
42067 return attrs;
42068
42069 gcc_checking_assert (data->tokens.length () == 1);
42070 ce = data->tokens[0];
42071
42072 cp_parser_push_lexer_for_tokens (parser, ce);
42073 parser->lexer->in_pragma = true;
42074 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
42075
42076 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
42077 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
42078 parser->oacc_routine->clauses
42079 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
42080 "#pragma acc routine", pragma_tok);
42081 /* The clauses are in reverse order; fix that to make later diagnostic
42082 emission easier. */
42083 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
42084 cp_parser_pop_lexer (parser);
42085 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
42086 fndecl_seen. */
42087
42088 return attrs;
42089 }
42090
42091 /* Apply any saved OpenACC routine clauses to a just-parsed
42092 declaration. */
42093
42094 static void
42095 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
42096 {
42097 if (__builtin_expect (parser->oacc_routine != NULL, 0))
42098 {
42099 /* Keep going if we're in error reporting mode. */
42100 if (parser->oacc_routine->error_seen
42101 || fndecl == error_mark_node)
42102 return;
42103
42104 if (parser->oacc_routine->fndecl_seen)
42105 {
42106 error_at (parser->oacc_routine->loc,
42107 "%<#pragma acc routine%> not immediately followed by"
42108 " a single function declaration or definition");
42109 parser->oacc_routine = NULL;
42110 return;
42111 }
42112 if (TREE_CODE (fndecl) != FUNCTION_DECL)
42113 {
42114 cp_ensure_no_oacc_routine (parser);
42115 return;
42116 }
42117
42118 int compatible
42119 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
42120 parser->oacc_routine->loc,
42121 "#pragma acc routine");
42122 if (compatible < 0)
42123 {
42124 parser->oacc_routine = NULL;
42125 return;
42126 }
42127 if (compatible > 0)
42128 {
42129 }
42130 else
42131 {
42132 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
42133 {
42134 error_at (parser->oacc_routine->loc,
42135 TREE_USED (fndecl)
42136 ? G_("%<#pragma acc routine%> must be applied before"
42137 " use")
42138 : G_("%<#pragma acc routine%> must be applied before"
42139 " definition"));
42140 parser->oacc_routine = NULL;
42141 return;
42142 }
42143
42144 /* Set the routine's level of parallelism. */
42145 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
42146 oacc_replace_fn_attrib (fndecl, dims);
42147
42148 /* Add an "omp declare target" attribute. */
42149 DECL_ATTRIBUTES (fndecl)
42150 = tree_cons (get_identifier ("omp declare target"),
42151 parser->oacc_routine->clauses,
42152 DECL_ATTRIBUTES (fndecl));
42153 }
42154
42155 /* Don't unset parser->oacc_routine here: we may still need it to
42156 diagnose wrong usage. But, remember that we've used this "#pragma acc
42157 routine". */
42158 parser->oacc_routine->fndecl_seen = true;
42159 }
42160 }
42161
42162 /* Main entry point to OpenMP statement pragmas. */
42163
42164 static void
42165 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
42166 {
42167 tree stmt;
42168 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
42169 omp_clause_mask mask (0);
42170
42171 switch (cp_parser_pragma_kind (pragma_tok))
42172 {
42173 case PRAGMA_OACC_ATOMIC:
42174 cp_parser_omp_atomic (parser, pragma_tok);
42175 return;
42176 case PRAGMA_OACC_CACHE:
42177 stmt = cp_parser_oacc_cache (parser, pragma_tok);
42178 break;
42179 case PRAGMA_OACC_DATA:
42180 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
42181 break;
42182 case PRAGMA_OACC_ENTER_DATA:
42183 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
42184 break;
42185 case PRAGMA_OACC_EXIT_DATA:
42186 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
42187 break;
42188 case PRAGMA_OACC_HOST_DATA:
42189 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
42190 break;
42191 case PRAGMA_OACC_KERNELS:
42192 case PRAGMA_OACC_PARALLEL:
42193 case PRAGMA_OACC_SERIAL:
42194 strcpy (p_name, "#pragma acc");
42195 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
42196 break;
42197 case PRAGMA_OACC_LOOP:
42198 strcpy (p_name, "#pragma acc");
42199 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
42200 if_p);
42201 break;
42202 case PRAGMA_OACC_UPDATE:
42203 stmt = cp_parser_oacc_update (parser, pragma_tok);
42204 break;
42205 case PRAGMA_OACC_WAIT:
42206 stmt = cp_parser_oacc_wait (parser, pragma_tok);
42207 break;
42208 case PRAGMA_OMP_ATOMIC:
42209 cp_parser_omp_atomic (parser, pragma_tok);
42210 return;
42211 case PRAGMA_OMP_CRITICAL:
42212 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
42213 break;
42214 case PRAGMA_OMP_DISTRIBUTE:
42215 strcpy (p_name, "#pragma omp");
42216 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
42217 if_p);
42218 break;
42219 case PRAGMA_OMP_FOR:
42220 strcpy (p_name, "#pragma omp");
42221 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
42222 if_p);
42223 break;
42224 case PRAGMA_OMP_LOOP:
42225 strcpy (p_name, "#pragma omp");
42226 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
42227 if_p);
42228 break;
42229 case PRAGMA_OMP_MASTER:
42230 strcpy (p_name, "#pragma omp");
42231 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
42232 if_p);
42233 break;
42234 case PRAGMA_OMP_PARALLEL:
42235 strcpy (p_name, "#pragma omp");
42236 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
42237 if_p);
42238 break;
42239 case PRAGMA_OMP_SECTIONS:
42240 strcpy (p_name, "#pragma omp");
42241 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
42242 break;
42243 case PRAGMA_OMP_SIMD:
42244 strcpy (p_name, "#pragma omp");
42245 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
42246 if_p);
42247 break;
42248 case PRAGMA_OMP_SINGLE:
42249 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
42250 break;
42251 case PRAGMA_OMP_TASK:
42252 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
42253 break;
42254 case PRAGMA_OMP_TASKGROUP:
42255 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
42256 break;
42257 case PRAGMA_OMP_TASKLOOP:
42258 strcpy (p_name, "#pragma omp");
42259 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
42260 if_p);
42261 break;
42262 case PRAGMA_OMP_TEAMS:
42263 strcpy (p_name, "#pragma omp");
42264 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
42265 if_p);
42266 break;
42267 default:
42268 gcc_unreachable ();
42269 }
42270
42271 protected_set_expr_location (stmt, pragma_tok->location);
42272 }
42273 \f
42274 /* Transactional Memory parsing routines. */
42275
42276 /* Parse a transaction attribute.
42277
42278 txn-attribute:
42279 attribute
42280 [ [ identifier ] ]
42281
42282 We use this instead of cp_parser_attributes_opt for transactions to avoid
42283 the pedwarn in C++98 mode. */
42284
42285 static tree
42286 cp_parser_txn_attribute_opt (cp_parser *parser)
42287 {
42288 cp_token *token;
42289 tree attr_name, attr = NULL;
42290
42291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
42292 return cp_parser_attributes_opt (parser);
42293
42294 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
42295 return NULL_TREE;
42296 cp_lexer_consume_token (parser->lexer);
42297 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
42298 goto error1;
42299
42300 token = cp_lexer_peek_token (parser->lexer);
42301 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
42302 {
42303 token = cp_lexer_consume_token (parser->lexer);
42304
42305 attr_name = (token->type == CPP_KEYWORD
42306 /* For keywords, use the canonical spelling,
42307 not the parsed identifier. */
42308 ? ridpointers[(int) token->keyword]
42309 : token->u.value);
42310 attr = build_tree_list (attr_name, NULL_TREE);
42311 }
42312 else
42313 cp_parser_error (parser, "expected identifier");
42314
42315 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
42316 error1:
42317 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
42318 return attr;
42319 }
42320
42321 /* Parse a __transaction_atomic or __transaction_relaxed statement.
42322
42323 transaction-statement:
42324 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
42325 compound-statement
42326 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
42327 */
42328
42329 static tree
42330 cp_parser_transaction (cp_parser *parser, cp_token *token)
42331 {
42332 unsigned char old_in = parser->in_transaction;
42333 unsigned char this_in = 1, new_in;
42334 enum rid keyword = token->keyword;
42335 tree stmt, attrs, noex;
42336
42337 cp_lexer_consume_token (parser->lexer);
42338
42339 if (keyword == RID_TRANSACTION_RELAXED
42340 || keyword == RID_SYNCHRONIZED)
42341 this_in |= TM_STMT_ATTR_RELAXED;
42342 else
42343 {
42344 attrs = cp_parser_txn_attribute_opt (parser);
42345 if (attrs)
42346 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
42347 }
42348
42349 /* Parse a noexcept specification. */
42350 if (keyword == RID_ATOMIC_NOEXCEPT)
42351 noex = boolean_true_node;
42352 else if (keyword == RID_ATOMIC_CANCEL)
42353 {
42354 /* cancel-and-throw is unimplemented. */
42355 sorry ("%<atomic_cancel%>");
42356 noex = NULL_TREE;
42357 }
42358 else
42359 noex = cp_parser_noexcept_specification_opt (parser,
42360 CP_PARSER_FLAGS_NONE,
42361 /*require_constexpr=*/true,
42362 /*consumed_expr=*/NULL,
42363 /*return_cond=*/true);
42364
42365 /* Keep track if we're in the lexical scope of an outer transaction. */
42366 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
42367
42368 stmt = begin_transaction_stmt (token->location, NULL, this_in);
42369
42370 parser->in_transaction = new_in;
42371 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
42372 parser->in_transaction = old_in;
42373
42374 finish_transaction_stmt (stmt, NULL, this_in, noex);
42375
42376 return stmt;
42377 }
42378
42379 /* Parse a __transaction_atomic or __transaction_relaxed expression.
42380
42381 transaction-expression:
42382 __transaction_atomic txn-noexcept-spec[opt] ( expression )
42383 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
42384 */
42385
42386 static tree
42387 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
42388 {
42389 unsigned char old_in = parser->in_transaction;
42390 unsigned char this_in = 1;
42391 cp_token *token;
42392 tree expr, noex;
42393 bool noex_expr;
42394 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42395
42396 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
42397 || keyword == RID_TRANSACTION_RELAXED);
42398
42399 if (!flag_tm)
42400 error_at (loc,
42401 keyword == RID_TRANSACTION_RELAXED
42402 ? G_("%<__transaction_relaxed%> without transactional memory "
42403 "support enabled")
42404 : G_("%<__transaction_atomic%> without transactional memory "
42405 "support enabled"));
42406
42407 token = cp_parser_require_keyword (parser, keyword,
42408 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
42409 : RT_TRANSACTION_RELAXED));
42410 gcc_assert (token != NULL);
42411
42412 if (keyword == RID_TRANSACTION_RELAXED)
42413 this_in |= TM_STMT_ATTR_RELAXED;
42414
42415 /* Set this early. This might mean that we allow transaction_cancel in
42416 an expression that we find out later actually has to be a constexpr.
42417 However, we expect that cxx_constant_value will be able to deal with
42418 this; also, if the noexcept has no constexpr, then what we parse next
42419 really is a transaction's body. */
42420 parser->in_transaction = this_in;
42421
42422 /* Parse a noexcept specification. */
42423 noex = cp_parser_noexcept_specification_opt (parser,
42424 CP_PARSER_FLAGS_NONE,
42425 /*require_constexpr=*/false,
42426 &noex_expr,
42427 /*return_cond=*/true);
42428
42429 if (!noex || !noex_expr
42430 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
42431 {
42432 matching_parens parens;
42433 parens.require_open (parser);
42434
42435 expr = cp_parser_expression (parser);
42436 expr = finish_parenthesized_expr (expr);
42437
42438 parens.require_close (parser);
42439 }
42440 else
42441 {
42442 /* The only expression that is available got parsed for the noexcept
42443 already. noexcept is true then. */
42444 expr = noex;
42445 noex = boolean_true_node;
42446 }
42447
42448 expr = build_transaction_expr (token->location, expr, this_in, noex);
42449 parser->in_transaction = old_in;
42450
42451 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
42452 return error_mark_node;
42453
42454 return (flag_tm ? expr : error_mark_node);
42455 }
42456
42457 /* Parse a function-transaction-block.
42458
42459 function-transaction-block:
42460 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
42461 function-body
42462 __transaction_atomic txn-attribute[opt] function-try-block
42463 __transaction_relaxed ctor-initializer[opt] function-body
42464 __transaction_relaxed function-try-block
42465 */
42466
42467 static void
42468 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
42469 {
42470 unsigned char old_in = parser->in_transaction;
42471 unsigned char new_in = 1;
42472 tree compound_stmt, stmt, attrs;
42473 cp_token *token;
42474
42475 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
42476 || keyword == RID_TRANSACTION_RELAXED);
42477 token = cp_parser_require_keyword (parser, keyword,
42478 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
42479 : RT_TRANSACTION_RELAXED));
42480 gcc_assert (token != NULL);
42481
42482 if (keyword == RID_TRANSACTION_RELAXED)
42483 new_in |= TM_STMT_ATTR_RELAXED;
42484 else
42485 {
42486 attrs = cp_parser_txn_attribute_opt (parser);
42487 if (attrs)
42488 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
42489 }
42490
42491 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
42492
42493 parser->in_transaction = new_in;
42494
42495 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
42496 cp_parser_function_try_block (parser);
42497 else
42498 cp_parser_ctor_initializer_opt_and_function_body
42499 (parser, /*in_function_try_block=*/false);
42500
42501 parser->in_transaction = old_in;
42502
42503 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
42504 }
42505
42506 /* Parse a __transaction_cancel statement.
42507
42508 cancel-statement:
42509 __transaction_cancel txn-attribute[opt] ;
42510 __transaction_cancel txn-attribute[opt] throw-expression ;
42511
42512 ??? Cancel and throw is not yet implemented. */
42513
42514 static tree
42515 cp_parser_transaction_cancel (cp_parser *parser)
42516 {
42517 cp_token *token;
42518 bool is_outer = false;
42519 tree stmt, attrs;
42520
42521 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
42522 RT_TRANSACTION_CANCEL);
42523 gcc_assert (token != NULL);
42524
42525 attrs = cp_parser_txn_attribute_opt (parser);
42526 if (attrs)
42527 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
42528
42529 /* ??? Parse cancel-and-throw here. */
42530
42531 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
42532
42533 if (!flag_tm)
42534 {
42535 error_at (token->location, "%<__transaction_cancel%> without "
42536 "transactional memory support enabled");
42537 return error_mark_node;
42538 }
42539 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
42540 {
42541 error_at (token->location, "%<__transaction_cancel%> within a "
42542 "%<__transaction_relaxed%>");
42543 return error_mark_node;
42544 }
42545 else if (is_outer)
42546 {
42547 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
42548 && !is_tm_may_cancel_outer (current_function_decl))
42549 {
42550 error_at (token->location, "outer %<__transaction_cancel%> not "
42551 "within outer %<__transaction_atomic%>");
42552 error_at (token->location,
42553 " or a %<transaction_may_cancel_outer%> function");
42554 return error_mark_node;
42555 }
42556 }
42557 else if (parser->in_transaction == 0)
42558 {
42559 error_at (token->location, "%<__transaction_cancel%> not within "
42560 "%<__transaction_atomic%>");
42561 return error_mark_node;
42562 }
42563
42564 stmt = build_tm_abort_call (token->location, is_outer);
42565 add_stmt (stmt);
42566
42567 return stmt;
42568 }
42569 \f
42570 /* The parser. */
42571
42572 static GTY (()) cp_parser *the_parser;
42573
42574 \f
42575 /* Special handling for the first token or line in the file. The first
42576 thing in the file might be #pragma GCC pch_preprocess, which loads a
42577 PCH file, which is a GC collection point. So we need to handle this
42578 first pragma without benefit of an existing lexer structure.
42579
42580 Always returns one token to the caller in *FIRST_TOKEN. This is
42581 either the true first token of the file, or the first token after
42582 the initial pragma. */
42583
42584 static void
42585 cp_parser_initial_pragma (cp_token *first_token)
42586 {
42587 tree name = NULL;
42588
42589 cp_lexer_get_preprocessor_token (NULL, first_token);
42590 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
42591 {
42592 c_common_no_more_pch ();
42593 return;
42594 }
42595
42596 cp_lexer_get_preprocessor_token (NULL, first_token);
42597 if (first_token->type == CPP_STRING)
42598 {
42599 name = first_token->u.value;
42600
42601 cp_lexer_get_preprocessor_token (NULL, first_token);
42602 if (first_token->type != CPP_PRAGMA_EOL)
42603 error_at (first_token->location,
42604 "junk at end of %<#pragma GCC pch_preprocess%>");
42605 }
42606 else
42607 error_at (first_token->location, "expected string literal");
42608
42609 /* Skip to the end of the pragma. */
42610 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
42611 cp_lexer_get_preprocessor_token (NULL, first_token);
42612
42613 /* Now actually load the PCH file. */
42614 if (name)
42615 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
42616
42617 /* Read one more token to return to our caller. We have to do this
42618 after reading the PCH file in, since its pointers have to be
42619 live. */
42620 cp_lexer_get_preprocessor_token (NULL, first_token);
42621 }
42622
42623 /* Parse a pragma GCC ivdep. */
42624
42625 static bool
42626 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
42627 {
42628 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42629 return true;
42630 }
42631
42632 /* Parse a pragma GCC unroll. */
42633
42634 static unsigned short
42635 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
42636 {
42637 location_t location = cp_lexer_peek_token (parser->lexer)->location;
42638 tree expr = cp_parser_constant_expression (parser);
42639 unsigned short unroll;
42640 expr = maybe_constant_value (expr);
42641 HOST_WIDE_INT lunroll = 0;
42642 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
42643 || TREE_CODE (expr) != INTEGER_CST
42644 || (lunroll = tree_to_shwi (expr)) < 0
42645 || lunroll >= USHRT_MAX)
42646 {
42647 error_at (location, "%<#pragma GCC unroll%> requires an"
42648 " assignment-expression that evaluates to a non-negative"
42649 " integral constant less than %u", USHRT_MAX);
42650 unroll = 0;
42651 }
42652 else
42653 {
42654 unroll = (unsigned short)lunroll;
42655 if (unroll == 0)
42656 unroll = 1;
42657 }
42658 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42659 return unroll;
42660 }
42661
42662 /* Normal parsing of a pragma token. Here we can (and must) use the
42663 regular lexer. */
42664
42665 static bool
42666 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
42667 {
42668 cp_token *pragma_tok;
42669 unsigned int id;
42670 tree stmt;
42671 bool ret;
42672
42673 pragma_tok = cp_lexer_consume_token (parser->lexer);
42674 gcc_assert (pragma_tok->type == CPP_PRAGMA);
42675 parser->lexer->in_pragma = true;
42676
42677 id = cp_parser_pragma_kind (pragma_tok);
42678 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
42679 cp_ensure_no_omp_declare_simd (parser);
42680 switch (id)
42681 {
42682 case PRAGMA_GCC_PCH_PREPROCESS:
42683 error_at (pragma_tok->location,
42684 "%<#pragma GCC pch_preprocess%> must be first");
42685 break;
42686
42687 case PRAGMA_OMP_BARRIER:
42688 switch (context)
42689 {
42690 case pragma_compound:
42691 cp_parser_omp_barrier (parser, pragma_tok);
42692 return false;
42693 case pragma_stmt:
42694 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
42695 "used in compound statements", "omp barrier");
42696 break;
42697 default:
42698 goto bad_stmt;
42699 }
42700 break;
42701
42702 case PRAGMA_OMP_DEPOBJ:
42703 switch (context)
42704 {
42705 case pragma_compound:
42706 cp_parser_omp_depobj (parser, pragma_tok);
42707 return false;
42708 case pragma_stmt:
42709 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
42710 "used in compound statements", "omp depobj");
42711 break;
42712 default:
42713 goto bad_stmt;
42714 }
42715 break;
42716
42717 case PRAGMA_OMP_FLUSH:
42718 switch (context)
42719 {
42720 case pragma_compound:
42721 cp_parser_omp_flush (parser, pragma_tok);
42722 return false;
42723 case pragma_stmt:
42724 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
42725 "used in compound statements", "omp flush");
42726 break;
42727 default:
42728 goto bad_stmt;
42729 }
42730 break;
42731
42732 case PRAGMA_OMP_TASKWAIT:
42733 switch (context)
42734 {
42735 case pragma_compound:
42736 cp_parser_omp_taskwait (parser, pragma_tok);
42737 return false;
42738 case pragma_stmt:
42739 error_at (pragma_tok->location,
42740 "%<#pragma %s%> may only be used in compound statements",
42741 "omp taskwait");
42742 break;
42743 default:
42744 goto bad_stmt;
42745 }
42746 break;
42747
42748 case PRAGMA_OMP_TASKYIELD:
42749 switch (context)
42750 {
42751 case pragma_compound:
42752 cp_parser_omp_taskyield (parser, pragma_tok);
42753 return false;
42754 case pragma_stmt:
42755 error_at (pragma_tok->location,
42756 "%<#pragma %s%> may only be used in compound statements",
42757 "omp taskyield");
42758 break;
42759 default:
42760 goto bad_stmt;
42761 }
42762 break;
42763
42764 case PRAGMA_OMP_CANCEL:
42765 switch (context)
42766 {
42767 case pragma_compound:
42768 cp_parser_omp_cancel (parser, pragma_tok);
42769 return false;
42770 case pragma_stmt:
42771 error_at (pragma_tok->location,
42772 "%<#pragma %s%> may only be used in compound statements",
42773 "omp cancel");
42774 break;
42775 default:
42776 goto bad_stmt;
42777 }
42778 break;
42779
42780 case PRAGMA_OMP_CANCELLATION_POINT:
42781 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
42782 return false;
42783
42784 case PRAGMA_OMP_THREADPRIVATE:
42785 cp_parser_omp_threadprivate (parser, pragma_tok);
42786 return false;
42787
42788 case PRAGMA_OMP_DECLARE:
42789 return cp_parser_omp_declare (parser, pragma_tok, context);
42790
42791 case PRAGMA_OACC_DECLARE:
42792 cp_parser_oacc_declare (parser, pragma_tok);
42793 return false;
42794
42795 case PRAGMA_OACC_ENTER_DATA:
42796 if (context == pragma_stmt)
42797 {
42798 error_at (pragma_tok->location,
42799 "%<#pragma %s%> may only be used in compound statements",
42800 "acc enter data");
42801 break;
42802 }
42803 else if (context != pragma_compound)
42804 goto bad_stmt;
42805 cp_parser_omp_construct (parser, pragma_tok, if_p);
42806 return true;
42807
42808 case PRAGMA_OACC_EXIT_DATA:
42809 if (context == pragma_stmt)
42810 {
42811 error_at (pragma_tok->location,
42812 "%<#pragma %s%> may only be used in compound statements",
42813 "acc exit data");
42814 break;
42815 }
42816 else if (context != pragma_compound)
42817 goto bad_stmt;
42818 cp_parser_omp_construct (parser, pragma_tok, if_p);
42819 return true;
42820
42821 case PRAGMA_OACC_ROUTINE:
42822 if (context != pragma_external)
42823 {
42824 error_at (pragma_tok->location,
42825 "%<#pragma acc routine%> must be at file scope");
42826 break;
42827 }
42828 cp_parser_oacc_routine (parser, pragma_tok, context);
42829 return false;
42830
42831 case PRAGMA_OACC_UPDATE:
42832 if (context == pragma_stmt)
42833 {
42834 error_at (pragma_tok->location,
42835 "%<#pragma %s%> may only be used in compound statements",
42836 "acc update");
42837 break;
42838 }
42839 else if (context != pragma_compound)
42840 goto bad_stmt;
42841 cp_parser_omp_construct (parser, pragma_tok, if_p);
42842 return true;
42843
42844 case PRAGMA_OACC_WAIT:
42845 if (context == pragma_stmt)
42846 {
42847 error_at (pragma_tok->location,
42848 "%<#pragma %s%> may only be used in compound statements",
42849 "acc wait");
42850 break;
42851 }
42852 else if (context != pragma_compound)
42853 goto bad_stmt;
42854 cp_parser_omp_construct (parser, pragma_tok, if_p);
42855 return true;
42856
42857 case PRAGMA_OACC_ATOMIC:
42858 case PRAGMA_OACC_CACHE:
42859 case PRAGMA_OACC_DATA:
42860 case PRAGMA_OACC_HOST_DATA:
42861 case PRAGMA_OACC_KERNELS:
42862 case PRAGMA_OACC_LOOP:
42863 case PRAGMA_OACC_PARALLEL:
42864 case PRAGMA_OACC_SERIAL:
42865 case PRAGMA_OMP_ATOMIC:
42866 case PRAGMA_OMP_CRITICAL:
42867 case PRAGMA_OMP_DISTRIBUTE:
42868 case PRAGMA_OMP_FOR:
42869 case PRAGMA_OMP_LOOP:
42870 case PRAGMA_OMP_MASTER:
42871 case PRAGMA_OMP_PARALLEL:
42872 case PRAGMA_OMP_SECTIONS:
42873 case PRAGMA_OMP_SIMD:
42874 case PRAGMA_OMP_SINGLE:
42875 case PRAGMA_OMP_TASK:
42876 case PRAGMA_OMP_TASKGROUP:
42877 case PRAGMA_OMP_TASKLOOP:
42878 case PRAGMA_OMP_TEAMS:
42879 if (context != pragma_stmt && context != pragma_compound)
42880 goto bad_stmt;
42881 stmt = push_omp_privatization_clauses (false);
42882 cp_parser_omp_construct (parser, pragma_tok, if_p);
42883 pop_omp_privatization_clauses (stmt);
42884 return true;
42885
42886 case PRAGMA_OMP_REQUIRES:
42887 return cp_parser_omp_requires (parser, pragma_tok);
42888
42889 case PRAGMA_OMP_ORDERED:
42890 if (context != pragma_stmt && context != pragma_compound)
42891 goto bad_stmt;
42892 stmt = push_omp_privatization_clauses (false);
42893 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
42894 pop_omp_privatization_clauses (stmt);
42895 return ret;
42896
42897 case PRAGMA_OMP_TARGET:
42898 if (context != pragma_stmt && context != pragma_compound)
42899 goto bad_stmt;
42900 stmt = push_omp_privatization_clauses (false);
42901 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
42902 pop_omp_privatization_clauses (stmt);
42903 return ret;
42904
42905 case PRAGMA_OMP_END_DECLARE_TARGET:
42906 cp_parser_omp_end_declare_target (parser, pragma_tok);
42907 return false;
42908
42909 case PRAGMA_OMP_SCAN:
42910 error_at (pragma_tok->location,
42911 "%<#pragma omp scan%> may only be used in "
42912 "a loop construct with %<inscan%> %<reduction%> clause");
42913 break;
42914
42915 case PRAGMA_OMP_SECTION:
42916 error_at (pragma_tok->location,
42917 "%<#pragma omp section%> may only be used in "
42918 "%<#pragma omp sections%> construct");
42919 break;
42920
42921 case PRAGMA_IVDEP:
42922 {
42923 if (context == pragma_external)
42924 {
42925 error_at (pragma_tok->location,
42926 "%<#pragma GCC ivdep%> must be inside a function");
42927 break;
42928 }
42929 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
42930 unsigned short unroll;
42931 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
42932 if (tok->type == CPP_PRAGMA
42933 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
42934 {
42935 tok = cp_lexer_consume_token (parser->lexer);
42936 unroll = cp_parser_pragma_unroll (parser, tok);
42937 tok = cp_lexer_peek_token (the_parser->lexer);
42938 }
42939 else
42940 unroll = 0;
42941 if (tok->type != CPP_KEYWORD
42942 || (tok->keyword != RID_FOR
42943 && tok->keyword != RID_WHILE
42944 && tok->keyword != RID_DO))
42945 {
42946 cp_parser_error (parser, "for, while or do statement expected");
42947 return false;
42948 }
42949 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
42950 return true;
42951 }
42952
42953 case PRAGMA_UNROLL:
42954 {
42955 if (context == pragma_external)
42956 {
42957 error_at (pragma_tok->location,
42958 "%<#pragma GCC unroll%> must be inside a function");
42959 break;
42960 }
42961 const unsigned short unroll
42962 = cp_parser_pragma_unroll (parser, pragma_tok);
42963 bool ivdep;
42964 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
42965 if (tok->type == CPP_PRAGMA
42966 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
42967 {
42968 tok = cp_lexer_consume_token (parser->lexer);
42969 ivdep = cp_parser_pragma_ivdep (parser, tok);
42970 tok = cp_lexer_peek_token (the_parser->lexer);
42971 }
42972 else
42973 ivdep = false;
42974 if (tok->type != CPP_KEYWORD
42975 || (tok->keyword != RID_FOR
42976 && tok->keyword != RID_WHILE
42977 && tok->keyword != RID_DO))
42978 {
42979 cp_parser_error (parser, "for, while or do statement expected");
42980 return false;
42981 }
42982 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
42983 return true;
42984 }
42985
42986 default:
42987 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
42988 c_invoke_pragma_handler (id);
42989 break;
42990
42991 bad_stmt:
42992 cp_parser_error (parser, "expected declaration specifiers");
42993 break;
42994 }
42995
42996 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42997 return false;
42998 }
42999
43000 /* The interface the pragma parsers have to the lexer. */
43001
43002 enum cpp_ttype
43003 pragma_lex (tree *value, location_t *loc)
43004 {
43005 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
43006 enum cpp_ttype ret = tok->type;
43007
43008 *value = tok->u.value;
43009 if (loc)
43010 *loc = tok->location;
43011
43012 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
43013 ret = CPP_EOF;
43014 else if (ret == CPP_STRING)
43015 *value = cp_parser_string_literal (the_parser, false, false);
43016 else
43017 {
43018 if (ret == CPP_KEYWORD)
43019 ret = CPP_NAME;
43020 cp_lexer_consume_token (the_parser->lexer);
43021 }
43022
43023 return ret;
43024 }
43025
43026 \f
43027 /* External interface. */
43028
43029 /* Parse one entire translation unit. */
43030
43031 void
43032 c_parse_file (void)
43033 {
43034 static bool already_called = false;
43035
43036 if (already_called)
43037 fatal_error (input_location,
43038 "inter-module optimizations not implemented for C++");
43039 already_called = true;
43040
43041 the_parser = cp_parser_new ();
43042 push_deferring_access_checks (flag_access_control
43043 ? dk_no_deferred : dk_no_check);
43044 cp_parser_translation_unit (the_parser);
43045 the_parser = NULL;
43046
43047 finish_translation_unit ();
43048 }
43049
43050 /* Create an identifier for a generic parameter type (a synthesized
43051 template parameter implied by `auto' or a concept identifier). */
43052
43053 static GTY(()) int generic_parm_count;
43054 static tree
43055 make_generic_type_name ()
43056 {
43057 char buf[32];
43058 sprintf (buf, "auto:%d", ++generic_parm_count);
43059 return get_identifier (buf);
43060 }
43061
43062 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
43063 (creating a new template parameter list if necessary). Returns the newly
43064 created template type parm. */
43065
43066 static tree
43067 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
43068 {
43069 /* A requires-clause is not a function and cannot have placeholders. */
43070 if (current_binding_level->kind == sk_block)
43071 {
43072 error ("placeholder type not allowed in this context");
43073 return error_mark_node;
43074 }
43075
43076 gcc_assert (current_binding_level->kind == sk_function_parms);
43077
43078 /* We are either continuing a function template that already contains implicit
43079 template parameters, creating a new fully-implicit function template, or
43080 extending an existing explicit function template with implicit template
43081 parameters. */
43082
43083 cp_binding_level *const entry_scope = current_binding_level;
43084
43085 bool become_template = false;
43086 cp_binding_level *parent_scope = 0;
43087
43088 if (parser->implicit_template_scope)
43089 {
43090 gcc_assert (parser->implicit_template_parms);
43091
43092 current_binding_level = parser->implicit_template_scope;
43093 }
43094 else
43095 {
43096 /* Roll back to the existing template parameter scope (in the case of
43097 extending an explicit function template) or introduce a new template
43098 parameter scope ahead of the function parameter scope (or class scope
43099 in the case of out-of-line member definitions). The function scope is
43100 added back after template parameter synthesis below. */
43101
43102 cp_binding_level *scope = entry_scope;
43103
43104 while (scope->kind == sk_function_parms)
43105 {
43106 parent_scope = scope;
43107 scope = scope->level_chain;
43108 }
43109 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
43110 {
43111 /* If not defining a class, then any class scope is a scope level in
43112 an out-of-line member definition. In this case simply wind back
43113 beyond the first such scope to inject the template parameter list.
43114 Otherwise wind back to the class being defined. The latter can
43115 occur in class member friend declarations such as:
43116
43117 class A {
43118 void foo (auto);
43119 };
43120 class B {
43121 friend void A::foo (auto);
43122 };
43123
43124 The template parameter list synthesized for the friend declaration
43125 must be injected in the scope of 'B'. This can also occur in
43126 erroneous cases such as:
43127
43128 struct A {
43129 struct B {
43130 void foo (auto);
43131 };
43132 void B::foo (auto) {}
43133 };
43134
43135 Here the attempted definition of 'B::foo' within 'A' is ill-formed
43136 but, nevertheless, the template parameter list synthesized for the
43137 declarator should be injected into the scope of 'A' as if the
43138 ill-formed template was specified explicitly. */
43139
43140 while (scope->kind == sk_class && !scope->defining_class_p)
43141 {
43142 parent_scope = scope;
43143 scope = scope->level_chain;
43144 }
43145 }
43146
43147 current_binding_level = scope;
43148
43149 if (scope->kind != sk_template_parms
43150 || !function_being_declared_is_template_p (parser))
43151 {
43152 /* Introduce a new template parameter list for implicit template
43153 parameters. */
43154
43155 become_template = true;
43156
43157 parser->implicit_template_scope
43158 = begin_scope (sk_template_parms, NULL);
43159
43160 ++processing_template_decl;
43161
43162 parser->fully_implicit_function_template_p = true;
43163 ++parser->num_template_parameter_lists;
43164 }
43165 else
43166 {
43167 /* Synthesize implicit template parameters at the end of the explicit
43168 template parameter list. */
43169
43170 gcc_assert (current_template_parms);
43171
43172 parser->implicit_template_scope = scope;
43173
43174 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
43175 parser->implicit_template_parms
43176 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
43177 }
43178 }
43179
43180 /* Synthesize a new template parameter and track the current template
43181 parameter chain with implicit_template_parms. */
43182
43183 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
43184 tree synth_id = make_generic_type_name ();
43185 tree synth_tmpl_parm;
43186 bool non_type = false;
43187
43188 /* Synthesize the type template parameter. */
43189 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
43190 synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
43191
43192 /* Attach the constraint to the parm before processing. */
43193 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
43194 TREE_TYPE (node) = constr;
43195 tree new_parm
43196 = process_template_parm (parser->implicit_template_parms,
43197 input_location,
43198 node,
43199 /*non_type=*/non_type,
43200 /*param_pack=*/false);
43201
43202 /* Mark the synthetic declaration "virtual". This is used when
43203 comparing template-heads to determine if whether an abbreviated
43204 function template is equivalent to an explicit template.
43205
43206 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
43207 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
43208
43209 // Chain the new parameter to the list of implicit parameters.
43210 if (parser->implicit_template_parms)
43211 parser->implicit_template_parms
43212 = TREE_CHAIN (parser->implicit_template_parms);
43213 else
43214 parser->implicit_template_parms = new_parm;
43215
43216 tree new_decl = get_local_decls ();
43217 if (non_type)
43218 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
43219 new_decl = DECL_INITIAL (new_decl);
43220
43221 /* If creating a fully implicit function template, start the new implicit
43222 template parameter list with this synthesized type, otherwise grow the
43223 current template parameter list. */
43224
43225 if (become_template)
43226 {
43227 parent_scope->level_chain = current_binding_level;
43228
43229 tree new_parms = make_tree_vec (1);
43230 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
43231 current_template_parms = tree_cons (size_int (processing_template_decl),
43232 new_parms, current_template_parms);
43233 }
43234 else
43235 {
43236 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
43237 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
43238 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
43239 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
43240 }
43241
43242 /* If the new parameter was constrained, we need to add that to the
43243 constraints in the template parameter list. */
43244 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
43245 {
43246 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
43247 reqs = combine_constraint_expressions (reqs, req);
43248 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
43249 }
43250
43251 current_binding_level = entry_scope;
43252
43253 return new_decl;
43254 }
43255
43256 /* Finish the declaration of a fully implicit function template. Such a
43257 template has no explicit template parameter list so has not been through the
43258 normal template head and tail processing. synthesize_implicit_template_parm
43259 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
43260 provided if the declaration is a class member such that its template
43261 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
43262 form is returned. Otherwise NULL_TREE is returned. */
43263
43264 static tree
43265 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
43266 {
43267 gcc_assert (parser->fully_implicit_function_template_p);
43268
43269 if (member_decl_opt && member_decl_opt != error_mark_node
43270 && DECL_VIRTUAL_P (member_decl_opt))
43271 {
43272 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
43273 "implicit templates may not be %<virtual%>");
43274 DECL_VIRTUAL_P (member_decl_opt) = false;
43275 }
43276
43277 if (member_decl_opt)
43278 member_decl_opt = finish_member_template_decl (member_decl_opt);
43279 end_template_decl ();
43280
43281 parser->fully_implicit_function_template_p = false;
43282 parser->implicit_template_parms = 0;
43283 parser->implicit_template_scope = 0;
43284 --parser->num_template_parameter_lists;
43285
43286 return member_decl_opt;
43287 }
43288
43289 /* Like finish_fully_implicit_template, but to be used in error
43290 recovery, rearranging scopes so that we restore the state we had
43291 before synthesize_implicit_template_parm inserted the implement
43292 template parms scope. */
43293
43294 static void
43295 abort_fully_implicit_template (cp_parser *parser)
43296 {
43297 cp_binding_level *return_to_scope = current_binding_level;
43298
43299 if (parser->implicit_template_scope
43300 && return_to_scope != parser->implicit_template_scope)
43301 {
43302 cp_binding_level *child = return_to_scope;
43303 for (cp_binding_level *scope = child->level_chain;
43304 scope != parser->implicit_template_scope;
43305 scope = child->level_chain)
43306 child = scope;
43307 child->level_chain = parser->implicit_template_scope->level_chain;
43308 parser->implicit_template_scope->level_chain = return_to_scope;
43309 current_binding_level = parser->implicit_template_scope;
43310 }
43311 else
43312 return_to_scope = return_to_scope->level_chain;
43313
43314 finish_fully_implicit_template (parser, NULL);
43315
43316 gcc_assert (current_binding_level == return_to_scope);
43317 }
43318
43319 /* Helper function for diagnostics that have complained about things
43320 being used with 'extern "C"' linkage.
43321
43322 Attempt to issue a note showing where the 'extern "C"' linkage began. */
43323
43324 void
43325 maybe_show_extern_c_location (void)
43326 {
43327 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
43328 inform (the_parser->innermost_linkage_specification_location,
43329 "%<extern \"C\"%> linkage started here");
43330 }
43331
43332 #include "gt-cp-parser.h"