]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
PR c++/60364 - noreturn after first decl not diagnosed.
[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 static cp_token eof_token =
56 {
57 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
58 };
59
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant {
62 NIC_NONE,
63 /* floating-point literal */
64 NIC_FLOAT,
65 /* %<this%> */
66 NIC_THIS,
67 /* %<__FUNCTION__%> */
68 NIC_FUNC_NAME,
69 /* %<__PRETTY_FUNCTION__%> */
70 NIC_PRETTY_FUNC,
71 /* %<__func__%> */
72 NIC_C99_FUNC,
73 /* "%<va_arg%> */
74 NIC_VA_ARG,
75 /* a cast */
76 NIC_CAST,
77 /* %<typeid%> operator */
78 NIC_TYPEID,
79 /* non-constant compound literals */
80 NIC_NCC,
81 /* a function call */
82 NIC_FUNC_CALL,
83 /* an increment */
84 NIC_INC,
85 /* an decrement */
86 NIC_DEC,
87 /* an array reference */
88 NIC_ARRAY_REF,
89 /* %<->%> */
90 NIC_ARROW,
91 /* %<.%> */
92 NIC_POINT,
93 /* the address of a label */
94 NIC_ADDR_LABEL,
95 /* %<*%> */
96 NIC_STAR,
97 /* %<&%> */
98 NIC_ADDR,
99 /* %<++%> */
100 NIC_PREINCREMENT,
101 /* %<--%> */
102 NIC_PREDECREMENT,
103 /* %<new%> */
104 NIC_NEW,
105 /* %<delete%> */
106 NIC_DEL,
107 /* calls to overloaded operators */
108 NIC_OVERLOADED,
109 /* an assignment */
110 NIC_ASSIGNMENT,
111 /* a comma operator */
112 NIC_COMMA,
113 /* a call to a constructor */
114 NIC_CONSTRUCTOR,
115 /* a transaction expression */
116 NIC_TRANSACTION
117 };
118
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error {
121 /* NULL */
122 NLE_NULL,
123 /* is not a type */
124 NLE_TYPE,
125 /* is not a class or namespace */
126 NLE_CXX98,
127 /* is not a class, namespace, or enumeration */
128 NLE_NOT_CXX98
129 };
130
131 /* The various kinds of required token */
132 enum required_token {
133 RT_NONE,
134 RT_SEMICOLON, /* ';' */
135 RT_OPEN_PAREN, /* '(' */
136 RT_CLOSE_BRACE, /* '}' */
137 RT_OPEN_BRACE, /* '{' */
138 RT_CLOSE_SQUARE, /* ']' */
139 RT_OPEN_SQUARE, /* '[' */
140 RT_COMMA, /* ',' */
141 RT_SCOPE, /* '::' */
142 RT_LESS, /* '<' */
143 RT_GREATER, /* '>' */
144 RT_EQ, /* '=' */
145 RT_ELLIPSIS, /* '...' */
146 RT_MULT, /* '*' */
147 RT_COMPL, /* '~' */
148 RT_COLON, /* ':' */
149 RT_COLON_SCOPE, /* ':' or '::' */
150 RT_CLOSE_PAREN, /* ')' */
151 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
152 RT_PRAGMA_EOL, /* end of line */
153 RT_NAME, /* identifier */
154
155 /* The type is CPP_KEYWORD */
156 RT_NEW, /* new */
157 RT_DELETE, /* delete */
158 RT_RETURN, /* return */
159 RT_WHILE, /* while */
160 RT_EXTERN, /* extern */
161 RT_STATIC_ASSERT, /* static_assert */
162 RT_DECLTYPE, /* decltype */
163 RT_OPERATOR, /* operator */
164 RT_CLASS, /* class */
165 RT_TEMPLATE, /* template */
166 RT_NAMESPACE, /* namespace */
167 RT_USING, /* using */
168 RT_ASM, /* asm */
169 RT_TRY, /* try */
170 RT_CATCH, /* catch */
171 RT_THROW, /* throw */
172 RT_LABEL, /* __label__ */
173 RT_AT_TRY, /* @try */
174 RT_AT_SYNCHRONIZED, /* @synchronized */
175 RT_AT_THROW, /* @throw */
176
177 RT_SELECT, /* selection-statement */
178 RT_ITERATION, /* iteration-statement */
179 RT_JUMP, /* jump-statement */
180 RT_CLASS_KEY, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL /* __transaction_cancel */
185 };
186
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
189
190 class type_id_in_expr_sentinel
191 {
192 cp_parser *parser;
193 bool saved;
194 public:
195 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
196 : parser (parser),
197 saved (parser->in_type_id_in_expr_p)
198 { parser->in_type_id_in_expr_p = set; }
199 ~type_id_in_expr_sentinel ()
200 { parser->in_type_id_in_expr_p = saved; }
201 };
202
203 /* Prototypes. */
204
205 static cp_lexer *cp_lexer_new_main
206 (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208 (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210 (cp_lexer *);
211 static int cp_lexer_saving_tokens
212 (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214 (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218 (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220 (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228 (cp_lexer *);
229 static void cp_lexer_purge_token
230 (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234 (cp_lexer *);
235 static void cp_lexer_commit_tokens
236 (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238 (cp_lexer *);
239 static void cp_lexer_print_token
240 (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242 (cp_lexer *);
243 static void cp_lexer_start_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
247
248 static cp_token_cache *cp_token_cache_new
249 (cp_token *, cp_token *);
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->last_token = lexer->buffer->address ()
659 + lexer->buffer->length ()
660 - 1;
661 lexer->next_token = lexer->buffer->length ()
662 ? lexer->buffer->address ()
663 : &eof_token;
664
665 /* Subsequent preprocessor diagnostics should use compiler
666 diagnostic functions to get the compiler source location. */
667 done_lexing = true;
668
669 gcc_assert (!lexer->next_token->purged_p);
670 return lexer;
671 }
672
673 /* Create a new lexer whose token stream is primed with the tokens in
674 CACHE. When these tokens are exhausted, no new tokens will be read. */
675
676 static cp_lexer *
677 cp_lexer_new_from_tokens (cp_token_cache *cache)
678 {
679 cp_token *first = cache->first;
680 cp_token *last = cache->last;
681 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
682
683 /* We do not own the buffer. */
684 lexer->buffer = NULL;
685 lexer->next_token = first == last ? &eof_token : first;
686 lexer->last_token = last;
687
688 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
689
690 /* Initially we are not debugging. */
691 lexer->debugging_p = false;
692
693 gcc_assert (!lexer->next_token->purged_p);
694 return lexer;
695 }
696
697 /* Frees all resources associated with LEXER. */
698
699 static void
700 cp_lexer_destroy (cp_lexer *lexer)
701 {
702 vec_free (lexer->buffer);
703 lexer->saved_tokens.release ();
704 ggc_free (lexer);
705 }
706
707 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
708 be used. The point of this flag is to help the compiler to fold away calls
709 to cp_lexer_debugging_p within this source file at compile time, when the
710 lexer is not being debugged. */
711
712 #define LEXER_DEBUGGING_ENABLED_P false
713
714 /* Returns nonzero if debugging information should be output. */
715
716 static inline bool
717 cp_lexer_debugging_p (cp_lexer *lexer)
718 {
719 if (!LEXER_DEBUGGING_ENABLED_P)
720 return false;
721
722 return lexer->debugging_p;
723 }
724
725
726 static inline cp_token_position
727 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
728 {
729 gcc_assert (!previous_p || lexer->next_token != &eof_token);
730
731 return lexer->next_token - previous_p;
732 }
733
734 static inline cp_token *
735 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
736 {
737 return pos;
738 }
739
740 static inline void
741 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
742 {
743 lexer->next_token = cp_lexer_token_at (lexer, pos);
744 }
745
746 static inline cp_token_position
747 cp_lexer_previous_token_position (cp_lexer *lexer)
748 {
749 if (lexer->next_token == &eof_token)
750 return lexer->last_token - 1;
751 else
752 return cp_lexer_token_position (lexer, true);
753 }
754
755 static inline cp_token *
756 cp_lexer_previous_token (cp_lexer *lexer)
757 {
758 cp_token_position tp = cp_lexer_previous_token_position (lexer);
759
760 /* Skip past purged tokens. */
761 while (tp->purged_p)
762 {
763 gcc_assert (tp != vec_safe_address (lexer->buffer));
764 tp--;
765 }
766
767 return cp_lexer_token_at (lexer, tp);
768 }
769
770 /* nonzero if we are presently saving tokens. */
771
772 static inline int
773 cp_lexer_saving_tokens (const cp_lexer* lexer)
774 {
775 return lexer->saved_tokens.length () != 0;
776 }
777
778 /* Store the next token from the preprocessor in *TOKEN. Return true
779 if we reach EOF. If LEXER is NULL, assume we are handling an
780 initial #pragma pch_preprocess, and thus want the lexer to return
781 processed strings. */
782
783 static void
784 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
785 {
786 static int is_extern_c = 0;
787
788 /* Get a new token from the preprocessor. */
789 token->type
790 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
791 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
792 token->keyword = RID_MAX;
793 token->purged_p = false;
794 token->error_reported = false;
795
796 /* On some systems, some header files are surrounded by an
797 implicit extern "C" block. Set a flag in the token if it
798 comes from such a header. */
799 is_extern_c += pending_lang_change;
800 pending_lang_change = 0;
801 token->implicit_extern_c = is_extern_c > 0;
802
803 /* Check to see if this token is a keyword. */
804 if (token->type == CPP_NAME)
805 {
806 if (IDENTIFIER_KEYWORD_P (token->u.value))
807 {
808 /* Mark this token as a keyword. */
809 token->type = CPP_KEYWORD;
810 /* Record which keyword. */
811 token->keyword = C_RID_CODE (token->u.value);
812 }
813 else
814 {
815 if (warn_cxx11_compat
816 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
817 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
818 {
819 /* Warn about the C++0x keyword (but still treat it as
820 an identifier). */
821 warning (OPT_Wc__11_compat,
822 "identifier %qE is a keyword in C++11",
823 token->u.value);
824
825 /* Clear out the C_RID_CODE so we don't warn about this
826 particular identifier-turned-keyword again. */
827 C_SET_RID_CODE (token->u.value, RID_MAX);
828 }
829
830 token->keyword = RID_MAX;
831 }
832 }
833 else if (token->type == CPP_AT_NAME)
834 {
835 /* This only happens in Objective-C++; it must be a keyword. */
836 token->type = CPP_KEYWORD;
837 switch (C_RID_CODE (token->u.value))
838 {
839 /* Replace 'class' with '@class', 'private' with '@private',
840 etc. This prevents confusion with the C++ keyword
841 'class', and makes the tokens consistent with other
842 Objective-C 'AT' keywords. For example '@class' is
843 reported as RID_AT_CLASS which is consistent with
844 '@synchronized', which is reported as
845 RID_AT_SYNCHRONIZED.
846 */
847 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
848 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
849 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
850 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
851 case RID_THROW: token->keyword = RID_AT_THROW; break;
852 case RID_TRY: token->keyword = RID_AT_TRY; break;
853 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
854 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
855 default: token->keyword = C_RID_CODE (token->u.value);
856 }
857 }
858 }
859
860 /* Update the globals input_location and the input file stack from TOKEN. */
861 static inline void
862 cp_lexer_set_source_position_from_token (cp_token *token)
863 {
864 if (token->type != CPP_EOF)
865 {
866 input_location = token->location;
867 }
868 }
869
870 /* Update the globals input_location and the input file stack from LEXER. */
871 static inline void
872 cp_lexer_set_source_position (cp_lexer *lexer)
873 {
874 cp_token *token = cp_lexer_peek_token (lexer);
875 cp_lexer_set_source_position_from_token (token);
876 }
877
878 /* Return a pointer to the next token in the token stream, but do not
879 consume it. */
880
881 static inline cp_token *
882 cp_lexer_peek_token (cp_lexer *lexer)
883 {
884 if (cp_lexer_debugging_p (lexer))
885 {
886 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
887 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
888 putc ('\n', cp_lexer_debug_stream);
889 }
890 return lexer->next_token;
891 }
892
893 /* Return true if the next token has the indicated TYPE. */
894
895 static inline bool
896 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
897 {
898 return cp_lexer_peek_token (lexer)->type == type;
899 }
900
901 /* Return true if the next token does not have the indicated TYPE. */
902
903 static inline bool
904 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
905 {
906 return !cp_lexer_next_token_is (lexer, type);
907 }
908
909 /* Return true if the next token is the indicated KEYWORD. */
910
911 static inline bool
912 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
913 {
914 return cp_lexer_peek_token (lexer)->keyword == keyword;
915 }
916
917 static inline bool
918 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
919 {
920 return cp_lexer_peek_nth_token (lexer, n)->type == type;
921 }
922
923 static inline bool
924 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
925 {
926 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
927 }
928
929 /* Return true if KEYWORD can start a decl-specifier. */
930
931 bool
932 cp_keyword_starts_decl_specifier_p (enum rid keyword)
933 {
934 switch (keyword)
935 {
936 /* auto specifier: storage-class-specifier in C++,
937 simple-type-specifier in C++0x. */
938 case RID_AUTO:
939 /* Storage classes. */
940 case RID_REGISTER:
941 case RID_STATIC:
942 case RID_EXTERN:
943 case RID_MUTABLE:
944 case RID_THREAD:
945 /* Elaborated type specifiers. */
946 case RID_ENUM:
947 case RID_CLASS:
948 case RID_STRUCT:
949 case RID_UNION:
950 case RID_TYPENAME:
951 /* Simple type specifiers. */
952 case RID_CHAR:
953 case RID_CHAR8:
954 case RID_CHAR16:
955 case RID_CHAR32:
956 case RID_WCHAR:
957 case RID_BOOL:
958 case RID_SHORT:
959 case RID_INT:
960 case RID_LONG:
961 case RID_SIGNED:
962 case RID_UNSIGNED:
963 case RID_FLOAT:
964 case RID_DOUBLE:
965 case RID_VOID:
966 /* GNU extensions. */
967 case RID_ATTRIBUTE:
968 case RID_TYPEOF:
969 /* C++0x extensions. */
970 case RID_DECLTYPE:
971 case RID_UNDERLYING_TYPE:
972 case RID_CONSTEXPR:
973 return true;
974
975 default:
976 if (keyword >= RID_FIRST_INT_N
977 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
978 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
979 return true;
980 return false;
981 }
982 }
983
984 /* Return true if the next token is a keyword for a decl-specifier. */
985
986 static bool
987 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
988 {
989 cp_token *token;
990
991 token = cp_lexer_peek_token (lexer);
992 return cp_keyword_starts_decl_specifier_p (token->keyword);
993 }
994
995 /* Returns TRUE iff the token T begins a decltype type. */
996
997 static bool
998 token_is_decltype (cp_token *t)
999 {
1000 return (t->keyword == RID_DECLTYPE
1001 || t->type == CPP_DECLTYPE);
1002 }
1003
1004 /* Returns TRUE iff the next token begins a decltype type. */
1005
1006 static bool
1007 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1008 {
1009 cp_token *t = cp_lexer_peek_token (lexer);
1010 return token_is_decltype (t);
1011 }
1012
1013 /* Called when processing a token with tree_check_value; perform or defer the
1014 associated checks and return the value. */
1015
1016 static tree
1017 saved_checks_value (struct tree_check *check_value)
1018 {
1019 /* Perform any access checks that were deferred. */
1020 vec<deferred_access_check, va_gc> *checks;
1021 deferred_access_check *chk;
1022 checks = check_value->checks;
1023 if (checks)
1024 {
1025 int i;
1026 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1027 perform_or_defer_access_check (chk->binfo,
1028 chk->decl,
1029 chk->diag_decl, tf_warning_or_error);
1030 }
1031 /* Return the stored value. */
1032 return check_value->value;
1033 }
1034
1035 /* Return a pointer to the Nth token in the token stream. If N is 1,
1036 then this is precisely equivalent to cp_lexer_peek_token (except
1037 that it is not inline). One would like to disallow that case, but
1038 there is one case (cp_parser_nth_token_starts_template_id) where
1039 the caller passes a variable for N and it might be 1. */
1040
1041 static cp_token *
1042 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1043 {
1044 cp_token *token;
1045
1046 /* N is 1-based, not zero-based. */
1047 gcc_assert (n > 0);
1048
1049 if (cp_lexer_debugging_p (lexer))
1050 fprintf (cp_lexer_debug_stream,
1051 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1052
1053 --n;
1054 token = lexer->next_token;
1055 gcc_assert (!n || token != &eof_token);
1056 while (n != 0)
1057 {
1058 ++token;
1059 if (token == lexer->last_token)
1060 {
1061 token = &eof_token;
1062 break;
1063 }
1064
1065 if (!token->purged_p)
1066 --n;
1067 }
1068
1069 if (cp_lexer_debugging_p (lexer))
1070 {
1071 cp_lexer_print_token (cp_lexer_debug_stream, token);
1072 putc ('\n', cp_lexer_debug_stream);
1073 }
1074
1075 return token;
1076 }
1077
1078 /* Return the next token, and advance the lexer's next_token pointer
1079 to point to the next non-purged token. */
1080
1081 static cp_token *
1082 cp_lexer_consume_token (cp_lexer* lexer)
1083 {
1084 cp_token *token = lexer->next_token;
1085
1086 gcc_assert (token != &eof_token);
1087 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1088
1089 do
1090 {
1091 lexer->next_token++;
1092 if (lexer->next_token == lexer->last_token)
1093 {
1094 lexer->next_token = &eof_token;
1095 break;
1096 }
1097
1098 }
1099 while (lexer->next_token->purged_p);
1100
1101 cp_lexer_set_source_position_from_token (token);
1102
1103 /* Provide debugging output. */
1104 if (cp_lexer_debugging_p (lexer))
1105 {
1106 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1107 cp_lexer_print_token (cp_lexer_debug_stream, token);
1108 putc ('\n', cp_lexer_debug_stream);
1109 }
1110
1111 return token;
1112 }
1113
1114 /* Permanently remove the next token from the token stream, and
1115 advance the next_token pointer to refer to the next non-purged
1116 token. */
1117
1118 static void
1119 cp_lexer_purge_token (cp_lexer *lexer)
1120 {
1121 cp_token *tok = lexer->next_token;
1122
1123 gcc_assert (tok != &eof_token);
1124 tok->purged_p = true;
1125 tok->location = UNKNOWN_LOCATION;
1126 tok->u.value = NULL_TREE;
1127 tok->keyword = RID_MAX;
1128
1129 do
1130 {
1131 tok++;
1132 if (tok == lexer->last_token)
1133 {
1134 tok = &eof_token;
1135 break;
1136 }
1137 }
1138 while (tok->purged_p);
1139 lexer->next_token = tok;
1140 }
1141
1142 /* Permanently remove all tokens after TOK, up to, but not
1143 including, the token that will be returned next by
1144 cp_lexer_peek_token. */
1145
1146 static void
1147 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1148 {
1149 cp_token *peek = lexer->next_token;
1150
1151 if (peek == &eof_token)
1152 peek = lexer->last_token;
1153
1154 gcc_assert (tok < peek);
1155
1156 for ( tok += 1; tok != peek; tok += 1)
1157 {
1158 tok->purged_p = true;
1159 tok->location = UNKNOWN_LOCATION;
1160 tok->u.value = NULL_TREE;
1161 tok->keyword = RID_MAX;
1162 }
1163 }
1164
1165 /* Begin saving tokens. All tokens consumed after this point will be
1166 preserved. */
1167
1168 static void
1169 cp_lexer_save_tokens (cp_lexer* lexer)
1170 {
1171 /* Provide debugging output. */
1172 if (cp_lexer_debugging_p (lexer))
1173 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1174
1175 lexer->saved_tokens.safe_push (lexer->next_token);
1176 }
1177
1178 /* Commit to the portion of the token stream most recently saved. */
1179
1180 static void
1181 cp_lexer_commit_tokens (cp_lexer* lexer)
1182 {
1183 /* Provide debugging output. */
1184 if (cp_lexer_debugging_p (lexer))
1185 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1186
1187 lexer->saved_tokens.pop ();
1188 }
1189
1190 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1191 to the token stream. Stop saving tokens. */
1192
1193 static void
1194 cp_lexer_rollback_tokens (cp_lexer* lexer)
1195 {
1196 /* Provide debugging output. */
1197 if (cp_lexer_debugging_p (lexer))
1198 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1199
1200 lexer->next_token = lexer->saved_tokens.pop ();
1201 }
1202
1203 /* RAII wrapper around the above functions, with sanity checking. Creating
1204 a variable saves tokens, which are committed when the variable is
1205 destroyed unless they are explicitly rolled back by calling the rollback
1206 member function. */
1207
1208 struct saved_token_sentinel
1209 {
1210 cp_lexer *lexer;
1211 unsigned len;
1212 bool commit;
1213 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1214 {
1215 len = lexer->saved_tokens.length ();
1216 cp_lexer_save_tokens (lexer);
1217 }
1218 void rollback ()
1219 {
1220 cp_lexer_rollback_tokens (lexer);
1221 commit = false;
1222 }
1223 ~saved_token_sentinel()
1224 {
1225 if (commit)
1226 cp_lexer_commit_tokens (lexer);
1227 gcc_assert (lexer->saved_tokens.length () == len);
1228 }
1229 };
1230
1231 /* Print a representation of the TOKEN on the STREAM. */
1232
1233 static void
1234 cp_lexer_print_token (FILE * stream, cp_token *token)
1235 {
1236 /* We don't use cpp_type2name here because the parser defines
1237 a few tokens of its own. */
1238 static const char *const token_names[] = {
1239 /* cpplib-defined token types */
1240 #define OP(e, s) #e,
1241 #define TK(e, s) #e,
1242 TTYPE_TABLE
1243 #undef OP
1244 #undef TK
1245 /* C++ parser token types - see "Manifest constants", above. */
1246 "KEYWORD",
1247 "TEMPLATE_ID",
1248 "NESTED_NAME_SPECIFIER",
1249 };
1250
1251 /* For some tokens, print the associated data. */
1252 switch (token->type)
1253 {
1254 case CPP_KEYWORD:
1255 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1256 For example, `struct' is mapped to an INTEGER_CST. */
1257 if (!identifier_p (token->u.value))
1258 break;
1259 /* fall through */
1260 case CPP_NAME:
1261 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1262 break;
1263
1264 case CPP_STRING:
1265 case CPP_STRING16:
1266 case CPP_STRING32:
1267 case CPP_WSTRING:
1268 case CPP_UTF8STRING:
1269 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1270 break;
1271
1272 case CPP_NUMBER:
1273 print_generic_expr (stream, token->u.value);
1274 break;
1275
1276 default:
1277 /* If we have a name for the token, print it out. Otherwise, we
1278 simply give the numeric code. */
1279 if (token->type < ARRAY_SIZE(token_names))
1280 fputs (token_names[token->type], stream);
1281 else
1282 fprintf (stream, "[%d]", token->type);
1283 break;
1284 }
1285 }
1286
1287 DEBUG_FUNCTION void
1288 debug (cp_token &ref)
1289 {
1290 cp_lexer_print_token (stderr, &ref);
1291 fprintf (stderr, "\n");
1292 }
1293
1294 DEBUG_FUNCTION void
1295 debug (cp_token *ptr)
1296 {
1297 if (ptr)
1298 debug (*ptr);
1299 else
1300 fprintf (stderr, "<nil>\n");
1301 }
1302
1303
1304 /* Start emitting debugging information. */
1305
1306 static void
1307 cp_lexer_start_debugging (cp_lexer* lexer)
1308 {
1309 if (!LEXER_DEBUGGING_ENABLED_P)
1310 fatal_error (input_location,
1311 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1312
1313 lexer->debugging_p = true;
1314 cp_lexer_debug_stream = stderr;
1315 }
1316
1317 /* Stop emitting debugging information. */
1318
1319 static void
1320 cp_lexer_stop_debugging (cp_lexer* lexer)
1321 {
1322 if (!LEXER_DEBUGGING_ENABLED_P)
1323 fatal_error (input_location,
1324 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1325
1326 lexer->debugging_p = false;
1327 cp_lexer_debug_stream = NULL;
1328 }
1329
1330 /* Create a new cp_token_cache, representing a range of tokens. */
1331
1332 static cp_token_cache *
1333 cp_token_cache_new (cp_token *first, cp_token *last)
1334 {
1335 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1336 cache->first = first;
1337 cache->last = last;
1338 return cache;
1339 }
1340
1341 /* Diagnose if #pragma omp declare simd isn't followed immediately
1342 by function declaration or definition. */
1343
1344 static inline void
1345 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1346 {
1347 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1348 {
1349 error ("%<#pragma omp declare simd%> not immediately followed by "
1350 "function declaration or definition");
1351 parser->omp_declare_simd = NULL;
1352 }
1353 }
1354
1355 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1356 and put that into "omp declare simd" attribute. */
1357
1358 static inline void
1359 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1360 {
1361 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1362 {
1363 if (fndecl == error_mark_node)
1364 {
1365 parser->omp_declare_simd = NULL;
1366 return;
1367 }
1368 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1369 {
1370 cp_ensure_no_omp_declare_simd (parser);
1371 return;
1372 }
1373 }
1374 }
1375
1376 /* Diagnose if #pragma acc routine isn't followed immediately by function
1377 declaration or definition. */
1378
1379 static inline void
1380 cp_ensure_no_oacc_routine (cp_parser *parser)
1381 {
1382 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1383 {
1384 error_at (parser->oacc_routine->loc,
1385 "%<#pragma acc routine%> not immediately followed by "
1386 "function declaration or definition");
1387 parser->oacc_routine = NULL;
1388 }
1389 }
1390 \f
1391 /* Decl-specifiers. */
1392
1393 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1394
1395 static void
1396 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1397 {
1398 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1399 }
1400
1401 /* Declarators. */
1402
1403 /* Nothing other than the parser should be creating declarators;
1404 declarators are a semi-syntactic representation of C++ entities.
1405 Other parts of the front end that need to create entities (like
1406 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1407
1408 static cp_declarator *make_call_declarator
1409 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1410 static cp_declarator *make_array_declarator
1411 (cp_declarator *, tree);
1412 static cp_declarator *make_pointer_declarator
1413 (cp_cv_quals, cp_declarator *, tree);
1414 static cp_declarator *make_reference_declarator
1415 (cp_cv_quals, cp_declarator *, bool, tree);
1416 static cp_declarator *make_ptrmem_declarator
1417 (cp_cv_quals, tree, cp_declarator *, tree);
1418
1419 /* An erroneous declarator. */
1420 static cp_declarator *cp_error_declarator;
1421
1422 /* The obstack on which declarators and related data structures are
1423 allocated. */
1424 static struct obstack declarator_obstack;
1425
1426 /* Alloc BYTES from the declarator memory pool. */
1427
1428 static inline void *
1429 alloc_declarator (size_t bytes)
1430 {
1431 return obstack_alloc (&declarator_obstack, bytes);
1432 }
1433
1434 /* Allocate a declarator of the indicated KIND. Clear fields that are
1435 common to all declarators. */
1436
1437 static cp_declarator *
1438 make_declarator (cp_declarator_kind kind)
1439 {
1440 cp_declarator *declarator;
1441
1442 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1443 declarator->kind = kind;
1444 declarator->parenthesized = UNKNOWN_LOCATION;
1445 declarator->attributes = NULL_TREE;
1446 declarator->std_attributes = NULL_TREE;
1447 declarator->declarator = NULL;
1448 declarator->parameter_pack_p = false;
1449 declarator->id_loc = UNKNOWN_LOCATION;
1450
1451 return declarator;
1452 }
1453
1454 /* Make a declarator for a generalized identifier. If
1455 QUALIFYING_SCOPE is non-NULL, the identifier is
1456 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1457 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1458 is, if any. */
1459
1460 static cp_declarator *
1461 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1462 special_function_kind sfk, location_t id_location)
1463 {
1464 cp_declarator *declarator;
1465
1466 /* It is valid to write:
1467
1468 class C { void f(); };
1469 typedef C D;
1470 void D::f();
1471
1472 The standard is not clear about whether `typedef const C D' is
1473 legal; as of 2002-09-15 the committee is considering that
1474 question. EDG 3.0 allows that syntax. Therefore, we do as
1475 well. */
1476 if (qualifying_scope && TYPE_P (qualifying_scope))
1477 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1478
1479 gcc_assert (identifier_p (unqualified_name)
1480 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1481 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1482
1483 declarator = make_declarator (cdk_id);
1484 declarator->u.id.qualifying_scope = qualifying_scope;
1485 declarator->u.id.unqualified_name = unqualified_name;
1486 declarator->u.id.sfk = sfk;
1487 declarator->id_loc = id_location;
1488
1489 return declarator;
1490 }
1491
1492 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1493 of modifiers such as const or volatile to apply to the pointer
1494 type, represented as identifiers. ATTRIBUTES represent the attributes that
1495 appertain to the pointer or reference. */
1496
1497 cp_declarator *
1498 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1499 tree attributes)
1500 {
1501 cp_declarator *declarator;
1502
1503 declarator = make_declarator (cdk_pointer);
1504 declarator->declarator = target;
1505 declarator->u.pointer.qualifiers = cv_qualifiers;
1506 declarator->u.pointer.class_type = NULL_TREE;
1507 if (target)
1508 {
1509 declarator->id_loc = target->id_loc;
1510 declarator->parameter_pack_p = target->parameter_pack_p;
1511 target->parameter_pack_p = false;
1512 }
1513 else
1514 declarator->parameter_pack_p = false;
1515
1516 declarator->std_attributes = attributes;
1517
1518 return declarator;
1519 }
1520
1521 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1522 represent the attributes that appertain to the pointer or
1523 reference. */
1524
1525 cp_declarator *
1526 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1527 bool rvalue_ref, tree attributes)
1528 {
1529 cp_declarator *declarator;
1530
1531 declarator = make_declarator (cdk_reference);
1532 declarator->declarator = target;
1533 declarator->u.reference.qualifiers = cv_qualifiers;
1534 declarator->u.reference.rvalue_ref = rvalue_ref;
1535 if (target)
1536 {
1537 declarator->id_loc = target->id_loc;
1538 declarator->parameter_pack_p = target->parameter_pack_p;
1539 target->parameter_pack_p = false;
1540 }
1541 else
1542 declarator->parameter_pack_p = false;
1543
1544 declarator->std_attributes = attributes;
1545
1546 return declarator;
1547 }
1548
1549 /* Like make_pointer_declarator -- but for a pointer to a non-static
1550 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1551 appertain to the pointer or reference. */
1552
1553 cp_declarator *
1554 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1555 cp_declarator *pointee,
1556 tree attributes)
1557 {
1558 cp_declarator *declarator;
1559
1560 declarator = make_declarator (cdk_ptrmem);
1561 declarator->declarator = pointee;
1562 declarator->u.pointer.qualifiers = cv_qualifiers;
1563 declarator->u.pointer.class_type = class_type;
1564
1565 if (pointee)
1566 {
1567 declarator->parameter_pack_p = pointee->parameter_pack_p;
1568 pointee->parameter_pack_p = false;
1569 }
1570 else
1571 declarator->parameter_pack_p = false;
1572
1573 declarator->std_attributes = attributes;
1574
1575 return declarator;
1576 }
1577
1578 /* Make a declarator for the function given by TARGET, with the
1579 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1580 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1581 indicates what exceptions can be thrown. */
1582
1583 cp_declarator *
1584 make_call_declarator (cp_declarator *target,
1585 tree parms,
1586 cp_cv_quals cv_qualifiers,
1587 cp_virt_specifiers virt_specifiers,
1588 cp_ref_qualifier ref_qualifier,
1589 tree tx_qualifier,
1590 tree exception_specification,
1591 tree late_return_type,
1592 tree requires_clause)
1593 {
1594 cp_declarator *declarator;
1595
1596 declarator = make_declarator (cdk_function);
1597 declarator->declarator = target;
1598 declarator->u.function.parameters = parms;
1599 declarator->u.function.qualifiers = cv_qualifiers;
1600 declarator->u.function.virt_specifiers = virt_specifiers;
1601 declarator->u.function.ref_qualifier = ref_qualifier;
1602 declarator->u.function.tx_qualifier = tx_qualifier;
1603 declarator->u.function.exception_specification = exception_specification;
1604 declarator->u.function.late_return_type = late_return_type;
1605 declarator->u.function.requires_clause = requires_clause;
1606 if (target)
1607 {
1608 declarator->id_loc = target->id_loc;
1609 declarator->parameter_pack_p = target->parameter_pack_p;
1610 target->parameter_pack_p = false;
1611 }
1612 else
1613 declarator->parameter_pack_p = false;
1614
1615 return declarator;
1616 }
1617
1618 /* Make a declarator for an array of BOUNDS elements, each of which is
1619 defined by ELEMENT. */
1620
1621 cp_declarator *
1622 make_array_declarator (cp_declarator *element, tree bounds)
1623 {
1624 cp_declarator *declarator;
1625
1626 declarator = make_declarator (cdk_array);
1627 declarator->declarator = element;
1628 declarator->u.array.bounds = bounds;
1629 if (element)
1630 {
1631 declarator->id_loc = element->id_loc;
1632 declarator->parameter_pack_p = element->parameter_pack_p;
1633 element->parameter_pack_p = false;
1634 }
1635 else
1636 declarator->parameter_pack_p = false;
1637
1638 return declarator;
1639 }
1640
1641 /* Determine whether the declarator we've seen so far can be a
1642 parameter pack, when followed by an ellipsis. */
1643 static bool
1644 declarator_can_be_parameter_pack (cp_declarator *declarator)
1645 {
1646 if (declarator && declarator->parameter_pack_p)
1647 /* We already saw an ellipsis. */
1648 return false;
1649
1650 /* Search for a declarator name, or any other declarator that goes
1651 after the point where the ellipsis could appear in a parameter
1652 pack. If we find any of these, then this declarator cannot be
1653 made into a parameter pack. */
1654 bool found = false;
1655 while (declarator && !found)
1656 {
1657 switch ((int)declarator->kind)
1658 {
1659 case cdk_id:
1660 case cdk_array:
1661 case cdk_decomp:
1662 found = true;
1663 break;
1664
1665 case cdk_error:
1666 return true;
1667
1668 default:
1669 declarator = declarator->declarator;
1670 break;
1671 }
1672 }
1673
1674 return !found;
1675 }
1676
1677 cp_parameter_declarator *no_parameters;
1678
1679 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1680 DECLARATOR and DEFAULT_ARGUMENT. */
1681
1682 cp_parameter_declarator *
1683 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1684 cp_declarator *declarator,
1685 tree default_argument,
1686 location_t loc,
1687 bool template_parameter_pack_p = false)
1688 {
1689 cp_parameter_declarator *parameter;
1690
1691 parameter = ((cp_parameter_declarator *)
1692 alloc_declarator (sizeof (cp_parameter_declarator)));
1693 parameter->next = NULL;
1694 if (decl_specifiers)
1695 parameter->decl_specifiers = *decl_specifiers;
1696 else
1697 clear_decl_specs (&parameter->decl_specifiers);
1698 parameter->declarator = declarator;
1699 parameter->default_argument = default_argument;
1700 parameter->template_parameter_pack_p = template_parameter_pack_p;
1701 parameter->loc = loc;
1702
1703 return parameter;
1704 }
1705
1706 /* Returns true iff DECLARATOR is a declaration for a function. */
1707
1708 static bool
1709 function_declarator_p (const cp_declarator *declarator)
1710 {
1711 while (declarator)
1712 {
1713 if (declarator->kind == cdk_function
1714 && declarator->declarator->kind == cdk_id)
1715 return true;
1716 if (declarator->kind == cdk_id
1717 || declarator->kind == cdk_decomp
1718 || declarator->kind == cdk_error)
1719 return false;
1720 declarator = declarator->declarator;
1721 }
1722 return false;
1723 }
1724
1725 /* The parser. */
1726
1727 /* Overview
1728 --------
1729
1730 A cp_parser parses the token stream as specified by the C++
1731 grammar. Its job is purely parsing, not semantic analysis. For
1732 example, the parser breaks the token stream into declarators,
1733 expressions, statements, and other similar syntactic constructs.
1734 It does not check that the types of the expressions on either side
1735 of an assignment-statement are compatible, or that a function is
1736 not declared with a parameter of type `void'.
1737
1738 The parser invokes routines elsewhere in the compiler to perform
1739 semantic analysis and to build up the abstract syntax tree for the
1740 code processed.
1741
1742 The parser (and the template instantiation code, which is, in a
1743 way, a close relative of parsing) are the only parts of the
1744 compiler that should be calling push_scope and pop_scope, or
1745 related functions. The parser (and template instantiation code)
1746 keeps track of what scope is presently active; everything else
1747 should simply honor that. (The code that generates static
1748 initializers may also need to set the scope, in order to check
1749 access control correctly when emitting the initializers.)
1750
1751 Methodology
1752 -----------
1753
1754 The parser is of the standard recursive-descent variety. Upcoming
1755 tokens in the token stream are examined in order to determine which
1756 production to use when parsing a non-terminal. Some C++ constructs
1757 require arbitrary look ahead to disambiguate. For example, it is
1758 impossible, in the general case, to tell whether a statement is an
1759 expression or declaration without scanning the entire statement.
1760 Therefore, the parser is capable of "parsing tentatively." When the
1761 parser is not sure what construct comes next, it enters this mode.
1762 Then, while we attempt to parse the construct, the parser queues up
1763 error messages, rather than issuing them immediately, and saves the
1764 tokens it consumes. If the construct is parsed successfully, the
1765 parser "commits", i.e., it issues any queued error messages and
1766 the tokens that were being preserved are permanently discarded.
1767 If, however, the construct is not parsed successfully, the parser
1768 rolls back its state completely so that it can resume parsing using
1769 a different alternative.
1770
1771 Future Improvements
1772 -------------------
1773
1774 The performance of the parser could probably be improved substantially.
1775 We could often eliminate the need to parse tentatively by looking ahead
1776 a little bit. In some places, this approach might not entirely eliminate
1777 the need to parse tentatively, but it might still speed up the average
1778 case. */
1779
1780 /* Flags that are passed to some parsing functions. These values can
1781 be bitwise-ored together. */
1782
1783 enum
1784 {
1785 /* No flags. */
1786 CP_PARSER_FLAGS_NONE = 0x0,
1787 /* The construct is optional. If it is not present, then no error
1788 should be issued. */
1789 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1790 /* When parsing a type-specifier, treat user-defined type-names
1791 as non-type identifiers. */
1792 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1793 /* When parsing a type-specifier, do not try to parse a class-specifier
1794 or enum-specifier. */
1795 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1796 /* When parsing a decl-specifier-seq, only allow type-specifier or
1797 constexpr. */
1798 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1799 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1800 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1801 /* When parsing a decl-specifier-seq, allow missing typename. */
1802 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20
1803 };
1804
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags;
1808
1809 /* The different kinds of declarators we want to parse. */
1810
1811 enum cp_parser_declarator_kind
1812 {
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1819 };
1820
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1824
1825 enum cp_parser_prec
1826 {
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1840 };
1841
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1844
1845 struct cp_parser_binary_operations_map_node
1846 {
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1853 };
1854
1855 struct cp_parser_expression_stack_entry
1856 {
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1869 };
1870
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1876
1877 /* Prototypes. */
1878
1879 /* Constructors and destructors. */
1880
1881 static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1883
1884 /* Class variables. */
1885
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1887
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1891
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1895
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1899
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1902
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1905
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1910
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1913
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1915
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1917
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1919
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1921
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1923 };
1924
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1929
1930 /* Constructors and destructors. */
1931
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1934
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1937 {
1938 cp_parser_context *context;
1939
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1942 {
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1947 }
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1950
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1956 {
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1962 }
1963
1964 return context;
1965 }
1966
1967 /* Managing the unparsed function queues. */
1968
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1977
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1980 {
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1983 }
1984
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1987 {
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1990 }
1991
1992 /* Prototypes. */
1993
1994 /* Constructors and destructors. */
1995
1996 static cp_parser *cp_parser_new
1997 (void);
1998
1999 /* Routines to parse various constructs.
2000
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2010
2011 /* Lexical conventions [gram.lex] */
2012
2013 static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020 (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2023
2024 /* Basic concepts [gram.basic] */
2025
2026 static void cp_parser_translation_unit (cp_parser *);
2027
2028 /* Expressions [gram.expr] */
2029
2030 static cp_expr cp_parser_primary_expression
2031 (cp_parser *, bool, bool, bool, cp_id_kind *);
2032 static cp_expr cp_parser_id_expression
2033 (cp_parser *, bool, bool, bool *, bool, bool);
2034 static cp_expr cp_parser_unqualified_id
2035 (cp_parser *, bool, bool, bool, bool);
2036 static tree cp_parser_nested_name_specifier_opt
2037 (cp_parser *, bool, bool, bool, bool, bool = false);
2038 static tree cp_parser_nested_name_specifier
2039 (cp_parser *, bool, bool, bool, bool);
2040 static tree cp_parser_qualifying_entity
2041 (cp_parser *, bool, bool, bool, bool, bool);
2042 static cp_expr cp_parser_postfix_expression
2043 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2044 static tree cp_parser_postfix_open_square_expression
2045 (cp_parser *, tree, bool, bool);
2046 static tree cp_parser_postfix_dot_deref_expression
2047 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2048 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2049 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2050 bool = false);
2051 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2052 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2053 static void cp_parser_pseudo_destructor_name
2054 (cp_parser *, tree, tree *, tree *);
2055 static cp_expr cp_parser_unary_expression
2056 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2057 static enum tree_code cp_parser_unary_operator
2058 (cp_token *);
2059 static tree cp_parser_has_attribute_expression
2060 (cp_parser *);
2061 static tree cp_parser_new_expression
2062 (cp_parser *);
2063 static vec<tree, va_gc> *cp_parser_new_placement
2064 (cp_parser *);
2065 static tree cp_parser_new_type_id
2066 (cp_parser *, tree *);
2067 static cp_declarator *cp_parser_new_declarator_opt
2068 (cp_parser *);
2069 static cp_declarator *cp_parser_direct_new_declarator
2070 (cp_parser *);
2071 static vec<tree, va_gc> *cp_parser_new_initializer
2072 (cp_parser *);
2073 static tree cp_parser_delete_expression
2074 (cp_parser *);
2075 static cp_expr cp_parser_cast_expression
2076 (cp_parser *, bool, bool, bool, cp_id_kind *);
2077 static cp_expr cp_parser_binary_expression
2078 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2079 static tree cp_parser_question_colon_clause
2080 (cp_parser *, cp_expr);
2081 static cp_expr cp_parser_assignment_expression
2082 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2083 static enum tree_code cp_parser_assignment_operator_opt
2084 (cp_parser *);
2085 static cp_expr cp_parser_expression
2086 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2087 static cp_expr cp_parser_constant_expression
2088 (cp_parser *, bool = false, bool * = NULL, bool = false);
2089 static cp_expr cp_parser_builtin_offsetof
2090 (cp_parser *);
2091 static cp_expr cp_parser_lambda_expression
2092 (cp_parser *);
2093 static void cp_parser_lambda_introducer
2094 (cp_parser *, tree);
2095 static bool cp_parser_lambda_declarator_opt
2096 (cp_parser *, tree);
2097 static void cp_parser_lambda_body
2098 (cp_parser *, tree);
2099
2100 /* Statements [gram.stmt.stmt] */
2101
2102 static void cp_parser_statement
2103 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2104 static void cp_parser_label_for_labeled_statement
2105 (cp_parser *, tree);
2106 static tree cp_parser_expression_statement
2107 (cp_parser *, tree);
2108 static tree cp_parser_compound_statement
2109 (cp_parser *, tree, int, bool);
2110 static void cp_parser_statement_seq_opt
2111 (cp_parser *, tree);
2112 static tree cp_parser_selection_statement
2113 (cp_parser *, bool *, vec<tree> *);
2114 static tree cp_parser_condition
2115 (cp_parser *);
2116 static tree cp_parser_iteration_statement
2117 (cp_parser *, bool *, bool, unsigned short);
2118 static bool cp_parser_init_statement
2119 (cp_parser *, tree *decl);
2120 static tree cp_parser_for
2121 (cp_parser *, bool, unsigned short);
2122 static tree cp_parser_c_for
2123 (cp_parser *, tree, tree, bool, unsigned short);
2124 static tree cp_parser_range_for
2125 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2126 static void do_range_for_auto_deduction
2127 (tree, tree);
2128 static tree cp_parser_perform_range_for_lookup
2129 (tree, tree *, tree *);
2130 static tree cp_parser_range_for_member_function
2131 (tree, tree);
2132 static tree cp_parser_jump_statement
2133 (cp_parser *);
2134 static void cp_parser_declaration_statement
2135 (cp_parser *);
2136
2137 static tree cp_parser_implicitly_scoped_statement
2138 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2139 static void cp_parser_already_scoped_statement
2140 (cp_parser *, bool *, const token_indent_info &);
2141
2142 /* Declarations [gram.dcl.dcl] */
2143
2144 static void cp_parser_declaration_seq_opt
2145 (cp_parser *);
2146 static void cp_parser_declaration
2147 (cp_parser *);
2148 static void cp_parser_toplevel_declaration
2149 (cp_parser *);
2150 static void cp_parser_block_declaration
2151 (cp_parser *, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser *, bool, tree *);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2157 (cp_parser *);
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser *, cp_decl_specifier_seq *);
2160 static tree cp_parser_type_specifier
2161 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162 int *, bool *);
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165 static tree cp_parser_type_name
2166 (cp_parser *, bool);
2167 static tree cp_parser_nonclass_name
2168 (cp_parser* parser);
2169 static tree cp_parser_elaborated_type_specifier
2170 (cp_parser *, bool, bool);
2171 static tree cp_parser_enum_specifier
2172 (cp_parser *);
2173 static void cp_parser_enumerator_list
2174 (cp_parser *, tree);
2175 static void cp_parser_enumerator_definition
2176 (cp_parser *, tree);
2177 static tree cp_parser_namespace_name
2178 (cp_parser *);
2179 static void cp_parser_namespace_definition
2180 (cp_parser *);
2181 static void cp_parser_namespace_body
2182 (cp_parser *);
2183 static tree cp_parser_qualified_namespace_specifier
2184 (cp_parser *);
2185 static void cp_parser_namespace_alias_definition
2186 (cp_parser *);
2187 static bool cp_parser_using_declaration
2188 (cp_parser *, bool);
2189 static void cp_parser_using_directive
2190 (cp_parser *);
2191 static tree cp_parser_alias_declaration
2192 (cp_parser *);
2193 static void cp_parser_asm_definition
2194 (cp_parser *);
2195 static void cp_parser_linkage_specification
2196 (cp_parser *);
2197 static void cp_parser_static_assert
2198 (cp_parser *, bool);
2199 static tree cp_parser_decltype
2200 (cp_parser *);
2201 static tree cp_parser_decomposition_declaration
2202 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2203
2204 /* Declarators [gram.dcl.decl] */
2205
2206 static tree cp_parser_init_declarator
2207 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2208 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2209 location_t *, tree *);
2210 static cp_declarator *cp_parser_declarator
2211 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2212 bool, bool, bool);
2213 static cp_declarator *cp_parser_direct_declarator
2214 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2215 bool);
2216 static enum tree_code cp_parser_ptr_operator
2217 (cp_parser *, tree *, cp_cv_quals *, tree *);
2218 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2219 (cp_parser *);
2220 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2221 (cp_parser *);
2222 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2223 (cp_parser *);
2224 static tree cp_parser_tx_qualifier_opt
2225 (cp_parser *);
2226 static tree cp_parser_late_return_type_opt
2227 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2228 static tree cp_parser_declarator_id
2229 (cp_parser *, bool);
2230 static tree cp_parser_type_id
2231 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2232 static tree cp_parser_template_type_arg
2233 (cp_parser *);
2234 static tree cp_parser_trailing_type_id (cp_parser *);
2235 static tree cp_parser_type_id_1
2236 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2237 static void cp_parser_type_specifier_seq
2238 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2239 static tree cp_parser_parameter_declaration_clause
2240 (cp_parser *, cp_parser_flags);
2241 static tree cp_parser_parameter_declaration_list
2242 (cp_parser *, cp_parser_flags);
2243 static cp_parameter_declarator *cp_parser_parameter_declaration
2244 (cp_parser *, cp_parser_flags, bool, bool *);
2245 static tree cp_parser_default_argument
2246 (cp_parser *, bool);
2247 static void cp_parser_function_body
2248 (cp_parser *, bool);
2249 static tree cp_parser_initializer
2250 (cp_parser *, bool *, bool *, bool = false);
2251 static cp_expr cp_parser_initializer_clause
2252 (cp_parser *, bool *);
2253 static cp_expr cp_parser_braced_list
2254 (cp_parser*, bool*);
2255 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2256 (cp_parser *, bool *, bool *);
2257
2258 static void cp_parser_ctor_initializer_opt_and_function_body
2259 (cp_parser *, bool);
2260
2261 static tree cp_parser_late_parsing_omp_declare_simd
2262 (cp_parser *, tree);
2263
2264 static tree cp_parser_late_parsing_oacc_routine
2265 (cp_parser *, tree);
2266
2267 static tree synthesize_implicit_template_parm
2268 (cp_parser *, tree);
2269 static tree finish_fully_implicit_template
2270 (cp_parser *, tree);
2271 static void abort_fully_implicit_template
2272 (cp_parser *);
2273
2274 /* Classes [gram.class] */
2275
2276 static tree cp_parser_class_name
2277 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2278 static tree cp_parser_class_specifier
2279 (cp_parser *);
2280 static tree cp_parser_class_head
2281 (cp_parser *, bool *);
2282 static enum tag_types cp_parser_class_key
2283 (cp_parser *);
2284 static void cp_parser_type_parameter_key
2285 (cp_parser* parser);
2286 static void cp_parser_member_specification_opt
2287 (cp_parser *);
2288 static void cp_parser_member_declaration
2289 (cp_parser *);
2290 static tree cp_parser_pure_specifier
2291 (cp_parser *);
2292 static tree cp_parser_constant_initializer
2293 (cp_parser *);
2294
2295 /* Derived classes [gram.class.derived] */
2296
2297 static tree cp_parser_base_clause
2298 (cp_parser *);
2299 static tree cp_parser_base_specifier
2300 (cp_parser *);
2301
2302 /* Special member functions [gram.special] */
2303
2304 static tree cp_parser_conversion_function_id
2305 (cp_parser *);
2306 static tree cp_parser_conversion_type_id
2307 (cp_parser *);
2308 static cp_declarator *cp_parser_conversion_declarator_opt
2309 (cp_parser *);
2310 static void cp_parser_ctor_initializer_opt
2311 (cp_parser *);
2312 static void cp_parser_mem_initializer_list
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer
2315 (cp_parser *);
2316 static tree cp_parser_mem_initializer_id
2317 (cp_parser *);
2318
2319 /* Overloading [gram.over] */
2320
2321 static cp_expr cp_parser_operator_function_id
2322 (cp_parser *);
2323 static cp_expr cp_parser_operator
2324 (cp_parser *, location_t);
2325
2326 /* Templates [gram.temp] */
2327
2328 static void cp_parser_template_declaration
2329 (cp_parser *, bool);
2330 static tree cp_parser_template_parameter_list
2331 (cp_parser *);
2332 static tree cp_parser_template_parameter
2333 (cp_parser *, bool *, bool *);
2334 static tree cp_parser_type_parameter
2335 (cp_parser *, bool *);
2336 static tree cp_parser_template_id
2337 (cp_parser *, bool, bool, enum tag_types, bool);
2338 static tree cp_parser_template_name
2339 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2340 static tree cp_parser_template_argument_list
2341 (cp_parser *);
2342 static tree cp_parser_template_argument
2343 (cp_parser *);
2344 static void cp_parser_explicit_instantiation
2345 (cp_parser *);
2346 static void cp_parser_explicit_specialization
2347 (cp_parser *);
2348
2349 /* Exception handling [gram.exception] */
2350
2351 static tree cp_parser_try_block
2352 (cp_parser *);
2353 static void cp_parser_function_try_block
2354 (cp_parser *);
2355 static void cp_parser_handler_seq
2356 (cp_parser *);
2357 static void cp_parser_handler
2358 (cp_parser *);
2359 static tree cp_parser_exception_declaration
2360 (cp_parser *);
2361 static tree cp_parser_throw_expression
2362 (cp_parser *);
2363 static tree cp_parser_exception_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_type_id_list
2366 (cp_parser *);
2367
2368 /* GNU Extensions */
2369
2370 static tree cp_parser_asm_specification_opt
2371 (cp_parser *);
2372 static tree cp_parser_asm_operand_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_clobber_list
2375 (cp_parser *);
2376 static tree cp_parser_asm_label_list
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_gnu_attribute_p
2381 (cp_parser *);
2382 static bool cp_next_tokens_can_be_std_attribute_p
2383 (cp_parser *);
2384 static bool cp_nth_tokens_can_be_std_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_gnu_attribute_p
2387 (cp_parser *, size_t);
2388 static bool cp_nth_tokens_can_be_attribute_p
2389 (cp_parser *, size_t);
2390 static tree cp_parser_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attributes_opt
2393 (cp_parser *);
2394 static tree cp_parser_gnu_attribute_list
2395 (cp_parser *, bool = false);
2396 static tree cp_parser_std_attribute
2397 (cp_parser *, tree);
2398 static tree cp_parser_std_attribute_spec
2399 (cp_parser *);
2400 static tree cp_parser_std_attribute_spec_seq
2401 (cp_parser *);
2402 static size_t cp_parser_skip_attributes_opt
2403 (cp_parser *, size_t);
2404 static bool cp_parser_extension_opt
2405 (cp_parser *, int *);
2406 static void cp_parser_label_declaration
2407 (cp_parser *);
2408
2409 /* Concept Extensions */
2410
2411 static tree cp_parser_requires_clause
2412 (cp_parser *);
2413 static tree cp_parser_requires_clause_opt
2414 (cp_parser *);
2415 static tree cp_parser_requires_expression
2416 (cp_parser *);
2417 static tree cp_parser_requirement_parameter_list
2418 (cp_parser *);
2419 static tree cp_parser_requirement_body
2420 (cp_parser *);
2421 static tree cp_parser_requirement_list
2422 (cp_parser *);
2423 static tree cp_parser_requirement
2424 (cp_parser *);
2425 static tree cp_parser_simple_requirement
2426 (cp_parser *);
2427 static tree cp_parser_compound_requirement
2428 (cp_parser *);
2429 static tree cp_parser_type_requirement
2430 (cp_parser *);
2431 static tree cp_parser_nested_requirement
2432 (cp_parser *);
2433
2434 /* Transactional Memory Extensions */
2435
2436 static tree cp_parser_transaction
2437 (cp_parser *, cp_token *);
2438 static tree cp_parser_transaction_expression
2439 (cp_parser *, enum rid);
2440 static void cp_parser_function_transaction
2441 (cp_parser *, enum rid);
2442 static tree cp_parser_transaction_cancel
2443 (cp_parser *);
2444
2445 enum pragma_context {
2446 pragma_external,
2447 pragma_member,
2448 pragma_objc_icode,
2449 pragma_stmt,
2450 pragma_compound
2451 };
2452 static bool cp_parser_pragma
2453 (cp_parser *, enum pragma_context, bool *);
2454
2455 /* Objective-C++ Productions */
2456
2457 static tree cp_parser_objc_message_receiver
2458 (cp_parser *);
2459 static tree cp_parser_objc_message_args
2460 (cp_parser *);
2461 static tree cp_parser_objc_message_expression
2462 (cp_parser *);
2463 static cp_expr cp_parser_objc_encode_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_defs_expression
2466 (cp_parser *);
2467 static tree cp_parser_objc_protocol_expression
2468 (cp_parser *);
2469 static tree cp_parser_objc_selector_expression
2470 (cp_parser *);
2471 static cp_expr cp_parser_objc_expression
2472 (cp_parser *);
2473 static bool cp_parser_objc_selector_p
2474 (enum cpp_ttype);
2475 static tree cp_parser_objc_selector
2476 (cp_parser *);
2477 static tree cp_parser_objc_protocol_refs_opt
2478 (cp_parser *);
2479 static void cp_parser_objc_declaration
2480 (cp_parser *, tree);
2481 static tree cp_parser_objc_statement
2482 (cp_parser *);
2483 static bool cp_parser_objc_valid_prefix_attributes
2484 (cp_parser *, tree *);
2485 static void cp_parser_objc_at_property_declaration
2486 (cp_parser *) ;
2487 static void cp_parser_objc_at_synthesize_declaration
2488 (cp_parser *) ;
2489 static void cp_parser_objc_at_dynamic_declaration
2490 (cp_parser *) ;
2491 static tree cp_parser_objc_struct_declaration
2492 (cp_parser *) ;
2493
2494 /* Utility Routines */
2495
2496 static cp_expr cp_parser_lookup_name
2497 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2498 static tree cp_parser_lookup_name_simple
2499 (cp_parser *, tree, location_t);
2500 static tree cp_parser_maybe_treat_template_as_class
2501 (tree, bool);
2502 static bool cp_parser_check_declarator_template_parameters
2503 (cp_parser *, cp_declarator *, location_t);
2504 static bool cp_parser_check_template_parameters
2505 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2506 static cp_expr cp_parser_simple_cast_expression
2507 (cp_parser *);
2508 static tree cp_parser_global_scope_opt
2509 (cp_parser *, bool);
2510 static bool cp_parser_constructor_declarator_p
2511 (cp_parser *, cp_parser_flags, bool);
2512 static tree cp_parser_function_definition_from_specifiers_and_declarator
2513 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2514 static tree cp_parser_function_definition_after_declarator
2515 (cp_parser *, bool);
2516 static bool cp_parser_template_declaration_after_export
2517 (cp_parser *, bool);
2518 static void cp_parser_perform_template_parameter_access_checks
2519 (vec<deferred_access_check, va_gc> *);
2520 static tree cp_parser_single_declaration
2521 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2522 static cp_expr cp_parser_functional_cast
2523 (cp_parser *, tree);
2524 static tree cp_parser_save_member_function_body
2525 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2526 static tree cp_parser_save_nsdmi
2527 (cp_parser *);
2528 static tree cp_parser_enclosed_template_argument_list
2529 (cp_parser *);
2530 static void cp_parser_save_default_args
2531 (cp_parser *, tree);
2532 static void cp_parser_late_parsing_for_member
2533 (cp_parser *, tree);
2534 static tree cp_parser_late_parse_one_default_arg
2535 (cp_parser *, tree, tree, tree);
2536 static void cp_parser_late_parsing_nsdmi
2537 (cp_parser *, tree);
2538 static void cp_parser_late_parsing_default_args
2539 (cp_parser *, tree);
2540 static tree cp_parser_sizeof_operand
2541 (cp_parser *, enum rid);
2542 static cp_expr cp_parser_trait_expr
2543 (cp_parser *, enum rid);
2544 static bool cp_parser_declares_only_class_p
2545 (cp_parser *);
2546 static void cp_parser_set_storage_class
2547 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2548 static void cp_parser_set_decl_spec_type
2549 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2550 static void set_and_check_decl_spec_loc
2551 (cp_decl_specifier_seq *decl_specs,
2552 cp_decl_spec ds, cp_token *);
2553 static bool cp_parser_friend_p
2554 (const cp_decl_specifier_seq *);
2555 static void cp_parser_required_error
2556 (cp_parser *, required_token, bool, location_t);
2557 static cp_token *cp_parser_require
2558 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2559 static cp_token *cp_parser_require_keyword
2560 (cp_parser *, enum rid, required_token);
2561 static bool cp_parser_token_starts_function_definition_p
2562 (cp_token *);
2563 static bool cp_parser_next_token_starts_class_definition_p
2564 (cp_parser *);
2565 static bool cp_parser_next_token_ends_template_argument_p
2566 (cp_parser *);
2567 static bool cp_parser_nth_token_starts_template_argument_list_p
2568 (cp_parser *, size_t);
2569 static enum tag_types cp_parser_token_is_class_key
2570 (cp_token *);
2571 static enum tag_types cp_parser_token_is_type_parameter_key
2572 (cp_token *);
2573 static void cp_parser_check_class_key
2574 (enum tag_types, tree type);
2575 static void cp_parser_check_access_in_redeclaration
2576 (tree type, location_t location);
2577 static bool cp_parser_optional_template_keyword
2578 (cp_parser *);
2579 static void cp_parser_pre_parsed_nested_name_specifier
2580 (cp_parser *);
2581 static bool cp_parser_cache_group
2582 (cp_parser *, enum cpp_ttype, unsigned);
2583 static tree cp_parser_cache_defarg
2584 (cp_parser *parser, bool nsdmi);
2585 static void cp_parser_parse_tentatively
2586 (cp_parser *);
2587 static void cp_parser_commit_to_tentative_parse
2588 (cp_parser *);
2589 static void cp_parser_commit_to_topmost_tentative_parse
2590 (cp_parser *);
2591 static void cp_parser_abort_tentative_parse
2592 (cp_parser *);
2593 static bool cp_parser_parse_definitely
2594 (cp_parser *);
2595 static inline bool cp_parser_parsing_tentatively
2596 (cp_parser *);
2597 static bool cp_parser_uncommitted_to_tentative_parse_p
2598 (cp_parser *);
2599 static void cp_parser_error
2600 (cp_parser *, const char *);
2601 static void cp_parser_name_lookup_error
2602 (cp_parser *, tree, tree, name_lookup_error, location_t);
2603 static bool cp_parser_simulate_error
2604 (cp_parser *);
2605 static bool cp_parser_check_type_definition
2606 (cp_parser *);
2607 static void cp_parser_check_for_definition_in_return_type
2608 (cp_declarator *, tree, location_t type_location);
2609 static void cp_parser_check_for_invalid_template_id
2610 (cp_parser *, tree, enum tag_types, location_t location);
2611 static bool cp_parser_non_integral_constant_expression
2612 (cp_parser *, non_integral_constant);
2613 static void cp_parser_diagnose_invalid_type_name
2614 (cp_parser *, tree, location_t);
2615 static bool cp_parser_parse_and_diagnose_invalid_type_name
2616 (cp_parser *);
2617 static int cp_parser_skip_to_closing_parenthesis
2618 (cp_parser *, bool, bool, bool);
2619 static void cp_parser_skip_to_end_of_statement
2620 (cp_parser *);
2621 static void cp_parser_consume_semicolon_at_end_of_statement
2622 (cp_parser *);
2623 static void cp_parser_skip_to_end_of_block_or_statement
2624 (cp_parser *);
2625 static bool cp_parser_skip_to_closing_brace
2626 (cp_parser *);
2627 static void cp_parser_skip_to_end_of_template_parameter_list
2628 (cp_parser *);
2629 static void cp_parser_skip_to_pragma_eol
2630 (cp_parser*, cp_token *);
2631 static bool cp_parser_error_occurred
2632 (cp_parser *);
2633 static bool cp_parser_allow_gnu_extensions_p
2634 (cp_parser *);
2635 static bool cp_parser_is_pure_string_literal
2636 (cp_token *);
2637 static bool cp_parser_is_string_literal
2638 (cp_token *);
2639 static bool cp_parser_is_keyword
2640 (cp_token *, enum rid);
2641 static tree cp_parser_make_typename_type
2642 (cp_parser *, tree, location_t location);
2643 static cp_declarator * cp_parser_make_indirect_declarator
2644 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2645 static bool cp_parser_compound_literal_p
2646 (cp_parser *);
2647 static bool cp_parser_array_designator_p
2648 (cp_parser *);
2649 static bool cp_parser_init_statement_p
2650 (cp_parser *);
2651 static bool cp_parser_skip_to_closing_square_bracket
2652 (cp_parser *);
2653
2654 /* Concept-related syntactic transformations */
2655
2656 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2657 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2658
2659 // -------------------------------------------------------------------------- //
2660 // Unevaluated Operand Guard
2661 //
2662 // Implementation of an RAII helper for unevaluated operand parsing.
2663 cp_unevaluated::cp_unevaluated ()
2664 {
2665 ++cp_unevaluated_operand;
2666 ++c_inhibit_evaluation_warnings;
2667 }
2668
2669 cp_unevaluated::~cp_unevaluated ()
2670 {
2671 --c_inhibit_evaluation_warnings;
2672 --cp_unevaluated_operand;
2673 }
2674
2675 // -------------------------------------------------------------------------- //
2676 // Tentative Parsing
2677
2678 /* Returns nonzero if we are parsing tentatively. */
2679
2680 static inline bool
2681 cp_parser_parsing_tentatively (cp_parser* parser)
2682 {
2683 return parser->context->next != NULL;
2684 }
2685
2686 /* Returns nonzero if TOKEN is a string literal. */
2687
2688 static bool
2689 cp_parser_is_pure_string_literal (cp_token* token)
2690 {
2691 return (token->type == CPP_STRING ||
2692 token->type == CPP_STRING16 ||
2693 token->type == CPP_STRING32 ||
2694 token->type == CPP_WSTRING ||
2695 token->type == CPP_UTF8STRING);
2696 }
2697
2698 /* Returns nonzero if TOKEN is a string literal
2699 of a user-defined string literal. */
2700
2701 static bool
2702 cp_parser_is_string_literal (cp_token* token)
2703 {
2704 return (cp_parser_is_pure_string_literal (token) ||
2705 token->type == CPP_STRING_USERDEF ||
2706 token->type == CPP_STRING16_USERDEF ||
2707 token->type == CPP_STRING32_USERDEF ||
2708 token->type == CPP_WSTRING_USERDEF ||
2709 token->type == CPP_UTF8STRING_USERDEF);
2710 }
2711
2712 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2713
2714 static bool
2715 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2716 {
2717 return token->keyword == keyword;
2718 }
2719
2720 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2721 PRAGMA_NONE. */
2722
2723 static enum pragma_kind
2724 cp_parser_pragma_kind (cp_token *token)
2725 {
2726 if (token->type != CPP_PRAGMA)
2727 return PRAGMA_NONE;
2728 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2729 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2730 }
2731
2732 /* Helper function for cp_parser_error.
2733 Having peeked a token of kind TOK1_KIND that might signify
2734 a conflict marker, peek successor tokens to determine
2735 if we actually do have a conflict marker.
2736 Specifically, we consider a run of 7 '<', '=' or '>' characters
2737 at the start of a line as a conflict marker.
2738 These come through the lexer as three pairs and a single,
2739 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2740 If it returns true, *OUT_LOC is written to with the location/range
2741 of the marker. */
2742
2743 static bool
2744 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2745 location_t *out_loc)
2746 {
2747 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2748 if (token2->type != tok1_kind)
2749 return false;
2750 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2751 if (token3->type != tok1_kind)
2752 return false;
2753 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2754 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2755 return false;
2756
2757 /* It must be at the start of the line. */
2758 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2759 if (LOCATION_COLUMN (start_loc) != 1)
2760 return false;
2761
2762 /* We have a conflict marker. Construct a location of the form:
2763 <<<<<<<
2764 ^~~~~~~
2765 with start == caret, finishing at the end of the marker. */
2766 location_t finish_loc = get_finish (token4->location);
2767 *out_loc = make_location (start_loc, start_loc, finish_loc);
2768
2769 return true;
2770 }
2771
2772 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2773 RT_CLOSE_PAREN. */
2774
2775 static const char *
2776 get_matching_symbol (required_token token_desc)
2777 {
2778 switch (token_desc)
2779 {
2780 default:
2781 gcc_unreachable ();
2782 return "";
2783 case RT_CLOSE_BRACE:
2784 return "{";
2785 case RT_CLOSE_PAREN:
2786 return "(";
2787 }
2788 }
2789
2790 /* Attempt to convert TOKEN_DESC from a required_token to an
2791 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2792
2793 static enum cpp_ttype
2794 get_required_cpp_ttype (required_token token_desc)
2795 {
2796 switch (token_desc)
2797 {
2798 case RT_SEMICOLON:
2799 return CPP_SEMICOLON;
2800 case RT_OPEN_PAREN:
2801 return CPP_OPEN_PAREN;
2802 case RT_CLOSE_BRACE:
2803 return CPP_CLOSE_BRACE;
2804 case RT_OPEN_BRACE:
2805 return CPP_OPEN_BRACE;
2806 case RT_CLOSE_SQUARE:
2807 return CPP_CLOSE_SQUARE;
2808 case RT_OPEN_SQUARE:
2809 return CPP_OPEN_SQUARE;
2810 case RT_COMMA:
2811 return CPP_COMMA;
2812 case RT_COLON:
2813 return CPP_COLON;
2814 case RT_CLOSE_PAREN:
2815 return CPP_CLOSE_PAREN;
2816
2817 default:
2818 /* Use CPP_EOF as a "no completions possible" code. */
2819 return CPP_EOF;
2820 }
2821 }
2822
2823
2824 /* Subroutine of cp_parser_error and cp_parser_required_error.
2825
2826 Issue a diagnostic of the form
2827 FILE:LINE: MESSAGE before TOKEN
2828 where TOKEN is the next token in the input stream. MESSAGE
2829 (specified by the caller) is usually of the form "expected
2830 OTHER-TOKEN".
2831
2832 This bypasses the check for tentative passing, and potentially
2833 adds material needed by cp_parser_required_error.
2834
2835 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2836 suggesting insertion of the missing token.
2837
2838 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2839 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2840 location. */
2841
2842 static void
2843 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2844 required_token missing_token_desc,
2845 location_t matching_location)
2846 {
2847 cp_token *token = cp_lexer_peek_token (parser->lexer);
2848 /* This diagnostic makes more sense if it is tagged to the line
2849 of the token we just peeked at. */
2850 cp_lexer_set_source_position_from_token (token);
2851
2852 if (token->type == CPP_PRAGMA)
2853 {
2854 error_at (token->location,
2855 "%<#pragma%> is not allowed here");
2856 cp_parser_skip_to_pragma_eol (parser, token);
2857 return;
2858 }
2859
2860 /* If this is actually a conflict marker, report it as such. */
2861 if (token->type == CPP_LSHIFT
2862 || token->type == CPP_RSHIFT
2863 || token->type == CPP_EQ_EQ)
2864 {
2865 location_t loc;
2866 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2867 {
2868 error_at (loc, "version control conflict marker in file");
2869 expanded_location token_exploc = expand_location (token->location);
2870 /* Consume tokens until the end of the source line. */
2871 while (1)
2872 {
2873 cp_lexer_consume_token (parser->lexer);
2874 cp_token *next = cp_lexer_peek_token (parser->lexer);
2875 if (next == NULL)
2876 break;
2877 expanded_location next_exploc = expand_location (next->location);
2878 if (next_exploc.file != token_exploc.file)
2879 break;
2880 if (next_exploc.line != token_exploc.line)
2881 break;
2882 }
2883 return;
2884 }
2885 }
2886
2887 gcc_rich_location richloc (input_location);
2888
2889 bool added_matching_location = false;
2890
2891 if (missing_token_desc != RT_NONE)
2892 {
2893 /* Potentially supply a fix-it hint, suggesting to add the
2894 missing token immediately after the *previous* token.
2895 This may move the primary location within richloc. */
2896 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2897 location_t prev_token_loc
2898 = cp_lexer_previous_token (parser->lexer)->location;
2899 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2900
2901 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2902 Attempt to consolidate diagnostics by printing it as a
2903 secondary range within the main diagnostic. */
2904 if (matching_location != UNKNOWN_LOCATION)
2905 added_matching_location
2906 = richloc.add_location_if_nearby (matching_location);
2907 }
2908
2909 /* Actually emit the error. */
2910 c_parse_error (gmsgid,
2911 /* Because c_parser_error does not understand
2912 CPP_KEYWORD, keywords are treated like
2913 identifiers. */
2914 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2915 token->u.value, token->flags, &richloc);
2916
2917 if (missing_token_desc != RT_NONE)
2918 {
2919 /* If we weren't able to consolidate matching_location, then
2920 print it as a secondary diagnostic. */
2921 if (matching_location != UNKNOWN_LOCATION
2922 && !added_matching_location)
2923 inform (matching_location, "to match this %qs",
2924 get_matching_symbol (missing_token_desc));
2925 }
2926 }
2927
2928 /* If not parsing tentatively, issue a diagnostic of the form
2929 FILE:LINE: MESSAGE before TOKEN
2930 where TOKEN is the next token in the input stream. MESSAGE
2931 (specified by the caller) is usually of the form "expected
2932 OTHER-TOKEN". */
2933
2934 static void
2935 cp_parser_error (cp_parser* parser, const char* gmsgid)
2936 {
2937 if (!cp_parser_simulate_error (parser))
2938 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2939 }
2940
2941 /* Issue an error about name-lookup failing. NAME is the
2942 IDENTIFIER_NODE DECL is the result of
2943 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2944 the thing that we hoped to find. */
2945
2946 static void
2947 cp_parser_name_lookup_error (cp_parser* parser,
2948 tree name,
2949 tree decl,
2950 name_lookup_error desired,
2951 location_t location)
2952 {
2953 /* If name lookup completely failed, tell the user that NAME was not
2954 declared. */
2955 if (decl == error_mark_node)
2956 {
2957 if (parser->scope && parser->scope != global_namespace)
2958 error_at (location, "%<%E::%E%> has not been declared",
2959 parser->scope, name);
2960 else if (parser->scope == global_namespace)
2961 error_at (location, "%<::%E%> has not been declared", name);
2962 else if (parser->object_scope
2963 && !CLASS_TYPE_P (parser->object_scope))
2964 error_at (location, "request for member %qE in non-class type %qT",
2965 name, parser->object_scope);
2966 else if (parser->object_scope)
2967 error_at (location, "%<%T::%E%> has not been declared",
2968 parser->object_scope, name);
2969 else
2970 error_at (location, "%qE has not been declared", name);
2971 }
2972 else if (parser->scope && parser->scope != global_namespace)
2973 {
2974 switch (desired)
2975 {
2976 case NLE_TYPE:
2977 error_at (location, "%<%E::%E%> is not a type",
2978 parser->scope, name);
2979 break;
2980 case NLE_CXX98:
2981 error_at (location, "%<%E::%E%> is not a class or namespace",
2982 parser->scope, name);
2983 break;
2984 case NLE_NOT_CXX98:
2985 error_at (location,
2986 "%<%E::%E%> is not a class, namespace, or enumeration",
2987 parser->scope, name);
2988 break;
2989 default:
2990 gcc_unreachable ();
2991
2992 }
2993 }
2994 else if (parser->scope == global_namespace)
2995 {
2996 switch (desired)
2997 {
2998 case NLE_TYPE:
2999 error_at (location, "%<::%E%> is not a type", name);
3000 break;
3001 case NLE_CXX98:
3002 error_at (location, "%<::%E%> is not a class or namespace", name);
3003 break;
3004 case NLE_NOT_CXX98:
3005 error_at (location,
3006 "%<::%E%> is not a class, namespace, or enumeration",
3007 name);
3008 break;
3009 default:
3010 gcc_unreachable ();
3011 }
3012 }
3013 else
3014 {
3015 switch (desired)
3016 {
3017 case NLE_TYPE:
3018 error_at (location, "%qE is not a type", name);
3019 break;
3020 case NLE_CXX98:
3021 error_at (location, "%qE is not a class or namespace", name);
3022 break;
3023 case NLE_NOT_CXX98:
3024 error_at (location,
3025 "%qE is not a class, namespace, or enumeration", name);
3026 break;
3027 default:
3028 gcc_unreachable ();
3029 }
3030 }
3031 }
3032
3033 /* If we are parsing tentatively, remember that an error has occurred
3034 during this tentative parse. Returns true if the error was
3035 simulated; false if a message should be issued by the caller. */
3036
3037 static bool
3038 cp_parser_simulate_error (cp_parser* parser)
3039 {
3040 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3041 {
3042 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3043 return true;
3044 }
3045 return false;
3046 }
3047
3048 /* This function is called when a type is defined. If type
3049 definitions are forbidden at this point, an error message is
3050 issued. */
3051
3052 static bool
3053 cp_parser_check_type_definition (cp_parser* parser)
3054 {
3055 /* If types are forbidden here, issue a message. */
3056 if (parser->type_definition_forbidden_message)
3057 {
3058 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3059 or %qs in the message need to be interpreted. */
3060 error (parser->type_definition_forbidden_message,
3061 parser->type_definition_forbidden_message_arg);
3062 return false;
3063 }
3064 return true;
3065 }
3066
3067 /* This function is called when the DECLARATOR is processed. The TYPE
3068 was a type defined in the decl-specifiers. If it is invalid to
3069 define a type in the decl-specifiers for DECLARATOR, an error is
3070 issued. TYPE_LOCATION is the location of TYPE and is used
3071 for error reporting. */
3072
3073 static void
3074 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3075 tree type, location_t type_location)
3076 {
3077 /* [dcl.fct] forbids type definitions in return types.
3078 Unfortunately, it's not easy to know whether or not we are
3079 processing a return type until after the fact. */
3080 while (declarator
3081 && (declarator->kind == cdk_pointer
3082 || declarator->kind == cdk_reference
3083 || declarator->kind == cdk_ptrmem))
3084 declarator = declarator->declarator;
3085 if (declarator
3086 && declarator->kind == cdk_function)
3087 {
3088 error_at (type_location,
3089 "new types may not be defined in a return type");
3090 inform (type_location,
3091 "(perhaps a semicolon is missing after the definition of %qT)",
3092 type);
3093 }
3094 }
3095
3096 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3097 "<" in any valid C++ program. If the next token is indeed "<",
3098 issue a message warning the user about what appears to be an
3099 invalid attempt to form a template-id. LOCATION is the location
3100 of the type-specifier (TYPE) */
3101
3102 static void
3103 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3104 tree type,
3105 enum tag_types tag_type,
3106 location_t location)
3107 {
3108 cp_token_position start = 0;
3109
3110 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3111 {
3112 if (TREE_CODE (type) == TYPE_DECL)
3113 type = TREE_TYPE (type);
3114 if (TYPE_P (type) && !template_placeholder_p (type))
3115 error_at (location, "%qT is not a template", type);
3116 else if (identifier_p (type))
3117 {
3118 if (tag_type != none_type)
3119 error_at (location, "%qE is not a class template", type);
3120 else
3121 error_at (location, "%qE is not a template", type);
3122 }
3123 else
3124 error_at (location, "invalid template-id");
3125 /* Remember the location of the invalid "<". */
3126 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3127 start = cp_lexer_token_position (parser->lexer, true);
3128 /* Consume the "<". */
3129 cp_lexer_consume_token (parser->lexer);
3130 /* Parse the template arguments. */
3131 cp_parser_enclosed_template_argument_list (parser);
3132 /* Permanently remove the invalid template arguments so that
3133 this error message is not issued again. */
3134 if (start)
3135 cp_lexer_purge_tokens_after (parser->lexer, start);
3136 }
3137 }
3138
3139 /* If parsing an integral constant-expression, issue an error message
3140 about the fact that THING appeared and return true. Otherwise,
3141 return false. In either case, set
3142 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3143
3144 static bool
3145 cp_parser_non_integral_constant_expression (cp_parser *parser,
3146 non_integral_constant thing)
3147 {
3148 parser->non_integral_constant_expression_p = true;
3149 if (parser->integral_constant_expression_p)
3150 {
3151 if (!parser->allow_non_integral_constant_expression_p)
3152 {
3153 const char *msg = NULL;
3154 switch (thing)
3155 {
3156 case NIC_FLOAT:
3157 pedwarn (input_location, OPT_Wpedantic,
3158 "ISO C++ forbids using a floating-point literal "
3159 "in a constant-expression");
3160 return true;
3161 case NIC_CAST:
3162 error ("a cast to a type other than an integral or "
3163 "enumeration type cannot appear in a "
3164 "constant-expression");
3165 return true;
3166 case NIC_TYPEID:
3167 error ("%<typeid%> operator "
3168 "cannot appear in a constant-expression");
3169 return true;
3170 case NIC_NCC:
3171 error ("non-constant compound literals "
3172 "cannot appear in a constant-expression");
3173 return true;
3174 case NIC_FUNC_CALL:
3175 error ("a function call "
3176 "cannot appear in a constant-expression");
3177 return true;
3178 case NIC_INC:
3179 error ("an increment "
3180 "cannot appear in a constant-expression");
3181 return true;
3182 case NIC_DEC:
3183 error ("an decrement "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_ARRAY_REF:
3187 error ("an array reference "
3188 "cannot appear in a constant-expression");
3189 return true;
3190 case NIC_ADDR_LABEL:
3191 error ("the address of a label "
3192 "cannot appear in a constant-expression");
3193 return true;
3194 case NIC_OVERLOADED:
3195 error ("calls to overloaded operators "
3196 "cannot appear in a constant-expression");
3197 return true;
3198 case NIC_ASSIGNMENT:
3199 error ("an assignment cannot appear in a constant-expression");
3200 return true;
3201 case NIC_COMMA:
3202 error ("a comma operator "
3203 "cannot appear in a constant-expression");
3204 return true;
3205 case NIC_CONSTRUCTOR:
3206 error ("a call to a constructor "
3207 "cannot appear in a constant-expression");
3208 return true;
3209 case NIC_TRANSACTION:
3210 error ("a transaction expression "
3211 "cannot appear in a constant-expression");
3212 return true;
3213 case NIC_THIS:
3214 msg = "this";
3215 break;
3216 case NIC_FUNC_NAME:
3217 msg = "__FUNCTION__";
3218 break;
3219 case NIC_PRETTY_FUNC:
3220 msg = "__PRETTY_FUNCTION__";
3221 break;
3222 case NIC_C99_FUNC:
3223 msg = "__func__";
3224 break;
3225 case NIC_VA_ARG:
3226 msg = "va_arg";
3227 break;
3228 case NIC_ARROW:
3229 msg = "->";
3230 break;
3231 case NIC_POINT:
3232 msg = ".";
3233 break;
3234 case NIC_STAR:
3235 msg = "*";
3236 break;
3237 case NIC_ADDR:
3238 msg = "&";
3239 break;
3240 case NIC_PREINCREMENT:
3241 msg = "++";
3242 break;
3243 case NIC_PREDECREMENT:
3244 msg = "--";
3245 break;
3246 case NIC_NEW:
3247 msg = "new";
3248 break;
3249 case NIC_DEL:
3250 msg = "delete";
3251 break;
3252 default:
3253 gcc_unreachable ();
3254 }
3255 if (msg)
3256 error ("%qs cannot appear in a constant-expression", msg);
3257 return true;
3258 }
3259 }
3260 return false;
3261 }
3262
3263 /* Emit a diagnostic for an invalid type name. This function commits
3264 to the current active tentative parse, if any. (Otherwise, the
3265 problematic construct might be encountered again later, resulting
3266 in duplicate error messages.) LOCATION is the location of ID. */
3267
3268 static void
3269 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3270 location_t location)
3271 {
3272 tree decl, ambiguous_decls;
3273 cp_parser_commit_to_tentative_parse (parser);
3274 /* Try to lookup the identifier. */
3275 decl = cp_parser_lookup_name (parser, id, none_type,
3276 /*is_template=*/false,
3277 /*is_namespace=*/false,
3278 /*check_dependency=*/true,
3279 &ambiguous_decls, location);
3280 if (ambiguous_decls)
3281 /* If the lookup was ambiguous, an error will already have
3282 been issued. */
3283 return;
3284 /* If the lookup found a template-name, it means that the user forgot
3285 to specify an argument list. Emit a useful error message. */
3286 if (DECL_TYPE_TEMPLATE_P (decl))
3287 {
3288 auto_diagnostic_group d;
3289 error_at (location,
3290 "invalid use of template-name %qE without an argument list",
3291 decl);
3292 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3293 inform (location, "class template argument deduction is only available "
3294 "with %<-std=c++17%> or %<-std=gnu++17%>");
3295 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3296 }
3297 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3298 error_at (location, "invalid use of destructor %qD as a type", id);
3299 else if (TREE_CODE (decl) == TYPE_DECL)
3300 /* Something like 'unsigned A a;' */
3301 error_at (location, "invalid combination of multiple type-specifiers");
3302 else if (!parser->scope)
3303 {
3304 /* Issue an error message. */
3305 auto_diagnostic_group d;
3306 name_hint hint;
3307 if (TREE_CODE (id) == IDENTIFIER_NODE)
3308 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3309 if (const char *suggestion = hint.suggestion ())
3310 {
3311 gcc_rich_location richloc (location);
3312 richloc.add_fixit_replace (suggestion);
3313 error_at (&richloc,
3314 "%qE does not name a type; did you mean %qs?",
3315 id, suggestion);
3316 }
3317 else
3318 error_at (location, "%qE does not name a type", id);
3319 /* If we're in a template class, it's possible that the user was
3320 referring to a type from a base class. For example:
3321
3322 template <typename T> struct A { typedef T X; };
3323 template <typename T> struct B : public A<T> { X x; };
3324
3325 The user should have said "typename A<T>::X". */
3326 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3327 inform (location, "C++11 %<constexpr%> only available with "
3328 "%<-std=c++11%> or %<-std=gnu++11%>");
3329 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3330 inform (location, "C++11 %<noexcept%> only available with "
3331 "%<-std=c++11%> or %<-std=gnu++11%>");
3332 else if (cxx_dialect < cxx11
3333 && TREE_CODE (id) == IDENTIFIER_NODE
3334 && id_equal (id, "thread_local"))
3335 inform (location, "C++11 %<thread_local%> only available with "
3336 "%<-std=c++11%> or %<-std=gnu++11%>");
3337 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3338 inform (location, "%<concept%> only available with %<-fconcepts%>");
3339 else if (processing_template_decl && current_class_type
3340 && TYPE_BINFO (current_class_type))
3341 {
3342 tree b;
3343
3344 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3345 b;
3346 b = TREE_CHAIN (b))
3347 {
3348 tree base_type = BINFO_TYPE (b);
3349 if (CLASS_TYPE_P (base_type)
3350 && dependent_type_p (base_type))
3351 {
3352 tree field;
3353 /* Go from a particular instantiation of the
3354 template (which will have an empty TYPE_FIELDs),
3355 to the main version. */
3356 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3357 for (field = TYPE_FIELDS (base_type);
3358 field;
3359 field = DECL_CHAIN (field))
3360 if (TREE_CODE (field) == TYPE_DECL
3361 && DECL_NAME (field) == id)
3362 {
3363 inform (location,
3364 "(perhaps %<typename %T::%E%> was intended)",
3365 BINFO_TYPE (b), id);
3366 break;
3367 }
3368 if (field)
3369 break;
3370 }
3371 }
3372 }
3373 }
3374 /* Here we diagnose qualified-ids where the scope is actually correct,
3375 but the identifier does not resolve to a valid type name. */
3376 else if (parser->scope != error_mark_node)
3377 {
3378 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3379 {
3380 auto_diagnostic_group d;
3381 name_hint hint;
3382 if (decl == error_mark_node)
3383 hint = suggest_alternative_in_explicit_scope (location, id,
3384 parser->scope);
3385 const char *suggestion = hint.suggestion ();
3386 gcc_rich_location richloc (location_of (id));
3387 if (suggestion)
3388 richloc.add_fixit_replace (suggestion);
3389 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3390 {
3391 if (suggestion)
3392 error_at (&richloc,
3393 "%qE in namespace %qE does not name a template"
3394 " type; did you mean %qs?",
3395 id, parser->scope, suggestion);
3396 else
3397 error_at (&richloc,
3398 "%qE in namespace %qE does not name a template type",
3399 id, parser->scope);
3400 }
3401 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3402 {
3403 if (suggestion)
3404 error_at (&richloc,
3405 "%qE in namespace %qE does not name a template"
3406 " type; did you mean %qs?",
3407 TREE_OPERAND (id, 0), parser->scope, suggestion);
3408 else
3409 error_at (&richloc,
3410 "%qE in namespace %qE does not name a template"
3411 " type",
3412 TREE_OPERAND (id, 0), parser->scope);
3413 }
3414 else
3415 {
3416 if (suggestion)
3417 error_at (&richloc,
3418 "%qE in namespace %qE does not name a type"
3419 "; did you mean %qs?",
3420 id, parser->scope, suggestion);
3421 else
3422 error_at (&richloc,
3423 "%qE in namespace %qE does not name a type",
3424 id, parser->scope);
3425 }
3426 if (DECL_P (decl))
3427 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3428 }
3429 else if (CLASS_TYPE_P (parser->scope)
3430 && constructor_name_p (id, parser->scope))
3431 {
3432 /* A<T>::A<T>() */
3433 auto_diagnostic_group d;
3434 error_at (location, "%<%T::%E%> names the constructor, not"
3435 " the type", parser->scope, id);
3436 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3437 error_at (location, "and %qT has no template constructors",
3438 parser->scope);
3439 }
3440 else if (TYPE_P (parser->scope)
3441 && dependent_scope_p (parser->scope))
3442 {
3443 gcc_rich_location richloc (location);
3444 richloc.add_fixit_insert_before ("typename ");
3445 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3446 error_at (&richloc,
3447 "need %<typename%> before %<%T::%D::%E%> because "
3448 "%<%T::%D%> is a dependent scope",
3449 TYPE_CONTEXT (parser->scope),
3450 TYPENAME_TYPE_FULLNAME (parser->scope),
3451 id,
3452 TYPE_CONTEXT (parser->scope),
3453 TYPENAME_TYPE_FULLNAME (parser->scope));
3454 else
3455 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3456 "%qT is a dependent scope",
3457 parser->scope, id, parser->scope);
3458 }
3459 else if (TYPE_P (parser->scope))
3460 {
3461 auto_diagnostic_group d;
3462 if (!COMPLETE_TYPE_P (parser->scope))
3463 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3464 parser->scope);
3465 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3466 error_at (location_of (id),
3467 "%qE in %q#T does not name a template type",
3468 id, parser->scope);
3469 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3470 error_at (location_of (id),
3471 "%qE in %q#T does not name a template type",
3472 TREE_OPERAND (id, 0), parser->scope);
3473 else
3474 error_at (location_of (id),
3475 "%qE in %q#T does not name a type",
3476 id, parser->scope);
3477 if (DECL_P (decl))
3478 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3479 }
3480 else
3481 gcc_unreachable ();
3482 }
3483 }
3484
3485 /* Check for a common situation where a type-name should be present,
3486 but is not, and issue a sensible error message. Returns true if an
3487 invalid type-name was detected.
3488
3489 The situation handled by this function are variable declarations of the
3490 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3491 Usually, `ID' should name a type, but if we got here it means that it
3492 does not. We try to emit the best possible error message depending on
3493 how exactly the id-expression looks like. */
3494
3495 static bool
3496 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3497 {
3498 tree id;
3499 cp_token *token = cp_lexer_peek_token (parser->lexer);
3500
3501 /* Avoid duplicate error about ambiguous lookup. */
3502 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3503 {
3504 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3505 if (next->type == CPP_NAME && next->error_reported)
3506 goto out;
3507 }
3508
3509 cp_parser_parse_tentatively (parser);
3510 id = cp_parser_id_expression (parser,
3511 /*template_keyword_p=*/false,
3512 /*check_dependency_p=*/true,
3513 /*template_p=*/NULL,
3514 /*declarator_p=*/false,
3515 /*optional_p=*/false);
3516 /* If the next token is a (, this is a function with no explicit return
3517 type, i.e. constructor, destructor or conversion op. */
3518 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3519 || TREE_CODE (id) == TYPE_DECL)
3520 {
3521 cp_parser_abort_tentative_parse (parser);
3522 return false;
3523 }
3524 if (!cp_parser_parse_definitely (parser))
3525 return false;
3526
3527 /* Emit a diagnostic for the invalid type. */
3528 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3529 out:
3530 /* If we aren't in the middle of a declarator (i.e. in a
3531 parameter-declaration-clause), skip to the end of the declaration;
3532 there's no point in trying to process it. */
3533 if (!parser->in_declarator_p)
3534 cp_parser_skip_to_end_of_block_or_statement (parser);
3535 return true;
3536 }
3537
3538 /* Consume tokens up to, and including, the next non-nested closing `)'.
3539 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3540 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3541 found an unnested token of that type. */
3542
3543 static int
3544 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3545 bool recovering,
3546 cpp_ttype or_ttype,
3547 bool consume_paren)
3548 {
3549 unsigned paren_depth = 0;
3550 unsigned brace_depth = 0;
3551 unsigned square_depth = 0;
3552 unsigned condop_depth = 0;
3553
3554 if (recovering && or_ttype == CPP_EOF
3555 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3556 return 0;
3557
3558 while (true)
3559 {
3560 cp_token * token = cp_lexer_peek_token (parser->lexer);
3561
3562 /* Have we found what we're looking for before the closing paren? */
3563 if (token->type == or_ttype && or_ttype != CPP_EOF
3564 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3565 return -1;
3566
3567 switch (token->type)
3568 {
3569 case CPP_PRAGMA_EOL:
3570 if (!parser->lexer->in_pragma)
3571 break;
3572 /* FALLTHRU */
3573 case CPP_EOF:
3574 /* If we've run out of tokens, then there is no closing `)'. */
3575 return 0;
3576
3577 /* This is good for lambda expression capture-lists. */
3578 case CPP_OPEN_SQUARE:
3579 ++square_depth;
3580 break;
3581 case CPP_CLOSE_SQUARE:
3582 if (!square_depth--)
3583 return 0;
3584 break;
3585
3586 case CPP_SEMICOLON:
3587 /* This matches the processing in skip_to_end_of_statement. */
3588 if (!brace_depth)
3589 return 0;
3590 break;
3591
3592 case CPP_OPEN_BRACE:
3593 ++brace_depth;
3594 break;
3595 case CPP_CLOSE_BRACE:
3596 if (!brace_depth--)
3597 return 0;
3598 break;
3599
3600 case CPP_OPEN_PAREN:
3601 if (!brace_depth)
3602 ++paren_depth;
3603 break;
3604
3605 case CPP_CLOSE_PAREN:
3606 if (!brace_depth && !paren_depth--)
3607 {
3608 if (consume_paren)
3609 cp_lexer_consume_token (parser->lexer);
3610 return 1;
3611 }
3612 break;
3613
3614 case CPP_QUERY:
3615 if (!brace_depth && !paren_depth && !square_depth)
3616 ++condop_depth;
3617 break;
3618
3619 case CPP_COLON:
3620 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3621 condop_depth--;
3622 break;
3623
3624 default:
3625 break;
3626 }
3627
3628 /* Consume the token. */
3629 cp_lexer_consume_token (parser->lexer);
3630 }
3631 }
3632
3633 /* Consume tokens up to, and including, the next non-nested closing `)'.
3634 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3635 are doing error recovery. Returns -1 if OR_COMMA is true and we
3636 found an unnested token of that type. */
3637
3638 static int
3639 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3640 bool recovering,
3641 bool or_comma,
3642 bool consume_paren)
3643 {
3644 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3645 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3646 ttype, consume_paren);
3647 }
3648
3649 /* Consume tokens until we reach the end of the current statement.
3650 Normally, that will be just before consuming a `;'. However, if a
3651 non-nested `}' comes first, then we stop before consuming that. */
3652
3653 static void
3654 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3655 {
3656 unsigned nesting_depth = 0;
3657
3658 /* Unwind generic function template scope if necessary. */
3659 if (parser->fully_implicit_function_template_p)
3660 abort_fully_implicit_template (parser);
3661
3662 while (true)
3663 {
3664 cp_token *token = cp_lexer_peek_token (parser->lexer);
3665
3666 switch (token->type)
3667 {
3668 case CPP_PRAGMA_EOL:
3669 if (!parser->lexer->in_pragma)
3670 break;
3671 /* FALLTHRU */
3672 case CPP_EOF:
3673 /* If we've run out of tokens, stop. */
3674 return;
3675
3676 case CPP_SEMICOLON:
3677 /* If the next token is a `;', we have reached the end of the
3678 statement. */
3679 if (!nesting_depth)
3680 return;
3681 break;
3682
3683 case CPP_CLOSE_BRACE:
3684 /* If this is a non-nested '}', stop before consuming it.
3685 That way, when confronted with something like:
3686
3687 { 3 + }
3688
3689 we stop before consuming the closing '}', even though we
3690 have not yet reached a `;'. */
3691 if (nesting_depth == 0)
3692 return;
3693
3694 /* If it is the closing '}' for a block that we have
3695 scanned, stop -- but only after consuming the token.
3696 That way given:
3697
3698 void f g () { ... }
3699 typedef int I;
3700
3701 we will stop after the body of the erroneously declared
3702 function, but before consuming the following `typedef'
3703 declaration. */
3704 if (--nesting_depth == 0)
3705 {
3706 cp_lexer_consume_token (parser->lexer);
3707 return;
3708 }
3709 break;
3710
3711 case CPP_OPEN_BRACE:
3712 ++nesting_depth;
3713 break;
3714
3715 default:
3716 break;
3717 }
3718
3719 /* Consume the token. */
3720 cp_lexer_consume_token (parser->lexer);
3721 }
3722 }
3723
3724 /* This function is called at the end of a statement or declaration.
3725 If the next token is a semicolon, it is consumed; otherwise, error
3726 recovery is attempted. */
3727
3728 static void
3729 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3730 {
3731 /* Look for the trailing `;'. */
3732 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3733 {
3734 /* If there is additional (erroneous) input, skip to the end of
3735 the statement. */
3736 cp_parser_skip_to_end_of_statement (parser);
3737 /* If the next token is now a `;', consume it. */
3738 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3739 cp_lexer_consume_token (parser->lexer);
3740 }
3741 }
3742
3743 /* Skip tokens until we have consumed an entire block, or until we
3744 have consumed a non-nested `;'. */
3745
3746 static void
3747 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3748 {
3749 int nesting_depth = 0;
3750
3751 /* Unwind generic function template scope if necessary. */
3752 if (parser->fully_implicit_function_template_p)
3753 abort_fully_implicit_template (parser);
3754
3755 while (nesting_depth >= 0)
3756 {
3757 cp_token *token = cp_lexer_peek_token (parser->lexer);
3758
3759 switch (token->type)
3760 {
3761 case CPP_PRAGMA_EOL:
3762 if (!parser->lexer->in_pragma)
3763 break;
3764 /* FALLTHRU */
3765 case CPP_EOF:
3766 /* If we've run out of tokens, stop. */
3767 return;
3768
3769 case CPP_SEMICOLON:
3770 /* Stop if this is an unnested ';'. */
3771 if (!nesting_depth)
3772 nesting_depth = -1;
3773 break;
3774
3775 case CPP_CLOSE_BRACE:
3776 /* Stop if this is an unnested '}', or closes the outermost
3777 nesting level. */
3778 nesting_depth--;
3779 if (nesting_depth < 0)
3780 return;
3781 if (!nesting_depth)
3782 nesting_depth = -1;
3783 break;
3784
3785 case CPP_OPEN_BRACE:
3786 /* Nest. */
3787 nesting_depth++;
3788 break;
3789
3790 default:
3791 break;
3792 }
3793
3794 /* Consume the token. */
3795 cp_lexer_consume_token (parser->lexer);
3796 }
3797 }
3798
3799 /* Skip tokens until a non-nested closing curly brace is the next
3800 token, or there are no more tokens. Return true in the first case,
3801 false otherwise. */
3802
3803 static bool
3804 cp_parser_skip_to_closing_brace (cp_parser *parser)
3805 {
3806 unsigned nesting_depth = 0;
3807
3808 while (true)
3809 {
3810 cp_token *token = cp_lexer_peek_token (parser->lexer);
3811
3812 switch (token->type)
3813 {
3814 case CPP_PRAGMA_EOL:
3815 if (!parser->lexer->in_pragma)
3816 break;
3817 /* FALLTHRU */
3818 case CPP_EOF:
3819 /* If we've run out of tokens, stop. */
3820 return false;
3821
3822 case CPP_CLOSE_BRACE:
3823 /* If the next token is a non-nested `}', then we have reached
3824 the end of the current block. */
3825 if (nesting_depth-- == 0)
3826 return true;
3827 break;
3828
3829 case CPP_OPEN_BRACE:
3830 /* If it the next token is a `{', then we are entering a new
3831 block. Consume the entire block. */
3832 ++nesting_depth;
3833 break;
3834
3835 default:
3836 break;
3837 }
3838
3839 /* Consume the token. */
3840 cp_lexer_consume_token (parser->lexer);
3841 }
3842 }
3843
3844 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3845 parameter is the PRAGMA token, allowing us to purge the entire pragma
3846 sequence. */
3847
3848 static void
3849 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3850 {
3851 cp_token *token;
3852
3853 parser->lexer->in_pragma = false;
3854
3855 do
3856 token = cp_lexer_consume_token (parser->lexer);
3857 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3858
3859 /* Ensure that the pragma is not parsed again. */
3860 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3861 }
3862
3863 /* Require pragma end of line, resyncing with it as necessary. The
3864 arguments are as for cp_parser_skip_to_pragma_eol. */
3865
3866 static void
3867 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3868 {
3869 parser->lexer->in_pragma = false;
3870 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3871 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3872 }
3873
3874 /* This is a simple wrapper around make_typename_type. When the id is
3875 an unresolved identifier node, we can provide a superior diagnostic
3876 using cp_parser_diagnose_invalid_type_name. */
3877
3878 static tree
3879 cp_parser_make_typename_type (cp_parser *parser, tree id,
3880 location_t id_location)
3881 {
3882 tree result;
3883 if (identifier_p (id))
3884 {
3885 result = make_typename_type (parser->scope, id, typename_type,
3886 /*complain=*/tf_none);
3887 if (result == error_mark_node)
3888 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3889 return result;
3890 }
3891 return make_typename_type (parser->scope, id, typename_type, tf_error);
3892 }
3893
3894 /* This is a wrapper around the
3895 make_{pointer,ptrmem,reference}_declarator functions that decides
3896 which one to call based on the CODE and CLASS_TYPE arguments. The
3897 CODE argument should be one of the values returned by
3898 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3899 appertain to the pointer or reference. */
3900
3901 static cp_declarator *
3902 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3903 cp_cv_quals cv_qualifiers,
3904 cp_declarator *target,
3905 tree attributes)
3906 {
3907 if (code == ERROR_MARK || target == cp_error_declarator)
3908 return cp_error_declarator;
3909
3910 if (code == INDIRECT_REF)
3911 if (class_type == NULL_TREE)
3912 return make_pointer_declarator (cv_qualifiers, target, attributes);
3913 else
3914 return make_ptrmem_declarator (cv_qualifiers, class_type,
3915 target, attributes);
3916 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3917 return make_reference_declarator (cv_qualifiers, target,
3918 false, attributes);
3919 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3920 return make_reference_declarator (cv_qualifiers, target,
3921 true, attributes);
3922 gcc_unreachable ();
3923 }
3924
3925 /* Create a new C++ parser. */
3926
3927 static cp_parser *
3928 cp_parser_new (void)
3929 {
3930 cp_parser *parser;
3931 cp_lexer *lexer;
3932 unsigned i;
3933
3934 /* cp_lexer_new_main is called before doing GC allocation because
3935 cp_lexer_new_main might load a PCH file. */
3936 lexer = cp_lexer_new_main ();
3937
3938 /* Initialize the binops_by_token so that we can get the tree
3939 directly from the token. */
3940 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3941 binops_by_token[binops[i].token_type] = binops[i];
3942
3943 parser = ggc_cleared_alloc<cp_parser> ();
3944 parser->lexer = lexer;
3945 parser->context = cp_parser_context_new (NULL);
3946
3947 /* For now, we always accept GNU extensions. */
3948 parser->allow_gnu_extensions_p = 1;
3949
3950 /* The `>' token is a greater-than operator, not the end of a
3951 template-id. */
3952 parser->greater_than_is_operator_p = true;
3953
3954 parser->default_arg_ok_p = true;
3955
3956 /* We are not parsing a constant-expression. */
3957 parser->integral_constant_expression_p = false;
3958 parser->allow_non_integral_constant_expression_p = false;
3959 parser->non_integral_constant_expression_p = false;
3960
3961 /* Local variable names are not forbidden. */
3962 parser->local_variables_forbidden_p = 0;
3963
3964 /* We are not processing an `extern "C"' declaration. */
3965 parser->in_unbraced_linkage_specification_p = false;
3966
3967 /* We are not processing a declarator. */
3968 parser->in_declarator_p = false;
3969
3970 /* We are not processing a template-argument-list. */
3971 parser->in_template_argument_list_p = false;
3972
3973 /* We are not in an iteration statement. */
3974 parser->in_statement = 0;
3975
3976 /* We are not in a switch statement. */
3977 parser->in_switch_statement_p = false;
3978
3979 /* We are not parsing a type-id inside an expression. */
3980 parser->in_type_id_in_expr_p = false;
3981
3982 /* String literals should be translated to the execution character set. */
3983 parser->translate_strings_p = true;
3984
3985 /* We are not parsing a function body. */
3986 parser->in_function_body = false;
3987
3988 /* We can correct until told otherwise. */
3989 parser->colon_corrects_to_scope_p = true;
3990
3991 /* The unparsed function queue is empty. */
3992 push_unparsed_function_queues (parser);
3993
3994 /* There are no classes being defined. */
3995 parser->num_classes_being_defined = 0;
3996
3997 /* No template parameters apply. */
3998 parser->num_template_parameter_lists = 0;
3999
4000 /* Special parsing data structures. */
4001 parser->omp_declare_simd = NULL;
4002 parser->oacc_routine = NULL;
4003
4004 /* Not declaring an implicit function template. */
4005 parser->auto_is_implicit_function_template_parm_p = false;
4006 parser->fully_implicit_function_template_p = false;
4007 parser->implicit_template_parms = 0;
4008 parser->implicit_template_scope = 0;
4009
4010 /* Allow constrained-type-specifiers. */
4011 parser->prevent_constrained_type_specifiers = 0;
4012
4013 /* We haven't yet seen an 'extern "C"'. */
4014 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4015
4016 return parser;
4017 }
4018
4019 /* Create a cp_lexer structure which will emit the tokens in CACHE
4020 and push it onto the parser's lexer stack. This is used for delayed
4021 parsing of in-class method bodies and default arguments, and should
4022 not be confused with tentative parsing. */
4023 static void
4024 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4025 {
4026 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4027 lexer->next = parser->lexer;
4028 parser->lexer = lexer;
4029
4030 /* Move the current source position to that of the first token in the
4031 new lexer. */
4032 cp_lexer_set_source_position_from_token (lexer->next_token);
4033 }
4034
4035 /* Pop the top lexer off the parser stack. This is never used for the
4036 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4037 static void
4038 cp_parser_pop_lexer (cp_parser *parser)
4039 {
4040 cp_lexer *lexer = parser->lexer;
4041 parser->lexer = lexer->next;
4042 cp_lexer_destroy (lexer);
4043
4044 /* Put the current source position back where it was before this
4045 lexer was pushed. */
4046 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4047 }
4048
4049 /* Lexical conventions [gram.lex] */
4050
4051 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4052 identifier. */
4053
4054 static cp_expr
4055 cp_parser_identifier (cp_parser* parser)
4056 {
4057 cp_token *token;
4058
4059 /* Look for the identifier. */
4060 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4061 /* Return the value. */
4062 if (token)
4063 return cp_expr (token->u.value, token->location);
4064 else
4065 return error_mark_node;
4066 }
4067
4068 /* Parse a sequence of adjacent string constants. Returns a
4069 TREE_STRING representing the combined, nul-terminated string
4070 constant. If TRANSLATE is true, translate the string to the
4071 execution character set. If WIDE_OK is true, a wide string is
4072 invalid here.
4073
4074 C++98 [lex.string] says that if a narrow string literal token is
4075 adjacent to a wide string literal token, the behavior is undefined.
4076 However, C99 6.4.5p4 says that this results in a wide string literal.
4077 We follow C99 here, for consistency with the C front end.
4078
4079 This code is largely lifted from lex_string() in c-lex.c.
4080
4081 FUTURE: ObjC++ will need to handle @-strings here. */
4082 static cp_expr
4083 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4084 bool lookup_udlit = true)
4085 {
4086 tree value;
4087 size_t count;
4088 struct obstack str_ob;
4089 struct obstack loc_ob;
4090 cpp_string str, istr, *strs;
4091 cp_token *tok;
4092 enum cpp_ttype type, curr_type;
4093 int have_suffix_p = 0;
4094 tree string_tree;
4095 tree suffix_id = NULL_TREE;
4096 bool curr_tok_is_userdef_p = false;
4097
4098 tok = cp_lexer_peek_token (parser->lexer);
4099 if (!cp_parser_is_string_literal (tok))
4100 {
4101 cp_parser_error (parser, "expected string-literal");
4102 return error_mark_node;
4103 }
4104
4105 location_t loc = tok->location;
4106
4107 if (cpp_userdef_string_p (tok->type))
4108 {
4109 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4110 curr_type = cpp_userdef_string_remove_type (tok->type);
4111 curr_tok_is_userdef_p = true;
4112 }
4113 else
4114 {
4115 string_tree = tok->u.value;
4116 curr_type = tok->type;
4117 }
4118 type = curr_type;
4119
4120 /* Try to avoid the overhead of creating and destroying an obstack
4121 for the common case of just one string. */
4122 if (!cp_parser_is_string_literal
4123 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4124 {
4125 cp_lexer_consume_token (parser->lexer);
4126
4127 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4128 str.len = TREE_STRING_LENGTH (string_tree);
4129 count = 1;
4130
4131 if (curr_tok_is_userdef_p)
4132 {
4133 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4134 have_suffix_p = 1;
4135 curr_type = cpp_userdef_string_remove_type (tok->type);
4136 }
4137 else
4138 curr_type = tok->type;
4139
4140 strs = &str;
4141 }
4142 else
4143 {
4144 location_t last_tok_loc = tok->location;
4145 gcc_obstack_init (&str_ob);
4146 gcc_obstack_init (&loc_ob);
4147 count = 0;
4148
4149 do
4150 {
4151 cp_lexer_consume_token (parser->lexer);
4152 count++;
4153 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4154 str.len = TREE_STRING_LENGTH (string_tree);
4155
4156 if (curr_tok_is_userdef_p)
4157 {
4158 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4159 if (have_suffix_p == 0)
4160 {
4161 suffix_id = curr_suffix_id;
4162 have_suffix_p = 1;
4163 }
4164 else if (have_suffix_p == 1
4165 && curr_suffix_id != suffix_id)
4166 {
4167 error ("inconsistent user-defined literal suffixes"
4168 " %qD and %qD in string literal",
4169 suffix_id, curr_suffix_id);
4170 have_suffix_p = -1;
4171 }
4172 curr_type = cpp_userdef_string_remove_type (tok->type);
4173 }
4174 else
4175 curr_type = tok->type;
4176
4177 if (type != curr_type)
4178 {
4179 if (type == CPP_STRING)
4180 type = curr_type;
4181 else if (curr_type != CPP_STRING)
4182 {
4183 rich_location rich_loc (line_table, tok->location);
4184 rich_loc.add_range (last_tok_loc);
4185 error_at (&rich_loc,
4186 "unsupported non-standard concatenation "
4187 "of string literals");
4188 }
4189 }
4190
4191 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4192 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4193
4194 last_tok_loc = tok->location;
4195
4196 tok = cp_lexer_peek_token (parser->lexer);
4197 if (cpp_userdef_string_p (tok->type))
4198 {
4199 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4200 curr_type = cpp_userdef_string_remove_type (tok->type);
4201 curr_tok_is_userdef_p = true;
4202 }
4203 else
4204 {
4205 string_tree = tok->u.value;
4206 curr_type = tok->type;
4207 curr_tok_is_userdef_p = false;
4208 }
4209 }
4210 while (cp_parser_is_string_literal (tok));
4211
4212 /* A string literal built by concatenation has its caret=start at
4213 the start of the initial string, and its finish at the finish of
4214 the final string literal. */
4215 loc = make_location (loc, loc, get_finish (last_tok_loc));
4216
4217 strs = (cpp_string *) obstack_finish (&str_ob);
4218 }
4219
4220 if (type != CPP_STRING && !wide_ok)
4221 {
4222 cp_parser_error (parser, "a wide string is invalid in this context");
4223 type = CPP_STRING;
4224 }
4225
4226 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4227 (parse_in, strs, count, &istr, type))
4228 {
4229 value = build_string (istr.len, (const char *)istr.text);
4230 free (CONST_CAST (unsigned char *, istr.text));
4231 if (count > 1)
4232 {
4233 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4234 gcc_assert (g_string_concat_db);
4235 g_string_concat_db->record_string_concatenation (count, locs);
4236 }
4237
4238 switch (type)
4239 {
4240 default:
4241 case CPP_STRING:
4242 TREE_TYPE (value) = char_array_type_node;
4243 break;
4244 case CPP_UTF8STRING:
4245 if (flag_char8_t)
4246 TREE_TYPE (value) = char8_array_type_node;
4247 else
4248 TREE_TYPE (value) = char_array_type_node;
4249 break;
4250 case CPP_STRING16:
4251 TREE_TYPE (value) = char16_array_type_node;
4252 break;
4253 case CPP_STRING32:
4254 TREE_TYPE (value) = char32_array_type_node;
4255 break;
4256 case CPP_WSTRING:
4257 TREE_TYPE (value) = wchar_array_type_node;
4258 break;
4259 }
4260
4261 value = fix_string_type (value);
4262
4263 if (have_suffix_p)
4264 {
4265 tree literal = build_userdef_literal (suffix_id, value,
4266 OT_NONE, NULL_TREE);
4267 if (lookup_udlit)
4268 value = cp_parser_userdef_string_literal (literal);
4269 else
4270 value = literal;
4271 }
4272 }
4273 else
4274 /* cpp_interpret_string has issued an error. */
4275 value = error_mark_node;
4276
4277 if (count > 1)
4278 {
4279 obstack_free (&str_ob, 0);
4280 obstack_free (&loc_ob, 0);
4281 }
4282
4283 return cp_expr (value, loc);
4284 }
4285
4286 /* Look up a literal operator with the name and the exact arguments. */
4287
4288 static tree
4289 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4290 {
4291 tree decl = lookup_name (name);
4292 if (!decl || !is_overloaded_fn (decl))
4293 return error_mark_node;
4294
4295 for (lkp_iterator iter (decl); iter; ++iter)
4296 {
4297 tree fn = *iter;
4298
4299 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4300 {
4301 unsigned int ix;
4302 bool found = true;
4303
4304 for (ix = 0;
4305 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4306 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4307 {
4308 tree tparm = TREE_VALUE (parmtypes);
4309 tree targ = TREE_TYPE ((*args)[ix]);
4310 bool ptr = TYPE_PTR_P (tparm);
4311 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4312 if ((ptr || arr || !same_type_p (tparm, targ))
4313 && (!ptr || !arr
4314 || !same_type_p (TREE_TYPE (tparm),
4315 TREE_TYPE (targ))))
4316 found = false;
4317 }
4318
4319 if (found
4320 && ix == vec_safe_length (args)
4321 /* May be this should be sufficient_parms_p instead,
4322 depending on how exactly should user-defined literals
4323 work in presence of default arguments on the literal
4324 operator parameters. */
4325 && parmtypes == void_list_node)
4326 return decl;
4327 }
4328 }
4329
4330 return error_mark_node;
4331 }
4332
4333 /* Parse a user-defined char constant. Returns a call to a user-defined
4334 literal operator taking the character as an argument. */
4335
4336 static cp_expr
4337 cp_parser_userdef_char_literal (cp_parser *parser)
4338 {
4339 cp_token *token = cp_lexer_consume_token (parser->lexer);
4340 tree literal = token->u.value;
4341 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4342 tree value = USERDEF_LITERAL_VALUE (literal);
4343 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4344 tree decl, result;
4345
4346 /* Build up a call to the user-defined operator */
4347 /* Lookup the name we got back from the id-expression. */
4348 releasing_vec args;
4349 vec_safe_push (args, value);
4350 decl = lookup_literal_operator (name, args);
4351 if (!decl || decl == error_mark_node)
4352 {
4353 error ("unable to find character literal operator %qD with %qT argument",
4354 name, TREE_TYPE (value));
4355 return error_mark_node;
4356 }
4357 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4358 return result;
4359 }
4360
4361 /* A subroutine of cp_parser_userdef_numeric_literal to
4362 create a char... template parameter pack from a string node. */
4363
4364 static tree
4365 make_char_string_pack (tree value)
4366 {
4367 tree charvec;
4368 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4369 const char *str = TREE_STRING_POINTER (value);
4370 int i, len = TREE_STRING_LENGTH (value) - 1;
4371 tree argvec = make_tree_vec (1);
4372
4373 /* Fill in CHARVEC with all of the parameters. */
4374 charvec = make_tree_vec (len);
4375 for (i = 0; i < len; ++i)
4376 {
4377 unsigned char s[3] = { '\'', str[i], '\'' };
4378 cpp_string in = { 3, s };
4379 cpp_string out = { 0, 0 };
4380 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4381 return NULL_TREE;
4382 gcc_assert (out.len == 2);
4383 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4384 out.text[0]);
4385 }
4386
4387 /* Build the argument packs. */
4388 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4389
4390 TREE_VEC_ELT (argvec, 0) = argpack;
4391
4392 return argvec;
4393 }
4394
4395 /* A subroutine of cp_parser_userdef_numeric_literal to
4396 create a char... template parameter pack from a string node. */
4397
4398 static tree
4399 make_string_pack (tree value)
4400 {
4401 tree charvec;
4402 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4403 const unsigned char *str
4404 = (const unsigned char *) TREE_STRING_POINTER (value);
4405 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4406 int len = TREE_STRING_LENGTH (value) / sz - 1;
4407 tree argvec = make_tree_vec (2);
4408
4409 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4410 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4411
4412 /* First template parm is character type. */
4413 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4414
4415 /* Fill in CHARVEC with all of the parameters. */
4416 charvec = make_tree_vec (len);
4417 for (int i = 0; i < len; ++i)
4418 TREE_VEC_ELT (charvec, i)
4419 = double_int_to_tree (str_char_type_node,
4420 double_int::from_buffer (str + i * sz, sz));
4421
4422 /* Build the argument packs. */
4423 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4424
4425 TREE_VEC_ELT (argvec, 1) = argpack;
4426
4427 return argvec;
4428 }
4429
4430 /* Parse a user-defined numeric constant. returns a call to a user-defined
4431 literal operator. */
4432
4433 static cp_expr
4434 cp_parser_userdef_numeric_literal (cp_parser *parser)
4435 {
4436 cp_token *token = cp_lexer_consume_token (parser->lexer);
4437 tree literal = token->u.value;
4438 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4439 tree value = USERDEF_LITERAL_VALUE (literal);
4440 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4441 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4442 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4443 tree decl, result;
4444
4445 /* Look for a literal operator taking the exact type of numeric argument
4446 as the literal value. */
4447 releasing_vec args;
4448 vec_safe_push (args, value);
4449 decl = lookup_literal_operator (name, args);
4450 if (decl && decl != error_mark_node)
4451 {
4452 result = finish_call_expr (decl, &args, false, true,
4453 tf_warning_or_error);
4454
4455 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4456 {
4457 warning_at (token->location, OPT_Woverflow,
4458 "integer literal exceeds range of %qT type",
4459 long_long_unsigned_type_node);
4460 }
4461 else
4462 {
4463 if (overflow > 0)
4464 warning_at (token->location, OPT_Woverflow,
4465 "floating literal exceeds range of %qT type",
4466 long_double_type_node);
4467 else if (overflow < 0)
4468 warning_at (token->location, OPT_Woverflow,
4469 "floating literal truncated to zero");
4470 }
4471
4472 return result;
4473 }
4474
4475 /* If the numeric argument didn't work, look for a raw literal
4476 operator taking a const char* argument consisting of the number
4477 in string format. */
4478 args->truncate (0);
4479 vec_safe_push (args, num_string);
4480 decl = lookup_literal_operator (name, args);
4481 if (decl && decl != error_mark_node)
4482 {
4483 result = finish_call_expr (decl, &args, false, true,
4484 tf_warning_or_error);
4485 return result;
4486 }
4487
4488 /* If the raw literal didn't work, look for a non-type template
4489 function with parameter pack char.... Call the function with
4490 template parameter characters representing the number. */
4491 args->truncate (0);
4492 decl = lookup_literal_operator (name, args);
4493 if (decl && decl != error_mark_node)
4494 {
4495 tree tmpl_args = make_char_string_pack (num_string);
4496 if (tmpl_args == NULL_TREE)
4497 {
4498 error ("failed to translate literal to execution character set %qT",
4499 num_string);
4500 return error_mark_node;
4501 }
4502 decl = lookup_template_function (decl, tmpl_args);
4503 result = finish_call_expr (decl, &args, false, true,
4504 tf_warning_or_error);
4505 return result;
4506 }
4507
4508 /* In C++14 the standard library defines complex number suffixes that
4509 conflict with GNU extensions. Prefer them if <complex> is #included. */
4510 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4511 bool i14 = (cxx_dialect > cxx11
4512 && (id_equal (suffix_id, "i")
4513 || id_equal (suffix_id, "if")
4514 || id_equal (suffix_id, "il")));
4515 diagnostic_t kind = DK_ERROR;
4516 int opt = 0;
4517
4518 if (i14 && ext)
4519 {
4520 tree cxlit = lookup_qualified_name (std_node,
4521 get_identifier ("complex_literals"),
4522 0, false, false);
4523 if (cxlit == error_mark_node)
4524 {
4525 /* No <complex>, so pedwarn and use GNU semantics. */
4526 kind = DK_PEDWARN;
4527 opt = OPT_Wpedantic;
4528 }
4529 }
4530
4531 bool complained
4532 = emit_diagnostic (kind, input_location, opt,
4533 "unable to find numeric literal operator %qD", name);
4534
4535 if (!complained)
4536 /* Don't inform either. */;
4537 else if (i14)
4538 {
4539 inform (token->location, "add %<using namespace std::complex_literals%> "
4540 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4541 "suffixes");
4542 if (ext)
4543 inform (token->location, "or use %<j%> instead of %<i%> for the "
4544 "GNU built-in suffix");
4545 }
4546 else if (!ext)
4547 inform (token->location, "use %<-fext-numeric-literals%> "
4548 "to enable more built-in suffixes");
4549
4550 if (kind == DK_ERROR)
4551 value = error_mark_node;
4552 else
4553 {
4554 /* Use the built-in semantics. */
4555 tree type;
4556 if (id_equal (suffix_id, "i"))
4557 {
4558 if (TREE_CODE (value) == INTEGER_CST)
4559 type = integer_type_node;
4560 else
4561 type = double_type_node;
4562 }
4563 else if (id_equal (suffix_id, "if"))
4564 type = float_type_node;
4565 else /* if (id_equal (suffix_id, "il")) */
4566 type = long_double_type_node;
4567
4568 value = build_complex (build_complex_type (type),
4569 fold_convert (type, integer_zero_node),
4570 fold_convert (type, value));
4571 }
4572
4573 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4574 /* Avoid repeated diagnostics. */
4575 token->u.value = value;
4576 return value;
4577 }
4578
4579 /* Parse a user-defined string constant. Returns a call to a user-defined
4580 literal operator taking a character pointer and the length of the string
4581 as arguments. */
4582
4583 static tree
4584 cp_parser_userdef_string_literal (tree literal)
4585 {
4586 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4587 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4588 tree value = USERDEF_LITERAL_VALUE (literal);
4589 int len = TREE_STRING_LENGTH (value)
4590 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4591 tree decl;
4592
4593 /* Build up a call to the user-defined operator. */
4594 /* Lookup the name we got back from the id-expression. */
4595 releasing_vec args;
4596 vec_safe_push (args, value);
4597 vec_safe_push (args, build_int_cst (size_type_node, len));
4598 decl = lookup_literal_operator (name, args);
4599
4600 if (decl && decl != error_mark_node)
4601 return finish_call_expr (decl, &args, false, true,
4602 tf_warning_or_error);
4603
4604 /* Look for a suitable template function, either (C++20) with a single
4605 parameter of class type, or (N3599) with typename parameter CharT and
4606 parameter pack CharT... */
4607 args->truncate (0);
4608 decl = lookup_literal_operator (name, args);
4609 if (decl && decl != error_mark_node)
4610 {
4611 /* Use resolve_nondeduced_context to try to choose one form of template
4612 or the other. */
4613 tree tmpl_args = make_tree_vec (1);
4614 TREE_VEC_ELT (tmpl_args, 0) = value;
4615 decl = lookup_template_function (decl, tmpl_args);
4616 tree res = resolve_nondeduced_context (decl, tf_none);
4617 if (DECL_P (res))
4618 decl = res;
4619 else
4620 {
4621 TREE_OPERAND (decl, 1) = make_string_pack (value);
4622 res = resolve_nondeduced_context (decl, tf_none);
4623 if (DECL_P (res))
4624 decl = res;
4625 }
4626 if (!DECL_P (decl) && cxx_dialect > cxx17)
4627 TREE_OPERAND (decl, 1) = tmpl_args;
4628 return finish_call_expr (decl, &args, false, true,
4629 tf_warning_or_error);
4630 }
4631
4632 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4633 name, TREE_TYPE (value), size_type_node);
4634 return error_mark_node;
4635 }
4636
4637
4638 /* Basic concepts [gram.basic] */
4639
4640 /* Parse a translation-unit.
4641
4642 translation-unit:
4643 declaration-seq [opt] */
4644
4645 static void
4646 cp_parser_translation_unit (cp_parser* parser)
4647 {
4648 gcc_checking_assert (!cp_error_declarator);
4649
4650 /* Create the declarator obstack. */
4651 gcc_obstack_init (&declarator_obstack);
4652 /* Create the error declarator. */
4653 cp_error_declarator = make_declarator (cdk_error);
4654 /* Create the empty parameter list. */
4655 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4656 UNKNOWN_LOCATION);
4657 /* Remember where the base of the declarator obstack lies. */
4658 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4659
4660 bool implicit_extern_c = false;
4661
4662 for (;;)
4663 {
4664 cp_token *token = cp_lexer_peek_token (parser->lexer);
4665
4666 /* If we're entering or exiting a region that's implicitly
4667 extern "C", modify the lang context appropriately. */
4668 if (implicit_extern_c
4669 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4670 {
4671 implicit_extern_c = !implicit_extern_c;
4672 if (implicit_extern_c)
4673 push_lang_context (lang_name_c);
4674 else
4675 pop_lang_context ();
4676 }
4677
4678 if (token->type == CPP_EOF)
4679 break;
4680
4681 if (token->type == CPP_CLOSE_BRACE)
4682 {
4683 cp_parser_error (parser, "expected declaration");
4684 cp_lexer_consume_token (parser->lexer);
4685 /* If the next token is now a `;', consume it. */
4686 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4687 cp_lexer_consume_token (parser->lexer);
4688 }
4689 else
4690 cp_parser_toplevel_declaration (parser);
4691 }
4692
4693 /* Get rid of the token array; we don't need it any more. */
4694 cp_lexer_destroy (parser->lexer);
4695 parser->lexer = NULL;
4696
4697 /* The EOF should have reset this. */
4698 gcc_checking_assert (!implicit_extern_c);
4699
4700 /* Make sure the declarator obstack was fully cleaned up. */
4701 gcc_assert (obstack_next_free (&declarator_obstack)
4702 == declarator_obstack_base);
4703 }
4704
4705 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4706 decltype context. */
4707
4708 static inline tsubst_flags_t
4709 complain_flags (bool decltype_p)
4710 {
4711 tsubst_flags_t complain = tf_warning_or_error;
4712 if (decltype_p)
4713 complain |= tf_decltype;
4714 return complain;
4715 }
4716
4717 /* We're about to parse a collection of statements. If we're currently
4718 parsing tentatively, set up a firewall so that any nested
4719 cp_parser_commit_to_tentative_parse won't affect the current context. */
4720
4721 static cp_token_position
4722 cp_parser_start_tentative_firewall (cp_parser *parser)
4723 {
4724 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4725 return 0;
4726
4727 cp_parser_parse_tentatively (parser);
4728 cp_parser_commit_to_topmost_tentative_parse (parser);
4729 return cp_lexer_token_position (parser->lexer, false);
4730 }
4731
4732 /* We've finished parsing the collection of statements. Wrap up the
4733 firewall and replace the relevant tokens with the parsed form. */
4734
4735 static void
4736 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4737 tree expr)
4738 {
4739 if (!start)
4740 return;
4741
4742 /* Finish the firewall level. */
4743 cp_parser_parse_definitely (parser);
4744 /* And remember the result of the parse for when we try again. */
4745 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4746 token->type = CPP_PREPARSED_EXPR;
4747 token->u.value = expr;
4748 token->keyword = RID_MAX;
4749 cp_lexer_purge_tokens_after (parser->lexer, start);
4750 }
4751
4752 /* Like the above functions, but let the user modify the tokens. Used by
4753 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4754 later parses, so it makes sense to localize the effects of
4755 cp_parser_commit_to_tentative_parse. */
4756
4757 struct tentative_firewall
4758 {
4759 cp_parser *parser;
4760 bool set;
4761
4762 tentative_firewall (cp_parser *p): parser(p)
4763 {
4764 /* If we're currently parsing tentatively, start a committed level as a
4765 firewall and then an inner tentative parse. */
4766 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4767 {
4768 cp_parser_parse_tentatively (parser);
4769 cp_parser_commit_to_topmost_tentative_parse (parser);
4770 cp_parser_parse_tentatively (parser);
4771 }
4772 }
4773
4774 ~tentative_firewall()
4775 {
4776 if (set)
4777 {
4778 /* Finish the inner tentative parse and the firewall, propagating any
4779 uncommitted error state to the outer tentative parse. */
4780 bool err = cp_parser_error_occurred (parser);
4781 cp_parser_parse_definitely (parser);
4782 cp_parser_parse_definitely (parser);
4783 if (err)
4784 cp_parser_simulate_error (parser);
4785 }
4786 }
4787 };
4788
4789 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4790 This class is for tracking such a matching pair of symbols.
4791 In particular, it tracks the location of the first token,
4792 so that if the second token is missing, we can highlight the
4793 location of the first token when notifying the user about the
4794 problem. */
4795
4796 template <typename traits_t>
4797 class token_pair
4798 {
4799 public:
4800 /* token_pair's ctor. */
4801 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4802
4803 /* If the next token is the opening symbol for this pair, consume it and
4804 return true.
4805 Otherwise, issue an error and return false.
4806 In either case, record the location of the opening token. */
4807
4808 bool require_open (cp_parser *parser)
4809 {
4810 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4811 return cp_parser_require (parser, traits_t::open_token_type,
4812 traits_t::required_token_open);
4813 }
4814
4815 /* Consume the next token from PARSER, recording its location as
4816 that of the opening token within the pair. */
4817
4818 cp_token * consume_open (cp_parser *parser)
4819 {
4820 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4821 gcc_assert (tok->type == traits_t::open_token_type);
4822 m_open_loc = tok->location;
4823 return tok;
4824 }
4825
4826 /* If the next token is the closing symbol for this pair, consume it
4827 and return it.
4828 Otherwise, issue an error, highlighting the location of the
4829 corresponding opening token, and return NULL. */
4830
4831 cp_token *require_close (cp_parser *parser) const
4832 {
4833 return cp_parser_require (parser, traits_t::close_token_type,
4834 traits_t::required_token_close,
4835 m_open_loc);
4836 }
4837
4838 private:
4839 location_t m_open_loc;
4840 };
4841
4842 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4843
4844 struct matching_paren_traits
4845 {
4846 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4847 static const enum required_token required_token_open = RT_OPEN_PAREN;
4848 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4849 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4850 };
4851
4852 /* "matching_parens" is a token_pair<T> class for tracking matching
4853 pairs of parentheses. */
4854
4855 typedef token_pair<matching_paren_traits> matching_parens;
4856
4857 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4858
4859 struct matching_brace_traits
4860 {
4861 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4862 static const enum required_token required_token_open = RT_OPEN_BRACE;
4863 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4864 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4865 };
4866
4867 /* "matching_braces" is a token_pair<T> class for tracking matching
4868 pairs of braces. */
4869
4870 typedef token_pair<matching_brace_traits> matching_braces;
4871
4872
4873 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4874 enclosing parentheses. */
4875
4876 static cp_expr
4877 cp_parser_statement_expr (cp_parser *parser)
4878 {
4879 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4880
4881 /* Consume the '('. */
4882 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4883 matching_parens parens;
4884 parens.consume_open (parser);
4885 /* Start the statement-expression. */
4886 tree expr = begin_stmt_expr ();
4887 /* Parse the compound-statement. */
4888 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4889 /* Finish up. */
4890 expr = finish_stmt_expr (expr, false);
4891 /* Consume the ')'. */
4892 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4893 if (!parens.require_close (parser))
4894 cp_parser_skip_to_end_of_statement (parser);
4895
4896 cp_parser_end_tentative_firewall (parser, start, expr);
4897 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4898 return cp_expr (expr, combined_loc);
4899 }
4900
4901 /* Expressions [gram.expr] */
4902
4903 /* Parse a fold-operator.
4904
4905 fold-operator:
4906 - * / % ^ & | = < > << >>
4907 = -= *= /= %= ^= &= |= <<= >>=
4908 == != <= >= && || , .* ->*
4909
4910 This returns the tree code corresponding to the matched operator
4911 as an int. When the current token matches a compound assignment
4912 opertor, the resulting tree code is the negative value of the
4913 non-assignment operator. */
4914
4915 static int
4916 cp_parser_fold_operator (cp_token *token)
4917 {
4918 switch (token->type)
4919 {
4920 case CPP_PLUS: return PLUS_EXPR;
4921 case CPP_MINUS: return MINUS_EXPR;
4922 case CPP_MULT: return MULT_EXPR;
4923 case CPP_DIV: return TRUNC_DIV_EXPR;
4924 case CPP_MOD: return TRUNC_MOD_EXPR;
4925 case CPP_XOR: return BIT_XOR_EXPR;
4926 case CPP_AND: return BIT_AND_EXPR;
4927 case CPP_OR: return BIT_IOR_EXPR;
4928 case CPP_LSHIFT: return LSHIFT_EXPR;
4929 case CPP_RSHIFT: return RSHIFT_EXPR;
4930
4931 case CPP_EQ: return -NOP_EXPR;
4932 case CPP_PLUS_EQ: return -PLUS_EXPR;
4933 case CPP_MINUS_EQ: return -MINUS_EXPR;
4934 case CPP_MULT_EQ: return -MULT_EXPR;
4935 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4936 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4937 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4938 case CPP_AND_EQ: return -BIT_AND_EXPR;
4939 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4940 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4941 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4942
4943 case CPP_EQ_EQ: return EQ_EXPR;
4944 case CPP_NOT_EQ: return NE_EXPR;
4945 case CPP_LESS: return LT_EXPR;
4946 case CPP_GREATER: return GT_EXPR;
4947 case CPP_LESS_EQ: return LE_EXPR;
4948 case CPP_GREATER_EQ: return GE_EXPR;
4949
4950 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4951 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4952
4953 case CPP_COMMA: return COMPOUND_EXPR;
4954
4955 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4956 case CPP_DEREF_STAR: return MEMBER_REF;
4957
4958 default: return ERROR_MARK;
4959 }
4960 }
4961
4962 /* Returns true if CODE indicates a binary expression, which is not allowed in
4963 the LHS of a fold-expression. More codes will need to be added to use this
4964 function in other contexts. */
4965
4966 static bool
4967 is_binary_op (tree_code code)
4968 {
4969 switch (code)
4970 {
4971 case PLUS_EXPR:
4972 case POINTER_PLUS_EXPR:
4973 case MINUS_EXPR:
4974 case MULT_EXPR:
4975 case TRUNC_DIV_EXPR:
4976 case TRUNC_MOD_EXPR:
4977 case BIT_XOR_EXPR:
4978 case BIT_AND_EXPR:
4979 case BIT_IOR_EXPR:
4980 case LSHIFT_EXPR:
4981 case RSHIFT_EXPR:
4982
4983 case MODOP_EXPR:
4984
4985 case EQ_EXPR:
4986 case NE_EXPR:
4987 case LE_EXPR:
4988 case GE_EXPR:
4989 case LT_EXPR:
4990 case GT_EXPR:
4991
4992 case TRUTH_ANDIF_EXPR:
4993 case TRUTH_ORIF_EXPR:
4994
4995 case COMPOUND_EXPR:
4996
4997 case DOTSTAR_EXPR:
4998 case MEMBER_REF:
4999 return true;
5000
5001 default:
5002 return false;
5003 }
5004 }
5005
5006 /* If the next token is a suitable fold operator, consume it and return as
5007 the function above. */
5008
5009 static int
5010 cp_parser_fold_operator (cp_parser *parser)
5011 {
5012 cp_token* token = cp_lexer_peek_token (parser->lexer);
5013 int code = cp_parser_fold_operator (token);
5014 if (code != ERROR_MARK)
5015 cp_lexer_consume_token (parser->lexer);
5016 return code;
5017 }
5018
5019 /* Parse a fold-expression.
5020
5021 fold-expression:
5022 ( ... folding-operator cast-expression)
5023 ( cast-expression folding-operator ... )
5024 ( cast-expression folding operator ... folding-operator cast-expression)
5025
5026 Note that the '(' and ')' are matched in primary expression. */
5027
5028 static cp_expr
5029 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5030 {
5031 cp_id_kind pidk;
5032
5033 // Left fold.
5034 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5035 {
5036 cp_lexer_consume_token (parser->lexer);
5037 int op = cp_parser_fold_operator (parser);
5038 if (op == ERROR_MARK)
5039 {
5040 cp_parser_error (parser, "expected binary operator");
5041 return error_mark_node;
5042 }
5043
5044 tree expr = cp_parser_cast_expression (parser, false, false,
5045 false, &pidk);
5046 if (expr == error_mark_node)
5047 return error_mark_node;
5048 return finish_left_unary_fold_expr (expr, op);
5049 }
5050
5051 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5052 int op = cp_parser_fold_operator (parser);
5053 if (op == ERROR_MARK)
5054 {
5055 cp_parser_error (parser, "expected binary operator");
5056 return error_mark_node;
5057 }
5058
5059 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5060 {
5061 cp_parser_error (parser, "expected ...");
5062 return error_mark_node;
5063 }
5064 cp_lexer_consume_token (parser->lexer);
5065
5066 /* The operands of a fold-expression are cast-expressions, so binary or
5067 conditional expressions are not allowed. We check this here to avoid
5068 tentative parsing. */
5069 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5070 /* OK, the expression was parenthesized. */;
5071 else if (is_binary_op (TREE_CODE (expr1)))
5072 error_at (location_of (expr1),
5073 "binary expression in operand of fold-expression");
5074 else if (TREE_CODE (expr1) == COND_EXPR
5075 || (REFERENCE_REF_P (expr1)
5076 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5077 error_at (location_of (expr1),
5078 "conditional expression in operand of fold-expression");
5079
5080 // Right fold.
5081 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5082 return finish_right_unary_fold_expr (expr1, op);
5083
5084 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5085 {
5086 cp_parser_error (parser, "mismatched operator in fold-expression");
5087 return error_mark_node;
5088 }
5089 cp_lexer_consume_token (parser->lexer);
5090
5091 // Binary left or right fold.
5092 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5093 if (expr2 == error_mark_node)
5094 return error_mark_node;
5095 return finish_binary_fold_expr (expr1, expr2, op);
5096 }
5097
5098 /* Parse a primary-expression.
5099
5100 primary-expression:
5101 literal
5102 this
5103 ( expression )
5104 id-expression
5105 lambda-expression (C++11)
5106
5107 GNU Extensions:
5108
5109 primary-expression:
5110 ( compound-statement )
5111 __builtin_va_arg ( assignment-expression , type-id )
5112 __builtin_offsetof ( type-id , offsetof-expression )
5113
5114 C++ Extensions:
5115 __has_nothrow_assign ( type-id )
5116 __has_nothrow_constructor ( type-id )
5117 __has_nothrow_copy ( type-id )
5118 __has_trivial_assign ( type-id )
5119 __has_trivial_constructor ( type-id )
5120 __has_trivial_copy ( type-id )
5121 __has_trivial_destructor ( type-id )
5122 __has_virtual_destructor ( type-id )
5123 __is_abstract ( type-id )
5124 __is_base_of ( type-id , type-id )
5125 __is_class ( type-id )
5126 __is_empty ( type-id )
5127 __is_enum ( type-id )
5128 __is_final ( type-id )
5129 __is_literal_type ( type-id )
5130 __is_pod ( type-id )
5131 __is_polymorphic ( type-id )
5132 __is_std_layout ( type-id )
5133 __is_trivial ( type-id )
5134 __is_union ( type-id )
5135
5136 Objective-C++ Extension:
5137
5138 primary-expression:
5139 objc-expression
5140
5141 literal:
5142 __null
5143
5144 ADDRESS_P is true iff this expression was immediately preceded by
5145 "&" and therefore might denote a pointer-to-member. CAST_P is true
5146 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5147 true iff this expression is a template argument.
5148
5149 Returns a representation of the expression. Upon return, *IDK
5150 indicates what kind of id-expression (if any) was present. */
5151
5152 static cp_expr
5153 cp_parser_primary_expression (cp_parser *parser,
5154 bool address_p,
5155 bool cast_p,
5156 bool template_arg_p,
5157 bool decltype_p,
5158 cp_id_kind *idk)
5159 {
5160 cp_token *token = NULL;
5161
5162 /* Assume the primary expression is not an id-expression. */
5163 *idk = CP_ID_KIND_NONE;
5164
5165 /* Peek at the next token. */
5166 token = cp_lexer_peek_token (parser->lexer);
5167 switch ((int) token->type)
5168 {
5169 /* literal:
5170 integer-literal
5171 character-literal
5172 floating-literal
5173 string-literal
5174 boolean-literal
5175 pointer-literal
5176 user-defined-literal */
5177 case CPP_CHAR:
5178 case CPP_CHAR16:
5179 case CPP_CHAR32:
5180 case CPP_WCHAR:
5181 case CPP_UTF8CHAR:
5182 case CPP_NUMBER:
5183 case CPP_PREPARSED_EXPR:
5184 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5185 return cp_parser_userdef_numeric_literal (parser);
5186 token = cp_lexer_consume_token (parser->lexer);
5187 if (TREE_CODE (token->u.value) == FIXED_CST)
5188 {
5189 error_at (token->location,
5190 "fixed-point types not supported in C++");
5191 return error_mark_node;
5192 }
5193 /* Floating-point literals are only allowed in an integral
5194 constant expression if they are cast to an integral or
5195 enumeration type. */
5196 if (TREE_CODE (token->u.value) == REAL_CST
5197 && parser->integral_constant_expression_p
5198 && pedantic)
5199 {
5200 /* CAST_P will be set even in invalid code like "int(2.7 +
5201 ...)". Therefore, we have to check that the next token
5202 is sure to end the cast. */
5203 if (cast_p)
5204 {
5205 cp_token *next_token;
5206
5207 next_token = cp_lexer_peek_token (parser->lexer);
5208 if (/* The comma at the end of an
5209 enumerator-definition. */
5210 next_token->type != CPP_COMMA
5211 /* The curly brace at the end of an enum-specifier. */
5212 && next_token->type != CPP_CLOSE_BRACE
5213 /* The end of a statement. */
5214 && next_token->type != CPP_SEMICOLON
5215 /* The end of the cast-expression. */
5216 && next_token->type != CPP_CLOSE_PAREN
5217 /* The end of an array bound. */
5218 && next_token->type != CPP_CLOSE_SQUARE
5219 /* The closing ">" in a template-argument-list. */
5220 && (next_token->type != CPP_GREATER
5221 || parser->greater_than_is_operator_p)
5222 /* C++0x only: A ">>" treated like two ">" tokens,
5223 in a template-argument-list. */
5224 && (next_token->type != CPP_RSHIFT
5225 || (cxx_dialect == cxx98)
5226 || parser->greater_than_is_operator_p))
5227 cast_p = false;
5228 }
5229
5230 /* If we are within a cast, then the constraint that the
5231 cast is to an integral or enumeration type will be
5232 checked at that point. If we are not within a cast, then
5233 this code is invalid. */
5234 if (!cast_p)
5235 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5236 }
5237 return (cp_expr (token->u.value, token->location)
5238 .maybe_add_location_wrapper ());
5239
5240 case CPP_CHAR_USERDEF:
5241 case CPP_CHAR16_USERDEF:
5242 case CPP_CHAR32_USERDEF:
5243 case CPP_WCHAR_USERDEF:
5244 case CPP_UTF8CHAR_USERDEF:
5245 return cp_parser_userdef_char_literal (parser);
5246
5247 case CPP_STRING:
5248 case CPP_STRING16:
5249 case CPP_STRING32:
5250 case CPP_WSTRING:
5251 case CPP_UTF8STRING:
5252 case CPP_STRING_USERDEF:
5253 case CPP_STRING16_USERDEF:
5254 case CPP_STRING32_USERDEF:
5255 case CPP_WSTRING_USERDEF:
5256 case CPP_UTF8STRING_USERDEF:
5257 /* ??? Should wide strings be allowed when parser->translate_strings_p
5258 is false (i.e. in attributes)? If not, we can kill the third
5259 argument to cp_parser_string_literal. */
5260 return (cp_parser_string_literal (parser,
5261 parser->translate_strings_p,
5262 true)
5263 .maybe_add_location_wrapper ());
5264
5265 case CPP_OPEN_PAREN:
5266 /* If we see `( { ' then we are looking at the beginning of
5267 a GNU statement-expression. */
5268 if (cp_parser_allow_gnu_extensions_p (parser)
5269 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5270 {
5271 /* Statement-expressions are not allowed by the standard. */
5272 pedwarn (token->location, OPT_Wpedantic,
5273 "ISO C++ forbids braced-groups within expressions");
5274
5275 /* And they're not allowed outside of a function-body; you
5276 cannot, for example, write:
5277
5278 int i = ({ int j = 3; j + 1; });
5279
5280 at class or namespace scope. */
5281 if (!parser->in_function_body
5282 || parser->in_template_argument_list_p)
5283 {
5284 error_at (token->location,
5285 "statement-expressions are not allowed outside "
5286 "functions nor in template-argument lists");
5287 cp_parser_skip_to_end_of_block_or_statement (parser);
5288 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5289 cp_lexer_consume_token (parser->lexer);
5290 return error_mark_node;
5291 }
5292 else
5293 return cp_parser_statement_expr (parser);
5294 }
5295 /* Otherwise it's a normal parenthesized expression. */
5296 {
5297 cp_expr expr;
5298 bool saved_greater_than_is_operator_p;
5299
5300 location_t open_paren_loc = token->location;
5301
5302 /* Consume the `('. */
5303 matching_parens parens;
5304 parens.consume_open (parser);
5305 /* Within a parenthesized expression, a `>' token is always
5306 the greater-than operator. */
5307 saved_greater_than_is_operator_p
5308 = parser->greater_than_is_operator_p;
5309 parser->greater_than_is_operator_p = true;
5310
5311 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5312 /* Left fold expression. */
5313 expr = NULL_TREE;
5314 else
5315 /* Parse the parenthesized expression. */
5316 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5317
5318 token = cp_lexer_peek_token (parser->lexer);
5319 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5320 {
5321 expr = cp_parser_fold_expression (parser, expr);
5322 if (expr != error_mark_node
5323 && cxx_dialect < cxx17
5324 && !in_system_header_at (input_location))
5325 pedwarn (input_location, 0, "fold-expressions only available "
5326 "with %<-std=c++17%> or %<-std=gnu++17%>");
5327 }
5328 else
5329 /* Let the front end know that this expression was
5330 enclosed in parentheses. This matters in case, for
5331 example, the expression is of the form `A::B', since
5332 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5333 not. */
5334 expr = finish_parenthesized_expr (expr);
5335
5336 /* DR 705: Wrapping an unqualified name in parentheses
5337 suppresses arg-dependent lookup. We want to pass back
5338 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5339 (c++/37862), but none of the others. */
5340 if (*idk != CP_ID_KIND_QUALIFIED)
5341 *idk = CP_ID_KIND_NONE;
5342
5343 /* The `>' token might be the end of a template-id or
5344 template-parameter-list now. */
5345 parser->greater_than_is_operator_p
5346 = saved_greater_than_is_operator_p;
5347
5348 /* Consume the `)'. */
5349 token = cp_lexer_peek_token (parser->lexer);
5350 location_t close_paren_loc = token->location;
5351 expr.set_range (open_paren_loc, close_paren_loc);
5352 if (!parens.require_close (parser)
5353 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5354 cp_parser_skip_to_end_of_statement (parser);
5355
5356 return expr;
5357 }
5358
5359 case CPP_OPEN_SQUARE:
5360 {
5361 if (c_dialect_objc ())
5362 {
5363 /* We might have an Objective-C++ message. */
5364 cp_parser_parse_tentatively (parser);
5365 tree msg = cp_parser_objc_message_expression (parser);
5366 /* If that works out, we're done ... */
5367 if (cp_parser_parse_definitely (parser))
5368 return msg;
5369 /* ... else, fall though to see if it's a lambda. */
5370 }
5371 cp_expr lam = cp_parser_lambda_expression (parser);
5372 /* Don't warn about a failed tentative parse. */
5373 if (cp_parser_error_occurred (parser))
5374 return error_mark_node;
5375 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5376 return lam;
5377 }
5378
5379 case CPP_OBJC_STRING:
5380 if (c_dialect_objc ())
5381 /* We have an Objective-C++ string literal. */
5382 return cp_parser_objc_expression (parser);
5383 cp_parser_error (parser, "expected primary-expression");
5384 return error_mark_node;
5385
5386 case CPP_KEYWORD:
5387 switch (token->keyword)
5388 {
5389 /* These two are the boolean literals. */
5390 case RID_TRUE:
5391 cp_lexer_consume_token (parser->lexer);
5392 return cp_expr (boolean_true_node, token->location);
5393 case RID_FALSE:
5394 cp_lexer_consume_token (parser->lexer);
5395 return cp_expr (boolean_false_node, token->location);
5396
5397 /* The `__null' literal. */
5398 case RID_NULL:
5399 cp_lexer_consume_token (parser->lexer);
5400 return cp_expr (null_node, token->location);
5401
5402 /* The `nullptr' literal. */
5403 case RID_NULLPTR:
5404 cp_lexer_consume_token (parser->lexer);
5405 return cp_expr (nullptr_node, token->location);
5406
5407 /* Recognize the `this' keyword. */
5408 case RID_THIS:
5409 cp_lexer_consume_token (parser->lexer);
5410 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5411 {
5412 error_at (token->location,
5413 "%<this%> may not be used in this context");
5414 return error_mark_node;
5415 }
5416 /* Pointers cannot appear in constant-expressions. */
5417 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5418 return error_mark_node;
5419 return cp_expr (finish_this_expr (), token->location);
5420
5421 /* The `operator' keyword can be the beginning of an
5422 id-expression. */
5423 case RID_OPERATOR:
5424 goto id_expression;
5425
5426 case RID_FUNCTION_NAME:
5427 case RID_PRETTY_FUNCTION_NAME:
5428 case RID_C99_FUNCTION_NAME:
5429 {
5430 non_integral_constant name;
5431
5432 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5433 __func__ are the names of variables -- but they are
5434 treated specially. Therefore, they are handled here,
5435 rather than relying on the generic id-expression logic
5436 below. Grammatically, these names are id-expressions.
5437
5438 Consume the token. */
5439 token = cp_lexer_consume_token (parser->lexer);
5440
5441 switch (token->keyword)
5442 {
5443 case RID_FUNCTION_NAME:
5444 name = NIC_FUNC_NAME;
5445 break;
5446 case RID_PRETTY_FUNCTION_NAME:
5447 name = NIC_PRETTY_FUNC;
5448 break;
5449 case RID_C99_FUNCTION_NAME:
5450 name = NIC_C99_FUNC;
5451 break;
5452 default:
5453 gcc_unreachable ();
5454 }
5455
5456 if (cp_parser_non_integral_constant_expression (parser, name))
5457 return error_mark_node;
5458
5459 /* Look up the name. */
5460 return finish_fname (token->u.value);
5461 }
5462
5463 case RID_VA_ARG:
5464 {
5465 tree expression;
5466 tree type;
5467 location_t type_location;
5468 location_t start_loc
5469 = cp_lexer_peek_token (parser->lexer)->location;
5470 /* The `__builtin_va_arg' construct is used to handle
5471 `va_arg'. Consume the `__builtin_va_arg' token. */
5472 cp_lexer_consume_token (parser->lexer);
5473 /* Look for the opening `('. */
5474 matching_parens parens;
5475 parens.require_open (parser);
5476 /* Now, parse the assignment-expression. */
5477 expression = cp_parser_assignment_expression (parser);
5478 /* Look for the `,'. */
5479 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5480 type_location = cp_lexer_peek_token (parser->lexer)->location;
5481 /* Parse the type-id. */
5482 {
5483 type_id_in_expr_sentinel s (parser);
5484 type = cp_parser_type_id (parser);
5485 }
5486 /* Look for the closing `)'. */
5487 location_t finish_loc
5488 = cp_lexer_peek_token (parser->lexer)->location;
5489 parens.require_close (parser);
5490 /* Using `va_arg' in a constant-expression is not
5491 allowed. */
5492 if (cp_parser_non_integral_constant_expression (parser,
5493 NIC_VA_ARG))
5494 return error_mark_node;
5495 /* Construct a location of the form:
5496 __builtin_va_arg (v, int)
5497 ~~~~~~~~~~~~~~~~~~~~~^~~~
5498 with the caret at the type, ranging from the start of the
5499 "__builtin_va_arg" token to the close paren. */
5500 location_t combined_loc
5501 = make_location (type_location, start_loc, finish_loc);
5502 return build_x_va_arg (combined_loc, expression, type);
5503 }
5504
5505 case RID_OFFSETOF:
5506 return cp_parser_builtin_offsetof (parser);
5507
5508 case RID_HAS_NOTHROW_ASSIGN:
5509 case RID_HAS_NOTHROW_CONSTRUCTOR:
5510 case RID_HAS_NOTHROW_COPY:
5511 case RID_HAS_TRIVIAL_ASSIGN:
5512 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5513 case RID_HAS_TRIVIAL_COPY:
5514 case RID_HAS_TRIVIAL_DESTRUCTOR:
5515 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5516 case RID_HAS_VIRTUAL_DESTRUCTOR:
5517 case RID_IS_ABSTRACT:
5518 case RID_IS_AGGREGATE:
5519 case RID_IS_BASE_OF:
5520 case RID_IS_CLASS:
5521 case RID_IS_EMPTY:
5522 case RID_IS_ENUM:
5523 case RID_IS_FINAL:
5524 case RID_IS_LITERAL_TYPE:
5525 case RID_IS_POD:
5526 case RID_IS_POLYMORPHIC:
5527 case RID_IS_SAME_AS:
5528 case RID_IS_STD_LAYOUT:
5529 case RID_IS_TRIVIAL:
5530 case RID_IS_TRIVIALLY_ASSIGNABLE:
5531 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5532 case RID_IS_TRIVIALLY_COPYABLE:
5533 case RID_IS_UNION:
5534 case RID_IS_ASSIGNABLE:
5535 case RID_IS_CONSTRUCTIBLE:
5536 return cp_parser_trait_expr (parser, token->keyword);
5537
5538 // C++ concepts
5539 case RID_REQUIRES:
5540 return cp_parser_requires_expression (parser);
5541
5542 /* Objective-C++ expressions. */
5543 case RID_AT_ENCODE:
5544 case RID_AT_PROTOCOL:
5545 case RID_AT_SELECTOR:
5546 return cp_parser_objc_expression (parser);
5547
5548 case RID_TEMPLATE:
5549 if (parser->in_function_body
5550 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5551 == CPP_LESS))
5552 {
5553 error_at (token->location,
5554 "a template declaration cannot appear at block scope");
5555 cp_parser_skip_to_end_of_block_or_statement (parser);
5556 return error_mark_node;
5557 }
5558 /* FALLTHRU */
5559 default:
5560 cp_parser_error (parser, "expected primary-expression");
5561 return error_mark_node;
5562 }
5563
5564 /* An id-expression can start with either an identifier, a
5565 `::' as the beginning of a qualified-id, or the "operator"
5566 keyword. */
5567 case CPP_NAME:
5568 case CPP_SCOPE:
5569 case CPP_TEMPLATE_ID:
5570 case CPP_NESTED_NAME_SPECIFIER:
5571 {
5572 id_expression:
5573 cp_expr id_expression;
5574 cp_expr decl;
5575 const char *error_msg;
5576 bool template_p;
5577 bool done;
5578 cp_token *id_expr_token;
5579
5580 /* Parse the id-expression. */
5581 id_expression
5582 = cp_parser_id_expression (parser,
5583 /*template_keyword_p=*/false,
5584 /*check_dependency_p=*/true,
5585 &template_p,
5586 /*declarator_p=*/false,
5587 /*optional_p=*/false);
5588 if (id_expression == error_mark_node)
5589 return error_mark_node;
5590 id_expr_token = token;
5591 token = cp_lexer_peek_token (parser->lexer);
5592 done = (token->type != CPP_OPEN_SQUARE
5593 && token->type != CPP_OPEN_PAREN
5594 && token->type != CPP_DOT
5595 && token->type != CPP_DEREF
5596 && token->type != CPP_PLUS_PLUS
5597 && token->type != CPP_MINUS_MINUS);
5598 /* If we have a template-id, then no further lookup is
5599 required. If the template-id was for a template-class, we
5600 will sometimes have a TYPE_DECL at this point. */
5601 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5602 || TREE_CODE (id_expression) == TYPE_DECL)
5603 decl = id_expression;
5604 /* Look up the name. */
5605 else
5606 {
5607 tree ambiguous_decls;
5608
5609 /* If we already know that this lookup is ambiguous, then
5610 we've already issued an error message; there's no reason
5611 to check again. */
5612 if (id_expr_token->type == CPP_NAME
5613 && id_expr_token->error_reported)
5614 {
5615 cp_parser_simulate_error (parser);
5616 return error_mark_node;
5617 }
5618
5619 decl = cp_parser_lookup_name (parser, id_expression,
5620 none_type,
5621 template_p,
5622 /*is_namespace=*/false,
5623 /*check_dependency=*/true,
5624 &ambiguous_decls,
5625 id_expression.get_location ());
5626 /* If the lookup was ambiguous, an error will already have
5627 been issued. */
5628 if (ambiguous_decls)
5629 return error_mark_node;
5630
5631 /* In Objective-C++, we may have an Objective-C 2.0
5632 dot-syntax for classes here. */
5633 if (c_dialect_objc ()
5634 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5635 && TREE_CODE (decl) == TYPE_DECL
5636 && objc_is_class_name (decl))
5637 {
5638 tree component;
5639 cp_lexer_consume_token (parser->lexer);
5640 component = cp_parser_identifier (parser);
5641 if (component == error_mark_node)
5642 return error_mark_node;
5643
5644 tree result = objc_build_class_component_ref (id_expression,
5645 component);
5646 /* Build a location of the form:
5647 expr.component
5648 ~~~~~^~~~~~~~~
5649 with caret at the start of the component name (at
5650 input_location), ranging from the start of the id_expression
5651 to the end of the component name. */
5652 location_t combined_loc
5653 = make_location (input_location, id_expression.get_start (),
5654 get_finish (input_location));
5655 protected_set_expr_location (result, combined_loc);
5656 return result;
5657 }
5658
5659 /* In Objective-C++, an instance variable (ivar) may be preferred
5660 to whatever cp_parser_lookup_name() found.
5661 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5662 rest of c-family, we have to do a little extra work to preserve
5663 any location information in cp_expr "decl". Given that
5664 objc_lookup_ivar is implemented in "c-family" and "objc", we
5665 have a trip through the pure "tree" type, rather than cp_expr.
5666 Naively copying it back to "decl" would implicitly give the
5667 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5668 store an EXPR_LOCATION. Hence we only update "decl" (and
5669 hence its location_t) if we get back a different tree node. */
5670 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5671 id_expression);
5672 if (decl_tree != decl.get_value ())
5673 decl = cp_expr (decl_tree);
5674
5675 /* If name lookup gives us a SCOPE_REF, then the
5676 qualifying scope was dependent. */
5677 if (TREE_CODE (decl) == SCOPE_REF)
5678 {
5679 /* At this point, we do not know if DECL is a valid
5680 integral constant expression. We assume that it is
5681 in fact such an expression, so that code like:
5682
5683 template <int N> struct A {
5684 int a[B<N>::i];
5685 };
5686
5687 is accepted. At template-instantiation time, we
5688 will check that B<N>::i is actually a constant. */
5689 return decl;
5690 }
5691 /* Check to see if DECL is a local variable in a context
5692 where that is forbidden. */
5693 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
5694 && local_variable_p (decl))
5695 {
5696 error_at (id_expression.get_location (),
5697 "local variable %qD may not appear in this context",
5698 decl.get_value ());
5699 return error_mark_node;
5700 }
5701 }
5702
5703 decl = (finish_id_expression
5704 (id_expression, decl, parser->scope,
5705 idk,
5706 parser->integral_constant_expression_p,
5707 parser->allow_non_integral_constant_expression_p,
5708 &parser->non_integral_constant_expression_p,
5709 template_p, done, address_p,
5710 template_arg_p,
5711 &error_msg,
5712 id_expression.get_location ()));
5713 if (error_msg)
5714 cp_parser_error (parser, error_msg);
5715 /* Build a location for an id-expression of the form:
5716 ::ns::id
5717 ~~~~~~^~
5718 or:
5719 id
5720 ^~
5721 i.e. from the start of the first token to the end of the final
5722 token, with the caret at the start of the unqualified-id. */
5723 location_t caret_loc = get_pure_location (id_expression.get_location ());
5724 location_t start_loc = get_start (id_expr_token->location);
5725 location_t finish_loc = get_finish (id_expression.get_location ());
5726 location_t combined_loc
5727 = make_location (caret_loc, start_loc, finish_loc);
5728
5729 decl.set_location (combined_loc);
5730 return decl;
5731 }
5732
5733 /* Anything else is an error. */
5734 default:
5735 cp_parser_error (parser, "expected primary-expression");
5736 return error_mark_node;
5737 }
5738 }
5739
5740 static inline cp_expr
5741 cp_parser_primary_expression (cp_parser *parser,
5742 bool address_p,
5743 bool cast_p,
5744 bool template_arg_p,
5745 cp_id_kind *idk)
5746 {
5747 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5748 /*decltype*/false, idk);
5749 }
5750
5751 /* Parse an id-expression.
5752
5753 id-expression:
5754 unqualified-id
5755 qualified-id
5756
5757 qualified-id:
5758 :: [opt] nested-name-specifier template [opt] unqualified-id
5759 :: identifier
5760 :: operator-function-id
5761 :: template-id
5762
5763 Return a representation of the unqualified portion of the
5764 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5765 a `::' or nested-name-specifier.
5766
5767 Often, if the id-expression was a qualified-id, the caller will
5768 want to make a SCOPE_REF to represent the qualified-id. This
5769 function does not do this in order to avoid wastefully creating
5770 SCOPE_REFs when they are not required.
5771
5772 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5773 `template' keyword.
5774
5775 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5776 uninstantiated templates.
5777
5778 If *TEMPLATE_P is non-NULL, it is set to true iff the
5779 `template' keyword is used to explicitly indicate that the entity
5780 named is a template.
5781
5782 If DECLARATOR_P is true, the id-expression is appearing as part of
5783 a declarator, rather than as part of an expression. */
5784
5785 static cp_expr
5786 cp_parser_id_expression (cp_parser *parser,
5787 bool template_keyword_p,
5788 bool check_dependency_p,
5789 bool *template_p,
5790 bool declarator_p,
5791 bool optional_p)
5792 {
5793 bool global_scope_p;
5794 bool nested_name_specifier_p;
5795
5796 /* Assume the `template' keyword was not used. */
5797 if (template_p)
5798 *template_p = template_keyword_p;
5799
5800 /* Look for the optional `::' operator. */
5801 global_scope_p
5802 = (!template_keyword_p
5803 && (cp_parser_global_scope_opt (parser,
5804 /*current_scope_valid_p=*/false)
5805 != NULL_TREE));
5806
5807 /* Look for the optional nested-name-specifier. */
5808 nested_name_specifier_p
5809 = (cp_parser_nested_name_specifier_opt (parser,
5810 /*typename_keyword_p=*/false,
5811 check_dependency_p,
5812 /*type_p=*/false,
5813 declarator_p,
5814 template_keyword_p)
5815 != NULL_TREE);
5816
5817 /* If there is a nested-name-specifier, then we are looking at
5818 the first qualified-id production. */
5819 if (nested_name_specifier_p)
5820 {
5821 tree saved_scope;
5822 tree saved_object_scope;
5823 tree saved_qualifying_scope;
5824 cp_expr unqualified_id;
5825 bool is_template;
5826
5827 /* See if the next token is the `template' keyword. */
5828 if (!template_p)
5829 template_p = &is_template;
5830 *template_p = cp_parser_optional_template_keyword (parser);
5831 /* Name lookup we do during the processing of the
5832 unqualified-id might obliterate SCOPE. */
5833 saved_scope = parser->scope;
5834 saved_object_scope = parser->object_scope;
5835 saved_qualifying_scope = parser->qualifying_scope;
5836 /* Process the final unqualified-id. */
5837 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5838 check_dependency_p,
5839 declarator_p,
5840 /*optional_p=*/false);
5841 /* Restore the SAVED_SCOPE for our caller. */
5842 parser->scope = saved_scope;
5843 parser->object_scope = saved_object_scope;
5844 parser->qualifying_scope = saved_qualifying_scope;
5845
5846 return unqualified_id;
5847 }
5848 /* Otherwise, if we are in global scope, then we are looking at one
5849 of the other qualified-id productions. */
5850 else if (global_scope_p)
5851 {
5852 cp_token *token;
5853 tree id;
5854
5855 /* Peek at the next token. */
5856 token = cp_lexer_peek_token (parser->lexer);
5857
5858 /* If it's an identifier, and the next token is not a "<", then
5859 we can avoid the template-id case. This is an optimization
5860 for this common case. */
5861 if (token->type == CPP_NAME
5862 && !cp_parser_nth_token_starts_template_argument_list_p
5863 (parser, 2))
5864 return cp_parser_identifier (parser);
5865
5866 cp_parser_parse_tentatively (parser);
5867 /* Try a template-id. */
5868 id = cp_parser_template_id (parser,
5869 /*template_keyword_p=*/false,
5870 /*check_dependency_p=*/true,
5871 none_type,
5872 declarator_p);
5873 /* If that worked, we're done. */
5874 if (cp_parser_parse_definitely (parser))
5875 return id;
5876
5877 /* Peek at the next token. (Changes in the token buffer may
5878 have invalidated the pointer obtained above.) */
5879 token = cp_lexer_peek_token (parser->lexer);
5880
5881 switch (token->type)
5882 {
5883 case CPP_NAME:
5884 return cp_parser_identifier (parser);
5885
5886 case CPP_KEYWORD:
5887 if (token->keyword == RID_OPERATOR)
5888 return cp_parser_operator_function_id (parser);
5889 /* Fall through. */
5890
5891 default:
5892 cp_parser_error (parser, "expected id-expression");
5893 return error_mark_node;
5894 }
5895 }
5896 else
5897 return cp_parser_unqualified_id (parser, template_keyword_p,
5898 /*check_dependency_p=*/true,
5899 declarator_p,
5900 optional_p);
5901 }
5902
5903 /* Parse an unqualified-id.
5904
5905 unqualified-id:
5906 identifier
5907 operator-function-id
5908 conversion-function-id
5909 ~ class-name
5910 template-id
5911
5912 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5913 keyword, in a construct like `A::template ...'.
5914
5915 Returns a representation of unqualified-id. For the `identifier'
5916 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5917 production a BIT_NOT_EXPR is returned; the operand of the
5918 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5919 other productions, see the documentation accompanying the
5920 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5921 names are looked up in uninstantiated templates. If DECLARATOR_P
5922 is true, the unqualified-id is appearing as part of a declarator,
5923 rather than as part of an expression. */
5924
5925 static cp_expr
5926 cp_parser_unqualified_id (cp_parser* parser,
5927 bool template_keyword_p,
5928 bool check_dependency_p,
5929 bool declarator_p,
5930 bool optional_p)
5931 {
5932 cp_token *token;
5933
5934 /* Peek at the next token. */
5935 token = cp_lexer_peek_token (parser->lexer);
5936
5937 switch ((int) token->type)
5938 {
5939 case CPP_NAME:
5940 {
5941 tree id;
5942
5943 /* We don't know yet whether or not this will be a
5944 template-id. */
5945 cp_parser_parse_tentatively (parser);
5946 /* Try a template-id. */
5947 id = cp_parser_template_id (parser, template_keyword_p,
5948 check_dependency_p,
5949 none_type,
5950 declarator_p);
5951 /* If it worked, we're done. */
5952 if (cp_parser_parse_definitely (parser))
5953 return id;
5954 /* Otherwise, it's an ordinary identifier. */
5955 return cp_parser_identifier (parser);
5956 }
5957
5958 case CPP_TEMPLATE_ID:
5959 return cp_parser_template_id (parser, template_keyword_p,
5960 check_dependency_p,
5961 none_type,
5962 declarator_p);
5963
5964 case CPP_COMPL:
5965 {
5966 tree type_decl;
5967 tree qualifying_scope;
5968 tree object_scope;
5969 tree scope;
5970 bool done;
5971 location_t tilde_loc = token->location;
5972
5973 /* Consume the `~' token. */
5974 cp_lexer_consume_token (parser->lexer);
5975 /* Parse the class-name. The standard, as written, seems to
5976 say that:
5977
5978 template <typename T> struct S { ~S (); };
5979 template <typename T> S<T>::~S() {}
5980
5981 is invalid, since `~' must be followed by a class-name, but
5982 `S<T>' is dependent, and so not known to be a class.
5983 That's not right; we need to look in uninstantiated
5984 templates. A further complication arises from:
5985
5986 template <typename T> void f(T t) {
5987 t.T::~T();
5988 }
5989
5990 Here, it is not possible to look up `T' in the scope of `T'
5991 itself. We must look in both the current scope, and the
5992 scope of the containing complete expression.
5993
5994 Yet another issue is:
5995
5996 struct S {
5997 int S;
5998 ~S();
5999 };
6000
6001 S::~S() {}
6002
6003 The standard does not seem to say that the `S' in `~S'
6004 should refer to the type `S' and not the data member
6005 `S::S'. */
6006
6007 /* DR 244 says that we look up the name after the "~" in the
6008 same scope as we looked up the qualifying name. That idea
6009 isn't fully worked out; it's more complicated than that. */
6010 scope = parser->scope;
6011 object_scope = parser->object_scope;
6012 qualifying_scope = parser->qualifying_scope;
6013
6014 /* Check for invalid scopes. */
6015 if (scope == error_mark_node)
6016 {
6017 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6018 cp_lexer_consume_token (parser->lexer);
6019 return error_mark_node;
6020 }
6021 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6022 {
6023 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6024 error_at (token->location,
6025 "scope %qT before %<~%> is not a class-name",
6026 scope);
6027 cp_parser_simulate_error (parser);
6028 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6029 cp_lexer_consume_token (parser->lexer);
6030 return error_mark_node;
6031 }
6032 gcc_assert (!scope || TYPE_P (scope));
6033
6034 token = cp_lexer_peek_token (parser->lexer);
6035
6036 /* Create a location with caret == start at the tilde,
6037 finishing at the end of the peeked token, e.g:
6038 ~token
6039 ^~~~~~. */
6040 location_t loc
6041 = make_location (tilde_loc, tilde_loc, token->location);
6042
6043 /* If the name is of the form "X::~X" it's OK even if X is a
6044 typedef. */
6045
6046 if (scope
6047 && token->type == CPP_NAME
6048 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6049 != CPP_LESS)
6050 && (token->u.value == TYPE_IDENTIFIER (scope)
6051 || (CLASS_TYPE_P (scope)
6052 && constructor_name_p (token->u.value, scope))))
6053 {
6054 cp_lexer_consume_token (parser->lexer);
6055 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6056 }
6057
6058 /* ~auto means the destructor of whatever the object is. */
6059 if (cp_parser_is_keyword (token, RID_AUTO))
6060 {
6061 if (cxx_dialect < cxx14)
6062 pedwarn (loc, 0,
6063 "%<~auto%> only available with "
6064 "%<-std=c++14%> or %<-std=gnu++14%>");
6065 cp_lexer_consume_token (parser->lexer);
6066 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6067 }
6068
6069 /* If there was an explicit qualification (S::~T), first look
6070 in the scope given by the qualification (i.e., S).
6071
6072 Note: in the calls to cp_parser_class_name below we pass
6073 typename_type so that lookup finds the injected-class-name
6074 rather than the constructor. */
6075 done = false;
6076 type_decl = NULL_TREE;
6077 if (scope)
6078 {
6079 cp_parser_parse_tentatively (parser);
6080 type_decl = cp_parser_class_name (parser,
6081 /*typename_keyword_p=*/false,
6082 /*template_keyword_p=*/false,
6083 typename_type,
6084 /*check_dependency=*/false,
6085 /*class_head_p=*/false,
6086 declarator_p);
6087 if (cp_parser_parse_definitely (parser))
6088 done = true;
6089 }
6090 /* In "N::S::~S", look in "N" as well. */
6091 if (!done && scope && qualifying_scope)
6092 {
6093 cp_parser_parse_tentatively (parser);
6094 parser->scope = qualifying_scope;
6095 parser->object_scope = NULL_TREE;
6096 parser->qualifying_scope = NULL_TREE;
6097 type_decl
6098 = cp_parser_class_name (parser,
6099 /*typename_keyword_p=*/false,
6100 /*template_keyword_p=*/false,
6101 typename_type,
6102 /*check_dependency=*/false,
6103 /*class_head_p=*/false,
6104 declarator_p);
6105 if (cp_parser_parse_definitely (parser))
6106 done = true;
6107 }
6108 /* In "p->S::~T", look in the scope given by "*p" as well. */
6109 else if (!done && object_scope)
6110 {
6111 cp_parser_parse_tentatively (parser);
6112 parser->scope = object_scope;
6113 parser->object_scope = NULL_TREE;
6114 parser->qualifying_scope = NULL_TREE;
6115 type_decl
6116 = cp_parser_class_name (parser,
6117 /*typename_keyword_p=*/false,
6118 /*template_keyword_p=*/false,
6119 typename_type,
6120 /*check_dependency=*/false,
6121 /*class_head_p=*/false,
6122 declarator_p);
6123 if (cp_parser_parse_definitely (parser))
6124 done = true;
6125 }
6126 /* Look in the surrounding context. */
6127 if (!done)
6128 {
6129 parser->scope = NULL_TREE;
6130 parser->object_scope = NULL_TREE;
6131 parser->qualifying_scope = NULL_TREE;
6132 if (processing_template_decl)
6133 cp_parser_parse_tentatively (parser);
6134 type_decl
6135 = cp_parser_class_name (parser,
6136 /*typename_keyword_p=*/false,
6137 /*template_keyword_p=*/false,
6138 typename_type,
6139 /*check_dependency=*/false,
6140 /*class_head_p=*/false,
6141 declarator_p);
6142 if (processing_template_decl
6143 && ! cp_parser_parse_definitely (parser))
6144 {
6145 /* We couldn't find a type with this name. If we're parsing
6146 tentatively, fail and try something else. */
6147 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6148 {
6149 cp_parser_simulate_error (parser);
6150 return error_mark_node;
6151 }
6152 /* Otherwise, accept it and check for a match at instantiation
6153 time. */
6154 type_decl = cp_parser_identifier (parser);
6155 if (type_decl != error_mark_node)
6156 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6157 return type_decl;
6158 }
6159 }
6160 /* If an error occurred, assume that the name of the
6161 destructor is the same as the name of the qualifying
6162 class. That allows us to keep parsing after running
6163 into ill-formed destructor names. */
6164 if (type_decl == error_mark_node && scope)
6165 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6166 else if (type_decl == error_mark_node)
6167 return error_mark_node;
6168
6169 /* Check that destructor name and scope match. */
6170 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6171 {
6172 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6173 error_at (loc,
6174 "declaration of %<~%T%> as member of %qT",
6175 type_decl, scope);
6176 cp_parser_simulate_error (parser);
6177 return error_mark_node;
6178 }
6179
6180 /* [class.dtor]
6181
6182 A typedef-name that names a class shall not be used as the
6183 identifier in the declarator for a destructor declaration. */
6184 if (declarator_p
6185 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6186 && !DECL_SELF_REFERENCE_P (type_decl)
6187 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6188 error_at (loc,
6189 "typedef-name %qD used as destructor declarator",
6190 type_decl);
6191
6192 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6193 }
6194
6195 case CPP_KEYWORD:
6196 if (token->keyword == RID_OPERATOR)
6197 {
6198 cp_expr id;
6199
6200 /* This could be a template-id, so we try that first. */
6201 cp_parser_parse_tentatively (parser);
6202 /* Try a template-id. */
6203 id = cp_parser_template_id (parser, template_keyword_p,
6204 /*check_dependency_p=*/true,
6205 none_type,
6206 declarator_p);
6207 /* If that worked, we're done. */
6208 if (cp_parser_parse_definitely (parser))
6209 return id;
6210 /* We still don't know whether we're looking at an
6211 operator-function-id or a conversion-function-id. */
6212 cp_parser_parse_tentatively (parser);
6213 /* Try an operator-function-id. */
6214 id = cp_parser_operator_function_id (parser);
6215 /* If that didn't work, try a conversion-function-id. */
6216 if (!cp_parser_parse_definitely (parser))
6217 id = cp_parser_conversion_function_id (parser);
6218
6219 return id;
6220 }
6221 /* Fall through. */
6222
6223 default:
6224 if (optional_p)
6225 return NULL_TREE;
6226 cp_parser_error (parser, "expected unqualified-id");
6227 return error_mark_node;
6228 }
6229 }
6230
6231 /* Parse an (optional) nested-name-specifier.
6232
6233 nested-name-specifier: [C++98]
6234 class-or-namespace-name :: nested-name-specifier [opt]
6235 class-or-namespace-name :: template nested-name-specifier [opt]
6236
6237 nested-name-specifier: [C++0x]
6238 type-name ::
6239 namespace-name ::
6240 nested-name-specifier identifier ::
6241 nested-name-specifier template [opt] simple-template-id ::
6242
6243 PARSER->SCOPE should be set appropriately before this function is
6244 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6245 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6246 in name lookups.
6247
6248 Sets PARSER->SCOPE to the class (TYPE) or namespace
6249 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6250 it unchanged if there is no nested-name-specifier. Returns the new
6251 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6252
6253 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6254 part of a declaration and/or decl-specifier. */
6255
6256 static tree
6257 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6258 bool typename_keyword_p,
6259 bool check_dependency_p,
6260 bool type_p,
6261 bool is_declaration,
6262 bool template_keyword_p /* = false */)
6263 {
6264 bool success = false;
6265 cp_token_position start = 0;
6266 cp_token *token;
6267
6268 /* Remember where the nested-name-specifier starts. */
6269 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6270 {
6271 start = cp_lexer_token_position (parser->lexer, false);
6272 push_deferring_access_checks (dk_deferred);
6273 }
6274
6275 while (true)
6276 {
6277 tree new_scope;
6278 tree old_scope;
6279 tree saved_qualifying_scope;
6280
6281 /* Spot cases that cannot be the beginning of a
6282 nested-name-specifier. */
6283 token = cp_lexer_peek_token (parser->lexer);
6284
6285 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6286 the already parsed nested-name-specifier. */
6287 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6288 {
6289 /* Grab the nested-name-specifier and continue the loop. */
6290 cp_parser_pre_parsed_nested_name_specifier (parser);
6291 /* If we originally encountered this nested-name-specifier
6292 with IS_DECLARATION set to false, we will not have
6293 resolved TYPENAME_TYPEs, so we must do so here. */
6294 if (is_declaration
6295 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6296 {
6297 new_scope = resolve_typename_type (parser->scope,
6298 /*only_current_p=*/false);
6299 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6300 parser->scope = new_scope;
6301 }
6302 success = true;
6303 continue;
6304 }
6305
6306 /* Spot cases that cannot be the beginning of a
6307 nested-name-specifier. On the second and subsequent times
6308 through the loop, we look for the `template' keyword. */
6309 if (success && token->keyword == RID_TEMPLATE)
6310 ;
6311 /* A template-id can start a nested-name-specifier. */
6312 else if (token->type == CPP_TEMPLATE_ID)
6313 ;
6314 /* DR 743: decltype can be used in a nested-name-specifier. */
6315 else if (token_is_decltype (token))
6316 ;
6317 else
6318 {
6319 /* If the next token is not an identifier, then it is
6320 definitely not a type-name or namespace-name. */
6321 if (token->type != CPP_NAME)
6322 break;
6323 /* If the following token is neither a `<' (to begin a
6324 template-id), nor a `::', then we are not looking at a
6325 nested-name-specifier. */
6326 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6327
6328 if (token->type == CPP_COLON
6329 && parser->colon_corrects_to_scope_p
6330 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6331 {
6332 gcc_rich_location richloc (token->location);
6333 richloc.add_fixit_replace ("::");
6334 error_at (&richloc,
6335 "found %<:%> in nested-name-specifier, "
6336 "expected %<::%>");
6337 token->type = CPP_SCOPE;
6338 }
6339
6340 if (token->type != CPP_SCOPE
6341 && !cp_parser_nth_token_starts_template_argument_list_p
6342 (parser, 2))
6343 break;
6344 }
6345
6346 /* The nested-name-specifier is optional, so we parse
6347 tentatively. */
6348 cp_parser_parse_tentatively (parser);
6349
6350 /* Look for the optional `template' keyword, if this isn't the
6351 first time through the loop. */
6352 if (success)
6353 template_keyword_p = cp_parser_optional_template_keyword (parser);
6354
6355 /* Save the old scope since the name lookup we are about to do
6356 might destroy it. */
6357 old_scope = parser->scope;
6358 saved_qualifying_scope = parser->qualifying_scope;
6359 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6360 look up names in "X<T>::I" in order to determine that "Y" is
6361 a template. So, if we have a typename at this point, we make
6362 an effort to look through it. */
6363 if (is_declaration
6364 && !typename_keyword_p
6365 && parser->scope
6366 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6367 parser->scope = resolve_typename_type (parser->scope,
6368 /*only_current_p=*/false);
6369 /* Parse the qualifying entity. */
6370 new_scope
6371 = cp_parser_qualifying_entity (parser,
6372 typename_keyword_p,
6373 template_keyword_p,
6374 check_dependency_p,
6375 type_p,
6376 is_declaration);
6377 /* Look for the `::' token. */
6378 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6379
6380 /* If we found what we wanted, we keep going; otherwise, we're
6381 done. */
6382 if (!cp_parser_parse_definitely (parser))
6383 {
6384 bool error_p = false;
6385
6386 /* Restore the OLD_SCOPE since it was valid before the
6387 failed attempt at finding the last
6388 class-or-namespace-name. */
6389 parser->scope = old_scope;
6390 parser->qualifying_scope = saved_qualifying_scope;
6391
6392 /* If the next token is a decltype, and the one after that is a
6393 `::', then the decltype has failed to resolve to a class or
6394 enumeration type. Give this error even when parsing
6395 tentatively since it can't possibly be valid--and we're going
6396 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6397 won't get another chance.*/
6398 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6399 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6400 == CPP_SCOPE))
6401 {
6402 token = cp_lexer_consume_token (parser->lexer);
6403 error_at (token->location, "%<decltype%> evaluates to %qT, "
6404 "which is not a class or enumeration type",
6405 token->u.tree_check_value->value);
6406 parser->scope = error_mark_node;
6407 error_p = true;
6408 /* As below. */
6409 success = true;
6410 cp_lexer_consume_token (parser->lexer);
6411 }
6412
6413 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6414 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6415 {
6416 /* If we have a non-type template-id followed by ::, it can't
6417 possibly be valid. */
6418 token = cp_lexer_peek_token (parser->lexer);
6419 tree tid = token->u.tree_check_value->value;
6420 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6421 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6422 {
6423 tree tmpl = NULL_TREE;
6424 if (is_overloaded_fn (tid))
6425 {
6426 tree fns = get_fns (tid);
6427 if (OVL_SINGLE_P (fns))
6428 tmpl = OVL_FIRST (fns);
6429 error_at (token->location, "function template-id %qD "
6430 "in nested-name-specifier", tid);
6431 }
6432 else
6433 {
6434 /* Variable template. */
6435 tmpl = TREE_OPERAND (tid, 0);
6436 gcc_assert (variable_template_p (tmpl));
6437 error_at (token->location, "variable template-id %qD "
6438 "in nested-name-specifier", tid);
6439 }
6440 if (tmpl)
6441 inform (DECL_SOURCE_LOCATION (tmpl),
6442 "%qD declared here", tmpl);
6443
6444 parser->scope = error_mark_node;
6445 error_p = true;
6446 /* As below. */
6447 success = true;
6448 cp_lexer_consume_token (parser->lexer);
6449 cp_lexer_consume_token (parser->lexer);
6450 }
6451 }
6452
6453 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6454 break;
6455 /* If the next token is an identifier, and the one after
6456 that is a `::', then any valid interpretation would have
6457 found a class-or-namespace-name. */
6458 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6459 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6460 == CPP_SCOPE)
6461 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6462 != CPP_COMPL))
6463 {
6464 token = cp_lexer_consume_token (parser->lexer);
6465 if (!error_p)
6466 {
6467 if (!token->error_reported)
6468 {
6469 tree decl;
6470 tree ambiguous_decls;
6471
6472 decl = cp_parser_lookup_name (parser, token->u.value,
6473 none_type,
6474 /*is_template=*/false,
6475 /*is_namespace=*/false,
6476 /*check_dependency=*/true,
6477 &ambiguous_decls,
6478 token->location);
6479 if (TREE_CODE (decl) == TEMPLATE_DECL)
6480 error_at (token->location,
6481 "%qD used without template arguments",
6482 decl);
6483 else if (ambiguous_decls)
6484 {
6485 // cp_parser_lookup_name has the same diagnostic,
6486 // thus make sure to emit it at most once.
6487 if (cp_parser_uncommitted_to_tentative_parse_p
6488 (parser))
6489 {
6490 error_at (token->location,
6491 "reference to %qD is ambiguous",
6492 token->u.value);
6493 print_candidates (ambiguous_decls);
6494 }
6495 decl = error_mark_node;
6496 }
6497 else
6498 {
6499 if (cxx_dialect != cxx98)
6500 cp_parser_name_lookup_error
6501 (parser, token->u.value, decl, NLE_NOT_CXX98,
6502 token->location);
6503 else
6504 cp_parser_name_lookup_error
6505 (parser, token->u.value, decl, NLE_CXX98,
6506 token->location);
6507 }
6508 }
6509 parser->scope = error_mark_node;
6510 error_p = true;
6511 /* Treat this as a successful nested-name-specifier
6512 due to:
6513
6514 [basic.lookup.qual]
6515
6516 If the name found is not a class-name (clause
6517 _class_) or namespace-name (_namespace.def_), the
6518 program is ill-formed. */
6519 success = true;
6520 }
6521 cp_lexer_consume_token (parser->lexer);
6522 }
6523 break;
6524 }
6525 /* We've found one valid nested-name-specifier. */
6526 success = true;
6527 /* Name lookup always gives us a DECL. */
6528 if (TREE_CODE (new_scope) == TYPE_DECL)
6529 new_scope = TREE_TYPE (new_scope);
6530 /* Uses of "template" must be followed by actual templates. */
6531 if (template_keyword_p
6532 && !(CLASS_TYPE_P (new_scope)
6533 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6534 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6535 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6536 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6537 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6538 == TEMPLATE_ID_EXPR)))
6539 permerror (input_location, TYPE_P (new_scope)
6540 ? G_("%qT is not a template")
6541 : G_("%qD is not a template"),
6542 new_scope);
6543 /* If it is a class scope, try to complete it; we are about to
6544 be looking up names inside the class. */
6545 if (TYPE_P (new_scope)
6546 /* Since checking types for dependency can be expensive,
6547 avoid doing it if the type is already complete. */
6548 && !COMPLETE_TYPE_P (new_scope)
6549 /* Do not try to complete dependent types. */
6550 && !dependent_type_p (new_scope))
6551 {
6552 new_scope = complete_type (new_scope);
6553 /* If it is a typedef to current class, use the current
6554 class instead, as the typedef won't have any names inside
6555 it yet. */
6556 if (!COMPLETE_TYPE_P (new_scope)
6557 && currently_open_class (new_scope))
6558 new_scope = TYPE_MAIN_VARIANT (new_scope);
6559 }
6560 /* Make sure we look in the right scope the next time through
6561 the loop. */
6562 parser->scope = new_scope;
6563 }
6564
6565 /* If parsing tentatively, replace the sequence of tokens that makes
6566 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6567 token. That way, should we re-parse the token stream, we will
6568 not have to repeat the effort required to do the parse, nor will
6569 we issue duplicate error messages. */
6570 if (success && start)
6571 {
6572 cp_token *token;
6573
6574 token = cp_lexer_token_at (parser->lexer, start);
6575 /* Reset the contents of the START token. */
6576 token->type = CPP_NESTED_NAME_SPECIFIER;
6577 /* Retrieve any deferred checks. Do not pop this access checks yet
6578 so the memory will not be reclaimed during token replacing below. */
6579 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6580 token->u.tree_check_value->value = parser->scope;
6581 token->u.tree_check_value->checks = get_deferred_access_checks ();
6582 token->u.tree_check_value->qualifying_scope =
6583 parser->qualifying_scope;
6584 token->keyword = RID_MAX;
6585
6586 /* Purge all subsequent tokens. */
6587 cp_lexer_purge_tokens_after (parser->lexer, start);
6588 }
6589
6590 if (start)
6591 pop_to_parent_deferring_access_checks ();
6592
6593 return success ? parser->scope : NULL_TREE;
6594 }
6595
6596 /* Parse a nested-name-specifier. See
6597 cp_parser_nested_name_specifier_opt for details. This function
6598 behaves identically, except that it will an issue an error if no
6599 nested-name-specifier is present. */
6600
6601 static tree
6602 cp_parser_nested_name_specifier (cp_parser *parser,
6603 bool typename_keyword_p,
6604 bool check_dependency_p,
6605 bool type_p,
6606 bool is_declaration)
6607 {
6608 tree scope;
6609
6610 /* Look for the nested-name-specifier. */
6611 scope = cp_parser_nested_name_specifier_opt (parser,
6612 typename_keyword_p,
6613 check_dependency_p,
6614 type_p,
6615 is_declaration);
6616 /* If it was not present, issue an error message. */
6617 if (!scope)
6618 {
6619 cp_parser_error (parser, "expected nested-name-specifier");
6620 parser->scope = NULL_TREE;
6621 }
6622
6623 return scope;
6624 }
6625
6626 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6627 this is either a class-name or a namespace-name (which corresponds
6628 to the class-or-namespace-name production in the grammar). For
6629 C++0x, it can also be a type-name that refers to an enumeration
6630 type or a simple-template-id.
6631
6632 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6633 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6634 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6635 TYPE_P is TRUE iff the next name should be taken as a class-name,
6636 even the same name is declared to be another entity in the same
6637 scope.
6638
6639 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6640 specified by the class-or-namespace-name. If neither is found the
6641 ERROR_MARK_NODE is returned. */
6642
6643 static tree
6644 cp_parser_qualifying_entity (cp_parser *parser,
6645 bool typename_keyword_p,
6646 bool template_keyword_p,
6647 bool check_dependency_p,
6648 bool type_p,
6649 bool is_declaration)
6650 {
6651 tree saved_scope;
6652 tree saved_qualifying_scope;
6653 tree saved_object_scope;
6654 tree scope;
6655 bool only_class_p;
6656 bool successful_parse_p;
6657
6658 /* DR 743: decltype can appear in a nested-name-specifier. */
6659 if (cp_lexer_next_token_is_decltype (parser->lexer))
6660 {
6661 scope = cp_parser_decltype (parser);
6662 if (TREE_CODE (scope) != ENUMERAL_TYPE
6663 && !MAYBE_CLASS_TYPE_P (scope))
6664 {
6665 cp_parser_simulate_error (parser);
6666 return error_mark_node;
6667 }
6668 if (TYPE_NAME (scope))
6669 scope = TYPE_NAME (scope);
6670 return scope;
6671 }
6672
6673 /* Before we try to parse the class-name, we must save away the
6674 current PARSER->SCOPE since cp_parser_class_name will destroy
6675 it. */
6676 saved_scope = parser->scope;
6677 saved_qualifying_scope = parser->qualifying_scope;
6678 saved_object_scope = parser->object_scope;
6679 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6680 there is no need to look for a namespace-name. */
6681 only_class_p = template_keyword_p
6682 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6683 if (!only_class_p)
6684 cp_parser_parse_tentatively (parser);
6685 scope = cp_parser_class_name (parser,
6686 typename_keyword_p,
6687 template_keyword_p,
6688 type_p ? class_type : none_type,
6689 check_dependency_p,
6690 /*class_head_p=*/false,
6691 is_declaration,
6692 /*enum_ok=*/cxx_dialect > cxx98);
6693 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6694 /* If that didn't work, try for a namespace-name. */
6695 if (!only_class_p && !successful_parse_p)
6696 {
6697 /* Restore the saved scope. */
6698 parser->scope = saved_scope;
6699 parser->qualifying_scope = saved_qualifying_scope;
6700 parser->object_scope = saved_object_scope;
6701 /* If we are not looking at an identifier followed by the scope
6702 resolution operator, then this is not part of a
6703 nested-name-specifier. (Note that this function is only used
6704 to parse the components of a nested-name-specifier.) */
6705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6706 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6707 return error_mark_node;
6708 scope = cp_parser_namespace_name (parser);
6709 }
6710
6711 return scope;
6712 }
6713
6714 /* Return true if we are looking at a compound-literal, false otherwise. */
6715
6716 static bool
6717 cp_parser_compound_literal_p (cp_parser *parser)
6718 {
6719 cp_lexer_save_tokens (parser->lexer);
6720
6721 /* Skip tokens until the next token is a closing parenthesis.
6722 If we find the closing `)', and the next token is a `{', then
6723 we are looking at a compound-literal. */
6724 bool compound_literal_p
6725 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6726 /*consume_paren=*/true)
6727 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6728
6729 /* Roll back the tokens we skipped. */
6730 cp_lexer_rollback_tokens (parser->lexer);
6731
6732 return compound_literal_p;
6733 }
6734
6735 /* Return true if EXPR is the integer constant zero or a complex constant
6736 of zero, without any folding, but ignoring location wrappers. */
6737
6738 bool
6739 literal_integer_zerop (const_tree expr)
6740 {
6741 return (location_wrapper_p (expr)
6742 && integer_zerop (TREE_OPERAND (expr, 0)));
6743 }
6744
6745 /* Parse a postfix-expression.
6746
6747 postfix-expression:
6748 primary-expression
6749 postfix-expression [ expression ]
6750 postfix-expression ( expression-list [opt] )
6751 simple-type-specifier ( expression-list [opt] )
6752 typename :: [opt] nested-name-specifier identifier
6753 ( expression-list [opt] )
6754 typename :: [opt] nested-name-specifier template [opt] template-id
6755 ( expression-list [opt] )
6756 postfix-expression . template [opt] id-expression
6757 postfix-expression -> template [opt] id-expression
6758 postfix-expression . pseudo-destructor-name
6759 postfix-expression -> pseudo-destructor-name
6760 postfix-expression ++
6761 postfix-expression --
6762 dynamic_cast < type-id > ( expression )
6763 static_cast < type-id > ( expression )
6764 reinterpret_cast < type-id > ( expression )
6765 const_cast < type-id > ( expression )
6766 typeid ( expression )
6767 typeid ( type-id )
6768
6769 GNU Extension:
6770
6771 postfix-expression:
6772 ( type-id ) { initializer-list , [opt] }
6773
6774 This extension is a GNU version of the C99 compound-literal
6775 construct. (The C99 grammar uses `type-name' instead of `type-id',
6776 but they are essentially the same concept.)
6777
6778 If ADDRESS_P is true, the postfix expression is the operand of the
6779 `&' operator. CAST_P is true if this expression is the target of a
6780 cast.
6781
6782 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6783 class member access expressions [expr.ref].
6784
6785 Returns a representation of the expression. */
6786
6787 static cp_expr
6788 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6789 bool member_access_only_p, bool decltype_p,
6790 cp_id_kind * pidk_return)
6791 {
6792 cp_token *token;
6793 location_t loc;
6794 enum rid keyword;
6795 cp_id_kind idk = CP_ID_KIND_NONE;
6796 cp_expr postfix_expression = NULL_TREE;
6797 bool is_member_access = false;
6798
6799 /* Peek at the next token. */
6800 token = cp_lexer_peek_token (parser->lexer);
6801 loc = token->location;
6802 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6803
6804 /* Some of the productions are determined by keywords. */
6805 keyword = token->keyword;
6806 switch (keyword)
6807 {
6808 case RID_DYNCAST:
6809 case RID_STATCAST:
6810 case RID_REINTCAST:
6811 case RID_CONSTCAST:
6812 {
6813 tree type;
6814 cp_expr expression;
6815 const char *saved_message;
6816 bool saved_in_type_id_in_expr_p;
6817
6818 /* All of these can be handled in the same way from the point
6819 of view of parsing. Begin by consuming the token
6820 identifying the cast. */
6821 cp_lexer_consume_token (parser->lexer);
6822
6823 /* New types cannot be defined in the cast. */
6824 saved_message = parser->type_definition_forbidden_message;
6825 parser->type_definition_forbidden_message
6826 = G_("types may not be defined in casts");
6827
6828 /* Look for the opening `<'. */
6829 cp_parser_require (parser, CPP_LESS, RT_LESS);
6830 /* Parse the type to which we are casting. */
6831 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6832 parser->in_type_id_in_expr_p = true;
6833 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
6834 NULL);
6835 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6836 /* Look for the closing `>'. */
6837 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6838 /* Restore the old message. */
6839 parser->type_definition_forbidden_message = saved_message;
6840
6841 bool saved_greater_than_is_operator_p
6842 = parser->greater_than_is_operator_p;
6843 parser->greater_than_is_operator_p = true;
6844
6845 /* And the expression which is being cast. */
6846 matching_parens parens;
6847 parens.require_open (parser);
6848 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6849 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6850 RT_CLOSE_PAREN);
6851 location_t end_loc = close_paren ?
6852 close_paren->location : UNKNOWN_LOCATION;
6853
6854 parser->greater_than_is_operator_p
6855 = saved_greater_than_is_operator_p;
6856
6857 /* Only type conversions to integral or enumeration types
6858 can be used in constant-expressions. */
6859 if (!cast_valid_in_integral_constant_expression_p (type)
6860 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6861 {
6862 postfix_expression = error_mark_node;
6863 break;
6864 }
6865
6866 switch (keyword)
6867 {
6868 case RID_DYNCAST:
6869 postfix_expression
6870 = build_dynamic_cast (type, expression, tf_warning_or_error);
6871 break;
6872 case RID_STATCAST:
6873 postfix_expression
6874 = build_static_cast (type, expression, tf_warning_or_error);
6875 break;
6876 case RID_REINTCAST:
6877 postfix_expression
6878 = build_reinterpret_cast (type, expression,
6879 tf_warning_or_error);
6880 break;
6881 case RID_CONSTCAST:
6882 postfix_expression
6883 = build_const_cast (type, expression, tf_warning_or_error);
6884 break;
6885 default:
6886 gcc_unreachable ();
6887 }
6888
6889 /* Construct a location e.g. :
6890 reinterpret_cast <int *> (expr)
6891 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6892 ranging from the start of the "*_cast" token to the final closing
6893 paren, with the caret at the start. */
6894 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6895 postfix_expression.set_location (cp_cast_loc);
6896 }
6897 break;
6898
6899 case RID_TYPEID:
6900 {
6901 tree type;
6902 const char *saved_message;
6903 bool saved_in_type_id_in_expr_p;
6904
6905 /* Consume the `typeid' token. */
6906 cp_lexer_consume_token (parser->lexer);
6907 /* Look for the `(' token. */
6908 matching_parens parens;
6909 parens.require_open (parser);
6910 /* Types cannot be defined in a `typeid' expression. */
6911 saved_message = parser->type_definition_forbidden_message;
6912 parser->type_definition_forbidden_message
6913 = G_("types may not be defined in a %<typeid%> expression");
6914 /* We can't be sure yet whether we're looking at a type-id or an
6915 expression. */
6916 cp_parser_parse_tentatively (parser);
6917 /* Try a type-id first. */
6918 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6919 parser->in_type_id_in_expr_p = true;
6920 type = cp_parser_type_id (parser);
6921 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6922 /* Look for the `)' token. Otherwise, we can't be sure that
6923 we're not looking at an expression: consider `typeid (int
6924 (3))', for example. */
6925 cp_token *close_paren = parens.require_close (parser);
6926 /* If all went well, simply lookup the type-id. */
6927 if (cp_parser_parse_definitely (parser))
6928 postfix_expression = get_typeid (type, tf_warning_or_error);
6929 /* Otherwise, fall back to the expression variant. */
6930 else
6931 {
6932 tree expression;
6933
6934 /* Look for an expression. */
6935 expression = cp_parser_expression (parser, & idk);
6936 /* Compute its typeid. */
6937 postfix_expression = build_typeid (expression, tf_warning_or_error);
6938 /* Look for the `)' token. */
6939 close_paren = parens.require_close (parser);
6940 }
6941 /* Restore the saved message. */
6942 parser->type_definition_forbidden_message = saved_message;
6943 /* `typeid' may not appear in an integral constant expression. */
6944 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6945 postfix_expression = error_mark_node;
6946
6947 /* Construct a location e.g. :
6948 typeid (expr)
6949 ^~~~~~~~~~~~~
6950 ranging from the start of the "typeid" token to the final closing
6951 paren, with the caret at the start. */
6952 if (close_paren)
6953 {
6954 location_t typeid_loc
6955 = make_location (start_loc, start_loc, close_paren->location);
6956 postfix_expression.set_location (typeid_loc);
6957 postfix_expression.maybe_add_location_wrapper ();
6958 }
6959 }
6960 break;
6961
6962 case RID_TYPENAME:
6963 {
6964 tree type;
6965 /* The syntax permitted here is the same permitted for an
6966 elaborated-type-specifier. */
6967 ++parser->prevent_constrained_type_specifiers;
6968 type = cp_parser_elaborated_type_specifier (parser,
6969 /*is_friend=*/false,
6970 /*is_declaration=*/false);
6971 --parser->prevent_constrained_type_specifiers;
6972 postfix_expression = cp_parser_functional_cast (parser, type);
6973 }
6974 break;
6975
6976 case RID_ADDRESSOF:
6977 case RID_BUILTIN_SHUFFLE:
6978 case RID_BUILTIN_LAUNDER:
6979 {
6980 vec<tree, va_gc> *vec;
6981 unsigned int i;
6982 tree p;
6983
6984 cp_lexer_consume_token (parser->lexer);
6985 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6986 /*cast_p=*/false, /*allow_expansion_p=*/true,
6987 /*non_constant_p=*/NULL);
6988 if (vec == NULL)
6989 {
6990 postfix_expression = error_mark_node;
6991 break;
6992 }
6993
6994 FOR_EACH_VEC_ELT (*vec, i, p)
6995 mark_exp_read (p);
6996
6997 switch (keyword)
6998 {
6999 case RID_ADDRESSOF:
7000 if (vec->length () == 1)
7001 postfix_expression
7002 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7003 else
7004 {
7005 error_at (loc, "wrong number of arguments to "
7006 "%<__builtin_addressof%>");
7007 postfix_expression = error_mark_node;
7008 }
7009 break;
7010
7011 case RID_BUILTIN_LAUNDER:
7012 if (vec->length () == 1)
7013 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7014 tf_warning_or_error);
7015 else
7016 {
7017 error_at (loc, "wrong number of arguments to "
7018 "%<__builtin_launder%>");
7019 postfix_expression = error_mark_node;
7020 }
7021 break;
7022
7023 case RID_BUILTIN_SHUFFLE:
7024 if (vec->length () == 2)
7025 postfix_expression
7026 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7027 (*vec)[1], tf_warning_or_error);
7028 else if (vec->length () == 3)
7029 postfix_expression
7030 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7031 (*vec)[2], tf_warning_or_error);
7032 else
7033 {
7034 error_at (loc, "wrong number of arguments to "
7035 "%<__builtin_shuffle%>");
7036 postfix_expression = error_mark_node;
7037 }
7038 break;
7039
7040 default:
7041 gcc_unreachable ();
7042 }
7043 break;
7044 }
7045
7046 case RID_BUILTIN_CONVERTVECTOR:
7047 {
7048 tree expression;
7049 tree type;
7050 /* Consume the `__builtin_convertvector' token. */
7051 cp_lexer_consume_token (parser->lexer);
7052 /* Look for the opening `('. */
7053 matching_parens parens;
7054 parens.require_open (parser);
7055 /* Now, parse the assignment-expression. */
7056 expression = cp_parser_assignment_expression (parser);
7057 /* Look for the `,'. */
7058 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7059 location_t type_location
7060 = cp_lexer_peek_token (parser->lexer)->location;
7061 /* Parse the type-id. */
7062 {
7063 type_id_in_expr_sentinel s (parser);
7064 type = cp_parser_type_id (parser);
7065 }
7066 /* Look for the closing `)'. */
7067 parens.require_close (parser);
7068 return cp_build_vec_convert (expression, type_location, type,
7069 tf_warning_or_error);
7070 }
7071
7072 default:
7073 {
7074 tree type;
7075
7076 /* If the next thing is a simple-type-specifier, we may be
7077 looking at a functional cast. We could also be looking at
7078 an id-expression. So, we try the functional cast, and if
7079 that doesn't work we fall back to the primary-expression. */
7080 cp_parser_parse_tentatively (parser);
7081 /* Look for the simple-type-specifier. */
7082 ++parser->prevent_constrained_type_specifiers;
7083 type = cp_parser_simple_type_specifier (parser,
7084 /*decl_specs=*/NULL,
7085 CP_PARSER_FLAGS_NONE);
7086 --parser->prevent_constrained_type_specifiers;
7087 /* Parse the cast itself. */
7088 if (!cp_parser_error_occurred (parser))
7089 postfix_expression
7090 = cp_parser_functional_cast (parser, type);
7091 /* If that worked, we're done. */
7092 if (cp_parser_parse_definitely (parser))
7093 break;
7094
7095 /* If the functional-cast didn't work out, try a
7096 compound-literal. */
7097 if (cp_parser_allow_gnu_extensions_p (parser)
7098 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7099 {
7100 cp_expr initializer = NULL_TREE;
7101
7102 cp_parser_parse_tentatively (parser);
7103
7104 matching_parens parens;
7105 parens.consume_open (parser);
7106
7107 /* Avoid calling cp_parser_type_id pointlessly, see comment
7108 in cp_parser_cast_expression about c++/29234. */
7109 if (!cp_parser_compound_literal_p (parser))
7110 cp_parser_simulate_error (parser);
7111 else
7112 {
7113 /* Parse the type. */
7114 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7115 parser->in_type_id_in_expr_p = true;
7116 type = cp_parser_type_id (parser);
7117 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7118 parens.require_close (parser);
7119 }
7120
7121 /* If things aren't going well, there's no need to
7122 keep going. */
7123 if (!cp_parser_error_occurred (parser))
7124 {
7125 bool non_constant_p;
7126 /* Parse the brace-enclosed initializer list. */
7127 initializer = cp_parser_braced_list (parser,
7128 &non_constant_p);
7129 }
7130 /* If that worked, we're definitely looking at a
7131 compound-literal expression. */
7132 if (cp_parser_parse_definitely (parser))
7133 {
7134 /* Warn the user that a compound literal is not
7135 allowed in standard C++. */
7136 pedwarn (input_location, OPT_Wpedantic,
7137 "ISO C++ forbids compound-literals");
7138 /* For simplicity, we disallow compound literals in
7139 constant-expressions. We could
7140 allow compound literals of integer type, whose
7141 initializer was a constant, in constant
7142 expressions. Permitting that usage, as a further
7143 extension, would not change the meaning of any
7144 currently accepted programs. (Of course, as
7145 compound literals are not part of ISO C++, the
7146 standard has nothing to say.) */
7147 if (cp_parser_non_integral_constant_expression (parser,
7148 NIC_NCC))
7149 {
7150 postfix_expression = error_mark_node;
7151 break;
7152 }
7153 /* Form the representation of the compound-literal. */
7154 postfix_expression
7155 = finish_compound_literal (type, initializer,
7156 tf_warning_or_error, fcl_c99);
7157 postfix_expression.set_location (initializer.get_location ());
7158 break;
7159 }
7160 }
7161
7162 /* It must be a primary-expression. */
7163 postfix_expression
7164 = cp_parser_primary_expression (parser, address_p, cast_p,
7165 /*template_arg_p=*/false,
7166 decltype_p,
7167 &idk);
7168 }
7169 break;
7170 }
7171
7172 /* Note that we don't need to worry about calling build_cplus_new on a
7173 class-valued CALL_EXPR in decltype when it isn't the end of the
7174 postfix-expression; unary_complex_lvalue will take care of that for
7175 all these cases. */
7176
7177 /* Keep looping until the postfix-expression is complete. */
7178 while (true)
7179 {
7180 if (idk == CP_ID_KIND_UNQUALIFIED
7181 && identifier_p (postfix_expression)
7182 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7183 /* It is not a Koenig lookup function call. */
7184 postfix_expression
7185 = unqualified_name_lookup_error (postfix_expression);
7186
7187 /* Peek at the next token. */
7188 token = cp_lexer_peek_token (parser->lexer);
7189
7190 switch (token->type)
7191 {
7192 case CPP_OPEN_SQUARE:
7193 if (cp_next_tokens_can_be_std_attribute_p (parser))
7194 {
7195 cp_parser_error (parser,
7196 "two consecutive %<[%> shall "
7197 "only introduce an attribute");
7198 return error_mark_node;
7199 }
7200 postfix_expression
7201 = cp_parser_postfix_open_square_expression (parser,
7202 postfix_expression,
7203 false,
7204 decltype_p);
7205 postfix_expression.set_range (start_loc,
7206 postfix_expression.get_location ());
7207
7208 idk = CP_ID_KIND_NONE;
7209 is_member_access = false;
7210 break;
7211
7212 case CPP_OPEN_PAREN:
7213 /* postfix-expression ( expression-list [opt] ) */
7214 {
7215 bool koenig_p;
7216 bool is_builtin_constant_p;
7217 bool saved_integral_constant_expression_p = false;
7218 bool saved_non_integral_constant_expression_p = false;
7219 tsubst_flags_t complain = complain_flags (decltype_p);
7220 vec<tree, va_gc> *args;
7221 location_t close_paren_loc = UNKNOWN_LOCATION;
7222
7223 is_member_access = false;
7224
7225 tree stripped_expression
7226 = tree_strip_any_location_wrapper (postfix_expression);
7227 is_builtin_constant_p
7228 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7229 if (is_builtin_constant_p)
7230 {
7231 /* The whole point of __builtin_constant_p is to allow
7232 non-constant expressions to appear as arguments. */
7233 saved_integral_constant_expression_p
7234 = parser->integral_constant_expression_p;
7235 saved_non_integral_constant_expression_p
7236 = parser->non_integral_constant_expression_p;
7237 parser->integral_constant_expression_p = false;
7238 }
7239 args = (cp_parser_parenthesized_expression_list
7240 (parser, non_attr,
7241 /*cast_p=*/false, /*allow_expansion_p=*/true,
7242 /*non_constant_p=*/NULL,
7243 /*close_paren_loc=*/&close_paren_loc,
7244 /*wrap_locations_p=*/true));
7245 if (is_builtin_constant_p)
7246 {
7247 parser->integral_constant_expression_p
7248 = saved_integral_constant_expression_p;
7249 parser->non_integral_constant_expression_p
7250 = saved_non_integral_constant_expression_p;
7251 }
7252
7253 if (args == NULL)
7254 {
7255 postfix_expression = error_mark_node;
7256 break;
7257 }
7258
7259 /* Function calls are not permitted in
7260 constant-expressions. */
7261 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7262 && cp_parser_non_integral_constant_expression (parser,
7263 NIC_FUNC_CALL))
7264 {
7265 postfix_expression = error_mark_node;
7266 release_tree_vector (args);
7267 break;
7268 }
7269
7270 koenig_p = false;
7271 if (idk == CP_ID_KIND_UNQUALIFIED
7272 || idk == CP_ID_KIND_TEMPLATE_ID)
7273 {
7274 if (identifier_p (postfix_expression)
7275 /* In C++2A, we may need to perform ADL for a template
7276 name. */
7277 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7278 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7279 {
7280 if (!args->is_empty ())
7281 {
7282 koenig_p = true;
7283 if (!any_type_dependent_arguments_p (args))
7284 postfix_expression
7285 = perform_koenig_lookup (postfix_expression, args,
7286 complain);
7287 }
7288 else
7289 postfix_expression
7290 = unqualified_fn_lookup_error (postfix_expression);
7291 }
7292 /* We do not perform argument-dependent lookup if
7293 normal lookup finds a non-function, in accordance
7294 with the expected resolution of DR 218. */
7295 else if (!args->is_empty ()
7296 && is_overloaded_fn (postfix_expression))
7297 {
7298 /* We only need to look at the first function,
7299 because all the fns share the attribute we're
7300 concerned with (all member fns or all local
7301 fns). */
7302 tree fn = get_first_fn (postfix_expression);
7303 fn = STRIP_TEMPLATE (fn);
7304
7305 /* Do not do argument dependent lookup if regular
7306 lookup finds a member function or a block-scope
7307 function declaration. [basic.lookup.argdep]/3 */
7308 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7309 || DECL_FUNCTION_MEMBER_P (fn)
7310 || DECL_LOCAL_FUNCTION_P (fn)))
7311 {
7312 koenig_p = true;
7313 if (!any_type_dependent_arguments_p (args))
7314 postfix_expression
7315 = perform_koenig_lookup (postfix_expression, args,
7316 complain);
7317 }
7318 }
7319 }
7320
7321 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7322 {
7323 tree instance = TREE_OPERAND (postfix_expression, 0);
7324 tree fn = TREE_OPERAND (postfix_expression, 1);
7325
7326 if (processing_template_decl
7327 && (type_dependent_object_expression_p (instance)
7328 || (!BASELINK_P (fn)
7329 && TREE_CODE (fn) != FIELD_DECL)
7330 || type_dependent_expression_p (fn)
7331 || any_type_dependent_arguments_p (args)))
7332 {
7333 maybe_generic_this_capture (instance, fn);
7334 postfix_expression
7335 = build_min_nt_call_vec (postfix_expression, args);
7336 release_tree_vector (args);
7337 break;
7338 }
7339
7340 if (BASELINK_P (fn))
7341 {
7342 postfix_expression
7343 = (build_new_method_call
7344 (instance, fn, &args, NULL_TREE,
7345 (idk == CP_ID_KIND_QUALIFIED
7346 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7347 : LOOKUP_NORMAL),
7348 /*fn_p=*/NULL,
7349 complain));
7350 }
7351 else
7352 postfix_expression
7353 = finish_call_expr (postfix_expression, &args,
7354 /*disallow_virtual=*/false,
7355 /*koenig_p=*/false,
7356 complain);
7357 }
7358 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7359 || TREE_CODE (postfix_expression) == MEMBER_REF
7360 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7361 postfix_expression = (build_offset_ref_call_from_tree
7362 (postfix_expression, &args,
7363 complain));
7364 else if (idk == CP_ID_KIND_QUALIFIED)
7365 /* A call to a static class member, or a namespace-scope
7366 function. */
7367 postfix_expression
7368 = finish_call_expr (postfix_expression, &args,
7369 /*disallow_virtual=*/true,
7370 koenig_p,
7371 complain);
7372 else
7373 /* All other function calls. */
7374 postfix_expression
7375 = finish_call_expr (postfix_expression, &args,
7376 /*disallow_virtual=*/false,
7377 koenig_p,
7378 complain);
7379
7380 if (close_paren_loc != UNKNOWN_LOCATION)
7381 {
7382 location_t combined_loc = make_location (token->location,
7383 start_loc,
7384 close_paren_loc);
7385 postfix_expression.set_location (combined_loc);
7386 }
7387
7388 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7389 idk = CP_ID_KIND_NONE;
7390
7391 release_tree_vector (args);
7392 }
7393 break;
7394
7395 case CPP_DOT:
7396 case CPP_DEREF:
7397 /* postfix-expression . template [opt] id-expression
7398 postfix-expression . pseudo-destructor-name
7399 postfix-expression -> template [opt] id-expression
7400 postfix-expression -> pseudo-destructor-name */
7401
7402 /* Consume the `.' or `->' operator. */
7403 cp_lexer_consume_token (parser->lexer);
7404
7405 postfix_expression
7406 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7407 postfix_expression,
7408 false, &idk, loc);
7409
7410 is_member_access = true;
7411 break;
7412
7413 case CPP_PLUS_PLUS:
7414 /* postfix-expression ++ */
7415 /* Consume the `++' token. */
7416 cp_lexer_consume_token (parser->lexer);
7417 /* Generate a representation for the complete expression. */
7418 postfix_expression
7419 = finish_increment_expr (postfix_expression,
7420 POSTINCREMENT_EXPR);
7421 /* Increments may not appear in constant-expressions. */
7422 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7423 postfix_expression = error_mark_node;
7424 idk = CP_ID_KIND_NONE;
7425 is_member_access = false;
7426 break;
7427
7428 case CPP_MINUS_MINUS:
7429 /* postfix-expression -- */
7430 /* Consume the `--' token. */
7431 cp_lexer_consume_token (parser->lexer);
7432 /* Generate a representation for the complete expression. */
7433 postfix_expression
7434 = finish_increment_expr (postfix_expression,
7435 POSTDECREMENT_EXPR);
7436 /* Decrements may not appear in constant-expressions. */
7437 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7438 postfix_expression = error_mark_node;
7439 idk = CP_ID_KIND_NONE;
7440 is_member_access = false;
7441 break;
7442
7443 default:
7444 if (pidk_return != NULL)
7445 * pidk_return = idk;
7446 if (member_access_only_p)
7447 return is_member_access
7448 ? postfix_expression
7449 : cp_expr (error_mark_node);
7450 else
7451 return postfix_expression;
7452 }
7453 }
7454
7455 /* We should never get here. */
7456 gcc_unreachable ();
7457 return error_mark_node;
7458 }
7459
7460 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7461 by cp_parser_builtin_offsetof. We're looking for
7462
7463 postfix-expression [ expression ]
7464 postfix-expression [ braced-init-list ] (C++11)
7465
7466 FOR_OFFSETOF is set if we're being called in that context, which
7467 changes how we deal with integer constant expressions. */
7468
7469 static tree
7470 cp_parser_postfix_open_square_expression (cp_parser *parser,
7471 tree postfix_expression,
7472 bool for_offsetof,
7473 bool decltype_p)
7474 {
7475 tree index = NULL_TREE;
7476 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7477 bool saved_greater_than_is_operator_p;
7478
7479 /* Consume the `[' token. */
7480 cp_lexer_consume_token (parser->lexer);
7481
7482 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7483 parser->greater_than_is_operator_p = true;
7484
7485 /* Parse the index expression. */
7486 /* ??? For offsetof, there is a question of what to allow here. If
7487 offsetof is not being used in an integral constant expression context,
7488 then we *could* get the right answer by computing the value at runtime.
7489 If we are in an integral constant expression context, then we might
7490 could accept any constant expression; hard to say without analysis.
7491 Rather than open the barn door too wide right away, allow only integer
7492 constant expressions here. */
7493 if (for_offsetof)
7494 index = cp_parser_constant_expression (parser);
7495 else
7496 {
7497 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7498 {
7499 bool expr_nonconst_p;
7500 cp_lexer_set_source_position (parser->lexer);
7501 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7502 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7503 }
7504 else
7505 index = cp_parser_expression (parser);
7506 }
7507
7508 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7509
7510 /* Look for the closing `]'. */
7511 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7512
7513 /* Build the ARRAY_REF. */
7514 postfix_expression = grok_array_decl (loc, postfix_expression,
7515 index, decltype_p);
7516
7517 /* When not doing offsetof, array references are not permitted in
7518 constant-expressions. */
7519 if (!for_offsetof
7520 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7521 postfix_expression = error_mark_node;
7522
7523 return postfix_expression;
7524 }
7525
7526 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7527 dereference of incomplete type, returns true if error_mark_node should
7528 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7529 and *DEPENDENT_P. */
7530
7531 bool
7532 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7533 bool *dependent_p)
7534 {
7535 /* In a template, be permissive by treating an object expression
7536 of incomplete type as dependent (after a pedwarn). */
7537 diagnostic_t kind = (processing_template_decl
7538 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7539
7540 switch (TREE_CODE (*postfix_expression))
7541 {
7542 case CAST_EXPR:
7543 case REINTERPRET_CAST_EXPR:
7544 case CONST_CAST_EXPR:
7545 case STATIC_CAST_EXPR:
7546 case DYNAMIC_CAST_EXPR:
7547 case IMPLICIT_CONV_EXPR:
7548 case VIEW_CONVERT_EXPR:
7549 case NON_LVALUE_EXPR:
7550 kind = DK_ERROR;
7551 break;
7552 case OVERLOAD:
7553 /* Don't emit any diagnostic for OVERLOADs. */
7554 kind = DK_IGNORED;
7555 break;
7556 default:
7557 /* Avoid clobbering e.g. DECLs. */
7558 if (!EXPR_P (*postfix_expression))
7559 kind = DK_ERROR;
7560 break;
7561 }
7562
7563 if (kind == DK_IGNORED)
7564 return false;
7565
7566 location_t exploc = location_of (*postfix_expression);
7567 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7568 if (!MAYBE_CLASS_TYPE_P (*scope))
7569 return true;
7570 if (kind == DK_ERROR)
7571 *scope = *postfix_expression = error_mark_node;
7572 else if (processing_template_decl)
7573 {
7574 *dependent_p = true;
7575 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7576 }
7577 return false;
7578 }
7579
7580 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7581 by cp_parser_builtin_offsetof. We're looking for
7582
7583 postfix-expression . template [opt] id-expression
7584 postfix-expression . pseudo-destructor-name
7585 postfix-expression -> template [opt] id-expression
7586 postfix-expression -> pseudo-destructor-name
7587
7588 FOR_OFFSETOF is set if we're being called in that context. That sorta
7589 limits what of the above we'll actually accept, but nevermind.
7590 TOKEN_TYPE is the "." or "->" token, which will already have been
7591 removed from the stream. */
7592
7593 static tree
7594 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7595 enum cpp_ttype token_type,
7596 cp_expr postfix_expression,
7597 bool for_offsetof, cp_id_kind *idk,
7598 location_t location)
7599 {
7600 tree name;
7601 bool dependent_p;
7602 bool pseudo_destructor_p;
7603 tree scope = NULL_TREE;
7604 location_t start_loc = postfix_expression.get_start ();
7605
7606 /* If this is a `->' operator, dereference the pointer. */
7607 if (token_type == CPP_DEREF)
7608 postfix_expression = build_x_arrow (location, postfix_expression,
7609 tf_warning_or_error);
7610 /* Check to see whether or not the expression is type-dependent and
7611 not the current instantiation. */
7612 dependent_p = type_dependent_object_expression_p (postfix_expression);
7613 /* The identifier following the `->' or `.' is not qualified. */
7614 parser->scope = NULL_TREE;
7615 parser->qualifying_scope = NULL_TREE;
7616 parser->object_scope = NULL_TREE;
7617 *idk = CP_ID_KIND_NONE;
7618
7619 /* Enter the scope corresponding to the type of the object
7620 given by the POSTFIX_EXPRESSION. */
7621 if (!dependent_p)
7622 {
7623 scope = TREE_TYPE (postfix_expression);
7624 /* According to the standard, no expression should ever have
7625 reference type. Unfortunately, we do not currently match
7626 the standard in this respect in that our internal representation
7627 of an expression may have reference type even when the standard
7628 says it does not. Therefore, we have to manually obtain the
7629 underlying type here. */
7630 scope = non_reference (scope);
7631 /* The type of the POSTFIX_EXPRESSION must be complete. */
7632 /* Unlike the object expression in other contexts, *this is not
7633 required to be of complete type for purposes of class member
7634 access (5.2.5) outside the member function body. */
7635 if (postfix_expression != current_class_ref
7636 && scope != error_mark_node
7637 && !currently_open_class (scope))
7638 {
7639 scope = complete_type (scope);
7640 if (!COMPLETE_TYPE_P (scope)
7641 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7642 &dependent_p))
7643 return error_mark_node;
7644 }
7645
7646 if (!dependent_p)
7647 {
7648 /* Let the name lookup machinery know that we are processing a
7649 class member access expression. */
7650 parser->context->object_type = scope;
7651 /* If something went wrong, we want to be able to discern that case,
7652 as opposed to the case where there was no SCOPE due to the type
7653 of expression being dependent. */
7654 if (!scope)
7655 scope = error_mark_node;
7656 /* If the SCOPE was erroneous, make the various semantic analysis
7657 functions exit quickly -- and without issuing additional error
7658 messages. */
7659 if (scope == error_mark_node)
7660 postfix_expression = error_mark_node;
7661 }
7662 }
7663
7664 if (dependent_p)
7665 /* Tell cp_parser_lookup_name that there was an object, even though it's
7666 type-dependent. */
7667 parser->context->object_type = unknown_type_node;
7668
7669 /* Assume this expression is not a pseudo-destructor access. */
7670 pseudo_destructor_p = false;
7671
7672 /* If the SCOPE is a scalar type, then, if this is a valid program,
7673 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7674 is type dependent, it can be pseudo-destructor-name or something else.
7675 Try to parse it as pseudo-destructor-name first. */
7676 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7677 {
7678 tree s;
7679 tree type;
7680
7681 cp_parser_parse_tentatively (parser);
7682 /* Parse the pseudo-destructor-name. */
7683 s = NULL_TREE;
7684 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7685 &s, &type);
7686 if (dependent_p
7687 && (cp_parser_error_occurred (parser)
7688 || !SCALAR_TYPE_P (type)))
7689 cp_parser_abort_tentative_parse (parser);
7690 else if (cp_parser_parse_definitely (parser))
7691 {
7692 pseudo_destructor_p = true;
7693 postfix_expression
7694 = finish_pseudo_destructor_expr (postfix_expression,
7695 s, type, location);
7696 }
7697 }
7698
7699 if (!pseudo_destructor_p)
7700 {
7701 /* If the SCOPE is not a scalar type, we are looking at an
7702 ordinary class member access expression, rather than a
7703 pseudo-destructor-name. */
7704 bool template_p;
7705 cp_token *token = cp_lexer_peek_token (parser->lexer);
7706 /* Parse the id-expression. */
7707 name = (cp_parser_id_expression
7708 (parser,
7709 cp_parser_optional_template_keyword (parser),
7710 /*check_dependency_p=*/true,
7711 &template_p,
7712 /*declarator_p=*/false,
7713 /*optional_p=*/false));
7714 /* In general, build a SCOPE_REF if the member name is qualified.
7715 However, if the name was not dependent and has already been
7716 resolved; there is no need to build the SCOPE_REF. For example;
7717
7718 struct X { void f(); };
7719 template <typename T> void f(T* t) { t->X::f(); }
7720
7721 Even though "t" is dependent, "X::f" is not and has been resolved
7722 to a BASELINK; there is no need to include scope information. */
7723
7724 /* But we do need to remember that there was an explicit scope for
7725 virtual function calls. */
7726 if (parser->scope)
7727 *idk = CP_ID_KIND_QUALIFIED;
7728
7729 /* If the name is a template-id that names a type, we will get a
7730 TYPE_DECL here. That is invalid code. */
7731 if (TREE_CODE (name) == TYPE_DECL)
7732 {
7733 error_at (token->location, "invalid use of %qD", name);
7734 postfix_expression = error_mark_node;
7735 }
7736 else
7737 {
7738 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7739 {
7740 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7741 {
7742 error_at (token->location, "%<%D::%D%> is not a class member",
7743 parser->scope, name);
7744 postfix_expression = error_mark_node;
7745 }
7746 else
7747 name = build_qualified_name (/*type=*/NULL_TREE,
7748 parser->scope,
7749 name,
7750 template_p);
7751 parser->scope = NULL_TREE;
7752 parser->qualifying_scope = NULL_TREE;
7753 parser->object_scope = NULL_TREE;
7754 }
7755 if (parser->scope && name && BASELINK_P (name))
7756 adjust_result_of_qualified_name_lookup
7757 (name, parser->scope, scope);
7758 postfix_expression
7759 = finish_class_member_access_expr (postfix_expression, name,
7760 template_p,
7761 tf_warning_or_error);
7762 /* Build a location e.g.:
7763 ptr->access_expr
7764 ~~~^~~~~~~~~~~~~
7765 where the caret is at the deref token, ranging from
7766 the start of postfix_expression to the end of the access expr. */
7767 location_t end_loc
7768 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7769 location_t combined_loc
7770 = make_location (input_location, start_loc, end_loc);
7771 protected_set_expr_location (postfix_expression, combined_loc);
7772 }
7773 }
7774
7775 /* We no longer need to look up names in the scope of the object on
7776 the left-hand side of the `.' or `->' operator. */
7777 parser->context->object_type = NULL_TREE;
7778
7779 /* Outside of offsetof, these operators may not appear in
7780 constant-expressions. */
7781 if (!for_offsetof
7782 && (cp_parser_non_integral_constant_expression
7783 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7784 postfix_expression = error_mark_node;
7785
7786 return postfix_expression;
7787 }
7788
7789 /* Parse a parenthesized expression-list.
7790
7791 expression-list:
7792 assignment-expression
7793 expression-list, assignment-expression
7794
7795 attribute-list:
7796 expression-list
7797 identifier
7798 identifier, expression-list
7799
7800 CAST_P is true if this expression is the target of a cast.
7801
7802 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7803 argument pack.
7804
7805 WRAP_LOCATIONS_P is true if expressions within this list for which
7806 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7807 their source locations.
7808
7809 Returns a vector of trees. Each element is a representation of an
7810 assignment-expression. NULL is returned if the ( and or ) are
7811 missing. An empty, but allocated, vector is returned on no
7812 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7813 if we are parsing an attribute list for an attribute that wants a
7814 plain identifier argument, normal_attr for an attribute that wants
7815 an expression, or non_attr if we aren't parsing an attribute list. If
7816 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7817 not all of the expressions in the list were constant.
7818 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7819 will be written to with the location of the closing parenthesis. If
7820 an error occurs, it may or may not be written to. */
7821
7822 static vec<tree, va_gc> *
7823 cp_parser_parenthesized_expression_list (cp_parser* parser,
7824 int is_attribute_list,
7825 bool cast_p,
7826 bool allow_expansion_p,
7827 bool *non_constant_p,
7828 location_t *close_paren_loc,
7829 bool wrap_locations_p)
7830 {
7831 vec<tree, va_gc> *expression_list;
7832 bool fold_expr_p = is_attribute_list != non_attr;
7833 tree identifier = NULL_TREE;
7834 bool saved_greater_than_is_operator_p;
7835
7836 /* Assume all the expressions will be constant. */
7837 if (non_constant_p)
7838 *non_constant_p = false;
7839
7840 matching_parens parens;
7841 if (!parens.require_open (parser))
7842 return NULL;
7843
7844 expression_list = make_tree_vector ();
7845
7846 /* Within a parenthesized expression, a `>' token is always
7847 the greater-than operator. */
7848 saved_greater_than_is_operator_p
7849 = parser->greater_than_is_operator_p;
7850 parser->greater_than_is_operator_p = true;
7851
7852 cp_expr expr (NULL_TREE);
7853
7854 /* Consume expressions until there are no more. */
7855 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7856 while (true)
7857 {
7858 /* At the beginning of attribute lists, check to see if the
7859 next token is an identifier. */
7860 if (is_attribute_list == id_attr
7861 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7862 {
7863 cp_token *token;
7864
7865 /* Consume the identifier. */
7866 token = cp_lexer_consume_token (parser->lexer);
7867 /* Save the identifier. */
7868 identifier = token->u.value;
7869 }
7870 else
7871 {
7872 bool expr_non_constant_p;
7873
7874 /* Parse the next assignment-expression. */
7875 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7876 {
7877 /* A braced-init-list. */
7878 cp_lexer_set_source_position (parser->lexer);
7879 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7880 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7881 if (non_constant_p && expr_non_constant_p)
7882 *non_constant_p = true;
7883 }
7884 else if (non_constant_p)
7885 {
7886 expr = (cp_parser_constant_expression
7887 (parser, /*allow_non_constant_p=*/true,
7888 &expr_non_constant_p));
7889 if (expr_non_constant_p)
7890 *non_constant_p = true;
7891 }
7892 else
7893 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7894 cast_p);
7895
7896 if (fold_expr_p)
7897 expr = instantiate_non_dependent_expr (expr);
7898
7899 /* If we have an ellipsis, then this is an expression
7900 expansion. */
7901 if (allow_expansion_p
7902 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7903 {
7904 /* Consume the `...'. */
7905 cp_lexer_consume_token (parser->lexer);
7906
7907 /* Build the argument pack. */
7908 expr = make_pack_expansion (expr);
7909 }
7910
7911 if (wrap_locations_p)
7912 expr.maybe_add_location_wrapper ();
7913
7914 /* Add it to the list. We add error_mark_node
7915 expressions to the list, so that we can still tell if
7916 the correct form for a parenthesized expression-list
7917 is found. That gives better errors. */
7918 vec_safe_push (expression_list, expr.get_value ());
7919
7920 if (expr == error_mark_node)
7921 goto skip_comma;
7922 }
7923
7924 /* After the first item, attribute lists look the same as
7925 expression lists. */
7926 is_attribute_list = non_attr;
7927
7928 get_comma:;
7929 /* If the next token isn't a `,', then we are done. */
7930 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7931 break;
7932
7933 /* Otherwise, consume the `,' and keep going. */
7934 cp_lexer_consume_token (parser->lexer);
7935 }
7936
7937 if (close_paren_loc)
7938 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7939
7940 if (!parens.require_close (parser))
7941 {
7942 int ending;
7943
7944 skip_comma:;
7945 /* We try and resync to an unnested comma, as that will give the
7946 user better diagnostics. */
7947 ending = cp_parser_skip_to_closing_parenthesis (parser,
7948 /*recovering=*/true,
7949 /*or_comma=*/true,
7950 /*consume_paren=*/true);
7951 if (ending < 0)
7952 goto get_comma;
7953 if (!ending)
7954 {
7955 parser->greater_than_is_operator_p
7956 = saved_greater_than_is_operator_p;
7957 return NULL;
7958 }
7959 }
7960
7961 parser->greater_than_is_operator_p
7962 = saved_greater_than_is_operator_p;
7963
7964 if (identifier)
7965 vec_safe_insert (expression_list, 0, identifier);
7966
7967 return expression_list;
7968 }
7969
7970 /* Parse a pseudo-destructor-name.
7971
7972 pseudo-destructor-name:
7973 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7974 :: [opt] nested-name-specifier template template-id :: ~ type-name
7975 :: [opt] nested-name-specifier [opt] ~ type-name
7976
7977 If either of the first two productions is used, sets *SCOPE to the
7978 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7979 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7980 or ERROR_MARK_NODE if the parse fails. */
7981
7982 static void
7983 cp_parser_pseudo_destructor_name (cp_parser* parser,
7984 tree object,
7985 tree* scope,
7986 tree* type)
7987 {
7988 bool nested_name_specifier_p;
7989
7990 /* Handle ~auto. */
7991 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7992 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7993 && !type_dependent_expression_p (object))
7994 {
7995 if (cxx_dialect < cxx14)
7996 pedwarn (input_location, 0,
7997 "%<~auto%> only available with "
7998 "%<-std=c++14%> or %<-std=gnu++14%>");
7999 cp_lexer_consume_token (parser->lexer);
8000 cp_lexer_consume_token (parser->lexer);
8001 *scope = NULL_TREE;
8002 *type = TREE_TYPE (object);
8003 return;
8004 }
8005
8006 /* Assume that things will not work out. */
8007 *type = error_mark_node;
8008
8009 /* Look for the optional `::' operator. */
8010 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8011 /* Look for the optional nested-name-specifier. */
8012 nested_name_specifier_p
8013 = (cp_parser_nested_name_specifier_opt (parser,
8014 /*typename_keyword_p=*/false,
8015 /*check_dependency_p=*/true,
8016 /*type_p=*/false,
8017 /*is_declaration=*/false)
8018 != NULL_TREE);
8019 /* Now, if we saw a nested-name-specifier, we might be doing the
8020 second production. */
8021 if (nested_name_specifier_p
8022 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8023 {
8024 /* Consume the `template' keyword. */
8025 cp_lexer_consume_token (parser->lexer);
8026 /* Parse the template-id. */
8027 cp_parser_template_id (parser,
8028 /*template_keyword_p=*/true,
8029 /*check_dependency_p=*/false,
8030 class_type,
8031 /*is_declaration=*/true);
8032 /* Look for the `::' token. */
8033 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8034 }
8035 /* If the next token is not a `~', then there might be some
8036 additional qualification. */
8037 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8038 {
8039 /* At this point, we're looking for "type-name :: ~". The type-name
8040 must not be a class-name, since this is a pseudo-destructor. So,
8041 it must be either an enum-name, or a typedef-name -- both of which
8042 are just identifiers. So, we peek ahead to check that the "::"
8043 and "~" tokens are present; if they are not, then we can avoid
8044 calling type_name. */
8045 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8046 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8047 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8048 {
8049 cp_parser_error (parser, "non-scalar type");
8050 return;
8051 }
8052
8053 /* Look for the type-name. */
8054 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8055 if (*scope == error_mark_node)
8056 return;
8057
8058 /* Look for the `::' token. */
8059 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8060 }
8061 else
8062 *scope = NULL_TREE;
8063
8064 /* Look for the `~'. */
8065 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8066
8067 /* Once we see the ~, this has to be a pseudo-destructor. */
8068 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8069 cp_parser_commit_to_topmost_tentative_parse (parser);
8070
8071 /* Look for the type-name again. We are not responsible for
8072 checking that it matches the first type-name. */
8073 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8074 }
8075
8076 /* Parse a unary-expression.
8077
8078 unary-expression:
8079 postfix-expression
8080 ++ cast-expression
8081 -- cast-expression
8082 unary-operator cast-expression
8083 sizeof unary-expression
8084 sizeof ( type-id )
8085 alignof ( type-id ) [C++0x]
8086 new-expression
8087 delete-expression
8088
8089 GNU Extensions:
8090
8091 unary-expression:
8092 __extension__ cast-expression
8093 __alignof__ unary-expression
8094 __alignof__ ( type-id )
8095 alignof unary-expression [C++0x]
8096 __real__ cast-expression
8097 __imag__ cast-expression
8098 && identifier
8099 sizeof ( type-id ) { initializer-list , [opt] }
8100 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8101 __alignof__ ( type-id ) { initializer-list , [opt] }
8102
8103 ADDRESS_P is true iff the unary-expression is appearing as the
8104 operand of the `&' operator. CAST_P is true if this expression is
8105 the target of a cast.
8106
8107 Returns a representation of the expression. */
8108
8109 static cp_expr
8110 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8111 bool address_p, bool cast_p, bool decltype_p)
8112 {
8113 cp_token *token;
8114 enum tree_code unary_operator;
8115
8116 /* Peek at the next token. */
8117 token = cp_lexer_peek_token (parser->lexer);
8118 /* Some keywords give away the kind of expression. */
8119 if (token->type == CPP_KEYWORD)
8120 {
8121 enum rid keyword = token->keyword;
8122
8123 switch (keyword)
8124 {
8125 case RID_ALIGNOF:
8126 case RID_SIZEOF:
8127 {
8128 tree operand, ret;
8129 enum tree_code op;
8130 location_t start_loc = token->location;
8131
8132 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8133 bool std_alignof = id_equal (token->u.value, "alignof");
8134
8135 /* Consume the token. */
8136 cp_lexer_consume_token (parser->lexer);
8137 /* Parse the operand. */
8138 operand = cp_parser_sizeof_operand (parser, keyword);
8139
8140 if (TYPE_P (operand))
8141 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8142 true);
8143 else
8144 {
8145 /* ISO C++ defines alignof only with types, not with
8146 expressions. So pedwarn if alignof is used with a non-
8147 type expression. However, __alignof__ is ok. */
8148 if (std_alignof)
8149 pedwarn (token->location, OPT_Wpedantic,
8150 "ISO C++ does not allow %<alignof%> "
8151 "with a non-type");
8152
8153 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8154 }
8155 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8156 SIZEOF_EXPR with the original operand. */
8157 if (op == SIZEOF_EXPR && ret != error_mark_node)
8158 {
8159 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8160 {
8161 if (!processing_template_decl && TYPE_P (operand))
8162 {
8163 ret = build_min (SIZEOF_EXPR, size_type_node,
8164 build1 (NOP_EXPR, operand,
8165 error_mark_node));
8166 SIZEOF_EXPR_TYPE_P (ret) = 1;
8167 }
8168 else
8169 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8170 TREE_SIDE_EFFECTS (ret) = 0;
8171 TREE_READONLY (ret) = 1;
8172 }
8173 }
8174
8175 /* Construct a location e.g. :
8176 alignof (expr)
8177 ^~~~~~~~~~~~~~
8178 with start == caret at the start of the "alignof"/"sizeof"
8179 token, with the endpoint at the final closing paren. */
8180 location_t finish_loc
8181 = cp_lexer_previous_token (parser->lexer)->location;
8182 location_t compound_loc
8183 = make_location (start_loc, start_loc, finish_loc);
8184
8185 cp_expr ret_expr (ret);
8186 ret_expr.set_location (compound_loc);
8187 ret_expr = ret_expr.maybe_add_location_wrapper ();
8188 return ret_expr;
8189 }
8190
8191 case RID_BUILTIN_HAS_ATTRIBUTE:
8192 return cp_parser_has_attribute_expression (parser);
8193
8194 case RID_NEW:
8195 return cp_parser_new_expression (parser);
8196
8197 case RID_DELETE:
8198 return cp_parser_delete_expression (parser);
8199
8200 case RID_EXTENSION:
8201 {
8202 /* The saved value of the PEDANTIC flag. */
8203 int saved_pedantic;
8204 tree expr;
8205
8206 /* Save away the PEDANTIC flag. */
8207 cp_parser_extension_opt (parser, &saved_pedantic);
8208 /* Parse the cast-expression. */
8209 expr = cp_parser_simple_cast_expression (parser);
8210 /* Restore the PEDANTIC flag. */
8211 pedantic = saved_pedantic;
8212
8213 return expr;
8214 }
8215
8216 case RID_REALPART:
8217 case RID_IMAGPART:
8218 {
8219 tree expression;
8220
8221 /* Consume the `__real__' or `__imag__' token. */
8222 cp_lexer_consume_token (parser->lexer);
8223 /* Parse the cast-expression. */
8224 expression = cp_parser_simple_cast_expression (parser);
8225 /* Create the complete representation. */
8226 return build_x_unary_op (token->location,
8227 (keyword == RID_REALPART
8228 ? REALPART_EXPR : IMAGPART_EXPR),
8229 expression,
8230 tf_warning_or_error);
8231 }
8232 break;
8233
8234 case RID_TRANSACTION_ATOMIC:
8235 case RID_TRANSACTION_RELAXED:
8236 return cp_parser_transaction_expression (parser, keyword);
8237
8238 case RID_NOEXCEPT:
8239 {
8240 tree expr;
8241 const char *saved_message;
8242 bool saved_integral_constant_expression_p;
8243 bool saved_non_integral_constant_expression_p;
8244 bool saved_greater_than_is_operator_p;
8245
8246 location_t start_loc = token->location;
8247
8248 cp_lexer_consume_token (parser->lexer);
8249 matching_parens parens;
8250 parens.require_open (parser);
8251
8252 saved_message = parser->type_definition_forbidden_message;
8253 parser->type_definition_forbidden_message
8254 = G_("types may not be defined in %<noexcept%> expressions");
8255
8256 saved_integral_constant_expression_p
8257 = parser->integral_constant_expression_p;
8258 saved_non_integral_constant_expression_p
8259 = parser->non_integral_constant_expression_p;
8260 parser->integral_constant_expression_p = false;
8261
8262 saved_greater_than_is_operator_p
8263 = parser->greater_than_is_operator_p;
8264 parser->greater_than_is_operator_p = true;
8265
8266 ++cp_unevaluated_operand;
8267 ++c_inhibit_evaluation_warnings;
8268 ++cp_noexcept_operand;
8269 expr = cp_parser_expression (parser);
8270 --cp_noexcept_operand;
8271 --c_inhibit_evaluation_warnings;
8272 --cp_unevaluated_operand;
8273
8274 parser->greater_than_is_operator_p
8275 = saved_greater_than_is_operator_p;
8276
8277 parser->integral_constant_expression_p
8278 = saved_integral_constant_expression_p;
8279 parser->non_integral_constant_expression_p
8280 = saved_non_integral_constant_expression_p;
8281
8282 parser->type_definition_forbidden_message = saved_message;
8283
8284 location_t finish_loc
8285 = cp_lexer_peek_token (parser->lexer)->location;
8286 parens.require_close (parser);
8287
8288 /* Construct a location of the form:
8289 noexcept (expr)
8290 ^~~~~~~~~~~~~~~
8291 with start == caret, finishing at the close-paren. */
8292 location_t noexcept_loc
8293 = make_location (start_loc, start_loc, finish_loc);
8294
8295 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8296 noexcept_loc);
8297 }
8298
8299 default:
8300 break;
8301 }
8302 }
8303
8304 /* Look for the `:: new' and `:: delete', which also signal the
8305 beginning of a new-expression, or delete-expression,
8306 respectively. If the next token is `::', then it might be one of
8307 these. */
8308 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8309 {
8310 enum rid keyword;
8311
8312 /* See if the token after the `::' is one of the keywords in
8313 which we're interested. */
8314 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8315 /* If it's `new', we have a new-expression. */
8316 if (keyword == RID_NEW)
8317 return cp_parser_new_expression (parser);
8318 /* Similarly, for `delete'. */
8319 else if (keyword == RID_DELETE)
8320 return cp_parser_delete_expression (parser);
8321 }
8322
8323 /* Look for a unary operator. */
8324 unary_operator = cp_parser_unary_operator (token);
8325 /* The `++' and `--' operators can be handled similarly, even though
8326 they are not technically unary-operators in the grammar. */
8327 if (unary_operator == ERROR_MARK)
8328 {
8329 if (token->type == CPP_PLUS_PLUS)
8330 unary_operator = PREINCREMENT_EXPR;
8331 else if (token->type == CPP_MINUS_MINUS)
8332 unary_operator = PREDECREMENT_EXPR;
8333 /* Handle the GNU address-of-label extension. */
8334 else if (cp_parser_allow_gnu_extensions_p (parser)
8335 && token->type == CPP_AND_AND)
8336 {
8337 tree identifier;
8338 tree expression;
8339 location_t start_loc = token->location;
8340
8341 /* Consume the '&&' token. */
8342 cp_lexer_consume_token (parser->lexer);
8343 /* Look for the identifier. */
8344 location_t finish_loc
8345 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8346 identifier = cp_parser_identifier (parser);
8347 /* Construct a location of the form:
8348 &&label
8349 ^~~~~~~
8350 with caret==start at the "&&", finish at the end of the label. */
8351 location_t combined_loc
8352 = make_location (start_loc, start_loc, finish_loc);
8353 /* Create an expression representing the address. */
8354 expression = finish_label_address_expr (identifier, combined_loc);
8355 if (cp_parser_non_integral_constant_expression (parser,
8356 NIC_ADDR_LABEL))
8357 expression = error_mark_node;
8358 return expression;
8359 }
8360 }
8361 if (unary_operator != ERROR_MARK)
8362 {
8363 cp_expr cast_expression;
8364 cp_expr expression = error_mark_node;
8365 non_integral_constant non_constant_p = NIC_NONE;
8366 location_t loc = token->location;
8367 tsubst_flags_t complain = complain_flags (decltype_p);
8368
8369 /* Consume the operator token. */
8370 token = cp_lexer_consume_token (parser->lexer);
8371 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8372
8373 /* Parse the cast-expression. */
8374 cast_expression
8375 = cp_parser_cast_expression (parser,
8376 unary_operator == ADDR_EXPR,
8377 /*cast_p=*/false,
8378 /*decltype*/false,
8379 pidk);
8380
8381 /* Make a location:
8382 OP_TOKEN CAST_EXPRESSION
8383 ^~~~~~~~~~~~~~~~~~~~~~~~~
8384 with start==caret at the operator token, and
8385 extending to the end of the cast_expression. */
8386 loc = make_location (loc, loc, cast_expression.get_finish ());
8387
8388 /* Now, build an appropriate representation. */
8389 switch (unary_operator)
8390 {
8391 case INDIRECT_REF:
8392 non_constant_p = NIC_STAR;
8393 expression = build_x_indirect_ref (loc, cast_expression,
8394 RO_UNARY_STAR,
8395 complain);
8396 /* TODO: build_x_indirect_ref does not always honor the
8397 location, so ensure it is set. */
8398 expression.set_location (loc);
8399 break;
8400
8401 case ADDR_EXPR:
8402 non_constant_p = NIC_ADDR;
8403 /* Fall through. */
8404 case BIT_NOT_EXPR:
8405 expression = build_x_unary_op (loc, unary_operator,
8406 cast_expression,
8407 complain);
8408 /* TODO: build_x_unary_op does not always honor the location,
8409 so ensure it is set. */
8410 expression.set_location (loc);
8411 break;
8412
8413 case PREINCREMENT_EXPR:
8414 case PREDECREMENT_EXPR:
8415 non_constant_p = unary_operator == PREINCREMENT_EXPR
8416 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8417 /* Fall through. */
8418 case NEGATE_EXPR:
8419 /* Immediately fold negation of a constant, unless the constant is 0
8420 (since -0 == 0) or it would overflow. */
8421 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8422 {
8423 tree stripped_expr
8424 = tree_strip_any_location_wrapper (cast_expression);
8425 if (CONSTANT_CLASS_P (stripped_expr)
8426 && !integer_zerop (stripped_expr)
8427 && !TREE_OVERFLOW (stripped_expr))
8428 {
8429 tree folded = fold_build1 (unary_operator,
8430 TREE_TYPE (stripped_expr),
8431 stripped_expr);
8432 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8433 {
8434 expression = maybe_wrap_with_location (folded, loc);
8435 break;
8436 }
8437 }
8438 }
8439 /* Fall through. */
8440 case UNARY_PLUS_EXPR:
8441 case TRUTH_NOT_EXPR:
8442 expression = finish_unary_op_expr (loc, unary_operator,
8443 cast_expression, complain);
8444 break;
8445
8446 default:
8447 gcc_unreachable ();
8448 }
8449
8450 if (non_constant_p != NIC_NONE
8451 && cp_parser_non_integral_constant_expression (parser,
8452 non_constant_p))
8453 expression = error_mark_node;
8454
8455 return expression;
8456 }
8457
8458 return cp_parser_postfix_expression (parser, address_p, cast_p,
8459 /*member_access_only_p=*/false,
8460 decltype_p,
8461 pidk);
8462 }
8463
8464 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8465 unary-operator, the corresponding tree code is returned. */
8466
8467 static enum tree_code
8468 cp_parser_unary_operator (cp_token* token)
8469 {
8470 switch (token->type)
8471 {
8472 case CPP_MULT:
8473 return INDIRECT_REF;
8474
8475 case CPP_AND:
8476 return ADDR_EXPR;
8477
8478 case CPP_PLUS:
8479 return UNARY_PLUS_EXPR;
8480
8481 case CPP_MINUS:
8482 return NEGATE_EXPR;
8483
8484 case CPP_NOT:
8485 return TRUTH_NOT_EXPR;
8486
8487 case CPP_COMPL:
8488 return BIT_NOT_EXPR;
8489
8490 default:
8491 return ERROR_MARK;
8492 }
8493 }
8494
8495 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8496 Returns a representation of the expression. */
8497
8498 static tree
8499 cp_parser_has_attribute_expression (cp_parser *parser)
8500 {
8501 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8502
8503 /* Consume the __builtin_has_attribute token. */
8504 cp_lexer_consume_token (parser->lexer);
8505
8506 matching_parens parens;
8507 if (!parens.require_open (parser))
8508 return error_mark_node;
8509
8510 /* Types cannot be defined in a `sizeof' expression. Save away the
8511 old message. */
8512 const char *saved_message = parser->type_definition_forbidden_message;
8513 const char *saved_message_arg
8514 = parser->type_definition_forbidden_message_arg;
8515 parser->type_definition_forbidden_message
8516 = G_("types may not be defined in %qs expressions");
8517 parser->type_definition_forbidden_message_arg
8518 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
8519
8520 /* The restrictions on constant-expressions do not apply inside
8521 sizeof expressions. */
8522 bool saved_integral_constant_expression_p
8523 = parser->integral_constant_expression_p;
8524 bool saved_non_integral_constant_expression_p
8525 = parser->non_integral_constant_expression_p;
8526 parser->integral_constant_expression_p = false;
8527
8528 /* Do not actually evaluate the expression. */
8529 ++cp_unevaluated_operand;
8530 ++c_inhibit_evaluation_warnings;
8531
8532 tree oper = NULL_TREE;
8533
8534 /* We can't be sure yet whether we're looking at a type-id or an
8535 expression. */
8536 cp_parser_parse_tentatively (parser);
8537
8538 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8539 parser->in_type_id_in_expr_p = true;
8540 /* Look for the type-id. */
8541 oper = cp_parser_type_id (parser);
8542 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8543
8544 cp_parser_parse_definitely (parser);
8545
8546 /* If the type-id production did not work out, then we must be
8547 looking at an expression. */
8548 if (!oper || oper == error_mark_node)
8549 oper = cp_parser_assignment_expression (parser);
8550
8551 STRIP_ANY_LOCATION_WRAPPER (oper);
8552
8553 /* Go back to evaluating expressions. */
8554 --cp_unevaluated_operand;
8555 --c_inhibit_evaluation_warnings;
8556
8557 /* And restore the old one. */
8558 parser->type_definition_forbidden_message = saved_message;
8559 parser->type_definition_forbidden_message_arg = saved_message_arg;
8560 parser->integral_constant_expression_p
8561 = saved_integral_constant_expression_p;
8562 parser->non_integral_constant_expression_p
8563 = saved_non_integral_constant_expression_p;
8564
8565 /* Consume the comma if it's there. */
8566 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8567 {
8568 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8569 /*consume_paren=*/true);
8570 return error_mark_node;
8571 }
8572
8573 /* Parse the attribute specification. */
8574 bool ret = false;
8575 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8576 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8577 {
8578 if (oper != error_mark_node)
8579 {
8580 /* Fold constant expressions used in attributes first. */
8581 cp_check_const_attributes (attr);
8582
8583 /* Finally, see if OPER has been declared with ATTR. */
8584 ret = has_attribute (atloc, oper, attr, default_conversion);
8585 }
8586
8587 parens.require_close (parser);
8588 }
8589 else
8590 {
8591 error_at (atloc, "expected identifier");
8592 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8593 }
8594
8595 /* Construct a location e.g. :
8596 __builtin_has_attribute (oper, attr)
8597 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8598 with start == caret at the start of the built-in token,
8599 and with the endpoint at the final closing paren. */
8600 location_t finish_loc
8601 = cp_lexer_previous_token (parser->lexer)->location;
8602 location_t compound_loc
8603 = make_location (start_loc, start_loc, finish_loc);
8604
8605 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8606 ret_expr.set_location (compound_loc);
8607 ret_expr = ret_expr.maybe_add_location_wrapper ();
8608 return ret_expr;
8609 }
8610
8611 /* Parse a new-expression.
8612
8613 new-expression:
8614 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8615 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8616
8617 Returns a representation of the expression. */
8618
8619 static tree
8620 cp_parser_new_expression (cp_parser* parser)
8621 {
8622 bool global_scope_p;
8623 vec<tree, va_gc> *placement;
8624 tree type;
8625 vec<tree, va_gc> *initializer;
8626 tree nelts = NULL_TREE;
8627 tree ret;
8628
8629 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8630
8631 /* Look for the optional `::' operator. */
8632 global_scope_p
8633 = (cp_parser_global_scope_opt (parser,
8634 /*current_scope_valid_p=*/false)
8635 != NULL_TREE);
8636 /* Look for the `new' operator. */
8637 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8638 /* There's no easy way to tell a new-placement from the
8639 `( type-id )' construct. */
8640 cp_parser_parse_tentatively (parser);
8641 /* Look for a new-placement. */
8642 placement = cp_parser_new_placement (parser);
8643 /* If that didn't work out, there's no new-placement. */
8644 if (!cp_parser_parse_definitely (parser))
8645 {
8646 if (placement != NULL)
8647 release_tree_vector (placement);
8648 placement = NULL;
8649 }
8650
8651 /* If the next token is a `(', then we have a parenthesized
8652 type-id. */
8653 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8654 {
8655 cp_token *token;
8656 const char *saved_message = parser->type_definition_forbidden_message;
8657
8658 /* Consume the `('. */
8659 matching_parens parens;
8660 parens.consume_open (parser);
8661
8662 /* Parse the type-id. */
8663 parser->type_definition_forbidden_message
8664 = G_("types may not be defined in a new-expression");
8665 {
8666 type_id_in_expr_sentinel s (parser);
8667 type = cp_parser_type_id (parser);
8668 }
8669 parser->type_definition_forbidden_message = saved_message;
8670
8671 /* Look for the closing `)'. */
8672 parens.require_close (parser);
8673 token = cp_lexer_peek_token (parser->lexer);
8674 /* There should not be a direct-new-declarator in this production,
8675 but GCC used to allowed this, so we check and emit a sensible error
8676 message for this case. */
8677 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8678 {
8679 error_at (token->location,
8680 "array bound forbidden after parenthesized type-id");
8681 inform (token->location,
8682 "try removing the parentheses around the type-id");
8683 cp_parser_direct_new_declarator (parser);
8684 }
8685 }
8686 /* Otherwise, there must be a new-type-id. */
8687 else
8688 type = cp_parser_new_type_id (parser, &nelts);
8689
8690 /* If the next token is a `(' or '{', then we have a new-initializer. */
8691 cp_token *token = cp_lexer_peek_token (parser->lexer);
8692 if (token->type == CPP_OPEN_PAREN
8693 || token->type == CPP_OPEN_BRACE)
8694 initializer = cp_parser_new_initializer (parser);
8695 else
8696 initializer = NULL;
8697
8698 /* A new-expression may not appear in an integral constant
8699 expression. */
8700 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8701 ret = error_mark_node;
8702 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8703 of a new-type-id or type-id of a new-expression, the new-expression shall
8704 contain a new-initializer of the form ( assignment-expression )".
8705 Additionally, consistently with the spirit of DR 1467, we want to accept
8706 'new auto { 2 }' too. */
8707 else if ((ret = type_uses_auto (type))
8708 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8709 && (vec_safe_length (initializer) != 1
8710 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8711 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8712 {
8713 error_at (token->location,
8714 "initialization of new-expression for type %<auto%> "
8715 "requires exactly one element");
8716 ret = error_mark_node;
8717 }
8718 else
8719 {
8720 /* Construct a location e.g.:
8721 ptr = new int[100]
8722 ^~~~~~~~~~~~
8723 with caret == start at the start of the "new" token, and the end
8724 at the end of the final token we consumed. */
8725 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8726 location_t end_loc = get_finish (end_tok->location);
8727 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8728
8729 /* Create a representation of the new-expression. */
8730 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8731 tf_warning_or_error);
8732 protected_set_expr_location (ret, combined_loc);
8733 }
8734
8735 if (placement != NULL)
8736 release_tree_vector (placement);
8737 if (initializer != NULL)
8738 release_tree_vector (initializer);
8739
8740 return ret;
8741 }
8742
8743 /* Parse a new-placement.
8744
8745 new-placement:
8746 ( expression-list )
8747
8748 Returns the same representation as for an expression-list. */
8749
8750 static vec<tree, va_gc> *
8751 cp_parser_new_placement (cp_parser* parser)
8752 {
8753 vec<tree, va_gc> *expression_list;
8754
8755 /* Parse the expression-list. */
8756 expression_list = (cp_parser_parenthesized_expression_list
8757 (parser, non_attr, /*cast_p=*/false,
8758 /*allow_expansion_p=*/true,
8759 /*non_constant_p=*/NULL));
8760
8761 if (expression_list && expression_list->is_empty ())
8762 error ("expected expression-list or type-id");
8763
8764 return expression_list;
8765 }
8766
8767 /* Parse a new-type-id.
8768
8769 new-type-id:
8770 type-specifier-seq new-declarator [opt]
8771
8772 Returns the TYPE allocated. If the new-type-id indicates an array
8773 type, *NELTS is set to the number of elements in the last array
8774 bound; the TYPE will not include the last array bound. */
8775
8776 static tree
8777 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8778 {
8779 cp_decl_specifier_seq type_specifier_seq;
8780 cp_declarator *new_declarator;
8781 cp_declarator *declarator;
8782 cp_declarator *outer_declarator;
8783 const char *saved_message;
8784
8785 /* The type-specifier sequence must not contain type definitions.
8786 (It cannot contain declarations of new types either, but if they
8787 are not definitions we will catch that because they are not
8788 complete.) */
8789 saved_message = parser->type_definition_forbidden_message;
8790 parser->type_definition_forbidden_message
8791 = G_("types may not be defined in a new-type-id");
8792 /* Parse the type-specifier-seq. */
8793 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
8794 /*is_declaration=*/false,
8795 /*is_trailing_return=*/false,
8796 &type_specifier_seq);
8797 /* Restore the old message. */
8798 parser->type_definition_forbidden_message = saved_message;
8799
8800 if (type_specifier_seq.type == error_mark_node)
8801 return error_mark_node;
8802
8803 /* Parse the new-declarator. */
8804 new_declarator = cp_parser_new_declarator_opt (parser);
8805
8806 /* Determine the number of elements in the last array dimension, if
8807 any. */
8808 *nelts = NULL_TREE;
8809 /* Skip down to the last array dimension. */
8810 declarator = new_declarator;
8811 outer_declarator = NULL;
8812 while (declarator && (declarator->kind == cdk_pointer
8813 || declarator->kind == cdk_ptrmem))
8814 {
8815 outer_declarator = declarator;
8816 declarator = declarator->declarator;
8817 }
8818 while (declarator
8819 && declarator->kind == cdk_array
8820 && declarator->declarator
8821 && declarator->declarator->kind == cdk_array)
8822 {
8823 outer_declarator = declarator;
8824 declarator = declarator->declarator;
8825 }
8826
8827 if (declarator && declarator->kind == cdk_array)
8828 {
8829 *nelts = declarator->u.array.bounds;
8830 if (*nelts == error_mark_node)
8831 *nelts = integer_one_node;
8832
8833 if (outer_declarator)
8834 outer_declarator->declarator = declarator->declarator;
8835 else
8836 new_declarator = NULL;
8837 }
8838
8839 return groktypename (&type_specifier_seq, new_declarator, false);
8840 }
8841
8842 /* Parse an (optional) new-declarator.
8843
8844 new-declarator:
8845 ptr-operator new-declarator [opt]
8846 direct-new-declarator
8847
8848 Returns the declarator. */
8849
8850 static cp_declarator *
8851 cp_parser_new_declarator_opt (cp_parser* parser)
8852 {
8853 enum tree_code code;
8854 tree type, std_attributes = NULL_TREE;
8855 cp_cv_quals cv_quals;
8856
8857 /* We don't know if there's a ptr-operator next, or not. */
8858 cp_parser_parse_tentatively (parser);
8859 /* Look for a ptr-operator. */
8860 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8861 /* If that worked, look for more new-declarators. */
8862 if (cp_parser_parse_definitely (parser))
8863 {
8864 cp_declarator *declarator;
8865
8866 /* Parse another optional declarator. */
8867 declarator = cp_parser_new_declarator_opt (parser);
8868
8869 declarator = cp_parser_make_indirect_declarator
8870 (code, type, cv_quals, declarator, std_attributes);
8871
8872 return declarator;
8873 }
8874
8875 /* If the next token is a `[', there is a direct-new-declarator. */
8876 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8877 return cp_parser_direct_new_declarator (parser);
8878
8879 return NULL;
8880 }
8881
8882 /* Parse a direct-new-declarator.
8883
8884 direct-new-declarator:
8885 [ expression ]
8886 direct-new-declarator [constant-expression]
8887
8888 */
8889
8890 static cp_declarator *
8891 cp_parser_direct_new_declarator (cp_parser* parser)
8892 {
8893 cp_declarator *declarator = NULL;
8894
8895 while (true)
8896 {
8897 tree expression;
8898 cp_token *token;
8899
8900 /* Look for the opening `['. */
8901 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8902
8903 token = cp_lexer_peek_token (parser->lexer);
8904 expression = cp_parser_expression (parser);
8905 /* The standard requires that the expression have integral
8906 type. DR 74 adds enumeration types. We believe that the
8907 real intent is that these expressions be handled like the
8908 expression in a `switch' condition, which also allows
8909 classes with a single conversion to integral or
8910 enumeration type. */
8911 if (!processing_template_decl)
8912 {
8913 expression
8914 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8915 expression,
8916 /*complain=*/true);
8917 if (!expression)
8918 {
8919 error_at (token->location,
8920 "expression in new-declarator must have integral "
8921 "or enumeration type");
8922 expression = error_mark_node;
8923 }
8924 }
8925
8926 /* Look for the closing `]'. */
8927 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8928
8929 /* Add this bound to the declarator. */
8930 declarator = make_array_declarator (declarator, expression);
8931
8932 /* If the next token is not a `[', then there are no more
8933 bounds. */
8934 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8935 break;
8936 }
8937
8938 return declarator;
8939 }
8940
8941 /* Parse a new-initializer.
8942
8943 new-initializer:
8944 ( expression-list [opt] )
8945 braced-init-list
8946
8947 Returns a representation of the expression-list. */
8948
8949 static vec<tree, va_gc> *
8950 cp_parser_new_initializer (cp_parser* parser)
8951 {
8952 vec<tree, va_gc> *expression_list;
8953
8954 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8955 {
8956 tree t;
8957 bool expr_non_constant_p;
8958 cp_lexer_set_source_position (parser->lexer);
8959 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8960 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8961 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8962 expression_list = make_tree_vector_single (t);
8963 }
8964 else
8965 expression_list = (cp_parser_parenthesized_expression_list
8966 (parser, non_attr, /*cast_p=*/false,
8967 /*allow_expansion_p=*/true,
8968 /*non_constant_p=*/NULL));
8969
8970 return expression_list;
8971 }
8972
8973 /* Parse a delete-expression.
8974
8975 delete-expression:
8976 :: [opt] delete cast-expression
8977 :: [opt] delete [ ] cast-expression
8978
8979 Returns a representation of the expression. */
8980
8981 static tree
8982 cp_parser_delete_expression (cp_parser* parser)
8983 {
8984 bool global_scope_p;
8985 bool array_p;
8986 tree expression;
8987
8988 /* Look for the optional `::' operator. */
8989 global_scope_p
8990 = (cp_parser_global_scope_opt (parser,
8991 /*current_scope_valid_p=*/false)
8992 != NULL_TREE);
8993 /* Look for the `delete' keyword. */
8994 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8995 /* See if the array syntax is in use. */
8996 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8997 {
8998 /* Consume the `[' token. */
8999 cp_lexer_consume_token (parser->lexer);
9000 /* Look for the `]' token. */
9001 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9002 /* Remember that this is the `[]' construct. */
9003 array_p = true;
9004 }
9005 else
9006 array_p = false;
9007
9008 /* Parse the cast-expression. */
9009 expression = cp_parser_simple_cast_expression (parser);
9010
9011 /* A delete-expression may not appear in an integral constant
9012 expression. */
9013 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9014 return error_mark_node;
9015
9016 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
9017 tf_warning_or_error);
9018 }
9019
9020 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9021 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9022 0 otherwise. */
9023
9024 static int
9025 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9026 {
9027 cp_token *token = cp_lexer_peek_token (parser->lexer);
9028 switch (token->type)
9029 {
9030 case CPP_COMMA:
9031 case CPP_SEMICOLON:
9032 case CPP_QUERY:
9033 case CPP_COLON:
9034 case CPP_CLOSE_SQUARE:
9035 case CPP_CLOSE_PAREN:
9036 case CPP_CLOSE_BRACE:
9037 case CPP_OPEN_BRACE:
9038 case CPP_DOT:
9039 case CPP_DOT_STAR:
9040 case CPP_DEREF:
9041 case CPP_DEREF_STAR:
9042 case CPP_DIV:
9043 case CPP_MOD:
9044 case CPP_LSHIFT:
9045 case CPP_RSHIFT:
9046 case CPP_LESS:
9047 case CPP_GREATER:
9048 case CPP_LESS_EQ:
9049 case CPP_GREATER_EQ:
9050 case CPP_EQ_EQ:
9051 case CPP_NOT_EQ:
9052 case CPP_EQ:
9053 case CPP_MULT_EQ:
9054 case CPP_DIV_EQ:
9055 case CPP_MOD_EQ:
9056 case CPP_PLUS_EQ:
9057 case CPP_MINUS_EQ:
9058 case CPP_RSHIFT_EQ:
9059 case CPP_LSHIFT_EQ:
9060 case CPP_AND_EQ:
9061 case CPP_XOR_EQ:
9062 case CPP_OR_EQ:
9063 case CPP_XOR:
9064 case CPP_OR:
9065 case CPP_OR_OR:
9066 case CPP_EOF:
9067 case CPP_ELLIPSIS:
9068 return 0;
9069
9070 case CPP_OPEN_PAREN:
9071 /* In ((type ()) () the last () isn't a valid cast-expression,
9072 so the whole must be parsed as postfix-expression. */
9073 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9074 != CPP_CLOSE_PAREN;
9075
9076 case CPP_OPEN_SQUARE:
9077 /* '[' may start a primary-expression in obj-c++ and in C++11,
9078 as a lambda-expression, eg, '(void)[]{}'. */
9079 if (cxx_dialect >= cxx11)
9080 return -1;
9081 return c_dialect_objc ();
9082
9083 case CPP_PLUS_PLUS:
9084 case CPP_MINUS_MINUS:
9085 /* '++' and '--' may or may not start a cast-expression:
9086
9087 struct T { void operator++(int); };
9088 void f() { (T())++; }
9089
9090 vs
9091
9092 int a;
9093 (int)++a; */
9094 return -1;
9095
9096 default:
9097 return 1;
9098 }
9099 }
9100
9101 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9102 in the order: const_cast, static_cast, reinterpret_cast.
9103
9104 Don't suggest dynamic_cast.
9105
9106 Return the first legal cast kind found, or NULL otherwise. */
9107
9108 static const char *
9109 get_cast_suggestion (tree dst_type, tree orig_expr)
9110 {
9111 tree trial;
9112
9113 /* Reuse the parser logic by attempting to build the various kinds of
9114 cast, with "complain" disabled.
9115 Identify the first such cast that is valid. */
9116
9117 /* Don't attempt to run such logic within template processing. */
9118 if (processing_template_decl)
9119 return NULL;
9120
9121 /* First try const_cast. */
9122 trial = build_const_cast (dst_type, orig_expr, tf_none);
9123 if (trial != error_mark_node)
9124 return "const_cast";
9125
9126 /* If that fails, try static_cast. */
9127 trial = build_static_cast (dst_type, orig_expr, tf_none);
9128 if (trial != error_mark_node)
9129 return "static_cast";
9130
9131 /* Finally, try reinterpret_cast. */
9132 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
9133 if (trial != error_mark_node)
9134 return "reinterpret_cast";
9135
9136 /* No such cast possible. */
9137 return NULL;
9138 }
9139
9140 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9141 suggesting how to convert a C-style cast of the form:
9142
9143 (DST_TYPE)ORIG_EXPR
9144
9145 to a C++-style cast.
9146
9147 The primary range of RICHLOC is asssumed to be that of the original
9148 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9149 of the parens in the C-style cast. */
9150
9151 static void
9152 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9153 location_t close_paren_loc, tree orig_expr,
9154 tree dst_type)
9155 {
9156 /* This function is non-trivial, so bail out now if the warning isn't
9157 going to be emitted. */
9158 if (!warn_old_style_cast)
9159 return;
9160
9161 /* Try to find a legal C++ cast, trying them in order:
9162 const_cast, static_cast, reinterpret_cast. */
9163 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9164 if (!cast_suggestion)
9165 return;
9166
9167 /* Replace the open paren with "CAST_SUGGESTION<". */
9168 pretty_printer pp;
9169 pp_printf (&pp, "%s<", cast_suggestion);
9170 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9171
9172 /* Replace the close paren with "> (". */
9173 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9174
9175 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9176 rich_loc->add_fixit_insert_after (")");
9177 }
9178
9179
9180 /* Parse a cast-expression.
9181
9182 cast-expression:
9183 unary-expression
9184 ( type-id ) cast-expression
9185
9186 ADDRESS_P is true iff the unary-expression is appearing as the
9187 operand of the `&' operator. CAST_P is true if this expression is
9188 the target of a cast.
9189
9190 Returns a representation of the expression. */
9191
9192 static cp_expr
9193 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9194 bool decltype_p, cp_id_kind * pidk)
9195 {
9196 /* If it's a `(', then we might be looking at a cast. */
9197 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9198 {
9199 tree type = NULL_TREE;
9200 cp_expr expr (NULL_TREE);
9201 int cast_expression = 0;
9202 const char *saved_message;
9203
9204 /* There's no way to know yet whether or not this is a cast.
9205 For example, `(int (3))' is a unary-expression, while `(int)
9206 3' is a cast. So, we resort to parsing tentatively. */
9207 cp_parser_parse_tentatively (parser);
9208 /* Types may not be defined in a cast. */
9209 saved_message = parser->type_definition_forbidden_message;
9210 parser->type_definition_forbidden_message
9211 = G_("types may not be defined in casts");
9212 /* Consume the `('. */
9213 matching_parens parens;
9214 cp_token *open_paren = parens.consume_open (parser);
9215 location_t open_paren_loc = open_paren->location;
9216 location_t close_paren_loc = UNKNOWN_LOCATION;
9217
9218 /* A very tricky bit is that `(struct S) { 3 }' is a
9219 compound-literal (which we permit in C++ as an extension).
9220 But, that construct is not a cast-expression -- it is a
9221 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9222 is legal; if the compound-literal were a cast-expression,
9223 you'd need an extra set of parentheses.) But, if we parse
9224 the type-id, and it happens to be a class-specifier, then we
9225 will commit to the parse at that point, because we cannot
9226 undo the action that is done when creating a new class. So,
9227 then we cannot back up and do a postfix-expression.
9228
9229 Another tricky case is the following (c++/29234):
9230
9231 struct S { void operator () (); };
9232
9233 void foo ()
9234 {
9235 ( S()() );
9236 }
9237
9238 As a type-id we parse the parenthesized S()() as a function
9239 returning a function, groktypename complains and we cannot
9240 back up in this case either.
9241
9242 Therefore, we scan ahead to the closing `)', and check to see
9243 if the tokens after the `)' can start a cast-expression. Otherwise
9244 we are dealing with an unary-expression, a postfix-expression
9245 or something else.
9246
9247 Yet another tricky case, in C++11, is the following (c++/54891):
9248
9249 (void)[]{};
9250
9251 The issue is that usually, besides the case of lambda-expressions,
9252 the parenthesized type-id cannot be followed by '[', and, eg, we
9253 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9254 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9255 we don't commit, we try a cast-expression, then an unary-expression.
9256
9257 Save tokens so that we can put them back. */
9258 cp_lexer_save_tokens (parser->lexer);
9259
9260 /* We may be looking at a cast-expression. */
9261 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9262 /*consume_paren=*/true))
9263 cast_expression
9264 = cp_parser_tokens_start_cast_expression (parser);
9265
9266 /* Roll back the tokens we skipped. */
9267 cp_lexer_rollback_tokens (parser->lexer);
9268 /* If we aren't looking at a cast-expression, simulate an error so
9269 that the call to cp_parser_error_occurred below returns true. */
9270 if (!cast_expression)
9271 cp_parser_simulate_error (parser);
9272 else
9273 {
9274 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9275 parser->in_type_id_in_expr_p = true;
9276 /* Look for the type-id. */
9277 type = cp_parser_type_id (parser);
9278 /* Look for the closing `)'. */
9279 cp_token *close_paren = parens.require_close (parser);
9280 if (close_paren)
9281 close_paren_loc = close_paren->location;
9282 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9283 }
9284
9285 /* Restore the saved message. */
9286 parser->type_definition_forbidden_message = saved_message;
9287
9288 /* At this point this can only be either a cast or a
9289 parenthesized ctor such as `(T ())' that looks like a cast to
9290 function returning T. */
9291 if (!cp_parser_error_occurred (parser))
9292 {
9293 /* Only commit if the cast-expression doesn't start with
9294 '++', '--', or '[' in C++11. */
9295 if (cast_expression > 0)
9296 cp_parser_commit_to_topmost_tentative_parse (parser);
9297
9298 expr = cp_parser_cast_expression (parser,
9299 /*address_p=*/false,
9300 /*cast_p=*/true,
9301 /*decltype_p=*/false,
9302 pidk);
9303
9304 if (cp_parser_parse_definitely (parser))
9305 {
9306 /* Warn about old-style casts, if so requested. */
9307 if (warn_old_style_cast
9308 && !in_system_header_at (input_location)
9309 && !VOID_TYPE_P (type)
9310 && current_lang_name != lang_name_c)
9311 {
9312 gcc_rich_location rich_loc (input_location);
9313 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9314 expr, type);
9315 warning_at (&rich_loc, OPT_Wold_style_cast,
9316 "use of old-style cast to %q#T", type);
9317 }
9318
9319 /* Only type conversions to integral or enumeration types
9320 can be used in constant-expressions. */
9321 if (!cast_valid_in_integral_constant_expression_p (type)
9322 && cp_parser_non_integral_constant_expression (parser,
9323 NIC_CAST))
9324 return error_mark_node;
9325
9326 /* Perform the cast. */
9327 /* Make a location:
9328 (TYPE) EXPR
9329 ^~~~~~~~~~~
9330 with start==caret at the open paren, extending to the
9331 end of "expr". */
9332 location_t cast_loc = make_location (open_paren_loc,
9333 open_paren_loc,
9334 expr.get_finish ());
9335 expr = build_c_cast (cast_loc, type, expr);
9336 return expr;
9337 }
9338 }
9339 else
9340 cp_parser_abort_tentative_parse (parser);
9341 }
9342
9343 /* If we get here, then it's not a cast, so it must be a
9344 unary-expression. */
9345 return cp_parser_unary_expression (parser, pidk, address_p,
9346 cast_p, decltype_p);
9347 }
9348
9349 /* Parse a binary expression of the general form:
9350
9351 pm-expression:
9352 cast-expression
9353 pm-expression .* cast-expression
9354 pm-expression ->* cast-expression
9355
9356 multiplicative-expression:
9357 pm-expression
9358 multiplicative-expression * pm-expression
9359 multiplicative-expression / pm-expression
9360 multiplicative-expression % pm-expression
9361
9362 additive-expression:
9363 multiplicative-expression
9364 additive-expression + multiplicative-expression
9365 additive-expression - multiplicative-expression
9366
9367 shift-expression:
9368 additive-expression
9369 shift-expression << additive-expression
9370 shift-expression >> additive-expression
9371
9372 relational-expression:
9373 shift-expression
9374 relational-expression < shift-expression
9375 relational-expression > shift-expression
9376 relational-expression <= shift-expression
9377 relational-expression >= shift-expression
9378
9379 GNU Extension:
9380
9381 relational-expression:
9382 relational-expression <? shift-expression
9383 relational-expression >? shift-expression
9384
9385 equality-expression:
9386 relational-expression
9387 equality-expression == relational-expression
9388 equality-expression != relational-expression
9389
9390 and-expression:
9391 equality-expression
9392 and-expression & equality-expression
9393
9394 exclusive-or-expression:
9395 and-expression
9396 exclusive-or-expression ^ and-expression
9397
9398 inclusive-or-expression:
9399 exclusive-or-expression
9400 inclusive-or-expression | exclusive-or-expression
9401
9402 logical-and-expression:
9403 inclusive-or-expression
9404 logical-and-expression && inclusive-or-expression
9405
9406 logical-or-expression:
9407 logical-and-expression
9408 logical-or-expression || logical-and-expression
9409
9410 All these are implemented with a single function like:
9411
9412 binary-expression:
9413 simple-cast-expression
9414 binary-expression <token> binary-expression
9415
9416 CAST_P is true if this expression is the target of a cast.
9417
9418 The binops_by_token map is used to get the tree codes for each <token> type.
9419 binary-expressions are associated according to a precedence table. */
9420
9421 #define TOKEN_PRECEDENCE(token) \
9422 (((token->type == CPP_GREATER \
9423 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9424 && !parser->greater_than_is_operator_p) \
9425 ? PREC_NOT_OPERATOR \
9426 : binops_by_token[token->type].prec)
9427
9428 static cp_expr
9429 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9430 bool no_toplevel_fold_p,
9431 bool decltype_p,
9432 enum cp_parser_prec prec,
9433 cp_id_kind * pidk)
9434 {
9435 cp_parser_expression_stack stack;
9436 cp_parser_expression_stack_entry *sp = &stack[0];
9437 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
9438 cp_parser_expression_stack_entry current;
9439 cp_expr rhs;
9440 cp_token *token;
9441 enum tree_code rhs_type;
9442 enum cp_parser_prec new_prec, lookahead_prec;
9443 tree overload;
9444
9445 /* Parse the first expression. */
9446 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9447 ? TRUTH_NOT_EXPR : ERROR_MARK);
9448 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9449 cast_p, decltype_p, pidk);
9450 current.prec = prec;
9451
9452 if (cp_parser_error_occurred (parser))
9453 return error_mark_node;
9454
9455 for (;;)
9456 {
9457 /* Get an operator token. */
9458 token = cp_lexer_peek_token (parser->lexer);
9459
9460 if (warn_cxx11_compat
9461 && token->type == CPP_RSHIFT
9462 && !parser->greater_than_is_operator_p)
9463 {
9464 if (warning_at (token->location, OPT_Wc__11_compat,
9465 "%<>>%> operator is treated"
9466 " as two right angle brackets in C++11"))
9467 inform (token->location,
9468 "suggest parentheses around %<>>%> expression");
9469 }
9470
9471 new_prec = TOKEN_PRECEDENCE (token);
9472 if (new_prec != PREC_NOT_OPERATOR
9473 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9474 /* This is a fold-expression; handle it later. */
9475 new_prec = PREC_NOT_OPERATOR;
9476
9477 /* Popping an entry off the stack means we completed a subexpression:
9478 - either we found a token which is not an operator (`>' where it is not
9479 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9480 will happen repeatedly;
9481 - or, we found an operator which has lower priority. This is the case
9482 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9483 parsing `3 * 4'. */
9484 if (new_prec <= current.prec)
9485 {
9486 if (sp == stack)
9487 break;
9488 else
9489 goto pop;
9490 }
9491
9492 get_rhs:
9493 current.tree_type = binops_by_token[token->type].tree_type;
9494 current.loc = token->location;
9495
9496 /* We used the operator token. */
9497 cp_lexer_consume_token (parser->lexer);
9498
9499 /* For "false && x" or "true || x", x will never be executed;
9500 disable warnings while evaluating it. */
9501 if ((current.tree_type == TRUTH_ANDIF_EXPR
9502 && cp_fully_fold (current.lhs) == truthvalue_false_node)
9503 || (current.tree_type == TRUTH_ORIF_EXPR
9504 && cp_fully_fold (current.lhs) == truthvalue_true_node))
9505 {
9506 disable_warnings_sp = sp;
9507 ++c_inhibit_evaluation_warnings;
9508 }
9509
9510 /* Extract another operand. It may be the RHS of this expression
9511 or the LHS of a new, higher priority expression. */
9512 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9513 ? TRUTH_NOT_EXPR : ERROR_MARK);
9514 rhs = cp_parser_simple_cast_expression (parser);
9515
9516 /* Get another operator token. Look up its precedence to avoid
9517 building a useless (immediately popped) stack entry for common
9518 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9519 token = cp_lexer_peek_token (parser->lexer);
9520 lookahead_prec = TOKEN_PRECEDENCE (token);
9521 if (lookahead_prec != PREC_NOT_OPERATOR
9522 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9523 lookahead_prec = PREC_NOT_OPERATOR;
9524 if (lookahead_prec > new_prec)
9525 {
9526 /* ... and prepare to parse the RHS of the new, higher priority
9527 expression. Since precedence levels on the stack are
9528 monotonically increasing, we do not have to care about
9529 stack overflows. */
9530 *sp = current;
9531 ++sp;
9532 current.lhs = rhs;
9533 current.lhs_type = rhs_type;
9534 current.prec = new_prec;
9535 new_prec = lookahead_prec;
9536 goto get_rhs;
9537
9538 pop:
9539 lookahead_prec = new_prec;
9540 /* If the stack is not empty, we have parsed into LHS the right side
9541 (`4' in the example above) of an expression we had suspended.
9542 We can use the information on the stack to recover the LHS (`3')
9543 from the stack together with the tree code (`MULT_EXPR'), and
9544 the precedence of the higher level subexpression
9545 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9546 which will be used to actually build the additive expression. */
9547 rhs = current.lhs;
9548 rhs_type = current.lhs_type;
9549 --sp;
9550 current = *sp;
9551 }
9552
9553 /* Undo the disabling of warnings done above. */
9554 if (sp == disable_warnings_sp)
9555 {
9556 disable_warnings_sp = NULL;
9557 --c_inhibit_evaluation_warnings;
9558 }
9559
9560 if (warn_logical_not_paren
9561 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9562 && current.lhs_type == TRUTH_NOT_EXPR
9563 /* Avoid warning for !!x == y. */
9564 && (TREE_CODE (current.lhs) != NE_EXPR
9565 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9566 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9567 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9568 /* Avoid warning for !b == y where b is boolean. */
9569 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9570 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9571 != BOOLEAN_TYPE))))
9572 /* Avoid warning for !!b == y where b is boolean. */
9573 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
9574 || TREE_TYPE (current.lhs) == NULL_TREE
9575 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9576 warn_logical_not_parentheses (current.loc, current.tree_type,
9577 current.lhs, maybe_constant_value (rhs));
9578
9579 overload = NULL;
9580
9581 location_t combined_loc = make_location (current.loc,
9582 current.lhs.get_start (),
9583 rhs.get_finish ());
9584
9585 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9586 ERROR_MARK for everything that is not a binary expression.
9587 This makes warn_about_parentheses miss some warnings that
9588 involve unary operators. For unary expressions we should
9589 pass the correct tree_code unless the unary expression was
9590 surrounded by parentheses.
9591 */
9592 if (no_toplevel_fold_p
9593 && lookahead_prec <= current.prec
9594 && sp == stack)
9595 {
9596 if (current.lhs == error_mark_node || rhs == error_mark_node)
9597 current.lhs = error_mark_node;
9598 else
9599 {
9600 current.lhs
9601 = build_min (current.tree_type,
9602 TREE_CODE_CLASS (current.tree_type)
9603 == tcc_comparison
9604 ? boolean_type_node : TREE_TYPE (current.lhs),
9605 current.lhs.get_value (), rhs.get_value ());
9606 SET_EXPR_LOCATION (current.lhs, combined_loc);
9607 }
9608 }
9609 else
9610 {
9611 op_location_t op_loc (current.loc, combined_loc);
9612 current.lhs = build_x_binary_op (op_loc, current.tree_type,
9613 current.lhs, current.lhs_type,
9614 rhs, rhs_type, &overload,
9615 complain_flags (decltype_p));
9616 /* TODO: build_x_binary_op doesn't always honor the location. */
9617 current.lhs.set_location (combined_loc);
9618 }
9619 current.lhs_type = current.tree_type;
9620
9621 /* If the binary operator required the use of an overloaded operator,
9622 then this expression cannot be an integral constant-expression.
9623 An overloaded operator can be used even if both operands are
9624 otherwise permissible in an integral constant-expression if at
9625 least one of the operands is of enumeration type. */
9626
9627 if (overload
9628 && cp_parser_non_integral_constant_expression (parser,
9629 NIC_OVERLOADED))
9630 return error_mark_node;
9631 }
9632
9633 return current.lhs;
9634 }
9635
9636 static cp_expr
9637 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9638 bool no_toplevel_fold_p,
9639 enum cp_parser_prec prec,
9640 cp_id_kind * pidk)
9641 {
9642 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9643 /*decltype*/false, prec, pidk);
9644 }
9645
9646 /* Parse the `? expression : assignment-expression' part of a
9647 conditional-expression. The LOGICAL_OR_EXPR is the
9648 logical-or-expression that started the conditional-expression.
9649 Returns a representation of the entire conditional-expression.
9650
9651 This routine is used by cp_parser_assignment_expression.
9652
9653 ? expression : assignment-expression
9654
9655 GNU Extensions:
9656
9657 ? : assignment-expression */
9658
9659 static tree
9660 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9661 {
9662 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9663 cp_expr assignment_expr;
9664 struct cp_token *token;
9665 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9666
9667 /* Consume the `?' token. */
9668 cp_lexer_consume_token (parser->lexer);
9669 token = cp_lexer_peek_token (parser->lexer);
9670 if (cp_parser_allow_gnu_extensions_p (parser)
9671 && token->type == CPP_COLON)
9672 {
9673 pedwarn (token->location, OPT_Wpedantic,
9674 "ISO C++ does not allow %<?:%> with omitted middle operand");
9675 /* Implicit true clause. */
9676 expr = NULL_TREE;
9677 c_inhibit_evaluation_warnings +=
9678 folded_logical_or_expr == truthvalue_true_node;
9679 warn_for_omitted_condop (token->location, logical_or_expr);
9680 }
9681 else
9682 {
9683 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9684 parser->colon_corrects_to_scope_p = false;
9685 /* Parse the expression. */
9686 c_inhibit_evaluation_warnings +=
9687 folded_logical_or_expr == truthvalue_false_node;
9688 expr = cp_parser_expression (parser);
9689 c_inhibit_evaluation_warnings +=
9690 ((folded_logical_or_expr == truthvalue_true_node)
9691 - (folded_logical_or_expr == truthvalue_false_node));
9692 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9693 }
9694
9695 /* The next token should be a `:'. */
9696 cp_parser_require (parser, CPP_COLON, RT_COLON);
9697 /* Parse the assignment-expression. */
9698 assignment_expr = cp_parser_assignment_expression (parser);
9699 c_inhibit_evaluation_warnings -=
9700 folded_logical_or_expr == truthvalue_true_node;
9701
9702 /* Make a location:
9703 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9704 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9705 with the caret at the "?", ranging from the start of
9706 the logical_or_expr to the end of the assignment_expr. */
9707 loc = make_location (loc,
9708 logical_or_expr.get_start (),
9709 assignment_expr.get_finish ());
9710
9711 /* Build the conditional-expression. */
9712 return build_x_conditional_expr (loc, logical_or_expr,
9713 expr,
9714 assignment_expr,
9715 tf_warning_or_error);
9716 }
9717
9718 /* Parse an assignment-expression.
9719
9720 assignment-expression:
9721 conditional-expression
9722 logical-or-expression assignment-operator assignment_expression
9723 throw-expression
9724
9725 CAST_P is true if this expression is the target of a cast.
9726 DECLTYPE_P is true if this expression is the operand of decltype.
9727
9728 Returns a representation for the expression. */
9729
9730 static cp_expr
9731 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9732 bool cast_p, bool decltype_p)
9733 {
9734 cp_expr expr;
9735
9736 /* If the next token is the `throw' keyword, then we're looking at
9737 a throw-expression. */
9738 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9739 expr = cp_parser_throw_expression (parser);
9740 /* Otherwise, it must be that we are looking at a
9741 logical-or-expression. */
9742 else
9743 {
9744 /* Parse the binary expressions (logical-or-expression). */
9745 expr = cp_parser_binary_expression (parser, cast_p, false,
9746 decltype_p,
9747 PREC_NOT_OPERATOR, pidk);
9748 /* If the next token is a `?' then we're actually looking at a
9749 conditional-expression. */
9750 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9751 return cp_parser_question_colon_clause (parser, expr);
9752 else
9753 {
9754 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9755
9756 /* If it's an assignment-operator, we're using the second
9757 production. */
9758 enum tree_code assignment_operator
9759 = cp_parser_assignment_operator_opt (parser);
9760 if (assignment_operator != ERROR_MARK)
9761 {
9762 bool non_constant_p;
9763
9764 /* Parse the right-hand side of the assignment. */
9765 cp_expr rhs = cp_parser_initializer_clause (parser,
9766 &non_constant_p);
9767
9768 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9769 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9770
9771 /* An assignment may not appear in a
9772 constant-expression. */
9773 if (cp_parser_non_integral_constant_expression (parser,
9774 NIC_ASSIGNMENT))
9775 return error_mark_node;
9776 /* Build the assignment expression. Its default
9777 location:
9778 LHS = RHS
9779 ~~~~^~~~~
9780 is the location of the '=' token as the
9781 caret, ranging from the start of the lhs to the
9782 end of the rhs. */
9783 loc = make_location (loc,
9784 expr.get_start (),
9785 rhs.get_finish ());
9786 expr = build_x_modify_expr (loc, expr,
9787 assignment_operator,
9788 rhs,
9789 complain_flags (decltype_p));
9790 /* TODO: build_x_modify_expr doesn't honor the location,
9791 so we must set it here. */
9792 expr.set_location (loc);
9793 }
9794 }
9795 }
9796
9797 return expr;
9798 }
9799
9800 /* Parse an (optional) assignment-operator.
9801
9802 assignment-operator: one of
9803 = *= /= %= += -= >>= <<= &= ^= |=
9804
9805 GNU Extension:
9806
9807 assignment-operator: one of
9808 <?= >?=
9809
9810 If the next token is an assignment operator, the corresponding tree
9811 code is returned, and the token is consumed. For example, for
9812 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9813 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9814 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9815 operator, ERROR_MARK is returned. */
9816
9817 static enum tree_code
9818 cp_parser_assignment_operator_opt (cp_parser* parser)
9819 {
9820 enum tree_code op;
9821 cp_token *token;
9822
9823 /* Peek at the next token. */
9824 token = cp_lexer_peek_token (parser->lexer);
9825
9826 switch (token->type)
9827 {
9828 case CPP_EQ:
9829 op = NOP_EXPR;
9830 break;
9831
9832 case CPP_MULT_EQ:
9833 op = MULT_EXPR;
9834 break;
9835
9836 case CPP_DIV_EQ:
9837 op = TRUNC_DIV_EXPR;
9838 break;
9839
9840 case CPP_MOD_EQ:
9841 op = TRUNC_MOD_EXPR;
9842 break;
9843
9844 case CPP_PLUS_EQ:
9845 op = PLUS_EXPR;
9846 break;
9847
9848 case CPP_MINUS_EQ:
9849 op = MINUS_EXPR;
9850 break;
9851
9852 case CPP_RSHIFT_EQ:
9853 op = RSHIFT_EXPR;
9854 break;
9855
9856 case CPP_LSHIFT_EQ:
9857 op = LSHIFT_EXPR;
9858 break;
9859
9860 case CPP_AND_EQ:
9861 op = BIT_AND_EXPR;
9862 break;
9863
9864 case CPP_XOR_EQ:
9865 op = BIT_XOR_EXPR;
9866 break;
9867
9868 case CPP_OR_EQ:
9869 op = BIT_IOR_EXPR;
9870 break;
9871
9872 default:
9873 /* Nothing else is an assignment operator. */
9874 op = ERROR_MARK;
9875 }
9876
9877 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9878 if (op != ERROR_MARK
9879 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9880 op = ERROR_MARK;
9881
9882 /* If it was an assignment operator, consume it. */
9883 if (op != ERROR_MARK)
9884 cp_lexer_consume_token (parser->lexer);
9885
9886 return op;
9887 }
9888
9889 /* Parse an expression.
9890
9891 expression:
9892 assignment-expression
9893 expression , assignment-expression
9894
9895 CAST_P is true if this expression is the target of a cast.
9896 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9897 except possibly parenthesized or on the RHS of a comma (N3276).
9898
9899 Returns a representation of the expression. */
9900
9901 static cp_expr
9902 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9903 bool cast_p, bool decltype_p)
9904 {
9905 cp_expr expression = NULL_TREE;
9906 location_t loc = UNKNOWN_LOCATION;
9907
9908 while (true)
9909 {
9910 cp_expr assignment_expression;
9911
9912 /* Parse the next assignment-expression. */
9913 assignment_expression
9914 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9915
9916 /* We don't create a temporary for a call that is the immediate operand
9917 of decltype or on the RHS of a comma. But when we see a comma, we
9918 need to create a temporary for a call on the LHS. */
9919 if (decltype_p && !processing_template_decl
9920 && TREE_CODE (assignment_expression) == CALL_EXPR
9921 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9922 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9923 assignment_expression
9924 = build_cplus_new (TREE_TYPE (assignment_expression),
9925 assignment_expression, tf_warning_or_error);
9926
9927 /* If this is the first assignment-expression, we can just
9928 save it away. */
9929 if (!expression)
9930 expression = assignment_expression;
9931 else
9932 {
9933 /* Create a location with caret at the comma, ranging
9934 from the start of the LHS to the end of the RHS. */
9935 loc = make_location (loc,
9936 expression.get_start (),
9937 assignment_expression.get_finish ());
9938 expression = build_x_compound_expr (loc, expression,
9939 assignment_expression,
9940 complain_flags (decltype_p));
9941 expression.set_location (loc);
9942 }
9943 /* If the next token is not a comma, or we're in a fold-expression, then
9944 we are done with the expression. */
9945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9946 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9947 break;
9948 /* Consume the `,'. */
9949 loc = cp_lexer_peek_token (parser->lexer)->location;
9950 cp_lexer_consume_token (parser->lexer);
9951 /* A comma operator cannot appear in a constant-expression. */
9952 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9953 expression = error_mark_node;
9954 }
9955
9956 return expression;
9957 }
9958
9959 /* Parse a constant-expression.
9960
9961 constant-expression:
9962 conditional-expression
9963
9964 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9965 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9966 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9967 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9968 only parse a conditional-expression, otherwise parse an
9969 assignment-expression. See below for rationale. */
9970
9971 static cp_expr
9972 cp_parser_constant_expression (cp_parser* parser,
9973 bool allow_non_constant_p,
9974 bool *non_constant_p,
9975 bool strict_p)
9976 {
9977 bool saved_integral_constant_expression_p;
9978 bool saved_allow_non_integral_constant_expression_p;
9979 bool saved_non_integral_constant_expression_p;
9980 cp_expr expression;
9981
9982 /* It might seem that we could simply parse the
9983 conditional-expression, and then check to see if it were
9984 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9985 one that the compiler can figure out is constant, possibly after
9986 doing some simplifications or optimizations. The standard has a
9987 precise definition of constant-expression, and we must honor
9988 that, even though it is somewhat more restrictive.
9989
9990 For example:
9991
9992 int i[(2, 3)];
9993
9994 is not a legal declaration, because `(2, 3)' is not a
9995 constant-expression. The `,' operator is forbidden in a
9996 constant-expression. However, GCC's constant-folding machinery
9997 will fold this operation to an INTEGER_CST for `3'. */
9998
9999 /* Save the old settings. */
10000 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10001 saved_allow_non_integral_constant_expression_p
10002 = parser->allow_non_integral_constant_expression_p;
10003 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10004 /* We are now parsing a constant-expression. */
10005 parser->integral_constant_expression_p = true;
10006 parser->allow_non_integral_constant_expression_p
10007 = (allow_non_constant_p || cxx_dialect >= cxx11);
10008 parser->non_integral_constant_expression_p = false;
10009 /* Although the grammar says "conditional-expression", when not STRICT_P,
10010 we parse an "assignment-expression", which also permits
10011 "throw-expression" and the use of assignment operators. In the case
10012 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10013 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10014 actually essential that we look for an assignment-expression.
10015 For example, cp_parser_initializer_clauses uses this function to
10016 determine whether a particular assignment-expression is in fact
10017 constant. */
10018 if (strict_p)
10019 {
10020 /* Parse the binary expressions (logical-or-expression). */
10021 expression = cp_parser_binary_expression (parser, false, false, false,
10022 PREC_NOT_OPERATOR, NULL);
10023 /* If the next token is a `?' then we're actually looking at
10024 a conditional-expression; otherwise we're done. */
10025 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10026 expression = cp_parser_question_colon_clause (parser, expression);
10027 }
10028 else
10029 expression = cp_parser_assignment_expression (parser);
10030 /* Restore the old settings. */
10031 parser->integral_constant_expression_p
10032 = saved_integral_constant_expression_p;
10033 parser->allow_non_integral_constant_expression_p
10034 = saved_allow_non_integral_constant_expression_p;
10035 if (cxx_dialect >= cxx11)
10036 {
10037 /* Require an rvalue constant expression here; that's what our
10038 callers expect. Reference constant expressions are handled
10039 separately in e.g. cp_parser_template_argument. */
10040 tree decay = expression;
10041 if (TREE_TYPE (expression)
10042 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10043 decay = build_address (expression);
10044 bool is_const = potential_rvalue_constant_expression (decay);
10045 parser->non_integral_constant_expression_p = !is_const;
10046 if (!is_const && !allow_non_constant_p)
10047 require_potential_rvalue_constant_expression (decay);
10048 }
10049 if (allow_non_constant_p)
10050 *non_constant_p = parser->non_integral_constant_expression_p;
10051 parser->non_integral_constant_expression_p
10052 = saved_non_integral_constant_expression_p;
10053
10054 return expression;
10055 }
10056
10057 /* Parse __builtin_offsetof.
10058
10059 offsetof-expression:
10060 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10061
10062 offsetof-member-designator:
10063 id-expression
10064 | offsetof-member-designator "." id-expression
10065 | offsetof-member-designator "[" expression "]"
10066 | offsetof-member-designator "->" id-expression */
10067
10068 static cp_expr
10069 cp_parser_builtin_offsetof (cp_parser *parser)
10070 {
10071 int save_ice_p, save_non_ice_p;
10072 tree type;
10073 cp_expr expr;
10074 cp_id_kind dummy;
10075 cp_token *token;
10076 location_t finish_loc;
10077
10078 /* We're about to accept non-integral-constant things, but will
10079 definitely yield an integral constant expression. Save and
10080 restore these values around our local parsing. */
10081 save_ice_p = parser->integral_constant_expression_p;
10082 save_non_ice_p = parser->non_integral_constant_expression_p;
10083
10084 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10085
10086 /* Consume the "__builtin_offsetof" token. */
10087 cp_lexer_consume_token (parser->lexer);
10088 /* Consume the opening `('. */
10089 matching_parens parens;
10090 parens.require_open (parser);
10091 /* Parse the type-id. */
10092 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10093 {
10094 const char *saved_message = parser->type_definition_forbidden_message;
10095 parser->type_definition_forbidden_message
10096 = G_("types may not be defined within %<__builtin_offsetof%>");
10097 type = cp_parser_type_id (parser);
10098 parser->type_definition_forbidden_message = saved_message;
10099 }
10100 /* Look for the `,'. */
10101 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10102 token = cp_lexer_peek_token (parser->lexer);
10103
10104 /* Build the (type *)null that begins the traditional offsetof macro. */
10105 tree object_ptr
10106 = build_static_cast (build_pointer_type (type), null_pointer_node,
10107 tf_warning_or_error);
10108
10109 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10110 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10111 true, &dummy, token->location);
10112 while (true)
10113 {
10114 token = cp_lexer_peek_token (parser->lexer);
10115 switch (token->type)
10116 {
10117 case CPP_OPEN_SQUARE:
10118 /* offsetof-member-designator "[" expression "]" */
10119 expr = cp_parser_postfix_open_square_expression (parser, expr,
10120 true, false);
10121 break;
10122
10123 case CPP_DEREF:
10124 /* offsetof-member-designator "->" identifier */
10125 expr = grok_array_decl (token->location, expr,
10126 integer_zero_node, false);
10127 /* FALLTHRU */
10128
10129 case CPP_DOT:
10130 /* offsetof-member-designator "." identifier */
10131 cp_lexer_consume_token (parser->lexer);
10132 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10133 expr, true, &dummy,
10134 token->location);
10135 break;
10136
10137 case CPP_CLOSE_PAREN:
10138 /* Consume the ")" token. */
10139 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10140 cp_lexer_consume_token (parser->lexer);
10141 goto success;
10142
10143 default:
10144 /* Error. We know the following require will fail, but
10145 that gives the proper error message. */
10146 parens.require_close (parser);
10147 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10148 expr = error_mark_node;
10149 goto failure;
10150 }
10151 }
10152
10153 success:
10154 /* Make a location of the form:
10155 __builtin_offsetof (struct s, f)
10156 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10157 with caret at the type-id, ranging from the start of the
10158 "_builtin_offsetof" token to the close paren. */
10159 loc = make_location (loc, start_loc, finish_loc);
10160 /* The result will be an INTEGER_CST, so we need to explicitly
10161 preserve the location. */
10162 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10163
10164 failure:
10165 parser->integral_constant_expression_p = save_ice_p;
10166 parser->non_integral_constant_expression_p = save_non_ice_p;
10167
10168 expr = expr.maybe_add_location_wrapper ();
10169 return expr;
10170 }
10171
10172 /* Parse a trait expression.
10173
10174 Returns a representation of the expression, the underlying type
10175 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10176
10177 static cp_expr
10178 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10179 {
10180 cp_trait_kind kind;
10181 tree type1, type2 = NULL_TREE;
10182 bool binary = false;
10183 bool variadic = false;
10184
10185 switch (keyword)
10186 {
10187 case RID_HAS_NOTHROW_ASSIGN:
10188 kind = CPTK_HAS_NOTHROW_ASSIGN;
10189 break;
10190 case RID_HAS_NOTHROW_CONSTRUCTOR:
10191 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10192 break;
10193 case RID_HAS_NOTHROW_COPY:
10194 kind = CPTK_HAS_NOTHROW_COPY;
10195 break;
10196 case RID_HAS_TRIVIAL_ASSIGN:
10197 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10198 break;
10199 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10200 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10201 break;
10202 case RID_HAS_TRIVIAL_COPY:
10203 kind = CPTK_HAS_TRIVIAL_COPY;
10204 break;
10205 case RID_HAS_TRIVIAL_DESTRUCTOR:
10206 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10207 break;
10208 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10209 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10210 break;
10211 case RID_HAS_VIRTUAL_DESTRUCTOR:
10212 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10213 break;
10214 case RID_IS_ABSTRACT:
10215 kind = CPTK_IS_ABSTRACT;
10216 break;
10217 case RID_IS_AGGREGATE:
10218 kind = CPTK_IS_AGGREGATE;
10219 break;
10220 case RID_IS_BASE_OF:
10221 kind = CPTK_IS_BASE_OF;
10222 binary = true;
10223 break;
10224 case RID_IS_CLASS:
10225 kind = CPTK_IS_CLASS;
10226 break;
10227 case RID_IS_EMPTY:
10228 kind = CPTK_IS_EMPTY;
10229 break;
10230 case RID_IS_ENUM:
10231 kind = CPTK_IS_ENUM;
10232 break;
10233 case RID_IS_FINAL:
10234 kind = CPTK_IS_FINAL;
10235 break;
10236 case RID_IS_LITERAL_TYPE:
10237 kind = CPTK_IS_LITERAL_TYPE;
10238 break;
10239 case RID_IS_POD:
10240 kind = CPTK_IS_POD;
10241 break;
10242 case RID_IS_POLYMORPHIC:
10243 kind = CPTK_IS_POLYMORPHIC;
10244 break;
10245 case RID_IS_SAME_AS:
10246 kind = CPTK_IS_SAME_AS;
10247 binary = true;
10248 break;
10249 case RID_IS_STD_LAYOUT:
10250 kind = CPTK_IS_STD_LAYOUT;
10251 break;
10252 case RID_IS_TRIVIAL:
10253 kind = CPTK_IS_TRIVIAL;
10254 break;
10255 case RID_IS_TRIVIALLY_ASSIGNABLE:
10256 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10257 binary = true;
10258 break;
10259 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10260 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10261 variadic = true;
10262 break;
10263 case RID_IS_TRIVIALLY_COPYABLE:
10264 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10265 break;
10266 case RID_IS_UNION:
10267 kind = CPTK_IS_UNION;
10268 break;
10269 case RID_UNDERLYING_TYPE:
10270 kind = CPTK_UNDERLYING_TYPE;
10271 break;
10272 case RID_BASES:
10273 kind = CPTK_BASES;
10274 break;
10275 case RID_DIRECT_BASES:
10276 kind = CPTK_DIRECT_BASES;
10277 break;
10278 case RID_IS_ASSIGNABLE:
10279 kind = CPTK_IS_ASSIGNABLE;
10280 binary = true;
10281 break;
10282 case RID_IS_CONSTRUCTIBLE:
10283 kind = CPTK_IS_CONSTRUCTIBLE;
10284 variadic = true;
10285 break;
10286 default:
10287 gcc_unreachable ();
10288 }
10289
10290 /* Get location of initial token. */
10291 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10292
10293 /* Consume the token. */
10294 cp_lexer_consume_token (parser->lexer);
10295
10296 matching_parens parens;
10297 parens.require_open (parser);
10298
10299 {
10300 type_id_in_expr_sentinel s (parser);
10301 type1 = cp_parser_type_id (parser);
10302 }
10303
10304 if (type1 == error_mark_node)
10305 return error_mark_node;
10306
10307 if (binary)
10308 {
10309 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10310
10311 {
10312 type_id_in_expr_sentinel s (parser);
10313 type2 = cp_parser_type_id (parser);
10314 }
10315
10316 if (type2 == error_mark_node)
10317 return error_mark_node;
10318 }
10319 else if (variadic)
10320 {
10321 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10322 {
10323 cp_lexer_consume_token (parser->lexer);
10324 tree elt = cp_parser_type_id (parser);
10325 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10326 {
10327 cp_lexer_consume_token (parser->lexer);
10328 elt = make_pack_expansion (elt);
10329 }
10330 if (elt == error_mark_node)
10331 return error_mark_node;
10332 type2 = tree_cons (NULL_TREE, elt, type2);
10333 }
10334 }
10335
10336 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10337 parens.require_close (parser);
10338
10339 /* Construct a location of the form:
10340 __is_trivially_copyable(_Tp)
10341 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10342 with start == caret, finishing at the close-paren. */
10343 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10344
10345 /* Complete the trait expression, which may mean either processing
10346 the trait expr now or saving it for template instantiation. */
10347 switch (kind)
10348 {
10349 case CPTK_UNDERLYING_TYPE:
10350 return cp_expr (finish_underlying_type (type1), trait_loc);
10351 case CPTK_BASES:
10352 return cp_expr (finish_bases (type1, false), trait_loc);
10353 case CPTK_DIRECT_BASES:
10354 return cp_expr (finish_bases (type1, true), trait_loc);
10355 default:
10356 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10357 }
10358 }
10359
10360 /* Parse a lambda expression.
10361
10362 lambda-expression:
10363 lambda-introducer lambda-declarator [opt] compound-statement
10364
10365 Returns a representation of the expression. */
10366
10367 static cp_expr
10368 cp_parser_lambda_expression (cp_parser* parser)
10369 {
10370 tree lambda_expr = build_lambda_expr ();
10371 tree type;
10372 bool ok = true;
10373 cp_token *token = cp_lexer_peek_token (parser->lexer);
10374 cp_token_position start = 0;
10375
10376 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10377
10378 if (cxx_dialect >= cxx2a)
10379 /* C++20 allows lambdas in unevaluated context. */;
10380 else if (cp_unevaluated_operand)
10381 {
10382 if (!token->error_reported)
10383 {
10384 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10385 "lambda-expression in unevaluated context"
10386 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10387 token->error_reported = true;
10388 }
10389 ok = false;
10390 }
10391 else if (parser->in_template_argument_list_p || processing_template_parmlist)
10392 {
10393 if (!token->error_reported)
10394 {
10395 error_at (token->location, "lambda-expression in template-argument"
10396 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10397 token->error_reported = true;
10398 }
10399 ok = false;
10400 }
10401
10402 /* We may be in the middle of deferred access check. Disable
10403 it now. */
10404 push_deferring_access_checks (dk_no_deferred);
10405
10406 cp_parser_lambda_introducer (parser, lambda_expr);
10407 if (cp_parser_error_occurred (parser))
10408 return error_mark_node;
10409
10410 type = begin_lambda_type (lambda_expr);
10411 if (type == error_mark_node)
10412 return error_mark_node;
10413
10414 record_lambda_scope (lambda_expr);
10415
10416 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10417 determine_visibility (TYPE_NAME (type));
10418
10419 /* Now that we've started the type, add the capture fields for any
10420 explicit captures. */
10421 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10422
10423 {
10424 /* Inside the class, surrounding template-parameter-lists do not apply. */
10425 unsigned int saved_num_template_parameter_lists
10426 = parser->num_template_parameter_lists;
10427 unsigned char in_statement = parser->in_statement;
10428 bool in_switch_statement_p = parser->in_switch_statement_p;
10429 bool fully_implicit_function_template_p
10430 = parser->fully_implicit_function_template_p;
10431 tree implicit_template_parms = parser->implicit_template_parms;
10432 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10433 bool auto_is_implicit_function_template_parm_p
10434 = parser->auto_is_implicit_function_template_parm_p;
10435
10436 parser->num_template_parameter_lists = 0;
10437 parser->in_statement = 0;
10438 parser->in_switch_statement_p = false;
10439 parser->fully_implicit_function_template_p = false;
10440 parser->implicit_template_parms = 0;
10441 parser->implicit_template_scope = 0;
10442 parser->auto_is_implicit_function_template_parm_p = false;
10443
10444 /* By virtue of defining a local class, a lambda expression has access to
10445 the private variables of enclosing classes. */
10446
10447 if (cp_parser_start_tentative_firewall (parser))
10448 start = token;
10449
10450 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10451
10452 if (ok && cp_parser_error_occurred (parser))
10453 ok = false;
10454
10455 if (ok)
10456 {
10457 cp_parser_lambda_body (parser, lambda_expr);
10458 }
10459 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10460 {
10461 if (cp_parser_skip_to_closing_brace (parser))
10462 cp_lexer_consume_token (parser->lexer);
10463 }
10464
10465 /* The capture list was built up in reverse order; fix that now. */
10466 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10467 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10468
10469 if (ok)
10470 maybe_add_lambda_conv_op (type);
10471
10472 type = finish_struct (type, /*attributes=*/NULL_TREE);
10473
10474 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10475 parser->in_statement = in_statement;
10476 parser->in_switch_statement_p = in_switch_statement_p;
10477 parser->fully_implicit_function_template_p
10478 = fully_implicit_function_template_p;
10479 parser->implicit_template_parms = implicit_template_parms;
10480 parser->implicit_template_scope = implicit_template_scope;
10481 parser->auto_is_implicit_function_template_parm_p
10482 = auto_is_implicit_function_template_parm_p;
10483 }
10484
10485 /* This field is only used during parsing of the lambda. */
10486 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10487
10488 /* This lambda shouldn't have any proxies left at this point. */
10489 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10490 /* And now that we're done, push proxies for an enclosing lambda. */
10491 insert_pending_capture_proxies ();
10492
10493 /* Update the lambda expression to a range. */
10494 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10495 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10496 token->location,
10497 end_tok->location);
10498
10499 if (ok)
10500 lambda_expr = build_lambda_object (lambda_expr);
10501 else
10502 lambda_expr = error_mark_node;
10503
10504 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10505
10506 pop_deferring_access_checks ();
10507
10508 return lambda_expr;
10509 }
10510
10511 /* Parse the beginning of a lambda expression.
10512
10513 lambda-introducer:
10514 [ lambda-capture [opt] ]
10515
10516 LAMBDA_EXPR is the current representation of the lambda expression. */
10517
10518 static void
10519 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10520 {
10521 /* Need commas after the first capture. */
10522 bool first = true;
10523
10524 /* Eat the leading `['. */
10525 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10526
10527 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10528 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10529 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
10530 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10531 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10532 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10533 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10534
10535 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10536 {
10537 cp_lexer_consume_token (parser->lexer);
10538 first = false;
10539
10540 if (!(at_function_scope_p () || parsing_nsdmi ()))
10541 error ("non-local lambda expression cannot have a capture-default");
10542 }
10543
10544 hash_set<tree, true> ids;
10545 tree first_capture_id = NULL_TREE;
10546 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10547 {
10548 cp_token* capture_token;
10549 tree capture_id;
10550 tree capture_init_expr;
10551 cp_id_kind idk = CP_ID_KIND_NONE;
10552 bool explicit_init_p = false;
10553
10554 enum capture_kind_type
10555 {
10556 BY_COPY,
10557 BY_REFERENCE
10558 };
10559 enum capture_kind_type capture_kind = BY_COPY;
10560
10561 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10562 {
10563 error ("expected end of capture-list");
10564 return;
10565 }
10566
10567 if (first)
10568 first = false;
10569 else
10570 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10571
10572 /* Possibly capture `this'. */
10573 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10574 {
10575 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10576 if (cxx_dialect < cxx2a
10577 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10578 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10579 "with by-copy capture default");
10580 cp_lexer_consume_token (parser->lexer);
10581 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10582 pedwarn (input_location, 0,
10583 "already captured %qD in lambda expression",
10584 this_identifier);
10585 else
10586 add_capture (lambda_expr, /*id=*/this_identifier,
10587 /*initializer=*/finish_this_expr (),
10588 /*by_reference_p=*/true, explicit_init_p);
10589 continue;
10590 }
10591
10592 /* Possibly capture `*this'. */
10593 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10594 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10595 {
10596 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10597 if (cxx_dialect < cxx17)
10598 pedwarn (loc, 0, "%<*this%> capture only available with "
10599 "%<-std=c++17%> or %<-std=gnu++17%>");
10600 cp_lexer_consume_token (parser->lexer);
10601 cp_lexer_consume_token (parser->lexer);
10602 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10603 pedwarn (input_location, 0,
10604 "already captured %qD in lambda expression",
10605 this_identifier);
10606 else
10607 add_capture (lambda_expr, /*id=*/this_identifier,
10608 /*initializer=*/finish_this_expr (),
10609 /*by_reference_p=*/false, explicit_init_p);
10610 continue;
10611 }
10612
10613 /* But reject `&this'. */
10614 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10615 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10616 {
10617 error_at (cp_lexer_peek_token (parser->lexer)->location,
10618 "%<this%> cannot be captured by reference");
10619 cp_lexer_consume_token (parser->lexer);
10620 cp_lexer_consume_token (parser->lexer);
10621 continue;
10622 }
10623
10624 bool init_pack_expansion = false;
10625 location_t ellipsis_loc = UNKNOWN_LOCATION;
10626 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10627 {
10628 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
10629 if (cxx_dialect < cxx2a)
10630 pedwarn (ellipsis_loc, 0, "pack init-capture only available with "
10631 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10632 cp_lexer_consume_token (parser->lexer);
10633 init_pack_expansion = true;
10634 }
10635
10636 /* Remember whether we want to capture as a reference or not. */
10637 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10638 {
10639 capture_kind = BY_REFERENCE;
10640 cp_lexer_consume_token (parser->lexer);
10641 }
10642
10643 /* Get the identifier. */
10644 capture_token = cp_lexer_peek_token (parser->lexer);
10645 capture_id = cp_parser_identifier (parser);
10646
10647 if (capture_id == error_mark_node)
10648 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10649 delimiters, but I modified this to stop on unnested ']' as well. It
10650 was already changed to stop on unnested '}', so the
10651 "closing_parenthesis" name is no more misleading with my change. */
10652 {
10653 cp_parser_skip_to_closing_parenthesis (parser,
10654 /*recovering=*/true,
10655 /*or_comma=*/true,
10656 /*consume_paren=*/true);
10657 break;
10658 }
10659
10660 /* Find the initializer for this capture. */
10661 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10662 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10663 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10664 {
10665 bool direct, non_constant;
10666 /* An explicit initializer exists. */
10667 if (cxx_dialect < cxx14)
10668 pedwarn (input_location, 0,
10669 "lambda capture initializers "
10670 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10671 capture_init_expr = cp_parser_initializer (parser, &direct,
10672 &non_constant, true);
10673 explicit_init_p = true;
10674 if (capture_init_expr == NULL_TREE)
10675 {
10676 error ("empty initializer for lambda init-capture");
10677 capture_init_expr = error_mark_node;
10678 }
10679 if (init_pack_expansion)
10680 capture_init_expr = make_pack_expansion (capture_init_expr);
10681 }
10682 else
10683 {
10684 const char* error_msg;
10685
10686 /* Turn the identifier into an id-expression. */
10687 capture_init_expr
10688 = cp_parser_lookup_name_simple (parser, capture_id,
10689 capture_token->location);
10690
10691 if (capture_init_expr == error_mark_node)
10692 {
10693 unqualified_name_lookup_error (capture_id);
10694 continue;
10695 }
10696 else if (!VAR_P (capture_init_expr)
10697 && TREE_CODE (capture_init_expr) != PARM_DECL)
10698 {
10699 error_at (capture_token->location,
10700 "capture of non-variable %qE",
10701 capture_init_expr);
10702 if (DECL_P (capture_init_expr))
10703 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10704 "%q#D declared here", capture_init_expr);
10705 continue;
10706 }
10707 if (VAR_P (capture_init_expr)
10708 && decl_storage_duration (capture_init_expr) != dk_auto)
10709 {
10710 if (pedwarn (capture_token->location, 0, "capture of variable "
10711 "%qD with non-automatic storage duration",
10712 capture_init_expr))
10713 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10714 "%q#D declared here", capture_init_expr);
10715 continue;
10716 }
10717
10718 capture_init_expr
10719 = finish_id_expression
10720 (capture_id,
10721 capture_init_expr,
10722 parser->scope,
10723 &idk,
10724 /*integral_constant_expression_p=*/false,
10725 /*allow_non_integral_constant_expression_p=*/false,
10726 /*non_integral_constant_expression_p=*/NULL,
10727 /*template_p=*/false,
10728 /*done=*/true,
10729 /*address_p=*/false,
10730 /*template_arg_p=*/false,
10731 &error_msg,
10732 capture_token->location);
10733
10734 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10735 {
10736 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10737 cp_lexer_consume_token (parser->lexer);
10738 capture_init_expr = make_pack_expansion (capture_init_expr);
10739 if (init_pack_expansion)
10740 {
10741 /* If what follows is an initializer, the second '...' is
10742 invalid. But for cases like [...xs...], the first one
10743 is invalid. */
10744 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10745 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10746 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10747 ellipsis_loc = loc;
10748 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
10749 continue;
10750 }
10751 }
10752 }
10753
10754 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10755 && !explicit_init_p)
10756 {
10757 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10758 && capture_kind == BY_COPY)
10759 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10760 "of %qD redundant with by-copy capture default",
10761 capture_id);
10762 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10763 && capture_kind == BY_REFERENCE)
10764 pedwarn (capture_token->location, 0, "explicit by-reference "
10765 "capture of %qD redundant with by-reference capture "
10766 "default", capture_id);
10767 }
10768
10769 /* Check for duplicates.
10770 Optimize for the zero or one explicit captures cases and only create
10771 the hash_set after adding second capture. */
10772 bool found = false;
10773 if (!ids.is_empty ())
10774 found = ids.add (capture_id);
10775 else if (first_capture_id == NULL_TREE)
10776 first_capture_id = capture_id;
10777 else if (capture_id == first_capture_id)
10778 found = true;
10779 else
10780 {
10781 ids.add (first_capture_id);
10782 ids.add (capture_id);
10783 }
10784 if (found)
10785 pedwarn (input_location, 0,
10786 "already captured %qD in lambda expression", capture_id);
10787 else
10788 add_capture (lambda_expr, capture_id, capture_init_expr,
10789 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10790 explicit_init_p);
10791
10792 /* If there is any qualification still in effect, clear it
10793 now; we will be starting fresh with the next capture. */
10794 parser->scope = NULL_TREE;
10795 parser->qualifying_scope = NULL_TREE;
10796 parser->object_scope = NULL_TREE;
10797 }
10798
10799 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10800 }
10801
10802 /* Parse the (optional) middle of a lambda expression.
10803
10804 lambda-declarator:
10805 < template-parameter-list [opt] >
10806 ( parameter-declaration-clause [opt] )
10807 attribute-specifier [opt]
10808 decl-specifier-seq [opt]
10809 exception-specification [opt]
10810 lambda-return-type-clause [opt]
10811
10812 LAMBDA_EXPR is the current representation of the lambda expression. */
10813
10814 static bool
10815 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10816 {
10817 /* 5.1.1.4 of the standard says:
10818 If a lambda-expression does not include a lambda-declarator, it is as if
10819 the lambda-declarator were ().
10820 This means an empty parameter list, no attributes, and no exception
10821 specification. */
10822 tree param_list = void_list_node;
10823 tree std_attrs = NULL_TREE;
10824 tree gnu_attrs = NULL_TREE;
10825 tree exception_spec = NULL_TREE;
10826 tree template_param_list = NULL_TREE;
10827 tree tx_qual = NULL_TREE;
10828 tree return_type = NULL_TREE;
10829 cp_decl_specifier_seq lambda_specs;
10830 clear_decl_specs (&lambda_specs);
10831
10832 /* The template-parameter-list is optional, but must begin with
10833 an opening angle if present. */
10834 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10835 {
10836 if (cxx_dialect < cxx14)
10837 pedwarn (parser->lexer->next_token->location, 0,
10838 "lambda templates are only available with "
10839 "%<-std=c++14%> or %<-std=gnu++14%>");
10840 else if (cxx_dialect < cxx2a)
10841 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10842 "lambda templates are only available with "
10843 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10844
10845 cp_lexer_consume_token (parser->lexer);
10846
10847 template_param_list = cp_parser_template_parameter_list (parser);
10848
10849 cp_parser_skip_to_end_of_template_parameter_list (parser);
10850
10851 /* We just processed one more parameter list. */
10852 ++parser->num_template_parameter_lists;
10853 }
10854
10855 /* The parameter-declaration-clause is optional (unless
10856 template-parameter-list was given), but must begin with an
10857 opening parenthesis if present. */
10858 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10859 {
10860 matching_parens parens;
10861 parens.consume_open (parser);
10862
10863 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10864
10865 /* Parse parameters. */
10866 param_list
10867 = cp_parser_parameter_declaration_clause
10868 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
10869
10870 /* Default arguments shall not be specified in the
10871 parameter-declaration-clause of a lambda-declarator. */
10872 if (cxx_dialect < cxx14)
10873 for (tree t = param_list; t; t = TREE_CHAIN (t))
10874 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10875 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10876 "default argument specified for lambda parameter");
10877
10878 parens.require_close (parser);
10879
10880 /* In the decl-specifier-seq of the lambda-declarator, each
10881 decl-specifier shall either be mutable or constexpr. */
10882 int declares_class_or_enum;
10883 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
10884 && !cp_next_tokens_can_be_gnu_attribute_p (parser))
10885 cp_parser_decl_specifier_seq (parser,
10886 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10887 &lambda_specs, &declares_class_or_enum);
10888 if (lambda_specs.storage_class == sc_mutable)
10889 {
10890 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10891 if (lambda_specs.conflicting_specifiers_p)
10892 error_at (lambda_specs.locations[ds_storage_class],
10893 "duplicate %<mutable%>");
10894 }
10895
10896 tx_qual = cp_parser_tx_qualifier_opt (parser);
10897
10898 /* Parse optional exception specification. */
10899 exception_spec = cp_parser_exception_specification_opt (parser);
10900
10901 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10902
10903 /* Parse optional trailing return type. */
10904 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10905 {
10906 cp_lexer_consume_token (parser->lexer);
10907 return_type = cp_parser_trailing_type_id (parser);
10908 }
10909
10910 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
10911 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
10912
10913 /* The function parameters must be in scope all the way until after the
10914 trailing-return-type in case of decltype. */
10915 pop_bindings_and_leave_scope ();
10916 }
10917 else if (template_param_list != NULL_TREE) // generate diagnostic
10918 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10919
10920 /* Create the function call operator.
10921
10922 Messing with declarators like this is no uglier than building up the
10923 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10924 other code. */
10925 {
10926 cp_decl_specifier_seq return_type_specs;
10927 cp_declarator* declarator;
10928 tree fco;
10929 int quals;
10930 void *p;
10931
10932 clear_decl_specs (&return_type_specs);
10933 return_type_specs.type = make_auto ();
10934
10935 if (lambda_specs.locations[ds_constexpr])
10936 {
10937 if (cxx_dialect >= cxx17)
10938 return_type_specs.locations[ds_constexpr]
10939 = lambda_specs.locations[ds_constexpr];
10940 else
10941 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10942 "lambda only available with %<-std=c++17%> or "
10943 "%<-std=gnu++17%>");
10944 }
10945
10946 p = obstack_alloc (&declarator_obstack, 0);
10947
10948 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
10949 LAMBDA_EXPR_LOCATION (lambda_expr));
10950
10951 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10952 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10953 declarator = make_call_declarator (declarator, param_list, quals,
10954 VIRT_SPEC_UNSPECIFIED,
10955 REF_QUAL_NONE,
10956 tx_qual,
10957 exception_spec,
10958 return_type,
10959 /*requires_clause*/NULL_TREE);
10960 declarator->std_attributes = std_attrs;
10961
10962 fco = grokmethod (&return_type_specs,
10963 declarator,
10964 gnu_attrs);
10965 if (fco != error_mark_node)
10966 {
10967 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10968 DECL_ARTIFICIAL (fco) = 1;
10969 /* Give the object parameter a different name. */
10970 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10971 DECL_LAMBDA_FUNCTION (fco) = 1;
10972 }
10973 if (template_param_list)
10974 {
10975 fco = finish_member_template_decl (fco);
10976 finish_template_decl (template_param_list);
10977 --parser->num_template_parameter_lists;
10978 }
10979 else if (parser->fully_implicit_function_template_p)
10980 fco = finish_fully_implicit_template (parser, fco);
10981
10982 finish_member_declaration (fco);
10983
10984 obstack_free (&declarator_obstack, p);
10985
10986 return (fco != error_mark_node);
10987 }
10988 }
10989
10990 /* Parse the body of a lambda expression, which is simply
10991
10992 compound-statement
10993
10994 but which requires special handling.
10995 LAMBDA_EXPR is the current representation of the lambda expression. */
10996
10997 static void
10998 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10999 {
11000 bool nested = (current_function_decl != NULL_TREE);
11001 unsigned char local_variables_forbidden_p
11002 = parser->local_variables_forbidden_p;
11003 bool in_function_body = parser->in_function_body;
11004
11005 /* The body of a lambda-expression is not a subexpression of the enclosing
11006 expression. */
11007 cp_evaluated ev;
11008
11009 if (nested)
11010 push_function_context ();
11011 else
11012 /* Still increment function_depth so that we don't GC in the
11013 middle of an expression. */
11014 ++function_depth;
11015
11016 vec<tree> omp_privatization_save;
11017 save_omp_privatization_clauses (omp_privatization_save);
11018 /* Clear this in case we're in the middle of a default argument. */
11019 parser->local_variables_forbidden_p = 0;
11020 parser->in_function_body = true;
11021
11022 {
11023 local_specialization_stack s (lss_copy);
11024 tree fco = lambda_function (lambda_expr);
11025 tree body = start_lambda_function (fco, lambda_expr);
11026 matching_braces braces;
11027
11028 if (braces.require_open (parser))
11029 {
11030 tree compound_stmt = begin_compound_stmt (0);
11031
11032 /* Originally C++11 required us to peek for 'return expr'; and
11033 process it specially here to deduce the return type. N3638
11034 removed the need for that. */
11035
11036 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11037 cp_parser_label_declaration (parser);
11038 cp_parser_statement_seq_opt (parser, NULL_TREE);
11039 braces.require_close (parser);
11040
11041 finish_compound_stmt (compound_stmt);
11042 }
11043
11044 finish_lambda_function (body);
11045 }
11046
11047 restore_omp_privatization_clauses (omp_privatization_save);
11048 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11049 parser->in_function_body = in_function_body;
11050 if (nested)
11051 pop_function_context();
11052 else
11053 --function_depth;
11054 }
11055
11056 /* Statements [gram.stmt.stmt] */
11057
11058 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11059
11060 static void
11061 add_debug_begin_stmt (location_t loc)
11062 {
11063 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11064 return;
11065 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11066 /* A concept is never expanded normally. */
11067 return;
11068
11069 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11070 SET_EXPR_LOCATION (stmt, loc);
11071 add_stmt (stmt);
11072 }
11073
11074 /* Parse a statement.
11075
11076 statement:
11077 labeled-statement
11078 expression-statement
11079 compound-statement
11080 selection-statement
11081 iteration-statement
11082 jump-statement
11083 declaration-statement
11084 try-block
11085
11086 C++11:
11087
11088 statement:
11089 labeled-statement
11090 attribute-specifier-seq (opt) expression-statement
11091 attribute-specifier-seq (opt) compound-statement
11092 attribute-specifier-seq (opt) selection-statement
11093 attribute-specifier-seq (opt) iteration-statement
11094 attribute-specifier-seq (opt) jump-statement
11095 declaration-statement
11096 attribute-specifier-seq (opt) try-block
11097
11098 init-statement:
11099 expression-statement
11100 simple-declaration
11101
11102 TM Extension:
11103
11104 statement:
11105 atomic-statement
11106
11107 IN_COMPOUND is true when the statement is nested inside a
11108 cp_parser_compound_statement; this matters for certain pragmas.
11109
11110 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11111 is a (possibly labeled) if statement which is not enclosed in braces
11112 and has an else clause. This is used to implement -Wparentheses.
11113
11114 CHAIN is a vector of if-else-if conditions. */
11115
11116 static void
11117 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11118 bool in_compound, bool *if_p, vec<tree> *chain,
11119 location_t *loc_after_labels)
11120 {
11121 tree statement, std_attrs = NULL_TREE;
11122 cp_token *token;
11123 location_t statement_location, attrs_loc;
11124
11125 restart:
11126 if (if_p != NULL)
11127 *if_p = false;
11128 /* There is no statement yet. */
11129 statement = NULL_TREE;
11130
11131 saved_token_sentinel saved_tokens (parser->lexer);
11132 attrs_loc = cp_lexer_peek_token (parser->lexer)->location;
11133 if (c_dialect_objc ())
11134 /* In obj-c++, seeing '[[' might be the either the beginning of
11135 c++11 attributes, or a nested objc-message-expression. So
11136 let's parse the c++11 attributes tentatively. */
11137 cp_parser_parse_tentatively (parser);
11138 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11139 if (std_attrs)
11140 {
11141 location_t end_loc
11142 = cp_lexer_previous_token (parser->lexer)->location;
11143 attrs_loc = make_location (attrs_loc, attrs_loc, end_loc);
11144 }
11145 if (c_dialect_objc ())
11146 {
11147 if (!cp_parser_parse_definitely (parser))
11148 std_attrs = NULL_TREE;
11149 }
11150
11151 /* Peek at the next token. */
11152 token = cp_lexer_peek_token (parser->lexer);
11153 /* Remember the location of the first token in the statement. */
11154 cp_token *statement_token = token;
11155 statement_location = token->location;
11156 add_debug_begin_stmt (statement_location);
11157 /* If this is a keyword, then that will often determine what kind of
11158 statement we have. */
11159 if (token->type == CPP_KEYWORD)
11160 {
11161 enum rid keyword = token->keyword;
11162
11163 switch (keyword)
11164 {
11165 case RID_CASE:
11166 case RID_DEFAULT:
11167 /* Looks like a labeled-statement with a case label.
11168 Parse the label, and then use tail recursion to parse
11169 the statement. */
11170 cp_parser_label_for_labeled_statement (parser, std_attrs);
11171 in_compound = false;
11172 goto restart;
11173
11174 case RID_IF:
11175 case RID_SWITCH:
11176 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11177 statement = cp_parser_selection_statement (parser, if_p, chain);
11178 break;
11179
11180 case RID_WHILE:
11181 case RID_DO:
11182 case RID_FOR:
11183 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11184 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11185 break;
11186
11187 case RID_BREAK:
11188 case RID_CONTINUE:
11189 case RID_RETURN:
11190 case RID_GOTO:
11191 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11192 statement = cp_parser_jump_statement (parser);
11193 break;
11194
11195 /* Objective-C++ exception-handling constructs. */
11196 case RID_AT_TRY:
11197 case RID_AT_CATCH:
11198 case RID_AT_FINALLY:
11199 case RID_AT_SYNCHRONIZED:
11200 case RID_AT_THROW:
11201 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11202 statement = cp_parser_objc_statement (parser);
11203 break;
11204
11205 case RID_TRY:
11206 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11207 statement = cp_parser_try_block (parser);
11208 break;
11209
11210 case RID_NAMESPACE:
11211 /* This must be a namespace alias definition. */
11212 if (std_attrs != NULL_TREE)
11213 {
11214 /* Attributes should be parsed as part of the the
11215 declaration, so let's un-parse them. */
11216 saved_tokens.rollback();
11217 std_attrs = NULL_TREE;
11218 }
11219 cp_parser_declaration_statement (parser);
11220 return;
11221
11222 case RID_TRANSACTION_ATOMIC:
11223 case RID_TRANSACTION_RELAXED:
11224 case RID_SYNCHRONIZED:
11225 case RID_ATOMIC_NOEXCEPT:
11226 case RID_ATOMIC_CANCEL:
11227 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11228 statement = cp_parser_transaction (parser, token);
11229 break;
11230 case RID_TRANSACTION_CANCEL:
11231 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11232 statement = cp_parser_transaction_cancel (parser);
11233 break;
11234
11235 default:
11236 /* It might be a keyword like `int' that can start a
11237 declaration-statement. */
11238 break;
11239 }
11240 }
11241 else if (token->type == CPP_NAME)
11242 {
11243 /* If the next token is a `:', then we are looking at a
11244 labeled-statement. */
11245 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11246 if (token->type == CPP_COLON)
11247 {
11248 /* Looks like a labeled-statement with an ordinary label.
11249 Parse the label, and then use tail recursion to parse
11250 the statement. */
11251
11252 cp_parser_label_for_labeled_statement (parser, std_attrs);
11253 in_compound = false;
11254 goto restart;
11255 }
11256 }
11257 /* Anything that starts with a `{' must be a compound-statement. */
11258 else if (token->type == CPP_OPEN_BRACE)
11259 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11260 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11261 a statement all its own. */
11262 else if (token->type == CPP_PRAGMA)
11263 {
11264 /* Only certain OpenMP pragmas are attached to statements, and thus
11265 are considered statements themselves. All others are not. In
11266 the context of a compound, accept the pragma as a "statement" and
11267 return so that we can check for a close brace. Otherwise we
11268 require a real statement and must go back and read one. */
11269 if (in_compound)
11270 cp_parser_pragma (parser, pragma_compound, if_p);
11271 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11272 goto restart;
11273 return;
11274 }
11275 else if (token->type == CPP_EOF)
11276 {
11277 cp_parser_error (parser, "expected statement");
11278 return;
11279 }
11280
11281 /* Everything else must be a declaration-statement or an
11282 expression-statement. Try for the declaration-statement
11283 first, unless we are looking at a `;', in which case we know that
11284 we have an expression-statement. */
11285 if (!statement)
11286 {
11287 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11288 {
11289 if (std_attrs != NULL_TREE)
11290 /* Attributes should be parsed as part of the declaration,
11291 so let's un-parse them. */
11292 saved_tokens.rollback();
11293
11294 cp_parser_parse_tentatively (parser);
11295 /* Try to parse the declaration-statement. */
11296 cp_parser_declaration_statement (parser);
11297 /* If that worked, we're done. */
11298 if (cp_parser_parse_definitely (parser))
11299 return;
11300 /* It didn't work, restore the post-attribute position. */
11301 if (std_attrs)
11302 cp_lexer_set_token_position (parser->lexer, statement_token);
11303 }
11304 /* All preceding labels have been parsed at this point. */
11305 if (loc_after_labels != NULL)
11306 *loc_after_labels = statement_location;
11307
11308 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11309
11310 /* Look for an expression-statement instead. */
11311 statement = cp_parser_expression_statement (parser, in_statement_expr);
11312
11313 /* Handle [[fallthrough]];. */
11314 if (attribute_fallthrough_p (std_attrs))
11315 {
11316 /* The next token after the fallthrough attribute is ';'. */
11317 if (statement == NULL_TREE)
11318 {
11319 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11320 statement = build_call_expr_internal_loc (statement_location,
11321 IFN_FALLTHROUGH,
11322 void_type_node, 0);
11323 finish_expr_stmt (statement);
11324 }
11325 else
11326 warning_at (statement_location, OPT_Wattributes,
11327 "%<fallthrough%> attribute not followed by %<;%>");
11328 std_attrs = NULL_TREE;
11329 }
11330 }
11331
11332 /* Set the line number for the statement. */
11333 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11334 SET_EXPR_LOCATION (statement, statement_location);
11335
11336 /* Allow "[[fallthrough]];", but warn otherwise. */
11337 if (std_attrs != NULL_TREE)
11338 warning_at (attrs_loc,
11339 OPT_Wattributes,
11340 "attributes at the beginning of statement are ignored");
11341 }
11342
11343 /* Append ATTR to attribute list ATTRS. */
11344
11345 static tree
11346 attr_chainon (tree attrs, tree attr)
11347 {
11348 if (attrs == error_mark_node)
11349 return error_mark_node;
11350 if (attr == error_mark_node)
11351 return error_mark_node;
11352 return chainon (attrs, attr);
11353 }
11354
11355 /* Parse the label for a labeled-statement, i.e.
11356
11357 identifier :
11358 case constant-expression :
11359 default :
11360
11361 GNU Extension:
11362 case constant-expression ... constant-expression : statement
11363
11364 When a label is parsed without errors, the label is added to the
11365 parse tree by the finish_* functions, so this function doesn't
11366 have to return the label. */
11367
11368 static void
11369 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11370 {
11371 cp_token *token;
11372 tree label = NULL_TREE;
11373 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11374
11375 /* The next token should be an identifier. */
11376 token = cp_lexer_peek_token (parser->lexer);
11377 if (token->type != CPP_NAME
11378 && token->type != CPP_KEYWORD)
11379 {
11380 cp_parser_error (parser, "expected labeled-statement");
11381 return;
11382 }
11383
11384 /* Remember whether this case or a user-defined label is allowed to fall
11385 through to. */
11386 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11387
11388 parser->colon_corrects_to_scope_p = false;
11389 switch (token->keyword)
11390 {
11391 case RID_CASE:
11392 {
11393 tree expr, expr_hi;
11394 cp_token *ellipsis;
11395
11396 /* Consume the `case' token. */
11397 cp_lexer_consume_token (parser->lexer);
11398 /* Parse the constant-expression. */
11399 expr = cp_parser_constant_expression (parser);
11400 if (check_for_bare_parameter_packs (expr))
11401 expr = error_mark_node;
11402
11403 ellipsis = cp_lexer_peek_token (parser->lexer);
11404 if (ellipsis->type == CPP_ELLIPSIS)
11405 {
11406 /* Consume the `...' token. */
11407 cp_lexer_consume_token (parser->lexer);
11408 expr_hi = cp_parser_constant_expression (parser);
11409 if (check_for_bare_parameter_packs (expr_hi))
11410 expr_hi = error_mark_node;
11411
11412 /* We don't need to emit warnings here, as the common code
11413 will do this for us. */
11414 }
11415 else
11416 expr_hi = NULL_TREE;
11417
11418 if (parser->in_switch_statement_p)
11419 {
11420 tree l = finish_case_label (token->location, expr, expr_hi);
11421 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11422 {
11423 label = CASE_LABEL (l);
11424 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11425 }
11426 }
11427 else
11428 error_at (token->location,
11429 "case label %qE not within a switch statement",
11430 expr);
11431 }
11432 break;
11433
11434 case RID_DEFAULT:
11435 /* Consume the `default' token. */
11436 cp_lexer_consume_token (parser->lexer);
11437
11438 if (parser->in_switch_statement_p)
11439 {
11440 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11441 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11442 {
11443 label = CASE_LABEL (l);
11444 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11445 }
11446 }
11447 else
11448 error_at (token->location, "case label not within a switch statement");
11449 break;
11450
11451 default:
11452 /* Anything else must be an ordinary label. */
11453 label = finish_label_stmt (cp_parser_identifier (parser));
11454 if (label && TREE_CODE (label) == LABEL_DECL)
11455 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11456 break;
11457 }
11458
11459 /* Require the `:' token. */
11460 cp_parser_require (parser, CPP_COLON, RT_COLON);
11461
11462 /* An ordinary label may optionally be followed by attributes.
11463 However, this is only permitted if the attributes are then
11464 followed by a semicolon. This is because, for backward
11465 compatibility, when parsing
11466 lab: __attribute__ ((unused)) int i;
11467 we want the attribute to attach to "i", not "lab". */
11468 if (label != NULL_TREE
11469 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11470 {
11471 tree attrs;
11472 cp_parser_parse_tentatively (parser);
11473 attrs = cp_parser_gnu_attributes_opt (parser);
11474 if (attrs == NULL_TREE
11475 /* And fallthrough always binds to the expression-statement. */
11476 || attribute_fallthrough_p (attrs)
11477 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11478 cp_parser_abort_tentative_parse (parser);
11479 else if (!cp_parser_parse_definitely (parser))
11480 ;
11481 else
11482 attributes = attr_chainon (attributes, attrs);
11483 }
11484
11485 if (attributes != NULL_TREE)
11486 cplus_decl_attributes (&label, attributes, 0);
11487
11488 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11489 }
11490
11491 /* Parse an expression-statement.
11492
11493 expression-statement:
11494 expression [opt] ;
11495
11496 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11497 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11498 indicates whether this expression-statement is part of an
11499 expression statement. */
11500
11501 static tree
11502 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11503 {
11504 tree statement = NULL_TREE;
11505 cp_token *token = cp_lexer_peek_token (parser->lexer);
11506 location_t loc = token->location;
11507
11508 /* There might be attribute fallthrough. */
11509 tree attr = cp_parser_gnu_attributes_opt (parser);
11510
11511 /* If the next token is a ';', then there is no expression
11512 statement. */
11513 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11514 {
11515 statement = cp_parser_expression (parser);
11516 if (statement == error_mark_node
11517 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11518 {
11519 cp_parser_skip_to_end_of_block_or_statement (parser);
11520 return error_mark_node;
11521 }
11522 }
11523
11524 /* Handle [[fallthrough]];. */
11525 if (attribute_fallthrough_p (attr))
11526 {
11527 /* The next token after the fallthrough attribute is ';'. */
11528 if (statement == NULL_TREE)
11529 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11530 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11531 void_type_node, 0);
11532 else
11533 warning_at (loc, OPT_Wattributes,
11534 "%<fallthrough%> attribute not followed by %<;%>");
11535 attr = NULL_TREE;
11536 }
11537
11538 /* Allow "[[fallthrough]];", but warn otherwise. */
11539 if (attr != NULL_TREE)
11540 warning_at (loc, OPT_Wattributes,
11541 "attributes at the beginning of statement are ignored");
11542
11543 /* Give a helpful message for "A<T>::type t;" and the like. */
11544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11545 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11546 {
11547 if (TREE_CODE (statement) == SCOPE_REF)
11548 error_at (token->location, "need %<typename%> before %qE because "
11549 "%qT is a dependent scope",
11550 statement, TREE_OPERAND (statement, 0));
11551 else if (is_overloaded_fn (statement)
11552 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11553 {
11554 /* A::A a; */
11555 tree fn = get_first_fn (statement);
11556 error_at (token->location,
11557 "%<%T::%D%> names the constructor, not the type",
11558 DECL_CONTEXT (fn), DECL_NAME (fn));
11559 }
11560 }
11561
11562 /* Consume the final `;'. */
11563 cp_parser_consume_semicolon_at_end_of_statement (parser);
11564
11565 if (in_statement_expr
11566 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11567 /* This is the final expression statement of a statement
11568 expression. */
11569 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11570 else if (statement)
11571 statement = finish_expr_stmt (statement);
11572
11573 return statement;
11574 }
11575
11576 /* Parse a compound-statement.
11577
11578 compound-statement:
11579 { statement-seq [opt] }
11580
11581 GNU extension:
11582
11583 compound-statement:
11584 { label-declaration-seq [opt] statement-seq [opt] }
11585
11586 label-declaration-seq:
11587 label-declaration
11588 label-declaration-seq label-declaration
11589
11590 Returns a tree representing the statement. */
11591
11592 static tree
11593 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11594 int bcs_flags, bool function_body)
11595 {
11596 tree compound_stmt;
11597 matching_braces braces;
11598
11599 /* Consume the `{'. */
11600 if (!braces.require_open (parser))
11601 return error_mark_node;
11602 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11603 && !function_body && cxx_dialect < cxx14)
11604 pedwarn (input_location, OPT_Wpedantic,
11605 "compound-statement in %<constexpr%> function");
11606 /* Begin the compound-statement. */
11607 compound_stmt = begin_compound_stmt (bcs_flags);
11608 /* If the next keyword is `__label__' we have a label declaration. */
11609 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11610 cp_parser_label_declaration (parser);
11611 /* Parse an (optional) statement-seq. */
11612 cp_parser_statement_seq_opt (parser, in_statement_expr);
11613 /* Finish the compound-statement. */
11614 finish_compound_stmt (compound_stmt);
11615 /* Consume the `}'. */
11616 braces.require_close (parser);
11617
11618 return compound_stmt;
11619 }
11620
11621 /* Parse an (optional) statement-seq.
11622
11623 statement-seq:
11624 statement
11625 statement-seq [opt] statement */
11626
11627 static void
11628 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11629 {
11630 /* Scan statements until there aren't any more. */
11631 while (true)
11632 {
11633 cp_token *token = cp_lexer_peek_token (parser->lexer);
11634
11635 /* If we are looking at a `}', then we have run out of
11636 statements; the same is true if we have reached the end
11637 of file, or have stumbled upon a stray '@end'. */
11638 if (token->type == CPP_CLOSE_BRACE
11639 || token->type == CPP_EOF
11640 || token->type == CPP_PRAGMA_EOL
11641 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11642 break;
11643
11644 /* If we are in a compound statement and find 'else' then
11645 something went wrong. */
11646 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11647 {
11648 if (parser->in_statement & IN_IF_STMT)
11649 break;
11650 else
11651 {
11652 token = cp_lexer_consume_token (parser->lexer);
11653 error_at (token->location, "%<else%> without a previous %<if%>");
11654 }
11655 }
11656
11657 /* Parse the statement. */
11658 cp_parser_statement (parser, in_statement_expr, true, NULL);
11659 }
11660 }
11661
11662 /* Return true if this is the C++20 version of range-based-for with
11663 init-statement. */
11664
11665 static bool
11666 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11667 {
11668 bool r = false;
11669
11670 /* Save tokens so that we can put them back. */
11671 cp_lexer_save_tokens (parser->lexer);
11672
11673 /* There has to be an unnested ; followed by an unnested :. */
11674 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11675 /*recovering=*/false,
11676 CPP_SEMICOLON,
11677 /*consume_paren=*/false) != -1)
11678 goto out;
11679
11680 /* We found the semicolon, eat it now. */
11681 cp_lexer_consume_token (parser->lexer);
11682
11683 /* Now look for ':' that is not nested in () or {}. */
11684 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11685 /*recovering=*/false,
11686 CPP_COLON,
11687 /*consume_paren=*/false) == -1);
11688
11689 out:
11690 /* Roll back the tokens we skipped. */
11691 cp_lexer_rollback_tokens (parser->lexer);
11692
11693 return r;
11694 }
11695
11696 /* Return true if we're looking at (init; cond), false otherwise. */
11697
11698 static bool
11699 cp_parser_init_statement_p (cp_parser *parser)
11700 {
11701 /* Save tokens so that we can put them back. */
11702 cp_lexer_save_tokens (parser->lexer);
11703
11704 /* Look for ';' that is not nested in () or {}. */
11705 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11706 /*recovering=*/false,
11707 CPP_SEMICOLON,
11708 /*consume_paren=*/false);
11709
11710 /* Roll back the tokens we skipped. */
11711 cp_lexer_rollback_tokens (parser->lexer);
11712
11713 return ret == -1;
11714 }
11715
11716 /* Parse a selection-statement.
11717
11718 selection-statement:
11719 if ( init-statement [opt] condition ) statement
11720 if ( init-statement [opt] condition ) statement else statement
11721 switch ( init-statement [opt] condition ) statement
11722
11723 Returns the new IF_STMT or SWITCH_STMT.
11724
11725 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11726 is a (possibly labeled) if statement which is not enclosed in
11727 braces and has an else clause. This is used to implement
11728 -Wparentheses.
11729
11730 CHAIN is a vector of if-else-if conditions. This is used to implement
11731 -Wduplicated-cond. */
11732
11733 static tree
11734 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11735 vec<tree> *chain)
11736 {
11737 cp_token *token;
11738 enum rid keyword;
11739 token_indent_info guard_tinfo;
11740
11741 if (if_p != NULL)
11742 *if_p = false;
11743
11744 /* Peek at the next token. */
11745 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11746 guard_tinfo = get_token_indent_info (token);
11747
11748 /* See what kind of keyword it is. */
11749 keyword = token->keyword;
11750 switch (keyword)
11751 {
11752 case RID_IF:
11753 case RID_SWITCH:
11754 {
11755 tree statement;
11756 tree condition;
11757
11758 bool cx = false;
11759 if (keyword == RID_IF
11760 && cp_lexer_next_token_is_keyword (parser->lexer,
11761 RID_CONSTEXPR))
11762 {
11763 cx = true;
11764 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11765 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11766 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11767 "with %<-std=c++17%> or %<-std=gnu++17%>");
11768 }
11769
11770 /* Look for the `('. */
11771 matching_parens parens;
11772 if (!parens.require_open (parser))
11773 {
11774 cp_parser_skip_to_end_of_statement (parser);
11775 return error_mark_node;
11776 }
11777
11778 /* Begin the selection-statement. */
11779 if (keyword == RID_IF)
11780 {
11781 statement = begin_if_stmt ();
11782 IF_STMT_CONSTEXPR_P (statement) = cx;
11783 }
11784 else
11785 statement = begin_switch_stmt ();
11786
11787 /* Parse the optional init-statement. */
11788 if (cp_parser_init_statement_p (parser))
11789 {
11790 tree decl;
11791 if (cxx_dialect < cxx17)
11792 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11793 "init-statement in selection statements only available "
11794 "with %<-std=c++17%> or %<-std=gnu++17%>");
11795 cp_parser_init_statement (parser, &decl);
11796 }
11797
11798 /* Parse the condition. */
11799 condition = cp_parser_condition (parser);
11800 /* Look for the `)'. */
11801 if (!parens.require_close (parser))
11802 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11803 /*consume_paren=*/true);
11804
11805 if (keyword == RID_IF)
11806 {
11807 bool nested_if;
11808 unsigned char in_statement;
11809
11810 /* Add the condition. */
11811 condition = finish_if_stmt_cond (condition, statement);
11812
11813 if (warn_duplicated_cond)
11814 warn_duplicated_cond_add_or_warn (token->location, condition,
11815 &chain);
11816
11817 /* Parse the then-clause. */
11818 in_statement = parser->in_statement;
11819 parser->in_statement |= IN_IF_STMT;
11820
11821 /* Outside a template, the non-selected branch of a constexpr
11822 if is a 'discarded statement', i.e. unevaluated. */
11823 bool was_discarded = in_discarded_stmt;
11824 bool discard_then = (cx && !processing_template_decl
11825 && integer_zerop (condition));
11826 if (discard_then)
11827 {
11828 in_discarded_stmt = true;
11829 ++c_inhibit_evaluation_warnings;
11830 }
11831
11832 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11833 guard_tinfo);
11834
11835 parser->in_statement = in_statement;
11836
11837 finish_then_clause (statement);
11838
11839 if (discard_then)
11840 {
11841 THEN_CLAUSE (statement) = NULL_TREE;
11842 in_discarded_stmt = was_discarded;
11843 --c_inhibit_evaluation_warnings;
11844 }
11845
11846 /* If the next token is `else', parse the else-clause. */
11847 if (cp_lexer_next_token_is_keyword (parser->lexer,
11848 RID_ELSE))
11849 {
11850 bool discard_else = (cx && !processing_template_decl
11851 && integer_nonzerop (condition));
11852 if (discard_else)
11853 {
11854 in_discarded_stmt = true;
11855 ++c_inhibit_evaluation_warnings;
11856 }
11857
11858 guard_tinfo
11859 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11860 /* Consume the `else' keyword. */
11861 cp_lexer_consume_token (parser->lexer);
11862 if (warn_duplicated_cond)
11863 {
11864 if (cp_lexer_next_token_is_keyword (parser->lexer,
11865 RID_IF)
11866 && chain == NULL)
11867 {
11868 /* We've got "if (COND) else if (COND2)". Start
11869 the condition chain and add COND as the first
11870 element. */
11871 chain = new vec<tree> ();
11872 if (!CONSTANT_CLASS_P (condition)
11873 && !TREE_SIDE_EFFECTS (condition))
11874 {
11875 /* Wrap it in a NOP_EXPR so that we can set the
11876 location of the condition. */
11877 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11878 condition);
11879 SET_EXPR_LOCATION (e, token->location);
11880 chain->safe_push (e);
11881 }
11882 }
11883 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11884 RID_IF))
11885 {
11886 /* This is if-else without subsequent if. Zap the
11887 condition chain; we would have already warned at
11888 this point. */
11889 delete chain;
11890 chain = NULL;
11891 }
11892 }
11893 begin_else_clause (statement);
11894 /* Parse the else-clause. */
11895 cp_parser_implicitly_scoped_statement (parser, NULL,
11896 guard_tinfo, chain);
11897
11898 finish_else_clause (statement);
11899
11900 /* If we are currently parsing a then-clause, then
11901 IF_P will not be NULL. We set it to true to
11902 indicate that this if statement has an else clause.
11903 This may trigger the Wparentheses warning below
11904 when we get back up to the parent if statement. */
11905 if (if_p != NULL)
11906 *if_p = true;
11907
11908 if (discard_else)
11909 {
11910 ELSE_CLAUSE (statement) = NULL_TREE;
11911 in_discarded_stmt = was_discarded;
11912 --c_inhibit_evaluation_warnings;
11913 }
11914 }
11915 else
11916 {
11917 /* This if statement does not have an else clause. If
11918 NESTED_IF is true, then the then-clause has an if
11919 statement which does have an else clause. We warn
11920 about the potential ambiguity. */
11921 if (nested_if)
11922 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11923 "suggest explicit braces to avoid ambiguous"
11924 " %<else%>");
11925 if (warn_duplicated_cond)
11926 {
11927 /* We don't need the condition chain anymore. */
11928 delete chain;
11929 chain = NULL;
11930 }
11931 }
11932
11933 /* Now we're all done with the if-statement. */
11934 finish_if_stmt (statement);
11935 }
11936 else
11937 {
11938 bool in_switch_statement_p;
11939 unsigned char in_statement;
11940
11941 /* Add the condition. */
11942 finish_switch_cond (condition, statement);
11943
11944 /* Parse the body of the switch-statement. */
11945 in_switch_statement_p = parser->in_switch_statement_p;
11946 in_statement = parser->in_statement;
11947 parser->in_switch_statement_p = true;
11948 parser->in_statement |= IN_SWITCH_STMT;
11949 cp_parser_implicitly_scoped_statement (parser, if_p,
11950 guard_tinfo);
11951 parser->in_switch_statement_p = in_switch_statement_p;
11952 parser->in_statement = in_statement;
11953
11954 /* Now we're all done with the switch-statement. */
11955 finish_switch_stmt (statement);
11956 }
11957
11958 return statement;
11959 }
11960 break;
11961
11962 default:
11963 cp_parser_error (parser, "expected selection-statement");
11964 return error_mark_node;
11965 }
11966 }
11967
11968 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11969 If we have seen at least one decl-specifier, and the next token
11970 is not a parenthesis, then we must be looking at a declaration.
11971 (After "int (" we might be looking at a functional cast.) */
11972
11973 static void
11974 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11975 bool any_specifiers_p)
11976 {
11977 if (any_specifiers_p
11978 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11979 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11980 && !cp_parser_error_occurred (parser))
11981 cp_parser_commit_to_tentative_parse (parser);
11982 }
11983
11984 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11985 The declarator shall not specify a function or an array. Returns
11986 TRUE if the declarator is valid, FALSE otherwise. */
11987
11988 static bool
11989 cp_parser_check_condition_declarator (cp_parser* parser,
11990 cp_declarator *declarator,
11991 location_t loc)
11992 {
11993 if (declarator == cp_error_declarator
11994 || function_declarator_p (declarator)
11995 || declarator->kind == cdk_array)
11996 {
11997 if (declarator == cp_error_declarator)
11998 /* Already complained. */;
11999 else if (declarator->kind == cdk_array)
12000 error_at (loc, "condition declares an array");
12001 else
12002 error_at (loc, "condition declares a function");
12003 if (parser->fully_implicit_function_template_p)
12004 abort_fully_implicit_template (parser);
12005 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
12006 /*or_comma=*/false,
12007 /*consume_paren=*/false);
12008 return false;
12009 }
12010 else
12011 return true;
12012 }
12013
12014 /* Parse a condition.
12015
12016 condition:
12017 expression
12018 type-specifier-seq declarator = initializer-clause
12019 type-specifier-seq declarator braced-init-list
12020
12021 GNU Extension:
12022
12023 condition:
12024 type-specifier-seq declarator asm-specification [opt]
12025 attributes [opt] = assignment-expression
12026
12027 Returns the expression that should be tested. */
12028
12029 static tree
12030 cp_parser_condition (cp_parser* parser)
12031 {
12032 cp_decl_specifier_seq type_specifiers;
12033 const char *saved_message;
12034 int declares_class_or_enum;
12035
12036 /* Try the declaration first. */
12037 cp_parser_parse_tentatively (parser);
12038 /* New types are not allowed in the type-specifier-seq for a
12039 condition. */
12040 saved_message = parser->type_definition_forbidden_message;
12041 parser->type_definition_forbidden_message
12042 = G_("types may not be defined in conditions");
12043 /* Parse the type-specifier-seq. */
12044 cp_parser_decl_specifier_seq (parser,
12045 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
12046 &type_specifiers,
12047 &declares_class_or_enum);
12048 /* Restore the saved message. */
12049 parser->type_definition_forbidden_message = saved_message;
12050
12051 cp_parser_maybe_commit_to_declaration (parser,
12052 type_specifiers.any_specifiers_p);
12053
12054 /* If all is well, we might be looking at a declaration. */
12055 if (!cp_parser_error_occurred (parser))
12056 {
12057 tree decl;
12058 tree asm_specification;
12059 tree attributes;
12060 cp_declarator *declarator;
12061 tree initializer = NULL_TREE;
12062 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12063
12064 /* Parse the declarator. */
12065 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12066 CP_PARSER_FLAGS_NONE,
12067 /*ctor_dtor_or_conv_p=*/NULL,
12068 /*parenthesized_p=*/NULL,
12069 /*member_p=*/false,
12070 /*friend_p=*/false,
12071 /*static_p=*/false);
12072 /* Parse the attributes. */
12073 attributes = cp_parser_attributes_opt (parser);
12074 /* Parse the asm-specification. */
12075 asm_specification = cp_parser_asm_specification_opt (parser);
12076 /* If the next token is not an `=' or '{', then we might still be
12077 looking at an expression. For example:
12078
12079 if (A(a).x)
12080
12081 looks like a decl-specifier-seq and a declarator -- but then
12082 there is no `=', so this is an expression. */
12083 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12084 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12085 cp_parser_simulate_error (parser);
12086
12087 /* If we did see an `=' or '{', then we are looking at a declaration
12088 for sure. */
12089 if (cp_parser_parse_definitely (parser))
12090 {
12091 tree pushed_scope;
12092 bool non_constant_p = false;
12093 int flags = LOOKUP_ONLYCONVERTING;
12094
12095 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12096 return error_mark_node;
12097
12098 /* Create the declaration. */
12099 decl = start_decl (declarator, &type_specifiers,
12100 /*initialized_p=*/true,
12101 attributes, /*prefix_attributes=*/NULL_TREE,
12102 &pushed_scope);
12103
12104 /* Parse the initializer. */
12105 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12106 {
12107 initializer = cp_parser_braced_list (parser, &non_constant_p);
12108 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12109 flags = 0;
12110 }
12111 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12112 {
12113 /* Consume the `='. */
12114 cp_lexer_consume_token (parser->lexer);
12115 initializer = cp_parser_initializer_clause (parser,
12116 &non_constant_p);
12117 }
12118 else
12119 {
12120 cp_parser_error (parser, "expected initializer");
12121 initializer = error_mark_node;
12122 }
12123 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12124 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12125
12126 /* Process the initializer. */
12127 cp_finish_decl (decl,
12128 initializer, !non_constant_p,
12129 asm_specification,
12130 flags);
12131
12132 if (pushed_scope)
12133 pop_scope (pushed_scope);
12134
12135 return convert_from_reference (decl);
12136 }
12137 }
12138 /* If we didn't even get past the declarator successfully, we are
12139 definitely not looking at a declaration. */
12140 else
12141 cp_parser_abort_tentative_parse (parser);
12142
12143 /* Otherwise, we are looking at an expression. */
12144 return cp_parser_expression (parser);
12145 }
12146
12147 /* Parses a for-statement or range-for-statement until the closing ')',
12148 not included. */
12149
12150 static tree
12151 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12152 {
12153 tree init, scope, decl;
12154 bool is_range_for;
12155
12156 /* Begin the for-statement. */
12157 scope = begin_for_scope (&init);
12158
12159 /* Parse the initialization. */
12160 is_range_for = cp_parser_init_statement (parser, &decl);
12161
12162 if (is_range_for)
12163 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12164 false);
12165 else
12166 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12167 }
12168
12169 static tree
12170 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12171 unsigned short unroll)
12172 {
12173 /* Normal for loop */
12174 tree condition = NULL_TREE;
12175 tree expression = NULL_TREE;
12176 tree stmt;
12177
12178 stmt = begin_for_stmt (scope, init);
12179 /* The init-statement has already been parsed in
12180 cp_parser_init_statement, so no work is needed here. */
12181 finish_init_stmt (stmt);
12182
12183 /* If there's a condition, process it. */
12184 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12185 condition = cp_parser_condition (parser);
12186 else if (ivdep)
12187 {
12188 cp_parser_error (parser, "missing loop condition in loop with "
12189 "%<GCC ivdep%> pragma");
12190 condition = error_mark_node;
12191 }
12192 else if (unroll)
12193 {
12194 cp_parser_error (parser, "missing loop condition in loop with "
12195 "%<GCC unroll%> pragma");
12196 condition = error_mark_node;
12197 }
12198 finish_for_cond (condition, stmt, ivdep, unroll);
12199 /* Look for the `;'. */
12200 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12201
12202 /* If there's an expression, process it. */
12203 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12204 expression = cp_parser_expression (parser);
12205 finish_for_expr (expression, stmt);
12206
12207 return stmt;
12208 }
12209
12210 /* Tries to parse a range-based for-statement:
12211
12212 range-based-for:
12213 decl-specifier-seq declarator : expression
12214
12215 The decl-specifier-seq declarator and the `:' are already parsed by
12216 cp_parser_init_statement. If processing_template_decl it returns a
12217 newly created RANGE_FOR_STMT; if not, it is converted to a
12218 regular FOR_STMT. */
12219
12220 static tree
12221 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12222 bool ivdep, unsigned short unroll, bool is_omp)
12223 {
12224 tree stmt, range_expr;
12225 auto_vec <cxx_binding *, 16> bindings;
12226 auto_vec <tree, 16> names;
12227 tree decomp_first_name = NULL_TREE;
12228 unsigned int decomp_cnt = 0;
12229
12230 /* Get the range declaration momentarily out of the way so that
12231 the range expression doesn't clash with it. */
12232 if (range_decl != error_mark_node)
12233 {
12234 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12235 {
12236 tree v = DECL_VALUE_EXPR (range_decl);
12237 /* For decomposition declaration get all of the corresponding
12238 declarations out of the way. */
12239 if (TREE_CODE (v) == ARRAY_REF
12240 && VAR_P (TREE_OPERAND (v, 0))
12241 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12242 {
12243 tree d = range_decl;
12244 range_decl = TREE_OPERAND (v, 0);
12245 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12246 decomp_first_name = d;
12247 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12248 {
12249 tree name = DECL_NAME (d);
12250 names.safe_push (name);
12251 bindings.safe_push (IDENTIFIER_BINDING (name));
12252 IDENTIFIER_BINDING (name)
12253 = IDENTIFIER_BINDING (name)->previous;
12254 }
12255 }
12256 }
12257 if (names.is_empty ())
12258 {
12259 tree name = DECL_NAME (range_decl);
12260 names.safe_push (name);
12261 bindings.safe_push (IDENTIFIER_BINDING (name));
12262 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12263 }
12264 }
12265
12266 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12267 {
12268 bool expr_non_constant_p;
12269 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12270 }
12271 else
12272 range_expr = cp_parser_expression (parser);
12273
12274 /* Put the range declaration(s) back into scope. */
12275 for (unsigned int i = 0; i < names.length (); i++)
12276 {
12277 cxx_binding *binding = bindings[i];
12278 binding->previous = IDENTIFIER_BINDING (names[i]);
12279 IDENTIFIER_BINDING (names[i]) = binding;
12280 }
12281
12282 /* finish_omp_for has its own code for the following, so just
12283 return the range_expr instead. */
12284 if (is_omp)
12285 return range_expr;
12286
12287 /* If in template, STMT is converted to a normal for-statement
12288 at instantiation. If not, it is done just ahead. */
12289 if (processing_template_decl)
12290 {
12291 if (check_for_bare_parameter_packs (range_expr))
12292 range_expr = error_mark_node;
12293 stmt = begin_range_for_stmt (scope, init);
12294 if (ivdep)
12295 RANGE_FOR_IVDEP (stmt) = 1;
12296 if (unroll)
12297 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12298 finish_range_for_decl (stmt, range_decl, range_expr);
12299 if (!type_dependent_expression_p (range_expr)
12300 /* do_auto_deduction doesn't mess with template init-lists. */
12301 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12302 do_range_for_auto_deduction (range_decl, range_expr);
12303 }
12304 else
12305 {
12306 stmt = begin_for_stmt (scope, init);
12307 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12308 decomp_first_name, decomp_cnt, ivdep,
12309 unroll);
12310 }
12311 return stmt;
12312 }
12313
12314 /* Subroutine of cp_convert_range_for: given the initializer expression,
12315 builds up the range temporary. */
12316
12317 static tree
12318 build_range_temp (tree range_expr)
12319 {
12320 tree range_type, range_temp;
12321
12322 /* Find out the type deduced by the declaration
12323 `auto &&__range = range_expr'. */
12324 range_type = cp_build_reference_type (make_auto (), true);
12325 range_type = do_auto_deduction (range_type, range_expr,
12326 type_uses_auto (range_type));
12327
12328 /* Create the __range variable. */
12329 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12330 range_type);
12331 TREE_USED (range_temp) = 1;
12332 DECL_ARTIFICIAL (range_temp) = 1;
12333
12334 return range_temp;
12335 }
12336
12337 /* Used by cp_parser_range_for in template context: we aren't going to
12338 do a full conversion yet, but we still need to resolve auto in the
12339 type of the for-range-declaration if present. This is basically
12340 a shortcut version of cp_convert_range_for. */
12341
12342 static void
12343 do_range_for_auto_deduction (tree decl, tree range_expr)
12344 {
12345 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12346 if (auto_node)
12347 {
12348 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12349 range_temp = convert_from_reference (build_range_temp (range_expr));
12350 iter_type = (cp_parser_perform_range_for_lookup
12351 (range_temp, &begin_dummy, &end_dummy));
12352 if (iter_type)
12353 {
12354 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12355 iter_type);
12356 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12357 RO_UNARY_STAR,
12358 tf_warning_or_error);
12359 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12360 iter_decl, auto_node);
12361 }
12362 }
12363 }
12364
12365 /* Converts a range-based for-statement into a normal
12366 for-statement, as per the definition.
12367
12368 for (RANGE_DECL : RANGE_EXPR)
12369 BLOCK
12370
12371 should be equivalent to:
12372
12373 {
12374 auto &&__range = RANGE_EXPR;
12375 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12376 __begin != __end;
12377 ++__begin)
12378 {
12379 RANGE_DECL = *__begin;
12380 BLOCK
12381 }
12382 }
12383
12384 If RANGE_EXPR is an array:
12385 BEGIN_EXPR = __range
12386 END_EXPR = __range + ARRAY_SIZE(__range)
12387 Else if RANGE_EXPR has a member 'begin' or 'end':
12388 BEGIN_EXPR = __range.begin()
12389 END_EXPR = __range.end()
12390 Else:
12391 BEGIN_EXPR = begin(__range)
12392 END_EXPR = end(__range);
12393
12394 If __range has a member 'begin' but not 'end', or vice versa, we must
12395 still use the second alternative (it will surely fail, however).
12396 When calling begin()/end() in the third alternative we must use
12397 argument dependent lookup, but always considering 'std' as an associated
12398 namespace. */
12399
12400 tree
12401 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12402 tree decomp_first_name, unsigned int decomp_cnt,
12403 bool ivdep, unsigned short unroll)
12404 {
12405 tree begin, end;
12406 tree iter_type, begin_expr, end_expr;
12407 tree condition, expression;
12408
12409 range_expr = mark_lvalue_use (range_expr);
12410
12411 if (range_decl == error_mark_node || range_expr == error_mark_node)
12412 /* If an error happened previously do nothing or else a lot of
12413 unhelpful errors would be issued. */
12414 begin_expr = end_expr = iter_type = error_mark_node;
12415 else
12416 {
12417 tree range_temp;
12418
12419 if (VAR_P (range_expr)
12420 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12421 /* Can't bind a reference to an array of runtime bound. */
12422 range_temp = range_expr;
12423 else
12424 {
12425 range_temp = build_range_temp (range_expr);
12426 pushdecl (range_temp);
12427 cp_finish_decl (range_temp, range_expr,
12428 /*is_constant_init*/false, NULL_TREE,
12429 LOOKUP_ONLYCONVERTING);
12430 range_temp = convert_from_reference (range_temp);
12431 }
12432 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12433 &begin_expr, &end_expr);
12434 }
12435
12436 /* The new for initialization statement. */
12437 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12438 iter_type);
12439 TREE_USED (begin) = 1;
12440 DECL_ARTIFICIAL (begin) = 1;
12441 pushdecl (begin);
12442 cp_finish_decl (begin, begin_expr,
12443 /*is_constant_init*/false, NULL_TREE,
12444 LOOKUP_ONLYCONVERTING);
12445
12446 if (cxx_dialect >= cxx17)
12447 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12448 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12449 TREE_USED (end) = 1;
12450 DECL_ARTIFICIAL (end) = 1;
12451 pushdecl (end);
12452 cp_finish_decl (end, end_expr,
12453 /*is_constant_init*/false, NULL_TREE,
12454 LOOKUP_ONLYCONVERTING);
12455
12456 finish_init_stmt (statement);
12457
12458 /* The new for condition. */
12459 condition = build_x_binary_op (input_location, NE_EXPR,
12460 begin, ERROR_MARK,
12461 end, ERROR_MARK,
12462 NULL, tf_warning_or_error);
12463 finish_for_cond (condition, statement, ivdep, unroll);
12464
12465 /* The new increment expression. */
12466 expression = finish_unary_op_expr (input_location,
12467 PREINCREMENT_EXPR, begin,
12468 tf_warning_or_error);
12469 finish_for_expr (expression, statement);
12470
12471 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12472 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12473
12474 /* The declaration is initialized with *__begin inside the loop body. */
12475 cp_finish_decl (range_decl,
12476 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12477 tf_warning_or_error),
12478 /*is_constant_init*/false, NULL_TREE,
12479 LOOKUP_ONLYCONVERTING);
12480 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12481 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12482
12483 return statement;
12484 }
12485
12486 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12487 We need to solve both at the same time because the method used
12488 depends on the existence of members begin or end.
12489 Returns the type deduced for the iterator expression. */
12490
12491 static tree
12492 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12493 {
12494 if (error_operand_p (range))
12495 {
12496 *begin = *end = error_mark_node;
12497 return error_mark_node;
12498 }
12499
12500 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12501 {
12502 error ("range-based %<for%> expression of type %qT "
12503 "has incomplete type", TREE_TYPE (range));
12504 *begin = *end = error_mark_node;
12505 return error_mark_node;
12506 }
12507 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12508 {
12509 /* If RANGE is an array, we will use pointer arithmetic. */
12510 *begin = decay_conversion (range, tf_warning_or_error);
12511 *end = build_binary_op (input_location, PLUS_EXPR,
12512 range,
12513 array_type_nelts_top (TREE_TYPE (range)),
12514 false);
12515 return TREE_TYPE (*begin);
12516 }
12517 else
12518 {
12519 /* If it is not an array, we must do a bit of magic. */
12520 tree id_begin, id_end;
12521 tree member_begin, member_end;
12522
12523 *begin = *end = error_mark_node;
12524
12525 id_begin = get_identifier ("begin");
12526 id_end = get_identifier ("end");
12527 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12528 /*protect=*/2, /*want_type=*/false,
12529 tf_warning_or_error);
12530 member_end = lookup_member (TREE_TYPE (range), id_end,
12531 /*protect=*/2, /*want_type=*/false,
12532 tf_warning_or_error);
12533
12534 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12535 {
12536 /* Use the member functions. */
12537 *begin = cp_parser_range_for_member_function (range, id_begin);
12538 *end = cp_parser_range_for_member_function (range, id_end);
12539 }
12540 else
12541 {
12542 /* Use global functions with ADL. */
12543 releasing_vec vec;
12544
12545 vec_safe_push (vec, range);
12546
12547 member_begin = perform_koenig_lookup (id_begin, vec,
12548 tf_warning_or_error);
12549 *begin = finish_call_expr (member_begin, &vec, false, true,
12550 tf_warning_or_error);
12551 member_end = perform_koenig_lookup (id_end, vec,
12552 tf_warning_or_error);
12553 *end = finish_call_expr (member_end, &vec, false, true,
12554 tf_warning_or_error);
12555 }
12556
12557 /* Last common checks. */
12558 if (*begin == error_mark_node || *end == error_mark_node)
12559 {
12560 /* If one of the expressions is an error do no more checks. */
12561 *begin = *end = error_mark_node;
12562 return error_mark_node;
12563 }
12564 else if (type_dependent_expression_p (*begin)
12565 || type_dependent_expression_p (*end))
12566 /* Can happen, when, eg, in a template context, Koenig lookup
12567 can't resolve begin/end (c++/58503). */
12568 return NULL_TREE;
12569 else
12570 {
12571 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12572 /* The unqualified type of the __begin and __end temporaries should
12573 be the same, as required by the multiple auto declaration. */
12574 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12575 {
12576 if (cxx_dialect >= cxx17
12577 && (build_x_binary_op (input_location, NE_EXPR,
12578 *begin, ERROR_MARK,
12579 *end, ERROR_MARK,
12580 NULL, tf_none)
12581 != error_mark_node))
12582 /* P0184R0 allows __begin and __end to have different types,
12583 but make sure they are comparable so we can give a better
12584 diagnostic. */;
12585 else
12586 error ("inconsistent begin/end types in range-based %<for%> "
12587 "statement: %qT and %qT",
12588 TREE_TYPE (*begin), TREE_TYPE (*end));
12589 }
12590 return iter_type;
12591 }
12592 }
12593 }
12594
12595 /* Helper function for cp_parser_perform_range_for_lookup.
12596 Builds a tree for RANGE.IDENTIFIER(). */
12597
12598 static tree
12599 cp_parser_range_for_member_function (tree range, tree identifier)
12600 {
12601 tree member, res;
12602
12603 member = finish_class_member_access_expr (range, identifier,
12604 false, tf_warning_or_error);
12605 if (member == error_mark_node)
12606 return error_mark_node;
12607
12608 releasing_vec vec;
12609 res = finish_call_expr (member, &vec,
12610 /*disallow_virtual=*/false,
12611 /*koenig_p=*/false,
12612 tf_warning_or_error);
12613 return res;
12614 }
12615
12616 /* Parse an iteration-statement.
12617
12618 iteration-statement:
12619 while ( condition ) statement
12620 do statement while ( expression ) ;
12621 for ( init-statement condition [opt] ; expression [opt] )
12622 statement
12623
12624 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12625
12626 static tree
12627 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12628 unsigned short unroll)
12629 {
12630 cp_token *token;
12631 enum rid keyword;
12632 tree statement;
12633 unsigned char in_statement;
12634 token_indent_info guard_tinfo;
12635
12636 /* Peek at the next token. */
12637 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12638 if (!token)
12639 return error_mark_node;
12640
12641 guard_tinfo = get_token_indent_info (token);
12642
12643 /* Remember whether or not we are already within an iteration
12644 statement. */
12645 in_statement = parser->in_statement;
12646
12647 /* See what kind of keyword it is. */
12648 keyword = token->keyword;
12649 switch (keyword)
12650 {
12651 case RID_WHILE:
12652 {
12653 tree condition;
12654
12655 /* Begin the while-statement. */
12656 statement = begin_while_stmt ();
12657 /* Look for the `('. */
12658 matching_parens parens;
12659 parens.require_open (parser);
12660 /* Parse the condition. */
12661 condition = cp_parser_condition (parser);
12662 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12663 /* Look for the `)'. */
12664 parens.require_close (parser);
12665 /* Parse the dependent statement. */
12666 parser->in_statement = IN_ITERATION_STMT;
12667 bool prev = note_iteration_stmt_body_start ();
12668 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12669 note_iteration_stmt_body_end (prev);
12670 parser->in_statement = in_statement;
12671 /* We're done with the while-statement. */
12672 finish_while_stmt (statement);
12673 }
12674 break;
12675
12676 case RID_DO:
12677 {
12678 tree expression;
12679
12680 /* Begin the do-statement. */
12681 statement = begin_do_stmt ();
12682 /* Parse the body of the do-statement. */
12683 parser->in_statement = IN_ITERATION_STMT;
12684 bool prev = note_iteration_stmt_body_start ();
12685 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12686 note_iteration_stmt_body_end (prev);
12687 parser->in_statement = in_statement;
12688 finish_do_body (statement);
12689 /* Look for the `while' keyword. */
12690 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12691 /* Look for the `('. */
12692 matching_parens parens;
12693 parens.require_open (parser);
12694 /* Parse the expression. */
12695 expression = cp_parser_expression (parser);
12696 /* We're done with the do-statement. */
12697 finish_do_stmt (expression, statement, ivdep, unroll);
12698 /* Look for the `)'. */
12699 parens.require_close (parser);
12700 /* Look for the `;'. */
12701 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12702 }
12703 break;
12704
12705 case RID_FOR:
12706 {
12707 /* Look for the `('. */
12708 matching_parens parens;
12709 parens.require_open (parser);
12710
12711 statement = cp_parser_for (parser, ivdep, unroll);
12712
12713 /* Look for the `)'. */
12714 parens.require_close (parser);
12715
12716 /* Parse the body of the for-statement. */
12717 parser->in_statement = IN_ITERATION_STMT;
12718 bool prev = note_iteration_stmt_body_start ();
12719 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12720 note_iteration_stmt_body_end (prev);
12721 parser->in_statement = in_statement;
12722
12723 /* We're done with the for-statement. */
12724 finish_for_stmt (statement);
12725 }
12726 break;
12727
12728 default:
12729 cp_parser_error (parser, "expected iteration-statement");
12730 statement = error_mark_node;
12731 break;
12732 }
12733
12734 return statement;
12735 }
12736
12737 /* Parse a init-statement or the declarator of a range-based-for.
12738 Returns true if a range-based-for declaration is seen.
12739
12740 init-statement:
12741 expression-statement
12742 simple-declaration */
12743
12744 static bool
12745 cp_parser_init_statement (cp_parser *parser, tree *decl)
12746 {
12747 /* If the next token is a `;', then we have an empty
12748 expression-statement. Grammatically, this is also a
12749 simple-declaration, but an invalid one, because it does not
12750 declare anything. Therefore, if we did not handle this case
12751 specially, we would issue an error message about an invalid
12752 declaration. */
12753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12754 {
12755 bool is_range_for = false;
12756 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12757
12758 /* Try to parse the init-statement. */
12759 if (cp_parser_range_based_for_with_init_p (parser))
12760 {
12761 tree dummy;
12762 cp_parser_parse_tentatively (parser);
12763 /* Parse the declaration. */
12764 cp_parser_simple_declaration (parser,
12765 /*function_definition_allowed_p=*/false,
12766 &dummy);
12767 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12768 if (!cp_parser_parse_definitely (parser))
12769 /* That didn't work, try to parse it as an expression-statement. */
12770 cp_parser_expression_statement (parser, NULL_TREE);
12771
12772 if (cxx_dialect < cxx2a)
12773 {
12774 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12775 "range-based %<for%> loops with initializer only "
12776 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
12777 *decl = error_mark_node;
12778 }
12779 }
12780
12781 /* A colon is used in range-based for. */
12782 parser->colon_corrects_to_scope_p = false;
12783
12784 /* We're going to speculatively look for a declaration, falling back
12785 to an expression, if necessary. */
12786 cp_parser_parse_tentatively (parser);
12787 /* Parse the declaration. */
12788 cp_parser_simple_declaration (parser,
12789 /*function_definition_allowed_p=*/false,
12790 decl);
12791 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12792 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12793 {
12794 /* It is a range-for, consume the ':'. */
12795 cp_lexer_consume_token (parser->lexer);
12796 is_range_for = true;
12797 if (cxx_dialect < cxx11)
12798 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12799 "range-based %<for%> loops only available with "
12800 "%<-std=c++11%> or %<-std=gnu++11%>");
12801 }
12802 else
12803 /* The ';' is not consumed yet because we told
12804 cp_parser_simple_declaration not to. */
12805 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12806
12807 if (cp_parser_parse_definitely (parser))
12808 return is_range_for;
12809 /* If the tentative parse failed, then we shall need to look for an
12810 expression-statement. */
12811 }
12812 /* If we are here, it is an expression-statement. */
12813 cp_parser_expression_statement (parser, NULL_TREE);
12814 return false;
12815 }
12816
12817 /* Parse a jump-statement.
12818
12819 jump-statement:
12820 break ;
12821 continue ;
12822 return expression [opt] ;
12823 return braced-init-list ;
12824 goto identifier ;
12825
12826 GNU extension:
12827
12828 jump-statement:
12829 goto * expression ;
12830
12831 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12832
12833 static tree
12834 cp_parser_jump_statement (cp_parser* parser)
12835 {
12836 tree statement = error_mark_node;
12837 cp_token *token;
12838 enum rid keyword;
12839 unsigned char in_statement;
12840
12841 /* Peek at the next token. */
12842 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12843 if (!token)
12844 return error_mark_node;
12845
12846 /* See what kind of keyword it is. */
12847 keyword = token->keyword;
12848 switch (keyword)
12849 {
12850 case RID_BREAK:
12851 in_statement = parser->in_statement & ~IN_IF_STMT;
12852 switch (in_statement)
12853 {
12854 case 0:
12855 error_at (token->location, "break statement not within loop or switch");
12856 break;
12857 default:
12858 gcc_assert ((in_statement & IN_SWITCH_STMT)
12859 || in_statement == IN_ITERATION_STMT);
12860 statement = finish_break_stmt ();
12861 if (in_statement == IN_ITERATION_STMT)
12862 break_maybe_infinite_loop ();
12863 break;
12864 case IN_OMP_BLOCK:
12865 error_at (token->location, "invalid exit from OpenMP structured block");
12866 break;
12867 case IN_OMP_FOR:
12868 error_at (token->location, "break statement used with OpenMP for loop");
12869 break;
12870 }
12871 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12872 break;
12873
12874 case RID_CONTINUE:
12875 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12876 {
12877 case 0:
12878 error_at (token->location, "continue statement not within a loop");
12879 break;
12880 /* Fall through. */
12881 case IN_ITERATION_STMT:
12882 case IN_OMP_FOR:
12883 statement = finish_continue_stmt ();
12884 break;
12885 case IN_OMP_BLOCK:
12886 error_at (token->location, "invalid exit from OpenMP structured block");
12887 break;
12888 default:
12889 gcc_unreachable ();
12890 }
12891 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12892 break;
12893
12894 case RID_RETURN:
12895 {
12896 tree expr;
12897 bool expr_non_constant_p;
12898
12899 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12900 {
12901 cp_lexer_set_source_position (parser->lexer);
12902 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12903 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12904 }
12905 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12906 expr = cp_parser_expression (parser);
12907 else
12908 /* If the next token is a `;', then there is no
12909 expression. */
12910 expr = NULL_TREE;
12911 /* Build the return-statement. */
12912 if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
12913 /* Don't deduce from a discarded return statement. */;
12914 else
12915 statement = finish_return_stmt (expr);
12916 /* Look for the final `;'. */
12917 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12918 }
12919 break;
12920
12921 case RID_GOTO:
12922 if (parser->in_function_body
12923 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12924 {
12925 error ("%<goto%> in %<constexpr%> function");
12926 cp_function_chain->invalid_constexpr = true;
12927 }
12928
12929 /* Create the goto-statement. */
12930 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12931 {
12932 /* Issue a warning about this use of a GNU extension. */
12933 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12934 /* Consume the '*' token. */
12935 cp_lexer_consume_token (parser->lexer);
12936 /* Parse the dependent expression. */
12937 finish_goto_stmt (cp_parser_expression (parser));
12938 }
12939 else
12940 finish_goto_stmt (cp_parser_identifier (parser));
12941 /* Look for the final `;'. */
12942 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12943 break;
12944
12945 default:
12946 cp_parser_error (parser, "expected jump-statement");
12947 break;
12948 }
12949
12950 return statement;
12951 }
12952
12953 /* Parse a declaration-statement.
12954
12955 declaration-statement:
12956 block-declaration */
12957
12958 static void
12959 cp_parser_declaration_statement (cp_parser* parser)
12960 {
12961 void *p;
12962
12963 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12964 p = obstack_alloc (&declarator_obstack, 0);
12965
12966 /* Parse the block-declaration. */
12967 cp_parser_block_declaration (parser, /*statement_p=*/true);
12968
12969 /* Free any declarators allocated. */
12970 obstack_free (&declarator_obstack, p);
12971 }
12972
12973 /* Some dependent statements (like `if (cond) statement'), are
12974 implicitly in their own scope. In other words, if the statement is
12975 a single statement (as opposed to a compound-statement), it is
12976 none-the-less treated as if it were enclosed in braces. Any
12977 declarations appearing in the dependent statement are out of scope
12978 after control passes that point. This function parses a statement,
12979 but ensures that is in its own scope, even if it is not a
12980 compound-statement.
12981
12982 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12983 is a (possibly labeled) if statement which is not enclosed in
12984 braces and has an else clause. This is used to implement
12985 -Wparentheses.
12986
12987 CHAIN is a vector of if-else-if conditions. This is used to implement
12988 -Wduplicated-cond.
12989
12990 Returns the new statement. */
12991
12992 static tree
12993 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12994 const token_indent_info &guard_tinfo,
12995 vec<tree> *chain)
12996 {
12997 tree statement;
12998 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12999 location_t body_loc_after_labels = UNKNOWN_LOCATION;
13000 token_indent_info body_tinfo
13001 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13002
13003 if (if_p != NULL)
13004 *if_p = false;
13005
13006 /* Mark if () ; with a special NOP_EXPR. */
13007 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13008 {
13009 cp_lexer_consume_token (parser->lexer);
13010 statement = add_stmt (build_empty_stmt (body_loc));
13011
13012 if (guard_tinfo.keyword == RID_IF
13013 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
13014 warning_at (body_loc, OPT_Wempty_body,
13015 "suggest braces around empty body in an %<if%> statement");
13016 else if (guard_tinfo.keyword == RID_ELSE)
13017 warning_at (body_loc, OPT_Wempty_body,
13018 "suggest braces around empty body in an %<else%> statement");
13019 }
13020 /* if a compound is opened, we simply parse the statement directly. */
13021 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13022 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
13023 /* If the token is not a `{', then we must take special action. */
13024 else
13025 {
13026 /* Create a compound-statement. */
13027 statement = begin_compound_stmt (0);
13028 /* Parse the dependent-statement. */
13029 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
13030 &body_loc_after_labels);
13031 /* Finish the dummy compound-statement. */
13032 finish_compound_stmt (statement);
13033 }
13034
13035 token_indent_info next_tinfo
13036 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13037 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13038
13039 if (body_loc_after_labels != UNKNOWN_LOCATION
13040 && next_tinfo.type != CPP_SEMICOLON)
13041 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
13042 guard_tinfo.location, guard_tinfo.keyword);
13043
13044 /* Return the statement. */
13045 return statement;
13046 }
13047
13048 /* For some dependent statements (like `while (cond) statement'), we
13049 have already created a scope. Therefore, even if the dependent
13050 statement is a compound-statement, we do not want to create another
13051 scope. */
13052
13053 static void
13054 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
13055 const token_indent_info &guard_tinfo)
13056 {
13057 /* If the token is a `{', then we must take special action. */
13058 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13059 {
13060 token_indent_info body_tinfo
13061 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13062 location_t loc_after_labels = UNKNOWN_LOCATION;
13063
13064 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
13065 &loc_after_labels);
13066 token_indent_info next_tinfo
13067 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13068 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13069
13070 if (loc_after_labels != UNKNOWN_LOCATION
13071 && next_tinfo.type != CPP_SEMICOLON)
13072 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13073 guard_tinfo.location,
13074 guard_tinfo.keyword);
13075 }
13076 else
13077 {
13078 /* Avoid calling cp_parser_compound_statement, so that we
13079 don't create a new scope. Do everything else by hand. */
13080 matching_braces braces;
13081 braces.require_open (parser);
13082 /* If the next keyword is `__label__' we have a label declaration. */
13083 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13084 cp_parser_label_declaration (parser);
13085 /* Parse an (optional) statement-seq. */
13086 cp_parser_statement_seq_opt (parser, NULL_TREE);
13087 braces.require_close (parser);
13088 }
13089 }
13090
13091 /* Declarations [gram.dcl.dcl] */
13092
13093 /* Parse an optional declaration-sequence.
13094
13095 declaration-seq:
13096 declaration
13097 declaration-seq declaration */
13098
13099 static void
13100 cp_parser_declaration_seq_opt (cp_parser* parser)
13101 {
13102 while (true)
13103 {
13104 cp_token *token = cp_lexer_peek_token (parser->lexer);
13105
13106 if (token->type == CPP_CLOSE_BRACE
13107 || token->type == CPP_EOF)
13108 break;
13109 else
13110 cp_parser_toplevel_declaration (parser);
13111 }
13112 }
13113
13114 /* Parse a declaration.
13115
13116 declaration:
13117 block-declaration
13118 function-definition
13119 template-declaration
13120 explicit-instantiation
13121 explicit-specialization
13122 linkage-specification
13123 namespace-definition
13124
13125 C++17:
13126 deduction-guide
13127
13128 GNU extension:
13129
13130 declaration:
13131 __extension__ declaration */
13132
13133 static void
13134 cp_parser_declaration (cp_parser* parser)
13135 {
13136 cp_token token1;
13137 cp_token token2;
13138 int saved_pedantic;
13139 void *p;
13140 tree attributes = NULL_TREE;
13141
13142 /* Check for the `__extension__' keyword. */
13143 if (cp_parser_extension_opt (parser, &saved_pedantic))
13144 {
13145 /* Parse the qualified declaration. */
13146 cp_parser_declaration (parser);
13147 /* Restore the PEDANTIC flag. */
13148 pedantic = saved_pedantic;
13149
13150 return;
13151 }
13152
13153 /* Try to figure out what kind of declaration is present. */
13154 token1 = *cp_lexer_peek_token (parser->lexer);
13155
13156 if (token1.type != CPP_EOF)
13157 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13158 else
13159 {
13160 token2.type = CPP_EOF;
13161 token2.keyword = RID_MAX;
13162 }
13163
13164 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13165 p = obstack_alloc (&declarator_obstack, 0);
13166
13167 /* If the next token is `extern' and the following token is a string
13168 literal, then we have a linkage specification. */
13169 if (token1.keyword == RID_EXTERN
13170 && cp_parser_is_pure_string_literal (&token2))
13171 cp_parser_linkage_specification (parser);
13172 /* If the next token is `template', then we have either a template
13173 declaration, an explicit instantiation, or an explicit
13174 specialization. */
13175 else if (token1.keyword == RID_TEMPLATE)
13176 {
13177 /* `template <>' indicates a template specialization. */
13178 if (token2.type == CPP_LESS
13179 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13180 cp_parser_explicit_specialization (parser);
13181 /* `template <' indicates a template declaration. */
13182 else if (token2.type == CPP_LESS)
13183 cp_parser_template_declaration (parser, /*member_p=*/false);
13184 /* Anything else must be an explicit instantiation. */
13185 else
13186 cp_parser_explicit_instantiation (parser);
13187 }
13188 /* If the next token is `export', then we have a template
13189 declaration. */
13190 else if (token1.keyword == RID_EXPORT)
13191 cp_parser_template_declaration (parser, /*member_p=*/false);
13192 /* If the next token is `extern', 'static' or 'inline' and the one
13193 after that is `template', we have a GNU extended explicit
13194 instantiation directive. */
13195 else if (cp_parser_allow_gnu_extensions_p (parser)
13196 && (token1.keyword == RID_EXTERN
13197 || token1.keyword == RID_STATIC
13198 || token1.keyword == RID_INLINE)
13199 && token2.keyword == RID_TEMPLATE)
13200 cp_parser_explicit_instantiation (parser);
13201 /* If the next token is `namespace', check for a named or unnamed
13202 namespace definition. */
13203 else if (token1.keyword == RID_NAMESPACE
13204 && (/* A named namespace definition. */
13205 (token2.type == CPP_NAME
13206 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13207 != CPP_EQ))
13208 || (token2.type == CPP_OPEN_SQUARE
13209 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13210 == CPP_OPEN_SQUARE)
13211 /* An unnamed namespace definition. */
13212 || token2.type == CPP_OPEN_BRACE
13213 || token2.keyword == RID_ATTRIBUTE))
13214 cp_parser_namespace_definition (parser);
13215 /* An inline (associated) namespace definition. */
13216 else if (token1.keyword == RID_INLINE
13217 && token2.keyword == RID_NAMESPACE)
13218 cp_parser_namespace_definition (parser);
13219 /* Objective-C++ declaration/definition. */
13220 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13221 cp_parser_objc_declaration (parser, NULL_TREE);
13222 else if (c_dialect_objc ()
13223 && token1.keyword == RID_ATTRIBUTE
13224 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13225 cp_parser_objc_declaration (parser, attributes);
13226 /* At this point we may have a template declared by a concept
13227 introduction. */
13228 else if (flag_concepts
13229 && cp_parser_template_declaration_after_export (parser,
13230 /*member_p=*/false))
13231 /* We did. */;
13232 else
13233 /* Try to parse a block-declaration, or a function-definition. */
13234 cp_parser_block_declaration (parser, /*statement_p=*/false);
13235
13236 /* Free any declarators allocated. */
13237 obstack_free (&declarator_obstack, p);
13238 }
13239
13240 /* Parse a namespace-scope declaration. */
13241
13242 static void
13243 cp_parser_toplevel_declaration (cp_parser* parser)
13244 {
13245 cp_token *token = cp_lexer_peek_token (parser->lexer);
13246
13247 if (token->type == CPP_PRAGMA)
13248 /* A top-level declaration can consist solely of a #pragma. A
13249 nested declaration cannot, so this is done here and not in
13250 cp_parser_declaration. (A #pragma at block scope is
13251 handled in cp_parser_statement.) */
13252 cp_parser_pragma (parser, pragma_external, NULL);
13253 else if (token->type == CPP_SEMICOLON)
13254 {
13255 /* A declaration consisting of a single semicolon is
13256 invalid. Allow it unless we're being pedantic. */
13257 cp_lexer_consume_token (parser->lexer);
13258 if (!in_system_header_at (input_location))
13259 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13260 }
13261 else
13262 /* Parse the declaration itself. */
13263 cp_parser_declaration (parser);
13264 }
13265
13266 /* Parse a block-declaration.
13267
13268 block-declaration:
13269 simple-declaration
13270 asm-definition
13271 namespace-alias-definition
13272 using-declaration
13273 using-directive
13274
13275 GNU Extension:
13276
13277 block-declaration:
13278 __extension__ block-declaration
13279
13280 C++0x Extension:
13281
13282 block-declaration:
13283 static_assert-declaration
13284
13285 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13286 part of a declaration-statement. */
13287
13288 static void
13289 cp_parser_block_declaration (cp_parser *parser,
13290 bool statement_p)
13291 {
13292 cp_token *token1;
13293 int saved_pedantic;
13294
13295 /* Check for the `__extension__' keyword. */
13296 if (cp_parser_extension_opt (parser, &saved_pedantic))
13297 {
13298 /* Parse the qualified declaration. */
13299 cp_parser_block_declaration (parser, statement_p);
13300 /* Restore the PEDANTIC flag. */
13301 pedantic = saved_pedantic;
13302
13303 return;
13304 }
13305
13306 /* Peek at the next token to figure out which kind of declaration is
13307 present. */
13308 token1 = cp_lexer_peek_token (parser->lexer);
13309
13310 /* If the next keyword is `asm', we have an asm-definition. */
13311 if (token1->keyword == RID_ASM)
13312 {
13313 if (statement_p)
13314 cp_parser_commit_to_tentative_parse (parser);
13315 cp_parser_asm_definition (parser);
13316 }
13317 /* If the next keyword is `namespace', we have a
13318 namespace-alias-definition. */
13319 else if (token1->keyword == RID_NAMESPACE)
13320 cp_parser_namespace_alias_definition (parser);
13321 /* If the next keyword is `using', we have a
13322 using-declaration, a using-directive, or an alias-declaration. */
13323 else if (token1->keyword == RID_USING)
13324 {
13325 cp_token *token2;
13326
13327 if (statement_p)
13328 cp_parser_commit_to_tentative_parse (parser);
13329 /* If the token after `using' is `namespace', then we have a
13330 using-directive. */
13331 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13332 if (token2->keyword == RID_NAMESPACE)
13333 cp_parser_using_directive (parser);
13334 /* If the second token after 'using' is '=', then we have an
13335 alias-declaration. */
13336 else if (cxx_dialect >= cxx11
13337 && token2->type == CPP_NAME
13338 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13339 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13340 cp_parser_alias_declaration (parser);
13341 /* Otherwise, it's a using-declaration. */
13342 else
13343 cp_parser_using_declaration (parser,
13344 /*access_declaration_p=*/false);
13345 }
13346 /* If the next keyword is `__label__' we have a misplaced label
13347 declaration. */
13348 else if (token1->keyword == RID_LABEL)
13349 {
13350 cp_lexer_consume_token (parser->lexer);
13351 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13352 cp_parser_skip_to_end_of_statement (parser);
13353 /* If the next token is now a `;', consume it. */
13354 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13355 cp_lexer_consume_token (parser->lexer);
13356 }
13357 /* If the next token is `static_assert' we have a static assertion. */
13358 else if (token1->keyword == RID_STATIC_ASSERT)
13359 cp_parser_static_assert (parser, /*member_p=*/false);
13360 /* Anything else must be a simple-declaration. */
13361 else
13362 cp_parser_simple_declaration (parser, !statement_p,
13363 /*maybe_range_for_decl*/NULL);
13364 }
13365
13366 /* Parse a simple-declaration.
13367
13368 simple-declaration:
13369 decl-specifier-seq [opt] init-declarator-list [opt] ;
13370 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13371 brace-or-equal-initializer ;
13372
13373 init-declarator-list:
13374 init-declarator
13375 init-declarator-list , init-declarator
13376
13377 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13378 function-definition as a simple-declaration.
13379
13380 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13381 parsed declaration if it is an uninitialized single declarator not followed
13382 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13383 if present, will not be consumed. */
13384
13385 static void
13386 cp_parser_simple_declaration (cp_parser* parser,
13387 bool function_definition_allowed_p,
13388 tree *maybe_range_for_decl)
13389 {
13390 cp_decl_specifier_seq decl_specifiers;
13391 int declares_class_or_enum;
13392 bool saw_declarator;
13393 location_t comma_loc = UNKNOWN_LOCATION;
13394 location_t init_loc = UNKNOWN_LOCATION;
13395
13396 if (maybe_range_for_decl)
13397 *maybe_range_for_decl = NULL_TREE;
13398
13399 /* Defer access checks until we know what is being declared; the
13400 checks for names appearing in the decl-specifier-seq should be
13401 done as if we were in the scope of the thing being declared. */
13402 push_deferring_access_checks (dk_deferred);
13403
13404 /* Parse the decl-specifier-seq. We have to keep track of whether
13405 or not the decl-specifier-seq declares a named class or
13406 enumeration type, since that is the only case in which the
13407 init-declarator-list is allowed to be empty.
13408
13409 [dcl.dcl]
13410
13411 In a simple-declaration, the optional init-declarator-list can be
13412 omitted only when declaring a class or enumeration, that is when
13413 the decl-specifier-seq contains either a class-specifier, an
13414 elaborated-type-specifier, or an enum-specifier. */
13415 cp_parser_decl_specifier_seq (parser,
13416 CP_PARSER_FLAGS_OPTIONAL,
13417 &decl_specifiers,
13418 &declares_class_or_enum);
13419 /* We no longer need to defer access checks. */
13420 stop_deferring_access_checks ();
13421
13422 /* In a block scope, a valid declaration must always have a
13423 decl-specifier-seq. By not trying to parse declarators, we can
13424 resolve the declaration/expression ambiguity more quickly. */
13425 if (!function_definition_allowed_p
13426 && !decl_specifiers.any_specifiers_p)
13427 {
13428 cp_parser_error (parser, "expected declaration");
13429 goto done;
13430 }
13431
13432 /* If the next two tokens are both identifiers, the code is
13433 erroneous. The usual cause of this situation is code like:
13434
13435 T t;
13436
13437 where "T" should name a type -- but does not. */
13438 if (!decl_specifiers.any_type_specifiers_p
13439 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13440 {
13441 /* If parsing tentatively, we should commit; we really are
13442 looking at a declaration. */
13443 cp_parser_commit_to_tentative_parse (parser);
13444 /* Give up. */
13445 goto done;
13446 }
13447
13448 cp_parser_maybe_commit_to_declaration (parser,
13449 decl_specifiers.any_specifiers_p);
13450
13451 /* Look for C++17 decomposition declaration. */
13452 for (size_t n = 1; ; n++)
13453 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13454 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13455 continue;
13456 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13457 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13458 && decl_specifiers.any_specifiers_p)
13459 {
13460 tree decl
13461 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13462 maybe_range_for_decl,
13463 &init_loc);
13464
13465 /* The next token should be either a `,' or a `;'. */
13466 cp_token *token = cp_lexer_peek_token (parser->lexer);
13467 /* If it's a `;', we are done. */
13468 if (token->type == CPP_SEMICOLON)
13469 goto finish;
13470 else if (maybe_range_for_decl)
13471 {
13472 if (*maybe_range_for_decl == NULL_TREE)
13473 *maybe_range_for_decl = error_mark_node;
13474 goto finish;
13475 }
13476 /* Anything else is an error. */
13477 else
13478 {
13479 /* If we have already issued an error message we don't need
13480 to issue another one. */
13481 if ((decl != error_mark_node
13482 && DECL_INITIAL (decl) != error_mark_node)
13483 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13484 cp_parser_error (parser, "expected %<,%> or %<;%>");
13485 /* Skip tokens until we reach the end of the statement. */
13486 cp_parser_skip_to_end_of_statement (parser);
13487 /* If the next token is now a `;', consume it. */
13488 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13489 cp_lexer_consume_token (parser->lexer);
13490 goto done;
13491 }
13492 }
13493 else
13494 break;
13495
13496 tree last_type;
13497 bool auto_specifier_p;
13498 /* NULL_TREE if both variable and function declaration are allowed,
13499 error_mark_node if function declaration are not allowed and
13500 a FUNCTION_DECL that should be diagnosed if it is followed by
13501 variable declarations. */
13502 tree auto_function_declaration;
13503
13504 last_type = NULL_TREE;
13505 auto_specifier_p
13506 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13507 auto_function_declaration = NULL_TREE;
13508
13509 /* Keep going until we hit the `;' at the end of the simple
13510 declaration. */
13511 saw_declarator = false;
13512 while (cp_lexer_next_token_is_not (parser->lexer,
13513 CPP_SEMICOLON))
13514 {
13515 cp_token *token;
13516 bool function_definition_p;
13517 tree decl;
13518 tree auto_result = NULL_TREE;
13519
13520 if (saw_declarator)
13521 {
13522 /* If we are processing next declarator, comma is expected */
13523 token = cp_lexer_peek_token (parser->lexer);
13524 gcc_assert (token->type == CPP_COMMA);
13525 cp_lexer_consume_token (parser->lexer);
13526 if (maybe_range_for_decl)
13527 {
13528 *maybe_range_for_decl = error_mark_node;
13529 if (comma_loc == UNKNOWN_LOCATION)
13530 comma_loc = token->location;
13531 }
13532 }
13533 else
13534 saw_declarator = true;
13535
13536 /* Parse the init-declarator. */
13537 decl = cp_parser_init_declarator (parser,
13538 CP_PARSER_FLAGS_NONE,
13539 &decl_specifiers,
13540 /*checks=*/NULL,
13541 function_definition_allowed_p,
13542 /*member_p=*/false,
13543 declares_class_or_enum,
13544 &function_definition_p,
13545 maybe_range_for_decl,
13546 &init_loc,
13547 &auto_result);
13548 /* If an error occurred while parsing tentatively, exit quickly.
13549 (That usually happens when in the body of a function; each
13550 statement is treated as a declaration-statement until proven
13551 otherwise.) */
13552 if (cp_parser_error_occurred (parser))
13553 goto done;
13554
13555 if (auto_specifier_p && cxx_dialect >= cxx14)
13556 {
13557 /* If the init-declarator-list contains more than one
13558 init-declarator, they shall all form declarations of
13559 variables. */
13560 if (auto_function_declaration == NULL_TREE)
13561 auto_function_declaration
13562 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13563 else if (TREE_CODE (decl) == FUNCTION_DECL
13564 || auto_function_declaration != error_mark_node)
13565 {
13566 error_at (decl_specifiers.locations[ds_type_spec],
13567 "non-variable %qD in declaration with more than one "
13568 "declarator with placeholder type",
13569 TREE_CODE (decl) == FUNCTION_DECL
13570 ? decl : auto_function_declaration);
13571 auto_function_declaration = error_mark_node;
13572 }
13573 }
13574
13575 if (auto_result
13576 && (!processing_template_decl || !type_uses_auto (auto_result)))
13577 {
13578 if (last_type
13579 && last_type != error_mark_node
13580 && !same_type_p (auto_result, last_type))
13581 {
13582 /* If the list of declarators contains more than one declarator,
13583 the type of each declared variable is determined as described
13584 above. If the type deduced for the template parameter U is not
13585 the same in each deduction, the program is ill-formed. */
13586 error_at (decl_specifiers.locations[ds_type_spec],
13587 "inconsistent deduction for %qT: %qT and then %qT",
13588 decl_specifiers.type, last_type, auto_result);
13589 last_type = error_mark_node;
13590 }
13591 else
13592 last_type = auto_result;
13593 }
13594
13595 /* Handle function definitions specially. */
13596 if (function_definition_p)
13597 {
13598 /* If the next token is a `,', then we are probably
13599 processing something like:
13600
13601 void f() {}, *p;
13602
13603 which is erroneous. */
13604 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13605 {
13606 cp_token *token = cp_lexer_peek_token (parser->lexer);
13607 error_at (token->location,
13608 "mixing"
13609 " declarations and function-definitions is forbidden");
13610 }
13611 /* Otherwise, we're done with the list of declarators. */
13612 else
13613 {
13614 pop_deferring_access_checks ();
13615 return;
13616 }
13617 }
13618 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13619 *maybe_range_for_decl = decl;
13620 /* The next token should be either a `,' or a `;'. */
13621 token = cp_lexer_peek_token (parser->lexer);
13622 /* If it's a `,', there are more declarators to come. */
13623 if (token->type == CPP_COMMA)
13624 /* will be consumed next time around */;
13625 /* If it's a `;', we are done. */
13626 else if (token->type == CPP_SEMICOLON)
13627 break;
13628 else if (maybe_range_for_decl)
13629 {
13630 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13631 permerror (decl_specifiers.locations[ds_type_spec],
13632 "types may not be defined in a for-range-declaration");
13633 break;
13634 }
13635 /* Anything else is an error. */
13636 else
13637 {
13638 /* If we have already issued an error message we don't need
13639 to issue another one. */
13640 if ((decl != error_mark_node
13641 && DECL_INITIAL (decl) != error_mark_node)
13642 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13643 cp_parser_error (parser, "expected %<,%> or %<;%>");
13644 /* Skip tokens until we reach the end of the statement. */
13645 cp_parser_skip_to_end_of_statement (parser);
13646 /* If the next token is now a `;', consume it. */
13647 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13648 cp_lexer_consume_token (parser->lexer);
13649 goto done;
13650 }
13651 /* After the first time around, a function-definition is not
13652 allowed -- even if it was OK at first. For example:
13653
13654 int i, f() {}
13655
13656 is not valid. */
13657 function_definition_allowed_p = false;
13658 }
13659
13660 /* Issue an error message if no declarators are present, and the
13661 decl-specifier-seq does not itself declare a class or
13662 enumeration: [dcl.dcl]/3. */
13663 if (!saw_declarator)
13664 {
13665 if (cp_parser_declares_only_class_p (parser))
13666 {
13667 if (!declares_class_or_enum
13668 && decl_specifiers.type
13669 && OVERLOAD_TYPE_P (decl_specifiers.type))
13670 /* Ensure an error is issued anyway when finish_decltype_type,
13671 called via cp_parser_decl_specifier_seq, returns a class or
13672 an enumeration (c++/51786). */
13673 decl_specifiers.type = NULL_TREE;
13674 shadow_tag (&decl_specifiers);
13675 }
13676 /* Perform any deferred access checks. */
13677 perform_deferred_access_checks (tf_warning_or_error);
13678 }
13679
13680 /* Consume the `;'. */
13681 finish:
13682 if (!maybe_range_for_decl)
13683 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13684 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13685 {
13686 if (init_loc != UNKNOWN_LOCATION)
13687 error_at (init_loc, "initializer in range-based %<for%> loop");
13688 if (comma_loc != UNKNOWN_LOCATION)
13689 error_at (comma_loc,
13690 "multiple declarations in range-based %<for%> loop");
13691 }
13692
13693 done:
13694 pop_deferring_access_checks ();
13695 }
13696
13697 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13698 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13699 initializer ; */
13700
13701 static tree
13702 cp_parser_decomposition_declaration (cp_parser *parser,
13703 cp_decl_specifier_seq *decl_specifiers,
13704 tree *maybe_range_for_decl,
13705 location_t *init_loc)
13706 {
13707 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13708 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13709 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13710
13711 /* Parse the identifier-list. */
13712 auto_vec<cp_expr, 10> v;
13713 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13714 while (true)
13715 {
13716 cp_expr e = cp_parser_identifier (parser);
13717 if (e.get_value () == error_mark_node)
13718 break;
13719 v.safe_push (e);
13720 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13721 break;
13722 cp_lexer_consume_token (parser->lexer);
13723 }
13724
13725 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13726 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13727 {
13728 end_loc = UNKNOWN_LOCATION;
13729 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13730 false);
13731 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13732 cp_lexer_consume_token (parser->lexer);
13733 else
13734 {
13735 cp_parser_skip_to_end_of_statement (parser);
13736 return error_mark_node;
13737 }
13738 }
13739
13740 if (cxx_dialect < cxx17)
13741 pedwarn (loc, 0, "structured bindings only available with "
13742 "%<-std=c++17%> or %<-std=gnu++17%>");
13743
13744 tree pushed_scope;
13745 cp_declarator *declarator = make_declarator (cdk_decomp);
13746 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13747 declarator->id_loc = loc;
13748 if (ref_qual != REF_QUAL_NONE)
13749 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13750 ref_qual == REF_QUAL_RVALUE,
13751 NULL_TREE);
13752 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13753 NULL_TREE, decl_specifiers->attributes,
13754 &pushed_scope);
13755 tree orig_decl = decl;
13756
13757 unsigned int i;
13758 cp_expr e;
13759 cp_decl_specifier_seq decl_specs;
13760 clear_decl_specs (&decl_specs);
13761 decl_specs.type = make_auto ();
13762 tree prev = decl;
13763 FOR_EACH_VEC_ELT (v, i, e)
13764 {
13765 if (i == 0)
13766 declarator = make_id_declarator (NULL_TREE, e.get_value (),
13767 sfk_none, e.get_location ());
13768 else
13769 {
13770 declarator->u.id.unqualified_name = e.get_value ();
13771 declarator->id_loc = e.get_location ();
13772 }
13773 tree elt_pushed_scope;
13774 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13775 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13776 if (decl2 == error_mark_node)
13777 decl = error_mark_node;
13778 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13779 {
13780 /* Ensure we've diagnosed redeclaration if we aren't creating
13781 a new VAR_DECL. */
13782 gcc_assert (errorcount);
13783 decl = error_mark_node;
13784 }
13785 else
13786 prev = decl2;
13787 if (elt_pushed_scope)
13788 pop_scope (elt_pushed_scope);
13789 }
13790
13791 if (v.is_empty ())
13792 {
13793 error_at (loc, "empty structured binding declaration");
13794 decl = error_mark_node;
13795 }
13796
13797 if (maybe_range_for_decl == NULL
13798 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13799 {
13800 bool non_constant_p = false, is_direct_init = false;
13801 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13802 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13803 &non_constant_p);
13804 if (initializer == NULL_TREE
13805 || (TREE_CODE (initializer) == TREE_LIST
13806 && TREE_CHAIN (initializer))
13807 || (is_direct_init
13808 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13809 && CONSTRUCTOR_NELTS (initializer) != 1))
13810 {
13811 error_at (loc, "invalid initializer for structured binding "
13812 "declaration");
13813 initializer = error_mark_node;
13814 }
13815
13816 if (decl != error_mark_node)
13817 {
13818 cp_maybe_mangle_decomp (decl, prev, v.length ());
13819 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13820 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13821 cp_finish_decomp (decl, prev, v.length ());
13822 }
13823 }
13824 else if (decl != error_mark_node)
13825 {
13826 *maybe_range_for_decl = prev;
13827 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13828 the underlying DECL. */
13829 cp_finish_decomp (decl, prev, v.length ());
13830 }
13831
13832 if (pushed_scope)
13833 pop_scope (pushed_scope);
13834
13835 if (decl == error_mark_node && DECL_P (orig_decl))
13836 {
13837 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13838 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13839 }
13840
13841 return decl;
13842 }
13843
13844 /* Parse a decl-specifier-seq.
13845
13846 decl-specifier-seq:
13847 decl-specifier-seq [opt] decl-specifier
13848 decl-specifier attribute-specifier-seq [opt] (C++11)
13849
13850 decl-specifier:
13851 storage-class-specifier
13852 type-specifier
13853 function-specifier
13854 friend
13855 typedef
13856
13857 GNU Extension:
13858
13859 decl-specifier:
13860 attributes
13861
13862 Concepts Extension:
13863
13864 decl-specifier:
13865 concept
13866
13867 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13868
13869 The parser flags FLAGS is used to control type-specifier parsing.
13870
13871 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13872 flags:
13873
13874 1: one of the decl-specifiers is an elaborated-type-specifier
13875 (i.e., a type declaration)
13876 2: one of the decl-specifiers is an enum-specifier or a
13877 class-specifier (i.e., a type definition)
13878
13879 */
13880
13881 static void
13882 cp_parser_decl_specifier_seq (cp_parser* parser,
13883 cp_parser_flags flags,
13884 cp_decl_specifier_seq *decl_specs,
13885 int* declares_class_or_enum)
13886 {
13887 bool constructor_possible_p = !parser->in_declarator_p;
13888 bool found_decl_spec = false;
13889 cp_token *start_token = NULL;
13890 cp_decl_spec ds;
13891
13892 /* Clear DECL_SPECS. */
13893 clear_decl_specs (decl_specs);
13894
13895 /* Assume no class or enumeration type is declared. */
13896 *declares_class_or_enum = 0;
13897
13898 /* Keep reading specifiers until there are no more to read. */
13899 while (true)
13900 {
13901 bool constructor_p;
13902 cp_token *token;
13903 ds = ds_last;
13904
13905 /* Peek at the next token. */
13906 token = cp_lexer_peek_token (parser->lexer);
13907
13908 /* Save the first token of the decl spec list for error
13909 reporting. */
13910 if (!start_token)
13911 start_token = token;
13912 /* Handle attributes. */
13913 if (cp_next_tokens_can_be_attribute_p (parser))
13914 {
13915 /* Parse the attributes. */
13916 tree attrs = cp_parser_attributes_opt (parser);
13917
13918 /* In a sequence of declaration specifiers, c++11 attributes
13919 appertain to the type that precede them. In that case
13920 [dcl.spec]/1 says:
13921
13922 The attribute-specifier-seq affects the type only for
13923 the declaration it appears in, not other declarations
13924 involving the same type.
13925
13926 But for now let's force the user to position the
13927 attribute either at the beginning of the declaration or
13928 after the declarator-id, which would clearly mean that it
13929 applies to the declarator. */
13930 if (cxx11_attribute_p (attrs))
13931 {
13932 if (!found_decl_spec)
13933 /* The c++11 attribute is at the beginning of the
13934 declaration. It appertains to the entity being
13935 declared. */;
13936 else
13937 {
13938 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13939 {
13940 /* This is an attribute following a
13941 class-specifier. */
13942 if (decl_specs->type_definition_p)
13943 warn_misplaced_attr_for_class_type (token->location,
13944 decl_specs->type);
13945 attrs = NULL_TREE;
13946 }
13947 else
13948 {
13949 decl_specs->std_attributes
13950 = attr_chainon (decl_specs->std_attributes, attrs);
13951 if (decl_specs->locations[ds_std_attribute] == 0)
13952 decl_specs->locations[ds_std_attribute] = token->location;
13953 }
13954 continue;
13955 }
13956 }
13957
13958 decl_specs->attributes
13959 = attr_chainon (decl_specs->attributes, attrs);
13960 if (decl_specs->locations[ds_attribute] == 0)
13961 decl_specs->locations[ds_attribute] = token->location;
13962 continue;
13963 }
13964 /* Assume we will find a decl-specifier keyword. */
13965 found_decl_spec = true;
13966 /* If the next token is an appropriate keyword, we can simply
13967 add it to the list. */
13968 switch (token->keyword)
13969 {
13970 /* decl-specifier:
13971 friend
13972 constexpr */
13973 case RID_FRIEND:
13974 if (!at_class_scope_p ())
13975 {
13976 gcc_rich_location richloc (token->location);
13977 richloc.add_fixit_remove ();
13978 error_at (&richloc, "%<friend%> used outside of class");
13979 cp_lexer_purge_token (parser->lexer);
13980 }
13981 else
13982 {
13983 ds = ds_friend;
13984 /* Consume the token. */
13985 cp_lexer_consume_token (parser->lexer);
13986 }
13987 break;
13988
13989 case RID_CONSTEXPR:
13990 ds = ds_constexpr;
13991 cp_lexer_consume_token (parser->lexer);
13992 break;
13993
13994 case RID_CONCEPT:
13995 ds = ds_concept;
13996 cp_lexer_consume_token (parser->lexer);
13997 /* In C++20 a concept definition is just 'concept name = expr;'
13998 Support that syntax by pretending we've seen 'bool'. */
13999 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14000 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
14001 {
14002 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
14003 token, /*type_definition*/false);
14004 decl_specs->any_type_specifiers_p = true;
14005 }
14006 break;
14007
14008 /* function-specifier:
14009 inline
14010 virtual
14011 explicit */
14012 case RID_INLINE:
14013 case RID_VIRTUAL:
14014 case RID_EXPLICIT:
14015 cp_parser_function_specifier_opt (parser, decl_specs);
14016 break;
14017
14018 /* decl-specifier:
14019 typedef */
14020 case RID_TYPEDEF:
14021 ds = ds_typedef;
14022 /* Consume the token. */
14023 cp_lexer_consume_token (parser->lexer);
14024 /* A constructor declarator cannot appear in a typedef. */
14025 constructor_possible_p = false;
14026 /* The "typedef" keyword can only occur in a declaration; we
14027 may as well commit at this point. */
14028 cp_parser_commit_to_tentative_parse (parser);
14029
14030 if (decl_specs->storage_class != sc_none)
14031 decl_specs->conflicting_specifiers_p = true;
14032 break;
14033
14034 /* storage-class-specifier:
14035 auto
14036 register
14037 static
14038 extern
14039 mutable
14040
14041 GNU Extension:
14042 thread */
14043 case RID_AUTO:
14044 if (cxx_dialect == cxx98)
14045 {
14046 /* Consume the token. */
14047 cp_lexer_consume_token (parser->lexer);
14048
14049 /* Complain about `auto' as a storage specifier, if
14050 we're complaining about C++0x compatibility. */
14051 gcc_rich_location richloc (token->location);
14052 richloc.add_fixit_remove ();
14053 warning_at (&richloc, OPT_Wc__11_compat,
14054 "%<auto%> changes meaning in C++11; "
14055 "please remove it");
14056
14057 /* Set the storage class anyway. */
14058 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
14059 token);
14060 }
14061 else
14062 /* C++0x auto type-specifier. */
14063 found_decl_spec = false;
14064 break;
14065
14066 case RID_REGISTER:
14067 case RID_STATIC:
14068 case RID_EXTERN:
14069 case RID_MUTABLE:
14070 /* Consume the token. */
14071 cp_lexer_consume_token (parser->lexer);
14072 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
14073 token);
14074 break;
14075 case RID_THREAD:
14076 /* Consume the token. */
14077 ds = ds_thread;
14078 cp_lexer_consume_token (parser->lexer);
14079 break;
14080
14081 default:
14082 /* We did not yet find a decl-specifier yet. */
14083 found_decl_spec = false;
14084 break;
14085 }
14086
14087 if (found_decl_spec
14088 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14089 && token->keyword != RID_CONSTEXPR)
14090 error ("%<decl-specifier%> invalid in condition");
14091
14092 if (found_decl_spec
14093 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14094 && token->keyword != RID_MUTABLE
14095 && token->keyword != RID_CONSTEXPR)
14096 error_at (token->location, "%qD invalid in lambda",
14097 ridpointers[token->keyword]);
14098
14099 if (ds != ds_last)
14100 set_and_check_decl_spec_loc (decl_specs, ds, token);
14101
14102 /* Constructors are a special case. The `S' in `S()' is not a
14103 decl-specifier; it is the beginning of the declarator. */
14104 constructor_p
14105 = (!found_decl_spec
14106 && constructor_possible_p
14107 && (cp_parser_constructor_declarator_p
14108 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
14109 ds_friend))));
14110
14111 /* If we don't have a DECL_SPEC yet, then we must be looking at
14112 a type-specifier. */
14113 if (!found_decl_spec && !constructor_p)
14114 {
14115 int decl_spec_declares_class_or_enum;
14116 bool is_cv_qualifier;
14117 tree type_spec;
14118
14119 type_spec
14120 = cp_parser_type_specifier (parser, flags,
14121 decl_specs,
14122 /*is_declaration=*/true,
14123 &decl_spec_declares_class_or_enum,
14124 &is_cv_qualifier);
14125 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14126
14127 /* If this type-specifier referenced a user-defined type
14128 (a typedef, class-name, etc.), then we can't allow any
14129 more such type-specifiers henceforth.
14130
14131 [dcl.spec]
14132
14133 The longest sequence of decl-specifiers that could
14134 possibly be a type name is taken as the
14135 decl-specifier-seq of a declaration. The sequence shall
14136 be self-consistent as described below.
14137
14138 [dcl.type]
14139
14140 As a general rule, at most one type-specifier is allowed
14141 in the complete decl-specifier-seq of a declaration. The
14142 only exceptions are the following:
14143
14144 -- const or volatile can be combined with any other
14145 type-specifier.
14146
14147 -- signed or unsigned can be combined with char, long,
14148 short, or int.
14149
14150 -- ..
14151
14152 Example:
14153
14154 typedef char* Pc;
14155 void g (const int Pc);
14156
14157 Here, Pc is *not* part of the decl-specifier seq; it's
14158 the declarator. Therefore, once we see a type-specifier
14159 (other than a cv-qualifier), we forbid any additional
14160 user-defined types. We *do* still allow things like `int
14161 int' to be considered a decl-specifier-seq, and issue the
14162 error message later. */
14163 if (type_spec && !is_cv_qualifier)
14164 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14165 /* A constructor declarator cannot follow a type-specifier. */
14166 if (type_spec)
14167 {
14168 constructor_possible_p = false;
14169 found_decl_spec = true;
14170 if (!is_cv_qualifier)
14171 decl_specs->any_type_specifiers_p = true;
14172
14173 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14174 error_at (token->location, "type-specifier invalid in lambda");
14175 }
14176 }
14177
14178 /* If we still do not have a DECL_SPEC, then there are no more
14179 decl-specifiers. */
14180 if (!found_decl_spec)
14181 break;
14182
14183 decl_specs->any_specifiers_p = true;
14184 /* After we see one decl-specifier, further decl-specifiers are
14185 always optional. */
14186 flags |= CP_PARSER_FLAGS_OPTIONAL;
14187 }
14188
14189 /* Don't allow a friend specifier with a class definition. */
14190 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14191 && (*declares_class_or_enum & 2))
14192 error_at (decl_specs->locations[ds_friend],
14193 "class definition may not be declared a friend");
14194 }
14195
14196 /* Parse an (optional) storage-class-specifier.
14197
14198 storage-class-specifier:
14199 auto
14200 register
14201 static
14202 extern
14203 mutable
14204
14205 GNU Extension:
14206
14207 storage-class-specifier:
14208 thread
14209
14210 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14211
14212 static tree
14213 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14214 {
14215 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14216 {
14217 case RID_AUTO:
14218 if (cxx_dialect != cxx98)
14219 return NULL_TREE;
14220 /* Fall through for C++98. */
14221 gcc_fallthrough ();
14222
14223 case RID_REGISTER:
14224 case RID_STATIC:
14225 case RID_EXTERN:
14226 case RID_MUTABLE:
14227 case RID_THREAD:
14228 /* Consume the token. */
14229 return cp_lexer_consume_token (parser->lexer)->u.value;
14230
14231 default:
14232 return NULL_TREE;
14233 }
14234 }
14235
14236 /* Parse an (optional) function-specifier.
14237
14238 function-specifier:
14239 inline
14240 virtual
14241 explicit
14242
14243 C++2A Extension:
14244 explicit(constant-expression)
14245
14246 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14247 Updates DECL_SPECS, if it is non-NULL. */
14248
14249 static tree
14250 cp_parser_function_specifier_opt (cp_parser* parser,
14251 cp_decl_specifier_seq *decl_specs)
14252 {
14253 cp_token *token = cp_lexer_peek_token (parser->lexer);
14254 switch (token->keyword)
14255 {
14256 case RID_INLINE:
14257 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14258 break;
14259
14260 case RID_VIRTUAL:
14261 /* 14.5.2.3 [temp.mem]
14262
14263 A member function template shall not be virtual. */
14264 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14265 && current_class_type)
14266 error_at (token->location, "templates may not be %<virtual%>");
14267 else
14268 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14269 break;
14270
14271 case RID_EXPLICIT:
14272 {
14273 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14274 /* If we see '(', it's C++20 explicit(bool). */
14275 tree expr;
14276 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14277 {
14278 matching_parens parens;
14279 parens.consume_open (parser);
14280
14281 /* New types are not allowed in an explicit-specifier. */
14282 const char *saved_message
14283 = parser->type_definition_forbidden_message;
14284 parser->type_definition_forbidden_message
14285 = G_("types may not be defined in explicit-specifier");
14286
14287 if (cxx_dialect < cxx2a)
14288 pedwarn (token->location, 0,
14289 "%<explicit(bool)%> only available with %<-std=c++2a%> "
14290 "or %<-std=gnu++2a%>");
14291
14292 /* Parse the constant-expression. */
14293 expr = cp_parser_constant_expression (parser);
14294
14295 /* Restore the saved message. */
14296 parser->type_definition_forbidden_message = saved_message;
14297 parens.require_close (parser);
14298 }
14299 else
14300 /* The explicit-specifier explicit without a constant-expression is
14301 equivalent to the explicit-specifier explicit(true). */
14302 expr = boolean_true_node;
14303
14304 /* [dcl.fct.spec]
14305 "the constant-expression, if supplied, shall be a contextually
14306 converted constant expression of type bool." */
14307 expr = build_explicit_specifier (expr, tf_warning_or_error);
14308 /* We could evaluate it -- mark the decl as appropriate. */
14309 if (expr == boolean_true_node)
14310 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14311 else if (expr == boolean_false_node)
14312 /* Don't mark the decl as explicit. */;
14313 else if (decl_specs)
14314 /* The expression was value-dependent. Remember it so that we can
14315 substitute it later. */
14316 decl_specs->explicit_specifier = expr;
14317 return id;
14318 }
14319
14320 default:
14321 return NULL_TREE;
14322 }
14323
14324 /* Consume the token. */
14325 return cp_lexer_consume_token (parser->lexer)->u.value;
14326 }
14327
14328 /* Parse a linkage-specification.
14329
14330 linkage-specification:
14331 extern string-literal { declaration-seq [opt] }
14332 extern string-literal declaration */
14333
14334 static void
14335 cp_parser_linkage_specification (cp_parser* parser)
14336 {
14337 tree linkage;
14338
14339 /* Look for the `extern' keyword. */
14340 cp_token *extern_token
14341 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14342
14343 /* Look for the string-literal. */
14344 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14345 linkage = cp_parser_string_literal (parser, false, false);
14346
14347 /* Transform the literal into an identifier. If the literal is a
14348 wide-character string, or contains embedded NULs, then we can't
14349 handle it as the user wants. */
14350 if (strlen (TREE_STRING_POINTER (linkage))
14351 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14352 {
14353 cp_parser_error (parser, "invalid linkage-specification");
14354 /* Assume C++ linkage. */
14355 linkage = lang_name_cplusplus;
14356 }
14357 else
14358 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14359
14360 /* We're now using the new linkage. */
14361 push_lang_context (linkage);
14362
14363 /* Preserve the location of the the innermost linkage specification,
14364 tracking the locations of nested specifications via a local. */
14365 location_t saved_location
14366 = parser->innermost_linkage_specification_location;
14367 /* Construct a location ranging from the start of the "extern" to
14368 the end of the string-literal, with the caret at the start, e.g.:
14369 extern "C" {
14370 ^~~~~~~~~~
14371 */
14372 parser->innermost_linkage_specification_location
14373 = make_location (extern_token->location,
14374 extern_token->location,
14375 get_finish (string_token->location));
14376
14377 /* If the next token is a `{', then we're using the first
14378 production. */
14379 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14380 {
14381 cp_ensure_no_omp_declare_simd (parser);
14382 cp_ensure_no_oacc_routine (parser);
14383
14384 /* Consume the `{' token. */
14385 matching_braces braces;
14386 braces.consume_open (parser);
14387 /* Parse the declarations. */
14388 cp_parser_declaration_seq_opt (parser);
14389 /* Look for the closing `}'. */
14390 braces.require_close (parser);
14391 }
14392 /* Otherwise, there's just one declaration. */
14393 else
14394 {
14395 bool saved_in_unbraced_linkage_specification_p;
14396
14397 saved_in_unbraced_linkage_specification_p
14398 = parser->in_unbraced_linkage_specification_p;
14399 parser->in_unbraced_linkage_specification_p = true;
14400 cp_parser_declaration (parser);
14401 parser->in_unbraced_linkage_specification_p
14402 = saved_in_unbraced_linkage_specification_p;
14403 }
14404
14405 /* We're done with the linkage-specification. */
14406 pop_lang_context ();
14407
14408 /* Restore location of parent linkage specification, if any. */
14409 parser->innermost_linkage_specification_location = saved_location;
14410 }
14411
14412 /* Parse a static_assert-declaration.
14413
14414 static_assert-declaration:
14415 static_assert ( constant-expression , string-literal ) ;
14416 static_assert ( constant-expression ) ; (C++17)
14417
14418 If MEMBER_P, this static_assert is a class member. */
14419
14420 static void
14421 cp_parser_static_assert(cp_parser *parser, bool member_p)
14422 {
14423 cp_expr condition;
14424 location_t token_loc;
14425 tree message;
14426 bool dummy;
14427
14428 /* Peek at the `static_assert' token so we can keep track of exactly
14429 where the static assertion started. */
14430 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14431
14432 /* Look for the `static_assert' keyword. */
14433 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14434 RT_STATIC_ASSERT))
14435 return;
14436
14437 /* We know we are in a static assertion; commit to any tentative
14438 parse. */
14439 if (cp_parser_parsing_tentatively (parser))
14440 cp_parser_commit_to_tentative_parse (parser);
14441
14442 /* Parse the `(' starting the static assertion condition. */
14443 matching_parens parens;
14444 parens.require_open (parser);
14445
14446 /* Parse the constant-expression. Allow a non-constant expression
14447 here in order to give better diagnostics in finish_static_assert. */
14448 condition =
14449 cp_parser_constant_expression (parser,
14450 /*allow_non_constant_p=*/true,
14451 /*non_constant_p=*/&dummy);
14452
14453 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14454 {
14455 if (cxx_dialect < cxx17)
14456 pedwarn (input_location, OPT_Wpedantic,
14457 "%<static_assert%> without a message "
14458 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14459 /* Eat the ')' */
14460 cp_lexer_consume_token (parser->lexer);
14461 message = build_string (1, "");
14462 TREE_TYPE (message) = char_array_type_node;
14463 fix_string_type (message);
14464 }
14465 else
14466 {
14467 /* Parse the separating `,'. */
14468 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14469
14470 /* Parse the string-literal message. */
14471 message = cp_parser_string_literal (parser,
14472 /*translate=*/false,
14473 /*wide_ok=*/true);
14474
14475 /* A `)' completes the static assertion. */
14476 if (!parens.require_close (parser))
14477 cp_parser_skip_to_closing_parenthesis (parser,
14478 /*recovering=*/true,
14479 /*or_comma=*/false,
14480 /*consume_paren=*/true);
14481 }
14482
14483 /* A semicolon terminates the declaration. */
14484 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14485
14486 /* Get the location for the static assertion. Use that of the
14487 condition if available, otherwise, use that of the "static_assert"
14488 token. */
14489 location_t assert_loc = condition.get_location ();
14490 if (assert_loc == UNKNOWN_LOCATION)
14491 assert_loc = token_loc;
14492
14493 /* Complete the static assertion, which may mean either processing
14494 the static assert now or saving it for template instantiation. */
14495 finish_static_assert (condition, message, assert_loc, member_p);
14496 }
14497
14498 /* Parse the expression in decltype ( expression ). */
14499
14500 static tree
14501 cp_parser_decltype_expr (cp_parser *parser,
14502 bool &id_expression_or_member_access_p)
14503 {
14504 cp_token *id_expr_start_token;
14505 tree expr;
14506
14507 /* Since we're going to preserve any side-effects from this parse, set up a
14508 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14509 in the expression. */
14510 tentative_firewall firewall (parser);
14511
14512 /* First, try parsing an id-expression. */
14513 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14514 cp_parser_parse_tentatively (parser);
14515 expr = cp_parser_id_expression (parser,
14516 /*template_keyword_p=*/false,
14517 /*check_dependency_p=*/true,
14518 /*template_p=*/NULL,
14519 /*declarator_p=*/false,
14520 /*optional_p=*/false);
14521
14522 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14523 {
14524 bool non_integral_constant_expression_p = false;
14525 tree id_expression = expr;
14526 cp_id_kind idk;
14527 const char *error_msg;
14528
14529 if (identifier_p (expr))
14530 /* Lookup the name we got back from the id-expression. */
14531 expr = cp_parser_lookup_name_simple (parser, expr,
14532 id_expr_start_token->location);
14533
14534 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14535 /* A template without args is not a complete id-expression. */
14536 expr = error_mark_node;
14537
14538 if (expr
14539 && expr != error_mark_node
14540 && TREE_CODE (expr) != TYPE_DECL
14541 && (TREE_CODE (expr) != BIT_NOT_EXPR
14542 || !TYPE_P (TREE_OPERAND (expr, 0)))
14543 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14544 {
14545 /* Complete lookup of the id-expression. */
14546 expr = (finish_id_expression
14547 (id_expression, expr, parser->scope, &idk,
14548 /*integral_constant_expression_p=*/false,
14549 /*allow_non_integral_constant_expression_p=*/true,
14550 &non_integral_constant_expression_p,
14551 /*template_p=*/false,
14552 /*done=*/true,
14553 /*address_p=*/false,
14554 /*template_arg_p=*/false,
14555 &error_msg,
14556 id_expr_start_token->location));
14557
14558 if (expr == error_mark_node)
14559 /* We found an id-expression, but it was something that we
14560 should not have found. This is an error, not something
14561 we can recover from, so note that we found an
14562 id-expression and we'll recover as gracefully as
14563 possible. */
14564 id_expression_or_member_access_p = true;
14565 }
14566
14567 if (expr
14568 && expr != error_mark_node
14569 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14570 /* We have an id-expression. */
14571 id_expression_or_member_access_p = true;
14572 }
14573
14574 if (!id_expression_or_member_access_p)
14575 {
14576 /* Abort the id-expression parse. */
14577 cp_parser_abort_tentative_parse (parser);
14578
14579 /* Parsing tentatively, again. */
14580 cp_parser_parse_tentatively (parser);
14581
14582 /* Parse a class member access. */
14583 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14584 /*cast_p=*/false, /*decltype*/true,
14585 /*member_access_only_p=*/true, NULL);
14586
14587 if (expr
14588 && expr != error_mark_node
14589 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14590 /* We have an id-expression. */
14591 id_expression_or_member_access_p = true;
14592 }
14593
14594 if (id_expression_or_member_access_p)
14595 /* We have parsed the complete id-expression or member access. */
14596 cp_parser_parse_definitely (parser);
14597 else
14598 {
14599 /* Abort our attempt to parse an id-expression or member access
14600 expression. */
14601 cp_parser_abort_tentative_parse (parser);
14602
14603 /* Commit to the tentative_firewall so we get syntax errors. */
14604 cp_parser_commit_to_tentative_parse (parser);
14605
14606 /* Parse a full expression. */
14607 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14608 /*decltype_p=*/true);
14609 }
14610
14611 return expr;
14612 }
14613
14614 /* Parse a `decltype' type. Returns the type.
14615
14616 simple-type-specifier:
14617 decltype ( expression )
14618 C++14 proposal:
14619 decltype ( auto ) */
14620
14621 static tree
14622 cp_parser_decltype (cp_parser *parser)
14623 {
14624 bool id_expression_or_member_access_p = false;
14625 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14626
14627 if (start_token->type == CPP_DECLTYPE)
14628 {
14629 /* Already parsed. */
14630 cp_lexer_consume_token (parser->lexer);
14631 return saved_checks_value (start_token->u.tree_check_value);
14632 }
14633
14634 /* Look for the `decltype' token. */
14635 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14636 return error_mark_node;
14637
14638 /* Parse the opening `('. */
14639 matching_parens parens;
14640 if (!parens.require_open (parser))
14641 return error_mark_node;
14642
14643 push_deferring_access_checks (dk_deferred);
14644
14645 tree expr = NULL_TREE;
14646
14647 if (cxx_dialect >= cxx14
14648 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14649 /* decltype (auto) */
14650 cp_lexer_consume_token (parser->lexer);
14651 else
14652 {
14653 /* decltype (expression) */
14654
14655 /* Types cannot be defined in a `decltype' expression. Save away the
14656 old message and set the new one. */
14657 const char *saved_message = parser->type_definition_forbidden_message;
14658 parser->type_definition_forbidden_message
14659 = G_("types may not be defined in %<decltype%> expressions");
14660
14661 /* The restrictions on constant-expressions do not apply inside
14662 decltype expressions. */
14663 bool saved_integral_constant_expression_p
14664 = parser->integral_constant_expression_p;
14665 bool saved_non_integral_constant_expression_p
14666 = parser->non_integral_constant_expression_p;
14667 parser->integral_constant_expression_p = false;
14668
14669 /* Within a parenthesized expression, a `>' token is always
14670 the greater-than operator. */
14671 bool saved_greater_than_is_operator_p
14672 = parser->greater_than_is_operator_p;
14673 parser->greater_than_is_operator_p = true;
14674
14675 /* Do not actually evaluate the expression. */
14676 ++cp_unevaluated_operand;
14677
14678 /* Do not warn about problems with the expression. */
14679 ++c_inhibit_evaluation_warnings;
14680
14681 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14682 STRIP_ANY_LOCATION_WRAPPER (expr);
14683
14684 /* Go back to evaluating expressions. */
14685 --cp_unevaluated_operand;
14686 --c_inhibit_evaluation_warnings;
14687
14688 /* The `>' token might be the end of a template-id or
14689 template-parameter-list now. */
14690 parser->greater_than_is_operator_p
14691 = saved_greater_than_is_operator_p;
14692
14693 /* Restore the old message and the integral constant expression
14694 flags. */
14695 parser->type_definition_forbidden_message = saved_message;
14696 parser->integral_constant_expression_p
14697 = saved_integral_constant_expression_p;
14698 parser->non_integral_constant_expression_p
14699 = saved_non_integral_constant_expression_p;
14700 }
14701
14702 /* Parse to the closing `)'. */
14703 if (!parens.require_close (parser))
14704 {
14705 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14706 /*consume_paren=*/true);
14707 pop_deferring_access_checks ();
14708 return error_mark_node;
14709 }
14710
14711 if (!expr)
14712 {
14713 /* Build auto. */
14714 expr = make_decltype_auto ();
14715 AUTO_IS_DECLTYPE (expr) = true;
14716 }
14717 else
14718 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14719 tf_warning_or_error);
14720
14721 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14722 it again. */
14723 start_token->type = CPP_DECLTYPE;
14724 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14725 start_token->u.tree_check_value->value = expr;
14726 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14727 start_token->keyword = RID_MAX;
14728 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14729
14730 pop_to_parent_deferring_access_checks ();
14731
14732 return expr;
14733 }
14734
14735 /* Special member functions [gram.special] */
14736
14737 /* Parse a conversion-function-id.
14738
14739 conversion-function-id:
14740 operator conversion-type-id
14741
14742 Returns an IDENTIFIER_NODE representing the operator. */
14743
14744 static tree
14745 cp_parser_conversion_function_id (cp_parser* parser)
14746 {
14747 tree type;
14748 tree saved_scope;
14749 tree saved_qualifying_scope;
14750 tree saved_object_scope;
14751 tree pushed_scope = NULL_TREE;
14752
14753 /* Look for the `operator' token. */
14754 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14755 return error_mark_node;
14756 /* When we parse the conversion-type-id, the current scope will be
14757 reset. However, we need that information in able to look up the
14758 conversion function later, so we save it here. */
14759 saved_scope = parser->scope;
14760 saved_qualifying_scope = parser->qualifying_scope;
14761 saved_object_scope = parser->object_scope;
14762 /* We must enter the scope of the class so that the names of
14763 entities declared within the class are available in the
14764 conversion-type-id. For example, consider:
14765
14766 struct S {
14767 typedef int I;
14768 operator I();
14769 };
14770
14771 S::operator I() { ... }
14772
14773 In order to see that `I' is a type-name in the definition, we
14774 must be in the scope of `S'. */
14775 if (saved_scope)
14776 pushed_scope = push_scope (saved_scope);
14777 /* Parse the conversion-type-id. */
14778 type = cp_parser_conversion_type_id (parser);
14779 /* Leave the scope of the class, if any. */
14780 if (pushed_scope)
14781 pop_scope (pushed_scope);
14782 /* Restore the saved scope. */
14783 parser->scope = saved_scope;
14784 parser->qualifying_scope = saved_qualifying_scope;
14785 parser->object_scope = saved_object_scope;
14786 /* If the TYPE is invalid, indicate failure. */
14787 if (type == error_mark_node)
14788 return error_mark_node;
14789 return make_conv_op_name (type);
14790 }
14791
14792 /* Parse a conversion-type-id:
14793
14794 conversion-type-id:
14795 type-specifier-seq conversion-declarator [opt]
14796
14797 Returns the TYPE specified. */
14798
14799 static tree
14800 cp_parser_conversion_type_id (cp_parser* parser)
14801 {
14802 tree attributes;
14803 cp_decl_specifier_seq type_specifiers;
14804 cp_declarator *declarator;
14805 tree type_specified;
14806 const char *saved_message;
14807
14808 /* Parse the attributes. */
14809 attributes = cp_parser_attributes_opt (parser);
14810
14811 saved_message = parser->type_definition_forbidden_message;
14812 parser->type_definition_forbidden_message
14813 = G_("types may not be defined in a conversion-type-id");
14814
14815 /* Parse the type-specifiers. */
14816 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
14817 /*is_declaration=*/false,
14818 /*is_trailing_return=*/false,
14819 &type_specifiers);
14820
14821 parser->type_definition_forbidden_message = saved_message;
14822
14823 /* If that didn't work, stop. */
14824 if (type_specifiers.type == error_mark_node)
14825 return error_mark_node;
14826 /* Parse the conversion-declarator. */
14827 declarator = cp_parser_conversion_declarator_opt (parser);
14828
14829 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14830 /*initialized=*/0, &attributes);
14831 if (attributes)
14832 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14833
14834 /* Don't give this error when parsing tentatively. This happens to
14835 work because we always parse this definitively once. */
14836 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14837 && type_uses_auto (type_specified))
14838 {
14839 if (cxx_dialect < cxx14)
14840 {
14841 error ("invalid use of %<auto%> in conversion operator");
14842 return error_mark_node;
14843 }
14844 else if (template_parm_scope_p ())
14845 warning (0, "use of %<auto%> in member template "
14846 "conversion operator can never be deduced");
14847 }
14848
14849 return type_specified;
14850 }
14851
14852 /* Parse an (optional) conversion-declarator.
14853
14854 conversion-declarator:
14855 ptr-operator conversion-declarator [opt]
14856
14857 */
14858
14859 static cp_declarator *
14860 cp_parser_conversion_declarator_opt (cp_parser* parser)
14861 {
14862 enum tree_code code;
14863 tree class_type, std_attributes = NULL_TREE;
14864 cp_cv_quals cv_quals;
14865
14866 /* We don't know if there's a ptr-operator next, or not. */
14867 cp_parser_parse_tentatively (parser);
14868 /* Try the ptr-operator. */
14869 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14870 &std_attributes);
14871 /* If it worked, look for more conversion-declarators. */
14872 if (cp_parser_parse_definitely (parser))
14873 {
14874 cp_declarator *declarator;
14875
14876 /* Parse another optional declarator. */
14877 declarator = cp_parser_conversion_declarator_opt (parser);
14878
14879 declarator = cp_parser_make_indirect_declarator
14880 (code, class_type, cv_quals, declarator, std_attributes);
14881
14882 return declarator;
14883 }
14884
14885 return NULL;
14886 }
14887
14888 /* Parse an (optional) ctor-initializer.
14889
14890 ctor-initializer:
14891 : mem-initializer-list */
14892
14893 static void
14894 cp_parser_ctor_initializer_opt (cp_parser* parser)
14895 {
14896 /* If the next token is not a `:', then there is no
14897 ctor-initializer. */
14898 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14899 {
14900 /* Do default initialization of any bases and members. */
14901 if (DECL_CONSTRUCTOR_P (current_function_decl))
14902 finish_mem_initializers (NULL_TREE);
14903 return;
14904 }
14905
14906 /* Consume the `:' token. */
14907 cp_lexer_consume_token (parser->lexer);
14908 /* And the mem-initializer-list. */
14909 cp_parser_mem_initializer_list (parser);
14910 }
14911
14912 /* Parse a mem-initializer-list.
14913
14914 mem-initializer-list:
14915 mem-initializer ... [opt]
14916 mem-initializer ... [opt] , mem-initializer-list */
14917
14918 static void
14919 cp_parser_mem_initializer_list (cp_parser* parser)
14920 {
14921 tree mem_initializer_list = NULL_TREE;
14922 tree target_ctor = error_mark_node;
14923 cp_token *token = cp_lexer_peek_token (parser->lexer);
14924
14925 /* Let the semantic analysis code know that we are starting the
14926 mem-initializer-list. */
14927 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14928 error_at (token->location,
14929 "only constructors take member initializers");
14930
14931 /* Loop through the list. */
14932 while (true)
14933 {
14934 tree mem_initializer;
14935
14936 token = cp_lexer_peek_token (parser->lexer);
14937 /* Parse the mem-initializer. */
14938 mem_initializer = cp_parser_mem_initializer (parser);
14939 /* If the next token is a `...', we're expanding member initializers. */
14940 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14941 if (ellipsis
14942 || (mem_initializer != error_mark_node
14943 && check_for_bare_parameter_packs (TREE_PURPOSE
14944 (mem_initializer))))
14945 {
14946 /* Consume the `...'. */
14947 if (ellipsis)
14948 cp_lexer_consume_token (parser->lexer);
14949
14950 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14951 can be expanded but members cannot. */
14952 if (mem_initializer != error_mark_node
14953 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14954 {
14955 error_at (token->location,
14956 "cannot expand initializer for member %qD",
14957 TREE_PURPOSE (mem_initializer));
14958 mem_initializer = error_mark_node;
14959 }
14960
14961 /* Construct the pack expansion type. */
14962 if (mem_initializer != error_mark_node)
14963 mem_initializer = make_pack_expansion (mem_initializer);
14964 }
14965 if (target_ctor != error_mark_node
14966 && mem_initializer != error_mark_node)
14967 {
14968 error ("mem-initializer for %qD follows constructor delegation",
14969 TREE_PURPOSE (mem_initializer));
14970 mem_initializer = error_mark_node;
14971 }
14972 /* Look for a target constructor. */
14973 if (mem_initializer != error_mark_node
14974 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14975 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14976 {
14977 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14978 if (mem_initializer_list)
14979 {
14980 error ("constructor delegation follows mem-initializer for %qD",
14981 TREE_PURPOSE (mem_initializer_list));
14982 mem_initializer = error_mark_node;
14983 }
14984 target_ctor = mem_initializer;
14985 }
14986 /* Add it to the list, unless it was erroneous. */
14987 if (mem_initializer != error_mark_node)
14988 {
14989 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14990 mem_initializer_list = mem_initializer;
14991 }
14992 /* If the next token is not a `,', we're done. */
14993 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14994 break;
14995 /* Consume the `,' token. */
14996 cp_lexer_consume_token (parser->lexer);
14997 }
14998
14999 /* Perform semantic analysis. */
15000 if (DECL_CONSTRUCTOR_P (current_function_decl))
15001 finish_mem_initializers (mem_initializer_list);
15002 }
15003
15004 /* Parse a mem-initializer.
15005
15006 mem-initializer:
15007 mem-initializer-id ( expression-list [opt] )
15008 mem-initializer-id braced-init-list
15009
15010 GNU extension:
15011
15012 mem-initializer:
15013 ( expression-list [opt] )
15014
15015 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15016 class) or FIELD_DECL (for a non-static data member) to initialize;
15017 the TREE_VALUE is the expression-list. An empty initialization
15018 list is represented by void_list_node. */
15019
15020 static tree
15021 cp_parser_mem_initializer (cp_parser* parser)
15022 {
15023 tree mem_initializer_id;
15024 tree expression_list;
15025 tree member;
15026 cp_token *token = cp_lexer_peek_token (parser->lexer);
15027
15028 /* Find out what is being initialized. */
15029 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15030 {
15031 permerror (token->location,
15032 "anachronistic old-style base class initializer");
15033 mem_initializer_id = NULL_TREE;
15034 }
15035 else
15036 {
15037 mem_initializer_id = cp_parser_mem_initializer_id (parser);
15038 if (mem_initializer_id == error_mark_node)
15039 return mem_initializer_id;
15040 }
15041 member = expand_member_init (mem_initializer_id);
15042 if (member && !DECL_P (member))
15043 in_base_initializer = 1;
15044
15045 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15046 {
15047 bool expr_non_constant_p;
15048 cp_lexer_set_source_position (parser->lexer);
15049 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15050 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
15051 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
15052 expression_list = build_tree_list (NULL_TREE, expression_list);
15053 }
15054 else
15055 {
15056 vec<tree, va_gc> *vec;
15057 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15058 /*cast_p=*/false,
15059 /*allow_expansion_p=*/true,
15060 /*non_constant_p=*/NULL,
15061 /*close_paren_loc=*/NULL,
15062 /*wrap_locations_p=*/true);
15063 if (vec == NULL)
15064 return error_mark_node;
15065 expression_list = build_tree_list_vec (vec);
15066 release_tree_vector (vec);
15067 }
15068
15069 if (expression_list == error_mark_node)
15070 return error_mark_node;
15071 if (!expression_list)
15072 expression_list = void_type_node;
15073
15074 in_base_initializer = 0;
15075
15076 return member ? build_tree_list (member, expression_list) : error_mark_node;
15077 }
15078
15079 /* Parse a mem-initializer-id.
15080
15081 mem-initializer-id:
15082 :: [opt] nested-name-specifier [opt] class-name
15083 decltype-specifier (C++11)
15084 identifier
15085
15086 Returns a TYPE indicating the class to be initialized for the first
15087 production (and the second in C++11). Returns an IDENTIFIER_NODE
15088 indicating the data member to be initialized for the last production. */
15089
15090 static tree
15091 cp_parser_mem_initializer_id (cp_parser* parser)
15092 {
15093 bool global_scope_p;
15094 bool nested_name_specifier_p;
15095 bool template_p = false;
15096 tree id;
15097
15098 cp_token *token = cp_lexer_peek_token (parser->lexer);
15099
15100 /* `typename' is not allowed in this context ([temp.res]). */
15101 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15102 {
15103 error_at (token->location,
15104 "keyword %<typename%> not allowed in this context (a qualified "
15105 "member initializer is implicitly a type)");
15106 cp_lexer_consume_token (parser->lexer);
15107 }
15108 /* Look for the optional `::' operator. */
15109 global_scope_p
15110 = (cp_parser_global_scope_opt (parser,
15111 /*current_scope_valid_p=*/false)
15112 != NULL_TREE);
15113 /* Look for the optional nested-name-specifier. The simplest way to
15114 implement:
15115
15116 [temp.res]
15117
15118 The keyword `typename' is not permitted in a base-specifier or
15119 mem-initializer; in these contexts a qualified name that
15120 depends on a template-parameter is implicitly assumed to be a
15121 type name.
15122
15123 is to assume that we have seen the `typename' keyword at this
15124 point. */
15125 nested_name_specifier_p
15126 = (cp_parser_nested_name_specifier_opt (parser,
15127 /*typename_keyword_p=*/true,
15128 /*check_dependency_p=*/true,
15129 /*type_p=*/true,
15130 /*is_declaration=*/true)
15131 != NULL_TREE);
15132 if (nested_name_specifier_p)
15133 template_p = cp_parser_optional_template_keyword (parser);
15134 /* If there is a `::' operator or a nested-name-specifier, then we
15135 are definitely looking for a class-name. */
15136 if (global_scope_p || nested_name_specifier_p)
15137 return cp_parser_class_name (parser,
15138 /*typename_keyword_p=*/true,
15139 /*template_keyword_p=*/template_p,
15140 typename_type,
15141 /*check_dependency_p=*/true,
15142 /*class_head_p=*/false,
15143 /*is_declaration=*/true);
15144 /* Otherwise, we could also be looking for an ordinary identifier. */
15145 cp_parser_parse_tentatively (parser);
15146 if (cp_lexer_next_token_is_decltype (parser->lexer))
15147 /* Try a decltype-specifier. */
15148 id = cp_parser_decltype (parser);
15149 else
15150 /* Otherwise, try a class-name. */
15151 id = cp_parser_class_name (parser,
15152 /*typename_keyword_p=*/true,
15153 /*template_keyword_p=*/false,
15154 none_type,
15155 /*check_dependency_p=*/true,
15156 /*class_head_p=*/false,
15157 /*is_declaration=*/true);
15158 /* If we found one, we're done. */
15159 if (cp_parser_parse_definitely (parser))
15160 return id;
15161 /* Otherwise, look for an ordinary identifier. */
15162 return cp_parser_identifier (parser);
15163 }
15164
15165 /* Overloading [gram.over] */
15166
15167 /* Parse an operator-function-id.
15168
15169 operator-function-id:
15170 operator operator
15171
15172 Returns an IDENTIFIER_NODE for the operator which is a
15173 human-readable spelling of the identifier, e.g., `operator +'. */
15174
15175 static cp_expr
15176 cp_parser_operator_function_id (cp_parser* parser)
15177 {
15178 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15179 /* Look for the `operator' keyword. */
15180 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15181 return error_mark_node;
15182 /* And then the name of the operator itself. */
15183 return cp_parser_operator (parser, start_loc);
15184 }
15185
15186 /* Return an identifier node for a user-defined literal operator.
15187 The suffix identifier is chained to the operator name identifier. */
15188
15189 tree
15190 cp_literal_operator_id (const char* name)
15191 {
15192 tree identifier;
15193 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15194 + strlen (name) + 10);
15195 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15196 identifier = get_identifier (buffer);
15197
15198 return identifier;
15199 }
15200
15201 /* Parse an operator.
15202
15203 operator:
15204 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15205 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15206 || ++ -- , ->* -> () []
15207
15208 GNU Extensions:
15209
15210 operator:
15211 <? >? <?= >?=
15212
15213 Returns an IDENTIFIER_NODE for the operator which is a
15214 human-readable spelling of the identifier, e.g., `operator +'. */
15215
15216 static cp_expr
15217 cp_parser_operator (cp_parser* parser, location_t start_loc)
15218 {
15219 tree id = NULL_TREE;
15220 cp_token *token;
15221 bool utf8 = false;
15222
15223 /* Peek at the next token. */
15224 token = cp_lexer_peek_token (parser->lexer);
15225
15226 location_t end_loc = token->location;
15227
15228 /* Figure out which operator we have. */
15229 enum tree_code op = ERROR_MARK;
15230 bool assop = false;
15231 bool consumed = false;
15232 switch (token->type)
15233 {
15234 case CPP_KEYWORD:
15235 {
15236 /* The keyword should be either `new' or `delete'. */
15237 if (token->keyword == RID_NEW)
15238 op = NEW_EXPR;
15239 else if (token->keyword == RID_DELETE)
15240 op = DELETE_EXPR;
15241 else
15242 break;
15243
15244 /* Consume the `new' or `delete' token. */
15245 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15246
15247 /* Peek at the next token. */
15248 token = cp_lexer_peek_token (parser->lexer);
15249 /* If it's a `[' token then this is the array variant of the
15250 operator. */
15251 if (token->type == CPP_OPEN_SQUARE)
15252 {
15253 /* Consume the `[' token. */
15254 cp_lexer_consume_token (parser->lexer);
15255 /* Look for the `]' token. */
15256 if (cp_token *close_token
15257 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15258 end_loc = close_token->location;
15259 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15260 }
15261 consumed = true;
15262 break;
15263 }
15264
15265 case CPP_PLUS:
15266 op = PLUS_EXPR;
15267 break;
15268
15269 case CPP_MINUS:
15270 op = MINUS_EXPR;
15271 break;
15272
15273 case CPP_MULT:
15274 op = MULT_EXPR;
15275 break;
15276
15277 case CPP_DIV:
15278 op = TRUNC_DIV_EXPR;
15279 break;
15280
15281 case CPP_MOD:
15282 op = TRUNC_MOD_EXPR;
15283 break;
15284
15285 case CPP_XOR:
15286 op = BIT_XOR_EXPR;
15287 break;
15288
15289 case CPP_AND:
15290 op = BIT_AND_EXPR;
15291 break;
15292
15293 case CPP_OR:
15294 op = BIT_IOR_EXPR;
15295 break;
15296
15297 case CPP_COMPL:
15298 op = BIT_NOT_EXPR;
15299 break;
15300
15301 case CPP_NOT:
15302 op = TRUTH_NOT_EXPR;
15303 break;
15304
15305 case CPP_EQ:
15306 assop = true;
15307 op = NOP_EXPR;
15308 break;
15309
15310 case CPP_LESS:
15311 op = LT_EXPR;
15312 break;
15313
15314 case CPP_GREATER:
15315 op = GT_EXPR;
15316 break;
15317
15318 case CPP_PLUS_EQ:
15319 assop = true;
15320 op = PLUS_EXPR;
15321 break;
15322
15323 case CPP_MINUS_EQ:
15324 assop = true;
15325 op = MINUS_EXPR;
15326 break;
15327
15328 case CPP_MULT_EQ:
15329 assop = true;
15330 op = MULT_EXPR;
15331 break;
15332
15333 case CPP_DIV_EQ:
15334 assop = true;
15335 op = TRUNC_DIV_EXPR;
15336 break;
15337
15338 case CPP_MOD_EQ:
15339 assop = true;
15340 op = TRUNC_MOD_EXPR;
15341 break;
15342
15343 case CPP_XOR_EQ:
15344 assop = true;
15345 op = BIT_XOR_EXPR;
15346 break;
15347
15348 case CPP_AND_EQ:
15349 assop = true;
15350 op = BIT_AND_EXPR;
15351 break;
15352
15353 case CPP_OR_EQ:
15354 assop = true;
15355 op = BIT_IOR_EXPR;
15356 break;
15357
15358 case CPP_LSHIFT:
15359 op = LSHIFT_EXPR;
15360 break;
15361
15362 case CPP_RSHIFT:
15363 op = RSHIFT_EXPR;
15364 break;
15365
15366 case CPP_LSHIFT_EQ:
15367 assop = true;
15368 op = LSHIFT_EXPR;
15369 break;
15370
15371 case CPP_RSHIFT_EQ:
15372 assop = true;
15373 op = RSHIFT_EXPR;
15374 break;
15375
15376 case CPP_EQ_EQ:
15377 op = EQ_EXPR;
15378 break;
15379
15380 case CPP_NOT_EQ:
15381 op = NE_EXPR;
15382 break;
15383
15384 case CPP_LESS_EQ:
15385 op = LE_EXPR;
15386 break;
15387
15388 case CPP_GREATER_EQ:
15389 op = GE_EXPR;
15390 break;
15391
15392 case CPP_AND_AND:
15393 op = TRUTH_ANDIF_EXPR;
15394 break;
15395
15396 case CPP_OR_OR:
15397 op = TRUTH_ORIF_EXPR;
15398 break;
15399
15400 case CPP_PLUS_PLUS:
15401 op = POSTINCREMENT_EXPR;
15402 break;
15403
15404 case CPP_MINUS_MINUS:
15405 op = PREDECREMENT_EXPR;
15406 break;
15407
15408 case CPP_COMMA:
15409 op = COMPOUND_EXPR;
15410 break;
15411
15412 case CPP_DEREF_STAR:
15413 op = MEMBER_REF;
15414 break;
15415
15416 case CPP_DEREF:
15417 op = COMPONENT_REF;
15418 break;
15419
15420 case CPP_OPEN_PAREN:
15421 {
15422 /* Consume the `('. */
15423 matching_parens parens;
15424 parens.consume_open (parser);
15425 /* Look for the matching `)'. */
15426 token = parens.require_close (parser);
15427 if (token)
15428 end_loc = token->location;
15429 op = CALL_EXPR;
15430 consumed = true;
15431 break;
15432 }
15433
15434 case CPP_OPEN_SQUARE:
15435 /* Consume the `['. */
15436 cp_lexer_consume_token (parser->lexer);
15437 /* Look for the matching `]'. */
15438 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15439 if (token)
15440 end_loc = token->location;
15441 op = ARRAY_REF;
15442 consumed = true;
15443 break;
15444
15445 case CPP_UTF8STRING:
15446 case CPP_UTF8STRING_USERDEF:
15447 utf8 = true;
15448 /* FALLTHRU */
15449 case CPP_STRING:
15450 case CPP_WSTRING:
15451 case CPP_STRING16:
15452 case CPP_STRING32:
15453 case CPP_STRING_USERDEF:
15454 case CPP_WSTRING_USERDEF:
15455 case CPP_STRING16_USERDEF:
15456 case CPP_STRING32_USERDEF:
15457 {
15458 cp_expr str;
15459 tree string_tree;
15460 int sz, len;
15461
15462 if (cxx_dialect == cxx98)
15463 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15464
15465 /* Consume the string. */
15466 str = cp_parser_string_literal (parser, /*translate=*/true,
15467 /*wide_ok=*/true, /*lookup_udlit=*/false);
15468 if (str == error_mark_node)
15469 return error_mark_node;
15470 else if (TREE_CODE (str) == USERDEF_LITERAL)
15471 {
15472 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15473 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15474 end_loc = str.get_location ();
15475 }
15476 else
15477 {
15478 string_tree = str;
15479 /* Look for the suffix identifier. */
15480 token = cp_lexer_peek_token (parser->lexer);
15481 if (token->type == CPP_NAME)
15482 {
15483 id = cp_parser_identifier (parser);
15484 end_loc = token->location;
15485 }
15486 else if (token->type == CPP_KEYWORD)
15487 {
15488 error ("unexpected keyword;"
15489 " remove space between quotes and suffix identifier");
15490 return error_mark_node;
15491 }
15492 else
15493 {
15494 error ("expected suffix identifier");
15495 return error_mark_node;
15496 }
15497 }
15498 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15499 (TREE_TYPE (TREE_TYPE (string_tree))));
15500 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15501 if (len != 0)
15502 {
15503 error ("expected empty string after %<operator%> keyword");
15504 return error_mark_node;
15505 }
15506 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15507 != char_type_node)
15508 {
15509 error ("invalid encoding prefix in literal operator");
15510 return error_mark_node;
15511 }
15512 if (id != error_mark_node)
15513 {
15514 const char *name = IDENTIFIER_POINTER (id);
15515 id = cp_literal_operator_id (name);
15516 }
15517 /* Generate a location of the form:
15518 "" _suffix_identifier
15519 ^~~~~~~~~~~~~~~~~~~~~
15520 with caret == start at the start token, finish at the end of the
15521 suffix identifier. */
15522 location_t finish_loc
15523 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15524 location_t combined_loc
15525 = make_location (start_loc, start_loc, finish_loc);
15526 return cp_expr (id, combined_loc);
15527 }
15528
15529 default:
15530 /* Anything else is an error. */
15531 break;
15532 }
15533
15534 /* If we have selected an identifier, we need to consume the
15535 operator token. */
15536 if (op != ERROR_MARK)
15537 {
15538 id = ovl_op_identifier (assop, op);
15539 if (!consumed)
15540 cp_lexer_consume_token (parser->lexer);
15541 }
15542 /* Otherwise, no valid operator name was present. */
15543 else
15544 {
15545 cp_parser_error (parser, "expected operator");
15546 id = error_mark_node;
15547 }
15548
15549 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15550 return cp_expr (id, start_loc);
15551 }
15552
15553 /* Parse a template-declaration.
15554
15555 template-declaration:
15556 export [opt] template < template-parameter-list > declaration
15557
15558 If MEMBER_P is TRUE, this template-declaration occurs within a
15559 class-specifier.
15560
15561 The grammar rule given by the standard isn't correct. What
15562 is really meant is:
15563
15564 template-declaration:
15565 export [opt] template-parameter-list-seq
15566 decl-specifier-seq [opt] init-declarator [opt] ;
15567 export [opt] template-parameter-list-seq
15568 function-definition
15569
15570 template-parameter-list-seq:
15571 template-parameter-list-seq [opt]
15572 template < template-parameter-list >
15573
15574 Concept Extensions:
15575
15576 template-parameter-list-seq:
15577 template < template-parameter-list > requires-clause [opt]
15578
15579 requires-clause:
15580 requires logical-or-expression */
15581
15582 static void
15583 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15584 {
15585 /* Check for `export'. */
15586 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15587 {
15588 /* Consume the `export' token. */
15589 cp_lexer_consume_token (parser->lexer);
15590 /* Warn that we do not support `export'. */
15591 warning (0, "keyword %<export%> not implemented, and will be ignored");
15592 }
15593
15594 cp_parser_template_declaration_after_export (parser, member_p);
15595 }
15596
15597 /* Parse a template-parameter-list.
15598
15599 template-parameter-list:
15600 template-parameter
15601 template-parameter-list , template-parameter
15602
15603 Returns a TREE_LIST. Each node represents a template parameter.
15604 The nodes are connected via their TREE_CHAINs. */
15605
15606 static tree
15607 cp_parser_template_parameter_list (cp_parser* parser)
15608 {
15609 tree parameter_list = NULL_TREE;
15610
15611 /* Don't create wrapper nodes within a template-parameter-list,
15612 since we don't want to have different types based on the
15613 spelling location of constants and decls within them. */
15614 auto_suppress_location_wrappers sentinel;
15615
15616 begin_template_parm_list ();
15617
15618 /* The loop below parses the template parms. We first need to know
15619 the total number of template parms to be able to compute proper
15620 canonical types of each dependent type. So after the loop, when
15621 we know the total number of template parms,
15622 end_template_parm_list computes the proper canonical types and
15623 fixes up the dependent types accordingly. */
15624 while (true)
15625 {
15626 tree parameter;
15627 bool is_non_type;
15628 bool is_parameter_pack;
15629 location_t parm_loc;
15630
15631 /* Parse the template-parameter. */
15632 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15633 parameter = cp_parser_template_parameter (parser,
15634 &is_non_type,
15635 &is_parameter_pack);
15636 /* Add it to the list. */
15637 if (parameter != error_mark_node)
15638 parameter_list = process_template_parm (parameter_list,
15639 parm_loc,
15640 parameter,
15641 is_non_type,
15642 is_parameter_pack);
15643 else
15644 {
15645 tree err_parm = build_tree_list (parameter, parameter);
15646 parameter_list = chainon (parameter_list, err_parm);
15647 }
15648
15649 /* If the next token is not a `,', we're done. */
15650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15651 break;
15652 /* Otherwise, consume the `,' token. */
15653 cp_lexer_consume_token (parser->lexer);
15654 }
15655
15656 return end_template_parm_list (parameter_list);
15657 }
15658
15659 /* Parse a introduction-list.
15660
15661 introduction-list:
15662 introduced-parameter
15663 introduction-list , introduced-parameter
15664
15665 introduced-parameter:
15666 ...[opt] identifier
15667
15668 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15669 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15670 WILDCARD_DECL will also have DECL_NAME set and token location in
15671 DECL_SOURCE_LOCATION. */
15672
15673 static tree
15674 cp_parser_introduction_list (cp_parser *parser)
15675 {
15676 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15677
15678 while (true)
15679 {
15680 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15681 if (is_pack)
15682 cp_lexer_consume_token (parser->lexer);
15683
15684 tree identifier = cp_parser_identifier (parser);
15685 if (identifier == error_mark_node)
15686 break;
15687
15688 /* Build placeholder. */
15689 tree parm = build_nt (WILDCARD_DECL);
15690 DECL_SOURCE_LOCATION (parm)
15691 = cp_lexer_peek_token (parser->lexer)->location;
15692 DECL_NAME (parm) = identifier;
15693 WILDCARD_PACK_P (parm) = is_pack;
15694 vec_safe_push (introduction_vec, parm);
15695
15696 /* If the next token is not a `,', we're done. */
15697 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15698 break;
15699 /* Otherwise, consume the `,' token. */
15700 cp_lexer_consume_token (parser->lexer);
15701 }
15702
15703 /* Convert the vec into a TREE_VEC. */
15704 tree introduction_list = make_tree_vec (introduction_vec->length ());
15705 unsigned int n;
15706 tree parm;
15707 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15708 TREE_VEC_ELT (introduction_list, n) = parm;
15709
15710 release_tree_vector (introduction_vec);
15711 return introduction_list;
15712 }
15713
15714 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15715 is an abstract declarator. */
15716
15717 static inline cp_declarator*
15718 get_id_declarator (cp_declarator *declarator)
15719 {
15720 cp_declarator *d = declarator;
15721 while (d && d->kind != cdk_id)
15722 d = d->declarator;
15723 return d;
15724 }
15725
15726 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15727 is an abstract declarator. */
15728
15729 static inline tree
15730 get_unqualified_id (cp_declarator *declarator)
15731 {
15732 declarator = get_id_declarator (declarator);
15733 if (declarator)
15734 return declarator->u.id.unqualified_name;
15735 else
15736 return NULL_TREE;
15737 }
15738
15739 /* Returns true if DECL represents a constrained-parameter. */
15740
15741 static inline bool
15742 is_constrained_parameter (tree decl)
15743 {
15744 return (decl
15745 && TREE_CODE (decl) == TYPE_DECL
15746 && CONSTRAINED_PARM_CONCEPT (decl)
15747 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15748 }
15749
15750 /* Returns true if PARM declares a constrained-parameter. */
15751
15752 static inline bool
15753 is_constrained_parameter (cp_parameter_declarator *parm)
15754 {
15755 return is_constrained_parameter (parm->decl_specifiers.type);
15756 }
15757
15758 /* Check that the type parameter is only a declarator-id, and that its
15759 type is not cv-qualified. */
15760
15761 bool
15762 cp_parser_check_constrained_type_parm (cp_parser *parser,
15763 cp_parameter_declarator *parm)
15764 {
15765 if (!parm->declarator)
15766 return true;
15767
15768 if (parm->declarator->kind != cdk_id)
15769 {
15770 cp_parser_error (parser, "invalid constrained type parameter");
15771 return false;
15772 }
15773
15774 /* Don't allow cv-qualified type parameters. */
15775 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15776 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15777 {
15778 cp_parser_error (parser, "cv-qualified type parameter");
15779 return false;
15780 }
15781
15782 return true;
15783 }
15784
15785 /* Finish parsing/processing a template type parameter and checking
15786 various restrictions. */
15787
15788 static inline tree
15789 cp_parser_constrained_type_template_parm (cp_parser *parser,
15790 tree id,
15791 cp_parameter_declarator* parmdecl)
15792 {
15793 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15794 return finish_template_type_parm (class_type_node, id);
15795 else
15796 return error_mark_node;
15797 }
15798
15799 static tree
15800 finish_constrained_template_template_parm (tree proto, tree id)
15801 {
15802 /* FIXME: This should probably be copied, and we may need to adjust
15803 the template parameter depths. */
15804 tree saved_parms = current_template_parms;
15805 begin_template_parm_list ();
15806 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15807 end_template_parm_list ();
15808
15809 tree parm = finish_template_template_parm (class_type_node, id);
15810 current_template_parms = saved_parms;
15811
15812 return parm;
15813 }
15814
15815 /* Finish parsing/processing a template template parameter by borrowing
15816 the template parameter list from the prototype parameter. */
15817
15818 static tree
15819 cp_parser_constrained_template_template_parm (cp_parser *parser,
15820 tree proto,
15821 tree id,
15822 cp_parameter_declarator *parmdecl)
15823 {
15824 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15825 return error_mark_node;
15826 return finish_constrained_template_template_parm (proto, id);
15827 }
15828
15829 /* Create a new non-type template parameter from the given PARM
15830 declarator. */
15831
15832 static tree
15833 constrained_non_type_template_parm (bool *is_non_type,
15834 cp_parameter_declarator *parm)
15835 {
15836 *is_non_type = true;
15837 cp_declarator *decl = parm->declarator;
15838 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15839 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15840 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15841 }
15842
15843 /* Build a constrained template parameter based on the PARMDECL
15844 declarator. The type of PARMDECL is the constrained type, which
15845 refers to the prototype template parameter that ultimately
15846 specifies the type of the declared parameter. */
15847
15848 static tree
15849 finish_constrained_parameter (cp_parser *parser,
15850 cp_parameter_declarator *parmdecl,
15851 bool *is_non_type,
15852 bool *is_parameter_pack)
15853 {
15854 tree decl = parmdecl->decl_specifiers.type;
15855 tree id = get_unqualified_id (parmdecl->declarator);
15856 tree def = parmdecl->default_argument;
15857 tree proto = DECL_INITIAL (decl);
15858
15859 /* A template parameter constrained by a variadic concept shall also
15860 be declared as a template parameter pack. */
15861 bool is_variadic = template_parameter_pack_p (proto);
15862 if (is_variadic && !*is_parameter_pack)
15863 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15864
15865 /* Build the parameter. Return an error if the declarator was invalid. */
15866 tree parm;
15867 if (TREE_CODE (proto) == TYPE_DECL)
15868 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15869 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15870 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15871 parmdecl);
15872 else
15873 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15874 if (parm == error_mark_node)
15875 return error_mark_node;
15876
15877 /* Finish the parameter decl and create a node attaching the
15878 default argument and constraint. */
15879 parm = build_tree_list (def, parm);
15880 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15881
15882 return parm;
15883 }
15884
15885 /* Returns true if the parsed type actually represents the declaration
15886 of a type template-parameter. */
15887
15888 static inline bool
15889 declares_constrained_type_template_parameter (tree type)
15890 {
15891 return (is_constrained_parameter (type)
15892 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15893 }
15894
15895
15896 /* Returns true if the parsed type actually represents the declaration of
15897 a template template-parameter. */
15898
15899 static bool
15900 declares_constrained_template_template_parameter (tree type)
15901 {
15902 return (is_constrained_parameter (type)
15903 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15904 }
15905
15906 /* Parse a default argument for a type template-parameter.
15907 Note that diagnostics are handled in cp_parser_template_parameter. */
15908
15909 static tree
15910 cp_parser_default_type_template_argument (cp_parser *parser)
15911 {
15912 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15913
15914 /* Consume the `=' token. */
15915 cp_lexer_consume_token (parser->lexer);
15916
15917 cp_token *token = cp_lexer_peek_token (parser->lexer);
15918
15919 /* Parse the default-argument. */
15920 push_deferring_access_checks (dk_no_deferred);
15921 tree default_argument = cp_parser_type_id (parser,
15922 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15923 NULL);
15924 pop_deferring_access_checks ();
15925
15926 if (flag_concepts && type_uses_auto (default_argument))
15927 {
15928 error_at (token->location,
15929 "invalid use of %<auto%> in default template argument");
15930 return error_mark_node;
15931 }
15932
15933 return default_argument;
15934 }
15935
15936 /* Parse a default argument for a template template-parameter. */
15937
15938 static tree
15939 cp_parser_default_template_template_argument (cp_parser *parser)
15940 {
15941 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15942
15943 bool is_template;
15944
15945 /* Consume the `='. */
15946 cp_lexer_consume_token (parser->lexer);
15947 /* Parse the id-expression. */
15948 push_deferring_access_checks (dk_no_deferred);
15949 /* save token before parsing the id-expression, for error
15950 reporting */
15951 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15952 tree default_argument
15953 = cp_parser_id_expression (parser,
15954 /*template_keyword_p=*/false,
15955 /*check_dependency_p=*/true,
15956 /*template_p=*/&is_template,
15957 /*declarator_p=*/false,
15958 /*optional_p=*/false);
15959 if (TREE_CODE (default_argument) == TYPE_DECL)
15960 /* If the id-expression was a template-id that refers to
15961 a template-class, we already have the declaration here,
15962 so no further lookup is needed. */
15963 ;
15964 else
15965 /* Look up the name. */
15966 default_argument
15967 = cp_parser_lookup_name (parser, default_argument,
15968 none_type,
15969 /*is_template=*/is_template,
15970 /*is_namespace=*/false,
15971 /*check_dependency=*/true,
15972 /*ambiguous_decls=*/NULL,
15973 token->location);
15974 /* See if the default argument is valid. */
15975 default_argument = check_template_template_default_arg (default_argument);
15976 pop_deferring_access_checks ();
15977 return default_argument;
15978 }
15979
15980 /* Parse a template-parameter.
15981
15982 template-parameter:
15983 type-parameter
15984 parameter-declaration
15985
15986 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15987 the parameter. The TREE_PURPOSE is the default value, if any.
15988 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15989 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15990 set to true iff this parameter is a parameter pack. */
15991
15992 static tree
15993 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15994 bool *is_parameter_pack)
15995 {
15996 cp_token *token;
15997 cp_parameter_declarator *parameter_declarator;
15998 tree parm;
15999
16000 /* Assume it is a type parameter or a template parameter. */
16001 *is_non_type = false;
16002 /* Assume it not a parameter pack. */
16003 *is_parameter_pack = false;
16004 /* Peek at the next token. */
16005 token = cp_lexer_peek_token (parser->lexer);
16006 /* If it is `template', we have a type-parameter. */
16007 if (token->keyword == RID_TEMPLATE)
16008 return cp_parser_type_parameter (parser, is_parameter_pack);
16009 /* If it is `class' or `typename' we do not know yet whether it is a
16010 type parameter or a non-type parameter. Consider:
16011
16012 template <typename T, typename T::X X> ...
16013
16014 or:
16015
16016 template <class C, class D*> ...
16017
16018 Here, the first parameter is a type parameter, and the second is
16019 a non-type parameter. We can tell by looking at the token after
16020 the identifier -- if it is a `,', `=', or `>' then we have a type
16021 parameter. */
16022 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
16023 {
16024 /* Peek at the token after `class' or `typename'. */
16025 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16026 /* If it's an ellipsis, we have a template type parameter
16027 pack. */
16028 if (token->type == CPP_ELLIPSIS)
16029 return cp_parser_type_parameter (parser, is_parameter_pack);
16030 /* If it's an identifier, skip it. */
16031 if (token->type == CPP_NAME)
16032 token = cp_lexer_peek_nth_token (parser->lexer, 3);
16033 /* Now, see if the token looks like the end of a template
16034 parameter. */
16035 if (token->type == CPP_COMMA
16036 || token->type == CPP_EQ
16037 || token->type == CPP_GREATER)
16038 return cp_parser_type_parameter (parser, is_parameter_pack);
16039 }
16040
16041 /* Otherwise, it is a non-type parameter or a constrained parameter.
16042
16043 [temp.param]
16044
16045 When parsing a default template-argument for a non-type
16046 template-parameter, the first non-nested `>' is taken as the end
16047 of the template parameter-list rather than a greater-than
16048 operator. */
16049 parameter_declarator
16050 = cp_parser_parameter_declaration (parser,
16051 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16052 /*template_parm_p=*/true,
16053 /*parenthesized_p=*/NULL);
16054
16055 if (!parameter_declarator)
16056 return error_mark_node;
16057
16058 /* If the parameter declaration is marked as a parameter pack, set
16059 *IS_PARAMETER_PACK to notify the caller. */
16060 if (parameter_declarator->template_parameter_pack_p)
16061 *is_parameter_pack = true;
16062
16063 if (parameter_declarator->default_argument)
16064 {
16065 /* Can happen in some cases of erroneous input (c++/34892). */
16066 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16067 /* Consume the `...' for better error recovery. */
16068 cp_lexer_consume_token (parser->lexer);
16069 }
16070
16071 // The parameter may have been constrained.
16072 if (is_constrained_parameter (parameter_declarator))
16073 return finish_constrained_parameter (parser,
16074 parameter_declarator,
16075 is_non_type,
16076 is_parameter_pack);
16077
16078 // Now we're sure that the parameter is a non-type parameter.
16079 *is_non_type = true;
16080
16081 parm = grokdeclarator (parameter_declarator->declarator,
16082 &parameter_declarator->decl_specifiers,
16083 TPARM, /*initialized=*/0,
16084 /*attrlist=*/NULL);
16085 if (parm == error_mark_node)
16086 return error_mark_node;
16087
16088 return build_tree_list (parameter_declarator->default_argument, parm);
16089 }
16090
16091 /* Parse a type-parameter.
16092
16093 type-parameter:
16094 class identifier [opt]
16095 class identifier [opt] = type-id
16096 typename identifier [opt]
16097 typename identifier [opt] = type-id
16098 template < template-parameter-list > class identifier [opt]
16099 template < template-parameter-list > class identifier [opt]
16100 = id-expression
16101
16102 GNU Extension (variadic templates):
16103
16104 type-parameter:
16105 class ... identifier [opt]
16106 typename ... identifier [opt]
16107
16108 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16109 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16110 the declaration of the parameter.
16111
16112 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16113
16114 static tree
16115 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
16116 {
16117 cp_token *token;
16118 tree parameter;
16119
16120 /* Look for a keyword to tell us what kind of parameter this is. */
16121 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
16122 if (!token)
16123 return error_mark_node;
16124
16125 switch (token->keyword)
16126 {
16127 case RID_CLASS:
16128 case RID_TYPENAME:
16129 {
16130 tree identifier;
16131 tree default_argument;
16132
16133 /* If the next token is an ellipsis, we have a template
16134 argument pack. */
16135 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16136 {
16137 /* Consume the `...' token. */
16138 cp_lexer_consume_token (parser->lexer);
16139 maybe_warn_variadic_templates ();
16140
16141 *is_parameter_pack = true;
16142 }
16143
16144 /* If the next token is an identifier, then it names the
16145 parameter. */
16146 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16147 identifier = cp_parser_identifier (parser);
16148 else
16149 identifier = NULL_TREE;
16150
16151 /* Create the parameter. */
16152 parameter = finish_template_type_parm (class_type_node, identifier);
16153
16154 /* If the next token is an `=', we have a default argument. */
16155 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16156 {
16157 default_argument
16158 = cp_parser_default_type_template_argument (parser);
16159
16160 /* Template parameter packs cannot have default
16161 arguments. */
16162 if (*is_parameter_pack)
16163 {
16164 if (identifier)
16165 error_at (token->location,
16166 "template parameter pack %qD cannot have a "
16167 "default argument", identifier);
16168 else
16169 error_at (token->location,
16170 "template parameter packs cannot have "
16171 "default arguments");
16172 default_argument = NULL_TREE;
16173 }
16174 else if (check_for_bare_parameter_packs (default_argument))
16175 default_argument = error_mark_node;
16176 }
16177 else
16178 default_argument = NULL_TREE;
16179
16180 /* Create the combined representation of the parameter and the
16181 default argument. */
16182 parameter = build_tree_list (default_argument, parameter);
16183 }
16184 break;
16185
16186 case RID_TEMPLATE:
16187 {
16188 tree identifier;
16189 tree default_argument;
16190
16191 /* Look for the `<'. */
16192 cp_parser_require (parser, CPP_LESS, RT_LESS);
16193 /* Parse the template-parameter-list. */
16194 cp_parser_template_parameter_list (parser);
16195 /* Look for the `>'. */
16196 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16197
16198 // If template requirements are present, parse them.
16199 if (flag_concepts)
16200 {
16201 tree reqs = get_shorthand_constraints (current_template_parms);
16202 if (tree r = cp_parser_requires_clause_opt (parser))
16203 reqs = conjoin_constraints (reqs, normalize_expression (r));
16204 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16205 }
16206
16207 /* Look for the `class' or 'typename' keywords. */
16208 cp_parser_type_parameter_key (parser);
16209 /* If the next token is an ellipsis, we have a template
16210 argument pack. */
16211 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16212 {
16213 /* Consume the `...' token. */
16214 cp_lexer_consume_token (parser->lexer);
16215 maybe_warn_variadic_templates ();
16216
16217 *is_parameter_pack = true;
16218 }
16219 /* If the next token is an `=', then there is a
16220 default-argument. If the next token is a `>', we are at
16221 the end of the parameter-list. If the next token is a `,',
16222 then we are at the end of this parameter. */
16223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16224 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16225 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16226 {
16227 identifier = cp_parser_identifier (parser);
16228 /* Treat invalid names as if the parameter were nameless. */
16229 if (identifier == error_mark_node)
16230 identifier = NULL_TREE;
16231 }
16232 else
16233 identifier = NULL_TREE;
16234
16235 /* Create the template parameter. */
16236 parameter = finish_template_template_parm (class_type_node,
16237 identifier);
16238
16239 /* If the next token is an `=', then there is a
16240 default-argument. */
16241 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16242 {
16243 default_argument
16244 = cp_parser_default_template_template_argument (parser);
16245
16246 /* Template parameter packs cannot have default
16247 arguments. */
16248 if (*is_parameter_pack)
16249 {
16250 if (identifier)
16251 error_at (token->location,
16252 "template parameter pack %qD cannot "
16253 "have a default argument",
16254 identifier);
16255 else
16256 error_at (token->location, "template parameter packs cannot "
16257 "have default arguments");
16258 default_argument = NULL_TREE;
16259 }
16260 }
16261 else
16262 default_argument = NULL_TREE;
16263
16264 /* Create the combined representation of the parameter and the
16265 default argument. */
16266 parameter = build_tree_list (default_argument, parameter);
16267 }
16268 break;
16269
16270 default:
16271 gcc_unreachable ();
16272 break;
16273 }
16274
16275 return parameter;
16276 }
16277
16278 /* Parse a template-id.
16279
16280 template-id:
16281 template-name < template-argument-list [opt] >
16282
16283 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16284 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16285 returned. Otherwise, if the template-name names a function, or set
16286 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16287 names a class, returns a TYPE_DECL for the specialization.
16288
16289 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16290 uninstantiated templates. */
16291
16292 static tree
16293 cp_parser_template_id (cp_parser *parser,
16294 bool template_keyword_p,
16295 bool check_dependency_p,
16296 enum tag_types tag_type,
16297 bool is_declaration)
16298 {
16299 tree templ;
16300 tree arguments;
16301 tree template_id;
16302 cp_token_position start_of_id = 0;
16303 cp_token *next_token = NULL, *next_token_2 = NULL;
16304 bool is_identifier;
16305
16306 /* If the next token corresponds to a template-id, there is no need
16307 to reparse it. */
16308 cp_token *token = cp_lexer_peek_token (parser->lexer);
16309 if (token->type == CPP_TEMPLATE_ID)
16310 {
16311 cp_lexer_consume_token (parser->lexer);
16312 return saved_checks_value (token->u.tree_check_value);
16313 }
16314
16315 /* Avoid performing name lookup if there is no possibility of
16316 finding a template-id. */
16317 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16318 || (token->type == CPP_NAME
16319 && !cp_parser_nth_token_starts_template_argument_list_p
16320 (parser, 2)))
16321 {
16322 cp_parser_error (parser, "expected template-id");
16323 return error_mark_node;
16324 }
16325
16326 /* Remember where the template-id starts. */
16327 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16328 start_of_id = cp_lexer_token_position (parser->lexer, false);
16329
16330 push_deferring_access_checks (dk_deferred);
16331
16332 /* Parse the template-name. */
16333 is_identifier = false;
16334 templ = cp_parser_template_name (parser, template_keyword_p,
16335 check_dependency_p,
16336 is_declaration,
16337 tag_type,
16338 &is_identifier);
16339
16340 /* Push any access checks inside the firewall we're about to create. */
16341 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16342 pop_deferring_access_checks ();
16343 if (templ == error_mark_node || is_identifier)
16344 return templ;
16345
16346 /* Since we're going to preserve any side-effects from this parse, set up a
16347 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16348 in the template arguments. */
16349 tentative_firewall firewall (parser);
16350 reopen_deferring_access_checks (checks);
16351
16352 /* If we find the sequence `[:' after a template-name, it's probably
16353 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16354 parse correctly the argument list. */
16355 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16356 == CPP_OPEN_SQUARE)
16357 && next_token->flags & DIGRAPH
16358 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16359 == CPP_COLON)
16360 && !(next_token_2->flags & PREV_WHITE))
16361 {
16362 cp_parser_parse_tentatively (parser);
16363 /* Change `:' into `::'. */
16364 next_token_2->type = CPP_SCOPE;
16365 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16366 CPP_LESS. */
16367 cp_lexer_consume_token (parser->lexer);
16368
16369 /* Parse the arguments. */
16370 arguments = cp_parser_enclosed_template_argument_list (parser);
16371 if (!cp_parser_parse_definitely (parser))
16372 {
16373 /* If we couldn't parse an argument list, then we revert our changes
16374 and return simply an error. Maybe this is not a template-id
16375 after all. */
16376 next_token_2->type = CPP_COLON;
16377 cp_parser_error (parser, "expected %<<%>");
16378 pop_deferring_access_checks ();
16379 return error_mark_node;
16380 }
16381 /* Otherwise, emit an error about the invalid digraph, but continue
16382 parsing because we got our argument list. */
16383 if (permerror (next_token->location,
16384 "%<<::%> cannot begin a template-argument list"))
16385 {
16386 static bool hint = false;
16387 inform (next_token->location,
16388 "%<<:%> is an alternate spelling for %<[%>."
16389 " Insert whitespace between %<<%> and %<::%>");
16390 if (!hint && !flag_permissive)
16391 {
16392 inform (next_token->location, "(if you use %<-fpermissive%> "
16393 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16394 "accept your code)");
16395 hint = true;
16396 }
16397 }
16398 }
16399 else
16400 {
16401 /* Look for the `<' that starts the template-argument-list. */
16402 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16403 {
16404 pop_deferring_access_checks ();
16405 return error_mark_node;
16406 }
16407 /* Parse the arguments. */
16408 arguments = cp_parser_enclosed_template_argument_list (parser);
16409
16410 if ((cxx_dialect > cxx17)
16411 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16412 && !template_keyword_p
16413 && (cp_parser_error_occurred (parser)
16414 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16415 {
16416 /* This didn't go well. */
16417 if (TREE_CODE (templ) == FUNCTION_DECL)
16418 {
16419 /* C++2A says that "function-name < a;" is now ill-formed. */
16420 if (cp_parser_error_occurred (parser))
16421 {
16422 error_at (token->location, "invalid template-argument-list");
16423 inform (token->location, "function name as the left hand "
16424 "operand of %<<%> is ill-formed in C++2a; wrap the "
16425 "function name in %<()%>");
16426 }
16427 else
16428 /* We expect "f<targs>" to be followed by "(args)". */
16429 error_at (cp_lexer_peek_token (parser->lexer)->location,
16430 "expected %<(%> after template-argument-list");
16431 if (start_of_id)
16432 /* Purge all subsequent tokens. */
16433 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16434 }
16435 else
16436 cp_parser_simulate_error (parser);
16437 pop_deferring_access_checks ();
16438 return error_mark_node;
16439 }
16440 }
16441
16442 /* Set the location to be of the form:
16443 template-name < template-argument-list [opt] >
16444 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16445 with caret == start at the start of the template-name,
16446 ranging until the closing '>'. */
16447 location_t finish_loc
16448 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16449 location_t combined_loc
16450 = make_location (token->location, token->location, finish_loc);
16451
16452 /* Check for concepts autos where they don't belong. We could
16453 identify types in some cases of idnetifier TEMPL, looking ahead
16454 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16455 types. We reject them in functions, but if what we have is an
16456 identifier, even with none_type we can't conclude it's NOT a
16457 type, we have to wait for template substitution. */
16458 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16459 template_id = error_mark_node;
16460 /* Build a representation of the specialization. */
16461 else if (identifier_p (templ))
16462 template_id = build_min_nt_loc (combined_loc,
16463 TEMPLATE_ID_EXPR,
16464 templ, arguments);
16465 else if (DECL_TYPE_TEMPLATE_P (templ)
16466 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16467 {
16468 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16469 template (rather than some instantiation thereof) only if
16470 is not nested within some other construct. For example, in
16471 "template <typename T> void f(T) { A<T>::", A<T> is just an
16472 instantiation of A. */
16473 bool entering_scope
16474 = (template_parm_scope_p ()
16475 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16476 template_id
16477 = finish_template_type (templ, arguments, entering_scope);
16478 }
16479 /* A template-like identifier may be a partial concept id. */
16480 else if (flag_concepts
16481 && (template_id = (cp_parser_maybe_partial_concept_id
16482 (parser, templ, arguments))))
16483 return template_id;
16484 else if (variable_template_p (templ))
16485 {
16486 template_id = lookup_template_variable (templ, arguments);
16487 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16488 SET_EXPR_LOCATION (template_id, combined_loc);
16489 }
16490 else
16491 {
16492 /* If it's not a class-template or a template-template, it should be
16493 a function-template. */
16494 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
16495
16496 template_id = lookup_template_function (templ, arguments);
16497 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16498 SET_EXPR_LOCATION (template_id, combined_loc);
16499 }
16500
16501 /* If parsing tentatively, replace the sequence of tokens that makes
16502 up the template-id with a CPP_TEMPLATE_ID token. That way,
16503 should we re-parse the token stream, we will not have to repeat
16504 the effort required to do the parse, nor will we issue duplicate
16505 error messages about problems during instantiation of the
16506 template. */
16507 if (start_of_id
16508 /* Don't do this if we had a parse error in a declarator; re-parsing
16509 might succeed if a name changes meaning (60361). */
16510 && !(cp_parser_error_occurred (parser)
16511 && cp_parser_parsing_tentatively (parser)
16512 && parser->in_declarator_p))
16513 {
16514 /* Reset the contents of the START_OF_ID token. */
16515 token->type = CPP_TEMPLATE_ID;
16516 token->location = combined_loc;
16517
16518 /* Retrieve any deferred checks. Do not pop this access checks yet
16519 so the memory will not be reclaimed during token replacing below. */
16520 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16521 token->u.tree_check_value->value = template_id;
16522 token->u.tree_check_value->checks = get_deferred_access_checks ();
16523 token->keyword = RID_MAX;
16524
16525 /* Purge all subsequent tokens. */
16526 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16527
16528 /* ??? Can we actually assume that, if template_id ==
16529 error_mark_node, we will have issued a diagnostic to the
16530 user, as opposed to simply marking the tentative parse as
16531 failed? */
16532 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16533 error_at (token->location, "parse error in template argument list");
16534 }
16535
16536 pop_to_parent_deferring_access_checks ();
16537 return template_id;
16538 }
16539
16540 /* Parse a template-name.
16541
16542 template-name:
16543 identifier
16544
16545 The standard should actually say:
16546
16547 template-name:
16548 identifier
16549 operator-function-id
16550
16551 A defect report has been filed about this issue.
16552
16553 A conversion-function-id cannot be a template name because they cannot
16554 be part of a template-id. In fact, looking at this code:
16555
16556 a.operator K<int>()
16557
16558 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16559 It is impossible to call a templated conversion-function-id with an
16560 explicit argument list, since the only allowed template parameter is
16561 the type to which it is converting.
16562
16563 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16564 `template' keyword, in a construction like:
16565
16566 T::template f<3>()
16567
16568 In that case `f' is taken to be a template-name, even though there
16569 is no way of knowing for sure.
16570
16571 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16572 name refers to a set of overloaded functions, at least one of which
16573 is a template, or an IDENTIFIER_NODE with the name of the template,
16574 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16575 names are looked up inside uninstantiated templates. */
16576
16577 static tree
16578 cp_parser_template_name (cp_parser* parser,
16579 bool template_keyword_p,
16580 bool check_dependency_p,
16581 bool is_declaration,
16582 enum tag_types tag_type,
16583 bool *is_identifier)
16584 {
16585 tree identifier;
16586 tree decl;
16587 cp_token *token = cp_lexer_peek_token (parser->lexer);
16588
16589 /* If the next token is `operator', then we have either an
16590 operator-function-id or a conversion-function-id. */
16591 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16592 {
16593 /* We don't know whether we're looking at an
16594 operator-function-id or a conversion-function-id. */
16595 cp_parser_parse_tentatively (parser);
16596 /* Try an operator-function-id. */
16597 identifier = cp_parser_operator_function_id (parser);
16598 /* If that didn't work, try a conversion-function-id. */
16599 if (!cp_parser_parse_definitely (parser))
16600 {
16601 cp_parser_error (parser, "expected template-name");
16602 return error_mark_node;
16603 }
16604 }
16605 /* Look for the identifier. */
16606 else
16607 identifier = cp_parser_identifier (parser);
16608
16609 /* If we didn't find an identifier, we don't have a template-id. */
16610 if (identifier == error_mark_node)
16611 return error_mark_node;
16612
16613 /* If the name immediately followed the `template' keyword, then it
16614 is a template-name. However, if the next token is not `<', then
16615 we do not treat it as a template-name, since it is not being used
16616 as part of a template-id. This enables us to handle constructs
16617 like:
16618
16619 template <typename T> struct S { S(); };
16620 template <typename T> S<T>::S();
16621
16622 correctly. We would treat `S' as a template -- if it were `S<T>'
16623 -- but we do not if there is no `<'. */
16624
16625 if (processing_template_decl
16626 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16627 {
16628 /* In a declaration, in a dependent context, we pretend that the
16629 "template" keyword was present in order to improve error
16630 recovery. For example, given:
16631
16632 template <typename T> void f(T::X<int>);
16633
16634 we want to treat "X<int>" as a template-id. */
16635 if (is_declaration
16636 && !template_keyword_p
16637 && parser->scope && TYPE_P (parser->scope)
16638 && check_dependency_p
16639 && dependent_scope_p (parser->scope)
16640 /* Do not do this for dtors (or ctors), since they never
16641 need the template keyword before their name. */
16642 && !constructor_name_p (identifier, parser->scope))
16643 {
16644 cp_token_position start = 0;
16645
16646 /* Explain what went wrong. */
16647 error_at (token->location, "non-template %qD used as template",
16648 identifier);
16649 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16650 parser->scope, identifier);
16651 /* If parsing tentatively, find the location of the "<" token. */
16652 if (cp_parser_simulate_error (parser))
16653 start = cp_lexer_token_position (parser->lexer, true);
16654 /* Parse the template arguments so that we can issue error
16655 messages about them. */
16656 cp_lexer_consume_token (parser->lexer);
16657 cp_parser_enclosed_template_argument_list (parser);
16658 /* Skip tokens until we find a good place from which to
16659 continue parsing. */
16660 cp_parser_skip_to_closing_parenthesis (parser,
16661 /*recovering=*/true,
16662 /*or_comma=*/true,
16663 /*consume_paren=*/false);
16664 /* If parsing tentatively, permanently remove the
16665 template argument list. That will prevent duplicate
16666 error messages from being issued about the missing
16667 "template" keyword. */
16668 if (start)
16669 cp_lexer_purge_tokens_after (parser->lexer, start);
16670 if (is_identifier)
16671 *is_identifier = true;
16672 parser->context->object_type = NULL_TREE;
16673 return identifier;
16674 }
16675
16676 /* If the "template" keyword is present, then there is generally
16677 no point in doing name-lookup, so we just return IDENTIFIER.
16678 But, if the qualifying scope is non-dependent then we can
16679 (and must) do name-lookup normally. */
16680 if (template_keyword_p)
16681 {
16682 tree scope = (parser->scope ? parser->scope
16683 : parser->context->object_type);
16684 if (scope && TYPE_P (scope)
16685 && (!CLASS_TYPE_P (scope)
16686 || (check_dependency_p && dependent_type_p (scope))))
16687 {
16688 /* We're optimizing away the call to cp_parser_lookup_name, but
16689 we still need to do this. */
16690 parser->context->object_type = NULL_TREE;
16691 return identifier;
16692 }
16693 }
16694 }
16695
16696 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16697 const bool scoped_p = ((parser->scope ? parser->scope
16698 : parser->context->object_type) != NULL_TREE);
16699
16700 /* Look up the name. */
16701 decl = cp_parser_lookup_name (parser, identifier,
16702 tag_type,
16703 /*is_template=*/true,
16704 /*is_namespace=*/false,
16705 check_dependency_p,
16706 /*ambiguous_decls=*/NULL,
16707 token->location);
16708
16709 decl = strip_using_decl (decl);
16710
16711 /* If DECL is a template, then the name was a template-name. */
16712 if (TREE_CODE (decl) == TEMPLATE_DECL)
16713 {
16714 if (TREE_DEPRECATED (decl)
16715 && deprecated_state != DEPRECATED_SUPPRESS)
16716 warn_deprecated_use (decl, NULL_TREE);
16717 }
16718 else
16719 {
16720 /* The standard does not explicitly indicate whether a name that
16721 names a set of overloaded declarations, some of which are
16722 templates, is a template-name. However, such a name should
16723 be a template-name; otherwise, there is no way to form a
16724 template-id for the overloaded templates. */
16725 bool found = false;
16726
16727 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16728 !found && iter; ++iter)
16729 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16730 found = true;
16731
16732 if (!found
16733 && (cxx_dialect > cxx17)
16734 && !scoped_p
16735 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
16736 && tag_type == none_type)
16737 {
16738 /* [temp.names] says "A name is also considered to refer to a template
16739 if it is an unqualified-id followed by a < and name lookup finds
16740 either one or more functions or finds nothing." */
16741
16742 /* The "more functions" case. Just use the OVERLOAD as normally.
16743 We don't use is_overloaded_fn here to avoid considering
16744 BASELINKs. */
16745 if (TREE_CODE (decl) == OVERLOAD
16746 /* Name lookup found one function. */
16747 || TREE_CODE (decl) == FUNCTION_DECL)
16748 found = true;
16749 /* Name lookup found nothing. */
16750 else if (decl == error_mark_node)
16751 return identifier;
16752 }
16753
16754 if (!found)
16755 {
16756 /* The name does not name a template. */
16757 cp_parser_error (parser, "expected template-name");
16758 return error_mark_node;
16759 }
16760 }
16761
16762 return decl;
16763 }
16764
16765 /* Parse a template-argument-list.
16766
16767 template-argument-list:
16768 template-argument ... [opt]
16769 template-argument-list , template-argument ... [opt]
16770
16771 Returns a TREE_VEC containing the arguments. */
16772
16773 static tree
16774 cp_parser_template_argument_list (cp_parser* parser)
16775 {
16776 tree fixed_args[10];
16777 unsigned n_args = 0;
16778 unsigned alloced = 10;
16779 tree *arg_ary = fixed_args;
16780 tree vec;
16781 bool saved_in_template_argument_list_p;
16782 bool saved_ice_p;
16783 bool saved_non_ice_p;
16784
16785 /* Don't create location wrapper nodes within a template-argument-list. */
16786 auto_suppress_location_wrappers sentinel;
16787
16788 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16789 parser->in_template_argument_list_p = true;
16790 /* Even if the template-id appears in an integral
16791 constant-expression, the contents of the argument list do
16792 not. */
16793 saved_ice_p = parser->integral_constant_expression_p;
16794 parser->integral_constant_expression_p = false;
16795 saved_non_ice_p = parser->non_integral_constant_expression_p;
16796 parser->non_integral_constant_expression_p = false;
16797
16798 /* Parse the arguments. */
16799 do
16800 {
16801 tree argument;
16802
16803 if (n_args)
16804 /* Consume the comma. */
16805 cp_lexer_consume_token (parser->lexer);
16806
16807 /* Parse the template-argument. */
16808 argument = cp_parser_template_argument (parser);
16809
16810 /* If the next token is an ellipsis, we're expanding a template
16811 argument pack. */
16812 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16813 {
16814 if (argument == error_mark_node)
16815 {
16816 cp_token *token = cp_lexer_peek_token (parser->lexer);
16817 error_at (token->location,
16818 "expected parameter pack before %<...%>");
16819 }
16820 /* Consume the `...' token. */
16821 cp_lexer_consume_token (parser->lexer);
16822
16823 /* Make the argument into a TYPE_PACK_EXPANSION or
16824 EXPR_PACK_EXPANSION. */
16825 argument = make_pack_expansion (argument);
16826 }
16827
16828 if (n_args == alloced)
16829 {
16830 alloced *= 2;
16831
16832 if (arg_ary == fixed_args)
16833 {
16834 arg_ary = XNEWVEC (tree, alloced);
16835 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16836 }
16837 else
16838 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16839 }
16840 arg_ary[n_args++] = argument;
16841 }
16842 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16843
16844 vec = make_tree_vec (n_args);
16845
16846 while (n_args--)
16847 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16848
16849 if (arg_ary != fixed_args)
16850 free (arg_ary);
16851 parser->non_integral_constant_expression_p = saved_non_ice_p;
16852 parser->integral_constant_expression_p = saved_ice_p;
16853 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16854 if (CHECKING_P)
16855 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16856 return vec;
16857 }
16858
16859 /* Parse a template-argument.
16860
16861 template-argument:
16862 assignment-expression
16863 type-id
16864 id-expression
16865
16866 The representation is that of an assignment-expression, type-id, or
16867 id-expression -- except that the qualified id-expression is
16868 evaluated, so that the value returned is either a DECL or an
16869 OVERLOAD.
16870
16871 Although the standard says "assignment-expression", it forbids
16872 throw-expressions or assignments in the template argument.
16873 Therefore, we use "conditional-expression" instead. */
16874
16875 static tree
16876 cp_parser_template_argument (cp_parser* parser)
16877 {
16878 tree argument;
16879 bool template_p;
16880 bool address_p;
16881 bool maybe_type_id = false;
16882 cp_token *token = NULL, *argument_start_token = NULL;
16883 location_t loc = 0;
16884 cp_id_kind idk;
16885
16886 /* There's really no way to know what we're looking at, so we just
16887 try each alternative in order.
16888
16889 [temp.arg]
16890
16891 In a template-argument, an ambiguity between a type-id and an
16892 expression is resolved to a type-id, regardless of the form of
16893 the corresponding template-parameter.
16894
16895 Therefore, we try a type-id first. */
16896 cp_parser_parse_tentatively (parser);
16897 argument = cp_parser_template_type_arg (parser);
16898 /* If there was no error parsing the type-id but the next token is a
16899 '>>', our behavior depends on which dialect of C++ we're
16900 parsing. In C++98, we probably found a typo for '> >'. But there
16901 are type-id which are also valid expressions. For instance:
16902
16903 struct X { int operator >> (int); };
16904 template <int V> struct Foo {};
16905 Foo<X () >> 5> r;
16906
16907 Here 'X()' is a valid type-id of a function type, but the user just
16908 wanted to write the expression "X() >> 5". Thus, we remember that we
16909 found a valid type-id, but we still try to parse the argument as an
16910 expression to see what happens.
16911
16912 In C++0x, the '>>' will be considered two separate '>'
16913 tokens. */
16914 if (!cp_parser_error_occurred (parser)
16915 && cxx_dialect == cxx98
16916 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16917 {
16918 maybe_type_id = true;
16919 cp_parser_abort_tentative_parse (parser);
16920 }
16921 else
16922 {
16923 /* If the next token isn't a `,' or a `>', then this argument wasn't
16924 really finished. This means that the argument is not a valid
16925 type-id. */
16926 if (!cp_parser_next_token_ends_template_argument_p (parser))
16927 cp_parser_error (parser, "expected template-argument");
16928 /* If that worked, we're done. */
16929 if (cp_parser_parse_definitely (parser))
16930 return argument;
16931 }
16932 /* We're still not sure what the argument will be. */
16933 cp_parser_parse_tentatively (parser);
16934 /* Try a template. */
16935 argument_start_token = cp_lexer_peek_token (parser->lexer);
16936 argument = cp_parser_id_expression (parser,
16937 /*template_keyword_p=*/false,
16938 /*check_dependency_p=*/true,
16939 &template_p,
16940 /*declarator_p=*/false,
16941 /*optional_p=*/false);
16942 /* If the next token isn't a `,' or a `>', then this argument wasn't
16943 really finished. */
16944 if (!cp_parser_next_token_ends_template_argument_p (parser))
16945 cp_parser_error (parser, "expected template-argument");
16946 if (!cp_parser_error_occurred (parser))
16947 {
16948 /* Figure out what is being referred to. If the id-expression
16949 was for a class template specialization, then we will have a
16950 TYPE_DECL at this point. There is no need to do name lookup
16951 at this point in that case. */
16952 if (TREE_CODE (argument) != TYPE_DECL)
16953 argument = cp_parser_lookup_name (parser, argument,
16954 none_type,
16955 /*is_template=*/template_p,
16956 /*is_namespace=*/false,
16957 /*check_dependency=*/true,
16958 /*ambiguous_decls=*/NULL,
16959 argument_start_token->location);
16960 /* Handle a constrained-type-specifier for a non-type template
16961 parameter. */
16962 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16963 argument = decl;
16964 else if (TREE_CODE (argument) != TEMPLATE_DECL
16965 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16966 cp_parser_error (parser, "expected template-name");
16967 }
16968 if (cp_parser_parse_definitely (parser))
16969 {
16970 if (TREE_DEPRECATED (argument))
16971 warn_deprecated_use (argument, NULL_TREE);
16972 return argument;
16973 }
16974 /* It must be a non-type argument. In C++17 any constant-expression is
16975 allowed. */
16976 if (cxx_dialect > cxx14)
16977 goto general_expr;
16978
16979 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16980
16981 -- an integral constant-expression of integral or enumeration
16982 type; or
16983
16984 -- the name of a non-type template-parameter; or
16985
16986 -- the name of an object or function with external linkage...
16987
16988 -- the address of an object or function with external linkage...
16989
16990 -- a pointer to member... */
16991 /* Look for a non-type template parameter. */
16992 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16993 {
16994 cp_parser_parse_tentatively (parser);
16995 argument = cp_parser_primary_expression (parser,
16996 /*address_p=*/false,
16997 /*cast_p=*/false,
16998 /*template_arg_p=*/true,
16999 &idk);
17000 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
17001 || !cp_parser_next_token_ends_template_argument_p (parser))
17002 cp_parser_simulate_error (parser);
17003 if (cp_parser_parse_definitely (parser))
17004 return argument;
17005 }
17006
17007 /* If the next token is "&", the argument must be the address of an
17008 object or function with external linkage. */
17009 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
17010 if (address_p)
17011 {
17012 loc = cp_lexer_peek_token (parser->lexer)->location;
17013 cp_lexer_consume_token (parser->lexer);
17014 }
17015 /* See if we might have an id-expression. */
17016 token = cp_lexer_peek_token (parser->lexer);
17017 if (token->type == CPP_NAME
17018 || token->keyword == RID_OPERATOR
17019 || token->type == CPP_SCOPE
17020 || token->type == CPP_TEMPLATE_ID
17021 || token->type == CPP_NESTED_NAME_SPECIFIER)
17022 {
17023 cp_parser_parse_tentatively (parser);
17024 argument = cp_parser_primary_expression (parser,
17025 address_p,
17026 /*cast_p=*/false,
17027 /*template_arg_p=*/true,
17028 &idk);
17029 if (cp_parser_error_occurred (parser)
17030 || !cp_parser_next_token_ends_template_argument_p (parser))
17031 cp_parser_abort_tentative_parse (parser);
17032 else
17033 {
17034 tree probe;
17035
17036 if (INDIRECT_REF_P (argument))
17037 {
17038 /* Strip the dereference temporarily. */
17039 gcc_assert (REFERENCE_REF_P (argument));
17040 argument = TREE_OPERAND (argument, 0);
17041 }
17042
17043 /* If we're in a template, we represent a qualified-id referring
17044 to a static data member as a SCOPE_REF even if the scope isn't
17045 dependent so that we can check access control later. */
17046 probe = argument;
17047 if (TREE_CODE (probe) == SCOPE_REF)
17048 probe = TREE_OPERAND (probe, 1);
17049 if (VAR_P (probe))
17050 {
17051 /* A variable without external linkage might still be a
17052 valid constant-expression, so no error is issued here
17053 if the external-linkage check fails. */
17054 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
17055 cp_parser_simulate_error (parser);
17056 }
17057 else if (is_overloaded_fn (argument))
17058 /* All overloaded functions are allowed; if the external
17059 linkage test does not pass, an error will be issued
17060 later. */
17061 ;
17062 else if (address_p
17063 && (TREE_CODE (argument) == OFFSET_REF
17064 || TREE_CODE (argument) == SCOPE_REF))
17065 /* A pointer-to-member. */
17066 ;
17067 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
17068 ;
17069 else
17070 cp_parser_simulate_error (parser);
17071
17072 if (cp_parser_parse_definitely (parser))
17073 {
17074 if (address_p)
17075 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
17076 tf_warning_or_error);
17077 else
17078 argument = convert_from_reference (argument);
17079 return argument;
17080 }
17081 }
17082 }
17083 /* If the argument started with "&", there are no other valid
17084 alternatives at this point. */
17085 if (address_p)
17086 {
17087 cp_parser_error (parser, "invalid non-type template argument");
17088 return error_mark_node;
17089 }
17090
17091 general_expr:
17092 /* If the argument wasn't successfully parsed as a type-id followed
17093 by '>>', the argument can only be a constant expression now.
17094 Otherwise, we try parsing the constant-expression tentatively,
17095 because the argument could really be a type-id. */
17096 if (maybe_type_id)
17097 cp_parser_parse_tentatively (parser);
17098
17099 if (cxx_dialect <= cxx14)
17100 argument = cp_parser_constant_expression (parser);
17101 else
17102 {
17103 /* In C++20, we can encounter a braced-init-list. */
17104 if (cxx_dialect >= cxx2a
17105 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17106 {
17107 bool expr_non_constant_p;
17108 return cp_parser_braced_list (parser, &expr_non_constant_p);
17109 }
17110
17111 /* With C++17 generalized non-type template arguments we need to handle
17112 lvalue constant expressions, too. */
17113 argument = cp_parser_assignment_expression (parser);
17114 require_potential_constant_expression (argument);
17115 }
17116
17117 if (!maybe_type_id)
17118 return argument;
17119 if (!cp_parser_next_token_ends_template_argument_p (parser))
17120 cp_parser_error (parser, "expected template-argument");
17121 if (cp_parser_parse_definitely (parser))
17122 return argument;
17123 /* We did our best to parse the argument as a non type-id, but that
17124 was the only alternative that matched (albeit with a '>' after
17125 it). We can assume it's just a typo from the user, and a
17126 diagnostic will then be issued. */
17127 return cp_parser_template_type_arg (parser);
17128 }
17129
17130 /* Parse an explicit-instantiation.
17131
17132 explicit-instantiation:
17133 template declaration
17134
17135 Although the standard says `declaration', what it really means is:
17136
17137 explicit-instantiation:
17138 template decl-specifier-seq [opt] declarator [opt] ;
17139
17140 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17141 supposed to be allowed. A defect report has been filed about this
17142 issue.
17143
17144 GNU Extension:
17145
17146 explicit-instantiation:
17147 storage-class-specifier template
17148 decl-specifier-seq [opt] declarator [opt] ;
17149 function-specifier template
17150 decl-specifier-seq [opt] declarator [opt] ; */
17151
17152 static void
17153 cp_parser_explicit_instantiation (cp_parser* parser)
17154 {
17155 int declares_class_or_enum;
17156 cp_decl_specifier_seq decl_specifiers;
17157 tree extension_specifier = NULL_TREE;
17158
17159 timevar_push (TV_TEMPLATE_INST);
17160
17161 /* Look for an (optional) storage-class-specifier or
17162 function-specifier. */
17163 if (cp_parser_allow_gnu_extensions_p (parser))
17164 {
17165 extension_specifier
17166 = cp_parser_storage_class_specifier_opt (parser);
17167 if (!extension_specifier)
17168 extension_specifier
17169 = cp_parser_function_specifier_opt (parser,
17170 /*decl_specs=*/NULL);
17171 }
17172
17173 /* Look for the `template' keyword. */
17174 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17175 /* Let the front end know that we are processing an explicit
17176 instantiation. */
17177 begin_explicit_instantiation ();
17178 /* [temp.explicit] says that we are supposed to ignore access
17179 control while processing explicit instantiation directives. */
17180 push_deferring_access_checks (dk_no_check);
17181 /* Parse a decl-specifier-seq. */
17182 cp_parser_decl_specifier_seq (parser,
17183 CP_PARSER_FLAGS_OPTIONAL,
17184 &decl_specifiers,
17185 &declares_class_or_enum);
17186 /* If there was exactly one decl-specifier, and it declared a class,
17187 and there's no declarator, then we have an explicit type
17188 instantiation. */
17189 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17190 {
17191 tree type;
17192
17193 type = check_tag_decl (&decl_specifiers,
17194 /*explicit_type_instantiation_p=*/true);
17195 /* Turn access control back on for names used during
17196 template instantiation. */
17197 pop_deferring_access_checks ();
17198 if (type)
17199 do_type_instantiation (type, extension_specifier,
17200 /*complain=*/tf_error);
17201 }
17202 else
17203 {
17204 cp_declarator *declarator;
17205 tree decl;
17206
17207 /* Parse the declarator. */
17208 declarator
17209 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17210 CP_PARSER_FLAGS_NONE,
17211 /*ctor_dtor_or_conv_p=*/NULL,
17212 /*parenthesized_p=*/NULL,
17213 /*member_p=*/false,
17214 /*friend_p=*/false,
17215 /*static_p=*/false);
17216 if (declares_class_or_enum & 2)
17217 cp_parser_check_for_definition_in_return_type (declarator,
17218 decl_specifiers.type,
17219 decl_specifiers.locations[ds_type_spec]);
17220 if (declarator != cp_error_declarator)
17221 {
17222 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17223 permerror (decl_specifiers.locations[ds_inline],
17224 "explicit instantiation shall not use"
17225 " %<inline%> specifier");
17226 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17227 permerror (decl_specifiers.locations[ds_constexpr],
17228 "explicit instantiation shall not use"
17229 " %<constexpr%> specifier");
17230
17231 decl = grokdeclarator (declarator, &decl_specifiers,
17232 NORMAL, 0, &decl_specifiers.attributes);
17233 /* Turn access control back on for names used during
17234 template instantiation. */
17235 pop_deferring_access_checks ();
17236 /* Do the explicit instantiation. */
17237 do_decl_instantiation (decl, extension_specifier);
17238 }
17239 else
17240 {
17241 pop_deferring_access_checks ();
17242 /* Skip the body of the explicit instantiation. */
17243 cp_parser_skip_to_end_of_statement (parser);
17244 }
17245 }
17246 /* We're done with the instantiation. */
17247 end_explicit_instantiation ();
17248
17249 cp_parser_consume_semicolon_at_end_of_statement (parser);
17250
17251 timevar_pop (TV_TEMPLATE_INST);
17252 }
17253
17254 /* Parse an explicit-specialization.
17255
17256 explicit-specialization:
17257 template < > declaration
17258
17259 Although the standard says `declaration', what it really means is:
17260
17261 explicit-specialization:
17262 template <> decl-specifier [opt] init-declarator [opt] ;
17263 template <> function-definition
17264 template <> explicit-specialization
17265 template <> template-declaration */
17266
17267 static void
17268 cp_parser_explicit_specialization (cp_parser* parser)
17269 {
17270 bool need_lang_pop;
17271 cp_token *token = cp_lexer_peek_token (parser->lexer);
17272
17273 /* Look for the `template' keyword. */
17274 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17275 /* Look for the `<'. */
17276 cp_parser_require (parser, CPP_LESS, RT_LESS);
17277 /* Look for the `>'. */
17278 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17279 /* We have processed another parameter list. */
17280 ++parser->num_template_parameter_lists;
17281 /* [temp]
17282
17283 A template ... explicit specialization ... shall not have C
17284 linkage. */
17285 if (current_lang_name == lang_name_c)
17286 {
17287 error_at (token->location, "template specialization with C linkage");
17288 maybe_show_extern_c_location ();
17289 /* Give it C++ linkage to avoid confusing other parts of the
17290 front end. */
17291 push_lang_context (lang_name_cplusplus);
17292 need_lang_pop = true;
17293 }
17294 else
17295 need_lang_pop = false;
17296 /* Let the front end know that we are beginning a specialization. */
17297 if (!begin_specialization ())
17298 {
17299 end_specialization ();
17300 return;
17301 }
17302
17303 /* If the next keyword is `template', we need to figure out whether
17304 or not we're looking a template-declaration. */
17305 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17306 {
17307 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17308 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17309 cp_parser_template_declaration_after_export (parser,
17310 /*member_p=*/false);
17311 else
17312 cp_parser_explicit_specialization (parser);
17313 }
17314 else
17315 /* Parse the dependent declaration. */
17316 cp_parser_single_declaration (parser,
17317 /*checks=*/NULL,
17318 /*member_p=*/false,
17319 /*explicit_specialization_p=*/true,
17320 /*friend_p=*/NULL);
17321 /* We're done with the specialization. */
17322 end_specialization ();
17323 /* For the erroneous case of a template with C linkage, we pushed an
17324 implicit C++ linkage scope; exit that scope now. */
17325 if (need_lang_pop)
17326 pop_lang_context ();
17327 /* We're done with this parameter list. */
17328 --parser->num_template_parameter_lists;
17329 }
17330
17331 /* Parse a type-specifier.
17332
17333 type-specifier:
17334 simple-type-specifier
17335 class-specifier
17336 enum-specifier
17337 elaborated-type-specifier
17338 cv-qualifier
17339
17340 GNU Extension:
17341
17342 type-specifier:
17343 __complex__
17344
17345 Returns a representation of the type-specifier. For a
17346 class-specifier, enum-specifier, or elaborated-type-specifier, a
17347 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17348
17349 The parser flags FLAGS is used to control type-specifier parsing.
17350
17351 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17352 in a decl-specifier-seq.
17353
17354 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17355 class-specifier, enum-specifier, or elaborated-type-specifier, then
17356 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17357 if a type is declared; 2 if it is defined. Otherwise, it is set to
17358 zero.
17359
17360 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17361 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17362 is set to FALSE. */
17363
17364 static tree
17365 cp_parser_type_specifier (cp_parser* parser,
17366 cp_parser_flags flags,
17367 cp_decl_specifier_seq *decl_specs,
17368 bool is_declaration,
17369 int* declares_class_or_enum,
17370 bool* is_cv_qualifier)
17371 {
17372 tree type_spec = NULL_TREE;
17373 cp_token *token;
17374 enum rid keyword;
17375 cp_decl_spec ds = ds_last;
17376
17377 /* Assume this type-specifier does not declare a new type. */
17378 if (declares_class_or_enum)
17379 *declares_class_or_enum = 0;
17380 /* And that it does not specify a cv-qualifier. */
17381 if (is_cv_qualifier)
17382 *is_cv_qualifier = false;
17383 /* Peek at the next token. */
17384 token = cp_lexer_peek_token (parser->lexer);
17385
17386 /* If we're looking at a keyword, we can use that to guide the
17387 production we choose. */
17388 keyword = token->keyword;
17389 switch (keyword)
17390 {
17391 case RID_ENUM:
17392 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17393 goto elaborated_type_specifier;
17394
17395 /* Look for the enum-specifier. */
17396 type_spec = cp_parser_enum_specifier (parser);
17397 /* If that worked, we're done. */
17398 if (type_spec)
17399 {
17400 if (declares_class_or_enum)
17401 *declares_class_or_enum = 2;
17402 if (decl_specs)
17403 cp_parser_set_decl_spec_type (decl_specs,
17404 type_spec,
17405 token,
17406 /*type_definition_p=*/true);
17407 return type_spec;
17408 }
17409 else
17410 goto elaborated_type_specifier;
17411
17412 /* Any of these indicate either a class-specifier, or an
17413 elaborated-type-specifier. */
17414 case RID_CLASS:
17415 case RID_STRUCT:
17416 case RID_UNION:
17417 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17418 goto elaborated_type_specifier;
17419
17420 /* Parse tentatively so that we can back up if we don't find a
17421 class-specifier. */
17422 cp_parser_parse_tentatively (parser);
17423 /* Look for the class-specifier. */
17424 type_spec = cp_parser_class_specifier (parser);
17425 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17426 /* If that worked, we're done. */
17427 if (cp_parser_parse_definitely (parser))
17428 {
17429 if (declares_class_or_enum)
17430 *declares_class_or_enum = 2;
17431 if (decl_specs)
17432 cp_parser_set_decl_spec_type (decl_specs,
17433 type_spec,
17434 token,
17435 /*type_definition_p=*/true);
17436 return type_spec;
17437 }
17438
17439 /* Fall through. */
17440 elaborated_type_specifier:
17441 /* We're declaring (not defining) a class or enum. */
17442 if (declares_class_or_enum)
17443 *declares_class_or_enum = 1;
17444
17445 /* Fall through. */
17446 case RID_TYPENAME:
17447 /* Look for an elaborated-type-specifier. */
17448 type_spec
17449 = (cp_parser_elaborated_type_specifier
17450 (parser,
17451 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17452 is_declaration));
17453 if (decl_specs)
17454 cp_parser_set_decl_spec_type (decl_specs,
17455 type_spec,
17456 token,
17457 /*type_definition_p=*/false);
17458 return type_spec;
17459
17460 case RID_CONST:
17461 ds = ds_const;
17462 if (is_cv_qualifier)
17463 *is_cv_qualifier = true;
17464 break;
17465
17466 case RID_VOLATILE:
17467 ds = ds_volatile;
17468 if (is_cv_qualifier)
17469 *is_cv_qualifier = true;
17470 break;
17471
17472 case RID_RESTRICT:
17473 ds = ds_restrict;
17474 if (is_cv_qualifier)
17475 *is_cv_qualifier = true;
17476 break;
17477
17478 case RID_COMPLEX:
17479 /* The `__complex__' keyword is a GNU extension. */
17480 ds = ds_complex;
17481 break;
17482
17483 default:
17484 break;
17485 }
17486
17487 /* Handle simple keywords. */
17488 if (ds != ds_last)
17489 {
17490 if (decl_specs)
17491 {
17492 set_and_check_decl_spec_loc (decl_specs, ds, token);
17493 decl_specs->any_specifiers_p = true;
17494 }
17495 return cp_lexer_consume_token (parser->lexer)->u.value;
17496 }
17497
17498 /* If we do not already have a type-specifier, assume we are looking
17499 at a simple-type-specifier. */
17500 type_spec = cp_parser_simple_type_specifier (parser,
17501 decl_specs,
17502 flags);
17503
17504 /* If we didn't find a type-specifier, and a type-specifier was not
17505 optional in this context, issue an error message. */
17506 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17507 {
17508 cp_parser_error (parser, "expected type specifier");
17509 return error_mark_node;
17510 }
17511
17512 return type_spec;
17513 }
17514
17515 /* Parse a simple-type-specifier.
17516
17517 simple-type-specifier:
17518 :: [opt] nested-name-specifier [opt] type-name
17519 :: [opt] nested-name-specifier template template-id
17520 char
17521 wchar_t
17522 bool
17523 short
17524 int
17525 long
17526 signed
17527 unsigned
17528 float
17529 double
17530 void
17531
17532 C++11 Extension:
17533
17534 simple-type-specifier:
17535 auto
17536 decltype ( expression )
17537 char16_t
17538 char32_t
17539 __underlying_type ( type-id )
17540
17541 C++17 extension:
17542
17543 nested-name-specifier(opt) template-name
17544
17545 GNU Extension:
17546
17547 simple-type-specifier:
17548 __int128
17549 __typeof__ unary-expression
17550 __typeof__ ( type-id )
17551 __typeof__ ( type-id ) { initializer-list , [opt] }
17552
17553 Concepts Extension:
17554
17555 simple-type-specifier:
17556 constrained-type-specifier
17557
17558 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17559 appropriately updated. */
17560
17561 static tree
17562 cp_parser_simple_type_specifier (cp_parser* parser,
17563 cp_decl_specifier_seq *decl_specs,
17564 cp_parser_flags flags)
17565 {
17566 tree type = NULL_TREE;
17567 cp_token *token;
17568 int idx;
17569
17570 /* Peek at the next token. */
17571 token = cp_lexer_peek_token (parser->lexer);
17572
17573 /* If we're looking at a keyword, things are easy. */
17574 switch (token->keyword)
17575 {
17576 case RID_CHAR:
17577 if (decl_specs)
17578 decl_specs->explicit_char_p = true;
17579 type = char_type_node;
17580 break;
17581 case RID_CHAR8:
17582 type = char8_type_node;
17583 break;
17584 case RID_CHAR16:
17585 type = char16_type_node;
17586 break;
17587 case RID_CHAR32:
17588 type = char32_type_node;
17589 break;
17590 case RID_WCHAR:
17591 type = wchar_type_node;
17592 break;
17593 case RID_BOOL:
17594 type = boolean_type_node;
17595 break;
17596 case RID_SHORT:
17597 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17598 type = short_integer_type_node;
17599 break;
17600 case RID_INT:
17601 if (decl_specs)
17602 decl_specs->explicit_int_p = true;
17603 type = integer_type_node;
17604 break;
17605 case RID_INT_N_0:
17606 case RID_INT_N_1:
17607 case RID_INT_N_2:
17608 case RID_INT_N_3:
17609 idx = token->keyword - RID_INT_N_0;
17610 if (! int_n_enabled_p [idx])
17611 break;
17612 if (decl_specs)
17613 {
17614 decl_specs->explicit_intN_p = true;
17615 decl_specs->int_n_idx = idx;
17616 }
17617 type = int_n_trees [idx].signed_type;
17618 break;
17619 case RID_LONG:
17620 if (decl_specs)
17621 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17622 type = long_integer_type_node;
17623 break;
17624 case RID_SIGNED:
17625 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17626 type = integer_type_node;
17627 break;
17628 case RID_UNSIGNED:
17629 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17630 type = unsigned_type_node;
17631 break;
17632 case RID_FLOAT:
17633 type = float_type_node;
17634 break;
17635 case RID_DOUBLE:
17636 type = double_type_node;
17637 break;
17638 case RID_VOID:
17639 type = void_type_node;
17640 break;
17641
17642 case RID_AUTO:
17643 maybe_warn_cpp0x (CPP0X_AUTO);
17644 if (parser->auto_is_implicit_function_template_parm_p)
17645 {
17646 /* The 'auto' might be the placeholder return type for a function decl
17647 with trailing return type. */
17648 bool have_trailing_return_fn_decl = false;
17649
17650 cp_parser_parse_tentatively (parser);
17651 cp_lexer_consume_token (parser->lexer);
17652 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17653 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17654 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17655 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17656 {
17657 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17658 {
17659 cp_lexer_consume_token (parser->lexer);
17660 cp_parser_skip_to_closing_parenthesis (parser,
17661 /*recovering*/false,
17662 /*or_comma*/false,
17663 /*consume_paren*/true);
17664 continue;
17665 }
17666
17667 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17668 {
17669 have_trailing_return_fn_decl = true;
17670 break;
17671 }
17672
17673 cp_lexer_consume_token (parser->lexer);
17674 }
17675 cp_parser_abort_tentative_parse (parser);
17676
17677 if (have_trailing_return_fn_decl)
17678 {
17679 type = make_auto ();
17680 break;
17681 }
17682
17683 if (cxx_dialect >= cxx14)
17684 {
17685 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17686 type = TREE_TYPE (type);
17687 }
17688 else
17689 type = error_mark_node;
17690
17691 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17692 {
17693 if (cxx_dialect < cxx14)
17694 error_at (token->location,
17695 "use of %<auto%> in lambda parameter declaration "
17696 "only available with "
17697 "%<-std=c++14%> or %<-std=gnu++14%>");
17698 }
17699 else if (cxx_dialect < cxx14)
17700 error_at (token->location,
17701 "use of %<auto%> in parameter declaration "
17702 "only available with "
17703 "%<-std=c++14%> or %<-std=gnu++14%>");
17704 else if (!flag_concepts)
17705 pedwarn (token->location, 0,
17706 "use of %<auto%> in parameter declaration "
17707 "only available with %<-fconcepts%>");
17708 }
17709 else
17710 type = make_auto ();
17711 break;
17712
17713 case RID_DECLTYPE:
17714 /* Since DR 743, decltype can either be a simple-type-specifier by
17715 itself or begin a nested-name-specifier. Parsing it will replace
17716 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17717 handling below decide what to do. */
17718 cp_parser_decltype (parser);
17719 cp_lexer_set_token_position (parser->lexer, token);
17720 break;
17721
17722 case RID_TYPEOF:
17723 /* Consume the `typeof' token. */
17724 cp_lexer_consume_token (parser->lexer);
17725 /* Parse the operand to `typeof'. */
17726 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17727 /* If it is not already a TYPE, take its type. */
17728 if (!TYPE_P (type))
17729 type = finish_typeof (type);
17730
17731 if (decl_specs)
17732 cp_parser_set_decl_spec_type (decl_specs, type,
17733 token,
17734 /*type_definition_p=*/false);
17735
17736 return type;
17737
17738 case RID_UNDERLYING_TYPE:
17739 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17740 if (decl_specs)
17741 cp_parser_set_decl_spec_type (decl_specs, type,
17742 token,
17743 /*type_definition_p=*/false);
17744
17745 return type;
17746
17747 case RID_BASES:
17748 case RID_DIRECT_BASES:
17749 type = cp_parser_trait_expr (parser, token->keyword);
17750 if (decl_specs)
17751 cp_parser_set_decl_spec_type (decl_specs, type,
17752 token,
17753 /*type_definition_p=*/false);
17754 return type;
17755 default:
17756 break;
17757 }
17758
17759 /* If token is an already-parsed decltype not followed by ::,
17760 it's a simple-type-specifier. */
17761 if (token->type == CPP_DECLTYPE
17762 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17763 {
17764 type = saved_checks_value (token->u.tree_check_value);
17765 if (decl_specs)
17766 {
17767 cp_parser_set_decl_spec_type (decl_specs, type,
17768 token,
17769 /*type_definition_p=*/false);
17770 /* Remember that we are handling a decltype in order to
17771 implement the resolution of DR 1510 when the argument
17772 isn't instantiation dependent. */
17773 decl_specs->decltype_p = true;
17774 }
17775 cp_lexer_consume_token (parser->lexer);
17776 return type;
17777 }
17778
17779 /* If the type-specifier was for a built-in type, we're done. */
17780 if (type)
17781 {
17782 /* Record the type. */
17783 if (decl_specs
17784 && (token->keyword != RID_SIGNED
17785 && token->keyword != RID_UNSIGNED
17786 && token->keyword != RID_SHORT
17787 && token->keyword != RID_LONG))
17788 cp_parser_set_decl_spec_type (decl_specs,
17789 type,
17790 token,
17791 /*type_definition_p=*/false);
17792 if (decl_specs)
17793 decl_specs->any_specifiers_p = true;
17794
17795 /* Consume the token. */
17796 cp_lexer_consume_token (parser->lexer);
17797
17798 if (type == error_mark_node)
17799 return error_mark_node;
17800
17801 /* There is no valid C++ program where a non-template type is
17802 followed by a "<". That usually indicates that the user thought
17803 that the type was a template. */
17804 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17805 token->location);
17806
17807 return TYPE_NAME (type);
17808 }
17809
17810 /* The type-specifier must be a user-defined type. */
17811 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17812 {
17813 bool qualified_p;
17814 bool global_p;
17815 const bool typename_p = (cxx_dialect >= cxx2a
17816 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
17817
17818 /* Don't gobble tokens or issue error messages if this is an
17819 optional type-specifier. */
17820 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17821 cp_parser_parse_tentatively (parser);
17822
17823 token = cp_lexer_peek_token (parser->lexer);
17824
17825 /* Look for the optional `::' operator. */
17826 global_p
17827 = (cp_parser_global_scope_opt (parser,
17828 /*current_scope_valid_p=*/false)
17829 != NULL_TREE);
17830 /* Look for the nested-name specifier. */
17831 qualified_p
17832 = (cp_parser_nested_name_specifier_opt (parser,
17833 /*typename_keyword_p=*/false,
17834 /*check_dependency_p=*/true,
17835 /*type_p=*/false,
17836 /*is_declaration=*/false)
17837 != NULL_TREE);
17838 /* If we have seen a nested-name-specifier, and the next token
17839 is `template', then we are using the template-id production. */
17840 if (parser->scope
17841 && cp_parser_optional_template_keyword (parser))
17842 {
17843 /* Look for the template-id. */
17844 type = cp_parser_template_id (parser,
17845 /*template_keyword_p=*/true,
17846 /*check_dependency_p=*/true,
17847 none_type,
17848 /*is_declaration=*/false);
17849 /* If the template-id did not name a type, we are out of
17850 luck. */
17851 if (TREE_CODE (type) != TYPE_DECL)
17852 {
17853 /* ...unless we pretend we have seen 'typename'. */
17854 if (typename_p)
17855 type = cp_parser_make_typename_type (parser, type,
17856 token->location);
17857 else
17858 {
17859 cp_parser_error (parser, "expected template-id for type");
17860 type = NULL_TREE;
17861 }
17862 }
17863 }
17864 /* Otherwise, look for a type-name. */
17865 else
17866 type = cp_parser_type_name (parser, (qualified_p && typename_p));
17867
17868 /* Keep track of all name-lookups performed in class scopes. */
17869 if (type
17870 && !global_p
17871 && !qualified_p
17872 && TREE_CODE (type) == TYPE_DECL
17873 && identifier_p (DECL_NAME (type)))
17874 maybe_note_name_used_in_class (DECL_NAME (type), type);
17875 /* If it didn't work out, we don't have a TYPE. */
17876 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17877 && !cp_parser_parse_definitely (parser))
17878 type = NULL_TREE;
17879 if (!type && cxx_dialect >= cxx17)
17880 {
17881 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17882 cp_parser_parse_tentatively (parser);
17883
17884 cp_parser_global_scope_opt (parser,
17885 /*current_scope_valid_p=*/false);
17886 cp_parser_nested_name_specifier_opt (parser,
17887 /*typename_keyword_p=*/false,
17888 /*check_dependency_p=*/true,
17889 /*type_p=*/false,
17890 /*is_declaration=*/false);
17891 tree name = cp_parser_identifier (parser);
17892 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17893 && parser->scope != error_mark_node)
17894 {
17895 tree tmpl = cp_parser_lookup_name (parser, name,
17896 none_type,
17897 /*is_template=*/false,
17898 /*is_namespace=*/false,
17899 /*check_dependency=*/true,
17900 /*ambiguous_decls=*/NULL,
17901 token->location);
17902 if (tmpl && tmpl != error_mark_node
17903 && (DECL_CLASS_TEMPLATE_P (tmpl)
17904 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17905 type = make_template_placeholder (tmpl);
17906 else
17907 {
17908 type = error_mark_node;
17909 if (!cp_parser_simulate_error (parser))
17910 cp_parser_name_lookup_error (parser, name, tmpl,
17911 NLE_TYPE, token->location);
17912 }
17913 }
17914 else
17915 type = error_mark_node;
17916
17917 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17918 && !cp_parser_parse_definitely (parser))
17919 type = NULL_TREE;
17920 }
17921 if (type && decl_specs)
17922 cp_parser_set_decl_spec_type (decl_specs, type,
17923 token,
17924 /*type_definition_p=*/false);
17925 }
17926
17927 /* If we didn't get a type-name, issue an error message. */
17928 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17929 {
17930 cp_parser_error (parser, "expected type-name");
17931 return error_mark_node;
17932 }
17933
17934 if (type && type != error_mark_node)
17935 {
17936 /* See if TYPE is an Objective-C type, and if so, parse and
17937 accept any protocol references following it. Do this before
17938 the cp_parser_check_for_invalid_template_id() call, because
17939 Objective-C types can be followed by '<...>' which would
17940 enclose protocol names rather than template arguments, and so
17941 everything is fine. */
17942 if (c_dialect_objc () && !parser->scope
17943 && (objc_is_id (type) || objc_is_class_name (type)))
17944 {
17945 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17946 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17947
17948 /* Clobber the "unqualified" type previously entered into
17949 DECL_SPECS with the new, improved protocol-qualified version. */
17950 if (decl_specs)
17951 decl_specs->type = qual_type;
17952
17953 return qual_type;
17954 }
17955
17956 /* There is no valid C++ program where a non-template type is
17957 followed by a "<". That usually indicates that the user
17958 thought that the type was a template. */
17959 cp_parser_check_for_invalid_template_id (parser, type,
17960 none_type,
17961 token->location);
17962 }
17963
17964 return type;
17965 }
17966
17967 /* Parse a type-name.
17968
17969 type-name:
17970 class-name
17971 enum-name
17972 typedef-name
17973 simple-template-id [in c++0x]
17974
17975 enum-name:
17976 identifier
17977
17978 typedef-name:
17979 identifier
17980
17981 Concepts:
17982
17983 type-name:
17984 concept-name
17985 partial-concept-id
17986
17987 concept-name:
17988 identifier
17989
17990 Returns a TYPE_DECL for the type. */
17991
17992 static tree
17993 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17994 {
17995 tree type_decl;
17996
17997 /* We can't know yet whether it is a class-name or not. */
17998 cp_parser_parse_tentatively (parser);
17999 /* Try a class-name. */
18000 type_decl = cp_parser_class_name (parser,
18001 typename_keyword_p,
18002 /*template_keyword_p=*/false,
18003 none_type,
18004 /*check_dependency_p=*/true,
18005 /*class_head_p=*/false,
18006 /*is_declaration=*/false);
18007 /* If it's not a class-name, keep looking. */
18008 if (!cp_parser_parse_definitely (parser))
18009 {
18010 if (cxx_dialect < cxx11)
18011 /* It must be a typedef-name or an enum-name. */
18012 return cp_parser_nonclass_name (parser);
18013
18014 cp_parser_parse_tentatively (parser);
18015 /* It is either a simple-template-id representing an
18016 instantiation of an alias template... */
18017 type_decl = cp_parser_template_id (parser,
18018 /*template_keyword_p=*/false,
18019 /*check_dependency_p=*/true,
18020 none_type,
18021 /*is_declaration=*/false);
18022 /* Note that this must be an instantiation of an alias template
18023 because [temp.names]/6 says:
18024
18025 A template-id that names an alias template specialization
18026 is a type-name.
18027
18028 Whereas [temp.names]/7 says:
18029
18030 A simple-template-id that names a class template
18031 specialization is a class-name.
18032
18033 With concepts, this could also be a partial-concept-id that
18034 declares a non-type template parameter. */
18035 if (type_decl != NULL_TREE
18036 && TREE_CODE (type_decl) == TYPE_DECL
18037 && TYPE_DECL_ALIAS_P (type_decl))
18038 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
18039 else if (is_constrained_parameter (type_decl))
18040 /* Don't do anything. */ ;
18041 else
18042 cp_parser_simulate_error (parser);
18043
18044 if (!cp_parser_parse_definitely (parser))
18045 /* ... Or a typedef-name or an enum-name. */
18046 return cp_parser_nonclass_name (parser);
18047 }
18048
18049 return type_decl;
18050 }
18051
18052 /* Check if DECL and ARGS can form a constrained-type-specifier.
18053 If ARGS is non-null, we try to form a concept check of the
18054 form DECL<?, ARGS> where ? is a wildcard that matches any
18055 kind of template argument. If ARGS is NULL, then we try to
18056 form a concept check of the form DECL<?>. */
18057
18058 static tree
18059 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
18060 tree decl, tree args)
18061 {
18062 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
18063
18064 /* If we a constrained-type-specifier cannot be deduced. */
18065 if (parser->prevent_constrained_type_specifiers)
18066 return NULL_TREE;
18067
18068 /* A constrained type specifier can only be found in an
18069 overload set or as a reference to a template declaration.
18070
18071 FIXME: This might be masking a bug. It's possible that
18072 that the deduction below is causing template specializations
18073 to be formed with the wildcard as an argument. */
18074 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
18075 return NULL_TREE;
18076
18077 /* Try to build a call expression that evaluates the
18078 concept. This can fail if the overload set refers
18079 only to non-templates. */
18080 tree placeholder = build_nt (WILDCARD_DECL);
18081 tree check = build_concept_check (decl, placeholder, args);
18082 if (check == error_mark_node)
18083 return NULL_TREE;
18084
18085 /* Deduce the checked constraint and the prototype parameter.
18086
18087 FIXME: In certain cases, failure to deduce should be a
18088 diagnosable error. */
18089 tree conc;
18090 tree proto;
18091 if (!deduce_constrained_parameter (check, conc, proto))
18092 return NULL_TREE;
18093
18094 /* In template parameter scope, this results in a constrained
18095 parameter. Return a descriptor of that parm. */
18096 if (processing_template_parmlist)
18097 return build_constrained_parameter (conc, proto, args);
18098
18099 /* In a parameter-declaration-clause, constrained-type
18100 specifiers result in invented template parameters. */
18101 if (parser->auto_is_implicit_function_template_parm_p)
18102 {
18103 tree x = build_constrained_parameter (conc, proto, args);
18104 return synthesize_implicit_template_parm (parser, x);
18105 }
18106 else
18107 {
18108 /* Otherwise, we're in a context where the constrained
18109 type name is deduced and the constraint applies
18110 after deduction. */
18111 return make_constrained_auto (conc, args);
18112 }
18113
18114 return NULL_TREE;
18115 }
18116
18117 /* If DECL refers to a concept, return a TYPE_DECL representing
18118 the result of using the constrained type specifier in the
18119 current context. DECL refers to a concept if
18120
18121 - it is an overload set containing a function concept taking a single
18122 type argument, or
18123
18124 - it is a variable concept taking a single type argument. */
18125
18126 static tree
18127 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
18128 {
18129 if (flag_concepts
18130 && (TREE_CODE (decl) == OVERLOAD
18131 || BASELINK_P (decl)
18132 || variable_concept_p (decl)))
18133 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
18134 else
18135 return NULL_TREE;
18136 }
18137
18138 /* Check if DECL and ARGS form a partial-concept-id. If so,
18139 assign ID to the resulting constrained placeholder.
18140
18141 Returns true if the partial-concept-id designates a placeholder
18142 and false otherwise. Note that *id is set to NULL_TREE in
18143 this case. */
18144
18145 static tree
18146 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
18147 {
18148 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
18149 }
18150
18151 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18152 or a concept-name.
18153
18154 enum-name:
18155 identifier
18156
18157 typedef-name:
18158 identifier
18159
18160 concept-name:
18161 identifier
18162
18163 Returns a TYPE_DECL for the type. */
18164
18165 static tree
18166 cp_parser_nonclass_name (cp_parser* parser)
18167 {
18168 tree type_decl;
18169 tree identifier;
18170
18171 cp_token *token = cp_lexer_peek_token (parser->lexer);
18172 identifier = cp_parser_identifier (parser);
18173 if (identifier == error_mark_node)
18174 return error_mark_node;
18175
18176 /* Look up the type-name. */
18177 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18178
18179 type_decl = strip_using_decl (type_decl);
18180
18181 /* If we found an overload set, then it may refer to a concept-name. */
18182 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
18183 type_decl = decl;
18184
18185 if (TREE_CODE (type_decl) != TYPE_DECL
18186 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18187 {
18188 /* See if this is an Objective-C type. */
18189 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18190 tree type = objc_get_protocol_qualified_type (identifier, protos);
18191 if (type)
18192 type_decl = TYPE_NAME (type);
18193 }
18194
18195 /* Issue an error if we did not find a type-name. */
18196 if (TREE_CODE (type_decl) != TYPE_DECL
18197 /* In Objective-C, we have the complication that class names are
18198 normally type names and start declarations (eg, the
18199 "NSObject" in "NSObject *object;"), but can be used in an
18200 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18201 is an expression. So, a classname followed by a dot is not a
18202 valid type-name. */
18203 || (objc_is_class_name (TREE_TYPE (type_decl))
18204 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18205 {
18206 if (!cp_parser_simulate_error (parser))
18207 cp_parser_name_lookup_error (parser, identifier, type_decl,
18208 NLE_TYPE, token->location);
18209 return error_mark_node;
18210 }
18211 /* Remember that the name was used in the definition of the
18212 current class so that we can check later to see if the
18213 meaning would have been different after the class was
18214 entirely defined. */
18215 else if (type_decl != error_mark_node
18216 && !parser->scope)
18217 maybe_note_name_used_in_class (identifier, type_decl);
18218
18219 return type_decl;
18220 }
18221
18222 /* Parse an elaborated-type-specifier. Note that the grammar given
18223 here incorporates the resolution to DR68.
18224
18225 elaborated-type-specifier:
18226 class-key :: [opt] nested-name-specifier [opt] identifier
18227 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18228 enum-key :: [opt] nested-name-specifier [opt] identifier
18229 typename :: [opt] nested-name-specifier identifier
18230 typename :: [opt] nested-name-specifier template [opt]
18231 template-id
18232
18233 GNU extension:
18234
18235 elaborated-type-specifier:
18236 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18237 class-key attributes :: [opt] nested-name-specifier [opt]
18238 template [opt] template-id
18239 enum attributes :: [opt] nested-name-specifier [opt] identifier
18240
18241 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18242 declared `friend'. If IS_DECLARATION is TRUE, then this
18243 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18244 something is being declared.
18245
18246 Returns the TYPE specified. */
18247
18248 static tree
18249 cp_parser_elaborated_type_specifier (cp_parser* parser,
18250 bool is_friend,
18251 bool is_declaration)
18252 {
18253 enum tag_types tag_type;
18254 tree identifier;
18255 tree type = NULL_TREE;
18256 tree attributes = NULL_TREE;
18257 tree globalscope;
18258 cp_token *token = NULL;
18259
18260 /* See if we're looking at the `enum' keyword. */
18261 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18262 {
18263 /* Consume the `enum' token. */
18264 cp_lexer_consume_token (parser->lexer);
18265 /* Remember that it's an enumeration type. */
18266 tag_type = enum_type;
18267 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18268 enums) is used here. */
18269 cp_token *token = cp_lexer_peek_token (parser->lexer);
18270 if (cp_parser_is_keyword (token, RID_CLASS)
18271 || cp_parser_is_keyword (token, RID_STRUCT))
18272 {
18273 gcc_rich_location richloc (token->location);
18274 richloc.add_range (input_location);
18275 richloc.add_fixit_remove ();
18276 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18277 "a scoped enum must not use the %qD keyword",
18278 token->u.value);
18279 /* Consume the `struct' or `class' and parse it anyway. */
18280 cp_lexer_consume_token (parser->lexer);
18281 }
18282 /* Parse the attributes. */
18283 attributes = cp_parser_attributes_opt (parser);
18284 }
18285 /* Or, it might be `typename'. */
18286 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18287 RID_TYPENAME))
18288 {
18289 /* Consume the `typename' token. */
18290 cp_lexer_consume_token (parser->lexer);
18291 /* Remember that it's a `typename' type. */
18292 tag_type = typename_type;
18293 }
18294 /* Otherwise it must be a class-key. */
18295 else
18296 {
18297 tag_type = cp_parser_class_key (parser);
18298 if (tag_type == none_type)
18299 return error_mark_node;
18300 /* Parse the attributes. */
18301 attributes = cp_parser_attributes_opt (parser);
18302 }
18303
18304 /* Look for the `::' operator. */
18305 globalscope = cp_parser_global_scope_opt (parser,
18306 /*current_scope_valid_p=*/false);
18307 /* Look for the nested-name-specifier. */
18308 tree nested_name_specifier;
18309 if (tag_type == typename_type && !globalscope)
18310 {
18311 nested_name_specifier
18312 = cp_parser_nested_name_specifier (parser,
18313 /*typename_keyword_p=*/true,
18314 /*check_dependency_p=*/true,
18315 /*type_p=*/true,
18316 is_declaration);
18317 if (!nested_name_specifier)
18318 return error_mark_node;
18319 }
18320 else
18321 /* Even though `typename' is not present, the proposed resolution
18322 to Core Issue 180 says that in `class A<T>::B', `B' should be
18323 considered a type-name, even if `A<T>' is dependent. */
18324 nested_name_specifier
18325 = cp_parser_nested_name_specifier_opt (parser,
18326 /*typename_keyword_p=*/true,
18327 /*check_dependency_p=*/true,
18328 /*type_p=*/true,
18329 is_declaration);
18330 /* For everything but enumeration types, consider a template-id.
18331 For an enumeration type, consider only a plain identifier. */
18332 if (tag_type != enum_type)
18333 {
18334 bool template_p = false;
18335 tree decl;
18336
18337 /* Allow the `template' keyword. */
18338 template_p = cp_parser_optional_template_keyword (parser);
18339 /* If we didn't see `template', we don't know if there's a
18340 template-id or not. */
18341 if (!template_p)
18342 cp_parser_parse_tentatively (parser);
18343 /* The `template' keyword must follow a nested-name-specifier. */
18344 else if (!nested_name_specifier)
18345 {
18346 cp_parser_error (parser, "%<template%> must follow a nested-"
18347 "name-specifier");
18348 return error_mark_node;
18349 }
18350
18351 /* Parse the template-id. */
18352 token = cp_lexer_peek_token (parser->lexer);
18353 decl = cp_parser_template_id (parser, template_p,
18354 /*check_dependency_p=*/true,
18355 tag_type,
18356 is_declaration);
18357 /* If we didn't find a template-id, look for an ordinary
18358 identifier. */
18359 if (!template_p && !cp_parser_parse_definitely (parser))
18360 ;
18361 /* We can get here when cp_parser_template_id, called by
18362 cp_parser_class_name with tag_type == none_type, succeeds
18363 and caches a BASELINK. Then, when called again here,
18364 instead of failing and returning an error_mark_node
18365 returns it (see template/typename17.C in C++11).
18366 ??? Could we diagnose this earlier? */
18367 else if (tag_type == typename_type && BASELINK_P (decl))
18368 {
18369 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18370 type = error_mark_node;
18371 }
18372 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18373 in effect, then we must assume that, upon instantiation, the
18374 template will correspond to a class. */
18375 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18376 && tag_type == typename_type)
18377 type = make_typename_type (parser->scope, decl,
18378 typename_type,
18379 /*complain=*/tf_error);
18380 /* If the `typename' keyword is in effect and DECL is not a type
18381 decl, then type is non existent. */
18382 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18383 ;
18384 else if (TREE_CODE (decl) == TYPE_DECL)
18385 {
18386 type = check_elaborated_type_specifier (tag_type, decl,
18387 /*allow_template_p=*/true);
18388
18389 /* If the next token is a semicolon, this must be a specialization,
18390 instantiation, or friend declaration. Check the scope while we
18391 still know whether or not we had a nested-name-specifier. */
18392 if (type != error_mark_node
18393 && !nested_name_specifier && !is_friend
18394 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18395 check_unqualified_spec_or_inst (type, token->location);
18396 }
18397 else if (decl == error_mark_node)
18398 type = error_mark_node;
18399 }
18400
18401 if (!type)
18402 {
18403 token = cp_lexer_peek_token (parser->lexer);
18404 identifier = cp_parser_identifier (parser);
18405
18406 if (identifier == error_mark_node)
18407 {
18408 parser->scope = NULL_TREE;
18409 return error_mark_node;
18410 }
18411
18412 /* For a `typename', we needn't call xref_tag. */
18413 if (tag_type == typename_type
18414 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18415 return cp_parser_make_typename_type (parser, identifier,
18416 token->location);
18417
18418 /* Template parameter lists apply only if we are not within a
18419 function parameter list. */
18420 bool template_parm_lists_apply
18421 = parser->num_template_parameter_lists;
18422 if (template_parm_lists_apply)
18423 for (cp_binding_level *s = current_binding_level;
18424 s && s->kind != sk_template_parms;
18425 s = s->level_chain)
18426 if (s->kind == sk_function_parms)
18427 template_parm_lists_apply = false;
18428
18429 /* Look up a qualified name in the usual way. */
18430 if (parser->scope)
18431 {
18432 tree decl;
18433 tree ambiguous_decls;
18434
18435 decl = cp_parser_lookup_name (parser, identifier,
18436 tag_type,
18437 /*is_template=*/false,
18438 /*is_namespace=*/false,
18439 /*check_dependency=*/true,
18440 &ambiguous_decls,
18441 token->location);
18442
18443 /* If the lookup was ambiguous, an error will already have been
18444 issued. */
18445 if (ambiguous_decls)
18446 return error_mark_node;
18447
18448 /* If we are parsing friend declaration, DECL may be a
18449 TEMPLATE_DECL tree node here. However, we need to check
18450 whether this TEMPLATE_DECL results in valid code. Consider
18451 the following example:
18452
18453 namespace N {
18454 template <class T> class C {};
18455 }
18456 class X {
18457 template <class T> friend class N::C; // #1, valid code
18458 };
18459 template <class T> class Y {
18460 friend class N::C; // #2, invalid code
18461 };
18462
18463 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18464 name lookup of `N::C'. We see that friend declaration must
18465 be template for the code to be valid. Note that
18466 processing_template_decl does not work here since it is
18467 always 1 for the above two cases. */
18468
18469 decl = (cp_parser_maybe_treat_template_as_class
18470 (decl, /*tag_name_p=*/is_friend
18471 && template_parm_lists_apply));
18472
18473 if (TREE_CODE (decl) != TYPE_DECL)
18474 {
18475 cp_parser_diagnose_invalid_type_name (parser,
18476 identifier,
18477 token->location);
18478 return error_mark_node;
18479 }
18480
18481 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18482 {
18483 bool allow_template = (template_parm_lists_apply
18484 || DECL_SELF_REFERENCE_P (decl));
18485 type = check_elaborated_type_specifier (tag_type, decl,
18486 allow_template);
18487
18488 if (type == error_mark_node)
18489 return error_mark_node;
18490 }
18491
18492 /* Forward declarations of nested types, such as
18493
18494 class C1::C2;
18495 class C1::C2::C3;
18496
18497 are invalid unless all components preceding the final '::'
18498 are complete. If all enclosing types are complete, these
18499 declarations become merely pointless.
18500
18501 Invalid forward declarations of nested types are errors
18502 caught elsewhere in parsing. Those that are pointless arrive
18503 here. */
18504
18505 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18506 && !is_friend && !processing_explicit_instantiation)
18507 warning (0, "declaration %qD does not declare anything", decl);
18508
18509 type = TREE_TYPE (decl);
18510 }
18511 else
18512 {
18513 /* An elaborated-type-specifier sometimes introduces a new type and
18514 sometimes names an existing type. Normally, the rule is that it
18515 introduces a new type only if there is not an existing type of
18516 the same name already in scope. For example, given:
18517
18518 struct S {};
18519 void f() { struct S s; }
18520
18521 the `struct S' in the body of `f' is the same `struct S' as in
18522 the global scope; the existing definition is used. However, if
18523 there were no global declaration, this would introduce a new
18524 local class named `S'.
18525
18526 An exception to this rule applies to the following code:
18527
18528 namespace N { struct S; }
18529
18530 Here, the elaborated-type-specifier names a new type
18531 unconditionally; even if there is already an `S' in the
18532 containing scope this declaration names a new type.
18533 This exception only applies if the elaborated-type-specifier
18534 forms the complete declaration:
18535
18536 [class.name]
18537
18538 A declaration consisting solely of `class-key identifier ;' is
18539 either a redeclaration of the name in the current scope or a
18540 forward declaration of the identifier as a class name. It
18541 introduces the name into the current scope.
18542
18543 We are in this situation precisely when the next token is a `;'.
18544
18545 An exception to the exception is that a `friend' declaration does
18546 *not* name a new type; i.e., given:
18547
18548 struct S { friend struct T; };
18549
18550 `T' is not a new type in the scope of `S'.
18551
18552 Also, `new struct S' or `sizeof (struct S)' never results in the
18553 definition of a new type; a new type can only be declared in a
18554 declaration context. */
18555
18556 tag_scope ts;
18557 bool template_p;
18558
18559 if (is_friend)
18560 /* Friends have special name lookup rules. */
18561 ts = ts_within_enclosing_non_class;
18562 else if (is_declaration
18563 && cp_lexer_next_token_is (parser->lexer,
18564 CPP_SEMICOLON))
18565 /* This is a `class-key identifier ;' */
18566 ts = ts_current;
18567 else
18568 ts = ts_global;
18569
18570 template_p =
18571 (template_parm_lists_apply
18572 && (cp_parser_next_token_starts_class_definition_p (parser)
18573 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18574 /* An unqualified name was used to reference this type, so
18575 there were no qualifying templates. */
18576 if (template_parm_lists_apply
18577 && !cp_parser_check_template_parameters (parser,
18578 /*num_templates=*/0,
18579 /*template_id*/false,
18580 token->location,
18581 /*declarator=*/NULL))
18582 return error_mark_node;
18583 type = xref_tag (tag_type, identifier, ts, template_p);
18584 }
18585 }
18586
18587 if (type == error_mark_node)
18588 return error_mark_node;
18589
18590 /* Allow attributes on forward declarations of classes. */
18591 if (attributes)
18592 {
18593 if (TREE_CODE (type) == TYPENAME_TYPE)
18594 warning (OPT_Wattributes,
18595 "attributes ignored on uninstantiated type");
18596 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18597 && ! processing_explicit_instantiation)
18598 warning (OPT_Wattributes,
18599 "attributes ignored on template instantiation");
18600 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18601 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18602 else
18603 warning (OPT_Wattributes,
18604 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18605 }
18606
18607 if (tag_type != enum_type)
18608 {
18609 /* Indicate whether this class was declared as a `class' or as a
18610 `struct'. */
18611 if (CLASS_TYPE_P (type))
18612 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18613 cp_parser_check_class_key (tag_type, type);
18614 }
18615
18616 /* A "<" cannot follow an elaborated type specifier. If that
18617 happens, the user was probably trying to form a template-id. */
18618 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18619 token->location);
18620
18621 return type;
18622 }
18623
18624 /* Parse an enum-specifier.
18625
18626 enum-specifier:
18627 enum-head { enumerator-list [opt] }
18628 enum-head { enumerator-list , } [C++0x]
18629
18630 enum-head:
18631 enum-key identifier [opt] enum-base [opt]
18632 enum-key nested-name-specifier identifier enum-base [opt]
18633
18634 enum-key:
18635 enum
18636 enum class [C++0x]
18637 enum struct [C++0x]
18638
18639 enum-base: [C++0x]
18640 : type-specifier-seq
18641
18642 opaque-enum-specifier:
18643 enum-key identifier enum-base [opt] ;
18644
18645 GNU Extensions:
18646 enum-key attributes[opt] identifier [opt] enum-base [opt]
18647 { enumerator-list [opt] }attributes[opt]
18648 enum-key attributes[opt] identifier [opt] enum-base [opt]
18649 { enumerator-list, }attributes[opt] [C++0x]
18650
18651 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18652 if the token stream isn't an enum-specifier after all. */
18653
18654 static tree
18655 cp_parser_enum_specifier (cp_parser* parser)
18656 {
18657 tree identifier;
18658 tree type = NULL_TREE;
18659 tree prev_scope;
18660 tree nested_name_specifier = NULL_TREE;
18661 tree attributes;
18662 bool scoped_enum_p = false;
18663 bool has_underlying_type = false;
18664 bool nested_being_defined = false;
18665 bool new_value_list = false;
18666 bool is_new_type = false;
18667 bool is_unnamed = false;
18668 tree underlying_type = NULL_TREE;
18669 cp_token *type_start_token = NULL;
18670 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18671
18672 parser->colon_corrects_to_scope_p = false;
18673
18674 /* Parse tentatively so that we can back up if we don't find a
18675 enum-specifier. */
18676 cp_parser_parse_tentatively (parser);
18677
18678 /* Caller guarantees that the current token is 'enum', an identifier
18679 possibly follows, and the token after that is an opening brace.
18680 If we don't have an identifier, fabricate an anonymous name for
18681 the enumeration being defined. */
18682 cp_lexer_consume_token (parser->lexer);
18683
18684 /* Parse the "class" or "struct", which indicates a scoped
18685 enumeration type in C++0x. */
18686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18687 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18688 {
18689 if (cxx_dialect < cxx11)
18690 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18691
18692 /* Consume the `struct' or `class' token. */
18693 cp_lexer_consume_token (parser->lexer);
18694
18695 scoped_enum_p = true;
18696 }
18697
18698 attributes = cp_parser_attributes_opt (parser);
18699
18700 /* Clear the qualification. */
18701 parser->scope = NULL_TREE;
18702 parser->qualifying_scope = NULL_TREE;
18703 parser->object_scope = NULL_TREE;
18704
18705 /* Figure out in what scope the declaration is being placed. */
18706 prev_scope = current_scope ();
18707
18708 type_start_token = cp_lexer_peek_token (parser->lexer);
18709
18710 push_deferring_access_checks (dk_no_check);
18711 nested_name_specifier
18712 = cp_parser_nested_name_specifier_opt (parser,
18713 /*typename_keyword_p=*/true,
18714 /*check_dependency_p=*/false,
18715 /*type_p=*/false,
18716 /*is_declaration=*/false);
18717
18718 if (nested_name_specifier)
18719 {
18720 tree name;
18721
18722 identifier = cp_parser_identifier (parser);
18723 name = cp_parser_lookup_name (parser, identifier,
18724 enum_type,
18725 /*is_template=*/false,
18726 /*is_namespace=*/false,
18727 /*check_dependency=*/true,
18728 /*ambiguous_decls=*/NULL,
18729 input_location);
18730 if (name && name != error_mark_node)
18731 {
18732 type = TREE_TYPE (name);
18733 if (TREE_CODE (type) == TYPENAME_TYPE)
18734 {
18735 /* Are template enums allowed in ISO? */
18736 if (template_parm_scope_p ())
18737 pedwarn (type_start_token->location, OPT_Wpedantic,
18738 "%qD is an enumeration template", name);
18739 /* ignore a typename reference, for it will be solved by name
18740 in start_enum. */
18741 type = NULL_TREE;
18742 }
18743 }
18744 else if (nested_name_specifier == error_mark_node)
18745 /* We already issued an error. */;
18746 else
18747 {
18748 error_at (type_start_token->location,
18749 "%qD does not name an enumeration in %qT",
18750 identifier, nested_name_specifier);
18751 nested_name_specifier = error_mark_node;
18752 }
18753 }
18754 else
18755 {
18756 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18757 identifier = cp_parser_identifier (parser);
18758 else
18759 {
18760 identifier = make_anon_name ();
18761 is_unnamed = true;
18762 if (scoped_enum_p)
18763 error_at (type_start_token->location,
18764 "unnamed scoped enum is not allowed");
18765 }
18766 }
18767 pop_deferring_access_checks ();
18768
18769 /* Check for the `:' that denotes a specified underlying type in C++0x.
18770 Note that a ':' could also indicate a bitfield width, however. */
18771 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18772 {
18773 cp_decl_specifier_seq type_specifiers;
18774
18775 /* Consume the `:'. */
18776 cp_lexer_consume_token (parser->lexer);
18777
18778 /* Parse the type-specifier-seq. */
18779 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
18780 /*is_declaration=*/false,
18781 /*is_trailing_return=*/false,
18782 &type_specifiers);
18783
18784 /* At this point this is surely not elaborated type specifier. */
18785 if (!cp_parser_parse_definitely (parser))
18786 return NULL_TREE;
18787
18788 if (cxx_dialect < cxx11)
18789 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18790
18791 has_underlying_type = true;
18792
18793 /* If that didn't work, stop. */
18794 if (type_specifiers.type != error_mark_node)
18795 {
18796 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18797 /*initialized=*/0, NULL);
18798 if (underlying_type == error_mark_node
18799 || check_for_bare_parameter_packs (underlying_type))
18800 underlying_type = NULL_TREE;
18801 }
18802 }
18803
18804 /* Look for the `{' but don't consume it yet. */
18805 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18806 {
18807 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18808 {
18809 cp_parser_error (parser, "expected %<{%>");
18810 if (has_underlying_type)
18811 {
18812 type = NULL_TREE;
18813 goto out;
18814 }
18815 }
18816 /* An opaque-enum-specifier must have a ';' here. */
18817 if ((scoped_enum_p || underlying_type)
18818 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18819 {
18820 cp_parser_error (parser, "expected %<;%> or %<{%>");
18821 if (has_underlying_type)
18822 {
18823 type = NULL_TREE;
18824 goto out;
18825 }
18826 }
18827 }
18828
18829 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18830 return NULL_TREE;
18831
18832 if (nested_name_specifier)
18833 {
18834 if (CLASS_TYPE_P (nested_name_specifier))
18835 {
18836 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18837 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18838 push_scope (nested_name_specifier);
18839 }
18840 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18841 {
18842 push_nested_namespace (nested_name_specifier);
18843 }
18844 }
18845
18846 /* Issue an error message if type-definitions are forbidden here. */
18847 if (!cp_parser_check_type_definition (parser))
18848 type = error_mark_node;
18849 else
18850 /* Create the new type. We do this before consuming the opening
18851 brace so the enum will be recorded as being on the line of its
18852 tag (or the 'enum' keyword, if there is no tag). */
18853 type = start_enum (identifier, type, underlying_type,
18854 attributes, scoped_enum_p, &is_new_type);
18855
18856 /* If the next token is not '{' it is an opaque-enum-specifier or an
18857 elaborated-type-specifier. */
18858 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18859 {
18860 timevar_push (TV_PARSE_ENUM);
18861 if (nested_name_specifier
18862 && nested_name_specifier != error_mark_node)
18863 {
18864 /* The following catches invalid code such as:
18865 enum class S<int>::E { A, B, C }; */
18866 if (!processing_specialization
18867 && CLASS_TYPE_P (nested_name_specifier)
18868 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18869 error_at (type_start_token->location, "cannot add an enumerator "
18870 "list to a template instantiation");
18871
18872 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18873 {
18874 error_at (type_start_token->location,
18875 "%<%T::%E%> has not been declared",
18876 TYPE_CONTEXT (nested_name_specifier),
18877 nested_name_specifier);
18878 type = error_mark_node;
18879 }
18880 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18881 && !CLASS_TYPE_P (nested_name_specifier))
18882 {
18883 error_at (type_start_token->location, "nested name specifier "
18884 "%qT for enum declaration does not name a class "
18885 "or namespace", nested_name_specifier);
18886 type = error_mark_node;
18887 }
18888 /* If that scope does not contain the scope in which the
18889 class was originally declared, the program is invalid. */
18890 else if (prev_scope && !is_ancestor (prev_scope,
18891 nested_name_specifier))
18892 {
18893 if (at_namespace_scope_p ())
18894 error_at (type_start_token->location,
18895 "declaration of %qD in namespace %qD which does not "
18896 "enclose %qD",
18897 type, prev_scope, nested_name_specifier);
18898 else
18899 error_at (type_start_token->location,
18900 "declaration of %qD in %qD which does not "
18901 "enclose %qD",
18902 type, prev_scope, nested_name_specifier);
18903 type = error_mark_node;
18904 }
18905 /* If that scope is the scope where the declaration is being placed
18906 the program is invalid. */
18907 else if (CLASS_TYPE_P (nested_name_specifier)
18908 && CLASS_TYPE_P (prev_scope)
18909 && same_type_p (nested_name_specifier, prev_scope))
18910 {
18911 permerror (type_start_token->location,
18912 "extra qualification not allowed");
18913 nested_name_specifier = NULL_TREE;
18914 }
18915 }
18916
18917 if (scoped_enum_p)
18918 begin_scope (sk_scoped_enum, type);
18919
18920 /* Consume the opening brace. */
18921 matching_braces braces;
18922 braces.consume_open (parser);
18923
18924 if (type == error_mark_node)
18925 ; /* Nothing to add */
18926 else if (OPAQUE_ENUM_P (type)
18927 || (cxx_dialect > cxx98 && processing_specialization))
18928 {
18929 new_value_list = true;
18930 SET_OPAQUE_ENUM_P (type, false);
18931 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18932 }
18933 else
18934 {
18935 error_at (type_start_token->location,
18936 "multiple definition of %q#T", type);
18937 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18938 "previous definition here");
18939 type = error_mark_node;
18940 }
18941
18942 if (type == error_mark_node)
18943 cp_parser_skip_to_end_of_block_or_statement (parser);
18944 /* If the next token is not '}', then there are some enumerators. */
18945 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18946 {
18947 if (is_unnamed && !scoped_enum_p)
18948 pedwarn (type_start_token->location, OPT_Wpedantic,
18949 "ISO C++ forbids empty unnamed enum");
18950 }
18951 else
18952 cp_parser_enumerator_list (parser, type);
18953
18954 /* Consume the final '}'. */
18955 braces.require_close (parser);
18956
18957 if (scoped_enum_p)
18958 finish_scope ();
18959 timevar_pop (TV_PARSE_ENUM);
18960 }
18961 else
18962 {
18963 /* If a ';' follows, then it is an opaque-enum-specifier
18964 and additional restrictions apply. */
18965 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18966 {
18967 if (is_unnamed)
18968 error_at (type_start_token->location,
18969 "opaque-enum-specifier without name");
18970 else if (nested_name_specifier)
18971 error_at (type_start_token->location,
18972 "opaque-enum-specifier must use a simple identifier");
18973 }
18974 }
18975
18976 /* Look for trailing attributes to apply to this enumeration, and
18977 apply them if appropriate. */
18978 if (cp_parser_allow_gnu_extensions_p (parser))
18979 {
18980 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18981 cplus_decl_attributes (&type,
18982 trailing_attr,
18983 (int) ATTR_FLAG_TYPE_IN_PLACE);
18984 }
18985
18986 /* Finish up the enumeration. */
18987 if (type != error_mark_node)
18988 {
18989 if (new_value_list)
18990 finish_enum_value_list (type);
18991 if (is_new_type)
18992 finish_enum (type);
18993 }
18994
18995 if (nested_name_specifier)
18996 {
18997 if (CLASS_TYPE_P (nested_name_specifier))
18998 {
18999 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
19000 pop_scope (nested_name_specifier);
19001 }
19002 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19003 {
19004 pop_nested_namespace (nested_name_specifier);
19005 }
19006 }
19007 out:
19008 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19009 return type;
19010 }
19011
19012 /* Parse an enumerator-list. The enumerators all have the indicated
19013 TYPE.
19014
19015 enumerator-list:
19016 enumerator-definition
19017 enumerator-list , enumerator-definition */
19018
19019 static void
19020 cp_parser_enumerator_list (cp_parser* parser, tree type)
19021 {
19022 while (true)
19023 {
19024 /* Parse an enumerator-definition. */
19025 cp_parser_enumerator_definition (parser, type);
19026
19027 /* If the next token is not a ',', we've reached the end of
19028 the list. */
19029 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19030 break;
19031 /* Otherwise, consume the `,' and keep going. */
19032 cp_lexer_consume_token (parser->lexer);
19033 /* If the next token is a `}', there is a trailing comma. */
19034 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19035 {
19036 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
19037 pedwarn (input_location, OPT_Wpedantic,
19038 "comma at end of enumerator list");
19039 break;
19040 }
19041 }
19042 }
19043
19044 /* Parse an enumerator-definition. The enumerator has the indicated
19045 TYPE.
19046
19047 enumerator-definition:
19048 enumerator
19049 enumerator = constant-expression
19050
19051 enumerator:
19052 identifier
19053
19054 GNU Extensions:
19055
19056 enumerator-definition:
19057 enumerator attributes [opt]
19058 enumerator attributes [opt] = constant-expression */
19059
19060 static void
19061 cp_parser_enumerator_definition (cp_parser* parser, tree type)
19062 {
19063 tree identifier;
19064 tree value;
19065 location_t loc;
19066
19067 /* Save the input location because we are interested in the location
19068 of the identifier and not the location of the explicit value. */
19069 loc = cp_lexer_peek_token (parser->lexer)->location;
19070
19071 /* Look for the identifier. */
19072 identifier = cp_parser_identifier (parser);
19073 if (identifier == error_mark_node)
19074 return;
19075
19076 /* Parse any specified attributes. */
19077 tree attrs = cp_parser_attributes_opt (parser);
19078
19079 /* If the next token is an '=', then there is an explicit value. */
19080 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19081 {
19082 /* Consume the `=' token. */
19083 cp_lexer_consume_token (parser->lexer);
19084 /* Parse the value. */
19085 value = cp_parser_constant_expression (parser);
19086 }
19087 else
19088 value = NULL_TREE;
19089
19090 /* If we are processing a template, make sure the initializer of the
19091 enumerator doesn't contain any bare template parameter pack. */
19092 if (check_for_bare_parameter_packs (value))
19093 value = error_mark_node;
19094
19095 /* Create the enumerator. */
19096 build_enumerator (identifier, value, type, attrs, loc);
19097 }
19098
19099 /* Parse a namespace-name.
19100
19101 namespace-name:
19102 original-namespace-name
19103 namespace-alias
19104
19105 Returns the NAMESPACE_DECL for the namespace. */
19106
19107 static tree
19108 cp_parser_namespace_name (cp_parser* parser)
19109 {
19110 tree identifier;
19111 tree namespace_decl;
19112
19113 cp_token *token = cp_lexer_peek_token (parser->lexer);
19114
19115 /* Get the name of the namespace. */
19116 identifier = cp_parser_identifier (parser);
19117 if (identifier == error_mark_node)
19118 return error_mark_node;
19119
19120 /* Look up the identifier in the currently active scope. Look only
19121 for namespaces, due to:
19122
19123 [basic.lookup.udir]
19124
19125 When looking up a namespace-name in a using-directive or alias
19126 definition, only namespace names are considered.
19127
19128 And:
19129
19130 [basic.lookup.qual]
19131
19132 During the lookup of a name preceding the :: scope resolution
19133 operator, object, function, and enumerator names are ignored.
19134
19135 (Note that cp_parser_qualifying_entity only calls this
19136 function if the token after the name is the scope resolution
19137 operator.) */
19138 namespace_decl = cp_parser_lookup_name (parser, identifier,
19139 none_type,
19140 /*is_template=*/false,
19141 /*is_namespace=*/true,
19142 /*check_dependency=*/true,
19143 /*ambiguous_decls=*/NULL,
19144 token->location);
19145 /* If it's not a namespace, issue an error. */
19146 if (namespace_decl == error_mark_node
19147 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19148 {
19149 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19150 {
19151 auto_diagnostic_group d;
19152 name_hint hint;
19153 if (namespace_decl == error_mark_node
19154 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19155 hint = suggest_alternative_in_explicit_scope (token->location,
19156 identifier,
19157 parser->scope);
19158 if (const char *suggestion = hint.suggestion ())
19159 {
19160 gcc_rich_location richloc (token->location);
19161 richloc.add_fixit_replace (suggestion);
19162 error_at (&richloc,
19163 "%qD is not a namespace-name; did you mean %qs?",
19164 identifier, suggestion);
19165 }
19166 else
19167 error_at (token->location, "%qD is not a namespace-name",
19168 identifier);
19169 }
19170 else
19171 cp_parser_error (parser, "expected namespace-name");
19172 namespace_decl = error_mark_node;
19173 }
19174
19175 return namespace_decl;
19176 }
19177
19178 /* Parse a namespace-definition.
19179
19180 namespace-definition:
19181 named-namespace-definition
19182 unnamed-namespace-definition
19183
19184 named-namespace-definition:
19185 original-namespace-definition
19186 extension-namespace-definition
19187
19188 original-namespace-definition:
19189 namespace identifier { namespace-body }
19190
19191 extension-namespace-definition:
19192 namespace original-namespace-name { namespace-body }
19193
19194 unnamed-namespace-definition:
19195 namespace { namespace-body } */
19196
19197 static void
19198 cp_parser_namespace_definition (cp_parser* parser)
19199 {
19200 tree identifier;
19201 int nested_definition_count = 0;
19202
19203 cp_ensure_no_omp_declare_simd (parser);
19204 cp_ensure_no_oacc_routine (parser);
19205
19206 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19207 const bool topmost_inline_p = is_inline;
19208
19209 if (is_inline)
19210 {
19211 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19212 cp_lexer_consume_token (parser->lexer);
19213 }
19214
19215 /* Look for the `namespace' keyword. */
19216 cp_token* token
19217 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19218
19219 /* Parse any specified attributes before the identifier. */
19220 tree attribs = cp_parser_attributes_opt (parser);
19221
19222 for (;;)
19223 {
19224 identifier = NULL_TREE;
19225
19226 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19227 RID_INLINE);
19228 if (nested_inline_p && nested_definition_count != 0)
19229 {
19230 if (cxx_dialect < cxx2a)
19231 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19232 OPT_Wpedantic, "nested inline namespace definitions only "
19233 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
19234 cp_lexer_consume_token (parser->lexer);
19235 }
19236
19237 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19238 {
19239 identifier = cp_parser_identifier (parser);
19240
19241 if (cp_next_tokens_can_be_std_attribute_p (parser))
19242 pedwarn (input_location, OPT_Wpedantic,
19243 "standard attributes on namespaces must precede "
19244 "the namespace name");
19245
19246 /* Parse any attributes specified after the identifier. */
19247 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19248 }
19249
19250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19251 {
19252 /* Don't forget that the innermost namespace might have been
19253 marked as inline. Use |= because we cannot overwrite
19254 IS_INLINE in case the outermost namespace is inline, but
19255 there are no nested inlines. */
19256 is_inline |= nested_inline_p;
19257 break;
19258 }
19259
19260 if (!nested_definition_count && cxx_dialect < cxx17)
19261 pedwarn (input_location, OPT_Wpedantic,
19262 "nested namespace definitions only available with "
19263 "%<-std=c++17%> or %<-std=gnu++17%>");
19264
19265 /* Nested namespace names can create new namespaces (unlike
19266 other qualified-ids). */
19267 if (int count = (identifier
19268 ? push_namespace (identifier, nested_inline_p)
19269 : 0))
19270 nested_definition_count += count;
19271 else
19272 cp_parser_error (parser, "nested namespace name required");
19273 cp_lexer_consume_token (parser->lexer);
19274 }
19275
19276 if (nested_definition_count && !identifier)
19277 cp_parser_error (parser, "namespace name required");
19278
19279 if (nested_definition_count && attribs)
19280 error_at (token->location,
19281 "a nested namespace definition cannot have attributes");
19282 if (nested_definition_count && topmost_inline_p)
19283 error_at (token->location,
19284 "a nested namespace definition cannot be inline");
19285
19286 /* Start the namespace. */
19287 nested_definition_count += push_namespace (identifier, is_inline);
19288
19289 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19290
19291 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19292
19293 /* Look for the `{' to validate starting the namespace. */
19294 matching_braces braces;
19295 if (braces.require_open (parser))
19296 {
19297 /* Parse the body of the namespace. */
19298 cp_parser_namespace_body (parser);
19299
19300 /* Look for the final `}'. */
19301 braces.require_close (parser);
19302 }
19303
19304 if (has_visibility)
19305 pop_visibility (1);
19306
19307 /* Pop the nested namespace definitions. */
19308 while (nested_definition_count--)
19309 pop_namespace ();
19310 }
19311
19312 /* Parse a namespace-body.
19313
19314 namespace-body:
19315 declaration-seq [opt] */
19316
19317 static void
19318 cp_parser_namespace_body (cp_parser* parser)
19319 {
19320 cp_parser_declaration_seq_opt (parser);
19321 }
19322
19323 /* Parse a namespace-alias-definition.
19324
19325 namespace-alias-definition:
19326 namespace identifier = qualified-namespace-specifier ; */
19327
19328 static void
19329 cp_parser_namespace_alias_definition (cp_parser* parser)
19330 {
19331 tree identifier;
19332 tree namespace_specifier;
19333
19334 cp_token *token = cp_lexer_peek_token (parser->lexer);
19335
19336 /* Look for the `namespace' keyword. */
19337 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19338 /* Look for the identifier. */
19339 identifier = cp_parser_identifier (parser);
19340 if (identifier == error_mark_node)
19341 return;
19342 /* Look for the `=' token. */
19343 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19344 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19345 {
19346 error_at (token->location, "%<namespace%> definition is not allowed here");
19347 /* Skip the definition. */
19348 cp_lexer_consume_token (parser->lexer);
19349 if (cp_parser_skip_to_closing_brace (parser))
19350 cp_lexer_consume_token (parser->lexer);
19351 return;
19352 }
19353 cp_parser_require (parser, CPP_EQ, RT_EQ);
19354 /* Look for the qualified-namespace-specifier. */
19355 namespace_specifier
19356 = cp_parser_qualified_namespace_specifier (parser);
19357 /* Look for the `;' token. */
19358 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19359
19360 /* Register the alias in the symbol table. */
19361 do_namespace_alias (identifier, namespace_specifier);
19362 }
19363
19364 /* Parse a qualified-namespace-specifier.
19365
19366 qualified-namespace-specifier:
19367 :: [opt] nested-name-specifier [opt] namespace-name
19368
19369 Returns a NAMESPACE_DECL corresponding to the specified
19370 namespace. */
19371
19372 static tree
19373 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19374 {
19375 /* Look for the optional `::'. */
19376 cp_parser_global_scope_opt (parser,
19377 /*current_scope_valid_p=*/false);
19378
19379 /* Look for the optional nested-name-specifier. */
19380 cp_parser_nested_name_specifier_opt (parser,
19381 /*typename_keyword_p=*/false,
19382 /*check_dependency_p=*/true,
19383 /*type_p=*/false,
19384 /*is_declaration=*/true);
19385
19386 return cp_parser_namespace_name (parser);
19387 }
19388
19389 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19390 access declaration.
19391
19392 using-declaration:
19393 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19394 using :: unqualified-id ;
19395
19396 access-declaration:
19397 qualified-id ;
19398
19399 */
19400
19401 static bool
19402 cp_parser_using_declaration (cp_parser* parser,
19403 bool access_declaration_p)
19404 {
19405 cp_token *token;
19406 bool typename_p = false;
19407 bool global_scope_p;
19408 tree decl;
19409 tree identifier;
19410 tree qscope;
19411 int oldcount = errorcount;
19412 cp_token *diag_token = NULL;
19413
19414 if (access_declaration_p)
19415 {
19416 diag_token = cp_lexer_peek_token (parser->lexer);
19417 cp_parser_parse_tentatively (parser);
19418 }
19419 else
19420 {
19421 /* Look for the `using' keyword. */
19422 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19423
19424 again:
19425 /* Peek at the next token. */
19426 token = cp_lexer_peek_token (parser->lexer);
19427 /* See if it's `typename'. */
19428 if (token->keyword == RID_TYPENAME)
19429 {
19430 /* Remember that we've seen it. */
19431 typename_p = true;
19432 /* Consume the `typename' token. */
19433 cp_lexer_consume_token (parser->lexer);
19434 }
19435 }
19436
19437 /* Look for the optional global scope qualification. */
19438 global_scope_p
19439 = (cp_parser_global_scope_opt (parser,
19440 /*current_scope_valid_p=*/false)
19441 != NULL_TREE);
19442
19443 /* If we saw `typename', or didn't see `::', then there must be a
19444 nested-name-specifier present. */
19445 if (typename_p || !global_scope_p)
19446 {
19447 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19448 /*check_dependency_p=*/true,
19449 /*type_p=*/false,
19450 /*is_declaration=*/true);
19451 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19452 {
19453 cp_parser_skip_to_end_of_block_or_statement (parser);
19454 return false;
19455 }
19456 }
19457 /* Otherwise, we could be in either of the two productions. In that
19458 case, treat the nested-name-specifier as optional. */
19459 else
19460 qscope = cp_parser_nested_name_specifier_opt (parser,
19461 /*typename_keyword_p=*/false,
19462 /*check_dependency_p=*/true,
19463 /*type_p=*/false,
19464 /*is_declaration=*/true);
19465 if (!qscope)
19466 qscope = global_namespace;
19467 else if (UNSCOPED_ENUM_P (qscope)
19468 && !TYPE_FUNCTION_SCOPE_P (qscope))
19469 qscope = CP_TYPE_CONTEXT (qscope);
19470
19471 if (access_declaration_p && cp_parser_error_occurred (parser))
19472 /* Something has already gone wrong; there's no need to parse
19473 further. Since an error has occurred, the return value of
19474 cp_parser_parse_definitely will be false, as required. */
19475 return cp_parser_parse_definitely (parser);
19476
19477 token = cp_lexer_peek_token (parser->lexer);
19478 /* Parse the unqualified-id. */
19479 identifier = cp_parser_unqualified_id (parser,
19480 /*template_keyword_p=*/false,
19481 /*check_dependency_p=*/true,
19482 /*declarator_p=*/true,
19483 /*optional_p=*/false);
19484
19485 if (access_declaration_p)
19486 {
19487 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19488 cp_parser_simulate_error (parser);
19489 if (!cp_parser_parse_definitely (parser))
19490 return false;
19491 }
19492 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19493 {
19494 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19495 if (cxx_dialect < cxx17
19496 && !in_system_header_at (ell->location))
19497 pedwarn (ell->location, 0,
19498 "pack expansion in using-declaration only available "
19499 "with %<-std=c++17%> or %<-std=gnu++17%>");
19500 qscope = make_pack_expansion (qscope);
19501 }
19502
19503 /* The function we call to handle a using-declaration is different
19504 depending on what scope we are in. */
19505 if (qscope == error_mark_node || identifier == error_mark_node)
19506 ;
19507 else if (!identifier_p (identifier)
19508 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19509 /* [namespace.udecl]
19510
19511 A using declaration shall not name a template-id. */
19512 error_at (token->location,
19513 "a template-id may not appear in a using-declaration");
19514 else
19515 {
19516 if (at_class_scope_p ())
19517 {
19518 /* Create the USING_DECL. */
19519 decl = do_class_using_decl (qscope, identifier);
19520
19521 if (decl && typename_p)
19522 USING_DECL_TYPENAME_P (decl) = 1;
19523
19524 if (check_for_bare_parameter_packs (decl))
19525 {
19526 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19527 return false;
19528 }
19529 else
19530 /* Add it to the list of members in this class. */
19531 finish_member_declaration (decl);
19532 }
19533 else
19534 finish_nonmember_using_decl (qscope, identifier);
19535 }
19536
19537 if (!access_declaration_p
19538 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19539 {
19540 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19541 if (cxx_dialect < cxx17)
19542 pedwarn (comma->location, 0,
19543 "comma-separated list in using-declaration only available "
19544 "with %<-std=c++17%> or %<-std=gnu++17%>");
19545 goto again;
19546 }
19547
19548 /* Look for the final `;'. */
19549 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19550
19551 if (access_declaration_p && errorcount == oldcount)
19552 warning_at (diag_token->location, OPT_Wdeprecated,
19553 "access declarations are deprecated "
19554 "in favour of using-declarations; "
19555 "suggestion: add the %<using%> keyword");
19556
19557 return true;
19558 }
19559
19560 /* Parse an alias-declaration.
19561
19562 alias-declaration:
19563 using identifier attribute-specifier-seq [opt] = type-id */
19564
19565 static tree
19566 cp_parser_alias_declaration (cp_parser* parser)
19567 {
19568 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19569 location_t id_location, type_location;
19570 cp_declarator *declarator;
19571 cp_decl_specifier_seq decl_specs;
19572 bool member_p;
19573 const char *saved_message = NULL;
19574
19575 /* Look for the `using' keyword. */
19576 cp_token *using_token
19577 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19578 if (using_token == NULL)
19579 return error_mark_node;
19580
19581 id_location = cp_lexer_peek_token (parser->lexer)->location;
19582 id = cp_parser_identifier (parser);
19583 if (id == error_mark_node)
19584 return error_mark_node;
19585
19586 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19587 attributes = cp_parser_attributes_opt (parser);
19588 if (attributes == error_mark_node)
19589 return error_mark_node;
19590
19591 cp_parser_require (parser, CPP_EQ, RT_EQ);
19592
19593 if (cp_parser_error_occurred (parser))
19594 return error_mark_node;
19595
19596 cp_parser_commit_to_tentative_parse (parser);
19597
19598 /* Now we are going to parse the type-id of the declaration. */
19599
19600 /*
19601 [dcl.type]/3 says:
19602
19603 "A type-specifier-seq shall not define a class or enumeration
19604 unless it appears in the type-id of an alias-declaration (7.1.3) that
19605 is not the declaration of a template-declaration."
19606
19607 In other words, if we currently are in an alias template, the
19608 type-id should not define a type.
19609
19610 So let's set parser->type_definition_forbidden_message in that
19611 case; cp_parser_check_type_definition (called by
19612 cp_parser_class_specifier) will then emit an error if a type is
19613 defined in the type-id. */
19614 if (parser->num_template_parameter_lists)
19615 {
19616 saved_message = parser->type_definition_forbidden_message;
19617 parser->type_definition_forbidden_message =
19618 G_("types may not be defined in alias template declarations");
19619 }
19620
19621 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
19622 &type_location);
19623
19624 /* Restore the error message if need be. */
19625 if (parser->num_template_parameter_lists)
19626 parser->type_definition_forbidden_message = saved_message;
19627
19628 if (type == error_mark_node
19629 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19630 {
19631 cp_parser_skip_to_end_of_block_or_statement (parser);
19632 return error_mark_node;
19633 }
19634
19635 /* A typedef-name can also be introduced by an alias-declaration. The
19636 identifier following the using keyword becomes a typedef-name. It has
19637 the same semantics as if it were introduced by the typedef
19638 specifier. In particular, it does not define a new type and it shall
19639 not appear in the type-id. */
19640
19641 clear_decl_specs (&decl_specs);
19642 decl_specs.type = type;
19643 if (attributes != NULL_TREE)
19644 {
19645 decl_specs.attributes = attributes;
19646 set_and_check_decl_spec_loc (&decl_specs,
19647 ds_attribute,
19648 attrs_token);
19649 }
19650 set_and_check_decl_spec_loc (&decl_specs,
19651 ds_typedef,
19652 using_token);
19653 set_and_check_decl_spec_loc (&decl_specs,
19654 ds_alias,
19655 using_token);
19656 decl_specs.locations[ds_type_spec] = type_location;
19657
19658 if (parser->num_template_parameter_lists
19659 && !cp_parser_check_template_parameters (parser,
19660 /*num_templates=*/0,
19661 /*template_id*/false,
19662 id_location,
19663 /*declarator=*/NULL))
19664 return error_mark_node;
19665
19666 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19667
19668 member_p = at_class_scope_p ();
19669 if (member_p)
19670 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19671 NULL_TREE, attributes);
19672 else
19673 decl = start_decl (declarator, &decl_specs, 0,
19674 attributes, NULL_TREE, &pushed_scope);
19675 if (decl == error_mark_node)
19676 return decl;
19677
19678 // Attach constraints to the alias declaration.
19679 if (flag_concepts && current_template_parms)
19680 {
19681 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19682 tree constr = build_constraints (reqs, NULL_TREE);
19683 set_constraints (decl, constr);
19684 }
19685
19686 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19687
19688 if (pushed_scope)
19689 pop_scope (pushed_scope);
19690
19691 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19692 added into the symbol table; otherwise, return the TYPE_DECL. */
19693 if (DECL_LANG_SPECIFIC (decl)
19694 && DECL_TEMPLATE_INFO (decl)
19695 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19696 {
19697 decl = DECL_TI_TEMPLATE (decl);
19698 if (member_p)
19699 check_member_template (decl);
19700 }
19701
19702 return decl;
19703 }
19704
19705 /* Parse a using-directive.
19706
19707 using-directive:
19708 using namespace :: [opt] nested-name-specifier [opt]
19709 namespace-name ; */
19710
19711 static void
19712 cp_parser_using_directive (cp_parser* parser)
19713 {
19714 tree namespace_decl;
19715 tree attribs;
19716
19717 /* Look for the `using' keyword. */
19718 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19719 /* And the `namespace' keyword. */
19720 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19721 /* Look for the optional `::' operator. */
19722 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19723 /* And the optional nested-name-specifier. */
19724 cp_parser_nested_name_specifier_opt (parser,
19725 /*typename_keyword_p=*/false,
19726 /*check_dependency_p=*/true,
19727 /*type_p=*/false,
19728 /*is_declaration=*/true);
19729 /* Get the namespace being used. */
19730 namespace_decl = cp_parser_namespace_name (parser);
19731 /* And any specified attributes. */
19732 attribs = cp_parser_attributes_opt (parser);
19733
19734 /* Update the symbol table. */
19735 finish_using_directive (namespace_decl, attribs);
19736
19737 /* Look for the final `;'. */
19738 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19739 }
19740
19741 /* Parse an asm-definition.
19742
19743 asm-qualifier:
19744 volatile
19745 inline
19746 goto
19747
19748 asm-qualifier-list:
19749 asm-qualifier
19750 asm-qualifier-list asm-qualifier
19751
19752 asm-definition:
19753 asm ( string-literal ) ;
19754
19755 GNU Extension:
19756
19757 asm-definition:
19758 asm asm-qualifier-list [opt] ( string-literal ) ;
19759 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19760 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19761 : asm-operand-list [opt] ) ;
19762 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19763 : asm-operand-list [opt]
19764 : asm-clobber-list [opt] ) ;
19765 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19766 : asm-clobber-list [opt]
19767 : asm-goto-list ) ;
19768
19769 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19770 contains goto, and is the only allowed form in that case. No duplicates are
19771 allowed in an asm-qualifier-list. */
19772
19773 static void
19774 cp_parser_asm_definition (cp_parser* parser)
19775 {
19776 tree string;
19777 tree outputs = NULL_TREE;
19778 tree inputs = NULL_TREE;
19779 tree clobbers = NULL_TREE;
19780 tree labels = NULL_TREE;
19781 tree asm_stmt;
19782 bool extended_p = false;
19783 bool invalid_inputs_p = false;
19784 bool invalid_outputs_p = false;
19785 required_token missing = RT_NONE;
19786
19787 /* Look for the `asm' keyword. */
19788 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19789
19790 if (parser->in_function_body
19791 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19792 {
19793 error ("%<asm%> in %<constexpr%> function");
19794 cp_function_chain->invalid_constexpr = true;
19795 }
19796
19797 /* Handle the asm-qualifier-list. */
19798 location_t volatile_loc = UNKNOWN_LOCATION;
19799 location_t inline_loc = UNKNOWN_LOCATION;
19800 location_t goto_loc = UNKNOWN_LOCATION;
19801 location_t first_loc = UNKNOWN_LOCATION;
19802
19803 if (cp_parser_allow_gnu_extensions_p (parser))
19804 for (;;)
19805 {
19806 cp_token *token = cp_lexer_peek_token (parser->lexer);
19807 location_t loc = token->location;
19808 switch (cp_lexer_peek_token (parser->lexer)->keyword)
19809 {
19810 case RID_VOLATILE:
19811 if (volatile_loc)
19812 {
19813 error_at (loc, "duplicate %<asm%> qualifier %qT",
19814 token->u.value);
19815 inform (volatile_loc, "first seen here");
19816 }
19817 else
19818 {
19819 if (!parser->in_function_body)
19820 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
19821 "outside of function body", token->u.value);
19822 volatile_loc = loc;
19823 }
19824 cp_lexer_consume_token (parser->lexer);
19825 continue;
19826
19827 case RID_INLINE:
19828 if (inline_loc)
19829 {
19830 error_at (loc, "duplicate %<asm%> qualifier %qT",
19831 token->u.value);
19832 inform (inline_loc, "first seen here");
19833 }
19834 else
19835 inline_loc = loc;
19836 if (!first_loc)
19837 first_loc = loc;
19838 cp_lexer_consume_token (parser->lexer);
19839 continue;
19840
19841 case RID_GOTO:
19842 if (goto_loc)
19843 {
19844 error_at (loc, "duplicate %<asm%> qualifier %qT",
19845 token->u.value);
19846 inform (goto_loc, "first seen here");
19847 }
19848 else
19849 goto_loc = loc;
19850 if (!first_loc)
19851 first_loc = loc;
19852 cp_lexer_consume_token (parser->lexer);
19853 continue;
19854
19855 case RID_CONST:
19856 case RID_RESTRICT:
19857 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
19858 cp_lexer_consume_token (parser->lexer);
19859 continue;
19860
19861 default:
19862 break;
19863 }
19864 break;
19865 }
19866
19867 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
19868 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
19869 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
19870
19871 if (!parser->in_function_body && (inline_p || goto_p))
19872 {
19873 error_at (first_loc, "%<asm%> qualifier outside of function body");
19874 inline_p = goto_p = false;
19875 }
19876
19877 /* Look for the opening `('. */
19878 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19879 return;
19880 /* Look for the string. */
19881 string = cp_parser_string_literal (parser, false, false);
19882 if (string == error_mark_node)
19883 {
19884 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19885 /*consume_paren=*/true);
19886 return;
19887 }
19888
19889 /* If we're allowing GNU extensions, check for the extended assembly
19890 syntax. Unfortunately, the `:' tokens need not be separated by
19891 a space in C, and so, for compatibility, we tolerate that here
19892 too. Doing that means that we have to treat the `::' operator as
19893 two `:' tokens. */
19894 if (cp_parser_allow_gnu_extensions_p (parser)
19895 && parser->in_function_body
19896 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19897 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19898 {
19899 bool inputs_p = false;
19900 bool clobbers_p = false;
19901 bool labels_p = false;
19902
19903 /* The extended syntax was used. */
19904 extended_p = true;
19905
19906 /* Look for outputs. */
19907 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19908 {
19909 /* Consume the `:'. */
19910 cp_lexer_consume_token (parser->lexer);
19911 /* Parse the output-operands. */
19912 if (cp_lexer_next_token_is_not (parser->lexer,
19913 CPP_COLON)
19914 && cp_lexer_next_token_is_not (parser->lexer,
19915 CPP_SCOPE)
19916 && cp_lexer_next_token_is_not (parser->lexer,
19917 CPP_CLOSE_PAREN)
19918 && !goto_p)
19919 {
19920 outputs = cp_parser_asm_operand_list (parser);
19921 if (outputs == error_mark_node)
19922 invalid_outputs_p = true;
19923 }
19924 }
19925 /* If the next token is `::', there are no outputs, and the
19926 next token is the beginning of the inputs. */
19927 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19928 /* The inputs are coming next. */
19929 inputs_p = true;
19930
19931 /* Look for inputs. */
19932 if (inputs_p
19933 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19934 {
19935 /* Consume the `:' or `::'. */
19936 cp_lexer_consume_token (parser->lexer);
19937 /* Parse the output-operands. */
19938 if (cp_lexer_next_token_is_not (parser->lexer,
19939 CPP_COLON)
19940 && cp_lexer_next_token_is_not (parser->lexer,
19941 CPP_SCOPE)
19942 && cp_lexer_next_token_is_not (parser->lexer,
19943 CPP_CLOSE_PAREN))
19944 {
19945 inputs = cp_parser_asm_operand_list (parser);
19946 if (inputs == error_mark_node)
19947 invalid_inputs_p = true;
19948 }
19949 }
19950 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19951 /* The clobbers are coming next. */
19952 clobbers_p = true;
19953
19954 /* Look for clobbers. */
19955 if (clobbers_p
19956 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19957 {
19958 clobbers_p = true;
19959 /* Consume the `:' or `::'. */
19960 cp_lexer_consume_token (parser->lexer);
19961 /* Parse the clobbers. */
19962 if (cp_lexer_next_token_is_not (parser->lexer,
19963 CPP_COLON)
19964 && cp_lexer_next_token_is_not (parser->lexer,
19965 CPP_CLOSE_PAREN))
19966 clobbers = cp_parser_asm_clobber_list (parser);
19967 }
19968 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19969 /* The labels are coming next. */
19970 labels_p = true;
19971
19972 /* Look for labels. */
19973 if (labels_p
19974 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19975 {
19976 labels_p = true;
19977 /* Consume the `:' or `::'. */
19978 cp_lexer_consume_token (parser->lexer);
19979 /* Parse the labels. */
19980 labels = cp_parser_asm_label_list (parser);
19981 }
19982
19983 if (goto_p && !labels_p)
19984 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19985 }
19986 else if (goto_p)
19987 missing = RT_COLON_SCOPE;
19988
19989 /* Look for the closing `)'. */
19990 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19991 missing ? missing : RT_CLOSE_PAREN))
19992 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19993 /*consume_paren=*/true);
19994 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19995
19996 if (!invalid_inputs_p && !invalid_outputs_p)
19997 {
19998 /* Create the ASM_EXPR. */
19999 if (parser->in_function_body)
20000 {
20001 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
20002 inputs, clobbers, labels, inline_p);
20003 /* If the extended syntax was not used, mark the ASM_EXPR. */
20004 if (!extended_p)
20005 {
20006 tree temp = asm_stmt;
20007 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
20008 temp = TREE_OPERAND (temp, 0);
20009
20010 ASM_INPUT_P (temp) = 1;
20011 }
20012 }
20013 else
20014 symtab->finalize_toplevel_asm (string);
20015 }
20016 }
20017
20018 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20019 type that comes from the decl-specifier-seq. */
20020
20021 static tree
20022 strip_declarator_types (tree type, cp_declarator *declarator)
20023 {
20024 for (cp_declarator *d = declarator; d;)
20025 switch (d->kind)
20026 {
20027 case cdk_id:
20028 case cdk_decomp:
20029 case cdk_error:
20030 d = NULL;
20031 break;
20032
20033 default:
20034 if (TYPE_PTRMEMFUNC_P (type))
20035 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
20036 type = TREE_TYPE (type);
20037 d = d->declarator;
20038 break;
20039 }
20040
20041 return type;
20042 }
20043
20044 /* Declarators [gram.dcl.decl] */
20045
20046 /* Parse an init-declarator.
20047
20048 init-declarator:
20049 declarator initializer [opt]
20050
20051 GNU Extension:
20052
20053 init-declarator:
20054 declarator asm-specification [opt] attributes [opt] initializer [opt]
20055
20056 function-definition:
20057 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20058 function-body
20059 decl-specifier-seq [opt] declarator function-try-block
20060
20061 GNU Extension:
20062
20063 function-definition:
20064 __extension__ function-definition
20065
20066 TM Extension:
20067
20068 function-definition:
20069 decl-specifier-seq [opt] declarator function-transaction-block
20070
20071 The parser flags FLAGS is used to control type-specifier parsing.
20072
20073 The DECL_SPECIFIERS apply to this declarator. Returns a
20074 representation of the entity declared. If MEMBER_P is TRUE, then
20075 this declarator appears in a class scope. The new DECL created by
20076 this declarator is returned.
20077
20078 The CHECKS are access checks that should be performed once we know
20079 what entity is being declared (and, therefore, what classes have
20080 befriended it).
20081
20082 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20083 for a function-definition here as well. If the declarator is a
20084 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20085 be TRUE upon return. By that point, the function-definition will
20086 have been completely parsed.
20087
20088 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20089 is FALSE.
20090
20091 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20092 parsed declaration if it is an uninitialized single declarator not followed
20093 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20094 if present, will not be consumed. If returned, this declarator will be
20095 created with SD_INITIALIZED but will not call cp_finish_decl.
20096
20097 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20098 and there is an initializer, the pointed location_t is set to the
20099 location of the '=' or `(', or '{' in C++11 token introducing the
20100 initializer. */
20101
20102 static tree
20103 cp_parser_init_declarator (cp_parser* parser,
20104 cp_parser_flags flags,
20105 cp_decl_specifier_seq *decl_specifiers,
20106 vec<deferred_access_check, va_gc> *checks,
20107 bool function_definition_allowed_p,
20108 bool member_p,
20109 int declares_class_or_enum,
20110 bool* function_definition_p,
20111 tree* maybe_range_for_decl,
20112 location_t* init_loc,
20113 tree* auto_result)
20114 {
20115 cp_token *token = NULL, *asm_spec_start_token = NULL,
20116 *attributes_start_token = NULL;
20117 cp_declarator *declarator;
20118 tree prefix_attributes;
20119 tree attributes = NULL;
20120 tree asm_specification;
20121 tree initializer;
20122 tree decl = NULL_TREE;
20123 tree scope;
20124 int is_initialized;
20125 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20126 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20127 "(...)". */
20128 enum cpp_ttype initialization_kind;
20129 bool is_direct_init = false;
20130 bool is_non_constant_init;
20131 int ctor_dtor_or_conv_p;
20132 bool friend_p = cp_parser_friend_p (decl_specifiers);
20133 tree pushed_scope = NULL_TREE;
20134 bool range_for_decl_p = false;
20135 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20136 location_t tmp_init_loc = UNKNOWN_LOCATION;
20137
20138 /* Gather the attributes that were provided with the
20139 decl-specifiers. */
20140 prefix_attributes = decl_specifiers->attributes;
20141
20142 /* Assume that this is not the declarator for a function
20143 definition. */
20144 if (function_definition_p)
20145 *function_definition_p = false;
20146
20147 /* Default arguments are only permitted for function parameters. */
20148 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20149 parser->default_arg_ok_p = false;
20150
20151 /* Defer access checks while parsing the declarator; we cannot know
20152 what names are accessible until we know what is being
20153 declared. */
20154 resume_deferring_access_checks ();
20155
20156 token = cp_lexer_peek_token (parser->lexer);
20157
20158 /* Parse the declarator. */
20159 declarator
20160 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20161 flags, &ctor_dtor_or_conv_p,
20162 /*parenthesized_p=*/NULL,
20163 member_p, friend_p, /*static_p=*/false);
20164 /* Gather up the deferred checks. */
20165 stop_deferring_access_checks ();
20166
20167 parser->default_arg_ok_p = saved_default_arg_ok_p;
20168
20169 /* If the DECLARATOR was erroneous, there's no need to go
20170 further. */
20171 if (declarator == cp_error_declarator)
20172 return error_mark_node;
20173
20174 /* Check that the number of template-parameter-lists is OK. */
20175 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20176 token->location))
20177 return error_mark_node;
20178
20179 if (declares_class_or_enum & 2)
20180 cp_parser_check_for_definition_in_return_type (declarator,
20181 decl_specifiers->type,
20182 decl_specifiers->locations[ds_type_spec]);
20183
20184 /* Figure out what scope the entity declared by the DECLARATOR is
20185 located in. `grokdeclarator' sometimes changes the scope, so
20186 we compute it now. */
20187 scope = get_scope_of_declarator (declarator);
20188
20189 /* Perform any lookups in the declared type which were thought to be
20190 dependent, but are not in the scope of the declarator. */
20191 decl_specifiers->type
20192 = maybe_update_decl_type (decl_specifiers->type, scope);
20193
20194 /* If we're allowing GNU extensions, look for an
20195 asm-specification. */
20196 if (cp_parser_allow_gnu_extensions_p (parser))
20197 {
20198 /* Look for an asm-specification. */
20199 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20200 asm_specification = cp_parser_asm_specification_opt (parser);
20201 }
20202 else
20203 asm_specification = NULL_TREE;
20204
20205 /* Look for attributes. */
20206 attributes_start_token = cp_lexer_peek_token (parser->lexer);
20207 attributes = cp_parser_attributes_opt (parser);
20208
20209 /* Peek at the next token. */
20210 token = cp_lexer_peek_token (parser->lexer);
20211
20212 bool bogus_implicit_tmpl = false;
20213
20214 if (function_declarator_p (declarator))
20215 {
20216 /* Handle C++17 deduction guides. */
20217 if (!decl_specifiers->type
20218 && ctor_dtor_or_conv_p <= 0
20219 && cxx_dialect >= cxx17)
20220 {
20221 cp_declarator *id = get_id_declarator (declarator);
20222 tree name = id->u.id.unqualified_name;
20223 parser->scope = id->u.id.qualifying_scope;
20224 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20225 if (tmpl
20226 && (DECL_CLASS_TEMPLATE_P (tmpl)
20227 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20228 {
20229 id->u.id.unqualified_name = dguide_name (tmpl);
20230 id->u.id.sfk = sfk_deduction_guide;
20231 ctor_dtor_or_conv_p = 1;
20232 }
20233 }
20234
20235 /* Check to see if the token indicates the start of a
20236 function-definition. */
20237 if (cp_parser_token_starts_function_definition_p (token))
20238 {
20239 if (!function_definition_allowed_p)
20240 {
20241 /* If a function-definition should not appear here, issue an
20242 error message. */
20243 cp_parser_error (parser,
20244 "a function-definition is not allowed here");
20245 return error_mark_node;
20246 }
20247
20248 location_t func_brace_location
20249 = cp_lexer_peek_token (parser->lexer)->location;
20250
20251 /* Neither attributes nor an asm-specification are allowed
20252 on a function-definition. */
20253 if (asm_specification)
20254 error_at (asm_spec_start_token->location,
20255 "an %<asm%> specification is not allowed "
20256 "on a function-definition");
20257 if (attributes)
20258 error_at (attributes_start_token->location,
20259 "attributes are not allowed "
20260 "on a function-definition");
20261 /* This is a function-definition. */
20262 *function_definition_p = true;
20263
20264 /* Parse the function definition. */
20265 if (member_p)
20266 decl = cp_parser_save_member_function_body (parser,
20267 decl_specifiers,
20268 declarator,
20269 prefix_attributes);
20270 else
20271 decl =
20272 (cp_parser_function_definition_from_specifiers_and_declarator
20273 (parser, decl_specifiers, prefix_attributes, declarator));
20274
20275 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20276 {
20277 /* This is where the prologue starts... */
20278 DECL_STRUCT_FUNCTION (decl)->function_start_locus
20279 = func_brace_location;
20280 }
20281
20282 return decl;
20283 }
20284 }
20285 else if (parser->fully_implicit_function_template_p)
20286 {
20287 /* A non-template declaration involving a function parameter list
20288 containing an implicit template parameter will be made into a
20289 template. If the resulting declaration is not going to be an
20290 actual function then finish the template scope here to prevent it.
20291 An error message will be issued once we have a decl to talk about.
20292
20293 FIXME probably we should do type deduction rather than create an
20294 implicit template, but the standard currently doesn't allow it. */
20295 bogus_implicit_tmpl = true;
20296 finish_fully_implicit_template (parser, NULL_TREE);
20297 }
20298
20299 /* [dcl.dcl]
20300
20301 Only in function declarations for constructors, destructors, type
20302 conversions, and deduction guides can the decl-specifier-seq be omitted.
20303
20304 We explicitly postpone this check past the point where we handle
20305 function-definitions because we tolerate function-definitions
20306 that are missing their return types in some modes. */
20307 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20308 {
20309 cp_parser_error (parser,
20310 "expected constructor, destructor, or type conversion");
20311 return error_mark_node;
20312 }
20313
20314 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20315 if (token->type == CPP_EQ
20316 || token->type == CPP_OPEN_PAREN
20317 || token->type == CPP_OPEN_BRACE)
20318 {
20319 is_initialized = SD_INITIALIZED;
20320 initialization_kind = token->type;
20321 if (maybe_range_for_decl)
20322 *maybe_range_for_decl = error_mark_node;
20323 tmp_init_loc = token->location;
20324 if (init_loc && *init_loc == UNKNOWN_LOCATION)
20325 *init_loc = tmp_init_loc;
20326
20327 if (token->type == CPP_EQ
20328 && function_declarator_p (declarator))
20329 {
20330 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20331 if (t2->keyword == RID_DEFAULT)
20332 is_initialized = SD_DEFAULTED;
20333 else if (t2->keyword == RID_DELETE)
20334 is_initialized = SD_DELETED;
20335 }
20336 }
20337 else
20338 {
20339 /* If the init-declarator isn't initialized and isn't followed by a
20340 `,' or `;', it's not a valid init-declarator. */
20341 if (token->type != CPP_COMMA
20342 && token->type != CPP_SEMICOLON)
20343 {
20344 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20345 range_for_decl_p = true;
20346 else
20347 {
20348 if (!maybe_range_for_decl)
20349 cp_parser_error (parser, "expected initializer");
20350 return error_mark_node;
20351 }
20352 }
20353 is_initialized = SD_UNINITIALIZED;
20354 initialization_kind = CPP_EOF;
20355 }
20356
20357 /* Because start_decl has side-effects, we should only call it if we
20358 know we're going ahead. By this point, we know that we cannot
20359 possibly be looking at any other construct. */
20360 cp_parser_commit_to_tentative_parse (parser);
20361
20362 /* Enter the newly declared entry in the symbol table. If we're
20363 processing a declaration in a class-specifier, we wait until
20364 after processing the initializer. */
20365 if (!member_p)
20366 {
20367 if (parser->in_unbraced_linkage_specification_p)
20368 decl_specifiers->storage_class = sc_extern;
20369 decl = start_decl (declarator, decl_specifiers,
20370 range_for_decl_p? SD_INITIALIZED : is_initialized,
20371 attributes, prefix_attributes, &pushed_scope);
20372 cp_finalize_omp_declare_simd (parser, decl);
20373 cp_finalize_oacc_routine (parser, decl, false);
20374 /* Adjust location of decl if declarator->id_loc is more appropriate:
20375 set, and decl wasn't merged with another decl, in which case its
20376 location would be different from input_location, and more accurate. */
20377 if (DECL_P (decl)
20378 && declarator->id_loc != UNKNOWN_LOCATION
20379 && DECL_SOURCE_LOCATION (decl) == input_location)
20380 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20381 }
20382 else if (scope)
20383 /* Enter the SCOPE. That way unqualified names appearing in the
20384 initializer will be looked up in SCOPE. */
20385 pushed_scope = push_scope (scope);
20386
20387 /* Perform deferred access control checks, now that we know in which
20388 SCOPE the declared entity resides. */
20389 if (!member_p && decl)
20390 {
20391 tree saved_current_function_decl = NULL_TREE;
20392
20393 /* If the entity being declared is a function, pretend that we
20394 are in its scope. If it is a `friend', it may have access to
20395 things that would not otherwise be accessible. */
20396 if (TREE_CODE (decl) == FUNCTION_DECL)
20397 {
20398 saved_current_function_decl = current_function_decl;
20399 current_function_decl = decl;
20400 }
20401
20402 /* Perform access checks for template parameters. */
20403 cp_parser_perform_template_parameter_access_checks (checks);
20404
20405 /* Perform the access control checks for the declarator and the
20406 decl-specifiers. */
20407 perform_deferred_access_checks (tf_warning_or_error);
20408
20409 /* Restore the saved value. */
20410 if (TREE_CODE (decl) == FUNCTION_DECL)
20411 current_function_decl = saved_current_function_decl;
20412 }
20413
20414 /* Parse the initializer. */
20415 initializer = NULL_TREE;
20416 is_direct_init = false;
20417 is_non_constant_init = true;
20418 if (is_initialized)
20419 {
20420 if (function_declarator_p (declarator))
20421 {
20422 if (initialization_kind == CPP_EQ)
20423 initializer = cp_parser_pure_specifier (parser);
20424 else
20425 {
20426 /* If the declaration was erroneous, we don't really
20427 know what the user intended, so just silently
20428 consume the initializer. */
20429 if (decl != error_mark_node)
20430 error_at (tmp_init_loc, "initializer provided for function");
20431 cp_parser_skip_to_closing_parenthesis (parser,
20432 /*recovering=*/true,
20433 /*or_comma=*/false,
20434 /*consume_paren=*/true);
20435 }
20436 }
20437 else
20438 {
20439 /* We want to record the extra mangling scope for in-class
20440 initializers of class members and initializers of static data
20441 member templates. The former involves deferring
20442 parsing of the initializer until end of class as with default
20443 arguments. So right here we only handle the latter. */
20444 if (!member_p && processing_template_decl && decl != error_mark_node)
20445 start_lambda_scope (decl);
20446 initializer = cp_parser_initializer (parser,
20447 &is_direct_init,
20448 &is_non_constant_init);
20449 if (!member_p && processing_template_decl && decl != error_mark_node)
20450 finish_lambda_scope ();
20451 if (initializer == error_mark_node)
20452 cp_parser_skip_to_end_of_statement (parser);
20453 }
20454 }
20455
20456 /* The old parser allows attributes to appear after a parenthesized
20457 initializer. Mark Mitchell proposed removing this functionality
20458 on the GCC mailing lists on 2002-08-13. This parser accepts the
20459 attributes -- but ignores them. Made a permerror in GCC 8. */
20460 if (cp_parser_allow_gnu_extensions_p (parser)
20461 && initialization_kind == CPP_OPEN_PAREN
20462 && cp_parser_attributes_opt (parser)
20463 && permerror (input_location,
20464 "attributes after parenthesized initializer ignored"))
20465 {
20466 static bool hint;
20467 if (flag_permissive && !hint)
20468 {
20469 hint = true;
20470 inform (input_location,
20471 "this flexibility is deprecated and will be removed");
20472 }
20473 }
20474
20475 /* And now complain about a non-function implicit template. */
20476 if (bogus_implicit_tmpl && decl != error_mark_node)
20477 error_at (DECL_SOURCE_LOCATION (decl),
20478 "non-function %qD declared as implicit template", decl);
20479
20480 /* For an in-class declaration, use `grokfield' to create the
20481 declaration. */
20482 if (member_p)
20483 {
20484 if (pushed_scope)
20485 {
20486 pop_scope (pushed_scope);
20487 pushed_scope = NULL_TREE;
20488 }
20489 decl = grokfield (declarator, decl_specifiers,
20490 initializer, !is_non_constant_init,
20491 /*asmspec=*/NULL_TREE,
20492 attr_chainon (attributes, prefix_attributes));
20493 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20494 cp_parser_save_default_args (parser, decl);
20495 cp_finalize_omp_declare_simd (parser, decl);
20496 cp_finalize_oacc_routine (parser, decl, false);
20497 }
20498
20499 /* Finish processing the declaration. But, skip member
20500 declarations. */
20501 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20502 {
20503 cp_finish_decl (decl,
20504 initializer, !is_non_constant_init,
20505 asm_specification,
20506 /* If the initializer is in parentheses, then this is
20507 a direct-initialization, which means that an
20508 `explicit' constructor is OK. Otherwise, an
20509 `explicit' constructor cannot be used. */
20510 ((is_direct_init || !is_initialized)
20511 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20512 }
20513 else if ((cxx_dialect != cxx98) && friend_p
20514 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20515 /* Core issue #226 (C++0x only): A default template-argument
20516 shall not be specified in a friend class template
20517 declaration. */
20518 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20519 /*is_partial=*/false, /*is_friend_decl=*/1);
20520
20521 if (!friend_p && pushed_scope)
20522 pop_scope (pushed_scope);
20523
20524 if (function_declarator_p (declarator)
20525 && parser->fully_implicit_function_template_p)
20526 {
20527 if (member_p)
20528 decl = finish_fully_implicit_template (parser, decl);
20529 else
20530 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20531 }
20532
20533 if (auto_result && is_initialized && decl_specifiers->type
20534 && type_uses_auto (decl_specifiers->type))
20535 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20536
20537 return decl;
20538 }
20539
20540 /* Parse a declarator.
20541
20542 declarator:
20543 direct-declarator
20544 ptr-operator declarator
20545
20546 abstract-declarator:
20547 ptr-operator abstract-declarator [opt]
20548 direct-abstract-declarator
20549
20550 GNU Extensions:
20551
20552 declarator:
20553 attributes [opt] direct-declarator
20554 attributes [opt] ptr-operator declarator
20555
20556 abstract-declarator:
20557 attributes [opt] ptr-operator abstract-declarator [opt]
20558 attributes [opt] direct-abstract-declarator
20559
20560 The parser flags FLAGS is used to control type-specifier parsing.
20561
20562 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20563 detect constructors, destructors, deduction guides, or conversion operators.
20564 It is set to -1 if the declarator is a name, and +1 if it is a
20565 function. Otherwise it is set to zero. Usually you just want to
20566 test for >0, but internally the negative value is used.
20567
20568 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20569 a decl-specifier-seq unless it declares a constructor, destructor,
20570 or conversion. It might seem that we could check this condition in
20571 semantic analysis, rather than parsing, but that makes it difficult
20572 to handle something like `f()'. We want to notice that there are
20573 no decl-specifiers, and therefore realize that this is an
20574 expression, not a declaration.)
20575
20576 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20577 the declarator is a direct-declarator of the form "(...)".
20578
20579 MEMBER_P is true iff this declarator is a member-declarator.
20580
20581 FRIEND_P is true iff this declarator is a friend.
20582
20583 STATIC_P is true iff the keyword static was seen. */
20584
20585 static cp_declarator *
20586 cp_parser_declarator (cp_parser* parser,
20587 cp_parser_declarator_kind dcl_kind,
20588 cp_parser_flags flags,
20589 int* ctor_dtor_or_conv_p,
20590 bool* parenthesized_p,
20591 bool member_p, bool friend_p, bool static_p)
20592 {
20593 cp_declarator *declarator;
20594 enum tree_code code;
20595 cp_cv_quals cv_quals;
20596 tree class_type;
20597 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20598
20599 /* Assume this is not a constructor, destructor, or type-conversion
20600 operator. */
20601 if (ctor_dtor_or_conv_p)
20602 *ctor_dtor_or_conv_p = 0;
20603
20604 if (cp_parser_allow_gnu_extensions_p (parser))
20605 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20606
20607 /* Check for the ptr-operator production. */
20608 cp_parser_parse_tentatively (parser);
20609 /* Parse the ptr-operator. */
20610 code = cp_parser_ptr_operator (parser,
20611 &class_type,
20612 &cv_quals,
20613 &std_attributes);
20614
20615 /* If that worked, then we have a ptr-operator. */
20616 if (cp_parser_parse_definitely (parser))
20617 {
20618 /* If a ptr-operator was found, then this declarator was not
20619 parenthesized. */
20620 if (parenthesized_p)
20621 *parenthesized_p = true;
20622 /* The dependent declarator is optional if we are parsing an
20623 abstract-declarator. */
20624 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20625 cp_parser_parse_tentatively (parser);
20626
20627 /* Parse the dependent declarator. */
20628 declarator = cp_parser_declarator (parser, dcl_kind,
20629 CP_PARSER_FLAGS_NONE,
20630 /*ctor_dtor_or_conv_p=*/NULL,
20631 /*parenthesized_p=*/NULL,
20632 /*member_p=*/false,
20633 friend_p, /*static_p=*/false);
20634
20635 /* If we are parsing an abstract-declarator, we must handle the
20636 case where the dependent declarator is absent. */
20637 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20638 && !cp_parser_parse_definitely (parser))
20639 declarator = NULL;
20640
20641 declarator = cp_parser_make_indirect_declarator
20642 (code, class_type, cv_quals, declarator, std_attributes);
20643 }
20644 /* Everything else is a direct-declarator. */
20645 else
20646 {
20647 if (parenthesized_p)
20648 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20649 CPP_OPEN_PAREN);
20650 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20651 flags, ctor_dtor_or_conv_p,
20652 member_p, friend_p, static_p);
20653 }
20654
20655 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20656 declarator->attributes = gnu_attributes;
20657 return declarator;
20658 }
20659
20660 /* Parse a direct-declarator or direct-abstract-declarator.
20661
20662 direct-declarator:
20663 declarator-id
20664 direct-declarator ( parameter-declaration-clause )
20665 cv-qualifier-seq [opt]
20666 ref-qualifier [opt]
20667 exception-specification [opt]
20668 direct-declarator [ constant-expression [opt] ]
20669 ( declarator )
20670
20671 direct-abstract-declarator:
20672 direct-abstract-declarator [opt]
20673 ( parameter-declaration-clause )
20674 cv-qualifier-seq [opt]
20675 ref-qualifier [opt]
20676 exception-specification [opt]
20677 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20678 ( abstract-declarator )
20679
20680 Returns a representation of the declarator. DCL_KIND is
20681 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20682 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20683 we are parsing a direct-declarator. It is
20684 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20685 of ambiguity we prefer an abstract declarator, as per
20686 [dcl.ambig.res].
20687 The parser flags FLAGS is used to control type-specifier parsing.
20688 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20689 as for cp_parser_declarator. */
20690
20691 static cp_declarator *
20692 cp_parser_direct_declarator (cp_parser* parser,
20693 cp_parser_declarator_kind dcl_kind,
20694 cp_parser_flags flags,
20695 int* ctor_dtor_or_conv_p,
20696 bool member_p, bool friend_p, bool static_p)
20697 {
20698 cp_token *token;
20699 cp_declarator *declarator = NULL;
20700 tree scope = NULL_TREE;
20701 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20702 bool saved_in_declarator_p = parser->in_declarator_p;
20703 bool first = true;
20704 tree pushed_scope = NULL_TREE;
20705 cp_token *open_paren = NULL, *close_paren = NULL;
20706
20707 while (true)
20708 {
20709 /* Peek at the next token. */
20710 token = cp_lexer_peek_token (parser->lexer);
20711 if (token->type == CPP_OPEN_PAREN)
20712 {
20713 /* This is either a parameter-declaration-clause, or a
20714 parenthesized declarator. When we know we are parsing a
20715 named declarator, it must be a parenthesized declarator
20716 if FIRST is true. For instance, `(int)' is a
20717 parameter-declaration-clause, with an omitted
20718 direct-abstract-declarator. But `((*))', is a
20719 parenthesized abstract declarator. Finally, when T is a
20720 template parameter `(T)' is a
20721 parameter-declaration-clause, and not a parenthesized
20722 named declarator.
20723
20724 We first try and parse a parameter-declaration-clause,
20725 and then try a nested declarator (if FIRST is true).
20726
20727 It is not an error for it not to be a
20728 parameter-declaration-clause, even when FIRST is
20729 false. Consider,
20730
20731 int i (int);
20732 int i (3);
20733
20734 The first is the declaration of a function while the
20735 second is the definition of a variable, including its
20736 initializer.
20737
20738 Having seen only the parenthesis, we cannot know which of
20739 these two alternatives should be selected. Even more
20740 complex are examples like:
20741
20742 int i (int (a));
20743 int i (int (3));
20744
20745 The former is a function-declaration; the latter is a
20746 variable initialization.
20747
20748 Thus again, we try a parameter-declaration-clause, and if
20749 that fails, we back out and return. */
20750
20751 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20752 {
20753 tree params;
20754 bool is_declarator = false;
20755
20756 open_paren = NULL;
20757
20758 /* In a member-declarator, the only valid interpretation
20759 of a parenthesis is the start of a
20760 parameter-declaration-clause. (It is invalid to
20761 initialize a static data member with a parenthesized
20762 initializer; only the "=" form of initialization is
20763 permitted.) */
20764 if (!member_p)
20765 cp_parser_parse_tentatively (parser);
20766
20767 /* Consume the `('. */
20768 matching_parens parens;
20769 parens.consume_open (parser);
20770 if (first)
20771 {
20772 /* If this is going to be an abstract declarator, we're
20773 in a declarator and we can't have default args. */
20774 parser->default_arg_ok_p = false;
20775 parser->in_declarator_p = true;
20776 }
20777
20778 begin_scope (sk_function_parms, NULL_TREE);
20779
20780 /* Parse the parameter-declaration-clause. */
20781 params
20782 = cp_parser_parameter_declaration_clause (parser, flags);
20783
20784 /* Consume the `)'. */
20785 parens.require_close (parser);
20786
20787 /* If all went well, parse the cv-qualifier-seq,
20788 ref-qualifier and the exception-specification. */
20789 if (member_p || cp_parser_parse_definitely (parser))
20790 {
20791 cp_cv_quals cv_quals;
20792 cp_virt_specifiers virt_specifiers;
20793 cp_ref_qualifier ref_qual;
20794 tree exception_specification;
20795 tree late_return;
20796 tree attrs;
20797 bool memfn = (member_p || (pushed_scope
20798 && CLASS_TYPE_P (pushed_scope)));
20799 unsigned char local_variables_forbidden_p
20800 = parser->local_variables_forbidden_p;
20801 /* 'this' is not allowed in static member functions. */
20802 if (static_p || friend_p)
20803 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
20804
20805 is_declarator = true;
20806
20807 if (ctor_dtor_or_conv_p)
20808 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20809 first = false;
20810
20811 /* Parse the cv-qualifier-seq. */
20812 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20813 /* Parse the ref-qualifier. */
20814 ref_qual = cp_parser_ref_qualifier_opt (parser);
20815 /* Parse the tx-qualifier. */
20816 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20817 /* And the exception-specification. */
20818 exception_specification
20819 = cp_parser_exception_specification_opt (parser);
20820
20821 attrs = cp_parser_std_attribute_spec_seq (parser);
20822
20823 /* In here, we handle cases where attribute is used after
20824 the function declaration. For example:
20825 void func (int x) __attribute__((vector(..))); */
20826 tree gnu_attrs = NULL_TREE;
20827 tree requires_clause = NULL_TREE;
20828 late_return = (cp_parser_late_return_type_opt
20829 (parser, declarator, requires_clause,
20830 memfn ? cv_quals : -1));
20831
20832 /* Parse the virt-specifier-seq. */
20833 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20834
20835 /* Create the function-declarator. */
20836 declarator = make_call_declarator (declarator,
20837 params,
20838 cv_quals,
20839 virt_specifiers,
20840 ref_qual,
20841 tx_qual,
20842 exception_specification,
20843 late_return,
20844 requires_clause);
20845 declarator->std_attributes = attrs;
20846 declarator->attributes = gnu_attrs;
20847 /* Any subsequent parameter lists are to do with
20848 return type, so are not those of the declared
20849 function. */
20850 parser->default_arg_ok_p = false;
20851
20852 /* Restore the state of local_variables_forbidden_p. */
20853 parser->local_variables_forbidden_p
20854 = local_variables_forbidden_p;
20855 }
20856
20857 /* Remove the function parms from scope. */
20858 pop_bindings_and_leave_scope ();
20859
20860 if (is_declarator)
20861 /* Repeat the main loop. */
20862 continue;
20863 }
20864
20865 /* If this is the first, we can try a parenthesized
20866 declarator. */
20867 if (first)
20868 {
20869 bool saved_in_type_id_in_expr_p;
20870
20871 parser->default_arg_ok_p = saved_default_arg_ok_p;
20872 parser->in_declarator_p = saved_in_declarator_p;
20873
20874 open_paren = token;
20875 /* Consume the `('. */
20876 matching_parens parens;
20877 parens.consume_open (parser);
20878 /* Parse the nested declarator. */
20879 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20880 parser->in_type_id_in_expr_p = true;
20881 declarator
20882 = cp_parser_declarator (parser, dcl_kind, flags,
20883 ctor_dtor_or_conv_p,
20884 /*parenthesized_p=*/NULL,
20885 member_p, friend_p,
20886 /*static_p=*/false);
20887 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20888 first = false;
20889 /* Expect a `)'. */
20890 close_paren = cp_lexer_peek_token (parser->lexer);
20891 if (!parens.require_close (parser))
20892 declarator = cp_error_declarator;
20893 if (declarator == cp_error_declarator)
20894 break;
20895
20896 goto handle_declarator;
20897 }
20898 /* Otherwise, we must be done. */
20899 else
20900 break;
20901 }
20902 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20903 && token->type == CPP_OPEN_SQUARE
20904 && !cp_next_tokens_can_be_attribute_p (parser))
20905 {
20906 /* Parse an array-declarator. */
20907 tree bounds, attrs;
20908
20909 if (ctor_dtor_or_conv_p)
20910 *ctor_dtor_or_conv_p = 0;
20911
20912 open_paren = NULL;
20913 first = false;
20914 parser->default_arg_ok_p = false;
20915 parser->in_declarator_p = true;
20916 /* Consume the `['. */
20917 cp_lexer_consume_token (parser->lexer);
20918 /* Peek at the next token. */
20919 token = cp_lexer_peek_token (parser->lexer);
20920 /* If the next token is `]', then there is no
20921 constant-expression. */
20922 if (token->type != CPP_CLOSE_SQUARE)
20923 {
20924 bool non_constant_p;
20925 bounds
20926 = cp_parser_constant_expression (parser,
20927 /*allow_non_constant=*/true,
20928 &non_constant_p);
20929 if (!non_constant_p)
20930 /* OK */;
20931 else if (error_operand_p (bounds))
20932 /* Already gave an error. */;
20933 else if (!parser->in_function_body
20934 || current_binding_level->kind == sk_function_parms)
20935 {
20936 /* Normally, the array bound must be an integral constant
20937 expression. However, as an extension, we allow VLAs
20938 in function scopes as long as they aren't part of a
20939 parameter declaration. */
20940 cp_parser_error (parser,
20941 "array bound is not an integer constant");
20942 bounds = error_mark_node;
20943 }
20944 else if (processing_template_decl
20945 && !type_dependent_expression_p (bounds))
20946 {
20947 /* Remember this wasn't a constant-expression. */
20948 bounds = build_nop (TREE_TYPE (bounds), bounds);
20949 TREE_SIDE_EFFECTS (bounds) = 1;
20950 }
20951 }
20952 else
20953 bounds = NULL_TREE;
20954 /* Look for the closing `]'. */
20955 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20956 {
20957 declarator = cp_error_declarator;
20958 break;
20959 }
20960
20961 attrs = cp_parser_std_attribute_spec_seq (parser);
20962 declarator = make_array_declarator (declarator, bounds);
20963 declarator->std_attributes = attrs;
20964 }
20965 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20966 {
20967 {
20968 tree qualifying_scope;
20969 tree unqualified_name;
20970 tree attrs;
20971 special_function_kind sfk;
20972 bool abstract_ok;
20973 bool pack_expansion_p = false;
20974 cp_token *declarator_id_start_token;
20975
20976 /* Parse a declarator-id */
20977 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20978 if (abstract_ok)
20979 {
20980 cp_parser_parse_tentatively (parser);
20981
20982 /* If we see an ellipsis, we should be looking at a
20983 parameter pack. */
20984 if (token->type == CPP_ELLIPSIS)
20985 {
20986 /* Consume the `...' */
20987 cp_lexer_consume_token (parser->lexer);
20988
20989 pack_expansion_p = true;
20990 }
20991 }
20992
20993 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20994 unqualified_name
20995 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20996 qualifying_scope = parser->scope;
20997 if (abstract_ok)
20998 {
20999 bool okay = false;
21000
21001 if (!unqualified_name && pack_expansion_p)
21002 {
21003 /* Check whether an error occurred. */
21004 okay = !cp_parser_error_occurred (parser);
21005
21006 /* We already consumed the ellipsis to mark a
21007 parameter pack, but we have no way to report it,
21008 so abort the tentative parse. We will be exiting
21009 immediately anyway. */
21010 cp_parser_abort_tentative_parse (parser);
21011 }
21012 else
21013 okay = cp_parser_parse_definitely (parser);
21014
21015 if (!okay)
21016 unqualified_name = error_mark_node;
21017 else if (unqualified_name
21018 && (qualifying_scope
21019 || (!identifier_p (unqualified_name))))
21020 {
21021 cp_parser_error (parser, "expected unqualified-id");
21022 unqualified_name = error_mark_node;
21023 }
21024 }
21025
21026 if (!unqualified_name)
21027 return NULL;
21028 if (unqualified_name == error_mark_node)
21029 {
21030 declarator = cp_error_declarator;
21031 pack_expansion_p = false;
21032 declarator->parameter_pack_p = false;
21033 break;
21034 }
21035
21036 attrs = cp_parser_std_attribute_spec_seq (parser);
21037
21038 if (qualifying_scope && at_namespace_scope_p ()
21039 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
21040 {
21041 /* In the declaration of a member of a template class
21042 outside of the class itself, the SCOPE will sometimes
21043 be a TYPENAME_TYPE. For example, given:
21044
21045 template <typename T>
21046 int S<T>::R::i = 3;
21047
21048 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
21049 this context, we must resolve S<T>::R to an ordinary
21050 type, rather than a typename type.
21051
21052 The reason we normally avoid resolving TYPENAME_TYPEs
21053 is that a specialization of `S' might render
21054 `S<T>::R' not a type. However, if `S' is
21055 specialized, then this `i' will not be used, so there
21056 is no harm in resolving the types here. */
21057 tree type;
21058
21059 /* Resolve the TYPENAME_TYPE. */
21060 type = resolve_typename_type (qualifying_scope,
21061 /*only_current_p=*/false);
21062 /* If that failed, the declarator is invalid. */
21063 if (TREE_CODE (type) == TYPENAME_TYPE)
21064 {
21065 if (typedef_variant_p (type))
21066 error_at (declarator_id_start_token->location,
21067 "cannot define member of dependent typedef "
21068 "%qT", type);
21069 else
21070 error_at (declarator_id_start_token->location,
21071 "%<%T::%E%> is not a type",
21072 TYPE_CONTEXT (qualifying_scope),
21073 TYPE_IDENTIFIER (qualifying_scope));
21074 }
21075 qualifying_scope = type;
21076 }
21077
21078 sfk = sfk_none;
21079
21080 if (unqualified_name)
21081 {
21082 tree class_type;
21083
21084 if (qualifying_scope
21085 && CLASS_TYPE_P (qualifying_scope))
21086 class_type = qualifying_scope;
21087 else
21088 class_type = current_class_type;
21089
21090 if (TREE_CODE (unqualified_name) == TYPE_DECL)
21091 {
21092 tree name_type = TREE_TYPE (unqualified_name);
21093
21094 if (!class_type || !same_type_p (name_type, class_type))
21095 {
21096 /* We do not attempt to print the declarator
21097 here because we do not have enough
21098 information about its original syntactic
21099 form. */
21100 cp_parser_error (parser, "invalid declarator");
21101 declarator = cp_error_declarator;
21102 break;
21103 }
21104 else if (qualifying_scope
21105 && CLASSTYPE_USE_TEMPLATE (name_type))
21106 {
21107 error_at (declarator_id_start_token->location,
21108 "invalid use of constructor as a template");
21109 inform (declarator_id_start_token->location,
21110 "use %<%T::%D%> instead of %<%T::%D%> to "
21111 "name the constructor in a qualified name",
21112 class_type,
21113 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
21114 class_type, name_type);
21115 declarator = cp_error_declarator;
21116 break;
21117 }
21118 unqualified_name = constructor_name (class_type);
21119 }
21120
21121 if (class_type)
21122 {
21123 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
21124 sfk = sfk_destructor;
21125 else if (identifier_p (unqualified_name)
21126 && IDENTIFIER_CONV_OP_P (unqualified_name))
21127 sfk = sfk_conversion;
21128 else if (/* There's no way to declare a constructor
21129 for an unnamed type, even if the type
21130 got a name for linkage purposes. */
21131 !TYPE_WAS_UNNAMED (class_type)
21132 /* Handle correctly (c++/19200):
21133
21134 struct S {
21135 struct T{};
21136 friend void S(T);
21137 };
21138
21139 and also:
21140
21141 namespace N {
21142 void S();
21143 }
21144
21145 struct S {
21146 friend void N::S();
21147 }; */
21148 && (!friend_p || class_type == qualifying_scope)
21149 && constructor_name_p (unqualified_name,
21150 class_type))
21151 sfk = sfk_constructor;
21152 else if (is_overloaded_fn (unqualified_name)
21153 && DECL_CONSTRUCTOR_P (get_first_fn
21154 (unqualified_name)))
21155 sfk = sfk_constructor;
21156
21157 if (ctor_dtor_or_conv_p && sfk != sfk_none)
21158 *ctor_dtor_or_conv_p = -1;
21159 }
21160 }
21161 declarator = make_id_declarator (qualifying_scope,
21162 unqualified_name,
21163 sfk, token->location);
21164 declarator->std_attributes = attrs;
21165 declarator->parameter_pack_p = pack_expansion_p;
21166
21167 if (pack_expansion_p)
21168 maybe_warn_variadic_templates ();
21169
21170 /* We're looking for this case in [temp.res]:
21171 A qualified-id is assumed to name a type if [...]
21172 - it is a decl-specifier of the decl-specifier-seq of a
21173 parameter-declaration in a declarator of a function or
21174 function template declaration, ... */
21175 if (cxx_dialect >= cxx2a
21176 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
21177 && declarator->kind == cdk_id
21178 && !at_class_scope_p ()
21179 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21180 {
21181 /* ...whose declarator-id is qualified. If it isn't, never
21182 assume the parameters to refer to types. */
21183 if (qualifying_scope == NULL_TREE)
21184 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21185 else
21186 {
21187 /* Now we have something like
21188 template <typename T> int C::x(S::p);
21189 which can be a function template declaration or a
21190 variable template definition. If name lookup for
21191 the declarator-id C::x finds one or more function
21192 templates, assume S::p to name a type. Otherwise,
21193 don't. */
21194 tree decl
21195 = cp_parser_lookup_name_simple (parser, unqualified_name,
21196 token->location);
21197 if (!is_overloaded_fn (decl)
21198 /* Allow
21199 template<typename T>
21200 A<T>::A(T::type) { } */
21201 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
21202 && constructor_name_p (unqualified_name,
21203 qualifying_scope)))
21204 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21205 }
21206 }
21207 }
21208
21209 handle_declarator:;
21210 scope = get_scope_of_declarator (declarator);
21211 if (scope)
21212 {
21213 /* Any names that appear after the declarator-id for a
21214 member are looked up in the containing scope. */
21215 if (at_function_scope_p ())
21216 {
21217 /* But declarations with qualified-ids can't appear in a
21218 function. */
21219 cp_parser_error (parser, "qualified-id in declaration");
21220 declarator = cp_error_declarator;
21221 break;
21222 }
21223 pushed_scope = push_scope (scope);
21224 }
21225 parser->in_declarator_p = true;
21226 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21227 || (declarator && declarator->kind == cdk_id))
21228 /* Default args are only allowed on function
21229 declarations. */
21230 parser->default_arg_ok_p = saved_default_arg_ok_p;
21231 else
21232 parser->default_arg_ok_p = false;
21233
21234 first = false;
21235 }
21236 /* We're done. */
21237 else
21238 break;
21239 }
21240
21241 /* For an abstract declarator, we might wind up with nothing at this
21242 point. That's an error; the declarator is not optional. */
21243 if (!declarator)
21244 cp_parser_error (parser, "expected declarator");
21245 else if (open_paren)
21246 {
21247 /* Record overly parenthesized declarator so we can give a
21248 diagnostic about confusing decl/expr disambiguation. */
21249 if (declarator->kind == cdk_array)
21250 {
21251 /* If the open and close parens are on different lines, this
21252 is probably a formatting thing, so ignore. */
21253 expanded_location open = expand_location (open_paren->location);
21254 expanded_location close = expand_location (close_paren->location);
21255 if (open.line != close.line || open.file != close.file)
21256 open_paren = NULL;
21257 }
21258 if (open_paren)
21259 declarator->parenthesized = open_paren->location;
21260 }
21261
21262 /* If we entered a scope, we must exit it now. */
21263 if (pushed_scope)
21264 pop_scope (pushed_scope);
21265
21266 parser->default_arg_ok_p = saved_default_arg_ok_p;
21267 parser->in_declarator_p = saved_in_declarator_p;
21268
21269 return declarator;
21270 }
21271
21272 /* Parse a ptr-operator.
21273
21274 ptr-operator:
21275 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21276 * cv-qualifier-seq [opt]
21277 &
21278 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21279 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21280
21281 GNU Extension:
21282
21283 ptr-operator:
21284 & cv-qualifier-seq [opt]
21285
21286 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21287 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21288 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21289 filled in with the TYPE containing the member. *CV_QUALS is
21290 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21291 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21292 Note that the tree codes returned by this function have nothing
21293 to do with the types of trees that will be eventually be created
21294 to represent the pointer or reference type being parsed. They are
21295 just constants with suggestive names. */
21296 static enum tree_code
21297 cp_parser_ptr_operator (cp_parser* parser,
21298 tree* type,
21299 cp_cv_quals *cv_quals,
21300 tree *attributes)
21301 {
21302 enum tree_code code = ERROR_MARK;
21303 cp_token *token;
21304 tree attrs = NULL_TREE;
21305
21306 /* Assume that it's not a pointer-to-member. */
21307 *type = NULL_TREE;
21308 /* And that there are no cv-qualifiers. */
21309 *cv_quals = TYPE_UNQUALIFIED;
21310
21311 /* Peek at the next token. */
21312 token = cp_lexer_peek_token (parser->lexer);
21313
21314 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21315 if (token->type == CPP_MULT)
21316 code = INDIRECT_REF;
21317 else if (token->type == CPP_AND)
21318 code = ADDR_EXPR;
21319 else if ((cxx_dialect != cxx98) &&
21320 token->type == CPP_AND_AND) /* C++0x only */
21321 code = NON_LVALUE_EXPR;
21322
21323 if (code != ERROR_MARK)
21324 {
21325 /* Consume the `*', `&' or `&&'. */
21326 cp_lexer_consume_token (parser->lexer);
21327
21328 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21329 `&', if we are allowing GNU extensions. (The only qualifier
21330 that can legally appear after `&' is `restrict', but that is
21331 enforced during semantic analysis. */
21332 if (code == INDIRECT_REF
21333 || cp_parser_allow_gnu_extensions_p (parser))
21334 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21335
21336 attrs = cp_parser_std_attribute_spec_seq (parser);
21337 if (attributes != NULL)
21338 *attributes = attrs;
21339 }
21340 else
21341 {
21342 /* Try the pointer-to-member case. */
21343 cp_parser_parse_tentatively (parser);
21344 /* Look for the optional `::' operator. */
21345 cp_parser_global_scope_opt (parser,
21346 /*current_scope_valid_p=*/false);
21347 /* Look for the nested-name specifier. */
21348 token = cp_lexer_peek_token (parser->lexer);
21349 cp_parser_nested_name_specifier (parser,
21350 /*typename_keyword_p=*/false,
21351 /*check_dependency_p=*/true,
21352 /*type_p=*/false,
21353 /*is_declaration=*/false);
21354 /* If we found it, and the next token is a `*', then we are
21355 indeed looking at a pointer-to-member operator. */
21356 if (!cp_parser_error_occurred (parser)
21357 && cp_parser_require (parser, CPP_MULT, RT_MULT))
21358 {
21359 /* Indicate that the `*' operator was used. */
21360 code = INDIRECT_REF;
21361
21362 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21363 error_at (token->location, "%qD is a namespace", parser->scope);
21364 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21365 error_at (token->location, "cannot form pointer to member of "
21366 "non-class %q#T", parser->scope);
21367 else
21368 {
21369 /* The type of which the member is a member is given by the
21370 current SCOPE. */
21371 *type = parser->scope;
21372 /* The next name will not be qualified. */
21373 parser->scope = NULL_TREE;
21374 parser->qualifying_scope = NULL_TREE;
21375 parser->object_scope = NULL_TREE;
21376 /* Look for optional c++11 attributes. */
21377 attrs = cp_parser_std_attribute_spec_seq (parser);
21378 if (attributes != NULL)
21379 *attributes = attrs;
21380 /* Look for the optional cv-qualifier-seq. */
21381 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21382 }
21383 }
21384 /* If that didn't work we don't have a ptr-operator. */
21385 if (!cp_parser_parse_definitely (parser))
21386 cp_parser_error (parser, "expected ptr-operator");
21387 }
21388
21389 return code;
21390 }
21391
21392 /* Parse an (optional) cv-qualifier-seq.
21393
21394 cv-qualifier-seq:
21395 cv-qualifier cv-qualifier-seq [opt]
21396
21397 cv-qualifier:
21398 const
21399 volatile
21400
21401 GNU Extension:
21402
21403 cv-qualifier:
21404 __restrict__
21405
21406 Returns a bitmask representing the cv-qualifiers. */
21407
21408 static cp_cv_quals
21409 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21410 {
21411 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21412
21413 while (true)
21414 {
21415 cp_token *token;
21416 cp_cv_quals cv_qualifier;
21417
21418 /* Peek at the next token. */
21419 token = cp_lexer_peek_token (parser->lexer);
21420 /* See if it's a cv-qualifier. */
21421 switch (token->keyword)
21422 {
21423 case RID_CONST:
21424 cv_qualifier = TYPE_QUAL_CONST;
21425 break;
21426
21427 case RID_VOLATILE:
21428 cv_qualifier = TYPE_QUAL_VOLATILE;
21429 break;
21430
21431 case RID_RESTRICT:
21432 cv_qualifier = TYPE_QUAL_RESTRICT;
21433 break;
21434
21435 default:
21436 cv_qualifier = TYPE_UNQUALIFIED;
21437 break;
21438 }
21439
21440 if (!cv_qualifier)
21441 break;
21442
21443 if (cv_quals & cv_qualifier)
21444 {
21445 gcc_rich_location richloc (token->location);
21446 richloc.add_fixit_remove ();
21447 error_at (&richloc, "duplicate cv-qualifier");
21448 cp_lexer_purge_token (parser->lexer);
21449 }
21450 else
21451 {
21452 cp_lexer_consume_token (parser->lexer);
21453 cv_quals |= cv_qualifier;
21454 }
21455 }
21456
21457 return cv_quals;
21458 }
21459
21460 /* Parse an (optional) ref-qualifier
21461
21462 ref-qualifier:
21463 &
21464 &&
21465
21466 Returns cp_ref_qualifier representing ref-qualifier. */
21467
21468 static cp_ref_qualifier
21469 cp_parser_ref_qualifier_opt (cp_parser* parser)
21470 {
21471 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21472
21473 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21474 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21475 return ref_qual;
21476
21477 while (true)
21478 {
21479 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21480 cp_token *token = cp_lexer_peek_token (parser->lexer);
21481
21482 switch (token->type)
21483 {
21484 case CPP_AND:
21485 curr_ref_qual = REF_QUAL_LVALUE;
21486 break;
21487
21488 case CPP_AND_AND:
21489 curr_ref_qual = REF_QUAL_RVALUE;
21490 break;
21491
21492 default:
21493 curr_ref_qual = REF_QUAL_NONE;
21494 break;
21495 }
21496
21497 if (!curr_ref_qual)
21498 break;
21499 else if (ref_qual)
21500 {
21501 error_at (token->location, "multiple ref-qualifiers");
21502 cp_lexer_purge_token (parser->lexer);
21503 }
21504 else
21505 {
21506 ref_qual = curr_ref_qual;
21507 cp_lexer_consume_token (parser->lexer);
21508 }
21509 }
21510
21511 return ref_qual;
21512 }
21513
21514 /* Parse an optional tx-qualifier.
21515
21516 tx-qualifier:
21517 transaction_safe
21518 transaction_safe_dynamic */
21519
21520 static tree
21521 cp_parser_tx_qualifier_opt (cp_parser *parser)
21522 {
21523 cp_token *token = cp_lexer_peek_token (parser->lexer);
21524 if (token->type == CPP_NAME)
21525 {
21526 tree name = token->u.value;
21527 const char *p = IDENTIFIER_POINTER (name);
21528 const int len = strlen ("transaction_safe");
21529 if (!strncmp (p, "transaction_safe", len))
21530 {
21531 p += len;
21532 if (*p == '\0'
21533 || !strcmp (p, "_dynamic"))
21534 {
21535 cp_lexer_consume_token (parser->lexer);
21536 if (!flag_tm)
21537 {
21538 error ("%qE requires %<-fgnu-tm%>", name);
21539 return NULL_TREE;
21540 }
21541 else
21542 return name;
21543 }
21544 }
21545 }
21546 return NULL_TREE;
21547 }
21548
21549 /* Parse an (optional) virt-specifier-seq.
21550
21551 virt-specifier-seq:
21552 virt-specifier virt-specifier-seq [opt]
21553
21554 virt-specifier:
21555 override
21556 final
21557
21558 Returns a bitmask representing the virt-specifiers. */
21559
21560 static cp_virt_specifiers
21561 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21562 {
21563 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21564
21565 while (true)
21566 {
21567 cp_token *token;
21568 cp_virt_specifiers virt_specifier;
21569
21570 /* Peek at the next token. */
21571 token = cp_lexer_peek_token (parser->lexer);
21572 /* See if it's a virt-specifier-qualifier. */
21573 if (token->type != CPP_NAME)
21574 break;
21575 if (id_equal (token->u.value, "override"))
21576 {
21577 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21578 virt_specifier = VIRT_SPEC_OVERRIDE;
21579 }
21580 else if (id_equal (token->u.value, "final"))
21581 {
21582 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21583 virt_specifier = VIRT_SPEC_FINAL;
21584 }
21585 else if (id_equal (token->u.value, "__final"))
21586 {
21587 virt_specifier = VIRT_SPEC_FINAL;
21588 }
21589 else
21590 break;
21591
21592 if (virt_specifiers & virt_specifier)
21593 {
21594 gcc_rich_location richloc (token->location);
21595 richloc.add_fixit_remove ();
21596 error_at (&richloc, "duplicate virt-specifier");
21597 cp_lexer_purge_token (parser->lexer);
21598 }
21599 else
21600 {
21601 cp_lexer_consume_token (parser->lexer);
21602 virt_specifiers |= virt_specifier;
21603 }
21604 }
21605 return virt_specifiers;
21606 }
21607
21608 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21609 is in scope even though it isn't real. */
21610
21611 void
21612 inject_this_parameter (tree ctype, cp_cv_quals quals)
21613 {
21614 tree this_parm;
21615
21616 if (current_class_ptr)
21617 {
21618 /* We don't clear this between NSDMIs. Is it already what we want? */
21619 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21620 if (DECL_P (current_class_ptr)
21621 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21622 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21623 && cp_type_quals (type) == quals)
21624 return;
21625 }
21626
21627 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21628 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21629 current_class_ptr = NULL_TREE;
21630 current_class_ref
21631 = cp_build_fold_indirect_ref (this_parm);
21632 current_class_ptr = this_parm;
21633 }
21634
21635 /* Return true iff our current scope is a non-static data member
21636 initializer. */
21637
21638 bool
21639 parsing_nsdmi (void)
21640 {
21641 /* We recognize NSDMI context by the context-less 'this' pointer set up
21642 by the function above. */
21643 if (current_class_ptr
21644 && TREE_CODE (current_class_ptr) == PARM_DECL
21645 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21646 return true;
21647 return false;
21648 }
21649
21650 /* Parse a late-specified return type, if any. This is not a separate
21651 non-terminal, but part of a function declarator, which looks like
21652
21653 -> trailing-type-specifier-seq abstract-declarator(opt)
21654
21655 Returns the type indicated by the type-id.
21656
21657 In addition to this, parse any queued up #pragma omp declare simd
21658 clauses, and #pragma acc routine clauses.
21659
21660 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21661 function. */
21662
21663 static tree
21664 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21665 tree& requires_clause, cp_cv_quals quals)
21666 {
21667 cp_token *token;
21668 tree type = NULL_TREE;
21669 bool declare_simd_p = (parser->omp_declare_simd
21670 && declarator
21671 && declarator->kind == cdk_id);
21672
21673 bool oacc_routine_p = (parser->oacc_routine
21674 && declarator
21675 && declarator->kind == cdk_id);
21676
21677 /* Peek at the next token. */
21678 token = cp_lexer_peek_token (parser->lexer);
21679 /* A late-specified return type is indicated by an initial '->'. */
21680 if (token->type != CPP_DEREF
21681 && token->keyword != RID_REQUIRES
21682 && !(token->type == CPP_NAME
21683 && token->u.value == ridpointers[RID_REQUIRES])
21684 && !(declare_simd_p || oacc_routine_p))
21685 return NULL_TREE;
21686
21687 tree save_ccp = current_class_ptr;
21688 tree save_ccr = current_class_ref;
21689 if (quals >= 0)
21690 {
21691 /* DR 1207: 'this' is in scope in the trailing return type. */
21692 inject_this_parameter (current_class_type, quals);
21693 }
21694
21695 if (token->type == CPP_DEREF)
21696 {
21697 /* Consume the ->. */
21698 cp_lexer_consume_token (parser->lexer);
21699
21700 type = cp_parser_trailing_type_id (parser);
21701 }
21702
21703 /* Function declarations may be followed by a trailing
21704 requires-clause. */
21705 requires_clause = cp_parser_requires_clause_opt (parser);
21706
21707 if (declare_simd_p)
21708 declarator->attributes
21709 = cp_parser_late_parsing_omp_declare_simd (parser,
21710 declarator->attributes);
21711 if (oacc_routine_p)
21712 declarator->attributes
21713 = cp_parser_late_parsing_oacc_routine (parser,
21714 declarator->attributes);
21715
21716 if (quals >= 0)
21717 {
21718 current_class_ptr = save_ccp;
21719 current_class_ref = save_ccr;
21720 }
21721
21722 return type;
21723 }
21724
21725 /* Parse a declarator-id.
21726
21727 declarator-id:
21728 id-expression
21729 :: [opt] nested-name-specifier [opt] type-name
21730
21731 In the `id-expression' case, the value returned is as for
21732 cp_parser_id_expression if the id-expression was an unqualified-id.
21733 If the id-expression was a qualified-id, then a SCOPE_REF is
21734 returned. The first operand is the scope (either a NAMESPACE_DECL
21735 or TREE_TYPE), but the second is still just a representation of an
21736 unqualified-id. */
21737
21738 static tree
21739 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21740 {
21741 tree id;
21742 /* The expression must be an id-expression. Assume that qualified
21743 names are the names of types so that:
21744
21745 template <class T>
21746 int S<T>::R::i = 3;
21747
21748 will work; we must treat `S<T>::R' as the name of a type.
21749 Similarly, assume that qualified names are templates, where
21750 required, so that:
21751
21752 template <class T>
21753 int S<T>::R<T>::i = 3;
21754
21755 will work, too. */
21756 id = cp_parser_id_expression (parser,
21757 /*template_keyword_p=*/false,
21758 /*check_dependency_p=*/false,
21759 /*template_p=*/NULL,
21760 /*declarator_p=*/true,
21761 optional_p);
21762 if (id && BASELINK_P (id))
21763 id = BASELINK_FUNCTIONS (id);
21764 return id;
21765 }
21766
21767 /* Parse a type-id.
21768
21769 type-id:
21770 type-specifier-seq abstract-declarator [opt]
21771
21772 The parser flags FLAGS is used to control type-specifier parsing.
21773
21774 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21775
21776 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21777 i.e. we've just seen "->".
21778
21779 Returns the TYPE specified. */
21780
21781 static tree
21782 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
21783 bool is_template_arg, bool is_trailing_return,
21784 location_t *type_location)
21785 {
21786 cp_decl_specifier_seq type_specifier_seq;
21787 cp_declarator *abstract_declarator;
21788
21789 /* Parse the type-specifier-seq. */
21790 cp_parser_type_specifier_seq (parser, flags,
21791 /*is_declaration=*/false,
21792 is_trailing_return,
21793 &type_specifier_seq);
21794 if (type_location)
21795 *type_location = type_specifier_seq.locations[ds_type_spec];
21796
21797 if (is_template_arg && type_specifier_seq.type
21798 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21799 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21800 /* A bare template name as a template argument is a template template
21801 argument, not a placeholder, so fail parsing it as a type argument. */
21802 {
21803 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21804 cp_parser_simulate_error (parser);
21805 return error_mark_node;
21806 }
21807 if (type_specifier_seq.type == error_mark_node)
21808 return error_mark_node;
21809
21810 /* There might or might not be an abstract declarator. */
21811 cp_parser_parse_tentatively (parser);
21812 /* Look for the declarator. */
21813 abstract_declarator
21814 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
21815 CP_PARSER_FLAGS_NONE, NULL,
21816 /*parenthesized_p=*/NULL,
21817 /*member_p=*/false,
21818 /*friend_p=*/false,
21819 /*static_p=*/false);
21820 /* Check to see if there really was a declarator. */
21821 if (!cp_parser_parse_definitely (parser))
21822 abstract_declarator = NULL;
21823
21824 if (type_specifier_seq.type
21825 /* The concepts TS allows 'auto' as a type-id. */
21826 && (!flag_concepts || parser->in_type_id_in_expr_p)
21827 /* None of the valid uses of 'auto' in C++14 involve the type-id
21828 nonterminal, but it is valid in a trailing-return-type. */
21829 && !(cxx_dialect >= cxx14 && is_trailing_return))
21830 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21831 {
21832 /* A type-id with type 'auto' is only ok if the abstract declarator
21833 is a function declarator with a late-specified return type.
21834
21835 A type-id with 'auto' is also valid in a trailing-return-type
21836 in a compound-requirement. */
21837 if (abstract_declarator
21838 && abstract_declarator->kind == cdk_function
21839 && abstract_declarator->u.function.late_return_type)
21840 /* OK */;
21841 else if (parser->in_result_type_constraint_p)
21842 /* OK */;
21843 else
21844 {
21845 location_t loc = type_specifier_seq.locations[ds_type_spec];
21846 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21847 {
21848 error_at (loc, "missing template arguments after %qT",
21849 auto_node);
21850 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21851 tmpl);
21852 }
21853 else
21854 error_at (loc, "invalid use of %qT", auto_node);
21855 return error_mark_node;
21856 }
21857 }
21858
21859 return groktypename (&type_specifier_seq, abstract_declarator,
21860 is_template_arg);
21861 }
21862
21863 /* Wrapper for cp_parser_type_id_1. */
21864
21865 static tree
21866 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
21867 location_t *type_location)
21868 {
21869 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
21870 }
21871
21872 /* Wrapper for cp_parser_type_id_1. */
21873
21874 static tree
21875 cp_parser_template_type_arg (cp_parser *parser)
21876 {
21877 tree r;
21878 const char *saved_message = parser->type_definition_forbidden_message;
21879 parser->type_definition_forbidden_message
21880 = G_("types may not be defined in template arguments");
21881 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
21882 parser->type_definition_forbidden_message = saved_message;
21883 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21884 {
21885 error ("invalid use of %<auto%> in template argument");
21886 r = error_mark_node;
21887 }
21888 return r;
21889 }
21890
21891 /* Wrapper for cp_parser_type_id_1. */
21892
21893 static tree
21894 cp_parser_trailing_type_id (cp_parser *parser)
21895 {
21896 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21897 false, true, NULL);
21898 }
21899
21900 /* Parse a type-specifier-seq.
21901
21902 type-specifier-seq:
21903 type-specifier type-specifier-seq [opt]
21904
21905 GNU extension:
21906
21907 type-specifier-seq:
21908 attributes type-specifier-seq [opt]
21909
21910 The parser flags FLAGS is used to control type-specifier parsing.
21911
21912 If IS_DECLARATION is true, we are at the start of a "condition" or
21913 exception-declaration, so we might be followed by a declarator-id.
21914
21915 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21916 i.e. we've just seen "->".
21917
21918 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21919
21920 static void
21921 cp_parser_type_specifier_seq (cp_parser* parser,
21922 cp_parser_flags flags,
21923 bool is_declaration,
21924 bool is_trailing_return,
21925 cp_decl_specifier_seq *type_specifier_seq)
21926 {
21927 bool seen_type_specifier = false;
21928 cp_token *start_token = NULL;
21929
21930 /* Clear the TYPE_SPECIFIER_SEQ. */
21931 clear_decl_specs (type_specifier_seq);
21932
21933 flags |= CP_PARSER_FLAGS_OPTIONAL;
21934 /* In the context of a trailing return type, enum E { } is an
21935 elaborated-type-specifier followed by a function-body, not an
21936 enum-specifier. */
21937 if (is_trailing_return)
21938 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21939
21940 /* Parse the type-specifiers and attributes. */
21941 while (true)
21942 {
21943 tree type_specifier;
21944 bool is_cv_qualifier;
21945
21946 /* Check for attributes first. */
21947 if (cp_next_tokens_can_be_attribute_p (parser))
21948 {
21949 type_specifier_seq->attributes
21950 = attr_chainon (type_specifier_seq->attributes,
21951 cp_parser_attributes_opt (parser));
21952 continue;
21953 }
21954
21955 /* record the token of the beginning of the type specifier seq,
21956 for error reporting purposes*/
21957 if (!start_token)
21958 start_token = cp_lexer_peek_token (parser->lexer);
21959
21960 /* Look for the type-specifier. */
21961 type_specifier = cp_parser_type_specifier (parser,
21962 flags,
21963 type_specifier_seq,
21964 /*is_declaration=*/false,
21965 NULL,
21966 &is_cv_qualifier);
21967 if (!type_specifier)
21968 {
21969 /* If the first type-specifier could not be found, this is not a
21970 type-specifier-seq at all. */
21971 if (!seen_type_specifier)
21972 {
21973 /* Set in_declarator_p to avoid skipping to the semicolon. */
21974 int in_decl = parser->in_declarator_p;
21975 parser->in_declarator_p = true;
21976
21977 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21978 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21979 cp_parser_error (parser, "expected type-specifier");
21980
21981 parser->in_declarator_p = in_decl;
21982
21983 type_specifier_seq->type = error_mark_node;
21984 return;
21985 }
21986 /* If subsequent type-specifiers could not be found, the
21987 type-specifier-seq is complete. */
21988 break;
21989 }
21990
21991 seen_type_specifier = true;
21992 /* The standard says that a condition can be:
21993
21994 type-specifier-seq declarator = assignment-expression
21995
21996 However, given:
21997
21998 struct S {};
21999 if (int S = ...)
22000
22001 we should treat the "S" as a declarator, not as a
22002 type-specifier. The standard doesn't say that explicitly for
22003 type-specifier-seq, but it does say that for
22004 decl-specifier-seq in an ordinary declaration. Perhaps it
22005 would be clearer just to allow a decl-specifier-seq here, and
22006 then add a semantic restriction that if any decl-specifiers
22007 that are not type-specifiers appear, the program is invalid. */
22008 if (is_declaration && !is_cv_qualifier)
22009 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
22010 }
22011 }
22012
22013 /* Return whether the function currently being declared has an associated
22014 template parameter list. */
22015
22016 static bool
22017 function_being_declared_is_template_p (cp_parser* parser)
22018 {
22019 if (!current_template_parms || processing_template_parmlist)
22020 return false;
22021
22022 if (parser->implicit_template_scope)
22023 return true;
22024
22025 if (at_class_scope_p ()
22026 && TYPE_BEING_DEFINED (current_class_type))
22027 return parser->num_template_parameter_lists != 0;
22028
22029 return ((int) parser->num_template_parameter_lists > template_class_depth
22030 (current_class_type));
22031 }
22032
22033 /* Parse a parameter-declaration-clause.
22034
22035 parameter-declaration-clause:
22036 parameter-declaration-list [opt] ... [opt]
22037 parameter-declaration-list , ...
22038
22039 The parser flags FLAGS is used to control type-specifier parsing.
22040
22041 Returns a representation for the parameter declarations. A return
22042 value of NULL indicates a parameter-declaration-clause consisting
22043 only of an ellipsis. */
22044
22045 static tree
22046 cp_parser_parameter_declaration_clause (cp_parser* parser,
22047 cp_parser_flags flags)
22048 {
22049 tree parameters;
22050 cp_token *token;
22051 bool ellipsis_p;
22052
22053 temp_override<bool> cleanup
22054 (parser->auto_is_implicit_function_template_parm_p);
22055
22056 if (!processing_specialization
22057 && !processing_template_parmlist
22058 && !processing_explicit_instantiation
22059 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22060 actual function or a random abstract declarator. */
22061 && parser->default_arg_ok_p)
22062 if (!current_function_decl
22063 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
22064 parser->auto_is_implicit_function_template_parm_p = true;
22065
22066 /* Peek at the next token. */
22067 token = cp_lexer_peek_token (parser->lexer);
22068 /* Check for trivial parameter-declaration-clauses. */
22069 if (token->type == CPP_ELLIPSIS)
22070 {
22071 /* Consume the `...' token. */
22072 cp_lexer_consume_token (parser->lexer);
22073 return NULL_TREE;
22074 }
22075 else if (token->type == CPP_CLOSE_PAREN)
22076 /* There are no parameters. */
22077 return void_list_node;
22078 /* Check for `(void)', too, which is a special case. */
22079 else if (token->keyword == RID_VOID
22080 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22081 == CPP_CLOSE_PAREN))
22082 {
22083 /* Consume the `void' token. */
22084 cp_lexer_consume_token (parser->lexer);
22085 /* There are no parameters. */
22086 return void_list_node;
22087 }
22088
22089 /* Parse the parameter-declaration-list. */
22090 parameters = cp_parser_parameter_declaration_list (parser, flags);
22091 /* If a parse error occurred while parsing the
22092 parameter-declaration-list, then the entire
22093 parameter-declaration-clause is erroneous. */
22094 if (parameters == error_mark_node)
22095 return NULL_TREE;
22096
22097 /* Peek at the next token. */
22098 token = cp_lexer_peek_token (parser->lexer);
22099 /* If it's a `,', the clause should terminate with an ellipsis. */
22100 if (token->type == CPP_COMMA)
22101 {
22102 /* Consume the `,'. */
22103 cp_lexer_consume_token (parser->lexer);
22104 /* Expect an ellipsis. */
22105 ellipsis_p
22106 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
22107 }
22108 /* It might also be `...' if the optional trailing `,' was
22109 omitted. */
22110 else if (token->type == CPP_ELLIPSIS)
22111 {
22112 /* Consume the `...' token. */
22113 cp_lexer_consume_token (parser->lexer);
22114 /* And remember that we saw it. */
22115 ellipsis_p = true;
22116 }
22117 else
22118 ellipsis_p = false;
22119
22120 /* Finish the parameter list. */
22121 if (!ellipsis_p)
22122 parameters = chainon (parameters, void_list_node);
22123
22124 return parameters;
22125 }
22126
22127 /* Parse a parameter-declaration-list.
22128
22129 parameter-declaration-list:
22130 parameter-declaration
22131 parameter-declaration-list , parameter-declaration
22132
22133 The parser flags FLAGS is used to control type-specifier parsing.
22134
22135 Returns a representation of the parameter-declaration-list, as for
22136 cp_parser_parameter_declaration_clause. However, the
22137 `void_list_node' is never appended to the list. */
22138
22139 static tree
22140 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
22141 {
22142 tree parameters = NULL_TREE;
22143 tree *tail = &parameters;
22144 bool saved_in_unbraced_linkage_specification_p;
22145 int index = 0;
22146
22147 /* The special considerations that apply to a function within an
22148 unbraced linkage specifications do not apply to the parameters
22149 to the function. */
22150 saved_in_unbraced_linkage_specification_p
22151 = parser->in_unbraced_linkage_specification_p;
22152 parser->in_unbraced_linkage_specification_p = false;
22153
22154 /* Look for more parameters. */
22155 while (true)
22156 {
22157 cp_parameter_declarator *parameter;
22158 tree decl = error_mark_node;
22159 bool parenthesized_p = false;
22160
22161 /* Parse the parameter. */
22162 parameter
22163 = cp_parser_parameter_declaration (parser, flags,
22164 /*template_parm_p=*/false,
22165 &parenthesized_p);
22166
22167 /* We don't know yet if the enclosing context is deprecated, so wait
22168 and warn in grokparms if appropriate. */
22169 deprecated_state = DEPRECATED_SUPPRESS;
22170
22171 if (parameter)
22172 {
22173 decl = grokdeclarator (parameter->declarator,
22174 &parameter->decl_specifiers,
22175 PARM,
22176 parameter->default_argument != NULL_TREE,
22177 &parameter->decl_specifiers.attributes);
22178 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
22179 DECL_SOURCE_LOCATION (decl) = parameter->loc;
22180 }
22181
22182 deprecated_state = DEPRECATED_NORMAL;
22183
22184 /* If a parse error occurred parsing the parameter declaration,
22185 then the entire parameter-declaration-list is erroneous. */
22186 if (decl == error_mark_node)
22187 {
22188 parameters = error_mark_node;
22189 break;
22190 }
22191
22192 if (parameter->decl_specifiers.attributes)
22193 cplus_decl_attributes (&decl,
22194 parameter->decl_specifiers.attributes,
22195 0);
22196 if (DECL_NAME (decl))
22197 decl = pushdecl (decl);
22198
22199 if (decl != error_mark_node)
22200 {
22201 retrofit_lang_decl (decl);
22202 DECL_PARM_INDEX (decl) = ++index;
22203 DECL_PARM_LEVEL (decl) = function_parm_depth ();
22204 }
22205
22206 /* Add the new parameter to the list. */
22207 *tail = build_tree_list (parameter->default_argument, decl);
22208 tail = &TREE_CHAIN (*tail);
22209
22210 /* Peek at the next token. */
22211 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22212 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22213 /* These are for Objective-C++ */
22214 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22215 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22216 /* The parameter-declaration-list is complete. */
22217 break;
22218 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22219 {
22220 cp_token *token;
22221
22222 /* Peek at the next token. */
22223 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22224 /* If it's an ellipsis, then the list is complete. */
22225 if (token->type == CPP_ELLIPSIS)
22226 break;
22227 /* Otherwise, there must be more parameters. Consume the
22228 `,'. */
22229 cp_lexer_consume_token (parser->lexer);
22230 /* When parsing something like:
22231
22232 int i(float f, double d)
22233
22234 we can tell after seeing the declaration for "f" that we
22235 are not looking at an initialization of a variable "i",
22236 but rather at the declaration of a function "i".
22237
22238 Due to the fact that the parsing of template arguments
22239 (as specified to a template-id) requires backtracking we
22240 cannot use this technique when inside a template argument
22241 list. */
22242 if (!parser->in_template_argument_list_p
22243 && !parser->in_type_id_in_expr_p
22244 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22245 /* However, a parameter-declaration of the form
22246 "float(f)" (which is a valid declaration of a
22247 parameter "f") can also be interpreted as an
22248 expression (the conversion of "f" to "float"). */
22249 && !parenthesized_p)
22250 cp_parser_commit_to_tentative_parse (parser);
22251 }
22252 else
22253 {
22254 cp_parser_error (parser, "expected %<,%> or %<...%>");
22255 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22256 cp_parser_skip_to_closing_parenthesis (parser,
22257 /*recovering=*/true,
22258 /*or_comma=*/false,
22259 /*consume_paren=*/false);
22260 break;
22261 }
22262 }
22263
22264 parser->in_unbraced_linkage_specification_p
22265 = saved_in_unbraced_linkage_specification_p;
22266
22267 /* Reset implicit_template_scope if we are about to leave the function
22268 parameter list that introduced it. Note that for out-of-line member
22269 definitions, there will be one or more class scopes before we get to
22270 the template parameter scope. */
22271
22272 if (cp_binding_level *its = parser->implicit_template_scope)
22273 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
22274 {
22275 while (maybe_its->kind == sk_class)
22276 maybe_its = maybe_its->level_chain;
22277 if (maybe_its == its)
22278 {
22279 parser->implicit_template_parms = 0;
22280 parser->implicit_template_scope = 0;
22281 }
22282 }
22283
22284 return parameters;
22285 }
22286
22287 /* Parse a parameter declaration.
22288
22289 parameter-declaration:
22290 decl-specifier-seq ... [opt] declarator
22291 decl-specifier-seq declarator = assignment-expression
22292 decl-specifier-seq ... [opt] abstract-declarator [opt]
22293 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22294
22295 The parser flags FLAGS is used to control type-specifier parsing.
22296
22297 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22298 declares a template parameter. (In that case, a non-nested `>'
22299 token encountered during the parsing of the assignment-expression
22300 is not interpreted as a greater-than operator.)
22301
22302 Returns a representation of the parameter, or NULL if an error
22303 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22304 true iff the declarator is of the form "(p)". */
22305
22306 static cp_parameter_declarator *
22307 cp_parser_parameter_declaration (cp_parser *parser,
22308 cp_parser_flags flags,
22309 bool template_parm_p,
22310 bool *parenthesized_p)
22311 {
22312 int declares_class_or_enum;
22313 cp_decl_specifier_seq decl_specifiers;
22314 cp_declarator *declarator;
22315 tree default_argument;
22316 cp_token *token = NULL, *declarator_token_start = NULL;
22317 const char *saved_message;
22318 bool template_parameter_pack_p = false;
22319
22320 /* In a template parameter, `>' is not an operator.
22321
22322 [temp.param]
22323
22324 When parsing a default template-argument for a non-type
22325 template-parameter, the first non-nested `>' is taken as the end
22326 of the template parameter-list rather than a greater-than
22327 operator. */
22328
22329 /* Type definitions may not appear in parameter types. */
22330 saved_message = parser->type_definition_forbidden_message;
22331 parser->type_definition_forbidden_message
22332 = G_("types may not be defined in parameter types");
22333
22334 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
22335 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22336 (current_template_parms)) : 0);
22337
22338 /* Parse the declaration-specifiers. */
22339 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22340 cp_parser_decl_specifier_seq (parser,
22341 flags,
22342 &decl_specifiers,
22343 &declares_class_or_enum);
22344
22345 /* Complain about missing 'typename' or other invalid type names. */
22346 if (!decl_specifiers.any_type_specifiers_p
22347 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22348 decl_specifiers.type = error_mark_node;
22349
22350 /* If an error occurred, there's no reason to attempt to parse the
22351 rest of the declaration. */
22352 if (cp_parser_error_occurred (parser))
22353 {
22354 parser->type_definition_forbidden_message = saved_message;
22355 return NULL;
22356 }
22357
22358 /* Peek at the next token. */
22359 token = cp_lexer_peek_token (parser->lexer);
22360
22361 /* If the next token is a `)', `,', `=', `>', or `...', then there
22362 is no declarator. However, when variadic templates are enabled,
22363 there may be a declarator following `...'. */
22364 if (token->type == CPP_CLOSE_PAREN
22365 || token->type == CPP_COMMA
22366 || token->type == CPP_EQ
22367 || token->type == CPP_GREATER)
22368 {
22369 declarator = NULL;
22370 if (parenthesized_p)
22371 *parenthesized_p = false;
22372 }
22373 /* Otherwise, there should be a declarator. */
22374 else
22375 {
22376 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22377 parser->default_arg_ok_p = false;
22378
22379 /* After seeing a decl-specifier-seq, if the next token is not a
22380 "(", there is no possibility that the code is a valid
22381 expression. Therefore, if parsing tentatively, we commit at
22382 this point. */
22383 if (!parser->in_template_argument_list_p
22384 /* In an expression context, having seen:
22385
22386 (int((char ...
22387
22388 we cannot be sure whether we are looking at a
22389 function-type (taking a "char" as a parameter) or a cast
22390 of some object of type "char" to "int". */
22391 && !parser->in_type_id_in_expr_p
22392 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22393 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22394 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22395 cp_parser_commit_to_tentative_parse (parser);
22396 /* Parse the declarator. */
22397 declarator_token_start = token;
22398 declarator = cp_parser_declarator (parser,
22399 CP_PARSER_DECLARATOR_EITHER,
22400 CP_PARSER_FLAGS_NONE,
22401 /*ctor_dtor_or_conv_p=*/NULL,
22402 parenthesized_p,
22403 /*member_p=*/false,
22404 /*friend_p=*/false,
22405 /*static_p=*/false);
22406 parser->default_arg_ok_p = saved_default_arg_ok_p;
22407 /* After the declarator, allow more attributes. */
22408 decl_specifiers.attributes
22409 = attr_chainon (decl_specifiers.attributes,
22410 cp_parser_attributes_opt (parser));
22411
22412 /* If the declarator is a template parameter pack, remember that and
22413 clear the flag in the declarator itself so we don't get errors
22414 from grokdeclarator. */
22415 if (template_parm_p && declarator && declarator->parameter_pack_p)
22416 {
22417 declarator->parameter_pack_p = false;
22418 template_parameter_pack_p = true;
22419 }
22420 }
22421
22422 /* If the next token is an ellipsis, and we have not seen a declarator
22423 name, and if either the type of the declarator contains parameter
22424 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22425 for, eg, abbreviated integral type names), then we actually have a
22426 parameter pack expansion expression. Otherwise, leave the ellipsis
22427 for a C-style variadic function. */
22428 token = cp_lexer_peek_token (parser->lexer);
22429
22430 /* If a function parameter pack was specified and an implicit template
22431 parameter was introduced during cp_parser_parameter_declaration,
22432 change any implicit parameters introduced into packs. */
22433 if (parser->implicit_template_parms
22434 && ((token->type == CPP_ELLIPSIS
22435 && declarator_can_be_parameter_pack (declarator))
22436 || (declarator && declarator->parameter_pack_p)))
22437 {
22438 int latest_template_parm_idx = TREE_VEC_LENGTH
22439 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
22440
22441 if (latest_template_parm_idx != template_parm_idx)
22442 decl_specifiers.type = convert_generic_types_to_packs
22443 (decl_specifiers.type,
22444 template_parm_idx, latest_template_parm_idx);
22445 }
22446
22447 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22448 {
22449 tree type = decl_specifiers.type;
22450
22451 if (type && DECL_P (type))
22452 type = TREE_TYPE (type);
22453
22454 if (((type
22455 && TREE_CODE (type) != TYPE_PACK_EXPANSION
22456 && (template_parm_p || uses_parameter_packs (type)))
22457 || (!type && template_parm_p))
22458 && declarator_can_be_parameter_pack (declarator))
22459 {
22460 /* Consume the `...'. */
22461 cp_lexer_consume_token (parser->lexer);
22462 maybe_warn_variadic_templates ();
22463
22464 /* Build a pack expansion type */
22465 if (template_parm_p)
22466 template_parameter_pack_p = true;
22467 else if (declarator)
22468 declarator->parameter_pack_p = true;
22469 else
22470 decl_specifiers.type = make_pack_expansion (type);
22471 }
22472 }
22473
22474 /* The restriction on defining new types applies only to the type
22475 of the parameter, not to the default argument. */
22476 parser->type_definition_forbidden_message = saved_message;
22477
22478 /* If the next token is `=', then process a default argument. */
22479 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22480 {
22481 tree type = decl_specifiers.type;
22482 token = cp_lexer_peek_token (parser->lexer);
22483 /* If we are defining a class, then the tokens that make up the
22484 default argument must be saved and processed later. */
22485 if (!template_parm_p && at_class_scope_p ()
22486 && TYPE_BEING_DEFINED (current_class_type)
22487 && !LAMBDA_TYPE_P (current_class_type))
22488 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22489
22490 // A constrained-type-specifier may declare a type template-parameter.
22491 else if (declares_constrained_type_template_parameter (type))
22492 default_argument
22493 = cp_parser_default_type_template_argument (parser);
22494
22495 // A constrained-type-specifier may declare a template-template-parameter.
22496 else if (declares_constrained_template_template_parameter (type))
22497 default_argument
22498 = cp_parser_default_template_template_argument (parser);
22499
22500 /* Outside of a class definition, we can just parse the
22501 assignment-expression. */
22502 else
22503 default_argument
22504 = cp_parser_default_argument (parser, template_parm_p);
22505
22506 if (!parser->default_arg_ok_p)
22507 {
22508 permerror (token->location,
22509 "default arguments are only "
22510 "permitted for function parameters");
22511 }
22512 else if ((declarator && declarator->parameter_pack_p)
22513 || template_parameter_pack_p
22514 || (decl_specifiers.type
22515 && PACK_EXPANSION_P (decl_specifiers.type)))
22516 {
22517 /* Find the name of the parameter pack. */
22518 cp_declarator *id_declarator = declarator;
22519 while (id_declarator && id_declarator->kind != cdk_id)
22520 id_declarator = id_declarator->declarator;
22521
22522 if (id_declarator && id_declarator->kind == cdk_id)
22523 error_at (declarator_token_start->location,
22524 template_parm_p
22525 ? G_("template parameter pack %qD "
22526 "cannot have a default argument")
22527 : G_("parameter pack %qD cannot have "
22528 "a default argument"),
22529 id_declarator->u.id.unqualified_name);
22530 else
22531 error_at (declarator_token_start->location,
22532 template_parm_p
22533 ? G_("template parameter pack cannot have "
22534 "a default argument")
22535 : G_("parameter pack cannot have a "
22536 "default argument"));
22537
22538 default_argument = NULL_TREE;
22539 }
22540 }
22541 else
22542 default_argument = NULL_TREE;
22543
22544 if (default_argument)
22545 STRIP_ANY_LOCATION_WRAPPER (default_argument);
22546
22547 /* Generate a location for the parameter, ranging from the start of the
22548 initial token to the end of the final token (using input_location for
22549 the latter, set up by cp_lexer_set_source_position_from_token when
22550 consuming tokens).
22551
22552 If we have a identifier, then use it for the caret location, e.g.
22553
22554 extern int callee (int one, int (*two)(int, int), float three);
22555 ~~~~~~^~~~~~~~~~~~~~
22556
22557 otherwise, reuse the start location for the caret location e.g.:
22558
22559 extern int callee (int one, int (*)(int, int), float three);
22560 ^~~~~~~~~~~~~~~~~
22561
22562 */
22563 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22564 ? declarator->id_loc
22565 : decl_spec_token_start->location);
22566 location_t param_loc = make_location (caret_loc,
22567 decl_spec_token_start->location,
22568 input_location);
22569
22570 return make_parameter_declarator (&decl_specifiers,
22571 declarator,
22572 default_argument,
22573 param_loc,
22574 template_parameter_pack_p);
22575 }
22576
22577 /* Parse a default argument and return it.
22578
22579 TEMPLATE_PARM_P is true if this is a default argument for a
22580 non-type template parameter. */
22581 static tree
22582 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22583 {
22584 tree default_argument = NULL_TREE;
22585 bool saved_greater_than_is_operator_p;
22586 unsigned char saved_local_variables_forbidden_p;
22587 bool non_constant_p, is_direct_init;
22588
22589 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22590 set correctly. */
22591 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22592 parser->greater_than_is_operator_p = !template_parm_p;
22593 /* Local variable names (and the `this' keyword) may not
22594 appear in a default argument. */
22595 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22596 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
22597 /* Parse the assignment-expression. */
22598 if (template_parm_p)
22599 push_deferring_access_checks (dk_no_deferred);
22600 tree saved_class_ptr = NULL_TREE;
22601 tree saved_class_ref = NULL_TREE;
22602 /* The "this" pointer is not valid in a default argument. */
22603 if (cfun)
22604 {
22605 saved_class_ptr = current_class_ptr;
22606 cp_function_chain->x_current_class_ptr = NULL_TREE;
22607 saved_class_ref = current_class_ref;
22608 cp_function_chain->x_current_class_ref = NULL_TREE;
22609 }
22610 default_argument
22611 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22612 /* Restore the "this" pointer. */
22613 if (cfun)
22614 {
22615 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22616 cp_function_chain->x_current_class_ref = saved_class_ref;
22617 }
22618 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22619 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22620 if (template_parm_p)
22621 pop_deferring_access_checks ();
22622 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22623 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22624
22625 return default_argument;
22626 }
22627
22628 /* Parse a function-body.
22629
22630 function-body:
22631 compound_statement */
22632
22633 static void
22634 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22635 {
22636 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22637 ? BCS_TRY_BLOCK : BCS_NORMAL),
22638 true);
22639 }
22640
22641 /* Parse a ctor-initializer-opt followed by a function-body. Return
22642 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22643 is true we are parsing a function-try-block. */
22644
22645 static void
22646 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22647 bool in_function_try_block)
22648 {
22649 tree body, list;
22650 const bool check_body_p
22651 = (DECL_CONSTRUCTOR_P (current_function_decl)
22652 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
22653 tree last = NULL;
22654
22655 if (in_function_try_block
22656 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22657 && cxx_dialect < cxx2a)
22658 {
22659 if (DECL_CONSTRUCTOR_P (current_function_decl))
22660 pedwarn (input_location, 0,
22661 "function-try-block body of %<constexpr%> constructor only "
22662 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22663 else
22664 pedwarn (input_location, 0,
22665 "function-try-block body of %<constexpr%> function only "
22666 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22667 }
22668
22669 /* Begin the function body. */
22670 body = begin_function_body ();
22671 /* Parse the optional ctor-initializer. */
22672 cp_parser_ctor_initializer_opt (parser);
22673
22674 /* If we're parsing a constexpr constructor definition, we need
22675 to check that the constructor body is indeed empty. However,
22676 before we get to cp_parser_function_body lot of junk has been
22677 generated, so we can't just check that we have an empty block.
22678 Rather we take a snapshot of the outermost block, and check whether
22679 cp_parser_function_body changed its state. */
22680 if (check_body_p)
22681 {
22682 list = cur_stmt_list;
22683 if (STATEMENT_LIST_TAIL (list))
22684 last = STATEMENT_LIST_TAIL (list)->stmt;
22685 }
22686 /* Parse the function-body. */
22687 cp_parser_function_body (parser, in_function_try_block);
22688 if (check_body_p)
22689 check_constexpr_ctor_body (last, list, /*complain=*/true);
22690 /* Finish the function body. */
22691 finish_function_body (body);
22692 }
22693
22694 /* Parse an initializer.
22695
22696 initializer:
22697 = initializer-clause
22698 ( expression-list )
22699
22700 Returns an expression representing the initializer. If no
22701 initializer is present, NULL_TREE is returned.
22702
22703 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22704 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22705 set to TRUE if there is no initializer present. If there is an
22706 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22707 is set to true; otherwise it is set to false. */
22708
22709 static tree
22710 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22711 bool* non_constant_p, bool subexpression_p)
22712 {
22713 cp_token *token;
22714 tree init;
22715
22716 /* Peek at the next token. */
22717 token = cp_lexer_peek_token (parser->lexer);
22718
22719 /* Let our caller know whether or not this initializer was
22720 parenthesized. */
22721 *is_direct_init = (token->type != CPP_EQ);
22722 /* Assume that the initializer is constant. */
22723 *non_constant_p = false;
22724
22725 if (token->type == CPP_EQ)
22726 {
22727 /* Consume the `='. */
22728 cp_lexer_consume_token (parser->lexer);
22729 /* Parse the initializer-clause. */
22730 init = cp_parser_initializer_clause (parser, non_constant_p);
22731 }
22732 else if (token->type == CPP_OPEN_PAREN)
22733 {
22734 vec<tree, va_gc> *vec;
22735 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22736 /*cast_p=*/false,
22737 /*allow_expansion_p=*/true,
22738 non_constant_p);
22739 if (vec == NULL)
22740 return error_mark_node;
22741 init = build_tree_list_vec (vec);
22742 release_tree_vector (vec);
22743 }
22744 else if (token->type == CPP_OPEN_BRACE)
22745 {
22746 cp_lexer_set_source_position (parser->lexer);
22747 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22748 init = cp_parser_braced_list (parser, non_constant_p);
22749 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22750 }
22751 else
22752 {
22753 /* Anything else is an error. */
22754 cp_parser_error (parser, "expected initializer");
22755 init = error_mark_node;
22756 }
22757
22758 if (!subexpression_p && check_for_bare_parameter_packs (init))
22759 init = error_mark_node;
22760
22761 return init;
22762 }
22763
22764 /* Parse an initializer-clause.
22765
22766 initializer-clause:
22767 assignment-expression
22768 braced-init-list
22769
22770 Returns an expression representing the initializer.
22771
22772 If the `assignment-expression' production is used the value
22773 returned is simply a representation for the expression.
22774
22775 Otherwise, calls cp_parser_braced_list. */
22776
22777 static cp_expr
22778 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22779 {
22780 cp_expr initializer;
22781
22782 /* Assume the expression is constant. */
22783 *non_constant_p = false;
22784
22785 /* If it is not a `{', then we are looking at an
22786 assignment-expression. */
22787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22788 {
22789 initializer
22790 = cp_parser_constant_expression (parser,
22791 /*allow_non_constant_p=*/true,
22792 non_constant_p);
22793 }
22794 else
22795 initializer = cp_parser_braced_list (parser, non_constant_p);
22796
22797 return initializer;
22798 }
22799
22800 /* Parse a brace-enclosed initializer list.
22801
22802 braced-init-list:
22803 { initializer-list , [opt] }
22804 { designated-initializer-list , [opt] }
22805 { }
22806
22807 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22808 the elements of the initializer-list (or NULL, if the last
22809 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22810 NULL_TREE. There is no way to detect whether or not the optional
22811 trailing `,' was provided. NON_CONSTANT_P is as for
22812 cp_parser_initializer. */
22813
22814 static cp_expr
22815 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22816 {
22817 tree initializer;
22818 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22819
22820 /* Consume the `{' token. */
22821 matching_braces braces;
22822 braces.require_open (parser);
22823 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22824 initializer = make_node (CONSTRUCTOR);
22825 /* If it's not a `}', then there is a non-trivial initializer. */
22826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22827 {
22828 bool designated;
22829 /* Parse the initializer list. */
22830 CONSTRUCTOR_ELTS (initializer)
22831 = cp_parser_initializer_list (parser, non_constant_p, &designated);
22832 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
22833 /* A trailing `,' token is allowed. */
22834 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22835 cp_lexer_consume_token (parser->lexer);
22836 }
22837 else
22838 *non_constant_p = false;
22839 /* Now, there should be a trailing `}'. */
22840 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22841 braces.require_close (parser);
22842 TREE_TYPE (initializer) = init_list_type_node;
22843
22844 cp_expr result (initializer);
22845 /* Build a location of the form:
22846 { ... }
22847 ^~~~~~~
22848 with caret==start at the open brace, finish at the close brace. */
22849 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22850 result.set_location (combined_loc);
22851 return result;
22852 }
22853
22854 /* Consume tokens up to, and including, the next non-nested closing `]'.
22855 Returns true iff we found a closing `]'. */
22856
22857 static bool
22858 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22859 {
22860 unsigned square_depth = 0;
22861
22862 while (true)
22863 {
22864 cp_token * token = cp_lexer_peek_token (parser->lexer);
22865
22866 switch (token->type)
22867 {
22868 case CPP_PRAGMA_EOL:
22869 if (!parser->lexer->in_pragma)
22870 break;
22871 /* FALLTHRU */
22872 case CPP_EOF:
22873 /* If we've run out of tokens, then there is no closing `]'. */
22874 return false;
22875
22876 case CPP_OPEN_SQUARE:
22877 ++square_depth;
22878 break;
22879
22880 case CPP_CLOSE_SQUARE:
22881 if (!square_depth--)
22882 {
22883 cp_lexer_consume_token (parser->lexer);
22884 return true;
22885 }
22886 break;
22887
22888 default:
22889 break;
22890 }
22891
22892 /* Consume the token. */
22893 cp_lexer_consume_token (parser->lexer);
22894 }
22895 }
22896
22897 /* Return true if we are looking at an array-designator, false otherwise. */
22898
22899 static bool
22900 cp_parser_array_designator_p (cp_parser *parser)
22901 {
22902 /* Consume the `['. */
22903 cp_lexer_consume_token (parser->lexer);
22904
22905 cp_lexer_save_tokens (parser->lexer);
22906
22907 /* Skip tokens until the next token is a closing square bracket.
22908 If we find the closing `]', and the next token is a `=', then
22909 we are looking at an array designator. */
22910 bool array_designator_p
22911 = (cp_parser_skip_to_closing_square_bracket (parser)
22912 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22913
22914 /* Roll back the tokens we skipped. */
22915 cp_lexer_rollback_tokens (parser->lexer);
22916
22917 return array_designator_p;
22918 }
22919
22920 /* Parse an initializer-list.
22921
22922 initializer-list:
22923 initializer-clause ... [opt]
22924 initializer-list , initializer-clause ... [opt]
22925
22926 C++2A Extension:
22927
22928 designated-initializer-list:
22929 designated-initializer-clause
22930 designated-initializer-list , designated-initializer-clause
22931
22932 designated-initializer-clause:
22933 designator brace-or-equal-initializer
22934
22935 designator:
22936 . identifier
22937
22938 GNU Extension:
22939
22940 initializer-list:
22941 designation initializer-clause ...[opt]
22942 initializer-list , designation initializer-clause ...[opt]
22943
22944 designation:
22945 . identifier =
22946 identifier :
22947 [ constant-expression ] =
22948
22949 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22950 for the initializer. If the INDEX of the elt is non-NULL, it is the
22951 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22952 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
22953 are any designators. */
22954
22955 static vec<constructor_elt, va_gc> *
22956 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
22957 bool *designated)
22958 {
22959 vec<constructor_elt, va_gc> *v = NULL;
22960 bool first_p = true;
22961 tree first_designator = NULL_TREE;
22962
22963 /* Assume all of the expressions are constant. */
22964 *non_constant_p = false;
22965
22966 /* Parse the rest of the list. */
22967 while (true)
22968 {
22969 cp_token *token;
22970 tree designator;
22971 tree initializer;
22972 bool clause_non_constant_p;
22973 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22974
22975 /* Handle the C++2A syntax, '. id ='. */
22976 if ((cxx_dialect >= cxx2a
22977 || cp_parser_allow_gnu_extensions_p (parser))
22978 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22979 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22980 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22981 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22982 == CPP_OPEN_BRACE)))
22983 {
22984 if (cxx_dialect < cxx2a)
22985 pedwarn (loc, OPT_Wpedantic,
22986 "C++ designated initializers only available with "
22987 "%<-std=c++2a%> or %<-std=gnu++2a%>");
22988 /* Consume the `.'. */
22989 cp_lexer_consume_token (parser->lexer);
22990 /* Consume the identifier. */
22991 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22992 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22993 /* Consume the `='. */
22994 cp_lexer_consume_token (parser->lexer);
22995 }
22996 /* Also, if the next token is an identifier and the following one is a
22997 colon, we are looking at the GNU designated-initializer
22998 syntax. */
22999 else if (cp_parser_allow_gnu_extensions_p (parser)
23000 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
23001 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23002 == CPP_COLON))
23003 {
23004 /* Warn the user that they are using an extension. */
23005 pedwarn (loc, OPT_Wpedantic,
23006 "ISO C++ does not allow GNU designated initializers");
23007 /* Consume the identifier. */
23008 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23009 /* Consume the `:'. */
23010 cp_lexer_consume_token (parser->lexer);
23011 }
23012 /* Also handle C99 array designators, '[ const ] ='. */
23013 else if (cp_parser_allow_gnu_extensions_p (parser)
23014 && !c_dialect_objc ()
23015 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23016 {
23017 /* In C++11, [ could start a lambda-introducer. */
23018 bool non_const = false;
23019
23020 cp_parser_parse_tentatively (parser);
23021
23022 if (!cp_parser_array_designator_p (parser))
23023 {
23024 cp_parser_simulate_error (parser);
23025 designator = NULL_TREE;
23026 }
23027 else
23028 {
23029 designator = cp_parser_constant_expression (parser, true,
23030 &non_const);
23031 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23032 cp_parser_require (parser, CPP_EQ, RT_EQ);
23033 }
23034
23035 if (!cp_parser_parse_definitely (parser))
23036 designator = NULL_TREE;
23037 else if (non_const
23038 && (!require_potential_rvalue_constant_expression
23039 (designator)))
23040 designator = NULL_TREE;
23041 if (designator)
23042 /* Warn the user that they are using an extension. */
23043 pedwarn (loc, OPT_Wpedantic,
23044 "ISO C++ does not allow C99 designated initializers");
23045 }
23046 else
23047 designator = NULL_TREE;
23048
23049 if (first_p)
23050 {
23051 first_designator = designator;
23052 first_p = false;
23053 }
23054 else if (cxx_dialect >= cxx2a
23055 && first_designator != error_mark_node
23056 && (!first_designator != !designator))
23057 {
23058 error_at (loc, "either all initializer clauses should be designated "
23059 "or none of them should be");
23060 first_designator = error_mark_node;
23061 }
23062 else if (cxx_dialect < cxx2a && !first_designator)
23063 first_designator = designator;
23064
23065 /* Parse the initializer. */
23066 initializer = cp_parser_initializer_clause (parser,
23067 &clause_non_constant_p);
23068 /* If any clause is non-constant, so is the entire initializer. */
23069 if (clause_non_constant_p)
23070 *non_constant_p = true;
23071
23072 /* If we have an ellipsis, this is an initializer pack
23073 expansion. */
23074 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23075 {
23076 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23077
23078 /* Consume the `...'. */
23079 cp_lexer_consume_token (parser->lexer);
23080
23081 if (designator && cxx_dialect >= cxx2a)
23082 error_at (loc,
23083 "%<...%> not allowed in designated initializer list");
23084
23085 /* Turn the initializer into an initializer expansion. */
23086 initializer = make_pack_expansion (initializer);
23087 }
23088
23089 /* Add it to the vector. */
23090 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
23091
23092 /* If the next token is not a comma, we have reached the end of
23093 the list. */
23094 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23095 break;
23096
23097 /* Peek at the next token. */
23098 token = cp_lexer_peek_nth_token (parser->lexer, 2);
23099 /* If the next token is a `}', then we're still done. An
23100 initializer-clause can have a trailing `,' after the
23101 initializer-list and before the closing `}'. */
23102 if (token->type == CPP_CLOSE_BRACE)
23103 break;
23104
23105 /* Consume the `,' token. */
23106 cp_lexer_consume_token (parser->lexer);
23107 }
23108
23109 /* The same identifier shall not appear in multiple designators
23110 of a designated-initializer-list. */
23111 if (first_designator)
23112 {
23113 unsigned int i;
23114 tree designator, val;
23115 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23116 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23117 {
23118 if (IDENTIFIER_MARKED (designator))
23119 {
23120 error_at (cp_expr_loc_or_loc (val, input_location),
23121 "%<.%s%> designator used multiple times in "
23122 "the same initializer list",
23123 IDENTIFIER_POINTER (designator));
23124 (*v)[i].index = error_mark_node;
23125 }
23126 else
23127 IDENTIFIER_MARKED (designator) = 1;
23128 }
23129 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23130 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23131 IDENTIFIER_MARKED (designator) = 0;
23132 }
23133
23134 *designated = first_designator != NULL_TREE;
23135 return v;
23136 }
23137
23138 /* Classes [gram.class] */
23139
23140 /* Parse a class-name.
23141
23142 class-name:
23143 identifier
23144 template-id
23145
23146 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23147 to indicate that names looked up in dependent types should be
23148 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23149 keyword has been used to indicate that the name that appears next
23150 is a template. TAG_TYPE indicates the explicit tag given before
23151 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23152 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23153 is the class being defined in a class-head. If ENUM_OK is TRUE,
23154 enum-names are also accepted.
23155
23156 Returns the TYPE_DECL representing the class. */
23157
23158 static tree
23159 cp_parser_class_name (cp_parser *parser,
23160 bool typename_keyword_p,
23161 bool template_keyword_p,
23162 enum tag_types tag_type,
23163 bool check_dependency_p,
23164 bool class_head_p,
23165 bool is_declaration,
23166 bool enum_ok)
23167 {
23168 tree decl;
23169 tree scope;
23170 bool typename_p;
23171 cp_token *token;
23172 tree identifier = NULL_TREE;
23173
23174 /* All class-names start with an identifier. */
23175 token = cp_lexer_peek_token (parser->lexer);
23176 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
23177 {
23178 cp_parser_error (parser, "expected class-name");
23179 return error_mark_node;
23180 }
23181
23182 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23183 to a template-id, so we save it here. */
23184 scope = parser->scope;
23185 if (scope == error_mark_node)
23186 return error_mark_node;
23187
23188 /* Any name names a type if we're following the `typename' keyword
23189 in a qualified name where the enclosing scope is type-dependent. */
23190 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
23191 && dependent_type_p (scope));
23192 /* Handle the common case (an identifier, but not a template-id)
23193 efficiently. */
23194 if (token->type == CPP_NAME
23195 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
23196 {
23197 cp_token *identifier_token;
23198 bool ambiguous_p;
23199
23200 /* Look for the identifier. */
23201 identifier_token = cp_lexer_peek_token (parser->lexer);
23202 ambiguous_p = identifier_token->error_reported;
23203 identifier = cp_parser_identifier (parser);
23204 /* If the next token isn't an identifier, we are certainly not
23205 looking at a class-name. */
23206 if (identifier == error_mark_node)
23207 decl = error_mark_node;
23208 /* If we know this is a type-name, there's no need to look it
23209 up. */
23210 else if (typename_p)
23211 decl = identifier;
23212 else
23213 {
23214 tree ambiguous_decls;
23215 /* If we already know that this lookup is ambiguous, then
23216 we've already issued an error message; there's no reason
23217 to check again. */
23218 if (ambiguous_p)
23219 {
23220 cp_parser_simulate_error (parser);
23221 return error_mark_node;
23222 }
23223 /* If the next token is a `::', then the name must be a type
23224 name.
23225
23226 [basic.lookup.qual]
23227
23228 During the lookup for a name preceding the :: scope
23229 resolution operator, object, function, and enumerator
23230 names are ignored. */
23231 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23232 tag_type = scope_type;
23233 /* Look up the name. */
23234 decl = cp_parser_lookup_name (parser, identifier,
23235 tag_type,
23236 /*is_template=*/false,
23237 /*is_namespace=*/false,
23238 check_dependency_p,
23239 &ambiguous_decls,
23240 identifier_token->location);
23241 if (ambiguous_decls)
23242 {
23243 if (cp_parser_parsing_tentatively (parser))
23244 cp_parser_simulate_error (parser);
23245 return error_mark_node;
23246 }
23247 }
23248 }
23249 else
23250 {
23251 /* Try a template-id. */
23252 decl = cp_parser_template_id (parser, template_keyword_p,
23253 check_dependency_p,
23254 tag_type,
23255 is_declaration);
23256 if (decl == error_mark_node)
23257 return error_mark_node;
23258 }
23259
23260 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
23261
23262 /* If this is a typename, create a TYPENAME_TYPE. */
23263 if (typename_p
23264 && decl != error_mark_node
23265 && !is_overloaded_fn (decl))
23266 {
23267 decl = make_typename_type (scope, decl, typename_type,
23268 /*complain=*/tf_error);
23269 if (decl != error_mark_node)
23270 decl = TYPE_NAME (decl);
23271 }
23272
23273 decl = strip_using_decl (decl);
23274
23275 /* Check to see that it is really the name of a class. */
23276 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
23277 && identifier_p (TREE_OPERAND (decl, 0))
23278 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23279 /* Situations like this:
23280
23281 template <typename T> struct A {
23282 typename T::template X<int>::I i;
23283 };
23284
23285 are problematic. Is `T::template X<int>' a class-name? The
23286 standard does not seem to be definitive, but there is no other
23287 valid interpretation of the following `::'. Therefore, those
23288 names are considered class-names. */
23289 {
23290 decl = make_typename_type (scope, decl, tag_type, tf_error);
23291 if (decl != error_mark_node)
23292 decl = TYPE_NAME (decl);
23293 }
23294 else if (TREE_CODE (decl) != TYPE_DECL
23295 || TREE_TYPE (decl) == error_mark_node
23296 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
23297 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
23298 /* In Objective-C 2.0, a classname followed by '.' starts a
23299 dot-syntax expression, and it's not a type-name. */
23300 || (c_dialect_objc ()
23301 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
23302 && objc_is_class_name (decl)))
23303 decl = error_mark_node;
23304
23305 if (decl == error_mark_node)
23306 cp_parser_error (parser, "expected class-name");
23307 else if (identifier && !parser->scope)
23308 maybe_note_name_used_in_class (identifier, decl);
23309
23310 return decl;
23311 }
23312
23313 /* Parse a class-specifier.
23314
23315 class-specifier:
23316 class-head { member-specification [opt] }
23317
23318 Returns the TREE_TYPE representing the class. */
23319
23320 static tree
23321 cp_parser_class_specifier_1 (cp_parser* parser)
23322 {
23323 tree type;
23324 tree attributes = NULL_TREE;
23325 bool nested_name_specifier_p;
23326 unsigned saved_num_template_parameter_lists;
23327 bool saved_in_function_body;
23328 unsigned char in_statement;
23329 bool in_switch_statement_p;
23330 bool saved_in_unbraced_linkage_specification_p;
23331 tree old_scope = NULL_TREE;
23332 tree scope = NULL_TREE;
23333 cp_token *closing_brace;
23334
23335 push_deferring_access_checks (dk_no_deferred);
23336
23337 /* Parse the class-head. */
23338 type = cp_parser_class_head (parser,
23339 &nested_name_specifier_p);
23340 /* If the class-head was a semantic disaster, skip the entire body
23341 of the class. */
23342 if (!type)
23343 {
23344 cp_parser_skip_to_end_of_block_or_statement (parser);
23345 pop_deferring_access_checks ();
23346 return error_mark_node;
23347 }
23348
23349 /* Look for the `{'. */
23350 matching_braces braces;
23351 if (!braces.require_open (parser))
23352 {
23353 pop_deferring_access_checks ();
23354 return error_mark_node;
23355 }
23356
23357 cp_ensure_no_omp_declare_simd (parser);
23358 cp_ensure_no_oacc_routine (parser);
23359
23360 /* Issue an error message if type-definitions are forbidden here. */
23361 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
23362 /* Remember that we are defining one more class. */
23363 ++parser->num_classes_being_defined;
23364 /* Inside the class, surrounding template-parameter-lists do not
23365 apply. */
23366 saved_num_template_parameter_lists
23367 = parser->num_template_parameter_lists;
23368 parser->num_template_parameter_lists = 0;
23369 /* We are not in a function body. */
23370 saved_in_function_body = parser->in_function_body;
23371 parser->in_function_body = false;
23372 /* Or in a loop. */
23373 in_statement = parser->in_statement;
23374 parser->in_statement = 0;
23375 /* Or in a switch. */
23376 in_switch_statement_p = parser->in_switch_statement_p;
23377 parser->in_switch_statement_p = false;
23378 /* We are not immediately inside an extern "lang" block. */
23379 saved_in_unbraced_linkage_specification_p
23380 = parser->in_unbraced_linkage_specification_p;
23381 parser->in_unbraced_linkage_specification_p = false;
23382
23383 // Associate constraints with the type.
23384 if (flag_concepts)
23385 type = associate_classtype_constraints (type);
23386
23387 /* Start the class. */
23388 if (nested_name_specifier_p)
23389 {
23390 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23391 old_scope = push_inner_scope (scope);
23392 }
23393 type = begin_class_definition (type);
23394
23395 if (type == error_mark_node)
23396 /* If the type is erroneous, skip the entire body of the class. */
23397 cp_parser_skip_to_closing_brace (parser);
23398 else
23399 /* Parse the member-specification. */
23400 cp_parser_member_specification_opt (parser);
23401
23402 /* Look for the trailing `}'. */
23403 closing_brace = braces.require_close (parser);
23404 /* Look for trailing attributes to apply to this class. */
23405 if (cp_parser_allow_gnu_extensions_p (parser))
23406 attributes = cp_parser_gnu_attributes_opt (parser);
23407 if (type != error_mark_node)
23408 type = finish_struct (type, attributes);
23409 if (nested_name_specifier_p)
23410 pop_inner_scope (old_scope, scope);
23411
23412 /* We've finished a type definition. Check for the common syntax
23413 error of forgetting a semicolon after the definition. We need to
23414 be careful, as we can't just check for not-a-semicolon and be done
23415 with it; the user might have typed:
23416
23417 class X { } c = ...;
23418 class X { } *p = ...;
23419
23420 and so forth. Instead, enumerate all the possible tokens that
23421 might follow this production; if we don't see one of them, then
23422 complain and silently insert the semicolon. */
23423 {
23424 cp_token *token = cp_lexer_peek_token (parser->lexer);
23425 bool want_semicolon = true;
23426
23427 if (cp_next_tokens_can_be_std_attribute_p (parser))
23428 /* Don't try to parse c++11 attributes here. As per the
23429 grammar, that should be a task for
23430 cp_parser_decl_specifier_seq. */
23431 want_semicolon = false;
23432
23433 switch (token->type)
23434 {
23435 case CPP_NAME:
23436 case CPP_SEMICOLON:
23437 case CPP_MULT:
23438 case CPP_AND:
23439 case CPP_OPEN_PAREN:
23440 case CPP_CLOSE_PAREN:
23441 case CPP_COMMA:
23442 want_semicolon = false;
23443 break;
23444
23445 /* While it's legal for type qualifiers and storage class
23446 specifiers to follow type definitions in the grammar, only
23447 compiler testsuites contain code like that. Assume that if
23448 we see such code, then what we're really seeing is a case
23449 like:
23450
23451 class X { }
23452 const <type> var = ...;
23453
23454 or
23455
23456 class Y { }
23457 static <type> func (...) ...
23458
23459 i.e. the qualifier or specifier applies to the next
23460 declaration. To do so, however, we need to look ahead one
23461 more token to see if *that* token is a type specifier.
23462
23463 This code could be improved to handle:
23464
23465 class Z { }
23466 static const <type> var = ...; */
23467 case CPP_KEYWORD:
23468 if (keyword_is_decl_specifier (token->keyword))
23469 {
23470 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
23471
23472 /* Handling user-defined types here would be nice, but very
23473 tricky. */
23474 want_semicolon
23475 = (lookahead->type == CPP_KEYWORD
23476 && keyword_begins_type_specifier (lookahead->keyword));
23477 }
23478 break;
23479 default:
23480 break;
23481 }
23482
23483 /* If we don't have a type, then something is very wrong and we
23484 shouldn't try to do anything clever. Likewise for not seeing the
23485 closing brace. */
23486 if (closing_brace && TYPE_P (type) && want_semicolon)
23487 {
23488 /* Locate the closing brace. */
23489 cp_token_position prev
23490 = cp_lexer_previous_token_position (parser->lexer);
23491 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
23492 location_t loc = prev_token->location;
23493
23494 /* We want to suggest insertion of a ';' immediately *after* the
23495 closing brace, so, if we can, offset the location by 1 column. */
23496 location_t next_loc = loc;
23497 if (!linemap_location_from_macro_expansion_p (line_table, loc))
23498 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
23499
23500 rich_location richloc (line_table, next_loc);
23501
23502 /* If we successfully offset the location, suggest the fix-it. */
23503 if (next_loc != loc)
23504 richloc.add_fixit_insert_before (next_loc, ";");
23505
23506 if (CLASSTYPE_DECLARED_CLASS (type))
23507 error_at (&richloc,
23508 "expected %<;%> after class definition");
23509 else if (TREE_CODE (type) == RECORD_TYPE)
23510 error_at (&richloc,
23511 "expected %<;%> after struct definition");
23512 else if (TREE_CODE (type) == UNION_TYPE)
23513 error_at (&richloc,
23514 "expected %<;%> after union definition");
23515 else
23516 gcc_unreachable ();
23517
23518 /* Unget one token and smash it to look as though we encountered
23519 a semicolon in the input stream. */
23520 cp_lexer_set_token_position (parser->lexer, prev);
23521 token = cp_lexer_peek_token (parser->lexer);
23522 token->type = CPP_SEMICOLON;
23523 token->keyword = RID_MAX;
23524 }
23525 }
23526
23527 /* If this class is not itself within the scope of another class,
23528 then we need to parse the bodies of all of the queued function
23529 definitions. Note that the queued functions defined in a class
23530 are not always processed immediately following the
23531 class-specifier for that class. Consider:
23532
23533 struct A {
23534 struct B { void f() { sizeof (A); } };
23535 };
23536
23537 If `f' were processed before the processing of `A' were
23538 completed, there would be no way to compute the size of `A'.
23539 Note that the nesting we are interested in here is lexical --
23540 not the semantic nesting given by TYPE_CONTEXT. In particular,
23541 for:
23542
23543 struct A { struct B; };
23544 struct A::B { void f() { } };
23545
23546 there is no need to delay the parsing of `A::B::f'. */
23547 if (--parser->num_classes_being_defined == 0)
23548 {
23549 tree decl;
23550 tree class_type = NULL_TREE;
23551 tree pushed_scope = NULL_TREE;
23552 unsigned ix;
23553 cp_default_arg_entry *e;
23554 tree save_ccp, save_ccr;
23555
23556 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
23557 {
23558 /* Skip default arguments, NSDMIs, etc, in order to improve
23559 error recovery (c++/71169, c++/71832). */
23560 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23561 vec_safe_truncate (unparsed_nsdmis, 0);
23562 vec_safe_truncate (unparsed_classes, 0);
23563 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23564 }
23565
23566 /* In a first pass, parse default arguments to the functions.
23567 Then, in a second pass, parse the bodies of the functions.
23568 This two-phased approach handles cases like:
23569
23570 struct S {
23571 void f() { g(); }
23572 void g(int i = 3);
23573 };
23574
23575 */
23576 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23577 {
23578 decl = e->decl;
23579 /* If there are default arguments that have not yet been processed,
23580 take care of them now. */
23581 if (class_type != e->class_type)
23582 {
23583 if (pushed_scope)
23584 pop_scope (pushed_scope);
23585 class_type = e->class_type;
23586 pushed_scope = push_scope (class_type);
23587 }
23588 /* Make sure that any template parameters are in scope. */
23589 maybe_begin_member_template_processing (decl);
23590 /* Parse the default argument expressions. */
23591 cp_parser_late_parsing_default_args (parser, decl);
23592 /* Remove any template parameters from the symbol table. */
23593 maybe_end_member_template_processing ();
23594 }
23595 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23596 /* Now parse any NSDMIs. */
23597 save_ccp = current_class_ptr;
23598 save_ccr = current_class_ref;
23599 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23600 {
23601 if (class_type != DECL_CONTEXT (decl))
23602 {
23603 if (pushed_scope)
23604 pop_scope (pushed_scope);
23605 class_type = DECL_CONTEXT (decl);
23606 pushed_scope = push_scope (class_type);
23607 }
23608 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23609 cp_parser_late_parsing_nsdmi (parser, decl);
23610 }
23611 vec_safe_truncate (unparsed_nsdmis, 0);
23612 current_class_ptr = save_ccp;
23613 current_class_ref = save_ccr;
23614 if (pushed_scope)
23615 pop_scope (pushed_scope);
23616
23617 /* Now do some post-NSDMI bookkeeping. */
23618 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23619 after_nsdmi_defaulted_late_checks (class_type);
23620 vec_safe_truncate (unparsed_classes, 0);
23621 after_nsdmi_defaulted_late_checks (type);
23622
23623 /* Now parse the body of the functions. */
23624 if (flag_openmp)
23625 {
23626 /* OpenMP UDRs need to be parsed before all other functions. */
23627 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23628 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23629 cp_parser_late_parsing_for_member (parser, decl);
23630 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23631 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23632 cp_parser_late_parsing_for_member (parser, decl);
23633 }
23634 else
23635 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23636 cp_parser_late_parsing_for_member (parser, decl);
23637 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23638 }
23639 else
23640 vec_safe_push (unparsed_classes, type);
23641
23642 /* Put back any saved access checks. */
23643 pop_deferring_access_checks ();
23644
23645 /* Restore saved state. */
23646 parser->in_switch_statement_p = in_switch_statement_p;
23647 parser->in_statement = in_statement;
23648 parser->in_function_body = saved_in_function_body;
23649 parser->num_template_parameter_lists
23650 = saved_num_template_parameter_lists;
23651 parser->in_unbraced_linkage_specification_p
23652 = saved_in_unbraced_linkage_specification_p;
23653
23654 return type;
23655 }
23656
23657 static tree
23658 cp_parser_class_specifier (cp_parser* parser)
23659 {
23660 tree ret;
23661 timevar_push (TV_PARSE_STRUCT);
23662 ret = cp_parser_class_specifier_1 (parser);
23663 timevar_pop (TV_PARSE_STRUCT);
23664 return ret;
23665 }
23666
23667 /* Parse a class-head.
23668
23669 class-head:
23670 class-key identifier [opt] base-clause [opt]
23671 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23672 class-key nested-name-specifier [opt] template-id
23673 base-clause [opt]
23674
23675 class-virt-specifier:
23676 final
23677
23678 GNU Extensions:
23679 class-key attributes identifier [opt] base-clause [opt]
23680 class-key attributes nested-name-specifier identifier base-clause [opt]
23681 class-key attributes nested-name-specifier [opt] template-id
23682 base-clause [opt]
23683
23684 Upon return BASES is initialized to the list of base classes (or
23685 NULL, if there are none) in the same form returned by
23686 cp_parser_base_clause.
23687
23688 Returns the TYPE of the indicated class. Sets
23689 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23690 involving a nested-name-specifier was used, and FALSE otherwise.
23691
23692 Returns error_mark_node if this is not a class-head.
23693
23694 Returns NULL_TREE if the class-head is syntactically valid, but
23695 semantically invalid in a way that means we should skip the entire
23696 body of the class. */
23697
23698 static tree
23699 cp_parser_class_head (cp_parser* parser,
23700 bool* nested_name_specifier_p)
23701 {
23702 tree nested_name_specifier;
23703 enum tag_types class_key;
23704 tree id = NULL_TREE;
23705 tree type = NULL_TREE;
23706 tree attributes;
23707 tree bases;
23708 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23709 bool template_id_p = false;
23710 bool qualified_p = false;
23711 bool invalid_nested_name_p = false;
23712 bool invalid_explicit_specialization_p = false;
23713 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23714 tree pushed_scope = NULL_TREE;
23715 unsigned num_templates;
23716 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23717 /* Assume no nested-name-specifier will be present. */
23718 *nested_name_specifier_p = false;
23719 /* Assume no template parameter lists will be used in defining the
23720 type. */
23721 num_templates = 0;
23722 parser->colon_corrects_to_scope_p = false;
23723
23724 /* Look for the class-key. */
23725 class_key = cp_parser_class_key (parser);
23726 if (class_key == none_type)
23727 return error_mark_node;
23728
23729 location_t class_head_start_location = input_location;
23730
23731 /* Parse the attributes. */
23732 attributes = cp_parser_attributes_opt (parser);
23733
23734 /* If the next token is `::', that is invalid -- but sometimes
23735 people do try to write:
23736
23737 struct ::S {};
23738
23739 Handle this gracefully by accepting the extra qualifier, and then
23740 issuing an error about it later if this really is a
23741 class-head. If it turns out just to be an elaborated type
23742 specifier, remain silent. */
23743 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23744 qualified_p = true;
23745
23746 push_deferring_access_checks (dk_no_check);
23747
23748 /* Determine the name of the class. Begin by looking for an
23749 optional nested-name-specifier. */
23750 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23751 nested_name_specifier
23752 = cp_parser_nested_name_specifier_opt (parser,
23753 /*typename_keyword_p=*/false,
23754 /*check_dependency_p=*/false,
23755 /*type_p=*/true,
23756 /*is_declaration=*/false);
23757 /* If there was a nested-name-specifier, then there *must* be an
23758 identifier. */
23759
23760 cp_token *bad_template_keyword = NULL;
23761
23762 if (nested_name_specifier)
23763 {
23764 type_start_token = cp_lexer_peek_token (parser->lexer);
23765 /* Although the grammar says `identifier', it really means
23766 `class-name' or `template-name'. You are only allowed to
23767 define a class that has already been declared with this
23768 syntax.
23769
23770 The proposed resolution for Core Issue 180 says that wherever
23771 you see `class T::X' you should treat `X' as a type-name.
23772
23773 It is OK to define an inaccessible class; for example:
23774
23775 class A { class B; };
23776 class A::B {};
23777
23778 We do not know if we will see a class-name, or a
23779 template-name. We look for a class-name first, in case the
23780 class-name is a template-id; if we looked for the
23781 template-name first we would stop after the template-name. */
23782 cp_parser_parse_tentatively (parser);
23783 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23784 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23785 type = cp_parser_class_name (parser,
23786 /*typename_keyword_p=*/false,
23787 /*template_keyword_p=*/false,
23788 class_type,
23789 /*check_dependency_p=*/false,
23790 /*class_head_p=*/true,
23791 /*is_declaration=*/false);
23792 /* If that didn't work, ignore the nested-name-specifier. */
23793 if (!cp_parser_parse_definitely (parser))
23794 {
23795 invalid_nested_name_p = true;
23796 type_start_token = cp_lexer_peek_token (parser->lexer);
23797 id = cp_parser_identifier (parser);
23798 if (id == error_mark_node)
23799 id = NULL_TREE;
23800 }
23801 /* If we could not find a corresponding TYPE, treat this
23802 declaration like an unqualified declaration. */
23803 if (type == error_mark_node)
23804 nested_name_specifier = NULL_TREE;
23805 /* Otherwise, count the number of templates used in TYPE and its
23806 containing scopes. */
23807 else
23808 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23809 }
23810 /* Otherwise, the identifier is optional. */
23811 else
23812 {
23813 /* We don't know whether what comes next is a template-id,
23814 an identifier, or nothing at all. */
23815 cp_parser_parse_tentatively (parser);
23816 /* Check for a template-id. */
23817 type_start_token = cp_lexer_peek_token (parser->lexer);
23818 id = cp_parser_template_id (parser,
23819 /*template_keyword_p=*/false,
23820 /*check_dependency_p=*/true,
23821 class_key,
23822 /*is_declaration=*/true);
23823 /* If that didn't work, it could still be an identifier. */
23824 if (!cp_parser_parse_definitely (parser))
23825 {
23826 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23827 {
23828 type_start_token = cp_lexer_peek_token (parser->lexer);
23829 id = cp_parser_identifier (parser);
23830 }
23831 else
23832 id = NULL_TREE;
23833 }
23834 else
23835 {
23836 template_id_p = true;
23837 ++num_templates;
23838 }
23839 }
23840
23841 pop_deferring_access_checks ();
23842
23843 if (id)
23844 {
23845 cp_parser_check_for_invalid_template_id (parser, id,
23846 class_key,
23847 type_start_token->location);
23848 }
23849 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23850
23851 /* If it's not a `:' or a `{' then we can't really be looking at a
23852 class-head, since a class-head only appears as part of a
23853 class-specifier. We have to detect this situation before calling
23854 xref_tag, since that has irreversible side-effects. */
23855 if (!cp_parser_next_token_starts_class_definition_p (parser))
23856 {
23857 cp_parser_error (parser, "expected %<{%> or %<:%>");
23858 type = error_mark_node;
23859 goto out;
23860 }
23861
23862 /* At this point, we're going ahead with the class-specifier, even
23863 if some other problem occurs. */
23864 cp_parser_commit_to_tentative_parse (parser);
23865 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23866 {
23867 cp_parser_error (parser,
23868 "cannot specify %<override%> for a class");
23869 type = error_mark_node;
23870 goto out;
23871 }
23872 /* Issue the error about the overly-qualified name now. */
23873 if (qualified_p)
23874 {
23875 cp_parser_error (parser,
23876 "global qualification of class name is invalid");
23877 type = error_mark_node;
23878 goto out;
23879 }
23880 else if (invalid_nested_name_p)
23881 {
23882 cp_parser_error (parser,
23883 "qualified name does not name a class");
23884 type = error_mark_node;
23885 goto out;
23886 }
23887 else if (nested_name_specifier)
23888 {
23889 tree scope;
23890
23891 if (bad_template_keyword)
23892 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23893 keyword template shall not appear at the top level. */
23894 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23895 "keyword %<template%> not allowed in class-head-name");
23896
23897 /* Reject typedef-names in class heads. */
23898 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23899 {
23900 error_at (type_start_token->location,
23901 "invalid class name in declaration of %qD",
23902 type);
23903 type = NULL_TREE;
23904 goto done;
23905 }
23906
23907 /* Figure out in what scope the declaration is being placed. */
23908 scope = current_scope ();
23909 /* If that scope does not contain the scope in which the
23910 class was originally declared, the program is invalid. */
23911 if (scope && !is_ancestor (scope, nested_name_specifier))
23912 {
23913 if (at_namespace_scope_p ())
23914 error_at (type_start_token->location,
23915 "declaration of %qD in namespace %qD which does not "
23916 "enclose %qD",
23917 type, scope, nested_name_specifier);
23918 else
23919 error_at (type_start_token->location,
23920 "declaration of %qD in %qD which does not enclose %qD",
23921 type, scope, nested_name_specifier);
23922 type = NULL_TREE;
23923 goto done;
23924 }
23925 /* [dcl.meaning]
23926
23927 A declarator-id shall not be qualified except for the
23928 definition of a ... nested class outside of its class
23929 ... [or] the definition or explicit instantiation of a
23930 class member of a namespace outside of its namespace. */
23931 if (scope == nested_name_specifier)
23932 {
23933 permerror (nested_name_specifier_token_start->location,
23934 "extra qualification not allowed");
23935 nested_name_specifier = NULL_TREE;
23936 num_templates = 0;
23937 }
23938 }
23939 /* An explicit-specialization must be preceded by "template <>". If
23940 it is not, try to recover gracefully. */
23941 if (at_namespace_scope_p ()
23942 && parser->num_template_parameter_lists == 0
23943 && !processing_template_parmlist
23944 && template_id_p)
23945 {
23946 /* Build a location of this form:
23947 struct typename <ARGS>
23948 ^~~~~~~~~~~~~~~~~~~~~~
23949 with caret==start at the start token, and
23950 finishing at the end of the type. */
23951 location_t reported_loc
23952 = make_location (class_head_start_location,
23953 class_head_start_location,
23954 get_finish (type_start_token->location));
23955 rich_location richloc (line_table, reported_loc);
23956 richloc.add_fixit_insert_before (class_head_start_location,
23957 "template <> ");
23958 error_at (&richloc,
23959 "an explicit specialization must be preceded by"
23960 " %<template <>%>");
23961 invalid_explicit_specialization_p = true;
23962 /* Take the same action that would have been taken by
23963 cp_parser_explicit_specialization. */
23964 ++parser->num_template_parameter_lists;
23965 begin_specialization ();
23966 }
23967 /* There must be no "return" statements between this point and the
23968 end of this function; set "type "to the correct return value and
23969 use "goto done;" to return. */
23970 /* Make sure that the right number of template parameters were
23971 present. */
23972 if (!cp_parser_check_template_parameters (parser, num_templates,
23973 template_id_p,
23974 type_start_token->location,
23975 /*declarator=*/NULL))
23976 {
23977 /* If something went wrong, there is no point in even trying to
23978 process the class-definition. */
23979 type = NULL_TREE;
23980 goto done;
23981 }
23982
23983 /* Look up the type. */
23984 if (template_id_p)
23985 {
23986 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23987 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23988 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23989 {
23990 error_at (type_start_token->location,
23991 "function template %qD redeclared as a class template", id);
23992 type = error_mark_node;
23993 }
23994 else
23995 {
23996 type = TREE_TYPE (id);
23997 type = maybe_process_partial_specialization (type);
23998
23999 /* Check the scope while we still know whether or not we had a
24000 nested-name-specifier. */
24001 if (type != error_mark_node)
24002 check_unqualified_spec_or_inst (type, type_start_token->location);
24003 }
24004 if (nested_name_specifier)
24005 pushed_scope = push_scope (nested_name_specifier);
24006 }
24007 else if (nested_name_specifier)
24008 {
24009 tree class_type;
24010
24011 /* Given:
24012
24013 template <typename T> struct S { struct T };
24014 template <typename T> struct S<T>::T { };
24015
24016 we will get a TYPENAME_TYPE when processing the definition of
24017 `S::T'. We need to resolve it to the actual type before we
24018 try to define it. */
24019 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
24020 {
24021 class_type = resolve_typename_type (TREE_TYPE (type),
24022 /*only_current_p=*/false);
24023 if (TREE_CODE (class_type) != TYPENAME_TYPE)
24024 type = TYPE_NAME (class_type);
24025 else
24026 {
24027 cp_parser_error (parser, "could not resolve typename type");
24028 type = error_mark_node;
24029 }
24030 }
24031
24032 if (maybe_process_partial_specialization (TREE_TYPE (type))
24033 == error_mark_node)
24034 {
24035 type = NULL_TREE;
24036 goto done;
24037 }
24038
24039 class_type = current_class_type;
24040 /* Enter the scope indicated by the nested-name-specifier. */
24041 pushed_scope = push_scope (nested_name_specifier);
24042 /* Get the canonical version of this type. */
24043 type = TYPE_MAIN_DECL (TREE_TYPE (type));
24044 /* Call push_template_decl if it seems like we should be defining a
24045 template either from the template headers or the type we're
24046 defining, so that we diagnose both extra and missing headers. */
24047 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24048 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
24049 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
24050 {
24051 type = push_template_decl (type);
24052 if (type == error_mark_node)
24053 {
24054 type = NULL_TREE;
24055 goto done;
24056 }
24057 }
24058
24059 type = TREE_TYPE (type);
24060 *nested_name_specifier_p = true;
24061 }
24062 else /* The name is not a nested name. */
24063 {
24064 /* If the class was unnamed, create a dummy name. */
24065 if (!id)
24066 id = make_anon_name ();
24067 tag_scope tag_scope = (parser->in_type_id_in_expr_p
24068 ? ts_within_enclosing_non_class
24069 : ts_current);
24070 type = xref_tag (class_key, id, tag_scope,
24071 parser->num_template_parameter_lists);
24072 }
24073
24074 /* Indicate whether this class was declared as a `class' or as a
24075 `struct'. */
24076 if (TREE_CODE (type) == RECORD_TYPE)
24077 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
24078 cp_parser_check_class_key (class_key, type);
24079
24080 /* If this type was already complete, and we see another definition,
24081 that's an error. Likewise if the type is already being defined:
24082 this can happen, eg, when it's defined from within an expression
24083 (c++/84605). */
24084 if (type != error_mark_node
24085 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
24086 {
24087 error_at (type_start_token->location, "redefinition of %q#T",
24088 type);
24089 inform (location_of (type), "previous definition of %q#T",
24090 type);
24091 type = NULL_TREE;
24092 goto done;
24093 }
24094 else if (type == error_mark_node)
24095 type = NULL_TREE;
24096
24097 if (type)
24098 {
24099 /* Apply attributes now, before any use of the class as a template
24100 argument in its base list. */
24101 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
24102 fixup_attribute_variants (type);
24103 }
24104
24105 /* We will have entered the scope containing the class; the names of
24106 base classes should be looked up in that context. For example:
24107
24108 struct A { struct B {}; struct C; };
24109 struct A::C : B {};
24110
24111 is valid. */
24112
24113 /* Get the list of base-classes, if there is one. */
24114 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24115 {
24116 /* PR59482: enter the class scope so that base-specifiers are looked
24117 up correctly. */
24118 if (type)
24119 pushclass (type);
24120 bases = cp_parser_base_clause (parser);
24121 /* PR59482: get out of the previously pushed class scope so that the
24122 subsequent pops pop the right thing. */
24123 if (type)
24124 popclass ();
24125 }
24126 else
24127 bases = NULL_TREE;
24128
24129 /* If we're really defining a class, process the base classes.
24130 If they're invalid, fail. */
24131 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24132 xref_basetypes (type, bases);
24133
24134 done:
24135 /* Leave the scope given by the nested-name-specifier. We will
24136 enter the class scope itself while processing the members. */
24137 if (pushed_scope)
24138 pop_scope (pushed_scope);
24139
24140 if (invalid_explicit_specialization_p)
24141 {
24142 end_specialization ();
24143 --parser->num_template_parameter_lists;
24144 }
24145
24146 if (type)
24147 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
24148 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
24149 CLASSTYPE_FINAL (type) = 1;
24150 out:
24151 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24152 return type;
24153 }
24154
24155 /* Parse a class-key.
24156
24157 class-key:
24158 class
24159 struct
24160 union
24161
24162 Returns the kind of class-key specified, or none_type to indicate
24163 error. */
24164
24165 static enum tag_types
24166 cp_parser_class_key (cp_parser* parser)
24167 {
24168 cp_token *token;
24169 enum tag_types tag_type;
24170
24171 /* Look for the class-key. */
24172 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
24173 if (!token)
24174 return none_type;
24175
24176 /* Check to see if the TOKEN is a class-key. */
24177 tag_type = cp_parser_token_is_class_key (token);
24178 if (!tag_type)
24179 cp_parser_error (parser, "expected class-key");
24180 return tag_type;
24181 }
24182
24183 /* Parse a type-parameter-key.
24184
24185 type-parameter-key:
24186 class
24187 typename
24188 */
24189
24190 static void
24191 cp_parser_type_parameter_key (cp_parser* parser)
24192 {
24193 /* Look for the type-parameter-key. */
24194 enum tag_types tag_type = none_type;
24195 cp_token *token = cp_lexer_peek_token (parser->lexer);
24196 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
24197 {
24198 cp_lexer_consume_token (parser->lexer);
24199 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
24200 /* typename is not allowed in a template template parameter
24201 by the standard until C++17. */
24202 pedwarn (token->location, OPT_Wpedantic,
24203 "ISO C++ forbids typename key in template template parameter;"
24204 " use %<-std=c++17%> or %<-std=gnu++17%>");
24205 }
24206 else
24207 cp_parser_error (parser, "expected %<class%> or %<typename%>");
24208
24209 return;
24210 }
24211
24212 /* Parse an (optional) member-specification.
24213
24214 member-specification:
24215 member-declaration member-specification [opt]
24216 access-specifier : member-specification [opt] */
24217
24218 static void
24219 cp_parser_member_specification_opt (cp_parser* parser)
24220 {
24221 while (true)
24222 {
24223 cp_token *token;
24224 enum rid keyword;
24225
24226 /* Peek at the next token. */
24227 token = cp_lexer_peek_token (parser->lexer);
24228 /* If it's a `}', or EOF then we've seen all the members. */
24229 if (token->type == CPP_CLOSE_BRACE
24230 || token->type == CPP_EOF
24231 || token->type == CPP_PRAGMA_EOL)
24232 break;
24233
24234 /* See if this token is a keyword. */
24235 keyword = token->keyword;
24236 switch (keyword)
24237 {
24238 case RID_PUBLIC:
24239 case RID_PROTECTED:
24240 case RID_PRIVATE:
24241 /* Consume the access-specifier. */
24242 cp_lexer_consume_token (parser->lexer);
24243 /* Remember which access-specifier is active. */
24244 current_access_specifier = token->u.value;
24245 /* Look for the `:'. */
24246 cp_parser_require (parser, CPP_COLON, RT_COLON);
24247 break;
24248
24249 default:
24250 /* Accept #pragmas at class scope. */
24251 if (token->type == CPP_PRAGMA)
24252 {
24253 cp_parser_pragma (parser, pragma_member, NULL);
24254 break;
24255 }
24256
24257 /* Otherwise, the next construction must be a
24258 member-declaration. */
24259 cp_parser_member_declaration (parser);
24260 }
24261 }
24262 }
24263
24264 /* Parse a member-declaration.
24265
24266 member-declaration:
24267 decl-specifier-seq [opt] member-declarator-list [opt] ;
24268 function-definition ; [opt]
24269 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24270 using-declaration
24271 template-declaration
24272 alias-declaration
24273
24274 member-declarator-list:
24275 member-declarator
24276 member-declarator-list , member-declarator
24277
24278 member-declarator:
24279 declarator pure-specifier [opt]
24280 declarator constant-initializer [opt]
24281 identifier [opt] : constant-expression
24282
24283 GNU Extensions:
24284
24285 member-declaration:
24286 __extension__ member-declaration
24287
24288 member-declarator:
24289 declarator attributes [opt] pure-specifier [opt]
24290 declarator attributes [opt] constant-initializer [opt]
24291 identifier [opt] attributes [opt] : constant-expression
24292
24293 C++0x Extensions:
24294
24295 member-declaration:
24296 static_assert-declaration */
24297
24298 static void
24299 cp_parser_member_declaration (cp_parser* parser)
24300 {
24301 cp_decl_specifier_seq decl_specifiers;
24302 tree prefix_attributes;
24303 tree decl;
24304 int declares_class_or_enum;
24305 bool friend_p;
24306 cp_token *token = NULL;
24307 cp_token *decl_spec_token_start = NULL;
24308 cp_token *initializer_token_start = NULL;
24309 int saved_pedantic;
24310 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24311
24312 /* Check for the `__extension__' keyword. */
24313 if (cp_parser_extension_opt (parser, &saved_pedantic))
24314 {
24315 /* Recurse. */
24316 cp_parser_member_declaration (parser);
24317 /* Restore the old value of the PEDANTIC flag. */
24318 pedantic = saved_pedantic;
24319
24320 return;
24321 }
24322
24323 /* Check for a template-declaration. */
24324 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24325 {
24326 /* An explicit specialization here is an error condition, and we
24327 expect the specialization handler to detect and report this. */
24328 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
24329 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
24330 cp_parser_explicit_specialization (parser);
24331 else
24332 cp_parser_template_declaration (parser, /*member_p=*/true);
24333
24334 return;
24335 }
24336 /* Check for a template introduction. */
24337 else if (cp_parser_template_declaration_after_export (parser, true))
24338 return;
24339
24340 /* Check for a using-declaration. */
24341 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24342 {
24343 if (cxx_dialect < cxx11)
24344 {
24345 /* Parse the using-declaration. */
24346 cp_parser_using_declaration (parser,
24347 /*access_declaration_p=*/false);
24348 return;
24349 }
24350 else
24351 {
24352 tree decl;
24353 bool alias_decl_expected;
24354 cp_parser_parse_tentatively (parser);
24355 decl = cp_parser_alias_declaration (parser);
24356 /* Note that if we actually see the '=' token after the
24357 identifier, cp_parser_alias_declaration commits the
24358 tentative parse. In that case, we really expect an
24359 alias-declaration. Otherwise, we expect a using
24360 declaration. */
24361 alias_decl_expected =
24362 !cp_parser_uncommitted_to_tentative_parse_p (parser);
24363 cp_parser_parse_definitely (parser);
24364
24365 if (alias_decl_expected)
24366 finish_member_declaration (decl);
24367 else
24368 cp_parser_using_declaration (parser,
24369 /*access_declaration_p=*/false);
24370 return;
24371 }
24372 }
24373
24374 /* Check for @defs. */
24375 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
24376 {
24377 tree ivar, member;
24378 tree ivar_chains = cp_parser_objc_defs_expression (parser);
24379 ivar = ivar_chains;
24380 while (ivar)
24381 {
24382 member = ivar;
24383 ivar = TREE_CHAIN (member);
24384 TREE_CHAIN (member) = NULL_TREE;
24385 finish_member_declaration (member);
24386 }
24387 return;
24388 }
24389
24390 /* If the next token is `static_assert' we have a static assertion. */
24391 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
24392 {
24393 cp_parser_static_assert (parser, /*member_p=*/true);
24394 return;
24395 }
24396
24397 parser->colon_corrects_to_scope_p = false;
24398
24399 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
24400 goto out;
24401
24402 /* Parse the decl-specifier-seq. */
24403 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24404 cp_parser_decl_specifier_seq (parser,
24405 (CP_PARSER_FLAGS_OPTIONAL
24406 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
24407 &decl_specifiers,
24408 &declares_class_or_enum);
24409 /* Check for an invalid type-name. */
24410 if (!decl_specifiers.any_type_specifiers_p
24411 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24412 goto out;
24413 /* If there is no declarator, then the decl-specifier-seq should
24414 specify a type. */
24415 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24416 {
24417 /* If there was no decl-specifier-seq, and the next token is a
24418 `;', then we have something like:
24419
24420 struct S { ; };
24421
24422 [class.mem]
24423
24424 Each member-declaration shall declare at least one member
24425 name of the class. */
24426 if (!decl_specifiers.any_specifiers_p)
24427 {
24428 cp_token *token = cp_lexer_peek_token (parser->lexer);
24429 if (!in_system_header_at (token->location))
24430 {
24431 gcc_rich_location richloc (token->location);
24432 richloc.add_fixit_remove ();
24433 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
24434 }
24435 }
24436 else
24437 {
24438 tree type;
24439
24440 /* See if this declaration is a friend. */
24441 friend_p = cp_parser_friend_p (&decl_specifiers);
24442 /* If there were decl-specifiers, check to see if there was
24443 a class-declaration. */
24444 type = check_tag_decl (&decl_specifiers,
24445 /*explicit_type_instantiation_p=*/false);
24446 /* Nested classes have already been added to the class, but
24447 a `friend' needs to be explicitly registered. */
24448 if (friend_p)
24449 {
24450 /* If the `friend' keyword was present, the friend must
24451 be introduced with a class-key. */
24452 if (!declares_class_or_enum && cxx_dialect < cxx11)
24453 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
24454 "in C++03 a class-key must be used "
24455 "when declaring a friend");
24456 /* In this case:
24457
24458 template <typename T> struct A {
24459 friend struct A<T>::B;
24460 };
24461
24462 A<T>::B will be represented by a TYPENAME_TYPE, and
24463 therefore not recognized by check_tag_decl. */
24464 if (!type)
24465 {
24466 type = decl_specifiers.type;
24467 if (type && TREE_CODE (type) == TYPE_DECL)
24468 type = TREE_TYPE (type);
24469 }
24470 if (!type || !TYPE_P (type))
24471 error_at (decl_spec_token_start->location,
24472 "friend declaration does not name a class or "
24473 "function");
24474 else
24475 make_friend_class (current_class_type, type,
24476 /*complain=*/true);
24477 }
24478 /* If there is no TYPE, an error message will already have
24479 been issued. */
24480 else if (!type || type == error_mark_node)
24481 ;
24482 /* An anonymous aggregate has to be handled specially; such
24483 a declaration really declares a data member (with a
24484 particular type), as opposed to a nested class. */
24485 else if (ANON_AGGR_TYPE_P (type))
24486 {
24487 /* C++11 9.5/6. */
24488 if (decl_specifiers.storage_class != sc_none)
24489 error_at (decl_spec_token_start->location,
24490 "a storage class on an anonymous aggregate "
24491 "in class scope is not allowed");
24492
24493 /* Remove constructors and such from TYPE, now that we
24494 know it is an anonymous aggregate. */
24495 fixup_anonymous_aggr (type);
24496 /* And make the corresponding data member. */
24497 decl = build_decl (decl_spec_token_start->location,
24498 FIELD_DECL, NULL_TREE, type);
24499 /* Add it to the class. */
24500 finish_member_declaration (decl);
24501 }
24502 else
24503 cp_parser_check_access_in_redeclaration
24504 (TYPE_NAME (type),
24505 decl_spec_token_start->location);
24506 }
24507 }
24508 else
24509 {
24510 bool assume_semicolon = false;
24511
24512 /* Clear attributes from the decl_specifiers but keep them
24513 around as prefix attributes that apply them to the entity
24514 being declared. */
24515 prefix_attributes = decl_specifiers.attributes;
24516 decl_specifiers.attributes = NULL_TREE;
24517
24518 /* See if these declarations will be friends. */
24519 friend_p = cp_parser_friend_p (&decl_specifiers);
24520
24521 /* Keep going until we hit the `;' at the end of the
24522 declaration. */
24523 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24524 {
24525 tree attributes = NULL_TREE;
24526 tree first_attribute;
24527 tree initializer;
24528 bool named_bitfld = false;
24529
24530 /* Peek at the next token. */
24531 token = cp_lexer_peek_token (parser->lexer);
24532
24533 /* The following code wants to know early if it is a bit-field
24534 or some other declaration. Attributes can appear before
24535 the `:' token. Skip over them without consuming any tokens
24536 to peek if they are followed by `:'. */
24537 if (cp_next_tokens_can_be_attribute_p (parser)
24538 || (token->type == CPP_NAME
24539 && cp_nth_tokens_can_be_attribute_p (parser, 2)
24540 && (named_bitfld = true)))
24541 {
24542 size_t n
24543 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24544 token = cp_lexer_peek_nth_token (parser->lexer, n);
24545 }
24546
24547 /* Check for a bitfield declaration. */
24548 if (token->type == CPP_COLON
24549 || (token->type == CPP_NAME
24550 && token == cp_lexer_peek_token (parser->lexer)
24551 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24552 && (named_bitfld = true)))
24553 {
24554 tree identifier;
24555 tree width;
24556 tree late_attributes = NULL_TREE;
24557 location_t id_location
24558 = cp_lexer_peek_token (parser->lexer)->location;
24559
24560 if (named_bitfld)
24561 identifier = cp_parser_identifier (parser);
24562 else
24563 identifier = NULL_TREE;
24564
24565 /* Look for attributes that apply to the bitfield. */
24566 attributes = cp_parser_attributes_opt (parser);
24567
24568 /* Consume the `:' token. */
24569 cp_lexer_consume_token (parser->lexer);
24570
24571 /* Get the width of the bitfield. */
24572 width = cp_parser_constant_expression (parser, false, NULL,
24573 cxx_dialect >= cxx11);
24574
24575 /* In C++2A and as extension for C++11 and above we allow
24576 default member initializers for bit-fields. */
24577 initializer = NULL_TREE;
24578 if (cxx_dialect >= cxx11
24579 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24580 || cp_lexer_next_token_is (parser->lexer,
24581 CPP_OPEN_BRACE)))
24582 {
24583 location_t loc
24584 = cp_lexer_peek_token (parser->lexer)->location;
24585 if (cxx_dialect < cxx2a
24586 && !in_system_header_at (loc)
24587 && identifier != NULL_TREE)
24588 pedwarn (loc, 0,
24589 "default member initializers for bit-fields "
24590 "only available with %<-std=c++2a%> or "
24591 "%<-std=gnu++2a%>");
24592
24593 initializer = cp_parser_save_nsdmi (parser);
24594 if (identifier == NULL_TREE)
24595 {
24596 error_at (loc, "default member initializer for "
24597 "unnamed bit-field");
24598 initializer = NULL_TREE;
24599 }
24600 }
24601 else
24602 {
24603 /* Look for attributes that apply to the bitfield after
24604 the `:' token and width. This is where GCC used to
24605 parse attributes in the past, pedwarn if there is
24606 a std attribute. */
24607 if (cp_next_tokens_can_be_std_attribute_p (parser))
24608 pedwarn (input_location, OPT_Wpedantic,
24609 "ISO C++ allows bit-field attributes only "
24610 "before the %<:%> token");
24611
24612 late_attributes = cp_parser_attributes_opt (parser);
24613 }
24614
24615 attributes = attr_chainon (attributes, late_attributes);
24616
24617 /* Remember which attributes are prefix attributes and
24618 which are not. */
24619 first_attribute = attributes;
24620 /* Combine the attributes. */
24621 attributes = attr_chainon (prefix_attributes, attributes);
24622
24623 /* Create the bitfield declaration. */
24624 decl = grokbitfield (identifier
24625 ? make_id_declarator (NULL_TREE,
24626 identifier,
24627 sfk_none,
24628 id_location)
24629 : NULL,
24630 &decl_specifiers,
24631 width, initializer,
24632 attributes);
24633 }
24634 else
24635 {
24636 cp_declarator *declarator;
24637 tree asm_specification;
24638 int ctor_dtor_or_conv_p;
24639 bool static_p = (decl_specifiers.storage_class == sc_static);
24640
24641 /* Parse the declarator. */
24642 declarator
24643 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24644 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24645 &ctor_dtor_or_conv_p,
24646 /*parenthesized_p=*/NULL,
24647 /*member_p=*/true,
24648 friend_p, static_p);
24649
24650 /* If something went wrong parsing the declarator, make sure
24651 that we at least consume some tokens. */
24652 if (declarator == cp_error_declarator)
24653 {
24654 /* Skip to the end of the statement. */
24655 cp_parser_skip_to_end_of_statement (parser);
24656 /* If the next token is not a semicolon, that is
24657 probably because we just skipped over the body of
24658 a function. So, we consume a semicolon if
24659 present, but do not issue an error message if it
24660 is not present. */
24661 if (cp_lexer_next_token_is (parser->lexer,
24662 CPP_SEMICOLON))
24663 cp_lexer_consume_token (parser->lexer);
24664 goto out;
24665 }
24666
24667 if (declares_class_or_enum & 2)
24668 cp_parser_check_for_definition_in_return_type
24669 (declarator, decl_specifiers.type,
24670 decl_specifiers.locations[ds_type_spec]);
24671
24672 /* Look for an asm-specification. */
24673 asm_specification = cp_parser_asm_specification_opt (parser);
24674 /* Look for attributes that apply to the declaration. */
24675 attributes = cp_parser_attributes_opt (parser);
24676 /* Remember which attributes are prefix attributes and
24677 which are not. */
24678 first_attribute = attributes;
24679 /* Combine the attributes. */
24680 attributes = attr_chainon (prefix_attributes, attributes);
24681
24682 /* If it's an `=', then we have a constant-initializer or a
24683 pure-specifier. It is not correct to parse the
24684 initializer before registering the member declaration
24685 since the member declaration should be in scope while
24686 its initializer is processed. However, the rest of the
24687 front end does not yet provide an interface that allows
24688 us to handle this correctly. */
24689 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24690 {
24691 /* In [class.mem]:
24692
24693 A pure-specifier shall be used only in the declaration of
24694 a virtual function.
24695
24696 A member-declarator can contain a constant-initializer
24697 only if it declares a static member of integral or
24698 enumeration type.
24699
24700 Therefore, if the DECLARATOR is for a function, we look
24701 for a pure-specifier; otherwise, we look for a
24702 constant-initializer. When we call `grokfield', it will
24703 perform more stringent semantics checks. */
24704 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24705 if (function_declarator_p (declarator)
24706 || (decl_specifiers.type
24707 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24708 && declarator->kind == cdk_id
24709 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24710 == FUNCTION_TYPE)))
24711 initializer = cp_parser_pure_specifier (parser);
24712 else if (decl_specifiers.storage_class != sc_static)
24713 initializer = cp_parser_save_nsdmi (parser);
24714 else if (cxx_dialect >= cxx11)
24715 {
24716 bool nonconst;
24717 /* Don't require a constant rvalue in C++11, since we
24718 might want a reference constant. We'll enforce
24719 constancy later. */
24720 cp_lexer_consume_token (parser->lexer);
24721 /* Parse the initializer. */
24722 initializer = cp_parser_initializer_clause (parser,
24723 &nonconst);
24724 }
24725 else
24726 /* Parse the initializer. */
24727 initializer = cp_parser_constant_initializer (parser);
24728 }
24729 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24730 && !function_declarator_p (declarator))
24731 {
24732 bool x;
24733 if (decl_specifiers.storage_class != sc_static)
24734 initializer = cp_parser_save_nsdmi (parser);
24735 else
24736 initializer = cp_parser_initializer (parser, &x, &x);
24737 }
24738 /* Otherwise, there is no initializer. */
24739 else
24740 initializer = NULL_TREE;
24741
24742 /* See if we are probably looking at a function
24743 definition. We are certainly not looking at a
24744 member-declarator. Calling `grokfield' has
24745 side-effects, so we must not do it unless we are sure
24746 that we are looking at a member-declarator. */
24747 if (cp_parser_token_starts_function_definition_p
24748 (cp_lexer_peek_token (parser->lexer)))
24749 {
24750 /* The grammar does not allow a pure-specifier to be
24751 used when a member function is defined. (It is
24752 possible that this fact is an oversight in the
24753 standard, since a pure function may be defined
24754 outside of the class-specifier. */
24755 if (initializer && initializer_token_start)
24756 error_at (initializer_token_start->location,
24757 "pure-specifier on function-definition");
24758 decl = cp_parser_save_member_function_body (parser,
24759 &decl_specifiers,
24760 declarator,
24761 attributes);
24762 if (parser->fully_implicit_function_template_p)
24763 decl = finish_fully_implicit_template (parser, decl);
24764 /* If the member was not a friend, declare it here. */
24765 if (!friend_p)
24766 finish_member_declaration (decl);
24767 /* Peek at the next token. */
24768 token = cp_lexer_peek_token (parser->lexer);
24769 /* If the next token is a semicolon, consume it. */
24770 if (token->type == CPP_SEMICOLON)
24771 {
24772 location_t semicolon_loc
24773 = cp_lexer_consume_token (parser->lexer)->location;
24774 gcc_rich_location richloc (semicolon_loc);
24775 richloc.add_fixit_remove ();
24776 warning_at (&richloc, OPT_Wextra_semi,
24777 "extra %<;%> after in-class "
24778 "function definition");
24779 }
24780 goto out;
24781 }
24782 else
24783 if (declarator->kind == cdk_function)
24784 declarator->id_loc = token->location;
24785 /* Create the declaration. */
24786 decl = grokfield (declarator, &decl_specifiers,
24787 initializer, /*init_const_expr_p=*/true,
24788 asm_specification, attributes);
24789 if (parser->fully_implicit_function_template_p)
24790 {
24791 if (friend_p)
24792 finish_fully_implicit_template (parser, 0);
24793 else
24794 decl = finish_fully_implicit_template (parser, decl);
24795 }
24796 }
24797
24798 cp_finalize_omp_declare_simd (parser, decl);
24799 cp_finalize_oacc_routine (parser, decl, false);
24800
24801 /* Reset PREFIX_ATTRIBUTES. */
24802 if (attributes != error_mark_node)
24803 {
24804 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24805 attributes = TREE_CHAIN (attributes);
24806 if (attributes)
24807 TREE_CHAIN (attributes) = NULL_TREE;
24808 }
24809
24810 /* If there is any qualification still in effect, clear it
24811 now; we will be starting fresh with the next declarator. */
24812 parser->scope = NULL_TREE;
24813 parser->qualifying_scope = NULL_TREE;
24814 parser->object_scope = NULL_TREE;
24815 /* If it's a `,', then there are more declarators. */
24816 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24817 {
24818 cp_lexer_consume_token (parser->lexer);
24819 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24820 {
24821 cp_token *token = cp_lexer_previous_token (parser->lexer);
24822 gcc_rich_location richloc (token->location);
24823 richloc.add_fixit_remove ();
24824 error_at (&richloc, "stray %<,%> at end of "
24825 "member declaration");
24826 }
24827 }
24828 /* If the next token isn't a `;', then we have a parse error. */
24829 else if (cp_lexer_next_token_is_not (parser->lexer,
24830 CPP_SEMICOLON))
24831 {
24832 /* The next token might be a ways away from where the
24833 actual semicolon is missing. Find the previous token
24834 and use that for our error position. */
24835 cp_token *token = cp_lexer_previous_token (parser->lexer);
24836 gcc_rich_location richloc (token->location);
24837 richloc.add_fixit_insert_after (";");
24838 error_at (&richloc, "expected %<;%> at end of "
24839 "member declaration");
24840
24841 /* Assume that the user meant to provide a semicolon. If
24842 we were to cp_parser_skip_to_end_of_statement, we might
24843 skip to a semicolon inside a member function definition
24844 and issue nonsensical error messages. */
24845 assume_semicolon = true;
24846 }
24847
24848 if (decl)
24849 {
24850 /* Add DECL to the list of members. */
24851 if (!friend_p
24852 /* Explicitly include, eg, NSDMIs, for better error
24853 recovery (c++/58650). */
24854 || !DECL_DECLARES_FUNCTION_P (decl))
24855 finish_member_declaration (decl);
24856
24857 if (TREE_CODE (decl) == FUNCTION_DECL)
24858 cp_parser_save_default_args (parser, decl);
24859 else if (TREE_CODE (decl) == FIELD_DECL
24860 && DECL_INITIAL (decl))
24861 /* Add DECL to the queue of NSDMI to be parsed later. */
24862 vec_safe_push (unparsed_nsdmis, decl);
24863 }
24864
24865 if (assume_semicolon)
24866 goto out;
24867 }
24868 }
24869
24870 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24871 out:
24872 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24873 }
24874
24875 /* Parse a pure-specifier.
24876
24877 pure-specifier:
24878 = 0
24879
24880 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24881 Otherwise, ERROR_MARK_NODE is returned. */
24882
24883 static tree
24884 cp_parser_pure_specifier (cp_parser* parser)
24885 {
24886 cp_token *token;
24887
24888 /* Look for the `=' token. */
24889 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24890 return error_mark_node;
24891 /* Look for the `0' token. */
24892 token = cp_lexer_peek_token (parser->lexer);
24893
24894 if (token->type == CPP_EOF
24895 || token->type == CPP_PRAGMA_EOL)
24896 return error_mark_node;
24897
24898 cp_lexer_consume_token (parser->lexer);
24899
24900 /* Accept = default or = delete in c++0x mode. */
24901 if (token->keyword == RID_DEFAULT
24902 || token->keyword == RID_DELETE)
24903 {
24904 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24905 return token->u.value;
24906 }
24907
24908 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24909 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24910 {
24911 cp_parser_error (parser,
24912 "invalid pure specifier (only %<= 0%> is allowed)");
24913 cp_parser_skip_to_end_of_statement (parser);
24914 return error_mark_node;
24915 }
24916 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24917 {
24918 error_at (token->location, "templates may not be %<virtual%>");
24919 return error_mark_node;
24920 }
24921
24922 return integer_zero_node;
24923 }
24924
24925 /* Parse a constant-initializer.
24926
24927 constant-initializer:
24928 = constant-expression
24929
24930 Returns a representation of the constant-expression. */
24931
24932 static tree
24933 cp_parser_constant_initializer (cp_parser* parser)
24934 {
24935 /* Look for the `=' token. */
24936 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24937 return error_mark_node;
24938
24939 /* It is invalid to write:
24940
24941 struct S { static const int i = { 7 }; };
24942
24943 */
24944 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24945 {
24946 cp_parser_error (parser,
24947 "a brace-enclosed initializer is not allowed here");
24948 /* Consume the opening brace. */
24949 matching_braces braces;
24950 braces.consume_open (parser);
24951 /* Skip the initializer. */
24952 cp_parser_skip_to_closing_brace (parser);
24953 /* Look for the trailing `}'. */
24954 braces.require_close (parser);
24955
24956 return error_mark_node;
24957 }
24958
24959 return cp_parser_constant_expression (parser);
24960 }
24961
24962 /* Derived classes [gram.class.derived] */
24963
24964 /* Parse a base-clause.
24965
24966 base-clause:
24967 : base-specifier-list
24968
24969 base-specifier-list:
24970 base-specifier ... [opt]
24971 base-specifier-list , base-specifier ... [opt]
24972
24973 Returns a TREE_LIST representing the base-classes, in the order in
24974 which they were declared. The representation of each node is as
24975 described by cp_parser_base_specifier.
24976
24977 In the case that no bases are specified, this function will return
24978 NULL_TREE, not ERROR_MARK_NODE. */
24979
24980 static tree
24981 cp_parser_base_clause (cp_parser* parser)
24982 {
24983 tree bases = NULL_TREE;
24984
24985 /* Look for the `:' that begins the list. */
24986 cp_parser_require (parser, CPP_COLON, RT_COLON);
24987
24988 /* Scan the base-specifier-list. */
24989 while (true)
24990 {
24991 cp_token *token;
24992 tree base;
24993 bool pack_expansion_p = false;
24994
24995 /* Look for the base-specifier. */
24996 base = cp_parser_base_specifier (parser);
24997 /* Look for the (optional) ellipsis. */
24998 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24999 {
25000 /* Consume the `...'. */
25001 cp_lexer_consume_token (parser->lexer);
25002
25003 pack_expansion_p = true;
25004 }
25005
25006 /* Add BASE to the front of the list. */
25007 if (base && base != error_mark_node)
25008 {
25009 if (pack_expansion_p)
25010 /* Make this a pack expansion type. */
25011 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
25012
25013 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
25014 {
25015 TREE_CHAIN (base) = bases;
25016 bases = base;
25017 }
25018 }
25019 /* Peek at the next token. */
25020 token = cp_lexer_peek_token (parser->lexer);
25021 /* If it's not a comma, then the list is complete. */
25022 if (token->type != CPP_COMMA)
25023 break;
25024 /* Consume the `,'. */
25025 cp_lexer_consume_token (parser->lexer);
25026 }
25027
25028 /* PARSER->SCOPE may still be non-NULL at this point, if the last
25029 base class had a qualified name. However, the next name that
25030 appears is certainly not qualified. */
25031 parser->scope = NULL_TREE;
25032 parser->qualifying_scope = NULL_TREE;
25033 parser->object_scope = NULL_TREE;
25034
25035 return nreverse (bases);
25036 }
25037
25038 /* Parse a base-specifier.
25039
25040 base-specifier:
25041 :: [opt] nested-name-specifier [opt] class-name
25042 virtual access-specifier [opt] :: [opt] nested-name-specifier
25043 [opt] class-name
25044 access-specifier virtual [opt] :: [opt] nested-name-specifier
25045 [opt] class-name
25046
25047 Returns a TREE_LIST. The TREE_PURPOSE will be one of
25048 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25049 indicate the specifiers provided. The TREE_VALUE will be a TYPE
25050 (or the ERROR_MARK_NODE) indicating the type that was specified. */
25051
25052 static tree
25053 cp_parser_base_specifier (cp_parser* parser)
25054 {
25055 cp_token *token;
25056 bool done = false;
25057 bool virtual_p = false;
25058 bool duplicate_virtual_error_issued_p = false;
25059 bool duplicate_access_error_issued_p = false;
25060 bool class_scope_p, template_p;
25061 tree access = access_default_node;
25062 tree type;
25063
25064 /* Process the optional `virtual' and `access-specifier'. */
25065 while (!done)
25066 {
25067 /* Peek at the next token. */
25068 token = cp_lexer_peek_token (parser->lexer);
25069 /* Process `virtual'. */
25070 switch (token->keyword)
25071 {
25072 case RID_VIRTUAL:
25073 /* If `virtual' appears more than once, issue an error. */
25074 if (virtual_p && !duplicate_virtual_error_issued_p)
25075 {
25076 cp_parser_error (parser,
25077 "%<virtual%> specified more than once in base-specifier");
25078 duplicate_virtual_error_issued_p = true;
25079 }
25080
25081 virtual_p = true;
25082
25083 /* Consume the `virtual' token. */
25084 cp_lexer_consume_token (parser->lexer);
25085
25086 break;
25087
25088 case RID_PUBLIC:
25089 case RID_PROTECTED:
25090 case RID_PRIVATE:
25091 /* If more than one access specifier appears, issue an
25092 error. */
25093 if (access != access_default_node
25094 && !duplicate_access_error_issued_p)
25095 {
25096 cp_parser_error (parser,
25097 "more than one access specifier in base-specifier");
25098 duplicate_access_error_issued_p = true;
25099 }
25100
25101 access = ridpointers[(int) token->keyword];
25102
25103 /* Consume the access-specifier. */
25104 cp_lexer_consume_token (parser->lexer);
25105
25106 break;
25107
25108 default:
25109 done = true;
25110 break;
25111 }
25112 }
25113 /* It is not uncommon to see programs mechanically, erroneously, use
25114 the 'typename' keyword to denote (dependent) qualified types
25115 as base classes. */
25116 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25117 {
25118 token = cp_lexer_peek_token (parser->lexer);
25119 if (!processing_template_decl)
25120 error_at (token->location,
25121 "keyword %<typename%> not allowed outside of templates");
25122 else
25123 error_at (token->location,
25124 "keyword %<typename%> not allowed in this context "
25125 "(the base class is implicitly a type)");
25126 cp_lexer_consume_token (parser->lexer);
25127 }
25128
25129 /* Look for the optional `::' operator. */
25130 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
25131 /* Look for the nested-name-specifier. The simplest way to
25132 implement:
25133
25134 [temp.res]
25135
25136 The keyword `typename' is not permitted in a base-specifier or
25137 mem-initializer; in these contexts a qualified name that
25138 depends on a template-parameter is implicitly assumed to be a
25139 type name.
25140
25141 is to pretend that we have seen the `typename' keyword at this
25142 point. */
25143 cp_parser_nested_name_specifier_opt (parser,
25144 /*typename_keyword_p=*/true,
25145 /*check_dependency_p=*/true,
25146 /*type_p=*/true,
25147 /*is_declaration=*/true);
25148 /* If the base class is given by a qualified name, assume that names
25149 we see are type names or templates, as appropriate. */
25150 class_scope_p = (parser->scope && TYPE_P (parser->scope));
25151 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
25152
25153 if (!parser->scope
25154 && cp_lexer_next_token_is_decltype (parser->lexer))
25155 /* DR 950 allows decltype as a base-specifier. */
25156 type = cp_parser_decltype (parser);
25157 else
25158 {
25159 /* Otherwise, look for the class-name. */
25160 type = cp_parser_class_name (parser,
25161 class_scope_p,
25162 template_p,
25163 typename_type,
25164 /*check_dependency_p=*/true,
25165 /*class_head_p=*/false,
25166 /*is_declaration=*/true);
25167 type = TREE_TYPE (type);
25168 }
25169
25170 if (type == error_mark_node)
25171 return error_mark_node;
25172
25173 return finish_base_specifier (type, access, virtual_p);
25174 }
25175
25176 /* Exception handling [gram.exception] */
25177
25178 /* Parse an (optional) noexcept-specification.
25179
25180 noexcept-specification:
25181 noexcept ( constant-expression ) [opt]
25182
25183 If no noexcept-specification is present, returns NULL_TREE.
25184 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25185 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25186 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25187 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25188 in which case a boolean condition is returned instead. */
25189
25190 static tree
25191 cp_parser_noexcept_specification_opt (cp_parser* parser,
25192 bool require_constexpr,
25193 bool* consumed_expr,
25194 bool return_cond)
25195 {
25196 cp_token *token;
25197 const char *saved_message;
25198
25199 /* Peek at the next token. */
25200 token = cp_lexer_peek_token (parser->lexer);
25201
25202 /* Is it a noexcept-specification? */
25203 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
25204 {
25205 tree expr;
25206 cp_lexer_consume_token (parser->lexer);
25207
25208 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
25209 {
25210 matching_parens parens;
25211 parens.consume_open (parser);
25212
25213 tree save_ccp = current_class_ptr;
25214 tree save_ccr = current_class_ref;
25215
25216 if (current_class_type)
25217 inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
25218
25219 if (require_constexpr)
25220 {
25221 /* Types may not be defined in an exception-specification. */
25222 saved_message = parser->type_definition_forbidden_message;
25223 parser->type_definition_forbidden_message
25224 = G_("types may not be defined in an exception-specification");
25225
25226 bool non_constant_p;
25227 expr
25228 = cp_parser_constant_expression (parser,
25229 /*allow_non_constant=*/true,
25230 &non_constant_p);
25231 if (non_constant_p
25232 && !require_potential_rvalue_constant_expression (expr))
25233 {
25234 expr = NULL_TREE;
25235 return_cond = true;
25236 }
25237
25238 /* Restore the saved message. */
25239 parser->type_definition_forbidden_message = saved_message;
25240 }
25241 else
25242 {
25243 expr = cp_parser_expression (parser);
25244 *consumed_expr = true;
25245 }
25246
25247 parens.require_close (parser);
25248
25249 current_class_ptr = save_ccp;
25250 current_class_ref = save_ccr;
25251 }
25252 else
25253 {
25254 expr = boolean_true_node;
25255 if (!require_constexpr)
25256 *consumed_expr = false;
25257 }
25258
25259 /* We cannot build a noexcept-spec right away because this will check
25260 that expr is a constexpr. */
25261 if (!return_cond)
25262 return build_noexcept_spec (expr, tf_warning_or_error);
25263 else
25264 return expr;
25265 }
25266 else
25267 return NULL_TREE;
25268 }
25269
25270 /* Parse an (optional) exception-specification.
25271
25272 exception-specification:
25273 throw ( type-id-list [opt] )
25274
25275 Returns a TREE_LIST representing the exception-specification. The
25276 TREE_VALUE of each node is a type. */
25277
25278 static tree
25279 cp_parser_exception_specification_opt (cp_parser* parser)
25280 {
25281 cp_token *token;
25282 tree type_id_list;
25283 const char *saved_message;
25284
25285 /* Peek at the next token. */
25286 token = cp_lexer_peek_token (parser->lexer);
25287
25288 /* Is it a noexcept-specification? */
25289 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
25290 false);
25291 if (type_id_list != NULL_TREE)
25292 return type_id_list;
25293
25294 /* If it's not `throw', then there's no exception-specification. */
25295 if (!cp_parser_is_keyword (token, RID_THROW))
25296 return NULL_TREE;
25297
25298 location_t loc = token->location;
25299
25300 /* Consume the `throw'. */
25301 cp_lexer_consume_token (parser->lexer);
25302
25303 /* Look for the `('. */
25304 matching_parens parens;
25305 parens.require_open (parser);
25306
25307 /* Peek at the next token. */
25308 token = cp_lexer_peek_token (parser->lexer);
25309 /* If it's not a `)', then there is a type-id-list. */
25310 if (token->type != CPP_CLOSE_PAREN)
25311 {
25312 /* Types may not be defined in an exception-specification. */
25313 saved_message = parser->type_definition_forbidden_message;
25314 parser->type_definition_forbidden_message
25315 = G_("types may not be defined in an exception-specification");
25316 /* Parse the type-id-list. */
25317 type_id_list = cp_parser_type_id_list (parser);
25318 /* Restore the saved message. */
25319 parser->type_definition_forbidden_message = saved_message;
25320
25321 if (cxx_dialect >= cxx17)
25322 {
25323 error_at (loc, "ISO C++17 does not allow dynamic exception "
25324 "specifications");
25325 type_id_list = NULL_TREE;
25326 }
25327 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
25328 warning_at (loc, OPT_Wdeprecated,
25329 "dynamic exception specifications are deprecated in "
25330 "C++11");
25331 }
25332 /* In C++17, throw() is equivalent to noexcept (true). throw()
25333 is deprecated in C++11 and above as well, but is still widely used,
25334 so don't warn about it yet. */
25335 else if (cxx_dialect >= cxx17)
25336 type_id_list = noexcept_true_spec;
25337 else
25338 type_id_list = empty_except_spec;
25339
25340 /* Look for the `)'. */
25341 parens.require_close (parser);
25342
25343 return type_id_list;
25344 }
25345
25346 /* Parse an (optional) type-id-list.
25347
25348 type-id-list:
25349 type-id ... [opt]
25350 type-id-list , type-id ... [opt]
25351
25352 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25353 in the order that the types were presented. */
25354
25355 static tree
25356 cp_parser_type_id_list (cp_parser* parser)
25357 {
25358 tree types = NULL_TREE;
25359
25360 while (true)
25361 {
25362 cp_token *token;
25363 tree type;
25364
25365 token = cp_lexer_peek_token (parser->lexer);
25366
25367 /* Get the next type-id. */
25368 type = cp_parser_type_id (parser);
25369 /* Check for invalid 'auto'. */
25370 if (flag_concepts && type_uses_auto (type))
25371 {
25372 error_at (token->location,
25373 "invalid use of %<auto%> in exception-specification");
25374 type = error_mark_node;
25375 }
25376 /* Parse the optional ellipsis. */
25377 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25378 {
25379 /* Consume the `...'. */
25380 cp_lexer_consume_token (parser->lexer);
25381
25382 /* Turn the type into a pack expansion expression. */
25383 type = make_pack_expansion (type);
25384 }
25385 /* Add it to the list. */
25386 types = add_exception_specifier (types, type, /*complain=*/1);
25387 /* Peek at the next token. */
25388 token = cp_lexer_peek_token (parser->lexer);
25389 /* If it is not a `,', we are done. */
25390 if (token->type != CPP_COMMA)
25391 break;
25392 /* Consume the `,'. */
25393 cp_lexer_consume_token (parser->lexer);
25394 }
25395
25396 return nreverse (types);
25397 }
25398
25399 /* Parse a try-block.
25400
25401 try-block:
25402 try compound-statement handler-seq */
25403
25404 static tree
25405 cp_parser_try_block (cp_parser* parser)
25406 {
25407 tree try_block;
25408
25409 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
25410 if (parser->in_function_body
25411 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25412 && cxx_dialect < cxx2a)
25413 pedwarn (input_location, 0,
25414 "%<try%> in %<constexpr%> function only "
25415 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
25416
25417 try_block = begin_try_block ();
25418 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
25419 finish_try_block (try_block);
25420 cp_parser_handler_seq (parser);
25421 finish_handler_sequence (try_block);
25422
25423 return try_block;
25424 }
25425
25426 /* Parse a function-try-block.
25427
25428 function-try-block:
25429 try ctor-initializer [opt] function-body handler-seq */
25430
25431 static void
25432 cp_parser_function_try_block (cp_parser* parser)
25433 {
25434 tree compound_stmt;
25435 tree try_block;
25436
25437 /* Look for the `try' keyword. */
25438 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
25439 return;
25440 /* Let the rest of the front end know where we are. */
25441 try_block = begin_function_try_block (&compound_stmt);
25442 /* Parse the function-body. */
25443 cp_parser_ctor_initializer_opt_and_function_body
25444 (parser, /*in_function_try_block=*/true);
25445 /* We're done with the `try' part. */
25446 finish_function_try_block (try_block);
25447 /* Parse the handlers. */
25448 cp_parser_handler_seq (parser);
25449 /* We're done with the handlers. */
25450 finish_function_handler_sequence (try_block, compound_stmt);
25451 }
25452
25453 /* Parse a handler-seq.
25454
25455 handler-seq:
25456 handler handler-seq [opt] */
25457
25458 static void
25459 cp_parser_handler_seq (cp_parser* parser)
25460 {
25461 while (true)
25462 {
25463 cp_token *token;
25464
25465 /* Parse the handler. */
25466 cp_parser_handler (parser);
25467 /* Peek at the next token. */
25468 token = cp_lexer_peek_token (parser->lexer);
25469 /* If it's not `catch' then there are no more handlers. */
25470 if (!cp_parser_is_keyword (token, RID_CATCH))
25471 break;
25472 }
25473 }
25474
25475 /* Parse a handler.
25476
25477 handler:
25478 catch ( exception-declaration ) compound-statement */
25479
25480 static void
25481 cp_parser_handler (cp_parser* parser)
25482 {
25483 tree handler;
25484 tree declaration;
25485
25486 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
25487 handler = begin_handler ();
25488 matching_parens parens;
25489 parens.require_open (parser);
25490 declaration = cp_parser_exception_declaration (parser);
25491 finish_handler_parms (declaration, handler);
25492 parens.require_close (parser);
25493 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
25494 finish_handler (handler);
25495 }
25496
25497 /* Parse an exception-declaration.
25498
25499 exception-declaration:
25500 type-specifier-seq declarator
25501 type-specifier-seq abstract-declarator
25502 type-specifier-seq
25503 ...
25504
25505 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25506 ellipsis variant is used. */
25507
25508 static tree
25509 cp_parser_exception_declaration (cp_parser* parser)
25510 {
25511 cp_decl_specifier_seq type_specifiers;
25512 cp_declarator *declarator;
25513 const char *saved_message;
25514
25515 /* If it's an ellipsis, it's easy to handle. */
25516 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25517 {
25518 /* Consume the `...' token. */
25519 cp_lexer_consume_token (parser->lexer);
25520 return NULL_TREE;
25521 }
25522
25523 /* Types may not be defined in exception-declarations. */
25524 saved_message = parser->type_definition_forbidden_message;
25525 parser->type_definition_forbidden_message
25526 = G_("types may not be defined in exception-declarations");
25527
25528 /* Parse the type-specifier-seq. */
25529 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
25530 /*is_declaration=*/true,
25531 /*is_trailing_return=*/false,
25532 &type_specifiers);
25533 /* If it's a `)', then there is no declarator. */
25534 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25535 declarator = NULL;
25536 else
25537 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
25538 CP_PARSER_FLAGS_NONE,
25539 /*ctor_dtor_or_conv_p=*/NULL,
25540 /*parenthesized_p=*/NULL,
25541 /*member_p=*/false,
25542 /*friend_p=*/false,
25543 /*static_p=*/false);
25544
25545 /* Restore the saved message. */
25546 parser->type_definition_forbidden_message = saved_message;
25547
25548 if (!type_specifiers.any_specifiers_p)
25549 return error_mark_node;
25550
25551 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
25552 }
25553
25554 /* Parse a throw-expression.
25555
25556 throw-expression:
25557 throw assignment-expression [opt]
25558
25559 Returns a THROW_EXPR representing the throw-expression. */
25560
25561 static tree
25562 cp_parser_throw_expression (cp_parser* parser)
25563 {
25564 tree expression;
25565 cp_token* token;
25566
25567 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
25568 token = cp_lexer_peek_token (parser->lexer);
25569 /* Figure out whether or not there is an assignment-expression
25570 following the "throw" keyword. */
25571 if (token->type == CPP_COMMA
25572 || token->type == CPP_SEMICOLON
25573 || token->type == CPP_CLOSE_PAREN
25574 || token->type == CPP_CLOSE_SQUARE
25575 || token->type == CPP_CLOSE_BRACE
25576 || token->type == CPP_COLON)
25577 expression = NULL_TREE;
25578 else
25579 expression = cp_parser_assignment_expression (parser);
25580
25581 return build_throw (expression);
25582 }
25583
25584 /* GNU Extensions */
25585
25586 /* Parse an (optional) asm-specification.
25587
25588 asm-specification:
25589 asm ( string-literal )
25590
25591 If the asm-specification is present, returns a STRING_CST
25592 corresponding to the string-literal. Otherwise, returns
25593 NULL_TREE. */
25594
25595 static tree
25596 cp_parser_asm_specification_opt (cp_parser* parser)
25597 {
25598 cp_token *token;
25599 tree asm_specification;
25600
25601 /* Peek at the next token. */
25602 token = cp_lexer_peek_token (parser->lexer);
25603 /* If the next token isn't the `asm' keyword, then there's no
25604 asm-specification. */
25605 if (!cp_parser_is_keyword (token, RID_ASM))
25606 return NULL_TREE;
25607
25608 /* Consume the `asm' token. */
25609 cp_lexer_consume_token (parser->lexer);
25610 /* Look for the `('. */
25611 matching_parens parens;
25612 parens.require_open (parser);
25613
25614 /* Look for the string-literal. */
25615 asm_specification = cp_parser_string_literal (parser, false, false);
25616
25617 /* Look for the `)'. */
25618 parens.require_close (parser);
25619
25620 return asm_specification;
25621 }
25622
25623 /* Parse an asm-operand-list.
25624
25625 asm-operand-list:
25626 asm-operand
25627 asm-operand-list , asm-operand
25628
25629 asm-operand:
25630 string-literal ( expression )
25631 [ string-literal ] string-literal ( expression )
25632
25633 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25634 each node is the expression. The TREE_PURPOSE is itself a
25635 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25636 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25637 is a STRING_CST for the string literal before the parenthesis. Returns
25638 ERROR_MARK_NODE if any of the operands are invalid. */
25639
25640 static tree
25641 cp_parser_asm_operand_list (cp_parser* parser)
25642 {
25643 tree asm_operands = NULL_TREE;
25644 bool invalid_operands = false;
25645
25646 while (true)
25647 {
25648 tree string_literal;
25649 tree expression;
25650 tree name;
25651
25652 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25653 {
25654 /* Consume the `[' token. */
25655 cp_lexer_consume_token (parser->lexer);
25656 /* Read the operand name. */
25657 name = cp_parser_identifier (parser);
25658 if (name != error_mark_node)
25659 name = build_string (IDENTIFIER_LENGTH (name),
25660 IDENTIFIER_POINTER (name));
25661 /* Look for the closing `]'. */
25662 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25663 }
25664 else
25665 name = NULL_TREE;
25666 /* Look for the string-literal. */
25667 string_literal = cp_parser_string_literal (parser, false, false);
25668
25669 /* Look for the `('. */
25670 matching_parens parens;
25671 parens.require_open (parser);
25672 /* Parse the expression. */
25673 expression = cp_parser_expression (parser);
25674 /* Look for the `)'. */
25675 parens.require_close (parser);
25676
25677 if (name == error_mark_node
25678 || string_literal == error_mark_node
25679 || expression == error_mark_node)
25680 invalid_operands = true;
25681
25682 /* Add this operand to the list. */
25683 asm_operands = tree_cons (build_tree_list (name, string_literal),
25684 expression,
25685 asm_operands);
25686 /* If the next token is not a `,', there are no more
25687 operands. */
25688 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25689 break;
25690 /* Consume the `,'. */
25691 cp_lexer_consume_token (parser->lexer);
25692 }
25693
25694 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25695 }
25696
25697 /* Parse an asm-clobber-list.
25698
25699 asm-clobber-list:
25700 string-literal
25701 asm-clobber-list , string-literal
25702
25703 Returns a TREE_LIST, indicating the clobbers in the order that they
25704 appeared. The TREE_VALUE of each node is a STRING_CST. */
25705
25706 static tree
25707 cp_parser_asm_clobber_list (cp_parser* parser)
25708 {
25709 tree clobbers = NULL_TREE;
25710
25711 while (true)
25712 {
25713 tree string_literal;
25714
25715 /* Look for the string literal. */
25716 string_literal = cp_parser_string_literal (parser, false, false);
25717 /* Add it to the list. */
25718 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25719 /* If the next token is not a `,', then the list is
25720 complete. */
25721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25722 break;
25723 /* Consume the `,' token. */
25724 cp_lexer_consume_token (parser->lexer);
25725 }
25726
25727 return clobbers;
25728 }
25729
25730 /* Parse an asm-label-list.
25731
25732 asm-label-list:
25733 identifier
25734 asm-label-list , identifier
25735
25736 Returns a TREE_LIST, indicating the labels in the order that they
25737 appeared. The TREE_VALUE of each node is a label. */
25738
25739 static tree
25740 cp_parser_asm_label_list (cp_parser* parser)
25741 {
25742 tree labels = NULL_TREE;
25743
25744 while (true)
25745 {
25746 tree identifier, label, name;
25747
25748 /* Look for the identifier. */
25749 identifier = cp_parser_identifier (parser);
25750 if (!error_operand_p (identifier))
25751 {
25752 label = lookup_label (identifier);
25753 if (TREE_CODE (label) == LABEL_DECL)
25754 {
25755 TREE_USED (label) = 1;
25756 check_goto (label);
25757 name = build_string (IDENTIFIER_LENGTH (identifier),
25758 IDENTIFIER_POINTER (identifier));
25759 labels = tree_cons (name, label, labels);
25760 }
25761 }
25762 /* If the next token is not a `,', then the list is
25763 complete. */
25764 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25765 break;
25766 /* Consume the `,' token. */
25767 cp_lexer_consume_token (parser->lexer);
25768 }
25769
25770 return nreverse (labels);
25771 }
25772
25773 /* Return TRUE iff the next tokens in the stream are possibly the
25774 beginning of a GNU extension attribute. */
25775
25776 static bool
25777 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25778 {
25779 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25780 }
25781
25782 /* Return TRUE iff the next tokens in the stream are possibly the
25783 beginning of a standard C++-11 attribute specifier. */
25784
25785 static bool
25786 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25787 {
25788 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25789 }
25790
25791 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25792 beginning of a standard C++-11 attribute specifier. */
25793
25794 static bool
25795 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25796 {
25797 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25798
25799 return (cxx_dialect >= cxx11
25800 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25801 || (token->type == CPP_OPEN_SQUARE
25802 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25803 && token->type == CPP_OPEN_SQUARE)));
25804 }
25805
25806 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25807 beginning of a GNU extension attribute. */
25808
25809 static bool
25810 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25811 {
25812 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25813
25814 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25815 }
25816
25817 /* Return true iff the next tokens can be the beginning of either a
25818 GNU attribute list, or a standard C++11 attribute sequence. */
25819
25820 static bool
25821 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25822 {
25823 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25824 || cp_next_tokens_can_be_std_attribute_p (parser));
25825 }
25826
25827 /* Return true iff the next Nth tokens can be the beginning of either
25828 a GNU attribute list, or a standard C++11 attribute sequence. */
25829
25830 static bool
25831 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25832 {
25833 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25834 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25835 }
25836
25837 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25838 of GNU attributes, or return NULL. */
25839
25840 static tree
25841 cp_parser_attributes_opt (cp_parser *parser)
25842 {
25843 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25844 return cp_parser_gnu_attributes_opt (parser);
25845 return cp_parser_std_attribute_spec_seq (parser);
25846 }
25847
25848 /* Parse an (optional) series of attributes.
25849
25850 attributes:
25851 attributes attribute
25852
25853 attribute:
25854 __attribute__ (( attribute-list [opt] ))
25855
25856 The return value is as for cp_parser_gnu_attribute_list. */
25857
25858 static tree
25859 cp_parser_gnu_attributes_opt (cp_parser* parser)
25860 {
25861 tree attributes = NULL_TREE;
25862
25863 temp_override<bool> cleanup
25864 (parser->auto_is_implicit_function_template_parm_p, false);
25865
25866 while (true)
25867 {
25868 cp_token *token;
25869 tree attribute_list;
25870 bool ok = true;
25871
25872 /* Peek at the next token. */
25873 token = cp_lexer_peek_token (parser->lexer);
25874 /* If it's not `__attribute__', then we're done. */
25875 if (token->keyword != RID_ATTRIBUTE)
25876 break;
25877
25878 /* Consume the `__attribute__' keyword. */
25879 cp_lexer_consume_token (parser->lexer);
25880 /* Look for the two `(' tokens. */
25881 matching_parens outer_parens;
25882 if (!outer_parens.require_open (parser))
25883 ok = false;
25884 matching_parens inner_parens;
25885 if (!inner_parens.require_open (parser))
25886 ok = false;
25887
25888 /* Peek at the next token. */
25889 token = cp_lexer_peek_token (parser->lexer);
25890 if (token->type != CPP_CLOSE_PAREN)
25891 /* Parse the attribute-list. */
25892 attribute_list = cp_parser_gnu_attribute_list (parser);
25893 else
25894 /* If the next token is a `)', then there is no attribute
25895 list. */
25896 attribute_list = NULL;
25897
25898 /* Look for the two `)' tokens. */
25899 if (!inner_parens.require_close (parser))
25900 ok = false;
25901 if (!outer_parens.require_close (parser))
25902 ok = false;
25903 if (!ok)
25904 cp_parser_skip_to_end_of_statement (parser);
25905
25906 /* Add these new attributes to the list. */
25907 attributes = attr_chainon (attributes, attribute_list);
25908 }
25909
25910 return attributes;
25911 }
25912
25913 /* Parse a GNU attribute-list.
25914
25915 attribute-list:
25916 attribute
25917 attribute-list , attribute
25918
25919 attribute:
25920 identifier
25921 identifier ( identifier )
25922 identifier ( identifier , expression-list )
25923 identifier ( expression-list )
25924
25925 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25926 to an attribute. The TREE_PURPOSE of each node is the identifier
25927 indicating which attribute is in use. The TREE_VALUE represents
25928 the arguments, if any. */
25929
25930 static tree
25931 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
25932 {
25933 tree attribute_list = NULL_TREE;
25934 bool save_translate_strings_p = parser->translate_strings_p;
25935
25936 /* Don't create wrapper nodes within attributes: the
25937 handlers don't know how to handle them. */
25938 auto_suppress_location_wrappers sentinel;
25939
25940 parser->translate_strings_p = false;
25941 while (true)
25942 {
25943 cp_token *token;
25944 tree identifier;
25945 tree attribute;
25946
25947 /* Look for the identifier. We also allow keywords here; for
25948 example `__attribute__ ((const))' is legal. */
25949 token = cp_lexer_peek_token (parser->lexer);
25950 if (token->type == CPP_NAME
25951 || token->type == CPP_KEYWORD)
25952 {
25953 tree arguments = NULL_TREE;
25954
25955 /* Consume the token, but save it since we need it for the
25956 SIMD enabled function parsing. */
25957 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25958
25959 /* Save away the identifier that indicates which attribute
25960 this is. */
25961 identifier = (token->type == CPP_KEYWORD)
25962 /* For keywords, use the canonical spelling, not the
25963 parsed identifier. */
25964 ? ridpointers[(int) token->keyword]
25965 : id_token->u.value;
25966
25967 identifier = canonicalize_attr_name (identifier);
25968 attribute = build_tree_list (identifier, NULL_TREE);
25969
25970 /* Peek at the next token. */
25971 token = cp_lexer_peek_token (parser->lexer);
25972 /* If it's an `(', then parse the attribute arguments. */
25973 if (token->type == CPP_OPEN_PAREN)
25974 {
25975 vec<tree, va_gc> *vec;
25976 int attr_flag = (attribute_takes_identifier_p (identifier)
25977 ? id_attr : normal_attr);
25978 vec = cp_parser_parenthesized_expression_list
25979 (parser, attr_flag, /*cast_p=*/false,
25980 /*allow_expansion_p=*/false,
25981 /*non_constant_p=*/NULL);
25982 if (vec == NULL)
25983 arguments = error_mark_node;
25984 else
25985 {
25986 arguments = build_tree_list_vec (vec);
25987 release_tree_vector (vec);
25988 }
25989 /* Save the arguments away. */
25990 TREE_VALUE (attribute) = arguments;
25991 }
25992
25993 if (arguments != error_mark_node)
25994 {
25995 /* Add this attribute to the list. */
25996 TREE_CHAIN (attribute) = attribute_list;
25997 attribute_list = attribute;
25998 }
25999
26000 token = cp_lexer_peek_token (parser->lexer);
26001 }
26002 /* Unless EXACTLY_ONE is set look for more attributes.
26003 If the next token isn't a `,', we're done. */
26004 if (exactly_one || token->type != CPP_COMMA)
26005 break;
26006
26007 /* Consume the comma and keep going. */
26008 cp_lexer_consume_token (parser->lexer);
26009 }
26010 parser->translate_strings_p = save_translate_strings_p;
26011
26012 /* We built up the list in reverse order. */
26013 return nreverse (attribute_list);
26014 }
26015
26016 /* Parse a standard C++11 attribute.
26017
26018 The returned representation is a TREE_LIST which TREE_PURPOSE is
26019 the scoped name of the attribute, and the TREE_VALUE is its
26020 arguments list.
26021
26022 Note that the scoped name of the attribute is itself a TREE_LIST
26023 which TREE_PURPOSE is the namespace of the attribute, and
26024 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
26025 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
26026 and which TREE_PURPOSE is directly the attribute name.
26027
26028 Clients of the attribute code should use get_attribute_namespace
26029 and get_attribute_name to get the actual namespace and name of
26030 attributes, regardless of their being GNU or C++11 attributes.
26031
26032 attribute:
26033 attribute-token attribute-argument-clause [opt]
26034
26035 attribute-token:
26036 identifier
26037 attribute-scoped-token
26038
26039 attribute-scoped-token:
26040 attribute-namespace :: identifier
26041
26042 attribute-namespace:
26043 identifier
26044
26045 attribute-argument-clause:
26046 ( balanced-token-seq )
26047
26048 balanced-token-seq:
26049 balanced-token [opt]
26050 balanced-token-seq balanced-token
26051
26052 balanced-token:
26053 ( balanced-token-seq )
26054 [ balanced-token-seq ]
26055 { balanced-token-seq }. */
26056
26057 static tree
26058 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
26059 {
26060 tree attribute, attr_id = NULL_TREE, arguments;
26061 cp_token *token;
26062
26063 temp_override<bool> cleanup
26064 (parser->auto_is_implicit_function_template_parm_p, false);
26065
26066 /* First, parse name of the attribute, a.k.a attribute-token. */
26067
26068 token = cp_lexer_peek_token (parser->lexer);
26069 if (token->type == CPP_NAME)
26070 attr_id = token->u.value;
26071 else if (token->type == CPP_KEYWORD)
26072 attr_id = ridpointers[(int) token->keyword];
26073 else if (token->flags & NAMED_OP)
26074 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26075
26076 if (attr_id == NULL_TREE)
26077 return NULL_TREE;
26078
26079 cp_lexer_consume_token (parser->lexer);
26080
26081 token = cp_lexer_peek_token (parser->lexer);
26082 if (token->type == CPP_SCOPE)
26083 {
26084 /* We are seeing a scoped attribute token. */
26085
26086 cp_lexer_consume_token (parser->lexer);
26087 if (attr_ns)
26088 error_at (token->location, "attribute using prefix used together "
26089 "with scoped attribute token");
26090 attr_ns = attr_id;
26091
26092 token = cp_lexer_consume_token (parser->lexer);
26093 if (token->type == CPP_NAME)
26094 attr_id = token->u.value;
26095 else if (token->type == CPP_KEYWORD)
26096 attr_id = ridpointers[(int) token->keyword];
26097 else if (token->flags & NAMED_OP)
26098 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26099 else
26100 {
26101 error_at (token->location,
26102 "expected an identifier for the attribute name");
26103 return error_mark_node;
26104 }
26105
26106 attr_ns = canonicalize_attr_name (attr_ns);
26107 attr_id = canonicalize_attr_name (attr_id);
26108 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26109 NULL_TREE);
26110 token = cp_lexer_peek_token (parser->lexer);
26111 }
26112 else if (attr_ns)
26113 {
26114 attr_ns = canonicalize_attr_name (attr_ns);
26115 attr_id = canonicalize_attr_name (attr_id);
26116 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26117 NULL_TREE);
26118 }
26119 else
26120 {
26121 attr_id = canonicalize_attr_name (attr_id);
26122 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
26123 NULL_TREE);
26124 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
26125 but no longer: we have to be able to tell [[noreturn]] and
26126 __attribute__((noreturn)) apart. */
26127 /* C++14 deprecated attribute is equivalent to GNU's. */
26128 if (is_attribute_p ("deprecated", attr_id))
26129 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26130 /* C++17 fallthrough attribute is equivalent to GNU's. */
26131 else if (is_attribute_p ("fallthrough", attr_id))
26132 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26133 /* Transactional Memory TS optimize_for_synchronized attribute is
26134 equivalent to GNU transaction_callable. */
26135 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
26136 TREE_PURPOSE (attribute)
26137 = get_identifier ("transaction_callable");
26138 /* Transactional Memory attributes are GNU attributes. */
26139 else if (tm_attr_to_mask (attr_id))
26140 TREE_PURPOSE (attribute) = attr_id;
26141 }
26142
26143 /* Now parse the optional argument clause of the attribute. */
26144
26145 if (token->type != CPP_OPEN_PAREN)
26146 return attribute;
26147
26148 {
26149 vec<tree, va_gc> *vec;
26150 int attr_flag = normal_attr;
26151
26152 /* Maybe we don't expect to see any arguments for this attribute. */
26153 const attribute_spec *as
26154 = lookup_attribute_spec (TREE_PURPOSE (attribute));
26155 if (as && as->max_length == 0)
26156 {
26157 error_at (token->location, "%qE attribute does not take any arguments",
26158 attr_id);
26159 cp_parser_skip_to_closing_parenthesis (parser,
26160 /*recovering=*/true,
26161 /*or_comma=*/false,
26162 /*consume_paren=*/true);
26163 return error_mark_node;
26164 }
26165
26166 if (attr_ns == gnu_identifier
26167 && attribute_takes_identifier_p (attr_id))
26168 /* A GNU attribute that takes an identifier in parameter. */
26169 attr_flag = id_attr;
26170
26171 vec = cp_parser_parenthesized_expression_list
26172 (parser, attr_flag, /*cast_p=*/false,
26173 /*allow_expansion_p=*/true,
26174 /*non_constant_p=*/NULL);
26175 if (vec == NULL)
26176 arguments = error_mark_node;
26177 else
26178 {
26179 arguments = build_tree_list_vec (vec);
26180 release_tree_vector (vec);
26181 }
26182
26183 if (arguments == error_mark_node)
26184 attribute = error_mark_node;
26185 else
26186 TREE_VALUE (attribute) = arguments;
26187 }
26188
26189 return attribute;
26190 }
26191
26192 /* Check that the attribute ATTRIBUTE appears at most once in the
26193 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
26194 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
26195 isn't implemented yet in GCC. */
26196
26197 static void
26198 cp_parser_check_std_attribute (tree attributes, tree attribute)
26199 {
26200 if (attributes)
26201 {
26202 tree name = get_attribute_name (attribute);
26203 if (is_attribute_p ("noreturn", name)
26204 && lookup_attribute ("noreturn", attributes))
26205 error ("attribute %<noreturn%> can appear at most once "
26206 "in an attribute-list");
26207 else if (is_attribute_p ("deprecated", name)
26208 && lookup_attribute ("deprecated", attributes))
26209 error ("attribute %<deprecated%> can appear at most once "
26210 "in an attribute-list");
26211 }
26212 }
26213
26214 /* Parse a list of standard C++-11 attributes.
26215
26216 attribute-list:
26217 attribute [opt]
26218 attribute-list , attribute[opt]
26219 attribute ...
26220 attribute-list , attribute ...
26221 */
26222
26223 static tree
26224 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
26225 {
26226 tree attributes = NULL_TREE, attribute = NULL_TREE;
26227 cp_token *token = NULL;
26228
26229 while (true)
26230 {
26231 attribute = cp_parser_std_attribute (parser, attr_ns);
26232 if (attribute == error_mark_node)
26233 break;
26234 if (attribute != NULL_TREE)
26235 {
26236 cp_parser_check_std_attribute (attributes, attribute);
26237 TREE_CHAIN (attribute) = attributes;
26238 attributes = attribute;
26239 }
26240 token = cp_lexer_peek_token (parser->lexer);
26241 if (token->type == CPP_ELLIPSIS)
26242 {
26243 cp_lexer_consume_token (parser->lexer);
26244 if (attribute == NULL_TREE)
26245 error_at (token->location,
26246 "expected attribute before %<...%>");
26247 else
26248 {
26249 tree pack = make_pack_expansion (TREE_VALUE (attribute));
26250 if (pack == error_mark_node)
26251 return error_mark_node;
26252 TREE_VALUE (attribute) = pack;
26253 }
26254 token = cp_lexer_peek_token (parser->lexer);
26255 }
26256 if (token->type != CPP_COMMA)
26257 break;
26258 cp_lexer_consume_token (parser->lexer);
26259 }
26260 attributes = nreverse (attributes);
26261 return attributes;
26262 }
26263
26264 /* Parse a standard C++-11 attribute specifier.
26265
26266 attribute-specifier:
26267 [ [ attribute-using-prefix [opt] attribute-list ] ]
26268 alignment-specifier
26269
26270 attribute-using-prefix:
26271 using attribute-namespace :
26272
26273 alignment-specifier:
26274 alignas ( type-id ... [opt] )
26275 alignas ( alignment-expression ... [opt] ). */
26276
26277 static tree
26278 cp_parser_std_attribute_spec (cp_parser *parser)
26279 {
26280 tree attributes = NULL_TREE;
26281 cp_token *token = cp_lexer_peek_token (parser->lexer);
26282
26283 if (token->type == CPP_OPEN_SQUARE
26284 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
26285 {
26286 tree attr_ns = NULL_TREE;
26287
26288 cp_lexer_consume_token (parser->lexer);
26289 cp_lexer_consume_token (parser->lexer);
26290
26291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26292 {
26293 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26294 if (token->type == CPP_NAME)
26295 attr_ns = token->u.value;
26296 else if (token->type == CPP_KEYWORD)
26297 attr_ns = ridpointers[(int) token->keyword];
26298 else if (token->flags & NAMED_OP)
26299 attr_ns = get_identifier (cpp_type2name (token->type,
26300 token->flags));
26301 if (attr_ns
26302 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
26303 {
26304 if (cxx_dialect < cxx17
26305 && !in_system_header_at (input_location))
26306 pedwarn (input_location, 0,
26307 "attribute using prefix only available "
26308 "with %<-std=c++17%> or %<-std=gnu++17%>");
26309
26310 cp_lexer_consume_token (parser->lexer);
26311 cp_lexer_consume_token (parser->lexer);
26312 cp_lexer_consume_token (parser->lexer);
26313 }
26314 else
26315 attr_ns = NULL_TREE;
26316 }
26317
26318 attributes = cp_parser_std_attribute_list (parser, attr_ns);
26319
26320 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
26321 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
26322 cp_parser_skip_to_end_of_statement (parser);
26323 else
26324 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26325 when we are sure that we have actually parsed them. */
26326 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26327 }
26328 else
26329 {
26330 tree alignas_expr;
26331
26332 /* Look for an alignment-specifier. */
26333
26334 token = cp_lexer_peek_token (parser->lexer);
26335
26336 if (token->type != CPP_KEYWORD
26337 || token->keyword != RID_ALIGNAS)
26338 return NULL_TREE;
26339
26340 cp_lexer_consume_token (parser->lexer);
26341 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26342
26343 matching_parens parens;
26344 if (!parens.require_open (parser))
26345 return error_mark_node;
26346
26347 cp_parser_parse_tentatively (parser);
26348 alignas_expr = cp_parser_type_id (parser);
26349
26350 if (!cp_parser_parse_definitely (parser))
26351 {
26352 alignas_expr = cp_parser_assignment_expression (parser);
26353 if (alignas_expr == error_mark_node)
26354 cp_parser_skip_to_end_of_statement (parser);
26355 if (alignas_expr == NULL_TREE
26356 || alignas_expr == error_mark_node)
26357 return alignas_expr;
26358 }
26359
26360 alignas_expr = cxx_alignas_expr (alignas_expr);
26361 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
26362
26363 /* Handle alignas (pack...). */
26364 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26365 {
26366 cp_lexer_consume_token (parser->lexer);
26367 alignas_expr = make_pack_expansion (alignas_expr);
26368 }
26369
26370 /* Something went wrong, so don't build the attribute. */
26371 if (alignas_expr == error_mark_node)
26372 return error_mark_node;
26373
26374 if (!parens.require_close (parser))
26375 return error_mark_node;
26376
26377 /* Build the C++-11 representation of an 'aligned'
26378 attribute. */
26379 attributes
26380 = build_tree_list (build_tree_list (gnu_identifier,
26381 aligned_identifier), alignas_expr);
26382 }
26383
26384 return attributes;
26385 }
26386
26387 /* Parse a standard C++-11 attribute-specifier-seq.
26388
26389 attribute-specifier-seq:
26390 attribute-specifier-seq [opt] attribute-specifier
26391 */
26392
26393 static tree
26394 cp_parser_std_attribute_spec_seq (cp_parser *parser)
26395 {
26396 tree attr_specs = NULL_TREE;
26397 tree attr_last = NULL_TREE;
26398
26399 /* Don't create wrapper nodes within attributes: the
26400 handlers don't know how to handle them. */
26401 auto_suppress_location_wrappers sentinel;
26402
26403 while (true)
26404 {
26405 tree attr_spec = cp_parser_std_attribute_spec (parser);
26406 if (attr_spec == NULL_TREE)
26407 break;
26408 if (attr_spec == error_mark_node)
26409 return error_mark_node;
26410
26411 if (attr_last)
26412 TREE_CHAIN (attr_last) = attr_spec;
26413 else
26414 attr_specs = attr_last = attr_spec;
26415 attr_last = tree_last (attr_last);
26416 }
26417
26418 return attr_specs;
26419 }
26420
26421 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26422 return index of the first token after balanced-token, or N on failure. */
26423
26424 static size_t
26425 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
26426 {
26427 size_t orig_n = n;
26428 int nparens = 0, nbraces = 0, nsquares = 0;
26429 do
26430 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
26431 {
26432 case CPP_PRAGMA_EOL:
26433 if (!parser->lexer->in_pragma)
26434 break;
26435 /* FALLTHRU */
26436 case CPP_EOF:
26437 /* Ran out of tokens. */
26438 return orig_n;
26439 case CPP_OPEN_PAREN:
26440 ++nparens;
26441 break;
26442 case CPP_OPEN_BRACE:
26443 ++nbraces;
26444 break;
26445 case CPP_OPEN_SQUARE:
26446 ++nsquares;
26447 break;
26448 case CPP_CLOSE_PAREN:
26449 --nparens;
26450 break;
26451 case CPP_CLOSE_BRACE:
26452 --nbraces;
26453 break;
26454 case CPP_CLOSE_SQUARE:
26455 --nsquares;
26456 break;
26457 default:
26458 break;
26459 }
26460 while (nparens || nbraces || nsquares);
26461 return n;
26462 }
26463
26464 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26465 return index of the first token after the GNU attribute tokens, or N on
26466 failure. */
26467
26468 static size_t
26469 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
26470 {
26471 while (true)
26472 {
26473 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
26474 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
26475 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
26476 break;
26477
26478 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
26479 if (n2 == n + 2)
26480 break;
26481 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
26482 break;
26483 n = n2 + 1;
26484 }
26485 return n;
26486 }
26487
26488 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26489 next token), return index of the first token after the standard C++11
26490 attribute tokens, or N on failure. */
26491
26492 static size_t
26493 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
26494 {
26495 while (true)
26496 {
26497 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
26498 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
26499 {
26500 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26501 if (n2 == n + 1)
26502 break;
26503 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
26504 break;
26505 n = n2 + 1;
26506 }
26507 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
26508 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
26509 {
26510 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26511 if (n2 == n + 1)
26512 break;
26513 n = n2;
26514 }
26515 else
26516 break;
26517 }
26518 return n;
26519 }
26520
26521 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26522 as the next token), return index of the first token after the attribute
26523 tokens, or N on failure. */
26524
26525 static size_t
26526 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
26527 {
26528 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
26529 return cp_parser_skip_gnu_attributes_opt (parser, n);
26530 return cp_parser_skip_std_attribute_spec_seq (parser, n);
26531 }
26532
26533 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26534 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26535 current value of the PEDANTIC flag, regardless of whether or not
26536 the `__extension__' keyword is present. The caller is responsible
26537 for restoring the value of the PEDANTIC flag. */
26538
26539 static bool
26540 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
26541 {
26542 /* Save the old value of the PEDANTIC flag. */
26543 *saved_pedantic = pedantic;
26544
26545 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
26546 {
26547 /* Consume the `__extension__' token. */
26548 cp_lexer_consume_token (parser->lexer);
26549 /* We're not being pedantic while the `__extension__' keyword is
26550 in effect. */
26551 pedantic = 0;
26552
26553 return true;
26554 }
26555
26556 return false;
26557 }
26558
26559 /* Parse a label declaration.
26560
26561 label-declaration:
26562 __label__ label-declarator-seq ;
26563
26564 label-declarator-seq:
26565 identifier , label-declarator-seq
26566 identifier */
26567
26568 static void
26569 cp_parser_label_declaration (cp_parser* parser)
26570 {
26571 /* Look for the `__label__' keyword. */
26572 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
26573
26574 while (true)
26575 {
26576 tree identifier;
26577
26578 /* Look for an identifier. */
26579 identifier = cp_parser_identifier (parser);
26580 /* If we failed, stop. */
26581 if (identifier == error_mark_node)
26582 break;
26583 /* Declare it as a label. */
26584 finish_label_decl (identifier);
26585 /* If the next token is a `;', stop. */
26586 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26587 break;
26588 /* Look for the `,' separating the label declarations. */
26589 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
26590 }
26591
26592 /* Look for the final `;'. */
26593 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26594 }
26595
26596 // -------------------------------------------------------------------------- //
26597 // Requires Clause
26598
26599 // Parse a requires clause.
26600 //
26601 // requires-clause:
26602 // 'requires' logical-or-expression
26603 //
26604 // The required logical-or-expression must be a constant expression. Note
26605 // that we don't check that the expression is constepxr here. We defer until
26606 // we analyze constraints and then, we only check atomic constraints.
26607 static tree
26608 cp_parser_requires_clause (cp_parser *parser)
26609 {
26610 // Parse the requires clause so that it is not automatically folded.
26611 ++processing_template_decl;
26612 tree expr = cp_parser_binary_expression (parser, false, false,
26613 PREC_NOT_OPERATOR, NULL);
26614 if (check_for_bare_parameter_packs (expr))
26615 expr = error_mark_node;
26616 --processing_template_decl;
26617 return expr;
26618 }
26619
26620 // Optionally parse a requires clause:
26621 static tree
26622 cp_parser_requires_clause_opt (cp_parser *parser)
26623 {
26624 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26625 if (tok->keyword != RID_REQUIRES)
26626 {
26627 if (!flag_concepts && tok->type == CPP_NAME
26628 && tok->u.value == ridpointers[RID_REQUIRES])
26629 {
26630 error_at (cp_lexer_peek_token (parser->lexer)->location,
26631 "%<requires%> only available with %<-fconcepts%>");
26632 /* Parse and discard the requires-clause. */
26633 cp_lexer_consume_token (parser->lexer);
26634 cp_parser_requires_clause (parser);
26635 }
26636 return NULL_TREE;
26637 }
26638 cp_lexer_consume_token (parser->lexer);
26639 return cp_parser_requires_clause (parser);
26640 }
26641
26642
26643 /*---------------------------------------------------------------------------
26644 Requires expressions
26645 ---------------------------------------------------------------------------*/
26646
26647 /* Parse a requires expression
26648
26649 requirement-expression:
26650 'requires' requirement-parameter-list [opt] requirement-body */
26651 static tree
26652 cp_parser_requires_expression (cp_parser *parser)
26653 {
26654 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26655 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26656
26657 /* A requires-expression shall appear only within a concept
26658 definition or a requires-clause.
26659
26660 TODO: Implement this diagnostic correctly. */
26661 if (!processing_template_decl)
26662 {
26663 error_at (loc, "a requires expression cannot appear outside a template");
26664 cp_parser_skip_to_end_of_statement (parser);
26665 return error_mark_node;
26666 }
26667
26668 tree parms, reqs;
26669 {
26670 /* Local parameters are delared as variables within the scope
26671 of the expression. They are not visible past the end of
26672 the expression. Expressions within the requires-expression
26673 are unevaluated. */
26674 struct scope_sentinel
26675 {
26676 scope_sentinel ()
26677 {
26678 ++cp_unevaluated_operand;
26679 begin_scope (sk_block, NULL_TREE);
26680 }
26681
26682 ~scope_sentinel ()
26683 {
26684 pop_bindings_and_leave_scope ();
26685 --cp_unevaluated_operand;
26686 }
26687 } s;
26688
26689 /* Parse the optional parameter list. */
26690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26691 {
26692 parms = cp_parser_requirement_parameter_list (parser);
26693 if (parms == error_mark_node)
26694 return error_mark_node;
26695 }
26696 else
26697 parms = NULL_TREE;
26698
26699 /* Parse the requirement body. */
26700 reqs = cp_parser_requirement_body (parser);
26701 if (reqs == error_mark_node)
26702 return error_mark_node;
26703 }
26704
26705 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26706 the parm chain. */
26707 grokparms (parms, &parms);
26708 return finish_requires_expr (parms, reqs);
26709 }
26710
26711 /* Parse a parameterized requirement.
26712
26713 requirement-parameter-list:
26714 '(' parameter-declaration-clause ')' */
26715 static tree
26716 cp_parser_requirement_parameter_list (cp_parser *parser)
26717 {
26718 matching_parens parens;
26719 if (!parens.require_open (parser))
26720 return error_mark_node;
26721
26722 tree parms
26723 = cp_parser_parameter_declaration_clause (parser, CP_PARSER_FLAGS_NONE);
26724
26725 if (!parens.require_close (parser))
26726 return error_mark_node;
26727
26728 return parms;
26729 }
26730
26731 /* Parse the body of a requirement.
26732
26733 requirement-body:
26734 '{' requirement-list '}' */
26735 static tree
26736 cp_parser_requirement_body (cp_parser *parser)
26737 {
26738 matching_braces braces;
26739 if (!braces.require_open (parser))
26740 return error_mark_node;
26741
26742 tree reqs = cp_parser_requirement_list (parser);
26743
26744 if (!braces.require_close (parser))
26745 return error_mark_node;
26746
26747 return reqs;
26748 }
26749
26750 /* Parse a list of requirements.
26751
26752 requirement-list:
26753 requirement
26754 requirement-list ';' requirement[opt] */
26755 static tree
26756 cp_parser_requirement_list (cp_parser *parser)
26757 {
26758 tree result = NULL_TREE;
26759 while (true)
26760 {
26761 tree req = cp_parser_requirement (parser);
26762 if (req == error_mark_node)
26763 return error_mark_node;
26764
26765 result = tree_cons (NULL_TREE, req, result);
26766
26767 /* If we see a semi-colon, consume it. */
26768 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26769 cp_lexer_consume_token (parser->lexer);
26770
26771 /* Stop processing at the end of the list. */
26772 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26773 break;
26774 }
26775
26776 /* Reverse the order of requirements so they are analyzed in
26777 declaration order. */
26778 return nreverse (result);
26779 }
26780
26781 /* Parse a syntactic requirement or type requirement.
26782
26783 requirement:
26784 simple-requirement
26785 compound-requirement
26786 type-requirement
26787 nested-requirement */
26788 static tree
26789 cp_parser_requirement (cp_parser *parser)
26790 {
26791 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26792 return cp_parser_compound_requirement (parser);
26793 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26794 return cp_parser_type_requirement (parser);
26795 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26796 return cp_parser_nested_requirement (parser);
26797 else
26798 return cp_parser_simple_requirement (parser);
26799 }
26800
26801 /* Parse a simple requirement.
26802
26803 simple-requirement:
26804 expression ';' */
26805 static tree
26806 cp_parser_simple_requirement (cp_parser *parser)
26807 {
26808 tree expr = cp_parser_expression (parser, NULL, false, false);
26809 if (!expr || expr == error_mark_node)
26810 return error_mark_node;
26811
26812 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26813 return error_mark_node;
26814
26815 return finish_simple_requirement (expr);
26816 }
26817
26818 /* Parse a type requirement
26819
26820 type-requirement
26821 nested-name-specifier [opt] required-type-name ';'
26822
26823 required-type-name:
26824 type-name
26825 'template' [opt] simple-template-id */
26826 static tree
26827 cp_parser_type_requirement (cp_parser *parser)
26828 {
26829 cp_lexer_consume_token (parser->lexer);
26830
26831 // Save the scope before parsing name specifiers.
26832 tree saved_scope = parser->scope;
26833 tree saved_object_scope = parser->object_scope;
26834 tree saved_qualifying_scope = parser->qualifying_scope;
26835 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26836 cp_parser_nested_name_specifier_opt (parser,
26837 /*typename_keyword_p=*/true,
26838 /*check_dependency_p=*/false,
26839 /*type_p=*/true,
26840 /*is_declaration=*/false);
26841
26842 tree type;
26843 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26844 {
26845 cp_lexer_consume_token (parser->lexer);
26846 type = cp_parser_template_id (parser,
26847 /*template_keyword_p=*/true,
26848 /*check_dependency=*/false,
26849 /*tag_type=*/none_type,
26850 /*is_declaration=*/false);
26851 type = make_typename_type (parser->scope, type, typename_type,
26852 /*complain=*/tf_error);
26853 }
26854 else
26855 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26856
26857 if (TREE_CODE (type) == TYPE_DECL)
26858 type = TREE_TYPE (type);
26859
26860 parser->scope = saved_scope;
26861 parser->object_scope = saved_object_scope;
26862 parser->qualifying_scope = saved_qualifying_scope;
26863
26864 if (type == error_mark_node)
26865 cp_parser_skip_to_end_of_statement (parser);
26866
26867 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26868 return error_mark_node;
26869 if (type == error_mark_node)
26870 return error_mark_node;
26871
26872 return finish_type_requirement (type);
26873 }
26874
26875 /* Parse a compound requirement
26876
26877 compound-requirement:
26878 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26879 static tree
26880 cp_parser_compound_requirement (cp_parser *parser)
26881 {
26882 /* Parse an expression enclosed in '{ }'s. */
26883 matching_braces braces;
26884 if (!braces.require_open (parser))
26885 return error_mark_node;
26886
26887 tree expr = cp_parser_expression (parser, NULL, false, false);
26888 if (!expr || expr == error_mark_node)
26889 return error_mark_node;
26890
26891 if (!braces.require_close (parser))
26892 return error_mark_node;
26893
26894 /* Parse the optional noexcept. */
26895 bool noexcept_p = false;
26896 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26897 {
26898 cp_lexer_consume_token (parser->lexer);
26899 noexcept_p = true;
26900 }
26901
26902 /* Parse the optional trailing return type. */
26903 tree type = NULL_TREE;
26904 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26905 {
26906 cp_lexer_consume_token (parser->lexer);
26907 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26908 parser->in_result_type_constraint_p = true;
26909 type = cp_parser_trailing_type_id (parser);
26910 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26911 if (type == error_mark_node)
26912 return error_mark_node;
26913 }
26914
26915 return finish_compound_requirement (expr, type, noexcept_p);
26916 }
26917
26918 /* Parse a nested requirement. This is the same as a requires clause.
26919
26920 nested-requirement:
26921 requires-clause */
26922 static tree
26923 cp_parser_nested_requirement (cp_parser *parser)
26924 {
26925 cp_lexer_consume_token (parser->lexer);
26926 tree req = cp_parser_requires_clause (parser);
26927 if (req == error_mark_node)
26928 return error_mark_node;
26929 return finish_nested_requirement (req);
26930 }
26931
26932 /* Support Functions */
26933
26934 /* Return the appropriate prefer_type argument for lookup_name_real based on
26935 tag_type and template_mem_access. */
26936
26937 static inline int
26938 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26939 {
26940 /* DR 141: When looking in the current enclosing context for a template-name
26941 after -> or ., only consider class templates. */
26942 if (template_mem_access)
26943 return 2;
26944 switch (tag_type)
26945 {
26946 case none_type: return 0; // No preference.
26947 case scope_type: return 1; // Type or namespace.
26948 default: return 2; // Type only.
26949 }
26950 }
26951
26952 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26953 NAME should have one of the representations used for an
26954 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26955 is returned. If PARSER->SCOPE is a dependent type, then a
26956 SCOPE_REF is returned.
26957
26958 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26959 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26960 was formed. Abstractly, such entities should not be passed to this
26961 function, because they do not need to be looked up, but it is
26962 simpler to check for this special case here, rather than at the
26963 call-sites.
26964
26965 In cases not explicitly covered above, this function returns a
26966 DECL, OVERLOAD, or baselink representing the result of the lookup.
26967 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26968 is returned.
26969
26970 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26971 (e.g., "struct") that was used. In that case bindings that do not
26972 refer to types are ignored.
26973
26974 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26975 ignored.
26976
26977 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26978 are ignored.
26979
26980 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26981 types.
26982
26983 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26984 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26985 NULL_TREE otherwise. */
26986
26987 static cp_expr
26988 cp_parser_lookup_name (cp_parser *parser, tree name,
26989 enum tag_types tag_type,
26990 bool is_template,
26991 bool is_namespace,
26992 bool check_dependency,
26993 tree *ambiguous_decls,
26994 location_t name_location)
26995 {
26996 tree decl;
26997 tree object_type = parser->context->object_type;
26998
26999 /* Assume that the lookup will be unambiguous. */
27000 if (ambiguous_decls)
27001 *ambiguous_decls = NULL_TREE;
27002
27003 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
27004 no longer valid. Note that if we are parsing tentatively, and
27005 the parse fails, OBJECT_TYPE will be automatically restored. */
27006 parser->context->object_type = NULL_TREE;
27007
27008 if (name == error_mark_node)
27009 return error_mark_node;
27010
27011 /* A template-id has already been resolved; there is no lookup to
27012 do. */
27013 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
27014 return name;
27015 if (BASELINK_P (name))
27016 {
27017 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
27018 == TEMPLATE_ID_EXPR);
27019 return name;
27020 }
27021
27022 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
27023 it should already have been checked to make sure that the name
27024 used matches the type being destroyed. */
27025 if (TREE_CODE (name) == BIT_NOT_EXPR)
27026 {
27027 tree type;
27028
27029 /* Figure out to which type this destructor applies. */
27030 if (parser->scope)
27031 type = parser->scope;
27032 else if (object_type)
27033 type = object_type;
27034 else
27035 type = current_class_type;
27036 /* If that's not a class type, there is no destructor. */
27037 if (!type || !CLASS_TYPE_P (type))
27038 return error_mark_node;
27039
27040 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
27041 lazily_declare_fn (sfk_destructor, type);
27042
27043 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
27044 return dtor;
27045
27046 return error_mark_node;
27047 }
27048
27049 /* By this point, the NAME should be an ordinary identifier. If
27050 the id-expression was a qualified name, the qualifying scope is
27051 stored in PARSER->SCOPE at this point. */
27052 gcc_assert (identifier_p (name));
27053
27054 /* Perform the lookup. */
27055 if (parser->scope)
27056 {
27057 bool dependent_p;
27058
27059 if (parser->scope == error_mark_node)
27060 return error_mark_node;
27061
27062 /* If the SCOPE is dependent, the lookup must be deferred until
27063 the template is instantiated -- unless we are explicitly
27064 looking up names in uninstantiated templates. Even then, we
27065 cannot look up the name if the scope is not a class type; it
27066 might, for example, be a template type parameter. */
27067 dependent_p = (TYPE_P (parser->scope)
27068 && dependent_scope_p (parser->scope));
27069 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
27070 && dependent_p)
27071 /* Defer lookup. */
27072 decl = error_mark_node;
27073 else
27074 {
27075 tree pushed_scope = NULL_TREE;
27076
27077 /* If PARSER->SCOPE is a dependent type, then it must be a
27078 class type, and we must not be checking dependencies;
27079 otherwise, we would have processed this lookup above. So
27080 that PARSER->SCOPE is not considered a dependent base by
27081 lookup_member, we must enter the scope here. */
27082 if (dependent_p)
27083 pushed_scope = push_scope (parser->scope);
27084
27085 /* If the PARSER->SCOPE is a template specialization, it
27086 may be instantiated during name lookup. In that case,
27087 errors may be issued. Even if we rollback the current
27088 tentative parse, those errors are valid. */
27089 decl = lookup_qualified_name (parser->scope, name,
27090 prefer_type_arg (tag_type),
27091 /*complain=*/true);
27092
27093 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
27094 lookup result and the nested-name-specifier nominates a class C:
27095 * if the name specified after the nested-name-specifier, when
27096 looked up in C, is the injected-class-name of C (Clause 9), or
27097 * if the name specified after the nested-name-specifier is the
27098 same as the identifier or the simple-template-id's template-
27099 name in the last component of the nested-name-specifier,
27100 the name is instead considered to name the constructor of
27101 class C. [ Note: for example, the constructor is not an
27102 acceptable lookup result in an elaborated-type-specifier so
27103 the constructor would not be used in place of the
27104 injected-class-name. --end note ] Such a constructor name
27105 shall be used only in the declarator-id of a declaration that
27106 names a constructor or in a using-declaration. */
27107 if (tag_type == none_type
27108 && DECL_SELF_REFERENCE_P (decl)
27109 && same_type_p (DECL_CONTEXT (decl), parser->scope))
27110 decl = lookup_qualified_name (parser->scope, ctor_identifier,
27111 prefer_type_arg (tag_type),
27112 /*complain=*/true);
27113
27114 /* If we have a single function from a using decl, pull it out. */
27115 if (TREE_CODE (decl) == OVERLOAD
27116 && !really_overloaded_fn (decl))
27117 decl = OVL_FUNCTION (decl);
27118
27119 if (pushed_scope)
27120 pop_scope (pushed_scope);
27121 }
27122
27123 /* If the scope is a dependent type and either we deferred lookup or
27124 we did lookup but didn't find the name, rememeber the name. */
27125 if (decl == error_mark_node && TYPE_P (parser->scope)
27126 && dependent_type_p (parser->scope))
27127 {
27128 if (tag_type)
27129 {
27130 tree type;
27131
27132 /* The resolution to Core Issue 180 says that `struct
27133 A::B' should be considered a type-name, even if `A'
27134 is dependent. */
27135 type = make_typename_type (parser->scope, name, tag_type,
27136 /*complain=*/tf_error);
27137 if (type != error_mark_node)
27138 decl = TYPE_NAME (type);
27139 }
27140 else if (is_template
27141 && (cp_parser_next_token_ends_template_argument_p (parser)
27142 || cp_lexer_next_token_is (parser->lexer,
27143 CPP_CLOSE_PAREN)))
27144 decl = make_unbound_class_template (parser->scope,
27145 name, NULL_TREE,
27146 /*complain=*/tf_error);
27147 else
27148 decl = build_qualified_name (/*type=*/NULL_TREE,
27149 parser->scope, name,
27150 is_template);
27151 }
27152 parser->qualifying_scope = parser->scope;
27153 parser->object_scope = NULL_TREE;
27154 }
27155 else if (object_type)
27156 {
27157 /* Look up the name in the scope of the OBJECT_TYPE, unless the
27158 OBJECT_TYPE is not a class. */
27159 if (CLASS_TYPE_P (object_type))
27160 /* If the OBJECT_TYPE is a template specialization, it may
27161 be instantiated during name lookup. In that case, errors
27162 may be issued. Even if we rollback the current tentative
27163 parse, those errors are valid. */
27164 decl = lookup_member (object_type,
27165 name,
27166 /*protect=*/0,
27167 prefer_type_arg (tag_type),
27168 tf_warning_or_error);
27169 else
27170 decl = NULL_TREE;
27171
27172 if (!decl)
27173 /* Look it up in the enclosing context. DR 141: When looking for a
27174 template-name after -> or ., only consider class templates. */
27175 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
27176 /*nonclass=*/0,
27177 /*block_p=*/true, is_namespace, 0);
27178 if (object_type == unknown_type_node)
27179 /* The object is type-dependent, so we can't look anything up; we used
27180 this to get the DR 141 behavior. */
27181 object_type = NULL_TREE;
27182 parser->object_scope = object_type;
27183 parser->qualifying_scope = NULL_TREE;
27184 }
27185 else
27186 {
27187 decl = lookup_name_real (name, prefer_type_arg (tag_type),
27188 /*nonclass=*/0,
27189 /*block_p=*/true, is_namespace, 0);
27190 parser->qualifying_scope = NULL_TREE;
27191 parser->object_scope = NULL_TREE;
27192 }
27193
27194 /* If the lookup failed, let our caller know. */
27195 if (!decl || decl == error_mark_node)
27196 return error_mark_node;
27197
27198 /* Pull out the template from an injected-class-name (or multiple). */
27199 if (is_template)
27200 decl = maybe_get_template_decl_from_type_decl (decl);
27201
27202 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
27203 if (TREE_CODE (decl) == TREE_LIST)
27204 {
27205 if (ambiguous_decls)
27206 *ambiguous_decls = decl;
27207 /* The error message we have to print is too complicated for
27208 cp_parser_error, so we incorporate its actions directly. */
27209 if (!cp_parser_simulate_error (parser))
27210 {
27211 error_at (name_location, "reference to %qD is ambiguous",
27212 name);
27213 print_candidates (decl);
27214 }
27215 return error_mark_node;
27216 }
27217
27218 gcc_assert (DECL_P (decl)
27219 || TREE_CODE (decl) == OVERLOAD
27220 || TREE_CODE (decl) == SCOPE_REF
27221 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
27222 || BASELINK_P (decl));
27223
27224 /* If we have resolved the name of a member declaration, check to
27225 see if the declaration is accessible. When the name resolves to
27226 set of overloaded functions, accessibility is checked when
27227 overload resolution is done.
27228
27229 During an explicit instantiation, access is not checked at all,
27230 as per [temp.explicit]. */
27231 if (DECL_P (decl))
27232 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
27233
27234 maybe_record_typedef_use (decl);
27235
27236 return cp_expr (decl, name_location);
27237 }
27238
27239 /* Like cp_parser_lookup_name, but for use in the typical case where
27240 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27241 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
27242
27243 static tree
27244 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
27245 {
27246 return cp_parser_lookup_name (parser, name,
27247 none_type,
27248 /*is_template=*/false,
27249 /*is_namespace=*/false,
27250 /*check_dependency=*/true,
27251 /*ambiguous_decls=*/NULL,
27252 location);
27253 }
27254
27255 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27256 the current context, return the TYPE_DECL. If TAG_NAME_P is
27257 true, the DECL indicates the class being defined in a class-head,
27258 or declared in an elaborated-type-specifier.
27259
27260 Otherwise, return DECL. */
27261
27262 static tree
27263 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
27264 {
27265 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27266 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27267
27268 struct A {
27269 template <typename T> struct B;
27270 };
27271
27272 template <typename T> struct A::B {};
27273
27274 Similarly, in an elaborated-type-specifier:
27275
27276 namespace N { struct X{}; }
27277
27278 struct A {
27279 template <typename T> friend struct N::X;
27280 };
27281
27282 However, if the DECL refers to a class type, and we are in
27283 the scope of the class, then the name lookup automatically
27284 finds the TYPE_DECL created by build_self_reference rather
27285 than a TEMPLATE_DECL. For example, in:
27286
27287 template <class T> struct S {
27288 S s;
27289 };
27290
27291 there is no need to handle such case. */
27292
27293 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
27294 return DECL_TEMPLATE_RESULT (decl);
27295
27296 return decl;
27297 }
27298
27299 /* If too many, or too few, template-parameter lists apply to the
27300 declarator, issue an error message. Returns TRUE if all went well,
27301 and FALSE otherwise. */
27302
27303 static bool
27304 cp_parser_check_declarator_template_parameters (cp_parser* parser,
27305 cp_declarator *declarator,
27306 location_t declarator_location)
27307 {
27308 switch (declarator->kind)
27309 {
27310 case cdk_id:
27311 {
27312 unsigned num_templates = 0;
27313 tree scope = declarator->u.id.qualifying_scope;
27314 bool template_id_p = false;
27315
27316 if (scope)
27317 num_templates = num_template_headers_for_class (scope);
27318 else if (TREE_CODE (declarator->u.id.unqualified_name)
27319 == TEMPLATE_ID_EXPR)
27320 {
27321 /* If the DECLARATOR has the form `X<y>' then it uses one
27322 additional level of template parameters. */
27323 ++num_templates;
27324 template_id_p = true;
27325 }
27326
27327 return cp_parser_check_template_parameters
27328 (parser, num_templates, template_id_p, declarator_location,
27329 declarator);
27330 }
27331
27332 case cdk_function:
27333 case cdk_array:
27334 case cdk_pointer:
27335 case cdk_reference:
27336 case cdk_ptrmem:
27337 return (cp_parser_check_declarator_template_parameters
27338 (parser, declarator->declarator, declarator_location));
27339
27340 case cdk_decomp:
27341 case cdk_error:
27342 return true;
27343
27344 default:
27345 gcc_unreachable ();
27346 }
27347 return false;
27348 }
27349
27350 /* NUM_TEMPLATES were used in the current declaration. If that is
27351 invalid, return FALSE and issue an error messages. Otherwise,
27352 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27353 declarator and we can print more accurate diagnostics. */
27354
27355 static bool
27356 cp_parser_check_template_parameters (cp_parser* parser,
27357 unsigned num_templates,
27358 bool template_id_p,
27359 location_t location,
27360 cp_declarator *declarator)
27361 {
27362 /* If there are the same number of template classes and parameter
27363 lists, that's OK. */
27364 if (parser->num_template_parameter_lists == num_templates)
27365 return true;
27366 /* If there are more, but only one more, and the name ends in an identifier,
27367 then we are declaring a primary template. That's OK too. */
27368 if (!template_id_p
27369 && parser->num_template_parameter_lists == num_templates + 1)
27370 return true;
27371 /* If there are more template classes than parameter lists, we have
27372 something like:
27373
27374 template <class T> void S<T>::R<T>::f (); */
27375 if (parser->num_template_parameter_lists < num_templates)
27376 {
27377 if (declarator && !current_function_decl)
27378 error_at (location, "specializing member %<%T::%E%> "
27379 "requires %<template<>%> syntax",
27380 declarator->u.id.qualifying_scope,
27381 declarator->u.id.unqualified_name);
27382 else if (declarator)
27383 error_at (location, "invalid declaration of %<%T::%E%>",
27384 declarator->u.id.qualifying_scope,
27385 declarator->u.id.unqualified_name);
27386 else
27387 error_at (location, "too few template-parameter-lists");
27388 return false;
27389 }
27390 /* Otherwise, there are too many template parameter lists. We have
27391 something like:
27392
27393 template <class T> template <class U> void S::f(); */
27394 error_at (location, "too many template-parameter-lists");
27395 return false;
27396 }
27397
27398 /* Parse an optional `::' token indicating that the following name is
27399 from the global namespace. If so, PARSER->SCOPE is set to the
27400 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27401 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27402 Returns the new value of PARSER->SCOPE, if the `::' token is
27403 present, and NULL_TREE otherwise. */
27404
27405 static tree
27406 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
27407 {
27408 cp_token *token;
27409
27410 /* Peek at the next token. */
27411 token = cp_lexer_peek_token (parser->lexer);
27412 /* If we're looking at a `::' token then we're starting from the
27413 global namespace, not our current location. */
27414 if (token->type == CPP_SCOPE)
27415 {
27416 /* Consume the `::' token. */
27417 cp_lexer_consume_token (parser->lexer);
27418 /* Set the SCOPE so that we know where to start the lookup. */
27419 parser->scope = global_namespace;
27420 parser->qualifying_scope = global_namespace;
27421 parser->object_scope = NULL_TREE;
27422
27423 return parser->scope;
27424 }
27425 else if (!current_scope_valid_p)
27426 {
27427 parser->scope = NULL_TREE;
27428 parser->qualifying_scope = NULL_TREE;
27429 parser->object_scope = NULL_TREE;
27430 }
27431
27432 return NULL_TREE;
27433 }
27434
27435 /* Returns TRUE if the upcoming token sequence is the start of a
27436 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27437 declarator is preceded by the `friend' specifier. The parser flags FLAGS
27438 is used to control type-specifier parsing. */
27439
27440 static bool
27441 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
27442 bool friend_p)
27443 {
27444 bool constructor_p;
27445 bool outside_class_specifier_p;
27446 tree nested_name_specifier;
27447 cp_token *next_token;
27448
27449 /* The common case is that this is not a constructor declarator, so
27450 try to avoid doing lots of work if at all possible. It's not
27451 valid declare a constructor at function scope. */
27452 if (parser->in_function_body)
27453 return false;
27454 /* And only certain tokens can begin a constructor declarator. */
27455 next_token = cp_lexer_peek_token (parser->lexer);
27456 if (next_token->type != CPP_NAME
27457 && next_token->type != CPP_SCOPE
27458 && next_token->type != CPP_NESTED_NAME_SPECIFIER
27459 && next_token->type != CPP_TEMPLATE_ID)
27460 return false;
27461
27462 /* Parse tentatively; we are going to roll back all of the tokens
27463 consumed here. */
27464 cp_parser_parse_tentatively (parser);
27465 /* Assume that we are looking at a constructor declarator. */
27466 constructor_p = true;
27467
27468 /* Look for the optional `::' operator. */
27469 cp_parser_global_scope_opt (parser,
27470 /*current_scope_valid_p=*/false);
27471 /* Look for the nested-name-specifier. */
27472 nested_name_specifier
27473 = (cp_parser_nested_name_specifier_opt (parser,
27474 /*typename_keyword_p=*/false,
27475 /*check_dependency_p=*/false,
27476 /*type_p=*/false,
27477 /*is_declaration=*/false));
27478
27479 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27480 if (nested_name_specifier
27481 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
27482 {
27483 tree s = resolve_typename_type (nested_name_specifier,
27484 /*only_current_p=*/false);
27485 if (TREE_CODE (s) != TYPENAME_TYPE)
27486 nested_name_specifier = s;
27487 }
27488
27489 outside_class_specifier_p = (!at_class_scope_p ()
27490 || !TYPE_BEING_DEFINED (current_class_type)
27491 || friend_p);
27492
27493 /* Outside of a class-specifier, there must be a
27494 nested-name-specifier. Except in C++17 mode, where we
27495 might be declaring a guiding declaration. */
27496 if (!nested_name_specifier && outside_class_specifier_p
27497 && cxx_dialect < cxx17)
27498 constructor_p = false;
27499 else if (nested_name_specifier == error_mark_node)
27500 constructor_p = false;
27501
27502 /* If we have a class scope, this is easy; DR 147 says that S::S always
27503 names the constructor, and no other qualified name could. */
27504 if (constructor_p && nested_name_specifier
27505 && CLASS_TYPE_P (nested_name_specifier))
27506 {
27507 tree id = cp_parser_unqualified_id (parser,
27508 /*template_keyword_p=*/false,
27509 /*check_dependency_p=*/false,
27510 /*declarator_p=*/true,
27511 /*optional_p=*/false);
27512 if (is_overloaded_fn (id))
27513 id = DECL_NAME (get_first_fn (id));
27514 if (!constructor_name_p (id, nested_name_specifier))
27515 constructor_p = false;
27516 }
27517 /* If we still think that this might be a constructor-declarator,
27518 look for a class-name. */
27519 else if (constructor_p)
27520 {
27521 /* If we have:
27522
27523 template <typename T> struct S {
27524 S();
27525 };
27526
27527 we must recognize that the nested `S' names a class. */
27528 if (cxx_dialect >= cxx17)
27529 cp_parser_parse_tentatively (parser);
27530
27531 tree type_decl;
27532 type_decl = cp_parser_class_name (parser,
27533 /*typename_keyword_p=*/false,
27534 /*template_keyword_p=*/false,
27535 none_type,
27536 /*check_dependency_p=*/false,
27537 /*class_head_p=*/false,
27538 /*is_declaration=*/false);
27539
27540 if (cxx_dialect >= cxx17
27541 && !cp_parser_parse_definitely (parser))
27542 {
27543 type_decl = NULL_TREE;
27544 tree tmpl = cp_parser_template_name (parser,
27545 /*template_keyword*/false,
27546 /*check_dependency_p*/false,
27547 /*is_declaration*/false,
27548 none_type,
27549 /*is_identifier*/NULL);
27550 if (DECL_CLASS_TEMPLATE_P (tmpl)
27551 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27552 /* It's a deduction guide, return true. */;
27553 else
27554 cp_parser_simulate_error (parser);
27555 }
27556
27557 /* If there was no class-name, then this is not a constructor.
27558 Otherwise, if we are in a class-specifier and we aren't
27559 handling a friend declaration, check that its type matches
27560 current_class_type (c++/38313). Note: error_mark_node
27561 is left alone for error recovery purposes. */
27562 constructor_p = (!cp_parser_error_occurred (parser)
27563 && (outside_class_specifier_p
27564 || type_decl == NULL_TREE
27565 || type_decl == error_mark_node
27566 || same_type_p (current_class_type,
27567 TREE_TYPE (type_decl))));
27568
27569 /* If we're still considering a constructor, we have to see a `(',
27570 to begin the parameter-declaration-clause, followed by either a
27571 `)', an `...', or a decl-specifier. We need to check for a
27572 type-specifier to avoid being fooled into thinking that:
27573
27574 S (f) (int);
27575
27576 is a constructor. (It is actually a function named `f' that
27577 takes one parameter (of type `int') and returns a value of type
27578 `S'. */
27579 if (constructor_p
27580 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27581 constructor_p = false;
27582
27583 if (constructor_p
27584 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
27585 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
27586 /* A parameter declaration begins with a decl-specifier,
27587 which is either the "attribute" keyword, a storage class
27588 specifier, or (usually) a type-specifier. */
27589 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
27590 {
27591 tree type;
27592 tree pushed_scope = NULL_TREE;
27593 unsigned saved_num_template_parameter_lists;
27594
27595 /* Names appearing in the type-specifier should be looked up
27596 in the scope of the class. */
27597 if (current_class_type)
27598 type = NULL_TREE;
27599 else if (type_decl)
27600 {
27601 type = TREE_TYPE (type_decl);
27602 if (TREE_CODE (type) == TYPENAME_TYPE)
27603 {
27604 type = resolve_typename_type (type,
27605 /*only_current_p=*/false);
27606 if (TREE_CODE (type) == TYPENAME_TYPE)
27607 {
27608 cp_parser_abort_tentative_parse (parser);
27609 return false;
27610 }
27611 }
27612 pushed_scope = push_scope (type);
27613 }
27614
27615 /* Inside the constructor parameter list, surrounding
27616 template-parameter-lists do not apply. */
27617 saved_num_template_parameter_lists
27618 = parser->num_template_parameter_lists;
27619 parser->num_template_parameter_lists = 0;
27620
27621 /* Look for the type-specifier. It's not optional, but its typename
27622 might be. Unless this is a friend declaration; we don't want to
27623 treat
27624
27625 friend S (T::fn)(int);
27626
27627 as a constructor, but with P0634, we might assume a type when
27628 looking for the type-specifier. It is actually a function named
27629 `T::fn' that takes one parameter (of type `int') and returns a
27630 value of type `S'. Constructors can be friends, but they must
27631 use a qualified name. */
27632 cp_parser_type_specifier (parser,
27633 (friend_p ? CP_PARSER_FLAGS_NONE
27634 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
27635 /*decl_specs=*/NULL,
27636 /*is_declarator=*/true,
27637 /*declares_class_or_enum=*/NULL,
27638 /*is_cv_qualifier=*/NULL);
27639
27640 parser->num_template_parameter_lists
27641 = saved_num_template_parameter_lists;
27642
27643 /* Leave the scope of the class. */
27644 if (pushed_scope)
27645 pop_scope (pushed_scope);
27646
27647 constructor_p = !cp_parser_error_occurred (parser);
27648 }
27649 }
27650
27651 /* We did not really want to consume any tokens. */
27652 cp_parser_abort_tentative_parse (parser);
27653
27654 return constructor_p;
27655 }
27656
27657 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27658 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27659 they must be performed once we are in the scope of the function.
27660
27661 Returns the function defined. */
27662
27663 static tree
27664 cp_parser_function_definition_from_specifiers_and_declarator
27665 (cp_parser* parser,
27666 cp_decl_specifier_seq *decl_specifiers,
27667 tree attributes,
27668 const cp_declarator *declarator)
27669 {
27670 tree fn;
27671 bool success_p;
27672
27673 /* Begin the function-definition. */
27674 success_p = start_function (decl_specifiers, declarator, attributes);
27675
27676 /* The things we're about to see are not directly qualified by any
27677 template headers we've seen thus far. */
27678 reset_specialization ();
27679
27680 /* If there were names looked up in the decl-specifier-seq that we
27681 did not check, check them now. We must wait until we are in the
27682 scope of the function to perform the checks, since the function
27683 might be a friend. */
27684 perform_deferred_access_checks (tf_warning_or_error);
27685
27686 if (success_p)
27687 {
27688 cp_finalize_omp_declare_simd (parser, current_function_decl);
27689 parser->omp_declare_simd = NULL;
27690 cp_finalize_oacc_routine (parser, current_function_decl, true);
27691 parser->oacc_routine = NULL;
27692 }
27693
27694 if (!success_p)
27695 {
27696 /* Skip the entire function. */
27697 cp_parser_skip_to_end_of_block_or_statement (parser);
27698 fn = error_mark_node;
27699 }
27700 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27701 {
27702 /* Seen already, skip it. An error message has already been output. */
27703 cp_parser_skip_to_end_of_block_or_statement (parser);
27704 fn = current_function_decl;
27705 current_function_decl = NULL_TREE;
27706 /* If this is a function from a class, pop the nested class. */
27707 if (current_class_name)
27708 pop_nested_class ();
27709 }
27710 else
27711 {
27712 timevar_id_t tv;
27713 if (DECL_DECLARED_INLINE_P (current_function_decl))
27714 tv = TV_PARSE_INLINE;
27715 else
27716 tv = TV_PARSE_FUNC;
27717 timevar_push (tv);
27718 fn = cp_parser_function_definition_after_declarator (parser,
27719 /*inline_p=*/false);
27720 timevar_pop (tv);
27721 }
27722
27723 return fn;
27724 }
27725
27726 /* Parse the part of a function-definition that follows the
27727 declarator. INLINE_P is TRUE iff this function is an inline
27728 function defined within a class-specifier.
27729
27730 Returns the function defined. */
27731
27732 static tree
27733 cp_parser_function_definition_after_declarator (cp_parser* parser,
27734 bool inline_p)
27735 {
27736 tree fn;
27737 bool saved_in_unbraced_linkage_specification_p;
27738 bool saved_in_function_body;
27739 unsigned saved_num_template_parameter_lists;
27740 cp_token *token;
27741 bool fully_implicit_function_template_p
27742 = parser->fully_implicit_function_template_p;
27743 parser->fully_implicit_function_template_p = false;
27744 tree implicit_template_parms
27745 = parser->implicit_template_parms;
27746 parser->implicit_template_parms = 0;
27747 cp_binding_level* implicit_template_scope
27748 = parser->implicit_template_scope;
27749 parser->implicit_template_scope = 0;
27750
27751 saved_in_function_body = parser->in_function_body;
27752 parser->in_function_body = true;
27753 /* If the next token is `return', then the code may be trying to
27754 make use of the "named return value" extension that G++ used to
27755 support. */
27756 token = cp_lexer_peek_token (parser->lexer);
27757 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27758 {
27759 /* Consume the `return' keyword. */
27760 cp_lexer_consume_token (parser->lexer);
27761 /* Look for the identifier that indicates what value is to be
27762 returned. */
27763 cp_parser_identifier (parser);
27764 /* Issue an error message. */
27765 error_at (token->location,
27766 "named return values are no longer supported");
27767 /* Skip tokens until we reach the start of the function body. */
27768 while (true)
27769 {
27770 cp_token *token = cp_lexer_peek_token (parser->lexer);
27771 if (token->type == CPP_OPEN_BRACE
27772 || token->type == CPP_EOF
27773 || token->type == CPP_PRAGMA_EOL)
27774 break;
27775 cp_lexer_consume_token (parser->lexer);
27776 }
27777 }
27778 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27779 anything declared inside `f'. */
27780 saved_in_unbraced_linkage_specification_p
27781 = parser->in_unbraced_linkage_specification_p;
27782 parser->in_unbraced_linkage_specification_p = false;
27783 /* Inside the function, surrounding template-parameter-lists do not
27784 apply. */
27785 saved_num_template_parameter_lists
27786 = parser->num_template_parameter_lists;
27787 parser->num_template_parameter_lists = 0;
27788
27789 /* If the next token is `try', `__transaction_atomic', or
27790 `__transaction_relaxed`, then we are looking at either function-try-block
27791 or function-transaction-block. Note that all of these include the
27792 function-body. */
27793 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27794 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27795 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27796 RID_TRANSACTION_RELAXED))
27797 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27798 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27799 cp_parser_function_try_block (parser);
27800 else
27801 cp_parser_ctor_initializer_opt_and_function_body
27802 (parser, /*in_function_try_block=*/false);
27803
27804 /* Finish the function. */
27805 fn = finish_function (inline_p);
27806 /* Generate code for it, if necessary. */
27807 expand_or_defer_fn (fn);
27808 /* Restore the saved values. */
27809 parser->in_unbraced_linkage_specification_p
27810 = saved_in_unbraced_linkage_specification_p;
27811 parser->num_template_parameter_lists
27812 = saved_num_template_parameter_lists;
27813 parser->in_function_body = saved_in_function_body;
27814
27815 parser->fully_implicit_function_template_p
27816 = fully_implicit_function_template_p;
27817 parser->implicit_template_parms
27818 = implicit_template_parms;
27819 parser->implicit_template_scope
27820 = implicit_template_scope;
27821
27822 if (parser->fully_implicit_function_template_p)
27823 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27824
27825 return fn;
27826 }
27827
27828 /* Parse a template-declaration body (following argument list). */
27829
27830 static void
27831 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27832 tree parameter_list,
27833 bool member_p)
27834 {
27835 tree decl = NULL_TREE;
27836 bool friend_p = false;
27837
27838 /* We just processed one more parameter list. */
27839 ++parser->num_template_parameter_lists;
27840
27841 /* Get the deferred access checks from the parameter list. These
27842 will be checked once we know what is being declared, as for a
27843 member template the checks must be performed in the scope of the
27844 class containing the member. */
27845 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27846
27847 /* Tentatively parse for a new template parameter list, which can either be
27848 the template keyword or a template introduction. */
27849 if (cp_parser_template_declaration_after_export (parser, member_p))
27850 /* OK */;
27851 else if (cxx_dialect >= cxx11
27852 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27853 decl = cp_parser_alias_declaration (parser);
27854 else
27855 {
27856 /* There are no access checks when parsing a template, as we do not
27857 know if a specialization will be a friend. */
27858 push_deferring_access_checks (dk_no_check);
27859 cp_token *token = cp_lexer_peek_token (parser->lexer);
27860 decl = cp_parser_single_declaration (parser,
27861 checks,
27862 member_p,
27863 /*explicit_specialization_p=*/false,
27864 &friend_p);
27865 pop_deferring_access_checks ();
27866
27867 /* If this is a member template declaration, let the front
27868 end know. */
27869 if (member_p && !friend_p && decl)
27870 {
27871 if (TREE_CODE (decl) == TYPE_DECL)
27872 cp_parser_check_access_in_redeclaration (decl, token->location);
27873
27874 decl = finish_member_template_decl (decl);
27875 }
27876 else if (friend_p && decl
27877 && DECL_DECLARES_TYPE_P (decl))
27878 make_friend_class (current_class_type, TREE_TYPE (decl),
27879 /*complain=*/true);
27880 }
27881 /* We are done with the current parameter list. */
27882 --parser->num_template_parameter_lists;
27883
27884 pop_deferring_access_checks ();
27885
27886 /* Finish up. */
27887 finish_template_decl (parameter_list);
27888
27889 /* Check the template arguments for a literal operator template. */
27890 if (decl
27891 && DECL_DECLARES_FUNCTION_P (decl)
27892 && UDLIT_OPER_P (DECL_NAME (decl)))
27893 {
27894 bool ok = true;
27895 if (parameter_list == NULL_TREE)
27896 ok = false;
27897 else
27898 {
27899 int num_parms = TREE_VEC_LENGTH (parameter_list);
27900 if (num_parms == 1)
27901 {
27902 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27903 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27904 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27905 /* OK, C++20 string literal operator template. We don't need
27906 to warn in lower dialects here because we will have already
27907 warned about the template parameter. */;
27908 else if (TREE_TYPE (parm) != char_type_node
27909 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27910 ok = false;
27911 }
27912 else if (num_parms == 2 && cxx_dialect >= cxx14)
27913 {
27914 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27915 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27916 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27917 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27918 if (parm == error_mark_node
27919 || TREE_TYPE (parm) != TREE_TYPE (type)
27920 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27921 ok = false;
27922 else
27923 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27924 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27925 "ISO C++ did not adopt string literal operator templa"
27926 "tes taking an argument pack of characters");
27927 }
27928 else
27929 ok = false;
27930 }
27931 if (!ok)
27932 {
27933 if (cxx_dialect > cxx17)
27934 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
27935 "template %qD has invalid parameter list; expected "
27936 "non-type template parameter pack %<<char...>%> or "
27937 "single non-type parameter of class type",
27938 decl);
27939 else
27940 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
27941 "template %qD has invalid parameter list; expected "
27942 "non-type template parameter pack %<<char...>%>",
27943 decl);
27944 }
27945 }
27946
27947 /* Register member declarations. */
27948 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27949 finish_member_declaration (decl);
27950 /* If DECL is a function template, we must return to parse it later.
27951 (Even though there is no definition, there might be default
27952 arguments that need handling.) */
27953 if (member_p && decl
27954 && DECL_DECLARES_FUNCTION_P (decl))
27955 vec_safe_push (unparsed_funs_with_definitions, decl);
27956 }
27957
27958 /* Parse a template introduction header for a template-declaration. Returns
27959 false if tentative parse fails. */
27960
27961 static bool
27962 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27963 {
27964 cp_parser_parse_tentatively (parser);
27965
27966 tree saved_scope = parser->scope;
27967 tree saved_object_scope = parser->object_scope;
27968 tree saved_qualifying_scope = parser->qualifying_scope;
27969
27970 /* Look for the optional `::' operator. */
27971 cp_parser_global_scope_opt (parser,
27972 /*current_scope_valid_p=*/false);
27973 /* Look for the nested-name-specifier. */
27974 cp_parser_nested_name_specifier_opt (parser,
27975 /*typename_keyword_p=*/false,
27976 /*check_dependency_p=*/true,
27977 /*type_p=*/false,
27978 /*is_declaration=*/false);
27979
27980 cp_token *token = cp_lexer_peek_token (parser->lexer);
27981 tree concept_name = cp_parser_identifier (parser);
27982
27983 /* Look up the concept for which we will be matching
27984 template parameters. */
27985 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27986 token->location);
27987 parser->scope = saved_scope;
27988 parser->object_scope = saved_object_scope;
27989 parser->qualifying_scope = saved_qualifying_scope;
27990
27991 if (concept_name == error_mark_node)
27992 cp_parser_simulate_error (parser);
27993
27994 /* Look for opening brace for introduction. */
27995 matching_braces braces;
27996 braces.require_open (parser);
27997
27998 if (!cp_parser_parse_definitely (parser))
27999 return false;
28000
28001 push_deferring_access_checks (dk_deferred);
28002
28003 /* Build vector of placeholder parameters and grab
28004 matching identifiers. */
28005 tree introduction_list = cp_parser_introduction_list (parser);
28006
28007 /* Look for closing brace for introduction. */
28008 if (!braces.require_close (parser))
28009 return true;
28010
28011 /* The introduction-list shall not be empty. */
28012 int nargs = TREE_VEC_LENGTH (introduction_list);
28013 if (nargs == 0)
28014 {
28015 /* In cp_parser_introduction_list we have already issued an error. */
28016 return true;
28017 }
28018
28019 if (tmpl_decl == error_mark_node)
28020 {
28021 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
28022 token->location);
28023 return true;
28024 }
28025
28026 /* Build and associate the constraint. */
28027 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
28028 if (parms && parms != error_mark_node)
28029 {
28030 cp_parser_template_declaration_after_parameters (parser, parms,
28031 member_p);
28032 return true;
28033 }
28034
28035 error_at (token->location, "no matching concept for template-introduction");
28036 return true;
28037 }
28038
28039 /* Parse a normal template-declaration following the template keyword. */
28040
28041 static void
28042 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
28043 {
28044 tree parameter_list;
28045 bool need_lang_pop;
28046 location_t location = input_location;
28047
28048 /* Look for the `<' token. */
28049 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
28050 return;
28051 if (at_class_scope_p () && current_function_decl)
28052 {
28053 /* 14.5.2.2 [temp.mem]
28054
28055 A local class shall not have member templates. */
28056 error_at (location,
28057 "invalid declaration of member template in local class");
28058 cp_parser_skip_to_end_of_block_or_statement (parser);
28059 return;
28060 }
28061 /* [temp]
28062
28063 A template ... shall not have C linkage. */
28064 if (current_lang_name == lang_name_c)
28065 {
28066 error_at (location, "template with C linkage");
28067 maybe_show_extern_c_location ();
28068 /* Give it C++ linkage to avoid confusing other parts of the
28069 front end. */
28070 push_lang_context (lang_name_cplusplus);
28071 need_lang_pop = true;
28072 }
28073 else
28074 need_lang_pop = false;
28075
28076 /* We cannot perform access checks on the template parameter
28077 declarations until we know what is being declared, just as we
28078 cannot check the decl-specifier list. */
28079 push_deferring_access_checks (dk_deferred);
28080
28081 /* If the next token is `>', then we have an invalid
28082 specialization. Rather than complain about an invalid template
28083 parameter, issue an error message here. */
28084 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
28085 {
28086 cp_parser_error (parser, "invalid explicit specialization");
28087 begin_specialization ();
28088 parameter_list = NULL_TREE;
28089 }
28090 else
28091 {
28092 /* Parse the template parameters. */
28093 parameter_list = cp_parser_template_parameter_list (parser);
28094 }
28095
28096 /* Look for the `>'. */
28097 cp_parser_skip_to_end_of_template_parameter_list (parser);
28098
28099 /* Manage template requirements */
28100 if (flag_concepts)
28101 {
28102 tree reqs = get_shorthand_constraints (current_template_parms);
28103 if (tree r = cp_parser_requires_clause_opt (parser))
28104 reqs = conjoin_constraints (reqs, normalize_expression (r));
28105 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
28106 }
28107
28108 cp_parser_template_declaration_after_parameters (parser, parameter_list,
28109 member_p);
28110
28111 /* For the erroneous case of a template with C linkage, we pushed an
28112 implicit C++ linkage scope; exit that scope now. */
28113 if (need_lang_pop)
28114 pop_lang_context ();
28115 }
28116
28117 /* Parse a template-declaration, assuming that the `export' (and
28118 `extern') keywords, if present, has already been scanned. MEMBER_P
28119 is as for cp_parser_template_declaration. */
28120
28121 static bool
28122 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
28123 {
28124 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28125 {
28126 cp_lexer_consume_token (parser->lexer);
28127 cp_parser_explicit_template_declaration (parser, member_p);
28128 return true;
28129 }
28130 else if (flag_concepts)
28131 return cp_parser_template_introduction (parser, member_p);
28132
28133 return false;
28134 }
28135
28136 /* Perform the deferred access checks from a template-parameter-list.
28137 CHECKS is a TREE_LIST of access checks, as returned by
28138 get_deferred_access_checks. */
28139
28140 static void
28141 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
28142 {
28143 ++processing_template_parmlist;
28144 perform_access_checks (checks, tf_warning_or_error);
28145 --processing_template_parmlist;
28146 }
28147
28148 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
28149 `function-definition' sequence that follows a template header.
28150 If MEMBER_P is true, this declaration appears in a class scope.
28151
28152 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
28153 *FRIEND_P is set to TRUE iff the declaration is a friend. */
28154
28155 static tree
28156 cp_parser_single_declaration (cp_parser* parser,
28157 vec<deferred_access_check, va_gc> *checks,
28158 bool member_p,
28159 bool explicit_specialization_p,
28160 bool* friend_p)
28161 {
28162 int declares_class_or_enum;
28163 tree decl = NULL_TREE;
28164 cp_decl_specifier_seq decl_specifiers;
28165 bool function_definition_p = false;
28166 cp_token *decl_spec_token_start;
28167
28168 /* This function is only used when processing a template
28169 declaration. */
28170 gcc_assert (innermost_scope_kind () == sk_template_parms
28171 || innermost_scope_kind () == sk_template_spec);
28172
28173 /* Defer access checks until we know what is being declared. */
28174 push_deferring_access_checks (dk_deferred);
28175
28176 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
28177 alternative. */
28178 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
28179 cp_parser_decl_specifier_seq (parser,
28180 (CP_PARSER_FLAGS_OPTIONAL
28181 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
28182 &decl_specifiers,
28183 &declares_class_or_enum);
28184 if (friend_p)
28185 *friend_p = cp_parser_friend_p (&decl_specifiers);
28186
28187 /* There are no template typedefs. */
28188 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
28189 {
28190 error_at (decl_spec_token_start->location,
28191 "template declaration of %<typedef%>");
28192 decl = error_mark_node;
28193 }
28194
28195 /* Gather up the access checks that occurred the
28196 decl-specifier-seq. */
28197 stop_deferring_access_checks ();
28198
28199 /* Check for the declaration of a template class. */
28200 if (declares_class_or_enum)
28201 {
28202 if (cp_parser_declares_only_class_p (parser)
28203 || (declares_class_or_enum & 2))
28204 {
28205 // If this is a declaration, but not a definition, associate
28206 // any constraints with the type declaration. Constraints
28207 // are associated with definitions in cp_parser_class_specifier.
28208 if (declares_class_or_enum == 1)
28209 associate_classtype_constraints (decl_specifiers.type);
28210
28211 decl = shadow_tag (&decl_specifiers);
28212
28213 /* In this case:
28214
28215 struct C {
28216 friend template <typename T> struct A<T>::B;
28217 };
28218
28219 A<T>::B will be represented by a TYPENAME_TYPE, and
28220 therefore not recognized by shadow_tag. */
28221 if (friend_p && *friend_p
28222 && !decl
28223 && decl_specifiers.type
28224 && TYPE_P (decl_specifiers.type))
28225 decl = decl_specifiers.type;
28226
28227 if (decl && decl != error_mark_node)
28228 decl = TYPE_NAME (decl);
28229 else
28230 decl = error_mark_node;
28231
28232 /* Perform access checks for template parameters. */
28233 cp_parser_perform_template_parameter_access_checks (checks);
28234
28235 /* Give a helpful diagnostic for
28236 template <class T> struct A { } a;
28237 if we aren't already recovering from an error. */
28238 if (!cp_parser_declares_only_class_p (parser)
28239 && !seen_error ())
28240 {
28241 error_at (cp_lexer_peek_token (parser->lexer)->location,
28242 "a class template declaration must not declare "
28243 "anything else");
28244 cp_parser_skip_to_end_of_block_or_statement (parser);
28245 goto out;
28246 }
28247 }
28248 }
28249
28250 /* Complain about missing 'typename' or other invalid type names. */
28251 if (!decl_specifiers.any_type_specifiers_p
28252 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
28253 {
28254 /* cp_parser_parse_and_diagnose_invalid_type_name calls
28255 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28256 the rest of this declaration. */
28257 decl = error_mark_node;
28258 goto out;
28259 }
28260
28261 /* If it's not a template class, try for a template function. If
28262 the next token is a `;', then this declaration does not declare
28263 anything. But, if there were errors in the decl-specifiers, then
28264 the error might well have come from an attempted class-specifier.
28265 In that case, there's no need to warn about a missing declarator. */
28266 if (!decl
28267 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
28268 || decl_specifiers.type != error_mark_node))
28269 {
28270 decl = cp_parser_init_declarator (parser,
28271 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
28272 &decl_specifiers,
28273 checks,
28274 /*function_definition_allowed_p=*/true,
28275 member_p,
28276 declares_class_or_enum,
28277 &function_definition_p,
28278 NULL, NULL, NULL);
28279
28280 /* 7.1.1-1 [dcl.stc]
28281
28282 A storage-class-specifier shall not be specified in an explicit
28283 specialization... */
28284 if (decl
28285 && explicit_specialization_p
28286 && decl_specifiers.storage_class != sc_none)
28287 {
28288 error_at (decl_spec_token_start->location,
28289 "explicit template specialization cannot have a storage class");
28290 decl = error_mark_node;
28291 }
28292
28293 if (decl && VAR_P (decl))
28294 check_template_variable (decl);
28295 }
28296
28297 /* Look for a trailing `;' after the declaration. */
28298 if (!function_definition_p
28299 && (decl == error_mark_node
28300 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
28301 cp_parser_skip_to_end_of_block_or_statement (parser);
28302
28303 out:
28304 pop_deferring_access_checks ();
28305
28306 /* Clear any current qualification; whatever comes next is the start
28307 of something new. */
28308 parser->scope = NULL_TREE;
28309 parser->qualifying_scope = NULL_TREE;
28310 parser->object_scope = NULL_TREE;
28311
28312 return decl;
28313 }
28314
28315 /* Parse a cast-expression that is not the operand of a unary "&". */
28316
28317 static cp_expr
28318 cp_parser_simple_cast_expression (cp_parser *parser)
28319 {
28320 return cp_parser_cast_expression (parser, /*address_p=*/false,
28321 /*cast_p=*/false, /*decltype*/false, NULL);
28322 }
28323
28324 /* Parse a functional cast to TYPE. Returns an expression
28325 representing the cast. */
28326
28327 static cp_expr
28328 cp_parser_functional_cast (cp_parser* parser, tree type)
28329 {
28330 vec<tree, va_gc> *vec;
28331 tree expression_list;
28332 cp_expr cast;
28333 bool nonconst_p;
28334
28335 location_t start_loc = input_location;
28336
28337 if (!type)
28338 type = error_mark_node;
28339
28340 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28341 {
28342 cp_lexer_set_source_position (parser->lexer);
28343 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28344 expression_list = cp_parser_braced_list (parser, &nonconst_p);
28345 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
28346 if (TREE_CODE (type) == TYPE_DECL)
28347 type = TREE_TYPE (type);
28348
28349 cast = finish_compound_literal (type, expression_list,
28350 tf_warning_or_error, fcl_functional);
28351 /* Create a location of the form:
28352 type_name{i, f}
28353 ^~~~~~~~~~~~~~~
28354 with caret == start at the start of the type name,
28355 finishing at the closing brace. */
28356 location_t finish_loc
28357 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28358 location_t combined_loc = make_location (start_loc, start_loc,
28359 finish_loc);
28360 cast.set_location (combined_loc);
28361 return cast;
28362 }
28363
28364
28365 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
28366 /*cast_p=*/true,
28367 /*allow_expansion_p=*/true,
28368 /*non_constant_p=*/NULL);
28369 if (vec == NULL)
28370 expression_list = error_mark_node;
28371 else
28372 {
28373 expression_list = build_tree_list_vec (vec);
28374 release_tree_vector (vec);
28375 }
28376
28377 cast = build_functional_cast (type, expression_list,
28378 tf_warning_or_error);
28379 /* [expr.const]/1: In an integral constant expression "only type
28380 conversions to integral or enumeration type can be used". */
28381 if (TREE_CODE (type) == TYPE_DECL)
28382 type = TREE_TYPE (type);
28383 if (cast != error_mark_node
28384 && !cast_valid_in_integral_constant_expression_p (type)
28385 && cp_parser_non_integral_constant_expression (parser,
28386 NIC_CONSTRUCTOR))
28387 return error_mark_node;
28388
28389 /* Create a location of the form:
28390 float(i)
28391 ^~~~~~~~
28392 with caret == start at the start of the type name,
28393 finishing at the closing paren. */
28394 location_t finish_loc
28395 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28396 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
28397 cast.set_location (combined_loc);
28398 return cast;
28399 }
28400
28401 /* Save the tokens that make up the body of a member function defined
28402 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28403 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28404 specifiers applied to the declaration. Returns the FUNCTION_DECL
28405 for the member function. */
28406
28407 static tree
28408 cp_parser_save_member_function_body (cp_parser* parser,
28409 cp_decl_specifier_seq *decl_specifiers,
28410 cp_declarator *declarator,
28411 tree attributes)
28412 {
28413 cp_token *first;
28414 cp_token *last;
28415 tree fn;
28416 bool function_try_block = false;
28417
28418 /* Create the FUNCTION_DECL. */
28419 fn = grokmethod (decl_specifiers, declarator, attributes);
28420 cp_finalize_omp_declare_simd (parser, fn);
28421 cp_finalize_oacc_routine (parser, fn, true);
28422 /* If something went badly wrong, bail out now. */
28423 if (fn == error_mark_node)
28424 {
28425 /* If there's a function-body, skip it. */
28426 if (cp_parser_token_starts_function_definition_p
28427 (cp_lexer_peek_token (parser->lexer)))
28428 cp_parser_skip_to_end_of_block_or_statement (parser);
28429 return error_mark_node;
28430 }
28431
28432 /* Remember it, if there default args to post process. */
28433 cp_parser_save_default_args (parser, fn);
28434
28435 /* Save away the tokens that make up the body of the
28436 function. */
28437 first = parser->lexer->next_token;
28438
28439 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
28440 cp_lexer_consume_token (parser->lexer);
28441 else if (cp_lexer_next_token_is_keyword (parser->lexer,
28442 RID_TRANSACTION_ATOMIC))
28443 {
28444 cp_lexer_consume_token (parser->lexer);
28445 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
28447 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
28448 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
28449 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28450 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
28451 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
28452 {
28453 cp_lexer_consume_token (parser->lexer);
28454 cp_lexer_consume_token (parser->lexer);
28455 cp_lexer_consume_token (parser->lexer);
28456 cp_lexer_consume_token (parser->lexer);
28457 cp_lexer_consume_token (parser->lexer);
28458 }
28459 else
28460 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
28461 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
28462 {
28463 cp_lexer_consume_token (parser->lexer);
28464 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28465 break;
28466 }
28467 }
28468
28469 /* Handle function try blocks. */
28470 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28471 {
28472 cp_lexer_consume_token (parser->lexer);
28473 function_try_block = true;
28474 }
28475 /* We can have braced-init-list mem-initializers before the fn body. */
28476 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28477 {
28478 cp_lexer_consume_token (parser->lexer);
28479 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
28480 {
28481 /* cache_group will stop after an un-nested { } pair, too. */
28482 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28483 break;
28484
28485 /* variadic mem-inits have ... after the ')'. */
28486 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28487 cp_lexer_consume_token (parser->lexer);
28488 }
28489 }
28490 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28491 /* Handle function try blocks. */
28492 if (function_try_block)
28493 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
28494 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28495 last = parser->lexer->next_token;
28496
28497 /* Save away the inline definition; we will process it when the
28498 class is complete. */
28499 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
28500 DECL_PENDING_INLINE_P (fn) = 1;
28501
28502 /* We need to know that this was defined in the class, so that
28503 friend templates are handled correctly. */
28504 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
28505
28506 /* Add FN to the queue of functions to be parsed later. */
28507 vec_safe_push (unparsed_funs_with_definitions, fn);
28508
28509 return fn;
28510 }
28511
28512 /* Save the tokens that make up the in-class initializer for a non-static
28513 data member. Returns a DEFAULT_ARG. */
28514
28515 static tree
28516 cp_parser_save_nsdmi (cp_parser* parser)
28517 {
28518 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
28519 }
28520
28521 /* Parse a template-argument-list, as well as the trailing ">" (but
28522 not the opening "<"). See cp_parser_template_argument_list for the
28523 return value. */
28524
28525 static tree
28526 cp_parser_enclosed_template_argument_list (cp_parser* parser)
28527 {
28528 tree arguments;
28529 tree saved_scope;
28530 tree saved_qualifying_scope;
28531 tree saved_object_scope;
28532 bool saved_greater_than_is_operator_p;
28533
28534 /* [temp.names]
28535
28536 When parsing a template-id, the first non-nested `>' is taken as
28537 the end of the template-argument-list rather than a greater-than
28538 operator. */
28539 saved_greater_than_is_operator_p
28540 = parser->greater_than_is_operator_p;
28541 parser->greater_than_is_operator_p = false;
28542 /* Parsing the argument list may modify SCOPE, so we save it
28543 here. */
28544 saved_scope = parser->scope;
28545 saved_qualifying_scope = parser->qualifying_scope;
28546 saved_object_scope = parser->object_scope;
28547 /* We need to evaluate the template arguments, even though this
28548 template-id may be nested within a "sizeof". */
28549 cp_evaluated ev;
28550 /* Parse the template-argument-list itself. */
28551 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
28552 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28553 arguments = NULL_TREE;
28554 else
28555 arguments = cp_parser_template_argument_list (parser);
28556 /* Look for the `>' that ends the template-argument-list. If we find
28557 a '>>' instead, it's probably just a typo. */
28558 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28559 {
28560 if (cxx_dialect != cxx98)
28561 {
28562 /* In C++0x, a `>>' in a template argument list or cast
28563 expression is considered to be two separate `>'
28564 tokens. So, change the current token to a `>', but don't
28565 consume it: it will be consumed later when the outer
28566 template argument list (or cast expression) is parsed.
28567 Note that this replacement of `>' for `>>' is necessary
28568 even if we are parsing tentatively: in the tentative
28569 case, after calling
28570 cp_parser_enclosed_template_argument_list we will always
28571 throw away all of the template arguments and the first
28572 closing `>', either because the template argument list
28573 was erroneous or because we are replacing those tokens
28574 with a CPP_TEMPLATE_ID token. The second `>' (which will
28575 not have been thrown away) is needed either to close an
28576 outer template argument list or to complete a new-style
28577 cast. */
28578 cp_token *token = cp_lexer_peek_token (parser->lexer);
28579 token->type = CPP_GREATER;
28580 }
28581 else if (!saved_greater_than_is_operator_p)
28582 {
28583 /* If we're in a nested template argument list, the '>>' has
28584 to be a typo for '> >'. We emit the error message, but we
28585 continue parsing and we push a '>' as next token, so that
28586 the argument list will be parsed correctly. Note that the
28587 global source location is still on the token before the
28588 '>>', so we need to say explicitly where we want it. */
28589 cp_token *token = cp_lexer_peek_token (parser->lexer);
28590 gcc_rich_location richloc (token->location);
28591 richloc.add_fixit_replace ("> >");
28592 error_at (&richloc, "%<>>%> should be %<> >%> "
28593 "within a nested template argument list");
28594
28595 token->type = CPP_GREATER;
28596 }
28597 else
28598 {
28599 /* If this is not a nested template argument list, the '>>'
28600 is a typo for '>'. Emit an error message and continue.
28601 Same deal about the token location, but here we can get it
28602 right by consuming the '>>' before issuing the diagnostic. */
28603 cp_token *token = cp_lexer_consume_token (parser->lexer);
28604 error_at (token->location,
28605 "spurious %<>>%>, use %<>%> to terminate "
28606 "a template argument list");
28607 }
28608 }
28609 else
28610 cp_parser_skip_to_end_of_template_parameter_list (parser);
28611 /* The `>' token might be a greater-than operator again now. */
28612 parser->greater_than_is_operator_p
28613 = saved_greater_than_is_operator_p;
28614 /* Restore the SAVED_SCOPE. */
28615 parser->scope = saved_scope;
28616 parser->qualifying_scope = saved_qualifying_scope;
28617 parser->object_scope = saved_object_scope;
28618
28619 return arguments;
28620 }
28621
28622 /* MEMBER_FUNCTION is a member function, or a friend. If default
28623 arguments, or the body of the function have not yet been parsed,
28624 parse them now. */
28625
28626 static void
28627 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
28628 {
28629 timevar_push (TV_PARSE_INMETH);
28630 /* If this member is a template, get the underlying
28631 FUNCTION_DECL. */
28632 if (DECL_FUNCTION_TEMPLATE_P (member_function))
28633 member_function = DECL_TEMPLATE_RESULT (member_function);
28634
28635 /* There should not be any class definitions in progress at this
28636 point; the bodies of members are only parsed outside of all class
28637 definitions. */
28638 gcc_assert (parser->num_classes_being_defined == 0);
28639 /* While we're parsing the member functions we might encounter more
28640 classes. We want to handle them right away, but we don't want
28641 them getting mixed up with functions that are currently in the
28642 queue. */
28643 push_unparsed_function_queues (parser);
28644
28645 /* Make sure that any template parameters are in scope. */
28646 maybe_begin_member_template_processing (member_function);
28647
28648 /* If the body of the function has not yet been parsed, parse it
28649 now. */
28650 if (DECL_PENDING_INLINE_P (member_function))
28651 {
28652 tree function_scope;
28653 cp_token_cache *tokens;
28654
28655 /* The function is no longer pending; we are processing it. */
28656 tokens = DECL_PENDING_INLINE_INFO (member_function);
28657 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28658 DECL_PENDING_INLINE_P (member_function) = 0;
28659
28660 /* If this is a local class, enter the scope of the containing
28661 function. */
28662 function_scope = current_function_decl;
28663 if (function_scope)
28664 push_function_context ();
28665
28666 /* Push the body of the function onto the lexer stack. */
28667 cp_parser_push_lexer_for_tokens (parser, tokens);
28668
28669 /* Let the front end know that we going to be defining this
28670 function. */
28671 start_preparsed_function (member_function, NULL_TREE,
28672 SF_PRE_PARSED | SF_INCLASS_INLINE);
28673
28674 /* Don't do access checking if it is a templated function. */
28675 if (processing_template_decl)
28676 push_deferring_access_checks (dk_no_check);
28677
28678 /* #pragma omp declare reduction needs special parsing. */
28679 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28680 {
28681 parser->lexer->in_pragma = true;
28682 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28683 finish_function (/*inline_p=*/true);
28684 cp_check_omp_declare_reduction (member_function);
28685 }
28686 else
28687 /* Now, parse the body of the function. */
28688 cp_parser_function_definition_after_declarator (parser,
28689 /*inline_p=*/true);
28690
28691 if (processing_template_decl)
28692 pop_deferring_access_checks ();
28693
28694 /* Leave the scope of the containing function. */
28695 if (function_scope)
28696 pop_function_context ();
28697 cp_parser_pop_lexer (parser);
28698 }
28699
28700 /* Remove any template parameters from the symbol table. */
28701 maybe_end_member_template_processing ();
28702
28703 /* Restore the queue. */
28704 pop_unparsed_function_queues (parser);
28705 timevar_pop (TV_PARSE_INMETH);
28706 }
28707
28708 /* If DECL contains any default args, remember it on the unparsed
28709 functions queue. */
28710
28711 static void
28712 cp_parser_save_default_args (cp_parser* parser, tree decl)
28713 {
28714 tree probe;
28715
28716 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28717 probe;
28718 probe = TREE_CHAIN (probe))
28719 if (TREE_PURPOSE (probe))
28720 {
28721 cp_default_arg_entry entry = {current_class_type, decl};
28722 vec_safe_push (unparsed_funs_with_default_args, entry);
28723 break;
28724 }
28725 }
28726
28727 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28728 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28729 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28730 from the parameter-type-list. */
28731
28732 static tree
28733 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28734 tree default_arg, tree parmtype)
28735 {
28736 cp_token_cache *tokens;
28737 tree parsed_arg;
28738 bool dummy;
28739
28740 if (default_arg == error_mark_node)
28741 return error_mark_node;
28742
28743 /* Push the saved tokens for the default argument onto the parser's
28744 lexer stack. */
28745 tokens = DEFARG_TOKENS (default_arg);
28746 cp_parser_push_lexer_for_tokens (parser, tokens);
28747
28748 start_lambda_scope (decl);
28749
28750 /* Parse the default argument. */
28751 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28752 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28753 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28754
28755 finish_lambda_scope ();
28756
28757 if (parsed_arg == error_mark_node)
28758 cp_parser_skip_to_end_of_statement (parser);
28759
28760 if (!processing_template_decl)
28761 {
28762 /* In a non-template class, check conversions now. In a template,
28763 we'll wait and instantiate these as needed. */
28764 if (TREE_CODE (decl) == PARM_DECL)
28765 parsed_arg = check_default_argument (parmtype, parsed_arg,
28766 tf_warning_or_error);
28767 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28768 parsed_arg = error_mark_node;
28769 else
28770 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28771 }
28772
28773 /* If the token stream has not been completely used up, then
28774 there was extra junk after the end of the default
28775 argument. */
28776 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28777 {
28778 if (TREE_CODE (decl) == PARM_DECL)
28779 cp_parser_error (parser, "expected %<,%>");
28780 else
28781 cp_parser_error (parser, "expected %<;%>");
28782 }
28783
28784 /* Revert to the main lexer. */
28785 cp_parser_pop_lexer (parser);
28786
28787 return parsed_arg;
28788 }
28789
28790 /* FIELD is a non-static data member with an initializer which we saved for
28791 later; parse it now. */
28792
28793 static void
28794 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28795 {
28796 tree def;
28797
28798 maybe_begin_member_template_processing (field);
28799
28800 push_unparsed_function_queues (parser);
28801 def = cp_parser_late_parse_one_default_arg (parser, field,
28802 DECL_INITIAL (field),
28803 NULL_TREE);
28804 pop_unparsed_function_queues (parser);
28805
28806 maybe_end_member_template_processing ();
28807
28808 DECL_INITIAL (field) = def;
28809 }
28810
28811 /* FN is a FUNCTION_DECL which may contains a parameter with an
28812 unparsed DEFAULT_ARG. Parse the default args now. This function
28813 assumes that the current scope is the scope in which the default
28814 argument should be processed. */
28815
28816 static void
28817 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28818 {
28819 unsigned char saved_local_variables_forbidden_p;
28820 tree parm, parmdecl;
28821
28822 /* While we're parsing the default args, we might (due to the
28823 statement expression extension) encounter more classes. We want
28824 to handle them right away, but we don't want them getting mixed
28825 up with default args that are currently in the queue. */
28826 push_unparsed_function_queues (parser);
28827
28828 /* Local variable names (and the `this' keyword) may not appear
28829 in a default argument. */
28830 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28831 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
28832
28833 push_defarg_context (fn);
28834
28835 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28836 parmdecl = DECL_ARGUMENTS (fn);
28837 parm && parm != void_list_node;
28838 parm = TREE_CHAIN (parm),
28839 parmdecl = DECL_CHAIN (parmdecl))
28840 {
28841 tree default_arg = TREE_PURPOSE (parm);
28842 tree parsed_arg;
28843 vec<tree, va_gc> *insts;
28844 tree copy;
28845 unsigned ix;
28846
28847 if (!default_arg)
28848 continue;
28849
28850 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28851 /* This can happen for a friend declaration for a function
28852 already declared with default arguments. */
28853 continue;
28854
28855 parsed_arg
28856 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28857 default_arg,
28858 TREE_VALUE (parm));
28859 TREE_PURPOSE (parm) = parsed_arg;
28860
28861 /* Update any instantiations we've already created. */
28862 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28863 vec_safe_iterate (insts, ix, &copy); ix++)
28864 TREE_PURPOSE (copy) = parsed_arg;
28865 }
28866
28867 pop_defarg_context ();
28868
28869 /* Make sure no default arg is missing. */
28870 check_default_args (fn);
28871
28872 /* Restore the state of local_variables_forbidden_p. */
28873 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28874
28875 /* Restore the queue. */
28876 pop_unparsed_function_queues (parser);
28877 }
28878
28879 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28880
28881 sizeof ... ( identifier )
28882
28883 where the 'sizeof' token has already been consumed. */
28884
28885 static tree
28886 cp_parser_sizeof_pack (cp_parser *parser)
28887 {
28888 /* Consume the `...'. */
28889 cp_lexer_consume_token (parser->lexer);
28890 maybe_warn_variadic_templates ();
28891
28892 matching_parens parens;
28893 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28894 if (paren)
28895 parens.consume_open (parser);
28896 else
28897 permerror (cp_lexer_peek_token (parser->lexer)->location,
28898 "%<sizeof...%> argument must be surrounded by parentheses");
28899
28900 cp_token *token = cp_lexer_peek_token (parser->lexer);
28901 tree name = cp_parser_identifier (parser);
28902 if (name == error_mark_node)
28903 return error_mark_node;
28904 /* The name is not qualified. */
28905 parser->scope = NULL_TREE;
28906 parser->qualifying_scope = NULL_TREE;
28907 parser->object_scope = NULL_TREE;
28908 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28909 if (expr == error_mark_node)
28910 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28911 token->location);
28912 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28913 expr = TREE_TYPE (expr);
28914 else if (TREE_CODE (expr) == CONST_DECL)
28915 expr = DECL_INITIAL (expr);
28916 expr = make_pack_expansion (expr);
28917 PACK_EXPANSION_SIZEOF_P (expr) = true;
28918
28919 if (paren)
28920 parens.require_close (parser);
28921
28922 return expr;
28923 }
28924
28925 /* Parse the operand of `sizeof' (or a similar operator). Returns
28926 either a TYPE or an expression, depending on the form of the
28927 input. The KEYWORD indicates which kind of expression we have
28928 encountered. */
28929
28930 static tree
28931 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28932 {
28933 tree expr = NULL_TREE;
28934 const char *saved_message;
28935 const char *saved_message_arg;
28936 bool saved_integral_constant_expression_p;
28937 bool saved_non_integral_constant_expression_p;
28938
28939 /* If it's a `...', then we are computing the length of a parameter
28940 pack. */
28941 if (keyword == RID_SIZEOF
28942 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28943 return cp_parser_sizeof_pack (parser);
28944
28945 /* Types cannot be defined in a `sizeof' expression. Save away the
28946 old message. */
28947 saved_message = parser->type_definition_forbidden_message;
28948 saved_message_arg = parser->type_definition_forbidden_message_arg;
28949 parser->type_definition_forbidden_message
28950 = G_("types may not be defined in %qs expressions");
28951 parser->type_definition_forbidden_message_arg
28952 = IDENTIFIER_POINTER (ridpointers[keyword]);
28953
28954 /* The restrictions on constant-expressions do not apply inside
28955 sizeof expressions. */
28956 saved_integral_constant_expression_p
28957 = parser->integral_constant_expression_p;
28958 saved_non_integral_constant_expression_p
28959 = parser->non_integral_constant_expression_p;
28960 parser->integral_constant_expression_p = false;
28961
28962 /* Do not actually evaluate the expression. */
28963 ++cp_unevaluated_operand;
28964 ++c_inhibit_evaluation_warnings;
28965 /* If it's a `(', then we might be looking at the type-id
28966 construction. */
28967 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28968 {
28969 tree type = NULL_TREE;
28970
28971 tentative_firewall firewall (parser);
28972
28973 /* We can't be sure yet whether we're looking at a type-id or an
28974 expression. */
28975 cp_parser_parse_tentatively (parser);
28976
28977 matching_parens parens;
28978 parens.consume_open (parser);
28979
28980 /* Note: as a GNU Extension, compound literals are considered
28981 postfix-expressions as they are in C99, so they are valid
28982 arguments to sizeof. See comment in cp_parser_cast_expression
28983 for details. */
28984 if (cp_parser_compound_literal_p (parser))
28985 cp_parser_simulate_error (parser);
28986 else
28987 {
28988 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28989 parser->in_type_id_in_expr_p = true;
28990 /* Look for the type-id. */
28991 type = cp_parser_type_id (parser);
28992 /* Look for the closing `)'. */
28993 parens.require_close (parser);
28994 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28995 }
28996
28997 /* If all went well, then we're done. */
28998 if (cp_parser_parse_definitely (parser))
28999 expr = type;
29000 else
29001 {
29002 /* Commit to the tentative_firewall so we get syntax errors. */
29003 cp_parser_commit_to_tentative_parse (parser);
29004
29005 expr = cp_parser_unary_expression (parser);
29006 }
29007 }
29008 else
29009 expr = cp_parser_unary_expression (parser);
29010
29011 /* Go back to evaluating expressions. */
29012 --cp_unevaluated_operand;
29013 --c_inhibit_evaluation_warnings;
29014
29015 /* And restore the old one. */
29016 parser->type_definition_forbidden_message = saved_message;
29017 parser->type_definition_forbidden_message_arg = saved_message_arg;
29018 parser->integral_constant_expression_p
29019 = saved_integral_constant_expression_p;
29020 parser->non_integral_constant_expression_p
29021 = saved_non_integral_constant_expression_p;
29022
29023 return expr;
29024 }
29025
29026 /* If the current declaration has no declarator, return true. */
29027
29028 static bool
29029 cp_parser_declares_only_class_p (cp_parser *parser)
29030 {
29031 /* If the next token is a `;' or a `,' then there is no
29032 declarator. */
29033 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29034 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
29035 }
29036
29037 /* Update the DECL_SPECS to reflect the storage class indicated by
29038 KEYWORD. */
29039
29040 static void
29041 cp_parser_set_storage_class (cp_parser *parser,
29042 cp_decl_specifier_seq *decl_specs,
29043 enum rid keyword,
29044 cp_token *token)
29045 {
29046 cp_storage_class storage_class;
29047
29048 if (parser->in_unbraced_linkage_specification_p)
29049 {
29050 error_at (token->location, "invalid use of %qD in linkage specification",
29051 ridpointers[keyword]);
29052 return;
29053 }
29054 else if (decl_specs->storage_class != sc_none)
29055 {
29056 decl_specs->conflicting_specifiers_p = true;
29057 return;
29058 }
29059
29060 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
29061 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
29062 && decl_specs->gnu_thread_keyword_p)
29063 {
29064 pedwarn (decl_specs->locations[ds_thread], 0,
29065 "%<__thread%> before %qD", ridpointers[keyword]);
29066 }
29067
29068 switch (keyword)
29069 {
29070 case RID_AUTO:
29071 storage_class = sc_auto;
29072 break;
29073 case RID_REGISTER:
29074 storage_class = sc_register;
29075 break;
29076 case RID_STATIC:
29077 storage_class = sc_static;
29078 break;
29079 case RID_EXTERN:
29080 storage_class = sc_extern;
29081 break;
29082 case RID_MUTABLE:
29083 storage_class = sc_mutable;
29084 break;
29085 default:
29086 gcc_unreachable ();
29087 }
29088 decl_specs->storage_class = storage_class;
29089 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
29090
29091 /* A storage class specifier cannot be applied alongside a typedef
29092 specifier. If there is a typedef specifier present then set
29093 conflicting_specifiers_p which will trigger an error later
29094 on in grokdeclarator. */
29095 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
29096 decl_specs->conflicting_specifiers_p = true;
29097 }
29098
29099 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
29100 is true, the type is a class or enum definition. */
29101
29102 static void
29103 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
29104 tree type_spec,
29105 cp_token *token,
29106 bool type_definition_p)
29107 {
29108 decl_specs->any_specifiers_p = true;
29109
29110 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
29111 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
29112 this is what happened. In system headers, we ignore these
29113 declarations so that G++ can work with system headers that are not
29114 C++-safe. */
29115 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
29116 && !type_definition_p
29117 && (type_spec == boolean_type_node
29118 || type_spec == char8_type_node
29119 || type_spec == char16_type_node
29120 || type_spec == char32_type_node
29121 || type_spec == wchar_type_node)
29122 && (decl_specs->type
29123 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
29124 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
29125 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
29126 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
29127 {
29128 decl_specs->redefined_builtin_type = type_spec;
29129 set_and_check_decl_spec_loc (decl_specs,
29130 ds_redefined_builtin_type_spec,
29131 token);
29132 if (!decl_specs->type)
29133 {
29134 decl_specs->type = type_spec;
29135 decl_specs->type_definition_p = false;
29136 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
29137 }
29138 }
29139 else if (decl_specs->type)
29140 decl_specs->multiple_types_p = true;
29141 else
29142 {
29143 decl_specs->type = type_spec;
29144 decl_specs->type_definition_p = type_definition_p;
29145 decl_specs->redefined_builtin_type = NULL_TREE;
29146 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
29147 }
29148 }
29149
29150 /* True iff TOKEN is the GNU keyword __thread. */
29151
29152 static bool
29153 token_is__thread (cp_token *token)
29154 {
29155 gcc_assert (token->keyword == RID_THREAD);
29156 return id_equal (token->u.value, "__thread");
29157 }
29158
29159 /* Set the location for a declarator specifier and check if it is
29160 duplicated.
29161
29162 DECL_SPECS is the sequence of declarator specifiers onto which to
29163 set the location.
29164
29165 DS is the single declarator specifier to set which location is to
29166 be set onto the existing sequence of declarators.
29167
29168 LOCATION is the location for the declarator specifier to
29169 consider. */
29170
29171 static void
29172 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
29173 cp_decl_spec ds, cp_token *token)
29174 {
29175 gcc_assert (ds < ds_last);
29176
29177 if (decl_specs == NULL)
29178 return;
29179
29180 location_t location = token->location;
29181
29182 if (decl_specs->locations[ds] == 0)
29183 {
29184 decl_specs->locations[ds] = location;
29185 if (ds == ds_thread)
29186 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
29187 }
29188 else
29189 {
29190 if (ds == ds_long)
29191 {
29192 if (decl_specs->locations[ds_long_long] != 0)
29193 error_at (location,
29194 "%<long long long%> is too long for GCC");
29195 else
29196 {
29197 decl_specs->locations[ds_long_long] = location;
29198 pedwarn_cxx98 (location,
29199 OPT_Wlong_long,
29200 "ISO C++ 1998 does not support %<long long%>");
29201 }
29202 }
29203 else if (ds == ds_thread)
29204 {
29205 bool gnu = token_is__thread (token);
29206 gcc_rich_location richloc (location);
29207 if (gnu != decl_specs->gnu_thread_keyword_p)
29208 {
29209 richloc.add_range (decl_specs->locations[ds_thread]);
29210 error_at (&richloc,
29211 "both %<__thread%> and %<thread_local%> specified");
29212 }
29213 else
29214 {
29215 richloc.add_fixit_remove ();
29216 error_at (&richloc, "duplicate %qD", token->u.value);
29217 }
29218 }
29219 else
29220 {
29221 static const char *const decl_spec_names[] = {
29222 "signed",
29223 "unsigned",
29224 "short",
29225 "long",
29226 "const",
29227 "volatile",
29228 "restrict",
29229 "inline",
29230 "virtual",
29231 "explicit",
29232 "friend",
29233 "typedef",
29234 "using",
29235 "constexpr",
29236 "__complex"
29237 };
29238 gcc_rich_location richloc (location);
29239 richloc.add_fixit_remove ();
29240 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
29241 }
29242 }
29243 }
29244
29245 /* Return true iff the declarator specifier DS is present in the
29246 sequence of declarator specifiers DECL_SPECS. */
29247
29248 bool
29249 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
29250 cp_decl_spec ds)
29251 {
29252 gcc_assert (ds < ds_last);
29253
29254 if (decl_specs == NULL)
29255 return false;
29256
29257 return decl_specs->locations[ds] != 0;
29258 }
29259
29260 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29261 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
29262
29263 static bool
29264 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
29265 {
29266 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
29267 }
29268
29269 /* Issue an error message indicating that TOKEN_DESC was expected.
29270 If KEYWORD is true, it indicated this function is called by
29271 cp_parser_require_keword and the required token can only be
29272 a indicated keyword.
29273
29274 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29275 within any error as the location of an "opening" token matching
29276 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29277 RT_CLOSE_PAREN). */
29278
29279 static void
29280 cp_parser_required_error (cp_parser *parser,
29281 required_token token_desc,
29282 bool keyword,
29283 location_t matching_location)
29284 {
29285 if (cp_parser_simulate_error (parser))
29286 return;
29287
29288 const char *gmsgid = NULL;
29289 switch (token_desc)
29290 {
29291 case RT_NEW:
29292 gmsgid = G_("expected %<new%>");
29293 break;
29294 case RT_DELETE:
29295 gmsgid = G_("expected %<delete%>");
29296 break;
29297 case RT_RETURN:
29298 gmsgid = G_("expected %<return%>");
29299 break;
29300 case RT_WHILE:
29301 gmsgid = G_("expected %<while%>");
29302 break;
29303 case RT_EXTERN:
29304 gmsgid = G_("expected %<extern%>");
29305 break;
29306 case RT_STATIC_ASSERT:
29307 gmsgid = G_("expected %<static_assert%>");
29308 break;
29309 case RT_DECLTYPE:
29310 gmsgid = G_("expected %<decltype%>");
29311 break;
29312 case RT_OPERATOR:
29313 gmsgid = G_("expected %<operator%>");
29314 break;
29315 case RT_CLASS:
29316 gmsgid = G_("expected %<class%>");
29317 break;
29318 case RT_TEMPLATE:
29319 gmsgid = G_("expected %<template%>");
29320 break;
29321 case RT_NAMESPACE:
29322 gmsgid = G_("expected %<namespace%>");
29323 break;
29324 case RT_USING:
29325 gmsgid = G_("expected %<using%>");
29326 break;
29327 case RT_ASM:
29328 gmsgid = G_("expected %<asm%>");
29329 break;
29330 case RT_TRY:
29331 gmsgid = G_("expected %<try%>");
29332 break;
29333 case RT_CATCH:
29334 gmsgid = G_("expected %<catch%>");
29335 break;
29336 case RT_THROW:
29337 gmsgid = G_("expected %<throw%>");
29338 break;
29339 case RT_LABEL:
29340 gmsgid = G_("expected %<__label__%>");
29341 break;
29342 case RT_AT_TRY:
29343 gmsgid = G_("expected %<@try%>");
29344 break;
29345 case RT_AT_SYNCHRONIZED:
29346 gmsgid = G_("expected %<@synchronized%>");
29347 break;
29348 case RT_AT_THROW:
29349 gmsgid = G_("expected %<@throw%>");
29350 break;
29351 case RT_TRANSACTION_ATOMIC:
29352 gmsgid = G_("expected %<__transaction_atomic%>");
29353 break;
29354 case RT_TRANSACTION_RELAXED:
29355 gmsgid = G_("expected %<__transaction_relaxed%>");
29356 break;
29357 default:
29358 break;
29359 }
29360
29361 if (!gmsgid && !keyword)
29362 {
29363 switch (token_desc)
29364 {
29365 case RT_SEMICOLON:
29366 gmsgid = G_("expected %<;%>");
29367 break;
29368 case RT_OPEN_PAREN:
29369 gmsgid = G_("expected %<(%>");
29370 break;
29371 case RT_CLOSE_BRACE:
29372 gmsgid = G_("expected %<}%>");
29373 break;
29374 case RT_OPEN_BRACE:
29375 gmsgid = G_("expected %<{%>");
29376 break;
29377 case RT_CLOSE_SQUARE:
29378 gmsgid = G_("expected %<]%>");
29379 break;
29380 case RT_OPEN_SQUARE:
29381 gmsgid = G_("expected %<[%>");
29382 break;
29383 case RT_COMMA:
29384 gmsgid = G_("expected %<,%>");
29385 break;
29386 case RT_SCOPE:
29387 gmsgid = G_("expected %<::%>");
29388 break;
29389 case RT_LESS:
29390 gmsgid = G_("expected %<<%>");
29391 break;
29392 case RT_GREATER:
29393 gmsgid = G_("expected %<>%>");
29394 break;
29395 case RT_EQ:
29396 gmsgid = G_("expected %<=%>");
29397 break;
29398 case RT_ELLIPSIS:
29399 gmsgid = G_("expected %<...%>");
29400 break;
29401 case RT_MULT:
29402 gmsgid = G_("expected %<*%>");
29403 break;
29404 case RT_COMPL:
29405 gmsgid = G_("expected %<~%>");
29406 break;
29407 case RT_COLON:
29408 gmsgid = G_("expected %<:%>");
29409 break;
29410 case RT_COLON_SCOPE:
29411 gmsgid = G_("expected %<:%> or %<::%>");
29412 break;
29413 case RT_CLOSE_PAREN:
29414 gmsgid = G_("expected %<)%>");
29415 break;
29416 case RT_COMMA_CLOSE_PAREN:
29417 gmsgid = G_("expected %<,%> or %<)%>");
29418 break;
29419 case RT_PRAGMA_EOL:
29420 gmsgid = G_("expected end of line");
29421 break;
29422 case RT_NAME:
29423 gmsgid = G_("expected identifier");
29424 break;
29425 case RT_SELECT:
29426 gmsgid = G_("expected selection-statement");
29427 break;
29428 case RT_ITERATION:
29429 gmsgid = G_("expected iteration-statement");
29430 break;
29431 case RT_JUMP:
29432 gmsgid = G_("expected jump-statement");
29433 break;
29434 case RT_CLASS_KEY:
29435 gmsgid = G_("expected class-key");
29436 break;
29437 case RT_CLASS_TYPENAME_TEMPLATE:
29438 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
29439 break;
29440 default:
29441 gcc_unreachable ();
29442 }
29443 }
29444
29445 if (gmsgid)
29446 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
29447 }
29448
29449
29450 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29451 issue an error message indicating that TOKEN_DESC was expected.
29452
29453 Returns the token consumed, if the token had the appropriate type.
29454 Otherwise, returns NULL.
29455
29456 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29457 within any error as the location of an "opening" token matching
29458 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29459 RT_CLOSE_PAREN). */
29460
29461 static cp_token *
29462 cp_parser_require (cp_parser* parser,
29463 enum cpp_ttype type,
29464 required_token token_desc,
29465 location_t matching_location)
29466 {
29467 if (cp_lexer_next_token_is (parser->lexer, type))
29468 return cp_lexer_consume_token (parser->lexer);
29469 else
29470 {
29471 /* Output the MESSAGE -- unless we're parsing tentatively. */
29472 if (!cp_parser_simulate_error (parser))
29473 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
29474 matching_location);
29475 return NULL;
29476 }
29477 }
29478
29479 /* An error message is produced if the next token is not '>'.
29480 All further tokens are skipped until the desired token is
29481 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29482
29483 static void
29484 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
29485 {
29486 /* Current level of '< ... >'. */
29487 unsigned level = 0;
29488 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29489 unsigned nesting_depth = 0;
29490
29491 /* Are we ready, yet? If not, issue error message. */
29492 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
29493 return;
29494
29495 /* Skip tokens until the desired token is found. */
29496 while (true)
29497 {
29498 /* Peek at the next token. */
29499 switch (cp_lexer_peek_token (parser->lexer)->type)
29500 {
29501 case CPP_LESS:
29502 if (!nesting_depth)
29503 ++level;
29504 break;
29505
29506 case CPP_RSHIFT:
29507 if (cxx_dialect == cxx98)
29508 /* C++0x views the `>>' operator as two `>' tokens, but
29509 C++98 does not. */
29510 break;
29511 else if (!nesting_depth && level-- == 0)
29512 {
29513 /* We've hit a `>>' where the first `>' closes the
29514 template argument list, and the second `>' is
29515 spurious. Just consume the `>>' and stop; we've
29516 already produced at least one error. */
29517 cp_lexer_consume_token (parser->lexer);
29518 return;
29519 }
29520 /* Fall through for C++0x, so we handle the second `>' in
29521 the `>>'. */
29522 gcc_fallthrough ();
29523
29524 case CPP_GREATER:
29525 if (!nesting_depth && level-- == 0)
29526 {
29527 /* We've reached the token we want, consume it and stop. */
29528 cp_lexer_consume_token (parser->lexer);
29529 return;
29530 }
29531 break;
29532
29533 case CPP_OPEN_PAREN:
29534 case CPP_OPEN_SQUARE:
29535 ++nesting_depth;
29536 break;
29537
29538 case CPP_CLOSE_PAREN:
29539 case CPP_CLOSE_SQUARE:
29540 if (nesting_depth-- == 0)
29541 return;
29542 break;
29543
29544 case CPP_EOF:
29545 case CPP_PRAGMA_EOL:
29546 case CPP_SEMICOLON:
29547 case CPP_OPEN_BRACE:
29548 case CPP_CLOSE_BRACE:
29549 /* The '>' was probably forgotten, don't look further. */
29550 return;
29551
29552 default:
29553 break;
29554 }
29555
29556 /* Consume this token. */
29557 cp_lexer_consume_token (parser->lexer);
29558 }
29559 }
29560
29561 /* If the next token is the indicated keyword, consume it. Otherwise,
29562 issue an error message indicating that TOKEN_DESC was expected.
29563
29564 Returns the token consumed, if the token had the appropriate type.
29565 Otherwise, returns NULL. */
29566
29567 static cp_token *
29568 cp_parser_require_keyword (cp_parser* parser,
29569 enum rid keyword,
29570 required_token token_desc)
29571 {
29572 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
29573
29574 if (token && token->keyword != keyword)
29575 {
29576 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
29577 UNKNOWN_LOCATION);
29578 return NULL;
29579 }
29580
29581 return token;
29582 }
29583
29584 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29585 function-definition. */
29586
29587 static bool
29588 cp_parser_token_starts_function_definition_p (cp_token* token)
29589 {
29590 return (/* An ordinary function-body begins with an `{'. */
29591 token->type == CPP_OPEN_BRACE
29592 /* A ctor-initializer begins with a `:'. */
29593 || token->type == CPP_COLON
29594 /* A function-try-block begins with `try'. */
29595 || token->keyword == RID_TRY
29596 /* A function-transaction-block begins with `__transaction_atomic'
29597 or `__transaction_relaxed'. */
29598 || token->keyword == RID_TRANSACTION_ATOMIC
29599 || token->keyword == RID_TRANSACTION_RELAXED
29600 /* The named return value extension begins with `return'. */
29601 || token->keyword == RID_RETURN);
29602 }
29603
29604 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29605 definition. */
29606
29607 static bool
29608 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
29609 {
29610 cp_token *token;
29611
29612 token = cp_lexer_peek_token (parser->lexer);
29613 return (token->type == CPP_OPEN_BRACE
29614 || (token->type == CPP_COLON
29615 && !parser->colon_doesnt_start_class_def_p));
29616 }
29617
29618 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29619 C++0x) ending a template-argument. */
29620
29621 static bool
29622 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
29623 {
29624 cp_token *token;
29625
29626 token = cp_lexer_peek_token (parser->lexer);
29627 return (token->type == CPP_COMMA
29628 || token->type == CPP_GREATER
29629 || token->type == CPP_ELLIPSIS
29630 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
29631 }
29632
29633 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29634 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29635
29636 static bool
29637 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
29638 size_t n)
29639 {
29640 cp_token *token;
29641
29642 token = cp_lexer_peek_nth_token (parser->lexer, n);
29643 if (token->type == CPP_LESS)
29644 return true;
29645 /* Check for the sequence `<::' in the original code. It would be lexed as
29646 `[:', where `[' is a digraph, and there is no whitespace before
29647 `:'. */
29648 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
29649 {
29650 cp_token *token2;
29651 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29652 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29653 return true;
29654 }
29655 return false;
29656 }
29657
29658 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29659 or none_type otherwise. */
29660
29661 static enum tag_types
29662 cp_parser_token_is_class_key (cp_token* token)
29663 {
29664 switch (token->keyword)
29665 {
29666 case RID_CLASS:
29667 return class_type;
29668 case RID_STRUCT:
29669 return record_type;
29670 case RID_UNION:
29671 return union_type;
29672
29673 default:
29674 return none_type;
29675 }
29676 }
29677
29678 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29679 or none_type otherwise or if the token is null. */
29680
29681 static enum tag_types
29682 cp_parser_token_is_type_parameter_key (cp_token* token)
29683 {
29684 if (!token)
29685 return none_type;
29686
29687 switch (token->keyword)
29688 {
29689 case RID_CLASS:
29690 return class_type;
29691 case RID_TYPENAME:
29692 return typename_type;
29693
29694 default:
29695 return none_type;
29696 }
29697 }
29698
29699 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29700
29701 static void
29702 cp_parser_check_class_key (enum tag_types class_key, tree type)
29703 {
29704 if (type == error_mark_node)
29705 return;
29706 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29707 {
29708 if (permerror (input_location, "%qs tag used in naming %q#T",
29709 class_key == union_type ? "union"
29710 : class_key == record_type ? "struct" : "class",
29711 type))
29712 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29713 "%q#T was previously declared here", type);
29714 }
29715 }
29716
29717 /* Issue an error message if DECL is redeclared with different
29718 access than its original declaration [class.access.spec/3].
29719 This applies to nested classes, nested class templates and
29720 enumerations [class.mem/1]. */
29721
29722 static void
29723 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29724 {
29725 if (!decl
29726 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29727 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29728 return;
29729
29730 if ((TREE_PRIVATE (decl)
29731 != (current_access_specifier == access_private_node))
29732 || (TREE_PROTECTED (decl)
29733 != (current_access_specifier == access_protected_node)))
29734 error_at (location, "%qD redeclared with different access", decl);
29735 }
29736
29737 /* Look for the `template' keyword, as a syntactic disambiguator.
29738 Return TRUE iff it is present, in which case it will be
29739 consumed. */
29740
29741 static bool
29742 cp_parser_optional_template_keyword (cp_parser *parser)
29743 {
29744 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29745 {
29746 /* In C++98 the `template' keyword can only be used within templates;
29747 outside templates the parser can always figure out what is a
29748 template and what is not. In C++11, per the resolution of DR 468,
29749 `template' is allowed in cases where it is not strictly necessary. */
29750 if (!processing_template_decl
29751 && pedantic && cxx_dialect == cxx98)
29752 {
29753 cp_token *token = cp_lexer_peek_token (parser->lexer);
29754 pedwarn (token->location, OPT_Wpedantic,
29755 "in C++98 %<template%> (as a disambiguator) is only "
29756 "allowed within templates");
29757 /* If this part of the token stream is rescanned, the same
29758 error message would be generated. So, we purge the token
29759 from the stream. */
29760 cp_lexer_purge_token (parser->lexer);
29761 return false;
29762 }
29763 else
29764 {
29765 /* Consume the `template' keyword. */
29766 cp_lexer_consume_token (parser->lexer);
29767 return true;
29768 }
29769 }
29770 return false;
29771 }
29772
29773 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29774 set PARSER->SCOPE, and perform other related actions. */
29775
29776 static void
29777 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29778 {
29779 struct tree_check *check_value;
29780
29781 /* Get the stored value. */
29782 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29783 /* Set the scope from the stored value. */
29784 parser->scope = saved_checks_value (check_value);
29785 parser->qualifying_scope = check_value->qualifying_scope;
29786 parser->object_scope = NULL_TREE;
29787 }
29788
29789 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29790 encounter the end of a block before what we were looking for. */
29791
29792 static bool
29793 cp_parser_cache_group (cp_parser *parser,
29794 enum cpp_ttype end,
29795 unsigned depth)
29796 {
29797 while (true)
29798 {
29799 cp_token *token = cp_lexer_peek_token (parser->lexer);
29800
29801 /* Abort a parenthesized expression if we encounter a semicolon. */
29802 if ((end == CPP_CLOSE_PAREN || depth == 0)
29803 && token->type == CPP_SEMICOLON)
29804 return true;
29805 /* If we've reached the end of the file, stop. */
29806 if (token->type == CPP_EOF
29807 || (end != CPP_PRAGMA_EOL
29808 && token->type == CPP_PRAGMA_EOL))
29809 return true;
29810 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29811 /* We've hit the end of an enclosing block, so there's been some
29812 kind of syntax error. */
29813 return true;
29814
29815 /* Consume the token. */
29816 cp_lexer_consume_token (parser->lexer);
29817 /* See if it starts a new group. */
29818 if (token->type == CPP_OPEN_BRACE)
29819 {
29820 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29821 /* In theory this should probably check end == '}', but
29822 cp_parser_save_member_function_body needs it to exit
29823 after either '}' or ')' when called with ')'. */
29824 if (depth == 0)
29825 return false;
29826 }
29827 else if (token->type == CPP_OPEN_PAREN)
29828 {
29829 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29830 if (depth == 0 && end == CPP_CLOSE_PAREN)
29831 return false;
29832 }
29833 else if (token->type == CPP_PRAGMA)
29834 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29835 else if (token->type == end)
29836 return false;
29837 }
29838 }
29839
29840 /* Like above, for caching a default argument or NSDMI. Both of these are
29841 terminated by a non-nested comma, but it can be unclear whether or not a
29842 comma is nested in a template argument list unless we do more parsing.
29843 In order to handle this ambiguity, when we encounter a ',' after a '<'
29844 we try to parse what follows as a parameter-declaration-list (in the
29845 case of a default argument) or a member-declarator (in the case of an
29846 NSDMI). If that succeeds, then we stop caching. */
29847
29848 static tree
29849 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29850 {
29851 unsigned depth = 0;
29852 int maybe_template_id = 0;
29853 cp_token *first_token;
29854 cp_token *token;
29855 tree default_argument;
29856
29857 /* Add tokens until we have processed the entire default
29858 argument. We add the range [first_token, token). */
29859 first_token = cp_lexer_peek_token (parser->lexer);
29860 if (first_token->type == CPP_OPEN_BRACE)
29861 {
29862 /* For list-initialization, this is straightforward. */
29863 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29864 token = cp_lexer_peek_token (parser->lexer);
29865 }
29866 else while (true)
29867 {
29868 bool done = false;
29869
29870 /* Peek at the next token. */
29871 token = cp_lexer_peek_token (parser->lexer);
29872 /* What we do depends on what token we have. */
29873 switch (token->type)
29874 {
29875 /* In valid code, a default argument must be
29876 immediately followed by a `,' `)', or `...'. */
29877 case CPP_COMMA:
29878 if (depth == 0 && maybe_template_id)
29879 {
29880 /* If we've seen a '<', we might be in a
29881 template-argument-list. Until Core issue 325 is
29882 resolved, we don't know how this situation ought
29883 to be handled, so try to DTRT. We check whether
29884 what comes after the comma is a valid parameter
29885 declaration list. If it is, then the comma ends
29886 the default argument; otherwise the default
29887 argument continues. */
29888 bool error = false;
29889 cp_token *peek;
29890
29891 /* Set ITALP so cp_parser_parameter_declaration_list
29892 doesn't decide to commit to this parse. */
29893 bool saved_italp = parser->in_template_argument_list_p;
29894 parser->in_template_argument_list_p = true;
29895
29896 cp_parser_parse_tentatively (parser);
29897
29898 if (nsdmi)
29899 {
29900 /* Parse declarators until we reach a non-comma or
29901 somthing that cannot be an initializer.
29902 Just checking whether we're looking at a single
29903 declarator is insufficient. Consider:
29904 int var = tuple<T,U>::x;
29905 The template parameter 'U' looks exactly like a
29906 declarator. */
29907 do
29908 {
29909 int ctor_dtor_or_conv_p;
29910 cp_lexer_consume_token (parser->lexer);
29911 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29912 CP_PARSER_FLAGS_NONE,
29913 &ctor_dtor_or_conv_p,
29914 /*parenthesized_p=*/NULL,
29915 /*member_p=*/true,
29916 /*friend_p=*/false,
29917 /*static_p=*/false);
29918 peek = cp_lexer_peek_token (parser->lexer);
29919 if (cp_parser_error_occurred (parser))
29920 break;
29921 }
29922 while (peek->type == CPP_COMMA);
29923 /* If we met an '=' or ';' then the original comma
29924 was the end of the NSDMI. Otherwise assume
29925 we're still in the NSDMI. */
29926 error = (peek->type != CPP_EQ
29927 && peek->type != CPP_SEMICOLON);
29928 }
29929 else
29930 {
29931 cp_lexer_consume_token (parser->lexer);
29932 begin_scope (sk_function_parms, NULL_TREE);
29933 tree t = cp_parser_parameter_declaration_list
29934 (parser, CP_PARSER_FLAGS_NONE);
29935 if (t == error_mark_node)
29936 error = true;
29937 pop_bindings_and_leave_scope ();
29938 }
29939 if (!cp_parser_error_occurred (parser) && !error)
29940 done = true;
29941 cp_parser_abort_tentative_parse (parser);
29942
29943 parser->in_template_argument_list_p = saved_italp;
29944 break;
29945 }
29946 /* FALLTHRU */
29947 case CPP_CLOSE_PAREN:
29948 case CPP_ELLIPSIS:
29949 /* If we run into a non-nested `;', `}', or `]',
29950 then the code is invalid -- but the default
29951 argument is certainly over. */
29952 case CPP_SEMICOLON:
29953 case CPP_CLOSE_BRACE:
29954 case CPP_CLOSE_SQUARE:
29955 if (depth == 0
29956 /* Handle correctly int n = sizeof ... ( p ); */
29957 && token->type != CPP_ELLIPSIS)
29958 done = true;
29959 /* Update DEPTH, if necessary. */
29960 else if (token->type == CPP_CLOSE_PAREN
29961 || token->type == CPP_CLOSE_BRACE
29962 || token->type == CPP_CLOSE_SQUARE)
29963 --depth;
29964 break;
29965
29966 case CPP_OPEN_PAREN:
29967 case CPP_OPEN_SQUARE:
29968 case CPP_OPEN_BRACE:
29969 ++depth;
29970 break;
29971
29972 case CPP_LESS:
29973 if (depth == 0)
29974 /* This might be the comparison operator, or it might
29975 start a template argument list. */
29976 ++maybe_template_id;
29977 break;
29978
29979 case CPP_RSHIFT:
29980 if (cxx_dialect == cxx98)
29981 break;
29982 /* Fall through for C++0x, which treats the `>>'
29983 operator like two `>' tokens in certain
29984 cases. */
29985 gcc_fallthrough ();
29986
29987 case CPP_GREATER:
29988 if (depth == 0)
29989 {
29990 /* This might be an operator, or it might close a
29991 template argument list. But if a previous '<'
29992 started a template argument list, this will have
29993 closed it, so we can't be in one anymore. */
29994 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29995 if (maybe_template_id < 0)
29996 maybe_template_id = 0;
29997 }
29998 break;
29999
30000 /* If we run out of tokens, issue an error message. */
30001 case CPP_EOF:
30002 case CPP_PRAGMA_EOL:
30003 error_at (token->location, "file ends in default argument");
30004 return error_mark_node;
30005
30006 case CPP_NAME:
30007 case CPP_SCOPE:
30008 /* In these cases, we should look for template-ids.
30009 For example, if the default argument is
30010 `X<int, double>()', we need to do name lookup to
30011 figure out whether or not `X' is a template; if
30012 so, the `,' does not end the default argument.
30013
30014 That is not yet done. */
30015 break;
30016
30017 default:
30018 break;
30019 }
30020
30021 /* If we've reached the end, stop. */
30022 if (done)
30023 break;
30024
30025 /* Add the token to the token block. */
30026 token = cp_lexer_consume_token (parser->lexer);
30027 }
30028
30029 /* Create a DEFAULT_ARG to represent the unparsed default
30030 argument. */
30031 default_argument = make_node (DEFAULT_ARG);
30032 DEFARG_TOKENS (default_argument)
30033 = cp_token_cache_new (first_token, token);
30034 DEFARG_INSTANTIATIONS (default_argument) = NULL;
30035
30036 return default_argument;
30037 }
30038
30039 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
30040
30041 location_t
30042 defarg_location (tree default_argument)
30043 {
30044 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
30045 location_t start = tokens->first->location;
30046 location_t end = tokens->last->location;
30047 return make_location (start, start, end);
30048 }
30049
30050 /* Begin parsing tentatively. We always save tokens while parsing
30051 tentatively so that if the tentative parsing fails we can restore the
30052 tokens. */
30053
30054 static void
30055 cp_parser_parse_tentatively (cp_parser* parser)
30056 {
30057 /* Enter a new parsing context. */
30058 parser->context = cp_parser_context_new (parser->context);
30059 /* Begin saving tokens. */
30060 cp_lexer_save_tokens (parser->lexer);
30061 /* In order to avoid repetitive access control error messages,
30062 access checks are queued up until we are no longer parsing
30063 tentatively. */
30064 push_deferring_access_checks (dk_deferred);
30065 }
30066
30067 /* Commit to the currently active tentative parse. */
30068
30069 static void
30070 cp_parser_commit_to_tentative_parse (cp_parser* parser)
30071 {
30072 cp_parser_context *context;
30073 cp_lexer *lexer;
30074
30075 /* Mark all of the levels as committed. */
30076 lexer = parser->lexer;
30077 for (context = parser->context; context->next; context = context->next)
30078 {
30079 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
30080 break;
30081 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
30082 while (!cp_lexer_saving_tokens (lexer))
30083 lexer = lexer->next;
30084 cp_lexer_commit_tokens (lexer);
30085 }
30086 }
30087
30088 /* Commit to the topmost currently active tentative parse.
30089
30090 Note that this function shouldn't be called when there are
30091 irreversible side-effects while in a tentative state. For
30092 example, we shouldn't create a permanent entry in the symbol
30093 table, or issue an error message that might not apply if the
30094 tentative parse is aborted. */
30095
30096 static void
30097 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
30098 {
30099 cp_parser_context *context = parser->context;
30100 cp_lexer *lexer = parser->lexer;
30101
30102 if (context)
30103 {
30104 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
30105 return;
30106 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
30107
30108 while (!cp_lexer_saving_tokens (lexer))
30109 lexer = lexer->next;
30110 cp_lexer_commit_tokens (lexer);
30111 }
30112 }
30113
30114 /* Abort the currently active tentative parse. All consumed tokens
30115 will be rolled back, and no diagnostics will be issued. */
30116
30117 static void
30118 cp_parser_abort_tentative_parse (cp_parser* parser)
30119 {
30120 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
30121 || errorcount > 0);
30122 cp_parser_simulate_error (parser);
30123 /* Now, pretend that we want to see if the construct was
30124 successfully parsed. */
30125 cp_parser_parse_definitely (parser);
30126 }
30127
30128 /* Stop parsing tentatively. If a parse error has occurred, restore the
30129 token stream. Otherwise, commit to the tokens we have consumed.
30130 Returns true if no error occurred; false otherwise. */
30131
30132 static bool
30133 cp_parser_parse_definitely (cp_parser* parser)
30134 {
30135 bool error_occurred;
30136 cp_parser_context *context;
30137
30138 /* Remember whether or not an error occurred, since we are about to
30139 destroy that information. */
30140 error_occurred = cp_parser_error_occurred (parser);
30141 /* Remove the topmost context from the stack. */
30142 context = parser->context;
30143 parser->context = context->next;
30144 /* If no parse errors occurred, commit to the tentative parse. */
30145 if (!error_occurred)
30146 {
30147 /* Commit to the tokens read tentatively, unless that was
30148 already done. */
30149 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
30150 cp_lexer_commit_tokens (parser->lexer);
30151
30152 pop_to_parent_deferring_access_checks ();
30153 }
30154 /* Otherwise, if errors occurred, roll back our state so that things
30155 are just as they were before we began the tentative parse. */
30156 else
30157 {
30158 cp_lexer_rollback_tokens (parser->lexer);
30159 pop_deferring_access_checks ();
30160 }
30161 /* Add the context to the front of the free list. */
30162 context->next = cp_parser_context_free_list;
30163 cp_parser_context_free_list = context;
30164
30165 return !error_occurred;
30166 }
30167
30168 /* Returns true if we are parsing tentatively and are not committed to
30169 this tentative parse. */
30170
30171 static bool
30172 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
30173 {
30174 return (cp_parser_parsing_tentatively (parser)
30175 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
30176 }
30177
30178 /* Returns nonzero iff an error has occurred during the most recent
30179 tentative parse. */
30180
30181 static bool
30182 cp_parser_error_occurred (cp_parser* parser)
30183 {
30184 return (cp_parser_parsing_tentatively (parser)
30185 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
30186 }
30187
30188 /* Returns nonzero if GNU extensions are allowed. */
30189
30190 static bool
30191 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
30192 {
30193 return parser->allow_gnu_extensions_p;
30194 }
30195 \f
30196 /* Objective-C++ Productions */
30197
30198
30199 /* Parse an Objective-C expression, which feeds into a primary-expression
30200 above.
30201
30202 objc-expression:
30203 objc-message-expression
30204 objc-string-literal
30205 objc-encode-expression
30206 objc-protocol-expression
30207 objc-selector-expression
30208
30209 Returns a tree representation of the expression. */
30210
30211 static cp_expr
30212 cp_parser_objc_expression (cp_parser* parser)
30213 {
30214 /* Try to figure out what kind of declaration is present. */
30215 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30216
30217 switch (kwd->type)
30218 {
30219 case CPP_OPEN_SQUARE:
30220 return cp_parser_objc_message_expression (parser);
30221
30222 case CPP_OBJC_STRING:
30223 kwd = cp_lexer_consume_token (parser->lexer);
30224 return objc_build_string_object (kwd->u.value);
30225
30226 case CPP_KEYWORD:
30227 switch (kwd->keyword)
30228 {
30229 case RID_AT_ENCODE:
30230 return cp_parser_objc_encode_expression (parser);
30231
30232 case RID_AT_PROTOCOL:
30233 return cp_parser_objc_protocol_expression (parser);
30234
30235 case RID_AT_SELECTOR:
30236 return cp_parser_objc_selector_expression (parser);
30237
30238 default:
30239 break;
30240 }
30241 /* FALLTHRU */
30242 default:
30243 error_at (kwd->location,
30244 "misplaced %<@%D%> Objective-C++ construct",
30245 kwd->u.value);
30246 cp_parser_skip_to_end_of_block_or_statement (parser);
30247 }
30248
30249 return error_mark_node;
30250 }
30251
30252 /* Parse an Objective-C message expression.
30253
30254 objc-message-expression:
30255 [ objc-message-receiver objc-message-args ]
30256
30257 Returns a representation of an Objective-C message. */
30258
30259 static tree
30260 cp_parser_objc_message_expression (cp_parser* parser)
30261 {
30262 tree receiver, messageargs;
30263
30264 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30265 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
30266 receiver = cp_parser_objc_message_receiver (parser);
30267 messageargs = cp_parser_objc_message_args (parser);
30268 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
30269 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30270
30271 tree result = objc_build_message_expr (receiver, messageargs);
30272
30273 /* Construct a location e.g.
30274 [self func1:5]
30275 ^~~~~~~~~~~~~~
30276 ranging from the '[' to the ']', with the caret at the start. */
30277 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
30278 protected_set_expr_location (result, combined_loc);
30279
30280 return result;
30281 }
30282
30283 /* Parse an objc-message-receiver.
30284
30285 objc-message-receiver:
30286 expression
30287 simple-type-specifier
30288
30289 Returns a representation of the type or expression. */
30290
30291 static tree
30292 cp_parser_objc_message_receiver (cp_parser* parser)
30293 {
30294 tree rcv;
30295
30296 /* An Objective-C message receiver may be either (1) a type
30297 or (2) an expression. */
30298 cp_parser_parse_tentatively (parser);
30299 rcv = cp_parser_expression (parser);
30300
30301 /* If that worked out, fine. */
30302 if (cp_parser_parse_definitely (parser))
30303 return rcv;
30304
30305 cp_parser_parse_tentatively (parser);
30306 rcv = cp_parser_simple_type_specifier (parser,
30307 /*decl_specs=*/NULL,
30308 CP_PARSER_FLAGS_NONE);
30309
30310 if (cp_parser_parse_definitely (parser))
30311 return objc_get_class_reference (rcv);
30312
30313 cp_parser_error (parser, "objective-c++ message receiver expected");
30314 return error_mark_node;
30315 }
30316
30317 /* Parse the arguments and selectors comprising an Objective-C message.
30318
30319 objc-message-args:
30320 objc-selector
30321 objc-selector-args
30322 objc-selector-args , objc-comma-args
30323
30324 objc-selector-args:
30325 objc-selector [opt] : assignment-expression
30326 objc-selector-args objc-selector [opt] : assignment-expression
30327
30328 objc-comma-args:
30329 assignment-expression
30330 objc-comma-args , assignment-expression
30331
30332 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30333 selector arguments and TREE_VALUE containing a list of comma
30334 arguments. */
30335
30336 static tree
30337 cp_parser_objc_message_args (cp_parser* parser)
30338 {
30339 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
30340 bool maybe_unary_selector_p = true;
30341 cp_token *token = cp_lexer_peek_token (parser->lexer);
30342
30343 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30344 {
30345 tree selector = NULL_TREE, arg;
30346
30347 if (token->type != CPP_COLON)
30348 selector = cp_parser_objc_selector (parser);
30349
30350 /* Detect if we have a unary selector. */
30351 if (maybe_unary_selector_p
30352 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30353 return build_tree_list (selector, NULL_TREE);
30354
30355 maybe_unary_selector_p = false;
30356 cp_parser_require (parser, CPP_COLON, RT_COLON);
30357 arg = cp_parser_assignment_expression (parser);
30358
30359 sel_args
30360 = chainon (sel_args,
30361 build_tree_list (selector, arg));
30362
30363 token = cp_lexer_peek_token (parser->lexer);
30364 }
30365
30366 /* Handle non-selector arguments, if any. */
30367 while (token->type == CPP_COMMA)
30368 {
30369 tree arg;
30370
30371 cp_lexer_consume_token (parser->lexer);
30372 arg = cp_parser_assignment_expression (parser);
30373
30374 addl_args
30375 = chainon (addl_args,
30376 build_tree_list (NULL_TREE, arg));
30377
30378 token = cp_lexer_peek_token (parser->lexer);
30379 }
30380
30381 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
30382 {
30383 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
30384 return build_tree_list (error_mark_node, error_mark_node);
30385 }
30386
30387 return build_tree_list (sel_args, addl_args);
30388 }
30389
30390 /* Parse an Objective-C encode expression.
30391
30392 objc-encode-expression:
30393 @encode objc-typename
30394
30395 Returns an encoded representation of the type argument. */
30396
30397 static cp_expr
30398 cp_parser_objc_encode_expression (cp_parser* parser)
30399 {
30400 tree type;
30401 cp_token *token;
30402 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30403
30404 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
30405 matching_parens parens;
30406 parens.require_open (parser);
30407 token = cp_lexer_peek_token (parser->lexer);
30408 type = complete_type (cp_parser_type_id (parser));
30409 parens.require_close (parser);
30410
30411 if (!type)
30412 {
30413 error_at (token->location,
30414 "%<@encode%> must specify a type as an argument");
30415 return error_mark_node;
30416 }
30417
30418 /* This happens if we find @encode(T) (where T is a template
30419 typename or something dependent on a template typename) when
30420 parsing a template. In that case, we can't compile it
30421 immediately, but we rather create an AT_ENCODE_EXPR which will
30422 need to be instantiated when the template is used.
30423 */
30424 if (dependent_type_p (type))
30425 {
30426 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
30427 TREE_READONLY (value) = 1;
30428 return value;
30429 }
30430
30431
30432 /* Build a location of the form:
30433 @encode(int)
30434 ^~~~~~~~~~~~
30435 with caret==start at the @ token, finishing at the close paren. */
30436 location_t combined_loc
30437 = make_location (start_loc, start_loc,
30438 cp_lexer_previous_token (parser->lexer)->location);
30439
30440 return cp_expr (objc_build_encode_expr (type), combined_loc);
30441 }
30442
30443 /* Parse an Objective-C @defs expression. */
30444
30445 static tree
30446 cp_parser_objc_defs_expression (cp_parser *parser)
30447 {
30448 tree name;
30449
30450 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
30451 matching_parens parens;
30452 parens.require_open (parser);
30453 name = cp_parser_identifier (parser);
30454 parens.require_close (parser);
30455
30456 return objc_get_class_ivars (name);
30457 }
30458
30459 /* Parse an Objective-C protocol expression.
30460
30461 objc-protocol-expression:
30462 @protocol ( identifier )
30463
30464 Returns a representation of the protocol expression. */
30465
30466 static tree
30467 cp_parser_objc_protocol_expression (cp_parser* parser)
30468 {
30469 tree proto;
30470 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30471
30472 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30473 matching_parens parens;
30474 parens.require_open (parser);
30475 proto = cp_parser_identifier (parser);
30476 parens.require_close (parser);
30477
30478 /* Build a location of the form:
30479 @protocol(prot)
30480 ^~~~~~~~~~~~~~~
30481 with caret==start at the @ token, finishing at the close paren. */
30482 location_t combined_loc
30483 = make_location (start_loc, start_loc,
30484 cp_lexer_previous_token (parser->lexer)->location);
30485 tree result = objc_build_protocol_expr (proto);
30486 protected_set_expr_location (result, combined_loc);
30487 return result;
30488 }
30489
30490 /* Parse an Objective-C selector expression.
30491
30492 objc-selector-expression:
30493 @selector ( objc-method-signature )
30494
30495 objc-method-signature:
30496 objc-selector
30497 objc-selector-seq
30498
30499 objc-selector-seq:
30500 objc-selector :
30501 objc-selector-seq objc-selector :
30502
30503 Returns a representation of the method selector. */
30504
30505 static tree
30506 cp_parser_objc_selector_expression (cp_parser* parser)
30507 {
30508 tree sel_seq = NULL_TREE;
30509 bool maybe_unary_selector_p = true;
30510 cp_token *token;
30511 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30512
30513 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
30514 matching_parens parens;
30515 parens.require_open (parser);
30516 token = cp_lexer_peek_token (parser->lexer);
30517
30518 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
30519 || token->type == CPP_SCOPE)
30520 {
30521 tree selector = NULL_TREE;
30522
30523 if (token->type != CPP_COLON
30524 || token->type == CPP_SCOPE)
30525 selector = cp_parser_objc_selector (parser);
30526
30527 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
30528 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
30529 {
30530 /* Detect if we have a unary selector. */
30531 if (maybe_unary_selector_p)
30532 {
30533 sel_seq = selector;
30534 goto finish_selector;
30535 }
30536 else
30537 {
30538 cp_parser_error (parser, "expected %<:%>");
30539 }
30540 }
30541 maybe_unary_selector_p = false;
30542 token = cp_lexer_consume_token (parser->lexer);
30543
30544 if (token->type == CPP_SCOPE)
30545 {
30546 sel_seq
30547 = chainon (sel_seq,
30548 build_tree_list (selector, NULL_TREE));
30549 sel_seq
30550 = chainon (sel_seq,
30551 build_tree_list (NULL_TREE, NULL_TREE));
30552 }
30553 else
30554 sel_seq
30555 = chainon (sel_seq,
30556 build_tree_list (selector, NULL_TREE));
30557
30558 token = cp_lexer_peek_token (parser->lexer);
30559 }
30560
30561 finish_selector:
30562 parens.require_close (parser);
30563
30564
30565 /* Build a location of the form:
30566 @selector(func)
30567 ^~~~~~~~~~~~~~~
30568 with caret==start at the @ token, finishing at the close paren. */
30569 location_t combined_loc
30570 = make_location (loc, loc,
30571 cp_lexer_previous_token (parser->lexer)->location);
30572 tree result = objc_build_selector_expr (combined_loc, sel_seq);
30573 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30574 protected_set_expr_location (result, combined_loc);
30575 return result;
30576 }
30577
30578 /* Parse a list of identifiers.
30579
30580 objc-identifier-list:
30581 identifier
30582 objc-identifier-list , identifier
30583
30584 Returns a TREE_LIST of identifier nodes. */
30585
30586 static tree
30587 cp_parser_objc_identifier_list (cp_parser* parser)
30588 {
30589 tree identifier;
30590 tree list;
30591 cp_token *sep;
30592
30593 identifier = cp_parser_identifier (parser);
30594 if (identifier == error_mark_node)
30595 return error_mark_node;
30596
30597 list = build_tree_list (NULL_TREE, identifier);
30598 sep = cp_lexer_peek_token (parser->lexer);
30599
30600 while (sep->type == CPP_COMMA)
30601 {
30602 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30603 identifier = cp_parser_identifier (parser);
30604 if (identifier == error_mark_node)
30605 return list;
30606
30607 list = chainon (list, build_tree_list (NULL_TREE,
30608 identifier));
30609 sep = cp_lexer_peek_token (parser->lexer);
30610 }
30611
30612 return list;
30613 }
30614
30615 /* Parse an Objective-C alias declaration.
30616
30617 objc-alias-declaration:
30618 @compatibility_alias identifier identifier ;
30619
30620 This function registers the alias mapping with the Objective-C front end.
30621 It returns nothing. */
30622
30623 static void
30624 cp_parser_objc_alias_declaration (cp_parser* parser)
30625 {
30626 tree alias, orig;
30627
30628 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
30629 alias = cp_parser_identifier (parser);
30630 orig = cp_parser_identifier (parser);
30631 objc_declare_alias (alias, orig);
30632 cp_parser_consume_semicolon_at_end_of_statement (parser);
30633 }
30634
30635 /* Parse an Objective-C class forward-declaration.
30636
30637 objc-class-declaration:
30638 @class objc-identifier-list ;
30639
30640 The function registers the forward declarations with the Objective-C
30641 front end. It returns nothing. */
30642
30643 static void
30644 cp_parser_objc_class_declaration (cp_parser* parser)
30645 {
30646 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
30647 while (true)
30648 {
30649 tree id;
30650
30651 id = cp_parser_identifier (parser);
30652 if (id == error_mark_node)
30653 break;
30654
30655 objc_declare_class (id);
30656
30657 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30658 cp_lexer_consume_token (parser->lexer);
30659 else
30660 break;
30661 }
30662 cp_parser_consume_semicolon_at_end_of_statement (parser);
30663 }
30664
30665 /* Parse a list of Objective-C protocol references.
30666
30667 objc-protocol-refs-opt:
30668 objc-protocol-refs [opt]
30669
30670 objc-protocol-refs:
30671 < objc-identifier-list >
30672
30673 Returns a TREE_LIST of identifiers, if any. */
30674
30675 static tree
30676 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30677 {
30678 tree protorefs = NULL_TREE;
30679
30680 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30681 {
30682 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30683 protorefs = cp_parser_objc_identifier_list (parser);
30684 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30685 }
30686
30687 return protorefs;
30688 }
30689
30690 /* Parse a Objective-C visibility specification. */
30691
30692 static void
30693 cp_parser_objc_visibility_spec (cp_parser* parser)
30694 {
30695 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30696
30697 switch (vis->keyword)
30698 {
30699 case RID_AT_PRIVATE:
30700 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30701 break;
30702 case RID_AT_PROTECTED:
30703 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30704 break;
30705 case RID_AT_PUBLIC:
30706 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30707 break;
30708 case RID_AT_PACKAGE:
30709 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30710 break;
30711 default:
30712 return;
30713 }
30714
30715 /* Eat '@private'/'@protected'/'@public'. */
30716 cp_lexer_consume_token (parser->lexer);
30717 }
30718
30719 /* Parse an Objective-C method type. Return 'true' if it is a class
30720 (+) method, and 'false' if it is an instance (-) method. */
30721
30722 static inline bool
30723 cp_parser_objc_method_type (cp_parser* parser)
30724 {
30725 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30726 return true;
30727 else
30728 return false;
30729 }
30730
30731 /* Parse an Objective-C protocol qualifier. */
30732
30733 static tree
30734 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30735 {
30736 tree quals = NULL_TREE, node;
30737 cp_token *token = cp_lexer_peek_token (parser->lexer);
30738
30739 node = token->u.value;
30740
30741 while (node && identifier_p (node)
30742 && (node == ridpointers [(int) RID_IN]
30743 || node == ridpointers [(int) RID_OUT]
30744 || node == ridpointers [(int) RID_INOUT]
30745 || node == ridpointers [(int) RID_BYCOPY]
30746 || node == ridpointers [(int) RID_BYREF]
30747 || node == ridpointers [(int) RID_ONEWAY]))
30748 {
30749 quals = tree_cons (NULL_TREE, node, quals);
30750 cp_lexer_consume_token (parser->lexer);
30751 token = cp_lexer_peek_token (parser->lexer);
30752 node = token->u.value;
30753 }
30754
30755 return quals;
30756 }
30757
30758 /* Parse an Objective-C typename. */
30759
30760 static tree
30761 cp_parser_objc_typename (cp_parser* parser)
30762 {
30763 tree type_name = NULL_TREE;
30764
30765 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30766 {
30767 tree proto_quals, cp_type = NULL_TREE;
30768
30769 matching_parens parens;
30770 parens.consume_open (parser); /* Eat '('. */
30771 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30772
30773 /* An ObjC type name may consist of just protocol qualifiers, in which
30774 case the type shall default to 'id'. */
30775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30776 {
30777 cp_type = cp_parser_type_id (parser);
30778
30779 /* If the type could not be parsed, an error has already
30780 been produced. For error recovery, behave as if it had
30781 not been specified, which will use the default type
30782 'id'. */
30783 if (cp_type == error_mark_node)
30784 {
30785 cp_type = NULL_TREE;
30786 /* We need to skip to the closing parenthesis as
30787 cp_parser_type_id() does not seem to do it for
30788 us. */
30789 cp_parser_skip_to_closing_parenthesis (parser,
30790 /*recovering=*/true,
30791 /*or_comma=*/false,
30792 /*consume_paren=*/false);
30793 }
30794 }
30795
30796 parens.require_close (parser);
30797 type_name = build_tree_list (proto_quals, cp_type);
30798 }
30799
30800 return type_name;
30801 }
30802
30803 /* Check to see if TYPE refers to an Objective-C selector name. */
30804
30805 static bool
30806 cp_parser_objc_selector_p (enum cpp_ttype type)
30807 {
30808 return (type == CPP_NAME || type == CPP_KEYWORD
30809 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30810 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30811 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30812 || type == CPP_XOR || type == CPP_XOR_EQ);
30813 }
30814
30815 /* Parse an Objective-C selector. */
30816
30817 static tree
30818 cp_parser_objc_selector (cp_parser* parser)
30819 {
30820 cp_token *token = cp_lexer_consume_token (parser->lexer);
30821
30822 if (!cp_parser_objc_selector_p (token->type))
30823 {
30824 error_at (token->location, "invalid Objective-C++ selector name");
30825 return error_mark_node;
30826 }
30827
30828 /* C++ operator names are allowed to appear in ObjC selectors. */
30829 switch (token->type)
30830 {
30831 case CPP_AND_AND: return get_identifier ("and");
30832 case CPP_AND_EQ: return get_identifier ("and_eq");
30833 case CPP_AND: return get_identifier ("bitand");
30834 case CPP_OR: return get_identifier ("bitor");
30835 case CPP_COMPL: return get_identifier ("compl");
30836 case CPP_NOT: return get_identifier ("not");
30837 case CPP_NOT_EQ: return get_identifier ("not_eq");
30838 case CPP_OR_OR: return get_identifier ("or");
30839 case CPP_OR_EQ: return get_identifier ("or_eq");
30840 case CPP_XOR: return get_identifier ("xor");
30841 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30842 default: return token->u.value;
30843 }
30844 }
30845
30846 /* Parse an Objective-C params list. */
30847
30848 static tree
30849 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30850 {
30851 tree params = NULL_TREE;
30852 bool maybe_unary_selector_p = true;
30853 cp_token *token = cp_lexer_peek_token (parser->lexer);
30854
30855 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30856 {
30857 tree selector = NULL_TREE, type_name, identifier;
30858 tree parm_attr = NULL_TREE;
30859
30860 if (token->keyword == RID_ATTRIBUTE)
30861 break;
30862
30863 if (token->type != CPP_COLON)
30864 selector = cp_parser_objc_selector (parser);
30865
30866 /* Detect if we have a unary selector. */
30867 if (maybe_unary_selector_p
30868 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30869 {
30870 params = selector; /* Might be followed by attributes. */
30871 break;
30872 }
30873
30874 maybe_unary_selector_p = false;
30875 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30876 {
30877 /* Something went quite wrong. There should be a colon
30878 here, but there is not. Stop parsing parameters. */
30879 break;
30880 }
30881 type_name = cp_parser_objc_typename (parser);
30882 /* New ObjC allows attributes on parameters too. */
30883 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30884 parm_attr = cp_parser_attributes_opt (parser);
30885 identifier = cp_parser_identifier (parser);
30886
30887 params
30888 = chainon (params,
30889 objc_build_keyword_decl (selector,
30890 type_name,
30891 identifier,
30892 parm_attr));
30893
30894 token = cp_lexer_peek_token (parser->lexer);
30895 }
30896
30897 if (params == NULL_TREE)
30898 {
30899 cp_parser_error (parser, "objective-c++ method declaration is expected");
30900 return error_mark_node;
30901 }
30902
30903 /* We allow tail attributes for the method. */
30904 if (token->keyword == RID_ATTRIBUTE)
30905 {
30906 *attributes = cp_parser_attributes_opt (parser);
30907 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30908 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30909 return params;
30910 cp_parser_error (parser,
30911 "method attributes must be specified at the end");
30912 return error_mark_node;
30913 }
30914
30915 if (params == NULL_TREE)
30916 {
30917 cp_parser_error (parser, "objective-c++ method declaration is expected");
30918 return error_mark_node;
30919 }
30920 return params;
30921 }
30922
30923 /* Parse the non-keyword Objective-C params. */
30924
30925 static tree
30926 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30927 tree* attributes)
30928 {
30929 tree params = make_node (TREE_LIST);
30930 cp_token *token = cp_lexer_peek_token (parser->lexer);
30931 *ellipsisp = false; /* Initially, assume no ellipsis. */
30932
30933 while (token->type == CPP_COMMA)
30934 {
30935 cp_parameter_declarator *parmdecl;
30936 tree parm;
30937
30938 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30939 token = cp_lexer_peek_token (parser->lexer);
30940
30941 if (token->type == CPP_ELLIPSIS)
30942 {
30943 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30944 *ellipsisp = true;
30945 token = cp_lexer_peek_token (parser->lexer);
30946 break;
30947 }
30948
30949 /* TODO: parse attributes for tail parameters. */
30950 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
30951 false, NULL);
30952 parm = grokdeclarator (parmdecl->declarator,
30953 &parmdecl->decl_specifiers,
30954 PARM, /*initialized=*/0,
30955 /*attrlist=*/NULL);
30956
30957 chainon (params, build_tree_list (NULL_TREE, parm));
30958 token = cp_lexer_peek_token (parser->lexer);
30959 }
30960
30961 /* We allow tail attributes for the method. */
30962 if (token->keyword == RID_ATTRIBUTE)
30963 {
30964 if (*attributes == NULL_TREE)
30965 {
30966 *attributes = cp_parser_attributes_opt (parser);
30967 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30968 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30969 return params;
30970 }
30971 else
30972 /* We have an error, but parse the attributes, so that we can
30973 carry on. */
30974 *attributes = cp_parser_attributes_opt (parser);
30975
30976 cp_parser_error (parser,
30977 "method attributes must be specified at the end");
30978 return error_mark_node;
30979 }
30980
30981 return params;
30982 }
30983
30984 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30985
30986 static void
30987 cp_parser_objc_interstitial_code (cp_parser* parser)
30988 {
30989 cp_token *token = cp_lexer_peek_token (parser->lexer);
30990
30991 /* If the next token is `extern' and the following token is a string
30992 literal, then we have a linkage specification. */
30993 if (token->keyword == RID_EXTERN
30994 && cp_parser_is_pure_string_literal
30995 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30996 cp_parser_linkage_specification (parser);
30997 /* Handle #pragma, if any. */
30998 else if (token->type == CPP_PRAGMA)
30999 cp_parser_pragma (parser, pragma_objc_icode, NULL);
31000 /* Allow stray semicolons. */
31001 else if (token->type == CPP_SEMICOLON)
31002 cp_lexer_consume_token (parser->lexer);
31003 /* Mark methods as optional or required, when building protocols. */
31004 else if (token->keyword == RID_AT_OPTIONAL)
31005 {
31006 cp_lexer_consume_token (parser->lexer);
31007 objc_set_method_opt (true);
31008 }
31009 else if (token->keyword == RID_AT_REQUIRED)
31010 {
31011 cp_lexer_consume_token (parser->lexer);
31012 objc_set_method_opt (false);
31013 }
31014 else if (token->keyword == RID_NAMESPACE)
31015 cp_parser_namespace_definition (parser);
31016 /* Other stray characters must generate errors. */
31017 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
31018 {
31019 cp_lexer_consume_token (parser->lexer);
31020 error ("stray %qs between Objective-C++ methods",
31021 token->type == CPP_OPEN_BRACE ? "{" : "}");
31022 }
31023 /* Finally, try to parse a block-declaration, or a function-definition. */
31024 else
31025 cp_parser_block_declaration (parser, /*statement_p=*/false);
31026 }
31027
31028 /* Parse a method signature. */
31029
31030 static tree
31031 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
31032 {
31033 tree rettype, kwdparms, optparms;
31034 bool ellipsis = false;
31035 bool is_class_method;
31036
31037 is_class_method = cp_parser_objc_method_type (parser);
31038 rettype = cp_parser_objc_typename (parser);
31039 *attributes = NULL_TREE;
31040 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
31041 if (kwdparms == error_mark_node)
31042 return error_mark_node;
31043 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
31044 if (optparms == error_mark_node)
31045 return error_mark_node;
31046
31047 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
31048 }
31049
31050 static bool
31051 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
31052 {
31053 tree tattr;
31054 cp_lexer_save_tokens (parser->lexer);
31055 tattr = cp_parser_attributes_opt (parser);
31056 gcc_assert (tattr) ;
31057
31058 /* If the attributes are followed by a method introducer, this is not allowed.
31059 Dump the attributes and flag the situation. */
31060 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
31061 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31062 return true;
31063
31064 /* Otherwise, the attributes introduce some interstitial code, possibly so
31065 rewind to allow that check. */
31066 cp_lexer_rollback_tokens (parser->lexer);
31067 return false;
31068 }
31069
31070 /* Parse an Objective-C method prototype list. */
31071
31072 static void
31073 cp_parser_objc_method_prototype_list (cp_parser* parser)
31074 {
31075 cp_token *token = cp_lexer_peek_token (parser->lexer);
31076
31077 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31078 {
31079 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31080 {
31081 tree attributes, sig;
31082 bool is_class_method;
31083 if (token->type == CPP_PLUS)
31084 is_class_method = true;
31085 else
31086 is_class_method = false;
31087 sig = cp_parser_objc_method_signature (parser, &attributes);
31088 if (sig == error_mark_node)
31089 {
31090 cp_parser_skip_to_end_of_block_or_statement (parser);
31091 token = cp_lexer_peek_token (parser->lexer);
31092 continue;
31093 }
31094 objc_add_method_declaration (is_class_method, sig, attributes);
31095 cp_parser_consume_semicolon_at_end_of_statement (parser);
31096 }
31097 else if (token->keyword == RID_AT_PROPERTY)
31098 cp_parser_objc_at_property_declaration (parser);
31099 else if (token->keyword == RID_ATTRIBUTE
31100 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31101 warning_at (cp_lexer_peek_token (parser->lexer)->location,
31102 OPT_Wattributes,
31103 "prefix attributes are ignored for methods");
31104 else
31105 /* Allow for interspersed non-ObjC++ code. */
31106 cp_parser_objc_interstitial_code (parser);
31107
31108 token = cp_lexer_peek_token (parser->lexer);
31109 }
31110
31111 if (token->type != CPP_EOF)
31112 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31113 else
31114 cp_parser_error (parser, "expected %<@end%>");
31115
31116 objc_finish_interface ();
31117 }
31118
31119 /* Parse an Objective-C method definition list. */
31120
31121 static void
31122 cp_parser_objc_method_definition_list (cp_parser* parser)
31123 {
31124 cp_token *token = cp_lexer_peek_token (parser->lexer);
31125
31126 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31127 {
31128 tree meth;
31129
31130 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31131 {
31132 cp_token *ptk;
31133 tree sig, attribute;
31134 bool is_class_method;
31135 if (token->type == CPP_PLUS)
31136 is_class_method = true;
31137 else
31138 is_class_method = false;
31139 push_deferring_access_checks (dk_deferred);
31140 sig = cp_parser_objc_method_signature (parser, &attribute);
31141 if (sig == error_mark_node)
31142 {
31143 cp_parser_skip_to_end_of_block_or_statement (parser);
31144 token = cp_lexer_peek_token (parser->lexer);
31145 continue;
31146 }
31147 objc_start_method_definition (is_class_method, sig, attribute,
31148 NULL_TREE);
31149
31150 /* For historical reasons, we accept an optional semicolon. */
31151 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31152 cp_lexer_consume_token (parser->lexer);
31153
31154 ptk = cp_lexer_peek_token (parser->lexer);
31155 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
31156 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
31157 {
31158 perform_deferred_access_checks (tf_warning_or_error);
31159 stop_deferring_access_checks ();
31160 meth = cp_parser_function_definition_after_declarator (parser,
31161 false);
31162 pop_deferring_access_checks ();
31163 objc_finish_method_definition (meth);
31164 }
31165 }
31166 /* The following case will be removed once @synthesize is
31167 completely implemented. */
31168 else if (token->keyword == RID_AT_PROPERTY)
31169 cp_parser_objc_at_property_declaration (parser);
31170 else if (token->keyword == RID_AT_SYNTHESIZE)
31171 cp_parser_objc_at_synthesize_declaration (parser);
31172 else if (token->keyword == RID_AT_DYNAMIC)
31173 cp_parser_objc_at_dynamic_declaration (parser);
31174 else if (token->keyword == RID_ATTRIBUTE
31175 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31176 warning_at (token->location, OPT_Wattributes,
31177 "prefix attributes are ignored for methods");
31178 else
31179 /* Allow for interspersed non-ObjC++ code. */
31180 cp_parser_objc_interstitial_code (parser);
31181
31182 token = cp_lexer_peek_token (parser->lexer);
31183 }
31184
31185 if (token->type != CPP_EOF)
31186 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31187 else
31188 cp_parser_error (parser, "expected %<@end%>");
31189
31190 objc_finish_implementation ();
31191 }
31192
31193 /* Parse Objective-C ivars. */
31194
31195 static void
31196 cp_parser_objc_class_ivars (cp_parser* parser)
31197 {
31198 cp_token *token = cp_lexer_peek_token (parser->lexer);
31199
31200 if (token->type != CPP_OPEN_BRACE)
31201 return; /* No ivars specified. */
31202
31203 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
31204 token = cp_lexer_peek_token (parser->lexer);
31205
31206 while (token->type != CPP_CLOSE_BRACE
31207 && token->keyword != RID_AT_END && token->type != CPP_EOF)
31208 {
31209 cp_decl_specifier_seq declspecs;
31210 int decl_class_or_enum_p;
31211 tree prefix_attributes;
31212
31213 cp_parser_objc_visibility_spec (parser);
31214
31215 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31216 break;
31217
31218 cp_parser_decl_specifier_seq (parser,
31219 CP_PARSER_FLAGS_OPTIONAL,
31220 &declspecs,
31221 &decl_class_or_enum_p);
31222
31223 /* auto, register, static, extern, mutable. */
31224 if (declspecs.storage_class != sc_none)
31225 {
31226 cp_parser_error (parser, "invalid type for instance variable");
31227 declspecs.storage_class = sc_none;
31228 }
31229
31230 /* thread_local. */
31231 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31232 {
31233 cp_parser_error (parser, "invalid type for instance variable");
31234 declspecs.locations[ds_thread] = 0;
31235 }
31236
31237 /* typedef. */
31238 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31239 {
31240 cp_parser_error (parser, "invalid type for instance variable");
31241 declspecs.locations[ds_typedef] = 0;
31242 }
31243
31244 prefix_attributes = declspecs.attributes;
31245 declspecs.attributes = NULL_TREE;
31246
31247 /* Keep going until we hit the `;' at the end of the
31248 declaration. */
31249 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31250 {
31251 tree width = NULL_TREE, attributes, first_attribute, decl;
31252 cp_declarator *declarator = NULL;
31253 int ctor_dtor_or_conv_p;
31254
31255 /* Check for a (possibly unnamed) bitfield declaration. */
31256 token = cp_lexer_peek_token (parser->lexer);
31257 if (token->type == CPP_COLON)
31258 goto eat_colon;
31259
31260 if (token->type == CPP_NAME
31261 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
31262 == CPP_COLON))
31263 {
31264 /* Get the name of the bitfield. */
31265 declarator = make_id_declarator (NULL_TREE,
31266 cp_parser_identifier (parser),
31267 sfk_none, token->location);
31268
31269 eat_colon:
31270 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31271 /* Get the width of the bitfield. */
31272 width
31273 = cp_parser_constant_expression (parser);
31274 }
31275 else
31276 {
31277 /* Parse the declarator. */
31278 declarator
31279 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31280 CP_PARSER_FLAGS_NONE,
31281 &ctor_dtor_or_conv_p,
31282 /*parenthesized_p=*/NULL,
31283 /*member_p=*/false,
31284 /*friend_p=*/false,
31285 /*static_p=*/false);
31286 }
31287
31288 /* Look for attributes that apply to the ivar. */
31289 attributes = cp_parser_attributes_opt (parser);
31290 /* Remember which attributes are prefix attributes and
31291 which are not. */
31292 first_attribute = attributes;
31293 /* Combine the attributes. */
31294 attributes = attr_chainon (prefix_attributes, attributes);
31295
31296 if (width)
31297 /* Create the bitfield declaration. */
31298 decl = grokbitfield (declarator, &declspecs,
31299 width, NULL_TREE, attributes);
31300 else
31301 decl = grokfield (declarator, &declspecs,
31302 NULL_TREE, /*init_const_expr_p=*/false,
31303 NULL_TREE, attributes);
31304
31305 /* Add the instance variable. */
31306 if (decl != error_mark_node && decl != NULL_TREE)
31307 objc_add_instance_variable (decl);
31308
31309 /* Reset PREFIX_ATTRIBUTES. */
31310 if (attributes != error_mark_node)
31311 {
31312 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31313 attributes = TREE_CHAIN (attributes);
31314 if (attributes)
31315 TREE_CHAIN (attributes) = NULL_TREE;
31316 }
31317
31318 token = cp_lexer_peek_token (parser->lexer);
31319
31320 if (token->type == CPP_COMMA)
31321 {
31322 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31323 continue;
31324 }
31325 break;
31326 }
31327
31328 cp_parser_consume_semicolon_at_end_of_statement (parser);
31329 token = cp_lexer_peek_token (parser->lexer);
31330 }
31331
31332 if (token->keyword == RID_AT_END)
31333 cp_parser_error (parser, "expected %<}%>");
31334
31335 /* Do not consume the RID_AT_END, so it will be read again as terminating
31336 the @interface of @implementation. */
31337 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
31338 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
31339
31340 /* For historical reasons, we accept an optional semicolon. */
31341 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31342 cp_lexer_consume_token (parser->lexer);
31343 }
31344
31345 /* Parse an Objective-C protocol declaration. */
31346
31347 static void
31348 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
31349 {
31350 tree proto, protorefs;
31351 cp_token *tok;
31352
31353 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
31354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31355 {
31356 tok = cp_lexer_peek_token (parser->lexer);
31357 error_at (tok->location, "identifier expected after %<@protocol%>");
31358 cp_parser_consume_semicolon_at_end_of_statement (parser);
31359 return;
31360 }
31361
31362 /* See if we have a forward declaration or a definition. */
31363 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
31364
31365 /* Try a forward declaration first. */
31366 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
31367 {
31368 while (true)
31369 {
31370 tree id;
31371
31372 id = cp_parser_identifier (parser);
31373 if (id == error_mark_node)
31374 break;
31375
31376 objc_declare_protocol (id, attributes);
31377
31378 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31379 cp_lexer_consume_token (parser->lexer);
31380 else
31381 break;
31382 }
31383 cp_parser_consume_semicolon_at_end_of_statement (parser);
31384 }
31385
31386 /* Ok, we got a full-fledged definition (or at least should). */
31387 else
31388 {
31389 proto = cp_parser_identifier (parser);
31390 protorefs = cp_parser_objc_protocol_refs_opt (parser);
31391 objc_start_protocol (proto, protorefs, attributes);
31392 cp_parser_objc_method_prototype_list (parser);
31393 }
31394 }
31395
31396 /* Parse an Objective-C superclass or category. */
31397
31398 static void
31399 cp_parser_objc_superclass_or_category (cp_parser *parser,
31400 bool iface_p,
31401 tree *super,
31402 tree *categ, bool *is_class_extension)
31403 {
31404 cp_token *next = cp_lexer_peek_token (parser->lexer);
31405
31406 *super = *categ = NULL_TREE;
31407 *is_class_extension = false;
31408 if (next->type == CPP_COLON)
31409 {
31410 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31411 *super = cp_parser_identifier (parser);
31412 }
31413 else if (next->type == CPP_OPEN_PAREN)
31414 {
31415 matching_parens parens;
31416 parens.consume_open (parser); /* Eat '('. */
31417
31418 /* If there is no category name, and this is an @interface, we
31419 have a class extension. */
31420 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31421 {
31422 *categ = NULL_TREE;
31423 *is_class_extension = true;
31424 }
31425 else
31426 *categ = cp_parser_identifier (parser);
31427
31428 parens.require_close (parser);
31429 }
31430 }
31431
31432 /* Parse an Objective-C class interface. */
31433
31434 static void
31435 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
31436 {
31437 tree name, super, categ, protos;
31438 bool is_class_extension;
31439
31440 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
31441 name = cp_parser_identifier (parser);
31442 if (name == error_mark_node)
31443 {
31444 /* It's hard to recover because even if valid @interface stuff
31445 is to follow, we can't compile it (or validate it) if we
31446 don't even know which class it refers to. Let's assume this
31447 was a stray '@interface' token in the stream and skip it.
31448 */
31449 return;
31450 }
31451 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
31452 &is_class_extension);
31453 protos = cp_parser_objc_protocol_refs_opt (parser);
31454
31455 /* We have either a class or a category on our hands. */
31456 if (categ || is_class_extension)
31457 objc_start_category_interface (name, categ, protos, attributes);
31458 else
31459 {
31460 objc_start_class_interface (name, super, protos, attributes);
31461 /* Handle instance variable declarations, if any. */
31462 cp_parser_objc_class_ivars (parser);
31463 objc_continue_interface ();
31464 }
31465
31466 cp_parser_objc_method_prototype_list (parser);
31467 }
31468
31469 /* Parse an Objective-C class implementation. */
31470
31471 static void
31472 cp_parser_objc_class_implementation (cp_parser* parser)
31473 {
31474 tree name, super, categ;
31475 bool is_class_extension;
31476
31477 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
31478 name = cp_parser_identifier (parser);
31479 if (name == error_mark_node)
31480 {
31481 /* It's hard to recover because even if valid @implementation
31482 stuff is to follow, we can't compile it (or validate it) if
31483 we don't even know which class it refers to. Let's assume
31484 this was a stray '@implementation' token in the stream and
31485 skip it.
31486 */
31487 return;
31488 }
31489 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
31490 &is_class_extension);
31491
31492 /* We have either a class or a category on our hands. */
31493 if (categ)
31494 objc_start_category_implementation (name, categ);
31495 else
31496 {
31497 objc_start_class_implementation (name, super);
31498 /* Handle instance variable declarations, if any. */
31499 cp_parser_objc_class_ivars (parser);
31500 objc_continue_implementation ();
31501 }
31502
31503 cp_parser_objc_method_definition_list (parser);
31504 }
31505
31506 /* Consume the @end token and finish off the implementation. */
31507
31508 static void
31509 cp_parser_objc_end_implementation (cp_parser* parser)
31510 {
31511 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31512 objc_finish_implementation ();
31513 }
31514
31515 /* Parse an Objective-C declaration. */
31516
31517 static void
31518 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
31519 {
31520 /* Try to figure out what kind of declaration is present. */
31521 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31522
31523 if (attributes)
31524 switch (kwd->keyword)
31525 {
31526 case RID_AT_ALIAS:
31527 case RID_AT_CLASS:
31528 case RID_AT_END:
31529 error_at (kwd->location, "attributes may not be specified before"
31530 " the %<@%D%> Objective-C++ keyword",
31531 kwd->u.value);
31532 attributes = NULL;
31533 break;
31534 case RID_AT_IMPLEMENTATION:
31535 warning_at (kwd->location, OPT_Wattributes,
31536 "prefix attributes are ignored before %<@%D%>",
31537 kwd->u.value);
31538 attributes = NULL;
31539 default:
31540 break;
31541 }
31542
31543 switch (kwd->keyword)
31544 {
31545 case RID_AT_ALIAS:
31546 cp_parser_objc_alias_declaration (parser);
31547 break;
31548 case RID_AT_CLASS:
31549 cp_parser_objc_class_declaration (parser);
31550 break;
31551 case RID_AT_PROTOCOL:
31552 cp_parser_objc_protocol_declaration (parser, attributes);
31553 break;
31554 case RID_AT_INTERFACE:
31555 cp_parser_objc_class_interface (parser, attributes);
31556 break;
31557 case RID_AT_IMPLEMENTATION:
31558 cp_parser_objc_class_implementation (parser);
31559 break;
31560 case RID_AT_END:
31561 cp_parser_objc_end_implementation (parser);
31562 break;
31563 default:
31564 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31565 kwd->u.value);
31566 cp_parser_skip_to_end_of_block_or_statement (parser);
31567 }
31568 }
31569
31570 /* Parse an Objective-C try-catch-finally statement.
31571
31572 objc-try-catch-finally-stmt:
31573 @try compound-statement objc-catch-clause-seq [opt]
31574 objc-finally-clause [opt]
31575
31576 objc-catch-clause-seq:
31577 objc-catch-clause objc-catch-clause-seq [opt]
31578
31579 objc-catch-clause:
31580 @catch ( objc-exception-declaration ) compound-statement
31581
31582 objc-finally-clause:
31583 @finally compound-statement
31584
31585 objc-exception-declaration:
31586 parameter-declaration
31587 '...'
31588
31589 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31590
31591 Returns NULL_TREE.
31592
31593 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31594 for C. Keep them in sync. */
31595
31596 static tree
31597 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
31598 {
31599 location_t location;
31600 tree stmt;
31601
31602 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
31603 location = cp_lexer_peek_token (parser->lexer)->location;
31604 objc_maybe_warn_exceptions (location);
31605 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31606 node, lest it get absorbed into the surrounding block. */
31607 stmt = push_stmt_list ();
31608 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31609 objc_begin_try_stmt (location, pop_stmt_list (stmt));
31610
31611 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
31612 {
31613 cp_parameter_declarator *parm;
31614 tree parameter_declaration = error_mark_node;
31615 bool seen_open_paren = false;
31616 matching_parens parens;
31617
31618 cp_lexer_consume_token (parser->lexer);
31619 if (parens.require_open (parser))
31620 seen_open_paren = true;
31621 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31622 {
31623 /* We have "@catch (...)" (where the '...' are literally
31624 what is in the code). Skip the '...'.
31625 parameter_declaration is set to NULL_TREE, and
31626 objc_being_catch_clauses() knows that that means
31627 '...'. */
31628 cp_lexer_consume_token (parser->lexer);
31629 parameter_declaration = NULL_TREE;
31630 }
31631 else
31632 {
31633 /* We have "@catch (NSException *exception)" or something
31634 like that. Parse the parameter declaration. */
31635 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31636 false, NULL);
31637 if (parm == NULL)
31638 parameter_declaration = error_mark_node;
31639 else
31640 parameter_declaration = grokdeclarator (parm->declarator,
31641 &parm->decl_specifiers,
31642 PARM, /*initialized=*/0,
31643 /*attrlist=*/NULL);
31644 }
31645 if (seen_open_paren)
31646 parens.require_close (parser);
31647 else
31648 {
31649 /* If there was no open parenthesis, we are recovering from
31650 an error, and we are trying to figure out what mistake
31651 the user has made. */
31652
31653 /* If there is an immediate closing parenthesis, the user
31654 probably forgot the opening one (ie, they typed "@catch
31655 NSException *e)". Parse the closing parenthesis and keep
31656 going. */
31657 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31658 cp_lexer_consume_token (parser->lexer);
31659
31660 /* If these is no immediate closing parenthesis, the user
31661 probably doesn't know that parenthesis are required at
31662 all (ie, they typed "@catch NSException *e"). So, just
31663 forget about the closing parenthesis and keep going. */
31664 }
31665 objc_begin_catch_clause (parameter_declaration);
31666 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31667 objc_finish_catch_clause ();
31668 }
31669 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31670 {
31671 cp_lexer_consume_token (parser->lexer);
31672 location = cp_lexer_peek_token (parser->lexer)->location;
31673 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31674 node, lest it get absorbed into the surrounding block. */
31675 stmt = push_stmt_list ();
31676 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31677 objc_build_finally_clause (location, pop_stmt_list (stmt));
31678 }
31679
31680 return objc_finish_try_stmt ();
31681 }
31682
31683 /* Parse an Objective-C synchronized statement.
31684
31685 objc-synchronized-stmt:
31686 @synchronized ( expression ) compound-statement
31687
31688 Returns NULL_TREE. */
31689
31690 static tree
31691 cp_parser_objc_synchronized_statement (cp_parser *parser)
31692 {
31693 location_t location;
31694 tree lock, stmt;
31695
31696 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31697
31698 location = cp_lexer_peek_token (parser->lexer)->location;
31699 objc_maybe_warn_exceptions (location);
31700 matching_parens parens;
31701 parens.require_open (parser);
31702 lock = cp_parser_expression (parser);
31703 parens.require_close (parser);
31704
31705 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31706 node, lest it get absorbed into the surrounding block. */
31707 stmt = push_stmt_list ();
31708 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31709
31710 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31711 }
31712
31713 /* Parse an Objective-C throw statement.
31714
31715 objc-throw-stmt:
31716 @throw assignment-expression [opt] ;
31717
31718 Returns a constructed '@throw' statement. */
31719
31720 static tree
31721 cp_parser_objc_throw_statement (cp_parser *parser)
31722 {
31723 tree expr = NULL_TREE;
31724 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31725
31726 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31727
31728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31729 expr = cp_parser_expression (parser);
31730
31731 cp_parser_consume_semicolon_at_end_of_statement (parser);
31732
31733 return objc_build_throw_stmt (loc, expr);
31734 }
31735
31736 /* Parse an Objective-C statement. */
31737
31738 static tree
31739 cp_parser_objc_statement (cp_parser * parser)
31740 {
31741 /* Try to figure out what kind of declaration is present. */
31742 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31743
31744 switch (kwd->keyword)
31745 {
31746 case RID_AT_TRY:
31747 return cp_parser_objc_try_catch_finally_statement (parser);
31748 case RID_AT_SYNCHRONIZED:
31749 return cp_parser_objc_synchronized_statement (parser);
31750 case RID_AT_THROW:
31751 return cp_parser_objc_throw_statement (parser);
31752 default:
31753 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31754 kwd->u.value);
31755 cp_parser_skip_to_end_of_block_or_statement (parser);
31756 }
31757
31758 return error_mark_node;
31759 }
31760
31761 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31762 look ahead to see if an objc keyword follows the attributes. This
31763 is to detect the use of prefix attributes on ObjC @interface and
31764 @protocol. */
31765
31766 static bool
31767 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31768 {
31769 cp_lexer_save_tokens (parser->lexer);
31770 *attrib = cp_parser_attributes_opt (parser);
31771 gcc_assert (*attrib);
31772 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31773 {
31774 cp_lexer_commit_tokens (parser->lexer);
31775 return true;
31776 }
31777 cp_lexer_rollback_tokens (parser->lexer);
31778 return false;
31779 }
31780
31781 /* This routine is a minimal replacement for
31782 c_parser_struct_declaration () used when parsing the list of
31783 types/names or ObjC++ properties. For example, when parsing the
31784 code
31785
31786 @property (readonly) int a, b, c;
31787
31788 this function is responsible for parsing "int a, int b, int c" and
31789 returning the declarations as CHAIN of DECLs.
31790
31791 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31792 similar parsing. */
31793 static tree
31794 cp_parser_objc_struct_declaration (cp_parser *parser)
31795 {
31796 tree decls = NULL_TREE;
31797 cp_decl_specifier_seq declspecs;
31798 int decl_class_or_enum_p;
31799 tree prefix_attributes;
31800
31801 cp_parser_decl_specifier_seq (parser,
31802 CP_PARSER_FLAGS_NONE,
31803 &declspecs,
31804 &decl_class_or_enum_p);
31805
31806 if (declspecs.type == error_mark_node)
31807 return error_mark_node;
31808
31809 /* auto, register, static, extern, mutable. */
31810 if (declspecs.storage_class != sc_none)
31811 {
31812 cp_parser_error (parser, "invalid type for property");
31813 declspecs.storage_class = sc_none;
31814 }
31815
31816 /* thread_local. */
31817 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31818 {
31819 cp_parser_error (parser, "invalid type for property");
31820 declspecs.locations[ds_thread] = 0;
31821 }
31822
31823 /* typedef. */
31824 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31825 {
31826 cp_parser_error (parser, "invalid type for property");
31827 declspecs.locations[ds_typedef] = 0;
31828 }
31829
31830 prefix_attributes = declspecs.attributes;
31831 declspecs.attributes = NULL_TREE;
31832
31833 /* Keep going until we hit the `;' at the end of the declaration. */
31834 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31835 {
31836 tree attributes, first_attribute, decl;
31837 cp_declarator *declarator;
31838 cp_token *token;
31839
31840 /* Parse the declarator. */
31841 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31842 CP_PARSER_FLAGS_NONE,
31843 NULL, NULL, false, false, false);
31844
31845 /* Look for attributes that apply to the ivar. */
31846 attributes = cp_parser_attributes_opt (parser);
31847 /* Remember which attributes are prefix attributes and
31848 which are not. */
31849 first_attribute = attributes;
31850 /* Combine the attributes. */
31851 attributes = attr_chainon (prefix_attributes, attributes);
31852
31853 decl = grokfield (declarator, &declspecs,
31854 NULL_TREE, /*init_const_expr_p=*/false,
31855 NULL_TREE, attributes);
31856
31857 if (decl == error_mark_node || decl == NULL_TREE)
31858 return error_mark_node;
31859
31860 /* Reset PREFIX_ATTRIBUTES. */
31861 if (attributes != error_mark_node)
31862 {
31863 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31864 attributes = TREE_CHAIN (attributes);
31865 if (attributes)
31866 TREE_CHAIN (attributes) = NULL_TREE;
31867 }
31868
31869 DECL_CHAIN (decl) = decls;
31870 decls = decl;
31871
31872 token = cp_lexer_peek_token (parser->lexer);
31873 if (token->type == CPP_COMMA)
31874 {
31875 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31876 continue;
31877 }
31878 else
31879 break;
31880 }
31881 return decls;
31882 }
31883
31884 /* Parse an Objective-C @property declaration. The syntax is:
31885
31886 objc-property-declaration:
31887 '@property' objc-property-attributes[opt] struct-declaration ;
31888
31889 objc-property-attributes:
31890 '(' objc-property-attribute-list ')'
31891
31892 objc-property-attribute-list:
31893 objc-property-attribute
31894 objc-property-attribute-list, objc-property-attribute
31895
31896 objc-property-attribute
31897 'getter' = identifier
31898 'setter' = identifier
31899 'readonly'
31900 'readwrite'
31901 'assign'
31902 'retain'
31903 'copy'
31904 'nonatomic'
31905
31906 For example:
31907 @property NSString *name;
31908 @property (readonly) id object;
31909 @property (retain, nonatomic, getter=getTheName) id name;
31910 @property int a, b, c;
31911
31912 PS: This function is identical to
31913 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31914 static void
31915 cp_parser_objc_at_property_declaration (cp_parser *parser)
31916 {
31917 /* The following variables hold the attributes of the properties as
31918 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31919 seen. When we see an attribute, we set them to 'true' (if they
31920 are boolean properties) or to the identifier (if they have an
31921 argument, ie, for getter and setter). Note that here we only
31922 parse the list of attributes, check the syntax and accumulate the
31923 attributes that we find. objc_add_property_declaration() will
31924 then process the information. */
31925 bool property_assign = false;
31926 bool property_copy = false;
31927 tree property_getter_ident = NULL_TREE;
31928 bool property_nonatomic = false;
31929 bool property_readonly = false;
31930 bool property_readwrite = false;
31931 bool property_retain = false;
31932 tree property_setter_ident = NULL_TREE;
31933
31934 /* 'properties' is the list of properties that we read. Usually a
31935 single one, but maybe more (eg, in "@property int a, b, c;" there
31936 are three). */
31937 tree properties;
31938 location_t loc;
31939
31940 loc = cp_lexer_peek_token (parser->lexer)->location;
31941
31942 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31943
31944 /* Parse the optional attribute list... */
31945 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31946 {
31947 /* Eat the '('. */
31948 matching_parens parens;
31949 parens.consume_open (parser);
31950
31951 while (true)
31952 {
31953 bool syntax_error = false;
31954 cp_token *token = cp_lexer_peek_token (parser->lexer);
31955 enum rid keyword;
31956
31957 if (token->type != CPP_NAME)
31958 {
31959 cp_parser_error (parser, "expected identifier");
31960 break;
31961 }
31962 keyword = C_RID_CODE (token->u.value);
31963 cp_lexer_consume_token (parser->lexer);
31964 switch (keyword)
31965 {
31966 case RID_ASSIGN: property_assign = true; break;
31967 case RID_COPY: property_copy = true; break;
31968 case RID_NONATOMIC: property_nonatomic = true; break;
31969 case RID_READONLY: property_readonly = true; break;
31970 case RID_READWRITE: property_readwrite = true; break;
31971 case RID_RETAIN: property_retain = true; break;
31972
31973 case RID_GETTER:
31974 case RID_SETTER:
31975 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31976 {
31977 if (keyword == RID_GETTER)
31978 cp_parser_error (parser,
31979 "missing %<=%> (after %<getter%> attribute)");
31980 else
31981 cp_parser_error (parser,
31982 "missing %<=%> (after %<setter%> attribute)");
31983 syntax_error = true;
31984 break;
31985 }
31986 cp_lexer_consume_token (parser->lexer); /* eat the = */
31987 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31988 {
31989 cp_parser_error (parser, "expected identifier");
31990 syntax_error = true;
31991 break;
31992 }
31993 if (keyword == RID_SETTER)
31994 {
31995 if (property_setter_ident != NULL_TREE)
31996 {
31997 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31998 cp_lexer_consume_token (parser->lexer);
31999 }
32000 else
32001 property_setter_ident = cp_parser_objc_selector (parser);
32002 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
32003 cp_parser_error (parser, "setter name must terminate with %<:%>");
32004 else
32005 cp_lexer_consume_token (parser->lexer);
32006 }
32007 else
32008 {
32009 if (property_getter_ident != NULL_TREE)
32010 {
32011 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
32012 cp_lexer_consume_token (parser->lexer);
32013 }
32014 else
32015 property_getter_ident = cp_parser_objc_selector (parser);
32016 }
32017 break;
32018 default:
32019 cp_parser_error (parser, "unknown property attribute");
32020 syntax_error = true;
32021 break;
32022 }
32023
32024 if (syntax_error)
32025 break;
32026
32027 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32028 cp_lexer_consume_token (parser->lexer);
32029 else
32030 break;
32031 }
32032
32033 /* FIXME: "@property (setter, assign);" will generate a spurious
32034 "error: expected ‘)’ before ‘,’ token". This is because
32035 cp_parser_require, unlike the C counterpart, will produce an
32036 error even if we are in error recovery. */
32037 if (!parens.require_close (parser))
32038 {
32039 cp_parser_skip_to_closing_parenthesis (parser,
32040 /*recovering=*/true,
32041 /*or_comma=*/false,
32042 /*consume_paren=*/true);
32043 }
32044 }
32045
32046 /* ... and the property declaration(s). */
32047 properties = cp_parser_objc_struct_declaration (parser);
32048
32049 if (properties == error_mark_node)
32050 {
32051 cp_parser_skip_to_end_of_statement (parser);
32052 /* If the next token is now a `;', consume it. */
32053 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32054 cp_lexer_consume_token (parser->lexer);
32055 return;
32056 }
32057
32058 if (properties == NULL_TREE)
32059 cp_parser_error (parser, "expected identifier");
32060 else
32061 {
32062 /* Comma-separated properties are chained together in
32063 reverse order; add them one by one. */
32064 properties = nreverse (properties);
32065
32066 for (; properties; properties = TREE_CHAIN (properties))
32067 objc_add_property_declaration (loc, copy_node (properties),
32068 property_readonly, property_readwrite,
32069 property_assign, property_retain,
32070 property_copy, property_nonatomic,
32071 property_getter_ident, property_setter_ident);
32072 }
32073
32074 cp_parser_consume_semicolon_at_end_of_statement (parser);
32075 }
32076
32077 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
32078
32079 objc-synthesize-declaration:
32080 @synthesize objc-synthesize-identifier-list ;
32081
32082 objc-synthesize-identifier-list:
32083 objc-synthesize-identifier
32084 objc-synthesize-identifier-list, objc-synthesize-identifier
32085
32086 objc-synthesize-identifier
32087 identifier
32088 identifier = identifier
32089
32090 For example:
32091 @synthesize MyProperty;
32092 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
32093
32094 PS: This function is identical to c_parser_objc_at_synthesize_declaration
32095 for C. Keep them in sync.
32096 */
32097 static void
32098 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
32099 {
32100 tree list = NULL_TREE;
32101 location_t loc;
32102 loc = cp_lexer_peek_token (parser->lexer)->location;
32103
32104 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
32105 while (true)
32106 {
32107 tree property, ivar;
32108 property = cp_parser_identifier (parser);
32109 if (property == error_mark_node)
32110 {
32111 cp_parser_consume_semicolon_at_end_of_statement (parser);
32112 return;
32113 }
32114 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
32115 {
32116 cp_lexer_consume_token (parser->lexer);
32117 ivar = cp_parser_identifier (parser);
32118 if (ivar == error_mark_node)
32119 {
32120 cp_parser_consume_semicolon_at_end_of_statement (parser);
32121 return;
32122 }
32123 }
32124 else
32125 ivar = NULL_TREE;
32126 list = chainon (list, build_tree_list (ivar, property));
32127 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32128 cp_lexer_consume_token (parser->lexer);
32129 else
32130 break;
32131 }
32132 cp_parser_consume_semicolon_at_end_of_statement (parser);
32133 objc_add_synthesize_declaration (loc, list);
32134 }
32135
32136 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
32137
32138 objc-dynamic-declaration:
32139 @dynamic identifier-list ;
32140
32141 For example:
32142 @dynamic MyProperty;
32143 @dynamic MyProperty, AnotherProperty;
32144
32145 PS: This function is identical to c_parser_objc_at_dynamic_declaration
32146 for C. Keep them in sync.
32147 */
32148 static void
32149 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
32150 {
32151 tree list = NULL_TREE;
32152 location_t loc;
32153 loc = cp_lexer_peek_token (parser->lexer)->location;
32154
32155 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
32156 while (true)
32157 {
32158 tree property;
32159 property = cp_parser_identifier (parser);
32160 if (property == error_mark_node)
32161 {
32162 cp_parser_consume_semicolon_at_end_of_statement (parser);
32163 return;
32164 }
32165 list = chainon (list, build_tree_list (NULL, property));
32166 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32167 cp_lexer_consume_token (parser->lexer);
32168 else
32169 break;
32170 }
32171 cp_parser_consume_semicolon_at_end_of_statement (parser);
32172 objc_add_dynamic_declaration (loc, list);
32173 }
32174
32175 \f
32176 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
32177
32178 /* Returns name of the next clause.
32179 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
32180 the token is not consumed. Otherwise appropriate pragma_omp_clause is
32181 returned and the token is consumed. */
32182
32183 static pragma_omp_clause
32184 cp_parser_omp_clause_name (cp_parser *parser)
32185 {
32186 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
32187
32188 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32189 result = PRAGMA_OACC_CLAUSE_AUTO;
32190 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
32191 result = PRAGMA_OMP_CLAUSE_IF;
32192 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32193 result = PRAGMA_OMP_CLAUSE_DEFAULT;
32194 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
32195 result = PRAGMA_OACC_CLAUSE_DELETE;
32196 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
32197 result = PRAGMA_OMP_CLAUSE_PRIVATE;
32198 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32199 result = PRAGMA_OMP_CLAUSE_FOR;
32200 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32201 {
32202 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32203 const char *p = IDENTIFIER_POINTER (id);
32204
32205 switch (p[0])
32206 {
32207 case 'a':
32208 if (!strcmp ("aligned", p))
32209 result = PRAGMA_OMP_CLAUSE_ALIGNED;
32210 else if (!strcmp ("async", p))
32211 result = PRAGMA_OACC_CLAUSE_ASYNC;
32212 break;
32213 case 'c':
32214 if (!strcmp ("collapse", p))
32215 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
32216 else if (!strcmp ("copy", p))
32217 result = PRAGMA_OACC_CLAUSE_COPY;
32218 else if (!strcmp ("copyin", p))
32219 result = PRAGMA_OMP_CLAUSE_COPYIN;
32220 else if (!strcmp ("copyout", p))
32221 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32222 else if (!strcmp ("copyprivate", p))
32223 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
32224 else if (!strcmp ("create", p))
32225 result = PRAGMA_OACC_CLAUSE_CREATE;
32226 break;
32227 case 'd':
32228 if (!strcmp ("defaultmap", p))
32229 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
32230 else if (!strcmp ("depend", p))
32231 result = PRAGMA_OMP_CLAUSE_DEPEND;
32232 else if (!strcmp ("device", p))
32233 result = PRAGMA_OMP_CLAUSE_DEVICE;
32234 else if (!strcmp ("deviceptr", p))
32235 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
32236 else if (!strcmp ("device_resident", p))
32237 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
32238 else if (!strcmp ("dist_schedule", p))
32239 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
32240 break;
32241 case 'f':
32242 if (!strcmp ("final", p))
32243 result = PRAGMA_OMP_CLAUSE_FINAL;
32244 else if (!strcmp ("finalize", p))
32245 result = PRAGMA_OACC_CLAUSE_FINALIZE;
32246 else if (!strcmp ("firstprivate", p))
32247 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
32248 else if (!strcmp ("from", p))
32249 result = PRAGMA_OMP_CLAUSE_FROM;
32250 break;
32251 case 'g':
32252 if (!strcmp ("gang", p))
32253 result = PRAGMA_OACC_CLAUSE_GANG;
32254 else if (!strcmp ("grainsize", p))
32255 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
32256 break;
32257 case 'h':
32258 if (!strcmp ("hint", p))
32259 result = PRAGMA_OMP_CLAUSE_HINT;
32260 else if (!strcmp ("host", p))
32261 result = PRAGMA_OACC_CLAUSE_HOST;
32262 break;
32263 case 'i':
32264 if (!strcmp ("if_present", p))
32265 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
32266 else if (!strcmp ("in_reduction", p))
32267 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
32268 else if (!strcmp ("inbranch", p))
32269 result = PRAGMA_OMP_CLAUSE_INBRANCH;
32270 else if (!strcmp ("independent", p))
32271 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
32272 else if (!strcmp ("is_device_ptr", p))
32273 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
32274 break;
32275 case 'l':
32276 if (!strcmp ("lastprivate", p))
32277 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
32278 else if (!strcmp ("linear", p))
32279 result = PRAGMA_OMP_CLAUSE_LINEAR;
32280 else if (!strcmp ("link", p))
32281 result = PRAGMA_OMP_CLAUSE_LINK;
32282 break;
32283 case 'm':
32284 if (!strcmp ("map", p))
32285 result = PRAGMA_OMP_CLAUSE_MAP;
32286 else if (!strcmp ("mergeable", p))
32287 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
32288 break;
32289 case 'n':
32290 if (!strcmp ("nogroup", p))
32291 result = PRAGMA_OMP_CLAUSE_NOGROUP;
32292 else if (!strcmp ("nontemporal", p))
32293 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
32294 else if (!strcmp ("notinbranch", p))
32295 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
32296 else if (!strcmp ("nowait", p))
32297 result = PRAGMA_OMP_CLAUSE_NOWAIT;
32298 else if (!strcmp ("num_gangs", p))
32299 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
32300 else if (!strcmp ("num_tasks", p))
32301 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
32302 else if (!strcmp ("num_teams", p))
32303 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
32304 else if (!strcmp ("num_threads", p))
32305 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
32306 else if (!strcmp ("num_workers", p))
32307 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
32308 break;
32309 case 'o':
32310 if (!strcmp ("ordered", p))
32311 result = PRAGMA_OMP_CLAUSE_ORDERED;
32312 break;
32313 case 'p':
32314 if (!strcmp ("parallel", p))
32315 result = PRAGMA_OMP_CLAUSE_PARALLEL;
32316 else if (!strcmp ("present", p))
32317 result = PRAGMA_OACC_CLAUSE_PRESENT;
32318 else if (!strcmp ("present_or_copy", p)
32319 || !strcmp ("pcopy", p))
32320 result = PRAGMA_OACC_CLAUSE_COPY;
32321 else if (!strcmp ("present_or_copyin", p)
32322 || !strcmp ("pcopyin", p))
32323 result = PRAGMA_OACC_CLAUSE_COPYIN;
32324 else if (!strcmp ("present_or_copyout", p)
32325 || !strcmp ("pcopyout", p))
32326 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32327 else if (!strcmp ("present_or_create", p)
32328 || !strcmp ("pcreate", p))
32329 result = PRAGMA_OACC_CLAUSE_CREATE;
32330 else if (!strcmp ("priority", p))
32331 result = PRAGMA_OMP_CLAUSE_PRIORITY;
32332 else if (!strcmp ("proc_bind", p))
32333 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
32334 break;
32335 case 'r':
32336 if (!strcmp ("reduction", p))
32337 result = PRAGMA_OMP_CLAUSE_REDUCTION;
32338 break;
32339 case 's':
32340 if (!strcmp ("safelen", p))
32341 result = PRAGMA_OMP_CLAUSE_SAFELEN;
32342 else if (!strcmp ("schedule", p))
32343 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
32344 else if (!strcmp ("sections", p))
32345 result = PRAGMA_OMP_CLAUSE_SECTIONS;
32346 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
32347 result = PRAGMA_OACC_CLAUSE_HOST;
32348 else if (!strcmp ("seq", p))
32349 result = PRAGMA_OACC_CLAUSE_SEQ;
32350 else if (!strcmp ("shared", p))
32351 result = PRAGMA_OMP_CLAUSE_SHARED;
32352 else if (!strcmp ("simd", p))
32353 result = PRAGMA_OMP_CLAUSE_SIMD;
32354 else if (!strcmp ("simdlen", p))
32355 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
32356 break;
32357 case 't':
32358 if (!strcmp ("task_reduction", p))
32359 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
32360 else if (!strcmp ("taskgroup", p))
32361 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
32362 else if (!strcmp ("thread_limit", p))
32363 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
32364 else if (!strcmp ("threads", p))
32365 result = PRAGMA_OMP_CLAUSE_THREADS;
32366 else if (!strcmp ("tile", p))
32367 result = PRAGMA_OACC_CLAUSE_TILE;
32368 else if (!strcmp ("to", p))
32369 result = PRAGMA_OMP_CLAUSE_TO;
32370 break;
32371 case 'u':
32372 if (!strcmp ("uniform", p))
32373 result = PRAGMA_OMP_CLAUSE_UNIFORM;
32374 else if (!strcmp ("untied", p))
32375 result = PRAGMA_OMP_CLAUSE_UNTIED;
32376 else if (!strcmp ("use_device", p))
32377 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
32378 else if (!strcmp ("use_device_ptr", p))
32379 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
32380 break;
32381 case 'v':
32382 if (!strcmp ("vector", p))
32383 result = PRAGMA_OACC_CLAUSE_VECTOR;
32384 else if (!strcmp ("vector_length", p))
32385 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
32386 break;
32387 case 'w':
32388 if (!strcmp ("wait", p))
32389 result = PRAGMA_OACC_CLAUSE_WAIT;
32390 else if (!strcmp ("worker", p))
32391 result = PRAGMA_OACC_CLAUSE_WORKER;
32392 break;
32393 }
32394 }
32395
32396 if (result != PRAGMA_OMP_CLAUSE_NONE)
32397 cp_lexer_consume_token (parser->lexer);
32398
32399 return result;
32400 }
32401
32402 /* Validate that a clause of the given type does not already exist. */
32403
32404 static void
32405 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
32406 const char *name, location_t location)
32407 {
32408 tree c;
32409
32410 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
32411 if (OMP_CLAUSE_CODE (c) == code)
32412 {
32413 error_at (location, "too many %qs clauses", name);
32414 break;
32415 }
32416 }
32417
32418 /* OpenMP 2.5:
32419 variable-list:
32420 identifier
32421 variable-list , identifier
32422
32423 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32424 colon). An opening parenthesis will have been consumed by the caller.
32425
32426 If KIND is nonzero, create the appropriate node and install the decl
32427 in OMP_CLAUSE_DECL and add the node to the head of the list.
32428
32429 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32430 return the list created.
32431
32432 COLON can be NULL if only closing parenthesis should end the list,
32433 or pointer to bool which will receive false if the list is terminated
32434 by closing parenthesis or true if the list is terminated by colon. */
32435
32436 static tree
32437 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
32438 tree list, bool *colon)
32439 {
32440 cp_token *token;
32441 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32442 if (colon)
32443 {
32444 parser->colon_corrects_to_scope_p = false;
32445 *colon = false;
32446 }
32447 while (1)
32448 {
32449 tree name, decl;
32450
32451 if (kind == OMP_CLAUSE_DEPEND)
32452 cp_parser_parse_tentatively (parser);
32453 token = cp_lexer_peek_token (parser->lexer);
32454 if (kind != 0
32455 && current_class_ptr
32456 && cp_parser_is_keyword (token, RID_THIS))
32457 {
32458 decl = finish_this_expr ();
32459 if (TREE_CODE (decl) == NON_LVALUE_EXPR
32460 || CONVERT_EXPR_P (decl))
32461 decl = TREE_OPERAND (decl, 0);
32462 cp_lexer_consume_token (parser->lexer);
32463 }
32464 else
32465 {
32466 name = cp_parser_id_expression (parser, /*template_p=*/false,
32467 /*check_dependency_p=*/true,
32468 /*template_p=*/NULL,
32469 /*declarator_p=*/false,
32470 /*optional_p=*/false);
32471 if (name == error_mark_node)
32472 {
32473 if (kind == OMP_CLAUSE_DEPEND
32474 && cp_parser_simulate_error (parser))
32475 goto depend_lvalue;
32476 goto skip_comma;
32477 }
32478
32479 if (identifier_p (name))
32480 decl = cp_parser_lookup_name_simple (parser, name, token->location);
32481 else
32482 decl = name;
32483 if (decl == error_mark_node)
32484 {
32485 if (kind == OMP_CLAUSE_DEPEND
32486 && cp_parser_simulate_error (parser))
32487 goto depend_lvalue;
32488 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
32489 token->location);
32490 }
32491 }
32492 if (decl == error_mark_node)
32493 ;
32494 else if (kind != 0)
32495 {
32496 switch (kind)
32497 {
32498 case OMP_CLAUSE__CACHE_:
32499 /* The OpenACC cache directive explicitly only allows "array
32500 elements or subarrays". */
32501 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
32502 {
32503 error_at (token->location, "expected %<[%>");
32504 decl = error_mark_node;
32505 break;
32506 }
32507 /* FALLTHROUGH. */
32508 case OMP_CLAUSE_MAP:
32509 case OMP_CLAUSE_FROM:
32510 case OMP_CLAUSE_TO:
32511 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
32512 {
32513 location_t loc
32514 = cp_lexer_peek_token (parser->lexer)->location;
32515 cp_id_kind idk = CP_ID_KIND_NONE;
32516 cp_lexer_consume_token (parser->lexer);
32517 decl = convert_from_reference (decl);
32518 decl
32519 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
32520 decl, false,
32521 &idk, loc);
32522 }
32523 /* FALLTHROUGH. */
32524 case OMP_CLAUSE_DEPEND:
32525 case OMP_CLAUSE_REDUCTION:
32526 case OMP_CLAUSE_IN_REDUCTION:
32527 case OMP_CLAUSE_TASK_REDUCTION:
32528 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
32529 {
32530 tree low_bound = NULL_TREE, length = NULL_TREE;
32531
32532 parser->colon_corrects_to_scope_p = false;
32533 cp_lexer_consume_token (parser->lexer);
32534 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32535 low_bound = cp_parser_expression (parser);
32536 if (!colon)
32537 parser->colon_corrects_to_scope_p
32538 = saved_colon_corrects_to_scope_p;
32539 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
32540 length = integer_one_node;
32541 else
32542 {
32543 /* Look for `:'. */
32544 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32545 {
32546 if (kind == OMP_CLAUSE_DEPEND
32547 && cp_parser_simulate_error (parser))
32548 goto depend_lvalue;
32549 goto skip_comma;
32550 }
32551 if (kind == OMP_CLAUSE_DEPEND)
32552 cp_parser_commit_to_tentative_parse (parser);
32553 if (!cp_lexer_next_token_is (parser->lexer,
32554 CPP_CLOSE_SQUARE))
32555 length = cp_parser_expression (parser);
32556 }
32557 /* Look for the closing `]'. */
32558 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
32559 RT_CLOSE_SQUARE))
32560 {
32561 if (kind == OMP_CLAUSE_DEPEND
32562 && cp_parser_simulate_error (parser))
32563 goto depend_lvalue;
32564 goto skip_comma;
32565 }
32566
32567 decl = tree_cons (low_bound, length, decl);
32568 }
32569 break;
32570 default:
32571 break;
32572 }
32573
32574 if (kind == OMP_CLAUSE_DEPEND)
32575 {
32576 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
32577 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32578 && cp_parser_simulate_error (parser))
32579 {
32580 depend_lvalue:
32581 cp_parser_abort_tentative_parse (parser);
32582 decl = cp_parser_assignment_expression (parser, NULL,
32583 false, false);
32584 }
32585 else
32586 cp_parser_parse_definitely (parser);
32587 }
32588
32589 tree u = build_omp_clause (token->location, kind);
32590 OMP_CLAUSE_DECL (u) = decl;
32591 OMP_CLAUSE_CHAIN (u) = list;
32592 list = u;
32593 }
32594 else
32595 list = tree_cons (decl, NULL_TREE, list);
32596
32597 get_comma:
32598 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32599 break;
32600 cp_lexer_consume_token (parser->lexer);
32601 }
32602
32603 if (colon)
32604 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32605
32606 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32607 {
32608 *colon = true;
32609 cp_parser_require (parser, CPP_COLON, RT_COLON);
32610 return list;
32611 }
32612
32613 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32614 {
32615 int ending;
32616
32617 /* Try to resync to an unnested comma. Copied from
32618 cp_parser_parenthesized_expression_list. */
32619 skip_comma:
32620 if (colon)
32621 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32622 ending = cp_parser_skip_to_closing_parenthesis (parser,
32623 /*recovering=*/true,
32624 /*or_comma=*/true,
32625 /*consume_paren=*/true);
32626 if (ending < 0)
32627 goto get_comma;
32628 }
32629
32630 return list;
32631 }
32632
32633 /* Similarly, but expect leading and trailing parenthesis. This is a very
32634 common case for omp clauses. */
32635
32636 static tree
32637 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
32638 {
32639 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32640 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
32641 return list;
32642 }
32643
32644 /* OpenACC 2.0:
32645 copy ( variable-list )
32646 copyin ( variable-list )
32647 copyout ( variable-list )
32648 create ( variable-list )
32649 delete ( variable-list )
32650 present ( variable-list ) */
32651
32652 static tree
32653 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
32654 tree list)
32655 {
32656 enum gomp_map_kind kind;
32657 switch (c_kind)
32658 {
32659 case PRAGMA_OACC_CLAUSE_COPY:
32660 kind = GOMP_MAP_TOFROM;
32661 break;
32662 case PRAGMA_OACC_CLAUSE_COPYIN:
32663 kind = GOMP_MAP_TO;
32664 break;
32665 case PRAGMA_OACC_CLAUSE_COPYOUT:
32666 kind = GOMP_MAP_FROM;
32667 break;
32668 case PRAGMA_OACC_CLAUSE_CREATE:
32669 kind = GOMP_MAP_ALLOC;
32670 break;
32671 case PRAGMA_OACC_CLAUSE_DELETE:
32672 kind = GOMP_MAP_RELEASE;
32673 break;
32674 case PRAGMA_OACC_CLAUSE_DEVICE:
32675 kind = GOMP_MAP_FORCE_TO;
32676 break;
32677 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32678 kind = GOMP_MAP_DEVICE_RESIDENT;
32679 break;
32680 case PRAGMA_OACC_CLAUSE_HOST:
32681 kind = GOMP_MAP_FORCE_FROM;
32682 break;
32683 case PRAGMA_OACC_CLAUSE_LINK:
32684 kind = GOMP_MAP_LINK;
32685 break;
32686 case PRAGMA_OACC_CLAUSE_PRESENT:
32687 kind = GOMP_MAP_FORCE_PRESENT;
32688 break;
32689 default:
32690 gcc_unreachable ();
32691 }
32692 tree nl, c;
32693 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32694
32695 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32696 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32697
32698 return nl;
32699 }
32700
32701 /* OpenACC 2.0:
32702 deviceptr ( variable-list ) */
32703
32704 static tree
32705 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32706 {
32707 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32708 tree vars, t;
32709
32710 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32711 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32712 variable-list must only allow for pointer variables. */
32713 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32714 for (t = vars; t; t = TREE_CHAIN (t))
32715 {
32716 tree v = TREE_PURPOSE (t);
32717 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32718 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32719 OMP_CLAUSE_DECL (u) = v;
32720 OMP_CLAUSE_CHAIN (u) = list;
32721 list = u;
32722 }
32723
32724 return list;
32725 }
32726
32727 /* OpenACC 2.5:
32728 auto
32729 finalize
32730 independent
32731 nohost
32732 seq */
32733
32734 static tree
32735 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
32736 tree list)
32737 {
32738 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
32739
32740 tree c = build_omp_clause (loc, code);
32741 OMP_CLAUSE_CHAIN (c) = list;
32742
32743 return c;
32744 }
32745
32746 /* OpenACC:
32747 num_gangs ( expression )
32748 num_workers ( expression )
32749 vector_length ( expression ) */
32750
32751 static tree
32752 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32753 const char *str, tree list)
32754 {
32755 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32756
32757 matching_parens parens;
32758 if (!parens.require_open (parser))
32759 return list;
32760
32761 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32762
32763 if (t == error_mark_node
32764 || !parens.require_close (parser))
32765 {
32766 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32767 /*or_comma=*/false,
32768 /*consume_paren=*/true);
32769 return list;
32770 }
32771
32772 check_no_duplicate_clause (list, code, str, loc);
32773
32774 tree c = build_omp_clause (loc, code);
32775 OMP_CLAUSE_OPERAND (c, 0) = t;
32776 OMP_CLAUSE_CHAIN (c) = list;
32777 return c;
32778 }
32779
32780 /* OpenACC:
32781
32782 gang [( gang-arg-list )]
32783 worker [( [num:] int-expr )]
32784 vector [( [length:] int-expr )]
32785
32786 where gang-arg is one of:
32787
32788 [num:] int-expr
32789 static: size-expr
32790
32791 and size-expr may be:
32792
32793 *
32794 int-expr
32795 */
32796
32797 static tree
32798 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
32799 omp_clause_code kind,
32800 const char *str, tree list)
32801 {
32802 const char *id = "num";
32803 cp_lexer *lexer = parser->lexer;
32804 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32805
32806 if (kind == OMP_CLAUSE_VECTOR)
32807 id = "length";
32808
32809 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32810 {
32811 matching_parens parens;
32812 parens.consume_open (parser);
32813
32814 do
32815 {
32816 cp_token *next = cp_lexer_peek_token (lexer);
32817 int idx = 0;
32818
32819 /* Gang static argument. */
32820 if (kind == OMP_CLAUSE_GANG
32821 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32822 {
32823 cp_lexer_consume_token (lexer);
32824
32825 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32826 goto cleanup_error;
32827
32828 idx = 1;
32829 if (ops[idx] != NULL)
32830 {
32831 cp_parser_error (parser, "too many %<static%> arguments");
32832 goto cleanup_error;
32833 }
32834
32835 /* Check for the '*' argument. */
32836 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32837 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32838 || cp_lexer_nth_token_is (parser->lexer, 2,
32839 CPP_CLOSE_PAREN)))
32840 {
32841 cp_lexer_consume_token (lexer);
32842 ops[idx] = integer_minus_one_node;
32843
32844 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32845 {
32846 cp_lexer_consume_token (lexer);
32847 continue;
32848 }
32849 else break;
32850 }
32851 }
32852 /* Worker num: argument and vector length: arguments. */
32853 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32854 && id_equal (next->u.value, id)
32855 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32856 {
32857 cp_lexer_consume_token (lexer); /* id */
32858 cp_lexer_consume_token (lexer); /* ':' */
32859 }
32860
32861 /* Now collect the actual argument. */
32862 if (ops[idx] != NULL_TREE)
32863 {
32864 cp_parser_error (parser, "unexpected argument");
32865 goto cleanup_error;
32866 }
32867
32868 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32869 false);
32870 if (expr == error_mark_node)
32871 goto cleanup_error;
32872
32873 mark_exp_read (expr);
32874 ops[idx] = expr;
32875
32876 if (kind == OMP_CLAUSE_GANG
32877 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32878 {
32879 cp_lexer_consume_token (lexer);
32880 continue;
32881 }
32882 break;
32883 }
32884 while (1);
32885
32886 if (!parens.require_close (parser))
32887 goto cleanup_error;
32888 }
32889
32890 check_no_duplicate_clause (list, kind, str, loc);
32891
32892 c = build_omp_clause (loc, kind);
32893
32894 if (ops[1])
32895 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32896
32897 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32898 OMP_CLAUSE_CHAIN (c) = list;
32899
32900 return c;
32901
32902 cleanup_error:
32903 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32904 return list;
32905 }
32906
32907 /* OpenACC 2.0:
32908 tile ( size-expr-list ) */
32909
32910 static tree
32911 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32912 {
32913 tree c, expr = error_mark_node;
32914 tree tile = NULL_TREE;
32915
32916 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32917 so, but the spec authors never considered such a case and have
32918 differing opinions on what it might mean, including 'not
32919 allowed'.) */
32920 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32921 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32922 clause_loc);
32923
32924 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32925 return list;
32926
32927 do
32928 {
32929 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32930 return list;
32931
32932 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32933 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32934 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32935 {
32936 cp_lexer_consume_token (parser->lexer);
32937 expr = integer_zero_node;
32938 }
32939 else
32940 expr = cp_parser_constant_expression (parser);
32941
32942 tile = tree_cons (NULL_TREE, expr, tile);
32943 }
32944 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32945
32946 /* Consume the trailing ')'. */
32947 cp_lexer_consume_token (parser->lexer);
32948
32949 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32950 tile = nreverse (tile);
32951 OMP_CLAUSE_TILE_LIST (c) = tile;
32952 OMP_CLAUSE_CHAIN (c) = list;
32953 return c;
32954 }
32955
32956 /* OpenACC 2.0
32957 Parse wait clause or directive parameters. */
32958
32959 static tree
32960 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32961 {
32962 vec<tree, va_gc> *args;
32963 tree t, args_tree;
32964
32965 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32966 /*cast_p=*/false,
32967 /*allow_expansion_p=*/true,
32968 /*non_constant_p=*/NULL);
32969
32970 if (args == NULL || args->length () == 0)
32971 {
32972 if (args != NULL)
32973 {
32974 cp_parser_error (parser, "expected integer expression list");
32975 release_tree_vector (args);
32976 }
32977 return list;
32978 }
32979
32980 args_tree = build_tree_list_vec (args);
32981
32982 release_tree_vector (args);
32983
32984 for (t = args_tree; t; t = TREE_CHAIN (t))
32985 {
32986 tree targ = TREE_VALUE (t);
32987
32988 if (targ != error_mark_node)
32989 {
32990 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32991 error ("%<wait%> expression must be integral");
32992 else
32993 {
32994 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32995
32996 targ = mark_rvalue_use (targ);
32997 OMP_CLAUSE_DECL (c) = targ;
32998 OMP_CLAUSE_CHAIN (c) = list;
32999 list = c;
33000 }
33001 }
33002 }
33003
33004 return list;
33005 }
33006
33007 /* OpenACC:
33008 wait [( int-expr-list )] */
33009
33010 static tree
33011 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
33012 {
33013 location_t location = cp_lexer_peek_token (parser->lexer)->location;
33014
33015 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33016 list = cp_parser_oacc_wait_list (parser, location, list);
33017 else
33018 {
33019 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
33020
33021 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33022 OMP_CLAUSE_CHAIN (c) = list;
33023 list = c;
33024 }
33025
33026 return list;
33027 }
33028
33029 /* OpenMP 3.0:
33030 collapse ( constant-expression ) */
33031
33032 static tree
33033 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
33034 {
33035 tree c, num;
33036 location_t loc;
33037 HOST_WIDE_INT n;
33038
33039 loc = cp_lexer_peek_token (parser->lexer)->location;
33040 matching_parens parens;
33041 if (!parens.require_open (parser))
33042 return list;
33043
33044 num = cp_parser_constant_expression (parser);
33045
33046 if (!parens.require_close (parser))
33047 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33048 /*or_comma=*/false,
33049 /*consume_paren=*/true);
33050
33051 if (num == error_mark_node)
33052 return list;
33053 num = fold_non_dependent_expr (num);
33054 if (!tree_fits_shwi_p (num)
33055 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33056 || (n = tree_to_shwi (num)) <= 0
33057 || (int) n != n)
33058 {
33059 error_at (loc, "collapse argument needs positive constant integer expression");
33060 return list;
33061 }
33062
33063 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
33064 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
33065 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
33066 OMP_CLAUSE_CHAIN (c) = list;
33067 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
33068
33069 return c;
33070 }
33071
33072 /* OpenMP 2.5:
33073 default ( none | shared )
33074
33075 OpenACC:
33076 default ( none | present ) */
33077
33078 static tree
33079 cp_parser_omp_clause_default (cp_parser *parser, tree list,
33080 location_t location, bool is_oacc)
33081 {
33082 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
33083 tree c;
33084
33085 matching_parens parens;
33086 if (!parens.require_open (parser))
33087 return list;
33088 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33089 {
33090 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33091 const char *p = IDENTIFIER_POINTER (id);
33092
33093 switch (p[0])
33094 {
33095 case 'n':
33096 if (strcmp ("none", p) != 0)
33097 goto invalid_kind;
33098 kind = OMP_CLAUSE_DEFAULT_NONE;
33099 break;
33100
33101 case 'p':
33102 if (strcmp ("present", p) != 0 || !is_oacc)
33103 goto invalid_kind;
33104 kind = OMP_CLAUSE_DEFAULT_PRESENT;
33105 break;
33106
33107 case 's':
33108 if (strcmp ("shared", p) != 0 || is_oacc)
33109 goto invalid_kind;
33110 kind = OMP_CLAUSE_DEFAULT_SHARED;
33111 break;
33112
33113 default:
33114 goto invalid_kind;
33115 }
33116
33117 cp_lexer_consume_token (parser->lexer);
33118 }
33119 else
33120 {
33121 invalid_kind:
33122 if (is_oacc)
33123 cp_parser_error (parser, "expected %<none%> or %<present%>");
33124 else
33125 cp_parser_error (parser, "expected %<none%> or %<shared%>");
33126 }
33127
33128 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
33129 || !parens.require_close (parser))
33130 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33131 /*or_comma=*/false,
33132 /*consume_paren=*/true);
33133
33134 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
33135 return list;
33136
33137 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
33138 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
33139 OMP_CLAUSE_CHAIN (c) = list;
33140 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
33141
33142 return c;
33143 }
33144
33145 /* OpenMP 3.1:
33146 final ( expression ) */
33147
33148 static tree
33149 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
33150 {
33151 tree t, c;
33152
33153 matching_parens parens;
33154 if (!parens.require_open (parser))
33155 return list;
33156
33157 t = cp_parser_assignment_expression (parser);
33158
33159 if (t == error_mark_node
33160 || !parens.require_close (parser))
33161 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33162 /*or_comma=*/false,
33163 /*consume_paren=*/true);
33164
33165 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
33166
33167 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
33168 OMP_CLAUSE_FINAL_EXPR (c) = t;
33169 OMP_CLAUSE_CHAIN (c) = list;
33170
33171 return c;
33172 }
33173
33174 /* OpenMP 2.5:
33175 if ( expression )
33176
33177 OpenMP 4.5:
33178 if ( directive-name-modifier : expression )
33179
33180 directive-name-modifier:
33181 parallel | task | taskloop | target data | target | target update
33182 | target enter data | target exit data
33183
33184 OpenMP 5.0:
33185 directive-name-modifier:
33186 ... | simd | cancel */
33187
33188 static tree
33189 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
33190 bool is_omp)
33191 {
33192 tree t, c;
33193 enum tree_code if_modifier = ERROR_MARK;
33194
33195 matching_parens parens;
33196 if (!parens.require_open (parser))
33197 return list;
33198
33199 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33200 {
33201 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33202 const char *p = IDENTIFIER_POINTER (id);
33203 int n = 2;
33204
33205 if (strcmp ("cancel", p) == 0)
33206 if_modifier = VOID_CST;
33207 else if (strcmp ("parallel", p) == 0)
33208 if_modifier = OMP_PARALLEL;
33209 else if (strcmp ("simd", p) == 0)
33210 if_modifier = OMP_SIMD;
33211 else if (strcmp ("task", p) == 0)
33212 if_modifier = OMP_TASK;
33213 else if (strcmp ("taskloop", p) == 0)
33214 if_modifier = OMP_TASKLOOP;
33215 else if (strcmp ("target", p) == 0)
33216 {
33217 if_modifier = OMP_TARGET;
33218 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
33219 {
33220 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
33221 p = IDENTIFIER_POINTER (id);
33222 if (strcmp ("data", p) == 0)
33223 if_modifier = OMP_TARGET_DATA;
33224 else if (strcmp ("update", p) == 0)
33225 if_modifier = OMP_TARGET_UPDATE;
33226 else if (strcmp ("enter", p) == 0)
33227 if_modifier = OMP_TARGET_ENTER_DATA;
33228 else if (strcmp ("exit", p) == 0)
33229 if_modifier = OMP_TARGET_EXIT_DATA;
33230 if (if_modifier != OMP_TARGET)
33231 n = 3;
33232 else
33233 {
33234 location_t loc
33235 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
33236 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
33237 "or %<exit%>");
33238 if_modifier = ERROR_MARK;
33239 }
33240 if (if_modifier == OMP_TARGET_ENTER_DATA
33241 || if_modifier == OMP_TARGET_EXIT_DATA)
33242 {
33243 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
33244 {
33245 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
33246 p = IDENTIFIER_POINTER (id);
33247 if (strcmp ("data", p) == 0)
33248 n = 4;
33249 }
33250 if (n != 4)
33251 {
33252 location_t loc
33253 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
33254 error_at (loc, "expected %<data%>");
33255 if_modifier = ERROR_MARK;
33256 }
33257 }
33258 }
33259 }
33260 if (if_modifier != ERROR_MARK)
33261 {
33262 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
33263 {
33264 while (n-- > 0)
33265 cp_lexer_consume_token (parser->lexer);
33266 }
33267 else
33268 {
33269 if (n > 2)
33270 {
33271 location_t loc
33272 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
33273 error_at (loc, "expected %<:%>");
33274 }
33275 if_modifier = ERROR_MARK;
33276 }
33277 }
33278 }
33279
33280 t = cp_parser_assignment_expression (parser);
33281
33282 if (t == error_mark_node
33283 || !parens.require_close (parser))
33284 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33285 /*or_comma=*/false,
33286 /*consume_paren=*/true);
33287
33288 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33289 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
33290 {
33291 if (if_modifier != ERROR_MARK
33292 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33293 {
33294 const char *p = NULL;
33295 switch (if_modifier)
33296 {
33297 case VOID_CST: p = "cancel"; break;
33298 case OMP_PARALLEL: p = "parallel"; break;
33299 case OMP_SIMD: p = "simd"; break;
33300 case OMP_TASK: p = "task"; break;
33301 case OMP_TASKLOOP: p = "taskloop"; break;
33302 case OMP_TARGET_DATA: p = "target data"; break;
33303 case OMP_TARGET: p = "target"; break;
33304 case OMP_TARGET_UPDATE: p = "target update"; break;
33305 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
33306 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
33307 default: gcc_unreachable ();
33308 }
33309 error_at (location, "too many %<if%> clauses with %qs modifier",
33310 p);
33311 return list;
33312 }
33313 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33314 {
33315 if (!is_omp)
33316 error_at (location, "too many %<if%> clauses");
33317 else
33318 error_at (location, "too many %<if%> clauses without modifier");
33319 return list;
33320 }
33321 else if (if_modifier == ERROR_MARK
33322 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
33323 {
33324 error_at (location, "if any %<if%> clause has modifier, then all "
33325 "%<if%> clauses have to use modifier");
33326 return list;
33327 }
33328 }
33329
33330 c = build_omp_clause (location, OMP_CLAUSE_IF);
33331 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
33332 OMP_CLAUSE_IF_EXPR (c) = t;
33333 OMP_CLAUSE_CHAIN (c) = list;
33334
33335 return c;
33336 }
33337
33338 /* OpenMP 3.1:
33339 mergeable */
33340
33341 static tree
33342 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
33343 tree list, location_t location)
33344 {
33345 tree c;
33346
33347 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
33348 location);
33349
33350 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
33351 OMP_CLAUSE_CHAIN (c) = list;
33352 return c;
33353 }
33354
33355 /* OpenMP 2.5:
33356 nowait */
33357
33358 static tree
33359 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
33360 tree list, location_t location)
33361 {
33362 tree c;
33363
33364 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
33365
33366 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
33367 OMP_CLAUSE_CHAIN (c) = list;
33368 return c;
33369 }
33370
33371 /* OpenMP 2.5:
33372 num_threads ( expression ) */
33373
33374 static tree
33375 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
33376 location_t location)
33377 {
33378 tree t, c;
33379
33380 matching_parens parens;
33381 if (!parens.require_open (parser))
33382 return list;
33383
33384 t = cp_parser_assignment_expression (parser);
33385
33386 if (t == error_mark_node
33387 || !parens.require_close (parser))
33388 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33389 /*or_comma=*/false,
33390 /*consume_paren=*/true);
33391
33392 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
33393 "num_threads", location);
33394
33395 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
33396 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
33397 OMP_CLAUSE_CHAIN (c) = list;
33398
33399 return c;
33400 }
33401
33402 /* OpenMP 4.5:
33403 num_tasks ( expression ) */
33404
33405 static tree
33406 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
33407 location_t location)
33408 {
33409 tree t, c;
33410
33411 matching_parens parens;
33412 if (!parens.require_open (parser))
33413 return list;
33414
33415 t = cp_parser_assignment_expression (parser);
33416
33417 if (t == error_mark_node
33418 || !parens.require_close (parser))
33419 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33420 /*or_comma=*/false,
33421 /*consume_paren=*/true);
33422
33423 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
33424 "num_tasks", location);
33425
33426 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
33427 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
33428 OMP_CLAUSE_CHAIN (c) = list;
33429
33430 return c;
33431 }
33432
33433 /* OpenMP 4.5:
33434 grainsize ( expression ) */
33435
33436 static tree
33437 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
33438 location_t location)
33439 {
33440 tree t, c;
33441
33442 matching_parens parens;
33443 if (!parens.require_open (parser))
33444 return list;
33445
33446 t = cp_parser_assignment_expression (parser);
33447
33448 if (t == error_mark_node
33449 || !parens.require_close (parser))
33450 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33451 /*or_comma=*/false,
33452 /*consume_paren=*/true);
33453
33454 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
33455 "grainsize", location);
33456
33457 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
33458 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
33459 OMP_CLAUSE_CHAIN (c) = list;
33460
33461 return c;
33462 }
33463
33464 /* OpenMP 4.5:
33465 priority ( expression ) */
33466
33467 static tree
33468 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
33469 location_t location)
33470 {
33471 tree t, c;
33472
33473 matching_parens parens;
33474 if (!parens.require_open (parser))
33475 return list;
33476
33477 t = cp_parser_assignment_expression (parser);
33478
33479 if (t == error_mark_node
33480 || !parens.require_close (parser))
33481 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33482 /*or_comma=*/false,
33483 /*consume_paren=*/true);
33484
33485 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
33486 "priority", location);
33487
33488 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
33489 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
33490 OMP_CLAUSE_CHAIN (c) = list;
33491
33492 return c;
33493 }
33494
33495 /* OpenMP 4.5:
33496 hint ( expression ) */
33497
33498 static tree
33499 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
33500 {
33501 tree t, c;
33502
33503 matching_parens parens;
33504 if (!parens.require_open (parser))
33505 return list;
33506
33507 t = cp_parser_assignment_expression (parser);
33508
33509 if (t == error_mark_node
33510 || !parens.require_close (parser))
33511 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33512 /*or_comma=*/false,
33513 /*consume_paren=*/true);
33514
33515 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
33516
33517 c = build_omp_clause (location, OMP_CLAUSE_HINT);
33518 OMP_CLAUSE_HINT_EXPR (c) = t;
33519 OMP_CLAUSE_CHAIN (c) = list;
33520
33521 return c;
33522 }
33523
33524 /* OpenMP 4.5:
33525 defaultmap ( tofrom : scalar )
33526
33527 OpenMP 5.0:
33528 defaultmap ( implicit-behavior [ : variable-category ] ) */
33529
33530 static tree
33531 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
33532 location_t location)
33533 {
33534 tree c, id;
33535 const char *p;
33536 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33537 enum omp_clause_defaultmap_kind category
33538 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
33539
33540 matching_parens parens;
33541 if (!parens.require_open (parser))
33542 return list;
33543
33544 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
33545 p = "default";
33546 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33547 {
33548 invalid_behavior:
33549 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
33550 "%<tofrom%>, %<firstprivate%>, %<none%> "
33551 "or %<default%>");
33552 goto out_err;
33553 }
33554 else
33555 {
33556 id = cp_lexer_peek_token (parser->lexer)->u.value;
33557 p = IDENTIFIER_POINTER (id);
33558 }
33559
33560 switch (p[0])
33561 {
33562 case 'a':
33563 if (strcmp ("alloc", p) == 0)
33564 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
33565 else
33566 goto invalid_behavior;
33567 break;
33568
33569 case 'd':
33570 if (strcmp ("default", p) == 0)
33571 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33572 else
33573 goto invalid_behavior;
33574 break;
33575
33576 case 'f':
33577 if (strcmp ("firstprivate", p) == 0)
33578 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
33579 else if (strcmp ("from", p) == 0)
33580 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
33581 else
33582 goto invalid_behavior;
33583 break;
33584
33585 case 'n':
33586 if (strcmp ("none", p) == 0)
33587 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
33588 else
33589 goto invalid_behavior;
33590 break;
33591
33592 case 't':
33593 if (strcmp ("tofrom", p) == 0)
33594 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
33595 else if (strcmp ("to", p) == 0)
33596 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
33597 else
33598 goto invalid_behavior;
33599 break;
33600
33601 default:
33602 goto invalid_behavior;
33603 }
33604 cp_lexer_consume_token (parser->lexer);
33605
33606 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33607 {
33608 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33609 goto out_err;
33610
33611 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33612 {
33613 invalid_category:
33614 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
33615 "%<pointer%>");
33616 goto out_err;
33617 }
33618 id = cp_lexer_peek_token (parser->lexer)->u.value;
33619 p = IDENTIFIER_POINTER (id);
33620
33621 switch (p[0])
33622 {
33623 case 'a':
33624 if (strcmp ("aggregate", p) == 0)
33625 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
33626 else
33627 goto invalid_category;
33628 break;
33629
33630 case 'p':
33631 if (strcmp ("pointer", p) == 0)
33632 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
33633 else
33634 goto invalid_category;
33635 break;
33636
33637 case 's':
33638 if (strcmp ("scalar", p) == 0)
33639 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
33640 else
33641 goto invalid_category;
33642 break;
33643
33644 default:
33645 goto invalid_category;
33646 }
33647
33648 cp_lexer_consume_token (parser->lexer);
33649 }
33650 if (!parens.require_close (parser))
33651 goto out_err;
33652
33653 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33654 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
33655 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33656 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
33657 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
33658 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
33659 {
33660 enum omp_clause_defaultmap_kind cat = category;
33661 location_t loc = OMP_CLAUSE_LOCATION (c);
33662 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
33663 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
33664 p = NULL;
33665 switch (cat)
33666 {
33667 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
33668 p = NULL;
33669 break;
33670 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33671 p = "aggregate";
33672 break;
33673 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33674 p = "pointer";
33675 break;
33676 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33677 p = "scalar";
33678 break;
33679 default:
33680 gcc_unreachable ();
33681 }
33682 if (p)
33683 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33684 p);
33685 else
33686 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33687 "category");
33688 break;
33689 }
33690
33691 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33692 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33693 OMP_CLAUSE_CHAIN (c) = list;
33694 return c;
33695
33696 out_err:
33697 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33698 /*or_comma=*/false,
33699 /*consume_paren=*/true);
33700 return list;
33701 }
33702
33703 /* OpenMP 2.5:
33704 ordered
33705
33706 OpenMP 4.5:
33707 ordered ( constant-expression ) */
33708
33709 static tree
33710 cp_parser_omp_clause_ordered (cp_parser *parser,
33711 tree list, location_t location)
33712 {
33713 tree c, num = NULL_TREE;
33714 HOST_WIDE_INT n;
33715
33716 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33717 "ordered", location);
33718
33719 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33720 {
33721 matching_parens parens;
33722 parens.consume_open (parser);
33723
33724 num = cp_parser_constant_expression (parser);
33725
33726 if (!parens.require_close (parser))
33727 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33728 /*or_comma=*/false,
33729 /*consume_paren=*/true);
33730
33731 if (num == error_mark_node)
33732 return list;
33733 num = fold_non_dependent_expr (num);
33734 if (!tree_fits_shwi_p (num)
33735 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33736 || (n = tree_to_shwi (num)) <= 0
33737 || (int) n != n)
33738 {
33739 error_at (location,
33740 "ordered argument needs positive constant integer "
33741 "expression");
33742 return list;
33743 }
33744 }
33745
33746 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33747 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33748 OMP_CLAUSE_CHAIN (c) = list;
33749 return c;
33750 }
33751
33752 /* OpenMP 2.5:
33753 reduction ( reduction-operator : variable-list )
33754
33755 reduction-operator:
33756 One of: + * - & ^ | && ||
33757
33758 OpenMP 3.1:
33759
33760 reduction-operator:
33761 One of: + * - & ^ | && || min max
33762
33763 OpenMP 4.0:
33764
33765 reduction-operator:
33766 One of: + * - & ^ | && ||
33767 id-expression
33768
33769 OpenMP 5.0:
33770 reduction ( reduction-modifier, reduction-operator : variable-list )
33771 in_reduction ( reduction-operator : variable-list )
33772 task_reduction ( reduction-operator : variable-list ) */
33773
33774 static tree
33775 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33776 bool is_omp, tree list)
33777 {
33778 enum tree_code code = ERROR_MARK;
33779 tree nlist, c, id = NULL_TREE;
33780 bool task = false;
33781 bool inscan = false;
33782
33783 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33784 return list;
33785
33786 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33787 {
33788 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33789 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33790 {
33791 cp_lexer_consume_token (parser->lexer);
33792 cp_lexer_consume_token (parser->lexer);
33793 }
33794 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33795 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33796 {
33797 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33798 const char *p = IDENTIFIER_POINTER (id);
33799 if (strcmp (p, "task") == 0)
33800 task = true;
33801 else if (strcmp (p, "inscan") == 0)
33802 inscan = true;
33803 if (task || inscan)
33804 {
33805 cp_lexer_consume_token (parser->lexer);
33806 cp_lexer_consume_token (parser->lexer);
33807 }
33808 }
33809 }
33810
33811 switch (cp_lexer_peek_token (parser->lexer)->type)
33812 {
33813 case CPP_PLUS: code = PLUS_EXPR; break;
33814 case CPP_MULT: code = MULT_EXPR; break;
33815 case CPP_MINUS: code = MINUS_EXPR; break;
33816 case CPP_AND: code = BIT_AND_EXPR; break;
33817 case CPP_XOR: code = BIT_XOR_EXPR; break;
33818 case CPP_OR: code = BIT_IOR_EXPR; break;
33819 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33820 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33821 default: break;
33822 }
33823
33824 if (code != ERROR_MARK)
33825 cp_lexer_consume_token (parser->lexer);
33826 else
33827 {
33828 bool saved_colon_corrects_to_scope_p;
33829 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33830 parser->colon_corrects_to_scope_p = false;
33831 id = cp_parser_id_expression (parser, /*template_p=*/false,
33832 /*check_dependency_p=*/true,
33833 /*template_p=*/NULL,
33834 /*declarator_p=*/false,
33835 /*optional_p=*/false);
33836 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33837 if (identifier_p (id))
33838 {
33839 const char *p = IDENTIFIER_POINTER (id);
33840
33841 if (strcmp (p, "min") == 0)
33842 code = MIN_EXPR;
33843 else if (strcmp (p, "max") == 0)
33844 code = MAX_EXPR;
33845 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33846 code = PLUS_EXPR;
33847 else if (id == ovl_op_identifier (false, MULT_EXPR))
33848 code = MULT_EXPR;
33849 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33850 code = MINUS_EXPR;
33851 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33852 code = BIT_AND_EXPR;
33853 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33854 code = BIT_IOR_EXPR;
33855 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33856 code = BIT_XOR_EXPR;
33857 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33858 code = TRUTH_ANDIF_EXPR;
33859 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33860 code = TRUTH_ORIF_EXPR;
33861 id = omp_reduction_id (code, id, NULL_TREE);
33862 tree scope = parser->scope;
33863 if (scope)
33864 id = build_qualified_name (NULL_TREE, scope, id, false);
33865 parser->scope = NULL_TREE;
33866 parser->qualifying_scope = NULL_TREE;
33867 parser->object_scope = NULL_TREE;
33868 }
33869 else
33870 {
33871 error ("invalid reduction-identifier");
33872 resync_fail:
33873 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33874 /*or_comma=*/false,
33875 /*consume_paren=*/true);
33876 return list;
33877 }
33878 }
33879
33880 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33881 goto resync_fail;
33882
33883 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33884 NULL);
33885 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33886 {
33887 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33888 if (task)
33889 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33890 else if (inscan)
33891 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33892 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33893 }
33894
33895 return nlist;
33896 }
33897
33898 /* OpenMP 2.5:
33899 schedule ( schedule-kind )
33900 schedule ( schedule-kind , expression )
33901
33902 schedule-kind:
33903 static | dynamic | guided | runtime | auto
33904
33905 OpenMP 4.5:
33906 schedule ( schedule-modifier : schedule-kind )
33907 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33908
33909 schedule-modifier:
33910 simd
33911 monotonic
33912 nonmonotonic */
33913
33914 static tree
33915 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33916 {
33917 tree c, t;
33918 int modifiers = 0, nmodifiers = 0;
33919
33920 matching_parens parens;
33921 if (!parens.require_open (parser))
33922 return list;
33923
33924 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33925
33926 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33927 {
33928 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33929 const char *p = IDENTIFIER_POINTER (id);
33930 if (strcmp ("simd", p) == 0)
33931 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33932 else if (strcmp ("monotonic", p) == 0)
33933 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33934 else if (strcmp ("nonmonotonic", p) == 0)
33935 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33936 else
33937 break;
33938 cp_lexer_consume_token (parser->lexer);
33939 if (nmodifiers++ == 0
33940 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33941 cp_lexer_consume_token (parser->lexer);
33942 else
33943 {
33944 cp_parser_require (parser, CPP_COLON, RT_COLON);
33945 break;
33946 }
33947 }
33948
33949 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33950 {
33951 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33952 const char *p = IDENTIFIER_POINTER (id);
33953
33954 switch (p[0])
33955 {
33956 case 'd':
33957 if (strcmp ("dynamic", p) != 0)
33958 goto invalid_kind;
33959 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33960 break;
33961
33962 case 'g':
33963 if (strcmp ("guided", p) != 0)
33964 goto invalid_kind;
33965 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33966 break;
33967
33968 case 'r':
33969 if (strcmp ("runtime", p) != 0)
33970 goto invalid_kind;
33971 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33972 break;
33973
33974 default:
33975 goto invalid_kind;
33976 }
33977 }
33978 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33979 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33980 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33981 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33982 else
33983 goto invalid_kind;
33984 cp_lexer_consume_token (parser->lexer);
33985
33986 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33987 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33988 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33989 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33990 {
33991 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33992 "specified");
33993 modifiers = 0;
33994 }
33995
33996 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33997 {
33998 cp_token *token;
33999 cp_lexer_consume_token (parser->lexer);
34000
34001 token = cp_lexer_peek_token (parser->lexer);
34002 t = cp_parser_assignment_expression (parser);
34003
34004 if (t == error_mark_node)
34005 goto resync_fail;
34006 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
34007 error_at (token->location, "schedule %<runtime%> does not take "
34008 "a %<chunk_size%> parameter");
34009 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
34010 error_at (token->location, "schedule %<auto%> does not take "
34011 "a %<chunk_size%> parameter");
34012 else
34013 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
34014
34015 if (!parens.require_close (parser))
34016 goto resync_fail;
34017 }
34018 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34019 goto resync_fail;
34020
34021 OMP_CLAUSE_SCHEDULE_KIND (c)
34022 = (enum omp_clause_schedule_kind)
34023 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
34024
34025 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
34026 OMP_CLAUSE_CHAIN (c) = list;
34027 return c;
34028
34029 invalid_kind:
34030 cp_parser_error (parser, "invalid schedule kind");
34031 resync_fail:
34032 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34033 /*or_comma=*/false,
34034 /*consume_paren=*/true);
34035 return list;
34036 }
34037
34038 /* OpenMP 3.0:
34039 untied */
34040
34041 static tree
34042 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
34043 tree list, location_t location)
34044 {
34045 tree c;
34046
34047 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
34048
34049 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
34050 OMP_CLAUSE_CHAIN (c) = list;
34051 return c;
34052 }
34053
34054 /* OpenMP 4.0:
34055 inbranch
34056 notinbranch */
34057
34058 static tree
34059 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
34060 tree list, location_t location)
34061 {
34062 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
34063 tree c = build_omp_clause (location, code);
34064 OMP_CLAUSE_CHAIN (c) = list;
34065 return c;
34066 }
34067
34068 /* OpenMP 4.0:
34069 parallel
34070 for
34071 sections
34072 taskgroup */
34073
34074 static tree
34075 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
34076 enum omp_clause_code code,
34077 tree list, location_t location)
34078 {
34079 tree c = build_omp_clause (location, code);
34080 OMP_CLAUSE_CHAIN (c) = list;
34081 return c;
34082 }
34083
34084 /* OpenMP 4.5:
34085 nogroup */
34086
34087 static tree
34088 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
34089 tree list, location_t location)
34090 {
34091 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
34092 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
34093 OMP_CLAUSE_CHAIN (c) = list;
34094 return c;
34095 }
34096
34097 /* OpenMP 4.5:
34098 simd
34099 threads */
34100
34101 static tree
34102 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
34103 enum omp_clause_code code,
34104 tree list, location_t location)
34105 {
34106 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
34107 tree c = build_omp_clause (location, code);
34108 OMP_CLAUSE_CHAIN (c) = list;
34109 return c;
34110 }
34111
34112 /* OpenMP 4.0:
34113 num_teams ( expression ) */
34114
34115 static tree
34116 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
34117 location_t location)
34118 {
34119 tree t, c;
34120
34121 matching_parens parens;
34122 if (!parens.require_open (parser))
34123 return list;
34124
34125 t = cp_parser_assignment_expression (parser);
34126
34127 if (t == error_mark_node
34128 || !parens.require_close (parser))
34129 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34130 /*or_comma=*/false,
34131 /*consume_paren=*/true);
34132
34133 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
34134 "num_teams", location);
34135
34136 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
34137 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
34138 OMP_CLAUSE_CHAIN (c) = list;
34139
34140 return c;
34141 }
34142
34143 /* OpenMP 4.0:
34144 thread_limit ( expression ) */
34145
34146 static tree
34147 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
34148 location_t location)
34149 {
34150 tree t, c;
34151
34152 matching_parens parens;
34153 if (!parens.require_open (parser))
34154 return list;
34155
34156 t = cp_parser_assignment_expression (parser);
34157
34158 if (t == error_mark_node
34159 || !parens.require_close (parser))
34160 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34161 /*or_comma=*/false,
34162 /*consume_paren=*/true);
34163
34164 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
34165 "thread_limit", location);
34166
34167 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
34168 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
34169 OMP_CLAUSE_CHAIN (c) = list;
34170
34171 return c;
34172 }
34173
34174 /* OpenMP 4.0:
34175 aligned ( variable-list )
34176 aligned ( variable-list : constant-expression ) */
34177
34178 static tree
34179 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
34180 {
34181 tree nlist, c, alignment = NULL_TREE;
34182 bool colon;
34183
34184 matching_parens parens;
34185 if (!parens.require_open (parser))
34186 return list;
34187
34188 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
34189 &colon);
34190
34191 if (colon)
34192 {
34193 alignment = cp_parser_constant_expression (parser);
34194
34195 if (!parens.require_close (parser))
34196 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34197 /*or_comma=*/false,
34198 /*consume_paren=*/true);
34199
34200 if (alignment == error_mark_node)
34201 alignment = NULL_TREE;
34202 }
34203
34204 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34205 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
34206
34207 return nlist;
34208 }
34209
34210 /* OpenMP 2.5:
34211 lastprivate ( variable-list )
34212
34213 OpenMP 5.0:
34214 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
34215
34216 static tree
34217 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
34218 {
34219 bool conditional = false;
34220
34221 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34222 return list;
34223
34224 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34225 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
34226 {
34227 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34228 const char *p = IDENTIFIER_POINTER (id);
34229
34230 if (strcmp ("conditional", p) == 0)
34231 {
34232 conditional = true;
34233 cp_lexer_consume_token (parser->lexer);
34234 cp_lexer_consume_token (parser->lexer);
34235 }
34236 }
34237
34238 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
34239 list, NULL);
34240
34241 if (conditional)
34242 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34243 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
34244 return nlist;
34245 }
34246
34247 /* OpenMP 4.0:
34248 linear ( variable-list )
34249 linear ( variable-list : expression )
34250
34251 OpenMP 4.5:
34252 linear ( modifier ( variable-list ) )
34253 linear ( modifier ( variable-list ) : expression ) */
34254
34255 static tree
34256 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
34257 bool declare_simd)
34258 {
34259 tree nlist, c, step = integer_one_node;
34260 bool colon;
34261 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
34262
34263 matching_parens parens;
34264 if (!parens.require_open (parser))
34265 return list;
34266
34267 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34268 {
34269 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34270 const char *p = IDENTIFIER_POINTER (id);
34271
34272 if (strcmp ("ref", p) == 0)
34273 kind = OMP_CLAUSE_LINEAR_REF;
34274 else if (strcmp ("val", p) == 0)
34275 kind = OMP_CLAUSE_LINEAR_VAL;
34276 else if (strcmp ("uval", p) == 0)
34277 kind = OMP_CLAUSE_LINEAR_UVAL;
34278 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
34279 cp_lexer_consume_token (parser->lexer);
34280 else
34281 kind = OMP_CLAUSE_LINEAR_DEFAULT;
34282 }
34283
34284 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
34285 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
34286 &colon);
34287 else
34288 {
34289 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
34290 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
34291 if (colon)
34292 cp_parser_require (parser, CPP_COLON, RT_COLON);
34293 else if (!parens.require_close (parser))
34294 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34295 /*or_comma=*/false,
34296 /*consume_paren=*/true);
34297 }
34298
34299 if (colon)
34300 {
34301 step = NULL_TREE;
34302 if (declare_simd
34303 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34304 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
34305 {
34306 cp_token *token = cp_lexer_peek_token (parser->lexer);
34307 cp_parser_parse_tentatively (parser);
34308 step = cp_parser_id_expression (parser, /*template_p=*/false,
34309 /*check_dependency_p=*/true,
34310 /*template_p=*/NULL,
34311 /*declarator_p=*/false,
34312 /*optional_p=*/false);
34313 if (step != error_mark_node)
34314 step = cp_parser_lookup_name_simple (parser, step, token->location);
34315 if (step == error_mark_node)
34316 {
34317 step = NULL_TREE;
34318 cp_parser_abort_tentative_parse (parser);
34319 }
34320 else if (!cp_parser_parse_definitely (parser))
34321 step = NULL_TREE;
34322 }
34323 if (!step)
34324 step = cp_parser_assignment_expression (parser);
34325
34326 if (!parens.require_close (parser))
34327 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34328 /*or_comma=*/false,
34329 /*consume_paren=*/true);
34330
34331 if (step == error_mark_node)
34332 return list;
34333 }
34334
34335 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34336 {
34337 OMP_CLAUSE_LINEAR_STEP (c) = step;
34338 OMP_CLAUSE_LINEAR_KIND (c) = kind;
34339 }
34340
34341 return nlist;
34342 }
34343
34344 /* OpenMP 4.0:
34345 safelen ( constant-expression ) */
34346
34347 static tree
34348 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
34349 location_t location)
34350 {
34351 tree t, c;
34352
34353 matching_parens parens;
34354 if (!parens.require_open (parser))
34355 return list;
34356
34357 t = cp_parser_constant_expression (parser);
34358
34359 if (t == error_mark_node
34360 || !parens.require_close (parser))
34361 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34362 /*or_comma=*/false,
34363 /*consume_paren=*/true);
34364
34365 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
34366
34367 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
34368 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
34369 OMP_CLAUSE_CHAIN (c) = list;
34370
34371 return c;
34372 }
34373
34374 /* OpenMP 4.0:
34375 simdlen ( constant-expression ) */
34376
34377 static tree
34378 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
34379 location_t location)
34380 {
34381 tree t, c;
34382
34383 matching_parens parens;
34384 if (!parens.require_open (parser))
34385 return list;
34386
34387 t = cp_parser_constant_expression (parser);
34388
34389 if (t == error_mark_node
34390 || !parens.require_close (parser))
34391 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34392 /*or_comma=*/false,
34393 /*consume_paren=*/true);
34394
34395 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
34396
34397 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
34398 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
34399 OMP_CLAUSE_CHAIN (c) = list;
34400
34401 return c;
34402 }
34403
34404 /* OpenMP 4.5:
34405 vec:
34406 identifier [+/- integer]
34407 vec , identifier [+/- integer]
34408 */
34409
34410 static tree
34411 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
34412 tree list)
34413 {
34414 tree vec = NULL;
34415
34416 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34417 {
34418 cp_parser_error (parser, "expected identifier");
34419 return list;
34420 }
34421
34422 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34423 {
34424 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
34425 tree t, identifier = cp_parser_identifier (parser);
34426 tree addend = NULL;
34427
34428 if (identifier == error_mark_node)
34429 t = error_mark_node;
34430 else
34431 {
34432 t = cp_parser_lookup_name_simple
34433 (parser, identifier,
34434 cp_lexer_peek_token (parser->lexer)->location);
34435 if (t == error_mark_node)
34436 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
34437 id_loc);
34438 }
34439
34440 bool neg = false;
34441 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
34442 neg = true;
34443 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
34444 {
34445 addend = integer_zero_node;
34446 goto add_to_vector;
34447 }
34448 cp_lexer_consume_token (parser->lexer);
34449
34450 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
34451 {
34452 cp_parser_error (parser, "expected integer");
34453 return list;
34454 }
34455
34456 addend = cp_lexer_peek_token (parser->lexer)->u.value;
34457 if (TREE_CODE (addend) != INTEGER_CST)
34458 {
34459 cp_parser_error (parser, "expected integer");
34460 return list;
34461 }
34462 cp_lexer_consume_token (parser->lexer);
34463
34464 add_to_vector:
34465 if (t != error_mark_node)
34466 {
34467 vec = tree_cons (addend, t, vec);
34468 if (neg)
34469 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
34470 }
34471
34472 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34473 break;
34474
34475 cp_lexer_consume_token (parser->lexer);
34476 }
34477
34478 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
34479 {
34480 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
34481 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
34482 OMP_CLAUSE_DECL (u) = nreverse (vec);
34483 OMP_CLAUSE_CHAIN (u) = list;
34484 return u;
34485 }
34486 return list;
34487 }
34488
34489 /* OpenMP 5.0:
34490 iterators ( iterators-definition )
34491
34492 iterators-definition:
34493 iterator-specifier
34494 iterator-specifier , iterators-definition
34495
34496 iterator-specifier:
34497 identifier = range-specification
34498 iterator-type identifier = range-specification
34499
34500 range-specification:
34501 begin : end
34502 begin : end : step */
34503
34504 static tree
34505 cp_parser_omp_iterators (cp_parser *parser)
34506 {
34507 tree ret = NULL_TREE, *last = &ret;
34508 cp_lexer_consume_token (parser->lexer);
34509
34510 matching_parens parens;
34511 if (!parens.require_open (parser))
34512 return error_mark_node;
34513
34514 bool saved_colon_corrects_to_scope_p
34515 = parser->colon_corrects_to_scope_p;
34516 bool saved_colon_doesnt_start_class_def_p
34517 = parser->colon_doesnt_start_class_def_p;
34518
34519 do
34520 {
34521 tree iter_type;
34522 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34523 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
34524 iter_type = integer_type_node;
34525 else
34526 {
34527 const char *saved_message
34528 = parser->type_definition_forbidden_message;
34529 parser->type_definition_forbidden_message
34530 = G_("types may not be defined in iterator type");
34531
34532 iter_type = cp_parser_type_id (parser);
34533
34534 parser->type_definition_forbidden_message = saved_message;
34535 }
34536
34537 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34538 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34539 {
34540 cp_parser_error (parser, "expected identifier");
34541 break;
34542 }
34543
34544 tree id = cp_parser_identifier (parser);
34545 if (id == error_mark_node)
34546 break;
34547
34548 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34549 break;
34550
34551 parser->colon_corrects_to_scope_p = false;
34552 parser->colon_doesnt_start_class_def_p = true;
34553 tree begin = cp_parser_assignment_expression (parser);
34554
34555 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34556 break;
34557
34558 tree end = cp_parser_assignment_expression (parser);
34559
34560 tree step = integer_one_node;
34561 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34562 {
34563 cp_lexer_consume_token (parser->lexer);
34564 step = cp_parser_assignment_expression (parser);
34565 }
34566
34567 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
34568 DECL_ARTIFICIAL (iter_var) = 1;
34569 DECL_CONTEXT (iter_var) = current_function_decl;
34570 pushdecl (iter_var);
34571
34572 *last = make_tree_vec (6);
34573 TREE_VEC_ELT (*last, 0) = iter_var;
34574 TREE_VEC_ELT (*last, 1) = begin;
34575 TREE_VEC_ELT (*last, 2) = end;
34576 TREE_VEC_ELT (*last, 3) = step;
34577 last = &TREE_CHAIN (*last);
34578
34579 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34580 {
34581 cp_lexer_consume_token (parser->lexer);
34582 continue;
34583 }
34584 break;
34585 }
34586 while (1);
34587
34588 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34589 parser->colon_doesnt_start_class_def_p
34590 = saved_colon_doesnt_start_class_def_p;
34591
34592 if (!parens.require_close (parser))
34593 cp_parser_skip_to_closing_parenthesis (parser,
34594 /*recovering=*/true,
34595 /*or_comma=*/false,
34596 /*consume_paren=*/true);
34597
34598 return ret ? ret : error_mark_node;
34599 }
34600
34601 /* OpenMP 4.0:
34602 depend ( depend-kind : variable-list )
34603
34604 depend-kind:
34605 in | out | inout
34606
34607 OpenMP 4.5:
34608 depend ( source )
34609
34610 depend ( sink : vec )
34611
34612 OpenMP 5.0:
34613 depend ( depend-modifier , depend-kind: variable-list )
34614
34615 depend-kind:
34616 in | out | inout | mutexinoutset | depobj
34617
34618 depend-modifier:
34619 iterator ( iterators-definition ) */
34620
34621 static tree
34622 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
34623 {
34624 tree nlist, c, iterators = NULL_TREE;
34625 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
34626
34627 matching_parens parens;
34628 if (!parens.require_open (parser))
34629 return list;
34630
34631 do
34632 {
34633 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34634 goto invalid_kind;
34635
34636 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34637 const char *p = IDENTIFIER_POINTER (id);
34638
34639 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
34640 {
34641 begin_scope (sk_omp, NULL);
34642 iterators = cp_parser_omp_iterators (parser);
34643 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
34644 continue;
34645 }
34646 if (strcmp ("in", p) == 0)
34647 kind = OMP_CLAUSE_DEPEND_IN;
34648 else if (strcmp ("inout", p) == 0)
34649 kind = OMP_CLAUSE_DEPEND_INOUT;
34650 else if (strcmp ("mutexinoutset", p) == 0)
34651 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
34652 else if (strcmp ("out", p) == 0)
34653 kind = OMP_CLAUSE_DEPEND_OUT;
34654 else if (strcmp ("depobj", p) == 0)
34655 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
34656 else if (strcmp ("sink", p) == 0)
34657 kind = OMP_CLAUSE_DEPEND_SINK;
34658 else if (strcmp ("source", p) == 0)
34659 kind = OMP_CLAUSE_DEPEND_SOURCE;
34660 else
34661 goto invalid_kind;
34662 break;
34663 }
34664 while (1);
34665
34666 cp_lexer_consume_token (parser->lexer);
34667
34668 if (iterators
34669 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34670 {
34671 poplevel (0, 1, 0);
34672 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34673 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34674 iterators = NULL_TREE;
34675 }
34676
34677 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34678 {
34679 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34680 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34681 OMP_CLAUSE_DECL (c) = NULL_TREE;
34682 OMP_CLAUSE_CHAIN (c) = list;
34683 if (!parens.require_close (parser))
34684 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34685 /*or_comma=*/false,
34686 /*consume_paren=*/true);
34687 return c;
34688 }
34689
34690 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34691 goto resync_fail;
34692
34693 if (kind == OMP_CLAUSE_DEPEND_SINK)
34694 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34695 else
34696 {
34697 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34698 list, NULL);
34699
34700 if (iterators)
34701 {
34702 tree block = poplevel (1, 1, 0);
34703 if (iterators == error_mark_node)
34704 iterators = NULL_TREE;
34705 else
34706 TREE_VEC_ELT (iterators, 5) = block;
34707 }
34708
34709 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34710 {
34711 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34712 if (iterators)
34713 OMP_CLAUSE_DECL (c)
34714 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34715 }
34716 }
34717 return nlist;
34718
34719 invalid_kind:
34720 cp_parser_error (parser, "invalid depend kind");
34721 resync_fail:
34722 if (iterators)
34723 poplevel (0, 1, 0);
34724 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34725 /*or_comma=*/false,
34726 /*consume_paren=*/true);
34727 return list;
34728 }
34729
34730 /* OpenMP 4.0:
34731 map ( map-kind : variable-list )
34732 map ( variable-list )
34733
34734 map-kind:
34735 alloc | to | from | tofrom
34736
34737 OpenMP 4.5:
34738 map-kind:
34739 alloc | to | from | tofrom | release | delete
34740
34741 map ( always [,] map-kind: variable-list ) */
34742
34743 static tree
34744 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34745 {
34746 tree nlist, c;
34747 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34748 bool always = false;
34749
34750 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34751 return list;
34752
34753 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34754 {
34755 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34756 const char *p = IDENTIFIER_POINTER (id);
34757
34758 if (strcmp ("always", p) == 0)
34759 {
34760 int nth = 2;
34761 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34762 nth++;
34763 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34764 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34765 == RID_DELETE))
34766 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34767 == CPP_COLON))
34768 {
34769 always = true;
34770 cp_lexer_consume_token (parser->lexer);
34771 if (nth == 3)
34772 cp_lexer_consume_token (parser->lexer);
34773 }
34774 }
34775 }
34776
34777 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34778 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34779 {
34780 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34781 const char *p = IDENTIFIER_POINTER (id);
34782
34783 if (strcmp ("alloc", p) == 0)
34784 kind = GOMP_MAP_ALLOC;
34785 else if (strcmp ("to", p) == 0)
34786 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34787 else if (strcmp ("from", p) == 0)
34788 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34789 else if (strcmp ("tofrom", p) == 0)
34790 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34791 else if (strcmp ("release", p) == 0)
34792 kind = GOMP_MAP_RELEASE;
34793 else
34794 {
34795 cp_parser_error (parser, "invalid map kind");
34796 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34797 /*or_comma=*/false,
34798 /*consume_paren=*/true);
34799 return list;
34800 }
34801 cp_lexer_consume_token (parser->lexer);
34802 cp_lexer_consume_token (parser->lexer);
34803 }
34804 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34805 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34806 {
34807 kind = GOMP_MAP_DELETE;
34808 cp_lexer_consume_token (parser->lexer);
34809 cp_lexer_consume_token (parser->lexer);
34810 }
34811
34812 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34813 NULL);
34814
34815 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34816 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34817
34818 return nlist;
34819 }
34820
34821 /* OpenMP 4.0:
34822 device ( expression ) */
34823
34824 static tree
34825 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34826 location_t location)
34827 {
34828 tree t, c;
34829
34830 matching_parens parens;
34831 if (!parens.require_open (parser))
34832 return list;
34833
34834 t = cp_parser_assignment_expression (parser);
34835
34836 if (t == error_mark_node
34837 || !parens.require_close (parser))
34838 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34839 /*or_comma=*/false,
34840 /*consume_paren=*/true);
34841
34842 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34843 "device", location);
34844
34845 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34846 OMP_CLAUSE_DEVICE_ID (c) = t;
34847 OMP_CLAUSE_CHAIN (c) = list;
34848
34849 return c;
34850 }
34851
34852 /* OpenMP 4.0:
34853 dist_schedule ( static )
34854 dist_schedule ( static , expression ) */
34855
34856 static tree
34857 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34858 location_t location)
34859 {
34860 tree c, t;
34861
34862 matching_parens parens;
34863 if (!parens.require_open (parser))
34864 return list;
34865
34866 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34867
34868 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34869 goto invalid_kind;
34870 cp_lexer_consume_token (parser->lexer);
34871
34872 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34873 {
34874 cp_lexer_consume_token (parser->lexer);
34875
34876 t = cp_parser_assignment_expression (parser);
34877
34878 if (t == error_mark_node)
34879 goto resync_fail;
34880 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34881
34882 if (!parens.require_close (parser))
34883 goto resync_fail;
34884 }
34885 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34886 goto resync_fail;
34887
34888 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34889 location);
34890 OMP_CLAUSE_CHAIN (c) = list;
34891 return c;
34892
34893 invalid_kind:
34894 cp_parser_error (parser, "invalid dist_schedule kind");
34895 resync_fail:
34896 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34897 /*or_comma=*/false,
34898 /*consume_paren=*/true);
34899 return list;
34900 }
34901
34902 /* OpenMP 4.0:
34903 proc_bind ( proc-bind-kind )
34904
34905 proc-bind-kind:
34906 master | close | spread */
34907
34908 static tree
34909 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34910 location_t location)
34911 {
34912 tree c;
34913 enum omp_clause_proc_bind_kind kind;
34914
34915 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34916 return list;
34917
34918 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34919 {
34920 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34921 const char *p = IDENTIFIER_POINTER (id);
34922
34923 if (strcmp ("master", p) == 0)
34924 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34925 else if (strcmp ("close", p) == 0)
34926 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34927 else if (strcmp ("spread", p) == 0)
34928 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34929 else
34930 goto invalid_kind;
34931 }
34932 else
34933 goto invalid_kind;
34934
34935 cp_lexer_consume_token (parser->lexer);
34936 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34937 goto resync_fail;
34938
34939 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34940 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34941 location);
34942 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34943 OMP_CLAUSE_CHAIN (c) = list;
34944 return c;
34945
34946 invalid_kind:
34947 cp_parser_error (parser, "invalid depend kind");
34948 resync_fail:
34949 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34950 /*or_comma=*/false,
34951 /*consume_paren=*/true);
34952 return list;
34953 }
34954
34955 /* OpenACC:
34956 async [( int-expr )] */
34957
34958 static tree
34959 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34960 {
34961 tree c, t;
34962 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34963
34964 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34965
34966 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34967 {
34968 matching_parens parens;
34969 parens.consume_open (parser);
34970
34971 t = cp_parser_expression (parser);
34972 if (t == error_mark_node
34973 || !parens.require_close (parser))
34974 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34975 /*or_comma=*/false,
34976 /*consume_paren=*/true);
34977 }
34978
34979 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34980
34981 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34982 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34983 OMP_CLAUSE_CHAIN (c) = list;
34984 list = c;
34985
34986 return list;
34987 }
34988
34989 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34990 is a bitmask in MASK. Return the list of clauses found. */
34991
34992 static tree
34993 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34994 const char *where, cp_token *pragma_tok,
34995 bool finish_p = true)
34996 {
34997 tree clauses = NULL;
34998 bool first = true;
34999
35000 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35001 {
35002 location_t here;
35003 pragma_omp_clause c_kind;
35004 omp_clause_code code;
35005 const char *c_name;
35006 tree prev = clauses;
35007
35008 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35009 cp_lexer_consume_token (parser->lexer);
35010
35011 here = cp_lexer_peek_token (parser->lexer)->location;
35012 c_kind = cp_parser_omp_clause_name (parser);
35013
35014 switch (c_kind)
35015 {
35016 case PRAGMA_OACC_CLAUSE_ASYNC:
35017 clauses = cp_parser_oacc_clause_async (parser, clauses);
35018 c_name = "async";
35019 break;
35020 case PRAGMA_OACC_CLAUSE_AUTO:
35021 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
35022 clauses);
35023 c_name = "auto";
35024 break;
35025 case PRAGMA_OACC_CLAUSE_COLLAPSE:
35026 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
35027 c_name = "collapse";
35028 break;
35029 case PRAGMA_OACC_CLAUSE_COPY:
35030 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35031 c_name = "copy";
35032 break;
35033 case PRAGMA_OACC_CLAUSE_COPYIN:
35034 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35035 c_name = "copyin";
35036 break;
35037 case PRAGMA_OACC_CLAUSE_COPYOUT:
35038 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35039 c_name = "copyout";
35040 break;
35041 case PRAGMA_OACC_CLAUSE_CREATE:
35042 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35043 c_name = "create";
35044 break;
35045 case PRAGMA_OACC_CLAUSE_DELETE:
35046 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35047 c_name = "delete";
35048 break;
35049 case PRAGMA_OMP_CLAUSE_DEFAULT:
35050 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
35051 c_name = "default";
35052 break;
35053 case PRAGMA_OACC_CLAUSE_DEVICE:
35054 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35055 c_name = "device";
35056 break;
35057 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
35058 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
35059 c_name = "deviceptr";
35060 break;
35061 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
35062 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35063 c_name = "device_resident";
35064 break;
35065 case PRAGMA_OACC_CLAUSE_FINALIZE:
35066 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
35067 clauses);
35068 c_name = "finalize";
35069 break;
35070 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
35071 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35072 clauses);
35073 c_name = "firstprivate";
35074 break;
35075 case PRAGMA_OACC_CLAUSE_GANG:
35076 c_name = "gang";
35077 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
35078 c_name, clauses);
35079 break;
35080 case PRAGMA_OACC_CLAUSE_HOST:
35081 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35082 c_name = "host";
35083 break;
35084 case PRAGMA_OACC_CLAUSE_IF:
35085 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
35086 c_name = "if";
35087 break;
35088 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
35089 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
35090 clauses);
35091 c_name = "if_present";
35092 break;
35093 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
35094 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
35095 clauses);
35096 c_name = "independent";
35097 break;
35098 case PRAGMA_OACC_CLAUSE_LINK:
35099 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35100 c_name = "link";
35101 break;
35102 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
35103 code = OMP_CLAUSE_NUM_GANGS;
35104 c_name = "num_gangs";
35105 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35106 clauses);
35107 break;
35108 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
35109 c_name = "num_workers";
35110 code = OMP_CLAUSE_NUM_WORKERS;
35111 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35112 clauses);
35113 break;
35114 case PRAGMA_OACC_CLAUSE_PRESENT:
35115 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35116 c_name = "present";
35117 break;
35118 case PRAGMA_OACC_CLAUSE_PRIVATE:
35119 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35120 clauses);
35121 c_name = "private";
35122 break;
35123 case PRAGMA_OACC_CLAUSE_REDUCTION:
35124 clauses
35125 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35126 false, clauses);
35127 c_name = "reduction";
35128 break;
35129 case PRAGMA_OACC_CLAUSE_SEQ:
35130 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
35131 clauses);
35132 c_name = "seq";
35133 break;
35134 case PRAGMA_OACC_CLAUSE_TILE:
35135 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
35136 c_name = "tile";
35137 break;
35138 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
35139 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35140 clauses);
35141 c_name = "use_device";
35142 break;
35143 case PRAGMA_OACC_CLAUSE_VECTOR:
35144 c_name = "vector";
35145 clauses = cp_parser_oacc_shape_clause (parser, here,
35146 OMP_CLAUSE_VECTOR,
35147 c_name, clauses);
35148 break;
35149 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
35150 c_name = "vector_length";
35151 code = OMP_CLAUSE_VECTOR_LENGTH;
35152 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35153 clauses);
35154 break;
35155 case PRAGMA_OACC_CLAUSE_WAIT:
35156 clauses = cp_parser_oacc_clause_wait (parser, clauses);
35157 c_name = "wait";
35158 break;
35159 case PRAGMA_OACC_CLAUSE_WORKER:
35160 c_name = "worker";
35161 clauses = cp_parser_oacc_shape_clause (parser, here,
35162 OMP_CLAUSE_WORKER,
35163 c_name, clauses);
35164 break;
35165 default:
35166 cp_parser_error (parser, "expected %<#pragma acc%> clause");
35167 goto saw_error;
35168 }
35169
35170 first = false;
35171
35172 if (((mask >> c_kind) & 1) == 0)
35173 {
35174 /* Remove the invalid clause(s) from the list to avoid
35175 confusing the rest of the compiler. */
35176 clauses = prev;
35177 error_at (here, "%qs is not valid for %qs", c_name, where);
35178 }
35179 }
35180
35181 saw_error:
35182 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35183
35184 if (finish_p)
35185 return finish_omp_clauses (clauses, C_ORT_ACC);
35186
35187 return clauses;
35188 }
35189
35190 /* Parse all OpenMP clauses. The set clauses allowed by the directive
35191 is a bitmask in MASK. Return the list of clauses found; the result
35192 of clause default goes in *pdefault. */
35193
35194 static tree
35195 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
35196 const char *where, cp_token *pragma_tok,
35197 bool finish_p = true)
35198 {
35199 tree clauses = NULL;
35200 bool first = true;
35201 cp_token *token = NULL;
35202
35203 /* Don't create location wrapper nodes within OpenMP clauses. */
35204 auto_suppress_location_wrappers sentinel;
35205
35206 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35207 {
35208 pragma_omp_clause c_kind;
35209 const char *c_name;
35210 tree prev = clauses;
35211
35212 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35213 cp_lexer_consume_token (parser->lexer);
35214
35215 token = cp_lexer_peek_token (parser->lexer);
35216 c_kind = cp_parser_omp_clause_name (parser);
35217
35218 switch (c_kind)
35219 {
35220 case PRAGMA_OMP_CLAUSE_COLLAPSE:
35221 clauses = cp_parser_omp_clause_collapse (parser, clauses,
35222 token->location);
35223 c_name = "collapse";
35224 break;
35225 case PRAGMA_OMP_CLAUSE_COPYIN:
35226 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
35227 c_name = "copyin";
35228 break;
35229 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
35230 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
35231 clauses);
35232 c_name = "copyprivate";
35233 break;
35234 case PRAGMA_OMP_CLAUSE_DEFAULT:
35235 clauses = cp_parser_omp_clause_default (parser, clauses,
35236 token->location, false);
35237 c_name = "default";
35238 break;
35239 case PRAGMA_OMP_CLAUSE_FINAL:
35240 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
35241 c_name = "final";
35242 break;
35243 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
35244 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35245 clauses);
35246 c_name = "firstprivate";
35247 break;
35248 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
35249 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
35250 token->location);
35251 c_name = "grainsize";
35252 break;
35253 case PRAGMA_OMP_CLAUSE_HINT:
35254 clauses = cp_parser_omp_clause_hint (parser, clauses,
35255 token->location);
35256 c_name = "hint";
35257 break;
35258 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
35259 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
35260 token->location);
35261 c_name = "defaultmap";
35262 break;
35263 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
35264 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35265 clauses);
35266 c_name = "use_device_ptr";
35267 break;
35268 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
35269 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
35270 clauses);
35271 c_name = "is_device_ptr";
35272 break;
35273 case PRAGMA_OMP_CLAUSE_IF:
35274 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
35275 true);
35276 c_name = "if";
35277 break;
35278 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
35279 clauses
35280 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
35281 true, clauses);
35282 c_name = "in_reduction";
35283 break;
35284 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
35285 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
35286 c_name = "lastprivate";
35287 break;
35288 case PRAGMA_OMP_CLAUSE_MERGEABLE:
35289 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
35290 token->location);
35291 c_name = "mergeable";
35292 break;
35293 case PRAGMA_OMP_CLAUSE_NOWAIT:
35294 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
35295 c_name = "nowait";
35296 break;
35297 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
35298 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
35299 token->location);
35300 c_name = "num_tasks";
35301 break;
35302 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
35303 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
35304 token->location);
35305 c_name = "num_threads";
35306 break;
35307 case PRAGMA_OMP_CLAUSE_ORDERED:
35308 clauses = cp_parser_omp_clause_ordered (parser, clauses,
35309 token->location);
35310 c_name = "ordered";
35311 break;
35312 case PRAGMA_OMP_CLAUSE_PRIORITY:
35313 clauses = cp_parser_omp_clause_priority (parser, clauses,
35314 token->location);
35315 c_name = "priority";
35316 break;
35317 case PRAGMA_OMP_CLAUSE_PRIVATE:
35318 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35319 clauses);
35320 c_name = "private";
35321 break;
35322 case PRAGMA_OMP_CLAUSE_REDUCTION:
35323 clauses
35324 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35325 true, clauses);
35326 c_name = "reduction";
35327 break;
35328 case PRAGMA_OMP_CLAUSE_SCHEDULE:
35329 clauses = cp_parser_omp_clause_schedule (parser, clauses,
35330 token->location);
35331 c_name = "schedule";
35332 break;
35333 case PRAGMA_OMP_CLAUSE_SHARED:
35334 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
35335 clauses);
35336 c_name = "shared";
35337 break;
35338 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
35339 clauses
35340 = cp_parser_omp_clause_reduction (parser,
35341 OMP_CLAUSE_TASK_REDUCTION,
35342 true, clauses);
35343 c_name = "task_reduction";
35344 break;
35345 case PRAGMA_OMP_CLAUSE_UNTIED:
35346 clauses = cp_parser_omp_clause_untied (parser, clauses,
35347 token->location);
35348 c_name = "untied";
35349 break;
35350 case PRAGMA_OMP_CLAUSE_INBRANCH:
35351 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
35352 clauses, token->location);
35353 c_name = "inbranch";
35354 break;
35355 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
35356 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
35357 clauses);
35358 c_name = "nontemporal";
35359 break;
35360 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
35361 clauses = cp_parser_omp_clause_branch (parser,
35362 OMP_CLAUSE_NOTINBRANCH,
35363 clauses, token->location);
35364 c_name = "notinbranch";
35365 break;
35366 case PRAGMA_OMP_CLAUSE_PARALLEL:
35367 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
35368 clauses, token->location);
35369 c_name = "parallel";
35370 if (!first)
35371 {
35372 clause_not_first:
35373 error_at (token->location, "%qs must be the first clause of %qs",
35374 c_name, where);
35375 clauses = prev;
35376 }
35377 break;
35378 case PRAGMA_OMP_CLAUSE_FOR:
35379 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
35380 clauses, token->location);
35381 c_name = "for";
35382 if (!first)
35383 goto clause_not_first;
35384 break;
35385 case PRAGMA_OMP_CLAUSE_SECTIONS:
35386 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
35387 clauses, token->location);
35388 c_name = "sections";
35389 if (!first)
35390 goto clause_not_first;
35391 break;
35392 case PRAGMA_OMP_CLAUSE_TASKGROUP:
35393 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
35394 clauses, token->location);
35395 c_name = "taskgroup";
35396 if (!first)
35397 goto clause_not_first;
35398 break;
35399 case PRAGMA_OMP_CLAUSE_LINK:
35400 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
35401 c_name = "to";
35402 break;
35403 case PRAGMA_OMP_CLAUSE_TO:
35404 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
35405 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35406 clauses);
35407 else
35408 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
35409 c_name = "to";
35410 break;
35411 case PRAGMA_OMP_CLAUSE_FROM:
35412 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
35413 c_name = "from";
35414 break;
35415 case PRAGMA_OMP_CLAUSE_UNIFORM:
35416 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
35417 clauses);
35418 c_name = "uniform";
35419 break;
35420 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
35421 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
35422 token->location);
35423 c_name = "num_teams";
35424 break;
35425 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
35426 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
35427 token->location);
35428 c_name = "thread_limit";
35429 break;
35430 case PRAGMA_OMP_CLAUSE_ALIGNED:
35431 clauses = cp_parser_omp_clause_aligned (parser, clauses);
35432 c_name = "aligned";
35433 break;
35434 case PRAGMA_OMP_CLAUSE_LINEAR:
35435 {
35436 bool declare_simd = false;
35437 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
35438 declare_simd = true;
35439 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
35440 }
35441 c_name = "linear";
35442 break;
35443 case PRAGMA_OMP_CLAUSE_DEPEND:
35444 clauses = cp_parser_omp_clause_depend (parser, clauses,
35445 token->location);
35446 c_name = "depend";
35447 break;
35448 case PRAGMA_OMP_CLAUSE_MAP:
35449 clauses = cp_parser_omp_clause_map (parser, clauses);
35450 c_name = "map";
35451 break;
35452 case PRAGMA_OMP_CLAUSE_DEVICE:
35453 clauses = cp_parser_omp_clause_device (parser, clauses,
35454 token->location);
35455 c_name = "device";
35456 break;
35457 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
35458 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
35459 token->location);
35460 c_name = "dist_schedule";
35461 break;
35462 case PRAGMA_OMP_CLAUSE_PROC_BIND:
35463 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
35464 token->location);
35465 c_name = "proc_bind";
35466 break;
35467 case PRAGMA_OMP_CLAUSE_SAFELEN:
35468 clauses = cp_parser_omp_clause_safelen (parser, clauses,
35469 token->location);
35470 c_name = "safelen";
35471 break;
35472 case PRAGMA_OMP_CLAUSE_SIMDLEN:
35473 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
35474 token->location);
35475 c_name = "simdlen";
35476 break;
35477 case PRAGMA_OMP_CLAUSE_NOGROUP:
35478 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
35479 token->location);
35480 c_name = "nogroup";
35481 break;
35482 case PRAGMA_OMP_CLAUSE_THREADS:
35483 clauses
35484 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
35485 clauses, token->location);
35486 c_name = "threads";
35487 break;
35488 case PRAGMA_OMP_CLAUSE_SIMD:
35489 clauses
35490 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
35491 clauses, token->location);
35492 c_name = "simd";
35493 break;
35494 default:
35495 cp_parser_error (parser, "expected %<#pragma omp%> clause");
35496 goto saw_error;
35497 }
35498
35499 first = false;
35500
35501 if (((mask >> c_kind) & 1) == 0)
35502 {
35503 /* Remove the invalid clause(s) from the list to avoid
35504 confusing the rest of the compiler. */
35505 clauses = prev;
35506 error_at (token->location, "%qs is not valid for %qs", c_name, where);
35507 }
35508 }
35509 saw_error:
35510 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35511 if (finish_p)
35512 {
35513 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
35514 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
35515 else
35516 return finish_omp_clauses (clauses, C_ORT_OMP);
35517 }
35518 return clauses;
35519 }
35520
35521 /* OpenMP 2.5:
35522 structured-block:
35523 statement
35524
35525 In practice, we're also interested in adding the statement to an
35526 outer node. So it is convenient if we work around the fact that
35527 cp_parser_statement calls add_stmt. */
35528
35529 static unsigned
35530 cp_parser_begin_omp_structured_block (cp_parser *parser)
35531 {
35532 unsigned save = parser->in_statement;
35533
35534 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35535 This preserves the "not within loop or switch" style error messages
35536 for nonsense cases like
35537 void foo() {
35538 #pragma omp single
35539 break;
35540 }
35541 */
35542 if (parser->in_statement)
35543 parser->in_statement = IN_OMP_BLOCK;
35544
35545 return save;
35546 }
35547
35548 static void
35549 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
35550 {
35551 parser->in_statement = save;
35552 }
35553
35554 static tree
35555 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
35556 {
35557 tree stmt = begin_omp_structured_block ();
35558 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35559
35560 cp_parser_statement (parser, NULL_TREE, false, if_p);
35561
35562 cp_parser_end_omp_structured_block (parser, save);
35563 return finish_omp_structured_block (stmt);
35564 }
35565
35566 /* OpenMP 2.5:
35567 # pragma omp atomic new-line
35568 expression-stmt
35569
35570 expression-stmt:
35571 x binop= expr | x++ | ++x | x-- | --x
35572 binop:
35573 +, *, -, /, &, ^, |, <<, >>
35574
35575 where x is an lvalue expression with scalar type.
35576
35577 OpenMP 3.1:
35578 # pragma omp atomic new-line
35579 update-stmt
35580
35581 # pragma omp atomic read new-line
35582 read-stmt
35583
35584 # pragma omp atomic write new-line
35585 write-stmt
35586
35587 # pragma omp atomic update new-line
35588 update-stmt
35589
35590 # pragma omp atomic capture new-line
35591 capture-stmt
35592
35593 # pragma omp atomic capture new-line
35594 capture-block
35595
35596 read-stmt:
35597 v = x
35598 write-stmt:
35599 x = expr
35600 update-stmt:
35601 expression-stmt | x = x binop expr
35602 capture-stmt:
35603 v = expression-stmt
35604 capture-block:
35605 { v = x; update-stmt; } | { update-stmt; v = x; }
35606
35607 OpenMP 4.0:
35608 update-stmt:
35609 expression-stmt | x = x binop expr | x = expr binop x
35610 capture-stmt:
35611 v = update-stmt
35612 capture-block:
35613 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35614
35615 where x and v are lvalue expressions with scalar type. */
35616
35617 static void
35618 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
35619 {
35620 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
35621 tree rhs1 = NULL_TREE, orig_lhs;
35622 location_t loc = pragma_tok->location;
35623 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
35624 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
35625 bool structured_block = false;
35626 bool first = true;
35627 tree clauses = NULL_TREE;
35628
35629 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35630 {
35631 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35632 cp_lexer_consume_token (parser->lexer);
35633
35634 first = false;
35635
35636 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35637 {
35638 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35639 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
35640 const char *p = IDENTIFIER_POINTER (id);
35641 enum tree_code new_code = ERROR_MARK;
35642 enum omp_memory_order new_memory_order
35643 = OMP_MEMORY_ORDER_UNSPECIFIED;
35644
35645 if (!strcmp (p, "read"))
35646 new_code = OMP_ATOMIC_READ;
35647 else if (!strcmp (p, "write"))
35648 new_code = NOP_EXPR;
35649 else if (!strcmp (p, "update"))
35650 new_code = OMP_ATOMIC;
35651 else if (!strcmp (p, "capture"))
35652 new_code = OMP_ATOMIC_CAPTURE_NEW;
35653 else if (!strcmp (p, "seq_cst"))
35654 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35655 else if (!strcmp (p, "acq_rel"))
35656 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35657 else if (!strcmp (p, "release"))
35658 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
35659 else if (!strcmp (p, "acquire"))
35660 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35661 else if (!strcmp (p, "relaxed"))
35662 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
35663 else if (!strcmp (p, "hint"))
35664 {
35665 cp_lexer_consume_token (parser->lexer);
35666 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
35667 continue;
35668 }
35669 else
35670 {
35671 p = NULL;
35672 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35673 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35674 "%<release%>, %<relaxed%> or %<hint%> clause");
35675 }
35676 if (p)
35677 {
35678 if (new_code != ERROR_MARK)
35679 {
35680 if (code != ERROR_MARK)
35681 error_at (cloc, "too many atomic clauses");
35682 else
35683 code = new_code;
35684 }
35685 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35686 {
35687 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35688 error_at (cloc, "too many memory order clauses");
35689 else
35690 memory_order = new_memory_order;
35691 }
35692 cp_lexer_consume_token (parser->lexer);
35693 continue;
35694 }
35695 }
35696 break;
35697 }
35698 cp_parser_require_pragma_eol (parser, pragma_tok);
35699
35700 if (code == ERROR_MARK)
35701 code = OMP_ATOMIC;
35702 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35703 {
35704 omp_requires_mask
35705 = (enum omp_requires) (omp_requires_mask
35706 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35707 switch ((enum omp_memory_order)
35708 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35709 {
35710 case OMP_MEMORY_ORDER_UNSPECIFIED:
35711 case OMP_MEMORY_ORDER_RELAXED:
35712 memory_order = OMP_MEMORY_ORDER_RELAXED;
35713 break;
35714 case OMP_MEMORY_ORDER_SEQ_CST:
35715 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35716 break;
35717 case OMP_MEMORY_ORDER_ACQ_REL:
35718 switch (code)
35719 {
35720 case OMP_ATOMIC_READ:
35721 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35722 break;
35723 case NOP_EXPR: /* atomic write */
35724 case OMP_ATOMIC:
35725 memory_order = OMP_MEMORY_ORDER_RELEASE;
35726 break;
35727 default:
35728 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35729 break;
35730 }
35731 break;
35732 default:
35733 gcc_unreachable ();
35734 }
35735 }
35736 else
35737 switch (code)
35738 {
35739 case OMP_ATOMIC_READ:
35740 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35741 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35742 {
35743 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35744 "%<acq_rel%> or %<release%> clauses");
35745 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35746 }
35747 break;
35748 case NOP_EXPR: /* atomic write */
35749 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35750 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35751 {
35752 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35753 "%<acq_rel%> or %<acquire%> clauses");
35754 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35755 }
35756 break;
35757 case OMP_ATOMIC:
35758 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35759 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35760 {
35761 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35762 "%<acq_rel%> or %<acquire%> clauses");
35763 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35764 }
35765 break;
35766 default:
35767 break;
35768 }
35769
35770 switch (code)
35771 {
35772 case OMP_ATOMIC_READ:
35773 case NOP_EXPR: /* atomic write */
35774 v = cp_parser_unary_expression (parser);
35775 if (v == error_mark_node)
35776 goto saw_error;
35777 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35778 goto saw_error;
35779 if (code == NOP_EXPR)
35780 lhs = cp_parser_expression (parser);
35781 else
35782 lhs = cp_parser_unary_expression (parser);
35783 if (lhs == error_mark_node)
35784 goto saw_error;
35785 if (code == NOP_EXPR)
35786 {
35787 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35788 opcode. */
35789 code = OMP_ATOMIC;
35790 rhs = lhs;
35791 lhs = v;
35792 v = NULL_TREE;
35793 }
35794 goto done;
35795 case OMP_ATOMIC_CAPTURE_NEW:
35796 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35797 {
35798 cp_lexer_consume_token (parser->lexer);
35799 structured_block = true;
35800 }
35801 else
35802 {
35803 v = cp_parser_unary_expression (parser);
35804 if (v == error_mark_node)
35805 goto saw_error;
35806 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35807 goto saw_error;
35808 }
35809 default:
35810 break;
35811 }
35812
35813 restart:
35814 lhs = cp_parser_unary_expression (parser);
35815 orig_lhs = lhs;
35816 switch (TREE_CODE (lhs))
35817 {
35818 case ERROR_MARK:
35819 goto saw_error;
35820
35821 case POSTINCREMENT_EXPR:
35822 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35823 code = OMP_ATOMIC_CAPTURE_OLD;
35824 /* FALLTHROUGH */
35825 case PREINCREMENT_EXPR:
35826 lhs = TREE_OPERAND (lhs, 0);
35827 opcode = PLUS_EXPR;
35828 rhs = integer_one_node;
35829 break;
35830
35831 case POSTDECREMENT_EXPR:
35832 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35833 code = OMP_ATOMIC_CAPTURE_OLD;
35834 /* FALLTHROUGH */
35835 case PREDECREMENT_EXPR:
35836 lhs = TREE_OPERAND (lhs, 0);
35837 opcode = MINUS_EXPR;
35838 rhs = integer_one_node;
35839 break;
35840
35841 case COMPOUND_EXPR:
35842 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35843 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35844 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35845 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35846 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35847 (TREE_OPERAND (lhs, 1), 0), 0)))
35848 == BOOLEAN_TYPE)
35849 /* Undo effects of boolean_increment for post {in,de}crement. */
35850 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35851 /* FALLTHRU */
35852 case MODIFY_EXPR:
35853 if (TREE_CODE (lhs) == MODIFY_EXPR
35854 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35855 {
35856 /* Undo effects of boolean_increment. */
35857 if (integer_onep (TREE_OPERAND (lhs, 1)))
35858 {
35859 /* This is pre or post increment. */
35860 rhs = TREE_OPERAND (lhs, 1);
35861 lhs = TREE_OPERAND (lhs, 0);
35862 opcode = NOP_EXPR;
35863 if (code == OMP_ATOMIC_CAPTURE_NEW
35864 && !structured_block
35865 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35866 code = OMP_ATOMIC_CAPTURE_OLD;
35867 break;
35868 }
35869 }
35870 /* FALLTHRU */
35871 default:
35872 switch (cp_lexer_peek_token (parser->lexer)->type)
35873 {
35874 case CPP_MULT_EQ:
35875 opcode = MULT_EXPR;
35876 break;
35877 case CPP_DIV_EQ:
35878 opcode = TRUNC_DIV_EXPR;
35879 break;
35880 case CPP_PLUS_EQ:
35881 opcode = PLUS_EXPR;
35882 break;
35883 case CPP_MINUS_EQ:
35884 opcode = MINUS_EXPR;
35885 break;
35886 case CPP_LSHIFT_EQ:
35887 opcode = LSHIFT_EXPR;
35888 break;
35889 case CPP_RSHIFT_EQ:
35890 opcode = RSHIFT_EXPR;
35891 break;
35892 case CPP_AND_EQ:
35893 opcode = BIT_AND_EXPR;
35894 break;
35895 case CPP_OR_EQ:
35896 opcode = BIT_IOR_EXPR;
35897 break;
35898 case CPP_XOR_EQ:
35899 opcode = BIT_XOR_EXPR;
35900 break;
35901 case CPP_EQ:
35902 enum cp_parser_prec oprec;
35903 cp_token *token;
35904 cp_lexer_consume_token (parser->lexer);
35905 cp_parser_parse_tentatively (parser);
35906 rhs1 = cp_parser_simple_cast_expression (parser);
35907 if (rhs1 == error_mark_node)
35908 {
35909 cp_parser_abort_tentative_parse (parser);
35910 cp_parser_simple_cast_expression (parser);
35911 goto saw_error;
35912 }
35913 token = cp_lexer_peek_token (parser->lexer);
35914 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35915 {
35916 cp_parser_abort_tentative_parse (parser);
35917 cp_parser_parse_tentatively (parser);
35918 rhs = cp_parser_binary_expression (parser, false, true,
35919 PREC_NOT_OPERATOR, NULL);
35920 if (rhs == error_mark_node)
35921 {
35922 cp_parser_abort_tentative_parse (parser);
35923 cp_parser_binary_expression (parser, false, true,
35924 PREC_NOT_OPERATOR, NULL);
35925 goto saw_error;
35926 }
35927 switch (TREE_CODE (rhs))
35928 {
35929 case MULT_EXPR:
35930 case TRUNC_DIV_EXPR:
35931 case RDIV_EXPR:
35932 case PLUS_EXPR:
35933 case MINUS_EXPR:
35934 case LSHIFT_EXPR:
35935 case RSHIFT_EXPR:
35936 case BIT_AND_EXPR:
35937 case BIT_IOR_EXPR:
35938 case BIT_XOR_EXPR:
35939 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35940 {
35941 if (cp_parser_parse_definitely (parser))
35942 {
35943 opcode = TREE_CODE (rhs);
35944 rhs1 = TREE_OPERAND (rhs, 0);
35945 rhs = TREE_OPERAND (rhs, 1);
35946 goto stmt_done;
35947 }
35948 else
35949 goto saw_error;
35950 }
35951 break;
35952 default:
35953 break;
35954 }
35955 cp_parser_abort_tentative_parse (parser);
35956 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35957 {
35958 rhs = cp_parser_expression (parser);
35959 if (rhs == error_mark_node)
35960 goto saw_error;
35961 opcode = NOP_EXPR;
35962 rhs1 = NULL_TREE;
35963 goto stmt_done;
35964 }
35965 cp_parser_error (parser,
35966 "invalid form of %<#pragma omp atomic%>");
35967 goto saw_error;
35968 }
35969 if (!cp_parser_parse_definitely (parser))
35970 goto saw_error;
35971 switch (token->type)
35972 {
35973 case CPP_SEMICOLON:
35974 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35975 {
35976 code = OMP_ATOMIC_CAPTURE_OLD;
35977 v = lhs;
35978 lhs = NULL_TREE;
35979 lhs1 = rhs1;
35980 rhs1 = NULL_TREE;
35981 cp_lexer_consume_token (parser->lexer);
35982 goto restart;
35983 }
35984 else if (structured_block)
35985 {
35986 opcode = NOP_EXPR;
35987 rhs = rhs1;
35988 rhs1 = NULL_TREE;
35989 goto stmt_done;
35990 }
35991 cp_parser_error (parser,
35992 "invalid form of %<#pragma omp atomic%>");
35993 goto saw_error;
35994 case CPP_MULT:
35995 opcode = MULT_EXPR;
35996 break;
35997 case CPP_DIV:
35998 opcode = TRUNC_DIV_EXPR;
35999 break;
36000 case CPP_PLUS:
36001 opcode = PLUS_EXPR;
36002 break;
36003 case CPP_MINUS:
36004 opcode = MINUS_EXPR;
36005 break;
36006 case CPP_LSHIFT:
36007 opcode = LSHIFT_EXPR;
36008 break;
36009 case CPP_RSHIFT:
36010 opcode = RSHIFT_EXPR;
36011 break;
36012 case CPP_AND:
36013 opcode = BIT_AND_EXPR;
36014 break;
36015 case CPP_OR:
36016 opcode = BIT_IOR_EXPR;
36017 break;
36018 case CPP_XOR:
36019 opcode = BIT_XOR_EXPR;
36020 break;
36021 default:
36022 cp_parser_error (parser,
36023 "invalid operator for %<#pragma omp atomic%>");
36024 goto saw_error;
36025 }
36026 oprec = TOKEN_PRECEDENCE (token);
36027 gcc_assert (oprec != PREC_NOT_OPERATOR);
36028 if (commutative_tree_code (opcode))
36029 oprec = (enum cp_parser_prec) (oprec - 1);
36030 cp_lexer_consume_token (parser->lexer);
36031 rhs = cp_parser_binary_expression (parser, false, false,
36032 oprec, NULL);
36033 if (rhs == error_mark_node)
36034 goto saw_error;
36035 goto stmt_done;
36036 /* FALLTHROUGH */
36037 default:
36038 cp_parser_error (parser,
36039 "invalid operator for %<#pragma omp atomic%>");
36040 goto saw_error;
36041 }
36042 cp_lexer_consume_token (parser->lexer);
36043
36044 rhs = cp_parser_expression (parser);
36045 if (rhs == error_mark_node)
36046 goto saw_error;
36047 break;
36048 }
36049 stmt_done:
36050 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
36051 {
36052 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
36053 goto saw_error;
36054 v = cp_parser_unary_expression (parser);
36055 if (v == error_mark_node)
36056 goto saw_error;
36057 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36058 goto saw_error;
36059 lhs1 = cp_parser_unary_expression (parser);
36060 if (lhs1 == error_mark_node)
36061 goto saw_error;
36062 }
36063 if (structured_block)
36064 {
36065 cp_parser_consume_semicolon_at_end_of_statement (parser);
36066 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
36067 }
36068 done:
36069 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36070 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
36071 rhs1, clauses, memory_order);
36072 if (!structured_block)
36073 cp_parser_consume_semicolon_at_end_of_statement (parser);
36074 return;
36075
36076 saw_error:
36077 cp_parser_skip_to_end_of_block_or_statement (parser);
36078 if (structured_block)
36079 {
36080 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36081 cp_lexer_consume_token (parser->lexer);
36082 else if (code == OMP_ATOMIC_CAPTURE_NEW)
36083 {
36084 cp_parser_skip_to_end_of_block_or_statement (parser);
36085 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36086 cp_lexer_consume_token (parser->lexer);
36087 }
36088 }
36089 }
36090
36091
36092 /* OpenMP 2.5:
36093 # pragma omp barrier new-line */
36094
36095 static void
36096 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
36097 {
36098 cp_parser_require_pragma_eol (parser, pragma_tok);
36099 finish_omp_barrier ();
36100 }
36101
36102 /* OpenMP 2.5:
36103 # pragma omp critical [(name)] new-line
36104 structured-block
36105
36106 OpenMP 4.5:
36107 # pragma omp critical [(name) [hint(expression)]] new-line
36108 structured-block */
36109
36110 #define OMP_CRITICAL_CLAUSE_MASK \
36111 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
36112
36113 static tree
36114 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36115 {
36116 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
36117
36118 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36119 {
36120 matching_parens parens;
36121 parens.consume_open (parser);
36122
36123 name = cp_parser_identifier (parser);
36124
36125 if (name == error_mark_node
36126 || !parens.require_close (parser))
36127 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36128 /*or_comma=*/false,
36129 /*consume_paren=*/true);
36130 if (name == error_mark_node)
36131 name = NULL;
36132
36133 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
36134 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
36135 cp_lexer_consume_token (parser->lexer);
36136
36137 clauses = cp_parser_omp_all_clauses (parser,
36138 OMP_CRITICAL_CLAUSE_MASK,
36139 "#pragma omp critical", pragma_tok);
36140 }
36141 else
36142 cp_parser_require_pragma_eol (parser, pragma_tok);
36143
36144 stmt = cp_parser_omp_structured_block (parser, if_p);
36145 return c_finish_omp_critical (input_location, stmt, name, clauses);
36146 }
36147
36148 /* OpenMP 5.0:
36149 # pragma omp depobj ( depobj ) depobj-clause new-line
36150
36151 depobj-clause:
36152 depend (dependence-type : locator)
36153 destroy
36154 update (dependence-type)
36155
36156 dependence-type:
36157 in
36158 out
36159 inout
36160 mutexinout */
36161
36162 static void
36163 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
36164 {
36165 location_t loc = pragma_tok->location;
36166 matching_parens parens;
36167 if (!parens.require_open (parser))
36168 {
36169 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36170 return;
36171 }
36172
36173 tree depobj = cp_parser_assignment_expression (parser);
36174
36175 if (!parens.require_close (parser))
36176 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36177 /*or_comma=*/false,
36178 /*consume_paren=*/true);
36179
36180 tree clause = NULL_TREE;
36181 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
36182 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
36183 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36184 {
36185 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36186 const char *p = IDENTIFIER_POINTER (id);
36187
36188 cp_lexer_consume_token (parser->lexer);
36189 if (!strcmp ("depend", p))
36190 {
36191 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
36192 if (clause)
36193 clause = finish_omp_clauses (clause, C_ORT_OMP);
36194 if (!clause)
36195 clause = error_mark_node;
36196 }
36197 else if (!strcmp ("destroy", p))
36198 kind = OMP_CLAUSE_DEPEND_LAST;
36199 else if (!strcmp ("update", p))
36200 {
36201 matching_parens c_parens;
36202 if (c_parens.require_open (parser))
36203 {
36204 location_t c2_loc
36205 = cp_lexer_peek_token (parser->lexer)->location;
36206 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36207 {
36208 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
36209 const char *p2 = IDENTIFIER_POINTER (id2);
36210
36211 cp_lexer_consume_token (parser->lexer);
36212 if (!strcmp ("in", p2))
36213 kind = OMP_CLAUSE_DEPEND_IN;
36214 else if (!strcmp ("out", p2))
36215 kind = OMP_CLAUSE_DEPEND_OUT;
36216 else if (!strcmp ("inout", p2))
36217 kind = OMP_CLAUSE_DEPEND_INOUT;
36218 else if (!strcmp ("mutexinoutset", p2))
36219 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
36220 }
36221 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
36222 {
36223 clause = error_mark_node;
36224 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
36225 "%<mutexinoutset%>");
36226 }
36227 if (!c_parens.require_close (parser))
36228 cp_parser_skip_to_closing_parenthesis (parser,
36229 /*recovering=*/true,
36230 /*or_comma=*/false,
36231 /*consume_paren=*/true);
36232 }
36233 else
36234 clause = error_mark_node;
36235 }
36236 }
36237 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
36238 {
36239 clause = error_mark_node;
36240 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
36241 }
36242 cp_parser_require_pragma_eol (parser, pragma_tok);
36243
36244 finish_omp_depobj (loc, depobj, kind, clause);
36245 }
36246
36247
36248 /* OpenMP 2.5:
36249 # pragma omp flush flush-vars[opt] new-line
36250
36251 flush-vars:
36252 ( variable-list )
36253
36254 OpenMP 5.0:
36255 # pragma omp flush memory-order-clause new-line */
36256
36257 static void
36258 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
36259 {
36260 enum memmodel mo = MEMMODEL_LAST;
36261 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36262 {
36263 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36264 const char *p = IDENTIFIER_POINTER (id);
36265 if (!strcmp (p, "acq_rel"))
36266 mo = MEMMODEL_ACQ_REL;
36267 else if (!strcmp (p, "release"))
36268 mo = MEMMODEL_RELEASE;
36269 else if (!strcmp (p, "acquire"))
36270 mo = MEMMODEL_ACQUIRE;
36271 else
36272 error_at (cp_lexer_peek_token (parser->lexer)->location,
36273 "expected %<acq_rel%>, %<release%> or %<acquire%>");
36274 cp_lexer_consume_token (parser->lexer);
36275 }
36276 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36277 {
36278 if (mo != MEMMODEL_LAST)
36279 error_at (cp_lexer_peek_token (parser->lexer)->location,
36280 "%<flush%> list specified together with memory order "
36281 "clause");
36282 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36283 }
36284 cp_parser_require_pragma_eol (parser, pragma_tok);
36285
36286 finish_omp_flush (mo);
36287 }
36288
36289 /* Helper function, to parse omp for increment expression. */
36290
36291 static tree
36292 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
36293 {
36294 tree cond = cp_parser_binary_expression (parser, false, true,
36295 PREC_NOT_OPERATOR, NULL);
36296 if (cond == error_mark_node
36297 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36298 {
36299 cp_parser_skip_to_end_of_statement (parser);
36300 return error_mark_node;
36301 }
36302
36303 switch (TREE_CODE (cond))
36304 {
36305 case GT_EXPR:
36306 case GE_EXPR:
36307 case LT_EXPR:
36308 case LE_EXPR:
36309 break;
36310 case NE_EXPR:
36311 if (code != OACC_LOOP)
36312 break;
36313 gcc_fallthrough ();
36314 default:
36315 return error_mark_node;
36316 }
36317
36318 /* If decl is an iterator, preserve LHS and RHS of the relational
36319 expr until finish_omp_for. */
36320 if (decl
36321 && (type_dependent_expression_p (decl)
36322 || CLASS_TYPE_P (TREE_TYPE (decl))))
36323 return cond;
36324
36325 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
36326 TREE_CODE (cond),
36327 TREE_OPERAND (cond, 0), ERROR_MARK,
36328 TREE_OPERAND (cond, 1), ERROR_MARK,
36329 /*overload=*/NULL, tf_warning_or_error);
36330 }
36331
36332 /* Helper function, to parse omp for increment expression. */
36333
36334 static tree
36335 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
36336 {
36337 cp_token *token = cp_lexer_peek_token (parser->lexer);
36338 enum tree_code op;
36339 tree lhs, rhs;
36340 cp_id_kind idk;
36341 bool decl_first;
36342
36343 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36344 {
36345 op = (token->type == CPP_PLUS_PLUS
36346 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
36347 cp_lexer_consume_token (parser->lexer);
36348 lhs = cp_parser_simple_cast_expression (parser);
36349 if (lhs != decl
36350 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36351 return error_mark_node;
36352 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36353 }
36354
36355 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
36356 if (lhs != decl
36357 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36358 return error_mark_node;
36359
36360 token = cp_lexer_peek_token (parser->lexer);
36361 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36362 {
36363 op = (token->type == CPP_PLUS_PLUS
36364 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
36365 cp_lexer_consume_token (parser->lexer);
36366 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36367 }
36368
36369 op = cp_parser_assignment_operator_opt (parser);
36370 if (op == ERROR_MARK)
36371 return error_mark_node;
36372
36373 if (op != NOP_EXPR)
36374 {
36375 rhs = cp_parser_assignment_expression (parser);
36376 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
36377 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36378 }
36379
36380 lhs = cp_parser_binary_expression (parser, false, false,
36381 PREC_ADDITIVE_EXPRESSION, NULL);
36382 token = cp_lexer_peek_token (parser->lexer);
36383 decl_first = (lhs == decl
36384 || (processing_template_decl && cp_tree_equal (lhs, decl)));
36385 if (decl_first)
36386 lhs = NULL_TREE;
36387 if (token->type != CPP_PLUS
36388 && token->type != CPP_MINUS)
36389 return error_mark_node;
36390
36391 do
36392 {
36393 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
36394 cp_lexer_consume_token (parser->lexer);
36395 rhs = cp_parser_binary_expression (parser, false, false,
36396 PREC_ADDITIVE_EXPRESSION, NULL);
36397 token = cp_lexer_peek_token (parser->lexer);
36398 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
36399 {
36400 if (lhs == NULL_TREE)
36401 {
36402 if (op == PLUS_EXPR)
36403 lhs = rhs;
36404 else
36405 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
36406 tf_warning_or_error);
36407 }
36408 else
36409 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
36410 ERROR_MARK, NULL, tf_warning_or_error);
36411 }
36412 }
36413 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
36414
36415 if (!decl_first)
36416 {
36417 if ((rhs != decl
36418 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
36419 || op == MINUS_EXPR)
36420 return error_mark_node;
36421 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
36422 }
36423 else
36424 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
36425
36426 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36427 }
36428
36429 /* Parse the initialization statement of an OpenMP for loop.
36430
36431 Return true if the resulting construct should have an
36432 OMP_CLAUSE_PRIVATE added to it. */
36433
36434 static tree
36435 cp_parser_omp_for_loop_init (cp_parser *parser,
36436 tree &this_pre_body,
36437 releasing_vec &for_block,
36438 tree &init,
36439 tree &orig_init,
36440 tree &decl,
36441 tree &real_decl)
36442 {
36443 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36444 return NULL_TREE;
36445
36446 tree add_private_clause = NULL_TREE;
36447
36448 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36449
36450 init-expr:
36451 var = lb
36452 integer-type var = lb
36453 random-access-iterator-type var = lb
36454 pointer-type var = lb
36455 */
36456 cp_decl_specifier_seq type_specifiers;
36457
36458 /* First, try to parse as an initialized declaration. See
36459 cp_parser_condition, from whence the bulk of this is copied. */
36460
36461 cp_parser_parse_tentatively (parser);
36462 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
36463 /*is_declaration=*/true,
36464 /*is_trailing_return=*/false,
36465 &type_specifiers);
36466 if (cp_parser_parse_definitely (parser))
36467 {
36468 /* If parsing a type specifier seq succeeded, then this
36469 MUST be a initialized declaration. */
36470 tree asm_specification, attributes;
36471 cp_declarator *declarator;
36472
36473 declarator = cp_parser_declarator (parser,
36474 CP_PARSER_DECLARATOR_NAMED,
36475 CP_PARSER_FLAGS_NONE,
36476 /*ctor_dtor_or_conv_p=*/NULL,
36477 /*parenthesized_p=*/NULL,
36478 /*member_p=*/false,
36479 /*friend_p=*/false,
36480 /*static_p=*/false);
36481 attributes = cp_parser_attributes_opt (parser);
36482 asm_specification = cp_parser_asm_specification_opt (parser);
36483
36484 if (declarator == cp_error_declarator)
36485 cp_parser_skip_to_end_of_statement (parser);
36486
36487 else
36488 {
36489 tree pushed_scope, auto_node;
36490
36491 decl = start_decl (declarator, &type_specifiers,
36492 SD_INITIALIZED, attributes,
36493 /*prefix_attributes=*/NULL_TREE,
36494 &pushed_scope);
36495
36496 auto_node = type_uses_auto (TREE_TYPE (decl));
36497 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36498 {
36499 if (cp_lexer_next_token_is (parser->lexer,
36500 CPP_OPEN_PAREN))
36501 error ("parenthesized initialization is not allowed in "
36502 "OpenMP %<for%> loop");
36503 else
36504 /* Trigger an error. */
36505 cp_parser_require (parser, CPP_EQ, RT_EQ);
36506
36507 init = error_mark_node;
36508 cp_parser_skip_to_end_of_statement (parser);
36509 }
36510 else if (CLASS_TYPE_P (TREE_TYPE (decl))
36511 || type_dependent_expression_p (decl)
36512 || auto_node)
36513 {
36514 bool is_direct_init, is_non_constant_init;
36515
36516 init = cp_parser_initializer (parser,
36517 &is_direct_init,
36518 &is_non_constant_init);
36519
36520 if (auto_node)
36521 {
36522 TREE_TYPE (decl)
36523 = do_auto_deduction (TREE_TYPE (decl), init,
36524 auto_node);
36525
36526 if (!CLASS_TYPE_P (TREE_TYPE (decl))
36527 && !type_dependent_expression_p (decl))
36528 goto non_class;
36529 }
36530
36531 cp_finish_decl (decl, init, !is_non_constant_init,
36532 asm_specification,
36533 LOOKUP_ONLYCONVERTING);
36534 orig_init = init;
36535 if (CLASS_TYPE_P (TREE_TYPE (decl)))
36536 {
36537 vec_safe_push (for_block, this_pre_body);
36538 init = NULL_TREE;
36539 }
36540 else
36541 {
36542 init = pop_stmt_list (this_pre_body);
36543 if (init && TREE_CODE (init) == STATEMENT_LIST)
36544 {
36545 tree_stmt_iterator i = tsi_start (init);
36546 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36547 while (!tsi_end_p (i))
36548 {
36549 tree t = tsi_stmt (i);
36550 if (TREE_CODE (t) == DECL_EXPR
36551 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
36552 {
36553 tsi_delink (&i);
36554 vec_safe_push (for_block, t);
36555 continue;
36556 }
36557 break;
36558 }
36559 if (tsi_one_before_end_p (i))
36560 {
36561 tree t = tsi_stmt (i);
36562 tsi_delink (&i);
36563 free_stmt_list (init);
36564 init = t;
36565 }
36566 }
36567 }
36568 this_pre_body = NULL_TREE;
36569 }
36570 else
36571 {
36572 /* Consume '='. */
36573 cp_lexer_consume_token (parser->lexer);
36574 init = cp_parser_assignment_expression (parser);
36575
36576 non_class:
36577 if (TYPE_REF_P (TREE_TYPE (decl)))
36578 init = error_mark_node;
36579 else
36580 cp_finish_decl (decl, NULL_TREE,
36581 /*init_const_expr_p=*/false,
36582 asm_specification,
36583 LOOKUP_ONLYCONVERTING);
36584 }
36585
36586 if (pushed_scope)
36587 pop_scope (pushed_scope);
36588 }
36589 }
36590 else
36591 {
36592 cp_id_kind idk;
36593 /* If parsing a type specifier sequence failed, then
36594 this MUST be a simple expression. */
36595 cp_parser_parse_tentatively (parser);
36596 decl = cp_parser_primary_expression (parser, false, false,
36597 false, &idk);
36598 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
36599 if (!cp_parser_error_occurred (parser)
36600 && decl
36601 && (TREE_CODE (decl) == COMPONENT_REF
36602 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
36603 {
36604 cp_parser_abort_tentative_parse (parser);
36605 cp_parser_parse_tentatively (parser);
36606 cp_token *token = cp_lexer_peek_token (parser->lexer);
36607 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
36608 /*check_dependency_p=*/true,
36609 /*template_p=*/NULL,
36610 /*declarator_p=*/false,
36611 /*optional_p=*/false);
36612 if (name != error_mark_node
36613 && last_tok == cp_lexer_peek_token (parser->lexer))
36614 {
36615 decl = cp_parser_lookup_name_simple (parser, name,
36616 token->location);
36617 if (TREE_CODE (decl) == FIELD_DECL)
36618 add_private_clause = omp_privatize_field (decl, false);
36619 }
36620 cp_parser_abort_tentative_parse (parser);
36621 cp_parser_parse_tentatively (parser);
36622 decl = cp_parser_primary_expression (parser, false, false,
36623 false, &idk);
36624 }
36625 if (!cp_parser_error_occurred (parser)
36626 && decl
36627 && DECL_P (decl)
36628 && CLASS_TYPE_P (TREE_TYPE (decl)))
36629 {
36630 tree rhs;
36631
36632 cp_parser_parse_definitely (parser);
36633 cp_parser_require (parser, CPP_EQ, RT_EQ);
36634 rhs = cp_parser_assignment_expression (parser);
36635 orig_init = rhs;
36636 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
36637 decl, NOP_EXPR,
36638 rhs,
36639 tf_warning_or_error));
36640 if (!add_private_clause)
36641 add_private_clause = decl;
36642 }
36643 else
36644 {
36645 decl = NULL;
36646 cp_parser_abort_tentative_parse (parser);
36647 init = cp_parser_expression (parser);
36648 if (init)
36649 {
36650 if (TREE_CODE (init) == MODIFY_EXPR
36651 || TREE_CODE (init) == MODOP_EXPR)
36652 real_decl = TREE_OPERAND (init, 0);
36653 }
36654 }
36655 }
36656 return add_private_clause;
36657 }
36658
36659 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36660
36661 void
36662 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
36663 tree &decl, tree &orig_decl, tree &init,
36664 tree &orig_init, tree &cond, tree &incr)
36665 {
36666 tree begin, end, range_temp_decl = NULL_TREE;
36667 tree iter_type, begin_expr, end_expr;
36668
36669 if (processing_template_decl)
36670 {
36671 if (check_for_bare_parameter_packs (init))
36672 init = error_mark_node;
36673 if (!type_dependent_expression_p (init)
36674 /* do_auto_deduction doesn't mess with template init-lists. */
36675 && !BRACE_ENCLOSED_INITIALIZER_P (init))
36676 {
36677 tree d = decl;
36678 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36679 {
36680 tree v = DECL_VALUE_EXPR (decl);
36681 if (TREE_CODE (v) == ARRAY_REF
36682 && VAR_P (TREE_OPERAND (v, 0))
36683 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36684 d = TREE_OPERAND (v, 0);
36685 }
36686 do_range_for_auto_deduction (d, init);
36687 }
36688 cond = global_namespace;
36689 incr = NULL_TREE;
36690 orig_init = init;
36691 if (this_pre_body)
36692 this_pre_body = pop_stmt_list (this_pre_body);
36693 return;
36694 }
36695
36696 init = mark_lvalue_use (init);
36697
36698 if (decl == error_mark_node || init == error_mark_node)
36699 /* If an error happened previously do nothing or else a lot of
36700 unhelpful errors would be issued. */
36701 begin_expr = end_expr = iter_type = error_mark_node;
36702 else
36703 {
36704 tree range_temp;
36705
36706 if (VAR_P (init)
36707 && array_of_runtime_bound_p (TREE_TYPE (init)))
36708 /* Can't bind a reference to an array of runtime bound. */
36709 range_temp = init;
36710 else
36711 {
36712 range_temp = build_range_temp (init);
36713 DECL_NAME (range_temp) = NULL_TREE;
36714 pushdecl (range_temp);
36715 cp_finish_decl (range_temp, init,
36716 /*is_constant_init*/false, NULL_TREE,
36717 LOOKUP_ONLYCONVERTING);
36718 range_temp_decl = range_temp;
36719 range_temp = convert_from_reference (range_temp);
36720 }
36721 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36722 &begin_expr, &end_expr);
36723 }
36724
36725 tree end_iter_type = iter_type;
36726 if (cxx_dialect >= cxx17)
36727 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36728 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36729 TREE_USED (end) = 1;
36730 DECL_ARTIFICIAL (end) = 1;
36731 pushdecl (end);
36732 cp_finish_decl (end, end_expr,
36733 /*is_constant_init*/false, NULL_TREE,
36734 LOOKUP_ONLYCONVERTING);
36735
36736 /* The new for initialization statement. */
36737 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36738 TREE_USED (begin) = 1;
36739 DECL_ARTIFICIAL (begin) = 1;
36740 pushdecl (begin);
36741 orig_init = init;
36742 if (CLASS_TYPE_P (iter_type))
36743 init = NULL_TREE;
36744 else
36745 {
36746 init = begin_expr;
36747 begin_expr = NULL_TREE;
36748 }
36749 cp_finish_decl (begin, begin_expr,
36750 /*is_constant_init*/false, NULL_TREE,
36751 LOOKUP_ONLYCONVERTING);
36752
36753 /* The new for condition. */
36754 if (CLASS_TYPE_P (iter_type))
36755 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36756 else
36757 cond = build_x_binary_op (input_location, NE_EXPR,
36758 begin, ERROR_MARK,
36759 end, ERROR_MARK,
36760 NULL, tf_warning_or_error);
36761
36762 /* The new increment expression. */
36763 if (CLASS_TYPE_P (iter_type))
36764 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36765 else
36766 incr = finish_unary_op_expr (input_location,
36767 PREINCREMENT_EXPR, begin,
36768 tf_warning_or_error);
36769
36770 orig_decl = decl;
36771 decl = begin;
36772 if (for_block)
36773 {
36774 vec_safe_push (for_block, this_pre_body);
36775 this_pre_body = NULL_TREE;
36776 }
36777
36778 tree decomp_first_name = NULL_TREE;
36779 unsigned decomp_cnt = 0;
36780 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36781 {
36782 tree v = DECL_VALUE_EXPR (orig_decl);
36783 if (TREE_CODE (v) == ARRAY_REF
36784 && VAR_P (TREE_OPERAND (v, 0))
36785 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36786 {
36787 tree d = orig_decl;
36788 orig_decl = TREE_OPERAND (v, 0);
36789 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36790 decomp_first_name = d;
36791 }
36792 }
36793
36794 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36795 if (auto_node)
36796 {
36797 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36798 tf_none);
36799 if (!error_operand_p (t))
36800 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36801 t, auto_node);
36802 }
36803
36804 tree v = make_tree_vec (decomp_cnt + 3);
36805 TREE_VEC_ELT (v, 0) = range_temp_decl;
36806 TREE_VEC_ELT (v, 1) = end;
36807 TREE_VEC_ELT (v, 2) = orig_decl;
36808 for (unsigned i = 0; i < decomp_cnt; i++)
36809 {
36810 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36811 decomp_first_name = DECL_CHAIN (decomp_first_name);
36812 }
36813 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36814 }
36815
36816 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36817 inside of the collapsed body. */
36818
36819 void
36820 cp_finish_omp_range_for (tree orig, tree begin)
36821 {
36822 gcc_assert (TREE_CODE (orig) == TREE_LIST
36823 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36824 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36825 tree decomp_first_name = NULL_TREE;
36826 unsigned int decomp_cnt = 0;
36827
36828 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36829 {
36830 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36831 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36832 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36833 }
36834
36835 /* The declaration is initialized with *__begin inside the loop body. */
36836 cp_finish_decl (decl,
36837 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36838 tf_warning_or_error),
36839 /*is_constant_init*/false, NULL_TREE,
36840 LOOKUP_ONLYCONVERTING);
36841 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36842 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36843 }
36844
36845 /* OpenMP 5.0:
36846
36847 scan-loop-body:
36848 { structured-block scan-directive structured-block } */
36849
36850 static void
36851 cp_parser_omp_scan_loop_body (cp_parser *parser)
36852 {
36853 tree substmt, clauses = NULL_TREE;
36854
36855 matching_braces braces;
36856 if (!braces.require_open (parser))
36857 return;
36858
36859 substmt = cp_parser_omp_structured_block (parser, NULL);
36860 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
36861 add_stmt (substmt);
36862
36863 cp_token *tok = cp_lexer_peek_token (parser->lexer);
36864 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
36865 {
36866 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
36867
36868 cp_lexer_consume_token (parser->lexer);
36869
36870 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36871 {
36872 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36873 const char *p = IDENTIFIER_POINTER (id);
36874 if (strcmp (p, "inclusive") == 0)
36875 clause = OMP_CLAUSE_INCLUSIVE;
36876 else if (strcmp (p, "exclusive") == 0)
36877 clause = OMP_CLAUSE_EXCLUSIVE;
36878 }
36879 if (clause != OMP_CLAUSE_ERROR)
36880 {
36881 cp_lexer_consume_token (parser->lexer);
36882 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
36883 }
36884 else
36885 cp_parser_error (parser, "expected %<inclusive%> or "
36886 "%<exclusive%> clause");
36887
36888 cp_parser_require_pragma_eol (parser, tok);
36889 }
36890 else
36891 error ("expected %<#pragma omp scan%>");
36892
36893 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36894 substmt = cp_parser_omp_structured_block (parser, NULL);
36895 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
36896 clauses);
36897 add_stmt (substmt);
36898
36899 braces.require_close (parser);
36900 }
36901
36902 /* Parse the restricted form of the for statement allowed by OpenMP. */
36903
36904 static tree
36905 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36906 tree *cclauses, bool *if_p)
36907 {
36908 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36909 tree orig_decl;
36910 tree real_decl, initv, condv, incrv, declv, orig_declv;
36911 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36912 location_t loc_first;
36913 bool collapse_err = false;
36914 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36915 releasing_vec for_block;
36916 auto_vec<tree, 4> orig_inits;
36917 bool tiling = false;
36918 bool inscan = false;
36919
36920 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36921 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36922 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36923 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36924 {
36925 tiling = true;
36926 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36927 }
36928 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36929 && OMP_CLAUSE_ORDERED_EXPR (cl))
36930 {
36931 ordered_cl = cl;
36932 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36933 }
36934 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
36935 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
36936 && (code == OMP_SIMD || code == OMP_FOR))
36937 inscan = true;
36938
36939 if (ordered && ordered < collapse)
36940 {
36941 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36942 "%<ordered%> clause parameter is less than %<collapse%>");
36943 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36944 = build_int_cst (NULL_TREE, collapse);
36945 ordered = collapse;
36946 }
36947 if (ordered)
36948 {
36949 for (tree *pc = &clauses; *pc; )
36950 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36951 {
36952 error_at (OMP_CLAUSE_LOCATION (*pc),
36953 "%<linear%> clause may not be specified together "
36954 "with %<ordered%> clause with a parameter");
36955 *pc = OMP_CLAUSE_CHAIN (*pc);
36956 }
36957 else
36958 pc = &OMP_CLAUSE_CHAIN (*pc);
36959 }
36960
36961 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36962 count = ordered ? ordered : collapse;
36963
36964 declv = make_tree_vec (count);
36965 initv = make_tree_vec (count);
36966 condv = make_tree_vec (count);
36967 incrv = make_tree_vec (count);
36968 orig_declv = NULL_TREE;
36969
36970 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36971
36972 for (i = 0; i < count; i++)
36973 {
36974 int bracecount = 0;
36975 tree add_private_clause = NULL_TREE;
36976 location_t loc;
36977
36978 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36979 {
36980 if (!collapse_err)
36981 cp_parser_error (parser, "for statement expected");
36982 return NULL;
36983 }
36984 loc = cp_lexer_consume_token (parser->lexer)->location;
36985
36986 /* Don't create location wrapper nodes within an OpenMP "for"
36987 statement. */
36988 auto_suppress_location_wrappers sentinel;
36989
36990 matching_parens parens;
36991 if (!parens.require_open (parser))
36992 return NULL;
36993
36994 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36995 this_pre_body = push_stmt_list ();
36996
36997 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36998 {
36999 /* Save tokens so that we can put them back. */
37000 cp_lexer_save_tokens (parser->lexer);
37001
37002 /* Look for ':' that is not nested in () or {}. */
37003 bool is_range_for
37004 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
37005 /*recovering=*/false,
37006 CPP_COLON,
37007 /*consume_paren=*/
37008 false) == -1);
37009
37010 /* Roll back the tokens we skipped. */
37011 cp_lexer_rollback_tokens (parser->lexer);
37012
37013 if (is_range_for)
37014 {
37015 bool saved_colon_corrects_to_scope_p
37016 = parser->colon_corrects_to_scope_p;
37017
37018 /* A colon is used in range-based for. */
37019 parser->colon_corrects_to_scope_p = false;
37020
37021 /* Parse the declaration. */
37022 cp_parser_simple_declaration (parser,
37023 /*function_definition_allowed_p=*/
37024 false, &decl);
37025 parser->colon_corrects_to_scope_p
37026 = saved_colon_corrects_to_scope_p;
37027
37028 cp_parser_require (parser, CPP_COLON, RT_COLON);
37029
37030 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
37031 false, 0, true);
37032
37033 cp_convert_omp_range_for (this_pre_body, for_block, decl,
37034 orig_decl, init, orig_init,
37035 cond, incr);
37036 if (this_pre_body)
37037 {
37038 if (pre_body)
37039 {
37040 tree t = pre_body;
37041 pre_body = push_stmt_list ();
37042 add_stmt (t);
37043 add_stmt (this_pre_body);
37044 pre_body = pop_stmt_list (pre_body);
37045 }
37046 else
37047 pre_body = this_pre_body;
37048 }
37049
37050 if (ordered_cl)
37051 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
37052 "%<ordered%> clause with parameter on "
37053 "range-based %<for%> loop");
37054
37055 goto parse_close_paren;
37056 }
37057 }
37058
37059 add_private_clause
37060 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
37061 init, orig_init, decl, real_decl);
37062
37063 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37064 if (this_pre_body)
37065 {
37066 this_pre_body = pop_stmt_list (this_pre_body);
37067 if (pre_body)
37068 {
37069 tree t = pre_body;
37070 pre_body = push_stmt_list ();
37071 add_stmt (t);
37072 add_stmt (this_pre_body);
37073 pre_body = pop_stmt_list (pre_body);
37074 }
37075 else
37076 pre_body = this_pre_body;
37077 }
37078
37079 if (decl)
37080 real_decl = decl;
37081 if (cclauses != NULL
37082 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
37083 && real_decl != NULL_TREE)
37084 {
37085 tree *c;
37086 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
37087 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
37088 && OMP_CLAUSE_DECL (*c) == real_decl)
37089 {
37090 error_at (loc, "iteration variable %qD"
37091 " should not be firstprivate", real_decl);
37092 *c = OMP_CLAUSE_CHAIN (*c);
37093 }
37094 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
37095 && OMP_CLAUSE_DECL (*c) == real_decl)
37096 {
37097 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
37098 tree l = *c;
37099 *c = OMP_CLAUSE_CHAIN (*c);
37100 if (code == OMP_SIMD)
37101 {
37102 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37103 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
37104 }
37105 else
37106 {
37107 OMP_CLAUSE_CHAIN (l) = clauses;
37108 clauses = l;
37109 }
37110 add_private_clause = NULL_TREE;
37111 }
37112 else
37113 {
37114 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
37115 && OMP_CLAUSE_DECL (*c) == real_decl)
37116 add_private_clause = NULL_TREE;
37117 c = &OMP_CLAUSE_CHAIN (*c);
37118 }
37119 }
37120
37121 if (add_private_clause)
37122 {
37123 tree c;
37124 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
37125 {
37126 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
37127 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
37128 && OMP_CLAUSE_DECL (c) == decl)
37129 break;
37130 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
37131 && OMP_CLAUSE_DECL (c) == decl)
37132 error_at (loc, "iteration variable %qD "
37133 "should not be firstprivate",
37134 decl);
37135 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
37136 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
37137 && OMP_CLAUSE_DECL (c) == decl)
37138 error_at (loc, "iteration variable %qD should not be reduction",
37139 decl);
37140 }
37141 if (c == NULL)
37142 {
37143 if (code != OMP_SIMD)
37144 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
37145 else if (collapse == 1)
37146 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37147 else
37148 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
37149 OMP_CLAUSE_DECL (c) = add_private_clause;
37150 c = finish_omp_clauses (c, C_ORT_OMP);
37151 if (c)
37152 {
37153 OMP_CLAUSE_CHAIN (c) = clauses;
37154 clauses = c;
37155 /* For linear, signal that we need to fill up
37156 the so far unknown linear step. */
37157 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
37158 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
37159 }
37160 }
37161 }
37162
37163 cond = NULL;
37164 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37165 cond = cp_parser_omp_for_cond (parser, decl, code);
37166 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37167
37168 incr = NULL;
37169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
37170 {
37171 /* If decl is an iterator, preserve the operator on decl
37172 until finish_omp_for. */
37173 if (real_decl
37174 && ((processing_template_decl
37175 && (TREE_TYPE (real_decl) == NULL_TREE
37176 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
37177 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
37178 incr = cp_parser_omp_for_incr (parser, real_decl);
37179 else
37180 incr = cp_parser_expression (parser);
37181 if (!EXPR_HAS_LOCATION (incr))
37182 protected_set_expr_location (incr, input_location);
37183 }
37184
37185 parse_close_paren:
37186 if (!parens.require_close (parser))
37187 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37188 /*or_comma=*/false,
37189 /*consume_paren=*/true);
37190
37191 TREE_VEC_ELT (declv, i) = decl;
37192 TREE_VEC_ELT (initv, i) = init;
37193 TREE_VEC_ELT (condv, i) = cond;
37194 TREE_VEC_ELT (incrv, i) = incr;
37195 if (orig_init)
37196 {
37197 orig_inits.safe_grow_cleared (i + 1);
37198 orig_inits[i] = orig_init;
37199 }
37200 if (orig_decl)
37201 {
37202 if (!orig_declv)
37203 orig_declv = copy_node (declv);
37204 TREE_VEC_ELT (orig_declv, i) = orig_decl;
37205 }
37206 else if (orig_declv)
37207 TREE_VEC_ELT (orig_declv, i) = decl;
37208
37209 if (i == count - 1)
37210 break;
37211
37212 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
37213 in between the collapsed for loops to be still considered perfectly
37214 nested. Hopefully the final version clarifies this.
37215 For now handle (multiple) {'s and empty statements. */
37216 cp_parser_parse_tentatively (parser);
37217 for (;;)
37218 {
37219 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37220 break;
37221 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
37222 {
37223 cp_lexer_consume_token (parser->lexer);
37224 bracecount++;
37225 }
37226 else if (bracecount
37227 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37228 cp_lexer_consume_token (parser->lexer);
37229 else
37230 {
37231 loc = cp_lexer_peek_token (parser->lexer)->location;
37232 error_at (loc, "not enough for loops to collapse");
37233 collapse_err = true;
37234 cp_parser_abort_tentative_parse (parser);
37235 declv = NULL_TREE;
37236 break;
37237 }
37238 }
37239
37240 if (declv)
37241 {
37242 cp_parser_parse_definitely (parser);
37243 nbraces += bracecount;
37244 }
37245 }
37246
37247 if (nbraces)
37248 if_p = NULL;
37249
37250 /* Note that we saved the original contents of this flag when we entered
37251 the structured block, and so we don't need to re-save it here. */
37252 parser->in_statement = IN_OMP_FOR;
37253
37254 /* Note that the grammar doesn't call for a structured block here,
37255 though the loop as a whole is a structured block. */
37256 if (orig_declv)
37257 {
37258 body = begin_omp_structured_block ();
37259 for (i = 0; i < count; i++)
37260 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
37261 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
37262 TREE_VEC_ELT (declv, i));
37263 }
37264 else
37265 body = push_stmt_list ();
37266 if (inscan)
37267 cp_parser_omp_scan_loop_body (parser);
37268 else
37269 cp_parser_statement (parser, NULL_TREE, false, if_p);
37270 if (orig_declv)
37271 body = finish_omp_structured_block (body);
37272 else
37273 body = pop_stmt_list (body);
37274
37275 if (declv == NULL_TREE)
37276 ret = NULL_TREE;
37277 else
37278 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
37279 incrv, body, pre_body, &orig_inits, clauses);
37280
37281 while (nbraces)
37282 {
37283 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
37284 {
37285 cp_lexer_consume_token (parser->lexer);
37286 nbraces--;
37287 }
37288 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37289 cp_lexer_consume_token (parser->lexer);
37290 else
37291 {
37292 if (!collapse_err)
37293 {
37294 error_at (cp_lexer_peek_token (parser->lexer)->location,
37295 "collapsed loops not perfectly nested");
37296 }
37297 collapse_err = true;
37298 cp_parser_statement_seq_opt (parser, NULL);
37299 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
37300 break;
37301 }
37302 }
37303
37304 while (!for_block->is_empty ())
37305 {
37306 tree t = for_block->pop ();
37307 if (TREE_CODE (t) == STATEMENT_LIST)
37308 add_stmt (pop_stmt_list (t));
37309 else
37310 add_stmt (t);
37311 }
37312
37313 return ret;
37314 }
37315
37316 /* Helper function for OpenMP parsing, split clauses and call
37317 finish_omp_clauses on each of the set of clauses afterwards. */
37318
37319 static void
37320 cp_omp_split_clauses (location_t loc, enum tree_code code,
37321 omp_clause_mask mask, tree clauses, tree *cclauses)
37322 {
37323 int i;
37324 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
37325 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
37326 if (cclauses[i])
37327 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
37328 }
37329
37330 /* OpenMP 4.0:
37331 #pragma omp simd simd-clause[optseq] new-line
37332 for-loop */
37333
37334 #define OMP_SIMD_CLAUSE_MASK \
37335 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
37336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
37345
37346 static tree
37347 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
37348 char *p_name, omp_clause_mask mask, tree *cclauses,
37349 bool *if_p)
37350 {
37351 tree clauses, sb, ret;
37352 unsigned int save;
37353 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37354
37355 strcat (p_name, " simd");
37356 mask |= OMP_SIMD_CLAUSE_MASK;
37357
37358 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37359 cclauses == NULL);
37360 if (cclauses)
37361 {
37362 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
37363 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
37364 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
37365 OMP_CLAUSE_ORDERED);
37366 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
37367 {
37368 error_at (OMP_CLAUSE_LOCATION (c),
37369 "%<ordered%> clause with parameter may not be specified "
37370 "on %qs construct", p_name);
37371 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
37372 }
37373 }
37374
37375 keep_next_level (true);
37376 sb = begin_omp_structured_block ();
37377 save = cp_parser_begin_omp_structured_block (parser);
37378
37379 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
37380
37381 cp_parser_end_omp_structured_block (parser, save);
37382 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37383
37384 return ret;
37385 }
37386
37387 /* OpenMP 2.5:
37388 #pragma omp for for-clause[optseq] new-line
37389 for-loop
37390
37391 OpenMP 4.0:
37392 #pragma omp for simd for-simd-clause[optseq] new-line
37393 for-loop */
37394
37395 #define OMP_FOR_CLAUSE_MASK \
37396 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37405
37406 static tree
37407 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
37408 char *p_name, omp_clause_mask mask, tree *cclauses,
37409 bool *if_p)
37410 {
37411 tree clauses, sb, ret;
37412 unsigned int save;
37413 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37414
37415 strcat (p_name, " for");
37416 mask |= OMP_FOR_CLAUSE_MASK;
37417 /* parallel for{, simd} disallows nowait clause, but for
37418 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37419 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
37420 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37421 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37422 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37423 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
37424
37425 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37426 {
37427 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37428 const char *p = IDENTIFIER_POINTER (id);
37429
37430 if (strcmp (p, "simd") == 0)
37431 {
37432 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37433 if (cclauses == NULL)
37434 cclauses = cclauses_buf;
37435
37436 cp_lexer_consume_token (parser->lexer);
37437 if (!flag_openmp) /* flag_openmp_simd */
37438 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37439 cclauses, if_p);
37440 sb = begin_omp_structured_block ();
37441 save = cp_parser_begin_omp_structured_block (parser);
37442 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37443 cclauses, if_p);
37444 cp_parser_end_omp_structured_block (parser, save);
37445 tree body = finish_omp_structured_block (sb);
37446 if (ret == NULL)
37447 return ret;
37448 ret = make_node (OMP_FOR);
37449 TREE_TYPE (ret) = void_type_node;
37450 OMP_FOR_BODY (ret) = body;
37451 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37452 SET_EXPR_LOCATION (ret, loc);
37453 add_stmt (ret);
37454 return ret;
37455 }
37456 }
37457 if (!flag_openmp) /* flag_openmp_simd */
37458 {
37459 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37460 return NULL_TREE;
37461 }
37462
37463 /* Composite distribute parallel for disallows linear clause. */
37464 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37465 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
37466
37467 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37468 cclauses == NULL);
37469 if (cclauses)
37470 {
37471 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
37472 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37473 }
37474
37475 keep_next_level (true);
37476 sb = begin_omp_structured_block ();
37477 save = cp_parser_begin_omp_structured_block (parser);
37478
37479 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
37480
37481 cp_parser_end_omp_structured_block (parser, save);
37482 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37483
37484 return ret;
37485 }
37486
37487 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
37488 omp_clause_mask, tree *, bool *);
37489
37490 /* OpenMP 2.5:
37491 # pragma omp master new-line
37492 structured-block */
37493
37494 static tree
37495 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
37496 char *p_name, omp_clause_mask mask, tree *cclauses,
37497 bool *if_p)
37498 {
37499 tree clauses, sb, ret;
37500 unsigned int save;
37501 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37502
37503 strcat (p_name, " master");
37504
37505 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37506 {
37507 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37508 const char *p = IDENTIFIER_POINTER (id);
37509
37510 if (strcmp (p, "taskloop") == 0)
37511 {
37512 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37513 if (cclauses == NULL)
37514 cclauses = cclauses_buf;
37515
37516 cp_lexer_consume_token (parser->lexer);
37517 if (!flag_openmp) /* flag_openmp_simd */
37518 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37519 cclauses, if_p);
37520 sb = begin_omp_structured_block ();
37521 save = cp_parser_begin_omp_structured_block (parser);
37522 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37523 cclauses, if_p);
37524 cp_parser_end_omp_structured_block (parser, save);
37525 tree body = finish_omp_structured_block (sb);
37526 if (ret == NULL)
37527 return ret;
37528 return c_finish_omp_master (loc, body);
37529 }
37530 }
37531 if (!flag_openmp) /* flag_openmp_simd */
37532 {
37533 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37534 return NULL_TREE;
37535 }
37536
37537 if (cclauses)
37538 {
37539 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37540 false);
37541 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
37542 }
37543 else
37544 cp_parser_require_pragma_eol (parser, pragma_tok);
37545
37546 return c_finish_omp_master (loc,
37547 cp_parser_omp_structured_block (parser, if_p));
37548 }
37549
37550 /* OpenMP 2.5:
37551 # pragma omp ordered new-line
37552 structured-block
37553
37554 OpenMP 4.5:
37555 # pragma omp ordered ordered-clauses new-line
37556 structured-block */
37557
37558 #define OMP_ORDERED_CLAUSE_MASK \
37559 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37561
37562 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37563 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37564
37565 static bool
37566 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
37567 enum pragma_context context, bool *if_p)
37568 {
37569 location_t loc = pragma_tok->location;
37570
37571 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37572 {
37573 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37574 const char *p = IDENTIFIER_POINTER (id);
37575
37576 if (strcmp (p, "depend") == 0)
37577 {
37578 if (!flag_openmp) /* flag_openmp_simd */
37579 {
37580 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37581 return false;
37582 }
37583 if (context == pragma_stmt)
37584 {
37585 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
37586 "%<depend%> clause may only be used in compound "
37587 "statements");
37588 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37589 return false;
37590 }
37591 tree clauses
37592 = cp_parser_omp_all_clauses (parser,
37593 OMP_ORDERED_DEPEND_CLAUSE_MASK,
37594 "#pragma omp ordered", pragma_tok);
37595 c_finish_omp_ordered (loc, clauses, NULL_TREE);
37596 return false;
37597 }
37598 }
37599
37600 tree clauses
37601 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
37602 "#pragma omp ordered", pragma_tok);
37603
37604 if (!flag_openmp /* flag_openmp_simd */
37605 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
37606 return false;
37607
37608 c_finish_omp_ordered (loc, clauses,
37609 cp_parser_omp_structured_block (parser, if_p));
37610 return true;
37611 }
37612
37613 /* OpenMP 2.5:
37614
37615 section-scope:
37616 { section-sequence }
37617
37618 section-sequence:
37619 section-directive[opt] structured-block
37620 section-sequence section-directive structured-block */
37621
37622 static tree
37623 cp_parser_omp_sections_scope (cp_parser *parser)
37624 {
37625 tree stmt, substmt;
37626 bool error_suppress = false;
37627 cp_token *tok;
37628
37629 matching_braces braces;
37630 if (!braces.require_open (parser))
37631 return NULL_TREE;
37632
37633 stmt = push_stmt_list ();
37634
37635 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
37636 != PRAGMA_OMP_SECTION)
37637 {
37638 substmt = cp_parser_omp_structured_block (parser, NULL);
37639 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37640 add_stmt (substmt);
37641 }
37642
37643 while (1)
37644 {
37645 tok = cp_lexer_peek_token (parser->lexer);
37646 if (tok->type == CPP_CLOSE_BRACE)
37647 break;
37648 if (tok->type == CPP_EOF)
37649 break;
37650
37651 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
37652 {
37653 cp_lexer_consume_token (parser->lexer);
37654 cp_parser_require_pragma_eol (parser, tok);
37655 error_suppress = false;
37656 }
37657 else if (!error_suppress)
37658 {
37659 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
37660 error_suppress = true;
37661 }
37662
37663 substmt = cp_parser_omp_structured_block (parser, NULL);
37664 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37665 add_stmt (substmt);
37666 }
37667 braces.require_close (parser);
37668
37669 substmt = pop_stmt_list (stmt);
37670
37671 stmt = make_node (OMP_SECTIONS);
37672 TREE_TYPE (stmt) = void_type_node;
37673 OMP_SECTIONS_BODY (stmt) = substmt;
37674
37675 add_stmt (stmt);
37676 return stmt;
37677 }
37678
37679 /* OpenMP 2.5:
37680 # pragma omp sections sections-clause[optseq] newline
37681 sections-scope */
37682
37683 #define OMP_SECTIONS_CLAUSE_MASK \
37684 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37689
37690 static tree
37691 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
37692 char *p_name, omp_clause_mask mask, tree *cclauses)
37693 {
37694 tree clauses, ret;
37695 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37696
37697 strcat (p_name, " sections");
37698 mask |= OMP_SECTIONS_CLAUSE_MASK;
37699 if (cclauses)
37700 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37701
37702 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37703 cclauses == NULL);
37704 if (cclauses)
37705 {
37706 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
37707 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
37708 }
37709
37710 ret = cp_parser_omp_sections_scope (parser);
37711 if (ret)
37712 OMP_SECTIONS_CLAUSES (ret) = clauses;
37713
37714 return ret;
37715 }
37716
37717 /* OpenMP 2.5:
37718 # pragma omp parallel parallel-clause[optseq] new-line
37719 structured-block
37720 # pragma omp parallel for parallel-for-clause[optseq] new-line
37721 structured-block
37722 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37723 structured-block
37724
37725 OpenMP 4.0:
37726 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37727 structured-block */
37728
37729 #define OMP_PARALLEL_CLAUSE_MASK \
37730 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37739
37740 static tree
37741 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
37742 char *p_name, omp_clause_mask mask, tree *cclauses,
37743 bool *if_p)
37744 {
37745 tree stmt, clauses, block;
37746 unsigned int save;
37747 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37748
37749 strcat (p_name, " parallel");
37750 mask |= OMP_PARALLEL_CLAUSE_MASK;
37751 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37752 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
37753 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
37754 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37755
37756 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37757 {
37758 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37759 if (cclauses == NULL)
37760 cclauses = cclauses_buf;
37761
37762 cp_lexer_consume_token (parser->lexer);
37763 if (!flag_openmp) /* flag_openmp_simd */
37764 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37765 if_p);
37766 block = begin_omp_parallel ();
37767 save = cp_parser_begin_omp_structured_block (parser);
37768 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37769 if_p);
37770 cp_parser_end_omp_structured_block (parser, save);
37771 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37772 block);
37773 if (ret == NULL_TREE)
37774 return ret;
37775 OMP_PARALLEL_COMBINED (stmt) = 1;
37776 return stmt;
37777 }
37778 /* When combined with distribute, parallel has to be followed by for.
37779 #pragma omp target parallel is allowed though. */
37780 else if (cclauses
37781 && (mask & (OMP_CLAUSE_MASK_1
37782 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37783 {
37784 error_at (loc, "expected %<for%> after %qs", p_name);
37785 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37786 return NULL_TREE;
37787 }
37788 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37789 {
37790 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37791 const char *p = IDENTIFIER_POINTER (id);
37792 if (strcmp (p, "master") == 0)
37793 {
37794 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37795 cclauses = cclauses_buf;
37796
37797 cp_lexer_consume_token (parser->lexer);
37798 block = begin_omp_parallel ();
37799 save = cp_parser_begin_omp_structured_block (parser);
37800 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37801 cclauses, if_p);
37802 cp_parser_end_omp_structured_block (parser, save);
37803 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37804 block);
37805 OMP_PARALLEL_COMBINED (stmt) = 1;
37806 if (ret == NULL_TREE)
37807 return ret;
37808 return stmt;
37809 }
37810 else if (!flag_openmp) /* flag_openmp_simd */
37811 {
37812 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37813 return NULL_TREE;
37814 }
37815 else if (strcmp (p, "sections") == 0)
37816 {
37817 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37818 cclauses = cclauses_buf;
37819
37820 cp_lexer_consume_token (parser->lexer);
37821 block = begin_omp_parallel ();
37822 save = cp_parser_begin_omp_structured_block (parser);
37823 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37824 cp_parser_end_omp_structured_block (parser, save);
37825 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37826 block);
37827 OMP_PARALLEL_COMBINED (stmt) = 1;
37828 return stmt;
37829 }
37830 }
37831 else if (!flag_openmp) /* flag_openmp_simd */
37832 {
37833 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37834 return NULL_TREE;
37835 }
37836
37837 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37838 cclauses == NULL);
37839 if (cclauses)
37840 {
37841 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37842 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37843 }
37844
37845 block = begin_omp_parallel ();
37846 save = cp_parser_begin_omp_structured_block (parser);
37847 cp_parser_statement (parser, NULL_TREE, false, if_p);
37848 cp_parser_end_omp_structured_block (parser, save);
37849 stmt = finish_omp_parallel (clauses, block);
37850 return stmt;
37851 }
37852
37853 /* OpenMP 2.5:
37854 # pragma omp single single-clause[optseq] new-line
37855 structured-block */
37856
37857 #define OMP_SINGLE_CLAUSE_MASK \
37858 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37862
37863 static tree
37864 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37865 {
37866 tree stmt = make_node (OMP_SINGLE);
37867 TREE_TYPE (stmt) = void_type_node;
37868 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37869
37870 OMP_SINGLE_CLAUSES (stmt)
37871 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37872 "#pragma omp single", pragma_tok);
37873 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37874
37875 return add_stmt (stmt);
37876 }
37877
37878 /* OpenMP 3.0:
37879 # pragma omp task task-clause[optseq] new-line
37880 structured-block */
37881
37882 #define OMP_TASK_CLAUSE_MASK \
37883 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37894
37895 static tree
37896 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37897 {
37898 tree clauses, block;
37899 unsigned int save;
37900
37901 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37902 "#pragma omp task", pragma_tok);
37903 block = begin_omp_task ();
37904 save = cp_parser_begin_omp_structured_block (parser);
37905 cp_parser_statement (parser, NULL_TREE, false, if_p);
37906 cp_parser_end_omp_structured_block (parser, save);
37907 return finish_omp_task (clauses, block);
37908 }
37909
37910 /* OpenMP 3.0:
37911 # pragma omp taskwait new-line
37912
37913 OpenMP 5.0:
37914 # pragma omp taskwait taskwait-clause[opt] new-line */
37915
37916 #define OMP_TASKWAIT_CLAUSE_MASK \
37917 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37918
37919 static void
37920 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37921 {
37922 tree clauses
37923 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37924 "#pragma omp taskwait", pragma_tok);
37925
37926 if (clauses)
37927 {
37928 tree stmt = make_node (OMP_TASK);
37929 TREE_TYPE (stmt) = void_node;
37930 OMP_TASK_CLAUSES (stmt) = clauses;
37931 OMP_TASK_BODY (stmt) = NULL_TREE;
37932 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37933 add_stmt (stmt);
37934 }
37935 else
37936 finish_omp_taskwait ();
37937 }
37938
37939 /* OpenMP 3.1:
37940 # pragma omp taskyield new-line */
37941
37942 static void
37943 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37944 {
37945 cp_parser_require_pragma_eol (parser, pragma_tok);
37946 finish_omp_taskyield ();
37947 }
37948
37949 /* OpenMP 4.0:
37950 # pragma omp taskgroup new-line
37951 structured-block
37952
37953 OpenMP 5.0:
37954 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37955
37956 #define OMP_TASKGROUP_CLAUSE_MASK \
37957 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37958
37959 static tree
37960 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37961 {
37962 tree clauses
37963 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37964 "#pragma omp taskgroup", pragma_tok);
37965 return c_finish_omp_taskgroup (input_location,
37966 cp_parser_omp_structured_block (parser,
37967 if_p),
37968 clauses);
37969 }
37970
37971
37972 /* OpenMP 2.5:
37973 # pragma omp threadprivate (variable-list) */
37974
37975 static void
37976 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37977 {
37978 tree vars;
37979
37980 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37981 cp_parser_require_pragma_eol (parser, pragma_tok);
37982
37983 finish_omp_threadprivate (vars);
37984 }
37985
37986 /* OpenMP 4.0:
37987 # pragma omp cancel cancel-clause[optseq] new-line */
37988
37989 #define OMP_CANCEL_CLAUSE_MASK \
37990 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37995
37996 static void
37997 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37998 {
37999 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
38000 "#pragma omp cancel", pragma_tok);
38001 finish_omp_cancel (clauses);
38002 }
38003
38004 /* OpenMP 4.0:
38005 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
38006
38007 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
38008 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
38009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
38010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
38011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
38012
38013 static void
38014 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
38015 enum pragma_context context)
38016 {
38017 tree clauses;
38018 bool point_seen = false;
38019
38020 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38021 {
38022 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38023 const char *p = IDENTIFIER_POINTER (id);
38024
38025 if (strcmp (p, "point") == 0)
38026 {
38027 cp_lexer_consume_token (parser->lexer);
38028 point_seen = true;
38029 }
38030 }
38031 if (!point_seen)
38032 {
38033 cp_parser_error (parser, "expected %<point%>");
38034 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38035 return;
38036 }
38037
38038 if (context != pragma_compound)
38039 {
38040 if (context == pragma_stmt)
38041 error_at (pragma_tok->location,
38042 "%<#pragma %s%> may only be used in compound statements",
38043 "omp cancellation point");
38044 else
38045 cp_parser_error (parser, "expected declaration specifiers");
38046 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38047 return;
38048 }
38049
38050 clauses = cp_parser_omp_all_clauses (parser,
38051 OMP_CANCELLATION_POINT_CLAUSE_MASK,
38052 "#pragma omp cancellation point",
38053 pragma_tok);
38054 finish_omp_cancellation_point (clauses);
38055 }
38056
38057 /* OpenMP 4.0:
38058 #pragma omp distribute distribute-clause[optseq] new-line
38059 for-loop */
38060
38061 #define OMP_DISTRIBUTE_CLAUSE_MASK \
38062 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
38066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
38067
38068 static tree
38069 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
38070 char *p_name, omp_clause_mask mask, tree *cclauses,
38071 bool *if_p)
38072 {
38073 tree clauses, sb, ret;
38074 unsigned int save;
38075 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38076
38077 strcat (p_name, " distribute");
38078 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
38079
38080 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38081 {
38082 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38083 const char *p = IDENTIFIER_POINTER (id);
38084 bool simd = false;
38085 bool parallel = false;
38086
38087 if (strcmp (p, "simd") == 0)
38088 simd = true;
38089 else
38090 parallel = strcmp (p, "parallel") == 0;
38091 if (parallel || simd)
38092 {
38093 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38094 if (cclauses == NULL)
38095 cclauses = cclauses_buf;
38096 cp_lexer_consume_token (parser->lexer);
38097 if (!flag_openmp) /* flag_openmp_simd */
38098 {
38099 if (simd)
38100 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38101 cclauses, if_p);
38102 else
38103 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
38104 cclauses, if_p);
38105 }
38106 sb = begin_omp_structured_block ();
38107 save = cp_parser_begin_omp_structured_block (parser);
38108 if (simd)
38109 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38110 cclauses, if_p);
38111 else
38112 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
38113 cclauses, if_p);
38114 cp_parser_end_omp_structured_block (parser, save);
38115 tree body = finish_omp_structured_block (sb);
38116 if (ret == NULL)
38117 return ret;
38118 ret = make_node (OMP_DISTRIBUTE);
38119 TREE_TYPE (ret) = void_type_node;
38120 OMP_FOR_BODY (ret) = body;
38121 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
38122 SET_EXPR_LOCATION (ret, loc);
38123 add_stmt (ret);
38124 return ret;
38125 }
38126 }
38127 if (!flag_openmp) /* flag_openmp_simd */
38128 {
38129 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38130 return NULL_TREE;
38131 }
38132
38133 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38134 cclauses == NULL);
38135 if (cclauses)
38136 {
38137 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
38138 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
38139 }
38140
38141 keep_next_level (true);
38142 sb = begin_omp_structured_block ();
38143 save = cp_parser_begin_omp_structured_block (parser);
38144
38145 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
38146
38147 cp_parser_end_omp_structured_block (parser, save);
38148 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
38149
38150 return ret;
38151 }
38152
38153 /* OpenMP 4.0:
38154 # pragma omp teams teams-clause[optseq] new-line
38155 structured-block */
38156
38157 #define OMP_TEAMS_CLAUSE_MASK \
38158 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
38163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
38164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
38165
38166 static tree
38167 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
38168 char *p_name, omp_clause_mask mask, tree *cclauses,
38169 bool *if_p)
38170 {
38171 tree clauses, sb, ret;
38172 unsigned int save;
38173 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38174
38175 strcat (p_name, " teams");
38176 mask |= OMP_TEAMS_CLAUSE_MASK;
38177
38178 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38179 {
38180 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38181 const char *p = IDENTIFIER_POINTER (id);
38182 if (strcmp (p, "distribute") == 0)
38183 {
38184 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38185 if (cclauses == NULL)
38186 cclauses = cclauses_buf;
38187
38188 cp_lexer_consume_token (parser->lexer);
38189 if (!flag_openmp) /* flag_openmp_simd */
38190 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
38191 cclauses, if_p);
38192 keep_next_level (true);
38193 sb = begin_omp_structured_block ();
38194 save = cp_parser_begin_omp_structured_block (parser);
38195 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
38196 cclauses, if_p);
38197 cp_parser_end_omp_structured_block (parser, save);
38198 tree body = finish_omp_structured_block (sb);
38199 if (ret == NULL)
38200 return ret;
38201 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38202 ret = make_node (OMP_TEAMS);
38203 TREE_TYPE (ret) = void_type_node;
38204 OMP_TEAMS_CLAUSES (ret) = clauses;
38205 OMP_TEAMS_BODY (ret) = body;
38206 OMP_TEAMS_COMBINED (ret) = 1;
38207 SET_EXPR_LOCATION (ret, loc);
38208 return add_stmt (ret);
38209 }
38210 }
38211 if (!flag_openmp) /* flag_openmp_simd */
38212 {
38213 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38214 return NULL_TREE;
38215 }
38216
38217 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38218 cclauses == NULL);
38219 if (cclauses)
38220 {
38221 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
38222 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38223 }
38224
38225 tree stmt = make_node (OMP_TEAMS);
38226 TREE_TYPE (stmt) = void_type_node;
38227 OMP_TEAMS_CLAUSES (stmt) = clauses;
38228 keep_next_level (true);
38229 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38230 SET_EXPR_LOCATION (stmt, loc);
38231
38232 return add_stmt (stmt);
38233 }
38234
38235 /* OpenMP 4.0:
38236 # pragma omp target data target-data-clause[optseq] new-line
38237 structured-block */
38238
38239 #define OMP_TARGET_DATA_CLAUSE_MASK \
38240 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
38244
38245 static tree
38246 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38247 {
38248 tree clauses
38249 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
38250 "#pragma omp target data", pragma_tok);
38251 int map_seen = 0;
38252 for (tree *pc = &clauses; *pc;)
38253 {
38254 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38255 switch (OMP_CLAUSE_MAP_KIND (*pc))
38256 {
38257 case GOMP_MAP_TO:
38258 case GOMP_MAP_ALWAYS_TO:
38259 case GOMP_MAP_FROM:
38260 case GOMP_MAP_ALWAYS_FROM:
38261 case GOMP_MAP_TOFROM:
38262 case GOMP_MAP_ALWAYS_TOFROM:
38263 case GOMP_MAP_ALLOC:
38264 map_seen = 3;
38265 break;
38266 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38267 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38268 case GOMP_MAP_ALWAYS_POINTER:
38269 break;
38270 default:
38271 map_seen |= 1;
38272 error_at (OMP_CLAUSE_LOCATION (*pc),
38273 "%<#pragma omp target data%> with map-type other "
38274 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38275 "on %<map%> clause");
38276 *pc = OMP_CLAUSE_CHAIN (*pc);
38277 continue;
38278 }
38279 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
38280 map_seen = 3;
38281 pc = &OMP_CLAUSE_CHAIN (*pc);
38282 }
38283
38284 if (map_seen != 3)
38285 {
38286 if (map_seen == 0)
38287 error_at (pragma_tok->location,
38288 "%<#pragma omp target data%> must contain at least "
38289 "one %<map%> or %<use_device_ptr%> clause");
38290 return NULL_TREE;
38291 }
38292
38293 tree stmt = make_node (OMP_TARGET_DATA);
38294 TREE_TYPE (stmt) = void_type_node;
38295 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
38296
38297 keep_next_level (true);
38298 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38299
38300 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38301 return add_stmt (stmt);
38302 }
38303
38304 /* OpenMP 4.5:
38305 # pragma omp target enter data target-enter-data-clause[optseq] new-line
38306 structured-block */
38307
38308 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
38309 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38314
38315 static tree
38316 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
38317 enum pragma_context context)
38318 {
38319 bool data_seen = false;
38320 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38321 {
38322 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38323 const char *p = IDENTIFIER_POINTER (id);
38324
38325 if (strcmp (p, "data") == 0)
38326 {
38327 cp_lexer_consume_token (parser->lexer);
38328 data_seen = true;
38329 }
38330 }
38331 if (!data_seen)
38332 {
38333 cp_parser_error (parser, "expected %<data%>");
38334 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38335 return NULL_TREE;
38336 }
38337
38338 if (context == pragma_stmt)
38339 {
38340 error_at (pragma_tok->location,
38341 "%<#pragma %s%> may only be used in compound statements",
38342 "omp target enter data");
38343 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38344 return NULL_TREE;
38345 }
38346
38347 tree clauses
38348 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
38349 "#pragma omp target enter data", pragma_tok);
38350 int map_seen = 0;
38351 for (tree *pc = &clauses; *pc;)
38352 {
38353 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38354 switch (OMP_CLAUSE_MAP_KIND (*pc))
38355 {
38356 case GOMP_MAP_TO:
38357 case GOMP_MAP_ALWAYS_TO:
38358 case GOMP_MAP_ALLOC:
38359 map_seen = 3;
38360 break;
38361 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38362 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38363 case GOMP_MAP_ALWAYS_POINTER:
38364 break;
38365 default:
38366 map_seen |= 1;
38367 error_at (OMP_CLAUSE_LOCATION (*pc),
38368 "%<#pragma omp target enter data%> with map-type other "
38369 "than %<to%> or %<alloc%> on %<map%> clause");
38370 *pc = OMP_CLAUSE_CHAIN (*pc);
38371 continue;
38372 }
38373 pc = &OMP_CLAUSE_CHAIN (*pc);
38374 }
38375
38376 if (map_seen != 3)
38377 {
38378 if (map_seen == 0)
38379 error_at (pragma_tok->location,
38380 "%<#pragma omp target enter data%> must contain at least "
38381 "one %<map%> clause");
38382 return NULL_TREE;
38383 }
38384
38385 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
38386 TREE_TYPE (stmt) = void_type_node;
38387 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
38388 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38389 return add_stmt (stmt);
38390 }
38391
38392 /* OpenMP 4.5:
38393 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38394 structured-block */
38395
38396 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38397 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38402
38403 static tree
38404 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
38405 enum pragma_context context)
38406 {
38407 bool data_seen = false;
38408 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38409 {
38410 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38411 const char *p = IDENTIFIER_POINTER (id);
38412
38413 if (strcmp (p, "data") == 0)
38414 {
38415 cp_lexer_consume_token (parser->lexer);
38416 data_seen = true;
38417 }
38418 }
38419 if (!data_seen)
38420 {
38421 cp_parser_error (parser, "expected %<data%>");
38422 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38423 return NULL_TREE;
38424 }
38425
38426 if (context == pragma_stmt)
38427 {
38428 error_at (pragma_tok->location,
38429 "%<#pragma %s%> may only be used in compound statements",
38430 "omp target exit data");
38431 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38432 return NULL_TREE;
38433 }
38434
38435 tree clauses
38436 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
38437 "#pragma omp target exit data", pragma_tok);
38438 int map_seen = 0;
38439 for (tree *pc = &clauses; *pc;)
38440 {
38441 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38442 switch (OMP_CLAUSE_MAP_KIND (*pc))
38443 {
38444 case GOMP_MAP_FROM:
38445 case GOMP_MAP_ALWAYS_FROM:
38446 case GOMP_MAP_RELEASE:
38447 case GOMP_MAP_DELETE:
38448 map_seen = 3;
38449 break;
38450 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38451 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38452 case GOMP_MAP_ALWAYS_POINTER:
38453 break;
38454 default:
38455 map_seen |= 1;
38456 error_at (OMP_CLAUSE_LOCATION (*pc),
38457 "%<#pragma omp target exit data%> with map-type other "
38458 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38459 " clause");
38460 *pc = OMP_CLAUSE_CHAIN (*pc);
38461 continue;
38462 }
38463 pc = &OMP_CLAUSE_CHAIN (*pc);
38464 }
38465
38466 if (map_seen != 3)
38467 {
38468 if (map_seen == 0)
38469 error_at (pragma_tok->location,
38470 "%<#pragma omp target exit data%> must contain at least "
38471 "one %<map%> clause");
38472 return NULL_TREE;
38473 }
38474
38475 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
38476 TREE_TYPE (stmt) = void_type_node;
38477 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
38478 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38479 return add_stmt (stmt);
38480 }
38481
38482 /* OpenMP 4.0:
38483 # pragma omp target update target-update-clause[optseq] new-line */
38484
38485 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38486 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38492
38493 static bool
38494 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
38495 enum pragma_context context)
38496 {
38497 if (context == pragma_stmt)
38498 {
38499 error_at (pragma_tok->location,
38500 "%<#pragma %s%> may only be used in compound statements",
38501 "omp target update");
38502 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38503 return false;
38504 }
38505
38506 tree clauses
38507 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
38508 "#pragma omp target update", pragma_tok);
38509 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
38510 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
38511 {
38512 error_at (pragma_tok->location,
38513 "%<#pragma omp target update%> must contain at least one "
38514 "%<from%> or %<to%> clauses");
38515 return false;
38516 }
38517
38518 tree stmt = make_node (OMP_TARGET_UPDATE);
38519 TREE_TYPE (stmt) = void_type_node;
38520 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
38521 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38522 add_stmt (stmt);
38523 return false;
38524 }
38525
38526 /* OpenMP 4.0:
38527 # pragma omp target target-clause[optseq] new-line
38528 structured-block */
38529
38530 #define OMP_TARGET_CLAUSE_MASK \
38531 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38540
38541 static bool
38542 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
38543 enum pragma_context context, bool *if_p)
38544 {
38545 tree *pc = NULL, stmt;
38546
38547 if (flag_openmp)
38548 omp_requires_mask
38549 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
38550
38551 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38552 {
38553 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38554 const char *p = IDENTIFIER_POINTER (id);
38555 enum tree_code ccode = ERROR_MARK;
38556
38557 if (strcmp (p, "teams") == 0)
38558 ccode = OMP_TEAMS;
38559 else if (strcmp (p, "parallel") == 0)
38560 ccode = OMP_PARALLEL;
38561 else if (strcmp (p, "simd") == 0)
38562 ccode = OMP_SIMD;
38563 if (ccode != ERROR_MARK)
38564 {
38565 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
38566 char p_name[sizeof ("#pragma omp target teams distribute "
38567 "parallel for simd")];
38568
38569 cp_lexer_consume_token (parser->lexer);
38570 strcpy (p_name, "#pragma omp target");
38571 if (!flag_openmp) /* flag_openmp_simd */
38572 {
38573 tree stmt;
38574 switch (ccode)
38575 {
38576 case OMP_TEAMS:
38577 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
38578 OMP_TARGET_CLAUSE_MASK,
38579 cclauses, if_p);
38580 break;
38581 case OMP_PARALLEL:
38582 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38583 OMP_TARGET_CLAUSE_MASK,
38584 cclauses, if_p);
38585 break;
38586 case OMP_SIMD:
38587 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
38588 OMP_TARGET_CLAUSE_MASK,
38589 cclauses, if_p);
38590 break;
38591 default:
38592 gcc_unreachable ();
38593 }
38594 return stmt != NULL_TREE;
38595 }
38596 keep_next_level (true);
38597 tree sb = begin_omp_structured_block (), ret;
38598 unsigned save = cp_parser_begin_omp_structured_block (parser);
38599 switch (ccode)
38600 {
38601 case OMP_TEAMS:
38602 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
38603 OMP_TARGET_CLAUSE_MASK, cclauses,
38604 if_p);
38605 break;
38606 case OMP_PARALLEL:
38607 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38608 OMP_TARGET_CLAUSE_MASK, cclauses,
38609 if_p);
38610 break;
38611 case OMP_SIMD:
38612 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
38613 OMP_TARGET_CLAUSE_MASK, cclauses,
38614 if_p);
38615 break;
38616 default:
38617 gcc_unreachable ();
38618 }
38619 cp_parser_end_omp_structured_block (parser, save);
38620 tree body = finish_omp_structured_block (sb);
38621 if (ret == NULL_TREE)
38622 return false;
38623 if (ccode == OMP_TEAMS && !processing_template_decl)
38624 {
38625 /* For combined target teams, ensure the num_teams and
38626 thread_limit clause expressions are evaluated on the host,
38627 before entering the target construct. */
38628 tree c;
38629 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38630 c; c = OMP_CLAUSE_CHAIN (c))
38631 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
38632 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
38633 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
38634 {
38635 tree expr = OMP_CLAUSE_OPERAND (c, 0);
38636 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
38637 if (expr == error_mark_node)
38638 continue;
38639 tree tmp = TARGET_EXPR_SLOT (expr);
38640 add_stmt (expr);
38641 OMP_CLAUSE_OPERAND (c, 0) = expr;
38642 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
38643 OMP_CLAUSE_FIRSTPRIVATE);
38644 OMP_CLAUSE_DECL (tc) = tmp;
38645 OMP_CLAUSE_CHAIN (tc)
38646 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38647 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
38648 }
38649 }
38650 tree stmt = make_node (OMP_TARGET);
38651 TREE_TYPE (stmt) = void_type_node;
38652 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38653 OMP_TARGET_BODY (stmt) = body;
38654 OMP_TARGET_COMBINED (stmt) = 1;
38655 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38656 add_stmt (stmt);
38657 pc = &OMP_TARGET_CLAUSES (stmt);
38658 goto check_clauses;
38659 }
38660 else if (!flag_openmp) /* flag_openmp_simd */
38661 {
38662 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38663 return false;
38664 }
38665 else if (strcmp (p, "data") == 0)
38666 {
38667 cp_lexer_consume_token (parser->lexer);
38668 cp_parser_omp_target_data (parser, pragma_tok, if_p);
38669 return true;
38670 }
38671 else if (strcmp (p, "enter") == 0)
38672 {
38673 cp_lexer_consume_token (parser->lexer);
38674 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
38675 return false;
38676 }
38677 else if (strcmp (p, "exit") == 0)
38678 {
38679 cp_lexer_consume_token (parser->lexer);
38680 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
38681 return false;
38682 }
38683 else if (strcmp (p, "update") == 0)
38684 {
38685 cp_lexer_consume_token (parser->lexer);
38686 return cp_parser_omp_target_update (parser, pragma_tok, context);
38687 }
38688 }
38689 if (!flag_openmp) /* flag_openmp_simd */
38690 {
38691 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38692 return false;
38693 }
38694
38695 stmt = make_node (OMP_TARGET);
38696 TREE_TYPE (stmt) = void_type_node;
38697
38698 OMP_TARGET_CLAUSES (stmt)
38699 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
38700 "#pragma omp target", pragma_tok);
38701 pc = &OMP_TARGET_CLAUSES (stmt);
38702 keep_next_level (true);
38703 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38704
38705 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38706 add_stmt (stmt);
38707
38708 check_clauses:
38709 while (*pc)
38710 {
38711 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38712 switch (OMP_CLAUSE_MAP_KIND (*pc))
38713 {
38714 case GOMP_MAP_TO:
38715 case GOMP_MAP_ALWAYS_TO:
38716 case GOMP_MAP_FROM:
38717 case GOMP_MAP_ALWAYS_FROM:
38718 case GOMP_MAP_TOFROM:
38719 case GOMP_MAP_ALWAYS_TOFROM:
38720 case GOMP_MAP_ALLOC:
38721 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38722 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38723 case GOMP_MAP_ALWAYS_POINTER:
38724 break;
38725 default:
38726 error_at (OMP_CLAUSE_LOCATION (*pc),
38727 "%<#pragma omp target%> with map-type other "
38728 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38729 "on %<map%> clause");
38730 *pc = OMP_CLAUSE_CHAIN (*pc);
38731 continue;
38732 }
38733 pc = &OMP_CLAUSE_CHAIN (*pc);
38734 }
38735 return true;
38736 }
38737
38738 /* OpenACC 2.0:
38739 # pragma acc cache (variable-list) new-line
38740 */
38741
38742 static tree
38743 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
38744 {
38745 tree stmt, clauses;
38746
38747 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
38748 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38749
38750 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
38751
38752 stmt = make_node (OACC_CACHE);
38753 TREE_TYPE (stmt) = void_type_node;
38754 OACC_CACHE_CLAUSES (stmt) = clauses;
38755 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38756 add_stmt (stmt);
38757
38758 return stmt;
38759 }
38760
38761 /* OpenACC 2.0:
38762 # pragma acc data oacc-data-clause[optseq] new-line
38763 structured-block */
38764
38765 #define OACC_DATA_CLAUSE_MASK \
38766 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38773
38774 static tree
38775 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38776 {
38777 tree stmt, clauses, block;
38778 unsigned int save;
38779
38780 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38781 "#pragma acc data", pragma_tok);
38782
38783 block = begin_omp_parallel ();
38784 save = cp_parser_begin_omp_structured_block (parser);
38785 cp_parser_statement (parser, NULL_TREE, false, if_p);
38786 cp_parser_end_omp_structured_block (parser, save);
38787 stmt = finish_oacc_data (clauses, block);
38788 return stmt;
38789 }
38790
38791 /* OpenACC 2.0:
38792 # pragma acc host_data <clauses> new-line
38793 structured-block */
38794
38795 #define OACC_HOST_DATA_CLAUSE_MASK \
38796 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38797
38798 static tree
38799 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38800 {
38801 tree stmt, clauses, block;
38802 unsigned int save;
38803
38804 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38805 "#pragma acc host_data", pragma_tok);
38806
38807 block = begin_omp_parallel ();
38808 save = cp_parser_begin_omp_structured_block (parser);
38809 cp_parser_statement (parser, NULL_TREE, false, if_p);
38810 cp_parser_end_omp_structured_block (parser, save);
38811 stmt = finish_oacc_host_data (clauses, block);
38812 return stmt;
38813 }
38814
38815 /* OpenACC 2.0:
38816 # pragma acc declare oacc-data-clause[optseq] new-line
38817 */
38818
38819 #define OACC_DECLARE_CLAUSE_MASK \
38820 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38828
38829 static tree
38830 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38831 {
38832 tree clauses, stmt;
38833 bool error = false;
38834
38835 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38836 "#pragma acc declare", pragma_tok, true);
38837
38838
38839 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38840 {
38841 error_at (pragma_tok->location,
38842 "no valid clauses specified in %<#pragma acc declare%>");
38843 return NULL_TREE;
38844 }
38845
38846 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38847 {
38848 location_t loc = OMP_CLAUSE_LOCATION (t);
38849 tree decl = OMP_CLAUSE_DECL (t);
38850 if (!DECL_P (decl))
38851 {
38852 error_at (loc, "array section in %<#pragma acc declare%>");
38853 error = true;
38854 continue;
38855 }
38856 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38857 switch (OMP_CLAUSE_MAP_KIND (t))
38858 {
38859 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38860 case GOMP_MAP_ALLOC:
38861 case GOMP_MAP_TO:
38862 case GOMP_MAP_FORCE_DEVICEPTR:
38863 case GOMP_MAP_DEVICE_RESIDENT:
38864 break;
38865
38866 case GOMP_MAP_LINK:
38867 if (!global_bindings_p ()
38868 && (TREE_STATIC (decl)
38869 || !DECL_EXTERNAL (decl)))
38870 {
38871 error_at (loc,
38872 "%qD must be a global variable in "
38873 "%<#pragma acc declare link%>",
38874 decl);
38875 error = true;
38876 continue;
38877 }
38878 break;
38879
38880 default:
38881 if (global_bindings_p ())
38882 {
38883 error_at (loc, "invalid OpenACC clause at file scope");
38884 error = true;
38885 continue;
38886 }
38887 if (DECL_EXTERNAL (decl))
38888 {
38889 error_at (loc,
38890 "invalid use of %<extern%> variable %qD "
38891 "in %<#pragma acc declare%>", decl);
38892 error = true;
38893 continue;
38894 }
38895 else if (TREE_PUBLIC (decl))
38896 {
38897 error_at (loc,
38898 "invalid use of %<global%> variable %qD "
38899 "in %<#pragma acc declare%>", decl);
38900 error = true;
38901 continue;
38902 }
38903 break;
38904 }
38905
38906 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38907 || lookup_attribute ("omp declare target link",
38908 DECL_ATTRIBUTES (decl)))
38909 {
38910 error_at (loc, "variable %qD used more than once with "
38911 "%<#pragma acc declare%>", decl);
38912 error = true;
38913 continue;
38914 }
38915
38916 if (!error)
38917 {
38918 tree id;
38919
38920 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38921 id = get_identifier ("omp declare target link");
38922 else
38923 id = get_identifier ("omp declare target");
38924
38925 DECL_ATTRIBUTES (decl)
38926 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38927 if (global_bindings_p ())
38928 {
38929 symtab_node *node = symtab_node::get (decl);
38930 if (node != NULL)
38931 {
38932 node->offloadable = 1;
38933 if (ENABLE_OFFLOADING)
38934 {
38935 g->have_offload = true;
38936 if (is_a <varpool_node *> (node))
38937 vec_safe_push (offload_vars, decl);
38938 }
38939 }
38940 }
38941 }
38942 }
38943
38944 if (error || global_bindings_p ())
38945 return NULL_TREE;
38946
38947 stmt = make_node (OACC_DECLARE);
38948 TREE_TYPE (stmt) = void_type_node;
38949 OACC_DECLARE_CLAUSES (stmt) = clauses;
38950 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38951
38952 add_stmt (stmt);
38953
38954 return NULL_TREE;
38955 }
38956
38957 /* OpenACC 2.0:
38958 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38959
38960 or
38961
38962 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38963
38964 LOC is the location of the #pragma token.
38965 */
38966
38967 #define OACC_ENTER_DATA_CLAUSE_MASK \
38968 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38973
38974 #define OACC_EXIT_DATA_CLAUSE_MASK \
38975 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38981
38982 static tree
38983 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38984 bool enter)
38985 {
38986 location_t loc = pragma_tok->location;
38987 tree stmt, clauses;
38988 const char *p = "";
38989
38990 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38991 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38992
38993 if (strcmp (p, "data") != 0)
38994 {
38995 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38996 enter ? "enter" : "exit");
38997 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38998 return NULL_TREE;
38999 }
39000
39001 cp_lexer_consume_token (parser->lexer);
39002
39003 if (enter)
39004 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
39005 "#pragma acc enter data", pragma_tok);
39006 else
39007 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
39008 "#pragma acc exit data", pragma_tok);
39009
39010 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
39011 {
39012 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
39013 enter ? "enter" : "exit");
39014 return NULL_TREE;
39015 }
39016
39017 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
39018 TREE_TYPE (stmt) = void_type_node;
39019 OMP_STANDALONE_CLAUSES (stmt) = clauses;
39020 SET_EXPR_LOCATION (stmt, loc);
39021 add_stmt (stmt);
39022 return stmt;
39023 }
39024
39025 /* OpenACC 2.0:
39026 # pragma acc loop oacc-loop-clause[optseq] new-line
39027 structured-block */
39028
39029 #define OACC_LOOP_CLAUSE_MASK \
39030 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
39031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
39032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
39033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
39037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
39038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
39039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
39040
39041 static tree
39042 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
39043 omp_clause_mask mask, tree *cclauses, bool *if_p)
39044 {
39045 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
39046
39047 strcat (p_name, " loop");
39048 mask |= OACC_LOOP_CLAUSE_MASK;
39049
39050 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
39051 cclauses == NULL);
39052 if (cclauses)
39053 {
39054 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
39055 if (*cclauses)
39056 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
39057 if (clauses)
39058 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
39059 }
39060
39061 tree block = begin_omp_structured_block ();
39062 int save = cp_parser_begin_omp_structured_block (parser);
39063 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
39064 cp_parser_end_omp_structured_block (parser, save);
39065 add_stmt (finish_omp_structured_block (block));
39066
39067 return stmt;
39068 }
39069
39070 /* OpenACC 2.0:
39071 # pragma acc kernels oacc-kernels-clause[optseq] new-line
39072 structured-block
39073
39074 or
39075
39076 # pragma acc parallel oacc-parallel-clause[optseq] new-line
39077 structured-block
39078 */
39079
39080 #define OACC_KERNELS_CLAUSE_MASK \
39081 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
39087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
39090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
39091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
39092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
39093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39094
39095 #define OACC_PARALLEL_CLAUSE_MASK \
39096 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
39102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
39104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
39106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
39107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
39108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
39109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
39110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
39111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39112
39113 static tree
39114 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
39115 char *p_name, bool *if_p)
39116 {
39117 omp_clause_mask mask;
39118 enum tree_code code;
39119 switch (cp_parser_pragma_kind (pragma_tok))
39120 {
39121 case PRAGMA_OACC_KERNELS:
39122 strcat (p_name, " kernels");
39123 mask = OACC_KERNELS_CLAUSE_MASK;
39124 code = OACC_KERNELS;
39125 break;
39126 case PRAGMA_OACC_PARALLEL:
39127 strcat (p_name, " parallel");
39128 mask = OACC_PARALLEL_CLAUSE_MASK;
39129 code = OACC_PARALLEL;
39130 break;
39131 default:
39132 gcc_unreachable ();
39133 }
39134
39135 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39136 {
39137 const char *p
39138 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
39139 if (strcmp (p, "loop") == 0)
39140 {
39141 cp_lexer_consume_token (parser->lexer);
39142 tree block = begin_omp_parallel ();
39143 tree clauses;
39144 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
39145 &clauses, if_p);
39146 protected_set_expr_location (stmt, pragma_tok->location);
39147 return finish_omp_construct (code, block, clauses);
39148 }
39149 }
39150
39151 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
39152
39153 tree block = begin_omp_parallel ();
39154 unsigned int save = cp_parser_begin_omp_structured_block (parser);
39155 cp_parser_statement (parser, NULL_TREE, false, if_p);
39156 cp_parser_end_omp_structured_block (parser, save);
39157 return finish_omp_construct (code, block, clauses);
39158 }
39159
39160 /* OpenACC 2.0:
39161 # pragma acc update oacc-update-clause[optseq] new-line
39162 */
39163
39164 #define OACC_UPDATE_CLAUSE_MASK \
39165 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
39167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
39168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
39170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
39171
39172 static tree
39173 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
39174 {
39175 tree stmt, clauses;
39176
39177 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
39178 "#pragma acc update", pragma_tok);
39179
39180 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
39181 {
39182 error_at (pragma_tok->location,
39183 "%<#pragma acc update%> must contain at least one "
39184 "%<device%> or %<host%> or %<self%> clause");
39185 return NULL_TREE;
39186 }
39187
39188 stmt = make_node (OACC_UPDATE);
39189 TREE_TYPE (stmt) = void_type_node;
39190 OACC_UPDATE_CLAUSES (stmt) = clauses;
39191 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39192 add_stmt (stmt);
39193 return stmt;
39194 }
39195
39196 /* OpenACC 2.0:
39197 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
39198
39199 LOC is the location of the #pragma token.
39200 */
39201
39202 #define OACC_WAIT_CLAUSE_MASK \
39203 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
39204
39205 static tree
39206 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
39207 {
39208 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
39209 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39210
39211 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39212 list = cp_parser_oacc_wait_list (parser, loc, list);
39213
39214 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
39215 "#pragma acc wait", pragma_tok);
39216
39217 stmt = c_finish_oacc_wait (loc, list, clauses);
39218 stmt = finish_expr_stmt (stmt);
39219
39220 return stmt;
39221 }
39222
39223 /* OpenMP 4.0:
39224 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
39225
39226 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
39227 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
39231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
39232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
39233
39234 static void
39235 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
39236 enum pragma_context context)
39237 {
39238 bool first_p = parser->omp_declare_simd == NULL;
39239 cp_omp_declare_simd_data data;
39240 if (first_p)
39241 {
39242 data.error_seen = false;
39243 data.fndecl_seen = false;
39244 data.tokens = vNULL;
39245 data.clauses = NULL_TREE;
39246 /* It is safe to take the address of a local variable; it will only be
39247 used while this scope is live. */
39248 parser->omp_declare_simd = &data;
39249 }
39250
39251 /* Store away all pragma tokens. */
39252 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39253 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39254 cp_lexer_consume_token (parser->lexer);
39255 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39256 parser->omp_declare_simd->error_seen = true;
39257 cp_parser_require_pragma_eol (parser, pragma_tok);
39258 struct cp_token_cache *cp
39259 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39260 parser->omp_declare_simd->tokens.safe_push (cp);
39261
39262 if (first_p)
39263 {
39264 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39265 cp_parser_pragma (parser, context, NULL);
39266 switch (context)
39267 {
39268 case pragma_external:
39269 cp_parser_declaration (parser);
39270 break;
39271 case pragma_member:
39272 cp_parser_member_declaration (parser);
39273 break;
39274 case pragma_objc_icode:
39275 cp_parser_block_declaration (parser, /*statement_p=*/false);
39276 break;
39277 default:
39278 cp_parser_declaration_statement (parser);
39279 break;
39280 }
39281 if (parser->omp_declare_simd
39282 && !parser->omp_declare_simd->error_seen
39283 && !parser->omp_declare_simd->fndecl_seen)
39284 error_at (pragma_tok->location,
39285 "%<#pragma omp declare simd%> not immediately followed by "
39286 "function declaration or definition");
39287 data.tokens.release ();
39288 parser->omp_declare_simd = NULL;
39289 }
39290 }
39291
39292 /* Finalize #pragma omp declare simd clauses after direct declarator has
39293 been parsed, and put that into "omp declare simd" attribute. */
39294
39295 static tree
39296 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
39297 {
39298 struct cp_token_cache *ce;
39299 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
39300 int i;
39301
39302 if (!data->error_seen && data->fndecl_seen)
39303 {
39304 error ("%<#pragma omp declare simd%> not immediately followed by "
39305 "a single function declaration or definition");
39306 data->error_seen = true;
39307 }
39308 if (data->error_seen)
39309 return attrs;
39310
39311 FOR_EACH_VEC_ELT (data->tokens, i, ce)
39312 {
39313 tree c, cl;
39314
39315 cp_parser_push_lexer_for_tokens (parser, ce);
39316 parser->lexer->in_pragma = true;
39317 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39318 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39319 cp_lexer_consume_token (parser->lexer);
39320 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
39321 "#pragma omp declare simd", pragma_tok);
39322 cp_parser_pop_lexer (parser);
39323 if (cl)
39324 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
39325 c = build_tree_list (get_identifier ("omp declare simd"), cl);
39326 TREE_CHAIN (c) = attrs;
39327 if (processing_template_decl)
39328 ATTR_IS_DEPENDENT (c) = 1;
39329 attrs = c;
39330 }
39331
39332 data->fndecl_seen = true;
39333 return attrs;
39334 }
39335
39336
39337 /* OpenMP 4.0:
39338 # pragma omp declare target new-line
39339 declarations and definitions
39340 # pragma omp end declare target new-line
39341
39342 OpenMP 4.5:
39343 # pragma omp declare target ( extended-list ) new-line
39344
39345 # pragma omp declare target declare-target-clauses[seq] new-line */
39346
39347 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
39348 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
39350
39351 static void
39352 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
39353 {
39354 tree clauses = NULL_TREE;
39355 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39356 clauses
39357 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
39358 "#pragma omp declare target", pragma_tok);
39359 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39360 {
39361 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
39362 clauses);
39363 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39364 cp_parser_require_pragma_eol (parser, pragma_tok);
39365 }
39366 else
39367 {
39368 cp_parser_require_pragma_eol (parser, pragma_tok);
39369 scope_chain->omp_declare_target_attribute++;
39370 return;
39371 }
39372 if (scope_chain->omp_declare_target_attribute)
39373 error_at (pragma_tok->location,
39374 "%<#pragma omp declare target%> with clauses in between "
39375 "%<#pragma omp declare target%> without clauses and "
39376 "%<#pragma omp end declare target%>");
39377 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
39378 {
39379 tree t = OMP_CLAUSE_DECL (c), id;
39380 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
39381 tree at2 = lookup_attribute ("omp declare target link",
39382 DECL_ATTRIBUTES (t));
39383 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
39384 {
39385 id = get_identifier ("omp declare target link");
39386 std::swap (at1, at2);
39387 }
39388 else
39389 id = get_identifier ("omp declare target");
39390 if (at2)
39391 {
39392 error_at (OMP_CLAUSE_LOCATION (c),
39393 "%qD specified both in declare target %<link%> and %<to%>"
39394 " clauses", t);
39395 continue;
39396 }
39397 if (!at1)
39398 {
39399 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
39400 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
39401 continue;
39402
39403 symtab_node *node = symtab_node::get (t);
39404 if (node != NULL)
39405 {
39406 node->offloadable = 1;
39407 if (ENABLE_OFFLOADING)
39408 {
39409 g->have_offload = true;
39410 if (is_a <varpool_node *> (node))
39411 vec_safe_push (offload_vars, t);
39412 }
39413 }
39414 }
39415 }
39416 }
39417
39418 static void
39419 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
39420 {
39421 const char *p = "";
39422 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39423 {
39424 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39425 p = IDENTIFIER_POINTER (id);
39426 }
39427 if (strcmp (p, "declare") == 0)
39428 {
39429 cp_lexer_consume_token (parser->lexer);
39430 p = "";
39431 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39432 {
39433 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39434 p = IDENTIFIER_POINTER (id);
39435 }
39436 if (strcmp (p, "target") == 0)
39437 cp_lexer_consume_token (parser->lexer);
39438 else
39439 {
39440 cp_parser_error (parser, "expected %<target%>");
39441 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39442 return;
39443 }
39444 }
39445 else
39446 {
39447 cp_parser_error (parser, "expected %<declare%>");
39448 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39449 return;
39450 }
39451 cp_parser_require_pragma_eol (parser, pragma_tok);
39452 if (!scope_chain->omp_declare_target_attribute)
39453 error_at (pragma_tok->location,
39454 "%<#pragma omp end declare target%> without corresponding "
39455 "%<#pragma omp declare target%>");
39456 else
39457 scope_chain->omp_declare_target_attribute--;
39458 }
39459
39460 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39461 expression and optional initializer clause of
39462 #pragma omp declare reduction. We store the expression(s) as
39463 either 3, 6 or 7 special statements inside of the artificial function's
39464 body. The first two statements are DECL_EXPRs for the artificial
39465 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39466 expression that uses those variables.
39467 If there was any INITIALIZER clause, this is followed by further statements,
39468 the fourth and fifth statements are DECL_EXPRs for the artificial
39469 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39470 constructor variant (first token after open paren is not omp_priv),
39471 then the sixth statement is a statement with the function call expression
39472 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39473 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39474 to initialize the OMP_PRIV artificial variable and there is seventh
39475 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39476
39477 static bool
39478 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
39479 {
39480 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
39481 gcc_assert (TYPE_REF_P (type));
39482 type = TREE_TYPE (type);
39483 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
39484 DECL_ARTIFICIAL (omp_out) = 1;
39485 pushdecl (omp_out);
39486 add_decl_expr (omp_out);
39487 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
39488 DECL_ARTIFICIAL (omp_in) = 1;
39489 pushdecl (omp_in);
39490 add_decl_expr (omp_in);
39491 tree combiner;
39492 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
39493
39494 keep_next_level (true);
39495 tree block = begin_omp_structured_block ();
39496 combiner = cp_parser_expression (parser);
39497 finish_expr_stmt (combiner);
39498 block = finish_omp_structured_block (block);
39499 add_stmt (block);
39500
39501 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
39502 return false;
39503
39504 const char *p = "";
39505 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39506 {
39507 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39508 p = IDENTIFIER_POINTER (id);
39509 }
39510
39511 if (strcmp (p, "initializer") == 0)
39512 {
39513 cp_lexer_consume_token (parser->lexer);
39514 matching_parens parens;
39515 if (!parens.require_open (parser))
39516 return false;
39517
39518 p = "";
39519 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39520 {
39521 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39522 p = IDENTIFIER_POINTER (id);
39523 }
39524
39525 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
39526 DECL_ARTIFICIAL (omp_priv) = 1;
39527 pushdecl (omp_priv);
39528 add_decl_expr (omp_priv);
39529 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
39530 DECL_ARTIFICIAL (omp_orig) = 1;
39531 pushdecl (omp_orig);
39532 add_decl_expr (omp_orig);
39533
39534 keep_next_level (true);
39535 block = begin_omp_structured_block ();
39536
39537 bool ctor = false;
39538 if (strcmp (p, "omp_priv") == 0)
39539 {
39540 bool is_direct_init, is_non_constant_init;
39541 ctor = true;
39542 cp_lexer_consume_token (parser->lexer);
39543 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39544 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
39545 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39546 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
39547 == CPP_CLOSE_PAREN
39548 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
39549 == CPP_CLOSE_PAREN))
39550 {
39551 finish_omp_structured_block (block);
39552 error ("invalid initializer clause");
39553 return false;
39554 }
39555 initializer = cp_parser_initializer (parser, &is_direct_init,
39556 &is_non_constant_init);
39557 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
39558 NULL_TREE, LOOKUP_ONLYCONVERTING);
39559 }
39560 else
39561 {
39562 cp_parser_parse_tentatively (parser);
39563 /* Don't create location wrapper nodes here. */
39564 auto_suppress_location_wrappers sentinel;
39565 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
39566 /*check_dependency_p=*/true,
39567 /*template_p=*/NULL,
39568 /*declarator_p=*/false,
39569 /*optional_p=*/false);
39570 vec<tree, va_gc> *args;
39571 if (fn_name == error_mark_node
39572 || cp_parser_error_occurred (parser)
39573 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39574 || ((args = cp_parser_parenthesized_expression_list
39575 (parser, non_attr, /*cast_p=*/false,
39576 /*allow_expansion_p=*/true,
39577 /*non_constant_p=*/NULL)),
39578 cp_parser_error_occurred (parser)))
39579 {
39580 finish_omp_structured_block (block);
39581 cp_parser_abort_tentative_parse (parser);
39582 cp_parser_error (parser, "expected id-expression (arguments)");
39583 return false;
39584 }
39585 unsigned int i;
39586 tree arg;
39587 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
39588 if (arg == omp_priv
39589 || (TREE_CODE (arg) == ADDR_EXPR
39590 && TREE_OPERAND (arg, 0) == omp_priv))
39591 break;
39592 cp_parser_abort_tentative_parse (parser);
39593 if (arg == NULL_TREE)
39594 error ("one of the initializer call arguments should be %<omp_priv%>"
39595 " or %<&omp_priv%>");
39596 initializer = cp_parser_postfix_expression (parser, false, false, false,
39597 false, NULL);
39598 finish_expr_stmt (initializer);
39599 }
39600
39601 block = finish_omp_structured_block (block);
39602 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
39603 add_stmt (block);
39604
39605 if (ctor)
39606 add_decl_expr (omp_orig);
39607
39608 if (!parens.require_close (parser))
39609 return false;
39610 }
39611
39612 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
39613 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
39614 UNKNOWN_LOCATION);
39615
39616 return true;
39617 }
39618
39619 /* OpenMP 4.0
39620 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39621 initializer-clause[opt] new-line
39622
39623 initializer-clause:
39624 initializer (omp_priv initializer)
39625 initializer (function-name (argument-list)) */
39626
39627 static void
39628 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
39629 enum pragma_context)
39630 {
39631 auto_vec<tree> types;
39632 enum tree_code reduc_code = ERROR_MARK;
39633 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
39634 unsigned int i;
39635 cp_token *first_token;
39636 cp_token_cache *cp;
39637 int errs;
39638 void *p;
39639
39640 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39641 p = obstack_alloc (&declarator_obstack, 0);
39642
39643 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39644 goto fail;
39645
39646 switch (cp_lexer_peek_token (parser->lexer)->type)
39647 {
39648 case CPP_PLUS:
39649 reduc_code = PLUS_EXPR;
39650 break;
39651 case CPP_MULT:
39652 reduc_code = MULT_EXPR;
39653 break;
39654 case CPP_MINUS:
39655 reduc_code = MINUS_EXPR;
39656 break;
39657 case CPP_AND:
39658 reduc_code = BIT_AND_EXPR;
39659 break;
39660 case CPP_XOR:
39661 reduc_code = BIT_XOR_EXPR;
39662 break;
39663 case CPP_OR:
39664 reduc_code = BIT_IOR_EXPR;
39665 break;
39666 case CPP_AND_AND:
39667 reduc_code = TRUTH_ANDIF_EXPR;
39668 break;
39669 case CPP_OR_OR:
39670 reduc_code = TRUTH_ORIF_EXPR;
39671 break;
39672 case CPP_NAME:
39673 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
39674 break;
39675 default:
39676 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39677 "%<|%>, %<&&%>, %<||%> or identifier");
39678 goto fail;
39679 }
39680
39681 if (reduc_code != ERROR_MARK)
39682 cp_lexer_consume_token (parser->lexer);
39683
39684 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
39685 if (reduc_id == error_mark_node)
39686 goto fail;
39687
39688 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39689 goto fail;
39690
39691 /* Types may not be defined in declare reduction type list. */
39692 const char *saved_message;
39693 saved_message = parser->type_definition_forbidden_message;
39694 parser->type_definition_forbidden_message
39695 = G_("types may not be defined in declare reduction type list");
39696 bool saved_colon_corrects_to_scope_p;
39697 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39698 parser->colon_corrects_to_scope_p = false;
39699 bool saved_colon_doesnt_start_class_def_p;
39700 saved_colon_doesnt_start_class_def_p
39701 = parser->colon_doesnt_start_class_def_p;
39702 parser->colon_doesnt_start_class_def_p = true;
39703
39704 while (true)
39705 {
39706 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39707 type = cp_parser_type_id (parser);
39708 if (type == error_mark_node)
39709 ;
39710 else if (ARITHMETIC_TYPE_P (type)
39711 && (orig_reduc_id == NULL_TREE
39712 || (TREE_CODE (type) != COMPLEX_TYPE
39713 && (id_equal (orig_reduc_id, "min")
39714 || id_equal (orig_reduc_id, "max")))))
39715 error_at (loc, "predeclared arithmetic type %qT in "
39716 "%<#pragma omp declare reduction%>", type);
39717 else if (FUNC_OR_METHOD_TYPE_P (type)
39718 || TREE_CODE (type) == ARRAY_TYPE)
39719 error_at (loc, "function or array type %qT in "
39720 "%<#pragma omp declare reduction%>", type);
39721 else if (TYPE_REF_P (type))
39722 error_at (loc, "reference type %qT in "
39723 "%<#pragma omp declare reduction%>", type);
39724 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
39725 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
39726 "type %qT in %<#pragma omp declare reduction%>", type);
39727 else
39728 types.safe_push (type);
39729
39730 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39731 cp_lexer_consume_token (parser->lexer);
39732 else
39733 break;
39734 }
39735
39736 /* Restore the saved message. */
39737 parser->type_definition_forbidden_message = saved_message;
39738 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39739 parser->colon_doesnt_start_class_def_p
39740 = saved_colon_doesnt_start_class_def_p;
39741
39742 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
39743 || types.is_empty ())
39744 {
39745 fail:
39746 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39747 goto done;
39748 }
39749
39750 first_token = cp_lexer_peek_token (parser->lexer);
39751 cp = NULL;
39752 errs = errorcount;
39753 FOR_EACH_VEC_ELT (types, i, type)
39754 {
39755 tree fntype
39756 = build_function_type_list (void_type_node,
39757 cp_build_reference_type (type, false),
39758 NULL_TREE);
39759 tree this_reduc_id = reduc_id;
39760 if (!dependent_type_p (type))
39761 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39762 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39763 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39764 DECL_ARTIFICIAL (fndecl) = 1;
39765 DECL_EXTERNAL (fndecl) = 1;
39766 DECL_DECLARED_INLINE_P (fndecl) = 1;
39767 DECL_IGNORED_P (fndecl) = 1;
39768 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39769 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39770 DECL_ATTRIBUTES (fndecl)
39771 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39772 DECL_ATTRIBUTES (fndecl));
39773 if (processing_template_decl)
39774 fndecl = push_template_decl (fndecl);
39775 bool block_scope = false;
39776 tree block = NULL_TREE;
39777 if (current_function_decl)
39778 {
39779 block_scope = true;
39780 DECL_CONTEXT (fndecl) = global_namespace;
39781 if (!processing_template_decl)
39782 pushdecl (fndecl);
39783 }
39784 else if (current_class_type)
39785 {
39786 if (cp == NULL)
39787 {
39788 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39789 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39790 cp_lexer_consume_token (parser->lexer);
39791 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39792 goto fail;
39793 cp = cp_token_cache_new (first_token,
39794 cp_lexer_peek_nth_token (parser->lexer,
39795 2));
39796 }
39797 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39798 finish_member_declaration (fndecl);
39799 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39800 DECL_PENDING_INLINE_P (fndecl) = 1;
39801 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39802 continue;
39803 }
39804 else
39805 {
39806 DECL_CONTEXT (fndecl) = current_namespace;
39807 pushdecl (fndecl);
39808 }
39809 if (!block_scope)
39810 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39811 else
39812 block = begin_omp_structured_block ();
39813 if (cp)
39814 {
39815 cp_parser_push_lexer_for_tokens (parser, cp);
39816 parser->lexer->in_pragma = true;
39817 }
39818 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39819 {
39820 if (!block_scope)
39821 finish_function (/*inline_p=*/false);
39822 else
39823 DECL_CONTEXT (fndecl) = current_function_decl;
39824 if (cp)
39825 cp_parser_pop_lexer (parser);
39826 goto fail;
39827 }
39828 if (cp)
39829 cp_parser_pop_lexer (parser);
39830 if (!block_scope)
39831 finish_function (/*inline_p=*/false);
39832 else
39833 {
39834 DECL_CONTEXT (fndecl) = current_function_decl;
39835 block = finish_omp_structured_block (block);
39836 if (TREE_CODE (block) == BIND_EXPR)
39837 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39838 else if (TREE_CODE (block) == STATEMENT_LIST)
39839 DECL_SAVED_TREE (fndecl) = block;
39840 if (processing_template_decl)
39841 add_decl_expr (fndecl);
39842 }
39843 cp_check_omp_declare_reduction (fndecl);
39844 if (cp == NULL && types.length () > 1)
39845 cp = cp_token_cache_new (first_token,
39846 cp_lexer_peek_nth_token (parser->lexer, 2));
39847 if (errs != errorcount)
39848 break;
39849 }
39850
39851 cp_parser_require_pragma_eol (parser, pragma_tok);
39852
39853 done:
39854 /* Free any declarators allocated. */
39855 obstack_free (&declarator_obstack, p);
39856 }
39857
39858 /* OpenMP 4.0
39859 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39860 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39861 initializer-clause[opt] new-line
39862 #pragma omp declare target new-line */
39863
39864 static bool
39865 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39866 enum pragma_context context)
39867 {
39868 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39869 {
39870 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39871 const char *p = IDENTIFIER_POINTER (id);
39872
39873 if (strcmp (p, "simd") == 0)
39874 {
39875 cp_lexer_consume_token (parser->lexer);
39876 cp_parser_omp_declare_simd (parser, pragma_tok,
39877 context);
39878 return true;
39879 }
39880 cp_ensure_no_omp_declare_simd (parser);
39881 if (strcmp (p, "reduction") == 0)
39882 {
39883 cp_lexer_consume_token (parser->lexer);
39884 cp_parser_omp_declare_reduction (parser, pragma_tok,
39885 context);
39886 return false;
39887 }
39888 if (!flag_openmp) /* flag_openmp_simd */
39889 {
39890 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39891 return false;
39892 }
39893 if (strcmp (p, "target") == 0)
39894 {
39895 cp_lexer_consume_token (parser->lexer);
39896 cp_parser_omp_declare_target (parser, pragma_tok);
39897 return false;
39898 }
39899 }
39900 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39901 "or %<target%>");
39902 cp_parser_require_pragma_eol (parser, pragma_tok);
39903 return false;
39904 }
39905
39906 /* OpenMP 5.0
39907 #pragma omp requires clauses[optseq] new-line */
39908
39909 static bool
39910 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39911 {
39912 bool first = true;
39913 enum omp_requires new_req = (enum omp_requires) 0;
39914
39915 location_t loc = pragma_tok->location;
39916 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39917 {
39918 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39919 cp_lexer_consume_token (parser->lexer);
39920
39921 first = false;
39922
39923 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39924 {
39925 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39926 const char *p = IDENTIFIER_POINTER (id);
39927 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39928 enum omp_requires this_req = (enum omp_requires) 0;
39929
39930 if (!strcmp (p, "unified_address"))
39931 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39932 else if (!strcmp (p, "unified_shared_memory"))
39933 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39934 else if (!strcmp (p, "dynamic_allocators"))
39935 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39936 else if (!strcmp (p, "reverse_offload"))
39937 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39938 else if (!strcmp (p, "atomic_default_mem_order"))
39939 {
39940 cp_lexer_consume_token (parser->lexer);
39941
39942 matching_parens parens;
39943 if (parens.require_open (parser))
39944 {
39945 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39946 {
39947 id = cp_lexer_peek_token (parser->lexer)->u.value;
39948 p = IDENTIFIER_POINTER (id);
39949
39950 if (!strcmp (p, "seq_cst"))
39951 this_req
39952 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39953 else if (!strcmp (p, "relaxed"))
39954 this_req
39955 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39956 else if (!strcmp (p, "acq_rel"))
39957 this_req
39958 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39959 }
39960 if (this_req == 0)
39961 {
39962 error_at (cp_lexer_peek_token (parser->lexer)->location,
39963 "expected %<seq_cst%>, %<relaxed%> or "
39964 "%<acq_rel%>");
39965 if (cp_lexer_nth_token_is (parser->lexer, 2,
39966 CPP_CLOSE_PAREN))
39967 cp_lexer_consume_token (parser->lexer);
39968 }
39969 else
39970 cp_lexer_consume_token (parser->lexer);
39971
39972 if (!parens.require_close (parser))
39973 cp_parser_skip_to_closing_parenthesis (parser,
39974 /*recovering=*/true,
39975 /*or_comma=*/false,
39976 /*consume_paren=*/
39977 true);
39978
39979 if (this_req == 0)
39980 {
39981 cp_parser_require_pragma_eol (parser, pragma_tok);
39982 return false;
39983 }
39984 }
39985 p = NULL;
39986 }
39987 else
39988 {
39989 error_at (cloc, "expected %<unified_address%>, "
39990 "%<unified_shared_memory%>, "
39991 "%<dynamic_allocators%>, "
39992 "%<reverse_offload%> "
39993 "or %<atomic_default_mem_order%> clause");
39994 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39995 return false;
39996 }
39997 if (p)
39998 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39999 "supported yet", p);
40000 if (p)
40001 cp_lexer_consume_token (parser->lexer);
40002 if (this_req)
40003 {
40004 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
40005 {
40006 if ((this_req & new_req) != 0)
40007 error_at (cloc, "too many %qs clauses", p);
40008 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
40009 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
40010 error_at (cloc, "%qs clause used lexically after first "
40011 "target construct or offloading API", p);
40012 }
40013 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
40014 {
40015 error_at (cloc, "too many %qs clauses",
40016 "atomic_default_mem_order");
40017 this_req = (enum omp_requires) 0;
40018 }
40019 else if ((omp_requires_mask
40020 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
40021 {
40022 error_at (cloc, "more than one %<atomic_default_mem_order%>"
40023 " clause in a single compilation unit");
40024 this_req
40025 = (enum omp_requires)
40026 (omp_requires_mask
40027 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
40028 }
40029 else if ((omp_requires_mask
40030 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
40031 error_at (cloc, "%<atomic_default_mem_order%> clause used "
40032 "lexically after first %<atomic%> construct "
40033 "without memory order clause");
40034 new_req = (enum omp_requires) (new_req | this_req);
40035 omp_requires_mask
40036 = (enum omp_requires) (omp_requires_mask | this_req);
40037 continue;
40038 }
40039 }
40040 break;
40041 }
40042 cp_parser_require_pragma_eol (parser, pragma_tok);
40043
40044 if (new_req == 0)
40045 error_at (loc, "%<pragma omp requires%> requires at least one clause");
40046 return false;
40047 }
40048
40049
40050 /* OpenMP 4.5:
40051 #pragma omp taskloop taskloop-clause[optseq] new-line
40052 for-loop
40053
40054 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
40055 for-loop */
40056
40057 #define OMP_TASKLOOP_CLAUSE_MASK \
40058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
40063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
40064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
40065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
40066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
40067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
40069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
40070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
40071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
40072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
40074
40075 static tree
40076 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
40077 char *p_name, omp_clause_mask mask, tree *cclauses,
40078 bool *if_p)
40079 {
40080 tree clauses, sb, ret;
40081 unsigned int save;
40082 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40083
40084 strcat (p_name, " taskloop");
40085 mask |= OMP_TASKLOOP_CLAUSE_MASK;
40086 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
40087 clause. */
40088 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
40089 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
40090
40091 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40092 {
40093 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40094 const char *p = IDENTIFIER_POINTER (id);
40095
40096 if (strcmp (p, "simd") == 0)
40097 {
40098 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40099 if (cclauses == NULL)
40100 cclauses = cclauses_buf;
40101
40102 cp_lexer_consume_token (parser->lexer);
40103 if (!flag_openmp) /* flag_openmp_simd */
40104 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40105 cclauses, if_p);
40106 sb = begin_omp_structured_block ();
40107 save = cp_parser_begin_omp_structured_block (parser);
40108 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40109 cclauses, if_p);
40110 cp_parser_end_omp_structured_block (parser, save);
40111 tree body = finish_omp_structured_block (sb);
40112 if (ret == NULL)
40113 return ret;
40114 ret = make_node (OMP_TASKLOOP);
40115 TREE_TYPE (ret) = void_type_node;
40116 OMP_FOR_BODY (ret) = body;
40117 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
40118 SET_EXPR_LOCATION (ret, loc);
40119 add_stmt (ret);
40120 return ret;
40121 }
40122 }
40123 if (!flag_openmp) /* flag_openmp_simd */
40124 {
40125 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40126 return NULL_TREE;
40127 }
40128
40129 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40130 cclauses == NULL);
40131 if (cclauses)
40132 {
40133 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
40134 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
40135 }
40136
40137 keep_next_level (true);
40138 sb = begin_omp_structured_block ();
40139 save = cp_parser_begin_omp_structured_block (parser);
40140
40141 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
40142 if_p);
40143
40144 cp_parser_end_omp_structured_block (parser, save);
40145 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40146
40147 return ret;
40148 }
40149
40150
40151 /* OpenACC 2.0:
40152 # pragma acc routine oacc-routine-clause[optseq] new-line
40153 function-definition
40154
40155 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
40156 */
40157
40158 #define OACC_ROUTINE_CLAUSE_MASK \
40159 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
40160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
40161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
40162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
40163
40164
40165 /* Parse the OpenACC routine pragma. This has an optional '( name )'
40166 component, which must resolve to a declared namespace-scope
40167 function. The clauses are either processed directly (for a named
40168 function), or defered until the immediatley following declaration
40169 is parsed. */
40170
40171 static void
40172 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
40173 enum pragma_context context)
40174 {
40175 gcc_checking_assert (context == pragma_external);
40176 /* The checking for "another pragma following this one" in the "no optional
40177 '( name )'" case makes sure that we dont re-enter. */
40178 gcc_checking_assert (parser->oacc_routine == NULL);
40179
40180 cp_oacc_routine_data data;
40181 data.error_seen = false;
40182 data.fndecl_seen = false;
40183 data.tokens = vNULL;
40184 data.clauses = NULL_TREE;
40185 data.loc = pragma_tok->location;
40186 /* It is safe to take the address of a local variable; it will only be
40187 used while this scope is live. */
40188 parser->oacc_routine = &data;
40189
40190 /* Look for optional '( name )'. */
40191 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
40192 {
40193 matching_parens parens;
40194 parens.consume_open (parser); /* '(' */
40195
40196 /* We parse the name as an id-expression. If it resolves to
40197 anything other than a non-overloaded function at namespace
40198 scope, it's an error. */
40199 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
40200 tree name = cp_parser_id_expression (parser,
40201 /*template_keyword_p=*/false,
40202 /*check_dependency_p=*/false,
40203 /*template_p=*/NULL,
40204 /*declarator_p=*/false,
40205 /*optional_p=*/false);
40206 tree decl = (identifier_p (name)
40207 ? cp_parser_lookup_name_simple (parser, name, name_loc)
40208 : name);
40209 if (name != error_mark_node && decl == error_mark_node)
40210 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
40211
40212 if (decl == error_mark_node
40213 || !parens.require_close (parser))
40214 {
40215 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40216 parser->oacc_routine = NULL;
40217 return;
40218 }
40219
40220 data.clauses
40221 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40222 "#pragma acc routine",
40223 cp_lexer_peek_token (parser->lexer));
40224 /* The clauses are in reverse order; fix that to make later diagnostic
40225 emission easier. */
40226 data.clauses = nreverse (data.clauses);
40227
40228 if (decl && is_overloaded_fn (decl)
40229 && (TREE_CODE (decl) != FUNCTION_DECL
40230 || DECL_FUNCTION_TEMPLATE_P (decl)))
40231 {
40232 error_at (name_loc,
40233 "%<#pragma acc routine%> names a set of overloads");
40234 parser->oacc_routine = NULL;
40235 return;
40236 }
40237
40238 /* Perhaps we should use the same rule as declarations in different
40239 namespaces? */
40240 if (!DECL_NAMESPACE_SCOPE_P (decl))
40241 {
40242 error_at (name_loc,
40243 "%qD does not refer to a namespace scope function", decl);
40244 parser->oacc_routine = NULL;
40245 return;
40246 }
40247
40248 if (TREE_CODE (decl) != FUNCTION_DECL)
40249 {
40250 error_at (name_loc, "%qD does not refer to a function", decl);
40251 parser->oacc_routine = NULL;
40252 return;
40253 }
40254
40255 cp_finalize_oacc_routine (parser, decl, false);
40256 parser->oacc_routine = NULL;
40257 }
40258 else /* No optional '( name )'. */
40259 {
40260 /* Store away all pragma tokens. */
40261 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
40262 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
40263 cp_lexer_consume_token (parser->lexer);
40264 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40265 parser->oacc_routine->error_seen = true;
40266 cp_parser_require_pragma_eol (parser, pragma_tok);
40267 struct cp_token_cache *cp
40268 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
40269 parser->oacc_routine->tokens.safe_push (cp);
40270
40271 /* Emit a helpful diagnostic if there's another pragma following this
40272 one. */
40273 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
40274 {
40275 cp_ensure_no_oacc_routine (parser);
40276 data.tokens.release ();
40277 /* ..., and then just keep going. */
40278 return;
40279 }
40280
40281 /* We only have to consider the pragma_external case here. */
40282 cp_parser_declaration (parser);
40283 if (parser->oacc_routine
40284 && !parser->oacc_routine->fndecl_seen)
40285 cp_ensure_no_oacc_routine (parser);
40286 else
40287 parser->oacc_routine = NULL;
40288 data.tokens.release ();
40289 }
40290 }
40291
40292 /* Finalize #pragma acc routine clauses after direct declarator has
40293 been parsed. */
40294
40295 static tree
40296 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
40297 {
40298 struct cp_token_cache *ce;
40299 cp_oacc_routine_data *data = parser->oacc_routine;
40300
40301 if (!data->error_seen && data->fndecl_seen)
40302 {
40303 error_at (data->loc,
40304 "%<#pragma acc routine%> not immediately followed by "
40305 "a single function declaration or definition");
40306 data->error_seen = true;
40307 }
40308 if (data->error_seen)
40309 return attrs;
40310
40311 gcc_checking_assert (data->tokens.length () == 1);
40312 ce = data->tokens[0];
40313
40314 cp_parser_push_lexer_for_tokens (parser, ce);
40315 parser->lexer->in_pragma = true;
40316 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
40317
40318 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
40319 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
40320 parser->oacc_routine->clauses
40321 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40322 "#pragma acc routine", pragma_tok);
40323 /* The clauses are in reverse order; fix that to make later diagnostic
40324 emission easier. */
40325 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
40326 cp_parser_pop_lexer (parser);
40327 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40328 fndecl_seen. */
40329
40330 return attrs;
40331 }
40332
40333 /* Apply any saved OpenACC routine clauses to a just-parsed
40334 declaration. */
40335
40336 static void
40337 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
40338 {
40339 if (__builtin_expect (parser->oacc_routine != NULL, 0))
40340 {
40341 /* Keep going if we're in error reporting mode. */
40342 if (parser->oacc_routine->error_seen
40343 || fndecl == error_mark_node)
40344 return;
40345
40346 if (parser->oacc_routine->fndecl_seen)
40347 {
40348 error_at (parser->oacc_routine->loc,
40349 "%<#pragma acc routine%> not immediately followed by"
40350 " a single function declaration or definition");
40351 parser->oacc_routine = NULL;
40352 return;
40353 }
40354 if (TREE_CODE (fndecl) != FUNCTION_DECL)
40355 {
40356 cp_ensure_no_oacc_routine (parser);
40357 return;
40358 }
40359
40360 int compatible
40361 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
40362 parser->oacc_routine->loc,
40363 "#pragma acc routine");
40364 if (compatible < 0)
40365 {
40366 parser->oacc_routine = NULL;
40367 return;
40368 }
40369 if (compatible > 0)
40370 {
40371 }
40372 else
40373 {
40374 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
40375 {
40376 error_at (parser->oacc_routine->loc,
40377 TREE_USED (fndecl)
40378 ? G_("%<#pragma acc routine%> must be applied before"
40379 " use")
40380 : G_("%<#pragma acc routine%> must be applied before"
40381 " definition"));
40382 parser->oacc_routine = NULL;
40383 return;
40384 }
40385
40386 /* Set the routine's level of parallelism. */
40387 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
40388 oacc_replace_fn_attrib (fndecl, dims);
40389
40390 /* Add an "omp declare target" attribute. */
40391 DECL_ATTRIBUTES (fndecl)
40392 = tree_cons (get_identifier ("omp declare target"),
40393 parser->oacc_routine->clauses,
40394 DECL_ATTRIBUTES (fndecl));
40395 }
40396
40397 /* Don't unset parser->oacc_routine here: we may still need it to
40398 diagnose wrong usage. But, remember that we've used this "#pragma acc
40399 routine". */
40400 parser->oacc_routine->fndecl_seen = true;
40401 }
40402 }
40403
40404 /* Main entry point to OpenMP statement pragmas. */
40405
40406 static void
40407 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40408 {
40409 tree stmt;
40410 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
40411 omp_clause_mask mask (0);
40412
40413 switch (cp_parser_pragma_kind (pragma_tok))
40414 {
40415 case PRAGMA_OACC_ATOMIC:
40416 cp_parser_omp_atomic (parser, pragma_tok);
40417 return;
40418 case PRAGMA_OACC_CACHE:
40419 stmt = cp_parser_oacc_cache (parser, pragma_tok);
40420 break;
40421 case PRAGMA_OACC_DATA:
40422 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
40423 break;
40424 case PRAGMA_OACC_ENTER_DATA:
40425 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
40426 break;
40427 case PRAGMA_OACC_EXIT_DATA:
40428 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
40429 break;
40430 case PRAGMA_OACC_HOST_DATA:
40431 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
40432 break;
40433 case PRAGMA_OACC_KERNELS:
40434 case PRAGMA_OACC_PARALLEL:
40435 strcpy (p_name, "#pragma acc");
40436 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
40437 if_p);
40438 break;
40439 case PRAGMA_OACC_LOOP:
40440 strcpy (p_name, "#pragma acc");
40441 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
40442 if_p);
40443 break;
40444 case PRAGMA_OACC_UPDATE:
40445 stmt = cp_parser_oacc_update (parser, pragma_tok);
40446 break;
40447 case PRAGMA_OACC_WAIT:
40448 stmt = cp_parser_oacc_wait (parser, pragma_tok);
40449 break;
40450 case PRAGMA_OMP_ATOMIC:
40451 cp_parser_omp_atomic (parser, pragma_tok);
40452 return;
40453 case PRAGMA_OMP_CRITICAL:
40454 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
40455 break;
40456 case PRAGMA_OMP_DISTRIBUTE:
40457 strcpy (p_name, "#pragma omp");
40458 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
40459 if_p);
40460 break;
40461 case PRAGMA_OMP_FOR:
40462 strcpy (p_name, "#pragma omp");
40463 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
40464 if_p);
40465 break;
40466 case PRAGMA_OMP_MASTER:
40467 strcpy (p_name, "#pragma omp");
40468 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
40469 if_p);
40470 break;
40471 case PRAGMA_OMP_PARALLEL:
40472 strcpy (p_name, "#pragma omp");
40473 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
40474 if_p);
40475 break;
40476 case PRAGMA_OMP_SECTIONS:
40477 strcpy (p_name, "#pragma omp");
40478 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
40479 break;
40480 case PRAGMA_OMP_SIMD:
40481 strcpy (p_name, "#pragma omp");
40482 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
40483 if_p);
40484 break;
40485 case PRAGMA_OMP_SINGLE:
40486 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
40487 break;
40488 case PRAGMA_OMP_TASK:
40489 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
40490 break;
40491 case PRAGMA_OMP_TASKGROUP:
40492 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
40493 break;
40494 case PRAGMA_OMP_TASKLOOP:
40495 strcpy (p_name, "#pragma omp");
40496 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
40497 if_p);
40498 break;
40499 case PRAGMA_OMP_TEAMS:
40500 strcpy (p_name, "#pragma omp");
40501 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
40502 if_p);
40503 break;
40504 default:
40505 gcc_unreachable ();
40506 }
40507
40508 protected_set_expr_location (stmt, pragma_tok->location);
40509 }
40510 \f
40511 /* Transactional Memory parsing routines. */
40512
40513 /* Parse a transaction attribute.
40514
40515 txn-attribute:
40516 attribute
40517 [ [ identifier ] ]
40518
40519 We use this instead of cp_parser_attributes_opt for transactions to avoid
40520 the pedwarn in C++98 mode. */
40521
40522 static tree
40523 cp_parser_txn_attribute_opt (cp_parser *parser)
40524 {
40525 cp_token *token;
40526 tree attr_name, attr = NULL;
40527
40528 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
40529 return cp_parser_attributes_opt (parser);
40530
40531 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
40532 return NULL_TREE;
40533 cp_lexer_consume_token (parser->lexer);
40534 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
40535 goto error1;
40536
40537 token = cp_lexer_peek_token (parser->lexer);
40538 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
40539 {
40540 token = cp_lexer_consume_token (parser->lexer);
40541
40542 attr_name = (token->type == CPP_KEYWORD
40543 /* For keywords, use the canonical spelling,
40544 not the parsed identifier. */
40545 ? ridpointers[(int) token->keyword]
40546 : token->u.value);
40547 attr = build_tree_list (attr_name, NULL_TREE);
40548 }
40549 else
40550 cp_parser_error (parser, "expected identifier");
40551
40552 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40553 error1:
40554 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40555 return attr;
40556 }
40557
40558 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40559
40560 transaction-statement:
40561 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40562 compound-statement
40563 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40564 */
40565
40566 static tree
40567 cp_parser_transaction (cp_parser *parser, cp_token *token)
40568 {
40569 unsigned char old_in = parser->in_transaction;
40570 unsigned char this_in = 1, new_in;
40571 enum rid keyword = token->keyword;
40572 tree stmt, attrs, noex;
40573
40574 cp_lexer_consume_token (parser->lexer);
40575
40576 if (keyword == RID_TRANSACTION_RELAXED
40577 || keyword == RID_SYNCHRONIZED)
40578 this_in |= TM_STMT_ATTR_RELAXED;
40579 else
40580 {
40581 attrs = cp_parser_txn_attribute_opt (parser);
40582 if (attrs)
40583 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40584 }
40585
40586 /* Parse a noexcept specification. */
40587 if (keyword == RID_ATOMIC_NOEXCEPT)
40588 noex = boolean_true_node;
40589 else if (keyword == RID_ATOMIC_CANCEL)
40590 {
40591 /* cancel-and-throw is unimplemented. */
40592 sorry ("%<atomic_cancel%>");
40593 noex = NULL_TREE;
40594 }
40595 else
40596 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
40597
40598 /* Keep track if we're in the lexical scope of an outer transaction. */
40599 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
40600
40601 stmt = begin_transaction_stmt (token->location, NULL, this_in);
40602
40603 parser->in_transaction = new_in;
40604 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
40605 parser->in_transaction = old_in;
40606
40607 finish_transaction_stmt (stmt, NULL, this_in, noex);
40608
40609 return stmt;
40610 }
40611
40612 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40613
40614 transaction-expression:
40615 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40616 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40617 */
40618
40619 static tree
40620 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
40621 {
40622 unsigned char old_in = parser->in_transaction;
40623 unsigned char this_in = 1;
40624 cp_token *token;
40625 tree expr, noex;
40626 bool noex_expr;
40627 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40628
40629 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40630 || keyword == RID_TRANSACTION_RELAXED);
40631
40632 if (!flag_tm)
40633 error_at (loc,
40634 keyword == RID_TRANSACTION_RELAXED
40635 ? G_("%<__transaction_relaxed%> without transactional memory "
40636 "support enabled")
40637 : G_("%<__transaction_atomic%> without transactional memory "
40638 "support enabled"));
40639
40640 token = cp_parser_require_keyword (parser, keyword,
40641 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40642 : RT_TRANSACTION_RELAXED));
40643 gcc_assert (token != NULL);
40644
40645 if (keyword == RID_TRANSACTION_RELAXED)
40646 this_in |= TM_STMT_ATTR_RELAXED;
40647
40648 /* Set this early. This might mean that we allow transaction_cancel in
40649 an expression that we find out later actually has to be a constexpr.
40650 However, we expect that cxx_constant_value will be able to deal with
40651 this; also, if the noexcept has no constexpr, then what we parse next
40652 really is a transaction's body. */
40653 parser->in_transaction = this_in;
40654
40655 /* Parse a noexcept specification. */
40656 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
40657 true);
40658
40659 if (!noex || !noex_expr
40660 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40661 {
40662 matching_parens parens;
40663 parens.require_open (parser);
40664
40665 expr = cp_parser_expression (parser);
40666 expr = finish_parenthesized_expr (expr);
40667
40668 parens.require_close (parser);
40669 }
40670 else
40671 {
40672 /* The only expression that is available got parsed for the noexcept
40673 already. noexcept is true then. */
40674 expr = noex;
40675 noex = boolean_true_node;
40676 }
40677
40678 expr = build_transaction_expr (token->location, expr, this_in, noex);
40679 parser->in_transaction = old_in;
40680
40681 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
40682 return error_mark_node;
40683
40684 return (flag_tm ? expr : error_mark_node);
40685 }
40686
40687 /* Parse a function-transaction-block.
40688
40689 function-transaction-block:
40690 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40691 function-body
40692 __transaction_atomic txn-attribute[opt] function-try-block
40693 __transaction_relaxed ctor-initializer[opt] function-body
40694 __transaction_relaxed function-try-block
40695 */
40696
40697 static void
40698 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
40699 {
40700 unsigned char old_in = parser->in_transaction;
40701 unsigned char new_in = 1;
40702 tree compound_stmt, stmt, attrs;
40703 cp_token *token;
40704
40705 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40706 || keyword == RID_TRANSACTION_RELAXED);
40707 token = cp_parser_require_keyword (parser, keyword,
40708 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40709 : RT_TRANSACTION_RELAXED));
40710 gcc_assert (token != NULL);
40711
40712 if (keyword == RID_TRANSACTION_RELAXED)
40713 new_in |= TM_STMT_ATTR_RELAXED;
40714 else
40715 {
40716 attrs = cp_parser_txn_attribute_opt (parser);
40717 if (attrs)
40718 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40719 }
40720
40721 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
40722
40723 parser->in_transaction = new_in;
40724
40725 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
40726 cp_parser_function_try_block (parser);
40727 else
40728 cp_parser_ctor_initializer_opt_and_function_body
40729 (parser, /*in_function_try_block=*/false);
40730
40731 parser->in_transaction = old_in;
40732
40733 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
40734 }
40735
40736 /* Parse a __transaction_cancel statement.
40737
40738 cancel-statement:
40739 __transaction_cancel txn-attribute[opt] ;
40740 __transaction_cancel txn-attribute[opt] throw-expression ;
40741
40742 ??? Cancel and throw is not yet implemented. */
40743
40744 static tree
40745 cp_parser_transaction_cancel (cp_parser *parser)
40746 {
40747 cp_token *token;
40748 bool is_outer = false;
40749 tree stmt, attrs;
40750
40751 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
40752 RT_TRANSACTION_CANCEL);
40753 gcc_assert (token != NULL);
40754
40755 attrs = cp_parser_txn_attribute_opt (parser);
40756 if (attrs)
40757 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
40758
40759 /* ??? Parse cancel-and-throw here. */
40760
40761 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40762
40763 if (!flag_tm)
40764 {
40765 error_at (token->location, "%<__transaction_cancel%> without "
40766 "transactional memory support enabled");
40767 return error_mark_node;
40768 }
40769 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
40770 {
40771 error_at (token->location, "%<__transaction_cancel%> within a "
40772 "%<__transaction_relaxed%>");
40773 return error_mark_node;
40774 }
40775 else if (is_outer)
40776 {
40777 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40778 && !is_tm_may_cancel_outer (current_function_decl))
40779 {
40780 error_at (token->location, "outer %<__transaction_cancel%> not "
40781 "within outer %<__transaction_atomic%>");
40782 error_at (token->location,
40783 " or a %<transaction_may_cancel_outer%> function");
40784 return error_mark_node;
40785 }
40786 }
40787 else if (parser->in_transaction == 0)
40788 {
40789 error_at (token->location, "%<__transaction_cancel%> not within "
40790 "%<__transaction_atomic%>");
40791 return error_mark_node;
40792 }
40793
40794 stmt = build_tm_abort_call (token->location, is_outer);
40795 add_stmt (stmt);
40796
40797 return stmt;
40798 }
40799 \f
40800 /* The parser. */
40801
40802 static GTY (()) cp_parser *the_parser;
40803
40804 \f
40805 /* Special handling for the first token or line in the file. The first
40806 thing in the file might be #pragma GCC pch_preprocess, which loads a
40807 PCH file, which is a GC collection point. So we need to handle this
40808 first pragma without benefit of an existing lexer structure.
40809
40810 Always returns one token to the caller in *FIRST_TOKEN. This is
40811 either the true first token of the file, or the first token after
40812 the initial pragma. */
40813
40814 static void
40815 cp_parser_initial_pragma (cp_token *first_token)
40816 {
40817 tree name = NULL;
40818
40819 cp_lexer_get_preprocessor_token (NULL, first_token);
40820 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40821 return;
40822
40823 cp_lexer_get_preprocessor_token (NULL, first_token);
40824 if (first_token->type == CPP_STRING)
40825 {
40826 name = first_token->u.value;
40827
40828 cp_lexer_get_preprocessor_token (NULL, first_token);
40829 if (first_token->type != CPP_PRAGMA_EOL)
40830 error_at (first_token->location,
40831 "junk at end of %<#pragma GCC pch_preprocess%>");
40832 }
40833 else
40834 error_at (first_token->location, "expected string literal");
40835
40836 /* Skip to the end of the pragma. */
40837 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40838 cp_lexer_get_preprocessor_token (NULL, first_token);
40839
40840 /* Now actually load the PCH file. */
40841 if (name)
40842 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40843
40844 /* Read one more token to return to our caller. We have to do this
40845 after reading the PCH file in, since its pointers have to be
40846 live. */
40847 cp_lexer_get_preprocessor_token (NULL, first_token);
40848 }
40849
40850 /* Parse a pragma GCC ivdep. */
40851
40852 static bool
40853 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40854 {
40855 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40856 return true;
40857 }
40858
40859 /* Parse a pragma GCC unroll. */
40860
40861 static unsigned short
40862 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40863 {
40864 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40865 tree expr = cp_parser_constant_expression (parser);
40866 unsigned short unroll;
40867 expr = maybe_constant_value (expr);
40868 HOST_WIDE_INT lunroll = 0;
40869 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40870 || TREE_CODE (expr) != INTEGER_CST
40871 || (lunroll = tree_to_shwi (expr)) < 0
40872 || lunroll >= USHRT_MAX)
40873 {
40874 error_at (location, "%<#pragma GCC unroll%> requires an"
40875 " assignment-expression that evaluates to a non-negative"
40876 " integral constant less than %u", USHRT_MAX);
40877 unroll = 0;
40878 }
40879 else
40880 {
40881 unroll = (unsigned short)lunroll;
40882 if (unroll == 0)
40883 unroll = 1;
40884 }
40885 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40886 return unroll;
40887 }
40888
40889 /* Normal parsing of a pragma token. Here we can (and must) use the
40890 regular lexer. */
40891
40892 static bool
40893 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40894 {
40895 cp_token *pragma_tok;
40896 unsigned int id;
40897 tree stmt;
40898 bool ret;
40899
40900 pragma_tok = cp_lexer_consume_token (parser->lexer);
40901 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40902 parser->lexer->in_pragma = true;
40903
40904 id = cp_parser_pragma_kind (pragma_tok);
40905 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40906 cp_ensure_no_omp_declare_simd (parser);
40907 switch (id)
40908 {
40909 case PRAGMA_GCC_PCH_PREPROCESS:
40910 error_at (pragma_tok->location,
40911 "%<#pragma GCC pch_preprocess%> must be first");
40912 break;
40913
40914 case PRAGMA_OMP_BARRIER:
40915 switch (context)
40916 {
40917 case pragma_compound:
40918 cp_parser_omp_barrier (parser, pragma_tok);
40919 return false;
40920 case pragma_stmt:
40921 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40922 "used in compound statements", "omp barrier");
40923 break;
40924 default:
40925 goto bad_stmt;
40926 }
40927 break;
40928
40929 case PRAGMA_OMP_DEPOBJ:
40930 switch (context)
40931 {
40932 case pragma_compound:
40933 cp_parser_omp_depobj (parser, pragma_tok);
40934 return false;
40935 case pragma_stmt:
40936 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40937 "used in compound statements", "omp depobj");
40938 break;
40939 default:
40940 goto bad_stmt;
40941 }
40942 break;
40943
40944 case PRAGMA_OMP_FLUSH:
40945 switch (context)
40946 {
40947 case pragma_compound:
40948 cp_parser_omp_flush (parser, pragma_tok);
40949 return false;
40950 case pragma_stmt:
40951 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40952 "used in compound statements", "omp flush");
40953 break;
40954 default:
40955 goto bad_stmt;
40956 }
40957 break;
40958
40959 case PRAGMA_OMP_TASKWAIT:
40960 switch (context)
40961 {
40962 case pragma_compound:
40963 cp_parser_omp_taskwait (parser, pragma_tok);
40964 return false;
40965 case pragma_stmt:
40966 error_at (pragma_tok->location,
40967 "%<#pragma %s%> may only be used in compound statements",
40968 "omp taskwait");
40969 break;
40970 default:
40971 goto bad_stmt;
40972 }
40973 break;
40974
40975 case PRAGMA_OMP_TASKYIELD:
40976 switch (context)
40977 {
40978 case pragma_compound:
40979 cp_parser_omp_taskyield (parser, pragma_tok);
40980 return false;
40981 case pragma_stmt:
40982 error_at (pragma_tok->location,
40983 "%<#pragma %s%> may only be used in compound statements",
40984 "omp taskyield");
40985 break;
40986 default:
40987 goto bad_stmt;
40988 }
40989 break;
40990
40991 case PRAGMA_OMP_CANCEL:
40992 switch (context)
40993 {
40994 case pragma_compound:
40995 cp_parser_omp_cancel (parser, pragma_tok);
40996 return false;
40997 case pragma_stmt:
40998 error_at (pragma_tok->location,
40999 "%<#pragma %s%> may only be used in compound statements",
41000 "omp cancel");
41001 break;
41002 default:
41003 goto bad_stmt;
41004 }
41005 break;
41006
41007 case PRAGMA_OMP_CANCELLATION_POINT:
41008 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
41009 return false;
41010
41011 case PRAGMA_OMP_THREADPRIVATE:
41012 cp_parser_omp_threadprivate (parser, pragma_tok);
41013 return false;
41014
41015 case PRAGMA_OMP_DECLARE:
41016 return cp_parser_omp_declare (parser, pragma_tok, context);
41017
41018 case PRAGMA_OACC_DECLARE:
41019 cp_parser_oacc_declare (parser, pragma_tok);
41020 return false;
41021
41022 case PRAGMA_OACC_ENTER_DATA:
41023 if (context == pragma_stmt)
41024 {
41025 error_at (pragma_tok->location,
41026 "%<#pragma %s%> may only be used in compound statements",
41027 "acc enter data");
41028 break;
41029 }
41030 else if (context != pragma_compound)
41031 goto bad_stmt;
41032 cp_parser_omp_construct (parser, pragma_tok, if_p);
41033 return true;
41034
41035 case PRAGMA_OACC_EXIT_DATA:
41036 if (context == pragma_stmt)
41037 {
41038 error_at (pragma_tok->location,
41039 "%<#pragma %s%> may only be used in compound statements",
41040 "acc exit data");
41041 break;
41042 }
41043 else if (context != pragma_compound)
41044 goto bad_stmt;
41045 cp_parser_omp_construct (parser, pragma_tok, if_p);
41046 return true;
41047
41048 case PRAGMA_OACC_ROUTINE:
41049 if (context != pragma_external)
41050 {
41051 error_at (pragma_tok->location,
41052 "%<#pragma acc routine%> must be at file scope");
41053 break;
41054 }
41055 cp_parser_oacc_routine (parser, pragma_tok, context);
41056 return false;
41057
41058 case PRAGMA_OACC_UPDATE:
41059 if (context == pragma_stmt)
41060 {
41061 error_at (pragma_tok->location,
41062 "%<#pragma %s%> may only be used in compound statements",
41063 "acc update");
41064 break;
41065 }
41066 else if (context != pragma_compound)
41067 goto bad_stmt;
41068 cp_parser_omp_construct (parser, pragma_tok, if_p);
41069 return true;
41070
41071 case PRAGMA_OACC_WAIT:
41072 if (context == pragma_stmt)
41073 {
41074 error_at (pragma_tok->location,
41075 "%<#pragma %s%> may only be used in compound statements",
41076 "acc wait");
41077 break;
41078 }
41079 else if (context != pragma_compound)
41080 goto bad_stmt;
41081 cp_parser_omp_construct (parser, pragma_tok, if_p);
41082 return true;
41083
41084 case PRAGMA_OACC_ATOMIC:
41085 case PRAGMA_OACC_CACHE:
41086 case PRAGMA_OACC_DATA:
41087 case PRAGMA_OACC_HOST_DATA:
41088 case PRAGMA_OACC_KERNELS:
41089 case PRAGMA_OACC_PARALLEL:
41090 case PRAGMA_OACC_LOOP:
41091 case PRAGMA_OMP_ATOMIC:
41092 case PRAGMA_OMP_CRITICAL:
41093 case PRAGMA_OMP_DISTRIBUTE:
41094 case PRAGMA_OMP_FOR:
41095 case PRAGMA_OMP_MASTER:
41096 case PRAGMA_OMP_PARALLEL:
41097 case PRAGMA_OMP_SECTIONS:
41098 case PRAGMA_OMP_SIMD:
41099 case PRAGMA_OMP_SINGLE:
41100 case PRAGMA_OMP_TASK:
41101 case PRAGMA_OMP_TASKGROUP:
41102 case PRAGMA_OMP_TASKLOOP:
41103 case PRAGMA_OMP_TEAMS:
41104 if (context != pragma_stmt && context != pragma_compound)
41105 goto bad_stmt;
41106 stmt = push_omp_privatization_clauses (false);
41107 cp_parser_omp_construct (parser, pragma_tok, if_p);
41108 pop_omp_privatization_clauses (stmt);
41109 return true;
41110
41111 case PRAGMA_OMP_REQUIRES:
41112 return cp_parser_omp_requires (parser, pragma_tok);
41113
41114 case PRAGMA_OMP_ORDERED:
41115 if (context != pragma_stmt && context != pragma_compound)
41116 goto bad_stmt;
41117 stmt = push_omp_privatization_clauses (false);
41118 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
41119 pop_omp_privatization_clauses (stmt);
41120 return ret;
41121
41122 case PRAGMA_OMP_TARGET:
41123 if (context != pragma_stmt && context != pragma_compound)
41124 goto bad_stmt;
41125 stmt = push_omp_privatization_clauses (false);
41126 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
41127 pop_omp_privatization_clauses (stmt);
41128 return ret;
41129
41130 case PRAGMA_OMP_END_DECLARE_TARGET:
41131 cp_parser_omp_end_declare_target (parser, pragma_tok);
41132 return false;
41133
41134 case PRAGMA_OMP_SCAN:
41135 error_at (pragma_tok->location,
41136 "%<#pragma omp scan%> may only be used in "
41137 "a loop construct with %<inscan%> %<reduction%> clause");
41138 break;
41139
41140 case PRAGMA_OMP_SECTION:
41141 error_at (pragma_tok->location,
41142 "%<#pragma omp section%> may only be used in "
41143 "%<#pragma omp sections%> construct");
41144 break;
41145
41146 case PRAGMA_IVDEP:
41147 {
41148 if (context == pragma_external)
41149 {
41150 error_at (pragma_tok->location,
41151 "%<#pragma GCC ivdep%> must be inside a function");
41152 break;
41153 }
41154 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
41155 unsigned short unroll;
41156 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41157 if (tok->type == CPP_PRAGMA
41158 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
41159 {
41160 tok = cp_lexer_consume_token (parser->lexer);
41161 unroll = cp_parser_pragma_unroll (parser, tok);
41162 tok = cp_lexer_peek_token (the_parser->lexer);
41163 }
41164 else
41165 unroll = 0;
41166 if (tok->type != CPP_KEYWORD
41167 || (tok->keyword != RID_FOR
41168 && tok->keyword != RID_WHILE
41169 && tok->keyword != RID_DO))
41170 {
41171 cp_parser_error (parser, "for, while or do statement expected");
41172 return false;
41173 }
41174 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
41175 return true;
41176 }
41177
41178 case PRAGMA_UNROLL:
41179 {
41180 if (context == pragma_external)
41181 {
41182 error_at (pragma_tok->location,
41183 "%<#pragma GCC unroll%> must be inside a function");
41184 break;
41185 }
41186 const unsigned short unroll
41187 = cp_parser_pragma_unroll (parser, pragma_tok);
41188 bool ivdep;
41189 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41190 if (tok->type == CPP_PRAGMA
41191 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
41192 {
41193 tok = cp_lexer_consume_token (parser->lexer);
41194 ivdep = cp_parser_pragma_ivdep (parser, tok);
41195 tok = cp_lexer_peek_token (the_parser->lexer);
41196 }
41197 else
41198 ivdep = false;
41199 if (tok->type != CPP_KEYWORD
41200 || (tok->keyword != RID_FOR
41201 && tok->keyword != RID_WHILE
41202 && tok->keyword != RID_DO))
41203 {
41204 cp_parser_error (parser, "for, while or do statement expected");
41205 return false;
41206 }
41207 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
41208 return true;
41209 }
41210
41211 default:
41212 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
41213 c_invoke_pragma_handler (id);
41214 break;
41215
41216 bad_stmt:
41217 cp_parser_error (parser, "expected declaration specifiers");
41218 break;
41219 }
41220
41221 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41222 return false;
41223 }
41224
41225 /* The interface the pragma parsers have to the lexer. */
41226
41227 enum cpp_ttype
41228 pragma_lex (tree *value, location_t *loc)
41229 {
41230 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41231 enum cpp_ttype ret = tok->type;
41232
41233 *value = tok->u.value;
41234 if (loc)
41235 *loc = tok->location;
41236
41237 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
41238 ret = CPP_EOF;
41239 else if (ret == CPP_STRING)
41240 *value = cp_parser_string_literal (the_parser, false, false);
41241 else
41242 {
41243 if (ret == CPP_KEYWORD)
41244 ret = CPP_NAME;
41245 cp_lexer_consume_token (the_parser->lexer);
41246 }
41247
41248 return ret;
41249 }
41250
41251 \f
41252 /* External interface. */
41253
41254 /* Parse one entire translation unit. */
41255
41256 void
41257 c_parse_file (void)
41258 {
41259 static bool already_called = false;
41260
41261 if (already_called)
41262 fatal_error (input_location,
41263 "inter-module optimizations not implemented for C++");
41264 already_called = true;
41265
41266 the_parser = cp_parser_new ();
41267 push_deferring_access_checks (flag_access_control
41268 ? dk_no_deferred : dk_no_check);
41269 cp_parser_translation_unit (the_parser);
41270 the_parser = NULL;
41271
41272 finish_translation_unit ();
41273 }
41274
41275 /* Create an identifier for a generic parameter type (a synthesized
41276 template parameter implied by `auto' or a concept identifier). */
41277
41278 static GTY(()) int generic_parm_count;
41279 static tree
41280 make_generic_type_name ()
41281 {
41282 char buf[32];
41283 sprintf (buf, "auto:%d", ++generic_parm_count);
41284 return get_identifier (buf);
41285 }
41286
41287 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41288 (creating a new template parameter list if necessary). Returns the newly
41289 created template type parm. */
41290
41291 static tree
41292 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
41293 {
41294 gcc_assert (current_binding_level->kind == sk_function_parms);
41295
41296 /* Before committing to modifying any scope, if we're in an
41297 implicit template scope, and we're trying to synthesize a
41298 constrained parameter, try to find a previous parameter with
41299 the same name. This is the same-type rule for abbreviated
41300 function templates.
41301
41302 NOTE: We can generate implicit parameters when tentatively
41303 parsing a nested name specifier, only to reject that parse
41304 later. However, matching the same template-id as part of a
41305 direct-declarator should generate an identical template
41306 parameter, so this rule will merge them. */
41307 if (parser->implicit_template_scope && constr)
41308 {
41309 tree t = parser->implicit_template_parms;
41310 while (t)
41311 {
41312 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
41313 {
41314 tree d = TREE_VALUE (t);
41315 if (TREE_CODE (d) == PARM_DECL)
41316 /* Return the TEMPLATE_PARM_INDEX. */
41317 d = DECL_INITIAL (d);
41318 return d;
41319 }
41320 t = TREE_CHAIN (t);
41321 }
41322 }
41323
41324 /* We are either continuing a function template that already contains implicit
41325 template parameters, creating a new fully-implicit function template, or
41326 extending an existing explicit function template with implicit template
41327 parameters. */
41328
41329 cp_binding_level *const entry_scope = current_binding_level;
41330
41331 bool become_template = false;
41332 cp_binding_level *parent_scope = 0;
41333
41334 if (parser->implicit_template_scope)
41335 {
41336 gcc_assert (parser->implicit_template_parms);
41337
41338 current_binding_level = parser->implicit_template_scope;
41339 }
41340 else
41341 {
41342 /* Roll back to the existing template parameter scope (in the case of
41343 extending an explicit function template) or introduce a new template
41344 parameter scope ahead of the function parameter scope (or class scope
41345 in the case of out-of-line member definitions). The function scope is
41346 added back after template parameter synthesis below. */
41347
41348 cp_binding_level *scope = entry_scope;
41349
41350 while (scope->kind == sk_function_parms)
41351 {
41352 parent_scope = scope;
41353 scope = scope->level_chain;
41354 }
41355 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
41356 {
41357 /* If not defining a class, then any class scope is a scope level in
41358 an out-of-line member definition. In this case simply wind back
41359 beyond the first such scope to inject the template parameter list.
41360 Otherwise wind back to the class being defined. The latter can
41361 occur in class member friend declarations such as:
41362
41363 class A {
41364 void foo (auto);
41365 };
41366 class B {
41367 friend void A::foo (auto);
41368 };
41369
41370 The template parameter list synthesized for the friend declaration
41371 must be injected in the scope of 'B'. This can also occur in
41372 erroneous cases such as:
41373
41374 struct A {
41375 struct B {
41376 void foo (auto);
41377 };
41378 void B::foo (auto) {}
41379 };
41380
41381 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41382 but, nevertheless, the template parameter list synthesized for the
41383 declarator should be injected into the scope of 'A' as if the
41384 ill-formed template was specified explicitly. */
41385
41386 while (scope->kind == sk_class && !scope->defining_class_p)
41387 {
41388 parent_scope = scope;
41389 scope = scope->level_chain;
41390 }
41391 }
41392
41393 current_binding_level = scope;
41394
41395 if (scope->kind != sk_template_parms
41396 || !function_being_declared_is_template_p (parser))
41397 {
41398 /* Introduce a new template parameter list for implicit template
41399 parameters. */
41400
41401 become_template = true;
41402
41403 parser->implicit_template_scope
41404 = begin_scope (sk_template_parms, NULL);
41405
41406 ++processing_template_decl;
41407
41408 parser->fully_implicit_function_template_p = true;
41409 ++parser->num_template_parameter_lists;
41410 }
41411 else
41412 {
41413 /* Synthesize implicit template parameters at the end of the explicit
41414 template parameter list. */
41415
41416 gcc_assert (current_template_parms);
41417
41418 parser->implicit_template_scope = scope;
41419
41420 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41421 parser->implicit_template_parms
41422 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
41423 }
41424 }
41425
41426 /* Synthesize a new template parameter and track the current template
41427 parameter chain with implicit_template_parms. */
41428
41429 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
41430 tree synth_id = make_generic_type_name ();
41431 tree synth_tmpl_parm;
41432 bool non_type = false;
41433
41434 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
41435 synth_tmpl_parm
41436 = finish_template_type_parm (class_type_node, synth_id);
41437 else if (TREE_CODE (proto) == TEMPLATE_DECL)
41438 synth_tmpl_parm
41439 = finish_constrained_template_template_parm (proto, synth_id);
41440 else
41441 {
41442 synth_tmpl_parm = copy_decl (proto);
41443 DECL_NAME (synth_tmpl_parm) = synth_id;
41444 non_type = true;
41445 }
41446
41447 // Attach the constraint to the parm before processing.
41448 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
41449 TREE_TYPE (node) = constr;
41450 tree new_parm
41451 = process_template_parm (parser->implicit_template_parms,
41452 input_location,
41453 node,
41454 /*non_type=*/non_type,
41455 /*param_pack=*/false);
41456
41457 // Chain the new parameter to the list of implicit parameters.
41458 if (parser->implicit_template_parms)
41459 parser->implicit_template_parms
41460 = TREE_CHAIN (parser->implicit_template_parms);
41461 else
41462 parser->implicit_template_parms = new_parm;
41463
41464 tree new_decl = get_local_decls ();
41465 if (non_type)
41466 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41467 new_decl = DECL_INITIAL (new_decl);
41468
41469 /* If creating a fully implicit function template, start the new implicit
41470 template parameter list with this synthesized type, otherwise grow the
41471 current template parameter list. */
41472
41473 if (become_template)
41474 {
41475 parent_scope->level_chain = current_binding_level;
41476
41477 tree new_parms = make_tree_vec (1);
41478 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
41479 current_template_parms = tree_cons (size_int (processing_template_decl),
41480 new_parms, current_template_parms);
41481 }
41482 else
41483 {
41484 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41485 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
41486 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
41487 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
41488 }
41489
41490 // If the new parameter was constrained, we need to add that to the
41491 // constraints in the template parameter list.
41492 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
41493 {
41494 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
41495 reqs = conjoin_constraints (reqs, req);
41496 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
41497 }
41498
41499 current_binding_level = entry_scope;
41500
41501 return new_decl;
41502 }
41503
41504 /* Finish the declaration of a fully implicit function template. Such a
41505 template has no explicit template parameter list so has not been through the
41506 normal template head and tail processing. synthesize_implicit_template_parm
41507 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41508 provided if the declaration is a class member such that its template
41509 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41510 form is returned. Otherwise NULL_TREE is returned. */
41511
41512 static tree
41513 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
41514 {
41515 gcc_assert (parser->fully_implicit_function_template_p);
41516
41517 if (member_decl_opt && member_decl_opt != error_mark_node
41518 && DECL_VIRTUAL_P (member_decl_opt))
41519 {
41520 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
41521 "implicit templates may not be %<virtual%>");
41522 DECL_VIRTUAL_P (member_decl_opt) = false;
41523 }
41524
41525 if (member_decl_opt)
41526 member_decl_opt = finish_member_template_decl (member_decl_opt);
41527 end_template_decl ();
41528
41529 parser->fully_implicit_function_template_p = false;
41530 parser->implicit_template_parms = 0;
41531 parser->implicit_template_scope = 0;
41532 --parser->num_template_parameter_lists;
41533
41534 return member_decl_opt;
41535 }
41536
41537 /* Like finish_fully_implicit_template, but to be used in error
41538 recovery, rearranging scopes so that we restore the state we had
41539 before synthesize_implicit_template_parm inserted the implement
41540 template parms scope. */
41541
41542 static void
41543 abort_fully_implicit_template (cp_parser *parser)
41544 {
41545 cp_binding_level *return_to_scope = current_binding_level;
41546
41547 if (parser->implicit_template_scope
41548 && return_to_scope != parser->implicit_template_scope)
41549 {
41550 cp_binding_level *child = return_to_scope;
41551 for (cp_binding_level *scope = child->level_chain;
41552 scope != parser->implicit_template_scope;
41553 scope = child->level_chain)
41554 child = scope;
41555 child->level_chain = parser->implicit_template_scope->level_chain;
41556 parser->implicit_template_scope->level_chain = return_to_scope;
41557 current_binding_level = parser->implicit_template_scope;
41558 }
41559 else
41560 return_to_scope = return_to_scope->level_chain;
41561
41562 finish_fully_implicit_template (parser, NULL);
41563
41564 gcc_assert (current_binding_level == return_to_scope);
41565 }
41566
41567 /* Helper function for diagnostics that have complained about things
41568 being used with 'extern "C"' linkage.
41569
41570 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41571
41572 void
41573 maybe_show_extern_c_location (void)
41574 {
41575 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
41576 inform (the_parser->innermost_linkage_specification_location,
41577 "%<extern \"C\"%> linkage started here");
41578 }
41579
41580 #include "gt-cp-parser.h"