parser.c (cp_parser_template_declaration_after_parameters): Use DECL_SOURCE_LOCATION...
[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 cp_expr (build_nt (BIT_NOT_EXPR, scope), loc);
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 cp_expr (build_nt (BIT_NOT_EXPR, make_auto (), loc));
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_nt (BIT_NOT_EXPR, type_decl);
6157 return cp_expr (type_decl, loc);
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_nt (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 cp_expr (build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl), loc));
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_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10530 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10531 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10532 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10533
10534 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10535 {
10536 cp_lexer_consume_token (parser->lexer);
10537 first = false;
10538
10539 if (!(at_function_scope_p () || parsing_nsdmi ()))
10540 error ("non-local lambda expression cannot have a capture-default");
10541 }
10542
10543 hash_set<tree, true> ids;
10544 tree first_capture_id = NULL_TREE;
10545 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10546 {
10547 cp_token* capture_token;
10548 tree capture_id;
10549 tree capture_init_expr;
10550 cp_id_kind idk = CP_ID_KIND_NONE;
10551 bool explicit_init_p = false;
10552
10553 enum capture_kind_type
10554 {
10555 BY_COPY,
10556 BY_REFERENCE
10557 };
10558 enum capture_kind_type capture_kind = BY_COPY;
10559
10560 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10561 {
10562 error ("expected end of capture-list");
10563 return;
10564 }
10565
10566 if (first)
10567 first = false;
10568 else
10569 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10570
10571 /* Possibly capture `this'. */
10572 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10573 {
10574 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10575 if (cxx_dialect < cxx2a
10576 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10577 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10578 "with by-copy capture default");
10579 cp_lexer_consume_token (parser->lexer);
10580 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10581 pedwarn (input_location, 0,
10582 "already captured %qD in lambda expression",
10583 this_identifier);
10584 else
10585 add_capture (lambda_expr, /*id=*/this_identifier,
10586 /*initializer=*/finish_this_expr (),
10587 /*by_reference_p=*/true, explicit_init_p);
10588 continue;
10589 }
10590
10591 /* Possibly capture `*this'. */
10592 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10593 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10594 {
10595 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10596 if (cxx_dialect < cxx17)
10597 pedwarn (loc, 0, "%<*this%> capture only available with "
10598 "%<-std=c++17%> or %<-std=gnu++17%>");
10599 cp_lexer_consume_token (parser->lexer);
10600 cp_lexer_consume_token (parser->lexer);
10601 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10602 pedwarn (input_location, 0,
10603 "already captured %qD in lambda expression",
10604 this_identifier);
10605 else
10606 add_capture (lambda_expr, /*id=*/this_identifier,
10607 /*initializer=*/finish_this_expr (),
10608 /*by_reference_p=*/false, explicit_init_p);
10609 continue;
10610 }
10611
10612 bool init_pack_expansion = false;
10613 location_t ellipsis_loc = UNKNOWN_LOCATION;
10614 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10615 {
10616 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
10617 if (cxx_dialect < cxx2a)
10618 pedwarn (ellipsis_loc, 0, "pack init-capture only available with "
10619 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10620 cp_lexer_consume_token (parser->lexer);
10621 init_pack_expansion = true;
10622 }
10623
10624 /* Remember whether we want to capture as a reference or not. */
10625 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10626 {
10627 capture_kind = BY_REFERENCE;
10628 cp_lexer_consume_token (parser->lexer);
10629 }
10630
10631 /* Get the identifier. */
10632 capture_token = cp_lexer_peek_token (parser->lexer);
10633 capture_id = cp_parser_identifier (parser);
10634
10635 if (capture_id == error_mark_node)
10636 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10637 delimiters, but I modified this to stop on unnested ']' as well. It
10638 was already changed to stop on unnested '}', so the
10639 "closing_parenthesis" name is no more misleading with my change. */
10640 {
10641 cp_parser_skip_to_closing_parenthesis (parser,
10642 /*recovering=*/true,
10643 /*or_comma=*/true,
10644 /*consume_paren=*/true);
10645 break;
10646 }
10647
10648 /* Find the initializer for this capture. */
10649 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10650 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10651 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10652 {
10653 bool direct, non_constant;
10654 /* An explicit initializer exists. */
10655 if (cxx_dialect < cxx14)
10656 pedwarn (input_location, 0,
10657 "lambda capture initializers "
10658 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10659 capture_init_expr = cp_parser_initializer (parser, &direct,
10660 &non_constant, true);
10661 explicit_init_p = true;
10662 if (capture_init_expr == NULL_TREE)
10663 {
10664 error ("empty initializer for lambda init-capture");
10665 capture_init_expr = error_mark_node;
10666 }
10667 if (init_pack_expansion)
10668 capture_init_expr = make_pack_expansion (capture_init_expr);
10669 }
10670 else
10671 {
10672 const char* error_msg;
10673
10674 /* Turn the identifier into an id-expression. */
10675 capture_init_expr
10676 = cp_parser_lookup_name_simple (parser, capture_id,
10677 capture_token->location);
10678
10679 if (capture_init_expr == error_mark_node)
10680 {
10681 unqualified_name_lookup_error (capture_id);
10682 continue;
10683 }
10684 else if (!VAR_P (capture_init_expr)
10685 && TREE_CODE (capture_init_expr) != PARM_DECL)
10686 {
10687 error_at (capture_token->location,
10688 "capture of non-variable %qE",
10689 capture_init_expr);
10690 if (DECL_P (capture_init_expr))
10691 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10692 "%q#D declared here", capture_init_expr);
10693 continue;
10694 }
10695 if (VAR_P (capture_init_expr)
10696 && decl_storage_duration (capture_init_expr) != dk_auto)
10697 {
10698 if (pedwarn (capture_token->location, 0, "capture of variable "
10699 "%qD with non-automatic storage duration",
10700 capture_init_expr))
10701 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10702 "%q#D declared here", capture_init_expr);
10703 continue;
10704 }
10705
10706 capture_init_expr
10707 = finish_id_expression
10708 (capture_id,
10709 capture_init_expr,
10710 parser->scope,
10711 &idk,
10712 /*integral_constant_expression_p=*/false,
10713 /*allow_non_integral_constant_expression_p=*/false,
10714 /*non_integral_constant_expression_p=*/NULL,
10715 /*template_p=*/false,
10716 /*done=*/true,
10717 /*address_p=*/false,
10718 /*template_arg_p=*/false,
10719 &error_msg,
10720 capture_token->location);
10721
10722 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10723 {
10724 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10725 cp_lexer_consume_token (parser->lexer);
10726 capture_init_expr = make_pack_expansion (capture_init_expr);
10727 if (init_pack_expansion)
10728 {
10729 /* If what follows is an initializer, the second '...' is
10730 invalid. But for cases like [...xs...], the first one
10731 is invalid. */
10732 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10733 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10734 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10735 ellipsis_loc = loc;
10736 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
10737 continue;
10738 }
10739 }
10740 }
10741
10742 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10743 && !explicit_init_p)
10744 {
10745 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10746 && capture_kind == BY_COPY)
10747 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10748 "of %qD redundant with by-copy capture default",
10749 capture_id);
10750 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10751 && capture_kind == BY_REFERENCE)
10752 pedwarn (capture_token->location, 0, "explicit by-reference "
10753 "capture of %qD redundant with by-reference capture "
10754 "default", capture_id);
10755 }
10756
10757 /* Check for duplicates.
10758 Optimize for the zero or one explicit captures cases and only create
10759 the hash_set after adding second capture. */
10760 bool found = false;
10761 if (!ids.is_empty ())
10762 found = ids.add (capture_id);
10763 else if (first_capture_id == NULL_TREE)
10764 first_capture_id = capture_id;
10765 else if (capture_id == first_capture_id)
10766 found = true;
10767 else
10768 {
10769 ids.add (first_capture_id);
10770 ids.add (capture_id);
10771 }
10772 if (found)
10773 pedwarn (input_location, 0,
10774 "already captured %qD in lambda expression", capture_id);
10775 else
10776 add_capture (lambda_expr, capture_id, capture_init_expr,
10777 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10778 explicit_init_p);
10779
10780 /* If there is any qualification still in effect, clear it
10781 now; we will be starting fresh with the next capture. */
10782 parser->scope = NULL_TREE;
10783 parser->qualifying_scope = NULL_TREE;
10784 parser->object_scope = NULL_TREE;
10785 }
10786
10787 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10788 }
10789
10790 /* Parse the (optional) middle of a lambda expression.
10791
10792 lambda-declarator:
10793 < template-parameter-list [opt] >
10794 ( parameter-declaration-clause [opt] )
10795 attribute-specifier [opt]
10796 decl-specifier-seq [opt]
10797 exception-specification [opt]
10798 lambda-return-type-clause [opt]
10799
10800 LAMBDA_EXPR is the current representation of the lambda expression. */
10801
10802 static bool
10803 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10804 {
10805 /* 5.1.1.4 of the standard says:
10806 If a lambda-expression does not include a lambda-declarator, it is as if
10807 the lambda-declarator were ().
10808 This means an empty parameter list, no attributes, and no exception
10809 specification. */
10810 tree param_list = void_list_node;
10811 tree std_attrs = NULL_TREE;
10812 tree gnu_attrs = NULL_TREE;
10813 tree exception_spec = NULL_TREE;
10814 tree template_param_list = NULL_TREE;
10815 tree tx_qual = NULL_TREE;
10816 tree return_type = NULL_TREE;
10817 cp_decl_specifier_seq lambda_specs;
10818 clear_decl_specs (&lambda_specs);
10819
10820 /* The template-parameter-list is optional, but must begin with
10821 an opening angle if present. */
10822 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10823 {
10824 if (cxx_dialect < cxx14)
10825 pedwarn (parser->lexer->next_token->location, 0,
10826 "lambda templates are only available with "
10827 "%<-std=c++14%> or %<-std=gnu++14%>");
10828 else if (cxx_dialect < cxx2a)
10829 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10830 "lambda templates are only available with "
10831 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10832
10833 cp_lexer_consume_token (parser->lexer);
10834
10835 template_param_list = cp_parser_template_parameter_list (parser);
10836
10837 cp_parser_skip_to_end_of_template_parameter_list (parser);
10838
10839 /* We just processed one more parameter list. */
10840 ++parser->num_template_parameter_lists;
10841 }
10842
10843 /* The parameter-declaration-clause is optional (unless
10844 template-parameter-list was given), but must begin with an
10845 opening parenthesis if present. */
10846 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10847 {
10848 matching_parens parens;
10849 parens.consume_open (parser);
10850
10851 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10852
10853 /* Parse parameters. */
10854 param_list
10855 = cp_parser_parameter_declaration_clause
10856 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
10857
10858 /* Default arguments shall not be specified in the
10859 parameter-declaration-clause of a lambda-declarator. */
10860 if (cxx_dialect < cxx14)
10861 for (tree t = param_list; t; t = TREE_CHAIN (t))
10862 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10863 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10864 "default argument specified for lambda parameter");
10865
10866 parens.require_close (parser);
10867
10868 /* In the decl-specifier-seq of the lambda-declarator, each
10869 decl-specifier shall either be mutable or constexpr. */
10870 int declares_class_or_enum;
10871 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
10872 && !cp_next_tokens_can_be_gnu_attribute_p (parser))
10873 cp_parser_decl_specifier_seq (parser,
10874 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10875 &lambda_specs, &declares_class_or_enum);
10876 if (lambda_specs.storage_class == sc_mutable)
10877 {
10878 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10879 if (lambda_specs.conflicting_specifiers_p)
10880 error_at (lambda_specs.locations[ds_storage_class],
10881 "duplicate %<mutable%>");
10882 }
10883
10884 tx_qual = cp_parser_tx_qualifier_opt (parser);
10885
10886 /* Parse optional exception specification. */
10887 exception_spec = cp_parser_exception_specification_opt (parser);
10888
10889 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10890
10891 /* Parse optional trailing return type. */
10892 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10893 {
10894 cp_lexer_consume_token (parser->lexer);
10895 return_type = cp_parser_trailing_type_id (parser);
10896 }
10897
10898 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
10899 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
10900
10901 /* The function parameters must be in scope all the way until after the
10902 trailing-return-type in case of decltype. */
10903 pop_bindings_and_leave_scope ();
10904 }
10905 else if (template_param_list != NULL_TREE) // generate diagnostic
10906 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10907
10908 /* Create the function call operator.
10909
10910 Messing with declarators like this is no uglier than building up the
10911 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10912 other code. */
10913 {
10914 cp_decl_specifier_seq return_type_specs;
10915 cp_declarator* declarator;
10916 tree fco;
10917 int quals;
10918 void *p;
10919
10920 clear_decl_specs (&return_type_specs);
10921 return_type_specs.type = make_auto ();
10922
10923 if (lambda_specs.locations[ds_constexpr])
10924 {
10925 if (cxx_dialect >= cxx17)
10926 return_type_specs.locations[ds_constexpr]
10927 = lambda_specs.locations[ds_constexpr];
10928 else
10929 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10930 "lambda only available with %<-std=c++17%> or "
10931 "%<-std=gnu++17%>");
10932 }
10933
10934 p = obstack_alloc (&declarator_obstack, 0);
10935
10936 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
10937 LAMBDA_EXPR_LOCATION (lambda_expr));
10938
10939 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10940 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10941 declarator = make_call_declarator (declarator, param_list, quals,
10942 VIRT_SPEC_UNSPECIFIED,
10943 REF_QUAL_NONE,
10944 tx_qual,
10945 exception_spec,
10946 return_type,
10947 /*requires_clause*/NULL_TREE);
10948 declarator->std_attributes = std_attrs;
10949
10950 fco = grokmethod (&return_type_specs,
10951 declarator,
10952 gnu_attrs);
10953 if (fco != error_mark_node)
10954 {
10955 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10956 DECL_ARTIFICIAL (fco) = 1;
10957 /* Give the object parameter a different name. */
10958 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10959 DECL_LAMBDA_FUNCTION (fco) = 1;
10960 }
10961 if (template_param_list)
10962 {
10963 fco = finish_member_template_decl (fco);
10964 finish_template_decl (template_param_list);
10965 --parser->num_template_parameter_lists;
10966 }
10967 else if (parser->fully_implicit_function_template_p)
10968 fco = finish_fully_implicit_template (parser, fco);
10969
10970 finish_member_declaration (fco);
10971
10972 obstack_free (&declarator_obstack, p);
10973
10974 return (fco != error_mark_node);
10975 }
10976 }
10977
10978 /* Parse the body of a lambda expression, which is simply
10979
10980 compound-statement
10981
10982 but which requires special handling.
10983 LAMBDA_EXPR is the current representation of the lambda expression. */
10984
10985 static void
10986 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10987 {
10988 bool nested = (current_function_decl != NULL_TREE);
10989 unsigned char local_variables_forbidden_p
10990 = parser->local_variables_forbidden_p;
10991 bool in_function_body = parser->in_function_body;
10992
10993 /* The body of a lambda-expression is not a subexpression of the enclosing
10994 expression. */
10995 cp_evaluated ev;
10996
10997 if (nested)
10998 push_function_context ();
10999 else
11000 /* Still increment function_depth so that we don't GC in the
11001 middle of an expression. */
11002 ++function_depth;
11003
11004 vec<tree> omp_privatization_save;
11005 save_omp_privatization_clauses (omp_privatization_save);
11006 /* Clear this in case we're in the middle of a default argument. */
11007 parser->local_variables_forbidden_p = 0;
11008 parser->in_function_body = true;
11009
11010 {
11011 local_specialization_stack s (lss_copy);
11012 tree fco = lambda_function (lambda_expr);
11013 tree body = start_lambda_function (fco, lambda_expr);
11014 matching_braces braces;
11015
11016 if (braces.require_open (parser))
11017 {
11018 tree compound_stmt = begin_compound_stmt (0);
11019
11020 /* Originally C++11 required us to peek for 'return expr'; and
11021 process it specially here to deduce the return type. N3638
11022 removed the need for that. */
11023
11024 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11025 cp_parser_label_declaration (parser);
11026 cp_parser_statement_seq_opt (parser, NULL_TREE);
11027 braces.require_close (parser);
11028
11029 finish_compound_stmt (compound_stmt);
11030 }
11031
11032 finish_lambda_function (body);
11033 }
11034
11035 restore_omp_privatization_clauses (omp_privatization_save);
11036 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11037 parser->in_function_body = in_function_body;
11038 if (nested)
11039 pop_function_context();
11040 else
11041 --function_depth;
11042 }
11043
11044 /* Statements [gram.stmt.stmt] */
11045
11046 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11047
11048 static void
11049 add_debug_begin_stmt (location_t loc)
11050 {
11051 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11052 return;
11053 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11054 /* A concept is never expanded normally. */
11055 return;
11056
11057 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11058 SET_EXPR_LOCATION (stmt, loc);
11059 add_stmt (stmt);
11060 }
11061
11062 /* Parse a statement.
11063
11064 statement:
11065 labeled-statement
11066 expression-statement
11067 compound-statement
11068 selection-statement
11069 iteration-statement
11070 jump-statement
11071 declaration-statement
11072 try-block
11073
11074 C++11:
11075
11076 statement:
11077 labeled-statement
11078 attribute-specifier-seq (opt) expression-statement
11079 attribute-specifier-seq (opt) compound-statement
11080 attribute-specifier-seq (opt) selection-statement
11081 attribute-specifier-seq (opt) iteration-statement
11082 attribute-specifier-seq (opt) jump-statement
11083 declaration-statement
11084 attribute-specifier-seq (opt) try-block
11085
11086 init-statement:
11087 expression-statement
11088 simple-declaration
11089
11090 TM Extension:
11091
11092 statement:
11093 atomic-statement
11094
11095 IN_COMPOUND is true when the statement is nested inside a
11096 cp_parser_compound_statement; this matters for certain pragmas.
11097
11098 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11099 is a (possibly labeled) if statement which is not enclosed in braces
11100 and has an else clause. This is used to implement -Wparentheses.
11101
11102 CHAIN is a vector of if-else-if conditions. */
11103
11104 static void
11105 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11106 bool in_compound, bool *if_p, vec<tree> *chain,
11107 location_t *loc_after_labels)
11108 {
11109 tree statement, std_attrs = NULL_TREE;
11110 cp_token *token;
11111 location_t statement_location, attrs_loc;
11112
11113 restart:
11114 if (if_p != NULL)
11115 *if_p = false;
11116 /* There is no statement yet. */
11117 statement = NULL_TREE;
11118
11119 saved_token_sentinel saved_tokens (parser->lexer);
11120 attrs_loc = cp_lexer_peek_token (parser->lexer)->location;
11121 if (c_dialect_objc ())
11122 /* In obj-c++, seeing '[[' might be the either the beginning of
11123 c++11 attributes, or a nested objc-message-expression. So
11124 let's parse the c++11 attributes tentatively. */
11125 cp_parser_parse_tentatively (parser);
11126 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11127 if (std_attrs)
11128 {
11129 location_t end_loc
11130 = cp_lexer_previous_token (parser->lexer)->location;
11131 attrs_loc = make_location (attrs_loc, attrs_loc, end_loc);
11132 }
11133 if (c_dialect_objc ())
11134 {
11135 if (!cp_parser_parse_definitely (parser))
11136 std_attrs = NULL_TREE;
11137 }
11138
11139 /* Peek at the next token. */
11140 token = cp_lexer_peek_token (parser->lexer);
11141 /* Remember the location of the first token in the statement. */
11142 cp_token *statement_token = token;
11143 statement_location = token->location;
11144 add_debug_begin_stmt (statement_location);
11145 /* If this is a keyword, then that will often determine what kind of
11146 statement we have. */
11147 if (token->type == CPP_KEYWORD)
11148 {
11149 enum rid keyword = token->keyword;
11150
11151 switch (keyword)
11152 {
11153 case RID_CASE:
11154 case RID_DEFAULT:
11155 /* Looks like a labeled-statement with a case label.
11156 Parse the label, and then use tail recursion to parse
11157 the statement. */
11158 cp_parser_label_for_labeled_statement (parser, std_attrs);
11159 in_compound = false;
11160 goto restart;
11161
11162 case RID_IF:
11163 case RID_SWITCH:
11164 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11165 statement = cp_parser_selection_statement (parser, if_p, chain);
11166 break;
11167
11168 case RID_WHILE:
11169 case RID_DO:
11170 case RID_FOR:
11171 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11172 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11173 break;
11174
11175 case RID_BREAK:
11176 case RID_CONTINUE:
11177 case RID_RETURN:
11178 case RID_GOTO:
11179 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11180 statement = cp_parser_jump_statement (parser);
11181 break;
11182
11183 /* Objective-C++ exception-handling constructs. */
11184 case RID_AT_TRY:
11185 case RID_AT_CATCH:
11186 case RID_AT_FINALLY:
11187 case RID_AT_SYNCHRONIZED:
11188 case RID_AT_THROW:
11189 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11190 statement = cp_parser_objc_statement (parser);
11191 break;
11192
11193 case RID_TRY:
11194 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11195 statement = cp_parser_try_block (parser);
11196 break;
11197
11198 case RID_NAMESPACE:
11199 /* This must be a namespace alias definition. */
11200 if (std_attrs != NULL_TREE)
11201 {
11202 /* Attributes should be parsed as part of the the
11203 declaration, so let's un-parse them. */
11204 saved_tokens.rollback();
11205 std_attrs = NULL_TREE;
11206 }
11207 cp_parser_declaration_statement (parser);
11208 return;
11209
11210 case RID_TRANSACTION_ATOMIC:
11211 case RID_TRANSACTION_RELAXED:
11212 case RID_SYNCHRONIZED:
11213 case RID_ATOMIC_NOEXCEPT:
11214 case RID_ATOMIC_CANCEL:
11215 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11216 statement = cp_parser_transaction (parser, token);
11217 break;
11218 case RID_TRANSACTION_CANCEL:
11219 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11220 statement = cp_parser_transaction_cancel (parser);
11221 break;
11222
11223 default:
11224 /* It might be a keyword like `int' that can start a
11225 declaration-statement. */
11226 break;
11227 }
11228 }
11229 else if (token->type == CPP_NAME)
11230 {
11231 /* If the next token is a `:', then we are looking at a
11232 labeled-statement. */
11233 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11234 if (token->type == CPP_COLON)
11235 {
11236 /* Looks like a labeled-statement with an ordinary label.
11237 Parse the label, and then use tail recursion to parse
11238 the statement. */
11239
11240 cp_parser_label_for_labeled_statement (parser, std_attrs);
11241 in_compound = false;
11242 goto restart;
11243 }
11244 }
11245 /* Anything that starts with a `{' must be a compound-statement. */
11246 else if (token->type == CPP_OPEN_BRACE)
11247 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11248 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11249 a statement all its own. */
11250 else if (token->type == CPP_PRAGMA)
11251 {
11252 /* Only certain OpenMP pragmas are attached to statements, and thus
11253 are considered statements themselves. All others are not. In
11254 the context of a compound, accept the pragma as a "statement" and
11255 return so that we can check for a close brace. Otherwise we
11256 require a real statement and must go back and read one. */
11257 if (in_compound)
11258 cp_parser_pragma (parser, pragma_compound, if_p);
11259 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11260 goto restart;
11261 return;
11262 }
11263 else if (token->type == CPP_EOF)
11264 {
11265 cp_parser_error (parser, "expected statement");
11266 return;
11267 }
11268
11269 /* Everything else must be a declaration-statement or an
11270 expression-statement. Try for the declaration-statement
11271 first, unless we are looking at a `;', in which case we know that
11272 we have an expression-statement. */
11273 if (!statement)
11274 {
11275 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11276 {
11277 if (std_attrs != NULL_TREE)
11278 /* Attributes should be parsed as part of the declaration,
11279 so let's un-parse them. */
11280 saved_tokens.rollback();
11281
11282 cp_parser_parse_tentatively (parser);
11283 /* Try to parse the declaration-statement. */
11284 cp_parser_declaration_statement (parser);
11285 /* If that worked, we're done. */
11286 if (cp_parser_parse_definitely (parser))
11287 return;
11288 /* It didn't work, restore the post-attribute position. */
11289 if (std_attrs)
11290 cp_lexer_set_token_position (parser->lexer, statement_token);
11291 }
11292 /* All preceding labels have been parsed at this point. */
11293 if (loc_after_labels != NULL)
11294 *loc_after_labels = statement_location;
11295
11296 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11297
11298 /* Look for an expression-statement instead. */
11299 statement = cp_parser_expression_statement (parser, in_statement_expr);
11300
11301 /* Handle [[fallthrough]];. */
11302 if (attribute_fallthrough_p (std_attrs))
11303 {
11304 /* The next token after the fallthrough attribute is ';'. */
11305 if (statement == NULL_TREE)
11306 {
11307 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11308 statement = build_call_expr_internal_loc (statement_location,
11309 IFN_FALLTHROUGH,
11310 void_type_node, 0);
11311 finish_expr_stmt (statement);
11312 }
11313 else
11314 warning_at (statement_location, OPT_Wattributes,
11315 "%<fallthrough%> attribute not followed by %<;%>");
11316 std_attrs = NULL_TREE;
11317 }
11318 }
11319
11320 /* Set the line number for the statement. */
11321 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11322 SET_EXPR_LOCATION (statement, statement_location);
11323
11324 /* Allow "[[fallthrough]];", but warn otherwise. */
11325 if (std_attrs != NULL_TREE)
11326 warning_at (attrs_loc,
11327 OPT_Wattributes,
11328 "attributes at the beginning of statement are ignored");
11329 }
11330
11331 /* Append ATTR to attribute list ATTRS. */
11332
11333 static tree
11334 attr_chainon (tree attrs, tree attr)
11335 {
11336 if (attrs == error_mark_node)
11337 return error_mark_node;
11338 if (attr == error_mark_node)
11339 return error_mark_node;
11340 return chainon (attrs, attr);
11341 }
11342
11343 /* Parse the label for a labeled-statement, i.e.
11344
11345 identifier :
11346 case constant-expression :
11347 default :
11348
11349 GNU Extension:
11350 case constant-expression ... constant-expression : statement
11351
11352 When a label is parsed without errors, the label is added to the
11353 parse tree by the finish_* functions, so this function doesn't
11354 have to return the label. */
11355
11356 static void
11357 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11358 {
11359 cp_token *token;
11360 tree label = NULL_TREE;
11361 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11362
11363 /* The next token should be an identifier. */
11364 token = cp_lexer_peek_token (parser->lexer);
11365 if (token->type != CPP_NAME
11366 && token->type != CPP_KEYWORD)
11367 {
11368 cp_parser_error (parser, "expected labeled-statement");
11369 return;
11370 }
11371
11372 /* Remember whether this case or a user-defined label is allowed to fall
11373 through to. */
11374 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11375
11376 parser->colon_corrects_to_scope_p = false;
11377 switch (token->keyword)
11378 {
11379 case RID_CASE:
11380 {
11381 tree expr, expr_hi;
11382 cp_token *ellipsis;
11383
11384 /* Consume the `case' token. */
11385 cp_lexer_consume_token (parser->lexer);
11386 /* Parse the constant-expression. */
11387 expr = cp_parser_constant_expression (parser);
11388 if (check_for_bare_parameter_packs (expr))
11389 expr = error_mark_node;
11390
11391 ellipsis = cp_lexer_peek_token (parser->lexer);
11392 if (ellipsis->type == CPP_ELLIPSIS)
11393 {
11394 /* Consume the `...' token. */
11395 cp_lexer_consume_token (parser->lexer);
11396 expr_hi = cp_parser_constant_expression (parser);
11397 if (check_for_bare_parameter_packs (expr_hi))
11398 expr_hi = error_mark_node;
11399
11400 /* We don't need to emit warnings here, as the common code
11401 will do this for us. */
11402 }
11403 else
11404 expr_hi = NULL_TREE;
11405
11406 if (parser->in_switch_statement_p)
11407 {
11408 tree l = finish_case_label (token->location, expr, expr_hi);
11409 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11410 {
11411 label = CASE_LABEL (l);
11412 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11413 }
11414 }
11415 else
11416 error_at (token->location,
11417 "case label %qE not within a switch statement",
11418 expr);
11419 }
11420 break;
11421
11422 case RID_DEFAULT:
11423 /* Consume the `default' token. */
11424 cp_lexer_consume_token (parser->lexer);
11425
11426 if (parser->in_switch_statement_p)
11427 {
11428 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11429 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11430 {
11431 label = CASE_LABEL (l);
11432 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11433 }
11434 }
11435 else
11436 error_at (token->location, "case label not within a switch statement");
11437 break;
11438
11439 default:
11440 /* Anything else must be an ordinary label. */
11441 label = finish_label_stmt (cp_parser_identifier (parser));
11442 if (label && TREE_CODE (label) == LABEL_DECL)
11443 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11444 break;
11445 }
11446
11447 /* Require the `:' token. */
11448 cp_parser_require (parser, CPP_COLON, RT_COLON);
11449
11450 /* An ordinary label may optionally be followed by attributes.
11451 However, this is only permitted if the attributes are then
11452 followed by a semicolon. This is because, for backward
11453 compatibility, when parsing
11454 lab: __attribute__ ((unused)) int i;
11455 we want the attribute to attach to "i", not "lab". */
11456 if (label != NULL_TREE
11457 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11458 {
11459 tree attrs;
11460 cp_parser_parse_tentatively (parser);
11461 attrs = cp_parser_gnu_attributes_opt (parser);
11462 if (attrs == NULL_TREE
11463 /* And fallthrough always binds to the expression-statement. */
11464 || attribute_fallthrough_p (attrs)
11465 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11466 cp_parser_abort_tentative_parse (parser);
11467 else if (!cp_parser_parse_definitely (parser))
11468 ;
11469 else
11470 attributes = attr_chainon (attributes, attrs);
11471 }
11472
11473 if (attributes != NULL_TREE)
11474 cplus_decl_attributes (&label, attributes, 0);
11475
11476 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11477 }
11478
11479 /* Parse an expression-statement.
11480
11481 expression-statement:
11482 expression [opt] ;
11483
11484 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11485 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11486 indicates whether this expression-statement is part of an
11487 expression statement. */
11488
11489 static tree
11490 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11491 {
11492 tree statement = NULL_TREE;
11493 cp_token *token = cp_lexer_peek_token (parser->lexer);
11494 location_t loc = token->location;
11495
11496 /* There might be attribute fallthrough. */
11497 tree attr = cp_parser_gnu_attributes_opt (parser);
11498
11499 /* If the next token is a ';', then there is no expression
11500 statement. */
11501 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11502 {
11503 statement = cp_parser_expression (parser);
11504 if (statement == error_mark_node
11505 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11506 {
11507 cp_parser_skip_to_end_of_block_or_statement (parser);
11508 return error_mark_node;
11509 }
11510 }
11511
11512 /* Handle [[fallthrough]];. */
11513 if (attribute_fallthrough_p (attr))
11514 {
11515 /* The next token after the fallthrough attribute is ';'. */
11516 if (statement == NULL_TREE)
11517 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11518 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11519 void_type_node, 0);
11520 else
11521 warning_at (loc, OPT_Wattributes,
11522 "%<fallthrough%> attribute not followed by %<;%>");
11523 attr = NULL_TREE;
11524 }
11525
11526 /* Allow "[[fallthrough]];", but warn otherwise. */
11527 if (attr != NULL_TREE)
11528 warning_at (loc, OPT_Wattributes,
11529 "attributes at the beginning of statement are ignored");
11530
11531 /* Give a helpful message for "A<T>::type t;" and the like. */
11532 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11533 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11534 {
11535 if (TREE_CODE (statement) == SCOPE_REF)
11536 error_at (token->location, "need %<typename%> before %qE because "
11537 "%qT is a dependent scope",
11538 statement, TREE_OPERAND (statement, 0));
11539 else if (is_overloaded_fn (statement)
11540 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11541 {
11542 /* A::A a; */
11543 tree fn = get_first_fn (statement);
11544 error_at (token->location,
11545 "%<%T::%D%> names the constructor, not the type",
11546 DECL_CONTEXT (fn), DECL_NAME (fn));
11547 }
11548 }
11549
11550 /* Consume the final `;'. */
11551 cp_parser_consume_semicolon_at_end_of_statement (parser);
11552
11553 if (in_statement_expr
11554 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11555 /* This is the final expression statement of a statement
11556 expression. */
11557 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11558 else if (statement)
11559 statement = finish_expr_stmt (statement);
11560
11561 return statement;
11562 }
11563
11564 /* Parse a compound-statement.
11565
11566 compound-statement:
11567 { statement-seq [opt] }
11568
11569 GNU extension:
11570
11571 compound-statement:
11572 { label-declaration-seq [opt] statement-seq [opt] }
11573
11574 label-declaration-seq:
11575 label-declaration
11576 label-declaration-seq label-declaration
11577
11578 Returns a tree representing the statement. */
11579
11580 static tree
11581 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11582 int bcs_flags, bool function_body)
11583 {
11584 tree compound_stmt;
11585 matching_braces braces;
11586
11587 /* Consume the `{'. */
11588 if (!braces.require_open (parser))
11589 return error_mark_node;
11590 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11591 && !function_body && cxx_dialect < cxx14)
11592 pedwarn (input_location, OPT_Wpedantic,
11593 "compound-statement in %<constexpr%> function");
11594 /* Begin the compound-statement. */
11595 compound_stmt = begin_compound_stmt (bcs_flags);
11596 /* If the next keyword is `__label__' we have a label declaration. */
11597 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11598 cp_parser_label_declaration (parser);
11599 /* Parse an (optional) statement-seq. */
11600 cp_parser_statement_seq_opt (parser, in_statement_expr);
11601 /* Finish the compound-statement. */
11602 finish_compound_stmt (compound_stmt);
11603 /* Consume the `}'. */
11604 braces.require_close (parser);
11605
11606 return compound_stmt;
11607 }
11608
11609 /* Parse an (optional) statement-seq.
11610
11611 statement-seq:
11612 statement
11613 statement-seq [opt] statement */
11614
11615 static void
11616 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11617 {
11618 /* Scan statements until there aren't any more. */
11619 while (true)
11620 {
11621 cp_token *token = cp_lexer_peek_token (parser->lexer);
11622
11623 /* If we are looking at a `}', then we have run out of
11624 statements; the same is true if we have reached the end
11625 of file, or have stumbled upon a stray '@end'. */
11626 if (token->type == CPP_CLOSE_BRACE
11627 || token->type == CPP_EOF
11628 || token->type == CPP_PRAGMA_EOL
11629 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11630 break;
11631
11632 /* If we are in a compound statement and find 'else' then
11633 something went wrong. */
11634 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11635 {
11636 if (parser->in_statement & IN_IF_STMT)
11637 break;
11638 else
11639 {
11640 token = cp_lexer_consume_token (parser->lexer);
11641 error_at (token->location, "%<else%> without a previous %<if%>");
11642 }
11643 }
11644
11645 /* Parse the statement. */
11646 cp_parser_statement (parser, in_statement_expr, true, NULL);
11647 }
11648 }
11649
11650 /* Return true if this is the C++20 version of range-based-for with
11651 init-statement. */
11652
11653 static bool
11654 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11655 {
11656 bool r = false;
11657
11658 /* Save tokens so that we can put them back. */
11659 cp_lexer_save_tokens (parser->lexer);
11660
11661 /* There has to be an unnested ; followed by an unnested :. */
11662 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11663 /*recovering=*/false,
11664 CPP_SEMICOLON,
11665 /*consume_paren=*/false) != -1)
11666 goto out;
11667
11668 /* We found the semicolon, eat it now. */
11669 cp_lexer_consume_token (parser->lexer);
11670
11671 /* Now look for ':' that is not nested in () or {}. */
11672 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11673 /*recovering=*/false,
11674 CPP_COLON,
11675 /*consume_paren=*/false) == -1);
11676
11677 out:
11678 /* Roll back the tokens we skipped. */
11679 cp_lexer_rollback_tokens (parser->lexer);
11680
11681 return r;
11682 }
11683
11684 /* Return true if we're looking at (init; cond), false otherwise. */
11685
11686 static bool
11687 cp_parser_init_statement_p (cp_parser *parser)
11688 {
11689 /* Save tokens so that we can put them back. */
11690 cp_lexer_save_tokens (parser->lexer);
11691
11692 /* Look for ';' that is not nested in () or {}. */
11693 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11694 /*recovering=*/false,
11695 CPP_SEMICOLON,
11696 /*consume_paren=*/false);
11697
11698 /* Roll back the tokens we skipped. */
11699 cp_lexer_rollback_tokens (parser->lexer);
11700
11701 return ret == -1;
11702 }
11703
11704 /* Parse a selection-statement.
11705
11706 selection-statement:
11707 if ( init-statement [opt] condition ) statement
11708 if ( init-statement [opt] condition ) statement else statement
11709 switch ( init-statement [opt] condition ) statement
11710
11711 Returns the new IF_STMT or SWITCH_STMT.
11712
11713 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11714 is a (possibly labeled) if statement which is not enclosed in
11715 braces and has an else clause. This is used to implement
11716 -Wparentheses.
11717
11718 CHAIN is a vector of if-else-if conditions. This is used to implement
11719 -Wduplicated-cond. */
11720
11721 static tree
11722 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11723 vec<tree> *chain)
11724 {
11725 cp_token *token;
11726 enum rid keyword;
11727 token_indent_info guard_tinfo;
11728
11729 if (if_p != NULL)
11730 *if_p = false;
11731
11732 /* Peek at the next token. */
11733 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11734 guard_tinfo = get_token_indent_info (token);
11735
11736 /* See what kind of keyword it is. */
11737 keyword = token->keyword;
11738 switch (keyword)
11739 {
11740 case RID_IF:
11741 case RID_SWITCH:
11742 {
11743 tree statement;
11744 tree condition;
11745
11746 bool cx = false;
11747 if (keyword == RID_IF
11748 && cp_lexer_next_token_is_keyword (parser->lexer,
11749 RID_CONSTEXPR))
11750 {
11751 cx = true;
11752 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11753 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11754 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11755 "with %<-std=c++17%> or %<-std=gnu++17%>");
11756 }
11757
11758 /* Look for the `('. */
11759 matching_parens parens;
11760 if (!parens.require_open (parser))
11761 {
11762 cp_parser_skip_to_end_of_statement (parser);
11763 return error_mark_node;
11764 }
11765
11766 /* Begin the selection-statement. */
11767 if (keyword == RID_IF)
11768 {
11769 statement = begin_if_stmt ();
11770 IF_STMT_CONSTEXPR_P (statement) = cx;
11771 }
11772 else
11773 statement = begin_switch_stmt ();
11774
11775 /* Parse the optional init-statement. */
11776 if (cp_parser_init_statement_p (parser))
11777 {
11778 tree decl;
11779 if (cxx_dialect < cxx17)
11780 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11781 "init-statement in selection statements only available "
11782 "with %<-std=c++17%> or %<-std=gnu++17%>");
11783 cp_parser_init_statement (parser, &decl);
11784 }
11785
11786 /* Parse the condition. */
11787 condition = cp_parser_condition (parser);
11788 /* Look for the `)'. */
11789 if (!parens.require_close (parser))
11790 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11791 /*consume_paren=*/true);
11792
11793 if (keyword == RID_IF)
11794 {
11795 bool nested_if;
11796 unsigned char in_statement;
11797
11798 /* Add the condition. */
11799 condition = finish_if_stmt_cond (condition, statement);
11800
11801 if (warn_duplicated_cond)
11802 warn_duplicated_cond_add_or_warn (token->location, condition,
11803 &chain);
11804
11805 /* Parse the then-clause. */
11806 in_statement = parser->in_statement;
11807 parser->in_statement |= IN_IF_STMT;
11808
11809 /* Outside a template, the non-selected branch of a constexpr
11810 if is a 'discarded statement', i.e. unevaluated. */
11811 bool was_discarded = in_discarded_stmt;
11812 bool discard_then = (cx && !processing_template_decl
11813 && integer_zerop (condition));
11814 if (discard_then)
11815 {
11816 in_discarded_stmt = true;
11817 ++c_inhibit_evaluation_warnings;
11818 }
11819
11820 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11821 guard_tinfo);
11822
11823 parser->in_statement = in_statement;
11824
11825 finish_then_clause (statement);
11826
11827 if (discard_then)
11828 {
11829 THEN_CLAUSE (statement) = NULL_TREE;
11830 in_discarded_stmt = was_discarded;
11831 --c_inhibit_evaluation_warnings;
11832 }
11833
11834 /* If the next token is `else', parse the else-clause. */
11835 if (cp_lexer_next_token_is_keyword (parser->lexer,
11836 RID_ELSE))
11837 {
11838 bool discard_else = (cx && !processing_template_decl
11839 && integer_nonzerop (condition));
11840 if (discard_else)
11841 {
11842 in_discarded_stmt = true;
11843 ++c_inhibit_evaluation_warnings;
11844 }
11845
11846 guard_tinfo
11847 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11848 /* Consume the `else' keyword. */
11849 cp_lexer_consume_token (parser->lexer);
11850 if (warn_duplicated_cond)
11851 {
11852 if (cp_lexer_next_token_is_keyword (parser->lexer,
11853 RID_IF)
11854 && chain == NULL)
11855 {
11856 /* We've got "if (COND) else if (COND2)". Start
11857 the condition chain and add COND as the first
11858 element. */
11859 chain = new vec<tree> ();
11860 if (!CONSTANT_CLASS_P (condition)
11861 && !TREE_SIDE_EFFECTS (condition))
11862 {
11863 /* Wrap it in a NOP_EXPR so that we can set the
11864 location of the condition. */
11865 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11866 condition);
11867 SET_EXPR_LOCATION (e, token->location);
11868 chain->safe_push (e);
11869 }
11870 }
11871 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11872 RID_IF))
11873 {
11874 /* This is if-else without subsequent if. Zap the
11875 condition chain; we would have already warned at
11876 this point. */
11877 delete chain;
11878 chain = NULL;
11879 }
11880 }
11881 begin_else_clause (statement);
11882 /* Parse the else-clause. */
11883 cp_parser_implicitly_scoped_statement (parser, NULL,
11884 guard_tinfo, chain);
11885
11886 finish_else_clause (statement);
11887
11888 /* If we are currently parsing a then-clause, then
11889 IF_P will not be NULL. We set it to true to
11890 indicate that this if statement has an else clause.
11891 This may trigger the Wparentheses warning below
11892 when we get back up to the parent if statement. */
11893 if (if_p != NULL)
11894 *if_p = true;
11895
11896 if (discard_else)
11897 {
11898 ELSE_CLAUSE (statement) = NULL_TREE;
11899 in_discarded_stmt = was_discarded;
11900 --c_inhibit_evaluation_warnings;
11901 }
11902 }
11903 else
11904 {
11905 /* This if statement does not have an else clause. If
11906 NESTED_IF is true, then the then-clause has an if
11907 statement which does have an else clause. We warn
11908 about the potential ambiguity. */
11909 if (nested_if)
11910 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11911 "suggest explicit braces to avoid ambiguous"
11912 " %<else%>");
11913 if (warn_duplicated_cond)
11914 {
11915 /* We don't need the condition chain anymore. */
11916 delete chain;
11917 chain = NULL;
11918 }
11919 }
11920
11921 /* Now we're all done with the if-statement. */
11922 finish_if_stmt (statement);
11923 }
11924 else
11925 {
11926 bool in_switch_statement_p;
11927 unsigned char in_statement;
11928
11929 /* Add the condition. */
11930 finish_switch_cond (condition, statement);
11931
11932 /* Parse the body of the switch-statement. */
11933 in_switch_statement_p = parser->in_switch_statement_p;
11934 in_statement = parser->in_statement;
11935 parser->in_switch_statement_p = true;
11936 parser->in_statement |= IN_SWITCH_STMT;
11937 cp_parser_implicitly_scoped_statement (parser, if_p,
11938 guard_tinfo);
11939 parser->in_switch_statement_p = in_switch_statement_p;
11940 parser->in_statement = in_statement;
11941
11942 /* Now we're all done with the switch-statement. */
11943 finish_switch_stmt (statement);
11944 }
11945
11946 return statement;
11947 }
11948 break;
11949
11950 default:
11951 cp_parser_error (parser, "expected selection-statement");
11952 return error_mark_node;
11953 }
11954 }
11955
11956 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11957 If we have seen at least one decl-specifier, and the next token
11958 is not a parenthesis, then we must be looking at a declaration.
11959 (After "int (" we might be looking at a functional cast.) */
11960
11961 static void
11962 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11963 bool any_specifiers_p)
11964 {
11965 if (any_specifiers_p
11966 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11967 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11968 && !cp_parser_error_occurred (parser))
11969 cp_parser_commit_to_tentative_parse (parser);
11970 }
11971
11972 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11973 The declarator shall not specify a function or an array. Returns
11974 TRUE if the declarator is valid, FALSE otherwise. */
11975
11976 static bool
11977 cp_parser_check_condition_declarator (cp_parser* parser,
11978 cp_declarator *declarator,
11979 location_t loc)
11980 {
11981 if (declarator == cp_error_declarator
11982 || function_declarator_p (declarator)
11983 || declarator->kind == cdk_array)
11984 {
11985 if (declarator == cp_error_declarator)
11986 /* Already complained. */;
11987 else if (declarator->kind == cdk_array)
11988 error_at (loc, "condition declares an array");
11989 else
11990 error_at (loc, "condition declares a function");
11991 if (parser->fully_implicit_function_template_p)
11992 abort_fully_implicit_template (parser);
11993 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11994 /*or_comma=*/false,
11995 /*consume_paren=*/false);
11996 return false;
11997 }
11998 else
11999 return true;
12000 }
12001
12002 /* Parse a condition.
12003
12004 condition:
12005 expression
12006 type-specifier-seq declarator = initializer-clause
12007 type-specifier-seq declarator braced-init-list
12008
12009 GNU Extension:
12010
12011 condition:
12012 type-specifier-seq declarator asm-specification [opt]
12013 attributes [opt] = assignment-expression
12014
12015 Returns the expression that should be tested. */
12016
12017 static tree
12018 cp_parser_condition (cp_parser* parser)
12019 {
12020 cp_decl_specifier_seq type_specifiers;
12021 const char *saved_message;
12022 int declares_class_or_enum;
12023
12024 /* Try the declaration first. */
12025 cp_parser_parse_tentatively (parser);
12026 /* New types are not allowed in the type-specifier-seq for a
12027 condition. */
12028 saved_message = parser->type_definition_forbidden_message;
12029 parser->type_definition_forbidden_message
12030 = G_("types may not be defined in conditions");
12031 /* Parse the type-specifier-seq. */
12032 cp_parser_decl_specifier_seq (parser,
12033 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
12034 &type_specifiers,
12035 &declares_class_or_enum);
12036 /* Restore the saved message. */
12037 parser->type_definition_forbidden_message = saved_message;
12038
12039 cp_parser_maybe_commit_to_declaration (parser,
12040 type_specifiers.any_specifiers_p);
12041
12042 /* If all is well, we might be looking at a declaration. */
12043 if (!cp_parser_error_occurred (parser))
12044 {
12045 tree decl;
12046 tree asm_specification;
12047 tree attributes;
12048 cp_declarator *declarator;
12049 tree initializer = NULL_TREE;
12050 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12051
12052 /* Parse the declarator. */
12053 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12054 CP_PARSER_FLAGS_NONE,
12055 /*ctor_dtor_or_conv_p=*/NULL,
12056 /*parenthesized_p=*/NULL,
12057 /*member_p=*/false,
12058 /*friend_p=*/false,
12059 /*static_p=*/false);
12060 /* Parse the attributes. */
12061 attributes = cp_parser_attributes_opt (parser);
12062 /* Parse the asm-specification. */
12063 asm_specification = cp_parser_asm_specification_opt (parser);
12064 /* If the next token is not an `=' or '{', then we might still be
12065 looking at an expression. For example:
12066
12067 if (A(a).x)
12068
12069 looks like a decl-specifier-seq and a declarator -- but then
12070 there is no `=', so this is an expression. */
12071 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12072 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12073 cp_parser_simulate_error (parser);
12074
12075 /* If we did see an `=' or '{', then we are looking at a declaration
12076 for sure. */
12077 if (cp_parser_parse_definitely (parser))
12078 {
12079 tree pushed_scope;
12080 bool non_constant_p = false;
12081 int flags = LOOKUP_ONLYCONVERTING;
12082
12083 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12084 return error_mark_node;
12085
12086 /* Create the declaration. */
12087 decl = start_decl (declarator, &type_specifiers,
12088 /*initialized_p=*/true,
12089 attributes, /*prefix_attributes=*/NULL_TREE,
12090 &pushed_scope);
12091
12092 /* Parse the initializer. */
12093 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12094 {
12095 initializer = cp_parser_braced_list (parser, &non_constant_p);
12096 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12097 flags = 0;
12098 }
12099 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12100 {
12101 /* Consume the `='. */
12102 cp_lexer_consume_token (parser->lexer);
12103 initializer = cp_parser_initializer_clause (parser,
12104 &non_constant_p);
12105 }
12106 else
12107 {
12108 cp_parser_error (parser, "expected initializer");
12109 initializer = error_mark_node;
12110 }
12111 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12112 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12113
12114 /* Process the initializer. */
12115 cp_finish_decl (decl,
12116 initializer, !non_constant_p,
12117 asm_specification,
12118 flags);
12119
12120 if (pushed_scope)
12121 pop_scope (pushed_scope);
12122
12123 return convert_from_reference (decl);
12124 }
12125 }
12126 /* If we didn't even get past the declarator successfully, we are
12127 definitely not looking at a declaration. */
12128 else
12129 cp_parser_abort_tentative_parse (parser);
12130
12131 /* Otherwise, we are looking at an expression. */
12132 return cp_parser_expression (parser);
12133 }
12134
12135 /* Parses a for-statement or range-for-statement until the closing ')',
12136 not included. */
12137
12138 static tree
12139 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12140 {
12141 tree init, scope, decl;
12142 bool is_range_for;
12143
12144 /* Begin the for-statement. */
12145 scope = begin_for_scope (&init);
12146
12147 /* Parse the initialization. */
12148 is_range_for = cp_parser_init_statement (parser, &decl);
12149
12150 if (is_range_for)
12151 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12152 false);
12153 else
12154 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12155 }
12156
12157 static tree
12158 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12159 unsigned short unroll)
12160 {
12161 /* Normal for loop */
12162 tree condition = NULL_TREE;
12163 tree expression = NULL_TREE;
12164 tree stmt;
12165
12166 stmt = begin_for_stmt (scope, init);
12167 /* The init-statement has already been parsed in
12168 cp_parser_init_statement, so no work is needed here. */
12169 finish_init_stmt (stmt);
12170
12171 /* If there's a condition, process it. */
12172 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12173 condition = cp_parser_condition (parser);
12174 else if (ivdep)
12175 {
12176 cp_parser_error (parser, "missing loop condition in loop with "
12177 "%<GCC ivdep%> pragma");
12178 condition = error_mark_node;
12179 }
12180 else if (unroll)
12181 {
12182 cp_parser_error (parser, "missing loop condition in loop with "
12183 "%<GCC unroll%> pragma");
12184 condition = error_mark_node;
12185 }
12186 finish_for_cond (condition, stmt, ivdep, unroll);
12187 /* Look for the `;'. */
12188 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12189
12190 /* If there's an expression, process it. */
12191 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12192 expression = cp_parser_expression (parser);
12193 finish_for_expr (expression, stmt);
12194
12195 return stmt;
12196 }
12197
12198 /* Tries to parse a range-based for-statement:
12199
12200 range-based-for:
12201 decl-specifier-seq declarator : expression
12202
12203 The decl-specifier-seq declarator and the `:' are already parsed by
12204 cp_parser_init_statement. If processing_template_decl it returns a
12205 newly created RANGE_FOR_STMT; if not, it is converted to a
12206 regular FOR_STMT. */
12207
12208 static tree
12209 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12210 bool ivdep, unsigned short unroll, bool is_omp)
12211 {
12212 tree stmt, range_expr;
12213 auto_vec <cxx_binding *, 16> bindings;
12214 auto_vec <tree, 16> names;
12215 tree decomp_first_name = NULL_TREE;
12216 unsigned int decomp_cnt = 0;
12217
12218 /* Get the range declaration momentarily out of the way so that
12219 the range expression doesn't clash with it. */
12220 if (range_decl != error_mark_node)
12221 {
12222 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12223 {
12224 tree v = DECL_VALUE_EXPR (range_decl);
12225 /* For decomposition declaration get all of the corresponding
12226 declarations out of the way. */
12227 if (TREE_CODE (v) == ARRAY_REF
12228 && VAR_P (TREE_OPERAND (v, 0))
12229 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12230 {
12231 tree d = range_decl;
12232 range_decl = TREE_OPERAND (v, 0);
12233 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12234 decomp_first_name = d;
12235 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12236 {
12237 tree name = DECL_NAME (d);
12238 names.safe_push (name);
12239 bindings.safe_push (IDENTIFIER_BINDING (name));
12240 IDENTIFIER_BINDING (name)
12241 = IDENTIFIER_BINDING (name)->previous;
12242 }
12243 }
12244 }
12245 if (names.is_empty ())
12246 {
12247 tree name = DECL_NAME (range_decl);
12248 names.safe_push (name);
12249 bindings.safe_push (IDENTIFIER_BINDING (name));
12250 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12251 }
12252 }
12253
12254 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12255 {
12256 bool expr_non_constant_p;
12257 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12258 }
12259 else
12260 range_expr = cp_parser_expression (parser);
12261
12262 /* Put the range declaration(s) back into scope. */
12263 for (unsigned int i = 0; i < names.length (); i++)
12264 {
12265 cxx_binding *binding = bindings[i];
12266 binding->previous = IDENTIFIER_BINDING (names[i]);
12267 IDENTIFIER_BINDING (names[i]) = binding;
12268 }
12269
12270 /* finish_omp_for has its own code for the following, so just
12271 return the range_expr instead. */
12272 if (is_omp)
12273 return range_expr;
12274
12275 /* If in template, STMT is converted to a normal for-statement
12276 at instantiation. If not, it is done just ahead. */
12277 if (processing_template_decl)
12278 {
12279 if (check_for_bare_parameter_packs (range_expr))
12280 range_expr = error_mark_node;
12281 stmt = begin_range_for_stmt (scope, init);
12282 if (ivdep)
12283 RANGE_FOR_IVDEP (stmt) = 1;
12284 if (unroll)
12285 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12286 finish_range_for_decl (stmt, range_decl, range_expr);
12287 if (!type_dependent_expression_p (range_expr)
12288 /* do_auto_deduction doesn't mess with template init-lists. */
12289 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12290 do_range_for_auto_deduction (range_decl, range_expr);
12291 }
12292 else
12293 {
12294 stmt = begin_for_stmt (scope, init);
12295 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12296 decomp_first_name, decomp_cnt, ivdep,
12297 unroll);
12298 }
12299 return stmt;
12300 }
12301
12302 /* Subroutine of cp_convert_range_for: given the initializer expression,
12303 builds up the range temporary. */
12304
12305 static tree
12306 build_range_temp (tree range_expr)
12307 {
12308 tree range_type, range_temp;
12309
12310 /* Find out the type deduced by the declaration
12311 `auto &&__range = range_expr'. */
12312 range_type = cp_build_reference_type (make_auto (), true);
12313 range_type = do_auto_deduction (range_type, range_expr,
12314 type_uses_auto (range_type));
12315
12316 /* Create the __range variable. */
12317 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12318 range_type);
12319 TREE_USED (range_temp) = 1;
12320 DECL_ARTIFICIAL (range_temp) = 1;
12321
12322 return range_temp;
12323 }
12324
12325 /* Used by cp_parser_range_for in template context: we aren't going to
12326 do a full conversion yet, but we still need to resolve auto in the
12327 type of the for-range-declaration if present. This is basically
12328 a shortcut version of cp_convert_range_for. */
12329
12330 static void
12331 do_range_for_auto_deduction (tree decl, tree range_expr)
12332 {
12333 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12334 if (auto_node)
12335 {
12336 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12337 range_temp = convert_from_reference (build_range_temp (range_expr));
12338 iter_type = (cp_parser_perform_range_for_lookup
12339 (range_temp, &begin_dummy, &end_dummy));
12340 if (iter_type)
12341 {
12342 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12343 iter_type);
12344 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12345 RO_UNARY_STAR,
12346 tf_warning_or_error);
12347 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12348 iter_decl, auto_node);
12349 }
12350 }
12351 }
12352
12353 /* Converts a range-based for-statement into a normal
12354 for-statement, as per the definition.
12355
12356 for (RANGE_DECL : RANGE_EXPR)
12357 BLOCK
12358
12359 should be equivalent to:
12360
12361 {
12362 auto &&__range = RANGE_EXPR;
12363 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12364 __begin != __end;
12365 ++__begin)
12366 {
12367 RANGE_DECL = *__begin;
12368 BLOCK
12369 }
12370 }
12371
12372 If RANGE_EXPR is an array:
12373 BEGIN_EXPR = __range
12374 END_EXPR = __range + ARRAY_SIZE(__range)
12375 Else if RANGE_EXPR has a member 'begin' or 'end':
12376 BEGIN_EXPR = __range.begin()
12377 END_EXPR = __range.end()
12378 Else:
12379 BEGIN_EXPR = begin(__range)
12380 END_EXPR = end(__range);
12381
12382 If __range has a member 'begin' but not 'end', or vice versa, we must
12383 still use the second alternative (it will surely fail, however).
12384 When calling begin()/end() in the third alternative we must use
12385 argument dependent lookup, but always considering 'std' as an associated
12386 namespace. */
12387
12388 tree
12389 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12390 tree decomp_first_name, unsigned int decomp_cnt,
12391 bool ivdep, unsigned short unroll)
12392 {
12393 tree begin, end;
12394 tree iter_type, begin_expr, end_expr;
12395 tree condition, expression;
12396
12397 range_expr = mark_lvalue_use (range_expr);
12398
12399 if (range_decl == error_mark_node || range_expr == error_mark_node)
12400 /* If an error happened previously do nothing or else a lot of
12401 unhelpful errors would be issued. */
12402 begin_expr = end_expr = iter_type = error_mark_node;
12403 else
12404 {
12405 tree range_temp;
12406
12407 if (VAR_P (range_expr)
12408 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12409 /* Can't bind a reference to an array of runtime bound. */
12410 range_temp = range_expr;
12411 else
12412 {
12413 range_temp = build_range_temp (range_expr);
12414 pushdecl (range_temp);
12415 cp_finish_decl (range_temp, range_expr,
12416 /*is_constant_init*/false, NULL_TREE,
12417 LOOKUP_ONLYCONVERTING);
12418 range_temp = convert_from_reference (range_temp);
12419 }
12420 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12421 &begin_expr, &end_expr);
12422 }
12423
12424 /* The new for initialization statement. */
12425 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12426 iter_type);
12427 TREE_USED (begin) = 1;
12428 DECL_ARTIFICIAL (begin) = 1;
12429 pushdecl (begin);
12430 cp_finish_decl (begin, begin_expr,
12431 /*is_constant_init*/false, NULL_TREE,
12432 LOOKUP_ONLYCONVERTING);
12433
12434 if (cxx_dialect >= cxx17)
12435 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12436 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12437 TREE_USED (end) = 1;
12438 DECL_ARTIFICIAL (end) = 1;
12439 pushdecl (end);
12440 cp_finish_decl (end, end_expr,
12441 /*is_constant_init*/false, NULL_TREE,
12442 LOOKUP_ONLYCONVERTING);
12443
12444 finish_init_stmt (statement);
12445
12446 /* The new for condition. */
12447 condition = build_x_binary_op (input_location, NE_EXPR,
12448 begin, ERROR_MARK,
12449 end, ERROR_MARK,
12450 NULL, tf_warning_or_error);
12451 finish_for_cond (condition, statement, ivdep, unroll);
12452
12453 /* The new increment expression. */
12454 expression = finish_unary_op_expr (input_location,
12455 PREINCREMENT_EXPR, begin,
12456 tf_warning_or_error);
12457 finish_for_expr (expression, statement);
12458
12459 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12460 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12461
12462 /* The declaration is initialized with *__begin inside the loop body. */
12463 cp_finish_decl (range_decl,
12464 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12465 tf_warning_or_error),
12466 /*is_constant_init*/false, NULL_TREE,
12467 LOOKUP_ONLYCONVERTING);
12468 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12469 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12470
12471 return statement;
12472 }
12473
12474 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12475 We need to solve both at the same time because the method used
12476 depends on the existence of members begin or end.
12477 Returns the type deduced for the iterator expression. */
12478
12479 static tree
12480 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12481 {
12482 if (error_operand_p (range))
12483 {
12484 *begin = *end = error_mark_node;
12485 return error_mark_node;
12486 }
12487
12488 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12489 {
12490 error ("range-based %<for%> expression of type %qT "
12491 "has incomplete type", TREE_TYPE (range));
12492 *begin = *end = error_mark_node;
12493 return error_mark_node;
12494 }
12495 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12496 {
12497 /* If RANGE is an array, we will use pointer arithmetic. */
12498 *begin = decay_conversion (range, tf_warning_or_error);
12499 *end = build_binary_op (input_location, PLUS_EXPR,
12500 range,
12501 array_type_nelts_top (TREE_TYPE (range)),
12502 false);
12503 return TREE_TYPE (*begin);
12504 }
12505 else
12506 {
12507 /* If it is not an array, we must do a bit of magic. */
12508 tree id_begin, id_end;
12509 tree member_begin, member_end;
12510
12511 *begin = *end = error_mark_node;
12512
12513 id_begin = get_identifier ("begin");
12514 id_end = get_identifier ("end");
12515 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12516 /*protect=*/2, /*want_type=*/false,
12517 tf_warning_or_error);
12518 member_end = lookup_member (TREE_TYPE (range), id_end,
12519 /*protect=*/2, /*want_type=*/false,
12520 tf_warning_or_error);
12521
12522 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12523 {
12524 /* Use the member functions. */
12525 *begin = cp_parser_range_for_member_function (range, id_begin);
12526 *end = cp_parser_range_for_member_function (range, id_end);
12527 }
12528 else
12529 {
12530 /* Use global functions with ADL. */
12531 releasing_vec vec;
12532
12533 vec_safe_push (vec, range);
12534
12535 member_begin = perform_koenig_lookup (id_begin, vec,
12536 tf_warning_or_error);
12537 *begin = finish_call_expr (member_begin, &vec, false, true,
12538 tf_warning_or_error);
12539 member_end = perform_koenig_lookup (id_end, vec,
12540 tf_warning_or_error);
12541 *end = finish_call_expr (member_end, &vec, false, true,
12542 tf_warning_or_error);
12543 }
12544
12545 /* Last common checks. */
12546 if (*begin == error_mark_node || *end == error_mark_node)
12547 {
12548 /* If one of the expressions is an error do no more checks. */
12549 *begin = *end = error_mark_node;
12550 return error_mark_node;
12551 }
12552 else if (type_dependent_expression_p (*begin)
12553 || type_dependent_expression_p (*end))
12554 /* Can happen, when, eg, in a template context, Koenig lookup
12555 can't resolve begin/end (c++/58503). */
12556 return NULL_TREE;
12557 else
12558 {
12559 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12560 /* The unqualified type of the __begin and __end temporaries should
12561 be the same, as required by the multiple auto declaration. */
12562 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12563 {
12564 if (cxx_dialect >= cxx17
12565 && (build_x_binary_op (input_location, NE_EXPR,
12566 *begin, ERROR_MARK,
12567 *end, ERROR_MARK,
12568 NULL, tf_none)
12569 != error_mark_node))
12570 /* P0184R0 allows __begin and __end to have different types,
12571 but make sure they are comparable so we can give a better
12572 diagnostic. */;
12573 else
12574 error ("inconsistent begin/end types in range-based %<for%> "
12575 "statement: %qT and %qT",
12576 TREE_TYPE (*begin), TREE_TYPE (*end));
12577 }
12578 return iter_type;
12579 }
12580 }
12581 }
12582
12583 /* Helper function for cp_parser_perform_range_for_lookup.
12584 Builds a tree for RANGE.IDENTIFIER(). */
12585
12586 static tree
12587 cp_parser_range_for_member_function (tree range, tree identifier)
12588 {
12589 tree member, res;
12590
12591 member = finish_class_member_access_expr (range, identifier,
12592 false, tf_warning_or_error);
12593 if (member == error_mark_node)
12594 return error_mark_node;
12595
12596 releasing_vec vec;
12597 res = finish_call_expr (member, &vec,
12598 /*disallow_virtual=*/false,
12599 /*koenig_p=*/false,
12600 tf_warning_or_error);
12601 return res;
12602 }
12603
12604 /* Parse an iteration-statement.
12605
12606 iteration-statement:
12607 while ( condition ) statement
12608 do statement while ( expression ) ;
12609 for ( init-statement condition [opt] ; expression [opt] )
12610 statement
12611
12612 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12613
12614 static tree
12615 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12616 unsigned short unroll)
12617 {
12618 cp_token *token;
12619 enum rid keyword;
12620 tree statement;
12621 unsigned char in_statement;
12622 token_indent_info guard_tinfo;
12623
12624 /* Peek at the next token. */
12625 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12626 if (!token)
12627 return error_mark_node;
12628
12629 guard_tinfo = get_token_indent_info (token);
12630
12631 /* Remember whether or not we are already within an iteration
12632 statement. */
12633 in_statement = parser->in_statement;
12634
12635 /* See what kind of keyword it is. */
12636 keyword = token->keyword;
12637 switch (keyword)
12638 {
12639 case RID_WHILE:
12640 {
12641 tree condition;
12642
12643 /* Begin the while-statement. */
12644 statement = begin_while_stmt ();
12645 /* Look for the `('. */
12646 matching_parens parens;
12647 parens.require_open (parser);
12648 /* Parse the condition. */
12649 condition = cp_parser_condition (parser);
12650 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12651 /* Look for the `)'. */
12652 parens.require_close (parser);
12653 /* Parse the dependent statement. */
12654 parser->in_statement = IN_ITERATION_STMT;
12655 bool prev = note_iteration_stmt_body_start ();
12656 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12657 note_iteration_stmt_body_end (prev);
12658 parser->in_statement = in_statement;
12659 /* We're done with the while-statement. */
12660 finish_while_stmt (statement);
12661 }
12662 break;
12663
12664 case RID_DO:
12665 {
12666 tree expression;
12667
12668 /* Begin the do-statement. */
12669 statement = begin_do_stmt ();
12670 /* Parse the body of the do-statement. */
12671 parser->in_statement = IN_ITERATION_STMT;
12672 bool prev = note_iteration_stmt_body_start ();
12673 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12674 note_iteration_stmt_body_end (prev);
12675 parser->in_statement = in_statement;
12676 finish_do_body (statement);
12677 /* Look for the `while' keyword. */
12678 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12679 /* Look for the `('. */
12680 matching_parens parens;
12681 parens.require_open (parser);
12682 /* Parse the expression. */
12683 expression = cp_parser_expression (parser);
12684 /* We're done with the do-statement. */
12685 finish_do_stmt (expression, statement, ivdep, unroll);
12686 /* Look for the `)'. */
12687 parens.require_close (parser);
12688 /* Look for the `;'. */
12689 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12690 }
12691 break;
12692
12693 case RID_FOR:
12694 {
12695 /* Look for the `('. */
12696 matching_parens parens;
12697 parens.require_open (parser);
12698
12699 statement = cp_parser_for (parser, ivdep, unroll);
12700
12701 /* Look for the `)'. */
12702 parens.require_close (parser);
12703
12704 /* Parse the body of the for-statement. */
12705 parser->in_statement = IN_ITERATION_STMT;
12706 bool prev = note_iteration_stmt_body_start ();
12707 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12708 note_iteration_stmt_body_end (prev);
12709 parser->in_statement = in_statement;
12710
12711 /* We're done with the for-statement. */
12712 finish_for_stmt (statement);
12713 }
12714 break;
12715
12716 default:
12717 cp_parser_error (parser, "expected iteration-statement");
12718 statement = error_mark_node;
12719 break;
12720 }
12721
12722 return statement;
12723 }
12724
12725 /* Parse a init-statement or the declarator of a range-based-for.
12726 Returns true if a range-based-for declaration is seen.
12727
12728 init-statement:
12729 expression-statement
12730 simple-declaration */
12731
12732 static bool
12733 cp_parser_init_statement (cp_parser *parser, tree *decl)
12734 {
12735 /* If the next token is a `;', then we have an empty
12736 expression-statement. Grammatically, this is also a
12737 simple-declaration, but an invalid one, because it does not
12738 declare anything. Therefore, if we did not handle this case
12739 specially, we would issue an error message about an invalid
12740 declaration. */
12741 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12742 {
12743 bool is_range_for = false;
12744 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12745
12746 /* Try to parse the init-statement. */
12747 if (cp_parser_range_based_for_with_init_p (parser))
12748 {
12749 tree dummy;
12750 cp_parser_parse_tentatively (parser);
12751 /* Parse the declaration. */
12752 cp_parser_simple_declaration (parser,
12753 /*function_definition_allowed_p=*/false,
12754 &dummy);
12755 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12756 if (!cp_parser_parse_definitely (parser))
12757 /* That didn't work, try to parse it as an expression-statement. */
12758 cp_parser_expression_statement (parser, NULL_TREE);
12759
12760 if (cxx_dialect < cxx2a)
12761 {
12762 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12763 "range-based %<for%> loops with initializer only "
12764 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
12765 *decl = error_mark_node;
12766 }
12767 }
12768
12769 /* A colon is used in range-based for. */
12770 parser->colon_corrects_to_scope_p = false;
12771
12772 /* We're going to speculatively look for a declaration, falling back
12773 to an expression, if necessary. */
12774 cp_parser_parse_tentatively (parser);
12775 /* Parse the declaration. */
12776 cp_parser_simple_declaration (parser,
12777 /*function_definition_allowed_p=*/false,
12778 decl);
12779 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12780 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12781 {
12782 /* It is a range-for, consume the ':'. */
12783 cp_lexer_consume_token (parser->lexer);
12784 is_range_for = true;
12785 if (cxx_dialect < cxx11)
12786 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12787 "range-based %<for%> loops only available with "
12788 "%<-std=c++11%> or %<-std=gnu++11%>");
12789 }
12790 else
12791 /* The ';' is not consumed yet because we told
12792 cp_parser_simple_declaration not to. */
12793 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12794
12795 if (cp_parser_parse_definitely (parser))
12796 return is_range_for;
12797 /* If the tentative parse failed, then we shall need to look for an
12798 expression-statement. */
12799 }
12800 /* If we are here, it is an expression-statement. */
12801 cp_parser_expression_statement (parser, NULL_TREE);
12802 return false;
12803 }
12804
12805 /* Parse a jump-statement.
12806
12807 jump-statement:
12808 break ;
12809 continue ;
12810 return expression [opt] ;
12811 return braced-init-list ;
12812 goto identifier ;
12813
12814 GNU extension:
12815
12816 jump-statement:
12817 goto * expression ;
12818
12819 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12820
12821 static tree
12822 cp_parser_jump_statement (cp_parser* parser)
12823 {
12824 tree statement = error_mark_node;
12825 cp_token *token;
12826 enum rid keyword;
12827 unsigned char in_statement;
12828
12829 /* Peek at the next token. */
12830 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12831 if (!token)
12832 return error_mark_node;
12833
12834 /* See what kind of keyword it is. */
12835 keyword = token->keyword;
12836 switch (keyword)
12837 {
12838 case RID_BREAK:
12839 in_statement = parser->in_statement & ~IN_IF_STMT;
12840 switch (in_statement)
12841 {
12842 case 0:
12843 error_at (token->location, "break statement not within loop or switch");
12844 break;
12845 default:
12846 gcc_assert ((in_statement & IN_SWITCH_STMT)
12847 || in_statement == IN_ITERATION_STMT);
12848 statement = finish_break_stmt ();
12849 if (in_statement == IN_ITERATION_STMT)
12850 break_maybe_infinite_loop ();
12851 break;
12852 case IN_OMP_BLOCK:
12853 error_at (token->location, "invalid exit from OpenMP structured block");
12854 break;
12855 case IN_OMP_FOR:
12856 error_at (token->location, "break statement used with OpenMP for loop");
12857 break;
12858 }
12859 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12860 break;
12861
12862 case RID_CONTINUE:
12863 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12864 {
12865 case 0:
12866 error_at (token->location, "continue statement not within a loop");
12867 break;
12868 /* Fall through. */
12869 case IN_ITERATION_STMT:
12870 case IN_OMP_FOR:
12871 statement = finish_continue_stmt ();
12872 break;
12873 case IN_OMP_BLOCK:
12874 error_at (token->location, "invalid exit from OpenMP structured block");
12875 break;
12876 default:
12877 gcc_unreachable ();
12878 }
12879 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12880 break;
12881
12882 case RID_RETURN:
12883 {
12884 tree expr;
12885 bool expr_non_constant_p;
12886
12887 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12888 {
12889 cp_lexer_set_source_position (parser->lexer);
12890 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12891 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12892 }
12893 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12894 expr = cp_parser_expression (parser);
12895 else
12896 /* If the next token is a `;', then there is no
12897 expression. */
12898 expr = NULL_TREE;
12899 /* Build the return-statement. */
12900 if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
12901 /* Don't deduce from a discarded return statement. */;
12902 else
12903 statement = finish_return_stmt (expr);
12904 /* Look for the final `;'. */
12905 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12906 }
12907 break;
12908
12909 case RID_GOTO:
12910 if (parser->in_function_body
12911 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12912 {
12913 error ("%<goto%> in %<constexpr%> function");
12914 cp_function_chain->invalid_constexpr = true;
12915 }
12916
12917 /* Create the goto-statement. */
12918 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12919 {
12920 /* Issue a warning about this use of a GNU extension. */
12921 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12922 /* Consume the '*' token. */
12923 cp_lexer_consume_token (parser->lexer);
12924 /* Parse the dependent expression. */
12925 finish_goto_stmt (cp_parser_expression (parser));
12926 }
12927 else
12928 finish_goto_stmt (cp_parser_identifier (parser));
12929 /* Look for the final `;'. */
12930 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12931 break;
12932
12933 default:
12934 cp_parser_error (parser, "expected jump-statement");
12935 break;
12936 }
12937
12938 return statement;
12939 }
12940
12941 /* Parse a declaration-statement.
12942
12943 declaration-statement:
12944 block-declaration */
12945
12946 static void
12947 cp_parser_declaration_statement (cp_parser* parser)
12948 {
12949 void *p;
12950
12951 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12952 p = obstack_alloc (&declarator_obstack, 0);
12953
12954 /* Parse the block-declaration. */
12955 cp_parser_block_declaration (parser, /*statement_p=*/true);
12956
12957 /* Free any declarators allocated. */
12958 obstack_free (&declarator_obstack, p);
12959 }
12960
12961 /* Some dependent statements (like `if (cond) statement'), are
12962 implicitly in their own scope. In other words, if the statement is
12963 a single statement (as opposed to a compound-statement), it is
12964 none-the-less treated as if it were enclosed in braces. Any
12965 declarations appearing in the dependent statement are out of scope
12966 after control passes that point. This function parses a statement,
12967 but ensures that is in its own scope, even if it is not a
12968 compound-statement.
12969
12970 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12971 is a (possibly labeled) if statement which is not enclosed in
12972 braces and has an else clause. This is used to implement
12973 -Wparentheses.
12974
12975 CHAIN is a vector of if-else-if conditions. This is used to implement
12976 -Wduplicated-cond.
12977
12978 Returns the new statement. */
12979
12980 static tree
12981 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12982 const token_indent_info &guard_tinfo,
12983 vec<tree> *chain)
12984 {
12985 tree statement;
12986 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12987 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12988 token_indent_info body_tinfo
12989 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12990
12991 if (if_p != NULL)
12992 *if_p = false;
12993
12994 /* Mark if () ; with a special NOP_EXPR. */
12995 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12996 {
12997 cp_lexer_consume_token (parser->lexer);
12998 statement = add_stmt (build_empty_stmt (body_loc));
12999
13000 if (guard_tinfo.keyword == RID_IF
13001 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
13002 warning_at (body_loc, OPT_Wempty_body,
13003 "suggest braces around empty body in an %<if%> statement");
13004 else if (guard_tinfo.keyword == RID_ELSE)
13005 warning_at (body_loc, OPT_Wempty_body,
13006 "suggest braces around empty body in an %<else%> statement");
13007 }
13008 /* if a compound is opened, we simply parse the statement directly. */
13009 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13010 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
13011 /* If the token is not a `{', then we must take special action. */
13012 else
13013 {
13014 /* Create a compound-statement. */
13015 statement = begin_compound_stmt (0);
13016 /* Parse the dependent-statement. */
13017 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
13018 &body_loc_after_labels);
13019 /* Finish the dummy compound-statement. */
13020 finish_compound_stmt (statement);
13021 }
13022
13023 token_indent_info next_tinfo
13024 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13025 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13026
13027 if (body_loc_after_labels != UNKNOWN_LOCATION
13028 && next_tinfo.type != CPP_SEMICOLON)
13029 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
13030 guard_tinfo.location, guard_tinfo.keyword);
13031
13032 /* Return the statement. */
13033 return statement;
13034 }
13035
13036 /* For some dependent statements (like `while (cond) statement'), we
13037 have already created a scope. Therefore, even if the dependent
13038 statement is a compound-statement, we do not want to create another
13039 scope. */
13040
13041 static void
13042 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
13043 const token_indent_info &guard_tinfo)
13044 {
13045 /* If the token is a `{', then we must take special action. */
13046 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13047 {
13048 token_indent_info body_tinfo
13049 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13050 location_t loc_after_labels = UNKNOWN_LOCATION;
13051
13052 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
13053 &loc_after_labels);
13054 token_indent_info next_tinfo
13055 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13056 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13057
13058 if (loc_after_labels != UNKNOWN_LOCATION
13059 && next_tinfo.type != CPP_SEMICOLON)
13060 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13061 guard_tinfo.location,
13062 guard_tinfo.keyword);
13063 }
13064 else
13065 {
13066 /* Avoid calling cp_parser_compound_statement, so that we
13067 don't create a new scope. Do everything else by hand. */
13068 matching_braces braces;
13069 braces.require_open (parser);
13070 /* If the next keyword is `__label__' we have a label declaration. */
13071 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13072 cp_parser_label_declaration (parser);
13073 /* Parse an (optional) statement-seq. */
13074 cp_parser_statement_seq_opt (parser, NULL_TREE);
13075 braces.require_close (parser);
13076 }
13077 }
13078
13079 /* Declarations [gram.dcl.dcl] */
13080
13081 /* Parse an optional declaration-sequence.
13082
13083 declaration-seq:
13084 declaration
13085 declaration-seq declaration */
13086
13087 static void
13088 cp_parser_declaration_seq_opt (cp_parser* parser)
13089 {
13090 while (true)
13091 {
13092 cp_token *token = cp_lexer_peek_token (parser->lexer);
13093
13094 if (token->type == CPP_CLOSE_BRACE
13095 || token->type == CPP_EOF)
13096 break;
13097 else
13098 cp_parser_toplevel_declaration (parser);
13099 }
13100 }
13101
13102 /* Parse a declaration.
13103
13104 declaration:
13105 block-declaration
13106 function-definition
13107 template-declaration
13108 explicit-instantiation
13109 explicit-specialization
13110 linkage-specification
13111 namespace-definition
13112
13113 C++17:
13114 deduction-guide
13115
13116 GNU extension:
13117
13118 declaration:
13119 __extension__ declaration */
13120
13121 static void
13122 cp_parser_declaration (cp_parser* parser)
13123 {
13124 cp_token token1;
13125 cp_token token2;
13126 int saved_pedantic;
13127 void *p;
13128 tree attributes = NULL_TREE;
13129
13130 /* Check for the `__extension__' keyword. */
13131 if (cp_parser_extension_opt (parser, &saved_pedantic))
13132 {
13133 /* Parse the qualified declaration. */
13134 cp_parser_declaration (parser);
13135 /* Restore the PEDANTIC flag. */
13136 pedantic = saved_pedantic;
13137
13138 return;
13139 }
13140
13141 /* Try to figure out what kind of declaration is present. */
13142 token1 = *cp_lexer_peek_token (parser->lexer);
13143
13144 if (token1.type != CPP_EOF)
13145 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13146 else
13147 {
13148 token2.type = CPP_EOF;
13149 token2.keyword = RID_MAX;
13150 }
13151
13152 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13153 p = obstack_alloc (&declarator_obstack, 0);
13154
13155 /* If the next token is `extern' and the following token is a string
13156 literal, then we have a linkage specification. */
13157 if (token1.keyword == RID_EXTERN
13158 && cp_parser_is_pure_string_literal (&token2))
13159 cp_parser_linkage_specification (parser);
13160 /* If the next token is `template', then we have either a template
13161 declaration, an explicit instantiation, or an explicit
13162 specialization. */
13163 else if (token1.keyword == RID_TEMPLATE)
13164 {
13165 /* `template <>' indicates a template specialization. */
13166 if (token2.type == CPP_LESS
13167 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13168 cp_parser_explicit_specialization (parser);
13169 /* `template <' indicates a template declaration. */
13170 else if (token2.type == CPP_LESS)
13171 cp_parser_template_declaration (parser, /*member_p=*/false);
13172 /* Anything else must be an explicit instantiation. */
13173 else
13174 cp_parser_explicit_instantiation (parser);
13175 }
13176 /* If the next token is `export', then we have a template
13177 declaration. */
13178 else if (token1.keyword == RID_EXPORT)
13179 cp_parser_template_declaration (parser, /*member_p=*/false);
13180 /* If the next token is `extern', 'static' or 'inline' and the one
13181 after that is `template', we have a GNU extended explicit
13182 instantiation directive. */
13183 else if (cp_parser_allow_gnu_extensions_p (parser)
13184 && (token1.keyword == RID_EXTERN
13185 || token1.keyword == RID_STATIC
13186 || token1.keyword == RID_INLINE)
13187 && token2.keyword == RID_TEMPLATE)
13188 cp_parser_explicit_instantiation (parser);
13189 /* If the next token is `namespace', check for a named or unnamed
13190 namespace definition. */
13191 else if (token1.keyword == RID_NAMESPACE
13192 && (/* A named namespace definition. */
13193 (token2.type == CPP_NAME
13194 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13195 != CPP_EQ))
13196 || (token2.type == CPP_OPEN_SQUARE
13197 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13198 == CPP_OPEN_SQUARE)
13199 /* An unnamed namespace definition. */
13200 || token2.type == CPP_OPEN_BRACE
13201 || token2.keyword == RID_ATTRIBUTE))
13202 cp_parser_namespace_definition (parser);
13203 /* An inline (associated) namespace definition. */
13204 else if (token1.keyword == RID_INLINE
13205 && token2.keyword == RID_NAMESPACE)
13206 cp_parser_namespace_definition (parser);
13207 /* Objective-C++ declaration/definition. */
13208 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13209 cp_parser_objc_declaration (parser, NULL_TREE);
13210 else if (c_dialect_objc ()
13211 && token1.keyword == RID_ATTRIBUTE
13212 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13213 cp_parser_objc_declaration (parser, attributes);
13214 /* At this point we may have a template declared by a concept
13215 introduction. */
13216 else if (flag_concepts
13217 && cp_parser_template_declaration_after_export (parser,
13218 /*member_p=*/false))
13219 /* We did. */;
13220 else
13221 /* Try to parse a block-declaration, or a function-definition. */
13222 cp_parser_block_declaration (parser, /*statement_p=*/false);
13223
13224 /* Free any declarators allocated. */
13225 obstack_free (&declarator_obstack, p);
13226 }
13227
13228 /* Parse a namespace-scope declaration. */
13229
13230 static void
13231 cp_parser_toplevel_declaration (cp_parser* parser)
13232 {
13233 cp_token *token = cp_lexer_peek_token (parser->lexer);
13234
13235 if (token->type == CPP_PRAGMA)
13236 /* A top-level declaration can consist solely of a #pragma. A
13237 nested declaration cannot, so this is done here and not in
13238 cp_parser_declaration. (A #pragma at block scope is
13239 handled in cp_parser_statement.) */
13240 cp_parser_pragma (parser, pragma_external, NULL);
13241 else if (token->type == CPP_SEMICOLON)
13242 {
13243 /* A declaration consisting of a single semicolon is
13244 invalid. Allow it unless we're being pedantic. */
13245 cp_lexer_consume_token (parser->lexer);
13246 if (!in_system_header_at (input_location))
13247 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13248 }
13249 else
13250 /* Parse the declaration itself. */
13251 cp_parser_declaration (parser);
13252 }
13253
13254 /* Parse a block-declaration.
13255
13256 block-declaration:
13257 simple-declaration
13258 asm-definition
13259 namespace-alias-definition
13260 using-declaration
13261 using-directive
13262
13263 GNU Extension:
13264
13265 block-declaration:
13266 __extension__ block-declaration
13267
13268 C++0x Extension:
13269
13270 block-declaration:
13271 static_assert-declaration
13272
13273 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13274 part of a declaration-statement. */
13275
13276 static void
13277 cp_parser_block_declaration (cp_parser *parser,
13278 bool statement_p)
13279 {
13280 cp_token *token1;
13281 int saved_pedantic;
13282
13283 /* Check for the `__extension__' keyword. */
13284 if (cp_parser_extension_opt (parser, &saved_pedantic))
13285 {
13286 /* Parse the qualified declaration. */
13287 cp_parser_block_declaration (parser, statement_p);
13288 /* Restore the PEDANTIC flag. */
13289 pedantic = saved_pedantic;
13290
13291 return;
13292 }
13293
13294 /* Peek at the next token to figure out which kind of declaration is
13295 present. */
13296 token1 = cp_lexer_peek_token (parser->lexer);
13297
13298 /* If the next keyword is `asm', we have an asm-definition. */
13299 if (token1->keyword == RID_ASM)
13300 {
13301 if (statement_p)
13302 cp_parser_commit_to_tentative_parse (parser);
13303 cp_parser_asm_definition (parser);
13304 }
13305 /* If the next keyword is `namespace', we have a
13306 namespace-alias-definition. */
13307 else if (token1->keyword == RID_NAMESPACE)
13308 cp_parser_namespace_alias_definition (parser);
13309 /* If the next keyword is `using', we have a
13310 using-declaration, a using-directive, or an alias-declaration. */
13311 else if (token1->keyword == RID_USING)
13312 {
13313 cp_token *token2;
13314
13315 if (statement_p)
13316 cp_parser_commit_to_tentative_parse (parser);
13317 /* If the token after `using' is `namespace', then we have a
13318 using-directive. */
13319 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13320 if (token2->keyword == RID_NAMESPACE)
13321 cp_parser_using_directive (parser);
13322 /* If the second token after 'using' is '=', then we have an
13323 alias-declaration. */
13324 else if (cxx_dialect >= cxx11
13325 && token2->type == CPP_NAME
13326 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13327 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13328 cp_parser_alias_declaration (parser);
13329 /* Otherwise, it's a using-declaration. */
13330 else
13331 cp_parser_using_declaration (parser,
13332 /*access_declaration_p=*/false);
13333 }
13334 /* If the next keyword is `__label__' we have a misplaced label
13335 declaration. */
13336 else if (token1->keyword == RID_LABEL)
13337 {
13338 cp_lexer_consume_token (parser->lexer);
13339 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13340 cp_parser_skip_to_end_of_statement (parser);
13341 /* If the next token is now a `;', consume it. */
13342 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13343 cp_lexer_consume_token (parser->lexer);
13344 }
13345 /* If the next token is `static_assert' we have a static assertion. */
13346 else if (token1->keyword == RID_STATIC_ASSERT)
13347 cp_parser_static_assert (parser, /*member_p=*/false);
13348 /* Anything else must be a simple-declaration. */
13349 else
13350 cp_parser_simple_declaration (parser, !statement_p,
13351 /*maybe_range_for_decl*/NULL);
13352 }
13353
13354 /* Parse a simple-declaration.
13355
13356 simple-declaration:
13357 decl-specifier-seq [opt] init-declarator-list [opt] ;
13358 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13359 brace-or-equal-initializer ;
13360
13361 init-declarator-list:
13362 init-declarator
13363 init-declarator-list , init-declarator
13364
13365 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13366 function-definition as a simple-declaration.
13367
13368 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13369 parsed declaration if it is an uninitialized single declarator not followed
13370 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13371 if present, will not be consumed. */
13372
13373 static void
13374 cp_parser_simple_declaration (cp_parser* parser,
13375 bool function_definition_allowed_p,
13376 tree *maybe_range_for_decl)
13377 {
13378 cp_decl_specifier_seq decl_specifiers;
13379 int declares_class_or_enum;
13380 bool saw_declarator;
13381 location_t comma_loc = UNKNOWN_LOCATION;
13382 location_t init_loc = UNKNOWN_LOCATION;
13383
13384 if (maybe_range_for_decl)
13385 *maybe_range_for_decl = NULL_TREE;
13386
13387 /* Defer access checks until we know what is being declared; the
13388 checks for names appearing in the decl-specifier-seq should be
13389 done as if we were in the scope of the thing being declared. */
13390 push_deferring_access_checks (dk_deferred);
13391
13392 /* Parse the decl-specifier-seq. We have to keep track of whether
13393 or not the decl-specifier-seq declares a named class or
13394 enumeration type, since that is the only case in which the
13395 init-declarator-list is allowed to be empty.
13396
13397 [dcl.dcl]
13398
13399 In a simple-declaration, the optional init-declarator-list can be
13400 omitted only when declaring a class or enumeration, that is when
13401 the decl-specifier-seq contains either a class-specifier, an
13402 elaborated-type-specifier, or an enum-specifier. */
13403 cp_parser_decl_specifier_seq (parser,
13404 CP_PARSER_FLAGS_OPTIONAL,
13405 &decl_specifiers,
13406 &declares_class_or_enum);
13407 /* We no longer need to defer access checks. */
13408 stop_deferring_access_checks ();
13409
13410 /* In a block scope, a valid declaration must always have a
13411 decl-specifier-seq. By not trying to parse declarators, we can
13412 resolve the declaration/expression ambiguity more quickly. */
13413 if (!function_definition_allowed_p
13414 && !decl_specifiers.any_specifiers_p)
13415 {
13416 cp_parser_error (parser, "expected declaration");
13417 goto done;
13418 }
13419
13420 /* If the next two tokens are both identifiers, the code is
13421 erroneous. The usual cause of this situation is code like:
13422
13423 T t;
13424
13425 where "T" should name a type -- but does not. */
13426 if (!decl_specifiers.any_type_specifiers_p
13427 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13428 {
13429 /* If parsing tentatively, we should commit; we really are
13430 looking at a declaration. */
13431 cp_parser_commit_to_tentative_parse (parser);
13432 /* Give up. */
13433 goto done;
13434 }
13435
13436 cp_parser_maybe_commit_to_declaration (parser,
13437 decl_specifiers.any_specifiers_p);
13438
13439 /* Look for C++17 decomposition declaration. */
13440 for (size_t n = 1; ; n++)
13441 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13442 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13443 continue;
13444 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13445 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13446 && decl_specifiers.any_specifiers_p)
13447 {
13448 tree decl
13449 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13450 maybe_range_for_decl,
13451 &init_loc);
13452
13453 /* The next token should be either a `,' or a `;'. */
13454 cp_token *token = cp_lexer_peek_token (parser->lexer);
13455 /* If it's a `;', we are done. */
13456 if (token->type == CPP_SEMICOLON)
13457 goto finish;
13458 else if (maybe_range_for_decl)
13459 {
13460 if (*maybe_range_for_decl == NULL_TREE)
13461 *maybe_range_for_decl = error_mark_node;
13462 goto finish;
13463 }
13464 /* Anything else is an error. */
13465 else
13466 {
13467 /* If we have already issued an error message we don't need
13468 to issue another one. */
13469 if ((decl != error_mark_node
13470 && DECL_INITIAL (decl) != error_mark_node)
13471 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13472 cp_parser_error (parser, "expected %<,%> or %<;%>");
13473 /* Skip tokens until we reach the end of the statement. */
13474 cp_parser_skip_to_end_of_statement (parser);
13475 /* If the next token is now a `;', consume it. */
13476 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13477 cp_lexer_consume_token (parser->lexer);
13478 goto done;
13479 }
13480 }
13481 else
13482 break;
13483
13484 tree last_type;
13485 bool auto_specifier_p;
13486 /* NULL_TREE if both variable and function declaration are allowed,
13487 error_mark_node if function declaration are not allowed and
13488 a FUNCTION_DECL that should be diagnosed if it is followed by
13489 variable declarations. */
13490 tree auto_function_declaration;
13491
13492 last_type = NULL_TREE;
13493 auto_specifier_p
13494 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13495 auto_function_declaration = NULL_TREE;
13496
13497 /* Keep going until we hit the `;' at the end of the simple
13498 declaration. */
13499 saw_declarator = false;
13500 while (cp_lexer_next_token_is_not (parser->lexer,
13501 CPP_SEMICOLON))
13502 {
13503 cp_token *token;
13504 bool function_definition_p;
13505 tree decl;
13506 tree auto_result = NULL_TREE;
13507
13508 if (saw_declarator)
13509 {
13510 /* If we are processing next declarator, comma is expected */
13511 token = cp_lexer_peek_token (parser->lexer);
13512 gcc_assert (token->type == CPP_COMMA);
13513 cp_lexer_consume_token (parser->lexer);
13514 if (maybe_range_for_decl)
13515 {
13516 *maybe_range_for_decl = error_mark_node;
13517 if (comma_loc == UNKNOWN_LOCATION)
13518 comma_loc = token->location;
13519 }
13520 }
13521 else
13522 saw_declarator = true;
13523
13524 /* Parse the init-declarator. */
13525 decl = cp_parser_init_declarator (parser,
13526 CP_PARSER_FLAGS_NONE,
13527 &decl_specifiers,
13528 /*checks=*/NULL,
13529 function_definition_allowed_p,
13530 /*member_p=*/false,
13531 declares_class_or_enum,
13532 &function_definition_p,
13533 maybe_range_for_decl,
13534 &init_loc,
13535 &auto_result);
13536 /* If an error occurred while parsing tentatively, exit quickly.
13537 (That usually happens when in the body of a function; each
13538 statement is treated as a declaration-statement until proven
13539 otherwise.) */
13540 if (cp_parser_error_occurred (parser))
13541 goto done;
13542
13543 if (auto_specifier_p && cxx_dialect >= cxx14)
13544 {
13545 /* If the init-declarator-list contains more than one
13546 init-declarator, they shall all form declarations of
13547 variables. */
13548 if (auto_function_declaration == NULL_TREE)
13549 auto_function_declaration
13550 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13551 else if (TREE_CODE (decl) == FUNCTION_DECL
13552 || auto_function_declaration != error_mark_node)
13553 {
13554 error_at (decl_specifiers.locations[ds_type_spec],
13555 "non-variable %qD in declaration with more than one "
13556 "declarator with placeholder type",
13557 TREE_CODE (decl) == FUNCTION_DECL
13558 ? decl : auto_function_declaration);
13559 auto_function_declaration = error_mark_node;
13560 }
13561 }
13562
13563 if (auto_result
13564 && (!processing_template_decl || !type_uses_auto (auto_result)))
13565 {
13566 if (last_type
13567 && last_type != error_mark_node
13568 && !same_type_p (auto_result, last_type))
13569 {
13570 /* If the list of declarators contains more than one declarator,
13571 the type of each declared variable is determined as described
13572 above. If the type deduced for the template parameter U is not
13573 the same in each deduction, the program is ill-formed. */
13574 error_at (decl_specifiers.locations[ds_type_spec],
13575 "inconsistent deduction for %qT: %qT and then %qT",
13576 decl_specifiers.type, last_type, auto_result);
13577 last_type = error_mark_node;
13578 }
13579 else
13580 last_type = auto_result;
13581 }
13582
13583 /* Handle function definitions specially. */
13584 if (function_definition_p)
13585 {
13586 /* If the next token is a `,', then we are probably
13587 processing something like:
13588
13589 void f() {}, *p;
13590
13591 which is erroneous. */
13592 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13593 {
13594 cp_token *token = cp_lexer_peek_token (parser->lexer);
13595 error_at (token->location,
13596 "mixing"
13597 " declarations and function-definitions is forbidden");
13598 }
13599 /* Otherwise, we're done with the list of declarators. */
13600 else
13601 {
13602 pop_deferring_access_checks ();
13603 return;
13604 }
13605 }
13606 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13607 *maybe_range_for_decl = decl;
13608 /* The next token should be either a `,' or a `;'. */
13609 token = cp_lexer_peek_token (parser->lexer);
13610 /* If it's a `,', there are more declarators to come. */
13611 if (token->type == CPP_COMMA)
13612 /* will be consumed next time around */;
13613 /* If it's a `;', we are done. */
13614 else if (token->type == CPP_SEMICOLON)
13615 break;
13616 else if (maybe_range_for_decl)
13617 {
13618 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13619 permerror (decl_specifiers.locations[ds_type_spec],
13620 "types may not be defined in a for-range-declaration");
13621 break;
13622 }
13623 /* Anything else is an error. */
13624 else
13625 {
13626 /* If we have already issued an error message we don't need
13627 to issue another one. */
13628 if ((decl != error_mark_node
13629 && DECL_INITIAL (decl) != error_mark_node)
13630 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13631 cp_parser_error (parser, "expected %<,%> or %<;%>");
13632 /* Skip tokens until we reach the end of the statement. */
13633 cp_parser_skip_to_end_of_statement (parser);
13634 /* If the next token is now a `;', consume it. */
13635 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13636 cp_lexer_consume_token (parser->lexer);
13637 goto done;
13638 }
13639 /* After the first time around, a function-definition is not
13640 allowed -- even if it was OK at first. For example:
13641
13642 int i, f() {}
13643
13644 is not valid. */
13645 function_definition_allowed_p = false;
13646 }
13647
13648 /* Issue an error message if no declarators are present, and the
13649 decl-specifier-seq does not itself declare a class or
13650 enumeration: [dcl.dcl]/3. */
13651 if (!saw_declarator)
13652 {
13653 if (cp_parser_declares_only_class_p (parser))
13654 {
13655 if (!declares_class_or_enum
13656 && decl_specifiers.type
13657 && OVERLOAD_TYPE_P (decl_specifiers.type))
13658 /* Ensure an error is issued anyway when finish_decltype_type,
13659 called via cp_parser_decl_specifier_seq, returns a class or
13660 an enumeration (c++/51786). */
13661 decl_specifiers.type = NULL_TREE;
13662 shadow_tag (&decl_specifiers);
13663 }
13664 /* Perform any deferred access checks. */
13665 perform_deferred_access_checks (tf_warning_or_error);
13666 }
13667
13668 /* Consume the `;'. */
13669 finish:
13670 if (!maybe_range_for_decl)
13671 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13672 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13673 {
13674 if (init_loc != UNKNOWN_LOCATION)
13675 error_at (init_loc, "initializer in range-based %<for%> loop");
13676 if (comma_loc != UNKNOWN_LOCATION)
13677 error_at (comma_loc,
13678 "multiple declarations in range-based %<for%> loop");
13679 }
13680
13681 done:
13682 pop_deferring_access_checks ();
13683 }
13684
13685 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13686 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13687 initializer ; */
13688
13689 static tree
13690 cp_parser_decomposition_declaration (cp_parser *parser,
13691 cp_decl_specifier_seq *decl_specifiers,
13692 tree *maybe_range_for_decl,
13693 location_t *init_loc)
13694 {
13695 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13696 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13697 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13698
13699 /* Parse the identifier-list. */
13700 auto_vec<cp_expr, 10> v;
13701 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13702 while (true)
13703 {
13704 cp_expr e = cp_parser_identifier (parser);
13705 if (e.get_value () == error_mark_node)
13706 break;
13707 v.safe_push (e);
13708 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13709 break;
13710 cp_lexer_consume_token (parser->lexer);
13711 }
13712
13713 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13714 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13715 {
13716 end_loc = UNKNOWN_LOCATION;
13717 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13718 false);
13719 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13720 cp_lexer_consume_token (parser->lexer);
13721 else
13722 {
13723 cp_parser_skip_to_end_of_statement (parser);
13724 return error_mark_node;
13725 }
13726 }
13727
13728 if (cxx_dialect < cxx17)
13729 pedwarn (loc, 0, "structured bindings only available with "
13730 "%<-std=c++17%> or %<-std=gnu++17%>");
13731
13732 tree pushed_scope;
13733 cp_declarator *declarator = make_declarator (cdk_decomp);
13734 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13735 declarator->id_loc = loc;
13736 if (ref_qual != REF_QUAL_NONE)
13737 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13738 ref_qual == REF_QUAL_RVALUE,
13739 NULL_TREE);
13740 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13741 NULL_TREE, decl_specifiers->attributes,
13742 &pushed_scope);
13743 tree orig_decl = decl;
13744
13745 unsigned int i;
13746 cp_expr e;
13747 cp_decl_specifier_seq decl_specs;
13748 clear_decl_specs (&decl_specs);
13749 decl_specs.type = make_auto ();
13750 tree prev = decl;
13751 FOR_EACH_VEC_ELT (v, i, e)
13752 {
13753 if (i == 0)
13754 declarator = make_id_declarator (NULL_TREE, e.get_value (),
13755 sfk_none, e.get_location ());
13756 else
13757 {
13758 declarator->u.id.unqualified_name = e.get_value ();
13759 declarator->id_loc = e.get_location ();
13760 }
13761 tree elt_pushed_scope;
13762 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13763 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13764 if (decl2 == error_mark_node)
13765 decl = error_mark_node;
13766 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13767 {
13768 /* Ensure we've diagnosed redeclaration if we aren't creating
13769 a new VAR_DECL. */
13770 gcc_assert (errorcount);
13771 decl = error_mark_node;
13772 }
13773 else
13774 prev = decl2;
13775 if (elt_pushed_scope)
13776 pop_scope (elt_pushed_scope);
13777 }
13778
13779 if (v.is_empty ())
13780 {
13781 error_at (loc, "empty structured binding declaration");
13782 decl = error_mark_node;
13783 }
13784
13785 if (maybe_range_for_decl == NULL
13786 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13787 {
13788 bool non_constant_p = false, is_direct_init = false;
13789 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13790 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13791 &non_constant_p);
13792 if (initializer == NULL_TREE
13793 || (TREE_CODE (initializer) == TREE_LIST
13794 && TREE_CHAIN (initializer))
13795 || (is_direct_init
13796 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13797 && CONSTRUCTOR_NELTS (initializer) != 1))
13798 {
13799 error_at (loc, "invalid initializer for structured binding "
13800 "declaration");
13801 initializer = error_mark_node;
13802 }
13803
13804 if (decl != error_mark_node)
13805 {
13806 cp_maybe_mangle_decomp (decl, prev, v.length ());
13807 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13808 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13809 cp_finish_decomp (decl, prev, v.length ());
13810 }
13811 }
13812 else if (decl != error_mark_node)
13813 {
13814 *maybe_range_for_decl = prev;
13815 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13816 the underlying DECL. */
13817 cp_finish_decomp (decl, prev, v.length ());
13818 }
13819
13820 if (pushed_scope)
13821 pop_scope (pushed_scope);
13822
13823 if (decl == error_mark_node && DECL_P (orig_decl))
13824 {
13825 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13826 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13827 }
13828
13829 return decl;
13830 }
13831
13832 /* Parse a decl-specifier-seq.
13833
13834 decl-specifier-seq:
13835 decl-specifier-seq [opt] decl-specifier
13836 decl-specifier attribute-specifier-seq [opt] (C++11)
13837
13838 decl-specifier:
13839 storage-class-specifier
13840 type-specifier
13841 function-specifier
13842 friend
13843 typedef
13844
13845 GNU Extension:
13846
13847 decl-specifier:
13848 attributes
13849
13850 Concepts Extension:
13851
13852 decl-specifier:
13853 concept
13854
13855 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13856
13857 The parser flags FLAGS is used to control type-specifier parsing.
13858
13859 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13860 flags:
13861
13862 1: one of the decl-specifiers is an elaborated-type-specifier
13863 (i.e., a type declaration)
13864 2: one of the decl-specifiers is an enum-specifier or a
13865 class-specifier (i.e., a type definition)
13866
13867 */
13868
13869 static void
13870 cp_parser_decl_specifier_seq (cp_parser* parser,
13871 cp_parser_flags flags,
13872 cp_decl_specifier_seq *decl_specs,
13873 int* declares_class_or_enum)
13874 {
13875 bool constructor_possible_p = !parser->in_declarator_p;
13876 bool found_decl_spec = false;
13877 cp_token *start_token = NULL;
13878 cp_decl_spec ds;
13879
13880 /* Clear DECL_SPECS. */
13881 clear_decl_specs (decl_specs);
13882
13883 /* Assume no class or enumeration type is declared. */
13884 *declares_class_or_enum = 0;
13885
13886 /* Keep reading specifiers until there are no more to read. */
13887 while (true)
13888 {
13889 bool constructor_p;
13890 cp_token *token;
13891 ds = ds_last;
13892
13893 /* Peek at the next token. */
13894 token = cp_lexer_peek_token (parser->lexer);
13895
13896 /* Save the first token of the decl spec list for error
13897 reporting. */
13898 if (!start_token)
13899 start_token = token;
13900 /* Handle attributes. */
13901 if (cp_next_tokens_can_be_attribute_p (parser))
13902 {
13903 /* Parse the attributes. */
13904 tree attrs = cp_parser_attributes_opt (parser);
13905
13906 /* In a sequence of declaration specifiers, c++11 attributes
13907 appertain to the type that precede them. In that case
13908 [dcl.spec]/1 says:
13909
13910 The attribute-specifier-seq affects the type only for
13911 the declaration it appears in, not other declarations
13912 involving the same type.
13913
13914 But for now let's force the user to position the
13915 attribute either at the beginning of the declaration or
13916 after the declarator-id, which would clearly mean that it
13917 applies to the declarator. */
13918 if (cxx11_attribute_p (attrs))
13919 {
13920 if (!found_decl_spec)
13921 /* The c++11 attribute is at the beginning of the
13922 declaration. It appertains to the entity being
13923 declared. */;
13924 else
13925 {
13926 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13927 {
13928 /* This is an attribute following a
13929 class-specifier. */
13930 if (decl_specs->type_definition_p)
13931 warn_misplaced_attr_for_class_type (token->location,
13932 decl_specs->type);
13933 attrs = NULL_TREE;
13934 }
13935 else
13936 {
13937 decl_specs->std_attributes
13938 = attr_chainon (decl_specs->std_attributes, attrs);
13939 if (decl_specs->locations[ds_std_attribute] == 0)
13940 decl_specs->locations[ds_std_attribute] = token->location;
13941 }
13942 continue;
13943 }
13944 }
13945
13946 decl_specs->attributes
13947 = attr_chainon (decl_specs->attributes, attrs);
13948 if (decl_specs->locations[ds_attribute] == 0)
13949 decl_specs->locations[ds_attribute] = token->location;
13950 continue;
13951 }
13952 /* Assume we will find a decl-specifier keyword. */
13953 found_decl_spec = true;
13954 /* If the next token is an appropriate keyword, we can simply
13955 add it to the list. */
13956 switch (token->keyword)
13957 {
13958 /* decl-specifier:
13959 friend
13960 constexpr */
13961 case RID_FRIEND:
13962 if (!at_class_scope_p ())
13963 {
13964 gcc_rich_location richloc (token->location);
13965 richloc.add_fixit_remove ();
13966 error_at (&richloc, "%<friend%> used outside of class");
13967 cp_lexer_purge_token (parser->lexer);
13968 }
13969 else
13970 {
13971 ds = ds_friend;
13972 /* Consume the token. */
13973 cp_lexer_consume_token (parser->lexer);
13974 }
13975 break;
13976
13977 case RID_CONSTEXPR:
13978 ds = ds_constexpr;
13979 cp_lexer_consume_token (parser->lexer);
13980 break;
13981
13982 case RID_CONCEPT:
13983 ds = ds_concept;
13984 cp_lexer_consume_token (parser->lexer);
13985 /* In C++20 a concept definition is just 'concept name = expr;'
13986 Support that syntax by pretending we've seen 'bool'. */
13987 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13988 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
13989 {
13990 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
13991 token, /*type_definition*/false);
13992 decl_specs->any_type_specifiers_p = true;
13993 }
13994 break;
13995
13996 /* function-specifier:
13997 inline
13998 virtual
13999 explicit */
14000 case RID_INLINE:
14001 case RID_VIRTUAL:
14002 case RID_EXPLICIT:
14003 cp_parser_function_specifier_opt (parser, decl_specs);
14004 break;
14005
14006 /* decl-specifier:
14007 typedef */
14008 case RID_TYPEDEF:
14009 ds = ds_typedef;
14010 /* Consume the token. */
14011 cp_lexer_consume_token (parser->lexer);
14012 /* A constructor declarator cannot appear in a typedef. */
14013 constructor_possible_p = false;
14014 /* The "typedef" keyword can only occur in a declaration; we
14015 may as well commit at this point. */
14016 cp_parser_commit_to_tentative_parse (parser);
14017
14018 if (decl_specs->storage_class != sc_none)
14019 decl_specs->conflicting_specifiers_p = true;
14020 break;
14021
14022 /* storage-class-specifier:
14023 auto
14024 register
14025 static
14026 extern
14027 mutable
14028
14029 GNU Extension:
14030 thread */
14031 case RID_AUTO:
14032 if (cxx_dialect == cxx98)
14033 {
14034 /* Consume the token. */
14035 cp_lexer_consume_token (parser->lexer);
14036
14037 /* Complain about `auto' as a storage specifier, if
14038 we're complaining about C++0x compatibility. */
14039 gcc_rich_location richloc (token->location);
14040 richloc.add_fixit_remove ();
14041 warning_at (&richloc, OPT_Wc__11_compat,
14042 "%<auto%> changes meaning in C++11; "
14043 "please remove it");
14044
14045 /* Set the storage class anyway. */
14046 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
14047 token);
14048 }
14049 else
14050 /* C++0x auto type-specifier. */
14051 found_decl_spec = false;
14052 break;
14053
14054 case RID_REGISTER:
14055 case RID_STATIC:
14056 case RID_EXTERN:
14057 case RID_MUTABLE:
14058 /* Consume the token. */
14059 cp_lexer_consume_token (parser->lexer);
14060 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
14061 token);
14062 break;
14063 case RID_THREAD:
14064 /* Consume the token. */
14065 ds = ds_thread;
14066 cp_lexer_consume_token (parser->lexer);
14067 break;
14068
14069 default:
14070 /* We did not yet find a decl-specifier yet. */
14071 found_decl_spec = false;
14072 break;
14073 }
14074
14075 if (found_decl_spec
14076 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14077 && token->keyword != RID_CONSTEXPR)
14078 error ("decl-specifier invalid in condition");
14079
14080 if (found_decl_spec
14081 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14082 && token->keyword != RID_MUTABLE
14083 && token->keyword != RID_CONSTEXPR)
14084 error_at (token->location, "%qD invalid in lambda",
14085 ridpointers[token->keyword]);
14086
14087 if (ds != ds_last)
14088 set_and_check_decl_spec_loc (decl_specs, ds, token);
14089
14090 /* Constructors are a special case. The `S' in `S()' is not a
14091 decl-specifier; it is the beginning of the declarator. */
14092 constructor_p
14093 = (!found_decl_spec
14094 && constructor_possible_p
14095 && (cp_parser_constructor_declarator_p
14096 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
14097 ds_friend))));
14098
14099 /* If we don't have a DECL_SPEC yet, then we must be looking at
14100 a type-specifier. */
14101 if (!found_decl_spec && !constructor_p)
14102 {
14103 int decl_spec_declares_class_or_enum;
14104 bool is_cv_qualifier;
14105 tree type_spec;
14106
14107 type_spec
14108 = cp_parser_type_specifier (parser, flags,
14109 decl_specs,
14110 /*is_declaration=*/true,
14111 &decl_spec_declares_class_or_enum,
14112 &is_cv_qualifier);
14113 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14114
14115 /* If this type-specifier referenced a user-defined type
14116 (a typedef, class-name, etc.), then we can't allow any
14117 more such type-specifiers henceforth.
14118
14119 [dcl.spec]
14120
14121 The longest sequence of decl-specifiers that could
14122 possibly be a type name is taken as the
14123 decl-specifier-seq of a declaration. The sequence shall
14124 be self-consistent as described below.
14125
14126 [dcl.type]
14127
14128 As a general rule, at most one type-specifier is allowed
14129 in the complete decl-specifier-seq of a declaration. The
14130 only exceptions are the following:
14131
14132 -- const or volatile can be combined with any other
14133 type-specifier.
14134
14135 -- signed or unsigned can be combined with char, long,
14136 short, or int.
14137
14138 -- ..
14139
14140 Example:
14141
14142 typedef char* Pc;
14143 void g (const int Pc);
14144
14145 Here, Pc is *not* part of the decl-specifier seq; it's
14146 the declarator. Therefore, once we see a type-specifier
14147 (other than a cv-qualifier), we forbid any additional
14148 user-defined types. We *do* still allow things like `int
14149 int' to be considered a decl-specifier-seq, and issue the
14150 error message later. */
14151 if (type_spec && !is_cv_qualifier)
14152 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14153 /* A constructor declarator cannot follow a type-specifier. */
14154 if (type_spec)
14155 {
14156 constructor_possible_p = false;
14157 found_decl_spec = true;
14158 if (!is_cv_qualifier)
14159 decl_specs->any_type_specifiers_p = true;
14160
14161 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14162 error_at (token->location, "type-specifier invalid in lambda");
14163 }
14164 }
14165
14166 /* If we still do not have a DECL_SPEC, then there are no more
14167 decl-specifiers. */
14168 if (!found_decl_spec)
14169 break;
14170
14171 decl_specs->any_specifiers_p = true;
14172 /* After we see one decl-specifier, further decl-specifiers are
14173 always optional. */
14174 flags |= CP_PARSER_FLAGS_OPTIONAL;
14175 }
14176
14177 /* Don't allow a friend specifier with a class definition. */
14178 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14179 && (*declares_class_or_enum & 2))
14180 error_at (decl_specs->locations[ds_friend],
14181 "class definition may not be declared a friend");
14182 }
14183
14184 /* Parse an (optional) storage-class-specifier.
14185
14186 storage-class-specifier:
14187 auto
14188 register
14189 static
14190 extern
14191 mutable
14192
14193 GNU Extension:
14194
14195 storage-class-specifier:
14196 thread
14197
14198 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14199
14200 static tree
14201 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14202 {
14203 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14204 {
14205 case RID_AUTO:
14206 if (cxx_dialect != cxx98)
14207 return NULL_TREE;
14208 /* Fall through for C++98. */
14209 gcc_fallthrough ();
14210
14211 case RID_REGISTER:
14212 case RID_STATIC:
14213 case RID_EXTERN:
14214 case RID_MUTABLE:
14215 case RID_THREAD:
14216 /* Consume the token. */
14217 return cp_lexer_consume_token (parser->lexer)->u.value;
14218
14219 default:
14220 return NULL_TREE;
14221 }
14222 }
14223
14224 /* Parse an (optional) function-specifier.
14225
14226 function-specifier:
14227 inline
14228 virtual
14229 explicit
14230
14231 C++2A Extension:
14232 explicit(constant-expression)
14233
14234 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14235 Updates DECL_SPECS, if it is non-NULL. */
14236
14237 static tree
14238 cp_parser_function_specifier_opt (cp_parser* parser,
14239 cp_decl_specifier_seq *decl_specs)
14240 {
14241 cp_token *token = cp_lexer_peek_token (parser->lexer);
14242 switch (token->keyword)
14243 {
14244 case RID_INLINE:
14245 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14246 break;
14247
14248 case RID_VIRTUAL:
14249 /* 14.5.2.3 [temp.mem]
14250
14251 A member function template shall not be virtual. */
14252 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14253 && current_class_type)
14254 error_at (token->location, "templates may not be %<virtual%>");
14255 else
14256 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14257 break;
14258
14259 case RID_EXPLICIT:
14260 {
14261 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14262 /* If we see '(', it's C++20 explicit(bool). */
14263 tree expr;
14264 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14265 {
14266 matching_parens parens;
14267 parens.consume_open (parser);
14268
14269 /* New types are not allowed in an explicit-specifier. */
14270 const char *saved_message
14271 = parser->type_definition_forbidden_message;
14272 parser->type_definition_forbidden_message
14273 = G_("types may not be defined in explicit-specifier");
14274
14275 if (cxx_dialect < cxx2a)
14276 pedwarn (token->location, 0,
14277 "%<explicit(bool)%> only available with %<-std=c++2a%> "
14278 "or %<-std=gnu++2a%>");
14279
14280 /* Parse the constant-expression. */
14281 expr = cp_parser_constant_expression (parser);
14282
14283 /* Restore the saved message. */
14284 parser->type_definition_forbidden_message = saved_message;
14285 parens.require_close (parser);
14286 }
14287 else
14288 /* The explicit-specifier explicit without a constant-expression is
14289 equivalent to the explicit-specifier explicit(true). */
14290 expr = boolean_true_node;
14291
14292 /* [dcl.fct.spec]
14293 "the constant-expression, if supplied, shall be a contextually
14294 converted constant expression of type bool." */
14295 expr = build_explicit_specifier (expr, tf_warning_or_error);
14296 /* We could evaluate it -- mark the decl as appropriate. */
14297 if (expr == boolean_true_node)
14298 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14299 else if (expr == boolean_false_node)
14300 /* Don't mark the decl as explicit. */;
14301 else if (decl_specs)
14302 /* The expression was value-dependent. Remember it so that we can
14303 substitute it later. */
14304 decl_specs->explicit_specifier = expr;
14305 return id;
14306 }
14307
14308 default:
14309 return NULL_TREE;
14310 }
14311
14312 /* Consume the token. */
14313 return cp_lexer_consume_token (parser->lexer)->u.value;
14314 }
14315
14316 /* Parse a linkage-specification.
14317
14318 linkage-specification:
14319 extern string-literal { declaration-seq [opt] }
14320 extern string-literal declaration */
14321
14322 static void
14323 cp_parser_linkage_specification (cp_parser* parser)
14324 {
14325 tree linkage;
14326
14327 /* Look for the `extern' keyword. */
14328 cp_token *extern_token
14329 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14330
14331 /* Look for the string-literal. */
14332 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14333 linkage = cp_parser_string_literal (parser, false, false);
14334
14335 /* Transform the literal into an identifier. If the literal is a
14336 wide-character string, or contains embedded NULs, then we can't
14337 handle it as the user wants. */
14338 if (strlen (TREE_STRING_POINTER (linkage))
14339 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14340 {
14341 cp_parser_error (parser, "invalid linkage-specification");
14342 /* Assume C++ linkage. */
14343 linkage = lang_name_cplusplus;
14344 }
14345 else
14346 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14347
14348 /* We're now using the new linkage. */
14349 push_lang_context (linkage);
14350
14351 /* Preserve the location of the the innermost linkage specification,
14352 tracking the locations of nested specifications via a local. */
14353 location_t saved_location
14354 = parser->innermost_linkage_specification_location;
14355 /* Construct a location ranging from the start of the "extern" to
14356 the end of the string-literal, with the caret at the start, e.g.:
14357 extern "C" {
14358 ^~~~~~~~~~
14359 */
14360 parser->innermost_linkage_specification_location
14361 = make_location (extern_token->location,
14362 extern_token->location,
14363 get_finish (string_token->location));
14364
14365 /* If the next token is a `{', then we're using the first
14366 production. */
14367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14368 {
14369 cp_ensure_no_omp_declare_simd (parser);
14370 cp_ensure_no_oacc_routine (parser);
14371
14372 /* Consume the `{' token. */
14373 matching_braces braces;
14374 braces.consume_open (parser);
14375 /* Parse the declarations. */
14376 cp_parser_declaration_seq_opt (parser);
14377 /* Look for the closing `}'. */
14378 braces.require_close (parser);
14379 }
14380 /* Otherwise, there's just one declaration. */
14381 else
14382 {
14383 bool saved_in_unbraced_linkage_specification_p;
14384
14385 saved_in_unbraced_linkage_specification_p
14386 = parser->in_unbraced_linkage_specification_p;
14387 parser->in_unbraced_linkage_specification_p = true;
14388 cp_parser_declaration (parser);
14389 parser->in_unbraced_linkage_specification_p
14390 = saved_in_unbraced_linkage_specification_p;
14391 }
14392
14393 /* We're done with the linkage-specification. */
14394 pop_lang_context ();
14395
14396 /* Restore location of parent linkage specification, if any. */
14397 parser->innermost_linkage_specification_location = saved_location;
14398 }
14399
14400 /* Parse a static_assert-declaration.
14401
14402 static_assert-declaration:
14403 static_assert ( constant-expression , string-literal ) ;
14404 static_assert ( constant-expression ) ; (C++17)
14405
14406 If MEMBER_P, this static_assert is a class member. */
14407
14408 static void
14409 cp_parser_static_assert(cp_parser *parser, bool member_p)
14410 {
14411 cp_expr condition;
14412 location_t token_loc;
14413 tree message;
14414 bool dummy;
14415
14416 /* Peek at the `static_assert' token so we can keep track of exactly
14417 where the static assertion started. */
14418 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14419
14420 /* Look for the `static_assert' keyword. */
14421 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14422 RT_STATIC_ASSERT))
14423 return;
14424
14425 /* We know we are in a static assertion; commit to any tentative
14426 parse. */
14427 if (cp_parser_parsing_tentatively (parser))
14428 cp_parser_commit_to_tentative_parse (parser);
14429
14430 /* Parse the `(' starting the static assertion condition. */
14431 matching_parens parens;
14432 parens.require_open (parser);
14433
14434 /* Parse the constant-expression. Allow a non-constant expression
14435 here in order to give better diagnostics in finish_static_assert. */
14436 condition =
14437 cp_parser_constant_expression (parser,
14438 /*allow_non_constant_p=*/true,
14439 /*non_constant_p=*/&dummy);
14440
14441 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14442 {
14443 if (cxx_dialect < cxx17)
14444 pedwarn (input_location, OPT_Wpedantic,
14445 "%<static_assert%> without a message "
14446 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14447 /* Eat the ')' */
14448 cp_lexer_consume_token (parser->lexer);
14449 message = build_string (1, "");
14450 TREE_TYPE (message) = char_array_type_node;
14451 fix_string_type (message);
14452 }
14453 else
14454 {
14455 /* Parse the separating `,'. */
14456 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14457
14458 /* Parse the string-literal message. */
14459 message = cp_parser_string_literal (parser,
14460 /*translate=*/false,
14461 /*wide_ok=*/true);
14462
14463 /* A `)' completes the static assertion. */
14464 if (!parens.require_close (parser))
14465 cp_parser_skip_to_closing_parenthesis (parser,
14466 /*recovering=*/true,
14467 /*or_comma=*/false,
14468 /*consume_paren=*/true);
14469 }
14470
14471 /* A semicolon terminates the declaration. */
14472 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14473
14474 /* Get the location for the static assertion. Use that of the
14475 condition if available, otherwise, use that of the "static_assert"
14476 token. */
14477 location_t assert_loc = condition.get_location ();
14478 if (assert_loc == UNKNOWN_LOCATION)
14479 assert_loc = token_loc;
14480
14481 /* Complete the static assertion, which may mean either processing
14482 the static assert now or saving it for template instantiation. */
14483 finish_static_assert (condition, message, assert_loc, member_p);
14484 }
14485
14486 /* Parse the expression in decltype ( expression ). */
14487
14488 static tree
14489 cp_parser_decltype_expr (cp_parser *parser,
14490 bool &id_expression_or_member_access_p)
14491 {
14492 cp_token *id_expr_start_token;
14493 tree expr;
14494
14495 /* Since we're going to preserve any side-effects from this parse, set up a
14496 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14497 in the expression. */
14498 tentative_firewall firewall (parser);
14499
14500 /* First, try parsing an id-expression. */
14501 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14502 cp_parser_parse_tentatively (parser);
14503 expr = cp_parser_id_expression (parser,
14504 /*template_keyword_p=*/false,
14505 /*check_dependency_p=*/true,
14506 /*template_p=*/NULL,
14507 /*declarator_p=*/false,
14508 /*optional_p=*/false);
14509
14510 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14511 {
14512 bool non_integral_constant_expression_p = false;
14513 tree id_expression = expr;
14514 cp_id_kind idk;
14515 const char *error_msg;
14516
14517 if (identifier_p (expr))
14518 /* Lookup the name we got back from the id-expression. */
14519 expr = cp_parser_lookup_name_simple (parser, expr,
14520 id_expr_start_token->location);
14521
14522 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14523 /* A template without args is not a complete id-expression. */
14524 expr = error_mark_node;
14525
14526 if (expr
14527 && expr != error_mark_node
14528 && TREE_CODE (expr) != TYPE_DECL
14529 && (TREE_CODE (expr) != BIT_NOT_EXPR
14530 || !TYPE_P (TREE_OPERAND (expr, 0)))
14531 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14532 {
14533 /* Complete lookup of the id-expression. */
14534 expr = (finish_id_expression
14535 (id_expression, expr, parser->scope, &idk,
14536 /*integral_constant_expression_p=*/false,
14537 /*allow_non_integral_constant_expression_p=*/true,
14538 &non_integral_constant_expression_p,
14539 /*template_p=*/false,
14540 /*done=*/true,
14541 /*address_p=*/false,
14542 /*template_arg_p=*/false,
14543 &error_msg,
14544 id_expr_start_token->location));
14545
14546 if (expr == error_mark_node)
14547 /* We found an id-expression, but it was something that we
14548 should not have found. This is an error, not something
14549 we can recover from, so note that we found an
14550 id-expression and we'll recover as gracefully as
14551 possible. */
14552 id_expression_or_member_access_p = true;
14553 }
14554
14555 if (expr
14556 && expr != error_mark_node
14557 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14558 /* We have an id-expression. */
14559 id_expression_or_member_access_p = true;
14560 }
14561
14562 if (!id_expression_or_member_access_p)
14563 {
14564 /* Abort the id-expression parse. */
14565 cp_parser_abort_tentative_parse (parser);
14566
14567 /* Parsing tentatively, again. */
14568 cp_parser_parse_tentatively (parser);
14569
14570 /* Parse a class member access. */
14571 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14572 /*cast_p=*/false, /*decltype*/true,
14573 /*member_access_only_p=*/true, NULL);
14574
14575 if (expr
14576 && expr != error_mark_node
14577 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14578 /* We have an id-expression. */
14579 id_expression_or_member_access_p = true;
14580 }
14581
14582 if (id_expression_or_member_access_p)
14583 /* We have parsed the complete id-expression or member access. */
14584 cp_parser_parse_definitely (parser);
14585 else
14586 {
14587 /* Abort our attempt to parse an id-expression or member access
14588 expression. */
14589 cp_parser_abort_tentative_parse (parser);
14590
14591 /* Commit to the tentative_firewall so we get syntax errors. */
14592 cp_parser_commit_to_tentative_parse (parser);
14593
14594 /* Parse a full expression. */
14595 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14596 /*decltype_p=*/true);
14597 }
14598
14599 return expr;
14600 }
14601
14602 /* Parse a `decltype' type. Returns the type.
14603
14604 simple-type-specifier:
14605 decltype ( expression )
14606 C++14 proposal:
14607 decltype ( auto ) */
14608
14609 static tree
14610 cp_parser_decltype (cp_parser *parser)
14611 {
14612 bool id_expression_or_member_access_p = false;
14613 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14614
14615 if (start_token->type == CPP_DECLTYPE)
14616 {
14617 /* Already parsed. */
14618 cp_lexer_consume_token (parser->lexer);
14619 return saved_checks_value (start_token->u.tree_check_value);
14620 }
14621
14622 /* Look for the `decltype' token. */
14623 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14624 return error_mark_node;
14625
14626 /* Parse the opening `('. */
14627 matching_parens parens;
14628 if (!parens.require_open (parser))
14629 return error_mark_node;
14630
14631 push_deferring_access_checks (dk_deferred);
14632
14633 tree expr = NULL_TREE;
14634
14635 if (cxx_dialect >= cxx14
14636 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14637 /* decltype (auto) */
14638 cp_lexer_consume_token (parser->lexer);
14639 else
14640 {
14641 /* decltype (expression) */
14642
14643 /* Types cannot be defined in a `decltype' expression. Save away the
14644 old message and set the new one. */
14645 const char *saved_message = parser->type_definition_forbidden_message;
14646 parser->type_definition_forbidden_message
14647 = G_("types may not be defined in %<decltype%> expressions");
14648
14649 /* The restrictions on constant-expressions do not apply inside
14650 decltype expressions. */
14651 bool saved_integral_constant_expression_p
14652 = parser->integral_constant_expression_p;
14653 bool saved_non_integral_constant_expression_p
14654 = parser->non_integral_constant_expression_p;
14655 parser->integral_constant_expression_p = false;
14656
14657 /* Within a parenthesized expression, a `>' token is always
14658 the greater-than operator. */
14659 bool saved_greater_than_is_operator_p
14660 = parser->greater_than_is_operator_p;
14661 parser->greater_than_is_operator_p = true;
14662
14663 /* Do not actually evaluate the expression. */
14664 ++cp_unevaluated_operand;
14665
14666 /* Do not warn about problems with the expression. */
14667 ++c_inhibit_evaluation_warnings;
14668
14669 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14670 STRIP_ANY_LOCATION_WRAPPER (expr);
14671
14672 /* Go back to evaluating expressions. */
14673 --cp_unevaluated_operand;
14674 --c_inhibit_evaluation_warnings;
14675
14676 /* The `>' token might be the end of a template-id or
14677 template-parameter-list now. */
14678 parser->greater_than_is_operator_p
14679 = saved_greater_than_is_operator_p;
14680
14681 /* Restore the old message and the integral constant expression
14682 flags. */
14683 parser->type_definition_forbidden_message = saved_message;
14684 parser->integral_constant_expression_p
14685 = saved_integral_constant_expression_p;
14686 parser->non_integral_constant_expression_p
14687 = saved_non_integral_constant_expression_p;
14688 }
14689
14690 /* Parse to the closing `)'. */
14691 if (!parens.require_close (parser))
14692 {
14693 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14694 /*consume_paren=*/true);
14695 pop_deferring_access_checks ();
14696 return error_mark_node;
14697 }
14698
14699 if (!expr)
14700 {
14701 /* Build auto. */
14702 expr = make_decltype_auto ();
14703 AUTO_IS_DECLTYPE (expr) = true;
14704 }
14705 else
14706 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14707 tf_warning_or_error);
14708
14709 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14710 it again. */
14711 start_token->type = CPP_DECLTYPE;
14712 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14713 start_token->u.tree_check_value->value = expr;
14714 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14715 start_token->keyword = RID_MAX;
14716 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14717
14718 pop_to_parent_deferring_access_checks ();
14719
14720 return expr;
14721 }
14722
14723 /* Special member functions [gram.special] */
14724
14725 /* Parse a conversion-function-id.
14726
14727 conversion-function-id:
14728 operator conversion-type-id
14729
14730 Returns an IDENTIFIER_NODE representing the operator. */
14731
14732 static tree
14733 cp_parser_conversion_function_id (cp_parser* parser)
14734 {
14735 tree type;
14736 tree saved_scope;
14737 tree saved_qualifying_scope;
14738 tree saved_object_scope;
14739 tree pushed_scope = NULL_TREE;
14740
14741 /* Look for the `operator' token. */
14742 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14743 return error_mark_node;
14744 /* When we parse the conversion-type-id, the current scope will be
14745 reset. However, we need that information in able to look up the
14746 conversion function later, so we save it here. */
14747 saved_scope = parser->scope;
14748 saved_qualifying_scope = parser->qualifying_scope;
14749 saved_object_scope = parser->object_scope;
14750 /* We must enter the scope of the class so that the names of
14751 entities declared within the class are available in the
14752 conversion-type-id. For example, consider:
14753
14754 struct S {
14755 typedef int I;
14756 operator I();
14757 };
14758
14759 S::operator I() { ... }
14760
14761 In order to see that `I' is a type-name in the definition, we
14762 must be in the scope of `S'. */
14763 if (saved_scope)
14764 pushed_scope = push_scope (saved_scope);
14765 /* Parse the conversion-type-id. */
14766 type = cp_parser_conversion_type_id (parser);
14767 /* Leave the scope of the class, if any. */
14768 if (pushed_scope)
14769 pop_scope (pushed_scope);
14770 /* Restore the saved scope. */
14771 parser->scope = saved_scope;
14772 parser->qualifying_scope = saved_qualifying_scope;
14773 parser->object_scope = saved_object_scope;
14774 /* If the TYPE is invalid, indicate failure. */
14775 if (type == error_mark_node)
14776 return error_mark_node;
14777 return make_conv_op_name (type);
14778 }
14779
14780 /* Parse a conversion-type-id:
14781
14782 conversion-type-id:
14783 type-specifier-seq conversion-declarator [opt]
14784
14785 Returns the TYPE specified. */
14786
14787 static tree
14788 cp_parser_conversion_type_id (cp_parser* parser)
14789 {
14790 tree attributes;
14791 cp_decl_specifier_seq type_specifiers;
14792 cp_declarator *declarator;
14793 tree type_specified;
14794 const char *saved_message;
14795
14796 /* Parse the attributes. */
14797 attributes = cp_parser_attributes_opt (parser);
14798
14799 saved_message = parser->type_definition_forbidden_message;
14800 parser->type_definition_forbidden_message
14801 = G_("types may not be defined in a conversion-type-id");
14802
14803 /* Parse the type-specifiers. */
14804 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
14805 /*is_declaration=*/false,
14806 /*is_trailing_return=*/false,
14807 &type_specifiers);
14808
14809 parser->type_definition_forbidden_message = saved_message;
14810
14811 /* If that didn't work, stop. */
14812 if (type_specifiers.type == error_mark_node)
14813 return error_mark_node;
14814 /* Parse the conversion-declarator. */
14815 declarator = cp_parser_conversion_declarator_opt (parser);
14816
14817 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14818 /*initialized=*/0, &attributes);
14819 if (attributes)
14820 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14821
14822 /* Don't give this error when parsing tentatively. This happens to
14823 work because we always parse this definitively once. */
14824 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14825 && type_uses_auto (type_specified))
14826 {
14827 if (cxx_dialect < cxx14)
14828 {
14829 error ("invalid use of %<auto%> in conversion operator");
14830 return error_mark_node;
14831 }
14832 else if (template_parm_scope_p ())
14833 warning (0, "use of %<auto%> in member template "
14834 "conversion operator can never be deduced");
14835 }
14836
14837 return type_specified;
14838 }
14839
14840 /* Parse an (optional) conversion-declarator.
14841
14842 conversion-declarator:
14843 ptr-operator conversion-declarator [opt]
14844
14845 */
14846
14847 static cp_declarator *
14848 cp_parser_conversion_declarator_opt (cp_parser* parser)
14849 {
14850 enum tree_code code;
14851 tree class_type, std_attributes = NULL_TREE;
14852 cp_cv_quals cv_quals;
14853
14854 /* We don't know if there's a ptr-operator next, or not. */
14855 cp_parser_parse_tentatively (parser);
14856 /* Try the ptr-operator. */
14857 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14858 &std_attributes);
14859 /* If it worked, look for more conversion-declarators. */
14860 if (cp_parser_parse_definitely (parser))
14861 {
14862 cp_declarator *declarator;
14863
14864 /* Parse another optional declarator. */
14865 declarator = cp_parser_conversion_declarator_opt (parser);
14866
14867 declarator = cp_parser_make_indirect_declarator
14868 (code, class_type, cv_quals, declarator, std_attributes);
14869
14870 return declarator;
14871 }
14872
14873 return NULL;
14874 }
14875
14876 /* Parse an (optional) ctor-initializer.
14877
14878 ctor-initializer:
14879 : mem-initializer-list */
14880
14881 static void
14882 cp_parser_ctor_initializer_opt (cp_parser* parser)
14883 {
14884 /* If the next token is not a `:', then there is no
14885 ctor-initializer. */
14886 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14887 {
14888 /* Do default initialization of any bases and members. */
14889 if (DECL_CONSTRUCTOR_P (current_function_decl))
14890 finish_mem_initializers (NULL_TREE);
14891 return;
14892 }
14893
14894 /* Consume the `:' token. */
14895 cp_lexer_consume_token (parser->lexer);
14896 /* And the mem-initializer-list. */
14897 cp_parser_mem_initializer_list (parser);
14898 }
14899
14900 /* Parse a mem-initializer-list.
14901
14902 mem-initializer-list:
14903 mem-initializer ... [opt]
14904 mem-initializer ... [opt] , mem-initializer-list */
14905
14906 static void
14907 cp_parser_mem_initializer_list (cp_parser* parser)
14908 {
14909 tree mem_initializer_list = NULL_TREE;
14910 tree target_ctor = error_mark_node;
14911 cp_token *token = cp_lexer_peek_token (parser->lexer);
14912
14913 /* Let the semantic analysis code know that we are starting the
14914 mem-initializer-list. */
14915 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14916 error_at (token->location,
14917 "only constructors take member initializers");
14918
14919 /* Loop through the list. */
14920 while (true)
14921 {
14922 tree mem_initializer;
14923
14924 token = cp_lexer_peek_token (parser->lexer);
14925 /* Parse the mem-initializer. */
14926 mem_initializer = cp_parser_mem_initializer (parser);
14927 /* If the next token is a `...', we're expanding member initializers. */
14928 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14929 if (ellipsis
14930 || (mem_initializer != error_mark_node
14931 && check_for_bare_parameter_packs (TREE_PURPOSE
14932 (mem_initializer))))
14933 {
14934 /* Consume the `...'. */
14935 if (ellipsis)
14936 cp_lexer_consume_token (parser->lexer);
14937
14938 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14939 can be expanded but members cannot. */
14940 if (mem_initializer != error_mark_node
14941 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14942 {
14943 error_at (token->location,
14944 "cannot expand initializer for member %qD",
14945 TREE_PURPOSE (mem_initializer));
14946 mem_initializer = error_mark_node;
14947 }
14948
14949 /* Construct the pack expansion type. */
14950 if (mem_initializer != error_mark_node)
14951 mem_initializer = make_pack_expansion (mem_initializer);
14952 }
14953 if (target_ctor != error_mark_node
14954 && mem_initializer != error_mark_node)
14955 {
14956 error ("mem-initializer for %qD follows constructor delegation",
14957 TREE_PURPOSE (mem_initializer));
14958 mem_initializer = error_mark_node;
14959 }
14960 /* Look for a target constructor. */
14961 if (mem_initializer != error_mark_node
14962 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14963 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14964 {
14965 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14966 if (mem_initializer_list)
14967 {
14968 error ("constructor delegation follows mem-initializer for %qD",
14969 TREE_PURPOSE (mem_initializer_list));
14970 mem_initializer = error_mark_node;
14971 }
14972 target_ctor = mem_initializer;
14973 }
14974 /* Add it to the list, unless it was erroneous. */
14975 if (mem_initializer != error_mark_node)
14976 {
14977 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14978 mem_initializer_list = mem_initializer;
14979 }
14980 /* If the next token is not a `,', we're done. */
14981 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14982 break;
14983 /* Consume the `,' token. */
14984 cp_lexer_consume_token (parser->lexer);
14985 }
14986
14987 /* Perform semantic analysis. */
14988 if (DECL_CONSTRUCTOR_P (current_function_decl))
14989 finish_mem_initializers (mem_initializer_list);
14990 }
14991
14992 /* Parse a mem-initializer.
14993
14994 mem-initializer:
14995 mem-initializer-id ( expression-list [opt] )
14996 mem-initializer-id braced-init-list
14997
14998 GNU extension:
14999
15000 mem-initializer:
15001 ( expression-list [opt] )
15002
15003 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15004 class) or FIELD_DECL (for a non-static data member) to initialize;
15005 the TREE_VALUE is the expression-list. An empty initialization
15006 list is represented by void_list_node. */
15007
15008 static tree
15009 cp_parser_mem_initializer (cp_parser* parser)
15010 {
15011 tree mem_initializer_id;
15012 tree expression_list;
15013 tree member;
15014 cp_token *token = cp_lexer_peek_token (parser->lexer);
15015
15016 /* Find out what is being initialized. */
15017 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15018 {
15019 permerror (token->location,
15020 "anachronistic old-style base class initializer");
15021 mem_initializer_id = NULL_TREE;
15022 }
15023 else
15024 {
15025 mem_initializer_id = cp_parser_mem_initializer_id (parser);
15026 if (mem_initializer_id == error_mark_node)
15027 return mem_initializer_id;
15028 }
15029 member = expand_member_init (mem_initializer_id);
15030 if (member && !DECL_P (member))
15031 in_base_initializer = 1;
15032
15033 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15034 {
15035 bool expr_non_constant_p;
15036 cp_lexer_set_source_position (parser->lexer);
15037 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15038 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
15039 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
15040 expression_list = build_tree_list (NULL_TREE, expression_list);
15041 }
15042 else
15043 {
15044 vec<tree, va_gc> *vec;
15045 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15046 /*cast_p=*/false,
15047 /*allow_expansion_p=*/true,
15048 /*non_constant_p=*/NULL,
15049 /*close_paren_loc=*/NULL,
15050 /*wrap_locations_p=*/true);
15051 if (vec == NULL)
15052 return error_mark_node;
15053 expression_list = build_tree_list_vec (vec);
15054 release_tree_vector (vec);
15055 }
15056
15057 if (expression_list == error_mark_node)
15058 return error_mark_node;
15059 if (!expression_list)
15060 expression_list = void_type_node;
15061
15062 in_base_initializer = 0;
15063
15064 return member ? build_tree_list (member, expression_list) : error_mark_node;
15065 }
15066
15067 /* Parse a mem-initializer-id.
15068
15069 mem-initializer-id:
15070 :: [opt] nested-name-specifier [opt] class-name
15071 decltype-specifier (C++11)
15072 identifier
15073
15074 Returns a TYPE indicating the class to be initialized for the first
15075 production (and the second in C++11). Returns an IDENTIFIER_NODE
15076 indicating the data member to be initialized for the last production. */
15077
15078 static tree
15079 cp_parser_mem_initializer_id (cp_parser* parser)
15080 {
15081 bool global_scope_p;
15082 bool nested_name_specifier_p;
15083 bool template_p = false;
15084 tree id;
15085
15086 cp_token *token = cp_lexer_peek_token (parser->lexer);
15087
15088 /* `typename' is not allowed in this context ([temp.res]). */
15089 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15090 {
15091 error_at (token->location,
15092 "keyword %<typename%> not allowed in this context (a qualified "
15093 "member initializer is implicitly a type)");
15094 cp_lexer_consume_token (parser->lexer);
15095 }
15096 /* Look for the optional `::' operator. */
15097 global_scope_p
15098 = (cp_parser_global_scope_opt (parser,
15099 /*current_scope_valid_p=*/false)
15100 != NULL_TREE);
15101 /* Look for the optional nested-name-specifier. The simplest way to
15102 implement:
15103
15104 [temp.res]
15105
15106 The keyword `typename' is not permitted in a base-specifier or
15107 mem-initializer; in these contexts a qualified name that
15108 depends on a template-parameter is implicitly assumed to be a
15109 type name.
15110
15111 is to assume that we have seen the `typename' keyword at this
15112 point. */
15113 nested_name_specifier_p
15114 = (cp_parser_nested_name_specifier_opt (parser,
15115 /*typename_keyword_p=*/true,
15116 /*check_dependency_p=*/true,
15117 /*type_p=*/true,
15118 /*is_declaration=*/true)
15119 != NULL_TREE);
15120 if (nested_name_specifier_p)
15121 template_p = cp_parser_optional_template_keyword (parser);
15122 /* If there is a `::' operator or a nested-name-specifier, then we
15123 are definitely looking for a class-name. */
15124 if (global_scope_p || nested_name_specifier_p)
15125 return cp_parser_class_name (parser,
15126 /*typename_keyword_p=*/true,
15127 /*template_keyword_p=*/template_p,
15128 typename_type,
15129 /*check_dependency_p=*/true,
15130 /*class_head_p=*/false,
15131 /*is_declaration=*/true);
15132 /* Otherwise, we could also be looking for an ordinary identifier. */
15133 cp_parser_parse_tentatively (parser);
15134 if (cp_lexer_next_token_is_decltype (parser->lexer))
15135 /* Try a decltype-specifier. */
15136 id = cp_parser_decltype (parser);
15137 else
15138 /* Otherwise, try a class-name. */
15139 id = cp_parser_class_name (parser,
15140 /*typename_keyword_p=*/true,
15141 /*template_keyword_p=*/false,
15142 none_type,
15143 /*check_dependency_p=*/true,
15144 /*class_head_p=*/false,
15145 /*is_declaration=*/true);
15146 /* If we found one, we're done. */
15147 if (cp_parser_parse_definitely (parser))
15148 return id;
15149 /* Otherwise, look for an ordinary identifier. */
15150 return cp_parser_identifier (parser);
15151 }
15152
15153 /* Overloading [gram.over] */
15154
15155 /* Parse an operator-function-id.
15156
15157 operator-function-id:
15158 operator operator
15159
15160 Returns an IDENTIFIER_NODE for the operator which is a
15161 human-readable spelling of the identifier, e.g., `operator +'. */
15162
15163 static cp_expr
15164 cp_parser_operator_function_id (cp_parser* parser)
15165 {
15166 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15167 /* Look for the `operator' keyword. */
15168 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15169 return error_mark_node;
15170 /* And then the name of the operator itself. */
15171 return cp_parser_operator (parser, start_loc);
15172 }
15173
15174 /* Return an identifier node for a user-defined literal operator.
15175 The suffix identifier is chained to the operator name identifier. */
15176
15177 tree
15178 cp_literal_operator_id (const char* name)
15179 {
15180 tree identifier;
15181 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15182 + strlen (name) + 10);
15183 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15184 identifier = get_identifier (buffer);
15185
15186 return identifier;
15187 }
15188
15189 /* Parse an operator.
15190
15191 operator:
15192 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15193 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15194 || ++ -- , ->* -> () []
15195
15196 GNU Extensions:
15197
15198 operator:
15199 <? >? <?= >?=
15200
15201 Returns an IDENTIFIER_NODE for the operator which is a
15202 human-readable spelling of the identifier, e.g., `operator +'. */
15203
15204 static cp_expr
15205 cp_parser_operator (cp_parser* parser, location_t start_loc)
15206 {
15207 tree id = NULL_TREE;
15208 cp_token *token;
15209 bool utf8 = false;
15210
15211 /* Peek at the next token. */
15212 token = cp_lexer_peek_token (parser->lexer);
15213
15214 location_t end_loc = token->location;
15215
15216 /* Figure out which operator we have. */
15217 enum tree_code op = ERROR_MARK;
15218 bool assop = false;
15219 bool consumed = false;
15220 switch (token->type)
15221 {
15222 case CPP_KEYWORD:
15223 {
15224 /* The keyword should be either `new' or `delete'. */
15225 if (token->keyword == RID_NEW)
15226 op = NEW_EXPR;
15227 else if (token->keyword == RID_DELETE)
15228 op = DELETE_EXPR;
15229 else
15230 break;
15231
15232 /* Consume the `new' or `delete' token. */
15233 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15234
15235 /* Peek at the next token. */
15236 token = cp_lexer_peek_token (parser->lexer);
15237 /* If it's a `[' token then this is the array variant of the
15238 operator. */
15239 if (token->type == CPP_OPEN_SQUARE)
15240 {
15241 /* Consume the `[' token. */
15242 cp_lexer_consume_token (parser->lexer);
15243 /* Look for the `]' token. */
15244 if (cp_token *close_token
15245 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15246 end_loc = close_token->location;
15247 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15248 }
15249 consumed = true;
15250 break;
15251 }
15252
15253 case CPP_PLUS:
15254 op = PLUS_EXPR;
15255 break;
15256
15257 case CPP_MINUS:
15258 op = MINUS_EXPR;
15259 break;
15260
15261 case CPP_MULT:
15262 op = MULT_EXPR;
15263 break;
15264
15265 case CPP_DIV:
15266 op = TRUNC_DIV_EXPR;
15267 break;
15268
15269 case CPP_MOD:
15270 op = TRUNC_MOD_EXPR;
15271 break;
15272
15273 case CPP_XOR:
15274 op = BIT_XOR_EXPR;
15275 break;
15276
15277 case CPP_AND:
15278 op = BIT_AND_EXPR;
15279 break;
15280
15281 case CPP_OR:
15282 op = BIT_IOR_EXPR;
15283 break;
15284
15285 case CPP_COMPL:
15286 op = BIT_NOT_EXPR;
15287 break;
15288
15289 case CPP_NOT:
15290 op = TRUTH_NOT_EXPR;
15291 break;
15292
15293 case CPP_EQ:
15294 assop = true;
15295 op = NOP_EXPR;
15296 break;
15297
15298 case CPP_LESS:
15299 op = LT_EXPR;
15300 break;
15301
15302 case CPP_GREATER:
15303 op = GT_EXPR;
15304 break;
15305
15306 case CPP_PLUS_EQ:
15307 assop = true;
15308 op = PLUS_EXPR;
15309 break;
15310
15311 case CPP_MINUS_EQ:
15312 assop = true;
15313 op = MINUS_EXPR;
15314 break;
15315
15316 case CPP_MULT_EQ:
15317 assop = true;
15318 op = MULT_EXPR;
15319 break;
15320
15321 case CPP_DIV_EQ:
15322 assop = true;
15323 op = TRUNC_DIV_EXPR;
15324 break;
15325
15326 case CPP_MOD_EQ:
15327 assop = true;
15328 op = TRUNC_MOD_EXPR;
15329 break;
15330
15331 case CPP_XOR_EQ:
15332 assop = true;
15333 op = BIT_XOR_EXPR;
15334 break;
15335
15336 case CPP_AND_EQ:
15337 assop = true;
15338 op = BIT_AND_EXPR;
15339 break;
15340
15341 case CPP_OR_EQ:
15342 assop = true;
15343 op = BIT_IOR_EXPR;
15344 break;
15345
15346 case CPP_LSHIFT:
15347 op = LSHIFT_EXPR;
15348 break;
15349
15350 case CPP_RSHIFT:
15351 op = RSHIFT_EXPR;
15352 break;
15353
15354 case CPP_LSHIFT_EQ:
15355 assop = true;
15356 op = LSHIFT_EXPR;
15357 break;
15358
15359 case CPP_RSHIFT_EQ:
15360 assop = true;
15361 op = RSHIFT_EXPR;
15362 break;
15363
15364 case CPP_EQ_EQ:
15365 op = EQ_EXPR;
15366 break;
15367
15368 case CPP_NOT_EQ:
15369 op = NE_EXPR;
15370 break;
15371
15372 case CPP_LESS_EQ:
15373 op = LE_EXPR;
15374 break;
15375
15376 case CPP_GREATER_EQ:
15377 op = GE_EXPR;
15378 break;
15379
15380 case CPP_AND_AND:
15381 op = TRUTH_ANDIF_EXPR;
15382 break;
15383
15384 case CPP_OR_OR:
15385 op = TRUTH_ORIF_EXPR;
15386 break;
15387
15388 case CPP_PLUS_PLUS:
15389 op = POSTINCREMENT_EXPR;
15390 break;
15391
15392 case CPP_MINUS_MINUS:
15393 op = PREDECREMENT_EXPR;
15394 break;
15395
15396 case CPP_COMMA:
15397 op = COMPOUND_EXPR;
15398 break;
15399
15400 case CPP_DEREF_STAR:
15401 op = MEMBER_REF;
15402 break;
15403
15404 case CPP_DEREF:
15405 op = COMPONENT_REF;
15406 break;
15407
15408 case CPP_OPEN_PAREN:
15409 {
15410 /* Consume the `('. */
15411 matching_parens parens;
15412 parens.consume_open (parser);
15413 /* Look for the matching `)'. */
15414 token = parens.require_close (parser);
15415 if (token)
15416 end_loc = token->location;
15417 op = CALL_EXPR;
15418 consumed = true;
15419 break;
15420 }
15421
15422 case CPP_OPEN_SQUARE:
15423 /* Consume the `['. */
15424 cp_lexer_consume_token (parser->lexer);
15425 /* Look for the matching `]'. */
15426 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15427 if (token)
15428 end_loc = token->location;
15429 op = ARRAY_REF;
15430 consumed = true;
15431 break;
15432
15433 case CPP_UTF8STRING:
15434 case CPP_UTF8STRING_USERDEF:
15435 utf8 = true;
15436 /* FALLTHRU */
15437 case CPP_STRING:
15438 case CPP_WSTRING:
15439 case CPP_STRING16:
15440 case CPP_STRING32:
15441 case CPP_STRING_USERDEF:
15442 case CPP_WSTRING_USERDEF:
15443 case CPP_STRING16_USERDEF:
15444 case CPP_STRING32_USERDEF:
15445 {
15446 cp_expr str;
15447 tree string_tree;
15448 int sz, len;
15449
15450 if (cxx_dialect == cxx98)
15451 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15452
15453 /* Consume the string. */
15454 str = cp_parser_string_literal (parser, /*translate=*/true,
15455 /*wide_ok=*/true, /*lookup_udlit=*/false);
15456 if (str == error_mark_node)
15457 return error_mark_node;
15458 else if (TREE_CODE (str) == USERDEF_LITERAL)
15459 {
15460 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15461 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15462 end_loc = str.get_location ();
15463 }
15464 else
15465 {
15466 string_tree = str;
15467 /* Look for the suffix identifier. */
15468 token = cp_lexer_peek_token (parser->lexer);
15469 if (token->type == CPP_NAME)
15470 {
15471 id = cp_parser_identifier (parser);
15472 end_loc = token->location;
15473 }
15474 else if (token->type == CPP_KEYWORD)
15475 {
15476 error ("unexpected keyword;"
15477 " remove space between quotes and suffix identifier");
15478 return error_mark_node;
15479 }
15480 else
15481 {
15482 error ("expected suffix identifier");
15483 return error_mark_node;
15484 }
15485 }
15486 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15487 (TREE_TYPE (TREE_TYPE (string_tree))));
15488 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15489 if (len != 0)
15490 {
15491 error ("expected empty string after %<operator%> keyword");
15492 return error_mark_node;
15493 }
15494 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15495 != char_type_node)
15496 {
15497 error ("invalid encoding prefix in literal operator");
15498 return error_mark_node;
15499 }
15500 if (id != error_mark_node)
15501 {
15502 const char *name = IDENTIFIER_POINTER (id);
15503 id = cp_literal_operator_id (name);
15504 }
15505 /* Generate a location of the form:
15506 "" _suffix_identifier
15507 ^~~~~~~~~~~~~~~~~~~~~
15508 with caret == start at the start token, finish at the end of the
15509 suffix identifier. */
15510 location_t finish_loc
15511 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15512 location_t combined_loc
15513 = make_location (start_loc, start_loc, finish_loc);
15514 return cp_expr (id, combined_loc);
15515 }
15516
15517 default:
15518 /* Anything else is an error. */
15519 break;
15520 }
15521
15522 /* If we have selected an identifier, we need to consume the
15523 operator token. */
15524 if (op != ERROR_MARK)
15525 {
15526 id = ovl_op_identifier (assop, op);
15527 if (!consumed)
15528 cp_lexer_consume_token (parser->lexer);
15529 }
15530 /* Otherwise, no valid operator name was present. */
15531 else
15532 {
15533 cp_parser_error (parser, "expected operator");
15534 id = error_mark_node;
15535 }
15536
15537 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15538 return cp_expr (id, start_loc);
15539 }
15540
15541 /* Parse a template-declaration.
15542
15543 template-declaration:
15544 export [opt] template < template-parameter-list > declaration
15545
15546 If MEMBER_P is TRUE, this template-declaration occurs within a
15547 class-specifier.
15548
15549 The grammar rule given by the standard isn't correct. What
15550 is really meant is:
15551
15552 template-declaration:
15553 export [opt] template-parameter-list-seq
15554 decl-specifier-seq [opt] init-declarator [opt] ;
15555 export [opt] template-parameter-list-seq
15556 function-definition
15557
15558 template-parameter-list-seq:
15559 template-parameter-list-seq [opt]
15560 template < template-parameter-list >
15561
15562 Concept Extensions:
15563
15564 template-parameter-list-seq:
15565 template < template-parameter-list > requires-clause [opt]
15566
15567 requires-clause:
15568 requires logical-or-expression */
15569
15570 static void
15571 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15572 {
15573 /* Check for `export'. */
15574 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15575 {
15576 /* Consume the `export' token. */
15577 cp_lexer_consume_token (parser->lexer);
15578 /* Warn that we do not support `export'. */
15579 warning (0, "keyword %<export%> not implemented, and will be ignored");
15580 }
15581
15582 cp_parser_template_declaration_after_export (parser, member_p);
15583 }
15584
15585 /* Parse a template-parameter-list.
15586
15587 template-parameter-list:
15588 template-parameter
15589 template-parameter-list , template-parameter
15590
15591 Returns a TREE_LIST. Each node represents a template parameter.
15592 The nodes are connected via their TREE_CHAINs. */
15593
15594 static tree
15595 cp_parser_template_parameter_list (cp_parser* parser)
15596 {
15597 tree parameter_list = NULL_TREE;
15598
15599 /* Don't create wrapper nodes within a template-parameter-list,
15600 since we don't want to have different types based on the
15601 spelling location of constants and decls within them. */
15602 auto_suppress_location_wrappers sentinel;
15603
15604 begin_template_parm_list ();
15605
15606 /* The loop below parses the template parms. We first need to know
15607 the total number of template parms to be able to compute proper
15608 canonical types of each dependent type. So after the loop, when
15609 we know the total number of template parms,
15610 end_template_parm_list computes the proper canonical types and
15611 fixes up the dependent types accordingly. */
15612 while (true)
15613 {
15614 tree parameter;
15615 bool is_non_type;
15616 bool is_parameter_pack;
15617 location_t parm_loc;
15618
15619 /* Parse the template-parameter. */
15620 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15621 parameter = cp_parser_template_parameter (parser,
15622 &is_non_type,
15623 &is_parameter_pack);
15624 /* Add it to the list. */
15625 if (parameter != error_mark_node)
15626 parameter_list = process_template_parm (parameter_list,
15627 parm_loc,
15628 parameter,
15629 is_non_type,
15630 is_parameter_pack);
15631 else
15632 {
15633 tree err_parm = build_tree_list (parameter, parameter);
15634 parameter_list = chainon (parameter_list, err_parm);
15635 }
15636
15637 /* If the next token is not a `,', we're done. */
15638 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15639 break;
15640 /* Otherwise, consume the `,' token. */
15641 cp_lexer_consume_token (parser->lexer);
15642 }
15643
15644 return end_template_parm_list (parameter_list);
15645 }
15646
15647 /* Parse a introduction-list.
15648
15649 introduction-list:
15650 introduced-parameter
15651 introduction-list , introduced-parameter
15652
15653 introduced-parameter:
15654 ...[opt] identifier
15655
15656 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15657 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15658 WILDCARD_DECL will also have DECL_NAME set and token location in
15659 DECL_SOURCE_LOCATION. */
15660
15661 static tree
15662 cp_parser_introduction_list (cp_parser *parser)
15663 {
15664 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15665
15666 while (true)
15667 {
15668 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15669 if (is_pack)
15670 cp_lexer_consume_token (parser->lexer);
15671
15672 tree identifier = cp_parser_identifier (parser);
15673 if (identifier == error_mark_node)
15674 break;
15675
15676 /* Build placeholder. */
15677 tree parm = build_nt (WILDCARD_DECL);
15678 DECL_SOURCE_LOCATION (parm)
15679 = cp_lexer_peek_token (parser->lexer)->location;
15680 DECL_NAME (parm) = identifier;
15681 WILDCARD_PACK_P (parm) = is_pack;
15682 vec_safe_push (introduction_vec, parm);
15683
15684 /* If the next token is not a `,', we're done. */
15685 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15686 break;
15687 /* Otherwise, consume the `,' token. */
15688 cp_lexer_consume_token (parser->lexer);
15689 }
15690
15691 /* Convert the vec into a TREE_VEC. */
15692 tree introduction_list = make_tree_vec (introduction_vec->length ());
15693 unsigned int n;
15694 tree parm;
15695 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15696 TREE_VEC_ELT (introduction_list, n) = parm;
15697
15698 release_tree_vector (introduction_vec);
15699 return introduction_list;
15700 }
15701
15702 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15703 is an abstract declarator. */
15704
15705 static inline cp_declarator*
15706 get_id_declarator (cp_declarator *declarator)
15707 {
15708 cp_declarator *d = declarator;
15709 while (d && d->kind != cdk_id)
15710 d = d->declarator;
15711 return d;
15712 }
15713
15714 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15715 is an abstract declarator. */
15716
15717 static inline tree
15718 get_unqualified_id (cp_declarator *declarator)
15719 {
15720 declarator = get_id_declarator (declarator);
15721 if (declarator)
15722 return declarator->u.id.unqualified_name;
15723 else
15724 return NULL_TREE;
15725 }
15726
15727 /* Returns true if DECL represents a constrained-parameter. */
15728
15729 static inline bool
15730 is_constrained_parameter (tree decl)
15731 {
15732 return (decl
15733 && TREE_CODE (decl) == TYPE_DECL
15734 && CONSTRAINED_PARM_CONCEPT (decl)
15735 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15736 }
15737
15738 /* Returns true if PARM declares a constrained-parameter. */
15739
15740 static inline bool
15741 is_constrained_parameter (cp_parameter_declarator *parm)
15742 {
15743 return is_constrained_parameter (parm->decl_specifiers.type);
15744 }
15745
15746 /* Check that the type parameter is only a declarator-id, and that its
15747 type is not cv-qualified. */
15748
15749 bool
15750 cp_parser_check_constrained_type_parm (cp_parser *parser,
15751 cp_parameter_declarator *parm)
15752 {
15753 if (!parm->declarator)
15754 return true;
15755
15756 if (parm->declarator->kind != cdk_id)
15757 {
15758 cp_parser_error (parser, "invalid constrained type parameter");
15759 return false;
15760 }
15761
15762 /* Don't allow cv-qualified type parameters. */
15763 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15764 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15765 {
15766 cp_parser_error (parser, "cv-qualified type parameter");
15767 return false;
15768 }
15769
15770 return true;
15771 }
15772
15773 /* Finish parsing/processing a template type parameter and checking
15774 various restrictions. */
15775
15776 static inline tree
15777 cp_parser_constrained_type_template_parm (cp_parser *parser,
15778 tree id,
15779 cp_parameter_declarator* parmdecl)
15780 {
15781 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15782 return finish_template_type_parm (class_type_node, id);
15783 else
15784 return error_mark_node;
15785 }
15786
15787 static tree
15788 finish_constrained_template_template_parm (tree proto, tree id)
15789 {
15790 /* FIXME: This should probably be copied, and we may need to adjust
15791 the template parameter depths. */
15792 tree saved_parms = current_template_parms;
15793 begin_template_parm_list ();
15794 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15795 end_template_parm_list ();
15796
15797 tree parm = finish_template_template_parm (class_type_node, id);
15798 current_template_parms = saved_parms;
15799
15800 return parm;
15801 }
15802
15803 /* Finish parsing/processing a template template parameter by borrowing
15804 the template parameter list from the prototype parameter. */
15805
15806 static tree
15807 cp_parser_constrained_template_template_parm (cp_parser *parser,
15808 tree proto,
15809 tree id,
15810 cp_parameter_declarator *parmdecl)
15811 {
15812 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15813 return error_mark_node;
15814 return finish_constrained_template_template_parm (proto, id);
15815 }
15816
15817 /* Create a new non-type template parameter from the given PARM
15818 declarator. */
15819
15820 static tree
15821 constrained_non_type_template_parm (bool *is_non_type,
15822 cp_parameter_declarator *parm)
15823 {
15824 *is_non_type = true;
15825 cp_declarator *decl = parm->declarator;
15826 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15827 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15828 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15829 }
15830
15831 /* Build a constrained template parameter based on the PARMDECL
15832 declarator. The type of PARMDECL is the constrained type, which
15833 refers to the prototype template parameter that ultimately
15834 specifies the type of the declared parameter. */
15835
15836 static tree
15837 finish_constrained_parameter (cp_parser *parser,
15838 cp_parameter_declarator *parmdecl,
15839 bool *is_non_type,
15840 bool *is_parameter_pack)
15841 {
15842 tree decl = parmdecl->decl_specifiers.type;
15843 tree id = get_unqualified_id (parmdecl->declarator);
15844 tree def = parmdecl->default_argument;
15845 tree proto = DECL_INITIAL (decl);
15846
15847 /* A template parameter constrained by a variadic concept shall also
15848 be declared as a template parameter pack. */
15849 bool is_variadic = template_parameter_pack_p (proto);
15850 if (is_variadic && !*is_parameter_pack)
15851 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15852
15853 /* Build the parameter. Return an error if the declarator was invalid. */
15854 tree parm;
15855 if (TREE_CODE (proto) == TYPE_DECL)
15856 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15857 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15858 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15859 parmdecl);
15860 else
15861 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15862 if (parm == error_mark_node)
15863 return error_mark_node;
15864
15865 /* Finish the parameter decl and create a node attaching the
15866 default argument and constraint. */
15867 parm = build_tree_list (def, parm);
15868 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15869
15870 return parm;
15871 }
15872
15873 /* Returns true if the parsed type actually represents the declaration
15874 of a type template-parameter. */
15875
15876 static inline bool
15877 declares_constrained_type_template_parameter (tree type)
15878 {
15879 return (is_constrained_parameter (type)
15880 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15881 }
15882
15883
15884 /* Returns true if the parsed type actually represents the declaration of
15885 a template template-parameter. */
15886
15887 static bool
15888 declares_constrained_template_template_parameter (tree type)
15889 {
15890 return (is_constrained_parameter (type)
15891 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15892 }
15893
15894 /* Parse a default argument for a type template-parameter.
15895 Note that diagnostics are handled in cp_parser_template_parameter. */
15896
15897 static tree
15898 cp_parser_default_type_template_argument (cp_parser *parser)
15899 {
15900 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15901
15902 /* Consume the `=' token. */
15903 cp_lexer_consume_token (parser->lexer);
15904
15905 cp_token *token = cp_lexer_peek_token (parser->lexer);
15906
15907 /* Parse the default-argument. */
15908 push_deferring_access_checks (dk_no_deferred);
15909 tree default_argument = cp_parser_type_id (parser,
15910 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15911 NULL);
15912 pop_deferring_access_checks ();
15913
15914 if (flag_concepts && type_uses_auto (default_argument))
15915 {
15916 error_at (token->location,
15917 "invalid use of %<auto%> in default template argument");
15918 return error_mark_node;
15919 }
15920
15921 return default_argument;
15922 }
15923
15924 /* Parse a default argument for a template template-parameter. */
15925
15926 static tree
15927 cp_parser_default_template_template_argument (cp_parser *parser)
15928 {
15929 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15930
15931 bool is_template;
15932
15933 /* Consume the `='. */
15934 cp_lexer_consume_token (parser->lexer);
15935 /* Parse the id-expression. */
15936 push_deferring_access_checks (dk_no_deferred);
15937 /* save token before parsing the id-expression, for error
15938 reporting */
15939 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15940 tree default_argument
15941 = cp_parser_id_expression (parser,
15942 /*template_keyword_p=*/false,
15943 /*check_dependency_p=*/true,
15944 /*template_p=*/&is_template,
15945 /*declarator_p=*/false,
15946 /*optional_p=*/false);
15947 if (TREE_CODE (default_argument) == TYPE_DECL)
15948 /* If the id-expression was a template-id that refers to
15949 a template-class, we already have the declaration here,
15950 so no further lookup is needed. */
15951 ;
15952 else
15953 /* Look up the name. */
15954 default_argument
15955 = cp_parser_lookup_name (parser, default_argument,
15956 none_type,
15957 /*is_template=*/is_template,
15958 /*is_namespace=*/false,
15959 /*check_dependency=*/true,
15960 /*ambiguous_decls=*/NULL,
15961 token->location);
15962 /* See if the default argument is valid. */
15963 default_argument = check_template_template_default_arg (default_argument);
15964 pop_deferring_access_checks ();
15965 return default_argument;
15966 }
15967
15968 /* Parse a template-parameter.
15969
15970 template-parameter:
15971 type-parameter
15972 parameter-declaration
15973
15974 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15975 the parameter. The TREE_PURPOSE is the default value, if any.
15976 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15977 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15978 set to true iff this parameter is a parameter pack. */
15979
15980 static tree
15981 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15982 bool *is_parameter_pack)
15983 {
15984 cp_token *token;
15985 cp_parameter_declarator *parameter_declarator;
15986 tree parm;
15987
15988 /* Assume it is a type parameter or a template parameter. */
15989 *is_non_type = false;
15990 /* Assume it not a parameter pack. */
15991 *is_parameter_pack = false;
15992 /* Peek at the next token. */
15993 token = cp_lexer_peek_token (parser->lexer);
15994 /* If it is `template', we have a type-parameter. */
15995 if (token->keyword == RID_TEMPLATE)
15996 return cp_parser_type_parameter (parser, is_parameter_pack);
15997 /* If it is `class' or `typename' we do not know yet whether it is a
15998 type parameter or a non-type parameter. Consider:
15999
16000 template <typename T, typename T::X X> ...
16001
16002 or:
16003
16004 template <class C, class D*> ...
16005
16006 Here, the first parameter is a type parameter, and the second is
16007 a non-type parameter. We can tell by looking at the token after
16008 the identifier -- if it is a `,', `=', or `>' then we have a type
16009 parameter. */
16010 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
16011 {
16012 /* Peek at the token after `class' or `typename'. */
16013 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16014 /* If it's an ellipsis, we have a template type parameter
16015 pack. */
16016 if (token->type == CPP_ELLIPSIS)
16017 return cp_parser_type_parameter (parser, is_parameter_pack);
16018 /* If it's an identifier, skip it. */
16019 if (token->type == CPP_NAME)
16020 token = cp_lexer_peek_nth_token (parser->lexer, 3);
16021 /* Now, see if the token looks like the end of a template
16022 parameter. */
16023 if (token->type == CPP_COMMA
16024 || token->type == CPP_EQ
16025 || token->type == CPP_GREATER)
16026 return cp_parser_type_parameter (parser, is_parameter_pack);
16027 }
16028
16029 /* Otherwise, it is a non-type parameter or a constrained parameter.
16030
16031 [temp.param]
16032
16033 When parsing a default template-argument for a non-type
16034 template-parameter, the first non-nested `>' is taken as the end
16035 of the template parameter-list rather than a greater-than
16036 operator. */
16037 parameter_declarator
16038 = cp_parser_parameter_declaration (parser,
16039 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16040 /*template_parm_p=*/true,
16041 /*parenthesized_p=*/NULL);
16042
16043 if (!parameter_declarator)
16044 return error_mark_node;
16045
16046 /* If the parameter declaration is marked as a parameter pack, set
16047 *IS_PARAMETER_PACK to notify the caller. */
16048 if (parameter_declarator->template_parameter_pack_p)
16049 *is_parameter_pack = true;
16050
16051 if (parameter_declarator->default_argument)
16052 {
16053 /* Can happen in some cases of erroneous input (c++/34892). */
16054 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16055 /* Consume the `...' for better error recovery. */
16056 cp_lexer_consume_token (parser->lexer);
16057 }
16058
16059 // The parameter may have been constrained.
16060 if (is_constrained_parameter (parameter_declarator))
16061 return finish_constrained_parameter (parser,
16062 parameter_declarator,
16063 is_non_type,
16064 is_parameter_pack);
16065
16066 // Now we're sure that the parameter is a non-type parameter.
16067 *is_non_type = true;
16068
16069 parm = grokdeclarator (parameter_declarator->declarator,
16070 &parameter_declarator->decl_specifiers,
16071 TPARM, /*initialized=*/0,
16072 /*attrlist=*/NULL);
16073 if (parm == error_mark_node)
16074 return error_mark_node;
16075
16076 return build_tree_list (parameter_declarator->default_argument, parm);
16077 }
16078
16079 /* Parse a type-parameter.
16080
16081 type-parameter:
16082 class identifier [opt]
16083 class identifier [opt] = type-id
16084 typename identifier [opt]
16085 typename identifier [opt] = type-id
16086 template < template-parameter-list > class identifier [opt]
16087 template < template-parameter-list > class identifier [opt]
16088 = id-expression
16089
16090 GNU Extension (variadic templates):
16091
16092 type-parameter:
16093 class ... identifier [opt]
16094 typename ... identifier [opt]
16095
16096 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16097 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16098 the declaration of the parameter.
16099
16100 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16101
16102 static tree
16103 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
16104 {
16105 cp_token *token;
16106 tree parameter;
16107
16108 /* Look for a keyword to tell us what kind of parameter this is. */
16109 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
16110 if (!token)
16111 return error_mark_node;
16112
16113 switch (token->keyword)
16114 {
16115 case RID_CLASS:
16116 case RID_TYPENAME:
16117 {
16118 tree identifier;
16119 tree default_argument;
16120
16121 /* If the next token is an ellipsis, we have a template
16122 argument pack. */
16123 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16124 {
16125 /* Consume the `...' token. */
16126 cp_lexer_consume_token (parser->lexer);
16127 maybe_warn_variadic_templates ();
16128
16129 *is_parameter_pack = true;
16130 }
16131
16132 /* If the next token is an identifier, then it names the
16133 parameter. */
16134 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16135 identifier = cp_parser_identifier (parser);
16136 else
16137 identifier = NULL_TREE;
16138
16139 /* Create the parameter. */
16140 parameter = finish_template_type_parm (class_type_node, identifier);
16141
16142 /* If the next token is an `=', we have a default argument. */
16143 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16144 {
16145 default_argument
16146 = cp_parser_default_type_template_argument (parser);
16147
16148 /* Template parameter packs cannot have default
16149 arguments. */
16150 if (*is_parameter_pack)
16151 {
16152 if (identifier)
16153 error_at (token->location,
16154 "template parameter pack %qD cannot have a "
16155 "default argument", identifier);
16156 else
16157 error_at (token->location,
16158 "template parameter packs cannot have "
16159 "default arguments");
16160 default_argument = NULL_TREE;
16161 }
16162 else if (check_for_bare_parameter_packs (default_argument))
16163 default_argument = error_mark_node;
16164 }
16165 else
16166 default_argument = NULL_TREE;
16167
16168 /* Create the combined representation of the parameter and the
16169 default argument. */
16170 parameter = build_tree_list (default_argument, parameter);
16171 }
16172 break;
16173
16174 case RID_TEMPLATE:
16175 {
16176 tree identifier;
16177 tree default_argument;
16178
16179 /* Look for the `<'. */
16180 cp_parser_require (parser, CPP_LESS, RT_LESS);
16181 /* Parse the template-parameter-list. */
16182 cp_parser_template_parameter_list (parser);
16183 /* Look for the `>'. */
16184 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16185
16186 // If template requirements are present, parse them.
16187 if (flag_concepts)
16188 {
16189 tree reqs = get_shorthand_constraints (current_template_parms);
16190 if (tree r = cp_parser_requires_clause_opt (parser))
16191 reqs = conjoin_constraints (reqs, normalize_expression (r));
16192 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16193 }
16194
16195 /* Look for the `class' or 'typename' keywords. */
16196 cp_parser_type_parameter_key (parser);
16197 /* If the next token is an ellipsis, we have a template
16198 argument pack. */
16199 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16200 {
16201 /* Consume the `...' token. */
16202 cp_lexer_consume_token (parser->lexer);
16203 maybe_warn_variadic_templates ();
16204
16205 *is_parameter_pack = true;
16206 }
16207 /* If the next token is an `=', then there is a
16208 default-argument. If the next token is a `>', we are at
16209 the end of the parameter-list. If the next token is a `,',
16210 then we are at the end of this parameter. */
16211 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16212 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16213 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16214 {
16215 identifier = cp_parser_identifier (parser);
16216 /* Treat invalid names as if the parameter were nameless. */
16217 if (identifier == error_mark_node)
16218 identifier = NULL_TREE;
16219 }
16220 else
16221 identifier = NULL_TREE;
16222
16223 /* Create the template parameter. */
16224 parameter = finish_template_template_parm (class_type_node,
16225 identifier);
16226
16227 /* If the next token is an `=', then there is a
16228 default-argument. */
16229 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16230 {
16231 default_argument
16232 = cp_parser_default_template_template_argument (parser);
16233
16234 /* Template parameter packs cannot have default
16235 arguments. */
16236 if (*is_parameter_pack)
16237 {
16238 if (identifier)
16239 error_at (token->location,
16240 "template parameter pack %qD cannot "
16241 "have a default argument",
16242 identifier);
16243 else
16244 error_at (token->location, "template parameter packs cannot "
16245 "have default arguments");
16246 default_argument = NULL_TREE;
16247 }
16248 }
16249 else
16250 default_argument = NULL_TREE;
16251
16252 /* Create the combined representation of the parameter and the
16253 default argument. */
16254 parameter = build_tree_list (default_argument, parameter);
16255 }
16256 break;
16257
16258 default:
16259 gcc_unreachable ();
16260 break;
16261 }
16262
16263 return parameter;
16264 }
16265
16266 /* Parse a template-id.
16267
16268 template-id:
16269 template-name < template-argument-list [opt] >
16270
16271 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16272 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16273 returned. Otherwise, if the template-name names a function, or set
16274 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16275 names a class, returns a TYPE_DECL for the specialization.
16276
16277 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16278 uninstantiated templates. */
16279
16280 static tree
16281 cp_parser_template_id (cp_parser *parser,
16282 bool template_keyword_p,
16283 bool check_dependency_p,
16284 enum tag_types tag_type,
16285 bool is_declaration)
16286 {
16287 tree templ;
16288 tree arguments;
16289 tree template_id;
16290 cp_token_position start_of_id = 0;
16291 cp_token *next_token = NULL, *next_token_2 = NULL;
16292 bool is_identifier;
16293
16294 /* If the next token corresponds to a template-id, there is no need
16295 to reparse it. */
16296 cp_token *token = cp_lexer_peek_token (parser->lexer);
16297 if (token->type == CPP_TEMPLATE_ID)
16298 {
16299 cp_lexer_consume_token (parser->lexer);
16300 return saved_checks_value (token->u.tree_check_value);
16301 }
16302
16303 /* Avoid performing name lookup if there is no possibility of
16304 finding a template-id. */
16305 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16306 || (token->type == CPP_NAME
16307 && !cp_parser_nth_token_starts_template_argument_list_p
16308 (parser, 2)))
16309 {
16310 cp_parser_error (parser, "expected template-id");
16311 return error_mark_node;
16312 }
16313
16314 /* Remember where the template-id starts. */
16315 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16316 start_of_id = cp_lexer_token_position (parser->lexer, false);
16317
16318 push_deferring_access_checks (dk_deferred);
16319
16320 /* Parse the template-name. */
16321 is_identifier = false;
16322 templ = cp_parser_template_name (parser, template_keyword_p,
16323 check_dependency_p,
16324 is_declaration,
16325 tag_type,
16326 &is_identifier);
16327
16328 /* Push any access checks inside the firewall we're about to create. */
16329 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16330 pop_deferring_access_checks ();
16331 if (templ == error_mark_node || is_identifier)
16332 return templ;
16333
16334 /* Since we're going to preserve any side-effects from this parse, set up a
16335 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16336 in the template arguments. */
16337 tentative_firewall firewall (parser);
16338 reopen_deferring_access_checks (checks);
16339
16340 /* If we find the sequence `[:' after a template-name, it's probably
16341 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16342 parse correctly the argument list. */
16343 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16344 == CPP_OPEN_SQUARE)
16345 && next_token->flags & DIGRAPH
16346 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16347 == CPP_COLON)
16348 && !(next_token_2->flags & PREV_WHITE))
16349 {
16350 cp_parser_parse_tentatively (parser);
16351 /* Change `:' into `::'. */
16352 next_token_2->type = CPP_SCOPE;
16353 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16354 CPP_LESS. */
16355 cp_lexer_consume_token (parser->lexer);
16356
16357 /* Parse the arguments. */
16358 arguments = cp_parser_enclosed_template_argument_list (parser);
16359 if (!cp_parser_parse_definitely (parser))
16360 {
16361 /* If we couldn't parse an argument list, then we revert our changes
16362 and return simply an error. Maybe this is not a template-id
16363 after all. */
16364 next_token_2->type = CPP_COLON;
16365 cp_parser_error (parser, "expected %<<%>");
16366 pop_deferring_access_checks ();
16367 return error_mark_node;
16368 }
16369 /* Otherwise, emit an error about the invalid digraph, but continue
16370 parsing because we got our argument list. */
16371 if (permerror (next_token->location,
16372 "%<<::%> cannot begin a template-argument list"))
16373 {
16374 static bool hint = false;
16375 inform (next_token->location,
16376 "%<<:%> is an alternate spelling for %<[%>."
16377 " Insert whitespace between %<<%> and %<::%>");
16378 if (!hint && !flag_permissive)
16379 {
16380 inform (next_token->location, "(if you use %<-fpermissive%> "
16381 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16382 "accept your code)");
16383 hint = true;
16384 }
16385 }
16386 }
16387 else
16388 {
16389 /* Look for the `<' that starts the template-argument-list. */
16390 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16391 {
16392 pop_deferring_access_checks ();
16393 return error_mark_node;
16394 }
16395 /* Parse the arguments. */
16396 arguments = cp_parser_enclosed_template_argument_list (parser);
16397
16398 if ((cxx_dialect > cxx17)
16399 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16400 && !template_keyword_p
16401 && (cp_parser_error_occurred (parser)
16402 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16403 {
16404 /* This didn't go well. */
16405 if (TREE_CODE (templ) == FUNCTION_DECL)
16406 {
16407 /* C++2A says that "function-name < a;" is now ill-formed. */
16408 if (cp_parser_error_occurred (parser))
16409 {
16410 error_at (token->location, "invalid template-argument-list");
16411 inform (token->location, "function name as the left hand "
16412 "operand of %<<%> is ill-formed in C++2a; wrap the "
16413 "function name in %<()%>");
16414 }
16415 else
16416 /* We expect "f<targs>" to be followed by "(args)". */
16417 error_at (cp_lexer_peek_token (parser->lexer)->location,
16418 "expected %<(%> after template-argument-list");
16419 if (start_of_id)
16420 /* Purge all subsequent tokens. */
16421 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16422 }
16423 else
16424 cp_parser_simulate_error (parser);
16425 pop_deferring_access_checks ();
16426 return error_mark_node;
16427 }
16428 }
16429
16430 /* Set the location to be of the form:
16431 template-name < template-argument-list [opt] >
16432 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16433 with caret == start at the start of the template-name,
16434 ranging until the closing '>'. */
16435 location_t finish_loc
16436 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16437 location_t combined_loc
16438 = make_location (token->location, token->location, finish_loc);
16439
16440 /* Check for concepts autos where they don't belong. We could
16441 identify types in some cases of idnetifier TEMPL, looking ahead
16442 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16443 types. We reject them in functions, but if what we have is an
16444 identifier, even with none_type we can't conclude it's NOT a
16445 type, we have to wait for template substitution. */
16446 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16447 template_id = error_mark_node;
16448 /* Build a representation of the specialization. */
16449 else if (identifier_p (templ))
16450 template_id = build_min_nt_loc (combined_loc,
16451 TEMPLATE_ID_EXPR,
16452 templ, arguments);
16453 else if (DECL_TYPE_TEMPLATE_P (templ)
16454 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16455 {
16456 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16457 template (rather than some instantiation thereof) only if
16458 is not nested within some other construct. For example, in
16459 "template <typename T> void f(T) { A<T>::", A<T> is just an
16460 instantiation of A. */
16461 bool entering_scope
16462 = (template_parm_scope_p ()
16463 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16464 template_id
16465 = finish_template_type (templ, arguments, entering_scope);
16466 }
16467 /* A template-like identifier may be a partial concept id. */
16468 else if (flag_concepts
16469 && (template_id = (cp_parser_maybe_partial_concept_id
16470 (parser, templ, arguments))))
16471 return template_id;
16472 else if (variable_template_p (templ))
16473 {
16474 template_id = lookup_template_variable (templ, arguments);
16475 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16476 SET_EXPR_LOCATION (template_id, combined_loc);
16477 }
16478 else
16479 {
16480 /* If it's not a class-template or a template-template, it should be
16481 a function-template. */
16482 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
16483
16484 template_id = lookup_template_function (templ, arguments);
16485 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16486 SET_EXPR_LOCATION (template_id, combined_loc);
16487 }
16488
16489 /* If parsing tentatively, replace the sequence of tokens that makes
16490 up the template-id with a CPP_TEMPLATE_ID token. That way,
16491 should we re-parse the token stream, we will not have to repeat
16492 the effort required to do the parse, nor will we issue duplicate
16493 error messages about problems during instantiation of the
16494 template. */
16495 if (start_of_id
16496 /* Don't do this if we had a parse error in a declarator; re-parsing
16497 might succeed if a name changes meaning (60361). */
16498 && !(cp_parser_error_occurred (parser)
16499 && cp_parser_parsing_tentatively (parser)
16500 && parser->in_declarator_p))
16501 {
16502 /* Reset the contents of the START_OF_ID token. */
16503 token->type = CPP_TEMPLATE_ID;
16504 token->location = combined_loc;
16505
16506 /* Retrieve any deferred checks. Do not pop this access checks yet
16507 so the memory will not be reclaimed during token replacing below. */
16508 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16509 token->u.tree_check_value->value = template_id;
16510 token->u.tree_check_value->checks = get_deferred_access_checks ();
16511 token->keyword = RID_MAX;
16512
16513 /* Purge all subsequent tokens. */
16514 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16515
16516 /* ??? Can we actually assume that, if template_id ==
16517 error_mark_node, we will have issued a diagnostic to the
16518 user, as opposed to simply marking the tentative parse as
16519 failed? */
16520 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16521 error_at (token->location, "parse error in template argument list");
16522 }
16523
16524 pop_to_parent_deferring_access_checks ();
16525 return template_id;
16526 }
16527
16528 /* Parse a template-name.
16529
16530 template-name:
16531 identifier
16532
16533 The standard should actually say:
16534
16535 template-name:
16536 identifier
16537 operator-function-id
16538
16539 A defect report has been filed about this issue.
16540
16541 A conversion-function-id cannot be a template name because they cannot
16542 be part of a template-id. In fact, looking at this code:
16543
16544 a.operator K<int>()
16545
16546 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16547 It is impossible to call a templated conversion-function-id with an
16548 explicit argument list, since the only allowed template parameter is
16549 the type to which it is converting.
16550
16551 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16552 `template' keyword, in a construction like:
16553
16554 T::template f<3>()
16555
16556 In that case `f' is taken to be a template-name, even though there
16557 is no way of knowing for sure.
16558
16559 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16560 name refers to a set of overloaded functions, at least one of which
16561 is a template, or an IDENTIFIER_NODE with the name of the template,
16562 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16563 names are looked up inside uninstantiated templates. */
16564
16565 static tree
16566 cp_parser_template_name (cp_parser* parser,
16567 bool template_keyword_p,
16568 bool check_dependency_p,
16569 bool is_declaration,
16570 enum tag_types tag_type,
16571 bool *is_identifier)
16572 {
16573 tree identifier;
16574 tree decl;
16575 cp_token *token = cp_lexer_peek_token (parser->lexer);
16576
16577 /* If the next token is `operator', then we have either an
16578 operator-function-id or a conversion-function-id. */
16579 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16580 {
16581 /* We don't know whether we're looking at an
16582 operator-function-id or a conversion-function-id. */
16583 cp_parser_parse_tentatively (parser);
16584 /* Try an operator-function-id. */
16585 identifier = cp_parser_operator_function_id (parser);
16586 /* If that didn't work, try a conversion-function-id. */
16587 if (!cp_parser_parse_definitely (parser))
16588 {
16589 cp_parser_error (parser, "expected template-name");
16590 return error_mark_node;
16591 }
16592 }
16593 /* Look for the identifier. */
16594 else
16595 identifier = cp_parser_identifier (parser);
16596
16597 /* If we didn't find an identifier, we don't have a template-id. */
16598 if (identifier == error_mark_node)
16599 return error_mark_node;
16600
16601 /* If the name immediately followed the `template' keyword, then it
16602 is a template-name. However, if the next token is not `<', then
16603 we do not treat it as a template-name, since it is not being used
16604 as part of a template-id. This enables us to handle constructs
16605 like:
16606
16607 template <typename T> struct S { S(); };
16608 template <typename T> S<T>::S();
16609
16610 correctly. We would treat `S' as a template -- if it were `S<T>'
16611 -- but we do not if there is no `<'. */
16612
16613 if (processing_template_decl
16614 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16615 {
16616 /* In a declaration, in a dependent context, we pretend that the
16617 "template" keyword was present in order to improve error
16618 recovery. For example, given:
16619
16620 template <typename T> void f(T::X<int>);
16621
16622 we want to treat "X<int>" as a template-id. */
16623 if (is_declaration
16624 && !template_keyword_p
16625 && parser->scope && TYPE_P (parser->scope)
16626 && check_dependency_p
16627 && dependent_scope_p (parser->scope)
16628 /* Do not do this for dtors (or ctors), since they never
16629 need the template keyword before their name. */
16630 && !constructor_name_p (identifier, parser->scope))
16631 {
16632 cp_token_position start = 0;
16633
16634 /* Explain what went wrong. */
16635 error_at (token->location, "non-template %qD used as template",
16636 identifier);
16637 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16638 parser->scope, identifier);
16639 /* If parsing tentatively, find the location of the "<" token. */
16640 if (cp_parser_simulate_error (parser))
16641 start = cp_lexer_token_position (parser->lexer, true);
16642 /* Parse the template arguments so that we can issue error
16643 messages about them. */
16644 cp_lexer_consume_token (parser->lexer);
16645 cp_parser_enclosed_template_argument_list (parser);
16646 /* Skip tokens until we find a good place from which to
16647 continue parsing. */
16648 cp_parser_skip_to_closing_parenthesis (parser,
16649 /*recovering=*/true,
16650 /*or_comma=*/true,
16651 /*consume_paren=*/false);
16652 /* If parsing tentatively, permanently remove the
16653 template argument list. That will prevent duplicate
16654 error messages from being issued about the missing
16655 "template" keyword. */
16656 if (start)
16657 cp_lexer_purge_tokens_after (parser->lexer, start);
16658 if (is_identifier)
16659 *is_identifier = true;
16660 parser->context->object_type = NULL_TREE;
16661 return identifier;
16662 }
16663
16664 /* If the "template" keyword is present, then there is generally
16665 no point in doing name-lookup, so we just return IDENTIFIER.
16666 But, if the qualifying scope is non-dependent then we can
16667 (and must) do name-lookup normally. */
16668 if (template_keyword_p)
16669 {
16670 tree scope = (parser->scope ? parser->scope
16671 : parser->context->object_type);
16672 if (scope && TYPE_P (scope)
16673 && (!CLASS_TYPE_P (scope)
16674 || (check_dependency_p && dependent_type_p (scope))))
16675 {
16676 /* We're optimizing away the call to cp_parser_lookup_name, but
16677 we still need to do this. */
16678 parser->context->object_type = NULL_TREE;
16679 return identifier;
16680 }
16681 }
16682 }
16683
16684 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16685 const bool scoped_p = ((parser->scope ? parser->scope
16686 : parser->context->object_type) != NULL_TREE);
16687
16688 /* Look up the name. */
16689 decl = cp_parser_lookup_name (parser, identifier,
16690 tag_type,
16691 /*is_template=*/true,
16692 /*is_namespace=*/false,
16693 check_dependency_p,
16694 /*ambiguous_decls=*/NULL,
16695 token->location);
16696
16697 decl = strip_using_decl (decl);
16698
16699 /* If DECL is a template, then the name was a template-name. */
16700 if (TREE_CODE (decl) == TEMPLATE_DECL)
16701 {
16702 if (TREE_DEPRECATED (decl)
16703 && deprecated_state != DEPRECATED_SUPPRESS)
16704 warn_deprecated_use (decl, NULL_TREE);
16705 }
16706 else
16707 {
16708 /* The standard does not explicitly indicate whether a name that
16709 names a set of overloaded declarations, some of which are
16710 templates, is a template-name. However, such a name should
16711 be a template-name; otherwise, there is no way to form a
16712 template-id for the overloaded templates. */
16713 bool found = false;
16714
16715 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16716 !found && iter; ++iter)
16717 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16718 found = true;
16719
16720 if (!found
16721 && (cxx_dialect > cxx17)
16722 && !scoped_p
16723 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
16724 && tag_type == none_type)
16725 {
16726 /* [temp.names] says "A name is also considered to refer to a template
16727 if it is an unqualified-id followed by a < and name lookup finds
16728 either one or more functions or finds nothing." */
16729
16730 /* The "more functions" case. Just use the OVERLOAD as normally.
16731 We don't use is_overloaded_fn here to avoid considering
16732 BASELINKs. */
16733 if (TREE_CODE (decl) == OVERLOAD
16734 /* Name lookup found one function. */
16735 || TREE_CODE (decl) == FUNCTION_DECL)
16736 found = true;
16737 /* Name lookup found nothing. */
16738 else if (decl == error_mark_node)
16739 return identifier;
16740 }
16741
16742 if (!found)
16743 {
16744 /* The name does not name a template. */
16745 cp_parser_error (parser, "expected template-name");
16746 return error_mark_node;
16747 }
16748 }
16749
16750 return decl;
16751 }
16752
16753 /* Parse a template-argument-list.
16754
16755 template-argument-list:
16756 template-argument ... [opt]
16757 template-argument-list , template-argument ... [opt]
16758
16759 Returns a TREE_VEC containing the arguments. */
16760
16761 static tree
16762 cp_parser_template_argument_list (cp_parser* parser)
16763 {
16764 tree fixed_args[10];
16765 unsigned n_args = 0;
16766 unsigned alloced = 10;
16767 tree *arg_ary = fixed_args;
16768 tree vec;
16769 bool saved_in_template_argument_list_p;
16770 bool saved_ice_p;
16771 bool saved_non_ice_p;
16772
16773 /* Don't create location wrapper nodes within a template-argument-list. */
16774 auto_suppress_location_wrappers sentinel;
16775
16776 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16777 parser->in_template_argument_list_p = true;
16778 /* Even if the template-id appears in an integral
16779 constant-expression, the contents of the argument list do
16780 not. */
16781 saved_ice_p = parser->integral_constant_expression_p;
16782 parser->integral_constant_expression_p = false;
16783 saved_non_ice_p = parser->non_integral_constant_expression_p;
16784 parser->non_integral_constant_expression_p = false;
16785
16786 /* Parse the arguments. */
16787 do
16788 {
16789 tree argument;
16790
16791 if (n_args)
16792 /* Consume the comma. */
16793 cp_lexer_consume_token (parser->lexer);
16794
16795 /* Parse the template-argument. */
16796 argument = cp_parser_template_argument (parser);
16797
16798 /* If the next token is an ellipsis, we're expanding a template
16799 argument pack. */
16800 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16801 {
16802 if (argument == error_mark_node)
16803 {
16804 cp_token *token = cp_lexer_peek_token (parser->lexer);
16805 error_at (token->location,
16806 "expected parameter pack before %<...%>");
16807 }
16808 /* Consume the `...' token. */
16809 cp_lexer_consume_token (parser->lexer);
16810
16811 /* Make the argument into a TYPE_PACK_EXPANSION or
16812 EXPR_PACK_EXPANSION. */
16813 argument = make_pack_expansion (argument);
16814 }
16815
16816 if (n_args == alloced)
16817 {
16818 alloced *= 2;
16819
16820 if (arg_ary == fixed_args)
16821 {
16822 arg_ary = XNEWVEC (tree, alloced);
16823 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16824 }
16825 else
16826 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16827 }
16828 arg_ary[n_args++] = argument;
16829 }
16830 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16831
16832 vec = make_tree_vec (n_args);
16833
16834 while (n_args--)
16835 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16836
16837 if (arg_ary != fixed_args)
16838 free (arg_ary);
16839 parser->non_integral_constant_expression_p = saved_non_ice_p;
16840 parser->integral_constant_expression_p = saved_ice_p;
16841 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16842 if (CHECKING_P)
16843 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16844 return vec;
16845 }
16846
16847 /* Parse a template-argument.
16848
16849 template-argument:
16850 assignment-expression
16851 type-id
16852 id-expression
16853
16854 The representation is that of an assignment-expression, type-id, or
16855 id-expression -- except that the qualified id-expression is
16856 evaluated, so that the value returned is either a DECL or an
16857 OVERLOAD.
16858
16859 Although the standard says "assignment-expression", it forbids
16860 throw-expressions or assignments in the template argument.
16861 Therefore, we use "conditional-expression" instead. */
16862
16863 static tree
16864 cp_parser_template_argument (cp_parser* parser)
16865 {
16866 tree argument;
16867 bool template_p;
16868 bool address_p;
16869 bool maybe_type_id = false;
16870 cp_token *token = NULL, *argument_start_token = NULL;
16871 location_t loc = 0;
16872 cp_id_kind idk;
16873
16874 /* There's really no way to know what we're looking at, so we just
16875 try each alternative in order.
16876
16877 [temp.arg]
16878
16879 In a template-argument, an ambiguity between a type-id and an
16880 expression is resolved to a type-id, regardless of the form of
16881 the corresponding template-parameter.
16882
16883 Therefore, we try a type-id first. */
16884 cp_parser_parse_tentatively (parser);
16885 argument = cp_parser_template_type_arg (parser);
16886 /* If there was no error parsing the type-id but the next token is a
16887 '>>', our behavior depends on which dialect of C++ we're
16888 parsing. In C++98, we probably found a typo for '> >'. But there
16889 are type-id which are also valid expressions. For instance:
16890
16891 struct X { int operator >> (int); };
16892 template <int V> struct Foo {};
16893 Foo<X () >> 5> r;
16894
16895 Here 'X()' is a valid type-id of a function type, but the user just
16896 wanted to write the expression "X() >> 5". Thus, we remember that we
16897 found a valid type-id, but we still try to parse the argument as an
16898 expression to see what happens.
16899
16900 In C++0x, the '>>' will be considered two separate '>'
16901 tokens. */
16902 if (!cp_parser_error_occurred (parser)
16903 && cxx_dialect == cxx98
16904 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16905 {
16906 maybe_type_id = true;
16907 cp_parser_abort_tentative_parse (parser);
16908 }
16909 else
16910 {
16911 /* If the next token isn't a `,' or a `>', then this argument wasn't
16912 really finished. This means that the argument is not a valid
16913 type-id. */
16914 if (!cp_parser_next_token_ends_template_argument_p (parser))
16915 cp_parser_error (parser, "expected template-argument");
16916 /* If that worked, we're done. */
16917 if (cp_parser_parse_definitely (parser))
16918 return argument;
16919 }
16920 /* We're still not sure what the argument will be. */
16921 cp_parser_parse_tentatively (parser);
16922 /* Try a template. */
16923 argument_start_token = cp_lexer_peek_token (parser->lexer);
16924 argument = cp_parser_id_expression (parser,
16925 /*template_keyword_p=*/false,
16926 /*check_dependency_p=*/true,
16927 &template_p,
16928 /*declarator_p=*/false,
16929 /*optional_p=*/false);
16930 /* If the next token isn't a `,' or a `>', then this argument wasn't
16931 really finished. */
16932 if (!cp_parser_next_token_ends_template_argument_p (parser))
16933 cp_parser_error (parser, "expected template-argument");
16934 if (!cp_parser_error_occurred (parser))
16935 {
16936 /* Figure out what is being referred to. If the id-expression
16937 was for a class template specialization, then we will have a
16938 TYPE_DECL at this point. There is no need to do name lookup
16939 at this point in that case. */
16940 if (TREE_CODE (argument) != TYPE_DECL)
16941 argument = cp_parser_lookup_name (parser, argument,
16942 none_type,
16943 /*is_template=*/template_p,
16944 /*is_namespace=*/false,
16945 /*check_dependency=*/true,
16946 /*ambiguous_decls=*/NULL,
16947 argument_start_token->location);
16948 /* Handle a constrained-type-specifier for a non-type template
16949 parameter. */
16950 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16951 argument = decl;
16952 else if (TREE_CODE (argument) != TEMPLATE_DECL
16953 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16954 cp_parser_error (parser, "expected template-name");
16955 }
16956 if (cp_parser_parse_definitely (parser))
16957 {
16958 if (TREE_DEPRECATED (argument))
16959 warn_deprecated_use (argument, NULL_TREE);
16960 return argument;
16961 }
16962 /* It must be a non-type argument. In C++17 any constant-expression is
16963 allowed. */
16964 if (cxx_dialect > cxx14)
16965 goto general_expr;
16966
16967 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16968
16969 -- an integral constant-expression of integral or enumeration
16970 type; or
16971
16972 -- the name of a non-type template-parameter; or
16973
16974 -- the name of an object or function with external linkage...
16975
16976 -- the address of an object or function with external linkage...
16977
16978 -- a pointer to member... */
16979 /* Look for a non-type template parameter. */
16980 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16981 {
16982 cp_parser_parse_tentatively (parser);
16983 argument = cp_parser_primary_expression (parser,
16984 /*address_p=*/false,
16985 /*cast_p=*/false,
16986 /*template_arg_p=*/true,
16987 &idk);
16988 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16989 || !cp_parser_next_token_ends_template_argument_p (parser))
16990 cp_parser_simulate_error (parser);
16991 if (cp_parser_parse_definitely (parser))
16992 return argument;
16993 }
16994
16995 /* If the next token is "&", the argument must be the address of an
16996 object or function with external linkage. */
16997 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16998 if (address_p)
16999 {
17000 loc = cp_lexer_peek_token (parser->lexer)->location;
17001 cp_lexer_consume_token (parser->lexer);
17002 }
17003 /* See if we might have an id-expression. */
17004 token = cp_lexer_peek_token (parser->lexer);
17005 if (token->type == CPP_NAME
17006 || token->keyword == RID_OPERATOR
17007 || token->type == CPP_SCOPE
17008 || token->type == CPP_TEMPLATE_ID
17009 || token->type == CPP_NESTED_NAME_SPECIFIER)
17010 {
17011 cp_parser_parse_tentatively (parser);
17012 argument = cp_parser_primary_expression (parser,
17013 address_p,
17014 /*cast_p=*/false,
17015 /*template_arg_p=*/true,
17016 &idk);
17017 if (cp_parser_error_occurred (parser)
17018 || !cp_parser_next_token_ends_template_argument_p (parser))
17019 cp_parser_abort_tentative_parse (parser);
17020 else
17021 {
17022 tree probe;
17023
17024 if (INDIRECT_REF_P (argument))
17025 {
17026 /* Strip the dereference temporarily. */
17027 gcc_assert (REFERENCE_REF_P (argument));
17028 argument = TREE_OPERAND (argument, 0);
17029 }
17030
17031 /* If we're in a template, we represent a qualified-id referring
17032 to a static data member as a SCOPE_REF even if the scope isn't
17033 dependent so that we can check access control later. */
17034 probe = argument;
17035 if (TREE_CODE (probe) == SCOPE_REF)
17036 probe = TREE_OPERAND (probe, 1);
17037 if (VAR_P (probe))
17038 {
17039 /* A variable without external linkage might still be a
17040 valid constant-expression, so no error is issued here
17041 if the external-linkage check fails. */
17042 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
17043 cp_parser_simulate_error (parser);
17044 }
17045 else if (is_overloaded_fn (argument))
17046 /* All overloaded functions are allowed; if the external
17047 linkage test does not pass, an error will be issued
17048 later. */
17049 ;
17050 else if (address_p
17051 && (TREE_CODE (argument) == OFFSET_REF
17052 || TREE_CODE (argument) == SCOPE_REF))
17053 /* A pointer-to-member. */
17054 ;
17055 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
17056 ;
17057 else
17058 cp_parser_simulate_error (parser);
17059
17060 if (cp_parser_parse_definitely (parser))
17061 {
17062 if (address_p)
17063 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
17064 tf_warning_or_error);
17065 else
17066 argument = convert_from_reference (argument);
17067 return argument;
17068 }
17069 }
17070 }
17071 /* If the argument started with "&", there are no other valid
17072 alternatives at this point. */
17073 if (address_p)
17074 {
17075 cp_parser_error (parser, "invalid non-type template argument");
17076 return error_mark_node;
17077 }
17078
17079 general_expr:
17080 /* If the argument wasn't successfully parsed as a type-id followed
17081 by '>>', the argument can only be a constant expression now.
17082 Otherwise, we try parsing the constant-expression tentatively,
17083 because the argument could really be a type-id. */
17084 if (maybe_type_id)
17085 cp_parser_parse_tentatively (parser);
17086
17087 if (cxx_dialect <= cxx14)
17088 argument = cp_parser_constant_expression (parser);
17089 else
17090 {
17091 /* In C++20, we can encounter a braced-init-list. */
17092 if (cxx_dialect >= cxx2a
17093 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17094 {
17095 bool expr_non_constant_p;
17096 return cp_parser_braced_list (parser, &expr_non_constant_p);
17097 }
17098
17099 /* With C++17 generalized non-type template arguments we need to handle
17100 lvalue constant expressions, too. */
17101 argument = cp_parser_assignment_expression (parser);
17102 require_potential_constant_expression (argument);
17103 }
17104
17105 if (!maybe_type_id)
17106 return argument;
17107 if (!cp_parser_next_token_ends_template_argument_p (parser))
17108 cp_parser_error (parser, "expected template-argument");
17109 if (cp_parser_parse_definitely (parser))
17110 return argument;
17111 /* We did our best to parse the argument as a non type-id, but that
17112 was the only alternative that matched (albeit with a '>' after
17113 it). We can assume it's just a typo from the user, and a
17114 diagnostic will then be issued. */
17115 return cp_parser_template_type_arg (parser);
17116 }
17117
17118 /* Parse an explicit-instantiation.
17119
17120 explicit-instantiation:
17121 template declaration
17122
17123 Although the standard says `declaration', what it really means is:
17124
17125 explicit-instantiation:
17126 template decl-specifier-seq [opt] declarator [opt] ;
17127
17128 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17129 supposed to be allowed. A defect report has been filed about this
17130 issue.
17131
17132 GNU Extension:
17133
17134 explicit-instantiation:
17135 storage-class-specifier template
17136 decl-specifier-seq [opt] declarator [opt] ;
17137 function-specifier template
17138 decl-specifier-seq [opt] declarator [opt] ; */
17139
17140 static void
17141 cp_parser_explicit_instantiation (cp_parser* parser)
17142 {
17143 int declares_class_or_enum;
17144 cp_decl_specifier_seq decl_specifiers;
17145 tree extension_specifier = NULL_TREE;
17146
17147 timevar_push (TV_TEMPLATE_INST);
17148
17149 /* Look for an (optional) storage-class-specifier or
17150 function-specifier. */
17151 if (cp_parser_allow_gnu_extensions_p (parser))
17152 {
17153 extension_specifier
17154 = cp_parser_storage_class_specifier_opt (parser);
17155 if (!extension_specifier)
17156 extension_specifier
17157 = cp_parser_function_specifier_opt (parser,
17158 /*decl_specs=*/NULL);
17159 }
17160
17161 /* Look for the `template' keyword. */
17162 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17163 /* Let the front end know that we are processing an explicit
17164 instantiation. */
17165 begin_explicit_instantiation ();
17166 /* [temp.explicit] says that we are supposed to ignore access
17167 control while processing explicit instantiation directives. */
17168 push_deferring_access_checks (dk_no_check);
17169 /* Parse a decl-specifier-seq. */
17170 cp_parser_decl_specifier_seq (parser,
17171 CP_PARSER_FLAGS_OPTIONAL,
17172 &decl_specifiers,
17173 &declares_class_or_enum);
17174 /* If there was exactly one decl-specifier, and it declared a class,
17175 and there's no declarator, then we have an explicit type
17176 instantiation. */
17177 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17178 {
17179 tree type;
17180
17181 type = check_tag_decl (&decl_specifiers,
17182 /*explicit_type_instantiation_p=*/true);
17183 /* Turn access control back on for names used during
17184 template instantiation. */
17185 pop_deferring_access_checks ();
17186 if (type)
17187 do_type_instantiation (type, extension_specifier,
17188 /*complain=*/tf_error);
17189 }
17190 else
17191 {
17192 cp_declarator *declarator;
17193 tree decl;
17194
17195 /* Parse the declarator. */
17196 declarator
17197 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17198 CP_PARSER_FLAGS_NONE,
17199 /*ctor_dtor_or_conv_p=*/NULL,
17200 /*parenthesized_p=*/NULL,
17201 /*member_p=*/false,
17202 /*friend_p=*/false,
17203 /*static_p=*/false);
17204 if (declares_class_or_enum & 2)
17205 cp_parser_check_for_definition_in_return_type (declarator,
17206 decl_specifiers.type,
17207 decl_specifiers.locations[ds_type_spec]);
17208 if (declarator != cp_error_declarator)
17209 {
17210 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17211 permerror (decl_specifiers.locations[ds_inline],
17212 "explicit instantiation shall not use"
17213 " %<inline%> specifier");
17214 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17215 permerror (decl_specifiers.locations[ds_constexpr],
17216 "explicit instantiation shall not use"
17217 " %<constexpr%> specifier");
17218
17219 decl = grokdeclarator (declarator, &decl_specifiers,
17220 NORMAL, 0, &decl_specifiers.attributes);
17221 /* Turn access control back on for names used during
17222 template instantiation. */
17223 pop_deferring_access_checks ();
17224 /* Do the explicit instantiation. */
17225 do_decl_instantiation (decl, extension_specifier);
17226 }
17227 else
17228 {
17229 pop_deferring_access_checks ();
17230 /* Skip the body of the explicit instantiation. */
17231 cp_parser_skip_to_end_of_statement (parser);
17232 }
17233 }
17234 /* We're done with the instantiation. */
17235 end_explicit_instantiation ();
17236
17237 cp_parser_consume_semicolon_at_end_of_statement (parser);
17238
17239 timevar_pop (TV_TEMPLATE_INST);
17240 }
17241
17242 /* Parse an explicit-specialization.
17243
17244 explicit-specialization:
17245 template < > declaration
17246
17247 Although the standard says `declaration', what it really means is:
17248
17249 explicit-specialization:
17250 template <> decl-specifier [opt] init-declarator [opt] ;
17251 template <> function-definition
17252 template <> explicit-specialization
17253 template <> template-declaration */
17254
17255 static void
17256 cp_parser_explicit_specialization (cp_parser* parser)
17257 {
17258 bool need_lang_pop;
17259 cp_token *token = cp_lexer_peek_token (parser->lexer);
17260
17261 /* Look for the `template' keyword. */
17262 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17263 /* Look for the `<'. */
17264 cp_parser_require (parser, CPP_LESS, RT_LESS);
17265 /* Look for the `>'. */
17266 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17267 /* We have processed another parameter list. */
17268 ++parser->num_template_parameter_lists;
17269 /* [temp]
17270
17271 A template ... explicit specialization ... shall not have C
17272 linkage. */
17273 if (current_lang_name == lang_name_c)
17274 {
17275 error_at (token->location, "template specialization with C linkage");
17276 maybe_show_extern_c_location ();
17277 /* Give it C++ linkage to avoid confusing other parts of the
17278 front end. */
17279 push_lang_context (lang_name_cplusplus);
17280 need_lang_pop = true;
17281 }
17282 else
17283 need_lang_pop = false;
17284 /* Let the front end know that we are beginning a specialization. */
17285 if (!begin_specialization ())
17286 {
17287 end_specialization ();
17288 return;
17289 }
17290
17291 /* If the next keyword is `template', we need to figure out whether
17292 or not we're looking a template-declaration. */
17293 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17294 {
17295 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17296 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17297 cp_parser_template_declaration_after_export (parser,
17298 /*member_p=*/false);
17299 else
17300 cp_parser_explicit_specialization (parser);
17301 }
17302 else
17303 /* Parse the dependent declaration. */
17304 cp_parser_single_declaration (parser,
17305 /*checks=*/NULL,
17306 /*member_p=*/false,
17307 /*explicit_specialization_p=*/true,
17308 /*friend_p=*/NULL);
17309 /* We're done with the specialization. */
17310 end_specialization ();
17311 /* For the erroneous case of a template with C linkage, we pushed an
17312 implicit C++ linkage scope; exit that scope now. */
17313 if (need_lang_pop)
17314 pop_lang_context ();
17315 /* We're done with this parameter list. */
17316 --parser->num_template_parameter_lists;
17317 }
17318
17319 /* Parse a type-specifier.
17320
17321 type-specifier:
17322 simple-type-specifier
17323 class-specifier
17324 enum-specifier
17325 elaborated-type-specifier
17326 cv-qualifier
17327
17328 GNU Extension:
17329
17330 type-specifier:
17331 __complex__
17332
17333 Returns a representation of the type-specifier. For a
17334 class-specifier, enum-specifier, or elaborated-type-specifier, a
17335 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17336
17337 The parser flags FLAGS is used to control type-specifier parsing.
17338
17339 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17340 in a decl-specifier-seq.
17341
17342 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17343 class-specifier, enum-specifier, or elaborated-type-specifier, then
17344 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17345 if a type is declared; 2 if it is defined. Otherwise, it is set to
17346 zero.
17347
17348 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17349 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17350 is set to FALSE. */
17351
17352 static tree
17353 cp_parser_type_specifier (cp_parser* parser,
17354 cp_parser_flags flags,
17355 cp_decl_specifier_seq *decl_specs,
17356 bool is_declaration,
17357 int* declares_class_or_enum,
17358 bool* is_cv_qualifier)
17359 {
17360 tree type_spec = NULL_TREE;
17361 cp_token *token;
17362 enum rid keyword;
17363 cp_decl_spec ds = ds_last;
17364
17365 /* Assume this type-specifier does not declare a new type. */
17366 if (declares_class_or_enum)
17367 *declares_class_or_enum = 0;
17368 /* And that it does not specify a cv-qualifier. */
17369 if (is_cv_qualifier)
17370 *is_cv_qualifier = false;
17371 /* Peek at the next token. */
17372 token = cp_lexer_peek_token (parser->lexer);
17373
17374 /* If we're looking at a keyword, we can use that to guide the
17375 production we choose. */
17376 keyword = token->keyword;
17377 switch (keyword)
17378 {
17379 case RID_ENUM:
17380 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17381 goto elaborated_type_specifier;
17382
17383 /* Look for the enum-specifier. */
17384 type_spec = cp_parser_enum_specifier (parser);
17385 /* If that worked, we're done. */
17386 if (type_spec)
17387 {
17388 if (declares_class_or_enum)
17389 *declares_class_or_enum = 2;
17390 if (decl_specs)
17391 cp_parser_set_decl_spec_type (decl_specs,
17392 type_spec,
17393 token,
17394 /*type_definition_p=*/true);
17395 return type_spec;
17396 }
17397 else
17398 goto elaborated_type_specifier;
17399
17400 /* Any of these indicate either a class-specifier, or an
17401 elaborated-type-specifier. */
17402 case RID_CLASS:
17403 case RID_STRUCT:
17404 case RID_UNION:
17405 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17406 goto elaborated_type_specifier;
17407
17408 /* Parse tentatively so that we can back up if we don't find a
17409 class-specifier. */
17410 cp_parser_parse_tentatively (parser);
17411 /* Look for the class-specifier. */
17412 type_spec = cp_parser_class_specifier (parser);
17413 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17414 /* If that worked, we're done. */
17415 if (cp_parser_parse_definitely (parser))
17416 {
17417 if (declares_class_or_enum)
17418 *declares_class_or_enum = 2;
17419 if (decl_specs)
17420 cp_parser_set_decl_spec_type (decl_specs,
17421 type_spec,
17422 token,
17423 /*type_definition_p=*/true);
17424 return type_spec;
17425 }
17426
17427 /* Fall through. */
17428 elaborated_type_specifier:
17429 /* We're declaring (not defining) a class or enum. */
17430 if (declares_class_or_enum)
17431 *declares_class_or_enum = 1;
17432
17433 /* Fall through. */
17434 case RID_TYPENAME:
17435 /* Look for an elaborated-type-specifier. */
17436 type_spec
17437 = (cp_parser_elaborated_type_specifier
17438 (parser,
17439 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17440 is_declaration));
17441 if (decl_specs)
17442 cp_parser_set_decl_spec_type (decl_specs,
17443 type_spec,
17444 token,
17445 /*type_definition_p=*/false);
17446 return type_spec;
17447
17448 case RID_CONST:
17449 ds = ds_const;
17450 if (is_cv_qualifier)
17451 *is_cv_qualifier = true;
17452 break;
17453
17454 case RID_VOLATILE:
17455 ds = ds_volatile;
17456 if (is_cv_qualifier)
17457 *is_cv_qualifier = true;
17458 break;
17459
17460 case RID_RESTRICT:
17461 ds = ds_restrict;
17462 if (is_cv_qualifier)
17463 *is_cv_qualifier = true;
17464 break;
17465
17466 case RID_COMPLEX:
17467 /* The `__complex__' keyword is a GNU extension. */
17468 ds = ds_complex;
17469 break;
17470
17471 default:
17472 break;
17473 }
17474
17475 /* Handle simple keywords. */
17476 if (ds != ds_last)
17477 {
17478 if (decl_specs)
17479 {
17480 set_and_check_decl_spec_loc (decl_specs, ds, token);
17481 decl_specs->any_specifiers_p = true;
17482 }
17483 return cp_lexer_consume_token (parser->lexer)->u.value;
17484 }
17485
17486 /* If we do not already have a type-specifier, assume we are looking
17487 at a simple-type-specifier. */
17488 type_spec = cp_parser_simple_type_specifier (parser,
17489 decl_specs,
17490 flags);
17491
17492 /* If we didn't find a type-specifier, and a type-specifier was not
17493 optional in this context, issue an error message. */
17494 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17495 {
17496 cp_parser_error (parser, "expected type specifier");
17497 return error_mark_node;
17498 }
17499
17500 return type_spec;
17501 }
17502
17503 /* Parse a simple-type-specifier.
17504
17505 simple-type-specifier:
17506 :: [opt] nested-name-specifier [opt] type-name
17507 :: [opt] nested-name-specifier template template-id
17508 char
17509 wchar_t
17510 bool
17511 short
17512 int
17513 long
17514 signed
17515 unsigned
17516 float
17517 double
17518 void
17519
17520 C++11 Extension:
17521
17522 simple-type-specifier:
17523 auto
17524 decltype ( expression )
17525 char16_t
17526 char32_t
17527 __underlying_type ( type-id )
17528
17529 C++17 extension:
17530
17531 nested-name-specifier(opt) template-name
17532
17533 GNU Extension:
17534
17535 simple-type-specifier:
17536 __int128
17537 __typeof__ unary-expression
17538 __typeof__ ( type-id )
17539 __typeof__ ( type-id ) { initializer-list , [opt] }
17540
17541 Concepts Extension:
17542
17543 simple-type-specifier:
17544 constrained-type-specifier
17545
17546 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17547 appropriately updated. */
17548
17549 static tree
17550 cp_parser_simple_type_specifier (cp_parser* parser,
17551 cp_decl_specifier_seq *decl_specs,
17552 cp_parser_flags flags)
17553 {
17554 tree type = NULL_TREE;
17555 cp_token *token;
17556 int idx;
17557
17558 /* Peek at the next token. */
17559 token = cp_lexer_peek_token (parser->lexer);
17560
17561 /* If we're looking at a keyword, things are easy. */
17562 switch (token->keyword)
17563 {
17564 case RID_CHAR:
17565 if (decl_specs)
17566 decl_specs->explicit_char_p = true;
17567 type = char_type_node;
17568 break;
17569 case RID_CHAR8:
17570 type = char8_type_node;
17571 break;
17572 case RID_CHAR16:
17573 type = char16_type_node;
17574 break;
17575 case RID_CHAR32:
17576 type = char32_type_node;
17577 break;
17578 case RID_WCHAR:
17579 type = wchar_type_node;
17580 break;
17581 case RID_BOOL:
17582 type = boolean_type_node;
17583 break;
17584 case RID_SHORT:
17585 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17586 type = short_integer_type_node;
17587 break;
17588 case RID_INT:
17589 if (decl_specs)
17590 decl_specs->explicit_int_p = true;
17591 type = integer_type_node;
17592 break;
17593 case RID_INT_N_0:
17594 case RID_INT_N_1:
17595 case RID_INT_N_2:
17596 case RID_INT_N_3:
17597 idx = token->keyword - RID_INT_N_0;
17598 if (! int_n_enabled_p [idx])
17599 break;
17600 if (decl_specs)
17601 {
17602 decl_specs->explicit_intN_p = true;
17603 decl_specs->int_n_idx = idx;
17604 }
17605 type = int_n_trees [idx].signed_type;
17606 break;
17607 case RID_LONG:
17608 if (decl_specs)
17609 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17610 type = long_integer_type_node;
17611 break;
17612 case RID_SIGNED:
17613 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17614 type = integer_type_node;
17615 break;
17616 case RID_UNSIGNED:
17617 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17618 type = unsigned_type_node;
17619 break;
17620 case RID_FLOAT:
17621 type = float_type_node;
17622 break;
17623 case RID_DOUBLE:
17624 type = double_type_node;
17625 break;
17626 case RID_VOID:
17627 type = void_type_node;
17628 break;
17629
17630 case RID_AUTO:
17631 maybe_warn_cpp0x (CPP0X_AUTO);
17632 if (parser->auto_is_implicit_function_template_parm_p)
17633 {
17634 /* The 'auto' might be the placeholder return type for a function decl
17635 with trailing return type. */
17636 bool have_trailing_return_fn_decl = false;
17637
17638 cp_parser_parse_tentatively (parser);
17639 cp_lexer_consume_token (parser->lexer);
17640 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17641 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17642 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17643 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17644 {
17645 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17646 {
17647 cp_lexer_consume_token (parser->lexer);
17648 cp_parser_skip_to_closing_parenthesis (parser,
17649 /*recovering*/false,
17650 /*or_comma*/false,
17651 /*consume_paren*/true);
17652 continue;
17653 }
17654
17655 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17656 {
17657 have_trailing_return_fn_decl = true;
17658 break;
17659 }
17660
17661 cp_lexer_consume_token (parser->lexer);
17662 }
17663 cp_parser_abort_tentative_parse (parser);
17664
17665 if (have_trailing_return_fn_decl)
17666 {
17667 type = make_auto ();
17668 break;
17669 }
17670
17671 if (cxx_dialect >= cxx14)
17672 {
17673 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17674 type = TREE_TYPE (type);
17675 }
17676 else
17677 type = error_mark_node;
17678
17679 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17680 {
17681 if (cxx_dialect < cxx14)
17682 error_at (token->location,
17683 "use of %<auto%> in lambda parameter declaration "
17684 "only available with "
17685 "%<-std=c++14%> or %<-std=gnu++14%>");
17686 }
17687 else if (cxx_dialect < cxx14)
17688 error_at (token->location,
17689 "use of %<auto%> in parameter declaration "
17690 "only available with "
17691 "%<-std=c++14%> or %<-std=gnu++14%>");
17692 else if (!flag_concepts)
17693 pedwarn (token->location, 0,
17694 "use of %<auto%> in parameter declaration "
17695 "only available with %<-fconcepts%>");
17696 }
17697 else
17698 type = make_auto ();
17699 break;
17700
17701 case RID_DECLTYPE:
17702 /* Since DR 743, decltype can either be a simple-type-specifier by
17703 itself or begin a nested-name-specifier. Parsing it will replace
17704 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17705 handling below decide what to do. */
17706 cp_parser_decltype (parser);
17707 cp_lexer_set_token_position (parser->lexer, token);
17708 break;
17709
17710 case RID_TYPEOF:
17711 /* Consume the `typeof' token. */
17712 cp_lexer_consume_token (parser->lexer);
17713 /* Parse the operand to `typeof'. */
17714 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17715 /* If it is not already a TYPE, take its type. */
17716 if (!TYPE_P (type))
17717 type = finish_typeof (type);
17718
17719 if (decl_specs)
17720 cp_parser_set_decl_spec_type (decl_specs, type,
17721 token,
17722 /*type_definition_p=*/false);
17723
17724 return type;
17725
17726 case RID_UNDERLYING_TYPE:
17727 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17728 if (decl_specs)
17729 cp_parser_set_decl_spec_type (decl_specs, type,
17730 token,
17731 /*type_definition_p=*/false);
17732
17733 return type;
17734
17735 case RID_BASES:
17736 case RID_DIRECT_BASES:
17737 type = cp_parser_trait_expr (parser, token->keyword);
17738 if (decl_specs)
17739 cp_parser_set_decl_spec_type (decl_specs, type,
17740 token,
17741 /*type_definition_p=*/false);
17742 return type;
17743 default:
17744 break;
17745 }
17746
17747 /* If token is an already-parsed decltype not followed by ::,
17748 it's a simple-type-specifier. */
17749 if (token->type == CPP_DECLTYPE
17750 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17751 {
17752 type = saved_checks_value (token->u.tree_check_value);
17753 if (decl_specs)
17754 {
17755 cp_parser_set_decl_spec_type (decl_specs, type,
17756 token,
17757 /*type_definition_p=*/false);
17758 /* Remember that we are handling a decltype in order to
17759 implement the resolution of DR 1510 when the argument
17760 isn't instantiation dependent. */
17761 decl_specs->decltype_p = true;
17762 }
17763 cp_lexer_consume_token (parser->lexer);
17764 return type;
17765 }
17766
17767 /* If the type-specifier was for a built-in type, we're done. */
17768 if (type)
17769 {
17770 /* Record the type. */
17771 if (decl_specs
17772 && (token->keyword != RID_SIGNED
17773 && token->keyword != RID_UNSIGNED
17774 && token->keyword != RID_SHORT
17775 && token->keyword != RID_LONG))
17776 cp_parser_set_decl_spec_type (decl_specs,
17777 type,
17778 token,
17779 /*type_definition_p=*/false);
17780 if (decl_specs)
17781 decl_specs->any_specifiers_p = true;
17782
17783 /* Consume the token. */
17784 cp_lexer_consume_token (parser->lexer);
17785
17786 if (type == error_mark_node)
17787 return error_mark_node;
17788
17789 /* There is no valid C++ program where a non-template type is
17790 followed by a "<". That usually indicates that the user thought
17791 that the type was a template. */
17792 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17793 token->location);
17794
17795 return TYPE_NAME (type);
17796 }
17797
17798 /* The type-specifier must be a user-defined type. */
17799 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17800 {
17801 bool qualified_p;
17802 bool global_p;
17803 const bool typename_p = (cxx_dialect >= cxx2a
17804 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
17805
17806 /* Don't gobble tokens or issue error messages if this is an
17807 optional type-specifier. */
17808 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17809 cp_parser_parse_tentatively (parser);
17810
17811 token = cp_lexer_peek_token (parser->lexer);
17812
17813 /* Look for the optional `::' operator. */
17814 global_p
17815 = (cp_parser_global_scope_opt (parser,
17816 /*current_scope_valid_p=*/false)
17817 != NULL_TREE);
17818 /* Look for the nested-name specifier. */
17819 qualified_p
17820 = (cp_parser_nested_name_specifier_opt (parser,
17821 /*typename_keyword_p=*/false,
17822 /*check_dependency_p=*/true,
17823 /*type_p=*/false,
17824 /*is_declaration=*/false)
17825 != NULL_TREE);
17826 /* If we have seen a nested-name-specifier, and the next token
17827 is `template', then we are using the template-id production. */
17828 if (parser->scope
17829 && cp_parser_optional_template_keyword (parser))
17830 {
17831 /* Look for the template-id. */
17832 type = cp_parser_template_id (parser,
17833 /*template_keyword_p=*/true,
17834 /*check_dependency_p=*/true,
17835 none_type,
17836 /*is_declaration=*/false);
17837 /* If the template-id did not name a type, we are out of
17838 luck. */
17839 if (TREE_CODE (type) != TYPE_DECL)
17840 {
17841 /* ...unless we pretend we have seen 'typename'. */
17842 if (typename_p)
17843 type = cp_parser_make_typename_type (parser, type,
17844 token->location);
17845 else
17846 {
17847 cp_parser_error (parser, "expected template-id for type");
17848 type = NULL_TREE;
17849 }
17850 }
17851 }
17852 /* Otherwise, look for a type-name. */
17853 else
17854 type = cp_parser_type_name (parser, (qualified_p && typename_p));
17855
17856 /* Keep track of all name-lookups performed in class scopes. */
17857 if (type
17858 && !global_p
17859 && !qualified_p
17860 && TREE_CODE (type) == TYPE_DECL
17861 && identifier_p (DECL_NAME (type)))
17862 maybe_note_name_used_in_class (DECL_NAME (type), type);
17863 /* If it didn't work out, we don't have a TYPE. */
17864 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17865 && !cp_parser_parse_definitely (parser))
17866 type = NULL_TREE;
17867 if (!type && cxx_dialect >= cxx17)
17868 {
17869 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17870 cp_parser_parse_tentatively (parser);
17871
17872 cp_parser_global_scope_opt (parser,
17873 /*current_scope_valid_p=*/false);
17874 cp_parser_nested_name_specifier_opt (parser,
17875 /*typename_keyword_p=*/false,
17876 /*check_dependency_p=*/true,
17877 /*type_p=*/false,
17878 /*is_declaration=*/false);
17879 tree name = cp_parser_identifier (parser);
17880 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17881 && parser->scope != error_mark_node)
17882 {
17883 tree tmpl = cp_parser_lookup_name (parser, name,
17884 none_type,
17885 /*is_template=*/false,
17886 /*is_namespace=*/false,
17887 /*check_dependency=*/true,
17888 /*ambiguous_decls=*/NULL,
17889 token->location);
17890 if (tmpl && tmpl != error_mark_node
17891 && (DECL_CLASS_TEMPLATE_P (tmpl)
17892 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17893 type = make_template_placeholder (tmpl);
17894 else
17895 {
17896 type = error_mark_node;
17897 if (!cp_parser_simulate_error (parser))
17898 cp_parser_name_lookup_error (parser, name, tmpl,
17899 NLE_TYPE, token->location);
17900 }
17901 }
17902 else
17903 type = error_mark_node;
17904
17905 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17906 && !cp_parser_parse_definitely (parser))
17907 type = NULL_TREE;
17908 }
17909 if (type && decl_specs)
17910 cp_parser_set_decl_spec_type (decl_specs, type,
17911 token,
17912 /*type_definition_p=*/false);
17913 }
17914
17915 /* If we didn't get a type-name, issue an error message. */
17916 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17917 {
17918 cp_parser_error (parser, "expected type-name");
17919 return error_mark_node;
17920 }
17921
17922 if (type && type != error_mark_node)
17923 {
17924 /* See if TYPE is an Objective-C type, and if so, parse and
17925 accept any protocol references following it. Do this before
17926 the cp_parser_check_for_invalid_template_id() call, because
17927 Objective-C types can be followed by '<...>' which would
17928 enclose protocol names rather than template arguments, and so
17929 everything is fine. */
17930 if (c_dialect_objc () && !parser->scope
17931 && (objc_is_id (type) || objc_is_class_name (type)))
17932 {
17933 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17934 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17935
17936 /* Clobber the "unqualified" type previously entered into
17937 DECL_SPECS with the new, improved protocol-qualified version. */
17938 if (decl_specs)
17939 decl_specs->type = qual_type;
17940
17941 return qual_type;
17942 }
17943
17944 /* There is no valid C++ program where a non-template type is
17945 followed by a "<". That usually indicates that the user
17946 thought that the type was a template. */
17947 cp_parser_check_for_invalid_template_id (parser, type,
17948 none_type,
17949 token->location);
17950 }
17951
17952 return type;
17953 }
17954
17955 /* Parse a type-name.
17956
17957 type-name:
17958 class-name
17959 enum-name
17960 typedef-name
17961 simple-template-id [in c++0x]
17962
17963 enum-name:
17964 identifier
17965
17966 typedef-name:
17967 identifier
17968
17969 Concepts:
17970
17971 type-name:
17972 concept-name
17973 partial-concept-id
17974
17975 concept-name:
17976 identifier
17977
17978 Returns a TYPE_DECL for the type. */
17979
17980 static tree
17981 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17982 {
17983 tree type_decl;
17984
17985 /* We can't know yet whether it is a class-name or not. */
17986 cp_parser_parse_tentatively (parser);
17987 /* Try a class-name. */
17988 type_decl = cp_parser_class_name (parser,
17989 typename_keyword_p,
17990 /*template_keyword_p=*/false,
17991 none_type,
17992 /*check_dependency_p=*/true,
17993 /*class_head_p=*/false,
17994 /*is_declaration=*/false);
17995 /* If it's not a class-name, keep looking. */
17996 if (!cp_parser_parse_definitely (parser))
17997 {
17998 if (cxx_dialect < cxx11)
17999 /* It must be a typedef-name or an enum-name. */
18000 return cp_parser_nonclass_name (parser);
18001
18002 cp_parser_parse_tentatively (parser);
18003 /* It is either a simple-template-id representing an
18004 instantiation of an alias template... */
18005 type_decl = cp_parser_template_id (parser,
18006 /*template_keyword_p=*/false,
18007 /*check_dependency_p=*/true,
18008 none_type,
18009 /*is_declaration=*/false);
18010 /* Note that this must be an instantiation of an alias template
18011 because [temp.names]/6 says:
18012
18013 A template-id that names an alias template specialization
18014 is a type-name.
18015
18016 Whereas [temp.names]/7 says:
18017
18018 A simple-template-id that names a class template
18019 specialization is a class-name.
18020
18021 With concepts, this could also be a partial-concept-id that
18022 declares a non-type template parameter. */
18023 if (type_decl != NULL_TREE
18024 && TREE_CODE (type_decl) == TYPE_DECL
18025 && TYPE_DECL_ALIAS_P (type_decl))
18026 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
18027 else if (is_constrained_parameter (type_decl))
18028 /* Don't do anything. */ ;
18029 else
18030 cp_parser_simulate_error (parser);
18031
18032 if (!cp_parser_parse_definitely (parser))
18033 /* ... Or a typedef-name or an enum-name. */
18034 return cp_parser_nonclass_name (parser);
18035 }
18036
18037 return type_decl;
18038 }
18039
18040 /* Check if DECL and ARGS can form a constrained-type-specifier.
18041 If ARGS is non-null, we try to form a concept check of the
18042 form DECL<?, ARGS> where ? is a wildcard that matches any
18043 kind of template argument. If ARGS is NULL, then we try to
18044 form a concept check of the form DECL<?>. */
18045
18046 static tree
18047 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
18048 tree decl, tree args)
18049 {
18050 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
18051
18052 /* If we a constrained-type-specifier cannot be deduced. */
18053 if (parser->prevent_constrained_type_specifiers)
18054 return NULL_TREE;
18055
18056 /* A constrained type specifier can only be found in an
18057 overload set or as a reference to a template declaration.
18058
18059 FIXME: This might be masking a bug. It's possible that
18060 that the deduction below is causing template specializations
18061 to be formed with the wildcard as an argument. */
18062 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
18063 return NULL_TREE;
18064
18065 /* Try to build a call expression that evaluates the
18066 concept. This can fail if the overload set refers
18067 only to non-templates. */
18068 tree placeholder = build_nt (WILDCARD_DECL);
18069 tree check = build_concept_check (decl, placeholder, args);
18070 if (check == error_mark_node)
18071 return NULL_TREE;
18072
18073 /* Deduce the checked constraint and the prototype parameter.
18074
18075 FIXME: In certain cases, failure to deduce should be a
18076 diagnosable error. */
18077 tree conc;
18078 tree proto;
18079 if (!deduce_constrained_parameter (check, conc, proto))
18080 return NULL_TREE;
18081
18082 /* In template parameter scope, this results in a constrained
18083 parameter. Return a descriptor of that parm. */
18084 if (processing_template_parmlist)
18085 return build_constrained_parameter (conc, proto, args);
18086
18087 /* In a parameter-declaration-clause, constrained-type
18088 specifiers result in invented template parameters. */
18089 if (parser->auto_is_implicit_function_template_parm_p)
18090 {
18091 tree x = build_constrained_parameter (conc, proto, args);
18092 return synthesize_implicit_template_parm (parser, x);
18093 }
18094 else
18095 {
18096 /* Otherwise, we're in a context where the constrained
18097 type name is deduced and the constraint applies
18098 after deduction. */
18099 return make_constrained_auto (conc, args);
18100 }
18101
18102 return NULL_TREE;
18103 }
18104
18105 /* If DECL refers to a concept, return a TYPE_DECL representing
18106 the result of using the constrained type specifier in the
18107 current context. DECL refers to a concept if
18108
18109 - it is an overload set containing a function concept taking a single
18110 type argument, or
18111
18112 - it is a variable concept taking a single type argument. */
18113
18114 static tree
18115 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
18116 {
18117 if (flag_concepts
18118 && (TREE_CODE (decl) == OVERLOAD
18119 || BASELINK_P (decl)
18120 || variable_concept_p (decl)))
18121 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
18122 else
18123 return NULL_TREE;
18124 }
18125
18126 /* Check if DECL and ARGS form a partial-concept-id. If so,
18127 assign ID to the resulting constrained placeholder.
18128
18129 Returns true if the partial-concept-id designates a placeholder
18130 and false otherwise. Note that *id is set to NULL_TREE in
18131 this case. */
18132
18133 static tree
18134 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
18135 {
18136 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
18137 }
18138
18139 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18140 or a concept-name.
18141
18142 enum-name:
18143 identifier
18144
18145 typedef-name:
18146 identifier
18147
18148 concept-name:
18149 identifier
18150
18151 Returns a TYPE_DECL for the type. */
18152
18153 static tree
18154 cp_parser_nonclass_name (cp_parser* parser)
18155 {
18156 tree type_decl;
18157 tree identifier;
18158
18159 cp_token *token = cp_lexer_peek_token (parser->lexer);
18160 identifier = cp_parser_identifier (parser);
18161 if (identifier == error_mark_node)
18162 return error_mark_node;
18163
18164 /* Look up the type-name. */
18165 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18166
18167 type_decl = strip_using_decl (type_decl);
18168
18169 /* If we found an overload set, then it may refer to a concept-name. */
18170 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
18171 type_decl = decl;
18172
18173 if (TREE_CODE (type_decl) != TYPE_DECL
18174 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18175 {
18176 /* See if this is an Objective-C type. */
18177 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18178 tree type = objc_get_protocol_qualified_type (identifier, protos);
18179 if (type)
18180 type_decl = TYPE_NAME (type);
18181 }
18182
18183 /* Issue an error if we did not find a type-name. */
18184 if (TREE_CODE (type_decl) != TYPE_DECL
18185 /* In Objective-C, we have the complication that class names are
18186 normally type names and start declarations (eg, the
18187 "NSObject" in "NSObject *object;"), but can be used in an
18188 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18189 is an expression. So, a classname followed by a dot is not a
18190 valid type-name. */
18191 || (objc_is_class_name (TREE_TYPE (type_decl))
18192 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18193 {
18194 if (!cp_parser_simulate_error (parser))
18195 cp_parser_name_lookup_error (parser, identifier, type_decl,
18196 NLE_TYPE, token->location);
18197 return error_mark_node;
18198 }
18199 /* Remember that the name was used in the definition of the
18200 current class so that we can check later to see if the
18201 meaning would have been different after the class was
18202 entirely defined. */
18203 else if (type_decl != error_mark_node
18204 && !parser->scope)
18205 maybe_note_name_used_in_class (identifier, type_decl);
18206
18207 return type_decl;
18208 }
18209
18210 /* Parse an elaborated-type-specifier. Note that the grammar given
18211 here incorporates the resolution to DR68.
18212
18213 elaborated-type-specifier:
18214 class-key :: [opt] nested-name-specifier [opt] identifier
18215 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18216 enum-key :: [opt] nested-name-specifier [opt] identifier
18217 typename :: [opt] nested-name-specifier identifier
18218 typename :: [opt] nested-name-specifier template [opt]
18219 template-id
18220
18221 GNU extension:
18222
18223 elaborated-type-specifier:
18224 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18225 class-key attributes :: [opt] nested-name-specifier [opt]
18226 template [opt] template-id
18227 enum attributes :: [opt] nested-name-specifier [opt] identifier
18228
18229 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18230 declared `friend'. If IS_DECLARATION is TRUE, then this
18231 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18232 something is being declared.
18233
18234 Returns the TYPE specified. */
18235
18236 static tree
18237 cp_parser_elaborated_type_specifier (cp_parser* parser,
18238 bool is_friend,
18239 bool is_declaration)
18240 {
18241 enum tag_types tag_type;
18242 tree identifier;
18243 tree type = NULL_TREE;
18244 tree attributes = NULL_TREE;
18245 tree globalscope;
18246 cp_token *token = NULL;
18247
18248 /* See if we're looking at the `enum' keyword. */
18249 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18250 {
18251 /* Consume the `enum' token. */
18252 cp_lexer_consume_token (parser->lexer);
18253 /* Remember that it's an enumeration type. */
18254 tag_type = enum_type;
18255 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18256 enums) is used here. */
18257 cp_token *token = cp_lexer_peek_token (parser->lexer);
18258 if (cp_parser_is_keyword (token, RID_CLASS)
18259 || cp_parser_is_keyword (token, RID_STRUCT))
18260 {
18261 gcc_rich_location richloc (token->location);
18262 richloc.add_range (input_location);
18263 richloc.add_fixit_remove ();
18264 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18265 "a scoped enum must not use the %qD keyword",
18266 token->u.value);
18267 /* Consume the `struct' or `class' and parse it anyway. */
18268 cp_lexer_consume_token (parser->lexer);
18269 }
18270 /* Parse the attributes. */
18271 attributes = cp_parser_attributes_opt (parser);
18272 }
18273 /* Or, it might be `typename'. */
18274 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18275 RID_TYPENAME))
18276 {
18277 /* Consume the `typename' token. */
18278 cp_lexer_consume_token (parser->lexer);
18279 /* Remember that it's a `typename' type. */
18280 tag_type = typename_type;
18281 }
18282 /* Otherwise it must be a class-key. */
18283 else
18284 {
18285 tag_type = cp_parser_class_key (parser);
18286 if (tag_type == none_type)
18287 return error_mark_node;
18288 /* Parse the attributes. */
18289 attributes = cp_parser_attributes_opt (parser);
18290 }
18291
18292 /* Look for the `::' operator. */
18293 globalscope = cp_parser_global_scope_opt (parser,
18294 /*current_scope_valid_p=*/false);
18295 /* Look for the nested-name-specifier. */
18296 tree nested_name_specifier;
18297 if (tag_type == typename_type && !globalscope)
18298 {
18299 nested_name_specifier
18300 = cp_parser_nested_name_specifier (parser,
18301 /*typename_keyword_p=*/true,
18302 /*check_dependency_p=*/true,
18303 /*type_p=*/true,
18304 is_declaration);
18305 if (!nested_name_specifier)
18306 return error_mark_node;
18307 }
18308 else
18309 /* Even though `typename' is not present, the proposed resolution
18310 to Core Issue 180 says that in `class A<T>::B', `B' should be
18311 considered a type-name, even if `A<T>' is dependent. */
18312 nested_name_specifier
18313 = cp_parser_nested_name_specifier_opt (parser,
18314 /*typename_keyword_p=*/true,
18315 /*check_dependency_p=*/true,
18316 /*type_p=*/true,
18317 is_declaration);
18318 /* For everything but enumeration types, consider a template-id.
18319 For an enumeration type, consider only a plain identifier. */
18320 if (tag_type != enum_type)
18321 {
18322 bool template_p = false;
18323 tree decl;
18324
18325 /* Allow the `template' keyword. */
18326 template_p = cp_parser_optional_template_keyword (parser);
18327 /* If we didn't see `template', we don't know if there's a
18328 template-id or not. */
18329 if (!template_p)
18330 cp_parser_parse_tentatively (parser);
18331 /* The `template' keyword must follow a nested-name-specifier. */
18332 else if (!nested_name_specifier)
18333 {
18334 cp_parser_error (parser, "%<template%> must follow a nested-"
18335 "name-specifier");
18336 return error_mark_node;
18337 }
18338
18339 /* Parse the template-id. */
18340 token = cp_lexer_peek_token (parser->lexer);
18341 decl = cp_parser_template_id (parser, template_p,
18342 /*check_dependency_p=*/true,
18343 tag_type,
18344 is_declaration);
18345 /* If we didn't find a template-id, look for an ordinary
18346 identifier. */
18347 if (!template_p && !cp_parser_parse_definitely (parser))
18348 ;
18349 /* We can get here when cp_parser_template_id, called by
18350 cp_parser_class_name with tag_type == none_type, succeeds
18351 and caches a BASELINK. Then, when called again here,
18352 instead of failing and returning an error_mark_node
18353 returns it (see template/typename17.C in C++11).
18354 ??? Could we diagnose this earlier? */
18355 else if (tag_type == typename_type && BASELINK_P (decl))
18356 {
18357 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18358 type = error_mark_node;
18359 }
18360 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18361 in effect, then we must assume that, upon instantiation, the
18362 template will correspond to a class. */
18363 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18364 && tag_type == typename_type)
18365 type = make_typename_type (parser->scope, decl,
18366 typename_type,
18367 /*complain=*/tf_error);
18368 /* If the `typename' keyword is in effect and DECL is not a type
18369 decl, then type is non existent. */
18370 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18371 ;
18372 else if (TREE_CODE (decl) == TYPE_DECL)
18373 {
18374 type = check_elaborated_type_specifier (tag_type, decl,
18375 /*allow_template_p=*/true);
18376
18377 /* If the next token is a semicolon, this must be a specialization,
18378 instantiation, or friend declaration. Check the scope while we
18379 still know whether or not we had a nested-name-specifier. */
18380 if (type != error_mark_node
18381 && !nested_name_specifier && !is_friend
18382 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18383 check_unqualified_spec_or_inst (type, token->location);
18384 }
18385 else if (decl == error_mark_node)
18386 type = error_mark_node;
18387 }
18388
18389 if (!type)
18390 {
18391 token = cp_lexer_peek_token (parser->lexer);
18392 identifier = cp_parser_identifier (parser);
18393
18394 if (identifier == error_mark_node)
18395 {
18396 parser->scope = NULL_TREE;
18397 return error_mark_node;
18398 }
18399
18400 /* For a `typename', we needn't call xref_tag. */
18401 if (tag_type == typename_type
18402 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18403 return cp_parser_make_typename_type (parser, identifier,
18404 token->location);
18405
18406 /* Template parameter lists apply only if we are not within a
18407 function parameter list. */
18408 bool template_parm_lists_apply
18409 = parser->num_template_parameter_lists;
18410 if (template_parm_lists_apply)
18411 for (cp_binding_level *s = current_binding_level;
18412 s && s->kind != sk_template_parms;
18413 s = s->level_chain)
18414 if (s->kind == sk_function_parms)
18415 template_parm_lists_apply = false;
18416
18417 /* Look up a qualified name in the usual way. */
18418 if (parser->scope)
18419 {
18420 tree decl;
18421 tree ambiguous_decls;
18422
18423 decl = cp_parser_lookup_name (parser, identifier,
18424 tag_type,
18425 /*is_template=*/false,
18426 /*is_namespace=*/false,
18427 /*check_dependency=*/true,
18428 &ambiguous_decls,
18429 token->location);
18430
18431 /* If the lookup was ambiguous, an error will already have been
18432 issued. */
18433 if (ambiguous_decls)
18434 return error_mark_node;
18435
18436 /* If we are parsing friend declaration, DECL may be a
18437 TEMPLATE_DECL tree node here. However, we need to check
18438 whether this TEMPLATE_DECL results in valid code. Consider
18439 the following example:
18440
18441 namespace N {
18442 template <class T> class C {};
18443 }
18444 class X {
18445 template <class T> friend class N::C; // #1, valid code
18446 };
18447 template <class T> class Y {
18448 friend class N::C; // #2, invalid code
18449 };
18450
18451 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18452 name lookup of `N::C'. We see that friend declaration must
18453 be template for the code to be valid. Note that
18454 processing_template_decl does not work here since it is
18455 always 1 for the above two cases. */
18456
18457 decl = (cp_parser_maybe_treat_template_as_class
18458 (decl, /*tag_name_p=*/is_friend
18459 && template_parm_lists_apply));
18460
18461 if (TREE_CODE (decl) != TYPE_DECL)
18462 {
18463 cp_parser_diagnose_invalid_type_name (parser,
18464 identifier,
18465 token->location);
18466 return error_mark_node;
18467 }
18468
18469 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18470 {
18471 bool allow_template = (template_parm_lists_apply
18472 || DECL_SELF_REFERENCE_P (decl));
18473 type = check_elaborated_type_specifier (tag_type, decl,
18474 allow_template);
18475
18476 if (type == error_mark_node)
18477 return error_mark_node;
18478 }
18479
18480 /* Forward declarations of nested types, such as
18481
18482 class C1::C2;
18483 class C1::C2::C3;
18484
18485 are invalid unless all components preceding the final '::'
18486 are complete. If all enclosing types are complete, these
18487 declarations become merely pointless.
18488
18489 Invalid forward declarations of nested types are errors
18490 caught elsewhere in parsing. Those that are pointless arrive
18491 here. */
18492
18493 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18494 && !is_friend && !processing_explicit_instantiation)
18495 warning (0, "declaration %qD does not declare anything", decl);
18496
18497 type = TREE_TYPE (decl);
18498 }
18499 else
18500 {
18501 /* An elaborated-type-specifier sometimes introduces a new type and
18502 sometimes names an existing type. Normally, the rule is that it
18503 introduces a new type only if there is not an existing type of
18504 the same name already in scope. For example, given:
18505
18506 struct S {};
18507 void f() { struct S s; }
18508
18509 the `struct S' in the body of `f' is the same `struct S' as in
18510 the global scope; the existing definition is used. However, if
18511 there were no global declaration, this would introduce a new
18512 local class named `S'.
18513
18514 An exception to this rule applies to the following code:
18515
18516 namespace N { struct S; }
18517
18518 Here, the elaborated-type-specifier names a new type
18519 unconditionally; even if there is already an `S' in the
18520 containing scope this declaration names a new type.
18521 This exception only applies if the elaborated-type-specifier
18522 forms the complete declaration:
18523
18524 [class.name]
18525
18526 A declaration consisting solely of `class-key identifier ;' is
18527 either a redeclaration of the name in the current scope or a
18528 forward declaration of the identifier as a class name. It
18529 introduces the name into the current scope.
18530
18531 We are in this situation precisely when the next token is a `;'.
18532
18533 An exception to the exception is that a `friend' declaration does
18534 *not* name a new type; i.e., given:
18535
18536 struct S { friend struct T; };
18537
18538 `T' is not a new type in the scope of `S'.
18539
18540 Also, `new struct S' or `sizeof (struct S)' never results in the
18541 definition of a new type; a new type can only be declared in a
18542 declaration context. */
18543
18544 tag_scope ts;
18545 bool template_p;
18546
18547 if (is_friend)
18548 /* Friends have special name lookup rules. */
18549 ts = ts_within_enclosing_non_class;
18550 else if (is_declaration
18551 && cp_lexer_next_token_is (parser->lexer,
18552 CPP_SEMICOLON))
18553 /* This is a `class-key identifier ;' */
18554 ts = ts_current;
18555 else
18556 ts = ts_global;
18557
18558 template_p =
18559 (template_parm_lists_apply
18560 && (cp_parser_next_token_starts_class_definition_p (parser)
18561 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18562 /* An unqualified name was used to reference this type, so
18563 there were no qualifying templates. */
18564 if (template_parm_lists_apply
18565 && !cp_parser_check_template_parameters (parser,
18566 /*num_templates=*/0,
18567 /*template_id*/false,
18568 token->location,
18569 /*declarator=*/NULL))
18570 return error_mark_node;
18571 type = xref_tag (tag_type, identifier, ts, template_p);
18572 }
18573 }
18574
18575 if (type == error_mark_node)
18576 return error_mark_node;
18577
18578 /* Allow attributes on forward declarations of classes. */
18579 if (attributes)
18580 {
18581 if (TREE_CODE (type) == TYPENAME_TYPE)
18582 warning (OPT_Wattributes,
18583 "attributes ignored on uninstantiated type");
18584 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18585 && ! processing_explicit_instantiation)
18586 warning (OPT_Wattributes,
18587 "attributes ignored on template instantiation");
18588 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18589 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18590 else
18591 warning (OPT_Wattributes,
18592 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18593 }
18594
18595 if (tag_type != enum_type)
18596 {
18597 /* Indicate whether this class was declared as a `class' or as a
18598 `struct'. */
18599 if (CLASS_TYPE_P (type))
18600 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18601 cp_parser_check_class_key (tag_type, type);
18602 }
18603
18604 /* A "<" cannot follow an elaborated type specifier. If that
18605 happens, the user was probably trying to form a template-id. */
18606 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18607 token->location);
18608
18609 return type;
18610 }
18611
18612 /* Parse an enum-specifier.
18613
18614 enum-specifier:
18615 enum-head { enumerator-list [opt] }
18616 enum-head { enumerator-list , } [C++0x]
18617
18618 enum-head:
18619 enum-key identifier [opt] enum-base [opt]
18620 enum-key nested-name-specifier identifier enum-base [opt]
18621
18622 enum-key:
18623 enum
18624 enum class [C++0x]
18625 enum struct [C++0x]
18626
18627 enum-base: [C++0x]
18628 : type-specifier-seq
18629
18630 opaque-enum-specifier:
18631 enum-key identifier enum-base [opt] ;
18632
18633 GNU Extensions:
18634 enum-key attributes[opt] identifier [opt] enum-base [opt]
18635 { enumerator-list [opt] }attributes[opt]
18636 enum-key attributes[opt] identifier [opt] enum-base [opt]
18637 { enumerator-list, }attributes[opt] [C++0x]
18638
18639 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18640 if the token stream isn't an enum-specifier after all. */
18641
18642 static tree
18643 cp_parser_enum_specifier (cp_parser* parser)
18644 {
18645 tree identifier;
18646 tree type = NULL_TREE;
18647 tree prev_scope;
18648 tree nested_name_specifier = NULL_TREE;
18649 tree attributes;
18650 bool scoped_enum_p = false;
18651 bool has_underlying_type = false;
18652 bool nested_being_defined = false;
18653 bool new_value_list = false;
18654 bool is_new_type = false;
18655 bool is_unnamed = false;
18656 tree underlying_type = NULL_TREE;
18657 cp_token *type_start_token = NULL;
18658 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18659
18660 parser->colon_corrects_to_scope_p = false;
18661
18662 /* Parse tentatively so that we can back up if we don't find a
18663 enum-specifier. */
18664 cp_parser_parse_tentatively (parser);
18665
18666 /* Caller guarantees that the current token is 'enum', an identifier
18667 possibly follows, and the token after that is an opening brace.
18668 If we don't have an identifier, fabricate an anonymous name for
18669 the enumeration being defined. */
18670 cp_lexer_consume_token (parser->lexer);
18671
18672 /* Parse the "class" or "struct", which indicates a scoped
18673 enumeration type in C++0x. */
18674 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18675 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18676 {
18677 if (cxx_dialect < cxx11)
18678 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18679
18680 /* Consume the `struct' or `class' token. */
18681 cp_lexer_consume_token (parser->lexer);
18682
18683 scoped_enum_p = true;
18684 }
18685
18686 attributes = cp_parser_attributes_opt (parser);
18687
18688 /* Clear the qualification. */
18689 parser->scope = NULL_TREE;
18690 parser->qualifying_scope = NULL_TREE;
18691 parser->object_scope = NULL_TREE;
18692
18693 /* Figure out in what scope the declaration is being placed. */
18694 prev_scope = current_scope ();
18695
18696 type_start_token = cp_lexer_peek_token (parser->lexer);
18697
18698 push_deferring_access_checks (dk_no_check);
18699 nested_name_specifier
18700 = cp_parser_nested_name_specifier_opt (parser,
18701 /*typename_keyword_p=*/true,
18702 /*check_dependency_p=*/false,
18703 /*type_p=*/false,
18704 /*is_declaration=*/false);
18705
18706 if (nested_name_specifier)
18707 {
18708 tree name;
18709
18710 identifier = cp_parser_identifier (parser);
18711 name = cp_parser_lookup_name (parser, identifier,
18712 enum_type,
18713 /*is_template=*/false,
18714 /*is_namespace=*/false,
18715 /*check_dependency=*/true,
18716 /*ambiguous_decls=*/NULL,
18717 input_location);
18718 if (name && name != error_mark_node)
18719 {
18720 type = TREE_TYPE (name);
18721 if (TREE_CODE (type) == TYPENAME_TYPE)
18722 {
18723 /* Are template enums allowed in ISO? */
18724 if (template_parm_scope_p ())
18725 pedwarn (type_start_token->location, OPT_Wpedantic,
18726 "%qD is an enumeration template", name);
18727 /* ignore a typename reference, for it will be solved by name
18728 in start_enum. */
18729 type = NULL_TREE;
18730 }
18731 }
18732 else if (nested_name_specifier == error_mark_node)
18733 /* We already issued an error. */;
18734 else
18735 {
18736 error_at (type_start_token->location,
18737 "%qD does not name an enumeration in %qT",
18738 identifier, nested_name_specifier);
18739 nested_name_specifier = error_mark_node;
18740 }
18741 }
18742 else
18743 {
18744 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18745 identifier = cp_parser_identifier (parser);
18746 else
18747 {
18748 identifier = make_anon_name ();
18749 is_unnamed = true;
18750 if (scoped_enum_p)
18751 error_at (type_start_token->location,
18752 "unnamed scoped enum is not allowed");
18753 }
18754 }
18755 pop_deferring_access_checks ();
18756
18757 /* Check for the `:' that denotes a specified underlying type in C++0x.
18758 Note that a ':' could also indicate a bitfield width, however. */
18759 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18760 {
18761 cp_decl_specifier_seq type_specifiers;
18762
18763 /* Consume the `:'. */
18764 cp_lexer_consume_token (parser->lexer);
18765
18766 /* Parse the type-specifier-seq. */
18767 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
18768 /*is_declaration=*/false,
18769 /*is_trailing_return=*/false,
18770 &type_specifiers);
18771
18772 /* At this point this is surely not elaborated type specifier. */
18773 if (!cp_parser_parse_definitely (parser))
18774 return NULL_TREE;
18775
18776 if (cxx_dialect < cxx11)
18777 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18778
18779 has_underlying_type = true;
18780
18781 /* If that didn't work, stop. */
18782 if (type_specifiers.type != error_mark_node)
18783 {
18784 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18785 /*initialized=*/0, NULL);
18786 if (underlying_type == error_mark_node
18787 || check_for_bare_parameter_packs (underlying_type))
18788 underlying_type = NULL_TREE;
18789 }
18790 }
18791
18792 /* Look for the `{' but don't consume it yet. */
18793 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18794 {
18795 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18796 {
18797 cp_parser_error (parser, "expected %<{%>");
18798 if (has_underlying_type)
18799 {
18800 type = NULL_TREE;
18801 goto out;
18802 }
18803 }
18804 /* An opaque-enum-specifier must have a ';' here. */
18805 if ((scoped_enum_p || underlying_type)
18806 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18807 {
18808 cp_parser_error (parser, "expected %<;%> or %<{%>");
18809 if (has_underlying_type)
18810 {
18811 type = NULL_TREE;
18812 goto out;
18813 }
18814 }
18815 }
18816
18817 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18818 return NULL_TREE;
18819
18820 if (nested_name_specifier)
18821 {
18822 if (CLASS_TYPE_P (nested_name_specifier))
18823 {
18824 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18825 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18826 push_scope (nested_name_specifier);
18827 }
18828 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18829 {
18830 push_nested_namespace (nested_name_specifier);
18831 }
18832 }
18833
18834 /* Issue an error message if type-definitions are forbidden here. */
18835 if (!cp_parser_check_type_definition (parser))
18836 type = error_mark_node;
18837 else
18838 /* Create the new type. We do this before consuming the opening
18839 brace so the enum will be recorded as being on the line of its
18840 tag (or the 'enum' keyword, if there is no tag). */
18841 type = start_enum (identifier, type, underlying_type,
18842 attributes, scoped_enum_p, &is_new_type);
18843
18844 /* If the next token is not '{' it is an opaque-enum-specifier or an
18845 elaborated-type-specifier. */
18846 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18847 {
18848 timevar_push (TV_PARSE_ENUM);
18849 if (nested_name_specifier
18850 && nested_name_specifier != error_mark_node)
18851 {
18852 /* The following catches invalid code such as:
18853 enum class S<int>::E { A, B, C }; */
18854 if (!processing_specialization
18855 && CLASS_TYPE_P (nested_name_specifier)
18856 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18857 error_at (type_start_token->location, "cannot add an enumerator "
18858 "list to a template instantiation");
18859
18860 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18861 {
18862 error_at (type_start_token->location,
18863 "%<%T::%E%> has not been declared",
18864 TYPE_CONTEXT (nested_name_specifier),
18865 nested_name_specifier);
18866 type = error_mark_node;
18867 }
18868 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18869 && !CLASS_TYPE_P (nested_name_specifier))
18870 {
18871 error_at (type_start_token->location, "nested name specifier "
18872 "%qT for enum declaration does not name a class "
18873 "or namespace", nested_name_specifier);
18874 type = error_mark_node;
18875 }
18876 /* If that scope does not contain the scope in which the
18877 class was originally declared, the program is invalid. */
18878 else if (prev_scope && !is_ancestor (prev_scope,
18879 nested_name_specifier))
18880 {
18881 if (at_namespace_scope_p ())
18882 error_at (type_start_token->location,
18883 "declaration of %qD in namespace %qD which does not "
18884 "enclose %qD",
18885 type, prev_scope, nested_name_specifier);
18886 else
18887 error_at (type_start_token->location,
18888 "declaration of %qD in %qD which does not "
18889 "enclose %qD",
18890 type, prev_scope, nested_name_specifier);
18891 type = error_mark_node;
18892 }
18893 /* If that scope is the scope where the declaration is being placed
18894 the program is invalid. */
18895 else if (CLASS_TYPE_P (nested_name_specifier)
18896 && CLASS_TYPE_P (prev_scope)
18897 && same_type_p (nested_name_specifier, prev_scope))
18898 {
18899 permerror (type_start_token->location,
18900 "extra qualification not allowed");
18901 nested_name_specifier = NULL_TREE;
18902 }
18903 }
18904
18905 if (scoped_enum_p)
18906 begin_scope (sk_scoped_enum, type);
18907
18908 /* Consume the opening brace. */
18909 matching_braces braces;
18910 braces.consume_open (parser);
18911
18912 if (type == error_mark_node)
18913 ; /* Nothing to add */
18914 else if (OPAQUE_ENUM_P (type)
18915 || (cxx_dialect > cxx98 && processing_specialization))
18916 {
18917 new_value_list = true;
18918 SET_OPAQUE_ENUM_P (type, false);
18919 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18920 }
18921 else
18922 {
18923 error_at (type_start_token->location,
18924 "multiple definition of %q#T", type);
18925 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18926 "previous definition here");
18927 type = error_mark_node;
18928 }
18929
18930 if (type == error_mark_node)
18931 cp_parser_skip_to_end_of_block_or_statement (parser);
18932 /* If the next token is not '}', then there are some enumerators. */
18933 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18934 {
18935 if (is_unnamed && !scoped_enum_p)
18936 pedwarn (type_start_token->location, OPT_Wpedantic,
18937 "ISO C++ forbids empty unnamed enum");
18938 }
18939 else
18940 cp_parser_enumerator_list (parser, type);
18941
18942 /* Consume the final '}'. */
18943 braces.require_close (parser);
18944
18945 if (scoped_enum_p)
18946 finish_scope ();
18947 timevar_pop (TV_PARSE_ENUM);
18948 }
18949 else
18950 {
18951 /* If a ';' follows, then it is an opaque-enum-specifier
18952 and additional restrictions apply. */
18953 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18954 {
18955 if (is_unnamed)
18956 error_at (type_start_token->location,
18957 "opaque-enum-specifier without name");
18958 else if (nested_name_specifier)
18959 error_at (type_start_token->location,
18960 "opaque-enum-specifier must use a simple identifier");
18961 }
18962 }
18963
18964 /* Look for trailing attributes to apply to this enumeration, and
18965 apply them if appropriate. */
18966 if (cp_parser_allow_gnu_extensions_p (parser))
18967 {
18968 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18969 cplus_decl_attributes (&type,
18970 trailing_attr,
18971 (int) ATTR_FLAG_TYPE_IN_PLACE);
18972 }
18973
18974 /* Finish up the enumeration. */
18975 if (type != error_mark_node)
18976 {
18977 if (new_value_list)
18978 finish_enum_value_list (type);
18979 if (is_new_type)
18980 finish_enum (type);
18981 }
18982
18983 if (nested_name_specifier)
18984 {
18985 if (CLASS_TYPE_P (nested_name_specifier))
18986 {
18987 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18988 pop_scope (nested_name_specifier);
18989 }
18990 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18991 {
18992 pop_nested_namespace (nested_name_specifier);
18993 }
18994 }
18995 out:
18996 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18997 return type;
18998 }
18999
19000 /* Parse an enumerator-list. The enumerators all have the indicated
19001 TYPE.
19002
19003 enumerator-list:
19004 enumerator-definition
19005 enumerator-list , enumerator-definition */
19006
19007 static void
19008 cp_parser_enumerator_list (cp_parser* parser, tree type)
19009 {
19010 while (true)
19011 {
19012 /* Parse an enumerator-definition. */
19013 cp_parser_enumerator_definition (parser, type);
19014
19015 /* If the next token is not a ',', we've reached the end of
19016 the list. */
19017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19018 break;
19019 /* Otherwise, consume the `,' and keep going. */
19020 cp_lexer_consume_token (parser->lexer);
19021 /* If the next token is a `}', there is a trailing comma. */
19022 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19023 {
19024 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
19025 pedwarn (input_location, OPT_Wpedantic,
19026 "comma at end of enumerator list");
19027 break;
19028 }
19029 }
19030 }
19031
19032 /* Parse an enumerator-definition. The enumerator has the indicated
19033 TYPE.
19034
19035 enumerator-definition:
19036 enumerator
19037 enumerator = constant-expression
19038
19039 enumerator:
19040 identifier
19041
19042 GNU Extensions:
19043
19044 enumerator-definition:
19045 enumerator attributes [opt]
19046 enumerator attributes [opt] = constant-expression */
19047
19048 static void
19049 cp_parser_enumerator_definition (cp_parser* parser, tree type)
19050 {
19051 tree identifier;
19052 tree value;
19053 location_t loc;
19054
19055 /* Save the input location because we are interested in the location
19056 of the identifier and not the location of the explicit value. */
19057 loc = cp_lexer_peek_token (parser->lexer)->location;
19058
19059 /* Look for the identifier. */
19060 identifier = cp_parser_identifier (parser);
19061 if (identifier == error_mark_node)
19062 return;
19063
19064 /* Parse any specified attributes. */
19065 tree attrs = cp_parser_attributes_opt (parser);
19066
19067 /* If the next token is an '=', then there is an explicit value. */
19068 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19069 {
19070 /* Consume the `=' token. */
19071 cp_lexer_consume_token (parser->lexer);
19072 /* Parse the value. */
19073 value = cp_parser_constant_expression (parser);
19074 }
19075 else
19076 value = NULL_TREE;
19077
19078 /* If we are processing a template, make sure the initializer of the
19079 enumerator doesn't contain any bare template parameter pack. */
19080 if (check_for_bare_parameter_packs (value))
19081 value = error_mark_node;
19082
19083 /* Create the enumerator. */
19084 build_enumerator (identifier, value, type, attrs, loc);
19085 }
19086
19087 /* Parse a namespace-name.
19088
19089 namespace-name:
19090 original-namespace-name
19091 namespace-alias
19092
19093 Returns the NAMESPACE_DECL for the namespace. */
19094
19095 static tree
19096 cp_parser_namespace_name (cp_parser* parser)
19097 {
19098 tree identifier;
19099 tree namespace_decl;
19100
19101 cp_token *token = cp_lexer_peek_token (parser->lexer);
19102
19103 /* Get the name of the namespace. */
19104 identifier = cp_parser_identifier (parser);
19105 if (identifier == error_mark_node)
19106 return error_mark_node;
19107
19108 /* Look up the identifier in the currently active scope. Look only
19109 for namespaces, due to:
19110
19111 [basic.lookup.udir]
19112
19113 When looking up a namespace-name in a using-directive or alias
19114 definition, only namespace names are considered.
19115
19116 And:
19117
19118 [basic.lookup.qual]
19119
19120 During the lookup of a name preceding the :: scope resolution
19121 operator, object, function, and enumerator names are ignored.
19122
19123 (Note that cp_parser_qualifying_entity only calls this
19124 function if the token after the name is the scope resolution
19125 operator.) */
19126 namespace_decl = cp_parser_lookup_name (parser, identifier,
19127 none_type,
19128 /*is_template=*/false,
19129 /*is_namespace=*/true,
19130 /*check_dependency=*/true,
19131 /*ambiguous_decls=*/NULL,
19132 token->location);
19133 /* If it's not a namespace, issue an error. */
19134 if (namespace_decl == error_mark_node
19135 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19136 {
19137 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19138 {
19139 auto_diagnostic_group d;
19140 name_hint hint;
19141 if (namespace_decl == error_mark_node
19142 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19143 hint = suggest_alternative_in_explicit_scope (token->location,
19144 identifier,
19145 parser->scope);
19146 if (const char *suggestion = hint.suggestion ())
19147 {
19148 gcc_rich_location richloc (token->location);
19149 richloc.add_fixit_replace (suggestion);
19150 error_at (&richloc,
19151 "%qD is not a namespace-name; did you mean %qs?",
19152 identifier, suggestion);
19153 }
19154 else
19155 error_at (token->location, "%qD is not a namespace-name",
19156 identifier);
19157 }
19158 else
19159 cp_parser_error (parser, "expected namespace-name");
19160 namespace_decl = error_mark_node;
19161 }
19162
19163 return namespace_decl;
19164 }
19165
19166 /* Parse a namespace-definition.
19167
19168 namespace-definition:
19169 named-namespace-definition
19170 unnamed-namespace-definition
19171
19172 named-namespace-definition:
19173 original-namespace-definition
19174 extension-namespace-definition
19175
19176 original-namespace-definition:
19177 namespace identifier { namespace-body }
19178
19179 extension-namespace-definition:
19180 namespace original-namespace-name { namespace-body }
19181
19182 unnamed-namespace-definition:
19183 namespace { namespace-body } */
19184
19185 static void
19186 cp_parser_namespace_definition (cp_parser* parser)
19187 {
19188 tree identifier;
19189 int nested_definition_count = 0;
19190
19191 cp_ensure_no_omp_declare_simd (parser);
19192 cp_ensure_no_oacc_routine (parser);
19193
19194 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19195 const bool topmost_inline_p = is_inline;
19196
19197 if (is_inline)
19198 {
19199 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19200 cp_lexer_consume_token (parser->lexer);
19201 }
19202
19203 /* Look for the `namespace' keyword. */
19204 cp_token* token
19205 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19206
19207 /* Parse any specified attributes before the identifier. */
19208 tree attribs = cp_parser_attributes_opt (parser);
19209
19210 for (;;)
19211 {
19212 identifier = NULL_TREE;
19213
19214 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19215 RID_INLINE);
19216 if (nested_inline_p && nested_definition_count != 0)
19217 {
19218 if (cxx_dialect < cxx2a)
19219 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19220 OPT_Wpedantic, "nested inline namespace definitions only "
19221 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
19222 cp_lexer_consume_token (parser->lexer);
19223 }
19224
19225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19226 {
19227 identifier = cp_parser_identifier (parser);
19228
19229 if (cp_next_tokens_can_be_std_attribute_p (parser))
19230 pedwarn (input_location, OPT_Wpedantic,
19231 "standard attributes on namespaces must precede "
19232 "the namespace name");
19233
19234 /* Parse any attributes specified after the identifier. */
19235 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19236 }
19237
19238 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19239 {
19240 /* Don't forget that the innermost namespace might have been
19241 marked as inline. Use |= because we cannot overwrite
19242 IS_INLINE in case the outermost namespace is inline, but
19243 there are no nested inlines. */
19244 is_inline |= nested_inline_p;
19245 break;
19246 }
19247
19248 if (!nested_definition_count && cxx_dialect < cxx17)
19249 pedwarn (input_location, OPT_Wpedantic,
19250 "nested namespace definitions only available with "
19251 "%<-std=c++17%> or %<-std=gnu++17%>");
19252
19253 /* Nested namespace names can create new namespaces (unlike
19254 other qualified-ids). */
19255 if (int count = (identifier
19256 ? push_namespace (identifier, nested_inline_p)
19257 : 0))
19258 nested_definition_count += count;
19259 else
19260 cp_parser_error (parser, "nested namespace name required");
19261 cp_lexer_consume_token (parser->lexer);
19262 }
19263
19264 if (nested_definition_count && !identifier)
19265 cp_parser_error (parser, "namespace name required");
19266
19267 if (nested_definition_count && attribs)
19268 error_at (token->location,
19269 "a nested namespace definition cannot have attributes");
19270 if (nested_definition_count && topmost_inline_p)
19271 error_at (token->location,
19272 "a nested namespace definition cannot be inline");
19273
19274 /* Start the namespace. */
19275 nested_definition_count += push_namespace (identifier, is_inline);
19276
19277 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19278
19279 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19280
19281 /* Look for the `{' to validate starting the namespace. */
19282 matching_braces braces;
19283 if (braces.require_open (parser))
19284 {
19285 /* Parse the body of the namespace. */
19286 cp_parser_namespace_body (parser);
19287
19288 /* Look for the final `}'. */
19289 braces.require_close (parser);
19290 }
19291
19292 if (has_visibility)
19293 pop_visibility (1);
19294
19295 /* Pop the nested namespace definitions. */
19296 while (nested_definition_count--)
19297 pop_namespace ();
19298 }
19299
19300 /* Parse a namespace-body.
19301
19302 namespace-body:
19303 declaration-seq [opt] */
19304
19305 static void
19306 cp_parser_namespace_body (cp_parser* parser)
19307 {
19308 cp_parser_declaration_seq_opt (parser);
19309 }
19310
19311 /* Parse a namespace-alias-definition.
19312
19313 namespace-alias-definition:
19314 namespace identifier = qualified-namespace-specifier ; */
19315
19316 static void
19317 cp_parser_namespace_alias_definition (cp_parser* parser)
19318 {
19319 tree identifier;
19320 tree namespace_specifier;
19321
19322 cp_token *token = cp_lexer_peek_token (parser->lexer);
19323
19324 /* Look for the `namespace' keyword. */
19325 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19326 /* Look for the identifier. */
19327 identifier = cp_parser_identifier (parser);
19328 if (identifier == error_mark_node)
19329 return;
19330 /* Look for the `=' token. */
19331 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19332 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19333 {
19334 error_at (token->location, "%<namespace%> definition is not allowed here");
19335 /* Skip the definition. */
19336 cp_lexer_consume_token (parser->lexer);
19337 if (cp_parser_skip_to_closing_brace (parser))
19338 cp_lexer_consume_token (parser->lexer);
19339 return;
19340 }
19341 cp_parser_require (parser, CPP_EQ, RT_EQ);
19342 /* Look for the qualified-namespace-specifier. */
19343 namespace_specifier
19344 = cp_parser_qualified_namespace_specifier (parser);
19345 /* Look for the `;' token. */
19346 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19347
19348 /* Register the alias in the symbol table. */
19349 do_namespace_alias (identifier, namespace_specifier);
19350 }
19351
19352 /* Parse a qualified-namespace-specifier.
19353
19354 qualified-namespace-specifier:
19355 :: [opt] nested-name-specifier [opt] namespace-name
19356
19357 Returns a NAMESPACE_DECL corresponding to the specified
19358 namespace. */
19359
19360 static tree
19361 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19362 {
19363 /* Look for the optional `::'. */
19364 cp_parser_global_scope_opt (parser,
19365 /*current_scope_valid_p=*/false);
19366
19367 /* Look for the optional nested-name-specifier. */
19368 cp_parser_nested_name_specifier_opt (parser,
19369 /*typename_keyword_p=*/false,
19370 /*check_dependency_p=*/true,
19371 /*type_p=*/false,
19372 /*is_declaration=*/true);
19373
19374 return cp_parser_namespace_name (parser);
19375 }
19376
19377 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19378 access declaration.
19379
19380 using-declaration:
19381 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19382 using :: unqualified-id ;
19383
19384 access-declaration:
19385 qualified-id ;
19386
19387 */
19388
19389 static bool
19390 cp_parser_using_declaration (cp_parser* parser,
19391 bool access_declaration_p)
19392 {
19393 cp_token *token;
19394 bool typename_p = false;
19395 bool global_scope_p;
19396 tree decl;
19397 tree identifier;
19398 tree qscope;
19399 int oldcount = errorcount;
19400 cp_token *diag_token = NULL;
19401
19402 if (access_declaration_p)
19403 {
19404 diag_token = cp_lexer_peek_token (parser->lexer);
19405 cp_parser_parse_tentatively (parser);
19406 }
19407 else
19408 {
19409 /* Look for the `using' keyword. */
19410 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19411
19412 again:
19413 /* Peek at the next token. */
19414 token = cp_lexer_peek_token (parser->lexer);
19415 /* See if it's `typename'. */
19416 if (token->keyword == RID_TYPENAME)
19417 {
19418 /* Remember that we've seen it. */
19419 typename_p = true;
19420 /* Consume the `typename' token. */
19421 cp_lexer_consume_token (parser->lexer);
19422 }
19423 }
19424
19425 /* Look for the optional global scope qualification. */
19426 global_scope_p
19427 = (cp_parser_global_scope_opt (parser,
19428 /*current_scope_valid_p=*/false)
19429 != NULL_TREE);
19430
19431 /* If we saw `typename', or didn't see `::', then there must be a
19432 nested-name-specifier present. */
19433 if (typename_p || !global_scope_p)
19434 {
19435 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19436 /*check_dependency_p=*/true,
19437 /*type_p=*/false,
19438 /*is_declaration=*/true);
19439 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19440 {
19441 cp_parser_skip_to_end_of_block_or_statement (parser);
19442 return false;
19443 }
19444 }
19445 /* Otherwise, we could be in either of the two productions. In that
19446 case, treat the nested-name-specifier as optional. */
19447 else
19448 qscope = cp_parser_nested_name_specifier_opt (parser,
19449 /*typename_keyword_p=*/false,
19450 /*check_dependency_p=*/true,
19451 /*type_p=*/false,
19452 /*is_declaration=*/true);
19453 if (!qscope)
19454 qscope = global_namespace;
19455 else if (UNSCOPED_ENUM_P (qscope)
19456 && !TYPE_FUNCTION_SCOPE_P (qscope))
19457 qscope = CP_TYPE_CONTEXT (qscope);
19458
19459 if (access_declaration_p && cp_parser_error_occurred (parser))
19460 /* Something has already gone wrong; there's no need to parse
19461 further. Since an error has occurred, the return value of
19462 cp_parser_parse_definitely will be false, as required. */
19463 return cp_parser_parse_definitely (parser);
19464
19465 token = cp_lexer_peek_token (parser->lexer);
19466 /* Parse the unqualified-id. */
19467 identifier = cp_parser_unqualified_id (parser,
19468 /*template_keyword_p=*/false,
19469 /*check_dependency_p=*/true,
19470 /*declarator_p=*/true,
19471 /*optional_p=*/false);
19472
19473 if (access_declaration_p)
19474 {
19475 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19476 cp_parser_simulate_error (parser);
19477 if (!cp_parser_parse_definitely (parser))
19478 return false;
19479 }
19480 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19481 {
19482 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19483 if (cxx_dialect < cxx17
19484 && !in_system_header_at (ell->location))
19485 pedwarn (ell->location, 0,
19486 "pack expansion in using-declaration only available "
19487 "with %<-std=c++17%> or %<-std=gnu++17%>");
19488 qscope = make_pack_expansion (qscope);
19489 }
19490
19491 /* The function we call to handle a using-declaration is different
19492 depending on what scope we are in. */
19493 if (qscope == error_mark_node || identifier == error_mark_node)
19494 ;
19495 else if (!identifier_p (identifier)
19496 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19497 /* [namespace.udecl]
19498
19499 A using declaration shall not name a template-id. */
19500 error_at (token->location,
19501 "a template-id may not appear in a using-declaration");
19502 else
19503 {
19504 if (at_class_scope_p ())
19505 {
19506 /* Create the USING_DECL. */
19507 decl = do_class_using_decl (qscope, identifier);
19508
19509 if (decl && typename_p)
19510 USING_DECL_TYPENAME_P (decl) = 1;
19511
19512 if (check_for_bare_parameter_packs (decl))
19513 {
19514 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19515 return false;
19516 }
19517 else
19518 /* Add it to the list of members in this class. */
19519 finish_member_declaration (decl);
19520 }
19521 else
19522 finish_nonmember_using_decl (qscope, identifier);
19523 }
19524
19525 if (!access_declaration_p
19526 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19527 {
19528 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19529 if (cxx_dialect < cxx17)
19530 pedwarn (comma->location, 0,
19531 "comma-separated list in using-declaration only available "
19532 "with %<-std=c++17%> or %<-std=gnu++17%>");
19533 goto again;
19534 }
19535
19536 /* Look for the final `;'. */
19537 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19538
19539 if (access_declaration_p && errorcount == oldcount)
19540 warning_at (diag_token->location, OPT_Wdeprecated,
19541 "access declarations are deprecated "
19542 "in favour of using-declarations; "
19543 "suggestion: add the %<using%> keyword");
19544
19545 return true;
19546 }
19547
19548 /* Parse an alias-declaration.
19549
19550 alias-declaration:
19551 using identifier attribute-specifier-seq [opt] = type-id */
19552
19553 static tree
19554 cp_parser_alias_declaration (cp_parser* parser)
19555 {
19556 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19557 location_t id_location, type_location;
19558 cp_declarator *declarator;
19559 cp_decl_specifier_seq decl_specs;
19560 bool member_p;
19561 const char *saved_message = NULL;
19562
19563 /* Look for the `using' keyword. */
19564 cp_token *using_token
19565 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19566 if (using_token == NULL)
19567 return error_mark_node;
19568
19569 id_location = cp_lexer_peek_token (parser->lexer)->location;
19570 id = cp_parser_identifier (parser);
19571 if (id == error_mark_node)
19572 return error_mark_node;
19573
19574 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19575 attributes = cp_parser_attributes_opt (parser);
19576 if (attributes == error_mark_node)
19577 return error_mark_node;
19578
19579 cp_parser_require (parser, CPP_EQ, RT_EQ);
19580
19581 if (cp_parser_error_occurred (parser))
19582 return error_mark_node;
19583
19584 cp_parser_commit_to_tentative_parse (parser);
19585
19586 /* Now we are going to parse the type-id of the declaration. */
19587
19588 /*
19589 [dcl.type]/3 says:
19590
19591 "A type-specifier-seq shall not define a class or enumeration
19592 unless it appears in the type-id of an alias-declaration (7.1.3) that
19593 is not the declaration of a template-declaration."
19594
19595 In other words, if we currently are in an alias template, the
19596 type-id should not define a type.
19597
19598 So let's set parser->type_definition_forbidden_message in that
19599 case; cp_parser_check_type_definition (called by
19600 cp_parser_class_specifier) will then emit an error if a type is
19601 defined in the type-id. */
19602 if (parser->num_template_parameter_lists)
19603 {
19604 saved_message = parser->type_definition_forbidden_message;
19605 parser->type_definition_forbidden_message =
19606 G_("types may not be defined in alias template declarations");
19607 }
19608
19609 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
19610 &type_location);
19611
19612 /* Restore the error message if need be. */
19613 if (parser->num_template_parameter_lists)
19614 parser->type_definition_forbidden_message = saved_message;
19615
19616 if (type == error_mark_node
19617 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19618 {
19619 cp_parser_skip_to_end_of_block_or_statement (parser);
19620 return error_mark_node;
19621 }
19622
19623 /* A typedef-name can also be introduced by an alias-declaration. The
19624 identifier following the using keyword becomes a typedef-name. It has
19625 the same semantics as if it were introduced by the typedef
19626 specifier. In particular, it does not define a new type and it shall
19627 not appear in the type-id. */
19628
19629 clear_decl_specs (&decl_specs);
19630 decl_specs.type = type;
19631 if (attributes != NULL_TREE)
19632 {
19633 decl_specs.attributes = attributes;
19634 set_and_check_decl_spec_loc (&decl_specs,
19635 ds_attribute,
19636 attrs_token);
19637 }
19638 set_and_check_decl_spec_loc (&decl_specs,
19639 ds_typedef,
19640 using_token);
19641 set_and_check_decl_spec_loc (&decl_specs,
19642 ds_alias,
19643 using_token);
19644 decl_specs.locations[ds_type_spec] = type_location;
19645
19646 if (parser->num_template_parameter_lists
19647 && !cp_parser_check_template_parameters (parser,
19648 /*num_templates=*/0,
19649 /*template_id*/false,
19650 id_location,
19651 /*declarator=*/NULL))
19652 return error_mark_node;
19653
19654 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19655
19656 member_p = at_class_scope_p ();
19657 if (member_p)
19658 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19659 NULL_TREE, attributes);
19660 else
19661 decl = start_decl (declarator, &decl_specs, 0,
19662 attributes, NULL_TREE, &pushed_scope);
19663 if (decl == error_mark_node)
19664 return decl;
19665
19666 // Attach constraints to the alias declaration.
19667 if (flag_concepts && current_template_parms)
19668 {
19669 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19670 tree constr = build_constraints (reqs, NULL_TREE);
19671 set_constraints (decl, constr);
19672 }
19673
19674 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19675
19676 if (pushed_scope)
19677 pop_scope (pushed_scope);
19678
19679 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19680 added into the symbol table; otherwise, return the TYPE_DECL. */
19681 if (DECL_LANG_SPECIFIC (decl)
19682 && DECL_TEMPLATE_INFO (decl)
19683 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19684 {
19685 decl = DECL_TI_TEMPLATE (decl);
19686 if (member_p)
19687 check_member_template (decl);
19688 }
19689
19690 return decl;
19691 }
19692
19693 /* Parse a using-directive.
19694
19695 using-directive:
19696 using namespace :: [opt] nested-name-specifier [opt]
19697 namespace-name ; */
19698
19699 static void
19700 cp_parser_using_directive (cp_parser* parser)
19701 {
19702 tree namespace_decl;
19703 tree attribs;
19704
19705 /* Look for the `using' keyword. */
19706 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19707 /* And the `namespace' keyword. */
19708 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19709 /* Look for the optional `::' operator. */
19710 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19711 /* And the optional nested-name-specifier. */
19712 cp_parser_nested_name_specifier_opt (parser,
19713 /*typename_keyword_p=*/false,
19714 /*check_dependency_p=*/true,
19715 /*type_p=*/false,
19716 /*is_declaration=*/true);
19717 /* Get the namespace being used. */
19718 namespace_decl = cp_parser_namespace_name (parser);
19719 /* And any specified attributes. */
19720 attribs = cp_parser_attributes_opt (parser);
19721
19722 /* Update the symbol table. */
19723 finish_using_directive (namespace_decl, attribs);
19724
19725 /* Look for the final `;'. */
19726 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19727 }
19728
19729 /* Parse an asm-definition.
19730
19731 asm-qualifier:
19732 volatile
19733 inline
19734 goto
19735
19736 asm-qualifier-list:
19737 asm-qualifier
19738 asm-qualifier-list asm-qualifier
19739
19740 asm-definition:
19741 asm ( string-literal ) ;
19742
19743 GNU Extension:
19744
19745 asm-definition:
19746 asm asm-qualifier-list [opt] ( string-literal ) ;
19747 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19748 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19749 : asm-operand-list [opt] ) ;
19750 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19751 : asm-operand-list [opt]
19752 : asm-clobber-list [opt] ) ;
19753 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19754 : asm-clobber-list [opt]
19755 : asm-goto-list ) ;
19756
19757 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19758 contains goto, and is the only allowed form in that case. No duplicates are
19759 allowed in an asm-qualifier-list. */
19760
19761 static void
19762 cp_parser_asm_definition (cp_parser* parser)
19763 {
19764 tree string;
19765 tree outputs = NULL_TREE;
19766 tree inputs = NULL_TREE;
19767 tree clobbers = NULL_TREE;
19768 tree labels = NULL_TREE;
19769 tree asm_stmt;
19770 bool extended_p = false;
19771 bool invalid_inputs_p = false;
19772 bool invalid_outputs_p = false;
19773 required_token missing = RT_NONE;
19774
19775 /* Look for the `asm' keyword. */
19776 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19777
19778 if (parser->in_function_body
19779 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19780 {
19781 error ("%<asm%> in %<constexpr%> function");
19782 cp_function_chain->invalid_constexpr = true;
19783 }
19784
19785 /* Handle the asm-qualifier-list. */
19786 location_t volatile_loc = UNKNOWN_LOCATION;
19787 location_t inline_loc = UNKNOWN_LOCATION;
19788 location_t goto_loc = UNKNOWN_LOCATION;
19789 location_t first_loc = UNKNOWN_LOCATION;
19790
19791 if (cp_parser_allow_gnu_extensions_p (parser))
19792 for (;;)
19793 {
19794 cp_token *token = cp_lexer_peek_token (parser->lexer);
19795 location_t loc = token->location;
19796 switch (cp_lexer_peek_token (parser->lexer)->keyword)
19797 {
19798 case RID_VOLATILE:
19799 if (volatile_loc)
19800 {
19801 error_at (loc, "duplicate %<asm%> qualifier %qT",
19802 token->u.value);
19803 inform (volatile_loc, "first seen here");
19804 }
19805 else
19806 {
19807 if (!parser->in_function_body)
19808 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
19809 "outside of function body", token->u.value);
19810 volatile_loc = loc;
19811 }
19812 cp_lexer_consume_token (parser->lexer);
19813 continue;
19814
19815 case RID_INLINE:
19816 if (inline_loc)
19817 {
19818 error_at (loc, "duplicate %<asm%> qualifier %qT",
19819 token->u.value);
19820 inform (inline_loc, "first seen here");
19821 }
19822 else
19823 inline_loc = loc;
19824 if (!first_loc)
19825 first_loc = loc;
19826 cp_lexer_consume_token (parser->lexer);
19827 continue;
19828
19829 case RID_GOTO:
19830 if (goto_loc)
19831 {
19832 error_at (loc, "duplicate %<asm%> qualifier %qT",
19833 token->u.value);
19834 inform (goto_loc, "first seen here");
19835 }
19836 else
19837 goto_loc = loc;
19838 if (!first_loc)
19839 first_loc = loc;
19840 cp_lexer_consume_token (parser->lexer);
19841 continue;
19842
19843 case RID_CONST:
19844 case RID_RESTRICT:
19845 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
19846 cp_lexer_consume_token (parser->lexer);
19847 continue;
19848
19849 default:
19850 break;
19851 }
19852 break;
19853 }
19854
19855 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
19856 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
19857 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
19858
19859 if (!parser->in_function_body && (inline_p || goto_p))
19860 {
19861 error_at (first_loc, "%<asm%> qualifier outside of function body");
19862 inline_p = goto_p = false;
19863 }
19864
19865 /* Look for the opening `('. */
19866 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19867 return;
19868 /* Look for the string. */
19869 string = cp_parser_string_literal (parser, false, false);
19870 if (string == error_mark_node)
19871 {
19872 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19873 /*consume_paren=*/true);
19874 return;
19875 }
19876
19877 /* If we're allowing GNU extensions, check for the extended assembly
19878 syntax. Unfortunately, the `:' tokens need not be separated by
19879 a space in C, and so, for compatibility, we tolerate that here
19880 too. Doing that means that we have to treat the `::' operator as
19881 two `:' tokens. */
19882 if (cp_parser_allow_gnu_extensions_p (parser)
19883 && parser->in_function_body
19884 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19885 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19886 {
19887 bool inputs_p = false;
19888 bool clobbers_p = false;
19889 bool labels_p = false;
19890
19891 /* The extended syntax was used. */
19892 extended_p = true;
19893
19894 /* Look for outputs. */
19895 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19896 {
19897 /* Consume the `:'. */
19898 cp_lexer_consume_token (parser->lexer);
19899 /* Parse the output-operands. */
19900 if (cp_lexer_next_token_is_not (parser->lexer,
19901 CPP_COLON)
19902 && cp_lexer_next_token_is_not (parser->lexer,
19903 CPP_SCOPE)
19904 && cp_lexer_next_token_is_not (parser->lexer,
19905 CPP_CLOSE_PAREN)
19906 && !goto_p)
19907 {
19908 outputs = cp_parser_asm_operand_list (parser);
19909 if (outputs == error_mark_node)
19910 invalid_outputs_p = true;
19911 }
19912 }
19913 /* If the next token is `::', there are no outputs, and the
19914 next token is the beginning of the inputs. */
19915 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19916 /* The inputs are coming next. */
19917 inputs_p = true;
19918
19919 /* Look for inputs. */
19920 if (inputs_p
19921 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19922 {
19923 /* Consume the `:' or `::'. */
19924 cp_lexer_consume_token (parser->lexer);
19925 /* Parse the output-operands. */
19926 if (cp_lexer_next_token_is_not (parser->lexer,
19927 CPP_COLON)
19928 && cp_lexer_next_token_is_not (parser->lexer,
19929 CPP_SCOPE)
19930 && cp_lexer_next_token_is_not (parser->lexer,
19931 CPP_CLOSE_PAREN))
19932 {
19933 inputs = cp_parser_asm_operand_list (parser);
19934 if (inputs == error_mark_node)
19935 invalid_inputs_p = true;
19936 }
19937 }
19938 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19939 /* The clobbers are coming next. */
19940 clobbers_p = true;
19941
19942 /* Look for clobbers. */
19943 if (clobbers_p
19944 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19945 {
19946 clobbers_p = true;
19947 /* Consume the `:' or `::'. */
19948 cp_lexer_consume_token (parser->lexer);
19949 /* Parse the clobbers. */
19950 if (cp_lexer_next_token_is_not (parser->lexer,
19951 CPP_COLON)
19952 && cp_lexer_next_token_is_not (parser->lexer,
19953 CPP_CLOSE_PAREN))
19954 clobbers = cp_parser_asm_clobber_list (parser);
19955 }
19956 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19957 /* The labels are coming next. */
19958 labels_p = true;
19959
19960 /* Look for labels. */
19961 if (labels_p
19962 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19963 {
19964 labels_p = true;
19965 /* Consume the `:' or `::'. */
19966 cp_lexer_consume_token (parser->lexer);
19967 /* Parse the labels. */
19968 labels = cp_parser_asm_label_list (parser);
19969 }
19970
19971 if (goto_p && !labels_p)
19972 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19973 }
19974 else if (goto_p)
19975 missing = RT_COLON_SCOPE;
19976
19977 /* Look for the closing `)'. */
19978 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19979 missing ? missing : RT_CLOSE_PAREN))
19980 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19981 /*consume_paren=*/true);
19982 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19983
19984 if (!invalid_inputs_p && !invalid_outputs_p)
19985 {
19986 /* Create the ASM_EXPR. */
19987 if (parser->in_function_body)
19988 {
19989 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19990 inputs, clobbers, labels, inline_p);
19991 /* If the extended syntax was not used, mark the ASM_EXPR. */
19992 if (!extended_p)
19993 {
19994 tree temp = asm_stmt;
19995 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19996 temp = TREE_OPERAND (temp, 0);
19997
19998 ASM_INPUT_P (temp) = 1;
19999 }
20000 }
20001 else
20002 symtab->finalize_toplevel_asm (string);
20003 }
20004 }
20005
20006 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20007 type that comes from the decl-specifier-seq. */
20008
20009 static tree
20010 strip_declarator_types (tree type, cp_declarator *declarator)
20011 {
20012 for (cp_declarator *d = declarator; d;)
20013 switch (d->kind)
20014 {
20015 case cdk_id:
20016 case cdk_decomp:
20017 case cdk_error:
20018 d = NULL;
20019 break;
20020
20021 default:
20022 if (TYPE_PTRMEMFUNC_P (type))
20023 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
20024 type = TREE_TYPE (type);
20025 d = d->declarator;
20026 break;
20027 }
20028
20029 return type;
20030 }
20031
20032 /* Declarators [gram.dcl.decl] */
20033
20034 /* Parse an init-declarator.
20035
20036 init-declarator:
20037 declarator initializer [opt]
20038
20039 GNU Extension:
20040
20041 init-declarator:
20042 declarator asm-specification [opt] attributes [opt] initializer [opt]
20043
20044 function-definition:
20045 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20046 function-body
20047 decl-specifier-seq [opt] declarator function-try-block
20048
20049 GNU Extension:
20050
20051 function-definition:
20052 __extension__ function-definition
20053
20054 TM Extension:
20055
20056 function-definition:
20057 decl-specifier-seq [opt] declarator function-transaction-block
20058
20059 The parser flags FLAGS is used to control type-specifier parsing.
20060
20061 The DECL_SPECIFIERS apply to this declarator. Returns a
20062 representation of the entity declared. If MEMBER_P is TRUE, then
20063 this declarator appears in a class scope. The new DECL created by
20064 this declarator is returned.
20065
20066 The CHECKS are access checks that should be performed once we know
20067 what entity is being declared (and, therefore, what classes have
20068 befriended it).
20069
20070 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20071 for a function-definition here as well. If the declarator is a
20072 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20073 be TRUE upon return. By that point, the function-definition will
20074 have been completely parsed.
20075
20076 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20077 is FALSE.
20078
20079 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20080 parsed declaration if it is an uninitialized single declarator not followed
20081 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20082 if present, will not be consumed. If returned, this declarator will be
20083 created with SD_INITIALIZED but will not call cp_finish_decl.
20084
20085 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20086 and there is an initializer, the pointed location_t is set to the
20087 location of the '=' or `(', or '{' in C++11 token introducing the
20088 initializer. */
20089
20090 static tree
20091 cp_parser_init_declarator (cp_parser* parser,
20092 cp_parser_flags flags,
20093 cp_decl_specifier_seq *decl_specifiers,
20094 vec<deferred_access_check, va_gc> *checks,
20095 bool function_definition_allowed_p,
20096 bool member_p,
20097 int declares_class_or_enum,
20098 bool* function_definition_p,
20099 tree* maybe_range_for_decl,
20100 location_t* init_loc,
20101 tree* auto_result)
20102 {
20103 cp_token *token = NULL, *asm_spec_start_token = NULL,
20104 *attributes_start_token = NULL;
20105 cp_declarator *declarator;
20106 tree prefix_attributes;
20107 tree attributes = NULL;
20108 tree asm_specification;
20109 tree initializer;
20110 tree decl = NULL_TREE;
20111 tree scope;
20112 int is_initialized;
20113 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20114 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20115 "(...)". */
20116 enum cpp_ttype initialization_kind;
20117 bool is_direct_init = false;
20118 bool is_non_constant_init;
20119 int ctor_dtor_or_conv_p;
20120 bool friend_p = cp_parser_friend_p (decl_specifiers);
20121 tree pushed_scope = NULL_TREE;
20122 bool range_for_decl_p = false;
20123 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20124 location_t tmp_init_loc = UNKNOWN_LOCATION;
20125
20126 /* Gather the attributes that were provided with the
20127 decl-specifiers. */
20128 prefix_attributes = decl_specifiers->attributes;
20129
20130 /* Assume that this is not the declarator for a function
20131 definition. */
20132 if (function_definition_p)
20133 *function_definition_p = false;
20134
20135 /* Default arguments are only permitted for function parameters. */
20136 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20137 parser->default_arg_ok_p = false;
20138
20139 /* Defer access checks while parsing the declarator; we cannot know
20140 what names are accessible until we know what is being
20141 declared. */
20142 resume_deferring_access_checks ();
20143
20144 token = cp_lexer_peek_token (parser->lexer);
20145
20146 /* Parse the declarator. */
20147 declarator
20148 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20149 flags, &ctor_dtor_or_conv_p,
20150 /*parenthesized_p=*/NULL,
20151 member_p, friend_p, /*static_p=*/false);
20152 /* Gather up the deferred checks. */
20153 stop_deferring_access_checks ();
20154
20155 parser->default_arg_ok_p = saved_default_arg_ok_p;
20156
20157 /* If the DECLARATOR was erroneous, there's no need to go
20158 further. */
20159 if (declarator == cp_error_declarator)
20160 return error_mark_node;
20161
20162 /* Check that the number of template-parameter-lists is OK. */
20163 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20164 token->location))
20165 return error_mark_node;
20166
20167 if (declares_class_or_enum & 2)
20168 cp_parser_check_for_definition_in_return_type (declarator,
20169 decl_specifiers->type,
20170 decl_specifiers->locations[ds_type_spec]);
20171
20172 /* Figure out what scope the entity declared by the DECLARATOR is
20173 located in. `grokdeclarator' sometimes changes the scope, so
20174 we compute it now. */
20175 scope = get_scope_of_declarator (declarator);
20176
20177 /* Perform any lookups in the declared type which were thought to be
20178 dependent, but are not in the scope of the declarator. */
20179 decl_specifiers->type
20180 = maybe_update_decl_type (decl_specifiers->type, scope);
20181
20182 /* If we're allowing GNU extensions, look for an
20183 asm-specification. */
20184 if (cp_parser_allow_gnu_extensions_p (parser))
20185 {
20186 /* Look for an asm-specification. */
20187 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20188 asm_specification = cp_parser_asm_specification_opt (parser);
20189 }
20190 else
20191 asm_specification = NULL_TREE;
20192
20193 /* Look for attributes. */
20194 attributes_start_token = cp_lexer_peek_token (parser->lexer);
20195 attributes = cp_parser_attributes_opt (parser);
20196
20197 /* Peek at the next token. */
20198 token = cp_lexer_peek_token (parser->lexer);
20199
20200 bool bogus_implicit_tmpl = false;
20201
20202 if (function_declarator_p (declarator))
20203 {
20204 /* Handle C++17 deduction guides. */
20205 if (!decl_specifiers->type
20206 && ctor_dtor_or_conv_p <= 0
20207 && cxx_dialect >= cxx17)
20208 {
20209 cp_declarator *id = get_id_declarator (declarator);
20210 tree name = id->u.id.unqualified_name;
20211 parser->scope = id->u.id.qualifying_scope;
20212 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20213 if (tmpl
20214 && (DECL_CLASS_TEMPLATE_P (tmpl)
20215 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20216 {
20217 id->u.id.unqualified_name = dguide_name (tmpl);
20218 id->u.id.sfk = sfk_deduction_guide;
20219 ctor_dtor_or_conv_p = 1;
20220 }
20221 }
20222
20223 /* Check to see if the token indicates the start of a
20224 function-definition. */
20225 if (cp_parser_token_starts_function_definition_p (token))
20226 {
20227 if (!function_definition_allowed_p)
20228 {
20229 /* If a function-definition should not appear here, issue an
20230 error message. */
20231 cp_parser_error (parser,
20232 "a function-definition is not allowed here");
20233 return error_mark_node;
20234 }
20235
20236 location_t func_brace_location
20237 = cp_lexer_peek_token (parser->lexer)->location;
20238
20239 /* Neither attributes nor an asm-specification are allowed
20240 on a function-definition. */
20241 if (asm_specification)
20242 error_at (asm_spec_start_token->location,
20243 "an %<asm%> specification is not allowed "
20244 "on a function-definition");
20245 if (attributes)
20246 error_at (attributes_start_token->location,
20247 "attributes are not allowed "
20248 "on a function-definition");
20249 /* This is a function-definition. */
20250 *function_definition_p = true;
20251
20252 /* Parse the function definition. */
20253 if (member_p)
20254 decl = cp_parser_save_member_function_body (parser,
20255 decl_specifiers,
20256 declarator,
20257 prefix_attributes);
20258 else
20259 decl =
20260 (cp_parser_function_definition_from_specifiers_and_declarator
20261 (parser, decl_specifiers, prefix_attributes, declarator));
20262
20263 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20264 {
20265 /* This is where the prologue starts... */
20266 DECL_STRUCT_FUNCTION (decl)->function_start_locus
20267 = func_brace_location;
20268 }
20269
20270 return decl;
20271 }
20272 }
20273 else if (parser->fully_implicit_function_template_p)
20274 {
20275 /* A non-template declaration involving a function parameter list
20276 containing an implicit template parameter will be made into a
20277 template. If the resulting declaration is not going to be an
20278 actual function then finish the template scope here to prevent it.
20279 An error message will be issued once we have a decl to talk about.
20280
20281 FIXME probably we should do type deduction rather than create an
20282 implicit template, but the standard currently doesn't allow it. */
20283 bogus_implicit_tmpl = true;
20284 finish_fully_implicit_template (parser, NULL_TREE);
20285 }
20286
20287 /* [dcl.dcl]
20288
20289 Only in function declarations for constructors, destructors, type
20290 conversions, and deduction guides can the decl-specifier-seq be omitted.
20291
20292 We explicitly postpone this check past the point where we handle
20293 function-definitions because we tolerate function-definitions
20294 that are missing their return types in some modes. */
20295 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20296 {
20297 cp_parser_error (parser,
20298 "expected constructor, destructor, or type conversion");
20299 return error_mark_node;
20300 }
20301
20302 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20303 if (token->type == CPP_EQ
20304 || token->type == CPP_OPEN_PAREN
20305 || token->type == CPP_OPEN_BRACE)
20306 {
20307 is_initialized = SD_INITIALIZED;
20308 initialization_kind = token->type;
20309 if (maybe_range_for_decl)
20310 *maybe_range_for_decl = error_mark_node;
20311 tmp_init_loc = token->location;
20312 if (init_loc && *init_loc == UNKNOWN_LOCATION)
20313 *init_loc = tmp_init_loc;
20314
20315 if (token->type == CPP_EQ
20316 && function_declarator_p (declarator))
20317 {
20318 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20319 if (t2->keyword == RID_DEFAULT)
20320 is_initialized = SD_DEFAULTED;
20321 else if (t2->keyword == RID_DELETE)
20322 is_initialized = SD_DELETED;
20323 }
20324 }
20325 else
20326 {
20327 /* If the init-declarator isn't initialized and isn't followed by a
20328 `,' or `;', it's not a valid init-declarator. */
20329 if (token->type != CPP_COMMA
20330 && token->type != CPP_SEMICOLON)
20331 {
20332 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20333 range_for_decl_p = true;
20334 else
20335 {
20336 if (!maybe_range_for_decl)
20337 cp_parser_error (parser, "expected initializer");
20338 return error_mark_node;
20339 }
20340 }
20341 is_initialized = SD_UNINITIALIZED;
20342 initialization_kind = CPP_EOF;
20343 }
20344
20345 /* Because start_decl has side-effects, we should only call it if we
20346 know we're going ahead. By this point, we know that we cannot
20347 possibly be looking at any other construct. */
20348 cp_parser_commit_to_tentative_parse (parser);
20349
20350 /* Enter the newly declared entry in the symbol table. If we're
20351 processing a declaration in a class-specifier, we wait until
20352 after processing the initializer. */
20353 if (!member_p)
20354 {
20355 if (parser->in_unbraced_linkage_specification_p)
20356 decl_specifiers->storage_class = sc_extern;
20357 decl = start_decl (declarator, decl_specifiers,
20358 range_for_decl_p? SD_INITIALIZED : is_initialized,
20359 attributes, prefix_attributes, &pushed_scope);
20360 cp_finalize_omp_declare_simd (parser, decl);
20361 cp_finalize_oacc_routine (parser, decl, false);
20362 /* Adjust location of decl if declarator->id_loc is more appropriate:
20363 set, and decl wasn't merged with another decl, in which case its
20364 location would be different from input_location, and more accurate. */
20365 if (DECL_P (decl)
20366 && declarator->id_loc != UNKNOWN_LOCATION
20367 && DECL_SOURCE_LOCATION (decl) == input_location)
20368 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20369 }
20370 else if (scope)
20371 /* Enter the SCOPE. That way unqualified names appearing in the
20372 initializer will be looked up in SCOPE. */
20373 pushed_scope = push_scope (scope);
20374
20375 /* Perform deferred access control checks, now that we know in which
20376 SCOPE the declared entity resides. */
20377 if (!member_p && decl)
20378 {
20379 tree saved_current_function_decl = NULL_TREE;
20380
20381 /* If the entity being declared is a function, pretend that we
20382 are in its scope. If it is a `friend', it may have access to
20383 things that would not otherwise be accessible. */
20384 if (TREE_CODE (decl) == FUNCTION_DECL)
20385 {
20386 saved_current_function_decl = current_function_decl;
20387 current_function_decl = decl;
20388 }
20389
20390 /* Perform access checks for template parameters. */
20391 cp_parser_perform_template_parameter_access_checks (checks);
20392
20393 /* Perform the access control checks for the declarator and the
20394 decl-specifiers. */
20395 perform_deferred_access_checks (tf_warning_or_error);
20396
20397 /* Restore the saved value. */
20398 if (TREE_CODE (decl) == FUNCTION_DECL)
20399 current_function_decl = saved_current_function_decl;
20400 }
20401
20402 /* Parse the initializer. */
20403 initializer = NULL_TREE;
20404 is_direct_init = false;
20405 is_non_constant_init = true;
20406 if (is_initialized)
20407 {
20408 if (function_declarator_p (declarator))
20409 {
20410 if (initialization_kind == CPP_EQ)
20411 initializer = cp_parser_pure_specifier (parser);
20412 else
20413 {
20414 /* If the declaration was erroneous, we don't really
20415 know what the user intended, so just silently
20416 consume the initializer. */
20417 if (decl != error_mark_node)
20418 error_at (tmp_init_loc, "initializer provided for function");
20419 cp_parser_skip_to_closing_parenthesis (parser,
20420 /*recovering=*/true,
20421 /*or_comma=*/false,
20422 /*consume_paren=*/true);
20423 }
20424 }
20425 else
20426 {
20427 /* We want to record the extra mangling scope for in-class
20428 initializers of class members and initializers of static data
20429 member templates. The former involves deferring
20430 parsing of the initializer until end of class as with default
20431 arguments. So right here we only handle the latter. */
20432 if (!member_p && processing_template_decl && decl != error_mark_node)
20433 start_lambda_scope (decl);
20434 initializer = cp_parser_initializer (parser,
20435 &is_direct_init,
20436 &is_non_constant_init);
20437 if (!member_p && processing_template_decl && decl != error_mark_node)
20438 finish_lambda_scope ();
20439 if (initializer == error_mark_node)
20440 cp_parser_skip_to_end_of_statement (parser);
20441 }
20442 }
20443
20444 /* The old parser allows attributes to appear after a parenthesized
20445 initializer. Mark Mitchell proposed removing this functionality
20446 on the GCC mailing lists on 2002-08-13. This parser accepts the
20447 attributes -- but ignores them. Made a permerror in GCC 8. */
20448 if (cp_parser_allow_gnu_extensions_p (parser)
20449 && initialization_kind == CPP_OPEN_PAREN
20450 && cp_parser_attributes_opt (parser)
20451 && permerror (input_location,
20452 "attributes after parenthesized initializer ignored"))
20453 {
20454 static bool hint;
20455 if (flag_permissive && !hint)
20456 {
20457 hint = true;
20458 inform (input_location,
20459 "this flexibility is deprecated and will be removed");
20460 }
20461 }
20462
20463 /* And now complain about a non-function implicit template. */
20464 if (bogus_implicit_tmpl && decl != error_mark_node)
20465 error_at (DECL_SOURCE_LOCATION (decl),
20466 "non-function %qD declared as implicit template", decl);
20467
20468 /* For an in-class declaration, use `grokfield' to create the
20469 declaration. */
20470 if (member_p)
20471 {
20472 if (pushed_scope)
20473 {
20474 pop_scope (pushed_scope);
20475 pushed_scope = NULL_TREE;
20476 }
20477 decl = grokfield (declarator, decl_specifiers,
20478 initializer, !is_non_constant_init,
20479 /*asmspec=*/NULL_TREE,
20480 attr_chainon (attributes, prefix_attributes));
20481 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20482 cp_parser_save_default_args (parser, decl);
20483 cp_finalize_omp_declare_simd (parser, decl);
20484 cp_finalize_oacc_routine (parser, decl, false);
20485 }
20486
20487 /* Finish processing the declaration. But, skip member
20488 declarations. */
20489 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20490 {
20491 cp_finish_decl (decl,
20492 initializer, !is_non_constant_init,
20493 asm_specification,
20494 /* If the initializer is in parentheses, then this is
20495 a direct-initialization, which means that an
20496 `explicit' constructor is OK. Otherwise, an
20497 `explicit' constructor cannot be used. */
20498 ((is_direct_init || !is_initialized)
20499 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20500 }
20501 else if ((cxx_dialect != cxx98) && friend_p
20502 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20503 /* Core issue #226 (C++0x only): A default template-argument
20504 shall not be specified in a friend class template
20505 declaration. */
20506 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20507 /*is_partial=*/false, /*is_friend_decl=*/1);
20508
20509 if (!friend_p && pushed_scope)
20510 pop_scope (pushed_scope);
20511
20512 if (function_declarator_p (declarator)
20513 && parser->fully_implicit_function_template_p)
20514 {
20515 if (member_p)
20516 decl = finish_fully_implicit_template (parser, decl);
20517 else
20518 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20519 }
20520
20521 if (auto_result && is_initialized && decl_specifiers->type
20522 && type_uses_auto (decl_specifiers->type))
20523 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20524
20525 return decl;
20526 }
20527
20528 /* Parse a declarator.
20529
20530 declarator:
20531 direct-declarator
20532 ptr-operator declarator
20533
20534 abstract-declarator:
20535 ptr-operator abstract-declarator [opt]
20536 direct-abstract-declarator
20537
20538 GNU Extensions:
20539
20540 declarator:
20541 attributes [opt] direct-declarator
20542 attributes [opt] ptr-operator declarator
20543
20544 abstract-declarator:
20545 attributes [opt] ptr-operator abstract-declarator [opt]
20546 attributes [opt] direct-abstract-declarator
20547
20548 The parser flags FLAGS is used to control type-specifier parsing.
20549
20550 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20551 detect constructors, destructors, deduction guides, or conversion operators.
20552 It is set to -1 if the declarator is a name, and +1 if it is a
20553 function. Otherwise it is set to zero. Usually you just want to
20554 test for >0, but internally the negative value is used.
20555
20556 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20557 a decl-specifier-seq unless it declares a constructor, destructor,
20558 or conversion. It might seem that we could check this condition in
20559 semantic analysis, rather than parsing, but that makes it difficult
20560 to handle something like `f()'. We want to notice that there are
20561 no decl-specifiers, and therefore realize that this is an
20562 expression, not a declaration.)
20563
20564 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20565 the declarator is a direct-declarator of the form "(...)".
20566
20567 MEMBER_P is true iff this declarator is a member-declarator.
20568
20569 FRIEND_P is true iff this declarator is a friend.
20570
20571 STATIC_P is true iff the keyword static was seen. */
20572
20573 static cp_declarator *
20574 cp_parser_declarator (cp_parser* parser,
20575 cp_parser_declarator_kind dcl_kind,
20576 cp_parser_flags flags,
20577 int* ctor_dtor_or_conv_p,
20578 bool* parenthesized_p,
20579 bool member_p, bool friend_p, bool static_p)
20580 {
20581 cp_declarator *declarator;
20582 enum tree_code code;
20583 cp_cv_quals cv_quals;
20584 tree class_type;
20585 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20586
20587 /* Assume this is not a constructor, destructor, or type-conversion
20588 operator. */
20589 if (ctor_dtor_or_conv_p)
20590 *ctor_dtor_or_conv_p = 0;
20591
20592 if (cp_parser_allow_gnu_extensions_p (parser))
20593 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20594
20595 /* Check for the ptr-operator production. */
20596 cp_parser_parse_tentatively (parser);
20597 /* Parse the ptr-operator. */
20598 code = cp_parser_ptr_operator (parser,
20599 &class_type,
20600 &cv_quals,
20601 &std_attributes);
20602
20603 /* If that worked, then we have a ptr-operator. */
20604 if (cp_parser_parse_definitely (parser))
20605 {
20606 /* If a ptr-operator was found, then this declarator was not
20607 parenthesized. */
20608 if (parenthesized_p)
20609 *parenthesized_p = true;
20610 /* The dependent declarator is optional if we are parsing an
20611 abstract-declarator. */
20612 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20613 cp_parser_parse_tentatively (parser);
20614
20615 /* Parse the dependent declarator. */
20616 declarator = cp_parser_declarator (parser, dcl_kind,
20617 CP_PARSER_FLAGS_NONE,
20618 /*ctor_dtor_or_conv_p=*/NULL,
20619 /*parenthesized_p=*/NULL,
20620 /*member_p=*/false,
20621 friend_p, /*static_p=*/false);
20622
20623 /* If we are parsing an abstract-declarator, we must handle the
20624 case where the dependent declarator is absent. */
20625 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20626 && !cp_parser_parse_definitely (parser))
20627 declarator = NULL;
20628
20629 declarator = cp_parser_make_indirect_declarator
20630 (code, class_type, cv_quals, declarator, std_attributes);
20631 }
20632 /* Everything else is a direct-declarator. */
20633 else
20634 {
20635 if (parenthesized_p)
20636 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20637 CPP_OPEN_PAREN);
20638 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20639 flags, ctor_dtor_or_conv_p,
20640 member_p, friend_p, static_p);
20641 }
20642
20643 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20644 declarator->attributes = gnu_attributes;
20645 return declarator;
20646 }
20647
20648 /* Parse a direct-declarator or direct-abstract-declarator.
20649
20650 direct-declarator:
20651 declarator-id
20652 direct-declarator ( parameter-declaration-clause )
20653 cv-qualifier-seq [opt]
20654 ref-qualifier [opt]
20655 exception-specification [opt]
20656 direct-declarator [ constant-expression [opt] ]
20657 ( declarator )
20658
20659 direct-abstract-declarator:
20660 direct-abstract-declarator [opt]
20661 ( parameter-declaration-clause )
20662 cv-qualifier-seq [opt]
20663 ref-qualifier [opt]
20664 exception-specification [opt]
20665 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20666 ( abstract-declarator )
20667
20668 Returns a representation of the declarator. DCL_KIND is
20669 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20670 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20671 we are parsing a direct-declarator. It is
20672 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20673 of ambiguity we prefer an abstract declarator, as per
20674 [dcl.ambig.res].
20675 The parser flags FLAGS is used to control type-specifier parsing.
20676 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20677 as for cp_parser_declarator. */
20678
20679 static cp_declarator *
20680 cp_parser_direct_declarator (cp_parser* parser,
20681 cp_parser_declarator_kind dcl_kind,
20682 cp_parser_flags flags,
20683 int* ctor_dtor_or_conv_p,
20684 bool member_p, bool friend_p, bool static_p)
20685 {
20686 cp_token *token;
20687 cp_declarator *declarator = NULL;
20688 tree scope = NULL_TREE;
20689 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20690 bool saved_in_declarator_p = parser->in_declarator_p;
20691 bool first = true;
20692 tree pushed_scope = NULL_TREE;
20693 cp_token *open_paren = NULL, *close_paren = NULL;
20694
20695 while (true)
20696 {
20697 /* Peek at the next token. */
20698 token = cp_lexer_peek_token (parser->lexer);
20699 if (token->type == CPP_OPEN_PAREN)
20700 {
20701 /* This is either a parameter-declaration-clause, or a
20702 parenthesized declarator. When we know we are parsing a
20703 named declarator, it must be a parenthesized declarator
20704 if FIRST is true. For instance, `(int)' is a
20705 parameter-declaration-clause, with an omitted
20706 direct-abstract-declarator. But `((*))', is a
20707 parenthesized abstract declarator. Finally, when T is a
20708 template parameter `(T)' is a
20709 parameter-declaration-clause, and not a parenthesized
20710 named declarator.
20711
20712 We first try and parse a parameter-declaration-clause,
20713 and then try a nested declarator (if FIRST is true).
20714
20715 It is not an error for it not to be a
20716 parameter-declaration-clause, even when FIRST is
20717 false. Consider,
20718
20719 int i (int);
20720 int i (3);
20721
20722 The first is the declaration of a function while the
20723 second is the definition of a variable, including its
20724 initializer.
20725
20726 Having seen only the parenthesis, we cannot know which of
20727 these two alternatives should be selected. Even more
20728 complex are examples like:
20729
20730 int i (int (a));
20731 int i (int (3));
20732
20733 The former is a function-declaration; the latter is a
20734 variable initialization.
20735
20736 Thus again, we try a parameter-declaration-clause, and if
20737 that fails, we back out and return. */
20738
20739 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20740 {
20741 tree params;
20742 bool is_declarator = false;
20743
20744 open_paren = NULL;
20745
20746 /* In a member-declarator, the only valid interpretation
20747 of a parenthesis is the start of a
20748 parameter-declaration-clause. (It is invalid to
20749 initialize a static data member with a parenthesized
20750 initializer; only the "=" form of initialization is
20751 permitted.) */
20752 if (!member_p)
20753 cp_parser_parse_tentatively (parser);
20754
20755 /* Consume the `('. */
20756 matching_parens parens;
20757 parens.consume_open (parser);
20758 if (first)
20759 {
20760 /* If this is going to be an abstract declarator, we're
20761 in a declarator and we can't have default args. */
20762 parser->default_arg_ok_p = false;
20763 parser->in_declarator_p = true;
20764 }
20765
20766 begin_scope (sk_function_parms, NULL_TREE);
20767
20768 /* Parse the parameter-declaration-clause. */
20769 params
20770 = cp_parser_parameter_declaration_clause (parser, flags);
20771
20772 /* Consume the `)'. */
20773 parens.require_close (parser);
20774
20775 /* If all went well, parse the cv-qualifier-seq,
20776 ref-qualifier and the exception-specification. */
20777 if (member_p || cp_parser_parse_definitely (parser))
20778 {
20779 cp_cv_quals cv_quals;
20780 cp_virt_specifiers virt_specifiers;
20781 cp_ref_qualifier ref_qual;
20782 tree exception_specification;
20783 tree late_return;
20784 tree attrs;
20785 bool memfn = (member_p || (pushed_scope
20786 && CLASS_TYPE_P (pushed_scope)));
20787 unsigned char local_variables_forbidden_p
20788 = parser->local_variables_forbidden_p;
20789 /* 'this' is not allowed in static member functions. */
20790 if (static_p || friend_p)
20791 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
20792
20793 is_declarator = true;
20794
20795 if (ctor_dtor_or_conv_p)
20796 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20797 first = false;
20798
20799 /* Parse the cv-qualifier-seq. */
20800 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20801 /* Parse the ref-qualifier. */
20802 ref_qual = cp_parser_ref_qualifier_opt (parser);
20803 /* Parse the tx-qualifier. */
20804 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20805 /* And the exception-specification. */
20806 exception_specification
20807 = cp_parser_exception_specification_opt (parser);
20808
20809 attrs = cp_parser_std_attribute_spec_seq (parser);
20810
20811 /* In here, we handle cases where attribute is used after
20812 the function declaration. For example:
20813 void func (int x) __attribute__((vector(..))); */
20814 tree gnu_attrs = NULL_TREE;
20815 tree requires_clause = NULL_TREE;
20816 late_return = (cp_parser_late_return_type_opt
20817 (parser, declarator, requires_clause,
20818 memfn ? cv_quals : -1));
20819
20820 /* Parse the virt-specifier-seq. */
20821 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20822
20823 /* Create the function-declarator. */
20824 declarator = make_call_declarator (declarator,
20825 params,
20826 cv_quals,
20827 virt_specifiers,
20828 ref_qual,
20829 tx_qual,
20830 exception_specification,
20831 late_return,
20832 requires_clause);
20833 declarator->std_attributes = attrs;
20834 declarator->attributes = gnu_attrs;
20835 /* Any subsequent parameter lists are to do with
20836 return type, so are not those of the declared
20837 function. */
20838 parser->default_arg_ok_p = false;
20839
20840 /* Restore the state of local_variables_forbidden_p. */
20841 parser->local_variables_forbidden_p
20842 = local_variables_forbidden_p;
20843 }
20844
20845 /* Remove the function parms from scope. */
20846 pop_bindings_and_leave_scope ();
20847
20848 if (is_declarator)
20849 /* Repeat the main loop. */
20850 continue;
20851 }
20852
20853 /* If this is the first, we can try a parenthesized
20854 declarator. */
20855 if (first)
20856 {
20857 bool saved_in_type_id_in_expr_p;
20858
20859 parser->default_arg_ok_p = saved_default_arg_ok_p;
20860 parser->in_declarator_p = saved_in_declarator_p;
20861
20862 open_paren = token;
20863 /* Consume the `('. */
20864 matching_parens parens;
20865 parens.consume_open (parser);
20866 /* Parse the nested declarator. */
20867 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20868 parser->in_type_id_in_expr_p = true;
20869 declarator
20870 = cp_parser_declarator (parser, dcl_kind, flags,
20871 ctor_dtor_or_conv_p,
20872 /*parenthesized_p=*/NULL,
20873 member_p, friend_p,
20874 /*static_p=*/false);
20875 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20876 first = false;
20877 /* Expect a `)'. */
20878 close_paren = cp_lexer_peek_token (parser->lexer);
20879 if (!parens.require_close (parser))
20880 declarator = cp_error_declarator;
20881 if (declarator == cp_error_declarator)
20882 break;
20883
20884 goto handle_declarator;
20885 }
20886 /* Otherwise, we must be done. */
20887 else
20888 break;
20889 }
20890 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20891 && token->type == CPP_OPEN_SQUARE
20892 && !cp_next_tokens_can_be_attribute_p (parser))
20893 {
20894 /* Parse an array-declarator. */
20895 tree bounds, attrs;
20896
20897 if (ctor_dtor_or_conv_p)
20898 *ctor_dtor_or_conv_p = 0;
20899
20900 open_paren = NULL;
20901 first = false;
20902 parser->default_arg_ok_p = false;
20903 parser->in_declarator_p = true;
20904 /* Consume the `['. */
20905 cp_lexer_consume_token (parser->lexer);
20906 /* Peek at the next token. */
20907 token = cp_lexer_peek_token (parser->lexer);
20908 /* If the next token is `]', then there is no
20909 constant-expression. */
20910 if (token->type != CPP_CLOSE_SQUARE)
20911 {
20912 bool non_constant_p;
20913 bounds
20914 = cp_parser_constant_expression (parser,
20915 /*allow_non_constant=*/true,
20916 &non_constant_p);
20917 if (!non_constant_p)
20918 /* OK */;
20919 else if (error_operand_p (bounds))
20920 /* Already gave an error. */;
20921 else if (!parser->in_function_body
20922 || current_binding_level->kind == sk_function_parms)
20923 {
20924 /* Normally, the array bound must be an integral constant
20925 expression. However, as an extension, we allow VLAs
20926 in function scopes as long as they aren't part of a
20927 parameter declaration. */
20928 cp_parser_error (parser,
20929 "array bound is not an integer constant");
20930 bounds = error_mark_node;
20931 }
20932 else if (processing_template_decl
20933 && !type_dependent_expression_p (bounds))
20934 {
20935 /* Remember this wasn't a constant-expression. */
20936 bounds = build_nop (TREE_TYPE (bounds), bounds);
20937 TREE_SIDE_EFFECTS (bounds) = 1;
20938 }
20939 }
20940 else
20941 bounds = NULL_TREE;
20942 /* Look for the closing `]'. */
20943 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20944 {
20945 declarator = cp_error_declarator;
20946 break;
20947 }
20948
20949 attrs = cp_parser_std_attribute_spec_seq (parser);
20950 declarator = make_array_declarator (declarator, bounds);
20951 declarator->std_attributes = attrs;
20952 }
20953 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20954 {
20955 {
20956 tree qualifying_scope;
20957 tree unqualified_name;
20958 tree attrs;
20959 special_function_kind sfk;
20960 bool abstract_ok;
20961 bool pack_expansion_p = false;
20962 cp_token *declarator_id_start_token;
20963
20964 /* Parse a declarator-id */
20965 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20966 if (abstract_ok)
20967 {
20968 cp_parser_parse_tentatively (parser);
20969
20970 /* If we see an ellipsis, we should be looking at a
20971 parameter pack. */
20972 if (token->type == CPP_ELLIPSIS)
20973 {
20974 /* Consume the `...' */
20975 cp_lexer_consume_token (parser->lexer);
20976
20977 pack_expansion_p = true;
20978 }
20979 }
20980
20981 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20982 unqualified_name
20983 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20984 qualifying_scope = parser->scope;
20985 if (abstract_ok)
20986 {
20987 bool okay = false;
20988
20989 if (!unqualified_name && pack_expansion_p)
20990 {
20991 /* Check whether an error occurred. */
20992 okay = !cp_parser_error_occurred (parser);
20993
20994 /* We already consumed the ellipsis to mark a
20995 parameter pack, but we have no way to report it,
20996 so abort the tentative parse. We will be exiting
20997 immediately anyway. */
20998 cp_parser_abort_tentative_parse (parser);
20999 }
21000 else
21001 okay = cp_parser_parse_definitely (parser);
21002
21003 if (!okay)
21004 unqualified_name = error_mark_node;
21005 else if (unqualified_name
21006 && (qualifying_scope
21007 || (!identifier_p (unqualified_name))))
21008 {
21009 cp_parser_error (parser, "expected unqualified-id");
21010 unqualified_name = error_mark_node;
21011 }
21012 }
21013
21014 if (!unqualified_name)
21015 return NULL;
21016 if (unqualified_name == error_mark_node)
21017 {
21018 declarator = cp_error_declarator;
21019 pack_expansion_p = false;
21020 declarator->parameter_pack_p = false;
21021 break;
21022 }
21023
21024 attrs = cp_parser_std_attribute_spec_seq (parser);
21025
21026 if (qualifying_scope && at_namespace_scope_p ()
21027 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
21028 {
21029 /* In the declaration of a member of a template class
21030 outside of the class itself, the SCOPE will sometimes
21031 be a TYPENAME_TYPE. For example, given:
21032
21033 template <typename T>
21034 int S<T>::R::i = 3;
21035
21036 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
21037 this context, we must resolve S<T>::R to an ordinary
21038 type, rather than a typename type.
21039
21040 The reason we normally avoid resolving TYPENAME_TYPEs
21041 is that a specialization of `S' might render
21042 `S<T>::R' not a type. However, if `S' is
21043 specialized, then this `i' will not be used, so there
21044 is no harm in resolving the types here. */
21045 tree type;
21046
21047 /* Resolve the TYPENAME_TYPE. */
21048 type = resolve_typename_type (qualifying_scope,
21049 /*only_current_p=*/false);
21050 /* If that failed, the declarator is invalid. */
21051 if (TREE_CODE (type) == TYPENAME_TYPE)
21052 {
21053 if (typedef_variant_p (type))
21054 error_at (declarator_id_start_token->location,
21055 "cannot define member of dependent typedef "
21056 "%qT", type);
21057 else
21058 error_at (declarator_id_start_token->location,
21059 "%<%T::%E%> is not a type",
21060 TYPE_CONTEXT (qualifying_scope),
21061 TYPE_IDENTIFIER (qualifying_scope));
21062 }
21063 qualifying_scope = type;
21064 }
21065
21066 sfk = sfk_none;
21067
21068 if (unqualified_name)
21069 {
21070 tree class_type;
21071
21072 if (qualifying_scope
21073 && CLASS_TYPE_P (qualifying_scope))
21074 class_type = qualifying_scope;
21075 else
21076 class_type = current_class_type;
21077
21078 if (TREE_CODE (unqualified_name) == TYPE_DECL)
21079 {
21080 tree name_type = TREE_TYPE (unqualified_name);
21081
21082 if (!class_type || !same_type_p (name_type, class_type))
21083 {
21084 /* We do not attempt to print the declarator
21085 here because we do not have enough
21086 information about its original syntactic
21087 form. */
21088 cp_parser_error (parser, "invalid declarator");
21089 declarator = cp_error_declarator;
21090 break;
21091 }
21092 else if (qualifying_scope
21093 && CLASSTYPE_USE_TEMPLATE (name_type))
21094 {
21095 error_at (declarator_id_start_token->location,
21096 "invalid use of constructor as a template");
21097 inform (declarator_id_start_token->location,
21098 "use %<%T::%D%> instead of %<%T::%D%> to "
21099 "name the constructor in a qualified name",
21100 class_type,
21101 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
21102 class_type, name_type);
21103 declarator = cp_error_declarator;
21104 break;
21105 }
21106 unqualified_name = constructor_name (class_type);
21107 }
21108
21109 if (class_type)
21110 {
21111 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
21112 sfk = sfk_destructor;
21113 else if (identifier_p (unqualified_name)
21114 && IDENTIFIER_CONV_OP_P (unqualified_name))
21115 sfk = sfk_conversion;
21116 else if (/* There's no way to declare a constructor
21117 for an unnamed type, even if the type
21118 got a name for linkage purposes. */
21119 !TYPE_WAS_UNNAMED (class_type)
21120 /* Handle correctly (c++/19200):
21121
21122 struct S {
21123 struct T{};
21124 friend void S(T);
21125 };
21126
21127 and also:
21128
21129 namespace N {
21130 void S();
21131 }
21132
21133 struct S {
21134 friend void N::S();
21135 }; */
21136 && (!friend_p || class_type == qualifying_scope)
21137 && constructor_name_p (unqualified_name,
21138 class_type))
21139 sfk = sfk_constructor;
21140 else if (is_overloaded_fn (unqualified_name)
21141 && DECL_CONSTRUCTOR_P (get_first_fn
21142 (unqualified_name)))
21143 sfk = sfk_constructor;
21144
21145 if (ctor_dtor_or_conv_p && sfk != sfk_none)
21146 *ctor_dtor_or_conv_p = -1;
21147 }
21148 }
21149 declarator = make_id_declarator (qualifying_scope,
21150 unqualified_name,
21151 sfk, token->location);
21152 declarator->std_attributes = attrs;
21153 declarator->parameter_pack_p = pack_expansion_p;
21154
21155 if (pack_expansion_p)
21156 maybe_warn_variadic_templates ();
21157
21158 /* We're looking for this case in [temp.res]:
21159 A qualified-id is assumed to name a type if [...]
21160 - it is a decl-specifier of the decl-specifier-seq of a
21161 parameter-declaration in a declarator of a function or
21162 function template declaration, ... */
21163 if (cxx_dialect >= cxx2a
21164 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
21165 && declarator->kind == cdk_id
21166 && !at_class_scope_p ()
21167 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21168 {
21169 /* ...whose declarator-id is qualified. If it isn't, never
21170 assume the parameters to refer to types. */
21171 if (qualifying_scope == NULL_TREE)
21172 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21173 else
21174 {
21175 /* Now we have something like
21176 template <typename T> int C::x(S::p);
21177 which can be a function template declaration or a
21178 variable template definition. If name lookup for
21179 the declarator-id C::x finds one or more function
21180 templates, assume S::p to name a type. Otherwise,
21181 don't. */
21182 tree decl
21183 = cp_parser_lookup_name_simple (parser, unqualified_name,
21184 token->location);
21185 if (!is_overloaded_fn (decl)
21186 /* Allow
21187 template<typename T>
21188 A<T>::A(T::type) { } */
21189 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
21190 && constructor_name_p (unqualified_name,
21191 qualifying_scope)))
21192 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21193 }
21194 }
21195 }
21196
21197 handle_declarator:;
21198 scope = get_scope_of_declarator (declarator);
21199 if (scope)
21200 {
21201 /* Any names that appear after the declarator-id for a
21202 member are looked up in the containing scope. */
21203 if (at_function_scope_p ())
21204 {
21205 /* But declarations with qualified-ids can't appear in a
21206 function. */
21207 cp_parser_error (parser, "qualified-id in declaration");
21208 declarator = cp_error_declarator;
21209 break;
21210 }
21211 pushed_scope = push_scope (scope);
21212 }
21213 parser->in_declarator_p = true;
21214 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21215 || (declarator && declarator->kind == cdk_id))
21216 /* Default args are only allowed on function
21217 declarations. */
21218 parser->default_arg_ok_p = saved_default_arg_ok_p;
21219 else
21220 parser->default_arg_ok_p = false;
21221
21222 first = false;
21223 }
21224 /* We're done. */
21225 else
21226 break;
21227 }
21228
21229 /* For an abstract declarator, we might wind up with nothing at this
21230 point. That's an error; the declarator is not optional. */
21231 if (!declarator)
21232 cp_parser_error (parser, "expected declarator");
21233 else if (open_paren)
21234 {
21235 /* Record overly parenthesized declarator so we can give a
21236 diagnostic about confusing decl/expr disambiguation. */
21237 if (declarator->kind == cdk_array)
21238 {
21239 /* If the open and close parens are on different lines, this
21240 is probably a formatting thing, so ignore. */
21241 expanded_location open = expand_location (open_paren->location);
21242 expanded_location close = expand_location (close_paren->location);
21243 if (open.line != close.line || open.file != close.file)
21244 open_paren = NULL;
21245 }
21246 if (open_paren)
21247 declarator->parenthesized = open_paren->location;
21248 }
21249
21250 /* If we entered a scope, we must exit it now. */
21251 if (pushed_scope)
21252 pop_scope (pushed_scope);
21253
21254 parser->default_arg_ok_p = saved_default_arg_ok_p;
21255 parser->in_declarator_p = saved_in_declarator_p;
21256
21257 return declarator;
21258 }
21259
21260 /* Parse a ptr-operator.
21261
21262 ptr-operator:
21263 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21264 * cv-qualifier-seq [opt]
21265 &
21266 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21267 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21268
21269 GNU Extension:
21270
21271 ptr-operator:
21272 & cv-qualifier-seq [opt]
21273
21274 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21275 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21276 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21277 filled in with the TYPE containing the member. *CV_QUALS is
21278 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21279 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21280 Note that the tree codes returned by this function have nothing
21281 to do with the types of trees that will be eventually be created
21282 to represent the pointer or reference type being parsed. They are
21283 just constants with suggestive names. */
21284 static enum tree_code
21285 cp_parser_ptr_operator (cp_parser* parser,
21286 tree* type,
21287 cp_cv_quals *cv_quals,
21288 tree *attributes)
21289 {
21290 enum tree_code code = ERROR_MARK;
21291 cp_token *token;
21292 tree attrs = NULL_TREE;
21293
21294 /* Assume that it's not a pointer-to-member. */
21295 *type = NULL_TREE;
21296 /* And that there are no cv-qualifiers. */
21297 *cv_quals = TYPE_UNQUALIFIED;
21298
21299 /* Peek at the next token. */
21300 token = cp_lexer_peek_token (parser->lexer);
21301
21302 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21303 if (token->type == CPP_MULT)
21304 code = INDIRECT_REF;
21305 else if (token->type == CPP_AND)
21306 code = ADDR_EXPR;
21307 else if ((cxx_dialect != cxx98) &&
21308 token->type == CPP_AND_AND) /* C++0x only */
21309 code = NON_LVALUE_EXPR;
21310
21311 if (code != ERROR_MARK)
21312 {
21313 /* Consume the `*', `&' or `&&'. */
21314 cp_lexer_consume_token (parser->lexer);
21315
21316 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21317 `&', if we are allowing GNU extensions. (The only qualifier
21318 that can legally appear after `&' is `restrict', but that is
21319 enforced during semantic analysis. */
21320 if (code == INDIRECT_REF
21321 || cp_parser_allow_gnu_extensions_p (parser))
21322 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21323
21324 attrs = cp_parser_std_attribute_spec_seq (parser);
21325 if (attributes != NULL)
21326 *attributes = attrs;
21327 }
21328 else
21329 {
21330 /* Try the pointer-to-member case. */
21331 cp_parser_parse_tentatively (parser);
21332 /* Look for the optional `::' operator. */
21333 cp_parser_global_scope_opt (parser,
21334 /*current_scope_valid_p=*/false);
21335 /* Look for the nested-name specifier. */
21336 token = cp_lexer_peek_token (parser->lexer);
21337 cp_parser_nested_name_specifier (parser,
21338 /*typename_keyword_p=*/false,
21339 /*check_dependency_p=*/true,
21340 /*type_p=*/false,
21341 /*is_declaration=*/false);
21342 /* If we found it, and the next token is a `*', then we are
21343 indeed looking at a pointer-to-member operator. */
21344 if (!cp_parser_error_occurred (parser)
21345 && cp_parser_require (parser, CPP_MULT, RT_MULT))
21346 {
21347 /* Indicate that the `*' operator was used. */
21348 code = INDIRECT_REF;
21349
21350 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21351 error_at (token->location, "%qD is a namespace", parser->scope);
21352 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21353 error_at (token->location, "cannot form pointer to member of "
21354 "non-class %q#T", parser->scope);
21355 else
21356 {
21357 /* The type of which the member is a member is given by the
21358 current SCOPE. */
21359 *type = parser->scope;
21360 /* The next name will not be qualified. */
21361 parser->scope = NULL_TREE;
21362 parser->qualifying_scope = NULL_TREE;
21363 parser->object_scope = NULL_TREE;
21364 /* Look for optional c++11 attributes. */
21365 attrs = cp_parser_std_attribute_spec_seq (parser);
21366 if (attributes != NULL)
21367 *attributes = attrs;
21368 /* Look for the optional cv-qualifier-seq. */
21369 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21370 }
21371 }
21372 /* If that didn't work we don't have a ptr-operator. */
21373 if (!cp_parser_parse_definitely (parser))
21374 cp_parser_error (parser, "expected ptr-operator");
21375 }
21376
21377 return code;
21378 }
21379
21380 /* Parse an (optional) cv-qualifier-seq.
21381
21382 cv-qualifier-seq:
21383 cv-qualifier cv-qualifier-seq [opt]
21384
21385 cv-qualifier:
21386 const
21387 volatile
21388
21389 GNU Extension:
21390
21391 cv-qualifier:
21392 __restrict__
21393
21394 Returns a bitmask representing the cv-qualifiers. */
21395
21396 static cp_cv_quals
21397 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21398 {
21399 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21400
21401 while (true)
21402 {
21403 cp_token *token;
21404 cp_cv_quals cv_qualifier;
21405
21406 /* Peek at the next token. */
21407 token = cp_lexer_peek_token (parser->lexer);
21408 /* See if it's a cv-qualifier. */
21409 switch (token->keyword)
21410 {
21411 case RID_CONST:
21412 cv_qualifier = TYPE_QUAL_CONST;
21413 break;
21414
21415 case RID_VOLATILE:
21416 cv_qualifier = TYPE_QUAL_VOLATILE;
21417 break;
21418
21419 case RID_RESTRICT:
21420 cv_qualifier = TYPE_QUAL_RESTRICT;
21421 break;
21422
21423 default:
21424 cv_qualifier = TYPE_UNQUALIFIED;
21425 break;
21426 }
21427
21428 if (!cv_qualifier)
21429 break;
21430
21431 if (cv_quals & cv_qualifier)
21432 {
21433 gcc_rich_location richloc (token->location);
21434 richloc.add_fixit_remove ();
21435 error_at (&richloc, "duplicate cv-qualifier");
21436 cp_lexer_purge_token (parser->lexer);
21437 }
21438 else
21439 {
21440 cp_lexer_consume_token (parser->lexer);
21441 cv_quals |= cv_qualifier;
21442 }
21443 }
21444
21445 return cv_quals;
21446 }
21447
21448 /* Parse an (optional) ref-qualifier
21449
21450 ref-qualifier:
21451 &
21452 &&
21453
21454 Returns cp_ref_qualifier representing ref-qualifier. */
21455
21456 static cp_ref_qualifier
21457 cp_parser_ref_qualifier_opt (cp_parser* parser)
21458 {
21459 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21460
21461 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21462 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21463 return ref_qual;
21464
21465 while (true)
21466 {
21467 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21468 cp_token *token = cp_lexer_peek_token (parser->lexer);
21469
21470 switch (token->type)
21471 {
21472 case CPP_AND:
21473 curr_ref_qual = REF_QUAL_LVALUE;
21474 break;
21475
21476 case CPP_AND_AND:
21477 curr_ref_qual = REF_QUAL_RVALUE;
21478 break;
21479
21480 default:
21481 curr_ref_qual = REF_QUAL_NONE;
21482 break;
21483 }
21484
21485 if (!curr_ref_qual)
21486 break;
21487 else if (ref_qual)
21488 {
21489 error_at (token->location, "multiple ref-qualifiers");
21490 cp_lexer_purge_token (parser->lexer);
21491 }
21492 else
21493 {
21494 ref_qual = curr_ref_qual;
21495 cp_lexer_consume_token (parser->lexer);
21496 }
21497 }
21498
21499 return ref_qual;
21500 }
21501
21502 /* Parse an optional tx-qualifier.
21503
21504 tx-qualifier:
21505 transaction_safe
21506 transaction_safe_dynamic */
21507
21508 static tree
21509 cp_parser_tx_qualifier_opt (cp_parser *parser)
21510 {
21511 cp_token *token = cp_lexer_peek_token (parser->lexer);
21512 if (token->type == CPP_NAME)
21513 {
21514 tree name = token->u.value;
21515 const char *p = IDENTIFIER_POINTER (name);
21516 const int len = strlen ("transaction_safe");
21517 if (!strncmp (p, "transaction_safe", len))
21518 {
21519 p += len;
21520 if (*p == '\0'
21521 || !strcmp (p, "_dynamic"))
21522 {
21523 cp_lexer_consume_token (parser->lexer);
21524 if (!flag_tm)
21525 {
21526 error ("%qE requires %<-fgnu-tm%>", name);
21527 return NULL_TREE;
21528 }
21529 else
21530 return name;
21531 }
21532 }
21533 }
21534 return NULL_TREE;
21535 }
21536
21537 /* Parse an (optional) virt-specifier-seq.
21538
21539 virt-specifier-seq:
21540 virt-specifier virt-specifier-seq [opt]
21541
21542 virt-specifier:
21543 override
21544 final
21545
21546 Returns a bitmask representing the virt-specifiers. */
21547
21548 static cp_virt_specifiers
21549 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21550 {
21551 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21552
21553 while (true)
21554 {
21555 cp_token *token;
21556 cp_virt_specifiers virt_specifier;
21557
21558 /* Peek at the next token. */
21559 token = cp_lexer_peek_token (parser->lexer);
21560 /* See if it's a virt-specifier-qualifier. */
21561 if (token->type != CPP_NAME)
21562 break;
21563 if (id_equal (token->u.value, "override"))
21564 {
21565 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21566 virt_specifier = VIRT_SPEC_OVERRIDE;
21567 }
21568 else if (id_equal (token->u.value, "final"))
21569 {
21570 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21571 virt_specifier = VIRT_SPEC_FINAL;
21572 }
21573 else if (id_equal (token->u.value, "__final"))
21574 {
21575 virt_specifier = VIRT_SPEC_FINAL;
21576 }
21577 else
21578 break;
21579
21580 if (virt_specifiers & virt_specifier)
21581 {
21582 gcc_rich_location richloc (token->location);
21583 richloc.add_fixit_remove ();
21584 error_at (&richloc, "duplicate virt-specifier");
21585 cp_lexer_purge_token (parser->lexer);
21586 }
21587 else
21588 {
21589 cp_lexer_consume_token (parser->lexer);
21590 virt_specifiers |= virt_specifier;
21591 }
21592 }
21593 return virt_specifiers;
21594 }
21595
21596 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21597 is in scope even though it isn't real. */
21598
21599 void
21600 inject_this_parameter (tree ctype, cp_cv_quals quals)
21601 {
21602 tree this_parm;
21603
21604 if (current_class_ptr)
21605 {
21606 /* We don't clear this between NSDMIs. Is it already what we want? */
21607 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21608 if (DECL_P (current_class_ptr)
21609 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21610 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21611 && cp_type_quals (type) == quals)
21612 return;
21613 }
21614
21615 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21616 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21617 current_class_ptr = NULL_TREE;
21618 current_class_ref
21619 = cp_build_fold_indirect_ref (this_parm);
21620 current_class_ptr = this_parm;
21621 }
21622
21623 /* Return true iff our current scope is a non-static data member
21624 initializer. */
21625
21626 bool
21627 parsing_nsdmi (void)
21628 {
21629 /* We recognize NSDMI context by the context-less 'this' pointer set up
21630 by the function above. */
21631 if (current_class_ptr
21632 && TREE_CODE (current_class_ptr) == PARM_DECL
21633 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21634 return true;
21635 return false;
21636 }
21637
21638 /* Parse a late-specified return type, if any. This is not a separate
21639 non-terminal, but part of a function declarator, which looks like
21640
21641 -> trailing-type-specifier-seq abstract-declarator(opt)
21642
21643 Returns the type indicated by the type-id.
21644
21645 In addition to this, parse any queued up #pragma omp declare simd
21646 clauses, and #pragma acc routine clauses.
21647
21648 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21649 function. */
21650
21651 static tree
21652 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21653 tree& requires_clause, cp_cv_quals quals)
21654 {
21655 cp_token *token;
21656 tree type = NULL_TREE;
21657 bool declare_simd_p = (parser->omp_declare_simd
21658 && declarator
21659 && declarator->kind == cdk_id);
21660
21661 bool oacc_routine_p = (parser->oacc_routine
21662 && declarator
21663 && declarator->kind == cdk_id);
21664
21665 /* Peek at the next token. */
21666 token = cp_lexer_peek_token (parser->lexer);
21667 /* A late-specified return type is indicated by an initial '->'. */
21668 if (token->type != CPP_DEREF
21669 && token->keyword != RID_REQUIRES
21670 && !(token->type == CPP_NAME
21671 && token->u.value == ridpointers[RID_REQUIRES])
21672 && !(declare_simd_p || oacc_routine_p))
21673 return NULL_TREE;
21674
21675 tree save_ccp = current_class_ptr;
21676 tree save_ccr = current_class_ref;
21677 if (quals >= 0)
21678 {
21679 /* DR 1207: 'this' is in scope in the trailing return type. */
21680 inject_this_parameter (current_class_type, quals);
21681 }
21682
21683 if (token->type == CPP_DEREF)
21684 {
21685 /* Consume the ->. */
21686 cp_lexer_consume_token (parser->lexer);
21687
21688 type = cp_parser_trailing_type_id (parser);
21689 }
21690
21691 /* Function declarations may be followed by a trailing
21692 requires-clause. */
21693 requires_clause = cp_parser_requires_clause_opt (parser);
21694
21695 if (declare_simd_p)
21696 declarator->attributes
21697 = cp_parser_late_parsing_omp_declare_simd (parser,
21698 declarator->attributes);
21699 if (oacc_routine_p)
21700 declarator->attributes
21701 = cp_parser_late_parsing_oacc_routine (parser,
21702 declarator->attributes);
21703
21704 if (quals >= 0)
21705 {
21706 current_class_ptr = save_ccp;
21707 current_class_ref = save_ccr;
21708 }
21709
21710 return type;
21711 }
21712
21713 /* Parse a declarator-id.
21714
21715 declarator-id:
21716 id-expression
21717 :: [opt] nested-name-specifier [opt] type-name
21718
21719 In the `id-expression' case, the value returned is as for
21720 cp_parser_id_expression if the id-expression was an unqualified-id.
21721 If the id-expression was a qualified-id, then a SCOPE_REF is
21722 returned. The first operand is the scope (either a NAMESPACE_DECL
21723 or TREE_TYPE), but the second is still just a representation of an
21724 unqualified-id. */
21725
21726 static tree
21727 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21728 {
21729 tree id;
21730 /* The expression must be an id-expression. Assume that qualified
21731 names are the names of types so that:
21732
21733 template <class T>
21734 int S<T>::R::i = 3;
21735
21736 will work; we must treat `S<T>::R' as the name of a type.
21737 Similarly, assume that qualified names are templates, where
21738 required, so that:
21739
21740 template <class T>
21741 int S<T>::R<T>::i = 3;
21742
21743 will work, too. */
21744 id = cp_parser_id_expression (parser,
21745 /*template_keyword_p=*/false,
21746 /*check_dependency_p=*/false,
21747 /*template_p=*/NULL,
21748 /*declarator_p=*/true,
21749 optional_p);
21750 if (id && BASELINK_P (id))
21751 id = BASELINK_FUNCTIONS (id);
21752 return id;
21753 }
21754
21755 /* Parse a type-id.
21756
21757 type-id:
21758 type-specifier-seq abstract-declarator [opt]
21759
21760 The parser flags FLAGS is used to control type-specifier parsing.
21761
21762 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21763
21764 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21765 i.e. we've just seen "->".
21766
21767 Returns the TYPE specified. */
21768
21769 static tree
21770 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
21771 bool is_template_arg, bool is_trailing_return,
21772 location_t *type_location)
21773 {
21774 cp_decl_specifier_seq type_specifier_seq;
21775 cp_declarator *abstract_declarator;
21776
21777 /* Parse the type-specifier-seq. */
21778 cp_parser_type_specifier_seq (parser, flags,
21779 /*is_declaration=*/false,
21780 is_trailing_return,
21781 &type_specifier_seq);
21782 if (type_location)
21783 *type_location = type_specifier_seq.locations[ds_type_spec];
21784
21785 if (is_template_arg && type_specifier_seq.type
21786 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21787 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21788 /* A bare template name as a template argument is a template template
21789 argument, not a placeholder, so fail parsing it as a type argument. */
21790 {
21791 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21792 cp_parser_simulate_error (parser);
21793 return error_mark_node;
21794 }
21795 if (type_specifier_seq.type == error_mark_node)
21796 return error_mark_node;
21797
21798 /* There might or might not be an abstract declarator. */
21799 cp_parser_parse_tentatively (parser);
21800 /* Look for the declarator. */
21801 abstract_declarator
21802 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
21803 CP_PARSER_FLAGS_NONE, NULL,
21804 /*parenthesized_p=*/NULL,
21805 /*member_p=*/false,
21806 /*friend_p=*/false,
21807 /*static_p=*/false);
21808 /* Check to see if there really was a declarator. */
21809 if (!cp_parser_parse_definitely (parser))
21810 abstract_declarator = NULL;
21811
21812 if (type_specifier_seq.type
21813 /* The concepts TS allows 'auto' as a type-id. */
21814 && (!flag_concepts || parser->in_type_id_in_expr_p)
21815 /* None of the valid uses of 'auto' in C++14 involve the type-id
21816 nonterminal, but it is valid in a trailing-return-type. */
21817 && !(cxx_dialect >= cxx14 && is_trailing_return))
21818 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21819 {
21820 /* A type-id with type 'auto' is only ok if the abstract declarator
21821 is a function declarator with a late-specified return type.
21822
21823 A type-id with 'auto' is also valid in a trailing-return-type
21824 in a compound-requirement. */
21825 if (abstract_declarator
21826 && abstract_declarator->kind == cdk_function
21827 && abstract_declarator->u.function.late_return_type)
21828 /* OK */;
21829 else if (parser->in_result_type_constraint_p)
21830 /* OK */;
21831 else
21832 {
21833 location_t loc = type_specifier_seq.locations[ds_type_spec];
21834 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21835 {
21836 error_at (loc, "missing template arguments after %qT",
21837 auto_node);
21838 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21839 tmpl);
21840 }
21841 else
21842 error_at (loc, "invalid use of %qT", auto_node);
21843 return error_mark_node;
21844 }
21845 }
21846
21847 return groktypename (&type_specifier_seq, abstract_declarator,
21848 is_template_arg);
21849 }
21850
21851 /* Wrapper for cp_parser_type_id_1. */
21852
21853 static tree
21854 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
21855 location_t *type_location)
21856 {
21857 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
21858 }
21859
21860 /* Wrapper for cp_parser_type_id_1. */
21861
21862 static tree
21863 cp_parser_template_type_arg (cp_parser *parser)
21864 {
21865 tree r;
21866 const char *saved_message = parser->type_definition_forbidden_message;
21867 parser->type_definition_forbidden_message
21868 = G_("types may not be defined in template arguments");
21869 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
21870 parser->type_definition_forbidden_message = saved_message;
21871 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21872 {
21873 error ("invalid use of %<auto%> in template argument");
21874 r = error_mark_node;
21875 }
21876 return r;
21877 }
21878
21879 /* Wrapper for cp_parser_type_id_1. */
21880
21881 static tree
21882 cp_parser_trailing_type_id (cp_parser *parser)
21883 {
21884 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21885 false, true, NULL);
21886 }
21887
21888 /* Parse a type-specifier-seq.
21889
21890 type-specifier-seq:
21891 type-specifier type-specifier-seq [opt]
21892
21893 GNU extension:
21894
21895 type-specifier-seq:
21896 attributes type-specifier-seq [opt]
21897
21898 The parser flags FLAGS is used to control type-specifier parsing.
21899
21900 If IS_DECLARATION is true, we are at the start of a "condition" or
21901 exception-declaration, so we might be followed by a declarator-id.
21902
21903 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21904 i.e. we've just seen "->".
21905
21906 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21907
21908 static void
21909 cp_parser_type_specifier_seq (cp_parser* parser,
21910 cp_parser_flags flags,
21911 bool is_declaration,
21912 bool is_trailing_return,
21913 cp_decl_specifier_seq *type_specifier_seq)
21914 {
21915 bool seen_type_specifier = false;
21916 cp_token *start_token = NULL;
21917
21918 /* Clear the TYPE_SPECIFIER_SEQ. */
21919 clear_decl_specs (type_specifier_seq);
21920
21921 flags |= CP_PARSER_FLAGS_OPTIONAL;
21922 /* In the context of a trailing return type, enum E { } is an
21923 elaborated-type-specifier followed by a function-body, not an
21924 enum-specifier. */
21925 if (is_trailing_return)
21926 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21927
21928 /* Parse the type-specifiers and attributes. */
21929 while (true)
21930 {
21931 tree type_specifier;
21932 bool is_cv_qualifier;
21933
21934 /* Check for attributes first. */
21935 if (cp_next_tokens_can_be_attribute_p (parser))
21936 {
21937 type_specifier_seq->attributes
21938 = attr_chainon (type_specifier_seq->attributes,
21939 cp_parser_attributes_opt (parser));
21940 continue;
21941 }
21942
21943 /* record the token of the beginning of the type specifier seq,
21944 for error reporting purposes*/
21945 if (!start_token)
21946 start_token = cp_lexer_peek_token (parser->lexer);
21947
21948 /* Look for the type-specifier. */
21949 type_specifier = cp_parser_type_specifier (parser,
21950 flags,
21951 type_specifier_seq,
21952 /*is_declaration=*/false,
21953 NULL,
21954 &is_cv_qualifier);
21955 if (!type_specifier)
21956 {
21957 /* If the first type-specifier could not be found, this is not a
21958 type-specifier-seq at all. */
21959 if (!seen_type_specifier)
21960 {
21961 /* Set in_declarator_p to avoid skipping to the semicolon. */
21962 int in_decl = parser->in_declarator_p;
21963 parser->in_declarator_p = true;
21964
21965 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21966 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21967 cp_parser_error (parser, "expected type-specifier");
21968
21969 parser->in_declarator_p = in_decl;
21970
21971 type_specifier_seq->type = error_mark_node;
21972 return;
21973 }
21974 /* If subsequent type-specifiers could not be found, the
21975 type-specifier-seq is complete. */
21976 break;
21977 }
21978
21979 seen_type_specifier = true;
21980 /* The standard says that a condition can be:
21981
21982 type-specifier-seq declarator = assignment-expression
21983
21984 However, given:
21985
21986 struct S {};
21987 if (int S = ...)
21988
21989 we should treat the "S" as a declarator, not as a
21990 type-specifier. The standard doesn't say that explicitly for
21991 type-specifier-seq, but it does say that for
21992 decl-specifier-seq in an ordinary declaration. Perhaps it
21993 would be clearer just to allow a decl-specifier-seq here, and
21994 then add a semantic restriction that if any decl-specifiers
21995 that are not type-specifiers appear, the program is invalid. */
21996 if (is_declaration && !is_cv_qualifier)
21997 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21998 }
21999 }
22000
22001 /* Return whether the function currently being declared has an associated
22002 template parameter list. */
22003
22004 static bool
22005 function_being_declared_is_template_p (cp_parser* parser)
22006 {
22007 if (!current_template_parms || processing_template_parmlist)
22008 return false;
22009
22010 if (parser->implicit_template_scope)
22011 return true;
22012
22013 if (at_class_scope_p ()
22014 && TYPE_BEING_DEFINED (current_class_type))
22015 return parser->num_template_parameter_lists != 0;
22016
22017 return ((int) parser->num_template_parameter_lists > template_class_depth
22018 (current_class_type));
22019 }
22020
22021 /* Parse a parameter-declaration-clause.
22022
22023 parameter-declaration-clause:
22024 parameter-declaration-list [opt] ... [opt]
22025 parameter-declaration-list , ...
22026
22027 The parser flags FLAGS is used to control type-specifier parsing.
22028
22029 Returns a representation for the parameter declarations. A return
22030 value of NULL indicates a parameter-declaration-clause consisting
22031 only of an ellipsis. */
22032
22033 static tree
22034 cp_parser_parameter_declaration_clause (cp_parser* parser,
22035 cp_parser_flags flags)
22036 {
22037 tree parameters;
22038 cp_token *token;
22039 bool ellipsis_p;
22040
22041 temp_override<bool> cleanup
22042 (parser->auto_is_implicit_function_template_parm_p);
22043
22044 if (!processing_specialization
22045 && !processing_template_parmlist
22046 && !processing_explicit_instantiation
22047 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22048 actual function or a random abstract declarator. */
22049 && parser->default_arg_ok_p)
22050 if (!current_function_decl
22051 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
22052 parser->auto_is_implicit_function_template_parm_p = true;
22053
22054 /* Peek at the next token. */
22055 token = cp_lexer_peek_token (parser->lexer);
22056 /* Check for trivial parameter-declaration-clauses. */
22057 if (token->type == CPP_ELLIPSIS)
22058 {
22059 /* Consume the `...' token. */
22060 cp_lexer_consume_token (parser->lexer);
22061 return NULL_TREE;
22062 }
22063 else if (token->type == CPP_CLOSE_PAREN)
22064 /* There are no parameters. */
22065 return void_list_node;
22066 /* Check for `(void)', too, which is a special case. */
22067 else if (token->keyword == RID_VOID
22068 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22069 == CPP_CLOSE_PAREN))
22070 {
22071 /* Consume the `void' token. */
22072 cp_lexer_consume_token (parser->lexer);
22073 /* There are no parameters. */
22074 return void_list_node;
22075 }
22076
22077 /* Parse the parameter-declaration-list. */
22078 parameters = cp_parser_parameter_declaration_list (parser, flags);
22079 /* If a parse error occurred while parsing the
22080 parameter-declaration-list, then the entire
22081 parameter-declaration-clause is erroneous. */
22082 if (parameters == error_mark_node)
22083 return NULL_TREE;
22084
22085 /* Peek at the next token. */
22086 token = cp_lexer_peek_token (parser->lexer);
22087 /* If it's a `,', the clause should terminate with an ellipsis. */
22088 if (token->type == CPP_COMMA)
22089 {
22090 /* Consume the `,'. */
22091 cp_lexer_consume_token (parser->lexer);
22092 /* Expect an ellipsis. */
22093 ellipsis_p
22094 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
22095 }
22096 /* It might also be `...' if the optional trailing `,' was
22097 omitted. */
22098 else if (token->type == CPP_ELLIPSIS)
22099 {
22100 /* Consume the `...' token. */
22101 cp_lexer_consume_token (parser->lexer);
22102 /* And remember that we saw it. */
22103 ellipsis_p = true;
22104 }
22105 else
22106 ellipsis_p = false;
22107
22108 /* Finish the parameter list. */
22109 if (!ellipsis_p)
22110 parameters = chainon (parameters, void_list_node);
22111
22112 return parameters;
22113 }
22114
22115 /* Parse a parameter-declaration-list.
22116
22117 parameter-declaration-list:
22118 parameter-declaration
22119 parameter-declaration-list , parameter-declaration
22120
22121 The parser flags FLAGS is used to control type-specifier parsing.
22122
22123 Returns a representation of the parameter-declaration-list, as for
22124 cp_parser_parameter_declaration_clause. However, the
22125 `void_list_node' is never appended to the list. */
22126
22127 static tree
22128 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
22129 {
22130 tree parameters = NULL_TREE;
22131 tree *tail = &parameters;
22132 bool saved_in_unbraced_linkage_specification_p;
22133 int index = 0;
22134
22135 /* The special considerations that apply to a function within an
22136 unbraced linkage specifications do not apply to the parameters
22137 to the function. */
22138 saved_in_unbraced_linkage_specification_p
22139 = parser->in_unbraced_linkage_specification_p;
22140 parser->in_unbraced_linkage_specification_p = false;
22141
22142 /* Look for more parameters. */
22143 while (true)
22144 {
22145 cp_parameter_declarator *parameter;
22146 tree decl = error_mark_node;
22147 bool parenthesized_p = false;
22148
22149 /* Parse the parameter. */
22150 parameter
22151 = cp_parser_parameter_declaration (parser, flags,
22152 /*template_parm_p=*/false,
22153 &parenthesized_p);
22154
22155 /* We don't know yet if the enclosing context is deprecated, so wait
22156 and warn in grokparms if appropriate. */
22157 deprecated_state = DEPRECATED_SUPPRESS;
22158
22159 if (parameter)
22160 {
22161 decl = grokdeclarator (parameter->declarator,
22162 &parameter->decl_specifiers,
22163 PARM,
22164 parameter->default_argument != NULL_TREE,
22165 &parameter->decl_specifiers.attributes);
22166 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
22167 DECL_SOURCE_LOCATION (decl) = parameter->loc;
22168 }
22169
22170 deprecated_state = DEPRECATED_NORMAL;
22171
22172 /* If a parse error occurred parsing the parameter declaration,
22173 then the entire parameter-declaration-list is erroneous. */
22174 if (decl == error_mark_node)
22175 {
22176 parameters = error_mark_node;
22177 break;
22178 }
22179
22180 if (parameter->decl_specifiers.attributes)
22181 cplus_decl_attributes (&decl,
22182 parameter->decl_specifiers.attributes,
22183 0);
22184 if (DECL_NAME (decl))
22185 decl = pushdecl (decl);
22186
22187 if (decl != error_mark_node)
22188 {
22189 retrofit_lang_decl (decl);
22190 DECL_PARM_INDEX (decl) = ++index;
22191 DECL_PARM_LEVEL (decl) = function_parm_depth ();
22192 }
22193
22194 /* Add the new parameter to the list. */
22195 *tail = build_tree_list (parameter->default_argument, decl);
22196 tail = &TREE_CHAIN (*tail);
22197
22198 /* Peek at the next token. */
22199 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22200 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22201 /* These are for Objective-C++ */
22202 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22203 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22204 /* The parameter-declaration-list is complete. */
22205 break;
22206 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22207 {
22208 cp_token *token;
22209
22210 /* Peek at the next token. */
22211 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22212 /* If it's an ellipsis, then the list is complete. */
22213 if (token->type == CPP_ELLIPSIS)
22214 break;
22215 /* Otherwise, there must be more parameters. Consume the
22216 `,'. */
22217 cp_lexer_consume_token (parser->lexer);
22218 /* When parsing something like:
22219
22220 int i(float f, double d)
22221
22222 we can tell after seeing the declaration for "f" that we
22223 are not looking at an initialization of a variable "i",
22224 but rather at the declaration of a function "i".
22225
22226 Due to the fact that the parsing of template arguments
22227 (as specified to a template-id) requires backtracking we
22228 cannot use this technique when inside a template argument
22229 list. */
22230 if (!parser->in_template_argument_list_p
22231 && !parser->in_type_id_in_expr_p
22232 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22233 /* However, a parameter-declaration of the form
22234 "float(f)" (which is a valid declaration of a
22235 parameter "f") can also be interpreted as an
22236 expression (the conversion of "f" to "float"). */
22237 && !parenthesized_p)
22238 cp_parser_commit_to_tentative_parse (parser);
22239 }
22240 else
22241 {
22242 cp_parser_error (parser, "expected %<,%> or %<...%>");
22243 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22244 cp_parser_skip_to_closing_parenthesis (parser,
22245 /*recovering=*/true,
22246 /*or_comma=*/false,
22247 /*consume_paren=*/false);
22248 break;
22249 }
22250 }
22251
22252 parser->in_unbraced_linkage_specification_p
22253 = saved_in_unbraced_linkage_specification_p;
22254
22255 /* Reset implicit_template_scope if we are about to leave the function
22256 parameter list that introduced it. Note that for out-of-line member
22257 definitions, there will be one or more class scopes before we get to
22258 the template parameter scope. */
22259
22260 if (cp_binding_level *its = parser->implicit_template_scope)
22261 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
22262 {
22263 while (maybe_its->kind == sk_class)
22264 maybe_its = maybe_its->level_chain;
22265 if (maybe_its == its)
22266 {
22267 parser->implicit_template_parms = 0;
22268 parser->implicit_template_scope = 0;
22269 }
22270 }
22271
22272 return parameters;
22273 }
22274
22275 /* Parse a parameter declaration.
22276
22277 parameter-declaration:
22278 decl-specifier-seq ... [opt] declarator
22279 decl-specifier-seq declarator = assignment-expression
22280 decl-specifier-seq ... [opt] abstract-declarator [opt]
22281 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22282
22283 The parser flags FLAGS is used to control type-specifier parsing.
22284
22285 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22286 declares a template parameter. (In that case, a non-nested `>'
22287 token encountered during the parsing of the assignment-expression
22288 is not interpreted as a greater-than operator.)
22289
22290 Returns a representation of the parameter, or NULL if an error
22291 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22292 true iff the declarator is of the form "(p)". */
22293
22294 static cp_parameter_declarator *
22295 cp_parser_parameter_declaration (cp_parser *parser,
22296 cp_parser_flags flags,
22297 bool template_parm_p,
22298 bool *parenthesized_p)
22299 {
22300 int declares_class_or_enum;
22301 cp_decl_specifier_seq decl_specifiers;
22302 cp_declarator *declarator;
22303 tree default_argument;
22304 cp_token *token = NULL, *declarator_token_start = NULL;
22305 const char *saved_message;
22306 bool template_parameter_pack_p = false;
22307
22308 /* In a template parameter, `>' is not an operator.
22309
22310 [temp.param]
22311
22312 When parsing a default template-argument for a non-type
22313 template-parameter, the first non-nested `>' is taken as the end
22314 of the template parameter-list rather than a greater-than
22315 operator. */
22316
22317 /* Type definitions may not appear in parameter types. */
22318 saved_message = parser->type_definition_forbidden_message;
22319 parser->type_definition_forbidden_message
22320 = G_("types may not be defined in parameter types");
22321
22322 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
22323 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22324 (current_template_parms)) : 0);
22325
22326 /* Parse the declaration-specifiers. */
22327 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22328 cp_parser_decl_specifier_seq (parser,
22329 flags,
22330 &decl_specifiers,
22331 &declares_class_or_enum);
22332
22333 /* Complain about missing 'typename' or other invalid type names. */
22334 if (!decl_specifiers.any_type_specifiers_p
22335 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22336 decl_specifiers.type = error_mark_node;
22337
22338 /* If an error occurred, there's no reason to attempt to parse the
22339 rest of the declaration. */
22340 if (cp_parser_error_occurred (parser))
22341 {
22342 parser->type_definition_forbidden_message = saved_message;
22343 return NULL;
22344 }
22345
22346 /* Peek at the next token. */
22347 token = cp_lexer_peek_token (parser->lexer);
22348
22349 /* If the next token is a `)', `,', `=', `>', or `...', then there
22350 is no declarator. However, when variadic templates are enabled,
22351 there may be a declarator following `...'. */
22352 if (token->type == CPP_CLOSE_PAREN
22353 || token->type == CPP_COMMA
22354 || token->type == CPP_EQ
22355 || token->type == CPP_GREATER)
22356 {
22357 declarator = NULL;
22358 if (parenthesized_p)
22359 *parenthesized_p = false;
22360 }
22361 /* Otherwise, there should be a declarator. */
22362 else
22363 {
22364 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22365 parser->default_arg_ok_p = false;
22366
22367 /* After seeing a decl-specifier-seq, if the next token is not a
22368 "(", there is no possibility that the code is a valid
22369 expression. Therefore, if parsing tentatively, we commit at
22370 this point. */
22371 if (!parser->in_template_argument_list_p
22372 /* In an expression context, having seen:
22373
22374 (int((char ...
22375
22376 we cannot be sure whether we are looking at a
22377 function-type (taking a "char" as a parameter) or a cast
22378 of some object of type "char" to "int". */
22379 && !parser->in_type_id_in_expr_p
22380 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22381 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22382 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22383 cp_parser_commit_to_tentative_parse (parser);
22384 /* Parse the declarator. */
22385 declarator_token_start = token;
22386 declarator = cp_parser_declarator (parser,
22387 CP_PARSER_DECLARATOR_EITHER,
22388 CP_PARSER_FLAGS_NONE,
22389 /*ctor_dtor_or_conv_p=*/NULL,
22390 parenthesized_p,
22391 /*member_p=*/false,
22392 /*friend_p=*/false,
22393 /*static_p=*/false);
22394 parser->default_arg_ok_p = saved_default_arg_ok_p;
22395 /* After the declarator, allow more attributes. */
22396 decl_specifiers.attributes
22397 = attr_chainon (decl_specifiers.attributes,
22398 cp_parser_attributes_opt (parser));
22399
22400 /* If the declarator is a template parameter pack, remember that and
22401 clear the flag in the declarator itself so we don't get errors
22402 from grokdeclarator. */
22403 if (template_parm_p && declarator && declarator->parameter_pack_p)
22404 {
22405 declarator->parameter_pack_p = false;
22406 template_parameter_pack_p = true;
22407 }
22408 }
22409
22410 /* If the next token is an ellipsis, and we have not seen a declarator
22411 name, and if either the type of the declarator contains parameter
22412 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22413 for, eg, abbreviated integral type names), then we actually have a
22414 parameter pack expansion expression. Otherwise, leave the ellipsis
22415 for a C-style variadic function. */
22416 token = cp_lexer_peek_token (parser->lexer);
22417
22418 /* If a function parameter pack was specified and an implicit template
22419 parameter was introduced during cp_parser_parameter_declaration,
22420 change any implicit parameters introduced into packs. */
22421 if (parser->implicit_template_parms
22422 && ((token->type == CPP_ELLIPSIS
22423 && declarator_can_be_parameter_pack (declarator))
22424 || (declarator && declarator->parameter_pack_p)))
22425 {
22426 int latest_template_parm_idx = TREE_VEC_LENGTH
22427 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
22428
22429 if (latest_template_parm_idx != template_parm_idx)
22430 decl_specifiers.type = convert_generic_types_to_packs
22431 (decl_specifiers.type,
22432 template_parm_idx, latest_template_parm_idx);
22433 }
22434
22435 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22436 {
22437 tree type = decl_specifiers.type;
22438
22439 if (type && DECL_P (type))
22440 type = TREE_TYPE (type);
22441
22442 if (((type
22443 && TREE_CODE (type) != TYPE_PACK_EXPANSION
22444 && (template_parm_p || uses_parameter_packs (type)))
22445 || (!type && template_parm_p))
22446 && declarator_can_be_parameter_pack (declarator))
22447 {
22448 /* Consume the `...'. */
22449 cp_lexer_consume_token (parser->lexer);
22450 maybe_warn_variadic_templates ();
22451
22452 /* Build a pack expansion type */
22453 if (template_parm_p)
22454 template_parameter_pack_p = true;
22455 else if (declarator)
22456 declarator->parameter_pack_p = true;
22457 else
22458 decl_specifiers.type = make_pack_expansion (type);
22459 }
22460 }
22461
22462 /* The restriction on defining new types applies only to the type
22463 of the parameter, not to the default argument. */
22464 parser->type_definition_forbidden_message = saved_message;
22465
22466 /* If the next token is `=', then process a default argument. */
22467 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22468 {
22469 tree type = decl_specifiers.type;
22470 token = cp_lexer_peek_token (parser->lexer);
22471 /* If we are defining a class, then the tokens that make up the
22472 default argument must be saved and processed later. */
22473 if (!template_parm_p && at_class_scope_p ()
22474 && TYPE_BEING_DEFINED (current_class_type)
22475 && !LAMBDA_TYPE_P (current_class_type))
22476 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22477
22478 // A constrained-type-specifier may declare a type template-parameter.
22479 else if (declares_constrained_type_template_parameter (type))
22480 default_argument
22481 = cp_parser_default_type_template_argument (parser);
22482
22483 // A constrained-type-specifier may declare a template-template-parameter.
22484 else if (declares_constrained_template_template_parameter (type))
22485 default_argument
22486 = cp_parser_default_template_template_argument (parser);
22487
22488 /* Outside of a class definition, we can just parse the
22489 assignment-expression. */
22490 else
22491 default_argument
22492 = cp_parser_default_argument (parser, template_parm_p);
22493
22494 if (!parser->default_arg_ok_p)
22495 {
22496 permerror (token->location,
22497 "default arguments are only "
22498 "permitted for function parameters");
22499 }
22500 else if ((declarator && declarator->parameter_pack_p)
22501 || template_parameter_pack_p
22502 || (decl_specifiers.type
22503 && PACK_EXPANSION_P (decl_specifiers.type)))
22504 {
22505 /* Find the name of the parameter pack. */
22506 cp_declarator *id_declarator = declarator;
22507 while (id_declarator && id_declarator->kind != cdk_id)
22508 id_declarator = id_declarator->declarator;
22509
22510 if (id_declarator && id_declarator->kind == cdk_id)
22511 error_at (declarator_token_start->location,
22512 template_parm_p
22513 ? G_("template parameter pack %qD "
22514 "cannot have a default argument")
22515 : G_("parameter pack %qD cannot have "
22516 "a default argument"),
22517 id_declarator->u.id.unqualified_name);
22518 else
22519 error_at (declarator_token_start->location,
22520 template_parm_p
22521 ? G_("template parameter pack cannot have "
22522 "a default argument")
22523 : G_("parameter pack cannot have a "
22524 "default argument"));
22525
22526 default_argument = NULL_TREE;
22527 }
22528 }
22529 else
22530 default_argument = NULL_TREE;
22531
22532 if (default_argument)
22533 STRIP_ANY_LOCATION_WRAPPER (default_argument);
22534
22535 /* Generate a location for the parameter, ranging from the start of the
22536 initial token to the end of the final token (using input_location for
22537 the latter, set up by cp_lexer_set_source_position_from_token when
22538 consuming tokens).
22539
22540 If we have a identifier, then use it for the caret location, e.g.
22541
22542 extern int callee (int one, int (*two)(int, int), float three);
22543 ~~~~~~^~~~~~~~~~~~~~
22544
22545 otherwise, reuse the start location for the caret location e.g.:
22546
22547 extern int callee (int one, int (*)(int, int), float three);
22548 ^~~~~~~~~~~~~~~~~
22549
22550 */
22551 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22552 ? declarator->id_loc
22553 : decl_spec_token_start->location);
22554 location_t param_loc = make_location (caret_loc,
22555 decl_spec_token_start->location,
22556 input_location);
22557
22558 return make_parameter_declarator (&decl_specifiers,
22559 declarator,
22560 default_argument,
22561 param_loc,
22562 template_parameter_pack_p);
22563 }
22564
22565 /* Parse a default argument and return it.
22566
22567 TEMPLATE_PARM_P is true if this is a default argument for a
22568 non-type template parameter. */
22569 static tree
22570 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22571 {
22572 tree default_argument = NULL_TREE;
22573 bool saved_greater_than_is_operator_p;
22574 unsigned char saved_local_variables_forbidden_p;
22575 bool non_constant_p, is_direct_init;
22576
22577 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22578 set correctly. */
22579 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22580 parser->greater_than_is_operator_p = !template_parm_p;
22581 /* Local variable names (and the `this' keyword) may not
22582 appear in a default argument. */
22583 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22584 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
22585 /* Parse the assignment-expression. */
22586 if (template_parm_p)
22587 push_deferring_access_checks (dk_no_deferred);
22588 tree saved_class_ptr = NULL_TREE;
22589 tree saved_class_ref = NULL_TREE;
22590 /* The "this" pointer is not valid in a default argument. */
22591 if (cfun)
22592 {
22593 saved_class_ptr = current_class_ptr;
22594 cp_function_chain->x_current_class_ptr = NULL_TREE;
22595 saved_class_ref = current_class_ref;
22596 cp_function_chain->x_current_class_ref = NULL_TREE;
22597 }
22598 default_argument
22599 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22600 /* Restore the "this" pointer. */
22601 if (cfun)
22602 {
22603 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22604 cp_function_chain->x_current_class_ref = saved_class_ref;
22605 }
22606 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22607 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22608 if (template_parm_p)
22609 pop_deferring_access_checks ();
22610 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22611 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22612
22613 return default_argument;
22614 }
22615
22616 /* Parse a function-body.
22617
22618 function-body:
22619 compound_statement */
22620
22621 static void
22622 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22623 {
22624 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22625 ? BCS_TRY_BLOCK : BCS_NORMAL),
22626 true);
22627 }
22628
22629 /* Parse a ctor-initializer-opt followed by a function-body. Return
22630 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22631 is true we are parsing a function-try-block. */
22632
22633 static void
22634 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22635 bool in_function_try_block)
22636 {
22637 tree body, list;
22638 const bool check_body_p
22639 = (DECL_CONSTRUCTOR_P (current_function_decl)
22640 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
22641 tree last = NULL;
22642
22643 if (in_function_try_block
22644 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22645 && cxx_dialect < cxx2a)
22646 {
22647 if (DECL_CONSTRUCTOR_P (current_function_decl))
22648 pedwarn (input_location, 0,
22649 "function-try-block body of %<constexpr%> constructor only "
22650 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22651 else
22652 pedwarn (input_location, 0,
22653 "function-try-block body of %<constexpr%> function only "
22654 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22655 }
22656
22657 /* Begin the function body. */
22658 body = begin_function_body ();
22659 /* Parse the optional ctor-initializer. */
22660 cp_parser_ctor_initializer_opt (parser);
22661
22662 /* If we're parsing a constexpr constructor definition, we need
22663 to check that the constructor body is indeed empty. However,
22664 before we get to cp_parser_function_body lot of junk has been
22665 generated, so we can't just check that we have an empty block.
22666 Rather we take a snapshot of the outermost block, and check whether
22667 cp_parser_function_body changed its state. */
22668 if (check_body_p)
22669 {
22670 list = cur_stmt_list;
22671 if (STATEMENT_LIST_TAIL (list))
22672 last = STATEMENT_LIST_TAIL (list)->stmt;
22673 }
22674 /* Parse the function-body. */
22675 cp_parser_function_body (parser, in_function_try_block);
22676 if (check_body_p)
22677 check_constexpr_ctor_body (last, list, /*complain=*/true);
22678 /* Finish the function body. */
22679 finish_function_body (body);
22680 }
22681
22682 /* Parse an initializer.
22683
22684 initializer:
22685 = initializer-clause
22686 ( expression-list )
22687
22688 Returns an expression representing the initializer. If no
22689 initializer is present, NULL_TREE is returned.
22690
22691 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22692 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22693 set to TRUE if there is no initializer present. If there is an
22694 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22695 is set to true; otherwise it is set to false. */
22696
22697 static tree
22698 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22699 bool* non_constant_p, bool subexpression_p)
22700 {
22701 cp_token *token;
22702 tree init;
22703
22704 /* Peek at the next token. */
22705 token = cp_lexer_peek_token (parser->lexer);
22706
22707 /* Let our caller know whether or not this initializer was
22708 parenthesized. */
22709 *is_direct_init = (token->type != CPP_EQ);
22710 /* Assume that the initializer is constant. */
22711 *non_constant_p = false;
22712
22713 if (token->type == CPP_EQ)
22714 {
22715 /* Consume the `='. */
22716 cp_lexer_consume_token (parser->lexer);
22717 /* Parse the initializer-clause. */
22718 init = cp_parser_initializer_clause (parser, non_constant_p);
22719 }
22720 else if (token->type == CPP_OPEN_PAREN)
22721 {
22722 vec<tree, va_gc> *vec;
22723 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22724 /*cast_p=*/false,
22725 /*allow_expansion_p=*/true,
22726 non_constant_p);
22727 if (vec == NULL)
22728 return error_mark_node;
22729 init = build_tree_list_vec (vec);
22730 release_tree_vector (vec);
22731 }
22732 else if (token->type == CPP_OPEN_BRACE)
22733 {
22734 cp_lexer_set_source_position (parser->lexer);
22735 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22736 init = cp_parser_braced_list (parser, non_constant_p);
22737 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22738 }
22739 else
22740 {
22741 /* Anything else is an error. */
22742 cp_parser_error (parser, "expected initializer");
22743 init = error_mark_node;
22744 }
22745
22746 if (!subexpression_p && check_for_bare_parameter_packs (init))
22747 init = error_mark_node;
22748
22749 return init;
22750 }
22751
22752 /* Parse an initializer-clause.
22753
22754 initializer-clause:
22755 assignment-expression
22756 braced-init-list
22757
22758 Returns an expression representing the initializer.
22759
22760 If the `assignment-expression' production is used the value
22761 returned is simply a representation for the expression.
22762
22763 Otherwise, calls cp_parser_braced_list. */
22764
22765 static cp_expr
22766 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22767 {
22768 cp_expr initializer;
22769
22770 /* Assume the expression is constant. */
22771 *non_constant_p = false;
22772
22773 /* If it is not a `{', then we are looking at an
22774 assignment-expression. */
22775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22776 {
22777 initializer
22778 = cp_parser_constant_expression (parser,
22779 /*allow_non_constant_p=*/true,
22780 non_constant_p);
22781 }
22782 else
22783 initializer = cp_parser_braced_list (parser, non_constant_p);
22784
22785 return initializer;
22786 }
22787
22788 /* Parse a brace-enclosed initializer list.
22789
22790 braced-init-list:
22791 { initializer-list , [opt] }
22792 { designated-initializer-list , [opt] }
22793 { }
22794
22795 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22796 the elements of the initializer-list (or NULL, if the last
22797 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22798 NULL_TREE. There is no way to detect whether or not the optional
22799 trailing `,' was provided. NON_CONSTANT_P is as for
22800 cp_parser_initializer. */
22801
22802 static cp_expr
22803 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22804 {
22805 tree initializer;
22806 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22807
22808 /* Consume the `{' token. */
22809 matching_braces braces;
22810 braces.require_open (parser);
22811 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22812 initializer = make_node (CONSTRUCTOR);
22813 /* If it's not a `}', then there is a non-trivial initializer. */
22814 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22815 {
22816 bool designated;
22817 /* Parse the initializer list. */
22818 CONSTRUCTOR_ELTS (initializer)
22819 = cp_parser_initializer_list (parser, non_constant_p, &designated);
22820 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
22821 /* A trailing `,' token is allowed. */
22822 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22823 cp_lexer_consume_token (parser->lexer);
22824 }
22825 else
22826 *non_constant_p = false;
22827 /* Now, there should be a trailing `}'. */
22828 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22829 braces.require_close (parser);
22830 TREE_TYPE (initializer) = init_list_type_node;
22831
22832 cp_expr result (initializer);
22833 /* Build a location of the form:
22834 { ... }
22835 ^~~~~~~
22836 with caret==start at the open brace, finish at the close brace. */
22837 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22838 result.set_location (combined_loc);
22839 return result;
22840 }
22841
22842 /* Consume tokens up to, and including, the next non-nested closing `]'.
22843 Returns true iff we found a closing `]'. */
22844
22845 static bool
22846 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22847 {
22848 unsigned square_depth = 0;
22849
22850 while (true)
22851 {
22852 cp_token * token = cp_lexer_peek_token (parser->lexer);
22853
22854 switch (token->type)
22855 {
22856 case CPP_PRAGMA_EOL:
22857 if (!parser->lexer->in_pragma)
22858 break;
22859 /* FALLTHRU */
22860 case CPP_EOF:
22861 /* If we've run out of tokens, then there is no closing `]'. */
22862 return false;
22863
22864 case CPP_OPEN_SQUARE:
22865 ++square_depth;
22866 break;
22867
22868 case CPP_CLOSE_SQUARE:
22869 if (!square_depth--)
22870 {
22871 cp_lexer_consume_token (parser->lexer);
22872 return true;
22873 }
22874 break;
22875
22876 default:
22877 break;
22878 }
22879
22880 /* Consume the token. */
22881 cp_lexer_consume_token (parser->lexer);
22882 }
22883 }
22884
22885 /* Return true if we are looking at an array-designator, false otherwise. */
22886
22887 static bool
22888 cp_parser_array_designator_p (cp_parser *parser)
22889 {
22890 /* Consume the `['. */
22891 cp_lexer_consume_token (parser->lexer);
22892
22893 cp_lexer_save_tokens (parser->lexer);
22894
22895 /* Skip tokens until the next token is a closing square bracket.
22896 If we find the closing `]', and the next token is a `=', then
22897 we are looking at an array designator. */
22898 bool array_designator_p
22899 = (cp_parser_skip_to_closing_square_bracket (parser)
22900 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22901
22902 /* Roll back the tokens we skipped. */
22903 cp_lexer_rollback_tokens (parser->lexer);
22904
22905 return array_designator_p;
22906 }
22907
22908 /* Parse an initializer-list.
22909
22910 initializer-list:
22911 initializer-clause ... [opt]
22912 initializer-list , initializer-clause ... [opt]
22913
22914 C++2A Extension:
22915
22916 designated-initializer-list:
22917 designated-initializer-clause
22918 designated-initializer-list , designated-initializer-clause
22919
22920 designated-initializer-clause:
22921 designator brace-or-equal-initializer
22922
22923 designator:
22924 . identifier
22925
22926 GNU Extension:
22927
22928 initializer-list:
22929 designation initializer-clause ...[opt]
22930 initializer-list , designation initializer-clause ...[opt]
22931
22932 designation:
22933 . identifier =
22934 identifier :
22935 [ constant-expression ] =
22936
22937 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22938 for the initializer. If the INDEX of the elt is non-NULL, it is the
22939 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22940 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
22941 are any designators. */
22942
22943 static vec<constructor_elt, va_gc> *
22944 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
22945 bool *designated)
22946 {
22947 vec<constructor_elt, va_gc> *v = NULL;
22948 bool first_p = true;
22949 tree first_designator = NULL_TREE;
22950
22951 /* Assume all of the expressions are constant. */
22952 *non_constant_p = false;
22953
22954 /* Parse the rest of the list. */
22955 while (true)
22956 {
22957 cp_token *token;
22958 tree designator;
22959 tree initializer;
22960 bool clause_non_constant_p;
22961 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22962
22963 /* Handle the C++2A syntax, '. id ='. */
22964 if ((cxx_dialect >= cxx2a
22965 || cp_parser_allow_gnu_extensions_p (parser))
22966 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22967 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22968 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22969 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22970 == CPP_OPEN_BRACE)))
22971 {
22972 if (cxx_dialect < cxx2a)
22973 pedwarn (loc, OPT_Wpedantic,
22974 "C++ designated initializers only available with "
22975 "%<-std=c++2a%> or %<-std=gnu++2a%>");
22976 /* Consume the `.'. */
22977 cp_lexer_consume_token (parser->lexer);
22978 /* Consume the identifier. */
22979 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22980 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22981 /* Consume the `='. */
22982 cp_lexer_consume_token (parser->lexer);
22983 }
22984 /* Also, if the next token is an identifier and the following one is a
22985 colon, we are looking at the GNU designated-initializer
22986 syntax. */
22987 else if (cp_parser_allow_gnu_extensions_p (parser)
22988 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22989 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22990 == CPP_COLON))
22991 {
22992 /* Warn the user that they are using an extension. */
22993 pedwarn (loc, OPT_Wpedantic,
22994 "ISO C++ does not allow GNU designated initializers");
22995 /* Consume the identifier. */
22996 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22997 /* Consume the `:'. */
22998 cp_lexer_consume_token (parser->lexer);
22999 }
23000 /* Also handle C99 array designators, '[ const ] ='. */
23001 else if (cp_parser_allow_gnu_extensions_p (parser)
23002 && !c_dialect_objc ()
23003 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23004 {
23005 /* In C++11, [ could start a lambda-introducer. */
23006 bool non_const = false;
23007
23008 cp_parser_parse_tentatively (parser);
23009
23010 if (!cp_parser_array_designator_p (parser))
23011 {
23012 cp_parser_simulate_error (parser);
23013 designator = NULL_TREE;
23014 }
23015 else
23016 {
23017 designator = cp_parser_constant_expression (parser, true,
23018 &non_const);
23019 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23020 cp_parser_require (parser, CPP_EQ, RT_EQ);
23021 }
23022
23023 if (!cp_parser_parse_definitely (parser))
23024 designator = NULL_TREE;
23025 else if (non_const
23026 && (!require_potential_rvalue_constant_expression
23027 (designator)))
23028 designator = NULL_TREE;
23029 if (designator)
23030 /* Warn the user that they are using an extension. */
23031 pedwarn (loc, OPT_Wpedantic,
23032 "ISO C++ does not allow C99 designated initializers");
23033 }
23034 else
23035 designator = NULL_TREE;
23036
23037 if (first_p)
23038 {
23039 first_designator = designator;
23040 first_p = false;
23041 }
23042 else if (cxx_dialect >= cxx2a
23043 && first_designator != error_mark_node
23044 && (!first_designator != !designator))
23045 {
23046 error_at (loc, "either all initializer clauses should be designated "
23047 "or none of them should be");
23048 first_designator = error_mark_node;
23049 }
23050 else if (cxx_dialect < cxx2a && !first_designator)
23051 first_designator = designator;
23052
23053 /* Parse the initializer. */
23054 initializer = cp_parser_initializer_clause (parser,
23055 &clause_non_constant_p);
23056 /* If any clause is non-constant, so is the entire initializer. */
23057 if (clause_non_constant_p)
23058 *non_constant_p = true;
23059
23060 /* If we have an ellipsis, this is an initializer pack
23061 expansion. */
23062 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23063 {
23064 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23065
23066 /* Consume the `...'. */
23067 cp_lexer_consume_token (parser->lexer);
23068
23069 if (designator && cxx_dialect >= cxx2a)
23070 error_at (loc,
23071 "%<...%> not allowed in designated initializer list");
23072
23073 /* Turn the initializer into an initializer expansion. */
23074 initializer = make_pack_expansion (initializer);
23075 }
23076
23077 /* Add it to the vector. */
23078 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
23079
23080 /* If the next token is not a comma, we have reached the end of
23081 the list. */
23082 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23083 break;
23084
23085 /* Peek at the next token. */
23086 token = cp_lexer_peek_nth_token (parser->lexer, 2);
23087 /* If the next token is a `}', then we're still done. An
23088 initializer-clause can have a trailing `,' after the
23089 initializer-list and before the closing `}'. */
23090 if (token->type == CPP_CLOSE_BRACE)
23091 break;
23092
23093 /* Consume the `,' token. */
23094 cp_lexer_consume_token (parser->lexer);
23095 }
23096
23097 /* The same identifier shall not appear in multiple designators
23098 of a designated-initializer-list. */
23099 if (first_designator)
23100 {
23101 unsigned int i;
23102 tree designator, val;
23103 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23104 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23105 {
23106 if (IDENTIFIER_MARKED (designator))
23107 {
23108 error_at (cp_expr_loc_or_loc (val, input_location),
23109 "%<.%s%> designator used multiple times in "
23110 "the same initializer list",
23111 IDENTIFIER_POINTER (designator));
23112 (*v)[i].index = error_mark_node;
23113 }
23114 else
23115 IDENTIFIER_MARKED (designator) = 1;
23116 }
23117 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23118 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23119 IDENTIFIER_MARKED (designator) = 0;
23120 }
23121
23122 *designated = first_designator != NULL_TREE;
23123 return v;
23124 }
23125
23126 /* Classes [gram.class] */
23127
23128 /* Parse a class-name.
23129
23130 class-name:
23131 identifier
23132 template-id
23133
23134 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23135 to indicate that names looked up in dependent types should be
23136 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23137 keyword has been used to indicate that the name that appears next
23138 is a template. TAG_TYPE indicates the explicit tag given before
23139 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23140 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23141 is the class being defined in a class-head. If ENUM_OK is TRUE,
23142 enum-names are also accepted.
23143
23144 Returns the TYPE_DECL representing the class. */
23145
23146 static tree
23147 cp_parser_class_name (cp_parser *parser,
23148 bool typename_keyword_p,
23149 bool template_keyword_p,
23150 enum tag_types tag_type,
23151 bool check_dependency_p,
23152 bool class_head_p,
23153 bool is_declaration,
23154 bool enum_ok)
23155 {
23156 tree decl;
23157 tree scope;
23158 bool typename_p;
23159 cp_token *token;
23160 tree identifier = NULL_TREE;
23161
23162 /* All class-names start with an identifier. */
23163 token = cp_lexer_peek_token (parser->lexer);
23164 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
23165 {
23166 cp_parser_error (parser, "expected class-name");
23167 return error_mark_node;
23168 }
23169
23170 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23171 to a template-id, so we save it here. */
23172 scope = parser->scope;
23173 if (scope == error_mark_node)
23174 return error_mark_node;
23175
23176 /* Any name names a type if we're following the `typename' keyword
23177 in a qualified name where the enclosing scope is type-dependent. */
23178 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
23179 && dependent_type_p (scope));
23180 /* Handle the common case (an identifier, but not a template-id)
23181 efficiently. */
23182 if (token->type == CPP_NAME
23183 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
23184 {
23185 cp_token *identifier_token;
23186 bool ambiguous_p;
23187
23188 /* Look for the identifier. */
23189 identifier_token = cp_lexer_peek_token (parser->lexer);
23190 ambiguous_p = identifier_token->error_reported;
23191 identifier = cp_parser_identifier (parser);
23192 /* If the next token isn't an identifier, we are certainly not
23193 looking at a class-name. */
23194 if (identifier == error_mark_node)
23195 decl = error_mark_node;
23196 /* If we know this is a type-name, there's no need to look it
23197 up. */
23198 else if (typename_p)
23199 decl = identifier;
23200 else
23201 {
23202 tree ambiguous_decls;
23203 /* If we already know that this lookup is ambiguous, then
23204 we've already issued an error message; there's no reason
23205 to check again. */
23206 if (ambiguous_p)
23207 {
23208 cp_parser_simulate_error (parser);
23209 return error_mark_node;
23210 }
23211 /* If the next token is a `::', then the name must be a type
23212 name.
23213
23214 [basic.lookup.qual]
23215
23216 During the lookup for a name preceding the :: scope
23217 resolution operator, object, function, and enumerator
23218 names are ignored. */
23219 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23220 tag_type = scope_type;
23221 /* Look up the name. */
23222 decl = cp_parser_lookup_name (parser, identifier,
23223 tag_type,
23224 /*is_template=*/false,
23225 /*is_namespace=*/false,
23226 check_dependency_p,
23227 &ambiguous_decls,
23228 identifier_token->location);
23229 if (ambiguous_decls)
23230 {
23231 if (cp_parser_parsing_tentatively (parser))
23232 cp_parser_simulate_error (parser);
23233 return error_mark_node;
23234 }
23235 }
23236 }
23237 else
23238 {
23239 /* Try a template-id. */
23240 decl = cp_parser_template_id (parser, template_keyword_p,
23241 check_dependency_p,
23242 tag_type,
23243 is_declaration);
23244 if (decl == error_mark_node)
23245 return error_mark_node;
23246 }
23247
23248 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
23249
23250 /* If this is a typename, create a TYPENAME_TYPE. */
23251 if (typename_p
23252 && decl != error_mark_node
23253 && !is_overloaded_fn (decl))
23254 {
23255 decl = make_typename_type (scope, decl, typename_type,
23256 /*complain=*/tf_error);
23257 if (decl != error_mark_node)
23258 decl = TYPE_NAME (decl);
23259 }
23260
23261 decl = strip_using_decl (decl);
23262
23263 /* Check to see that it is really the name of a class. */
23264 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
23265 && identifier_p (TREE_OPERAND (decl, 0))
23266 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23267 /* Situations like this:
23268
23269 template <typename T> struct A {
23270 typename T::template X<int>::I i;
23271 };
23272
23273 are problematic. Is `T::template X<int>' a class-name? The
23274 standard does not seem to be definitive, but there is no other
23275 valid interpretation of the following `::'. Therefore, those
23276 names are considered class-names. */
23277 {
23278 decl = make_typename_type (scope, decl, tag_type, tf_error);
23279 if (decl != error_mark_node)
23280 decl = TYPE_NAME (decl);
23281 }
23282 else if (TREE_CODE (decl) != TYPE_DECL
23283 || TREE_TYPE (decl) == error_mark_node
23284 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
23285 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
23286 /* In Objective-C 2.0, a classname followed by '.' starts a
23287 dot-syntax expression, and it's not a type-name. */
23288 || (c_dialect_objc ()
23289 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
23290 && objc_is_class_name (decl)))
23291 decl = error_mark_node;
23292
23293 if (decl == error_mark_node)
23294 cp_parser_error (parser, "expected class-name");
23295 else if (identifier && !parser->scope)
23296 maybe_note_name_used_in_class (identifier, decl);
23297
23298 return decl;
23299 }
23300
23301 /* Parse a class-specifier.
23302
23303 class-specifier:
23304 class-head { member-specification [opt] }
23305
23306 Returns the TREE_TYPE representing the class. */
23307
23308 static tree
23309 cp_parser_class_specifier_1 (cp_parser* parser)
23310 {
23311 tree type;
23312 tree attributes = NULL_TREE;
23313 bool nested_name_specifier_p;
23314 unsigned saved_num_template_parameter_lists;
23315 bool saved_in_function_body;
23316 unsigned char in_statement;
23317 bool in_switch_statement_p;
23318 bool saved_in_unbraced_linkage_specification_p;
23319 tree old_scope = NULL_TREE;
23320 tree scope = NULL_TREE;
23321 cp_token *closing_brace;
23322
23323 push_deferring_access_checks (dk_no_deferred);
23324
23325 /* Parse the class-head. */
23326 type = cp_parser_class_head (parser,
23327 &nested_name_specifier_p);
23328 /* If the class-head was a semantic disaster, skip the entire body
23329 of the class. */
23330 if (!type)
23331 {
23332 cp_parser_skip_to_end_of_block_or_statement (parser);
23333 pop_deferring_access_checks ();
23334 return error_mark_node;
23335 }
23336
23337 /* Look for the `{'. */
23338 matching_braces braces;
23339 if (!braces.require_open (parser))
23340 {
23341 pop_deferring_access_checks ();
23342 return error_mark_node;
23343 }
23344
23345 cp_ensure_no_omp_declare_simd (parser);
23346 cp_ensure_no_oacc_routine (parser);
23347
23348 /* Issue an error message if type-definitions are forbidden here. */
23349 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
23350 /* Remember that we are defining one more class. */
23351 ++parser->num_classes_being_defined;
23352 /* Inside the class, surrounding template-parameter-lists do not
23353 apply. */
23354 saved_num_template_parameter_lists
23355 = parser->num_template_parameter_lists;
23356 parser->num_template_parameter_lists = 0;
23357 /* We are not in a function body. */
23358 saved_in_function_body = parser->in_function_body;
23359 parser->in_function_body = false;
23360 /* Or in a loop. */
23361 in_statement = parser->in_statement;
23362 parser->in_statement = 0;
23363 /* Or in a switch. */
23364 in_switch_statement_p = parser->in_switch_statement_p;
23365 parser->in_switch_statement_p = false;
23366 /* We are not immediately inside an extern "lang" block. */
23367 saved_in_unbraced_linkage_specification_p
23368 = parser->in_unbraced_linkage_specification_p;
23369 parser->in_unbraced_linkage_specification_p = false;
23370
23371 // Associate constraints with the type.
23372 if (flag_concepts)
23373 type = associate_classtype_constraints (type);
23374
23375 /* Start the class. */
23376 if (nested_name_specifier_p)
23377 {
23378 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23379 old_scope = push_inner_scope (scope);
23380 }
23381 type = begin_class_definition (type);
23382
23383 if (type == error_mark_node)
23384 /* If the type is erroneous, skip the entire body of the class. */
23385 cp_parser_skip_to_closing_brace (parser);
23386 else
23387 /* Parse the member-specification. */
23388 cp_parser_member_specification_opt (parser);
23389
23390 /* Look for the trailing `}'. */
23391 closing_brace = braces.require_close (parser);
23392 /* Look for trailing attributes to apply to this class. */
23393 if (cp_parser_allow_gnu_extensions_p (parser))
23394 attributes = cp_parser_gnu_attributes_opt (parser);
23395 if (type != error_mark_node)
23396 type = finish_struct (type, attributes);
23397 if (nested_name_specifier_p)
23398 pop_inner_scope (old_scope, scope);
23399
23400 /* We've finished a type definition. Check for the common syntax
23401 error of forgetting a semicolon after the definition. We need to
23402 be careful, as we can't just check for not-a-semicolon and be done
23403 with it; the user might have typed:
23404
23405 class X { } c = ...;
23406 class X { } *p = ...;
23407
23408 and so forth. Instead, enumerate all the possible tokens that
23409 might follow this production; if we don't see one of them, then
23410 complain and silently insert the semicolon. */
23411 {
23412 cp_token *token = cp_lexer_peek_token (parser->lexer);
23413 bool want_semicolon = true;
23414
23415 if (cp_next_tokens_can_be_std_attribute_p (parser))
23416 /* Don't try to parse c++11 attributes here. As per the
23417 grammar, that should be a task for
23418 cp_parser_decl_specifier_seq. */
23419 want_semicolon = false;
23420
23421 switch (token->type)
23422 {
23423 case CPP_NAME:
23424 case CPP_SEMICOLON:
23425 case CPP_MULT:
23426 case CPP_AND:
23427 case CPP_OPEN_PAREN:
23428 case CPP_CLOSE_PAREN:
23429 case CPP_COMMA:
23430 want_semicolon = false;
23431 break;
23432
23433 /* While it's legal for type qualifiers and storage class
23434 specifiers to follow type definitions in the grammar, only
23435 compiler testsuites contain code like that. Assume that if
23436 we see such code, then what we're really seeing is a case
23437 like:
23438
23439 class X { }
23440 const <type> var = ...;
23441
23442 or
23443
23444 class Y { }
23445 static <type> func (...) ...
23446
23447 i.e. the qualifier or specifier applies to the next
23448 declaration. To do so, however, we need to look ahead one
23449 more token to see if *that* token is a type specifier.
23450
23451 This code could be improved to handle:
23452
23453 class Z { }
23454 static const <type> var = ...; */
23455 case CPP_KEYWORD:
23456 if (keyword_is_decl_specifier (token->keyword))
23457 {
23458 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
23459
23460 /* Handling user-defined types here would be nice, but very
23461 tricky. */
23462 want_semicolon
23463 = (lookahead->type == CPP_KEYWORD
23464 && keyword_begins_type_specifier (lookahead->keyword));
23465 }
23466 break;
23467 default:
23468 break;
23469 }
23470
23471 /* If we don't have a type, then something is very wrong and we
23472 shouldn't try to do anything clever. Likewise for not seeing the
23473 closing brace. */
23474 if (closing_brace && TYPE_P (type) && want_semicolon)
23475 {
23476 /* Locate the closing brace. */
23477 cp_token_position prev
23478 = cp_lexer_previous_token_position (parser->lexer);
23479 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
23480 location_t loc = prev_token->location;
23481
23482 /* We want to suggest insertion of a ';' immediately *after* the
23483 closing brace, so, if we can, offset the location by 1 column. */
23484 location_t next_loc = loc;
23485 if (!linemap_location_from_macro_expansion_p (line_table, loc))
23486 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
23487
23488 rich_location richloc (line_table, next_loc);
23489
23490 /* If we successfully offset the location, suggest the fix-it. */
23491 if (next_loc != loc)
23492 richloc.add_fixit_insert_before (next_loc, ";");
23493
23494 if (CLASSTYPE_DECLARED_CLASS (type))
23495 error_at (&richloc,
23496 "expected %<;%> after class definition");
23497 else if (TREE_CODE (type) == RECORD_TYPE)
23498 error_at (&richloc,
23499 "expected %<;%> after struct definition");
23500 else if (TREE_CODE (type) == UNION_TYPE)
23501 error_at (&richloc,
23502 "expected %<;%> after union definition");
23503 else
23504 gcc_unreachable ();
23505
23506 /* Unget one token and smash it to look as though we encountered
23507 a semicolon in the input stream. */
23508 cp_lexer_set_token_position (parser->lexer, prev);
23509 token = cp_lexer_peek_token (parser->lexer);
23510 token->type = CPP_SEMICOLON;
23511 token->keyword = RID_MAX;
23512 }
23513 }
23514
23515 /* If this class is not itself within the scope of another class,
23516 then we need to parse the bodies of all of the queued function
23517 definitions. Note that the queued functions defined in a class
23518 are not always processed immediately following the
23519 class-specifier for that class. Consider:
23520
23521 struct A {
23522 struct B { void f() { sizeof (A); } };
23523 };
23524
23525 If `f' were processed before the processing of `A' were
23526 completed, there would be no way to compute the size of `A'.
23527 Note that the nesting we are interested in here is lexical --
23528 not the semantic nesting given by TYPE_CONTEXT. In particular,
23529 for:
23530
23531 struct A { struct B; };
23532 struct A::B { void f() { } };
23533
23534 there is no need to delay the parsing of `A::B::f'. */
23535 if (--parser->num_classes_being_defined == 0)
23536 {
23537 tree decl;
23538 tree class_type = NULL_TREE;
23539 tree pushed_scope = NULL_TREE;
23540 unsigned ix;
23541 cp_default_arg_entry *e;
23542 tree save_ccp, save_ccr;
23543
23544 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
23545 {
23546 /* Skip default arguments, NSDMIs, etc, in order to improve
23547 error recovery (c++/71169, c++/71832). */
23548 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23549 vec_safe_truncate (unparsed_nsdmis, 0);
23550 vec_safe_truncate (unparsed_classes, 0);
23551 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23552 }
23553
23554 /* In a first pass, parse default arguments to the functions.
23555 Then, in a second pass, parse the bodies of the functions.
23556 This two-phased approach handles cases like:
23557
23558 struct S {
23559 void f() { g(); }
23560 void g(int i = 3);
23561 };
23562
23563 */
23564 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23565 {
23566 decl = e->decl;
23567 /* If there are default arguments that have not yet been processed,
23568 take care of them now. */
23569 if (class_type != e->class_type)
23570 {
23571 if (pushed_scope)
23572 pop_scope (pushed_scope);
23573 class_type = e->class_type;
23574 pushed_scope = push_scope (class_type);
23575 }
23576 /* Make sure that any template parameters are in scope. */
23577 maybe_begin_member_template_processing (decl);
23578 /* Parse the default argument expressions. */
23579 cp_parser_late_parsing_default_args (parser, decl);
23580 /* Remove any template parameters from the symbol table. */
23581 maybe_end_member_template_processing ();
23582 }
23583 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23584 /* Now parse any NSDMIs. */
23585 save_ccp = current_class_ptr;
23586 save_ccr = current_class_ref;
23587 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23588 {
23589 if (class_type != DECL_CONTEXT (decl))
23590 {
23591 if (pushed_scope)
23592 pop_scope (pushed_scope);
23593 class_type = DECL_CONTEXT (decl);
23594 pushed_scope = push_scope (class_type);
23595 }
23596 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23597 cp_parser_late_parsing_nsdmi (parser, decl);
23598 }
23599 vec_safe_truncate (unparsed_nsdmis, 0);
23600 current_class_ptr = save_ccp;
23601 current_class_ref = save_ccr;
23602 if (pushed_scope)
23603 pop_scope (pushed_scope);
23604
23605 /* Now do some post-NSDMI bookkeeping. */
23606 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23607 after_nsdmi_defaulted_late_checks (class_type);
23608 vec_safe_truncate (unparsed_classes, 0);
23609 after_nsdmi_defaulted_late_checks (type);
23610
23611 /* Now parse the body of the functions. */
23612 if (flag_openmp)
23613 {
23614 /* OpenMP UDRs need to be parsed before all other functions. */
23615 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23616 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23617 cp_parser_late_parsing_for_member (parser, decl);
23618 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23619 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23620 cp_parser_late_parsing_for_member (parser, decl);
23621 }
23622 else
23623 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23624 cp_parser_late_parsing_for_member (parser, decl);
23625 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23626 }
23627 else
23628 vec_safe_push (unparsed_classes, type);
23629
23630 /* Put back any saved access checks. */
23631 pop_deferring_access_checks ();
23632
23633 /* Restore saved state. */
23634 parser->in_switch_statement_p = in_switch_statement_p;
23635 parser->in_statement = in_statement;
23636 parser->in_function_body = saved_in_function_body;
23637 parser->num_template_parameter_lists
23638 = saved_num_template_parameter_lists;
23639 parser->in_unbraced_linkage_specification_p
23640 = saved_in_unbraced_linkage_specification_p;
23641
23642 return type;
23643 }
23644
23645 static tree
23646 cp_parser_class_specifier (cp_parser* parser)
23647 {
23648 tree ret;
23649 timevar_push (TV_PARSE_STRUCT);
23650 ret = cp_parser_class_specifier_1 (parser);
23651 timevar_pop (TV_PARSE_STRUCT);
23652 return ret;
23653 }
23654
23655 /* Parse a class-head.
23656
23657 class-head:
23658 class-key identifier [opt] base-clause [opt]
23659 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23660 class-key nested-name-specifier [opt] template-id
23661 base-clause [opt]
23662
23663 class-virt-specifier:
23664 final
23665
23666 GNU Extensions:
23667 class-key attributes identifier [opt] base-clause [opt]
23668 class-key attributes nested-name-specifier identifier base-clause [opt]
23669 class-key attributes nested-name-specifier [opt] template-id
23670 base-clause [opt]
23671
23672 Upon return BASES is initialized to the list of base classes (or
23673 NULL, if there are none) in the same form returned by
23674 cp_parser_base_clause.
23675
23676 Returns the TYPE of the indicated class. Sets
23677 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23678 involving a nested-name-specifier was used, and FALSE otherwise.
23679
23680 Returns error_mark_node if this is not a class-head.
23681
23682 Returns NULL_TREE if the class-head is syntactically valid, but
23683 semantically invalid in a way that means we should skip the entire
23684 body of the class. */
23685
23686 static tree
23687 cp_parser_class_head (cp_parser* parser,
23688 bool* nested_name_specifier_p)
23689 {
23690 tree nested_name_specifier;
23691 enum tag_types class_key;
23692 tree id = NULL_TREE;
23693 tree type = NULL_TREE;
23694 tree attributes;
23695 tree bases;
23696 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23697 bool template_id_p = false;
23698 bool qualified_p = false;
23699 bool invalid_nested_name_p = false;
23700 bool invalid_explicit_specialization_p = false;
23701 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23702 tree pushed_scope = NULL_TREE;
23703 unsigned num_templates;
23704 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23705 /* Assume no nested-name-specifier will be present. */
23706 *nested_name_specifier_p = false;
23707 /* Assume no template parameter lists will be used in defining the
23708 type. */
23709 num_templates = 0;
23710 parser->colon_corrects_to_scope_p = false;
23711
23712 /* Look for the class-key. */
23713 class_key = cp_parser_class_key (parser);
23714 if (class_key == none_type)
23715 return error_mark_node;
23716
23717 location_t class_head_start_location = input_location;
23718
23719 /* Parse the attributes. */
23720 attributes = cp_parser_attributes_opt (parser);
23721
23722 /* If the next token is `::', that is invalid -- but sometimes
23723 people do try to write:
23724
23725 struct ::S {};
23726
23727 Handle this gracefully by accepting the extra qualifier, and then
23728 issuing an error about it later if this really is a
23729 class-head. If it turns out just to be an elaborated type
23730 specifier, remain silent. */
23731 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23732 qualified_p = true;
23733
23734 push_deferring_access_checks (dk_no_check);
23735
23736 /* Determine the name of the class. Begin by looking for an
23737 optional nested-name-specifier. */
23738 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23739 nested_name_specifier
23740 = cp_parser_nested_name_specifier_opt (parser,
23741 /*typename_keyword_p=*/false,
23742 /*check_dependency_p=*/false,
23743 /*type_p=*/true,
23744 /*is_declaration=*/false);
23745 /* If there was a nested-name-specifier, then there *must* be an
23746 identifier. */
23747
23748 cp_token *bad_template_keyword = NULL;
23749
23750 if (nested_name_specifier)
23751 {
23752 type_start_token = cp_lexer_peek_token (parser->lexer);
23753 /* Although the grammar says `identifier', it really means
23754 `class-name' or `template-name'. You are only allowed to
23755 define a class that has already been declared with this
23756 syntax.
23757
23758 The proposed resolution for Core Issue 180 says that wherever
23759 you see `class T::X' you should treat `X' as a type-name.
23760
23761 It is OK to define an inaccessible class; for example:
23762
23763 class A { class B; };
23764 class A::B {};
23765
23766 We do not know if we will see a class-name, or a
23767 template-name. We look for a class-name first, in case the
23768 class-name is a template-id; if we looked for the
23769 template-name first we would stop after the template-name. */
23770 cp_parser_parse_tentatively (parser);
23771 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23772 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23773 type = cp_parser_class_name (parser,
23774 /*typename_keyword_p=*/false,
23775 /*template_keyword_p=*/false,
23776 class_type,
23777 /*check_dependency_p=*/false,
23778 /*class_head_p=*/true,
23779 /*is_declaration=*/false);
23780 /* If that didn't work, ignore the nested-name-specifier. */
23781 if (!cp_parser_parse_definitely (parser))
23782 {
23783 invalid_nested_name_p = true;
23784 type_start_token = cp_lexer_peek_token (parser->lexer);
23785 id = cp_parser_identifier (parser);
23786 if (id == error_mark_node)
23787 id = NULL_TREE;
23788 }
23789 /* If we could not find a corresponding TYPE, treat this
23790 declaration like an unqualified declaration. */
23791 if (type == error_mark_node)
23792 nested_name_specifier = NULL_TREE;
23793 /* Otherwise, count the number of templates used in TYPE and its
23794 containing scopes. */
23795 else
23796 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23797 }
23798 /* Otherwise, the identifier is optional. */
23799 else
23800 {
23801 /* We don't know whether what comes next is a template-id,
23802 an identifier, or nothing at all. */
23803 cp_parser_parse_tentatively (parser);
23804 /* Check for a template-id. */
23805 type_start_token = cp_lexer_peek_token (parser->lexer);
23806 id = cp_parser_template_id (parser,
23807 /*template_keyword_p=*/false,
23808 /*check_dependency_p=*/true,
23809 class_key,
23810 /*is_declaration=*/true);
23811 /* If that didn't work, it could still be an identifier. */
23812 if (!cp_parser_parse_definitely (parser))
23813 {
23814 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23815 {
23816 type_start_token = cp_lexer_peek_token (parser->lexer);
23817 id = cp_parser_identifier (parser);
23818 }
23819 else
23820 id = NULL_TREE;
23821 }
23822 else
23823 {
23824 template_id_p = true;
23825 ++num_templates;
23826 }
23827 }
23828
23829 pop_deferring_access_checks ();
23830
23831 if (id)
23832 {
23833 cp_parser_check_for_invalid_template_id (parser, id,
23834 class_key,
23835 type_start_token->location);
23836 }
23837 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23838
23839 /* If it's not a `:' or a `{' then we can't really be looking at a
23840 class-head, since a class-head only appears as part of a
23841 class-specifier. We have to detect this situation before calling
23842 xref_tag, since that has irreversible side-effects. */
23843 if (!cp_parser_next_token_starts_class_definition_p (parser))
23844 {
23845 cp_parser_error (parser, "expected %<{%> or %<:%>");
23846 type = error_mark_node;
23847 goto out;
23848 }
23849
23850 /* At this point, we're going ahead with the class-specifier, even
23851 if some other problem occurs. */
23852 cp_parser_commit_to_tentative_parse (parser);
23853 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23854 {
23855 cp_parser_error (parser,
23856 "cannot specify %<override%> for a class");
23857 type = error_mark_node;
23858 goto out;
23859 }
23860 /* Issue the error about the overly-qualified name now. */
23861 if (qualified_p)
23862 {
23863 cp_parser_error (parser,
23864 "global qualification of class name is invalid");
23865 type = error_mark_node;
23866 goto out;
23867 }
23868 else if (invalid_nested_name_p)
23869 {
23870 cp_parser_error (parser,
23871 "qualified name does not name a class");
23872 type = error_mark_node;
23873 goto out;
23874 }
23875 else if (nested_name_specifier)
23876 {
23877 tree scope;
23878
23879 if (bad_template_keyword)
23880 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23881 keyword template shall not appear at the top level. */
23882 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23883 "keyword %<template%> not allowed in class-head-name");
23884
23885 /* Reject typedef-names in class heads. */
23886 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23887 {
23888 error_at (type_start_token->location,
23889 "invalid class name in declaration of %qD",
23890 type);
23891 type = NULL_TREE;
23892 goto done;
23893 }
23894
23895 /* Figure out in what scope the declaration is being placed. */
23896 scope = current_scope ();
23897 /* If that scope does not contain the scope in which the
23898 class was originally declared, the program is invalid. */
23899 if (scope && !is_ancestor (scope, nested_name_specifier))
23900 {
23901 if (at_namespace_scope_p ())
23902 error_at (type_start_token->location,
23903 "declaration of %qD in namespace %qD which does not "
23904 "enclose %qD",
23905 type, scope, nested_name_specifier);
23906 else
23907 error_at (type_start_token->location,
23908 "declaration of %qD in %qD which does not enclose %qD",
23909 type, scope, nested_name_specifier);
23910 type = NULL_TREE;
23911 goto done;
23912 }
23913 /* [dcl.meaning]
23914
23915 A declarator-id shall not be qualified except for the
23916 definition of a ... nested class outside of its class
23917 ... [or] the definition or explicit instantiation of a
23918 class member of a namespace outside of its namespace. */
23919 if (scope == nested_name_specifier)
23920 {
23921 permerror (nested_name_specifier_token_start->location,
23922 "extra qualification not allowed");
23923 nested_name_specifier = NULL_TREE;
23924 num_templates = 0;
23925 }
23926 }
23927 /* An explicit-specialization must be preceded by "template <>". If
23928 it is not, try to recover gracefully. */
23929 if (at_namespace_scope_p ()
23930 && parser->num_template_parameter_lists == 0
23931 && !processing_template_parmlist
23932 && template_id_p)
23933 {
23934 /* Build a location of this form:
23935 struct typename <ARGS>
23936 ^~~~~~~~~~~~~~~~~~~~~~
23937 with caret==start at the start token, and
23938 finishing at the end of the type. */
23939 location_t reported_loc
23940 = make_location (class_head_start_location,
23941 class_head_start_location,
23942 get_finish (type_start_token->location));
23943 rich_location richloc (line_table, reported_loc);
23944 richloc.add_fixit_insert_before (class_head_start_location,
23945 "template <> ");
23946 error_at (&richloc,
23947 "an explicit specialization must be preceded by"
23948 " %<template <>%>");
23949 invalid_explicit_specialization_p = true;
23950 /* Take the same action that would have been taken by
23951 cp_parser_explicit_specialization. */
23952 ++parser->num_template_parameter_lists;
23953 begin_specialization ();
23954 }
23955 /* There must be no "return" statements between this point and the
23956 end of this function; set "type "to the correct return value and
23957 use "goto done;" to return. */
23958 /* Make sure that the right number of template parameters were
23959 present. */
23960 if (!cp_parser_check_template_parameters (parser, num_templates,
23961 template_id_p,
23962 type_start_token->location,
23963 /*declarator=*/NULL))
23964 {
23965 /* If something went wrong, there is no point in even trying to
23966 process the class-definition. */
23967 type = NULL_TREE;
23968 goto done;
23969 }
23970
23971 /* Look up the type. */
23972 if (template_id_p)
23973 {
23974 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23975 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23976 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23977 {
23978 error_at (type_start_token->location,
23979 "function template %qD redeclared as a class template", id);
23980 type = error_mark_node;
23981 }
23982 else
23983 {
23984 type = TREE_TYPE (id);
23985 type = maybe_process_partial_specialization (type);
23986
23987 /* Check the scope while we still know whether or not we had a
23988 nested-name-specifier. */
23989 if (type != error_mark_node)
23990 check_unqualified_spec_or_inst (type, type_start_token->location);
23991 }
23992 if (nested_name_specifier)
23993 pushed_scope = push_scope (nested_name_specifier);
23994 }
23995 else if (nested_name_specifier)
23996 {
23997 tree class_type;
23998
23999 /* Given:
24000
24001 template <typename T> struct S { struct T };
24002 template <typename T> struct S<T>::T { };
24003
24004 we will get a TYPENAME_TYPE when processing the definition of
24005 `S::T'. We need to resolve it to the actual type before we
24006 try to define it. */
24007 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
24008 {
24009 class_type = resolve_typename_type (TREE_TYPE (type),
24010 /*only_current_p=*/false);
24011 if (TREE_CODE (class_type) != TYPENAME_TYPE)
24012 type = TYPE_NAME (class_type);
24013 else
24014 {
24015 cp_parser_error (parser, "could not resolve typename type");
24016 type = error_mark_node;
24017 }
24018 }
24019
24020 if (maybe_process_partial_specialization (TREE_TYPE (type))
24021 == error_mark_node)
24022 {
24023 type = NULL_TREE;
24024 goto done;
24025 }
24026
24027 class_type = current_class_type;
24028 /* Enter the scope indicated by the nested-name-specifier. */
24029 pushed_scope = push_scope (nested_name_specifier);
24030 /* Get the canonical version of this type. */
24031 type = TYPE_MAIN_DECL (TREE_TYPE (type));
24032 /* Call push_template_decl if it seems like we should be defining a
24033 template either from the template headers or the type we're
24034 defining, so that we diagnose both extra and missing headers. */
24035 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24036 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
24037 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
24038 {
24039 type = push_template_decl (type);
24040 if (type == error_mark_node)
24041 {
24042 type = NULL_TREE;
24043 goto done;
24044 }
24045 }
24046
24047 type = TREE_TYPE (type);
24048 *nested_name_specifier_p = true;
24049 }
24050 else /* The name is not a nested name. */
24051 {
24052 /* If the class was unnamed, create a dummy name. */
24053 if (!id)
24054 id = make_anon_name ();
24055 tag_scope tag_scope = (parser->in_type_id_in_expr_p
24056 ? ts_within_enclosing_non_class
24057 : ts_current);
24058 type = xref_tag (class_key, id, tag_scope,
24059 parser->num_template_parameter_lists);
24060 }
24061
24062 /* Indicate whether this class was declared as a `class' or as a
24063 `struct'. */
24064 if (TREE_CODE (type) == RECORD_TYPE)
24065 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
24066 cp_parser_check_class_key (class_key, type);
24067
24068 /* If this type was already complete, and we see another definition,
24069 that's an error. Likewise if the type is already being defined:
24070 this can happen, eg, when it's defined from within an expression
24071 (c++/84605). */
24072 if (type != error_mark_node
24073 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
24074 {
24075 error_at (type_start_token->location, "redefinition of %q#T",
24076 type);
24077 inform (location_of (type), "previous definition of %q#T",
24078 type);
24079 type = NULL_TREE;
24080 goto done;
24081 }
24082 else if (type == error_mark_node)
24083 type = NULL_TREE;
24084
24085 if (type)
24086 {
24087 /* Apply attributes now, before any use of the class as a template
24088 argument in its base list. */
24089 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
24090 fixup_attribute_variants (type);
24091 }
24092
24093 /* We will have entered the scope containing the class; the names of
24094 base classes should be looked up in that context. For example:
24095
24096 struct A { struct B {}; struct C; };
24097 struct A::C : B {};
24098
24099 is valid. */
24100
24101 /* Get the list of base-classes, if there is one. */
24102 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24103 {
24104 /* PR59482: enter the class scope so that base-specifiers are looked
24105 up correctly. */
24106 if (type)
24107 pushclass (type);
24108 bases = cp_parser_base_clause (parser);
24109 /* PR59482: get out of the previously pushed class scope so that the
24110 subsequent pops pop the right thing. */
24111 if (type)
24112 popclass ();
24113 }
24114 else
24115 bases = NULL_TREE;
24116
24117 /* If we're really defining a class, process the base classes.
24118 If they're invalid, fail. */
24119 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24120 xref_basetypes (type, bases);
24121
24122 done:
24123 /* Leave the scope given by the nested-name-specifier. We will
24124 enter the class scope itself while processing the members. */
24125 if (pushed_scope)
24126 pop_scope (pushed_scope);
24127
24128 if (invalid_explicit_specialization_p)
24129 {
24130 end_specialization ();
24131 --parser->num_template_parameter_lists;
24132 }
24133
24134 if (type)
24135 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
24136 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
24137 CLASSTYPE_FINAL (type) = 1;
24138 out:
24139 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24140 return type;
24141 }
24142
24143 /* Parse a class-key.
24144
24145 class-key:
24146 class
24147 struct
24148 union
24149
24150 Returns the kind of class-key specified, or none_type to indicate
24151 error. */
24152
24153 static enum tag_types
24154 cp_parser_class_key (cp_parser* parser)
24155 {
24156 cp_token *token;
24157 enum tag_types tag_type;
24158
24159 /* Look for the class-key. */
24160 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
24161 if (!token)
24162 return none_type;
24163
24164 /* Check to see if the TOKEN is a class-key. */
24165 tag_type = cp_parser_token_is_class_key (token);
24166 if (!tag_type)
24167 cp_parser_error (parser, "expected class-key");
24168 return tag_type;
24169 }
24170
24171 /* Parse a type-parameter-key.
24172
24173 type-parameter-key:
24174 class
24175 typename
24176 */
24177
24178 static void
24179 cp_parser_type_parameter_key (cp_parser* parser)
24180 {
24181 /* Look for the type-parameter-key. */
24182 enum tag_types tag_type = none_type;
24183 cp_token *token = cp_lexer_peek_token (parser->lexer);
24184 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
24185 {
24186 cp_lexer_consume_token (parser->lexer);
24187 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
24188 /* typename is not allowed in a template template parameter
24189 by the standard until C++17. */
24190 pedwarn (token->location, OPT_Wpedantic,
24191 "ISO C++ forbids typename key in template template parameter;"
24192 " use %<-std=c++17%> or %<-std=gnu++17%>");
24193 }
24194 else
24195 cp_parser_error (parser, "expected %<class%> or %<typename%>");
24196
24197 return;
24198 }
24199
24200 /* Parse an (optional) member-specification.
24201
24202 member-specification:
24203 member-declaration member-specification [opt]
24204 access-specifier : member-specification [opt] */
24205
24206 static void
24207 cp_parser_member_specification_opt (cp_parser* parser)
24208 {
24209 while (true)
24210 {
24211 cp_token *token;
24212 enum rid keyword;
24213
24214 /* Peek at the next token. */
24215 token = cp_lexer_peek_token (parser->lexer);
24216 /* If it's a `}', or EOF then we've seen all the members. */
24217 if (token->type == CPP_CLOSE_BRACE
24218 || token->type == CPP_EOF
24219 || token->type == CPP_PRAGMA_EOL)
24220 break;
24221
24222 /* See if this token is a keyword. */
24223 keyword = token->keyword;
24224 switch (keyword)
24225 {
24226 case RID_PUBLIC:
24227 case RID_PROTECTED:
24228 case RID_PRIVATE:
24229 /* Consume the access-specifier. */
24230 cp_lexer_consume_token (parser->lexer);
24231 /* Remember which access-specifier is active. */
24232 current_access_specifier = token->u.value;
24233 /* Look for the `:'. */
24234 cp_parser_require (parser, CPP_COLON, RT_COLON);
24235 break;
24236
24237 default:
24238 /* Accept #pragmas at class scope. */
24239 if (token->type == CPP_PRAGMA)
24240 {
24241 cp_parser_pragma (parser, pragma_member, NULL);
24242 break;
24243 }
24244
24245 /* Otherwise, the next construction must be a
24246 member-declaration. */
24247 cp_parser_member_declaration (parser);
24248 }
24249 }
24250 }
24251
24252 /* Parse a member-declaration.
24253
24254 member-declaration:
24255 decl-specifier-seq [opt] member-declarator-list [opt] ;
24256 function-definition ; [opt]
24257 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24258 using-declaration
24259 template-declaration
24260 alias-declaration
24261
24262 member-declarator-list:
24263 member-declarator
24264 member-declarator-list , member-declarator
24265
24266 member-declarator:
24267 declarator pure-specifier [opt]
24268 declarator constant-initializer [opt]
24269 identifier [opt] : constant-expression
24270
24271 GNU Extensions:
24272
24273 member-declaration:
24274 __extension__ member-declaration
24275
24276 member-declarator:
24277 declarator attributes [opt] pure-specifier [opt]
24278 declarator attributes [opt] constant-initializer [opt]
24279 identifier [opt] attributes [opt] : constant-expression
24280
24281 C++0x Extensions:
24282
24283 member-declaration:
24284 static_assert-declaration */
24285
24286 static void
24287 cp_parser_member_declaration (cp_parser* parser)
24288 {
24289 cp_decl_specifier_seq decl_specifiers;
24290 tree prefix_attributes;
24291 tree decl;
24292 int declares_class_or_enum;
24293 bool friend_p;
24294 cp_token *token = NULL;
24295 cp_token *decl_spec_token_start = NULL;
24296 cp_token *initializer_token_start = NULL;
24297 int saved_pedantic;
24298 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24299
24300 /* Check for the `__extension__' keyword. */
24301 if (cp_parser_extension_opt (parser, &saved_pedantic))
24302 {
24303 /* Recurse. */
24304 cp_parser_member_declaration (parser);
24305 /* Restore the old value of the PEDANTIC flag. */
24306 pedantic = saved_pedantic;
24307
24308 return;
24309 }
24310
24311 /* Check for a template-declaration. */
24312 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24313 {
24314 /* An explicit specialization here is an error condition, and we
24315 expect the specialization handler to detect and report this. */
24316 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
24317 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
24318 cp_parser_explicit_specialization (parser);
24319 else
24320 cp_parser_template_declaration (parser, /*member_p=*/true);
24321
24322 return;
24323 }
24324 /* Check for a template introduction. */
24325 else if (cp_parser_template_declaration_after_export (parser, true))
24326 return;
24327
24328 /* Check for a using-declaration. */
24329 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24330 {
24331 if (cxx_dialect < cxx11)
24332 {
24333 /* Parse the using-declaration. */
24334 cp_parser_using_declaration (parser,
24335 /*access_declaration_p=*/false);
24336 return;
24337 }
24338 else
24339 {
24340 tree decl;
24341 bool alias_decl_expected;
24342 cp_parser_parse_tentatively (parser);
24343 decl = cp_parser_alias_declaration (parser);
24344 /* Note that if we actually see the '=' token after the
24345 identifier, cp_parser_alias_declaration commits the
24346 tentative parse. In that case, we really expect an
24347 alias-declaration. Otherwise, we expect a using
24348 declaration. */
24349 alias_decl_expected =
24350 !cp_parser_uncommitted_to_tentative_parse_p (parser);
24351 cp_parser_parse_definitely (parser);
24352
24353 if (alias_decl_expected)
24354 finish_member_declaration (decl);
24355 else
24356 cp_parser_using_declaration (parser,
24357 /*access_declaration_p=*/false);
24358 return;
24359 }
24360 }
24361
24362 /* Check for @defs. */
24363 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
24364 {
24365 tree ivar, member;
24366 tree ivar_chains = cp_parser_objc_defs_expression (parser);
24367 ivar = ivar_chains;
24368 while (ivar)
24369 {
24370 member = ivar;
24371 ivar = TREE_CHAIN (member);
24372 TREE_CHAIN (member) = NULL_TREE;
24373 finish_member_declaration (member);
24374 }
24375 return;
24376 }
24377
24378 /* If the next token is `static_assert' we have a static assertion. */
24379 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
24380 {
24381 cp_parser_static_assert (parser, /*member_p=*/true);
24382 return;
24383 }
24384
24385 parser->colon_corrects_to_scope_p = false;
24386
24387 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
24388 goto out;
24389
24390 /* Parse the decl-specifier-seq. */
24391 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24392 cp_parser_decl_specifier_seq (parser,
24393 (CP_PARSER_FLAGS_OPTIONAL
24394 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
24395 &decl_specifiers,
24396 &declares_class_or_enum);
24397 /* Check for an invalid type-name. */
24398 if (!decl_specifiers.any_type_specifiers_p
24399 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24400 goto out;
24401 /* If there is no declarator, then the decl-specifier-seq should
24402 specify a type. */
24403 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24404 {
24405 /* If there was no decl-specifier-seq, and the next token is a
24406 `;', then we have something like:
24407
24408 struct S { ; };
24409
24410 [class.mem]
24411
24412 Each member-declaration shall declare at least one member
24413 name of the class. */
24414 if (!decl_specifiers.any_specifiers_p)
24415 {
24416 cp_token *token = cp_lexer_peek_token (parser->lexer);
24417 if (!in_system_header_at (token->location))
24418 {
24419 gcc_rich_location richloc (token->location);
24420 richloc.add_fixit_remove ();
24421 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
24422 }
24423 }
24424 else
24425 {
24426 tree type;
24427
24428 /* See if this declaration is a friend. */
24429 friend_p = cp_parser_friend_p (&decl_specifiers);
24430 /* If there were decl-specifiers, check to see if there was
24431 a class-declaration. */
24432 type = check_tag_decl (&decl_specifiers,
24433 /*explicit_type_instantiation_p=*/false);
24434 /* Nested classes have already been added to the class, but
24435 a `friend' needs to be explicitly registered. */
24436 if (friend_p)
24437 {
24438 /* If the `friend' keyword was present, the friend must
24439 be introduced with a class-key. */
24440 if (!declares_class_or_enum && cxx_dialect < cxx11)
24441 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
24442 "in C++03 a class-key must be used "
24443 "when declaring a friend");
24444 /* In this case:
24445
24446 template <typename T> struct A {
24447 friend struct A<T>::B;
24448 };
24449
24450 A<T>::B will be represented by a TYPENAME_TYPE, and
24451 therefore not recognized by check_tag_decl. */
24452 if (!type)
24453 {
24454 type = decl_specifiers.type;
24455 if (type && TREE_CODE (type) == TYPE_DECL)
24456 type = TREE_TYPE (type);
24457 }
24458 if (!type || !TYPE_P (type))
24459 error_at (decl_spec_token_start->location,
24460 "friend declaration does not name a class or "
24461 "function");
24462 else
24463 make_friend_class (current_class_type, type,
24464 /*complain=*/true);
24465 }
24466 /* If there is no TYPE, an error message will already have
24467 been issued. */
24468 else if (!type || type == error_mark_node)
24469 ;
24470 /* An anonymous aggregate has to be handled specially; such
24471 a declaration really declares a data member (with a
24472 particular type), as opposed to a nested class. */
24473 else if (ANON_AGGR_TYPE_P (type))
24474 {
24475 /* C++11 9.5/6. */
24476 if (decl_specifiers.storage_class != sc_none)
24477 error_at (decl_spec_token_start->location,
24478 "a storage class on an anonymous aggregate "
24479 "in class scope is not allowed");
24480
24481 /* Remove constructors and such from TYPE, now that we
24482 know it is an anonymous aggregate. */
24483 fixup_anonymous_aggr (type);
24484 /* And make the corresponding data member. */
24485 decl = build_decl (decl_spec_token_start->location,
24486 FIELD_DECL, NULL_TREE, type);
24487 /* Add it to the class. */
24488 finish_member_declaration (decl);
24489 }
24490 else
24491 cp_parser_check_access_in_redeclaration
24492 (TYPE_NAME (type),
24493 decl_spec_token_start->location);
24494 }
24495 }
24496 else
24497 {
24498 bool assume_semicolon = false;
24499
24500 /* Clear attributes from the decl_specifiers but keep them
24501 around as prefix attributes that apply them to the entity
24502 being declared. */
24503 prefix_attributes = decl_specifiers.attributes;
24504 decl_specifiers.attributes = NULL_TREE;
24505
24506 /* See if these declarations will be friends. */
24507 friend_p = cp_parser_friend_p (&decl_specifiers);
24508
24509 /* Keep going until we hit the `;' at the end of the
24510 declaration. */
24511 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24512 {
24513 tree attributes = NULL_TREE;
24514 tree first_attribute;
24515 tree initializer;
24516 bool named_bitfld = false;
24517
24518 /* Peek at the next token. */
24519 token = cp_lexer_peek_token (parser->lexer);
24520
24521 /* The following code wants to know early if it is a bit-field
24522 or some other declaration. Attributes can appear before
24523 the `:' token. Skip over them without consuming any tokens
24524 to peek if they are followed by `:'. */
24525 if (cp_next_tokens_can_be_attribute_p (parser)
24526 || (token->type == CPP_NAME
24527 && cp_nth_tokens_can_be_attribute_p (parser, 2)
24528 && (named_bitfld = true)))
24529 {
24530 size_t n
24531 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24532 token = cp_lexer_peek_nth_token (parser->lexer, n);
24533 }
24534
24535 /* Check for a bitfield declaration. */
24536 if (token->type == CPP_COLON
24537 || (token->type == CPP_NAME
24538 && token == cp_lexer_peek_token (parser->lexer)
24539 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24540 && (named_bitfld = true)))
24541 {
24542 tree identifier;
24543 tree width;
24544 tree late_attributes = NULL_TREE;
24545 location_t id_location
24546 = cp_lexer_peek_token (parser->lexer)->location;
24547
24548 if (named_bitfld)
24549 identifier = cp_parser_identifier (parser);
24550 else
24551 identifier = NULL_TREE;
24552
24553 /* Look for attributes that apply to the bitfield. */
24554 attributes = cp_parser_attributes_opt (parser);
24555
24556 /* Consume the `:' token. */
24557 cp_lexer_consume_token (parser->lexer);
24558
24559 /* Get the width of the bitfield. */
24560 width = cp_parser_constant_expression (parser, false, NULL,
24561 cxx_dialect >= cxx11);
24562
24563 /* In C++2A and as extension for C++11 and above we allow
24564 default member initializers for bit-fields. */
24565 initializer = NULL_TREE;
24566 if (cxx_dialect >= cxx11
24567 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24568 || cp_lexer_next_token_is (parser->lexer,
24569 CPP_OPEN_BRACE)))
24570 {
24571 location_t loc
24572 = cp_lexer_peek_token (parser->lexer)->location;
24573 if (cxx_dialect < cxx2a
24574 && !in_system_header_at (loc)
24575 && identifier != NULL_TREE)
24576 pedwarn (loc, 0,
24577 "default member initializers for bit-fields "
24578 "only available with %<-std=c++2a%> or "
24579 "%<-std=gnu++2a%>");
24580
24581 initializer = cp_parser_save_nsdmi (parser);
24582 if (identifier == NULL_TREE)
24583 {
24584 error_at (loc, "default member initializer for "
24585 "unnamed bit-field");
24586 initializer = NULL_TREE;
24587 }
24588 }
24589 else
24590 {
24591 /* Look for attributes that apply to the bitfield after
24592 the `:' token and width. This is where GCC used to
24593 parse attributes in the past, pedwarn if there is
24594 a std attribute. */
24595 if (cp_next_tokens_can_be_std_attribute_p (parser))
24596 pedwarn (input_location, OPT_Wpedantic,
24597 "ISO C++ allows bit-field attributes only "
24598 "before the %<:%> token");
24599
24600 late_attributes = cp_parser_attributes_opt (parser);
24601 }
24602
24603 attributes = attr_chainon (attributes, late_attributes);
24604
24605 /* Remember which attributes are prefix attributes and
24606 which are not. */
24607 first_attribute = attributes;
24608 /* Combine the attributes. */
24609 attributes = attr_chainon (prefix_attributes, attributes);
24610
24611 /* Create the bitfield declaration. */
24612 decl = grokbitfield (identifier
24613 ? make_id_declarator (NULL_TREE,
24614 identifier,
24615 sfk_none,
24616 id_location)
24617 : NULL,
24618 &decl_specifiers,
24619 width, initializer,
24620 attributes);
24621 }
24622 else
24623 {
24624 cp_declarator *declarator;
24625 tree asm_specification;
24626 int ctor_dtor_or_conv_p;
24627 bool static_p = (decl_specifiers.storage_class == sc_static);
24628
24629 /* Parse the declarator. */
24630 declarator
24631 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24632 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24633 &ctor_dtor_or_conv_p,
24634 /*parenthesized_p=*/NULL,
24635 /*member_p=*/true,
24636 friend_p, static_p);
24637
24638 /* If something went wrong parsing the declarator, make sure
24639 that we at least consume some tokens. */
24640 if (declarator == cp_error_declarator)
24641 {
24642 /* Skip to the end of the statement. */
24643 cp_parser_skip_to_end_of_statement (parser);
24644 /* If the next token is not a semicolon, that is
24645 probably because we just skipped over the body of
24646 a function. So, we consume a semicolon if
24647 present, but do not issue an error message if it
24648 is not present. */
24649 if (cp_lexer_next_token_is (parser->lexer,
24650 CPP_SEMICOLON))
24651 cp_lexer_consume_token (parser->lexer);
24652 goto out;
24653 }
24654
24655 if (declares_class_or_enum & 2)
24656 cp_parser_check_for_definition_in_return_type
24657 (declarator, decl_specifiers.type,
24658 decl_specifiers.locations[ds_type_spec]);
24659
24660 /* Look for an asm-specification. */
24661 asm_specification = cp_parser_asm_specification_opt (parser);
24662 /* Look for attributes that apply to the declaration. */
24663 attributes = cp_parser_attributes_opt (parser);
24664 /* Remember which attributes are prefix attributes and
24665 which are not. */
24666 first_attribute = attributes;
24667 /* Combine the attributes. */
24668 attributes = attr_chainon (prefix_attributes, attributes);
24669
24670 /* If it's an `=', then we have a constant-initializer or a
24671 pure-specifier. It is not correct to parse the
24672 initializer before registering the member declaration
24673 since the member declaration should be in scope while
24674 its initializer is processed. However, the rest of the
24675 front end does not yet provide an interface that allows
24676 us to handle this correctly. */
24677 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24678 {
24679 /* In [class.mem]:
24680
24681 A pure-specifier shall be used only in the declaration of
24682 a virtual function.
24683
24684 A member-declarator can contain a constant-initializer
24685 only if it declares a static member of integral or
24686 enumeration type.
24687
24688 Therefore, if the DECLARATOR is for a function, we look
24689 for a pure-specifier; otherwise, we look for a
24690 constant-initializer. When we call `grokfield', it will
24691 perform more stringent semantics checks. */
24692 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24693 if (function_declarator_p (declarator)
24694 || (decl_specifiers.type
24695 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24696 && declarator->kind == cdk_id
24697 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24698 == FUNCTION_TYPE)))
24699 initializer = cp_parser_pure_specifier (parser);
24700 else if (decl_specifiers.storage_class != sc_static)
24701 initializer = cp_parser_save_nsdmi (parser);
24702 else if (cxx_dialect >= cxx11)
24703 {
24704 bool nonconst;
24705 /* Don't require a constant rvalue in C++11, since we
24706 might want a reference constant. We'll enforce
24707 constancy later. */
24708 cp_lexer_consume_token (parser->lexer);
24709 /* Parse the initializer. */
24710 initializer = cp_parser_initializer_clause (parser,
24711 &nonconst);
24712 }
24713 else
24714 /* Parse the initializer. */
24715 initializer = cp_parser_constant_initializer (parser);
24716 }
24717 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24718 && !function_declarator_p (declarator))
24719 {
24720 bool x;
24721 if (decl_specifiers.storage_class != sc_static)
24722 initializer = cp_parser_save_nsdmi (parser);
24723 else
24724 initializer = cp_parser_initializer (parser, &x, &x);
24725 }
24726 /* Otherwise, there is no initializer. */
24727 else
24728 initializer = NULL_TREE;
24729
24730 /* See if we are probably looking at a function
24731 definition. We are certainly not looking at a
24732 member-declarator. Calling `grokfield' has
24733 side-effects, so we must not do it unless we are sure
24734 that we are looking at a member-declarator. */
24735 if (cp_parser_token_starts_function_definition_p
24736 (cp_lexer_peek_token (parser->lexer)))
24737 {
24738 /* The grammar does not allow a pure-specifier to be
24739 used when a member function is defined. (It is
24740 possible that this fact is an oversight in the
24741 standard, since a pure function may be defined
24742 outside of the class-specifier. */
24743 if (initializer && initializer_token_start)
24744 error_at (initializer_token_start->location,
24745 "pure-specifier on function-definition");
24746 decl = cp_parser_save_member_function_body (parser,
24747 &decl_specifiers,
24748 declarator,
24749 attributes);
24750 if (parser->fully_implicit_function_template_p)
24751 decl = finish_fully_implicit_template (parser, decl);
24752 /* If the member was not a friend, declare it here. */
24753 if (!friend_p)
24754 finish_member_declaration (decl);
24755 /* Peek at the next token. */
24756 token = cp_lexer_peek_token (parser->lexer);
24757 /* If the next token is a semicolon, consume it. */
24758 if (token->type == CPP_SEMICOLON)
24759 {
24760 location_t semicolon_loc
24761 = cp_lexer_consume_token (parser->lexer)->location;
24762 gcc_rich_location richloc (semicolon_loc);
24763 richloc.add_fixit_remove ();
24764 warning_at (&richloc, OPT_Wextra_semi,
24765 "extra %<;%> after in-class "
24766 "function definition");
24767 }
24768 goto out;
24769 }
24770 else
24771 if (declarator->kind == cdk_function)
24772 declarator->id_loc = token->location;
24773 /* Create the declaration. */
24774 decl = grokfield (declarator, &decl_specifiers,
24775 initializer, /*init_const_expr_p=*/true,
24776 asm_specification, attributes);
24777 if (parser->fully_implicit_function_template_p)
24778 {
24779 if (friend_p)
24780 finish_fully_implicit_template (parser, 0);
24781 else
24782 decl = finish_fully_implicit_template (parser, decl);
24783 }
24784 }
24785
24786 cp_finalize_omp_declare_simd (parser, decl);
24787 cp_finalize_oacc_routine (parser, decl, false);
24788
24789 /* Reset PREFIX_ATTRIBUTES. */
24790 if (attributes != error_mark_node)
24791 {
24792 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24793 attributes = TREE_CHAIN (attributes);
24794 if (attributes)
24795 TREE_CHAIN (attributes) = NULL_TREE;
24796 }
24797
24798 /* If there is any qualification still in effect, clear it
24799 now; we will be starting fresh with the next declarator. */
24800 parser->scope = NULL_TREE;
24801 parser->qualifying_scope = NULL_TREE;
24802 parser->object_scope = NULL_TREE;
24803 /* If it's a `,', then there are more declarators. */
24804 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24805 {
24806 cp_lexer_consume_token (parser->lexer);
24807 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24808 {
24809 cp_token *token = cp_lexer_previous_token (parser->lexer);
24810 gcc_rich_location richloc (token->location);
24811 richloc.add_fixit_remove ();
24812 error_at (&richloc, "stray %<,%> at end of "
24813 "member declaration");
24814 }
24815 }
24816 /* If the next token isn't a `;', then we have a parse error. */
24817 else if (cp_lexer_next_token_is_not (parser->lexer,
24818 CPP_SEMICOLON))
24819 {
24820 /* The next token might be a ways away from where the
24821 actual semicolon is missing. Find the previous token
24822 and use that for our error position. */
24823 cp_token *token = cp_lexer_previous_token (parser->lexer);
24824 gcc_rich_location richloc (token->location);
24825 richloc.add_fixit_insert_after (";");
24826 error_at (&richloc, "expected %<;%> at end of "
24827 "member declaration");
24828
24829 /* Assume that the user meant to provide a semicolon. If
24830 we were to cp_parser_skip_to_end_of_statement, we might
24831 skip to a semicolon inside a member function definition
24832 and issue nonsensical error messages. */
24833 assume_semicolon = true;
24834 }
24835
24836 if (decl)
24837 {
24838 /* Add DECL to the list of members. */
24839 if (!friend_p
24840 /* Explicitly include, eg, NSDMIs, for better error
24841 recovery (c++/58650). */
24842 || !DECL_DECLARES_FUNCTION_P (decl))
24843 finish_member_declaration (decl);
24844
24845 if (TREE_CODE (decl) == FUNCTION_DECL)
24846 cp_parser_save_default_args (parser, decl);
24847 else if (TREE_CODE (decl) == FIELD_DECL
24848 && DECL_INITIAL (decl))
24849 /* Add DECL to the queue of NSDMI to be parsed later. */
24850 vec_safe_push (unparsed_nsdmis, decl);
24851 }
24852
24853 if (assume_semicolon)
24854 goto out;
24855 }
24856 }
24857
24858 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24859 out:
24860 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24861 }
24862
24863 /* Parse a pure-specifier.
24864
24865 pure-specifier:
24866 = 0
24867
24868 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24869 Otherwise, ERROR_MARK_NODE is returned. */
24870
24871 static tree
24872 cp_parser_pure_specifier (cp_parser* parser)
24873 {
24874 cp_token *token;
24875
24876 /* Look for the `=' token. */
24877 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24878 return error_mark_node;
24879 /* Look for the `0' token. */
24880 token = cp_lexer_peek_token (parser->lexer);
24881
24882 if (token->type == CPP_EOF
24883 || token->type == CPP_PRAGMA_EOL)
24884 return error_mark_node;
24885
24886 cp_lexer_consume_token (parser->lexer);
24887
24888 /* Accept = default or = delete in c++0x mode. */
24889 if (token->keyword == RID_DEFAULT
24890 || token->keyword == RID_DELETE)
24891 {
24892 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24893 return token->u.value;
24894 }
24895
24896 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24897 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24898 {
24899 cp_parser_error (parser,
24900 "invalid pure specifier (only %<= 0%> is allowed)");
24901 cp_parser_skip_to_end_of_statement (parser);
24902 return error_mark_node;
24903 }
24904 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24905 {
24906 error_at (token->location, "templates may not be %<virtual%>");
24907 return error_mark_node;
24908 }
24909
24910 return integer_zero_node;
24911 }
24912
24913 /* Parse a constant-initializer.
24914
24915 constant-initializer:
24916 = constant-expression
24917
24918 Returns a representation of the constant-expression. */
24919
24920 static tree
24921 cp_parser_constant_initializer (cp_parser* parser)
24922 {
24923 /* Look for the `=' token. */
24924 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24925 return error_mark_node;
24926
24927 /* It is invalid to write:
24928
24929 struct S { static const int i = { 7 }; };
24930
24931 */
24932 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24933 {
24934 cp_parser_error (parser,
24935 "a brace-enclosed initializer is not allowed here");
24936 /* Consume the opening brace. */
24937 matching_braces braces;
24938 braces.consume_open (parser);
24939 /* Skip the initializer. */
24940 cp_parser_skip_to_closing_brace (parser);
24941 /* Look for the trailing `}'. */
24942 braces.require_close (parser);
24943
24944 return error_mark_node;
24945 }
24946
24947 return cp_parser_constant_expression (parser);
24948 }
24949
24950 /* Derived classes [gram.class.derived] */
24951
24952 /* Parse a base-clause.
24953
24954 base-clause:
24955 : base-specifier-list
24956
24957 base-specifier-list:
24958 base-specifier ... [opt]
24959 base-specifier-list , base-specifier ... [opt]
24960
24961 Returns a TREE_LIST representing the base-classes, in the order in
24962 which they were declared. The representation of each node is as
24963 described by cp_parser_base_specifier.
24964
24965 In the case that no bases are specified, this function will return
24966 NULL_TREE, not ERROR_MARK_NODE. */
24967
24968 static tree
24969 cp_parser_base_clause (cp_parser* parser)
24970 {
24971 tree bases = NULL_TREE;
24972
24973 /* Look for the `:' that begins the list. */
24974 cp_parser_require (parser, CPP_COLON, RT_COLON);
24975
24976 /* Scan the base-specifier-list. */
24977 while (true)
24978 {
24979 cp_token *token;
24980 tree base;
24981 bool pack_expansion_p = false;
24982
24983 /* Look for the base-specifier. */
24984 base = cp_parser_base_specifier (parser);
24985 /* Look for the (optional) ellipsis. */
24986 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24987 {
24988 /* Consume the `...'. */
24989 cp_lexer_consume_token (parser->lexer);
24990
24991 pack_expansion_p = true;
24992 }
24993
24994 /* Add BASE to the front of the list. */
24995 if (base && base != error_mark_node)
24996 {
24997 if (pack_expansion_p)
24998 /* Make this a pack expansion type. */
24999 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
25000
25001 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
25002 {
25003 TREE_CHAIN (base) = bases;
25004 bases = base;
25005 }
25006 }
25007 /* Peek at the next token. */
25008 token = cp_lexer_peek_token (parser->lexer);
25009 /* If it's not a comma, then the list is complete. */
25010 if (token->type != CPP_COMMA)
25011 break;
25012 /* Consume the `,'. */
25013 cp_lexer_consume_token (parser->lexer);
25014 }
25015
25016 /* PARSER->SCOPE may still be non-NULL at this point, if the last
25017 base class had a qualified name. However, the next name that
25018 appears is certainly not qualified. */
25019 parser->scope = NULL_TREE;
25020 parser->qualifying_scope = NULL_TREE;
25021 parser->object_scope = NULL_TREE;
25022
25023 return nreverse (bases);
25024 }
25025
25026 /* Parse a base-specifier.
25027
25028 base-specifier:
25029 :: [opt] nested-name-specifier [opt] class-name
25030 virtual access-specifier [opt] :: [opt] nested-name-specifier
25031 [opt] class-name
25032 access-specifier virtual [opt] :: [opt] nested-name-specifier
25033 [opt] class-name
25034
25035 Returns a TREE_LIST. The TREE_PURPOSE will be one of
25036 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25037 indicate the specifiers provided. The TREE_VALUE will be a TYPE
25038 (or the ERROR_MARK_NODE) indicating the type that was specified. */
25039
25040 static tree
25041 cp_parser_base_specifier (cp_parser* parser)
25042 {
25043 cp_token *token;
25044 bool done = false;
25045 bool virtual_p = false;
25046 bool duplicate_virtual_error_issued_p = false;
25047 bool duplicate_access_error_issued_p = false;
25048 bool class_scope_p, template_p;
25049 tree access = access_default_node;
25050 tree type;
25051
25052 /* Process the optional `virtual' and `access-specifier'. */
25053 while (!done)
25054 {
25055 /* Peek at the next token. */
25056 token = cp_lexer_peek_token (parser->lexer);
25057 /* Process `virtual'. */
25058 switch (token->keyword)
25059 {
25060 case RID_VIRTUAL:
25061 /* If `virtual' appears more than once, issue an error. */
25062 if (virtual_p && !duplicate_virtual_error_issued_p)
25063 {
25064 cp_parser_error (parser,
25065 "%<virtual%> specified more than once in base-specifier");
25066 duplicate_virtual_error_issued_p = true;
25067 }
25068
25069 virtual_p = true;
25070
25071 /* Consume the `virtual' token. */
25072 cp_lexer_consume_token (parser->lexer);
25073
25074 break;
25075
25076 case RID_PUBLIC:
25077 case RID_PROTECTED:
25078 case RID_PRIVATE:
25079 /* If more than one access specifier appears, issue an
25080 error. */
25081 if (access != access_default_node
25082 && !duplicate_access_error_issued_p)
25083 {
25084 cp_parser_error (parser,
25085 "more than one access specifier in base-specifier");
25086 duplicate_access_error_issued_p = true;
25087 }
25088
25089 access = ridpointers[(int) token->keyword];
25090
25091 /* Consume the access-specifier. */
25092 cp_lexer_consume_token (parser->lexer);
25093
25094 break;
25095
25096 default:
25097 done = true;
25098 break;
25099 }
25100 }
25101 /* It is not uncommon to see programs mechanically, erroneously, use
25102 the 'typename' keyword to denote (dependent) qualified types
25103 as base classes. */
25104 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25105 {
25106 token = cp_lexer_peek_token (parser->lexer);
25107 if (!processing_template_decl)
25108 error_at (token->location,
25109 "keyword %<typename%> not allowed outside of templates");
25110 else
25111 error_at (token->location,
25112 "keyword %<typename%> not allowed in this context "
25113 "(the base class is implicitly a type)");
25114 cp_lexer_consume_token (parser->lexer);
25115 }
25116
25117 /* Look for the optional `::' operator. */
25118 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
25119 /* Look for the nested-name-specifier. The simplest way to
25120 implement:
25121
25122 [temp.res]
25123
25124 The keyword `typename' is not permitted in a base-specifier or
25125 mem-initializer; in these contexts a qualified name that
25126 depends on a template-parameter is implicitly assumed to be a
25127 type name.
25128
25129 is to pretend that we have seen the `typename' keyword at this
25130 point. */
25131 cp_parser_nested_name_specifier_opt (parser,
25132 /*typename_keyword_p=*/true,
25133 /*check_dependency_p=*/true,
25134 /*type_p=*/true,
25135 /*is_declaration=*/true);
25136 /* If the base class is given by a qualified name, assume that names
25137 we see are type names or templates, as appropriate. */
25138 class_scope_p = (parser->scope && TYPE_P (parser->scope));
25139 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
25140
25141 if (!parser->scope
25142 && cp_lexer_next_token_is_decltype (parser->lexer))
25143 /* DR 950 allows decltype as a base-specifier. */
25144 type = cp_parser_decltype (parser);
25145 else
25146 {
25147 /* Otherwise, look for the class-name. */
25148 type = cp_parser_class_name (parser,
25149 class_scope_p,
25150 template_p,
25151 typename_type,
25152 /*check_dependency_p=*/true,
25153 /*class_head_p=*/false,
25154 /*is_declaration=*/true);
25155 type = TREE_TYPE (type);
25156 }
25157
25158 if (type == error_mark_node)
25159 return error_mark_node;
25160
25161 return finish_base_specifier (type, access, virtual_p);
25162 }
25163
25164 /* Exception handling [gram.exception] */
25165
25166 /* Parse an (optional) noexcept-specification.
25167
25168 noexcept-specification:
25169 noexcept ( constant-expression ) [opt]
25170
25171 If no noexcept-specification is present, returns NULL_TREE.
25172 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25173 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25174 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25175 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25176 in which case a boolean condition is returned instead. */
25177
25178 static tree
25179 cp_parser_noexcept_specification_opt (cp_parser* parser,
25180 bool require_constexpr,
25181 bool* consumed_expr,
25182 bool return_cond)
25183 {
25184 cp_token *token;
25185 const char *saved_message;
25186
25187 /* Peek at the next token. */
25188 token = cp_lexer_peek_token (parser->lexer);
25189
25190 /* Is it a noexcept-specification? */
25191 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
25192 {
25193 tree expr;
25194 cp_lexer_consume_token (parser->lexer);
25195
25196 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
25197 {
25198 matching_parens parens;
25199 parens.consume_open (parser);
25200
25201 tree save_ccp = current_class_ptr;
25202 tree save_ccr = current_class_ref;
25203
25204 if (current_class_type)
25205 inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
25206
25207 if (require_constexpr)
25208 {
25209 /* Types may not be defined in an exception-specification. */
25210 saved_message = parser->type_definition_forbidden_message;
25211 parser->type_definition_forbidden_message
25212 = G_("types may not be defined in an exception-specification");
25213
25214 bool non_constant_p;
25215 expr
25216 = cp_parser_constant_expression (parser,
25217 /*allow_non_constant=*/true,
25218 &non_constant_p);
25219 if (non_constant_p
25220 && !require_potential_rvalue_constant_expression (expr))
25221 {
25222 expr = NULL_TREE;
25223 return_cond = true;
25224 }
25225
25226 /* Restore the saved message. */
25227 parser->type_definition_forbidden_message = saved_message;
25228 }
25229 else
25230 {
25231 expr = cp_parser_expression (parser);
25232 *consumed_expr = true;
25233 }
25234
25235 parens.require_close (parser);
25236
25237 current_class_ptr = save_ccp;
25238 current_class_ref = save_ccr;
25239 }
25240 else
25241 {
25242 expr = boolean_true_node;
25243 if (!require_constexpr)
25244 *consumed_expr = false;
25245 }
25246
25247 /* We cannot build a noexcept-spec right away because this will check
25248 that expr is a constexpr. */
25249 if (!return_cond)
25250 return build_noexcept_spec (expr, tf_warning_or_error);
25251 else
25252 return expr;
25253 }
25254 else
25255 return NULL_TREE;
25256 }
25257
25258 /* Parse an (optional) exception-specification.
25259
25260 exception-specification:
25261 throw ( type-id-list [opt] )
25262
25263 Returns a TREE_LIST representing the exception-specification. The
25264 TREE_VALUE of each node is a type. */
25265
25266 static tree
25267 cp_parser_exception_specification_opt (cp_parser* parser)
25268 {
25269 cp_token *token;
25270 tree type_id_list;
25271 const char *saved_message;
25272
25273 /* Peek at the next token. */
25274 token = cp_lexer_peek_token (parser->lexer);
25275
25276 /* Is it a noexcept-specification? */
25277 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
25278 false);
25279 if (type_id_list != NULL_TREE)
25280 return type_id_list;
25281
25282 /* If it's not `throw', then there's no exception-specification. */
25283 if (!cp_parser_is_keyword (token, RID_THROW))
25284 return NULL_TREE;
25285
25286 location_t loc = token->location;
25287
25288 /* Consume the `throw'. */
25289 cp_lexer_consume_token (parser->lexer);
25290
25291 /* Look for the `('. */
25292 matching_parens parens;
25293 parens.require_open (parser);
25294
25295 /* Peek at the next token. */
25296 token = cp_lexer_peek_token (parser->lexer);
25297 /* If it's not a `)', then there is a type-id-list. */
25298 if (token->type != CPP_CLOSE_PAREN)
25299 {
25300 /* Types may not be defined in an exception-specification. */
25301 saved_message = parser->type_definition_forbidden_message;
25302 parser->type_definition_forbidden_message
25303 = G_("types may not be defined in an exception-specification");
25304 /* Parse the type-id-list. */
25305 type_id_list = cp_parser_type_id_list (parser);
25306 /* Restore the saved message. */
25307 parser->type_definition_forbidden_message = saved_message;
25308
25309 if (cxx_dialect >= cxx17)
25310 {
25311 error_at (loc, "ISO C++17 does not allow dynamic exception "
25312 "specifications");
25313 type_id_list = NULL_TREE;
25314 }
25315 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
25316 warning_at (loc, OPT_Wdeprecated,
25317 "dynamic exception specifications are deprecated in "
25318 "C++11");
25319 }
25320 /* In C++17, throw() is equivalent to noexcept (true). throw()
25321 is deprecated in C++11 and above as well, but is still widely used,
25322 so don't warn about it yet. */
25323 else if (cxx_dialect >= cxx17)
25324 type_id_list = noexcept_true_spec;
25325 else
25326 type_id_list = empty_except_spec;
25327
25328 /* Look for the `)'. */
25329 parens.require_close (parser);
25330
25331 return type_id_list;
25332 }
25333
25334 /* Parse an (optional) type-id-list.
25335
25336 type-id-list:
25337 type-id ... [opt]
25338 type-id-list , type-id ... [opt]
25339
25340 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25341 in the order that the types were presented. */
25342
25343 static tree
25344 cp_parser_type_id_list (cp_parser* parser)
25345 {
25346 tree types = NULL_TREE;
25347
25348 while (true)
25349 {
25350 cp_token *token;
25351 tree type;
25352
25353 token = cp_lexer_peek_token (parser->lexer);
25354
25355 /* Get the next type-id. */
25356 type = cp_parser_type_id (parser);
25357 /* Check for invalid 'auto'. */
25358 if (flag_concepts && type_uses_auto (type))
25359 {
25360 error_at (token->location,
25361 "invalid use of %<auto%> in exception-specification");
25362 type = error_mark_node;
25363 }
25364 /* Parse the optional ellipsis. */
25365 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25366 {
25367 /* Consume the `...'. */
25368 cp_lexer_consume_token (parser->lexer);
25369
25370 /* Turn the type into a pack expansion expression. */
25371 type = make_pack_expansion (type);
25372 }
25373 /* Add it to the list. */
25374 types = add_exception_specifier (types, type, /*complain=*/1);
25375 /* Peek at the next token. */
25376 token = cp_lexer_peek_token (parser->lexer);
25377 /* If it is not a `,', we are done. */
25378 if (token->type != CPP_COMMA)
25379 break;
25380 /* Consume the `,'. */
25381 cp_lexer_consume_token (parser->lexer);
25382 }
25383
25384 return nreverse (types);
25385 }
25386
25387 /* Parse a try-block.
25388
25389 try-block:
25390 try compound-statement handler-seq */
25391
25392 static tree
25393 cp_parser_try_block (cp_parser* parser)
25394 {
25395 tree try_block;
25396
25397 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
25398 if (parser->in_function_body
25399 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25400 && cxx_dialect < cxx2a)
25401 pedwarn (input_location, 0,
25402 "%<try%> in %<constexpr%> function only "
25403 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
25404
25405 try_block = begin_try_block ();
25406 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
25407 finish_try_block (try_block);
25408 cp_parser_handler_seq (parser);
25409 finish_handler_sequence (try_block);
25410
25411 return try_block;
25412 }
25413
25414 /* Parse a function-try-block.
25415
25416 function-try-block:
25417 try ctor-initializer [opt] function-body handler-seq */
25418
25419 static void
25420 cp_parser_function_try_block (cp_parser* parser)
25421 {
25422 tree compound_stmt;
25423 tree try_block;
25424
25425 /* Look for the `try' keyword. */
25426 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
25427 return;
25428 /* Let the rest of the front end know where we are. */
25429 try_block = begin_function_try_block (&compound_stmt);
25430 /* Parse the function-body. */
25431 cp_parser_ctor_initializer_opt_and_function_body
25432 (parser, /*in_function_try_block=*/true);
25433 /* We're done with the `try' part. */
25434 finish_function_try_block (try_block);
25435 /* Parse the handlers. */
25436 cp_parser_handler_seq (parser);
25437 /* We're done with the handlers. */
25438 finish_function_handler_sequence (try_block, compound_stmt);
25439 }
25440
25441 /* Parse a handler-seq.
25442
25443 handler-seq:
25444 handler handler-seq [opt] */
25445
25446 static void
25447 cp_parser_handler_seq (cp_parser* parser)
25448 {
25449 while (true)
25450 {
25451 cp_token *token;
25452
25453 /* Parse the handler. */
25454 cp_parser_handler (parser);
25455 /* Peek at the next token. */
25456 token = cp_lexer_peek_token (parser->lexer);
25457 /* If it's not `catch' then there are no more handlers. */
25458 if (!cp_parser_is_keyword (token, RID_CATCH))
25459 break;
25460 }
25461 }
25462
25463 /* Parse a handler.
25464
25465 handler:
25466 catch ( exception-declaration ) compound-statement */
25467
25468 static void
25469 cp_parser_handler (cp_parser* parser)
25470 {
25471 tree handler;
25472 tree declaration;
25473
25474 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
25475 handler = begin_handler ();
25476 matching_parens parens;
25477 parens.require_open (parser);
25478 declaration = cp_parser_exception_declaration (parser);
25479 finish_handler_parms (declaration, handler);
25480 parens.require_close (parser);
25481 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
25482 finish_handler (handler);
25483 }
25484
25485 /* Parse an exception-declaration.
25486
25487 exception-declaration:
25488 type-specifier-seq declarator
25489 type-specifier-seq abstract-declarator
25490 type-specifier-seq
25491 ...
25492
25493 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25494 ellipsis variant is used. */
25495
25496 static tree
25497 cp_parser_exception_declaration (cp_parser* parser)
25498 {
25499 cp_decl_specifier_seq type_specifiers;
25500 cp_declarator *declarator;
25501 const char *saved_message;
25502
25503 /* If it's an ellipsis, it's easy to handle. */
25504 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25505 {
25506 /* Consume the `...' token. */
25507 cp_lexer_consume_token (parser->lexer);
25508 return NULL_TREE;
25509 }
25510
25511 /* Types may not be defined in exception-declarations. */
25512 saved_message = parser->type_definition_forbidden_message;
25513 parser->type_definition_forbidden_message
25514 = G_("types may not be defined in exception-declarations");
25515
25516 /* Parse the type-specifier-seq. */
25517 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
25518 /*is_declaration=*/true,
25519 /*is_trailing_return=*/false,
25520 &type_specifiers);
25521 /* If it's a `)', then there is no declarator. */
25522 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25523 declarator = NULL;
25524 else
25525 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
25526 CP_PARSER_FLAGS_NONE,
25527 /*ctor_dtor_or_conv_p=*/NULL,
25528 /*parenthesized_p=*/NULL,
25529 /*member_p=*/false,
25530 /*friend_p=*/false,
25531 /*static_p=*/false);
25532
25533 /* Restore the saved message. */
25534 parser->type_definition_forbidden_message = saved_message;
25535
25536 if (!type_specifiers.any_specifiers_p)
25537 return error_mark_node;
25538
25539 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
25540 }
25541
25542 /* Parse a throw-expression.
25543
25544 throw-expression:
25545 throw assignment-expression [opt]
25546
25547 Returns a THROW_EXPR representing the throw-expression. */
25548
25549 static tree
25550 cp_parser_throw_expression (cp_parser* parser)
25551 {
25552 tree expression;
25553 cp_token* token;
25554
25555 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
25556 token = cp_lexer_peek_token (parser->lexer);
25557 /* Figure out whether or not there is an assignment-expression
25558 following the "throw" keyword. */
25559 if (token->type == CPP_COMMA
25560 || token->type == CPP_SEMICOLON
25561 || token->type == CPP_CLOSE_PAREN
25562 || token->type == CPP_CLOSE_SQUARE
25563 || token->type == CPP_CLOSE_BRACE
25564 || token->type == CPP_COLON)
25565 expression = NULL_TREE;
25566 else
25567 expression = cp_parser_assignment_expression (parser);
25568
25569 return build_throw (expression);
25570 }
25571
25572 /* GNU Extensions */
25573
25574 /* Parse an (optional) asm-specification.
25575
25576 asm-specification:
25577 asm ( string-literal )
25578
25579 If the asm-specification is present, returns a STRING_CST
25580 corresponding to the string-literal. Otherwise, returns
25581 NULL_TREE. */
25582
25583 static tree
25584 cp_parser_asm_specification_opt (cp_parser* parser)
25585 {
25586 cp_token *token;
25587 tree asm_specification;
25588
25589 /* Peek at the next token. */
25590 token = cp_lexer_peek_token (parser->lexer);
25591 /* If the next token isn't the `asm' keyword, then there's no
25592 asm-specification. */
25593 if (!cp_parser_is_keyword (token, RID_ASM))
25594 return NULL_TREE;
25595
25596 /* Consume the `asm' token. */
25597 cp_lexer_consume_token (parser->lexer);
25598 /* Look for the `('. */
25599 matching_parens parens;
25600 parens.require_open (parser);
25601
25602 /* Look for the string-literal. */
25603 asm_specification = cp_parser_string_literal (parser, false, false);
25604
25605 /* Look for the `)'. */
25606 parens.require_close (parser);
25607
25608 return asm_specification;
25609 }
25610
25611 /* Parse an asm-operand-list.
25612
25613 asm-operand-list:
25614 asm-operand
25615 asm-operand-list , asm-operand
25616
25617 asm-operand:
25618 string-literal ( expression )
25619 [ string-literal ] string-literal ( expression )
25620
25621 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25622 each node is the expression. The TREE_PURPOSE is itself a
25623 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25624 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25625 is a STRING_CST for the string literal before the parenthesis. Returns
25626 ERROR_MARK_NODE if any of the operands are invalid. */
25627
25628 static tree
25629 cp_parser_asm_operand_list (cp_parser* parser)
25630 {
25631 tree asm_operands = NULL_TREE;
25632 bool invalid_operands = false;
25633
25634 while (true)
25635 {
25636 tree string_literal;
25637 tree expression;
25638 tree name;
25639
25640 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25641 {
25642 /* Consume the `[' token. */
25643 cp_lexer_consume_token (parser->lexer);
25644 /* Read the operand name. */
25645 name = cp_parser_identifier (parser);
25646 if (name != error_mark_node)
25647 name = build_string (IDENTIFIER_LENGTH (name),
25648 IDENTIFIER_POINTER (name));
25649 /* Look for the closing `]'. */
25650 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25651 }
25652 else
25653 name = NULL_TREE;
25654 /* Look for the string-literal. */
25655 string_literal = cp_parser_string_literal (parser, false, false);
25656
25657 /* Look for the `('. */
25658 matching_parens parens;
25659 parens.require_open (parser);
25660 /* Parse the expression. */
25661 expression = cp_parser_expression (parser);
25662 /* Look for the `)'. */
25663 parens.require_close (parser);
25664
25665 if (name == error_mark_node
25666 || string_literal == error_mark_node
25667 || expression == error_mark_node)
25668 invalid_operands = true;
25669
25670 /* Add this operand to the list. */
25671 asm_operands = tree_cons (build_tree_list (name, string_literal),
25672 expression,
25673 asm_operands);
25674 /* If the next token is not a `,', there are no more
25675 operands. */
25676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25677 break;
25678 /* Consume the `,'. */
25679 cp_lexer_consume_token (parser->lexer);
25680 }
25681
25682 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25683 }
25684
25685 /* Parse an asm-clobber-list.
25686
25687 asm-clobber-list:
25688 string-literal
25689 asm-clobber-list , string-literal
25690
25691 Returns a TREE_LIST, indicating the clobbers in the order that they
25692 appeared. The TREE_VALUE of each node is a STRING_CST. */
25693
25694 static tree
25695 cp_parser_asm_clobber_list (cp_parser* parser)
25696 {
25697 tree clobbers = NULL_TREE;
25698
25699 while (true)
25700 {
25701 tree string_literal;
25702
25703 /* Look for the string literal. */
25704 string_literal = cp_parser_string_literal (parser, false, false);
25705 /* Add it to the list. */
25706 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25707 /* If the next token is not a `,', then the list is
25708 complete. */
25709 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25710 break;
25711 /* Consume the `,' token. */
25712 cp_lexer_consume_token (parser->lexer);
25713 }
25714
25715 return clobbers;
25716 }
25717
25718 /* Parse an asm-label-list.
25719
25720 asm-label-list:
25721 identifier
25722 asm-label-list , identifier
25723
25724 Returns a TREE_LIST, indicating the labels in the order that they
25725 appeared. The TREE_VALUE of each node is a label. */
25726
25727 static tree
25728 cp_parser_asm_label_list (cp_parser* parser)
25729 {
25730 tree labels = NULL_TREE;
25731
25732 while (true)
25733 {
25734 tree identifier, label, name;
25735
25736 /* Look for the identifier. */
25737 identifier = cp_parser_identifier (parser);
25738 if (!error_operand_p (identifier))
25739 {
25740 label = lookup_label (identifier);
25741 if (TREE_CODE (label) == LABEL_DECL)
25742 {
25743 TREE_USED (label) = 1;
25744 check_goto (label);
25745 name = build_string (IDENTIFIER_LENGTH (identifier),
25746 IDENTIFIER_POINTER (identifier));
25747 labels = tree_cons (name, label, labels);
25748 }
25749 }
25750 /* If the next token is not a `,', then the list is
25751 complete. */
25752 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25753 break;
25754 /* Consume the `,' token. */
25755 cp_lexer_consume_token (parser->lexer);
25756 }
25757
25758 return nreverse (labels);
25759 }
25760
25761 /* Return TRUE iff the next tokens in the stream are possibly the
25762 beginning of a GNU extension attribute. */
25763
25764 static bool
25765 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25766 {
25767 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25768 }
25769
25770 /* Return TRUE iff the next tokens in the stream are possibly the
25771 beginning of a standard C++-11 attribute specifier. */
25772
25773 static bool
25774 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25775 {
25776 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25777 }
25778
25779 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25780 beginning of a standard C++-11 attribute specifier. */
25781
25782 static bool
25783 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25784 {
25785 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25786
25787 return (cxx_dialect >= cxx11
25788 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25789 || (token->type == CPP_OPEN_SQUARE
25790 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25791 && token->type == CPP_OPEN_SQUARE)));
25792 }
25793
25794 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25795 beginning of a GNU extension attribute. */
25796
25797 static bool
25798 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25799 {
25800 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25801
25802 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25803 }
25804
25805 /* Return true iff the next tokens can be the beginning of either a
25806 GNU attribute list, or a standard C++11 attribute sequence. */
25807
25808 static bool
25809 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25810 {
25811 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25812 || cp_next_tokens_can_be_std_attribute_p (parser));
25813 }
25814
25815 /* Return true iff the next Nth tokens can be the beginning of either
25816 a GNU attribute list, or a standard C++11 attribute sequence. */
25817
25818 static bool
25819 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25820 {
25821 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25822 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25823 }
25824
25825 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25826 of GNU attributes, or return NULL. */
25827
25828 static tree
25829 cp_parser_attributes_opt (cp_parser *parser)
25830 {
25831 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25832 return cp_parser_gnu_attributes_opt (parser);
25833 return cp_parser_std_attribute_spec_seq (parser);
25834 }
25835
25836 /* Parse an (optional) series of attributes.
25837
25838 attributes:
25839 attributes attribute
25840
25841 attribute:
25842 __attribute__ (( attribute-list [opt] ))
25843
25844 The return value is as for cp_parser_gnu_attribute_list. */
25845
25846 static tree
25847 cp_parser_gnu_attributes_opt (cp_parser* parser)
25848 {
25849 tree attributes = NULL_TREE;
25850
25851 temp_override<bool> cleanup
25852 (parser->auto_is_implicit_function_template_parm_p, false);
25853
25854 while (true)
25855 {
25856 cp_token *token;
25857 tree attribute_list;
25858 bool ok = true;
25859
25860 /* Peek at the next token. */
25861 token = cp_lexer_peek_token (parser->lexer);
25862 /* If it's not `__attribute__', then we're done. */
25863 if (token->keyword != RID_ATTRIBUTE)
25864 break;
25865
25866 /* Consume the `__attribute__' keyword. */
25867 cp_lexer_consume_token (parser->lexer);
25868 /* Look for the two `(' tokens. */
25869 matching_parens outer_parens;
25870 if (!outer_parens.require_open (parser))
25871 ok = false;
25872 matching_parens inner_parens;
25873 if (!inner_parens.require_open (parser))
25874 ok = false;
25875
25876 /* Peek at the next token. */
25877 token = cp_lexer_peek_token (parser->lexer);
25878 if (token->type != CPP_CLOSE_PAREN)
25879 /* Parse the attribute-list. */
25880 attribute_list = cp_parser_gnu_attribute_list (parser);
25881 else
25882 /* If the next token is a `)', then there is no attribute
25883 list. */
25884 attribute_list = NULL;
25885
25886 /* Look for the two `)' tokens. */
25887 if (!inner_parens.require_close (parser))
25888 ok = false;
25889 if (!outer_parens.require_close (parser))
25890 ok = false;
25891 if (!ok)
25892 cp_parser_skip_to_end_of_statement (parser);
25893
25894 /* Add these new attributes to the list. */
25895 attributes = attr_chainon (attributes, attribute_list);
25896 }
25897
25898 return attributes;
25899 }
25900
25901 /* Parse a GNU attribute-list.
25902
25903 attribute-list:
25904 attribute
25905 attribute-list , attribute
25906
25907 attribute:
25908 identifier
25909 identifier ( identifier )
25910 identifier ( identifier , expression-list )
25911 identifier ( expression-list )
25912
25913 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25914 to an attribute. The TREE_PURPOSE of each node is the identifier
25915 indicating which attribute is in use. The TREE_VALUE represents
25916 the arguments, if any. */
25917
25918 static tree
25919 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
25920 {
25921 tree attribute_list = NULL_TREE;
25922 bool save_translate_strings_p = parser->translate_strings_p;
25923
25924 /* Don't create wrapper nodes within attributes: the
25925 handlers don't know how to handle them. */
25926 auto_suppress_location_wrappers sentinel;
25927
25928 parser->translate_strings_p = false;
25929 while (true)
25930 {
25931 cp_token *token;
25932 tree identifier;
25933 tree attribute;
25934
25935 /* Look for the identifier. We also allow keywords here; for
25936 example `__attribute__ ((const))' is legal. */
25937 token = cp_lexer_peek_token (parser->lexer);
25938 if (token->type == CPP_NAME
25939 || token->type == CPP_KEYWORD)
25940 {
25941 tree arguments = NULL_TREE;
25942
25943 /* Consume the token, but save it since we need it for the
25944 SIMD enabled function parsing. */
25945 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25946
25947 /* Save away the identifier that indicates which attribute
25948 this is. */
25949 identifier = (token->type == CPP_KEYWORD)
25950 /* For keywords, use the canonical spelling, not the
25951 parsed identifier. */
25952 ? ridpointers[(int) token->keyword]
25953 : id_token->u.value;
25954
25955 identifier = canonicalize_attr_name (identifier);
25956 attribute = build_tree_list (identifier, NULL_TREE);
25957
25958 /* Peek at the next token. */
25959 token = cp_lexer_peek_token (parser->lexer);
25960 /* If it's an `(', then parse the attribute arguments. */
25961 if (token->type == CPP_OPEN_PAREN)
25962 {
25963 vec<tree, va_gc> *vec;
25964 int attr_flag = (attribute_takes_identifier_p (identifier)
25965 ? id_attr : normal_attr);
25966 vec = cp_parser_parenthesized_expression_list
25967 (parser, attr_flag, /*cast_p=*/false,
25968 /*allow_expansion_p=*/false,
25969 /*non_constant_p=*/NULL);
25970 if (vec == NULL)
25971 arguments = error_mark_node;
25972 else
25973 {
25974 arguments = build_tree_list_vec (vec);
25975 release_tree_vector (vec);
25976 }
25977 /* Save the arguments away. */
25978 TREE_VALUE (attribute) = arguments;
25979 }
25980
25981 if (arguments != error_mark_node)
25982 {
25983 /* Add this attribute to the list. */
25984 TREE_CHAIN (attribute) = attribute_list;
25985 attribute_list = attribute;
25986 }
25987
25988 token = cp_lexer_peek_token (parser->lexer);
25989 }
25990 /* Unless EXACTLY_ONE is set look for more attributes.
25991 If the next token isn't a `,', we're done. */
25992 if (exactly_one || token->type != CPP_COMMA)
25993 break;
25994
25995 /* Consume the comma and keep going. */
25996 cp_lexer_consume_token (parser->lexer);
25997 }
25998 parser->translate_strings_p = save_translate_strings_p;
25999
26000 /* We built up the list in reverse order. */
26001 return nreverse (attribute_list);
26002 }
26003
26004 /* Parse a standard C++11 attribute.
26005
26006 The returned representation is a TREE_LIST which TREE_PURPOSE is
26007 the scoped name of the attribute, and the TREE_VALUE is its
26008 arguments list.
26009
26010 Note that the scoped name of the attribute is itself a TREE_LIST
26011 which TREE_PURPOSE is the namespace of the attribute, and
26012 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
26013 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
26014 and which TREE_PURPOSE is directly the attribute name.
26015
26016 Clients of the attribute code should use get_attribute_namespace
26017 and get_attribute_name to get the actual namespace and name of
26018 attributes, regardless of their being GNU or C++11 attributes.
26019
26020 attribute:
26021 attribute-token attribute-argument-clause [opt]
26022
26023 attribute-token:
26024 identifier
26025 attribute-scoped-token
26026
26027 attribute-scoped-token:
26028 attribute-namespace :: identifier
26029
26030 attribute-namespace:
26031 identifier
26032
26033 attribute-argument-clause:
26034 ( balanced-token-seq )
26035
26036 balanced-token-seq:
26037 balanced-token [opt]
26038 balanced-token-seq balanced-token
26039
26040 balanced-token:
26041 ( balanced-token-seq )
26042 [ balanced-token-seq ]
26043 { balanced-token-seq }. */
26044
26045 static tree
26046 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
26047 {
26048 tree attribute, attr_id = NULL_TREE, arguments;
26049 cp_token *token;
26050
26051 temp_override<bool> cleanup
26052 (parser->auto_is_implicit_function_template_parm_p, false);
26053
26054 /* First, parse name of the attribute, a.k.a attribute-token. */
26055
26056 token = cp_lexer_peek_token (parser->lexer);
26057 if (token->type == CPP_NAME)
26058 attr_id = token->u.value;
26059 else if (token->type == CPP_KEYWORD)
26060 attr_id = ridpointers[(int) token->keyword];
26061 else if (token->flags & NAMED_OP)
26062 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26063
26064 if (attr_id == NULL_TREE)
26065 return NULL_TREE;
26066
26067 cp_lexer_consume_token (parser->lexer);
26068
26069 token = cp_lexer_peek_token (parser->lexer);
26070 if (token->type == CPP_SCOPE)
26071 {
26072 /* We are seeing a scoped attribute token. */
26073
26074 cp_lexer_consume_token (parser->lexer);
26075 if (attr_ns)
26076 error_at (token->location, "attribute using prefix used together "
26077 "with scoped attribute token");
26078 attr_ns = attr_id;
26079
26080 token = cp_lexer_consume_token (parser->lexer);
26081 if (token->type == CPP_NAME)
26082 attr_id = token->u.value;
26083 else if (token->type == CPP_KEYWORD)
26084 attr_id = ridpointers[(int) token->keyword];
26085 else if (token->flags & NAMED_OP)
26086 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26087 else
26088 {
26089 error_at (token->location,
26090 "expected an identifier for the attribute name");
26091 return error_mark_node;
26092 }
26093
26094 attr_ns = canonicalize_attr_name (attr_ns);
26095 attr_id = canonicalize_attr_name (attr_id);
26096 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26097 NULL_TREE);
26098 token = cp_lexer_peek_token (parser->lexer);
26099 }
26100 else if (attr_ns)
26101 {
26102 attr_ns = canonicalize_attr_name (attr_ns);
26103 attr_id = canonicalize_attr_name (attr_id);
26104 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26105 NULL_TREE);
26106 }
26107 else
26108 {
26109 attr_id = canonicalize_attr_name (attr_id);
26110 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
26111 NULL_TREE);
26112 /* C++11 noreturn attribute is equivalent to GNU's. */
26113 if (is_attribute_p ("noreturn", attr_id))
26114 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26115 /* C++14 deprecated attribute is equivalent to GNU's. */
26116 else if (is_attribute_p ("deprecated", attr_id))
26117 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26118 /* C++17 fallthrough attribute is equivalent to GNU's. */
26119 else if (is_attribute_p ("fallthrough", attr_id))
26120 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26121 /* Transactional Memory TS optimize_for_synchronized attribute is
26122 equivalent to GNU transaction_callable. */
26123 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
26124 TREE_PURPOSE (attribute)
26125 = get_identifier ("transaction_callable");
26126 /* Transactional Memory attributes are GNU attributes. */
26127 else if (tm_attr_to_mask (attr_id))
26128 TREE_PURPOSE (attribute) = attr_id;
26129 }
26130
26131 /* Now parse the optional argument clause of the attribute. */
26132
26133 if (token->type != CPP_OPEN_PAREN)
26134 return attribute;
26135
26136 {
26137 vec<tree, va_gc> *vec;
26138 int attr_flag = normal_attr;
26139
26140 if (attr_ns == gnu_identifier
26141 && attribute_takes_identifier_p (attr_id))
26142 /* A GNU attribute that takes an identifier in parameter. */
26143 attr_flag = id_attr;
26144
26145 vec = cp_parser_parenthesized_expression_list
26146 (parser, attr_flag, /*cast_p=*/false,
26147 /*allow_expansion_p=*/true,
26148 /*non_constant_p=*/NULL);
26149 if (vec == NULL)
26150 arguments = error_mark_node;
26151 else
26152 {
26153 arguments = build_tree_list_vec (vec);
26154 release_tree_vector (vec);
26155 }
26156
26157 if (arguments == error_mark_node)
26158 attribute = error_mark_node;
26159 else
26160 TREE_VALUE (attribute) = arguments;
26161 }
26162
26163 return attribute;
26164 }
26165
26166 /* Check that the attribute ATTRIBUTE appears at most once in the
26167 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
26168 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
26169 isn't implemented yet in GCC. */
26170
26171 static void
26172 cp_parser_check_std_attribute (tree attributes, tree attribute)
26173 {
26174 if (attributes)
26175 {
26176 tree name = get_attribute_name (attribute);
26177 if (is_attribute_p ("noreturn", name)
26178 && lookup_attribute ("noreturn", attributes))
26179 error ("attribute %<noreturn%> can appear at most once "
26180 "in an attribute-list");
26181 else if (is_attribute_p ("deprecated", name)
26182 && lookup_attribute ("deprecated", attributes))
26183 error ("attribute %<deprecated%> can appear at most once "
26184 "in an attribute-list");
26185 }
26186 }
26187
26188 /* Parse a list of standard C++-11 attributes.
26189
26190 attribute-list:
26191 attribute [opt]
26192 attribute-list , attribute[opt]
26193 attribute ...
26194 attribute-list , attribute ...
26195 */
26196
26197 static tree
26198 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
26199 {
26200 tree attributes = NULL_TREE, attribute = NULL_TREE;
26201 cp_token *token = NULL;
26202
26203 while (true)
26204 {
26205 attribute = cp_parser_std_attribute (parser, attr_ns);
26206 if (attribute == error_mark_node)
26207 break;
26208 if (attribute != NULL_TREE)
26209 {
26210 cp_parser_check_std_attribute (attributes, attribute);
26211 TREE_CHAIN (attribute) = attributes;
26212 attributes = attribute;
26213 }
26214 token = cp_lexer_peek_token (parser->lexer);
26215 if (token->type == CPP_ELLIPSIS)
26216 {
26217 cp_lexer_consume_token (parser->lexer);
26218 if (attribute == NULL_TREE)
26219 error_at (token->location,
26220 "expected attribute before %<...%>");
26221 else
26222 {
26223 tree pack = make_pack_expansion (TREE_VALUE (attribute));
26224 if (pack == error_mark_node)
26225 return error_mark_node;
26226 TREE_VALUE (attribute) = pack;
26227 }
26228 token = cp_lexer_peek_token (parser->lexer);
26229 }
26230 if (token->type != CPP_COMMA)
26231 break;
26232 cp_lexer_consume_token (parser->lexer);
26233 }
26234 attributes = nreverse (attributes);
26235 return attributes;
26236 }
26237
26238 /* Parse a standard C++-11 attribute specifier.
26239
26240 attribute-specifier:
26241 [ [ attribute-using-prefix [opt] attribute-list ] ]
26242 alignment-specifier
26243
26244 attribute-using-prefix:
26245 using attribute-namespace :
26246
26247 alignment-specifier:
26248 alignas ( type-id ... [opt] )
26249 alignas ( alignment-expression ... [opt] ). */
26250
26251 static tree
26252 cp_parser_std_attribute_spec (cp_parser *parser)
26253 {
26254 tree attributes = NULL_TREE;
26255 cp_token *token = cp_lexer_peek_token (parser->lexer);
26256
26257 if (token->type == CPP_OPEN_SQUARE
26258 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
26259 {
26260 tree attr_ns = NULL_TREE;
26261
26262 cp_lexer_consume_token (parser->lexer);
26263 cp_lexer_consume_token (parser->lexer);
26264
26265 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26266 {
26267 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26268 if (token->type == CPP_NAME)
26269 attr_ns = token->u.value;
26270 else if (token->type == CPP_KEYWORD)
26271 attr_ns = ridpointers[(int) token->keyword];
26272 else if (token->flags & NAMED_OP)
26273 attr_ns = get_identifier (cpp_type2name (token->type,
26274 token->flags));
26275 if (attr_ns
26276 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
26277 {
26278 if (cxx_dialect < cxx17
26279 && !in_system_header_at (input_location))
26280 pedwarn (input_location, 0,
26281 "attribute using prefix only available "
26282 "with %<-std=c++17%> or %<-std=gnu++17%>");
26283
26284 cp_lexer_consume_token (parser->lexer);
26285 cp_lexer_consume_token (parser->lexer);
26286 cp_lexer_consume_token (parser->lexer);
26287 }
26288 else
26289 attr_ns = NULL_TREE;
26290 }
26291
26292 attributes = cp_parser_std_attribute_list (parser, attr_ns);
26293
26294 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
26295 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
26296 cp_parser_skip_to_end_of_statement (parser);
26297 else
26298 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26299 when we are sure that we have actually parsed them. */
26300 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26301 }
26302 else
26303 {
26304 tree alignas_expr;
26305
26306 /* Look for an alignment-specifier. */
26307
26308 token = cp_lexer_peek_token (parser->lexer);
26309
26310 if (token->type != CPP_KEYWORD
26311 || token->keyword != RID_ALIGNAS)
26312 return NULL_TREE;
26313
26314 cp_lexer_consume_token (parser->lexer);
26315 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26316
26317 matching_parens parens;
26318 if (!parens.require_open (parser))
26319 return error_mark_node;
26320
26321 cp_parser_parse_tentatively (parser);
26322 alignas_expr = cp_parser_type_id (parser);
26323
26324 if (!cp_parser_parse_definitely (parser))
26325 {
26326 alignas_expr = cp_parser_assignment_expression (parser);
26327 if (alignas_expr == error_mark_node)
26328 cp_parser_skip_to_end_of_statement (parser);
26329 if (alignas_expr == NULL_TREE
26330 || alignas_expr == error_mark_node)
26331 return alignas_expr;
26332 }
26333
26334 alignas_expr = cxx_alignas_expr (alignas_expr);
26335 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
26336
26337 /* Handle alignas (pack...). */
26338 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26339 {
26340 cp_lexer_consume_token (parser->lexer);
26341 alignas_expr = make_pack_expansion (alignas_expr);
26342 }
26343
26344 /* Something went wrong, so don't build the attribute. */
26345 if (alignas_expr == error_mark_node)
26346 return error_mark_node;
26347
26348 if (!parens.require_close (parser))
26349 return error_mark_node;
26350
26351 /* Build the C++-11 representation of an 'aligned'
26352 attribute. */
26353 attributes
26354 = build_tree_list (build_tree_list (gnu_identifier,
26355 aligned_identifier), alignas_expr);
26356 }
26357
26358 return attributes;
26359 }
26360
26361 /* Parse a standard C++-11 attribute-specifier-seq.
26362
26363 attribute-specifier-seq:
26364 attribute-specifier-seq [opt] attribute-specifier
26365 */
26366
26367 static tree
26368 cp_parser_std_attribute_spec_seq (cp_parser *parser)
26369 {
26370 tree attr_specs = NULL_TREE;
26371 tree attr_last = NULL_TREE;
26372
26373 /* Don't create wrapper nodes within attributes: the
26374 handlers don't know how to handle them. */
26375 auto_suppress_location_wrappers sentinel;
26376
26377 while (true)
26378 {
26379 tree attr_spec = cp_parser_std_attribute_spec (parser);
26380 if (attr_spec == NULL_TREE)
26381 break;
26382 if (attr_spec == error_mark_node)
26383 return error_mark_node;
26384
26385 if (attr_last)
26386 TREE_CHAIN (attr_last) = attr_spec;
26387 else
26388 attr_specs = attr_last = attr_spec;
26389 attr_last = tree_last (attr_last);
26390 }
26391
26392 return attr_specs;
26393 }
26394
26395 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26396 return index of the first token after balanced-token, or N on failure. */
26397
26398 static size_t
26399 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
26400 {
26401 size_t orig_n = n;
26402 int nparens = 0, nbraces = 0, nsquares = 0;
26403 do
26404 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
26405 {
26406 case CPP_PRAGMA_EOL:
26407 if (!parser->lexer->in_pragma)
26408 break;
26409 /* FALLTHRU */
26410 case CPP_EOF:
26411 /* Ran out of tokens. */
26412 return orig_n;
26413 case CPP_OPEN_PAREN:
26414 ++nparens;
26415 break;
26416 case CPP_OPEN_BRACE:
26417 ++nbraces;
26418 break;
26419 case CPP_OPEN_SQUARE:
26420 ++nsquares;
26421 break;
26422 case CPP_CLOSE_PAREN:
26423 --nparens;
26424 break;
26425 case CPP_CLOSE_BRACE:
26426 --nbraces;
26427 break;
26428 case CPP_CLOSE_SQUARE:
26429 --nsquares;
26430 break;
26431 default:
26432 break;
26433 }
26434 while (nparens || nbraces || nsquares);
26435 return n;
26436 }
26437
26438 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26439 return index of the first token after the GNU attribute tokens, or N on
26440 failure. */
26441
26442 static size_t
26443 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
26444 {
26445 while (true)
26446 {
26447 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
26448 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
26449 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
26450 break;
26451
26452 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
26453 if (n2 == n + 2)
26454 break;
26455 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
26456 break;
26457 n = n2 + 1;
26458 }
26459 return n;
26460 }
26461
26462 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26463 next token), return index of the first token after the standard C++11
26464 attribute tokens, or N on failure. */
26465
26466 static size_t
26467 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
26468 {
26469 while (true)
26470 {
26471 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
26472 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
26473 {
26474 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26475 if (n2 == n + 1)
26476 break;
26477 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
26478 break;
26479 n = n2 + 1;
26480 }
26481 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
26482 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
26483 {
26484 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26485 if (n2 == n + 1)
26486 break;
26487 n = n2;
26488 }
26489 else
26490 break;
26491 }
26492 return n;
26493 }
26494
26495 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26496 as the next token), return index of the first token after the attribute
26497 tokens, or N on failure. */
26498
26499 static size_t
26500 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
26501 {
26502 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
26503 return cp_parser_skip_gnu_attributes_opt (parser, n);
26504 return cp_parser_skip_std_attribute_spec_seq (parser, n);
26505 }
26506
26507 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26508 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26509 current value of the PEDANTIC flag, regardless of whether or not
26510 the `__extension__' keyword is present. The caller is responsible
26511 for restoring the value of the PEDANTIC flag. */
26512
26513 static bool
26514 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
26515 {
26516 /* Save the old value of the PEDANTIC flag. */
26517 *saved_pedantic = pedantic;
26518
26519 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
26520 {
26521 /* Consume the `__extension__' token. */
26522 cp_lexer_consume_token (parser->lexer);
26523 /* We're not being pedantic while the `__extension__' keyword is
26524 in effect. */
26525 pedantic = 0;
26526
26527 return true;
26528 }
26529
26530 return false;
26531 }
26532
26533 /* Parse a label declaration.
26534
26535 label-declaration:
26536 __label__ label-declarator-seq ;
26537
26538 label-declarator-seq:
26539 identifier , label-declarator-seq
26540 identifier */
26541
26542 static void
26543 cp_parser_label_declaration (cp_parser* parser)
26544 {
26545 /* Look for the `__label__' keyword. */
26546 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
26547
26548 while (true)
26549 {
26550 tree identifier;
26551
26552 /* Look for an identifier. */
26553 identifier = cp_parser_identifier (parser);
26554 /* If we failed, stop. */
26555 if (identifier == error_mark_node)
26556 break;
26557 /* Declare it as a label. */
26558 finish_label_decl (identifier);
26559 /* If the next token is a `;', stop. */
26560 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26561 break;
26562 /* Look for the `,' separating the label declarations. */
26563 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
26564 }
26565
26566 /* Look for the final `;'. */
26567 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26568 }
26569
26570 // -------------------------------------------------------------------------- //
26571 // Requires Clause
26572
26573 // Parse a requires clause.
26574 //
26575 // requires-clause:
26576 // 'requires' logical-or-expression
26577 //
26578 // The required logical-or-expression must be a constant expression. Note
26579 // that we don't check that the expression is constepxr here. We defer until
26580 // we analyze constraints and then, we only check atomic constraints.
26581 static tree
26582 cp_parser_requires_clause (cp_parser *parser)
26583 {
26584 // Parse the requires clause so that it is not automatically folded.
26585 ++processing_template_decl;
26586 tree expr = cp_parser_binary_expression (parser, false, false,
26587 PREC_NOT_OPERATOR, NULL);
26588 if (check_for_bare_parameter_packs (expr))
26589 expr = error_mark_node;
26590 --processing_template_decl;
26591 return expr;
26592 }
26593
26594 // Optionally parse a requires clause:
26595 static tree
26596 cp_parser_requires_clause_opt (cp_parser *parser)
26597 {
26598 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26599 if (tok->keyword != RID_REQUIRES)
26600 {
26601 if (!flag_concepts && tok->type == CPP_NAME
26602 && tok->u.value == ridpointers[RID_REQUIRES])
26603 {
26604 error_at (cp_lexer_peek_token (parser->lexer)->location,
26605 "%<requires%> only available with %<-fconcepts%>");
26606 /* Parse and discard the requires-clause. */
26607 cp_lexer_consume_token (parser->lexer);
26608 cp_parser_requires_clause (parser);
26609 }
26610 return NULL_TREE;
26611 }
26612 cp_lexer_consume_token (parser->lexer);
26613 return cp_parser_requires_clause (parser);
26614 }
26615
26616
26617 /*---------------------------------------------------------------------------
26618 Requires expressions
26619 ---------------------------------------------------------------------------*/
26620
26621 /* Parse a requires expression
26622
26623 requirement-expression:
26624 'requires' requirement-parameter-list [opt] requirement-body */
26625 static tree
26626 cp_parser_requires_expression (cp_parser *parser)
26627 {
26628 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26629 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26630
26631 /* A requires-expression shall appear only within a concept
26632 definition or a requires-clause.
26633
26634 TODO: Implement this diagnostic correctly. */
26635 if (!processing_template_decl)
26636 {
26637 error_at (loc, "a requires expression cannot appear outside a template");
26638 cp_parser_skip_to_end_of_statement (parser);
26639 return error_mark_node;
26640 }
26641
26642 tree parms, reqs;
26643 {
26644 /* Local parameters are delared as variables within the scope
26645 of the expression. They are not visible past the end of
26646 the expression. Expressions within the requires-expression
26647 are unevaluated. */
26648 struct scope_sentinel
26649 {
26650 scope_sentinel ()
26651 {
26652 ++cp_unevaluated_operand;
26653 begin_scope (sk_block, NULL_TREE);
26654 }
26655
26656 ~scope_sentinel ()
26657 {
26658 pop_bindings_and_leave_scope ();
26659 --cp_unevaluated_operand;
26660 }
26661 } s;
26662
26663 /* Parse the optional parameter list. */
26664 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26665 {
26666 parms = cp_parser_requirement_parameter_list (parser);
26667 if (parms == error_mark_node)
26668 return error_mark_node;
26669 }
26670 else
26671 parms = NULL_TREE;
26672
26673 /* Parse the requirement body. */
26674 reqs = cp_parser_requirement_body (parser);
26675 if (reqs == error_mark_node)
26676 return error_mark_node;
26677 }
26678
26679 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26680 the parm chain. */
26681 grokparms (parms, &parms);
26682 return finish_requires_expr (parms, reqs);
26683 }
26684
26685 /* Parse a parameterized requirement.
26686
26687 requirement-parameter-list:
26688 '(' parameter-declaration-clause ')' */
26689 static tree
26690 cp_parser_requirement_parameter_list (cp_parser *parser)
26691 {
26692 matching_parens parens;
26693 if (!parens.require_open (parser))
26694 return error_mark_node;
26695
26696 tree parms
26697 = cp_parser_parameter_declaration_clause (parser, CP_PARSER_FLAGS_NONE);
26698
26699 if (!parens.require_close (parser))
26700 return error_mark_node;
26701
26702 return parms;
26703 }
26704
26705 /* Parse the body of a requirement.
26706
26707 requirement-body:
26708 '{' requirement-list '}' */
26709 static tree
26710 cp_parser_requirement_body (cp_parser *parser)
26711 {
26712 matching_braces braces;
26713 if (!braces.require_open (parser))
26714 return error_mark_node;
26715
26716 tree reqs = cp_parser_requirement_list (parser);
26717
26718 if (!braces.require_close (parser))
26719 return error_mark_node;
26720
26721 return reqs;
26722 }
26723
26724 /* Parse a list of requirements.
26725
26726 requirement-list:
26727 requirement
26728 requirement-list ';' requirement[opt] */
26729 static tree
26730 cp_parser_requirement_list (cp_parser *parser)
26731 {
26732 tree result = NULL_TREE;
26733 while (true)
26734 {
26735 tree req = cp_parser_requirement (parser);
26736 if (req == error_mark_node)
26737 return error_mark_node;
26738
26739 result = tree_cons (NULL_TREE, req, result);
26740
26741 /* If we see a semi-colon, consume it. */
26742 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26743 cp_lexer_consume_token (parser->lexer);
26744
26745 /* Stop processing at the end of the list. */
26746 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26747 break;
26748 }
26749
26750 /* Reverse the order of requirements so they are analyzed in
26751 declaration order. */
26752 return nreverse (result);
26753 }
26754
26755 /* Parse a syntactic requirement or type requirement.
26756
26757 requirement:
26758 simple-requirement
26759 compound-requirement
26760 type-requirement
26761 nested-requirement */
26762 static tree
26763 cp_parser_requirement (cp_parser *parser)
26764 {
26765 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26766 return cp_parser_compound_requirement (parser);
26767 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26768 return cp_parser_type_requirement (parser);
26769 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26770 return cp_parser_nested_requirement (parser);
26771 else
26772 return cp_parser_simple_requirement (parser);
26773 }
26774
26775 /* Parse a simple requirement.
26776
26777 simple-requirement:
26778 expression ';' */
26779 static tree
26780 cp_parser_simple_requirement (cp_parser *parser)
26781 {
26782 tree expr = cp_parser_expression (parser, NULL, false, false);
26783 if (!expr || expr == error_mark_node)
26784 return error_mark_node;
26785
26786 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26787 return error_mark_node;
26788
26789 return finish_simple_requirement (expr);
26790 }
26791
26792 /* Parse a type requirement
26793
26794 type-requirement
26795 nested-name-specifier [opt] required-type-name ';'
26796
26797 required-type-name:
26798 type-name
26799 'template' [opt] simple-template-id */
26800 static tree
26801 cp_parser_type_requirement (cp_parser *parser)
26802 {
26803 cp_lexer_consume_token (parser->lexer);
26804
26805 // Save the scope before parsing name specifiers.
26806 tree saved_scope = parser->scope;
26807 tree saved_object_scope = parser->object_scope;
26808 tree saved_qualifying_scope = parser->qualifying_scope;
26809 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26810 cp_parser_nested_name_specifier_opt (parser,
26811 /*typename_keyword_p=*/true,
26812 /*check_dependency_p=*/false,
26813 /*type_p=*/true,
26814 /*is_declaration=*/false);
26815
26816 tree type;
26817 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26818 {
26819 cp_lexer_consume_token (parser->lexer);
26820 type = cp_parser_template_id (parser,
26821 /*template_keyword_p=*/true,
26822 /*check_dependency=*/false,
26823 /*tag_type=*/none_type,
26824 /*is_declaration=*/false);
26825 type = make_typename_type (parser->scope, type, typename_type,
26826 /*complain=*/tf_error);
26827 }
26828 else
26829 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26830
26831 if (TREE_CODE (type) == TYPE_DECL)
26832 type = TREE_TYPE (type);
26833
26834 parser->scope = saved_scope;
26835 parser->object_scope = saved_object_scope;
26836 parser->qualifying_scope = saved_qualifying_scope;
26837
26838 if (type == error_mark_node)
26839 cp_parser_skip_to_end_of_statement (parser);
26840
26841 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26842 return error_mark_node;
26843 if (type == error_mark_node)
26844 return error_mark_node;
26845
26846 return finish_type_requirement (type);
26847 }
26848
26849 /* Parse a compound requirement
26850
26851 compound-requirement:
26852 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26853 static tree
26854 cp_parser_compound_requirement (cp_parser *parser)
26855 {
26856 /* Parse an expression enclosed in '{ }'s. */
26857 matching_braces braces;
26858 if (!braces.require_open (parser))
26859 return error_mark_node;
26860
26861 tree expr = cp_parser_expression (parser, NULL, false, false);
26862 if (!expr || expr == error_mark_node)
26863 return error_mark_node;
26864
26865 if (!braces.require_close (parser))
26866 return error_mark_node;
26867
26868 /* Parse the optional noexcept. */
26869 bool noexcept_p = false;
26870 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26871 {
26872 cp_lexer_consume_token (parser->lexer);
26873 noexcept_p = true;
26874 }
26875
26876 /* Parse the optional trailing return type. */
26877 tree type = NULL_TREE;
26878 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26879 {
26880 cp_lexer_consume_token (parser->lexer);
26881 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26882 parser->in_result_type_constraint_p = true;
26883 type = cp_parser_trailing_type_id (parser);
26884 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26885 if (type == error_mark_node)
26886 return error_mark_node;
26887 }
26888
26889 return finish_compound_requirement (expr, type, noexcept_p);
26890 }
26891
26892 /* Parse a nested requirement. This is the same as a requires clause.
26893
26894 nested-requirement:
26895 requires-clause */
26896 static tree
26897 cp_parser_nested_requirement (cp_parser *parser)
26898 {
26899 cp_lexer_consume_token (parser->lexer);
26900 tree req = cp_parser_requires_clause (parser);
26901 if (req == error_mark_node)
26902 return error_mark_node;
26903 return finish_nested_requirement (req);
26904 }
26905
26906 /* Support Functions */
26907
26908 /* Return the appropriate prefer_type argument for lookup_name_real based on
26909 tag_type and template_mem_access. */
26910
26911 static inline int
26912 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26913 {
26914 /* DR 141: When looking in the current enclosing context for a template-name
26915 after -> or ., only consider class templates. */
26916 if (template_mem_access)
26917 return 2;
26918 switch (tag_type)
26919 {
26920 case none_type: return 0; // No preference.
26921 case scope_type: return 1; // Type or namespace.
26922 default: return 2; // Type only.
26923 }
26924 }
26925
26926 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26927 NAME should have one of the representations used for an
26928 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26929 is returned. If PARSER->SCOPE is a dependent type, then a
26930 SCOPE_REF is returned.
26931
26932 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26933 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26934 was formed. Abstractly, such entities should not be passed to this
26935 function, because they do not need to be looked up, but it is
26936 simpler to check for this special case here, rather than at the
26937 call-sites.
26938
26939 In cases not explicitly covered above, this function returns a
26940 DECL, OVERLOAD, or baselink representing the result of the lookup.
26941 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26942 is returned.
26943
26944 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26945 (e.g., "struct") that was used. In that case bindings that do not
26946 refer to types are ignored.
26947
26948 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26949 ignored.
26950
26951 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26952 are ignored.
26953
26954 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26955 types.
26956
26957 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26958 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26959 NULL_TREE otherwise. */
26960
26961 static cp_expr
26962 cp_parser_lookup_name (cp_parser *parser, tree name,
26963 enum tag_types tag_type,
26964 bool is_template,
26965 bool is_namespace,
26966 bool check_dependency,
26967 tree *ambiguous_decls,
26968 location_t name_location)
26969 {
26970 tree decl;
26971 tree object_type = parser->context->object_type;
26972
26973 /* Assume that the lookup will be unambiguous. */
26974 if (ambiguous_decls)
26975 *ambiguous_decls = NULL_TREE;
26976
26977 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26978 no longer valid. Note that if we are parsing tentatively, and
26979 the parse fails, OBJECT_TYPE will be automatically restored. */
26980 parser->context->object_type = NULL_TREE;
26981
26982 if (name == error_mark_node)
26983 return error_mark_node;
26984
26985 /* A template-id has already been resolved; there is no lookup to
26986 do. */
26987 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26988 return name;
26989 if (BASELINK_P (name))
26990 {
26991 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26992 == TEMPLATE_ID_EXPR);
26993 return name;
26994 }
26995
26996 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26997 it should already have been checked to make sure that the name
26998 used matches the type being destroyed. */
26999 if (TREE_CODE (name) == BIT_NOT_EXPR)
27000 {
27001 tree type;
27002
27003 /* Figure out to which type this destructor applies. */
27004 if (parser->scope)
27005 type = parser->scope;
27006 else if (object_type)
27007 type = object_type;
27008 else
27009 type = current_class_type;
27010 /* If that's not a class type, there is no destructor. */
27011 if (!type || !CLASS_TYPE_P (type))
27012 return error_mark_node;
27013
27014 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
27015 lazily_declare_fn (sfk_destructor, type);
27016
27017 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
27018 return dtor;
27019
27020 return error_mark_node;
27021 }
27022
27023 /* By this point, the NAME should be an ordinary identifier. If
27024 the id-expression was a qualified name, the qualifying scope is
27025 stored in PARSER->SCOPE at this point. */
27026 gcc_assert (identifier_p (name));
27027
27028 /* Perform the lookup. */
27029 if (parser->scope)
27030 {
27031 bool dependent_p;
27032
27033 if (parser->scope == error_mark_node)
27034 return error_mark_node;
27035
27036 /* If the SCOPE is dependent, the lookup must be deferred until
27037 the template is instantiated -- unless we are explicitly
27038 looking up names in uninstantiated templates. Even then, we
27039 cannot look up the name if the scope is not a class type; it
27040 might, for example, be a template type parameter. */
27041 dependent_p = (TYPE_P (parser->scope)
27042 && dependent_scope_p (parser->scope));
27043 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
27044 && dependent_p)
27045 /* Defer lookup. */
27046 decl = error_mark_node;
27047 else
27048 {
27049 tree pushed_scope = NULL_TREE;
27050
27051 /* If PARSER->SCOPE is a dependent type, then it must be a
27052 class type, and we must not be checking dependencies;
27053 otherwise, we would have processed this lookup above. So
27054 that PARSER->SCOPE is not considered a dependent base by
27055 lookup_member, we must enter the scope here. */
27056 if (dependent_p)
27057 pushed_scope = push_scope (parser->scope);
27058
27059 /* If the PARSER->SCOPE is a template specialization, it
27060 may be instantiated during name lookup. In that case,
27061 errors may be issued. Even if we rollback the current
27062 tentative parse, those errors are valid. */
27063 decl = lookup_qualified_name (parser->scope, name,
27064 prefer_type_arg (tag_type),
27065 /*complain=*/true);
27066
27067 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
27068 lookup result and the nested-name-specifier nominates a class C:
27069 * if the name specified after the nested-name-specifier, when
27070 looked up in C, is the injected-class-name of C (Clause 9), or
27071 * if the name specified after the nested-name-specifier is the
27072 same as the identifier or the simple-template-id's template-
27073 name in the last component of the nested-name-specifier,
27074 the name is instead considered to name the constructor of
27075 class C. [ Note: for example, the constructor is not an
27076 acceptable lookup result in an elaborated-type-specifier so
27077 the constructor would not be used in place of the
27078 injected-class-name. --end note ] Such a constructor name
27079 shall be used only in the declarator-id of a declaration that
27080 names a constructor or in a using-declaration. */
27081 if (tag_type == none_type
27082 && DECL_SELF_REFERENCE_P (decl)
27083 && same_type_p (DECL_CONTEXT (decl), parser->scope))
27084 decl = lookup_qualified_name (parser->scope, ctor_identifier,
27085 prefer_type_arg (tag_type),
27086 /*complain=*/true);
27087
27088 /* If we have a single function from a using decl, pull it out. */
27089 if (TREE_CODE (decl) == OVERLOAD
27090 && !really_overloaded_fn (decl))
27091 decl = OVL_FUNCTION (decl);
27092
27093 if (pushed_scope)
27094 pop_scope (pushed_scope);
27095 }
27096
27097 /* If the scope is a dependent type and either we deferred lookup or
27098 we did lookup but didn't find the name, rememeber the name. */
27099 if (decl == error_mark_node && TYPE_P (parser->scope)
27100 && dependent_type_p (parser->scope))
27101 {
27102 if (tag_type)
27103 {
27104 tree type;
27105
27106 /* The resolution to Core Issue 180 says that `struct
27107 A::B' should be considered a type-name, even if `A'
27108 is dependent. */
27109 type = make_typename_type (parser->scope, name, tag_type,
27110 /*complain=*/tf_error);
27111 if (type != error_mark_node)
27112 decl = TYPE_NAME (type);
27113 }
27114 else if (is_template
27115 && (cp_parser_next_token_ends_template_argument_p (parser)
27116 || cp_lexer_next_token_is (parser->lexer,
27117 CPP_CLOSE_PAREN)))
27118 decl = make_unbound_class_template (parser->scope,
27119 name, NULL_TREE,
27120 /*complain=*/tf_error);
27121 else
27122 decl = build_qualified_name (/*type=*/NULL_TREE,
27123 parser->scope, name,
27124 is_template);
27125 }
27126 parser->qualifying_scope = parser->scope;
27127 parser->object_scope = NULL_TREE;
27128 }
27129 else if (object_type)
27130 {
27131 /* Look up the name in the scope of the OBJECT_TYPE, unless the
27132 OBJECT_TYPE is not a class. */
27133 if (CLASS_TYPE_P (object_type))
27134 /* If the OBJECT_TYPE is a template specialization, it may
27135 be instantiated during name lookup. In that case, errors
27136 may be issued. Even if we rollback the current tentative
27137 parse, those errors are valid. */
27138 decl = lookup_member (object_type,
27139 name,
27140 /*protect=*/0,
27141 prefer_type_arg (tag_type),
27142 tf_warning_or_error);
27143 else
27144 decl = NULL_TREE;
27145
27146 if (!decl)
27147 /* Look it up in the enclosing context. DR 141: When looking for a
27148 template-name after -> or ., only consider class templates. */
27149 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
27150 /*nonclass=*/0,
27151 /*block_p=*/true, is_namespace, 0);
27152 if (object_type == unknown_type_node)
27153 /* The object is type-dependent, so we can't look anything up; we used
27154 this to get the DR 141 behavior. */
27155 object_type = NULL_TREE;
27156 parser->object_scope = object_type;
27157 parser->qualifying_scope = NULL_TREE;
27158 }
27159 else
27160 {
27161 decl = lookup_name_real (name, prefer_type_arg (tag_type),
27162 /*nonclass=*/0,
27163 /*block_p=*/true, is_namespace, 0);
27164 parser->qualifying_scope = NULL_TREE;
27165 parser->object_scope = NULL_TREE;
27166 }
27167
27168 /* If the lookup failed, let our caller know. */
27169 if (!decl || decl == error_mark_node)
27170 return error_mark_node;
27171
27172 /* Pull out the template from an injected-class-name (or multiple). */
27173 if (is_template)
27174 decl = maybe_get_template_decl_from_type_decl (decl);
27175
27176 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
27177 if (TREE_CODE (decl) == TREE_LIST)
27178 {
27179 if (ambiguous_decls)
27180 *ambiguous_decls = decl;
27181 /* The error message we have to print is too complicated for
27182 cp_parser_error, so we incorporate its actions directly. */
27183 if (!cp_parser_simulate_error (parser))
27184 {
27185 error_at (name_location, "reference to %qD is ambiguous",
27186 name);
27187 print_candidates (decl);
27188 }
27189 return error_mark_node;
27190 }
27191
27192 gcc_assert (DECL_P (decl)
27193 || TREE_CODE (decl) == OVERLOAD
27194 || TREE_CODE (decl) == SCOPE_REF
27195 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
27196 || BASELINK_P (decl));
27197
27198 /* If we have resolved the name of a member declaration, check to
27199 see if the declaration is accessible. When the name resolves to
27200 set of overloaded functions, accessibility is checked when
27201 overload resolution is done.
27202
27203 During an explicit instantiation, access is not checked at all,
27204 as per [temp.explicit]. */
27205 if (DECL_P (decl))
27206 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
27207
27208 maybe_record_typedef_use (decl);
27209
27210 return cp_expr (decl, name_location);
27211 }
27212
27213 /* Like cp_parser_lookup_name, but for use in the typical case where
27214 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27215 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
27216
27217 static tree
27218 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
27219 {
27220 return cp_parser_lookup_name (parser, name,
27221 none_type,
27222 /*is_template=*/false,
27223 /*is_namespace=*/false,
27224 /*check_dependency=*/true,
27225 /*ambiguous_decls=*/NULL,
27226 location);
27227 }
27228
27229 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27230 the current context, return the TYPE_DECL. If TAG_NAME_P is
27231 true, the DECL indicates the class being defined in a class-head,
27232 or declared in an elaborated-type-specifier.
27233
27234 Otherwise, return DECL. */
27235
27236 static tree
27237 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
27238 {
27239 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27240 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27241
27242 struct A {
27243 template <typename T> struct B;
27244 };
27245
27246 template <typename T> struct A::B {};
27247
27248 Similarly, in an elaborated-type-specifier:
27249
27250 namespace N { struct X{}; }
27251
27252 struct A {
27253 template <typename T> friend struct N::X;
27254 };
27255
27256 However, if the DECL refers to a class type, and we are in
27257 the scope of the class, then the name lookup automatically
27258 finds the TYPE_DECL created by build_self_reference rather
27259 than a TEMPLATE_DECL. For example, in:
27260
27261 template <class T> struct S {
27262 S s;
27263 };
27264
27265 there is no need to handle such case. */
27266
27267 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
27268 return DECL_TEMPLATE_RESULT (decl);
27269
27270 return decl;
27271 }
27272
27273 /* If too many, or too few, template-parameter lists apply to the
27274 declarator, issue an error message. Returns TRUE if all went well,
27275 and FALSE otherwise. */
27276
27277 static bool
27278 cp_parser_check_declarator_template_parameters (cp_parser* parser,
27279 cp_declarator *declarator,
27280 location_t declarator_location)
27281 {
27282 switch (declarator->kind)
27283 {
27284 case cdk_id:
27285 {
27286 unsigned num_templates = 0;
27287 tree scope = declarator->u.id.qualifying_scope;
27288 bool template_id_p = false;
27289
27290 if (scope)
27291 num_templates = num_template_headers_for_class (scope);
27292 else if (TREE_CODE (declarator->u.id.unqualified_name)
27293 == TEMPLATE_ID_EXPR)
27294 {
27295 /* If the DECLARATOR has the form `X<y>' then it uses one
27296 additional level of template parameters. */
27297 ++num_templates;
27298 template_id_p = true;
27299 }
27300
27301 return cp_parser_check_template_parameters
27302 (parser, num_templates, template_id_p, declarator_location,
27303 declarator);
27304 }
27305
27306 case cdk_function:
27307 case cdk_array:
27308 case cdk_pointer:
27309 case cdk_reference:
27310 case cdk_ptrmem:
27311 return (cp_parser_check_declarator_template_parameters
27312 (parser, declarator->declarator, declarator_location));
27313
27314 case cdk_decomp:
27315 case cdk_error:
27316 return true;
27317
27318 default:
27319 gcc_unreachable ();
27320 }
27321 return false;
27322 }
27323
27324 /* NUM_TEMPLATES were used in the current declaration. If that is
27325 invalid, return FALSE and issue an error messages. Otherwise,
27326 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27327 declarator and we can print more accurate diagnostics. */
27328
27329 static bool
27330 cp_parser_check_template_parameters (cp_parser* parser,
27331 unsigned num_templates,
27332 bool template_id_p,
27333 location_t location,
27334 cp_declarator *declarator)
27335 {
27336 /* If there are the same number of template classes and parameter
27337 lists, that's OK. */
27338 if (parser->num_template_parameter_lists == num_templates)
27339 return true;
27340 /* If there are more, but only one more, and the name ends in an identifier,
27341 then we are declaring a primary template. That's OK too. */
27342 if (!template_id_p
27343 && parser->num_template_parameter_lists == num_templates + 1)
27344 return true;
27345 /* If there are more template classes than parameter lists, we have
27346 something like:
27347
27348 template <class T> void S<T>::R<T>::f (); */
27349 if (parser->num_template_parameter_lists < num_templates)
27350 {
27351 if (declarator && !current_function_decl)
27352 error_at (location, "specializing member %<%T::%E%> "
27353 "requires %<template<>%> syntax",
27354 declarator->u.id.qualifying_scope,
27355 declarator->u.id.unqualified_name);
27356 else if (declarator)
27357 error_at (location, "invalid declaration of %<%T::%E%>",
27358 declarator->u.id.qualifying_scope,
27359 declarator->u.id.unqualified_name);
27360 else
27361 error_at (location, "too few template-parameter-lists");
27362 return false;
27363 }
27364 /* Otherwise, there are too many template parameter lists. We have
27365 something like:
27366
27367 template <class T> template <class U> void S::f(); */
27368 error_at (location, "too many template-parameter-lists");
27369 return false;
27370 }
27371
27372 /* Parse an optional `::' token indicating that the following name is
27373 from the global namespace. If so, PARSER->SCOPE is set to the
27374 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27375 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27376 Returns the new value of PARSER->SCOPE, if the `::' token is
27377 present, and NULL_TREE otherwise. */
27378
27379 static tree
27380 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
27381 {
27382 cp_token *token;
27383
27384 /* Peek at the next token. */
27385 token = cp_lexer_peek_token (parser->lexer);
27386 /* If we're looking at a `::' token then we're starting from the
27387 global namespace, not our current location. */
27388 if (token->type == CPP_SCOPE)
27389 {
27390 /* Consume the `::' token. */
27391 cp_lexer_consume_token (parser->lexer);
27392 /* Set the SCOPE so that we know where to start the lookup. */
27393 parser->scope = global_namespace;
27394 parser->qualifying_scope = global_namespace;
27395 parser->object_scope = NULL_TREE;
27396
27397 return parser->scope;
27398 }
27399 else if (!current_scope_valid_p)
27400 {
27401 parser->scope = NULL_TREE;
27402 parser->qualifying_scope = NULL_TREE;
27403 parser->object_scope = NULL_TREE;
27404 }
27405
27406 return NULL_TREE;
27407 }
27408
27409 /* Returns TRUE if the upcoming token sequence is the start of a
27410 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27411 declarator is preceded by the `friend' specifier. The parser flags FLAGS
27412 is used to control type-specifier parsing. */
27413
27414 static bool
27415 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
27416 bool friend_p)
27417 {
27418 bool constructor_p;
27419 bool outside_class_specifier_p;
27420 tree nested_name_specifier;
27421 cp_token *next_token;
27422
27423 /* The common case is that this is not a constructor declarator, so
27424 try to avoid doing lots of work if at all possible. It's not
27425 valid declare a constructor at function scope. */
27426 if (parser->in_function_body)
27427 return false;
27428 /* And only certain tokens can begin a constructor declarator. */
27429 next_token = cp_lexer_peek_token (parser->lexer);
27430 if (next_token->type != CPP_NAME
27431 && next_token->type != CPP_SCOPE
27432 && next_token->type != CPP_NESTED_NAME_SPECIFIER
27433 && next_token->type != CPP_TEMPLATE_ID)
27434 return false;
27435
27436 /* Parse tentatively; we are going to roll back all of the tokens
27437 consumed here. */
27438 cp_parser_parse_tentatively (parser);
27439 /* Assume that we are looking at a constructor declarator. */
27440 constructor_p = true;
27441
27442 /* Look for the optional `::' operator. */
27443 cp_parser_global_scope_opt (parser,
27444 /*current_scope_valid_p=*/false);
27445 /* Look for the nested-name-specifier. */
27446 nested_name_specifier
27447 = (cp_parser_nested_name_specifier_opt (parser,
27448 /*typename_keyword_p=*/false,
27449 /*check_dependency_p=*/false,
27450 /*type_p=*/false,
27451 /*is_declaration=*/false));
27452
27453 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27454 if (nested_name_specifier
27455 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
27456 {
27457 tree s = resolve_typename_type (nested_name_specifier,
27458 /*only_current_p=*/false);
27459 if (TREE_CODE (s) != TYPENAME_TYPE)
27460 nested_name_specifier = s;
27461 }
27462
27463 outside_class_specifier_p = (!at_class_scope_p ()
27464 || !TYPE_BEING_DEFINED (current_class_type)
27465 || friend_p);
27466
27467 /* Outside of a class-specifier, there must be a
27468 nested-name-specifier. Except in C++17 mode, where we
27469 might be declaring a guiding declaration. */
27470 if (!nested_name_specifier && outside_class_specifier_p
27471 && cxx_dialect < cxx17)
27472 constructor_p = false;
27473 else if (nested_name_specifier == error_mark_node)
27474 constructor_p = false;
27475
27476 /* If we have a class scope, this is easy; DR 147 says that S::S always
27477 names the constructor, and no other qualified name could. */
27478 if (constructor_p && nested_name_specifier
27479 && CLASS_TYPE_P (nested_name_specifier))
27480 {
27481 tree id = cp_parser_unqualified_id (parser,
27482 /*template_keyword_p=*/false,
27483 /*check_dependency_p=*/false,
27484 /*declarator_p=*/true,
27485 /*optional_p=*/false);
27486 if (is_overloaded_fn (id))
27487 id = DECL_NAME (get_first_fn (id));
27488 if (!constructor_name_p (id, nested_name_specifier))
27489 constructor_p = false;
27490 }
27491 /* If we still think that this might be a constructor-declarator,
27492 look for a class-name. */
27493 else if (constructor_p)
27494 {
27495 /* If we have:
27496
27497 template <typename T> struct S {
27498 S();
27499 };
27500
27501 we must recognize that the nested `S' names a class. */
27502 if (cxx_dialect >= cxx17)
27503 cp_parser_parse_tentatively (parser);
27504
27505 tree type_decl;
27506 type_decl = cp_parser_class_name (parser,
27507 /*typename_keyword_p=*/false,
27508 /*template_keyword_p=*/false,
27509 none_type,
27510 /*check_dependency_p=*/false,
27511 /*class_head_p=*/false,
27512 /*is_declaration=*/false);
27513
27514 if (cxx_dialect >= cxx17
27515 && !cp_parser_parse_definitely (parser))
27516 {
27517 type_decl = NULL_TREE;
27518 tree tmpl = cp_parser_template_name (parser,
27519 /*template_keyword*/false,
27520 /*check_dependency_p*/false,
27521 /*is_declaration*/false,
27522 none_type,
27523 /*is_identifier*/NULL);
27524 if (DECL_CLASS_TEMPLATE_P (tmpl)
27525 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27526 /* It's a deduction guide, return true. */;
27527 else
27528 cp_parser_simulate_error (parser);
27529 }
27530
27531 /* If there was no class-name, then this is not a constructor.
27532 Otherwise, if we are in a class-specifier and we aren't
27533 handling a friend declaration, check that its type matches
27534 current_class_type (c++/38313). Note: error_mark_node
27535 is left alone for error recovery purposes. */
27536 constructor_p = (!cp_parser_error_occurred (parser)
27537 && (outside_class_specifier_p
27538 || type_decl == NULL_TREE
27539 || type_decl == error_mark_node
27540 || same_type_p (current_class_type,
27541 TREE_TYPE (type_decl))));
27542
27543 /* If we're still considering a constructor, we have to see a `(',
27544 to begin the parameter-declaration-clause, followed by either a
27545 `)', an `...', or a decl-specifier. We need to check for a
27546 type-specifier to avoid being fooled into thinking that:
27547
27548 S (f) (int);
27549
27550 is a constructor. (It is actually a function named `f' that
27551 takes one parameter (of type `int') and returns a value of type
27552 `S'. */
27553 if (constructor_p
27554 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27555 constructor_p = false;
27556
27557 if (constructor_p
27558 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
27559 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
27560 /* A parameter declaration begins with a decl-specifier,
27561 which is either the "attribute" keyword, a storage class
27562 specifier, or (usually) a type-specifier. */
27563 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
27564 {
27565 tree type;
27566 tree pushed_scope = NULL_TREE;
27567 unsigned saved_num_template_parameter_lists;
27568
27569 /* Names appearing in the type-specifier should be looked up
27570 in the scope of the class. */
27571 if (current_class_type)
27572 type = NULL_TREE;
27573 else if (type_decl)
27574 {
27575 type = TREE_TYPE (type_decl);
27576 if (TREE_CODE (type) == TYPENAME_TYPE)
27577 {
27578 type = resolve_typename_type (type,
27579 /*only_current_p=*/false);
27580 if (TREE_CODE (type) == TYPENAME_TYPE)
27581 {
27582 cp_parser_abort_tentative_parse (parser);
27583 return false;
27584 }
27585 }
27586 pushed_scope = push_scope (type);
27587 }
27588
27589 /* Inside the constructor parameter list, surrounding
27590 template-parameter-lists do not apply. */
27591 saved_num_template_parameter_lists
27592 = parser->num_template_parameter_lists;
27593 parser->num_template_parameter_lists = 0;
27594
27595 /* Look for the type-specifier. It's not optional, but its typename
27596 might be. */
27597 cp_parser_type_specifier (parser,
27598 (flags & ~CP_PARSER_FLAGS_OPTIONAL),
27599 /*decl_specs=*/NULL,
27600 /*is_declarator=*/true,
27601 /*declares_class_or_enum=*/NULL,
27602 /*is_cv_qualifier=*/NULL);
27603
27604 parser->num_template_parameter_lists
27605 = saved_num_template_parameter_lists;
27606
27607 /* Leave the scope of the class. */
27608 if (pushed_scope)
27609 pop_scope (pushed_scope);
27610
27611 constructor_p = !cp_parser_error_occurred (parser);
27612 }
27613 }
27614
27615 /* We did not really want to consume any tokens. */
27616 cp_parser_abort_tentative_parse (parser);
27617
27618 return constructor_p;
27619 }
27620
27621 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27622 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27623 they must be performed once we are in the scope of the function.
27624
27625 Returns the function defined. */
27626
27627 static tree
27628 cp_parser_function_definition_from_specifiers_and_declarator
27629 (cp_parser* parser,
27630 cp_decl_specifier_seq *decl_specifiers,
27631 tree attributes,
27632 const cp_declarator *declarator)
27633 {
27634 tree fn;
27635 bool success_p;
27636
27637 /* Begin the function-definition. */
27638 success_p = start_function (decl_specifiers, declarator, attributes);
27639
27640 /* The things we're about to see are not directly qualified by any
27641 template headers we've seen thus far. */
27642 reset_specialization ();
27643
27644 /* If there were names looked up in the decl-specifier-seq that we
27645 did not check, check them now. We must wait until we are in the
27646 scope of the function to perform the checks, since the function
27647 might be a friend. */
27648 perform_deferred_access_checks (tf_warning_or_error);
27649
27650 if (success_p)
27651 {
27652 cp_finalize_omp_declare_simd (parser, current_function_decl);
27653 parser->omp_declare_simd = NULL;
27654 cp_finalize_oacc_routine (parser, current_function_decl, true);
27655 parser->oacc_routine = NULL;
27656 }
27657
27658 if (!success_p)
27659 {
27660 /* Skip the entire function. */
27661 cp_parser_skip_to_end_of_block_or_statement (parser);
27662 fn = error_mark_node;
27663 }
27664 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27665 {
27666 /* Seen already, skip it. An error message has already been output. */
27667 cp_parser_skip_to_end_of_block_or_statement (parser);
27668 fn = current_function_decl;
27669 current_function_decl = NULL_TREE;
27670 /* If this is a function from a class, pop the nested class. */
27671 if (current_class_name)
27672 pop_nested_class ();
27673 }
27674 else
27675 {
27676 timevar_id_t tv;
27677 if (DECL_DECLARED_INLINE_P (current_function_decl))
27678 tv = TV_PARSE_INLINE;
27679 else
27680 tv = TV_PARSE_FUNC;
27681 timevar_push (tv);
27682 fn = cp_parser_function_definition_after_declarator (parser,
27683 /*inline_p=*/false);
27684 timevar_pop (tv);
27685 }
27686
27687 return fn;
27688 }
27689
27690 /* Parse the part of a function-definition that follows the
27691 declarator. INLINE_P is TRUE iff this function is an inline
27692 function defined within a class-specifier.
27693
27694 Returns the function defined. */
27695
27696 static tree
27697 cp_parser_function_definition_after_declarator (cp_parser* parser,
27698 bool inline_p)
27699 {
27700 tree fn;
27701 bool saved_in_unbraced_linkage_specification_p;
27702 bool saved_in_function_body;
27703 unsigned saved_num_template_parameter_lists;
27704 cp_token *token;
27705 bool fully_implicit_function_template_p
27706 = parser->fully_implicit_function_template_p;
27707 parser->fully_implicit_function_template_p = false;
27708 tree implicit_template_parms
27709 = parser->implicit_template_parms;
27710 parser->implicit_template_parms = 0;
27711 cp_binding_level* implicit_template_scope
27712 = parser->implicit_template_scope;
27713 parser->implicit_template_scope = 0;
27714
27715 saved_in_function_body = parser->in_function_body;
27716 parser->in_function_body = true;
27717 /* If the next token is `return', then the code may be trying to
27718 make use of the "named return value" extension that G++ used to
27719 support. */
27720 token = cp_lexer_peek_token (parser->lexer);
27721 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27722 {
27723 /* Consume the `return' keyword. */
27724 cp_lexer_consume_token (parser->lexer);
27725 /* Look for the identifier that indicates what value is to be
27726 returned. */
27727 cp_parser_identifier (parser);
27728 /* Issue an error message. */
27729 error_at (token->location,
27730 "named return values are no longer supported");
27731 /* Skip tokens until we reach the start of the function body. */
27732 while (true)
27733 {
27734 cp_token *token = cp_lexer_peek_token (parser->lexer);
27735 if (token->type == CPP_OPEN_BRACE
27736 || token->type == CPP_EOF
27737 || token->type == CPP_PRAGMA_EOL)
27738 break;
27739 cp_lexer_consume_token (parser->lexer);
27740 }
27741 }
27742 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27743 anything declared inside `f'. */
27744 saved_in_unbraced_linkage_specification_p
27745 = parser->in_unbraced_linkage_specification_p;
27746 parser->in_unbraced_linkage_specification_p = false;
27747 /* Inside the function, surrounding template-parameter-lists do not
27748 apply. */
27749 saved_num_template_parameter_lists
27750 = parser->num_template_parameter_lists;
27751 parser->num_template_parameter_lists = 0;
27752
27753 /* If the next token is `try', `__transaction_atomic', or
27754 `__transaction_relaxed`, then we are looking at either function-try-block
27755 or function-transaction-block. Note that all of these include the
27756 function-body. */
27757 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27758 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27759 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27760 RID_TRANSACTION_RELAXED))
27761 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27762 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27763 cp_parser_function_try_block (parser);
27764 else
27765 cp_parser_ctor_initializer_opt_and_function_body
27766 (parser, /*in_function_try_block=*/false);
27767
27768 /* Finish the function. */
27769 fn = finish_function (inline_p);
27770 /* Generate code for it, if necessary. */
27771 expand_or_defer_fn (fn);
27772 /* Restore the saved values. */
27773 parser->in_unbraced_linkage_specification_p
27774 = saved_in_unbraced_linkage_specification_p;
27775 parser->num_template_parameter_lists
27776 = saved_num_template_parameter_lists;
27777 parser->in_function_body = saved_in_function_body;
27778
27779 parser->fully_implicit_function_template_p
27780 = fully_implicit_function_template_p;
27781 parser->implicit_template_parms
27782 = implicit_template_parms;
27783 parser->implicit_template_scope
27784 = implicit_template_scope;
27785
27786 if (parser->fully_implicit_function_template_p)
27787 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27788
27789 return fn;
27790 }
27791
27792 /* Parse a template-declaration body (following argument list). */
27793
27794 static void
27795 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27796 tree parameter_list,
27797 bool member_p)
27798 {
27799 tree decl = NULL_TREE;
27800 bool friend_p = false;
27801
27802 /* We just processed one more parameter list. */
27803 ++parser->num_template_parameter_lists;
27804
27805 /* Get the deferred access checks from the parameter list. These
27806 will be checked once we know what is being declared, as for a
27807 member template the checks must be performed in the scope of the
27808 class containing the member. */
27809 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27810
27811 /* Tentatively parse for a new template parameter list, which can either be
27812 the template keyword or a template introduction. */
27813 if (cp_parser_template_declaration_after_export (parser, member_p))
27814 /* OK */;
27815 else if (cxx_dialect >= cxx11
27816 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27817 decl = cp_parser_alias_declaration (parser);
27818 else
27819 {
27820 /* There are no access checks when parsing a template, as we do not
27821 know if a specialization will be a friend. */
27822 push_deferring_access_checks (dk_no_check);
27823 cp_token *token = cp_lexer_peek_token (parser->lexer);
27824 decl = cp_parser_single_declaration (parser,
27825 checks,
27826 member_p,
27827 /*explicit_specialization_p=*/false,
27828 &friend_p);
27829 pop_deferring_access_checks ();
27830
27831 /* If this is a member template declaration, let the front
27832 end know. */
27833 if (member_p && !friend_p && decl)
27834 {
27835 if (TREE_CODE (decl) == TYPE_DECL)
27836 cp_parser_check_access_in_redeclaration (decl, token->location);
27837
27838 decl = finish_member_template_decl (decl);
27839 }
27840 else if (friend_p && decl
27841 && DECL_DECLARES_TYPE_P (decl))
27842 make_friend_class (current_class_type, TREE_TYPE (decl),
27843 /*complain=*/true);
27844 }
27845 /* We are done with the current parameter list. */
27846 --parser->num_template_parameter_lists;
27847
27848 pop_deferring_access_checks ();
27849
27850 /* Finish up. */
27851 finish_template_decl (parameter_list);
27852
27853 /* Check the template arguments for a literal operator template. */
27854 if (decl
27855 && DECL_DECLARES_FUNCTION_P (decl)
27856 && UDLIT_OPER_P (DECL_NAME (decl)))
27857 {
27858 bool ok = true;
27859 if (parameter_list == NULL_TREE)
27860 ok = false;
27861 else
27862 {
27863 int num_parms = TREE_VEC_LENGTH (parameter_list);
27864 if (num_parms == 1)
27865 {
27866 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27867 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27868 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27869 /* OK, C++20 string literal operator template. We don't need
27870 to warn in lower dialects here because we will have already
27871 warned about the template parameter. */;
27872 else if (TREE_TYPE (parm) != char_type_node
27873 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27874 ok = false;
27875 }
27876 else if (num_parms == 2 && cxx_dialect >= cxx14)
27877 {
27878 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27879 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27880 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27881 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27882 if (parm == error_mark_node
27883 || TREE_TYPE (parm) != TREE_TYPE (type)
27884 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27885 ok = false;
27886 else
27887 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27888 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27889 "ISO C++ did not adopt string literal operator templa"
27890 "tes taking an argument pack of characters");
27891 }
27892 else
27893 ok = false;
27894 }
27895 if (!ok)
27896 {
27897 if (cxx_dialect > cxx17)
27898 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
27899 "template %qD has invalid parameter list; expected "
27900 "non-type template parameter pack %<<char...>%> or "
27901 "single non-type parameter of class type",
27902 decl);
27903 else
27904 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
27905 "template %qD has invalid parameter list; expected "
27906 "non-type template parameter pack %<<char...>%>",
27907 decl);
27908 }
27909 }
27910
27911 /* Register member declarations. */
27912 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27913 finish_member_declaration (decl);
27914 /* If DECL is a function template, we must return to parse it later.
27915 (Even though there is no definition, there might be default
27916 arguments that need handling.) */
27917 if (member_p && decl
27918 && DECL_DECLARES_FUNCTION_P (decl))
27919 vec_safe_push (unparsed_funs_with_definitions, decl);
27920 }
27921
27922 /* Parse a template introduction header for a template-declaration. Returns
27923 false if tentative parse fails. */
27924
27925 static bool
27926 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27927 {
27928 cp_parser_parse_tentatively (parser);
27929
27930 tree saved_scope = parser->scope;
27931 tree saved_object_scope = parser->object_scope;
27932 tree saved_qualifying_scope = parser->qualifying_scope;
27933
27934 /* Look for the optional `::' operator. */
27935 cp_parser_global_scope_opt (parser,
27936 /*current_scope_valid_p=*/false);
27937 /* Look for the nested-name-specifier. */
27938 cp_parser_nested_name_specifier_opt (parser,
27939 /*typename_keyword_p=*/false,
27940 /*check_dependency_p=*/true,
27941 /*type_p=*/false,
27942 /*is_declaration=*/false);
27943
27944 cp_token *token = cp_lexer_peek_token (parser->lexer);
27945 tree concept_name = cp_parser_identifier (parser);
27946
27947 /* Look up the concept for which we will be matching
27948 template parameters. */
27949 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27950 token->location);
27951 parser->scope = saved_scope;
27952 parser->object_scope = saved_object_scope;
27953 parser->qualifying_scope = saved_qualifying_scope;
27954
27955 if (concept_name == error_mark_node)
27956 cp_parser_simulate_error (parser);
27957
27958 /* Look for opening brace for introduction. */
27959 matching_braces braces;
27960 braces.require_open (parser);
27961
27962 if (!cp_parser_parse_definitely (parser))
27963 return false;
27964
27965 push_deferring_access_checks (dk_deferred);
27966
27967 /* Build vector of placeholder parameters and grab
27968 matching identifiers. */
27969 tree introduction_list = cp_parser_introduction_list (parser);
27970
27971 /* Look for closing brace for introduction. */
27972 if (!braces.require_close (parser))
27973 return true;
27974
27975 /* The introduction-list shall not be empty. */
27976 int nargs = TREE_VEC_LENGTH (introduction_list);
27977 if (nargs == 0)
27978 {
27979 /* In cp_parser_introduction_list we have already issued an error. */
27980 return true;
27981 }
27982
27983 if (tmpl_decl == error_mark_node)
27984 {
27985 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27986 token->location);
27987 return true;
27988 }
27989
27990 /* Build and associate the constraint. */
27991 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27992 if (parms && parms != error_mark_node)
27993 {
27994 cp_parser_template_declaration_after_parameters (parser, parms,
27995 member_p);
27996 return true;
27997 }
27998
27999 error_at (token->location, "no matching concept for template-introduction");
28000 return true;
28001 }
28002
28003 /* Parse a normal template-declaration following the template keyword. */
28004
28005 static void
28006 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
28007 {
28008 tree parameter_list;
28009 bool need_lang_pop;
28010 location_t location = input_location;
28011
28012 /* Look for the `<' token. */
28013 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
28014 return;
28015 if (at_class_scope_p () && current_function_decl)
28016 {
28017 /* 14.5.2.2 [temp.mem]
28018
28019 A local class shall not have member templates. */
28020 error_at (location,
28021 "invalid declaration of member template in local class");
28022 cp_parser_skip_to_end_of_block_or_statement (parser);
28023 return;
28024 }
28025 /* [temp]
28026
28027 A template ... shall not have C linkage. */
28028 if (current_lang_name == lang_name_c)
28029 {
28030 error_at (location, "template with C linkage");
28031 maybe_show_extern_c_location ();
28032 /* Give it C++ linkage to avoid confusing other parts of the
28033 front end. */
28034 push_lang_context (lang_name_cplusplus);
28035 need_lang_pop = true;
28036 }
28037 else
28038 need_lang_pop = false;
28039
28040 /* We cannot perform access checks on the template parameter
28041 declarations until we know what is being declared, just as we
28042 cannot check the decl-specifier list. */
28043 push_deferring_access_checks (dk_deferred);
28044
28045 /* If the next token is `>', then we have an invalid
28046 specialization. Rather than complain about an invalid template
28047 parameter, issue an error message here. */
28048 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
28049 {
28050 cp_parser_error (parser, "invalid explicit specialization");
28051 begin_specialization ();
28052 parameter_list = NULL_TREE;
28053 }
28054 else
28055 {
28056 /* Parse the template parameters. */
28057 parameter_list = cp_parser_template_parameter_list (parser);
28058 }
28059
28060 /* Look for the `>'. */
28061 cp_parser_skip_to_end_of_template_parameter_list (parser);
28062
28063 /* Manage template requirements */
28064 if (flag_concepts)
28065 {
28066 tree reqs = get_shorthand_constraints (current_template_parms);
28067 if (tree r = cp_parser_requires_clause_opt (parser))
28068 reqs = conjoin_constraints (reqs, normalize_expression (r));
28069 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
28070 }
28071
28072 cp_parser_template_declaration_after_parameters (parser, parameter_list,
28073 member_p);
28074
28075 /* For the erroneous case of a template with C linkage, we pushed an
28076 implicit C++ linkage scope; exit that scope now. */
28077 if (need_lang_pop)
28078 pop_lang_context ();
28079 }
28080
28081 /* Parse a template-declaration, assuming that the `export' (and
28082 `extern') keywords, if present, has already been scanned. MEMBER_P
28083 is as for cp_parser_template_declaration. */
28084
28085 static bool
28086 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
28087 {
28088 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28089 {
28090 cp_lexer_consume_token (parser->lexer);
28091 cp_parser_explicit_template_declaration (parser, member_p);
28092 return true;
28093 }
28094 else if (flag_concepts)
28095 return cp_parser_template_introduction (parser, member_p);
28096
28097 return false;
28098 }
28099
28100 /* Perform the deferred access checks from a template-parameter-list.
28101 CHECKS is a TREE_LIST of access checks, as returned by
28102 get_deferred_access_checks. */
28103
28104 static void
28105 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
28106 {
28107 ++processing_template_parmlist;
28108 perform_access_checks (checks, tf_warning_or_error);
28109 --processing_template_parmlist;
28110 }
28111
28112 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
28113 `function-definition' sequence that follows a template header.
28114 If MEMBER_P is true, this declaration appears in a class scope.
28115
28116 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
28117 *FRIEND_P is set to TRUE iff the declaration is a friend. */
28118
28119 static tree
28120 cp_parser_single_declaration (cp_parser* parser,
28121 vec<deferred_access_check, va_gc> *checks,
28122 bool member_p,
28123 bool explicit_specialization_p,
28124 bool* friend_p)
28125 {
28126 int declares_class_or_enum;
28127 tree decl = NULL_TREE;
28128 cp_decl_specifier_seq decl_specifiers;
28129 bool function_definition_p = false;
28130 cp_token *decl_spec_token_start;
28131
28132 /* This function is only used when processing a template
28133 declaration. */
28134 gcc_assert (innermost_scope_kind () == sk_template_parms
28135 || innermost_scope_kind () == sk_template_spec);
28136
28137 /* Defer access checks until we know what is being declared. */
28138 push_deferring_access_checks (dk_deferred);
28139
28140 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
28141 alternative. */
28142 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
28143 cp_parser_decl_specifier_seq (parser,
28144 (CP_PARSER_FLAGS_OPTIONAL
28145 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
28146 &decl_specifiers,
28147 &declares_class_or_enum);
28148 if (friend_p)
28149 *friend_p = cp_parser_friend_p (&decl_specifiers);
28150
28151 /* There are no template typedefs. */
28152 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
28153 {
28154 error_at (decl_spec_token_start->location,
28155 "template declaration of %<typedef%>");
28156 decl = error_mark_node;
28157 }
28158
28159 /* Gather up the access checks that occurred the
28160 decl-specifier-seq. */
28161 stop_deferring_access_checks ();
28162
28163 /* Check for the declaration of a template class. */
28164 if (declares_class_or_enum)
28165 {
28166 if (cp_parser_declares_only_class_p (parser)
28167 || (declares_class_or_enum & 2))
28168 {
28169 // If this is a declaration, but not a definition, associate
28170 // any constraints with the type declaration. Constraints
28171 // are associated with definitions in cp_parser_class_specifier.
28172 if (declares_class_or_enum == 1)
28173 associate_classtype_constraints (decl_specifiers.type);
28174
28175 decl = shadow_tag (&decl_specifiers);
28176
28177 /* In this case:
28178
28179 struct C {
28180 friend template <typename T> struct A<T>::B;
28181 };
28182
28183 A<T>::B will be represented by a TYPENAME_TYPE, and
28184 therefore not recognized by shadow_tag. */
28185 if (friend_p && *friend_p
28186 && !decl
28187 && decl_specifiers.type
28188 && TYPE_P (decl_specifiers.type))
28189 decl = decl_specifiers.type;
28190
28191 if (decl && decl != error_mark_node)
28192 decl = TYPE_NAME (decl);
28193 else
28194 decl = error_mark_node;
28195
28196 /* Perform access checks for template parameters. */
28197 cp_parser_perform_template_parameter_access_checks (checks);
28198
28199 /* Give a helpful diagnostic for
28200 template <class T> struct A { } a;
28201 if we aren't already recovering from an error. */
28202 if (!cp_parser_declares_only_class_p (parser)
28203 && !seen_error ())
28204 {
28205 error_at (cp_lexer_peek_token (parser->lexer)->location,
28206 "a class template declaration must not declare "
28207 "anything else");
28208 cp_parser_skip_to_end_of_block_or_statement (parser);
28209 goto out;
28210 }
28211 }
28212 }
28213
28214 /* Complain about missing 'typename' or other invalid type names. */
28215 if (!decl_specifiers.any_type_specifiers_p
28216 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
28217 {
28218 /* cp_parser_parse_and_diagnose_invalid_type_name calls
28219 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28220 the rest of this declaration. */
28221 decl = error_mark_node;
28222 goto out;
28223 }
28224
28225 /* If it's not a template class, try for a template function. If
28226 the next token is a `;', then this declaration does not declare
28227 anything. But, if there were errors in the decl-specifiers, then
28228 the error might well have come from an attempted class-specifier.
28229 In that case, there's no need to warn about a missing declarator. */
28230 if (!decl
28231 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
28232 || decl_specifiers.type != error_mark_node))
28233 {
28234 decl = cp_parser_init_declarator (parser,
28235 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
28236 &decl_specifiers,
28237 checks,
28238 /*function_definition_allowed_p=*/true,
28239 member_p,
28240 declares_class_or_enum,
28241 &function_definition_p,
28242 NULL, NULL, NULL);
28243
28244 /* 7.1.1-1 [dcl.stc]
28245
28246 A storage-class-specifier shall not be specified in an explicit
28247 specialization... */
28248 if (decl
28249 && explicit_specialization_p
28250 && decl_specifiers.storage_class != sc_none)
28251 {
28252 error_at (decl_spec_token_start->location,
28253 "explicit template specialization cannot have a storage class");
28254 decl = error_mark_node;
28255 }
28256
28257 if (decl && VAR_P (decl))
28258 check_template_variable (decl);
28259 }
28260
28261 /* Look for a trailing `;' after the declaration. */
28262 if (!function_definition_p
28263 && (decl == error_mark_node
28264 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
28265 cp_parser_skip_to_end_of_block_or_statement (parser);
28266
28267 out:
28268 pop_deferring_access_checks ();
28269
28270 /* Clear any current qualification; whatever comes next is the start
28271 of something new. */
28272 parser->scope = NULL_TREE;
28273 parser->qualifying_scope = NULL_TREE;
28274 parser->object_scope = NULL_TREE;
28275
28276 return decl;
28277 }
28278
28279 /* Parse a cast-expression that is not the operand of a unary "&". */
28280
28281 static cp_expr
28282 cp_parser_simple_cast_expression (cp_parser *parser)
28283 {
28284 return cp_parser_cast_expression (parser, /*address_p=*/false,
28285 /*cast_p=*/false, /*decltype*/false, NULL);
28286 }
28287
28288 /* Parse a functional cast to TYPE. Returns an expression
28289 representing the cast. */
28290
28291 static cp_expr
28292 cp_parser_functional_cast (cp_parser* parser, tree type)
28293 {
28294 vec<tree, va_gc> *vec;
28295 tree expression_list;
28296 cp_expr cast;
28297 bool nonconst_p;
28298
28299 location_t start_loc = input_location;
28300
28301 if (!type)
28302 type = error_mark_node;
28303
28304 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28305 {
28306 cp_lexer_set_source_position (parser->lexer);
28307 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28308 expression_list = cp_parser_braced_list (parser, &nonconst_p);
28309 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
28310 if (TREE_CODE (type) == TYPE_DECL)
28311 type = TREE_TYPE (type);
28312
28313 cast = finish_compound_literal (type, expression_list,
28314 tf_warning_or_error, fcl_functional);
28315 /* Create a location of the form:
28316 type_name{i, f}
28317 ^~~~~~~~~~~~~~~
28318 with caret == start at the start of the type name,
28319 finishing at the closing brace. */
28320 location_t finish_loc
28321 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28322 location_t combined_loc = make_location (start_loc, start_loc,
28323 finish_loc);
28324 cast.set_location (combined_loc);
28325 return cast;
28326 }
28327
28328
28329 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
28330 /*cast_p=*/true,
28331 /*allow_expansion_p=*/true,
28332 /*non_constant_p=*/NULL);
28333 if (vec == NULL)
28334 expression_list = error_mark_node;
28335 else
28336 {
28337 expression_list = build_tree_list_vec (vec);
28338 release_tree_vector (vec);
28339 }
28340
28341 cast = build_functional_cast (type, expression_list,
28342 tf_warning_or_error);
28343 /* [expr.const]/1: In an integral constant expression "only type
28344 conversions to integral or enumeration type can be used". */
28345 if (TREE_CODE (type) == TYPE_DECL)
28346 type = TREE_TYPE (type);
28347 if (cast != error_mark_node
28348 && !cast_valid_in_integral_constant_expression_p (type)
28349 && cp_parser_non_integral_constant_expression (parser,
28350 NIC_CONSTRUCTOR))
28351 return error_mark_node;
28352
28353 /* Create a location of the form:
28354 float(i)
28355 ^~~~~~~~
28356 with caret == start at the start of the type name,
28357 finishing at the closing paren. */
28358 location_t finish_loc
28359 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28360 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
28361 cast.set_location (combined_loc);
28362 return cast;
28363 }
28364
28365 /* Save the tokens that make up the body of a member function defined
28366 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28367 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28368 specifiers applied to the declaration. Returns the FUNCTION_DECL
28369 for the member function. */
28370
28371 static tree
28372 cp_parser_save_member_function_body (cp_parser* parser,
28373 cp_decl_specifier_seq *decl_specifiers,
28374 cp_declarator *declarator,
28375 tree attributes)
28376 {
28377 cp_token *first;
28378 cp_token *last;
28379 tree fn;
28380 bool function_try_block = false;
28381
28382 /* Create the FUNCTION_DECL. */
28383 fn = grokmethod (decl_specifiers, declarator, attributes);
28384 cp_finalize_omp_declare_simd (parser, fn);
28385 cp_finalize_oacc_routine (parser, fn, true);
28386 /* If something went badly wrong, bail out now. */
28387 if (fn == error_mark_node)
28388 {
28389 /* If there's a function-body, skip it. */
28390 if (cp_parser_token_starts_function_definition_p
28391 (cp_lexer_peek_token (parser->lexer)))
28392 cp_parser_skip_to_end_of_block_or_statement (parser);
28393 return error_mark_node;
28394 }
28395
28396 /* Remember it, if there default args to post process. */
28397 cp_parser_save_default_args (parser, fn);
28398
28399 /* Save away the tokens that make up the body of the
28400 function. */
28401 first = parser->lexer->next_token;
28402
28403 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
28404 cp_lexer_consume_token (parser->lexer);
28405 else if (cp_lexer_next_token_is_keyword (parser->lexer,
28406 RID_TRANSACTION_ATOMIC))
28407 {
28408 cp_lexer_consume_token (parser->lexer);
28409 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28410 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
28411 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
28412 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
28413 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28414 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
28415 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
28416 {
28417 cp_lexer_consume_token (parser->lexer);
28418 cp_lexer_consume_token (parser->lexer);
28419 cp_lexer_consume_token (parser->lexer);
28420 cp_lexer_consume_token (parser->lexer);
28421 cp_lexer_consume_token (parser->lexer);
28422 }
28423 else
28424 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
28425 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
28426 {
28427 cp_lexer_consume_token (parser->lexer);
28428 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28429 break;
28430 }
28431 }
28432
28433 /* Handle function try blocks. */
28434 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28435 {
28436 cp_lexer_consume_token (parser->lexer);
28437 function_try_block = true;
28438 }
28439 /* We can have braced-init-list mem-initializers before the fn body. */
28440 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28441 {
28442 cp_lexer_consume_token (parser->lexer);
28443 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
28444 {
28445 /* cache_group will stop after an un-nested { } pair, too. */
28446 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28447 break;
28448
28449 /* variadic mem-inits have ... after the ')'. */
28450 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28451 cp_lexer_consume_token (parser->lexer);
28452 }
28453 }
28454 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28455 /* Handle function try blocks. */
28456 if (function_try_block)
28457 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
28458 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28459 last = parser->lexer->next_token;
28460
28461 /* Save away the inline definition; we will process it when the
28462 class is complete. */
28463 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
28464 DECL_PENDING_INLINE_P (fn) = 1;
28465
28466 /* We need to know that this was defined in the class, so that
28467 friend templates are handled correctly. */
28468 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
28469
28470 /* Add FN to the queue of functions to be parsed later. */
28471 vec_safe_push (unparsed_funs_with_definitions, fn);
28472
28473 return fn;
28474 }
28475
28476 /* Save the tokens that make up the in-class initializer for a non-static
28477 data member. Returns a DEFAULT_ARG. */
28478
28479 static tree
28480 cp_parser_save_nsdmi (cp_parser* parser)
28481 {
28482 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
28483 }
28484
28485 /* Parse a template-argument-list, as well as the trailing ">" (but
28486 not the opening "<"). See cp_parser_template_argument_list for the
28487 return value. */
28488
28489 static tree
28490 cp_parser_enclosed_template_argument_list (cp_parser* parser)
28491 {
28492 tree arguments;
28493 tree saved_scope;
28494 tree saved_qualifying_scope;
28495 tree saved_object_scope;
28496 bool saved_greater_than_is_operator_p;
28497
28498 /* [temp.names]
28499
28500 When parsing a template-id, the first non-nested `>' is taken as
28501 the end of the template-argument-list rather than a greater-than
28502 operator. */
28503 saved_greater_than_is_operator_p
28504 = parser->greater_than_is_operator_p;
28505 parser->greater_than_is_operator_p = false;
28506 /* Parsing the argument list may modify SCOPE, so we save it
28507 here. */
28508 saved_scope = parser->scope;
28509 saved_qualifying_scope = parser->qualifying_scope;
28510 saved_object_scope = parser->object_scope;
28511 /* We need to evaluate the template arguments, even though this
28512 template-id may be nested within a "sizeof". */
28513 cp_evaluated ev;
28514 /* Parse the template-argument-list itself. */
28515 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
28516 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28517 arguments = NULL_TREE;
28518 else
28519 arguments = cp_parser_template_argument_list (parser);
28520 /* Look for the `>' that ends the template-argument-list. If we find
28521 a '>>' instead, it's probably just a typo. */
28522 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28523 {
28524 if (cxx_dialect != cxx98)
28525 {
28526 /* In C++0x, a `>>' in a template argument list or cast
28527 expression is considered to be two separate `>'
28528 tokens. So, change the current token to a `>', but don't
28529 consume it: it will be consumed later when the outer
28530 template argument list (or cast expression) is parsed.
28531 Note that this replacement of `>' for `>>' is necessary
28532 even if we are parsing tentatively: in the tentative
28533 case, after calling
28534 cp_parser_enclosed_template_argument_list we will always
28535 throw away all of the template arguments and the first
28536 closing `>', either because the template argument list
28537 was erroneous or because we are replacing those tokens
28538 with a CPP_TEMPLATE_ID token. The second `>' (which will
28539 not have been thrown away) is needed either to close an
28540 outer template argument list or to complete a new-style
28541 cast. */
28542 cp_token *token = cp_lexer_peek_token (parser->lexer);
28543 token->type = CPP_GREATER;
28544 }
28545 else if (!saved_greater_than_is_operator_p)
28546 {
28547 /* If we're in a nested template argument list, the '>>' has
28548 to be a typo for '> >'. We emit the error message, but we
28549 continue parsing and we push a '>' as next token, so that
28550 the argument list will be parsed correctly. Note that the
28551 global source location is still on the token before the
28552 '>>', so we need to say explicitly where we want it. */
28553 cp_token *token = cp_lexer_peek_token (parser->lexer);
28554 gcc_rich_location richloc (token->location);
28555 richloc.add_fixit_replace ("> >");
28556 error_at (&richloc, "%<>>%> should be %<> >%> "
28557 "within a nested template argument list");
28558
28559 token->type = CPP_GREATER;
28560 }
28561 else
28562 {
28563 /* If this is not a nested template argument list, the '>>'
28564 is a typo for '>'. Emit an error message and continue.
28565 Same deal about the token location, but here we can get it
28566 right by consuming the '>>' before issuing the diagnostic. */
28567 cp_token *token = cp_lexer_consume_token (parser->lexer);
28568 error_at (token->location,
28569 "spurious %<>>%>, use %<>%> to terminate "
28570 "a template argument list");
28571 }
28572 }
28573 else
28574 cp_parser_skip_to_end_of_template_parameter_list (parser);
28575 /* The `>' token might be a greater-than operator again now. */
28576 parser->greater_than_is_operator_p
28577 = saved_greater_than_is_operator_p;
28578 /* Restore the SAVED_SCOPE. */
28579 parser->scope = saved_scope;
28580 parser->qualifying_scope = saved_qualifying_scope;
28581 parser->object_scope = saved_object_scope;
28582
28583 return arguments;
28584 }
28585
28586 /* MEMBER_FUNCTION is a member function, or a friend. If default
28587 arguments, or the body of the function have not yet been parsed,
28588 parse them now. */
28589
28590 static void
28591 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
28592 {
28593 timevar_push (TV_PARSE_INMETH);
28594 /* If this member is a template, get the underlying
28595 FUNCTION_DECL. */
28596 if (DECL_FUNCTION_TEMPLATE_P (member_function))
28597 member_function = DECL_TEMPLATE_RESULT (member_function);
28598
28599 /* There should not be any class definitions in progress at this
28600 point; the bodies of members are only parsed outside of all class
28601 definitions. */
28602 gcc_assert (parser->num_classes_being_defined == 0);
28603 /* While we're parsing the member functions we might encounter more
28604 classes. We want to handle them right away, but we don't want
28605 them getting mixed up with functions that are currently in the
28606 queue. */
28607 push_unparsed_function_queues (parser);
28608
28609 /* Make sure that any template parameters are in scope. */
28610 maybe_begin_member_template_processing (member_function);
28611
28612 /* If the body of the function has not yet been parsed, parse it
28613 now. */
28614 if (DECL_PENDING_INLINE_P (member_function))
28615 {
28616 tree function_scope;
28617 cp_token_cache *tokens;
28618
28619 /* The function is no longer pending; we are processing it. */
28620 tokens = DECL_PENDING_INLINE_INFO (member_function);
28621 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28622 DECL_PENDING_INLINE_P (member_function) = 0;
28623
28624 /* If this is a local class, enter the scope of the containing
28625 function. */
28626 function_scope = current_function_decl;
28627 if (function_scope)
28628 push_function_context ();
28629
28630 /* Push the body of the function onto the lexer stack. */
28631 cp_parser_push_lexer_for_tokens (parser, tokens);
28632
28633 /* Let the front end know that we going to be defining this
28634 function. */
28635 start_preparsed_function (member_function, NULL_TREE,
28636 SF_PRE_PARSED | SF_INCLASS_INLINE);
28637
28638 /* Don't do access checking if it is a templated function. */
28639 if (processing_template_decl)
28640 push_deferring_access_checks (dk_no_check);
28641
28642 /* #pragma omp declare reduction needs special parsing. */
28643 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28644 {
28645 parser->lexer->in_pragma = true;
28646 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28647 finish_function (/*inline_p=*/true);
28648 cp_check_omp_declare_reduction (member_function);
28649 }
28650 else
28651 /* Now, parse the body of the function. */
28652 cp_parser_function_definition_after_declarator (parser,
28653 /*inline_p=*/true);
28654
28655 if (processing_template_decl)
28656 pop_deferring_access_checks ();
28657
28658 /* Leave the scope of the containing function. */
28659 if (function_scope)
28660 pop_function_context ();
28661 cp_parser_pop_lexer (parser);
28662 }
28663
28664 /* Remove any template parameters from the symbol table. */
28665 maybe_end_member_template_processing ();
28666
28667 /* Restore the queue. */
28668 pop_unparsed_function_queues (parser);
28669 timevar_pop (TV_PARSE_INMETH);
28670 }
28671
28672 /* If DECL contains any default args, remember it on the unparsed
28673 functions queue. */
28674
28675 static void
28676 cp_parser_save_default_args (cp_parser* parser, tree decl)
28677 {
28678 tree probe;
28679
28680 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28681 probe;
28682 probe = TREE_CHAIN (probe))
28683 if (TREE_PURPOSE (probe))
28684 {
28685 cp_default_arg_entry entry = {current_class_type, decl};
28686 vec_safe_push (unparsed_funs_with_default_args, entry);
28687 break;
28688 }
28689 }
28690
28691 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28692 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28693 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28694 from the parameter-type-list. */
28695
28696 static tree
28697 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28698 tree default_arg, tree parmtype)
28699 {
28700 cp_token_cache *tokens;
28701 tree parsed_arg;
28702 bool dummy;
28703
28704 if (default_arg == error_mark_node)
28705 return error_mark_node;
28706
28707 /* Push the saved tokens for the default argument onto the parser's
28708 lexer stack. */
28709 tokens = DEFARG_TOKENS (default_arg);
28710 cp_parser_push_lexer_for_tokens (parser, tokens);
28711
28712 start_lambda_scope (decl);
28713
28714 /* Parse the default argument. */
28715 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28716 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28717 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28718
28719 finish_lambda_scope ();
28720
28721 if (parsed_arg == error_mark_node)
28722 cp_parser_skip_to_end_of_statement (parser);
28723
28724 if (!processing_template_decl)
28725 {
28726 /* In a non-template class, check conversions now. In a template,
28727 we'll wait and instantiate these as needed. */
28728 if (TREE_CODE (decl) == PARM_DECL)
28729 parsed_arg = check_default_argument (parmtype, parsed_arg,
28730 tf_warning_or_error);
28731 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28732 parsed_arg = error_mark_node;
28733 else
28734 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28735 }
28736
28737 /* If the token stream has not been completely used up, then
28738 there was extra junk after the end of the default
28739 argument. */
28740 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28741 {
28742 if (TREE_CODE (decl) == PARM_DECL)
28743 cp_parser_error (parser, "expected %<,%>");
28744 else
28745 cp_parser_error (parser, "expected %<;%>");
28746 }
28747
28748 /* Revert to the main lexer. */
28749 cp_parser_pop_lexer (parser);
28750
28751 return parsed_arg;
28752 }
28753
28754 /* FIELD is a non-static data member with an initializer which we saved for
28755 later; parse it now. */
28756
28757 static void
28758 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28759 {
28760 tree def;
28761
28762 maybe_begin_member_template_processing (field);
28763
28764 push_unparsed_function_queues (parser);
28765 def = cp_parser_late_parse_one_default_arg (parser, field,
28766 DECL_INITIAL (field),
28767 NULL_TREE);
28768 pop_unparsed_function_queues (parser);
28769
28770 maybe_end_member_template_processing ();
28771
28772 DECL_INITIAL (field) = def;
28773 }
28774
28775 /* FN is a FUNCTION_DECL which may contains a parameter with an
28776 unparsed DEFAULT_ARG. Parse the default args now. This function
28777 assumes that the current scope is the scope in which the default
28778 argument should be processed. */
28779
28780 static void
28781 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28782 {
28783 unsigned char saved_local_variables_forbidden_p;
28784 tree parm, parmdecl;
28785
28786 /* While we're parsing the default args, we might (due to the
28787 statement expression extension) encounter more classes. We want
28788 to handle them right away, but we don't want them getting mixed
28789 up with default args that are currently in the queue. */
28790 push_unparsed_function_queues (parser);
28791
28792 /* Local variable names (and the `this' keyword) may not appear
28793 in a default argument. */
28794 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28795 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
28796
28797 push_defarg_context (fn);
28798
28799 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28800 parmdecl = DECL_ARGUMENTS (fn);
28801 parm && parm != void_list_node;
28802 parm = TREE_CHAIN (parm),
28803 parmdecl = DECL_CHAIN (parmdecl))
28804 {
28805 tree default_arg = TREE_PURPOSE (parm);
28806 tree parsed_arg;
28807 vec<tree, va_gc> *insts;
28808 tree copy;
28809 unsigned ix;
28810
28811 if (!default_arg)
28812 continue;
28813
28814 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28815 /* This can happen for a friend declaration for a function
28816 already declared with default arguments. */
28817 continue;
28818
28819 parsed_arg
28820 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28821 default_arg,
28822 TREE_VALUE (parm));
28823 TREE_PURPOSE (parm) = parsed_arg;
28824
28825 /* Update any instantiations we've already created. */
28826 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28827 vec_safe_iterate (insts, ix, &copy); ix++)
28828 TREE_PURPOSE (copy) = parsed_arg;
28829 }
28830
28831 pop_defarg_context ();
28832
28833 /* Make sure no default arg is missing. */
28834 check_default_args (fn);
28835
28836 /* Restore the state of local_variables_forbidden_p. */
28837 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28838
28839 /* Restore the queue. */
28840 pop_unparsed_function_queues (parser);
28841 }
28842
28843 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28844
28845 sizeof ... ( identifier )
28846
28847 where the 'sizeof' token has already been consumed. */
28848
28849 static tree
28850 cp_parser_sizeof_pack (cp_parser *parser)
28851 {
28852 /* Consume the `...'. */
28853 cp_lexer_consume_token (parser->lexer);
28854 maybe_warn_variadic_templates ();
28855
28856 matching_parens parens;
28857 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28858 if (paren)
28859 parens.consume_open (parser);
28860 else
28861 permerror (cp_lexer_peek_token (parser->lexer)->location,
28862 "%<sizeof...%> argument must be surrounded by parentheses");
28863
28864 cp_token *token = cp_lexer_peek_token (parser->lexer);
28865 tree name = cp_parser_identifier (parser);
28866 if (name == error_mark_node)
28867 return error_mark_node;
28868 /* The name is not qualified. */
28869 parser->scope = NULL_TREE;
28870 parser->qualifying_scope = NULL_TREE;
28871 parser->object_scope = NULL_TREE;
28872 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28873 if (expr == error_mark_node)
28874 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28875 token->location);
28876 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28877 expr = TREE_TYPE (expr);
28878 else if (TREE_CODE (expr) == CONST_DECL)
28879 expr = DECL_INITIAL (expr);
28880 expr = make_pack_expansion (expr);
28881 PACK_EXPANSION_SIZEOF_P (expr) = true;
28882
28883 if (paren)
28884 parens.require_close (parser);
28885
28886 return expr;
28887 }
28888
28889 /* Parse the operand of `sizeof' (or a similar operator). Returns
28890 either a TYPE or an expression, depending on the form of the
28891 input. The KEYWORD indicates which kind of expression we have
28892 encountered. */
28893
28894 static tree
28895 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28896 {
28897 tree expr = NULL_TREE;
28898 const char *saved_message;
28899 const char *saved_message_arg;
28900 bool saved_integral_constant_expression_p;
28901 bool saved_non_integral_constant_expression_p;
28902
28903 /* If it's a `...', then we are computing the length of a parameter
28904 pack. */
28905 if (keyword == RID_SIZEOF
28906 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28907 return cp_parser_sizeof_pack (parser);
28908
28909 /* Types cannot be defined in a `sizeof' expression. Save away the
28910 old message. */
28911 saved_message = parser->type_definition_forbidden_message;
28912 saved_message_arg = parser->type_definition_forbidden_message_arg;
28913 parser->type_definition_forbidden_message
28914 = G_("types may not be defined in %qs expressions");
28915 parser->type_definition_forbidden_message_arg
28916 = IDENTIFIER_POINTER (ridpointers[keyword]);
28917
28918 /* The restrictions on constant-expressions do not apply inside
28919 sizeof expressions. */
28920 saved_integral_constant_expression_p
28921 = parser->integral_constant_expression_p;
28922 saved_non_integral_constant_expression_p
28923 = parser->non_integral_constant_expression_p;
28924 parser->integral_constant_expression_p = false;
28925
28926 /* Do not actually evaluate the expression. */
28927 ++cp_unevaluated_operand;
28928 ++c_inhibit_evaluation_warnings;
28929 /* If it's a `(', then we might be looking at the type-id
28930 construction. */
28931 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28932 {
28933 tree type = NULL_TREE;
28934
28935 /* We can't be sure yet whether we're looking at a type-id or an
28936 expression. */
28937 cp_parser_parse_tentatively (parser);
28938
28939 matching_parens parens;
28940 parens.consume_open (parser);
28941
28942 /* Note: as a GNU Extension, compound literals are considered
28943 postfix-expressions as they are in C99, so they are valid
28944 arguments to sizeof. See comment in cp_parser_cast_expression
28945 for details. */
28946 if (cp_parser_compound_literal_p (parser))
28947 cp_parser_simulate_error (parser);
28948 else
28949 {
28950 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28951 parser->in_type_id_in_expr_p = true;
28952 /* Look for the type-id. */
28953 type = cp_parser_type_id (parser);
28954 /* Look for the closing `)'. */
28955 parens.require_close (parser);
28956 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28957 }
28958
28959 /* If all went well, then we're done. */
28960 if (cp_parser_parse_definitely (parser))
28961 expr = type;
28962 }
28963
28964 /* If the type-id production did not work out, then we must be
28965 looking at the unary-expression production. */
28966 if (!expr)
28967 expr = cp_parser_unary_expression (parser);
28968
28969 /* Go back to evaluating expressions. */
28970 --cp_unevaluated_operand;
28971 --c_inhibit_evaluation_warnings;
28972
28973 /* And restore the old one. */
28974 parser->type_definition_forbidden_message = saved_message;
28975 parser->type_definition_forbidden_message_arg = saved_message_arg;
28976 parser->integral_constant_expression_p
28977 = saved_integral_constant_expression_p;
28978 parser->non_integral_constant_expression_p
28979 = saved_non_integral_constant_expression_p;
28980
28981 return expr;
28982 }
28983
28984 /* If the current declaration has no declarator, return true. */
28985
28986 static bool
28987 cp_parser_declares_only_class_p (cp_parser *parser)
28988 {
28989 /* If the next token is a `;' or a `,' then there is no
28990 declarator. */
28991 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28992 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28993 }
28994
28995 /* Update the DECL_SPECS to reflect the storage class indicated by
28996 KEYWORD. */
28997
28998 static void
28999 cp_parser_set_storage_class (cp_parser *parser,
29000 cp_decl_specifier_seq *decl_specs,
29001 enum rid keyword,
29002 cp_token *token)
29003 {
29004 cp_storage_class storage_class;
29005
29006 if (parser->in_unbraced_linkage_specification_p)
29007 {
29008 error_at (token->location, "invalid use of %qD in linkage specification",
29009 ridpointers[keyword]);
29010 return;
29011 }
29012 else if (decl_specs->storage_class != sc_none)
29013 {
29014 decl_specs->conflicting_specifiers_p = true;
29015 return;
29016 }
29017
29018 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
29019 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
29020 && decl_specs->gnu_thread_keyword_p)
29021 {
29022 pedwarn (decl_specs->locations[ds_thread], 0,
29023 "%<__thread%> before %qD", ridpointers[keyword]);
29024 }
29025
29026 switch (keyword)
29027 {
29028 case RID_AUTO:
29029 storage_class = sc_auto;
29030 break;
29031 case RID_REGISTER:
29032 storage_class = sc_register;
29033 break;
29034 case RID_STATIC:
29035 storage_class = sc_static;
29036 break;
29037 case RID_EXTERN:
29038 storage_class = sc_extern;
29039 break;
29040 case RID_MUTABLE:
29041 storage_class = sc_mutable;
29042 break;
29043 default:
29044 gcc_unreachable ();
29045 }
29046 decl_specs->storage_class = storage_class;
29047 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
29048
29049 /* A storage class specifier cannot be applied alongside a typedef
29050 specifier. If there is a typedef specifier present then set
29051 conflicting_specifiers_p which will trigger an error later
29052 on in grokdeclarator. */
29053 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
29054 decl_specs->conflicting_specifiers_p = true;
29055 }
29056
29057 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
29058 is true, the type is a class or enum definition. */
29059
29060 static void
29061 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
29062 tree type_spec,
29063 cp_token *token,
29064 bool type_definition_p)
29065 {
29066 decl_specs->any_specifiers_p = true;
29067
29068 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
29069 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
29070 this is what happened. In system headers, we ignore these
29071 declarations so that G++ can work with system headers that are not
29072 C++-safe. */
29073 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
29074 && !type_definition_p
29075 && (type_spec == boolean_type_node
29076 || type_spec == char8_type_node
29077 || type_spec == char16_type_node
29078 || type_spec == char32_type_node
29079 || type_spec == wchar_type_node)
29080 && (decl_specs->type
29081 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
29082 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
29083 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
29084 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
29085 {
29086 decl_specs->redefined_builtin_type = type_spec;
29087 set_and_check_decl_spec_loc (decl_specs,
29088 ds_redefined_builtin_type_spec,
29089 token);
29090 if (!decl_specs->type)
29091 {
29092 decl_specs->type = type_spec;
29093 decl_specs->type_definition_p = false;
29094 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
29095 }
29096 }
29097 else if (decl_specs->type)
29098 decl_specs->multiple_types_p = true;
29099 else
29100 {
29101 decl_specs->type = type_spec;
29102 decl_specs->type_definition_p = type_definition_p;
29103 decl_specs->redefined_builtin_type = NULL_TREE;
29104 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
29105 }
29106 }
29107
29108 /* True iff TOKEN is the GNU keyword __thread. */
29109
29110 static bool
29111 token_is__thread (cp_token *token)
29112 {
29113 gcc_assert (token->keyword == RID_THREAD);
29114 return id_equal (token->u.value, "__thread");
29115 }
29116
29117 /* Set the location for a declarator specifier and check if it is
29118 duplicated.
29119
29120 DECL_SPECS is the sequence of declarator specifiers onto which to
29121 set the location.
29122
29123 DS is the single declarator specifier to set which location is to
29124 be set onto the existing sequence of declarators.
29125
29126 LOCATION is the location for the declarator specifier to
29127 consider. */
29128
29129 static void
29130 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
29131 cp_decl_spec ds, cp_token *token)
29132 {
29133 gcc_assert (ds < ds_last);
29134
29135 if (decl_specs == NULL)
29136 return;
29137
29138 location_t location = token->location;
29139
29140 if (decl_specs->locations[ds] == 0)
29141 {
29142 decl_specs->locations[ds] = location;
29143 if (ds == ds_thread)
29144 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
29145 }
29146 else
29147 {
29148 if (ds == ds_long)
29149 {
29150 if (decl_specs->locations[ds_long_long] != 0)
29151 error_at (location,
29152 "%<long long long%> is too long for GCC");
29153 else
29154 {
29155 decl_specs->locations[ds_long_long] = location;
29156 pedwarn_cxx98 (location,
29157 OPT_Wlong_long,
29158 "ISO C++ 1998 does not support %<long long%>");
29159 }
29160 }
29161 else if (ds == ds_thread)
29162 {
29163 bool gnu = token_is__thread (token);
29164 gcc_rich_location richloc (location);
29165 if (gnu != decl_specs->gnu_thread_keyword_p)
29166 {
29167 richloc.add_range (decl_specs->locations[ds_thread]);
29168 error_at (&richloc,
29169 "both %<__thread%> and %<thread_local%> specified");
29170 }
29171 else
29172 {
29173 richloc.add_fixit_remove ();
29174 error_at (&richloc, "duplicate %qD", token->u.value);
29175 }
29176 }
29177 else
29178 {
29179 static const char *const decl_spec_names[] = {
29180 "signed",
29181 "unsigned",
29182 "short",
29183 "long",
29184 "const",
29185 "volatile",
29186 "restrict",
29187 "inline",
29188 "virtual",
29189 "explicit",
29190 "friend",
29191 "typedef",
29192 "using",
29193 "constexpr",
29194 "__complex"
29195 };
29196 gcc_rich_location richloc (location);
29197 richloc.add_fixit_remove ();
29198 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
29199 }
29200 }
29201 }
29202
29203 /* Return true iff the declarator specifier DS is present in the
29204 sequence of declarator specifiers DECL_SPECS. */
29205
29206 bool
29207 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
29208 cp_decl_spec ds)
29209 {
29210 gcc_assert (ds < ds_last);
29211
29212 if (decl_specs == NULL)
29213 return false;
29214
29215 return decl_specs->locations[ds] != 0;
29216 }
29217
29218 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29219 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
29220
29221 static bool
29222 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
29223 {
29224 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
29225 }
29226
29227 /* Issue an error message indicating that TOKEN_DESC was expected.
29228 If KEYWORD is true, it indicated this function is called by
29229 cp_parser_require_keword and the required token can only be
29230 a indicated keyword.
29231
29232 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29233 within any error as the location of an "opening" token matching
29234 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29235 RT_CLOSE_PAREN). */
29236
29237 static void
29238 cp_parser_required_error (cp_parser *parser,
29239 required_token token_desc,
29240 bool keyword,
29241 location_t matching_location)
29242 {
29243 if (cp_parser_simulate_error (parser))
29244 return;
29245
29246 const char *gmsgid = NULL;
29247 switch (token_desc)
29248 {
29249 case RT_NEW:
29250 gmsgid = G_("expected %<new%>");
29251 break;
29252 case RT_DELETE:
29253 gmsgid = G_("expected %<delete%>");
29254 break;
29255 case RT_RETURN:
29256 gmsgid = G_("expected %<return%>");
29257 break;
29258 case RT_WHILE:
29259 gmsgid = G_("expected %<while%>");
29260 break;
29261 case RT_EXTERN:
29262 gmsgid = G_("expected %<extern%>");
29263 break;
29264 case RT_STATIC_ASSERT:
29265 gmsgid = G_("expected %<static_assert%>");
29266 break;
29267 case RT_DECLTYPE:
29268 gmsgid = G_("expected %<decltype%>");
29269 break;
29270 case RT_OPERATOR:
29271 gmsgid = G_("expected %<operator%>");
29272 break;
29273 case RT_CLASS:
29274 gmsgid = G_("expected %<class%>");
29275 break;
29276 case RT_TEMPLATE:
29277 gmsgid = G_("expected %<template%>");
29278 break;
29279 case RT_NAMESPACE:
29280 gmsgid = G_("expected %<namespace%>");
29281 break;
29282 case RT_USING:
29283 gmsgid = G_("expected %<using%>");
29284 break;
29285 case RT_ASM:
29286 gmsgid = G_("expected %<asm%>");
29287 break;
29288 case RT_TRY:
29289 gmsgid = G_("expected %<try%>");
29290 break;
29291 case RT_CATCH:
29292 gmsgid = G_("expected %<catch%>");
29293 break;
29294 case RT_THROW:
29295 gmsgid = G_("expected %<throw%>");
29296 break;
29297 case RT_LABEL:
29298 gmsgid = G_("expected %<__label__%>");
29299 break;
29300 case RT_AT_TRY:
29301 gmsgid = G_("expected %<@try%>");
29302 break;
29303 case RT_AT_SYNCHRONIZED:
29304 gmsgid = G_("expected %<@synchronized%>");
29305 break;
29306 case RT_AT_THROW:
29307 gmsgid = G_("expected %<@throw%>");
29308 break;
29309 case RT_TRANSACTION_ATOMIC:
29310 gmsgid = G_("expected %<__transaction_atomic%>");
29311 break;
29312 case RT_TRANSACTION_RELAXED:
29313 gmsgid = G_("expected %<__transaction_relaxed%>");
29314 break;
29315 default:
29316 break;
29317 }
29318
29319 if (!gmsgid && !keyword)
29320 {
29321 switch (token_desc)
29322 {
29323 case RT_SEMICOLON:
29324 gmsgid = G_("expected %<;%>");
29325 break;
29326 case RT_OPEN_PAREN:
29327 gmsgid = G_("expected %<(%>");
29328 break;
29329 case RT_CLOSE_BRACE:
29330 gmsgid = G_("expected %<}%>");
29331 break;
29332 case RT_OPEN_BRACE:
29333 gmsgid = G_("expected %<{%>");
29334 break;
29335 case RT_CLOSE_SQUARE:
29336 gmsgid = G_("expected %<]%>");
29337 break;
29338 case RT_OPEN_SQUARE:
29339 gmsgid = G_("expected %<[%>");
29340 break;
29341 case RT_COMMA:
29342 gmsgid = G_("expected %<,%>");
29343 break;
29344 case RT_SCOPE:
29345 gmsgid = G_("expected %<::%>");
29346 break;
29347 case RT_LESS:
29348 gmsgid = G_("expected %<<%>");
29349 break;
29350 case RT_GREATER:
29351 gmsgid = G_("expected %<>%>");
29352 break;
29353 case RT_EQ:
29354 gmsgid = G_("expected %<=%>");
29355 break;
29356 case RT_ELLIPSIS:
29357 gmsgid = G_("expected %<...%>");
29358 break;
29359 case RT_MULT:
29360 gmsgid = G_("expected %<*%>");
29361 break;
29362 case RT_COMPL:
29363 gmsgid = G_("expected %<~%>");
29364 break;
29365 case RT_COLON:
29366 gmsgid = G_("expected %<:%>");
29367 break;
29368 case RT_COLON_SCOPE:
29369 gmsgid = G_("expected %<:%> or %<::%>");
29370 break;
29371 case RT_CLOSE_PAREN:
29372 gmsgid = G_("expected %<)%>");
29373 break;
29374 case RT_COMMA_CLOSE_PAREN:
29375 gmsgid = G_("expected %<,%> or %<)%>");
29376 break;
29377 case RT_PRAGMA_EOL:
29378 gmsgid = G_("expected end of line");
29379 break;
29380 case RT_NAME:
29381 gmsgid = G_("expected identifier");
29382 break;
29383 case RT_SELECT:
29384 gmsgid = G_("expected selection-statement");
29385 break;
29386 case RT_ITERATION:
29387 gmsgid = G_("expected iteration-statement");
29388 break;
29389 case RT_JUMP:
29390 gmsgid = G_("expected jump-statement");
29391 break;
29392 case RT_CLASS_KEY:
29393 gmsgid = G_("expected class-key");
29394 break;
29395 case RT_CLASS_TYPENAME_TEMPLATE:
29396 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
29397 break;
29398 default:
29399 gcc_unreachable ();
29400 }
29401 }
29402
29403 if (gmsgid)
29404 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
29405 }
29406
29407
29408 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29409 issue an error message indicating that TOKEN_DESC was expected.
29410
29411 Returns the token consumed, if the token had the appropriate type.
29412 Otherwise, returns NULL.
29413
29414 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29415 within any error as the location of an "opening" token matching
29416 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29417 RT_CLOSE_PAREN). */
29418
29419 static cp_token *
29420 cp_parser_require (cp_parser* parser,
29421 enum cpp_ttype type,
29422 required_token token_desc,
29423 location_t matching_location)
29424 {
29425 if (cp_lexer_next_token_is (parser->lexer, type))
29426 return cp_lexer_consume_token (parser->lexer);
29427 else
29428 {
29429 /* Output the MESSAGE -- unless we're parsing tentatively. */
29430 if (!cp_parser_simulate_error (parser))
29431 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
29432 matching_location);
29433 return NULL;
29434 }
29435 }
29436
29437 /* An error message is produced if the next token is not '>'.
29438 All further tokens are skipped until the desired token is
29439 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29440
29441 static void
29442 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
29443 {
29444 /* Current level of '< ... >'. */
29445 unsigned level = 0;
29446 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29447 unsigned nesting_depth = 0;
29448
29449 /* Are we ready, yet? If not, issue error message. */
29450 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
29451 return;
29452
29453 /* Skip tokens until the desired token is found. */
29454 while (true)
29455 {
29456 /* Peek at the next token. */
29457 switch (cp_lexer_peek_token (parser->lexer)->type)
29458 {
29459 case CPP_LESS:
29460 if (!nesting_depth)
29461 ++level;
29462 break;
29463
29464 case CPP_RSHIFT:
29465 if (cxx_dialect == cxx98)
29466 /* C++0x views the `>>' operator as two `>' tokens, but
29467 C++98 does not. */
29468 break;
29469 else if (!nesting_depth && level-- == 0)
29470 {
29471 /* We've hit a `>>' where the first `>' closes the
29472 template argument list, and the second `>' is
29473 spurious. Just consume the `>>' and stop; we've
29474 already produced at least one error. */
29475 cp_lexer_consume_token (parser->lexer);
29476 return;
29477 }
29478 /* Fall through for C++0x, so we handle the second `>' in
29479 the `>>'. */
29480 gcc_fallthrough ();
29481
29482 case CPP_GREATER:
29483 if (!nesting_depth && level-- == 0)
29484 {
29485 /* We've reached the token we want, consume it and stop. */
29486 cp_lexer_consume_token (parser->lexer);
29487 return;
29488 }
29489 break;
29490
29491 case CPP_OPEN_PAREN:
29492 case CPP_OPEN_SQUARE:
29493 ++nesting_depth;
29494 break;
29495
29496 case CPP_CLOSE_PAREN:
29497 case CPP_CLOSE_SQUARE:
29498 if (nesting_depth-- == 0)
29499 return;
29500 break;
29501
29502 case CPP_EOF:
29503 case CPP_PRAGMA_EOL:
29504 case CPP_SEMICOLON:
29505 case CPP_OPEN_BRACE:
29506 case CPP_CLOSE_BRACE:
29507 /* The '>' was probably forgotten, don't look further. */
29508 return;
29509
29510 default:
29511 break;
29512 }
29513
29514 /* Consume this token. */
29515 cp_lexer_consume_token (parser->lexer);
29516 }
29517 }
29518
29519 /* If the next token is the indicated keyword, consume it. Otherwise,
29520 issue an error message indicating that TOKEN_DESC was expected.
29521
29522 Returns the token consumed, if the token had the appropriate type.
29523 Otherwise, returns NULL. */
29524
29525 static cp_token *
29526 cp_parser_require_keyword (cp_parser* parser,
29527 enum rid keyword,
29528 required_token token_desc)
29529 {
29530 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
29531
29532 if (token && token->keyword != keyword)
29533 {
29534 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
29535 UNKNOWN_LOCATION);
29536 return NULL;
29537 }
29538
29539 return token;
29540 }
29541
29542 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29543 function-definition. */
29544
29545 static bool
29546 cp_parser_token_starts_function_definition_p (cp_token* token)
29547 {
29548 return (/* An ordinary function-body begins with an `{'. */
29549 token->type == CPP_OPEN_BRACE
29550 /* A ctor-initializer begins with a `:'. */
29551 || token->type == CPP_COLON
29552 /* A function-try-block begins with `try'. */
29553 || token->keyword == RID_TRY
29554 /* A function-transaction-block begins with `__transaction_atomic'
29555 or `__transaction_relaxed'. */
29556 || token->keyword == RID_TRANSACTION_ATOMIC
29557 || token->keyword == RID_TRANSACTION_RELAXED
29558 /* The named return value extension begins with `return'. */
29559 || token->keyword == RID_RETURN);
29560 }
29561
29562 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29563 definition. */
29564
29565 static bool
29566 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
29567 {
29568 cp_token *token;
29569
29570 token = cp_lexer_peek_token (parser->lexer);
29571 return (token->type == CPP_OPEN_BRACE
29572 || (token->type == CPP_COLON
29573 && !parser->colon_doesnt_start_class_def_p));
29574 }
29575
29576 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29577 C++0x) ending a template-argument. */
29578
29579 static bool
29580 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
29581 {
29582 cp_token *token;
29583
29584 token = cp_lexer_peek_token (parser->lexer);
29585 return (token->type == CPP_COMMA
29586 || token->type == CPP_GREATER
29587 || token->type == CPP_ELLIPSIS
29588 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
29589 }
29590
29591 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29592 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29593
29594 static bool
29595 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
29596 size_t n)
29597 {
29598 cp_token *token;
29599
29600 token = cp_lexer_peek_nth_token (parser->lexer, n);
29601 if (token->type == CPP_LESS)
29602 return true;
29603 /* Check for the sequence `<::' in the original code. It would be lexed as
29604 `[:', where `[' is a digraph, and there is no whitespace before
29605 `:'. */
29606 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
29607 {
29608 cp_token *token2;
29609 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29610 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29611 return true;
29612 }
29613 return false;
29614 }
29615
29616 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29617 or none_type otherwise. */
29618
29619 static enum tag_types
29620 cp_parser_token_is_class_key (cp_token* token)
29621 {
29622 switch (token->keyword)
29623 {
29624 case RID_CLASS:
29625 return class_type;
29626 case RID_STRUCT:
29627 return record_type;
29628 case RID_UNION:
29629 return union_type;
29630
29631 default:
29632 return none_type;
29633 }
29634 }
29635
29636 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29637 or none_type otherwise or if the token is null. */
29638
29639 static enum tag_types
29640 cp_parser_token_is_type_parameter_key (cp_token* token)
29641 {
29642 if (!token)
29643 return none_type;
29644
29645 switch (token->keyword)
29646 {
29647 case RID_CLASS:
29648 return class_type;
29649 case RID_TYPENAME:
29650 return typename_type;
29651
29652 default:
29653 return none_type;
29654 }
29655 }
29656
29657 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29658
29659 static void
29660 cp_parser_check_class_key (enum tag_types class_key, tree type)
29661 {
29662 if (type == error_mark_node)
29663 return;
29664 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29665 {
29666 if (permerror (input_location, "%qs tag used in naming %q#T",
29667 class_key == union_type ? "union"
29668 : class_key == record_type ? "struct" : "class",
29669 type))
29670 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29671 "%q#T was previously declared here", type);
29672 }
29673 }
29674
29675 /* Issue an error message if DECL is redeclared with different
29676 access than its original declaration [class.access.spec/3].
29677 This applies to nested classes, nested class templates and
29678 enumerations [class.mem/1]. */
29679
29680 static void
29681 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29682 {
29683 if (!decl
29684 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29685 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29686 return;
29687
29688 if ((TREE_PRIVATE (decl)
29689 != (current_access_specifier == access_private_node))
29690 || (TREE_PROTECTED (decl)
29691 != (current_access_specifier == access_protected_node)))
29692 error_at (location, "%qD redeclared with different access", decl);
29693 }
29694
29695 /* Look for the `template' keyword, as a syntactic disambiguator.
29696 Return TRUE iff it is present, in which case it will be
29697 consumed. */
29698
29699 static bool
29700 cp_parser_optional_template_keyword (cp_parser *parser)
29701 {
29702 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29703 {
29704 /* In C++98 the `template' keyword can only be used within templates;
29705 outside templates the parser can always figure out what is a
29706 template and what is not. In C++11, per the resolution of DR 468,
29707 `template' is allowed in cases where it is not strictly necessary. */
29708 if (!processing_template_decl
29709 && pedantic && cxx_dialect == cxx98)
29710 {
29711 cp_token *token = cp_lexer_peek_token (parser->lexer);
29712 pedwarn (token->location, OPT_Wpedantic,
29713 "in C++98 %<template%> (as a disambiguator) is only "
29714 "allowed within templates");
29715 /* If this part of the token stream is rescanned, the same
29716 error message would be generated. So, we purge the token
29717 from the stream. */
29718 cp_lexer_purge_token (parser->lexer);
29719 return false;
29720 }
29721 else
29722 {
29723 /* Consume the `template' keyword. */
29724 cp_lexer_consume_token (parser->lexer);
29725 return true;
29726 }
29727 }
29728 return false;
29729 }
29730
29731 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29732 set PARSER->SCOPE, and perform other related actions. */
29733
29734 static void
29735 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29736 {
29737 struct tree_check *check_value;
29738
29739 /* Get the stored value. */
29740 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29741 /* Set the scope from the stored value. */
29742 parser->scope = saved_checks_value (check_value);
29743 parser->qualifying_scope = check_value->qualifying_scope;
29744 parser->object_scope = NULL_TREE;
29745 }
29746
29747 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29748 encounter the end of a block before what we were looking for. */
29749
29750 static bool
29751 cp_parser_cache_group (cp_parser *parser,
29752 enum cpp_ttype end,
29753 unsigned depth)
29754 {
29755 while (true)
29756 {
29757 cp_token *token = cp_lexer_peek_token (parser->lexer);
29758
29759 /* Abort a parenthesized expression if we encounter a semicolon. */
29760 if ((end == CPP_CLOSE_PAREN || depth == 0)
29761 && token->type == CPP_SEMICOLON)
29762 return true;
29763 /* If we've reached the end of the file, stop. */
29764 if (token->type == CPP_EOF
29765 || (end != CPP_PRAGMA_EOL
29766 && token->type == CPP_PRAGMA_EOL))
29767 return true;
29768 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29769 /* We've hit the end of an enclosing block, so there's been some
29770 kind of syntax error. */
29771 return true;
29772
29773 /* Consume the token. */
29774 cp_lexer_consume_token (parser->lexer);
29775 /* See if it starts a new group. */
29776 if (token->type == CPP_OPEN_BRACE)
29777 {
29778 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29779 /* In theory this should probably check end == '}', but
29780 cp_parser_save_member_function_body needs it to exit
29781 after either '}' or ')' when called with ')'. */
29782 if (depth == 0)
29783 return false;
29784 }
29785 else if (token->type == CPP_OPEN_PAREN)
29786 {
29787 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29788 if (depth == 0 && end == CPP_CLOSE_PAREN)
29789 return false;
29790 }
29791 else if (token->type == CPP_PRAGMA)
29792 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29793 else if (token->type == end)
29794 return false;
29795 }
29796 }
29797
29798 /* Like above, for caching a default argument or NSDMI. Both of these are
29799 terminated by a non-nested comma, but it can be unclear whether or not a
29800 comma is nested in a template argument list unless we do more parsing.
29801 In order to handle this ambiguity, when we encounter a ',' after a '<'
29802 we try to parse what follows as a parameter-declaration-list (in the
29803 case of a default argument) or a member-declarator (in the case of an
29804 NSDMI). If that succeeds, then we stop caching. */
29805
29806 static tree
29807 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29808 {
29809 unsigned depth = 0;
29810 int maybe_template_id = 0;
29811 cp_token *first_token;
29812 cp_token *token;
29813 tree default_argument;
29814
29815 /* Add tokens until we have processed the entire default
29816 argument. We add the range [first_token, token). */
29817 first_token = cp_lexer_peek_token (parser->lexer);
29818 if (first_token->type == CPP_OPEN_BRACE)
29819 {
29820 /* For list-initialization, this is straightforward. */
29821 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29822 token = cp_lexer_peek_token (parser->lexer);
29823 }
29824 else while (true)
29825 {
29826 bool done = false;
29827
29828 /* Peek at the next token. */
29829 token = cp_lexer_peek_token (parser->lexer);
29830 /* What we do depends on what token we have. */
29831 switch (token->type)
29832 {
29833 /* In valid code, a default argument must be
29834 immediately followed by a `,' `)', or `...'. */
29835 case CPP_COMMA:
29836 if (depth == 0 && maybe_template_id)
29837 {
29838 /* If we've seen a '<', we might be in a
29839 template-argument-list. Until Core issue 325 is
29840 resolved, we don't know how this situation ought
29841 to be handled, so try to DTRT. We check whether
29842 what comes after the comma is a valid parameter
29843 declaration list. If it is, then the comma ends
29844 the default argument; otherwise the default
29845 argument continues. */
29846 bool error = false;
29847 cp_token *peek;
29848
29849 /* Set ITALP so cp_parser_parameter_declaration_list
29850 doesn't decide to commit to this parse. */
29851 bool saved_italp = parser->in_template_argument_list_p;
29852 parser->in_template_argument_list_p = true;
29853
29854 cp_parser_parse_tentatively (parser);
29855
29856 if (nsdmi)
29857 {
29858 /* Parse declarators until we reach a non-comma or
29859 somthing that cannot be an initializer.
29860 Just checking whether we're looking at a single
29861 declarator is insufficient. Consider:
29862 int var = tuple<T,U>::x;
29863 The template parameter 'U' looks exactly like a
29864 declarator. */
29865 do
29866 {
29867 int ctor_dtor_or_conv_p;
29868 cp_lexer_consume_token (parser->lexer);
29869 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29870 CP_PARSER_FLAGS_NONE,
29871 &ctor_dtor_or_conv_p,
29872 /*parenthesized_p=*/NULL,
29873 /*member_p=*/true,
29874 /*friend_p=*/false,
29875 /*static_p=*/false);
29876 peek = cp_lexer_peek_token (parser->lexer);
29877 if (cp_parser_error_occurred (parser))
29878 break;
29879 }
29880 while (peek->type == CPP_COMMA);
29881 /* If we met an '=' or ';' then the original comma
29882 was the end of the NSDMI. Otherwise assume
29883 we're still in the NSDMI. */
29884 error = (peek->type != CPP_EQ
29885 && peek->type != CPP_SEMICOLON);
29886 }
29887 else
29888 {
29889 cp_lexer_consume_token (parser->lexer);
29890 begin_scope (sk_function_parms, NULL_TREE);
29891 tree t = cp_parser_parameter_declaration_list
29892 (parser, CP_PARSER_FLAGS_NONE);
29893 if (t == error_mark_node)
29894 error = true;
29895 pop_bindings_and_leave_scope ();
29896 }
29897 if (!cp_parser_error_occurred (parser) && !error)
29898 done = true;
29899 cp_parser_abort_tentative_parse (parser);
29900
29901 parser->in_template_argument_list_p = saved_italp;
29902 break;
29903 }
29904 /* FALLTHRU */
29905 case CPP_CLOSE_PAREN:
29906 case CPP_ELLIPSIS:
29907 /* If we run into a non-nested `;', `}', or `]',
29908 then the code is invalid -- but the default
29909 argument is certainly over. */
29910 case CPP_SEMICOLON:
29911 case CPP_CLOSE_BRACE:
29912 case CPP_CLOSE_SQUARE:
29913 if (depth == 0
29914 /* Handle correctly int n = sizeof ... ( p ); */
29915 && token->type != CPP_ELLIPSIS)
29916 done = true;
29917 /* Update DEPTH, if necessary. */
29918 else if (token->type == CPP_CLOSE_PAREN
29919 || token->type == CPP_CLOSE_BRACE
29920 || token->type == CPP_CLOSE_SQUARE)
29921 --depth;
29922 break;
29923
29924 case CPP_OPEN_PAREN:
29925 case CPP_OPEN_SQUARE:
29926 case CPP_OPEN_BRACE:
29927 ++depth;
29928 break;
29929
29930 case CPP_LESS:
29931 if (depth == 0)
29932 /* This might be the comparison operator, or it might
29933 start a template argument list. */
29934 ++maybe_template_id;
29935 break;
29936
29937 case CPP_RSHIFT:
29938 if (cxx_dialect == cxx98)
29939 break;
29940 /* Fall through for C++0x, which treats the `>>'
29941 operator like two `>' tokens in certain
29942 cases. */
29943 gcc_fallthrough ();
29944
29945 case CPP_GREATER:
29946 if (depth == 0)
29947 {
29948 /* This might be an operator, or it might close a
29949 template argument list. But if a previous '<'
29950 started a template argument list, this will have
29951 closed it, so we can't be in one anymore. */
29952 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29953 if (maybe_template_id < 0)
29954 maybe_template_id = 0;
29955 }
29956 break;
29957
29958 /* If we run out of tokens, issue an error message. */
29959 case CPP_EOF:
29960 case CPP_PRAGMA_EOL:
29961 error_at (token->location, "file ends in default argument");
29962 return error_mark_node;
29963
29964 case CPP_NAME:
29965 case CPP_SCOPE:
29966 /* In these cases, we should look for template-ids.
29967 For example, if the default argument is
29968 `X<int, double>()', we need to do name lookup to
29969 figure out whether or not `X' is a template; if
29970 so, the `,' does not end the default argument.
29971
29972 That is not yet done. */
29973 break;
29974
29975 default:
29976 break;
29977 }
29978
29979 /* If we've reached the end, stop. */
29980 if (done)
29981 break;
29982
29983 /* Add the token to the token block. */
29984 token = cp_lexer_consume_token (parser->lexer);
29985 }
29986
29987 /* Create a DEFAULT_ARG to represent the unparsed default
29988 argument. */
29989 default_argument = make_node (DEFAULT_ARG);
29990 DEFARG_TOKENS (default_argument)
29991 = cp_token_cache_new (first_token, token);
29992 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29993
29994 return default_argument;
29995 }
29996
29997 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29998
29999 location_t
30000 defarg_location (tree default_argument)
30001 {
30002 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
30003 location_t start = tokens->first->location;
30004 location_t end = tokens->last->location;
30005 return make_location (start, start, end);
30006 }
30007
30008 /* Begin parsing tentatively. We always save tokens while parsing
30009 tentatively so that if the tentative parsing fails we can restore the
30010 tokens. */
30011
30012 static void
30013 cp_parser_parse_tentatively (cp_parser* parser)
30014 {
30015 /* Enter a new parsing context. */
30016 parser->context = cp_parser_context_new (parser->context);
30017 /* Begin saving tokens. */
30018 cp_lexer_save_tokens (parser->lexer);
30019 /* In order to avoid repetitive access control error messages,
30020 access checks are queued up until we are no longer parsing
30021 tentatively. */
30022 push_deferring_access_checks (dk_deferred);
30023 }
30024
30025 /* Commit to the currently active tentative parse. */
30026
30027 static void
30028 cp_parser_commit_to_tentative_parse (cp_parser* parser)
30029 {
30030 cp_parser_context *context;
30031 cp_lexer *lexer;
30032
30033 /* Mark all of the levels as committed. */
30034 lexer = parser->lexer;
30035 for (context = parser->context; context->next; context = context->next)
30036 {
30037 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
30038 break;
30039 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
30040 while (!cp_lexer_saving_tokens (lexer))
30041 lexer = lexer->next;
30042 cp_lexer_commit_tokens (lexer);
30043 }
30044 }
30045
30046 /* Commit to the topmost currently active tentative parse.
30047
30048 Note that this function shouldn't be called when there are
30049 irreversible side-effects while in a tentative state. For
30050 example, we shouldn't create a permanent entry in the symbol
30051 table, or issue an error message that might not apply if the
30052 tentative parse is aborted. */
30053
30054 static void
30055 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
30056 {
30057 cp_parser_context *context = parser->context;
30058 cp_lexer *lexer = parser->lexer;
30059
30060 if (context)
30061 {
30062 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
30063 return;
30064 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
30065
30066 while (!cp_lexer_saving_tokens (lexer))
30067 lexer = lexer->next;
30068 cp_lexer_commit_tokens (lexer);
30069 }
30070 }
30071
30072 /* Abort the currently active tentative parse. All consumed tokens
30073 will be rolled back, and no diagnostics will be issued. */
30074
30075 static void
30076 cp_parser_abort_tentative_parse (cp_parser* parser)
30077 {
30078 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
30079 || errorcount > 0);
30080 cp_parser_simulate_error (parser);
30081 /* Now, pretend that we want to see if the construct was
30082 successfully parsed. */
30083 cp_parser_parse_definitely (parser);
30084 }
30085
30086 /* Stop parsing tentatively. If a parse error has occurred, restore the
30087 token stream. Otherwise, commit to the tokens we have consumed.
30088 Returns true if no error occurred; false otherwise. */
30089
30090 static bool
30091 cp_parser_parse_definitely (cp_parser* parser)
30092 {
30093 bool error_occurred;
30094 cp_parser_context *context;
30095
30096 /* Remember whether or not an error occurred, since we are about to
30097 destroy that information. */
30098 error_occurred = cp_parser_error_occurred (parser);
30099 /* Remove the topmost context from the stack. */
30100 context = parser->context;
30101 parser->context = context->next;
30102 /* If no parse errors occurred, commit to the tentative parse. */
30103 if (!error_occurred)
30104 {
30105 /* Commit to the tokens read tentatively, unless that was
30106 already done. */
30107 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
30108 cp_lexer_commit_tokens (parser->lexer);
30109
30110 pop_to_parent_deferring_access_checks ();
30111 }
30112 /* Otherwise, if errors occurred, roll back our state so that things
30113 are just as they were before we began the tentative parse. */
30114 else
30115 {
30116 cp_lexer_rollback_tokens (parser->lexer);
30117 pop_deferring_access_checks ();
30118 }
30119 /* Add the context to the front of the free list. */
30120 context->next = cp_parser_context_free_list;
30121 cp_parser_context_free_list = context;
30122
30123 return !error_occurred;
30124 }
30125
30126 /* Returns true if we are parsing tentatively and are not committed to
30127 this tentative parse. */
30128
30129 static bool
30130 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
30131 {
30132 return (cp_parser_parsing_tentatively (parser)
30133 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
30134 }
30135
30136 /* Returns nonzero iff an error has occurred during the most recent
30137 tentative parse. */
30138
30139 static bool
30140 cp_parser_error_occurred (cp_parser* parser)
30141 {
30142 return (cp_parser_parsing_tentatively (parser)
30143 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
30144 }
30145
30146 /* Returns nonzero if GNU extensions are allowed. */
30147
30148 static bool
30149 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
30150 {
30151 return parser->allow_gnu_extensions_p;
30152 }
30153 \f
30154 /* Objective-C++ Productions */
30155
30156
30157 /* Parse an Objective-C expression, which feeds into a primary-expression
30158 above.
30159
30160 objc-expression:
30161 objc-message-expression
30162 objc-string-literal
30163 objc-encode-expression
30164 objc-protocol-expression
30165 objc-selector-expression
30166
30167 Returns a tree representation of the expression. */
30168
30169 static cp_expr
30170 cp_parser_objc_expression (cp_parser* parser)
30171 {
30172 /* Try to figure out what kind of declaration is present. */
30173 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30174
30175 switch (kwd->type)
30176 {
30177 case CPP_OPEN_SQUARE:
30178 return cp_parser_objc_message_expression (parser);
30179
30180 case CPP_OBJC_STRING:
30181 kwd = cp_lexer_consume_token (parser->lexer);
30182 return objc_build_string_object (kwd->u.value);
30183
30184 case CPP_KEYWORD:
30185 switch (kwd->keyword)
30186 {
30187 case RID_AT_ENCODE:
30188 return cp_parser_objc_encode_expression (parser);
30189
30190 case RID_AT_PROTOCOL:
30191 return cp_parser_objc_protocol_expression (parser);
30192
30193 case RID_AT_SELECTOR:
30194 return cp_parser_objc_selector_expression (parser);
30195
30196 default:
30197 break;
30198 }
30199 /* FALLTHRU */
30200 default:
30201 error_at (kwd->location,
30202 "misplaced %<@%D%> Objective-C++ construct",
30203 kwd->u.value);
30204 cp_parser_skip_to_end_of_block_or_statement (parser);
30205 }
30206
30207 return error_mark_node;
30208 }
30209
30210 /* Parse an Objective-C message expression.
30211
30212 objc-message-expression:
30213 [ objc-message-receiver objc-message-args ]
30214
30215 Returns a representation of an Objective-C message. */
30216
30217 static tree
30218 cp_parser_objc_message_expression (cp_parser* parser)
30219 {
30220 tree receiver, messageargs;
30221
30222 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30223 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
30224 receiver = cp_parser_objc_message_receiver (parser);
30225 messageargs = cp_parser_objc_message_args (parser);
30226 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
30227 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30228
30229 tree result = objc_build_message_expr (receiver, messageargs);
30230
30231 /* Construct a location e.g.
30232 [self func1:5]
30233 ^~~~~~~~~~~~~~
30234 ranging from the '[' to the ']', with the caret at the start. */
30235 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
30236 protected_set_expr_location (result, combined_loc);
30237
30238 return result;
30239 }
30240
30241 /* Parse an objc-message-receiver.
30242
30243 objc-message-receiver:
30244 expression
30245 simple-type-specifier
30246
30247 Returns a representation of the type or expression. */
30248
30249 static tree
30250 cp_parser_objc_message_receiver (cp_parser* parser)
30251 {
30252 tree rcv;
30253
30254 /* An Objective-C message receiver may be either (1) a type
30255 or (2) an expression. */
30256 cp_parser_parse_tentatively (parser);
30257 rcv = cp_parser_expression (parser);
30258
30259 /* If that worked out, fine. */
30260 if (cp_parser_parse_definitely (parser))
30261 return rcv;
30262
30263 cp_parser_parse_tentatively (parser);
30264 rcv = cp_parser_simple_type_specifier (parser,
30265 /*decl_specs=*/NULL,
30266 CP_PARSER_FLAGS_NONE);
30267
30268 if (cp_parser_parse_definitely (parser))
30269 return objc_get_class_reference (rcv);
30270
30271 cp_parser_error (parser, "objective-c++ message receiver expected");
30272 return error_mark_node;
30273 }
30274
30275 /* Parse the arguments and selectors comprising an Objective-C message.
30276
30277 objc-message-args:
30278 objc-selector
30279 objc-selector-args
30280 objc-selector-args , objc-comma-args
30281
30282 objc-selector-args:
30283 objc-selector [opt] : assignment-expression
30284 objc-selector-args objc-selector [opt] : assignment-expression
30285
30286 objc-comma-args:
30287 assignment-expression
30288 objc-comma-args , assignment-expression
30289
30290 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30291 selector arguments and TREE_VALUE containing a list of comma
30292 arguments. */
30293
30294 static tree
30295 cp_parser_objc_message_args (cp_parser* parser)
30296 {
30297 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
30298 bool maybe_unary_selector_p = true;
30299 cp_token *token = cp_lexer_peek_token (parser->lexer);
30300
30301 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30302 {
30303 tree selector = NULL_TREE, arg;
30304
30305 if (token->type != CPP_COLON)
30306 selector = cp_parser_objc_selector (parser);
30307
30308 /* Detect if we have a unary selector. */
30309 if (maybe_unary_selector_p
30310 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30311 return build_tree_list (selector, NULL_TREE);
30312
30313 maybe_unary_selector_p = false;
30314 cp_parser_require (parser, CPP_COLON, RT_COLON);
30315 arg = cp_parser_assignment_expression (parser);
30316
30317 sel_args
30318 = chainon (sel_args,
30319 build_tree_list (selector, arg));
30320
30321 token = cp_lexer_peek_token (parser->lexer);
30322 }
30323
30324 /* Handle non-selector arguments, if any. */
30325 while (token->type == CPP_COMMA)
30326 {
30327 tree arg;
30328
30329 cp_lexer_consume_token (parser->lexer);
30330 arg = cp_parser_assignment_expression (parser);
30331
30332 addl_args
30333 = chainon (addl_args,
30334 build_tree_list (NULL_TREE, arg));
30335
30336 token = cp_lexer_peek_token (parser->lexer);
30337 }
30338
30339 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
30340 {
30341 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
30342 return build_tree_list (error_mark_node, error_mark_node);
30343 }
30344
30345 return build_tree_list (sel_args, addl_args);
30346 }
30347
30348 /* Parse an Objective-C encode expression.
30349
30350 objc-encode-expression:
30351 @encode objc-typename
30352
30353 Returns an encoded representation of the type argument. */
30354
30355 static cp_expr
30356 cp_parser_objc_encode_expression (cp_parser* parser)
30357 {
30358 tree type;
30359 cp_token *token;
30360 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30361
30362 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
30363 matching_parens parens;
30364 parens.require_open (parser);
30365 token = cp_lexer_peek_token (parser->lexer);
30366 type = complete_type (cp_parser_type_id (parser));
30367 parens.require_close (parser);
30368
30369 if (!type)
30370 {
30371 error_at (token->location,
30372 "%<@encode%> must specify a type as an argument");
30373 return error_mark_node;
30374 }
30375
30376 /* This happens if we find @encode(T) (where T is a template
30377 typename or something dependent on a template typename) when
30378 parsing a template. In that case, we can't compile it
30379 immediately, but we rather create an AT_ENCODE_EXPR which will
30380 need to be instantiated when the template is used.
30381 */
30382 if (dependent_type_p (type))
30383 {
30384 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
30385 TREE_READONLY (value) = 1;
30386 return value;
30387 }
30388
30389
30390 /* Build a location of the form:
30391 @encode(int)
30392 ^~~~~~~~~~~~
30393 with caret==start at the @ token, finishing at the close paren. */
30394 location_t combined_loc
30395 = make_location (start_loc, start_loc,
30396 cp_lexer_previous_token (parser->lexer)->location);
30397
30398 return cp_expr (objc_build_encode_expr (type), combined_loc);
30399 }
30400
30401 /* Parse an Objective-C @defs expression. */
30402
30403 static tree
30404 cp_parser_objc_defs_expression (cp_parser *parser)
30405 {
30406 tree name;
30407
30408 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
30409 matching_parens parens;
30410 parens.require_open (parser);
30411 name = cp_parser_identifier (parser);
30412 parens.require_close (parser);
30413
30414 return objc_get_class_ivars (name);
30415 }
30416
30417 /* Parse an Objective-C protocol expression.
30418
30419 objc-protocol-expression:
30420 @protocol ( identifier )
30421
30422 Returns a representation of the protocol expression. */
30423
30424 static tree
30425 cp_parser_objc_protocol_expression (cp_parser* parser)
30426 {
30427 tree proto;
30428 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30429
30430 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30431 matching_parens parens;
30432 parens.require_open (parser);
30433 proto = cp_parser_identifier (parser);
30434 parens.require_close (parser);
30435
30436 /* Build a location of the form:
30437 @protocol(prot)
30438 ^~~~~~~~~~~~~~~
30439 with caret==start at the @ token, finishing at the close paren. */
30440 location_t combined_loc
30441 = make_location (start_loc, start_loc,
30442 cp_lexer_previous_token (parser->lexer)->location);
30443 tree result = objc_build_protocol_expr (proto);
30444 protected_set_expr_location (result, combined_loc);
30445 return result;
30446 }
30447
30448 /* Parse an Objective-C selector expression.
30449
30450 objc-selector-expression:
30451 @selector ( objc-method-signature )
30452
30453 objc-method-signature:
30454 objc-selector
30455 objc-selector-seq
30456
30457 objc-selector-seq:
30458 objc-selector :
30459 objc-selector-seq objc-selector :
30460
30461 Returns a representation of the method selector. */
30462
30463 static tree
30464 cp_parser_objc_selector_expression (cp_parser* parser)
30465 {
30466 tree sel_seq = NULL_TREE;
30467 bool maybe_unary_selector_p = true;
30468 cp_token *token;
30469 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30470
30471 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
30472 matching_parens parens;
30473 parens.require_open (parser);
30474 token = cp_lexer_peek_token (parser->lexer);
30475
30476 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
30477 || token->type == CPP_SCOPE)
30478 {
30479 tree selector = NULL_TREE;
30480
30481 if (token->type != CPP_COLON
30482 || token->type == CPP_SCOPE)
30483 selector = cp_parser_objc_selector (parser);
30484
30485 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
30486 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
30487 {
30488 /* Detect if we have a unary selector. */
30489 if (maybe_unary_selector_p)
30490 {
30491 sel_seq = selector;
30492 goto finish_selector;
30493 }
30494 else
30495 {
30496 cp_parser_error (parser, "expected %<:%>");
30497 }
30498 }
30499 maybe_unary_selector_p = false;
30500 token = cp_lexer_consume_token (parser->lexer);
30501
30502 if (token->type == CPP_SCOPE)
30503 {
30504 sel_seq
30505 = chainon (sel_seq,
30506 build_tree_list (selector, NULL_TREE));
30507 sel_seq
30508 = chainon (sel_seq,
30509 build_tree_list (NULL_TREE, NULL_TREE));
30510 }
30511 else
30512 sel_seq
30513 = chainon (sel_seq,
30514 build_tree_list (selector, NULL_TREE));
30515
30516 token = cp_lexer_peek_token (parser->lexer);
30517 }
30518
30519 finish_selector:
30520 parens.require_close (parser);
30521
30522
30523 /* Build a location of the form:
30524 @selector(func)
30525 ^~~~~~~~~~~~~~~
30526 with caret==start at the @ token, finishing at the close paren. */
30527 location_t combined_loc
30528 = make_location (loc, loc,
30529 cp_lexer_previous_token (parser->lexer)->location);
30530 tree result = objc_build_selector_expr (combined_loc, sel_seq);
30531 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30532 protected_set_expr_location (result, combined_loc);
30533 return result;
30534 }
30535
30536 /* Parse a list of identifiers.
30537
30538 objc-identifier-list:
30539 identifier
30540 objc-identifier-list , identifier
30541
30542 Returns a TREE_LIST of identifier nodes. */
30543
30544 static tree
30545 cp_parser_objc_identifier_list (cp_parser* parser)
30546 {
30547 tree identifier;
30548 tree list;
30549 cp_token *sep;
30550
30551 identifier = cp_parser_identifier (parser);
30552 if (identifier == error_mark_node)
30553 return error_mark_node;
30554
30555 list = build_tree_list (NULL_TREE, identifier);
30556 sep = cp_lexer_peek_token (parser->lexer);
30557
30558 while (sep->type == CPP_COMMA)
30559 {
30560 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30561 identifier = cp_parser_identifier (parser);
30562 if (identifier == error_mark_node)
30563 return list;
30564
30565 list = chainon (list, build_tree_list (NULL_TREE,
30566 identifier));
30567 sep = cp_lexer_peek_token (parser->lexer);
30568 }
30569
30570 return list;
30571 }
30572
30573 /* Parse an Objective-C alias declaration.
30574
30575 objc-alias-declaration:
30576 @compatibility_alias identifier identifier ;
30577
30578 This function registers the alias mapping with the Objective-C front end.
30579 It returns nothing. */
30580
30581 static void
30582 cp_parser_objc_alias_declaration (cp_parser* parser)
30583 {
30584 tree alias, orig;
30585
30586 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
30587 alias = cp_parser_identifier (parser);
30588 orig = cp_parser_identifier (parser);
30589 objc_declare_alias (alias, orig);
30590 cp_parser_consume_semicolon_at_end_of_statement (parser);
30591 }
30592
30593 /* Parse an Objective-C class forward-declaration.
30594
30595 objc-class-declaration:
30596 @class objc-identifier-list ;
30597
30598 The function registers the forward declarations with the Objective-C
30599 front end. It returns nothing. */
30600
30601 static void
30602 cp_parser_objc_class_declaration (cp_parser* parser)
30603 {
30604 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
30605 while (true)
30606 {
30607 tree id;
30608
30609 id = cp_parser_identifier (parser);
30610 if (id == error_mark_node)
30611 break;
30612
30613 objc_declare_class (id);
30614
30615 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30616 cp_lexer_consume_token (parser->lexer);
30617 else
30618 break;
30619 }
30620 cp_parser_consume_semicolon_at_end_of_statement (parser);
30621 }
30622
30623 /* Parse a list of Objective-C protocol references.
30624
30625 objc-protocol-refs-opt:
30626 objc-protocol-refs [opt]
30627
30628 objc-protocol-refs:
30629 < objc-identifier-list >
30630
30631 Returns a TREE_LIST of identifiers, if any. */
30632
30633 static tree
30634 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30635 {
30636 tree protorefs = NULL_TREE;
30637
30638 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30639 {
30640 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30641 protorefs = cp_parser_objc_identifier_list (parser);
30642 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30643 }
30644
30645 return protorefs;
30646 }
30647
30648 /* Parse a Objective-C visibility specification. */
30649
30650 static void
30651 cp_parser_objc_visibility_spec (cp_parser* parser)
30652 {
30653 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30654
30655 switch (vis->keyword)
30656 {
30657 case RID_AT_PRIVATE:
30658 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30659 break;
30660 case RID_AT_PROTECTED:
30661 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30662 break;
30663 case RID_AT_PUBLIC:
30664 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30665 break;
30666 case RID_AT_PACKAGE:
30667 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30668 break;
30669 default:
30670 return;
30671 }
30672
30673 /* Eat '@private'/'@protected'/'@public'. */
30674 cp_lexer_consume_token (parser->lexer);
30675 }
30676
30677 /* Parse an Objective-C method type. Return 'true' if it is a class
30678 (+) method, and 'false' if it is an instance (-) method. */
30679
30680 static inline bool
30681 cp_parser_objc_method_type (cp_parser* parser)
30682 {
30683 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30684 return true;
30685 else
30686 return false;
30687 }
30688
30689 /* Parse an Objective-C protocol qualifier. */
30690
30691 static tree
30692 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30693 {
30694 tree quals = NULL_TREE, node;
30695 cp_token *token = cp_lexer_peek_token (parser->lexer);
30696
30697 node = token->u.value;
30698
30699 while (node && identifier_p (node)
30700 && (node == ridpointers [(int) RID_IN]
30701 || node == ridpointers [(int) RID_OUT]
30702 || node == ridpointers [(int) RID_INOUT]
30703 || node == ridpointers [(int) RID_BYCOPY]
30704 || node == ridpointers [(int) RID_BYREF]
30705 || node == ridpointers [(int) RID_ONEWAY]))
30706 {
30707 quals = tree_cons (NULL_TREE, node, quals);
30708 cp_lexer_consume_token (parser->lexer);
30709 token = cp_lexer_peek_token (parser->lexer);
30710 node = token->u.value;
30711 }
30712
30713 return quals;
30714 }
30715
30716 /* Parse an Objective-C typename. */
30717
30718 static tree
30719 cp_parser_objc_typename (cp_parser* parser)
30720 {
30721 tree type_name = NULL_TREE;
30722
30723 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30724 {
30725 tree proto_quals, cp_type = NULL_TREE;
30726
30727 matching_parens parens;
30728 parens.consume_open (parser); /* Eat '('. */
30729 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30730
30731 /* An ObjC type name may consist of just protocol qualifiers, in which
30732 case the type shall default to 'id'. */
30733 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30734 {
30735 cp_type = cp_parser_type_id (parser);
30736
30737 /* If the type could not be parsed, an error has already
30738 been produced. For error recovery, behave as if it had
30739 not been specified, which will use the default type
30740 'id'. */
30741 if (cp_type == error_mark_node)
30742 {
30743 cp_type = NULL_TREE;
30744 /* We need to skip to the closing parenthesis as
30745 cp_parser_type_id() does not seem to do it for
30746 us. */
30747 cp_parser_skip_to_closing_parenthesis (parser,
30748 /*recovering=*/true,
30749 /*or_comma=*/false,
30750 /*consume_paren=*/false);
30751 }
30752 }
30753
30754 parens.require_close (parser);
30755 type_name = build_tree_list (proto_quals, cp_type);
30756 }
30757
30758 return type_name;
30759 }
30760
30761 /* Check to see if TYPE refers to an Objective-C selector name. */
30762
30763 static bool
30764 cp_parser_objc_selector_p (enum cpp_ttype type)
30765 {
30766 return (type == CPP_NAME || type == CPP_KEYWORD
30767 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30768 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30769 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30770 || type == CPP_XOR || type == CPP_XOR_EQ);
30771 }
30772
30773 /* Parse an Objective-C selector. */
30774
30775 static tree
30776 cp_parser_objc_selector (cp_parser* parser)
30777 {
30778 cp_token *token = cp_lexer_consume_token (parser->lexer);
30779
30780 if (!cp_parser_objc_selector_p (token->type))
30781 {
30782 error_at (token->location, "invalid Objective-C++ selector name");
30783 return error_mark_node;
30784 }
30785
30786 /* C++ operator names are allowed to appear in ObjC selectors. */
30787 switch (token->type)
30788 {
30789 case CPP_AND_AND: return get_identifier ("and");
30790 case CPP_AND_EQ: return get_identifier ("and_eq");
30791 case CPP_AND: return get_identifier ("bitand");
30792 case CPP_OR: return get_identifier ("bitor");
30793 case CPP_COMPL: return get_identifier ("compl");
30794 case CPP_NOT: return get_identifier ("not");
30795 case CPP_NOT_EQ: return get_identifier ("not_eq");
30796 case CPP_OR_OR: return get_identifier ("or");
30797 case CPP_OR_EQ: return get_identifier ("or_eq");
30798 case CPP_XOR: return get_identifier ("xor");
30799 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30800 default: return token->u.value;
30801 }
30802 }
30803
30804 /* Parse an Objective-C params list. */
30805
30806 static tree
30807 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30808 {
30809 tree params = NULL_TREE;
30810 bool maybe_unary_selector_p = true;
30811 cp_token *token = cp_lexer_peek_token (parser->lexer);
30812
30813 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30814 {
30815 tree selector = NULL_TREE, type_name, identifier;
30816 tree parm_attr = NULL_TREE;
30817
30818 if (token->keyword == RID_ATTRIBUTE)
30819 break;
30820
30821 if (token->type != CPP_COLON)
30822 selector = cp_parser_objc_selector (parser);
30823
30824 /* Detect if we have a unary selector. */
30825 if (maybe_unary_selector_p
30826 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30827 {
30828 params = selector; /* Might be followed by attributes. */
30829 break;
30830 }
30831
30832 maybe_unary_selector_p = false;
30833 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30834 {
30835 /* Something went quite wrong. There should be a colon
30836 here, but there is not. Stop parsing parameters. */
30837 break;
30838 }
30839 type_name = cp_parser_objc_typename (parser);
30840 /* New ObjC allows attributes on parameters too. */
30841 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30842 parm_attr = cp_parser_attributes_opt (parser);
30843 identifier = cp_parser_identifier (parser);
30844
30845 params
30846 = chainon (params,
30847 objc_build_keyword_decl (selector,
30848 type_name,
30849 identifier,
30850 parm_attr));
30851
30852 token = cp_lexer_peek_token (parser->lexer);
30853 }
30854
30855 if (params == NULL_TREE)
30856 {
30857 cp_parser_error (parser, "objective-c++ method declaration is expected");
30858 return error_mark_node;
30859 }
30860
30861 /* We allow tail attributes for the method. */
30862 if (token->keyword == RID_ATTRIBUTE)
30863 {
30864 *attributes = cp_parser_attributes_opt (parser);
30865 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30866 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30867 return params;
30868 cp_parser_error (parser,
30869 "method attributes must be specified at the end");
30870 return error_mark_node;
30871 }
30872
30873 if (params == NULL_TREE)
30874 {
30875 cp_parser_error (parser, "objective-c++ method declaration is expected");
30876 return error_mark_node;
30877 }
30878 return params;
30879 }
30880
30881 /* Parse the non-keyword Objective-C params. */
30882
30883 static tree
30884 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30885 tree* attributes)
30886 {
30887 tree params = make_node (TREE_LIST);
30888 cp_token *token = cp_lexer_peek_token (parser->lexer);
30889 *ellipsisp = false; /* Initially, assume no ellipsis. */
30890
30891 while (token->type == CPP_COMMA)
30892 {
30893 cp_parameter_declarator *parmdecl;
30894 tree parm;
30895
30896 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30897 token = cp_lexer_peek_token (parser->lexer);
30898
30899 if (token->type == CPP_ELLIPSIS)
30900 {
30901 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30902 *ellipsisp = true;
30903 token = cp_lexer_peek_token (parser->lexer);
30904 break;
30905 }
30906
30907 /* TODO: parse attributes for tail parameters. */
30908 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
30909 false, NULL);
30910 parm = grokdeclarator (parmdecl->declarator,
30911 &parmdecl->decl_specifiers,
30912 PARM, /*initialized=*/0,
30913 /*attrlist=*/NULL);
30914
30915 chainon (params, build_tree_list (NULL_TREE, parm));
30916 token = cp_lexer_peek_token (parser->lexer);
30917 }
30918
30919 /* We allow tail attributes for the method. */
30920 if (token->keyword == RID_ATTRIBUTE)
30921 {
30922 if (*attributes == NULL_TREE)
30923 {
30924 *attributes = cp_parser_attributes_opt (parser);
30925 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30926 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30927 return params;
30928 }
30929 else
30930 /* We have an error, but parse the attributes, so that we can
30931 carry on. */
30932 *attributes = cp_parser_attributes_opt (parser);
30933
30934 cp_parser_error (parser,
30935 "method attributes must be specified at the end");
30936 return error_mark_node;
30937 }
30938
30939 return params;
30940 }
30941
30942 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30943
30944 static void
30945 cp_parser_objc_interstitial_code (cp_parser* parser)
30946 {
30947 cp_token *token = cp_lexer_peek_token (parser->lexer);
30948
30949 /* If the next token is `extern' and the following token is a string
30950 literal, then we have a linkage specification. */
30951 if (token->keyword == RID_EXTERN
30952 && cp_parser_is_pure_string_literal
30953 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30954 cp_parser_linkage_specification (parser);
30955 /* Handle #pragma, if any. */
30956 else if (token->type == CPP_PRAGMA)
30957 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30958 /* Allow stray semicolons. */
30959 else if (token->type == CPP_SEMICOLON)
30960 cp_lexer_consume_token (parser->lexer);
30961 /* Mark methods as optional or required, when building protocols. */
30962 else if (token->keyword == RID_AT_OPTIONAL)
30963 {
30964 cp_lexer_consume_token (parser->lexer);
30965 objc_set_method_opt (true);
30966 }
30967 else if (token->keyword == RID_AT_REQUIRED)
30968 {
30969 cp_lexer_consume_token (parser->lexer);
30970 objc_set_method_opt (false);
30971 }
30972 else if (token->keyword == RID_NAMESPACE)
30973 cp_parser_namespace_definition (parser);
30974 /* Other stray characters must generate errors. */
30975 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30976 {
30977 cp_lexer_consume_token (parser->lexer);
30978 error ("stray %qs between Objective-C++ methods",
30979 token->type == CPP_OPEN_BRACE ? "{" : "}");
30980 }
30981 /* Finally, try to parse a block-declaration, or a function-definition. */
30982 else
30983 cp_parser_block_declaration (parser, /*statement_p=*/false);
30984 }
30985
30986 /* Parse a method signature. */
30987
30988 static tree
30989 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30990 {
30991 tree rettype, kwdparms, optparms;
30992 bool ellipsis = false;
30993 bool is_class_method;
30994
30995 is_class_method = cp_parser_objc_method_type (parser);
30996 rettype = cp_parser_objc_typename (parser);
30997 *attributes = NULL_TREE;
30998 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30999 if (kwdparms == error_mark_node)
31000 return error_mark_node;
31001 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
31002 if (optparms == error_mark_node)
31003 return error_mark_node;
31004
31005 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
31006 }
31007
31008 static bool
31009 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
31010 {
31011 tree tattr;
31012 cp_lexer_save_tokens (parser->lexer);
31013 tattr = cp_parser_attributes_opt (parser);
31014 gcc_assert (tattr) ;
31015
31016 /* If the attributes are followed by a method introducer, this is not allowed.
31017 Dump the attributes and flag the situation. */
31018 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
31019 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31020 return true;
31021
31022 /* Otherwise, the attributes introduce some interstitial code, possibly so
31023 rewind to allow that check. */
31024 cp_lexer_rollback_tokens (parser->lexer);
31025 return false;
31026 }
31027
31028 /* Parse an Objective-C method prototype list. */
31029
31030 static void
31031 cp_parser_objc_method_prototype_list (cp_parser* parser)
31032 {
31033 cp_token *token = cp_lexer_peek_token (parser->lexer);
31034
31035 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31036 {
31037 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31038 {
31039 tree attributes, sig;
31040 bool is_class_method;
31041 if (token->type == CPP_PLUS)
31042 is_class_method = true;
31043 else
31044 is_class_method = false;
31045 sig = cp_parser_objc_method_signature (parser, &attributes);
31046 if (sig == error_mark_node)
31047 {
31048 cp_parser_skip_to_end_of_block_or_statement (parser);
31049 token = cp_lexer_peek_token (parser->lexer);
31050 continue;
31051 }
31052 objc_add_method_declaration (is_class_method, sig, attributes);
31053 cp_parser_consume_semicolon_at_end_of_statement (parser);
31054 }
31055 else if (token->keyword == RID_AT_PROPERTY)
31056 cp_parser_objc_at_property_declaration (parser);
31057 else if (token->keyword == RID_ATTRIBUTE
31058 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31059 warning_at (cp_lexer_peek_token (parser->lexer)->location,
31060 OPT_Wattributes,
31061 "prefix attributes are ignored for methods");
31062 else
31063 /* Allow for interspersed non-ObjC++ code. */
31064 cp_parser_objc_interstitial_code (parser);
31065
31066 token = cp_lexer_peek_token (parser->lexer);
31067 }
31068
31069 if (token->type != CPP_EOF)
31070 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31071 else
31072 cp_parser_error (parser, "expected %<@end%>");
31073
31074 objc_finish_interface ();
31075 }
31076
31077 /* Parse an Objective-C method definition list. */
31078
31079 static void
31080 cp_parser_objc_method_definition_list (cp_parser* parser)
31081 {
31082 cp_token *token = cp_lexer_peek_token (parser->lexer);
31083
31084 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31085 {
31086 tree meth;
31087
31088 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31089 {
31090 cp_token *ptk;
31091 tree sig, attribute;
31092 bool is_class_method;
31093 if (token->type == CPP_PLUS)
31094 is_class_method = true;
31095 else
31096 is_class_method = false;
31097 push_deferring_access_checks (dk_deferred);
31098 sig = cp_parser_objc_method_signature (parser, &attribute);
31099 if (sig == error_mark_node)
31100 {
31101 cp_parser_skip_to_end_of_block_or_statement (parser);
31102 token = cp_lexer_peek_token (parser->lexer);
31103 continue;
31104 }
31105 objc_start_method_definition (is_class_method, sig, attribute,
31106 NULL_TREE);
31107
31108 /* For historical reasons, we accept an optional semicolon. */
31109 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31110 cp_lexer_consume_token (parser->lexer);
31111
31112 ptk = cp_lexer_peek_token (parser->lexer);
31113 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
31114 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
31115 {
31116 perform_deferred_access_checks (tf_warning_or_error);
31117 stop_deferring_access_checks ();
31118 meth = cp_parser_function_definition_after_declarator (parser,
31119 false);
31120 pop_deferring_access_checks ();
31121 objc_finish_method_definition (meth);
31122 }
31123 }
31124 /* The following case will be removed once @synthesize is
31125 completely implemented. */
31126 else if (token->keyword == RID_AT_PROPERTY)
31127 cp_parser_objc_at_property_declaration (parser);
31128 else if (token->keyword == RID_AT_SYNTHESIZE)
31129 cp_parser_objc_at_synthesize_declaration (parser);
31130 else if (token->keyword == RID_AT_DYNAMIC)
31131 cp_parser_objc_at_dynamic_declaration (parser);
31132 else if (token->keyword == RID_ATTRIBUTE
31133 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31134 warning_at (token->location, OPT_Wattributes,
31135 "prefix attributes are ignored for methods");
31136 else
31137 /* Allow for interspersed non-ObjC++ code. */
31138 cp_parser_objc_interstitial_code (parser);
31139
31140 token = cp_lexer_peek_token (parser->lexer);
31141 }
31142
31143 if (token->type != CPP_EOF)
31144 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31145 else
31146 cp_parser_error (parser, "expected %<@end%>");
31147
31148 objc_finish_implementation ();
31149 }
31150
31151 /* Parse Objective-C ivars. */
31152
31153 static void
31154 cp_parser_objc_class_ivars (cp_parser* parser)
31155 {
31156 cp_token *token = cp_lexer_peek_token (parser->lexer);
31157
31158 if (token->type != CPP_OPEN_BRACE)
31159 return; /* No ivars specified. */
31160
31161 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
31162 token = cp_lexer_peek_token (parser->lexer);
31163
31164 while (token->type != CPP_CLOSE_BRACE
31165 && token->keyword != RID_AT_END && token->type != CPP_EOF)
31166 {
31167 cp_decl_specifier_seq declspecs;
31168 int decl_class_or_enum_p;
31169 tree prefix_attributes;
31170
31171 cp_parser_objc_visibility_spec (parser);
31172
31173 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31174 break;
31175
31176 cp_parser_decl_specifier_seq (parser,
31177 CP_PARSER_FLAGS_OPTIONAL,
31178 &declspecs,
31179 &decl_class_or_enum_p);
31180
31181 /* auto, register, static, extern, mutable. */
31182 if (declspecs.storage_class != sc_none)
31183 {
31184 cp_parser_error (parser, "invalid type for instance variable");
31185 declspecs.storage_class = sc_none;
31186 }
31187
31188 /* thread_local. */
31189 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31190 {
31191 cp_parser_error (parser, "invalid type for instance variable");
31192 declspecs.locations[ds_thread] = 0;
31193 }
31194
31195 /* typedef. */
31196 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31197 {
31198 cp_parser_error (parser, "invalid type for instance variable");
31199 declspecs.locations[ds_typedef] = 0;
31200 }
31201
31202 prefix_attributes = declspecs.attributes;
31203 declspecs.attributes = NULL_TREE;
31204
31205 /* Keep going until we hit the `;' at the end of the
31206 declaration. */
31207 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31208 {
31209 tree width = NULL_TREE, attributes, first_attribute, decl;
31210 cp_declarator *declarator = NULL;
31211 int ctor_dtor_or_conv_p;
31212
31213 /* Check for a (possibly unnamed) bitfield declaration. */
31214 token = cp_lexer_peek_token (parser->lexer);
31215 if (token->type == CPP_COLON)
31216 goto eat_colon;
31217
31218 if (token->type == CPP_NAME
31219 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
31220 == CPP_COLON))
31221 {
31222 /* Get the name of the bitfield. */
31223 declarator = make_id_declarator (NULL_TREE,
31224 cp_parser_identifier (parser),
31225 sfk_none, token->location);
31226
31227 eat_colon:
31228 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31229 /* Get the width of the bitfield. */
31230 width
31231 = cp_parser_constant_expression (parser);
31232 }
31233 else
31234 {
31235 /* Parse the declarator. */
31236 declarator
31237 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31238 CP_PARSER_FLAGS_NONE,
31239 &ctor_dtor_or_conv_p,
31240 /*parenthesized_p=*/NULL,
31241 /*member_p=*/false,
31242 /*friend_p=*/false,
31243 /*static_p=*/false);
31244 }
31245
31246 /* Look for attributes that apply to the ivar. */
31247 attributes = cp_parser_attributes_opt (parser);
31248 /* Remember which attributes are prefix attributes and
31249 which are not. */
31250 first_attribute = attributes;
31251 /* Combine the attributes. */
31252 attributes = attr_chainon (prefix_attributes, attributes);
31253
31254 if (width)
31255 /* Create the bitfield declaration. */
31256 decl = grokbitfield (declarator, &declspecs,
31257 width, NULL_TREE, attributes);
31258 else
31259 decl = grokfield (declarator, &declspecs,
31260 NULL_TREE, /*init_const_expr_p=*/false,
31261 NULL_TREE, attributes);
31262
31263 /* Add the instance variable. */
31264 if (decl != error_mark_node && decl != NULL_TREE)
31265 objc_add_instance_variable (decl);
31266
31267 /* Reset PREFIX_ATTRIBUTES. */
31268 if (attributes != error_mark_node)
31269 {
31270 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31271 attributes = TREE_CHAIN (attributes);
31272 if (attributes)
31273 TREE_CHAIN (attributes) = NULL_TREE;
31274 }
31275
31276 token = cp_lexer_peek_token (parser->lexer);
31277
31278 if (token->type == CPP_COMMA)
31279 {
31280 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31281 continue;
31282 }
31283 break;
31284 }
31285
31286 cp_parser_consume_semicolon_at_end_of_statement (parser);
31287 token = cp_lexer_peek_token (parser->lexer);
31288 }
31289
31290 if (token->keyword == RID_AT_END)
31291 cp_parser_error (parser, "expected %<}%>");
31292
31293 /* Do not consume the RID_AT_END, so it will be read again as terminating
31294 the @interface of @implementation. */
31295 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
31296 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
31297
31298 /* For historical reasons, we accept an optional semicolon. */
31299 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31300 cp_lexer_consume_token (parser->lexer);
31301 }
31302
31303 /* Parse an Objective-C protocol declaration. */
31304
31305 static void
31306 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
31307 {
31308 tree proto, protorefs;
31309 cp_token *tok;
31310
31311 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
31312 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31313 {
31314 tok = cp_lexer_peek_token (parser->lexer);
31315 error_at (tok->location, "identifier expected after %<@protocol%>");
31316 cp_parser_consume_semicolon_at_end_of_statement (parser);
31317 return;
31318 }
31319
31320 /* See if we have a forward declaration or a definition. */
31321 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
31322
31323 /* Try a forward declaration first. */
31324 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
31325 {
31326 while (true)
31327 {
31328 tree id;
31329
31330 id = cp_parser_identifier (parser);
31331 if (id == error_mark_node)
31332 break;
31333
31334 objc_declare_protocol (id, attributes);
31335
31336 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31337 cp_lexer_consume_token (parser->lexer);
31338 else
31339 break;
31340 }
31341 cp_parser_consume_semicolon_at_end_of_statement (parser);
31342 }
31343
31344 /* Ok, we got a full-fledged definition (or at least should). */
31345 else
31346 {
31347 proto = cp_parser_identifier (parser);
31348 protorefs = cp_parser_objc_protocol_refs_opt (parser);
31349 objc_start_protocol (proto, protorefs, attributes);
31350 cp_parser_objc_method_prototype_list (parser);
31351 }
31352 }
31353
31354 /* Parse an Objective-C superclass or category. */
31355
31356 static void
31357 cp_parser_objc_superclass_or_category (cp_parser *parser,
31358 bool iface_p,
31359 tree *super,
31360 tree *categ, bool *is_class_extension)
31361 {
31362 cp_token *next = cp_lexer_peek_token (parser->lexer);
31363
31364 *super = *categ = NULL_TREE;
31365 *is_class_extension = false;
31366 if (next->type == CPP_COLON)
31367 {
31368 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31369 *super = cp_parser_identifier (parser);
31370 }
31371 else if (next->type == CPP_OPEN_PAREN)
31372 {
31373 matching_parens parens;
31374 parens.consume_open (parser); /* Eat '('. */
31375
31376 /* If there is no category name, and this is an @interface, we
31377 have a class extension. */
31378 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31379 {
31380 *categ = NULL_TREE;
31381 *is_class_extension = true;
31382 }
31383 else
31384 *categ = cp_parser_identifier (parser);
31385
31386 parens.require_close (parser);
31387 }
31388 }
31389
31390 /* Parse an Objective-C class interface. */
31391
31392 static void
31393 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
31394 {
31395 tree name, super, categ, protos;
31396 bool is_class_extension;
31397
31398 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
31399 name = cp_parser_identifier (parser);
31400 if (name == error_mark_node)
31401 {
31402 /* It's hard to recover because even if valid @interface stuff
31403 is to follow, we can't compile it (or validate it) if we
31404 don't even know which class it refers to. Let's assume this
31405 was a stray '@interface' token in the stream and skip it.
31406 */
31407 return;
31408 }
31409 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
31410 &is_class_extension);
31411 protos = cp_parser_objc_protocol_refs_opt (parser);
31412
31413 /* We have either a class or a category on our hands. */
31414 if (categ || is_class_extension)
31415 objc_start_category_interface (name, categ, protos, attributes);
31416 else
31417 {
31418 objc_start_class_interface (name, super, protos, attributes);
31419 /* Handle instance variable declarations, if any. */
31420 cp_parser_objc_class_ivars (parser);
31421 objc_continue_interface ();
31422 }
31423
31424 cp_parser_objc_method_prototype_list (parser);
31425 }
31426
31427 /* Parse an Objective-C class implementation. */
31428
31429 static void
31430 cp_parser_objc_class_implementation (cp_parser* parser)
31431 {
31432 tree name, super, categ;
31433 bool is_class_extension;
31434
31435 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
31436 name = cp_parser_identifier (parser);
31437 if (name == error_mark_node)
31438 {
31439 /* It's hard to recover because even if valid @implementation
31440 stuff is to follow, we can't compile it (or validate it) if
31441 we don't even know which class it refers to. Let's assume
31442 this was a stray '@implementation' token in the stream and
31443 skip it.
31444 */
31445 return;
31446 }
31447 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
31448 &is_class_extension);
31449
31450 /* We have either a class or a category on our hands. */
31451 if (categ)
31452 objc_start_category_implementation (name, categ);
31453 else
31454 {
31455 objc_start_class_implementation (name, super);
31456 /* Handle instance variable declarations, if any. */
31457 cp_parser_objc_class_ivars (parser);
31458 objc_continue_implementation ();
31459 }
31460
31461 cp_parser_objc_method_definition_list (parser);
31462 }
31463
31464 /* Consume the @end token and finish off the implementation. */
31465
31466 static void
31467 cp_parser_objc_end_implementation (cp_parser* parser)
31468 {
31469 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31470 objc_finish_implementation ();
31471 }
31472
31473 /* Parse an Objective-C declaration. */
31474
31475 static void
31476 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
31477 {
31478 /* Try to figure out what kind of declaration is present. */
31479 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31480
31481 if (attributes)
31482 switch (kwd->keyword)
31483 {
31484 case RID_AT_ALIAS:
31485 case RID_AT_CLASS:
31486 case RID_AT_END:
31487 error_at (kwd->location, "attributes may not be specified before"
31488 " the %<@%D%> Objective-C++ keyword",
31489 kwd->u.value);
31490 attributes = NULL;
31491 break;
31492 case RID_AT_IMPLEMENTATION:
31493 warning_at (kwd->location, OPT_Wattributes,
31494 "prefix attributes are ignored before %<@%D%>",
31495 kwd->u.value);
31496 attributes = NULL;
31497 default:
31498 break;
31499 }
31500
31501 switch (kwd->keyword)
31502 {
31503 case RID_AT_ALIAS:
31504 cp_parser_objc_alias_declaration (parser);
31505 break;
31506 case RID_AT_CLASS:
31507 cp_parser_objc_class_declaration (parser);
31508 break;
31509 case RID_AT_PROTOCOL:
31510 cp_parser_objc_protocol_declaration (parser, attributes);
31511 break;
31512 case RID_AT_INTERFACE:
31513 cp_parser_objc_class_interface (parser, attributes);
31514 break;
31515 case RID_AT_IMPLEMENTATION:
31516 cp_parser_objc_class_implementation (parser);
31517 break;
31518 case RID_AT_END:
31519 cp_parser_objc_end_implementation (parser);
31520 break;
31521 default:
31522 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31523 kwd->u.value);
31524 cp_parser_skip_to_end_of_block_or_statement (parser);
31525 }
31526 }
31527
31528 /* Parse an Objective-C try-catch-finally statement.
31529
31530 objc-try-catch-finally-stmt:
31531 @try compound-statement objc-catch-clause-seq [opt]
31532 objc-finally-clause [opt]
31533
31534 objc-catch-clause-seq:
31535 objc-catch-clause objc-catch-clause-seq [opt]
31536
31537 objc-catch-clause:
31538 @catch ( objc-exception-declaration ) compound-statement
31539
31540 objc-finally-clause:
31541 @finally compound-statement
31542
31543 objc-exception-declaration:
31544 parameter-declaration
31545 '...'
31546
31547 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31548
31549 Returns NULL_TREE.
31550
31551 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31552 for C. Keep them in sync. */
31553
31554 static tree
31555 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
31556 {
31557 location_t location;
31558 tree stmt;
31559
31560 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
31561 location = cp_lexer_peek_token (parser->lexer)->location;
31562 objc_maybe_warn_exceptions (location);
31563 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31564 node, lest it get absorbed into the surrounding block. */
31565 stmt = push_stmt_list ();
31566 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31567 objc_begin_try_stmt (location, pop_stmt_list (stmt));
31568
31569 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
31570 {
31571 cp_parameter_declarator *parm;
31572 tree parameter_declaration = error_mark_node;
31573 bool seen_open_paren = false;
31574 matching_parens parens;
31575
31576 cp_lexer_consume_token (parser->lexer);
31577 if (parens.require_open (parser))
31578 seen_open_paren = true;
31579 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31580 {
31581 /* We have "@catch (...)" (where the '...' are literally
31582 what is in the code). Skip the '...'.
31583 parameter_declaration is set to NULL_TREE, and
31584 objc_being_catch_clauses() knows that that means
31585 '...'. */
31586 cp_lexer_consume_token (parser->lexer);
31587 parameter_declaration = NULL_TREE;
31588 }
31589 else
31590 {
31591 /* We have "@catch (NSException *exception)" or something
31592 like that. Parse the parameter declaration. */
31593 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31594 false, NULL);
31595 if (parm == NULL)
31596 parameter_declaration = error_mark_node;
31597 else
31598 parameter_declaration = grokdeclarator (parm->declarator,
31599 &parm->decl_specifiers,
31600 PARM, /*initialized=*/0,
31601 /*attrlist=*/NULL);
31602 }
31603 if (seen_open_paren)
31604 parens.require_close (parser);
31605 else
31606 {
31607 /* If there was no open parenthesis, we are recovering from
31608 an error, and we are trying to figure out what mistake
31609 the user has made. */
31610
31611 /* If there is an immediate closing parenthesis, the user
31612 probably forgot the opening one (ie, they typed "@catch
31613 NSException *e)". Parse the closing parenthesis and keep
31614 going. */
31615 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31616 cp_lexer_consume_token (parser->lexer);
31617
31618 /* If these is no immediate closing parenthesis, the user
31619 probably doesn't know that parenthesis are required at
31620 all (ie, they typed "@catch NSException *e"). So, just
31621 forget about the closing parenthesis and keep going. */
31622 }
31623 objc_begin_catch_clause (parameter_declaration);
31624 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31625 objc_finish_catch_clause ();
31626 }
31627 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31628 {
31629 cp_lexer_consume_token (parser->lexer);
31630 location = cp_lexer_peek_token (parser->lexer)->location;
31631 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31632 node, lest it get absorbed into the surrounding block. */
31633 stmt = push_stmt_list ();
31634 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31635 objc_build_finally_clause (location, pop_stmt_list (stmt));
31636 }
31637
31638 return objc_finish_try_stmt ();
31639 }
31640
31641 /* Parse an Objective-C synchronized statement.
31642
31643 objc-synchronized-stmt:
31644 @synchronized ( expression ) compound-statement
31645
31646 Returns NULL_TREE. */
31647
31648 static tree
31649 cp_parser_objc_synchronized_statement (cp_parser *parser)
31650 {
31651 location_t location;
31652 tree lock, stmt;
31653
31654 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31655
31656 location = cp_lexer_peek_token (parser->lexer)->location;
31657 objc_maybe_warn_exceptions (location);
31658 matching_parens parens;
31659 parens.require_open (parser);
31660 lock = cp_parser_expression (parser);
31661 parens.require_close (parser);
31662
31663 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31664 node, lest it get absorbed into the surrounding block. */
31665 stmt = push_stmt_list ();
31666 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31667
31668 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31669 }
31670
31671 /* Parse an Objective-C throw statement.
31672
31673 objc-throw-stmt:
31674 @throw assignment-expression [opt] ;
31675
31676 Returns a constructed '@throw' statement. */
31677
31678 static tree
31679 cp_parser_objc_throw_statement (cp_parser *parser)
31680 {
31681 tree expr = NULL_TREE;
31682 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31683
31684 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31685
31686 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31687 expr = cp_parser_expression (parser);
31688
31689 cp_parser_consume_semicolon_at_end_of_statement (parser);
31690
31691 return objc_build_throw_stmt (loc, expr);
31692 }
31693
31694 /* Parse an Objective-C statement. */
31695
31696 static tree
31697 cp_parser_objc_statement (cp_parser * parser)
31698 {
31699 /* Try to figure out what kind of declaration is present. */
31700 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31701
31702 switch (kwd->keyword)
31703 {
31704 case RID_AT_TRY:
31705 return cp_parser_objc_try_catch_finally_statement (parser);
31706 case RID_AT_SYNCHRONIZED:
31707 return cp_parser_objc_synchronized_statement (parser);
31708 case RID_AT_THROW:
31709 return cp_parser_objc_throw_statement (parser);
31710 default:
31711 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31712 kwd->u.value);
31713 cp_parser_skip_to_end_of_block_or_statement (parser);
31714 }
31715
31716 return error_mark_node;
31717 }
31718
31719 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31720 look ahead to see if an objc keyword follows the attributes. This
31721 is to detect the use of prefix attributes on ObjC @interface and
31722 @protocol. */
31723
31724 static bool
31725 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31726 {
31727 cp_lexer_save_tokens (parser->lexer);
31728 *attrib = cp_parser_attributes_opt (parser);
31729 gcc_assert (*attrib);
31730 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31731 {
31732 cp_lexer_commit_tokens (parser->lexer);
31733 return true;
31734 }
31735 cp_lexer_rollback_tokens (parser->lexer);
31736 return false;
31737 }
31738
31739 /* This routine is a minimal replacement for
31740 c_parser_struct_declaration () used when parsing the list of
31741 types/names or ObjC++ properties. For example, when parsing the
31742 code
31743
31744 @property (readonly) int a, b, c;
31745
31746 this function is responsible for parsing "int a, int b, int c" and
31747 returning the declarations as CHAIN of DECLs.
31748
31749 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31750 similar parsing. */
31751 static tree
31752 cp_parser_objc_struct_declaration (cp_parser *parser)
31753 {
31754 tree decls = NULL_TREE;
31755 cp_decl_specifier_seq declspecs;
31756 int decl_class_or_enum_p;
31757 tree prefix_attributes;
31758
31759 cp_parser_decl_specifier_seq (parser,
31760 CP_PARSER_FLAGS_NONE,
31761 &declspecs,
31762 &decl_class_or_enum_p);
31763
31764 if (declspecs.type == error_mark_node)
31765 return error_mark_node;
31766
31767 /* auto, register, static, extern, mutable. */
31768 if (declspecs.storage_class != sc_none)
31769 {
31770 cp_parser_error (parser, "invalid type for property");
31771 declspecs.storage_class = sc_none;
31772 }
31773
31774 /* thread_local. */
31775 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31776 {
31777 cp_parser_error (parser, "invalid type for property");
31778 declspecs.locations[ds_thread] = 0;
31779 }
31780
31781 /* typedef. */
31782 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31783 {
31784 cp_parser_error (parser, "invalid type for property");
31785 declspecs.locations[ds_typedef] = 0;
31786 }
31787
31788 prefix_attributes = declspecs.attributes;
31789 declspecs.attributes = NULL_TREE;
31790
31791 /* Keep going until we hit the `;' at the end of the declaration. */
31792 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31793 {
31794 tree attributes, first_attribute, decl;
31795 cp_declarator *declarator;
31796 cp_token *token;
31797
31798 /* Parse the declarator. */
31799 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31800 CP_PARSER_FLAGS_NONE,
31801 NULL, NULL, false, false, false);
31802
31803 /* Look for attributes that apply to the ivar. */
31804 attributes = cp_parser_attributes_opt (parser);
31805 /* Remember which attributes are prefix attributes and
31806 which are not. */
31807 first_attribute = attributes;
31808 /* Combine the attributes. */
31809 attributes = attr_chainon (prefix_attributes, attributes);
31810
31811 decl = grokfield (declarator, &declspecs,
31812 NULL_TREE, /*init_const_expr_p=*/false,
31813 NULL_TREE, attributes);
31814
31815 if (decl == error_mark_node || decl == NULL_TREE)
31816 return error_mark_node;
31817
31818 /* Reset PREFIX_ATTRIBUTES. */
31819 if (attributes != error_mark_node)
31820 {
31821 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31822 attributes = TREE_CHAIN (attributes);
31823 if (attributes)
31824 TREE_CHAIN (attributes) = NULL_TREE;
31825 }
31826
31827 DECL_CHAIN (decl) = decls;
31828 decls = decl;
31829
31830 token = cp_lexer_peek_token (parser->lexer);
31831 if (token->type == CPP_COMMA)
31832 {
31833 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31834 continue;
31835 }
31836 else
31837 break;
31838 }
31839 return decls;
31840 }
31841
31842 /* Parse an Objective-C @property declaration. The syntax is:
31843
31844 objc-property-declaration:
31845 '@property' objc-property-attributes[opt] struct-declaration ;
31846
31847 objc-property-attributes:
31848 '(' objc-property-attribute-list ')'
31849
31850 objc-property-attribute-list:
31851 objc-property-attribute
31852 objc-property-attribute-list, objc-property-attribute
31853
31854 objc-property-attribute
31855 'getter' = identifier
31856 'setter' = identifier
31857 'readonly'
31858 'readwrite'
31859 'assign'
31860 'retain'
31861 'copy'
31862 'nonatomic'
31863
31864 For example:
31865 @property NSString *name;
31866 @property (readonly) id object;
31867 @property (retain, nonatomic, getter=getTheName) id name;
31868 @property int a, b, c;
31869
31870 PS: This function is identical to
31871 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31872 static void
31873 cp_parser_objc_at_property_declaration (cp_parser *parser)
31874 {
31875 /* The following variables hold the attributes of the properties as
31876 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31877 seen. When we see an attribute, we set them to 'true' (if they
31878 are boolean properties) or to the identifier (if they have an
31879 argument, ie, for getter and setter). Note that here we only
31880 parse the list of attributes, check the syntax and accumulate the
31881 attributes that we find. objc_add_property_declaration() will
31882 then process the information. */
31883 bool property_assign = false;
31884 bool property_copy = false;
31885 tree property_getter_ident = NULL_TREE;
31886 bool property_nonatomic = false;
31887 bool property_readonly = false;
31888 bool property_readwrite = false;
31889 bool property_retain = false;
31890 tree property_setter_ident = NULL_TREE;
31891
31892 /* 'properties' is the list of properties that we read. Usually a
31893 single one, but maybe more (eg, in "@property int a, b, c;" there
31894 are three). */
31895 tree properties;
31896 location_t loc;
31897
31898 loc = cp_lexer_peek_token (parser->lexer)->location;
31899
31900 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31901
31902 /* Parse the optional attribute list... */
31903 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31904 {
31905 /* Eat the '('. */
31906 matching_parens parens;
31907 parens.consume_open (parser);
31908
31909 while (true)
31910 {
31911 bool syntax_error = false;
31912 cp_token *token = cp_lexer_peek_token (parser->lexer);
31913 enum rid keyword;
31914
31915 if (token->type != CPP_NAME)
31916 {
31917 cp_parser_error (parser, "expected identifier");
31918 break;
31919 }
31920 keyword = C_RID_CODE (token->u.value);
31921 cp_lexer_consume_token (parser->lexer);
31922 switch (keyword)
31923 {
31924 case RID_ASSIGN: property_assign = true; break;
31925 case RID_COPY: property_copy = true; break;
31926 case RID_NONATOMIC: property_nonatomic = true; break;
31927 case RID_READONLY: property_readonly = true; break;
31928 case RID_READWRITE: property_readwrite = true; break;
31929 case RID_RETAIN: property_retain = true; break;
31930
31931 case RID_GETTER:
31932 case RID_SETTER:
31933 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31934 {
31935 if (keyword == RID_GETTER)
31936 cp_parser_error (parser,
31937 "missing %<=%> (after %<getter%> attribute)");
31938 else
31939 cp_parser_error (parser,
31940 "missing %<=%> (after %<setter%> attribute)");
31941 syntax_error = true;
31942 break;
31943 }
31944 cp_lexer_consume_token (parser->lexer); /* eat the = */
31945 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31946 {
31947 cp_parser_error (parser, "expected identifier");
31948 syntax_error = true;
31949 break;
31950 }
31951 if (keyword == RID_SETTER)
31952 {
31953 if (property_setter_ident != NULL_TREE)
31954 {
31955 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31956 cp_lexer_consume_token (parser->lexer);
31957 }
31958 else
31959 property_setter_ident = cp_parser_objc_selector (parser);
31960 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31961 cp_parser_error (parser, "setter name must terminate with %<:%>");
31962 else
31963 cp_lexer_consume_token (parser->lexer);
31964 }
31965 else
31966 {
31967 if (property_getter_ident != NULL_TREE)
31968 {
31969 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31970 cp_lexer_consume_token (parser->lexer);
31971 }
31972 else
31973 property_getter_ident = cp_parser_objc_selector (parser);
31974 }
31975 break;
31976 default:
31977 cp_parser_error (parser, "unknown property attribute");
31978 syntax_error = true;
31979 break;
31980 }
31981
31982 if (syntax_error)
31983 break;
31984
31985 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31986 cp_lexer_consume_token (parser->lexer);
31987 else
31988 break;
31989 }
31990
31991 /* FIXME: "@property (setter, assign);" will generate a spurious
31992 "error: expected ‘)’ before ‘,’ token". This is because
31993 cp_parser_require, unlike the C counterpart, will produce an
31994 error even if we are in error recovery. */
31995 if (!parens.require_close (parser))
31996 {
31997 cp_parser_skip_to_closing_parenthesis (parser,
31998 /*recovering=*/true,
31999 /*or_comma=*/false,
32000 /*consume_paren=*/true);
32001 }
32002 }
32003
32004 /* ... and the property declaration(s). */
32005 properties = cp_parser_objc_struct_declaration (parser);
32006
32007 if (properties == error_mark_node)
32008 {
32009 cp_parser_skip_to_end_of_statement (parser);
32010 /* If the next token is now a `;', consume it. */
32011 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32012 cp_lexer_consume_token (parser->lexer);
32013 return;
32014 }
32015
32016 if (properties == NULL_TREE)
32017 cp_parser_error (parser, "expected identifier");
32018 else
32019 {
32020 /* Comma-separated properties are chained together in
32021 reverse order; add them one by one. */
32022 properties = nreverse (properties);
32023
32024 for (; properties; properties = TREE_CHAIN (properties))
32025 objc_add_property_declaration (loc, copy_node (properties),
32026 property_readonly, property_readwrite,
32027 property_assign, property_retain,
32028 property_copy, property_nonatomic,
32029 property_getter_ident, property_setter_ident);
32030 }
32031
32032 cp_parser_consume_semicolon_at_end_of_statement (parser);
32033 }
32034
32035 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
32036
32037 objc-synthesize-declaration:
32038 @synthesize objc-synthesize-identifier-list ;
32039
32040 objc-synthesize-identifier-list:
32041 objc-synthesize-identifier
32042 objc-synthesize-identifier-list, objc-synthesize-identifier
32043
32044 objc-synthesize-identifier
32045 identifier
32046 identifier = identifier
32047
32048 For example:
32049 @synthesize MyProperty;
32050 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
32051
32052 PS: This function is identical to c_parser_objc_at_synthesize_declaration
32053 for C. Keep them in sync.
32054 */
32055 static void
32056 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
32057 {
32058 tree list = NULL_TREE;
32059 location_t loc;
32060 loc = cp_lexer_peek_token (parser->lexer)->location;
32061
32062 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
32063 while (true)
32064 {
32065 tree property, ivar;
32066 property = cp_parser_identifier (parser);
32067 if (property == error_mark_node)
32068 {
32069 cp_parser_consume_semicolon_at_end_of_statement (parser);
32070 return;
32071 }
32072 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
32073 {
32074 cp_lexer_consume_token (parser->lexer);
32075 ivar = cp_parser_identifier (parser);
32076 if (ivar == error_mark_node)
32077 {
32078 cp_parser_consume_semicolon_at_end_of_statement (parser);
32079 return;
32080 }
32081 }
32082 else
32083 ivar = NULL_TREE;
32084 list = chainon (list, build_tree_list (ivar, property));
32085 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32086 cp_lexer_consume_token (parser->lexer);
32087 else
32088 break;
32089 }
32090 cp_parser_consume_semicolon_at_end_of_statement (parser);
32091 objc_add_synthesize_declaration (loc, list);
32092 }
32093
32094 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
32095
32096 objc-dynamic-declaration:
32097 @dynamic identifier-list ;
32098
32099 For example:
32100 @dynamic MyProperty;
32101 @dynamic MyProperty, AnotherProperty;
32102
32103 PS: This function is identical to c_parser_objc_at_dynamic_declaration
32104 for C. Keep them in sync.
32105 */
32106 static void
32107 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
32108 {
32109 tree list = NULL_TREE;
32110 location_t loc;
32111 loc = cp_lexer_peek_token (parser->lexer)->location;
32112
32113 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
32114 while (true)
32115 {
32116 tree property;
32117 property = cp_parser_identifier (parser);
32118 if (property == error_mark_node)
32119 {
32120 cp_parser_consume_semicolon_at_end_of_statement (parser);
32121 return;
32122 }
32123 list = chainon (list, build_tree_list (NULL, property));
32124 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32125 cp_lexer_consume_token (parser->lexer);
32126 else
32127 break;
32128 }
32129 cp_parser_consume_semicolon_at_end_of_statement (parser);
32130 objc_add_dynamic_declaration (loc, list);
32131 }
32132
32133 \f
32134 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
32135
32136 /* Returns name of the next clause.
32137 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
32138 the token is not consumed. Otherwise appropriate pragma_omp_clause is
32139 returned and the token is consumed. */
32140
32141 static pragma_omp_clause
32142 cp_parser_omp_clause_name (cp_parser *parser)
32143 {
32144 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
32145
32146 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32147 result = PRAGMA_OACC_CLAUSE_AUTO;
32148 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
32149 result = PRAGMA_OMP_CLAUSE_IF;
32150 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32151 result = PRAGMA_OMP_CLAUSE_DEFAULT;
32152 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
32153 result = PRAGMA_OACC_CLAUSE_DELETE;
32154 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
32155 result = PRAGMA_OMP_CLAUSE_PRIVATE;
32156 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32157 result = PRAGMA_OMP_CLAUSE_FOR;
32158 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32159 {
32160 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32161 const char *p = IDENTIFIER_POINTER (id);
32162
32163 switch (p[0])
32164 {
32165 case 'a':
32166 if (!strcmp ("aligned", p))
32167 result = PRAGMA_OMP_CLAUSE_ALIGNED;
32168 else if (!strcmp ("async", p))
32169 result = PRAGMA_OACC_CLAUSE_ASYNC;
32170 break;
32171 case 'c':
32172 if (!strcmp ("collapse", p))
32173 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
32174 else if (!strcmp ("copy", p))
32175 result = PRAGMA_OACC_CLAUSE_COPY;
32176 else if (!strcmp ("copyin", p))
32177 result = PRAGMA_OMP_CLAUSE_COPYIN;
32178 else if (!strcmp ("copyout", p))
32179 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32180 else if (!strcmp ("copyprivate", p))
32181 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
32182 else if (!strcmp ("create", p))
32183 result = PRAGMA_OACC_CLAUSE_CREATE;
32184 break;
32185 case 'd':
32186 if (!strcmp ("defaultmap", p))
32187 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
32188 else if (!strcmp ("depend", p))
32189 result = PRAGMA_OMP_CLAUSE_DEPEND;
32190 else if (!strcmp ("device", p))
32191 result = PRAGMA_OMP_CLAUSE_DEVICE;
32192 else if (!strcmp ("deviceptr", p))
32193 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
32194 else if (!strcmp ("device_resident", p))
32195 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
32196 else if (!strcmp ("dist_schedule", p))
32197 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
32198 break;
32199 case 'f':
32200 if (!strcmp ("final", p))
32201 result = PRAGMA_OMP_CLAUSE_FINAL;
32202 else if (!strcmp ("finalize", p))
32203 result = PRAGMA_OACC_CLAUSE_FINALIZE;
32204 else if (!strcmp ("firstprivate", p))
32205 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
32206 else if (!strcmp ("from", p))
32207 result = PRAGMA_OMP_CLAUSE_FROM;
32208 break;
32209 case 'g':
32210 if (!strcmp ("gang", p))
32211 result = PRAGMA_OACC_CLAUSE_GANG;
32212 else if (!strcmp ("grainsize", p))
32213 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
32214 break;
32215 case 'h':
32216 if (!strcmp ("hint", p))
32217 result = PRAGMA_OMP_CLAUSE_HINT;
32218 else if (!strcmp ("host", p))
32219 result = PRAGMA_OACC_CLAUSE_HOST;
32220 break;
32221 case 'i':
32222 if (!strcmp ("if_present", p))
32223 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
32224 else if (!strcmp ("in_reduction", p))
32225 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
32226 else if (!strcmp ("inbranch", p))
32227 result = PRAGMA_OMP_CLAUSE_INBRANCH;
32228 else if (!strcmp ("independent", p))
32229 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
32230 else if (!strcmp ("is_device_ptr", p))
32231 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
32232 break;
32233 case 'l':
32234 if (!strcmp ("lastprivate", p))
32235 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
32236 else if (!strcmp ("linear", p))
32237 result = PRAGMA_OMP_CLAUSE_LINEAR;
32238 else if (!strcmp ("link", p))
32239 result = PRAGMA_OMP_CLAUSE_LINK;
32240 break;
32241 case 'm':
32242 if (!strcmp ("map", p))
32243 result = PRAGMA_OMP_CLAUSE_MAP;
32244 else if (!strcmp ("mergeable", p))
32245 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
32246 break;
32247 case 'n':
32248 if (!strcmp ("nogroup", p))
32249 result = PRAGMA_OMP_CLAUSE_NOGROUP;
32250 else if (!strcmp ("nontemporal", p))
32251 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
32252 else if (!strcmp ("notinbranch", p))
32253 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
32254 else if (!strcmp ("nowait", p))
32255 result = PRAGMA_OMP_CLAUSE_NOWAIT;
32256 else if (!strcmp ("num_gangs", p))
32257 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
32258 else if (!strcmp ("num_tasks", p))
32259 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
32260 else if (!strcmp ("num_teams", p))
32261 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
32262 else if (!strcmp ("num_threads", p))
32263 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
32264 else if (!strcmp ("num_workers", p))
32265 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
32266 break;
32267 case 'o':
32268 if (!strcmp ("ordered", p))
32269 result = PRAGMA_OMP_CLAUSE_ORDERED;
32270 break;
32271 case 'p':
32272 if (!strcmp ("parallel", p))
32273 result = PRAGMA_OMP_CLAUSE_PARALLEL;
32274 else if (!strcmp ("present", p))
32275 result = PRAGMA_OACC_CLAUSE_PRESENT;
32276 else if (!strcmp ("present_or_copy", p)
32277 || !strcmp ("pcopy", p))
32278 result = PRAGMA_OACC_CLAUSE_COPY;
32279 else if (!strcmp ("present_or_copyin", p)
32280 || !strcmp ("pcopyin", p))
32281 result = PRAGMA_OACC_CLAUSE_COPYIN;
32282 else if (!strcmp ("present_or_copyout", p)
32283 || !strcmp ("pcopyout", p))
32284 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32285 else if (!strcmp ("present_or_create", p)
32286 || !strcmp ("pcreate", p))
32287 result = PRAGMA_OACC_CLAUSE_CREATE;
32288 else if (!strcmp ("priority", p))
32289 result = PRAGMA_OMP_CLAUSE_PRIORITY;
32290 else if (!strcmp ("proc_bind", p))
32291 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
32292 break;
32293 case 'r':
32294 if (!strcmp ("reduction", p))
32295 result = PRAGMA_OMP_CLAUSE_REDUCTION;
32296 break;
32297 case 's':
32298 if (!strcmp ("safelen", p))
32299 result = PRAGMA_OMP_CLAUSE_SAFELEN;
32300 else if (!strcmp ("schedule", p))
32301 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
32302 else if (!strcmp ("sections", p))
32303 result = PRAGMA_OMP_CLAUSE_SECTIONS;
32304 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
32305 result = PRAGMA_OACC_CLAUSE_HOST;
32306 else if (!strcmp ("seq", p))
32307 result = PRAGMA_OACC_CLAUSE_SEQ;
32308 else if (!strcmp ("shared", p))
32309 result = PRAGMA_OMP_CLAUSE_SHARED;
32310 else if (!strcmp ("simd", p))
32311 result = PRAGMA_OMP_CLAUSE_SIMD;
32312 else if (!strcmp ("simdlen", p))
32313 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
32314 break;
32315 case 't':
32316 if (!strcmp ("task_reduction", p))
32317 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
32318 else if (!strcmp ("taskgroup", p))
32319 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
32320 else if (!strcmp ("thread_limit", p))
32321 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
32322 else if (!strcmp ("threads", p))
32323 result = PRAGMA_OMP_CLAUSE_THREADS;
32324 else if (!strcmp ("tile", p))
32325 result = PRAGMA_OACC_CLAUSE_TILE;
32326 else if (!strcmp ("to", p))
32327 result = PRAGMA_OMP_CLAUSE_TO;
32328 break;
32329 case 'u':
32330 if (!strcmp ("uniform", p))
32331 result = PRAGMA_OMP_CLAUSE_UNIFORM;
32332 else if (!strcmp ("untied", p))
32333 result = PRAGMA_OMP_CLAUSE_UNTIED;
32334 else if (!strcmp ("use_device", p))
32335 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
32336 else if (!strcmp ("use_device_ptr", p))
32337 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
32338 break;
32339 case 'v':
32340 if (!strcmp ("vector", p))
32341 result = PRAGMA_OACC_CLAUSE_VECTOR;
32342 else if (!strcmp ("vector_length", p))
32343 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
32344 break;
32345 case 'w':
32346 if (!strcmp ("wait", p))
32347 result = PRAGMA_OACC_CLAUSE_WAIT;
32348 else if (!strcmp ("worker", p))
32349 result = PRAGMA_OACC_CLAUSE_WORKER;
32350 break;
32351 }
32352 }
32353
32354 if (result != PRAGMA_OMP_CLAUSE_NONE)
32355 cp_lexer_consume_token (parser->lexer);
32356
32357 return result;
32358 }
32359
32360 /* Validate that a clause of the given type does not already exist. */
32361
32362 static void
32363 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
32364 const char *name, location_t location)
32365 {
32366 tree c;
32367
32368 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
32369 if (OMP_CLAUSE_CODE (c) == code)
32370 {
32371 error_at (location, "too many %qs clauses", name);
32372 break;
32373 }
32374 }
32375
32376 /* OpenMP 2.5:
32377 variable-list:
32378 identifier
32379 variable-list , identifier
32380
32381 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32382 colon). An opening parenthesis will have been consumed by the caller.
32383
32384 If KIND is nonzero, create the appropriate node and install the decl
32385 in OMP_CLAUSE_DECL and add the node to the head of the list.
32386
32387 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32388 return the list created.
32389
32390 COLON can be NULL if only closing parenthesis should end the list,
32391 or pointer to bool which will receive false if the list is terminated
32392 by closing parenthesis or true if the list is terminated by colon. */
32393
32394 static tree
32395 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
32396 tree list, bool *colon)
32397 {
32398 cp_token *token;
32399 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32400 if (colon)
32401 {
32402 parser->colon_corrects_to_scope_p = false;
32403 *colon = false;
32404 }
32405 while (1)
32406 {
32407 tree name, decl;
32408
32409 if (kind == OMP_CLAUSE_DEPEND)
32410 cp_parser_parse_tentatively (parser);
32411 token = cp_lexer_peek_token (parser->lexer);
32412 if (kind != 0
32413 && current_class_ptr
32414 && cp_parser_is_keyword (token, RID_THIS))
32415 {
32416 decl = finish_this_expr ();
32417 if (TREE_CODE (decl) == NON_LVALUE_EXPR
32418 || CONVERT_EXPR_P (decl))
32419 decl = TREE_OPERAND (decl, 0);
32420 cp_lexer_consume_token (parser->lexer);
32421 }
32422 else
32423 {
32424 name = cp_parser_id_expression (parser, /*template_p=*/false,
32425 /*check_dependency_p=*/true,
32426 /*template_p=*/NULL,
32427 /*declarator_p=*/false,
32428 /*optional_p=*/false);
32429 if (name == error_mark_node)
32430 {
32431 if (kind == OMP_CLAUSE_DEPEND
32432 && cp_parser_simulate_error (parser))
32433 goto depend_lvalue;
32434 goto skip_comma;
32435 }
32436
32437 if (identifier_p (name))
32438 decl = cp_parser_lookup_name_simple (parser, name, token->location);
32439 else
32440 decl = name;
32441 if (decl == error_mark_node)
32442 {
32443 if (kind == OMP_CLAUSE_DEPEND
32444 && cp_parser_simulate_error (parser))
32445 goto depend_lvalue;
32446 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
32447 token->location);
32448 }
32449 }
32450 if (decl == error_mark_node)
32451 ;
32452 else if (kind != 0)
32453 {
32454 switch (kind)
32455 {
32456 case OMP_CLAUSE__CACHE_:
32457 /* The OpenACC cache directive explicitly only allows "array
32458 elements or subarrays". */
32459 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
32460 {
32461 error_at (token->location, "expected %<[%>");
32462 decl = error_mark_node;
32463 break;
32464 }
32465 /* FALLTHROUGH. */
32466 case OMP_CLAUSE_MAP:
32467 case OMP_CLAUSE_FROM:
32468 case OMP_CLAUSE_TO:
32469 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
32470 {
32471 location_t loc
32472 = cp_lexer_peek_token (parser->lexer)->location;
32473 cp_id_kind idk = CP_ID_KIND_NONE;
32474 cp_lexer_consume_token (parser->lexer);
32475 decl = convert_from_reference (decl);
32476 decl
32477 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
32478 decl, false,
32479 &idk, loc);
32480 }
32481 /* FALLTHROUGH. */
32482 case OMP_CLAUSE_DEPEND:
32483 case OMP_CLAUSE_REDUCTION:
32484 case OMP_CLAUSE_IN_REDUCTION:
32485 case OMP_CLAUSE_TASK_REDUCTION:
32486 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
32487 {
32488 tree low_bound = NULL_TREE, length = NULL_TREE;
32489
32490 parser->colon_corrects_to_scope_p = false;
32491 cp_lexer_consume_token (parser->lexer);
32492 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32493 low_bound = cp_parser_expression (parser);
32494 if (!colon)
32495 parser->colon_corrects_to_scope_p
32496 = saved_colon_corrects_to_scope_p;
32497 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
32498 length = integer_one_node;
32499 else
32500 {
32501 /* Look for `:'. */
32502 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32503 {
32504 if (kind == OMP_CLAUSE_DEPEND
32505 && cp_parser_simulate_error (parser))
32506 goto depend_lvalue;
32507 goto skip_comma;
32508 }
32509 if (kind == OMP_CLAUSE_DEPEND)
32510 cp_parser_commit_to_tentative_parse (parser);
32511 if (!cp_lexer_next_token_is (parser->lexer,
32512 CPP_CLOSE_SQUARE))
32513 length = cp_parser_expression (parser);
32514 }
32515 /* Look for the closing `]'. */
32516 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
32517 RT_CLOSE_SQUARE))
32518 {
32519 if (kind == OMP_CLAUSE_DEPEND
32520 && cp_parser_simulate_error (parser))
32521 goto depend_lvalue;
32522 goto skip_comma;
32523 }
32524
32525 decl = tree_cons (low_bound, length, decl);
32526 }
32527 break;
32528 default:
32529 break;
32530 }
32531
32532 if (kind == OMP_CLAUSE_DEPEND)
32533 {
32534 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
32535 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32536 && cp_parser_simulate_error (parser))
32537 {
32538 depend_lvalue:
32539 cp_parser_abort_tentative_parse (parser);
32540 decl = cp_parser_assignment_expression (parser, NULL,
32541 false, false);
32542 }
32543 else
32544 cp_parser_parse_definitely (parser);
32545 }
32546
32547 tree u = build_omp_clause (token->location, kind);
32548 OMP_CLAUSE_DECL (u) = decl;
32549 OMP_CLAUSE_CHAIN (u) = list;
32550 list = u;
32551 }
32552 else
32553 list = tree_cons (decl, NULL_TREE, list);
32554
32555 get_comma:
32556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32557 break;
32558 cp_lexer_consume_token (parser->lexer);
32559 }
32560
32561 if (colon)
32562 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32563
32564 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32565 {
32566 *colon = true;
32567 cp_parser_require (parser, CPP_COLON, RT_COLON);
32568 return list;
32569 }
32570
32571 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32572 {
32573 int ending;
32574
32575 /* Try to resync to an unnested comma. Copied from
32576 cp_parser_parenthesized_expression_list. */
32577 skip_comma:
32578 if (colon)
32579 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32580 ending = cp_parser_skip_to_closing_parenthesis (parser,
32581 /*recovering=*/true,
32582 /*or_comma=*/true,
32583 /*consume_paren=*/true);
32584 if (ending < 0)
32585 goto get_comma;
32586 }
32587
32588 return list;
32589 }
32590
32591 /* Similarly, but expect leading and trailing parenthesis. This is a very
32592 common case for omp clauses. */
32593
32594 static tree
32595 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
32596 {
32597 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32598 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
32599 return list;
32600 }
32601
32602 /* OpenACC 2.0:
32603 copy ( variable-list )
32604 copyin ( variable-list )
32605 copyout ( variable-list )
32606 create ( variable-list )
32607 delete ( variable-list )
32608 present ( variable-list ) */
32609
32610 static tree
32611 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
32612 tree list)
32613 {
32614 enum gomp_map_kind kind;
32615 switch (c_kind)
32616 {
32617 case PRAGMA_OACC_CLAUSE_COPY:
32618 kind = GOMP_MAP_TOFROM;
32619 break;
32620 case PRAGMA_OACC_CLAUSE_COPYIN:
32621 kind = GOMP_MAP_TO;
32622 break;
32623 case PRAGMA_OACC_CLAUSE_COPYOUT:
32624 kind = GOMP_MAP_FROM;
32625 break;
32626 case PRAGMA_OACC_CLAUSE_CREATE:
32627 kind = GOMP_MAP_ALLOC;
32628 break;
32629 case PRAGMA_OACC_CLAUSE_DELETE:
32630 kind = GOMP_MAP_RELEASE;
32631 break;
32632 case PRAGMA_OACC_CLAUSE_DEVICE:
32633 kind = GOMP_MAP_FORCE_TO;
32634 break;
32635 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32636 kind = GOMP_MAP_DEVICE_RESIDENT;
32637 break;
32638 case PRAGMA_OACC_CLAUSE_HOST:
32639 kind = GOMP_MAP_FORCE_FROM;
32640 break;
32641 case PRAGMA_OACC_CLAUSE_LINK:
32642 kind = GOMP_MAP_LINK;
32643 break;
32644 case PRAGMA_OACC_CLAUSE_PRESENT:
32645 kind = GOMP_MAP_FORCE_PRESENT;
32646 break;
32647 default:
32648 gcc_unreachable ();
32649 }
32650 tree nl, c;
32651 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32652
32653 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32654 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32655
32656 return nl;
32657 }
32658
32659 /* OpenACC 2.0:
32660 deviceptr ( variable-list ) */
32661
32662 static tree
32663 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32664 {
32665 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32666 tree vars, t;
32667
32668 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32669 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32670 variable-list must only allow for pointer variables. */
32671 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32672 for (t = vars; t; t = TREE_CHAIN (t))
32673 {
32674 tree v = TREE_PURPOSE (t);
32675 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32676 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32677 OMP_CLAUSE_DECL (u) = v;
32678 OMP_CLAUSE_CHAIN (u) = list;
32679 list = u;
32680 }
32681
32682 return list;
32683 }
32684
32685 /* OpenACC 2.5:
32686 auto
32687 finalize
32688 independent
32689 nohost
32690 seq */
32691
32692 static tree
32693 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
32694 tree list)
32695 {
32696 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
32697
32698 tree c = build_omp_clause (loc, code);
32699 OMP_CLAUSE_CHAIN (c) = list;
32700
32701 return c;
32702 }
32703
32704 /* OpenACC:
32705 num_gangs ( expression )
32706 num_workers ( expression )
32707 vector_length ( expression ) */
32708
32709 static tree
32710 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32711 const char *str, tree list)
32712 {
32713 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32714
32715 matching_parens parens;
32716 if (!parens.require_open (parser))
32717 return list;
32718
32719 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32720
32721 if (t == error_mark_node
32722 || !parens.require_close (parser))
32723 {
32724 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32725 /*or_comma=*/false,
32726 /*consume_paren=*/true);
32727 return list;
32728 }
32729
32730 check_no_duplicate_clause (list, code, str, loc);
32731
32732 tree c = build_omp_clause (loc, code);
32733 OMP_CLAUSE_OPERAND (c, 0) = t;
32734 OMP_CLAUSE_CHAIN (c) = list;
32735 return c;
32736 }
32737
32738 /* OpenACC:
32739
32740 gang [( gang-arg-list )]
32741 worker [( [num:] int-expr )]
32742 vector [( [length:] int-expr )]
32743
32744 where gang-arg is one of:
32745
32746 [num:] int-expr
32747 static: size-expr
32748
32749 and size-expr may be:
32750
32751 *
32752 int-expr
32753 */
32754
32755 static tree
32756 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
32757 omp_clause_code kind,
32758 const char *str, tree list)
32759 {
32760 const char *id = "num";
32761 cp_lexer *lexer = parser->lexer;
32762 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32763
32764 if (kind == OMP_CLAUSE_VECTOR)
32765 id = "length";
32766
32767 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32768 {
32769 matching_parens parens;
32770 parens.consume_open (parser);
32771
32772 do
32773 {
32774 cp_token *next = cp_lexer_peek_token (lexer);
32775 int idx = 0;
32776
32777 /* Gang static argument. */
32778 if (kind == OMP_CLAUSE_GANG
32779 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32780 {
32781 cp_lexer_consume_token (lexer);
32782
32783 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32784 goto cleanup_error;
32785
32786 idx = 1;
32787 if (ops[idx] != NULL)
32788 {
32789 cp_parser_error (parser, "too many %<static%> arguments");
32790 goto cleanup_error;
32791 }
32792
32793 /* Check for the '*' argument. */
32794 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32795 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32796 || cp_lexer_nth_token_is (parser->lexer, 2,
32797 CPP_CLOSE_PAREN)))
32798 {
32799 cp_lexer_consume_token (lexer);
32800 ops[idx] = integer_minus_one_node;
32801
32802 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32803 {
32804 cp_lexer_consume_token (lexer);
32805 continue;
32806 }
32807 else break;
32808 }
32809 }
32810 /* Worker num: argument and vector length: arguments. */
32811 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32812 && id_equal (next->u.value, id)
32813 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32814 {
32815 cp_lexer_consume_token (lexer); /* id */
32816 cp_lexer_consume_token (lexer); /* ':' */
32817 }
32818
32819 /* Now collect the actual argument. */
32820 if (ops[idx] != NULL_TREE)
32821 {
32822 cp_parser_error (parser, "unexpected argument");
32823 goto cleanup_error;
32824 }
32825
32826 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32827 false);
32828 if (expr == error_mark_node)
32829 goto cleanup_error;
32830
32831 mark_exp_read (expr);
32832 ops[idx] = expr;
32833
32834 if (kind == OMP_CLAUSE_GANG
32835 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32836 {
32837 cp_lexer_consume_token (lexer);
32838 continue;
32839 }
32840 break;
32841 }
32842 while (1);
32843
32844 if (!parens.require_close (parser))
32845 goto cleanup_error;
32846 }
32847
32848 check_no_duplicate_clause (list, kind, str, loc);
32849
32850 c = build_omp_clause (loc, kind);
32851
32852 if (ops[1])
32853 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32854
32855 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32856 OMP_CLAUSE_CHAIN (c) = list;
32857
32858 return c;
32859
32860 cleanup_error:
32861 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32862 return list;
32863 }
32864
32865 /* OpenACC 2.0:
32866 tile ( size-expr-list ) */
32867
32868 static tree
32869 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32870 {
32871 tree c, expr = error_mark_node;
32872 tree tile = NULL_TREE;
32873
32874 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32875 so, but the spec authors never considered such a case and have
32876 differing opinions on what it might mean, including 'not
32877 allowed'.) */
32878 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32879 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32880 clause_loc);
32881
32882 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32883 return list;
32884
32885 do
32886 {
32887 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32888 return list;
32889
32890 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32891 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32892 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32893 {
32894 cp_lexer_consume_token (parser->lexer);
32895 expr = integer_zero_node;
32896 }
32897 else
32898 expr = cp_parser_constant_expression (parser);
32899
32900 tile = tree_cons (NULL_TREE, expr, tile);
32901 }
32902 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32903
32904 /* Consume the trailing ')'. */
32905 cp_lexer_consume_token (parser->lexer);
32906
32907 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32908 tile = nreverse (tile);
32909 OMP_CLAUSE_TILE_LIST (c) = tile;
32910 OMP_CLAUSE_CHAIN (c) = list;
32911 return c;
32912 }
32913
32914 /* OpenACC 2.0
32915 Parse wait clause or directive parameters. */
32916
32917 static tree
32918 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32919 {
32920 vec<tree, va_gc> *args;
32921 tree t, args_tree;
32922
32923 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32924 /*cast_p=*/false,
32925 /*allow_expansion_p=*/true,
32926 /*non_constant_p=*/NULL);
32927
32928 if (args == NULL || args->length () == 0)
32929 {
32930 if (args != NULL)
32931 {
32932 cp_parser_error (parser, "expected integer expression list");
32933 release_tree_vector (args);
32934 }
32935 return list;
32936 }
32937
32938 args_tree = build_tree_list_vec (args);
32939
32940 release_tree_vector (args);
32941
32942 for (t = args_tree; t; t = TREE_CHAIN (t))
32943 {
32944 tree targ = TREE_VALUE (t);
32945
32946 if (targ != error_mark_node)
32947 {
32948 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32949 error ("%<wait%> expression must be integral");
32950 else
32951 {
32952 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32953
32954 targ = mark_rvalue_use (targ);
32955 OMP_CLAUSE_DECL (c) = targ;
32956 OMP_CLAUSE_CHAIN (c) = list;
32957 list = c;
32958 }
32959 }
32960 }
32961
32962 return list;
32963 }
32964
32965 /* OpenACC:
32966 wait [( int-expr-list )] */
32967
32968 static tree
32969 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32970 {
32971 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32972
32973 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32974 list = cp_parser_oacc_wait_list (parser, location, list);
32975 else
32976 {
32977 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
32978
32979 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32980 OMP_CLAUSE_CHAIN (c) = list;
32981 list = c;
32982 }
32983
32984 return list;
32985 }
32986
32987 /* OpenMP 3.0:
32988 collapse ( constant-expression ) */
32989
32990 static tree
32991 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32992 {
32993 tree c, num;
32994 location_t loc;
32995 HOST_WIDE_INT n;
32996
32997 loc = cp_lexer_peek_token (parser->lexer)->location;
32998 matching_parens parens;
32999 if (!parens.require_open (parser))
33000 return list;
33001
33002 num = cp_parser_constant_expression (parser);
33003
33004 if (!parens.require_close (parser))
33005 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33006 /*or_comma=*/false,
33007 /*consume_paren=*/true);
33008
33009 if (num == error_mark_node)
33010 return list;
33011 num = fold_non_dependent_expr (num);
33012 if (!tree_fits_shwi_p (num)
33013 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33014 || (n = tree_to_shwi (num)) <= 0
33015 || (int) n != n)
33016 {
33017 error_at (loc, "collapse argument needs positive constant integer expression");
33018 return list;
33019 }
33020
33021 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
33022 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
33023 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
33024 OMP_CLAUSE_CHAIN (c) = list;
33025 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
33026
33027 return c;
33028 }
33029
33030 /* OpenMP 2.5:
33031 default ( none | shared )
33032
33033 OpenACC:
33034 default ( none | present ) */
33035
33036 static tree
33037 cp_parser_omp_clause_default (cp_parser *parser, tree list,
33038 location_t location, bool is_oacc)
33039 {
33040 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
33041 tree c;
33042
33043 matching_parens parens;
33044 if (!parens.require_open (parser))
33045 return list;
33046 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33047 {
33048 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33049 const char *p = IDENTIFIER_POINTER (id);
33050
33051 switch (p[0])
33052 {
33053 case 'n':
33054 if (strcmp ("none", p) != 0)
33055 goto invalid_kind;
33056 kind = OMP_CLAUSE_DEFAULT_NONE;
33057 break;
33058
33059 case 'p':
33060 if (strcmp ("present", p) != 0 || !is_oacc)
33061 goto invalid_kind;
33062 kind = OMP_CLAUSE_DEFAULT_PRESENT;
33063 break;
33064
33065 case 's':
33066 if (strcmp ("shared", p) != 0 || is_oacc)
33067 goto invalid_kind;
33068 kind = OMP_CLAUSE_DEFAULT_SHARED;
33069 break;
33070
33071 default:
33072 goto invalid_kind;
33073 }
33074
33075 cp_lexer_consume_token (parser->lexer);
33076 }
33077 else
33078 {
33079 invalid_kind:
33080 if (is_oacc)
33081 cp_parser_error (parser, "expected %<none%> or %<present%>");
33082 else
33083 cp_parser_error (parser, "expected %<none%> or %<shared%>");
33084 }
33085
33086 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
33087 || !parens.require_close (parser))
33088 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33089 /*or_comma=*/false,
33090 /*consume_paren=*/true);
33091
33092 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
33093 return list;
33094
33095 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
33096 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
33097 OMP_CLAUSE_CHAIN (c) = list;
33098 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
33099
33100 return c;
33101 }
33102
33103 /* OpenMP 3.1:
33104 final ( expression ) */
33105
33106 static tree
33107 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
33108 {
33109 tree t, c;
33110
33111 matching_parens parens;
33112 if (!parens.require_open (parser))
33113 return list;
33114
33115 t = cp_parser_assignment_expression (parser);
33116
33117 if (t == error_mark_node
33118 || !parens.require_close (parser))
33119 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33120 /*or_comma=*/false,
33121 /*consume_paren=*/true);
33122
33123 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
33124
33125 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
33126 OMP_CLAUSE_FINAL_EXPR (c) = t;
33127 OMP_CLAUSE_CHAIN (c) = list;
33128
33129 return c;
33130 }
33131
33132 /* OpenMP 2.5:
33133 if ( expression )
33134
33135 OpenMP 4.5:
33136 if ( directive-name-modifier : expression )
33137
33138 directive-name-modifier:
33139 parallel | task | taskloop | target data | target | target update
33140 | target enter data | target exit data
33141
33142 OpenMP 5.0:
33143 directive-name-modifier:
33144 ... | simd | cancel */
33145
33146 static tree
33147 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
33148 bool is_omp)
33149 {
33150 tree t, c;
33151 enum tree_code if_modifier = ERROR_MARK;
33152
33153 matching_parens parens;
33154 if (!parens.require_open (parser))
33155 return list;
33156
33157 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33158 {
33159 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33160 const char *p = IDENTIFIER_POINTER (id);
33161 int n = 2;
33162
33163 if (strcmp ("cancel", p) == 0)
33164 if_modifier = VOID_CST;
33165 else if (strcmp ("parallel", p) == 0)
33166 if_modifier = OMP_PARALLEL;
33167 else if (strcmp ("simd", p) == 0)
33168 if_modifier = OMP_SIMD;
33169 else if (strcmp ("task", p) == 0)
33170 if_modifier = OMP_TASK;
33171 else if (strcmp ("taskloop", p) == 0)
33172 if_modifier = OMP_TASKLOOP;
33173 else if (strcmp ("target", p) == 0)
33174 {
33175 if_modifier = OMP_TARGET;
33176 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
33177 {
33178 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
33179 p = IDENTIFIER_POINTER (id);
33180 if (strcmp ("data", p) == 0)
33181 if_modifier = OMP_TARGET_DATA;
33182 else if (strcmp ("update", p) == 0)
33183 if_modifier = OMP_TARGET_UPDATE;
33184 else if (strcmp ("enter", p) == 0)
33185 if_modifier = OMP_TARGET_ENTER_DATA;
33186 else if (strcmp ("exit", p) == 0)
33187 if_modifier = OMP_TARGET_EXIT_DATA;
33188 if (if_modifier != OMP_TARGET)
33189 n = 3;
33190 else
33191 {
33192 location_t loc
33193 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
33194 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
33195 "or %<exit%>");
33196 if_modifier = ERROR_MARK;
33197 }
33198 if (if_modifier == OMP_TARGET_ENTER_DATA
33199 || if_modifier == OMP_TARGET_EXIT_DATA)
33200 {
33201 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
33202 {
33203 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
33204 p = IDENTIFIER_POINTER (id);
33205 if (strcmp ("data", p) == 0)
33206 n = 4;
33207 }
33208 if (n != 4)
33209 {
33210 location_t loc
33211 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
33212 error_at (loc, "expected %<data%>");
33213 if_modifier = ERROR_MARK;
33214 }
33215 }
33216 }
33217 }
33218 if (if_modifier != ERROR_MARK)
33219 {
33220 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
33221 {
33222 while (n-- > 0)
33223 cp_lexer_consume_token (parser->lexer);
33224 }
33225 else
33226 {
33227 if (n > 2)
33228 {
33229 location_t loc
33230 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
33231 error_at (loc, "expected %<:%>");
33232 }
33233 if_modifier = ERROR_MARK;
33234 }
33235 }
33236 }
33237
33238 t = cp_parser_assignment_expression (parser);
33239
33240 if (t == error_mark_node
33241 || !parens.require_close (parser))
33242 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33243 /*or_comma=*/false,
33244 /*consume_paren=*/true);
33245
33246 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33247 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
33248 {
33249 if (if_modifier != ERROR_MARK
33250 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33251 {
33252 const char *p = NULL;
33253 switch (if_modifier)
33254 {
33255 case VOID_CST: p = "cancel"; break;
33256 case OMP_PARALLEL: p = "parallel"; break;
33257 case OMP_SIMD: p = "simd"; break;
33258 case OMP_TASK: p = "task"; break;
33259 case OMP_TASKLOOP: p = "taskloop"; break;
33260 case OMP_TARGET_DATA: p = "target data"; break;
33261 case OMP_TARGET: p = "target"; break;
33262 case OMP_TARGET_UPDATE: p = "target update"; break;
33263 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
33264 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
33265 default: gcc_unreachable ();
33266 }
33267 error_at (location, "too many %<if%> clauses with %qs modifier",
33268 p);
33269 return list;
33270 }
33271 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33272 {
33273 if (!is_omp)
33274 error_at (location, "too many %<if%> clauses");
33275 else
33276 error_at (location, "too many %<if%> clauses without modifier");
33277 return list;
33278 }
33279 else if (if_modifier == ERROR_MARK
33280 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
33281 {
33282 error_at (location, "if any %<if%> clause has modifier, then all "
33283 "%<if%> clauses have to use modifier");
33284 return list;
33285 }
33286 }
33287
33288 c = build_omp_clause (location, OMP_CLAUSE_IF);
33289 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
33290 OMP_CLAUSE_IF_EXPR (c) = t;
33291 OMP_CLAUSE_CHAIN (c) = list;
33292
33293 return c;
33294 }
33295
33296 /* OpenMP 3.1:
33297 mergeable */
33298
33299 static tree
33300 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
33301 tree list, location_t location)
33302 {
33303 tree c;
33304
33305 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
33306 location);
33307
33308 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
33309 OMP_CLAUSE_CHAIN (c) = list;
33310 return c;
33311 }
33312
33313 /* OpenMP 2.5:
33314 nowait */
33315
33316 static tree
33317 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
33318 tree list, location_t location)
33319 {
33320 tree c;
33321
33322 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
33323
33324 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
33325 OMP_CLAUSE_CHAIN (c) = list;
33326 return c;
33327 }
33328
33329 /* OpenMP 2.5:
33330 num_threads ( expression ) */
33331
33332 static tree
33333 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
33334 location_t location)
33335 {
33336 tree t, c;
33337
33338 matching_parens parens;
33339 if (!parens.require_open (parser))
33340 return list;
33341
33342 t = cp_parser_assignment_expression (parser);
33343
33344 if (t == error_mark_node
33345 || !parens.require_close (parser))
33346 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33347 /*or_comma=*/false,
33348 /*consume_paren=*/true);
33349
33350 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
33351 "num_threads", location);
33352
33353 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
33354 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
33355 OMP_CLAUSE_CHAIN (c) = list;
33356
33357 return c;
33358 }
33359
33360 /* OpenMP 4.5:
33361 num_tasks ( expression ) */
33362
33363 static tree
33364 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
33365 location_t location)
33366 {
33367 tree t, c;
33368
33369 matching_parens parens;
33370 if (!parens.require_open (parser))
33371 return list;
33372
33373 t = cp_parser_assignment_expression (parser);
33374
33375 if (t == error_mark_node
33376 || !parens.require_close (parser))
33377 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33378 /*or_comma=*/false,
33379 /*consume_paren=*/true);
33380
33381 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
33382 "num_tasks", location);
33383
33384 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
33385 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
33386 OMP_CLAUSE_CHAIN (c) = list;
33387
33388 return c;
33389 }
33390
33391 /* OpenMP 4.5:
33392 grainsize ( expression ) */
33393
33394 static tree
33395 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
33396 location_t location)
33397 {
33398 tree t, c;
33399
33400 matching_parens parens;
33401 if (!parens.require_open (parser))
33402 return list;
33403
33404 t = cp_parser_assignment_expression (parser);
33405
33406 if (t == error_mark_node
33407 || !parens.require_close (parser))
33408 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33409 /*or_comma=*/false,
33410 /*consume_paren=*/true);
33411
33412 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
33413 "grainsize", location);
33414
33415 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
33416 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
33417 OMP_CLAUSE_CHAIN (c) = list;
33418
33419 return c;
33420 }
33421
33422 /* OpenMP 4.5:
33423 priority ( expression ) */
33424
33425 static tree
33426 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
33427 location_t location)
33428 {
33429 tree t, c;
33430
33431 matching_parens parens;
33432 if (!parens.require_open (parser))
33433 return list;
33434
33435 t = cp_parser_assignment_expression (parser);
33436
33437 if (t == error_mark_node
33438 || !parens.require_close (parser))
33439 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33440 /*or_comma=*/false,
33441 /*consume_paren=*/true);
33442
33443 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
33444 "priority", location);
33445
33446 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
33447 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
33448 OMP_CLAUSE_CHAIN (c) = list;
33449
33450 return c;
33451 }
33452
33453 /* OpenMP 4.5:
33454 hint ( expression ) */
33455
33456 static tree
33457 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
33458 {
33459 tree t, c;
33460
33461 matching_parens parens;
33462 if (!parens.require_open (parser))
33463 return list;
33464
33465 t = cp_parser_assignment_expression (parser);
33466
33467 if (t == error_mark_node
33468 || !parens.require_close (parser))
33469 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33470 /*or_comma=*/false,
33471 /*consume_paren=*/true);
33472
33473 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
33474
33475 c = build_omp_clause (location, OMP_CLAUSE_HINT);
33476 OMP_CLAUSE_HINT_EXPR (c) = t;
33477 OMP_CLAUSE_CHAIN (c) = list;
33478
33479 return c;
33480 }
33481
33482 /* OpenMP 4.5:
33483 defaultmap ( tofrom : scalar )
33484
33485 OpenMP 5.0:
33486 defaultmap ( implicit-behavior [ : variable-category ] ) */
33487
33488 static tree
33489 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
33490 location_t location)
33491 {
33492 tree c, id;
33493 const char *p;
33494 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33495 enum omp_clause_defaultmap_kind category
33496 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
33497
33498 matching_parens parens;
33499 if (!parens.require_open (parser))
33500 return list;
33501
33502 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
33503 p = "default";
33504 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33505 {
33506 invalid_behavior:
33507 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
33508 "%<tofrom%>, %<firstprivate%>, %<none%> "
33509 "or %<default%>");
33510 goto out_err;
33511 }
33512 else
33513 {
33514 id = cp_lexer_peek_token (parser->lexer)->u.value;
33515 p = IDENTIFIER_POINTER (id);
33516 }
33517
33518 switch (p[0])
33519 {
33520 case 'a':
33521 if (strcmp ("alloc", p) == 0)
33522 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
33523 else
33524 goto invalid_behavior;
33525 break;
33526
33527 case 'd':
33528 if (strcmp ("default", p) == 0)
33529 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33530 else
33531 goto invalid_behavior;
33532 break;
33533
33534 case 'f':
33535 if (strcmp ("firstprivate", p) == 0)
33536 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
33537 else if (strcmp ("from", p) == 0)
33538 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
33539 else
33540 goto invalid_behavior;
33541 break;
33542
33543 case 'n':
33544 if (strcmp ("none", p) == 0)
33545 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
33546 else
33547 goto invalid_behavior;
33548 break;
33549
33550 case 't':
33551 if (strcmp ("tofrom", p) == 0)
33552 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
33553 else if (strcmp ("to", p) == 0)
33554 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
33555 else
33556 goto invalid_behavior;
33557 break;
33558
33559 default:
33560 goto invalid_behavior;
33561 }
33562 cp_lexer_consume_token (parser->lexer);
33563
33564 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33565 {
33566 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33567 goto out_err;
33568
33569 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33570 {
33571 invalid_category:
33572 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
33573 "%<pointer%>");
33574 goto out_err;
33575 }
33576 id = cp_lexer_peek_token (parser->lexer)->u.value;
33577 p = IDENTIFIER_POINTER (id);
33578
33579 switch (p[0])
33580 {
33581 case 'a':
33582 if (strcmp ("aggregate", p) == 0)
33583 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
33584 else
33585 goto invalid_category;
33586 break;
33587
33588 case 'p':
33589 if (strcmp ("pointer", p) == 0)
33590 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
33591 else
33592 goto invalid_category;
33593 break;
33594
33595 case 's':
33596 if (strcmp ("scalar", p) == 0)
33597 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
33598 else
33599 goto invalid_category;
33600 break;
33601
33602 default:
33603 goto invalid_category;
33604 }
33605
33606 cp_lexer_consume_token (parser->lexer);
33607 }
33608 if (!parens.require_close (parser))
33609 goto out_err;
33610
33611 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33612 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
33613 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33614 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
33615 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
33616 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
33617 {
33618 enum omp_clause_defaultmap_kind cat = category;
33619 location_t loc = OMP_CLAUSE_LOCATION (c);
33620 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
33621 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
33622 p = NULL;
33623 switch (cat)
33624 {
33625 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
33626 p = NULL;
33627 break;
33628 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33629 p = "aggregate";
33630 break;
33631 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33632 p = "pointer";
33633 break;
33634 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33635 p = "scalar";
33636 break;
33637 default:
33638 gcc_unreachable ();
33639 }
33640 if (p)
33641 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33642 p);
33643 else
33644 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33645 "category");
33646 break;
33647 }
33648
33649 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33650 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33651 OMP_CLAUSE_CHAIN (c) = list;
33652 return c;
33653
33654 out_err:
33655 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33656 /*or_comma=*/false,
33657 /*consume_paren=*/true);
33658 return list;
33659 }
33660
33661 /* OpenMP 2.5:
33662 ordered
33663
33664 OpenMP 4.5:
33665 ordered ( constant-expression ) */
33666
33667 static tree
33668 cp_parser_omp_clause_ordered (cp_parser *parser,
33669 tree list, location_t location)
33670 {
33671 tree c, num = NULL_TREE;
33672 HOST_WIDE_INT n;
33673
33674 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33675 "ordered", location);
33676
33677 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33678 {
33679 matching_parens parens;
33680 parens.consume_open (parser);
33681
33682 num = cp_parser_constant_expression (parser);
33683
33684 if (!parens.require_close (parser))
33685 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33686 /*or_comma=*/false,
33687 /*consume_paren=*/true);
33688
33689 if (num == error_mark_node)
33690 return list;
33691 num = fold_non_dependent_expr (num);
33692 if (!tree_fits_shwi_p (num)
33693 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33694 || (n = tree_to_shwi (num)) <= 0
33695 || (int) n != n)
33696 {
33697 error_at (location,
33698 "ordered argument needs positive constant integer "
33699 "expression");
33700 return list;
33701 }
33702 }
33703
33704 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33705 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33706 OMP_CLAUSE_CHAIN (c) = list;
33707 return c;
33708 }
33709
33710 /* OpenMP 2.5:
33711 reduction ( reduction-operator : variable-list )
33712
33713 reduction-operator:
33714 One of: + * - & ^ | && ||
33715
33716 OpenMP 3.1:
33717
33718 reduction-operator:
33719 One of: + * - & ^ | && || min max
33720
33721 OpenMP 4.0:
33722
33723 reduction-operator:
33724 One of: + * - & ^ | && ||
33725 id-expression
33726
33727 OpenMP 5.0:
33728 reduction ( reduction-modifier, reduction-operator : variable-list )
33729 in_reduction ( reduction-operator : variable-list )
33730 task_reduction ( reduction-operator : variable-list ) */
33731
33732 static tree
33733 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33734 bool is_omp, tree list)
33735 {
33736 enum tree_code code = ERROR_MARK;
33737 tree nlist, c, id = NULL_TREE;
33738 bool task = false;
33739 bool inscan = false;
33740
33741 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33742 return list;
33743
33744 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33745 {
33746 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33747 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33748 {
33749 cp_lexer_consume_token (parser->lexer);
33750 cp_lexer_consume_token (parser->lexer);
33751 }
33752 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33753 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33754 {
33755 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33756 const char *p = IDENTIFIER_POINTER (id);
33757 if (strcmp (p, "task") == 0)
33758 task = true;
33759 else if (strcmp (p, "inscan") == 0)
33760 {
33761 inscan = true;
33762 sorry ("%<inscan%> modifier on %<reduction%> clause "
33763 "not supported yet");
33764 }
33765 if (task || inscan)
33766 {
33767 cp_lexer_consume_token (parser->lexer);
33768 cp_lexer_consume_token (parser->lexer);
33769 }
33770 }
33771 }
33772
33773 switch (cp_lexer_peek_token (parser->lexer)->type)
33774 {
33775 case CPP_PLUS: code = PLUS_EXPR; break;
33776 case CPP_MULT: code = MULT_EXPR; break;
33777 case CPP_MINUS: code = MINUS_EXPR; break;
33778 case CPP_AND: code = BIT_AND_EXPR; break;
33779 case CPP_XOR: code = BIT_XOR_EXPR; break;
33780 case CPP_OR: code = BIT_IOR_EXPR; break;
33781 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33782 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33783 default: break;
33784 }
33785
33786 if (code != ERROR_MARK)
33787 cp_lexer_consume_token (parser->lexer);
33788 else
33789 {
33790 bool saved_colon_corrects_to_scope_p;
33791 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33792 parser->colon_corrects_to_scope_p = false;
33793 id = cp_parser_id_expression (parser, /*template_p=*/false,
33794 /*check_dependency_p=*/true,
33795 /*template_p=*/NULL,
33796 /*declarator_p=*/false,
33797 /*optional_p=*/false);
33798 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33799 if (identifier_p (id))
33800 {
33801 const char *p = IDENTIFIER_POINTER (id);
33802
33803 if (strcmp (p, "min") == 0)
33804 code = MIN_EXPR;
33805 else if (strcmp (p, "max") == 0)
33806 code = MAX_EXPR;
33807 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33808 code = PLUS_EXPR;
33809 else if (id == ovl_op_identifier (false, MULT_EXPR))
33810 code = MULT_EXPR;
33811 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33812 code = MINUS_EXPR;
33813 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33814 code = BIT_AND_EXPR;
33815 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33816 code = BIT_IOR_EXPR;
33817 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33818 code = BIT_XOR_EXPR;
33819 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33820 code = TRUTH_ANDIF_EXPR;
33821 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33822 code = TRUTH_ORIF_EXPR;
33823 id = omp_reduction_id (code, id, NULL_TREE);
33824 tree scope = parser->scope;
33825 if (scope)
33826 id = build_qualified_name (NULL_TREE, scope, id, false);
33827 parser->scope = NULL_TREE;
33828 parser->qualifying_scope = NULL_TREE;
33829 parser->object_scope = NULL_TREE;
33830 }
33831 else
33832 {
33833 error ("invalid reduction-identifier");
33834 resync_fail:
33835 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33836 /*or_comma=*/false,
33837 /*consume_paren=*/true);
33838 return list;
33839 }
33840 }
33841
33842 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33843 goto resync_fail;
33844
33845 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33846 NULL);
33847 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33848 {
33849 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33850 if (task)
33851 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33852 else if (inscan)
33853 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33854 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33855 }
33856
33857 return nlist;
33858 }
33859
33860 /* OpenMP 2.5:
33861 schedule ( schedule-kind )
33862 schedule ( schedule-kind , expression )
33863
33864 schedule-kind:
33865 static | dynamic | guided | runtime | auto
33866
33867 OpenMP 4.5:
33868 schedule ( schedule-modifier : schedule-kind )
33869 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33870
33871 schedule-modifier:
33872 simd
33873 monotonic
33874 nonmonotonic */
33875
33876 static tree
33877 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33878 {
33879 tree c, t;
33880 int modifiers = 0, nmodifiers = 0;
33881
33882 matching_parens parens;
33883 if (!parens.require_open (parser))
33884 return list;
33885
33886 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33887
33888 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33889 {
33890 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33891 const char *p = IDENTIFIER_POINTER (id);
33892 if (strcmp ("simd", p) == 0)
33893 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33894 else if (strcmp ("monotonic", p) == 0)
33895 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33896 else if (strcmp ("nonmonotonic", p) == 0)
33897 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33898 else
33899 break;
33900 cp_lexer_consume_token (parser->lexer);
33901 if (nmodifiers++ == 0
33902 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33903 cp_lexer_consume_token (parser->lexer);
33904 else
33905 {
33906 cp_parser_require (parser, CPP_COLON, RT_COLON);
33907 break;
33908 }
33909 }
33910
33911 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33912 {
33913 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33914 const char *p = IDENTIFIER_POINTER (id);
33915
33916 switch (p[0])
33917 {
33918 case 'd':
33919 if (strcmp ("dynamic", p) != 0)
33920 goto invalid_kind;
33921 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33922 break;
33923
33924 case 'g':
33925 if (strcmp ("guided", p) != 0)
33926 goto invalid_kind;
33927 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33928 break;
33929
33930 case 'r':
33931 if (strcmp ("runtime", p) != 0)
33932 goto invalid_kind;
33933 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33934 break;
33935
33936 default:
33937 goto invalid_kind;
33938 }
33939 }
33940 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33941 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33942 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33943 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33944 else
33945 goto invalid_kind;
33946 cp_lexer_consume_token (parser->lexer);
33947
33948 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33949 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33950 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33951 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33952 {
33953 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33954 "specified");
33955 modifiers = 0;
33956 }
33957
33958 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33959 {
33960 cp_token *token;
33961 cp_lexer_consume_token (parser->lexer);
33962
33963 token = cp_lexer_peek_token (parser->lexer);
33964 t = cp_parser_assignment_expression (parser);
33965
33966 if (t == error_mark_node)
33967 goto resync_fail;
33968 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
33969 error_at (token->location, "schedule %<runtime%> does not take "
33970 "a %<chunk_size%> parameter");
33971 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
33972 error_at (token->location, "schedule %<auto%> does not take "
33973 "a %<chunk_size%> parameter");
33974 else
33975 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
33976
33977 if (!parens.require_close (parser))
33978 goto resync_fail;
33979 }
33980 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33981 goto resync_fail;
33982
33983 OMP_CLAUSE_SCHEDULE_KIND (c)
33984 = (enum omp_clause_schedule_kind)
33985 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
33986
33987 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
33988 OMP_CLAUSE_CHAIN (c) = list;
33989 return c;
33990
33991 invalid_kind:
33992 cp_parser_error (parser, "invalid schedule kind");
33993 resync_fail:
33994 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33995 /*or_comma=*/false,
33996 /*consume_paren=*/true);
33997 return list;
33998 }
33999
34000 /* OpenMP 3.0:
34001 untied */
34002
34003 static tree
34004 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
34005 tree list, location_t location)
34006 {
34007 tree c;
34008
34009 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
34010
34011 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
34012 OMP_CLAUSE_CHAIN (c) = list;
34013 return c;
34014 }
34015
34016 /* OpenMP 4.0:
34017 inbranch
34018 notinbranch */
34019
34020 static tree
34021 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
34022 tree list, location_t location)
34023 {
34024 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
34025 tree c = build_omp_clause (location, code);
34026 OMP_CLAUSE_CHAIN (c) = list;
34027 return c;
34028 }
34029
34030 /* OpenMP 4.0:
34031 parallel
34032 for
34033 sections
34034 taskgroup */
34035
34036 static tree
34037 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
34038 enum omp_clause_code code,
34039 tree list, location_t location)
34040 {
34041 tree c = build_omp_clause (location, code);
34042 OMP_CLAUSE_CHAIN (c) = list;
34043 return c;
34044 }
34045
34046 /* OpenMP 4.5:
34047 nogroup */
34048
34049 static tree
34050 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
34051 tree list, location_t location)
34052 {
34053 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
34054 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
34055 OMP_CLAUSE_CHAIN (c) = list;
34056 return c;
34057 }
34058
34059 /* OpenMP 4.5:
34060 simd
34061 threads */
34062
34063 static tree
34064 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
34065 enum omp_clause_code code,
34066 tree list, location_t location)
34067 {
34068 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
34069 tree c = build_omp_clause (location, code);
34070 OMP_CLAUSE_CHAIN (c) = list;
34071 return c;
34072 }
34073
34074 /* OpenMP 4.0:
34075 num_teams ( expression ) */
34076
34077 static tree
34078 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
34079 location_t location)
34080 {
34081 tree t, c;
34082
34083 matching_parens parens;
34084 if (!parens.require_open (parser))
34085 return list;
34086
34087 t = cp_parser_assignment_expression (parser);
34088
34089 if (t == error_mark_node
34090 || !parens.require_close (parser))
34091 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34092 /*or_comma=*/false,
34093 /*consume_paren=*/true);
34094
34095 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
34096 "num_teams", location);
34097
34098 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
34099 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
34100 OMP_CLAUSE_CHAIN (c) = list;
34101
34102 return c;
34103 }
34104
34105 /* OpenMP 4.0:
34106 thread_limit ( expression ) */
34107
34108 static tree
34109 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
34110 location_t location)
34111 {
34112 tree t, c;
34113
34114 matching_parens parens;
34115 if (!parens.require_open (parser))
34116 return list;
34117
34118 t = cp_parser_assignment_expression (parser);
34119
34120 if (t == error_mark_node
34121 || !parens.require_close (parser))
34122 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34123 /*or_comma=*/false,
34124 /*consume_paren=*/true);
34125
34126 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
34127 "thread_limit", location);
34128
34129 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
34130 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
34131 OMP_CLAUSE_CHAIN (c) = list;
34132
34133 return c;
34134 }
34135
34136 /* OpenMP 4.0:
34137 aligned ( variable-list )
34138 aligned ( variable-list : constant-expression ) */
34139
34140 static tree
34141 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
34142 {
34143 tree nlist, c, alignment = NULL_TREE;
34144 bool colon;
34145
34146 matching_parens parens;
34147 if (!parens.require_open (parser))
34148 return list;
34149
34150 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
34151 &colon);
34152
34153 if (colon)
34154 {
34155 alignment = cp_parser_constant_expression (parser);
34156
34157 if (!parens.require_close (parser))
34158 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34159 /*or_comma=*/false,
34160 /*consume_paren=*/true);
34161
34162 if (alignment == error_mark_node)
34163 alignment = NULL_TREE;
34164 }
34165
34166 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34167 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
34168
34169 return nlist;
34170 }
34171
34172 /* OpenMP 2.5:
34173 lastprivate ( variable-list )
34174
34175 OpenMP 5.0:
34176 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
34177
34178 static tree
34179 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
34180 {
34181 bool conditional = false;
34182
34183 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34184 return list;
34185
34186 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34187 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
34188 {
34189 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34190 const char *p = IDENTIFIER_POINTER (id);
34191
34192 if (strcmp ("conditional", p) == 0)
34193 {
34194 conditional = true;
34195 cp_lexer_consume_token (parser->lexer);
34196 cp_lexer_consume_token (parser->lexer);
34197 }
34198 }
34199
34200 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
34201 list, NULL);
34202
34203 if (conditional)
34204 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34205 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
34206 return nlist;
34207 }
34208
34209 /* OpenMP 4.0:
34210 linear ( variable-list )
34211 linear ( variable-list : expression )
34212
34213 OpenMP 4.5:
34214 linear ( modifier ( variable-list ) )
34215 linear ( modifier ( variable-list ) : expression ) */
34216
34217 static tree
34218 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
34219 bool declare_simd)
34220 {
34221 tree nlist, c, step = integer_one_node;
34222 bool colon;
34223 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
34224
34225 matching_parens parens;
34226 if (!parens.require_open (parser))
34227 return list;
34228
34229 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34230 {
34231 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34232 const char *p = IDENTIFIER_POINTER (id);
34233
34234 if (strcmp ("ref", p) == 0)
34235 kind = OMP_CLAUSE_LINEAR_REF;
34236 else if (strcmp ("val", p) == 0)
34237 kind = OMP_CLAUSE_LINEAR_VAL;
34238 else if (strcmp ("uval", p) == 0)
34239 kind = OMP_CLAUSE_LINEAR_UVAL;
34240 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
34241 cp_lexer_consume_token (parser->lexer);
34242 else
34243 kind = OMP_CLAUSE_LINEAR_DEFAULT;
34244 }
34245
34246 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
34247 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
34248 &colon);
34249 else
34250 {
34251 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
34252 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
34253 if (colon)
34254 cp_parser_require (parser, CPP_COLON, RT_COLON);
34255 else if (!parens.require_close (parser))
34256 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34257 /*or_comma=*/false,
34258 /*consume_paren=*/true);
34259 }
34260
34261 if (colon)
34262 {
34263 step = NULL_TREE;
34264 if (declare_simd
34265 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34266 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
34267 {
34268 cp_token *token = cp_lexer_peek_token (parser->lexer);
34269 cp_parser_parse_tentatively (parser);
34270 step = cp_parser_id_expression (parser, /*template_p=*/false,
34271 /*check_dependency_p=*/true,
34272 /*template_p=*/NULL,
34273 /*declarator_p=*/false,
34274 /*optional_p=*/false);
34275 if (step != error_mark_node)
34276 step = cp_parser_lookup_name_simple (parser, step, token->location);
34277 if (step == error_mark_node)
34278 {
34279 step = NULL_TREE;
34280 cp_parser_abort_tentative_parse (parser);
34281 }
34282 else if (!cp_parser_parse_definitely (parser))
34283 step = NULL_TREE;
34284 }
34285 if (!step)
34286 step = cp_parser_assignment_expression (parser);
34287
34288 if (!parens.require_close (parser))
34289 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34290 /*or_comma=*/false,
34291 /*consume_paren=*/true);
34292
34293 if (step == error_mark_node)
34294 return list;
34295 }
34296
34297 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34298 {
34299 OMP_CLAUSE_LINEAR_STEP (c) = step;
34300 OMP_CLAUSE_LINEAR_KIND (c) = kind;
34301 }
34302
34303 return nlist;
34304 }
34305
34306 /* OpenMP 4.0:
34307 safelen ( constant-expression ) */
34308
34309 static tree
34310 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
34311 location_t location)
34312 {
34313 tree t, c;
34314
34315 matching_parens parens;
34316 if (!parens.require_open (parser))
34317 return list;
34318
34319 t = cp_parser_constant_expression (parser);
34320
34321 if (t == error_mark_node
34322 || !parens.require_close (parser))
34323 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34324 /*or_comma=*/false,
34325 /*consume_paren=*/true);
34326
34327 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
34328
34329 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
34330 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
34331 OMP_CLAUSE_CHAIN (c) = list;
34332
34333 return c;
34334 }
34335
34336 /* OpenMP 4.0:
34337 simdlen ( constant-expression ) */
34338
34339 static tree
34340 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
34341 location_t location)
34342 {
34343 tree t, c;
34344
34345 matching_parens parens;
34346 if (!parens.require_open (parser))
34347 return list;
34348
34349 t = cp_parser_constant_expression (parser);
34350
34351 if (t == error_mark_node
34352 || !parens.require_close (parser))
34353 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34354 /*or_comma=*/false,
34355 /*consume_paren=*/true);
34356
34357 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
34358
34359 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
34360 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
34361 OMP_CLAUSE_CHAIN (c) = list;
34362
34363 return c;
34364 }
34365
34366 /* OpenMP 4.5:
34367 vec:
34368 identifier [+/- integer]
34369 vec , identifier [+/- integer]
34370 */
34371
34372 static tree
34373 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
34374 tree list)
34375 {
34376 tree vec = NULL;
34377
34378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34379 {
34380 cp_parser_error (parser, "expected identifier");
34381 return list;
34382 }
34383
34384 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34385 {
34386 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
34387 tree t, identifier = cp_parser_identifier (parser);
34388 tree addend = NULL;
34389
34390 if (identifier == error_mark_node)
34391 t = error_mark_node;
34392 else
34393 {
34394 t = cp_parser_lookup_name_simple
34395 (parser, identifier,
34396 cp_lexer_peek_token (parser->lexer)->location);
34397 if (t == error_mark_node)
34398 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
34399 id_loc);
34400 }
34401
34402 bool neg = false;
34403 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
34404 neg = true;
34405 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
34406 {
34407 addend = integer_zero_node;
34408 goto add_to_vector;
34409 }
34410 cp_lexer_consume_token (parser->lexer);
34411
34412 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
34413 {
34414 cp_parser_error (parser, "expected integer");
34415 return list;
34416 }
34417
34418 addend = cp_lexer_peek_token (parser->lexer)->u.value;
34419 if (TREE_CODE (addend) != INTEGER_CST)
34420 {
34421 cp_parser_error (parser, "expected integer");
34422 return list;
34423 }
34424 cp_lexer_consume_token (parser->lexer);
34425
34426 add_to_vector:
34427 if (t != error_mark_node)
34428 {
34429 vec = tree_cons (addend, t, vec);
34430 if (neg)
34431 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
34432 }
34433
34434 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34435 break;
34436
34437 cp_lexer_consume_token (parser->lexer);
34438 }
34439
34440 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
34441 {
34442 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
34443 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
34444 OMP_CLAUSE_DECL (u) = nreverse (vec);
34445 OMP_CLAUSE_CHAIN (u) = list;
34446 return u;
34447 }
34448 return list;
34449 }
34450
34451 /* OpenMP 5.0:
34452 iterators ( iterators-definition )
34453
34454 iterators-definition:
34455 iterator-specifier
34456 iterator-specifier , iterators-definition
34457
34458 iterator-specifier:
34459 identifier = range-specification
34460 iterator-type identifier = range-specification
34461
34462 range-specification:
34463 begin : end
34464 begin : end : step */
34465
34466 static tree
34467 cp_parser_omp_iterators (cp_parser *parser)
34468 {
34469 tree ret = NULL_TREE, *last = &ret;
34470 cp_lexer_consume_token (parser->lexer);
34471
34472 matching_parens parens;
34473 if (!parens.require_open (parser))
34474 return error_mark_node;
34475
34476 bool saved_colon_corrects_to_scope_p
34477 = parser->colon_corrects_to_scope_p;
34478 bool saved_colon_doesnt_start_class_def_p
34479 = parser->colon_doesnt_start_class_def_p;
34480
34481 do
34482 {
34483 tree iter_type;
34484 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34485 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
34486 iter_type = integer_type_node;
34487 else
34488 {
34489 const char *saved_message
34490 = parser->type_definition_forbidden_message;
34491 parser->type_definition_forbidden_message
34492 = G_("types may not be defined in iterator type");
34493
34494 iter_type = cp_parser_type_id (parser);
34495
34496 parser->type_definition_forbidden_message = saved_message;
34497 }
34498
34499 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34501 {
34502 cp_parser_error (parser, "expected identifier");
34503 break;
34504 }
34505
34506 tree id = cp_parser_identifier (parser);
34507 if (id == error_mark_node)
34508 break;
34509
34510 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34511 break;
34512
34513 parser->colon_corrects_to_scope_p = false;
34514 parser->colon_doesnt_start_class_def_p = true;
34515 tree begin = cp_parser_assignment_expression (parser);
34516
34517 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34518 break;
34519
34520 tree end = cp_parser_assignment_expression (parser);
34521
34522 tree step = integer_one_node;
34523 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34524 {
34525 cp_lexer_consume_token (parser->lexer);
34526 step = cp_parser_assignment_expression (parser);
34527 }
34528
34529 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
34530 DECL_ARTIFICIAL (iter_var) = 1;
34531 DECL_CONTEXT (iter_var) = current_function_decl;
34532 pushdecl (iter_var);
34533
34534 *last = make_tree_vec (6);
34535 TREE_VEC_ELT (*last, 0) = iter_var;
34536 TREE_VEC_ELT (*last, 1) = begin;
34537 TREE_VEC_ELT (*last, 2) = end;
34538 TREE_VEC_ELT (*last, 3) = step;
34539 last = &TREE_CHAIN (*last);
34540
34541 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34542 {
34543 cp_lexer_consume_token (parser->lexer);
34544 continue;
34545 }
34546 break;
34547 }
34548 while (1);
34549
34550 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34551 parser->colon_doesnt_start_class_def_p
34552 = saved_colon_doesnt_start_class_def_p;
34553
34554 if (!parens.require_close (parser))
34555 cp_parser_skip_to_closing_parenthesis (parser,
34556 /*recovering=*/true,
34557 /*or_comma=*/false,
34558 /*consume_paren=*/true);
34559
34560 return ret ? ret : error_mark_node;
34561 }
34562
34563 /* OpenMP 4.0:
34564 depend ( depend-kind : variable-list )
34565
34566 depend-kind:
34567 in | out | inout
34568
34569 OpenMP 4.5:
34570 depend ( source )
34571
34572 depend ( sink : vec )
34573
34574 OpenMP 5.0:
34575 depend ( depend-modifier , depend-kind: variable-list )
34576
34577 depend-kind:
34578 in | out | inout | mutexinoutset | depobj
34579
34580 depend-modifier:
34581 iterator ( iterators-definition ) */
34582
34583 static tree
34584 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
34585 {
34586 tree nlist, c, iterators = NULL_TREE;
34587 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
34588
34589 matching_parens parens;
34590 if (!parens.require_open (parser))
34591 return list;
34592
34593 do
34594 {
34595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34596 goto invalid_kind;
34597
34598 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34599 const char *p = IDENTIFIER_POINTER (id);
34600
34601 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
34602 {
34603 begin_scope (sk_omp, NULL);
34604 iterators = cp_parser_omp_iterators (parser);
34605 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
34606 continue;
34607 }
34608 if (strcmp ("in", p) == 0)
34609 kind = OMP_CLAUSE_DEPEND_IN;
34610 else if (strcmp ("inout", p) == 0)
34611 kind = OMP_CLAUSE_DEPEND_INOUT;
34612 else if (strcmp ("mutexinoutset", p) == 0)
34613 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
34614 else if (strcmp ("out", p) == 0)
34615 kind = OMP_CLAUSE_DEPEND_OUT;
34616 else if (strcmp ("depobj", p) == 0)
34617 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
34618 else if (strcmp ("sink", p) == 0)
34619 kind = OMP_CLAUSE_DEPEND_SINK;
34620 else if (strcmp ("source", p) == 0)
34621 kind = OMP_CLAUSE_DEPEND_SOURCE;
34622 else
34623 goto invalid_kind;
34624 break;
34625 }
34626 while (1);
34627
34628 cp_lexer_consume_token (parser->lexer);
34629
34630 if (iterators
34631 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34632 {
34633 poplevel (0, 1, 0);
34634 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34635 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34636 iterators = NULL_TREE;
34637 }
34638
34639 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34640 {
34641 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34642 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34643 OMP_CLAUSE_DECL (c) = NULL_TREE;
34644 OMP_CLAUSE_CHAIN (c) = list;
34645 if (!parens.require_close (parser))
34646 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34647 /*or_comma=*/false,
34648 /*consume_paren=*/true);
34649 return c;
34650 }
34651
34652 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34653 goto resync_fail;
34654
34655 if (kind == OMP_CLAUSE_DEPEND_SINK)
34656 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34657 else
34658 {
34659 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34660 list, NULL);
34661
34662 if (iterators)
34663 {
34664 tree block = poplevel (1, 1, 0);
34665 if (iterators == error_mark_node)
34666 iterators = NULL_TREE;
34667 else
34668 TREE_VEC_ELT (iterators, 5) = block;
34669 }
34670
34671 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34672 {
34673 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34674 if (iterators)
34675 OMP_CLAUSE_DECL (c)
34676 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34677 }
34678 }
34679 return nlist;
34680
34681 invalid_kind:
34682 cp_parser_error (parser, "invalid depend kind");
34683 resync_fail:
34684 if (iterators)
34685 poplevel (0, 1, 0);
34686 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34687 /*or_comma=*/false,
34688 /*consume_paren=*/true);
34689 return list;
34690 }
34691
34692 /* OpenMP 4.0:
34693 map ( map-kind : variable-list )
34694 map ( variable-list )
34695
34696 map-kind:
34697 alloc | to | from | tofrom
34698
34699 OpenMP 4.5:
34700 map-kind:
34701 alloc | to | from | tofrom | release | delete
34702
34703 map ( always [,] map-kind: variable-list ) */
34704
34705 static tree
34706 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34707 {
34708 tree nlist, c;
34709 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34710 bool always = false;
34711
34712 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34713 return list;
34714
34715 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34716 {
34717 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34718 const char *p = IDENTIFIER_POINTER (id);
34719
34720 if (strcmp ("always", p) == 0)
34721 {
34722 int nth = 2;
34723 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34724 nth++;
34725 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34726 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34727 == RID_DELETE))
34728 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34729 == CPP_COLON))
34730 {
34731 always = true;
34732 cp_lexer_consume_token (parser->lexer);
34733 if (nth == 3)
34734 cp_lexer_consume_token (parser->lexer);
34735 }
34736 }
34737 }
34738
34739 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34740 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34741 {
34742 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34743 const char *p = IDENTIFIER_POINTER (id);
34744
34745 if (strcmp ("alloc", p) == 0)
34746 kind = GOMP_MAP_ALLOC;
34747 else if (strcmp ("to", p) == 0)
34748 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34749 else if (strcmp ("from", p) == 0)
34750 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34751 else if (strcmp ("tofrom", p) == 0)
34752 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34753 else if (strcmp ("release", p) == 0)
34754 kind = GOMP_MAP_RELEASE;
34755 else
34756 {
34757 cp_parser_error (parser, "invalid map kind");
34758 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34759 /*or_comma=*/false,
34760 /*consume_paren=*/true);
34761 return list;
34762 }
34763 cp_lexer_consume_token (parser->lexer);
34764 cp_lexer_consume_token (parser->lexer);
34765 }
34766 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34767 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34768 {
34769 kind = GOMP_MAP_DELETE;
34770 cp_lexer_consume_token (parser->lexer);
34771 cp_lexer_consume_token (parser->lexer);
34772 }
34773
34774 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34775 NULL);
34776
34777 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34778 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34779
34780 return nlist;
34781 }
34782
34783 /* OpenMP 4.0:
34784 device ( expression ) */
34785
34786 static tree
34787 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34788 location_t location)
34789 {
34790 tree t, c;
34791
34792 matching_parens parens;
34793 if (!parens.require_open (parser))
34794 return list;
34795
34796 t = cp_parser_assignment_expression (parser);
34797
34798 if (t == error_mark_node
34799 || !parens.require_close (parser))
34800 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34801 /*or_comma=*/false,
34802 /*consume_paren=*/true);
34803
34804 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34805 "device", location);
34806
34807 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34808 OMP_CLAUSE_DEVICE_ID (c) = t;
34809 OMP_CLAUSE_CHAIN (c) = list;
34810
34811 return c;
34812 }
34813
34814 /* OpenMP 4.0:
34815 dist_schedule ( static )
34816 dist_schedule ( static , expression ) */
34817
34818 static tree
34819 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34820 location_t location)
34821 {
34822 tree c, t;
34823
34824 matching_parens parens;
34825 if (!parens.require_open (parser))
34826 return list;
34827
34828 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34829
34830 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34831 goto invalid_kind;
34832 cp_lexer_consume_token (parser->lexer);
34833
34834 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34835 {
34836 cp_lexer_consume_token (parser->lexer);
34837
34838 t = cp_parser_assignment_expression (parser);
34839
34840 if (t == error_mark_node)
34841 goto resync_fail;
34842 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34843
34844 if (!parens.require_close (parser))
34845 goto resync_fail;
34846 }
34847 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34848 goto resync_fail;
34849
34850 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34851 location);
34852 OMP_CLAUSE_CHAIN (c) = list;
34853 return c;
34854
34855 invalid_kind:
34856 cp_parser_error (parser, "invalid dist_schedule kind");
34857 resync_fail:
34858 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34859 /*or_comma=*/false,
34860 /*consume_paren=*/true);
34861 return list;
34862 }
34863
34864 /* OpenMP 4.0:
34865 proc_bind ( proc-bind-kind )
34866
34867 proc-bind-kind:
34868 master | close | spread */
34869
34870 static tree
34871 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34872 location_t location)
34873 {
34874 tree c;
34875 enum omp_clause_proc_bind_kind kind;
34876
34877 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34878 return list;
34879
34880 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34881 {
34882 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34883 const char *p = IDENTIFIER_POINTER (id);
34884
34885 if (strcmp ("master", p) == 0)
34886 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34887 else if (strcmp ("close", p) == 0)
34888 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34889 else if (strcmp ("spread", p) == 0)
34890 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34891 else
34892 goto invalid_kind;
34893 }
34894 else
34895 goto invalid_kind;
34896
34897 cp_lexer_consume_token (parser->lexer);
34898 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34899 goto resync_fail;
34900
34901 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34902 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34903 location);
34904 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34905 OMP_CLAUSE_CHAIN (c) = list;
34906 return c;
34907
34908 invalid_kind:
34909 cp_parser_error (parser, "invalid depend kind");
34910 resync_fail:
34911 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34912 /*or_comma=*/false,
34913 /*consume_paren=*/true);
34914 return list;
34915 }
34916
34917 /* OpenACC:
34918 async [( int-expr )] */
34919
34920 static tree
34921 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34922 {
34923 tree c, t;
34924 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34925
34926 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34927
34928 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34929 {
34930 matching_parens parens;
34931 parens.consume_open (parser);
34932
34933 t = cp_parser_expression (parser);
34934 if (t == error_mark_node
34935 || !parens.require_close (parser))
34936 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34937 /*or_comma=*/false,
34938 /*consume_paren=*/true);
34939 }
34940
34941 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34942
34943 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34944 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34945 OMP_CLAUSE_CHAIN (c) = list;
34946 list = c;
34947
34948 return list;
34949 }
34950
34951 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34952 is a bitmask in MASK. Return the list of clauses found. */
34953
34954 static tree
34955 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34956 const char *where, cp_token *pragma_tok,
34957 bool finish_p = true)
34958 {
34959 tree clauses = NULL;
34960 bool first = true;
34961
34962 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34963 {
34964 location_t here;
34965 pragma_omp_clause c_kind;
34966 omp_clause_code code;
34967 const char *c_name;
34968 tree prev = clauses;
34969
34970 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34971 cp_lexer_consume_token (parser->lexer);
34972
34973 here = cp_lexer_peek_token (parser->lexer)->location;
34974 c_kind = cp_parser_omp_clause_name (parser);
34975
34976 switch (c_kind)
34977 {
34978 case PRAGMA_OACC_CLAUSE_ASYNC:
34979 clauses = cp_parser_oacc_clause_async (parser, clauses);
34980 c_name = "async";
34981 break;
34982 case PRAGMA_OACC_CLAUSE_AUTO:
34983 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
34984 clauses);
34985 c_name = "auto";
34986 break;
34987 case PRAGMA_OACC_CLAUSE_COLLAPSE:
34988 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
34989 c_name = "collapse";
34990 break;
34991 case PRAGMA_OACC_CLAUSE_COPY:
34992 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34993 c_name = "copy";
34994 break;
34995 case PRAGMA_OACC_CLAUSE_COPYIN:
34996 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34997 c_name = "copyin";
34998 break;
34999 case PRAGMA_OACC_CLAUSE_COPYOUT:
35000 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35001 c_name = "copyout";
35002 break;
35003 case PRAGMA_OACC_CLAUSE_CREATE:
35004 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35005 c_name = "create";
35006 break;
35007 case PRAGMA_OACC_CLAUSE_DELETE:
35008 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35009 c_name = "delete";
35010 break;
35011 case PRAGMA_OMP_CLAUSE_DEFAULT:
35012 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
35013 c_name = "default";
35014 break;
35015 case PRAGMA_OACC_CLAUSE_DEVICE:
35016 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35017 c_name = "device";
35018 break;
35019 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
35020 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
35021 c_name = "deviceptr";
35022 break;
35023 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
35024 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35025 c_name = "device_resident";
35026 break;
35027 case PRAGMA_OACC_CLAUSE_FINALIZE:
35028 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
35029 clauses);
35030 c_name = "finalize";
35031 break;
35032 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
35033 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35034 clauses);
35035 c_name = "firstprivate";
35036 break;
35037 case PRAGMA_OACC_CLAUSE_GANG:
35038 c_name = "gang";
35039 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
35040 c_name, clauses);
35041 break;
35042 case PRAGMA_OACC_CLAUSE_HOST:
35043 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35044 c_name = "host";
35045 break;
35046 case PRAGMA_OACC_CLAUSE_IF:
35047 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
35048 c_name = "if";
35049 break;
35050 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
35051 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
35052 clauses);
35053 c_name = "if_present";
35054 break;
35055 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
35056 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
35057 clauses);
35058 c_name = "independent";
35059 break;
35060 case PRAGMA_OACC_CLAUSE_LINK:
35061 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35062 c_name = "link";
35063 break;
35064 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
35065 code = OMP_CLAUSE_NUM_GANGS;
35066 c_name = "num_gangs";
35067 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35068 clauses);
35069 break;
35070 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
35071 c_name = "num_workers";
35072 code = OMP_CLAUSE_NUM_WORKERS;
35073 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35074 clauses);
35075 break;
35076 case PRAGMA_OACC_CLAUSE_PRESENT:
35077 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35078 c_name = "present";
35079 break;
35080 case PRAGMA_OACC_CLAUSE_PRIVATE:
35081 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35082 clauses);
35083 c_name = "private";
35084 break;
35085 case PRAGMA_OACC_CLAUSE_REDUCTION:
35086 clauses
35087 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35088 false, clauses);
35089 c_name = "reduction";
35090 break;
35091 case PRAGMA_OACC_CLAUSE_SEQ:
35092 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
35093 clauses);
35094 c_name = "seq";
35095 break;
35096 case PRAGMA_OACC_CLAUSE_TILE:
35097 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
35098 c_name = "tile";
35099 break;
35100 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
35101 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35102 clauses);
35103 c_name = "use_device";
35104 break;
35105 case PRAGMA_OACC_CLAUSE_VECTOR:
35106 c_name = "vector";
35107 clauses = cp_parser_oacc_shape_clause (parser, here,
35108 OMP_CLAUSE_VECTOR,
35109 c_name, clauses);
35110 break;
35111 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
35112 c_name = "vector_length";
35113 code = OMP_CLAUSE_VECTOR_LENGTH;
35114 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35115 clauses);
35116 break;
35117 case PRAGMA_OACC_CLAUSE_WAIT:
35118 clauses = cp_parser_oacc_clause_wait (parser, clauses);
35119 c_name = "wait";
35120 break;
35121 case PRAGMA_OACC_CLAUSE_WORKER:
35122 c_name = "worker";
35123 clauses = cp_parser_oacc_shape_clause (parser, here,
35124 OMP_CLAUSE_WORKER,
35125 c_name, clauses);
35126 break;
35127 default:
35128 cp_parser_error (parser, "expected %<#pragma acc%> clause");
35129 goto saw_error;
35130 }
35131
35132 first = false;
35133
35134 if (((mask >> c_kind) & 1) == 0)
35135 {
35136 /* Remove the invalid clause(s) from the list to avoid
35137 confusing the rest of the compiler. */
35138 clauses = prev;
35139 error_at (here, "%qs is not valid for %qs", c_name, where);
35140 }
35141 }
35142
35143 saw_error:
35144 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35145
35146 if (finish_p)
35147 return finish_omp_clauses (clauses, C_ORT_ACC);
35148
35149 return clauses;
35150 }
35151
35152 /* Parse all OpenMP clauses. The set clauses allowed by the directive
35153 is a bitmask in MASK. Return the list of clauses found; the result
35154 of clause default goes in *pdefault. */
35155
35156 static tree
35157 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
35158 const char *where, cp_token *pragma_tok,
35159 bool finish_p = true)
35160 {
35161 tree clauses = NULL;
35162 bool first = true;
35163 cp_token *token = NULL;
35164
35165 /* Don't create location wrapper nodes within OpenMP clauses. */
35166 auto_suppress_location_wrappers sentinel;
35167
35168 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35169 {
35170 pragma_omp_clause c_kind;
35171 const char *c_name;
35172 tree prev = clauses;
35173
35174 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35175 cp_lexer_consume_token (parser->lexer);
35176
35177 token = cp_lexer_peek_token (parser->lexer);
35178 c_kind = cp_parser_omp_clause_name (parser);
35179
35180 switch (c_kind)
35181 {
35182 case PRAGMA_OMP_CLAUSE_COLLAPSE:
35183 clauses = cp_parser_omp_clause_collapse (parser, clauses,
35184 token->location);
35185 c_name = "collapse";
35186 break;
35187 case PRAGMA_OMP_CLAUSE_COPYIN:
35188 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
35189 c_name = "copyin";
35190 break;
35191 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
35192 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
35193 clauses);
35194 c_name = "copyprivate";
35195 break;
35196 case PRAGMA_OMP_CLAUSE_DEFAULT:
35197 clauses = cp_parser_omp_clause_default (parser, clauses,
35198 token->location, false);
35199 c_name = "default";
35200 break;
35201 case PRAGMA_OMP_CLAUSE_FINAL:
35202 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
35203 c_name = "final";
35204 break;
35205 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
35206 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35207 clauses);
35208 c_name = "firstprivate";
35209 break;
35210 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
35211 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
35212 token->location);
35213 c_name = "grainsize";
35214 break;
35215 case PRAGMA_OMP_CLAUSE_HINT:
35216 clauses = cp_parser_omp_clause_hint (parser, clauses,
35217 token->location);
35218 c_name = "hint";
35219 break;
35220 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
35221 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
35222 token->location);
35223 c_name = "defaultmap";
35224 break;
35225 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
35226 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35227 clauses);
35228 c_name = "use_device_ptr";
35229 break;
35230 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
35231 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
35232 clauses);
35233 c_name = "is_device_ptr";
35234 break;
35235 case PRAGMA_OMP_CLAUSE_IF:
35236 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
35237 true);
35238 c_name = "if";
35239 break;
35240 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
35241 clauses
35242 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
35243 true, clauses);
35244 c_name = "in_reduction";
35245 break;
35246 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
35247 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
35248 c_name = "lastprivate";
35249 break;
35250 case PRAGMA_OMP_CLAUSE_MERGEABLE:
35251 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
35252 token->location);
35253 c_name = "mergeable";
35254 break;
35255 case PRAGMA_OMP_CLAUSE_NOWAIT:
35256 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
35257 c_name = "nowait";
35258 break;
35259 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
35260 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
35261 token->location);
35262 c_name = "num_tasks";
35263 break;
35264 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
35265 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
35266 token->location);
35267 c_name = "num_threads";
35268 break;
35269 case PRAGMA_OMP_CLAUSE_ORDERED:
35270 clauses = cp_parser_omp_clause_ordered (parser, clauses,
35271 token->location);
35272 c_name = "ordered";
35273 break;
35274 case PRAGMA_OMP_CLAUSE_PRIORITY:
35275 clauses = cp_parser_omp_clause_priority (parser, clauses,
35276 token->location);
35277 c_name = "priority";
35278 break;
35279 case PRAGMA_OMP_CLAUSE_PRIVATE:
35280 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35281 clauses);
35282 c_name = "private";
35283 break;
35284 case PRAGMA_OMP_CLAUSE_REDUCTION:
35285 clauses
35286 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35287 true, clauses);
35288 c_name = "reduction";
35289 break;
35290 case PRAGMA_OMP_CLAUSE_SCHEDULE:
35291 clauses = cp_parser_omp_clause_schedule (parser, clauses,
35292 token->location);
35293 c_name = "schedule";
35294 break;
35295 case PRAGMA_OMP_CLAUSE_SHARED:
35296 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
35297 clauses);
35298 c_name = "shared";
35299 break;
35300 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
35301 clauses
35302 = cp_parser_omp_clause_reduction (parser,
35303 OMP_CLAUSE_TASK_REDUCTION,
35304 true, clauses);
35305 c_name = "task_reduction";
35306 break;
35307 case PRAGMA_OMP_CLAUSE_UNTIED:
35308 clauses = cp_parser_omp_clause_untied (parser, clauses,
35309 token->location);
35310 c_name = "untied";
35311 break;
35312 case PRAGMA_OMP_CLAUSE_INBRANCH:
35313 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
35314 clauses, token->location);
35315 c_name = "inbranch";
35316 break;
35317 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
35318 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
35319 clauses);
35320 c_name = "nontemporal";
35321 break;
35322 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
35323 clauses = cp_parser_omp_clause_branch (parser,
35324 OMP_CLAUSE_NOTINBRANCH,
35325 clauses, token->location);
35326 c_name = "notinbranch";
35327 break;
35328 case PRAGMA_OMP_CLAUSE_PARALLEL:
35329 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
35330 clauses, token->location);
35331 c_name = "parallel";
35332 if (!first)
35333 {
35334 clause_not_first:
35335 error_at (token->location, "%qs must be the first clause of %qs",
35336 c_name, where);
35337 clauses = prev;
35338 }
35339 break;
35340 case PRAGMA_OMP_CLAUSE_FOR:
35341 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
35342 clauses, token->location);
35343 c_name = "for";
35344 if (!first)
35345 goto clause_not_first;
35346 break;
35347 case PRAGMA_OMP_CLAUSE_SECTIONS:
35348 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
35349 clauses, token->location);
35350 c_name = "sections";
35351 if (!first)
35352 goto clause_not_first;
35353 break;
35354 case PRAGMA_OMP_CLAUSE_TASKGROUP:
35355 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
35356 clauses, token->location);
35357 c_name = "taskgroup";
35358 if (!first)
35359 goto clause_not_first;
35360 break;
35361 case PRAGMA_OMP_CLAUSE_LINK:
35362 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
35363 c_name = "to";
35364 break;
35365 case PRAGMA_OMP_CLAUSE_TO:
35366 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
35367 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35368 clauses);
35369 else
35370 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
35371 c_name = "to";
35372 break;
35373 case PRAGMA_OMP_CLAUSE_FROM:
35374 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
35375 c_name = "from";
35376 break;
35377 case PRAGMA_OMP_CLAUSE_UNIFORM:
35378 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
35379 clauses);
35380 c_name = "uniform";
35381 break;
35382 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
35383 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
35384 token->location);
35385 c_name = "num_teams";
35386 break;
35387 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
35388 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
35389 token->location);
35390 c_name = "thread_limit";
35391 break;
35392 case PRAGMA_OMP_CLAUSE_ALIGNED:
35393 clauses = cp_parser_omp_clause_aligned (parser, clauses);
35394 c_name = "aligned";
35395 break;
35396 case PRAGMA_OMP_CLAUSE_LINEAR:
35397 {
35398 bool declare_simd = false;
35399 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
35400 declare_simd = true;
35401 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
35402 }
35403 c_name = "linear";
35404 break;
35405 case PRAGMA_OMP_CLAUSE_DEPEND:
35406 clauses = cp_parser_omp_clause_depend (parser, clauses,
35407 token->location);
35408 c_name = "depend";
35409 break;
35410 case PRAGMA_OMP_CLAUSE_MAP:
35411 clauses = cp_parser_omp_clause_map (parser, clauses);
35412 c_name = "map";
35413 break;
35414 case PRAGMA_OMP_CLAUSE_DEVICE:
35415 clauses = cp_parser_omp_clause_device (parser, clauses,
35416 token->location);
35417 c_name = "device";
35418 break;
35419 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
35420 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
35421 token->location);
35422 c_name = "dist_schedule";
35423 break;
35424 case PRAGMA_OMP_CLAUSE_PROC_BIND:
35425 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
35426 token->location);
35427 c_name = "proc_bind";
35428 break;
35429 case PRAGMA_OMP_CLAUSE_SAFELEN:
35430 clauses = cp_parser_omp_clause_safelen (parser, clauses,
35431 token->location);
35432 c_name = "safelen";
35433 break;
35434 case PRAGMA_OMP_CLAUSE_SIMDLEN:
35435 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
35436 token->location);
35437 c_name = "simdlen";
35438 break;
35439 case PRAGMA_OMP_CLAUSE_NOGROUP:
35440 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
35441 token->location);
35442 c_name = "nogroup";
35443 break;
35444 case PRAGMA_OMP_CLAUSE_THREADS:
35445 clauses
35446 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
35447 clauses, token->location);
35448 c_name = "threads";
35449 break;
35450 case PRAGMA_OMP_CLAUSE_SIMD:
35451 clauses
35452 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
35453 clauses, token->location);
35454 c_name = "simd";
35455 break;
35456 default:
35457 cp_parser_error (parser, "expected %<#pragma omp%> clause");
35458 goto saw_error;
35459 }
35460
35461 first = false;
35462
35463 if (((mask >> c_kind) & 1) == 0)
35464 {
35465 /* Remove the invalid clause(s) from the list to avoid
35466 confusing the rest of the compiler. */
35467 clauses = prev;
35468 error_at (token->location, "%qs is not valid for %qs", c_name, where);
35469 }
35470 }
35471 saw_error:
35472 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35473 if (finish_p)
35474 {
35475 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
35476 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
35477 else
35478 return finish_omp_clauses (clauses, C_ORT_OMP);
35479 }
35480 return clauses;
35481 }
35482
35483 /* OpenMP 2.5:
35484 structured-block:
35485 statement
35486
35487 In practice, we're also interested in adding the statement to an
35488 outer node. So it is convenient if we work around the fact that
35489 cp_parser_statement calls add_stmt. */
35490
35491 static unsigned
35492 cp_parser_begin_omp_structured_block (cp_parser *parser)
35493 {
35494 unsigned save = parser->in_statement;
35495
35496 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35497 This preserves the "not within loop or switch" style error messages
35498 for nonsense cases like
35499 void foo() {
35500 #pragma omp single
35501 break;
35502 }
35503 */
35504 if (parser->in_statement)
35505 parser->in_statement = IN_OMP_BLOCK;
35506
35507 return save;
35508 }
35509
35510 static void
35511 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
35512 {
35513 parser->in_statement = save;
35514 }
35515
35516 static tree
35517 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
35518 {
35519 tree stmt = begin_omp_structured_block ();
35520 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35521
35522 cp_parser_statement (parser, NULL_TREE, false, if_p);
35523
35524 cp_parser_end_omp_structured_block (parser, save);
35525 return finish_omp_structured_block (stmt);
35526 }
35527
35528 /* OpenMP 2.5:
35529 # pragma omp atomic new-line
35530 expression-stmt
35531
35532 expression-stmt:
35533 x binop= expr | x++ | ++x | x-- | --x
35534 binop:
35535 +, *, -, /, &, ^, |, <<, >>
35536
35537 where x is an lvalue expression with scalar type.
35538
35539 OpenMP 3.1:
35540 # pragma omp atomic new-line
35541 update-stmt
35542
35543 # pragma omp atomic read new-line
35544 read-stmt
35545
35546 # pragma omp atomic write new-line
35547 write-stmt
35548
35549 # pragma omp atomic update new-line
35550 update-stmt
35551
35552 # pragma omp atomic capture new-line
35553 capture-stmt
35554
35555 # pragma omp atomic capture new-line
35556 capture-block
35557
35558 read-stmt:
35559 v = x
35560 write-stmt:
35561 x = expr
35562 update-stmt:
35563 expression-stmt | x = x binop expr
35564 capture-stmt:
35565 v = expression-stmt
35566 capture-block:
35567 { v = x; update-stmt; } | { update-stmt; v = x; }
35568
35569 OpenMP 4.0:
35570 update-stmt:
35571 expression-stmt | x = x binop expr | x = expr binop x
35572 capture-stmt:
35573 v = update-stmt
35574 capture-block:
35575 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35576
35577 where x and v are lvalue expressions with scalar type. */
35578
35579 static void
35580 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
35581 {
35582 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
35583 tree rhs1 = NULL_TREE, orig_lhs;
35584 location_t loc = pragma_tok->location;
35585 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
35586 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
35587 bool structured_block = false;
35588 bool first = true;
35589 tree clauses = NULL_TREE;
35590
35591 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35592 {
35593 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35594 cp_lexer_consume_token (parser->lexer);
35595
35596 first = false;
35597
35598 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35599 {
35600 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35601 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
35602 const char *p = IDENTIFIER_POINTER (id);
35603 enum tree_code new_code = ERROR_MARK;
35604 enum omp_memory_order new_memory_order
35605 = OMP_MEMORY_ORDER_UNSPECIFIED;
35606
35607 if (!strcmp (p, "read"))
35608 new_code = OMP_ATOMIC_READ;
35609 else if (!strcmp (p, "write"))
35610 new_code = NOP_EXPR;
35611 else if (!strcmp (p, "update"))
35612 new_code = OMP_ATOMIC;
35613 else if (!strcmp (p, "capture"))
35614 new_code = OMP_ATOMIC_CAPTURE_NEW;
35615 else if (!strcmp (p, "seq_cst"))
35616 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35617 else if (!strcmp (p, "acq_rel"))
35618 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35619 else if (!strcmp (p, "release"))
35620 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
35621 else if (!strcmp (p, "acquire"))
35622 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35623 else if (!strcmp (p, "relaxed"))
35624 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
35625 else if (!strcmp (p, "hint"))
35626 {
35627 cp_lexer_consume_token (parser->lexer);
35628 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
35629 continue;
35630 }
35631 else
35632 {
35633 p = NULL;
35634 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35635 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35636 "%<release%>, %<relaxed%> or %<hint%> clause");
35637 }
35638 if (p)
35639 {
35640 if (new_code != ERROR_MARK)
35641 {
35642 if (code != ERROR_MARK)
35643 error_at (cloc, "too many atomic clauses");
35644 else
35645 code = new_code;
35646 }
35647 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35648 {
35649 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35650 error_at (cloc, "too many memory order clauses");
35651 else
35652 memory_order = new_memory_order;
35653 }
35654 cp_lexer_consume_token (parser->lexer);
35655 continue;
35656 }
35657 }
35658 break;
35659 }
35660 cp_parser_require_pragma_eol (parser, pragma_tok);
35661
35662 if (code == ERROR_MARK)
35663 code = OMP_ATOMIC;
35664 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35665 {
35666 omp_requires_mask
35667 = (enum omp_requires) (omp_requires_mask
35668 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35669 switch ((enum omp_memory_order)
35670 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35671 {
35672 case OMP_MEMORY_ORDER_UNSPECIFIED:
35673 case OMP_MEMORY_ORDER_RELAXED:
35674 memory_order = OMP_MEMORY_ORDER_RELAXED;
35675 break;
35676 case OMP_MEMORY_ORDER_SEQ_CST:
35677 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35678 break;
35679 case OMP_MEMORY_ORDER_ACQ_REL:
35680 switch (code)
35681 {
35682 case OMP_ATOMIC_READ:
35683 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35684 break;
35685 case NOP_EXPR: /* atomic write */
35686 case OMP_ATOMIC:
35687 memory_order = OMP_MEMORY_ORDER_RELEASE;
35688 break;
35689 default:
35690 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35691 break;
35692 }
35693 break;
35694 default:
35695 gcc_unreachable ();
35696 }
35697 }
35698 else
35699 switch (code)
35700 {
35701 case OMP_ATOMIC_READ:
35702 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35703 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35704 {
35705 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35706 "%<acq_rel%> or %<release%> clauses");
35707 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35708 }
35709 break;
35710 case NOP_EXPR: /* atomic write */
35711 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35712 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35713 {
35714 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35715 "%<acq_rel%> or %<acquire%> clauses");
35716 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35717 }
35718 break;
35719 case OMP_ATOMIC:
35720 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35721 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35722 {
35723 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35724 "%<acq_rel%> or %<acquire%> clauses");
35725 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35726 }
35727 break;
35728 default:
35729 break;
35730 }
35731
35732 switch (code)
35733 {
35734 case OMP_ATOMIC_READ:
35735 case NOP_EXPR: /* atomic write */
35736 v = cp_parser_unary_expression (parser);
35737 if (v == error_mark_node)
35738 goto saw_error;
35739 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35740 goto saw_error;
35741 if (code == NOP_EXPR)
35742 lhs = cp_parser_expression (parser);
35743 else
35744 lhs = cp_parser_unary_expression (parser);
35745 if (lhs == error_mark_node)
35746 goto saw_error;
35747 if (code == NOP_EXPR)
35748 {
35749 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35750 opcode. */
35751 code = OMP_ATOMIC;
35752 rhs = lhs;
35753 lhs = v;
35754 v = NULL_TREE;
35755 }
35756 goto done;
35757 case OMP_ATOMIC_CAPTURE_NEW:
35758 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35759 {
35760 cp_lexer_consume_token (parser->lexer);
35761 structured_block = true;
35762 }
35763 else
35764 {
35765 v = cp_parser_unary_expression (parser);
35766 if (v == error_mark_node)
35767 goto saw_error;
35768 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35769 goto saw_error;
35770 }
35771 default:
35772 break;
35773 }
35774
35775 restart:
35776 lhs = cp_parser_unary_expression (parser);
35777 orig_lhs = lhs;
35778 switch (TREE_CODE (lhs))
35779 {
35780 case ERROR_MARK:
35781 goto saw_error;
35782
35783 case POSTINCREMENT_EXPR:
35784 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35785 code = OMP_ATOMIC_CAPTURE_OLD;
35786 /* FALLTHROUGH */
35787 case PREINCREMENT_EXPR:
35788 lhs = TREE_OPERAND (lhs, 0);
35789 opcode = PLUS_EXPR;
35790 rhs = integer_one_node;
35791 break;
35792
35793 case POSTDECREMENT_EXPR:
35794 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35795 code = OMP_ATOMIC_CAPTURE_OLD;
35796 /* FALLTHROUGH */
35797 case PREDECREMENT_EXPR:
35798 lhs = TREE_OPERAND (lhs, 0);
35799 opcode = MINUS_EXPR;
35800 rhs = integer_one_node;
35801 break;
35802
35803 case COMPOUND_EXPR:
35804 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35805 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35806 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35807 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35808 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35809 (TREE_OPERAND (lhs, 1), 0), 0)))
35810 == BOOLEAN_TYPE)
35811 /* Undo effects of boolean_increment for post {in,de}crement. */
35812 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35813 /* FALLTHRU */
35814 case MODIFY_EXPR:
35815 if (TREE_CODE (lhs) == MODIFY_EXPR
35816 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35817 {
35818 /* Undo effects of boolean_increment. */
35819 if (integer_onep (TREE_OPERAND (lhs, 1)))
35820 {
35821 /* This is pre or post increment. */
35822 rhs = TREE_OPERAND (lhs, 1);
35823 lhs = TREE_OPERAND (lhs, 0);
35824 opcode = NOP_EXPR;
35825 if (code == OMP_ATOMIC_CAPTURE_NEW
35826 && !structured_block
35827 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35828 code = OMP_ATOMIC_CAPTURE_OLD;
35829 break;
35830 }
35831 }
35832 /* FALLTHRU */
35833 default:
35834 switch (cp_lexer_peek_token (parser->lexer)->type)
35835 {
35836 case CPP_MULT_EQ:
35837 opcode = MULT_EXPR;
35838 break;
35839 case CPP_DIV_EQ:
35840 opcode = TRUNC_DIV_EXPR;
35841 break;
35842 case CPP_PLUS_EQ:
35843 opcode = PLUS_EXPR;
35844 break;
35845 case CPP_MINUS_EQ:
35846 opcode = MINUS_EXPR;
35847 break;
35848 case CPP_LSHIFT_EQ:
35849 opcode = LSHIFT_EXPR;
35850 break;
35851 case CPP_RSHIFT_EQ:
35852 opcode = RSHIFT_EXPR;
35853 break;
35854 case CPP_AND_EQ:
35855 opcode = BIT_AND_EXPR;
35856 break;
35857 case CPP_OR_EQ:
35858 opcode = BIT_IOR_EXPR;
35859 break;
35860 case CPP_XOR_EQ:
35861 opcode = BIT_XOR_EXPR;
35862 break;
35863 case CPP_EQ:
35864 enum cp_parser_prec oprec;
35865 cp_token *token;
35866 cp_lexer_consume_token (parser->lexer);
35867 cp_parser_parse_tentatively (parser);
35868 rhs1 = cp_parser_simple_cast_expression (parser);
35869 if (rhs1 == error_mark_node)
35870 {
35871 cp_parser_abort_tentative_parse (parser);
35872 cp_parser_simple_cast_expression (parser);
35873 goto saw_error;
35874 }
35875 token = cp_lexer_peek_token (parser->lexer);
35876 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35877 {
35878 cp_parser_abort_tentative_parse (parser);
35879 cp_parser_parse_tentatively (parser);
35880 rhs = cp_parser_binary_expression (parser, false, true,
35881 PREC_NOT_OPERATOR, NULL);
35882 if (rhs == error_mark_node)
35883 {
35884 cp_parser_abort_tentative_parse (parser);
35885 cp_parser_binary_expression (parser, false, true,
35886 PREC_NOT_OPERATOR, NULL);
35887 goto saw_error;
35888 }
35889 switch (TREE_CODE (rhs))
35890 {
35891 case MULT_EXPR:
35892 case TRUNC_DIV_EXPR:
35893 case RDIV_EXPR:
35894 case PLUS_EXPR:
35895 case MINUS_EXPR:
35896 case LSHIFT_EXPR:
35897 case RSHIFT_EXPR:
35898 case BIT_AND_EXPR:
35899 case BIT_IOR_EXPR:
35900 case BIT_XOR_EXPR:
35901 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35902 {
35903 if (cp_parser_parse_definitely (parser))
35904 {
35905 opcode = TREE_CODE (rhs);
35906 rhs1 = TREE_OPERAND (rhs, 0);
35907 rhs = TREE_OPERAND (rhs, 1);
35908 goto stmt_done;
35909 }
35910 else
35911 goto saw_error;
35912 }
35913 break;
35914 default:
35915 break;
35916 }
35917 cp_parser_abort_tentative_parse (parser);
35918 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35919 {
35920 rhs = cp_parser_expression (parser);
35921 if (rhs == error_mark_node)
35922 goto saw_error;
35923 opcode = NOP_EXPR;
35924 rhs1 = NULL_TREE;
35925 goto stmt_done;
35926 }
35927 cp_parser_error (parser,
35928 "invalid form of %<#pragma omp atomic%>");
35929 goto saw_error;
35930 }
35931 if (!cp_parser_parse_definitely (parser))
35932 goto saw_error;
35933 switch (token->type)
35934 {
35935 case CPP_SEMICOLON:
35936 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35937 {
35938 code = OMP_ATOMIC_CAPTURE_OLD;
35939 v = lhs;
35940 lhs = NULL_TREE;
35941 lhs1 = rhs1;
35942 rhs1 = NULL_TREE;
35943 cp_lexer_consume_token (parser->lexer);
35944 goto restart;
35945 }
35946 else if (structured_block)
35947 {
35948 opcode = NOP_EXPR;
35949 rhs = rhs1;
35950 rhs1 = NULL_TREE;
35951 goto stmt_done;
35952 }
35953 cp_parser_error (parser,
35954 "invalid form of %<#pragma omp atomic%>");
35955 goto saw_error;
35956 case CPP_MULT:
35957 opcode = MULT_EXPR;
35958 break;
35959 case CPP_DIV:
35960 opcode = TRUNC_DIV_EXPR;
35961 break;
35962 case CPP_PLUS:
35963 opcode = PLUS_EXPR;
35964 break;
35965 case CPP_MINUS:
35966 opcode = MINUS_EXPR;
35967 break;
35968 case CPP_LSHIFT:
35969 opcode = LSHIFT_EXPR;
35970 break;
35971 case CPP_RSHIFT:
35972 opcode = RSHIFT_EXPR;
35973 break;
35974 case CPP_AND:
35975 opcode = BIT_AND_EXPR;
35976 break;
35977 case CPP_OR:
35978 opcode = BIT_IOR_EXPR;
35979 break;
35980 case CPP_XOR:
35981 opcode = BIT_XOR_EXPR;
35982 break;
35983 default:
35984 cp_parser_error (parser,
35985 "invalid operator for %<#pragma omp atomic%>");
35986 goto saw_error;
35987 }
35988 oprec = TOKEN_PRECEDENCE (token);
35989 gcc_assert (oprec != PREC_NOT_OPERATOR);
35990 if (commutative_tree_code (opcode))
35991 oprec = (enum cp_parser_prec) (oprec - 1);
35992 cp_lexer_consume_token (parser->lexer);
35993 rhs = cp_parser_binary_expression (parser, false, false,
35994 oprec, NULL);
35995 if (rhs == error_mark_node)
35996 goto saw_error;
35997 goto stmt_done;
35998 /* FALLTHROUGH */
35999 default:
36000 cp_parser_error (parser,
36001 "invalid operator for %<#pragma omp atomic%>");
36002 goto saw_error;
36003 }
36004 cp_lexer_consume_token (parser->lexer);
36005
36006 rhs = cp_parser_expression (parser);
36007 if (rhs == error_mark_node)
36008 goto saw_error;
36009 break;
36010 }
36011 stmt_done:
36012 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
36013 {
36014 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
36015 goto saw_error;
36016 v = cp_parser_unary_expression (parser);
36017 if (v == error_mark_node)
36018 goto saw_error;
36019 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36020 goto saw_error;
36021 lhs1 = cp_parser_unary_expression (parser);
36022 if (lhs1 == error_mark_node)
36023 goto saw_error;
36024 }
36025 if (structured_block)
36026 {
36027 cp_parser_consume_semicolon_at_end_of_statement (parser);
36028 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
36029 }
36030 done:
36031 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36032 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
36033 rhs1, clauses, memory_order);
36034 if (!structured_block)
36035 cp_parser_consume_semicolon_at_end_of_statement (parser);
36036 return;
36037
36038 saw_error:
36039 cp_parser_skip_to_end_of_block_or_statement (parser);
36040 if (structured_block)
36041 {
36042 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36043 cp_lexer_consume_token (parser->lexer);
36044 else if (code == OMP_ATOMIC_CAPTURE_NEW)
36045 {
36046 cp_parser_skip_to_end_of_block_or_statement (parser);
36047 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36048 cp_lexer_consume_token (parser->lexer);
36049 }
36050 }
36051 }
36052
36053
36054 /* OpenMP 2.5:
36055 # pragma omp barrier new-line */
36056
36057 static void
36058 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
36059 {
36060 cp_parser_require_pragma_eol (parser, pragma_tok);
36061 finish_omp_barrier ();
36062 }
36063
36064 /* OpenMP 2.5:
36065 # pragma omp critical [(name)] new-line
36066 structured-block
36067
36068 OpenMP 4.5:
36069 # pragma omp critical [(name) [hint(expression)]] new-line
36070 structured-block */
36071
36072 #define OMP_CRITICAL_CLAUSE_MASK \
36073 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
36074
36075 static tree
36076 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36077 {
36078 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
36079
36080 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36081 {
36082 matching_parens parens;
36083 parens.consume_open (parser);
36084
36085 name = cp_parser_identifier (parser);
36086
36087 if (name == error_mark_node
36088 || !parens.require_close (parser))
36089 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36090 /*or_comma=*/false,
36091 /*consume_paren=*/true);
36092 if (name == error_mark_node)
36093 name = NULL;
36094
36095 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
36096 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
36097 cp_lexer_consume_token (parser->lexer);
36098
36099 clauses = cp_parser_omp_all_clauses (parser,
36100 OMP_CRITICAL_CLAUSE_MASK,
36101 "#pragma omp critical", pragma_tok);
36102 }
36103 else
36104 cp_parser_require_pragma_eol (parser, pragma_tok);
36105
36106 stmt = cp_parser_omp_structured_block (parser, if_p);
36107 return c_finish_omp_critical (input_location, stmt, name, clauses);
36108 }
36109
36110 /* OpenMP 5.0:
36111 # pragma omp depobj ( depobj ) depobj-clause new-line
36112
36113 depobj-clause:
36114 depend (dependence-type : locator)
36115 destroy
36116 update (dependence-type)
36117
36118 dependence-type:
36119 in
36120 out
36121 inout
36122 mutexinout */
36123
36124 static void
36125 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
36126 {
36127 location_t loc = pragma_tok->location;
36128 matching_parens parens;
36129 if (!parens.require_open (parser))
36130 {
36131 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36132 return;
36133 }
36134
36135 tree depobj = cp_parser_assignment_expression (parser);
36136
36137 if (!parens.require_close (parser))
36138 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36139 /*or_comma=*/false,
36140 /*consume_paren=*/true);
36141
36142 tree clause = NULL_TREE;
36143 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
36144 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
36145 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36146 {
36147 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36148 const char *p = IDENTIFIER_POINTER (id);
36149
36150 cp_lexer_consume_token (parser->lexer);
36151 if (!strcmp ("depend", p))
36152 {
36153 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
36154 if (clause)
36155 clause = finish_omp_clauses (clause, C_ORT_OMP);
36156 if (!clause)
36157 clause = error_mark_node;
36158 }
36159 else if (!strcmp ("destroy", p))
36160 kind = OMP_CLAUSE_DEPEND_LAST;
36161 else if (!strcmp ("update", p))
36162 {
36163 matching_parens c_parens;
36164 if (c_parens.require_open (parser))
36165 {
36166 location_t c2_loc
36167 = cp_lexer_peek_token (parser->lexer)->location;
36168 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36169 {
36170 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
36171 const char *p2 = IDENTIFIER_POINTER (id2);
36172
36173 cp_lexer_consume_token (parser->lexer);
36174 if (!strcmp ("in", p2))
36175 kind = OMP_CLAUSE_DEPEND_IN;
36176 else if (!strcmp ("out", p2))
36177 kind = OMP_CLAUSE_DEPEND_OUT;
36178 else if (!strcmp ("inout", p2))
36179 kind = OMP_CLAUSE_DEPEND_INOUT;
36180 else if (!strcmp ("mutexinoutset", p2))
36181 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
36182 }
36183 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
36184 {
36185 clause = error_mark_node;
36186 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
36187 "%<mutexinoutset%>");
36188 }
36189 if (!c_parens.require_close (parser))
36190 cp_parser_skip_to_closing_parenthesis (parser,
36191 /*recovering=*/true,
36192 /*or_comma=*/false,
36193 /*consume_paren=*/true);
36194 }
36195 else
36196 clause = error_mark_node;
36197 }
36198 }
36199 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
36200 {
36201 clause = error_mark_node;
36202 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
36203 }
36204 cp_parser_require_pragma_eol (parser, pragma_tok);
36205
36206 finish_omp_depobj (loc, depobj, kind, clause);
36207 }
36208
36209
36210 /* OpenMP 2.5:
36211 # pragma omp flush flush-vars[opt] new-line
36212
36213 flush-vars:
36214 ( variable-list )
36215
36216 OpenMP 5.0:
36217 # pragma omp flush memory-order-clause new-line */
36218
36219 static void
36220 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
36221 {
36222 enum memmodel mo = MEMMODEL_LAST;
36223 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36224 {
36225 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36226 const char *p = IDENTIFIER_POINTER (id);
36227 if (!strcmp (p, "acq_rel"))
36228 mo = MEMMODEL_ACQ_REL;
36229 else if (!strcmp (p, "release"))
36230 mo = MEMMODEL_RELEASE;
36231 else if (!strcmp (p, "acquire"))
36232 mo = MEMMODEL_ACQUIRE;
36233 else
36234 error_at (cp_lexer_peek_token (parser->lexer)->location,
36235 "expected %<acq_rel%>, %<release%> or %<acquire%>");
36236 cp_lexer_consume_token (parser->lexer);
36237 }
36238 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36239 {
36240 if (mo != MEMMODEL_LAST)
36241 error_at (cp_lexer_peek_token (parser->lexer)->location,
36242 "%<flush%> list specified together with memory order "
36243 "clause");
36244 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36245 }
36246 cp_parser_require_pragma_eol (parser, pragma_tok);
36247
36248 finish_omp_flush (mo);
36249 }
36250
36251 /* Helper function, to parse omp for increment expression. */
36252
36253 static tree
36254 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
36255 {
36256 tree cond = cp_parser_binary_expression (parser, false, true,
36257 PREC_NOT_OPERATOR, NULL);
36258 if (cond == error_mark_node
36259 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36260 {
36261 cp_parser_skip_to_end_of_statement (parser);
36262 return error_mark_node;
36263 }
36264
36265 switch (TREE_CODE (cond))
36266 {
36267 case GT_EXPR:
36268 case GE_EXPR:
36269 case LT_EXPR:
36270 case LE_EXPR:
36271 break;
36272 case NE_EXPR:
36273 if (code != OACC_LOOP)
36274 break;
36275 gcc_fallthrough ();
36276 default:
36277 return error_mark_node;
36278 }
36279
36280 /* If decl is an iterator, preserve LHS and RHS of the relational
36281 expr until finish_omp_for. */
36282 if (decl
36283 && (type_dependent_expression_p (decl)
36284 || CLASS_TYPE_P (TREE_TYPE (decl))))
36285 return cond;
36286
36287 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
36288 TREE_CODE (cond),
36289 TREE_OPERAND (cond, 0), ERROR_MARK,
36290 TREE_OPERAND (cond, 1), ERROR_MARK,
36291 /*overload=*/NULL, tf_warning_or_error);
36292 }
36293
36294 /* Helper function, to parse omp for increment expression. */
36295
36296 static tree
36297 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
36298 {
36299 cp_token *token = cp_lexer_peek_token (parser->lexer);
36300 enum tree_code op;
36301 tree lhs, rhs;
36302 cp_id_kind idk;
36303 bool decl_first;
36304
36305 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36306 {
36307 op = (token->type == CPP_PLUS_PLUS
36308 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
36309 cp_lexer_consume_token (parser->lexer);
36310 lhs = cp_parser_simple_cast_expression (parser);
36311 if (lhs != decl
36312 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36313 return error_mark_node;
36314 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36315 }
36316
36317 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
36318 if (lhs != decl
36319 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36320 return error_mark_node;
36321
36322 token = cp_lexer_peek_token (parser->lexer);
36323 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36324 {
36325 op = (token->type == CPP_PLUS_PLUS
36326 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
36327 cp_lexer_consume_token (parser->lexer);
36328 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36329 }
36330
36331 op = cp_parser_assignment_operator_opt (parser);
36332 if (op == ERROR_MARK)
36333 return error_mark_node;
36334
36335 if (op != NOP_EXPR)
36336 {
36337 rhs = cp_parser_assignment_expression (parser);
36338 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
36339 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36340 }
36341
36342 lhs = cp_parser_binary_expression (parser, false, false,
36343 PREC_ADDITIVE_EXPRESSION, NULL);
36344 token = cp_lexer_peek_token (parser->lexer);
36345 decl_first = (lhs == decl
36346 || (processing_template_decl && cp_tree_equal (lhs, decl)));
36347 if (decl_first)
36348 lhs = NULL_TREE;
36349 if (token->type != CPP_PLUS
36350 && token->type != CPP_MINUS)
36351 return error_mark_node;
36352
36353 do
36354 {
36355 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
36356 cp_lexer_consume_token (parser->lexer);
36357 rhs = cp_parser_binary_expression (parser, false, false,
36358 PREC_ADDITIVE_EXPRESSION, NULL);
36359 token = cp_lexer_peek_token (parser->lexer);
36360 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
36361 {
36362 if (lhs == NULL_TREE)
36363 {
36364 if (op == PLUS_EXPR)
36365 lhs = rhs;
36366 else
36367 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
36368 tf_warning_or_error);
36369 }
36370 else
36371 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
36372 ERROR_MARK, NULL, tf_warning_or_error);
36373 }
36374 }
36375 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
36376
36377 if (!decl_first)
36378 {
36379 if ((rhs != decl
36380 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
36381 || op == MINUS_EXPR)
36382 return error_mark_node;
36383 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
36384 }
36385 else
36386 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
36387
36388 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36389 }
36390
36391 /* Parse the initialization statement of an OpenMP for loop.
36392
36393 Return true if the resulting construct should have an
36394 OMP_CLAUSE_PRIVATE added to it. */
36395
36396 static tree
36397 cp_parser_omp_for_loop_init (cp_parser *parser,
36398 tree &this_pre_body,
36399 releasing_vec &for_block,
36400 tree &init,
36401 tree &orig_init,
36402 tree &decl,
36403 tree &real_decl)
36404 {
36405 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36406 return NULL_TREE;
36407
36408 tree add_private_clause = NULL_TREE;
36409
36410 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36411
36412 init-expr:
36413 var = lb
36414 integer-type var = lb
36415 random-access-iterator-type var = lb
36416 pointer-type var = lb
36417 */
36418 cp_decl_specifier_seq type_specifiers;
36419
36420 /* First, try to parse as an initialized declaration. See
36421 cp_parser_condition, from whence the bulk of this is copied. */
36422
36423 cp_parser_parse_tentatively (parser);
36424 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
36425 /*is_declaration=*/true,
36426 /*is_trailing_return=*/false,
36427 &type_specifiers);
36428 if (cp_parser_parse_definitely (parser))
36429 {
36430 /* If parsing a type specifier seq succeeded, then this
36431 MUST be a initialized declaration. */
36432 tree asm_specification, attributes;
36433 cp_declarator *declarator;
36434
36435 declarator = cp_parser_declarator (parser,
36436 CP_PARSER_DECLARATOR_NAMED,
36437 CP_PARSER_FLAGS_NONE,
36438 /*ctor_dtor_or_conv_p=*/NULL,
36439 /*parenthesized_p=*/NULL,
36440 /*member_p=*/false,
36441 /*friend_p=*/false,
36442 /*static_p=*/false);
36443 attributes = cp_parser_attributes_opt (parser);
36444 asm_specification = cp_parser_asm_specification_opt (parser);
36445
36446 if (declarator == cp_error_declarator)
36447 cp_parser_skip_to_end_of_statement (parser);
36448
36449 else
36450 {
36451 tree pushed_scope, auto_node;
36452
36453 decl = start_decl (declarator, &type_specifiers,
36454 SD_INITIALIZED, attributes,
36455 /*prefix_attributes=*/NULL_TREE,
36456 &pushed_scope);
36457
36458 auto_node = type_uses_auto (TREE_TYPE (decl));
36459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36460 {
36461 if (cp_lexer_next_token_is (parser->lexer,
36462 CPP_OPEN_PAREN))
36463 error ("parenthesized initialization is not allowed in "
36464 "OpenMP %<for%> loop");
36465 else
36466 /* Trigger an error. */
36467 cp_parser_require (parser, CPP_EQ, RT_EQ);
36468
36469 init = error_mark_node;
36470 cp_parser_skip_to_end_of_statement (parser);
36471 }
36472 else if (CLASS_TYPE_P (TREE_TYPE (decl))
36473 || type_dependent_expression_p (decl)
36474 || auto_node)
36475 {
36476 bool is_direct_init, is_non_constant_init;
36477
36478 init = cp_parser_initializer (parser,
36479 &is_direct_init,
36480 &is_non_constant_init);
36481
36482 if (auto_node)
36483 {
36484 TREE_TYPE (decl)
36485 = do_auto_deduction (TREE_TYPE (decl), init,
36486 auto_node);
36487
36488 if (!CLASS_TYPE_P (TREE_TYPE (decl))
36489 && !type_dependent_expression_p (decl))
36490 goto non_class;
36491 }
36492
36493 cp_finish_decl (decl, init, !is_non_constant_init,
36494 asm_specification,
36495 LOOKUP_ONLYCONVERTING);
36496 orig_init = init;
36497 if (CLASS_TYPE_P (TREE_TYPE (decl)))
36498 {
36499 vec_safe_push (for_block, this_pre_body);
36500 init = NULL_TREE;
36501 }
36502 else
36503 {
36504 init = pop_stmt_list (this_pre_body);
36505 if (init && TREE_CODE (init) == STATEMENT_LIST)
36506 {
36507 tree_stmt_iterator i = tsi_start (init);
36508 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36509 while (!tsi_end_p (i))
36510 {
36511 tree t = tsi_stmt (i);
36512 if (TREE_CODE (t) == DECL_EXPR
36513 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
36514 {
36515 tsi_delink (&i);
36516 vec_safe_push (for_block, t);
36517 continue;
36518 }
36519 break;
36520 }
36521 if (tsi_one_before_end_p (i))
36522 {
36523 tree t = tsi_stmt (i);
36524 tsi_delink (&i);
36525 free_stmt_list (init);
36526 init = t;
36527 }
36528 }
36529 }
36530 this_pre_body = NULL_TREE;
36531 }
36532 else
36533 {
36534 /* Consume '='. */
36535 cp_lexer_consume_token (parser->lexer);
36536 init = cp_parser_assignment_expression (parser);
36537
36538 non_class:
36539 if (TYPE_REF_P (TREE_TYPE (decl)))
36540 init = error_mark_node;
36541 else
36542 cp_finish_decl (decl, NULL_TREE,
36543 /*init_const_expr_p=*/false,
36544 asm_specification,
36545 LOOKUP_ONLYCONVERTING);
36546 }
36547
36548 if (pushed_scope)
36549 pop_scope (pushed_scope);
36550 }
36551 }
36552 else
36553 {
36554 cp_id_kind idk;
36555 /* If parsing a type specifier sequence failed, then
36556 this MUST be a simple expression. */
36557 cp_parser_parse_tentatively (parser);
36558 decl = cp_parser_primary_expression (parser, false, false,
36559 false, &idk);
36560 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
36561 if (!cp_parser_error_occurred (parser)
36562 && decl
36563 && (TREE_CODE (decl) == COMPONENT_REF
36564 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
36565 {
36566 cp_parser_abort_tentative_parse (parser);
36567 cp_parser_parse_tentatively (parser);
36568 cp_token *token = cp_lexer_peek_token (parser->lexer);
36569 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
36570 /*check_dependency_p=*/true,
36571 /*template_p=*/NULL,
36572 /*declarator_p=*/false,
36573 /*optional_p=*/false);
36574 if (name != error_mark_node
36575 && last_tok == cp_lexer_peek_token (parser->lexer))
36576 {
36577 decl = cp_parser_lookup_name_simple (parser, name,
36578 token->location);
36579 if (TREE_CODE (decl) == FIELD_DECL)
36580 add_private_clause = omp_privatize_field (decl, false);
36581 }
36582 cp_parser_abort_tentative_parse (parser);
36583 cp_parser_parse_tentatively (parser);
36584 decl = cp_parser_primary_expression (parser, false, false,
36585 false, &idk);
36586 }
36587 if (!cp_parser_error_occurred (parser)
36588 && decl
36589 && DECL_P (decl)
36590 && CLASS_TYPE_P (TREE_TYPE (decl)))
36591 {
36592 tree rhs;
36593
36594 cp_parser_parse_definitely (parser);
36595 cp_parser_require (parser, CPP_EQ, RT_EQ);
36596 rhs = cp_parser_assignment_expression (parser);
36597 orig_init = rhs;
36598 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
36599 decl, NOP_EXPR,
36600 rhs,
36601 tf_warning_or_error));
36602 if (!add_private_clause)
36603 add_private_clause = decl;
36604 }
36605 else
36606 {
36607 decl = NULL;
36608 cp_parser_abort_tentative_parse (parser);
36609 init = cp_parser_expression (parser);
36610 if (init)
36611 {
36612 if (TREE_CODE (init) == MODIFY_EXPR
36613 || TREE_CODE (init) == MODOP_EXPR)
36614 real_decl = TREE_OPERAND (init, 0);
36615 }
36616 }
36617 }
36618 return add_private_clause;
36619 }
36620
36621 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36622
36623 void
36624 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
36625 tree &decl, tree &orig_decl, tree &init,
36626 tree &orig_init, tree &cond, tree &incr)
36627 {
36628 tree begin, end, range_temp_decl = NULL_TREE;
36629 tree iter_type, begin_expr, end_expr;
36630
36631 if (processing_template_decl)
36632 {
36633 if (check_for_bare_parameter_packs (init))
36634 init = error_mark_node;
36635 if (!type_dependent_expression_p (init)
36636 /* do_auto_deduction doesn't mess with template init-lists. */
36637 && !BRACE_ENCLOSED_INITIALIZER_P (init))
36638 {
36639 tree d = decl;
36640 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36641 {
36642 tree v = DECL_VALUE_EXPR (decl);
36643 if (TREE_CODE (v) == ARRAY_REF
36644 && VAR_P (TREE_OPERAND (v, 0))
36645 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36646 d = TREE_OPERAND (v, 0);
36647 }
36648 do_range_for_auto_deduction (d, init);
36649 }
36650 cond = global_namespace;
36651 incr = NULL_TREE;
36652 orig_init = init;
36653 if (this_pre_body)
36654 this_pre_body = pop_stmt_list (this_pre_body);
36655 return;
36656 }
36657
36658 init = mark_lvalue_use (init);
36659
36660 if (decl == error_mark_node || init == error_mark_node)
36661 /* If an error happened previously do nothing or else a lot of
36662 unhelpful errors would be issued. */
36663 begin_expr = end_expr = iter_type = error_mark_node;
36664 else
36665 {
36666 tree range_temp;
36667
36668 if (VAR_P (init)
36669 && array_of_runtime_bound_p (TREE_TYPE (init)))
36670 /* Can't bind a reference to an array of runtime bound. */
36671 range_temp = init;
36672 else
36673 {
36674 range_temp = build_range_temp (init);
36675 DECL_NAME (range_temp) = NULL_TREE;
36676 pushdecl (range_temp);
36677 cp_finish_decl (range_temp, init,
36678 /*is_constant_init*/false, NULL_TREE,
36679 LOOKUP_ONLYCONVERTING);
36680 range_temp_decl = range_temp;
36681 range_temp = convert_from_reference (range_temp);
36682 }
36683 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36684 &begin_expr, &end_expr);
36685 }
36686
36687 tree end_iter_type = iter_type;
36688 if (cxx_dialect >= cxx17)
36689 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36690 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36691 TREE_USED (end) = 1;
36692 DECL_ARTIFICIAL (end) = 1;
36693 pushdecl (end);
36694 cp_finish_decl (end, end_expr,
36695 /*is_constant_init*/false, NULL_TREE,
36696 LOOKUP_ONLYCONVERTING);
36697
36698 /* The new for initialization statement. */
36699 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36700 TREE_USED (begin) = 1;
36701 DECL_ARTIFICIAL (begin) = 1;
36702 pushdecl (begin);
36703 orig_init = init;
36704 if (CLASS_TYPE_P (iter_type))
36705 init = NULL_TREE;
36706 else
36707 {
36708 init = begin_expr;
36709 begin_expr = NULL_TREE;
36710 }
36711 cp_finish_decl (begin, begin_expr,
36712 /*is_constant_init*/false, NULL_TREE,
36713 LOOKUP_ONLYCONVERTING);
36714
36715 /* The new for condition. */
36716 if (CLASS_TYPE_P (iter_type))
36717 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36718 else
36719 cond = build_x_binary_op (input_location, NE_EXPR,
36720 begin, ERROR_MARK,
36721 end, ERROR_MARK,
36722 NULL, tf_warning_or_error);
36723
36724 /* The new increment expression. */
36725 if (CLASS_TYPE_P (iter_type))
36726 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36727 else
36728 incr = finish_unary_op_expr (input_location,
36729 PREINCREMENT_EXPR, begin,
36730 tf_warning_or_error);
36731
36732 orig_decl = decl;
36733 decl = begin;
36734 if (for_block)
36735 {
36736 vec_safe_push (for_block, this_pre_body);
36737 this_pre_body = NULL_TREE;
36738 }
36739
36740 tree decomp_first_name = NULL_TREE;
36741 unsigned decomp_cnt = 0;
36742 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36743 {
36744 tree v = DECL_VALUE_EXPR (orig_decl);
36745 if (TREE_CODE (v) == ARRAY_REF
36746 && VAR_P (TREE_OPERAND (v, 0))
36747 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36748 {
36749 tree d = orig_decl;
36750 orig_decl = TREE_OPERAND (v, 0);
36751 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36752 decomp_first_name = d;
36753 }
36754 }
36755
36756 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36757 if (auto_node)
36758 {
36759 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36760 tf_none);
36761 if (!error_operand_p (t))
36762 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36763 t, auto_node);
36764 }
36765
36766 tree v = make_tree_vec (decomp_cnt + 3);
36767 TREE_VEC_ELT (v, 0) = range_temp_decl;
36768 TREE_VEC_ELT (v, 1) = end;
36769 TREE_VEC_ELT (v, 2) = orig_decl;
36770 for (unsigned i = 0; i < decomp_cnt; i++)
36771 {
36772 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36773 decomp_first_name = DECL_CHAIN (decomp_first_name);
36774 }
36775 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36776 }
36777
36778 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36779 inside of the collapsed body. */
36780
36781 void
36782 cp_finish_omp_range_for (tree orig, tree begin)
36783 {
36784 gcc_assert (TREE_CODE (orig) == TREE_LIST
36785 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36786 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36787 tree decomp_first_name = NULL_TREE;
36788 unsigned int decomp_cnt = 0;
36789
36790 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36791 {
36792 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36793 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36794 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36795 }
36796
36797 /* The declaration is initialized with *__begin inside the loop body. */
36798 cp_finish_decl (decl,
36799 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36800 tf_warning_or_error),
36801 /*is_constant_init*/false, NULL_TREE,
36802 LOOKUP_ONLYCONVERTING);
36803 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36804 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36805 }
36806
36807 /* Parse the restricted form of the for statement allowed by OpenMP. */
36808
36809 static tree
36810 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36811 tree *cclauses, bool *if_p)
36812 {
36813 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36814 tree orig_decl;
36815 tree real_decl, initv, condv, incrv, declv, orig_declv;
36816 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36817 location_t loc_first;
36818 bool collapse_err = false;
36819 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36820 releasing_vec for_block;
36821 auto_vec<tree, 4> orig_inits;
36822 bool tiling = false;
36823
36824 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36825 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36826 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36827 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36828 {
36829 tiling = true;
36830 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36831 }
36832 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36833 && OMP_CLAUSE_ORDERED_EXPR (cl))
36834 {
36835 ordered_cl = cl;
36836 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36837 }
36838
36839 if (ordered && ordered < collapse)
36840 {
36841 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36842 "%<ordered%> clause parameter is less than %<collapse%>");
36843 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36844 = build_int_cst (NULL_TREE, collapse);
36845 ordered = collapse;
36846 }
36847 if (ordered)
36848 {
36849 for (tree *pc = &clauses; *pc; )
36850 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36851 {
36852 error_at (OMP_CLAUSE_LOCATION (*pc),
36853 "%<linear%> clause may not be specified together "
36854 "with %<ordered%> clause with a parameter");
36855 *pc = OMP_CLAUSE_CHAIN (*pc);
36856 }
36857 else
36858 pc = &OMP_CLAUSE_CHAIN (*pc);
36859 }
36860
36861 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36862 count = ordered ? ordered : collapse;
36863
36864 declv = make_tree_vec (count);
36865 initv = make_tree_vec (count);
36866 condv = make_tree_vec (count);
36867 incrv = make_tree_vec (count);
36868 orig_declv = NULL_TREE;
36869
36870 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36871
36872 for (i = 0; i < count; i++)
36873 {
36874 int bracecount = 0;
36875 tree add_private_clause = NULL_TREE;
36876 location_t loc;
36877
36878 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36879 {
36880 if (!collapse_err)
36881 cp_parser_error (parser, "for statement expected");
36882 return NULL;
36883 }
36884 loc = cp_lexer_consume_token (parser->lexer)->location;
36885
36886 /* Don't create location wrapper nodes within an OpenMP "for"
36887 statement. */
36888 auto_suppress_location_wrappers sentinel;
36889
36890 matching_parens parens;
36891 if (!parens.require_open (parser))
36892 return NULL;
36893
36894 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36895 this_pre_body = push_stmt_list ();
36896
36897 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36898 {
36899 /* Save tokens so that we can put them back. */
36900 cp_lexer_save_tokens (parser->lexer);
36901
36902 /* Look for ':' that is not nested in () or {}. */
36903 bool is_range_for
36904 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
36905 /*recovering=*/false,
36906 CPP_COLON,
36907 /*consume_paren=*/
36908 false) == -1);
36909
36910 /* Roll back the tokens we skipped. */
36911 cp_lexer_rollback_tokens (parser->lexer);
36912
36913 if (is_range_for)
36914 {
36915 bool saved_colon_corrects_to_scope_p
36916 = parser->colon_corrects_to_scope_p;
36917
36918 /* A colon is used in range-based for. */
36919 parser->colon_corrects_to_scope_p = false;
36920
36921 /* Parse the declaration. */
36922 cp_parser_simple_declaration (parser,
36923 /*function_definition_allowed_p=*/
36924 false, &decl);
36925 parser->colon_corrects_to_scope_p
36926 = saved_colon_corrects_to_scope_p;
36927
36928 cp_parser_require (parser, CPP_COLON, RT_COLON);
36929
36930 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
36931 false, 0, true);
36932
36933 cp_convert_omp_range_for (this_pre_body, for_block, decl,
36934 orig_decl, init, orig_init,
36935 cond, incr);
36936 if (this_pre_body)
36937 {
36938 if (pre_body)
36939 {
36940 tree t = pre_body;
36941 pre_body = push_stmt_list ();
36942 add_stmt (t);
36943 add_stmt (this_pre_body);
36944 pre_body = pop_stmt_list (pre_body);
36945 }
36946 else
36947 pre_body = this_pre_body;
36948 }
36949
36950 if (ordered_cl)
36951 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36952 "%<ordered%> clause with parameter on "
36953 "range-based %<for%> loop");
36954
36955 goto parse_close_paren;
36956 }
36957 }
36958
36959 add_private_clause
36960 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
36961 init, orig_init, decl, real_decl);
36962
36963 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36964 if (this_pre_body)
36965 {
36966 this_pre_body = pop_stmt_list (this_pre_body);
36967 if (pre_body)
36968 {
36969 tree t = pre_body;
36970 pre_body = push_stmt_list ();
36971 add_stmt (t);
36972 add_stmt (this_pre_body);
36973 pre_body = pop_stmt_list (pre_body);
36974 }
36975 else
36976 pre_body = this_pre_body;
36977 }
36978
36979 if (decl)
36980 real_decl = decl;
36981 if (cclauses != NULL
36982 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
36983 && real_decl != NULL_TREE)
36984 {
36985 tree *c;
36986 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
36987 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
36988 && OMP_CLAUSE_DECL (*c) == real_decl)
36989 {
36990 error_at (loc, "iteration variable %qD"
36991 " should not be firstprivate", real_decl);
36992 *c = OMP_CLAUSE_CHAIN (*c);
36993 }
36994 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
36995 && OMP_CLAUSE_DECL (*c) == real_decl)
36996 {
36997 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36998 tree l = *c;
36999 *c = OMP_CLAUSE_CHAIN (*c);
37000 if (code == OMP_SIMD)
37001 {
37002 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37003 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
37004 }
37005 else
37006 {
37007 OMP_CLAUSE_CHAIN (l) = clauses;
37008 clauses = l;
37009 }
37010 add_private_clause = NULL_TREE;
37011 }
37012 else
37013 {
37014 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
37015 && OMP_CLAUSE_DECL (*c) == real_decl)
37016 add_private_clause = NULL_TREE;
37017 c = &OMP_CLAUSE_CHAIN (*c);
37018 }
37019 }
37020
37021 if (add_private_clause)
37022 {
37023 tree c;
37024 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
37025 {
37026 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
37027 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
37028 && OMP_CLAUSE_DECL (c) == decl)
37029 break;
37030 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
37031 && OMP_CLAUSE_DECL (c) == decl)
37032 error_at (loc, "iteration variable %qD "
37033 "should not be firstprivate",
37034 decl);
37035 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
37036 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
37037 && OMP_CLAUSE_DECL (c) == decl)
37038 error_at (loc, "iteration variable %qD should not be reduction",
37039 decl);
37040 }
37041 if (c == NULL)
37042 {
37043 if (code != OMP_SIMD)
37044 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
37045 else if (collapse == 1)
37046 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37047 else
37048 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
37049 OMP_CLAUSE_DECL (c) = add_private_clause;
37050 c = finish_omp_clauses (c, C_ORT_OMP);
37051 if (c)
37052 {
37053 OMP_CLAUSE_CHAIN (c) = clauses;
37054 clauses = c;
37055 /* For linear, signal that we need to fill up
37056 the so far unknown linear step. */
37057 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
37058 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
37059 }
37060 }
37061 }
37062
37063 cond = NULL;
37064 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37065 cond = cp_parser_omp_for_cond (parser, decl, code);
37066 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37067
37068 incr = NULL;
37069 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
37070 {
37071 /* If decl is an iterator, preserve the operator on decl
37072 until finish_omp_for. */
37073 if (real_decl
37074 && ((processing_template_decl
37075 && (TREE_TYPE (real_decl) == NULL_TREE
37076 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
37077 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
37078 incr = cp_parser_omp_for_incr (parser, real_decl);
37079 else
37080 incr = cp_parser_expression (parser);
37081 if (!EXPR_HAS_LOCATION (incr))
37082 protected_set_expr_location (incr, input_location);
37083 }
37084
37085 parse_close_paren:
37086 if (!parens.require_close (parser))
37087 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37088 /*or_comma=*/false,
37089 /*consume_paren=*/true);
37090
37091 TREE_VEC_ELT (declv, i) = decl;
37092 TREE_VEC_ELT (initv, i) = init;
37093 TREE_VEC_ELT (condv, i) = cond;
37094 TREE_VEC_ELT (incrv, i) = incr;
37095 if (orig_init)
37096 {
37097 orig_inits.safe_grow_cleared (i + 1);
37098 orig_inits[i] = orig_init;
37099 }
37100 if (orig_decl)
37101 {
37102 if (!orig_declv)
37103 orig_declv = copy_node (declv);
37104 TREE_VEC_ELT (orig_declv, i) = orig_decl;
37105 }
37106 else if (orig_declv)
37107 TREE_VEC_ELT (orig_declv, i) = decl;
37108
37109 if (i == count - 1)
37110 break;
37111
37112 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
37113 in between the collapsed for loops to be still considered perfectly
37114 nested. Hopefully the final version clarifies this.
37115 For now handle (multiple) {'s and empty statements. */
37116 cp_parser_parse_tentatively (parser);
37117 for (;;)
37118 {
37119 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37120 break;
37121 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
37122 {
37123 cp_lexer_consume_token (parser->lexer);
37124 bracecount++;
37125 }
37126 else if (bracecount
37127 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37128 cp_lexer_consume_token (parser->lexer);
37129 else
37130 {
37131 loc = cp_lexer_peek_token (parser->lexer)->location;
37132 error_at (loc, "not enough for loops to collapse");
37133 collapse_err = true;
37134 cp_parser_abort_tentative_parse (parser);
37135 declv = NULL_TREE;
37136 break;
37137 }
37138 }
37139
37140 if (declv)
37141 {
37142 cp_parser_parse_definitely (parser);
37143 nbraces += bracecount;
37144 }
37145 }
37146
37147 if (nbraces)
37148 if_p = NULL;
37149
37150 /* Note that we saved the original contents of this flag when we entered
37151 the structured block, and so we don't need to re-save it here. */
37152 parser->in_statement = IN_OMP_FOR;
37153
37154 /* Note that the grammar doesn't call for a structured block here,
37155 though the loop as a whole is a structured block. */
37156 if (orig_declv)
37157 {
37158 body = begin_omp_structured_block ();
37159 for (i = 0; i < count; i++)
37160 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
37161 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
37162 TREE_VEC_ELT (declv, i));
37163 }
37164 else
37165 body = push_stmt_list ();
37166 cp_parser_statement (parser, NULL_TREE, false, if_p);
37167 if (orig_declv)
37168 body = finish_omp_structured_block (body);
37169 else
37170 body = pop_stmt_list (body);
37171
37172 if (declv == NULL_TREE)
37173 ret = NULL_TREE;
37174 else
37175 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
37176 incrv, body, pre_body, &orig_inits, clauses);
37177
37178 while (nbraces)
37179 {
37180 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
37181 {
37182 cp_lexer_consume_token (parser->lexer);
37183 nbraces--;
37184 }
37185 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37186 cp_lexer_consume_token (parser->lexer);
37187 else
37188 {
37189 if (!collapse_err)
37190 {
37191 error_at (cp_lexer_peek_token (parser->lexer)->location,
37192 "collapsed loops not perfectly nested");
37193 }
37194 collapse_err = true;
37195 cp_parser_statement_seq_opt (parser, NULL);
37196 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
37197 break;
37198 }
37199 }
37200
37201 while (!for_block->is_empty ())
37202 {
37203 tree t = for_block->pop ();
37204 if (TREE_CODE (t) == STATEMENT_LIST)
37205 add_stmt (pop_stmt_list (t));
37206 else
37207 add_stmt (t);
37208 }
37209
37210 return ret;
37211 }
37212
37213 /* Helper function for OpenMP parsing, split clauses and call
37214 finish_omp_clauses on each of the set of clauses afterwards. */
37215
37216 static void
37217 cp_omp_split_clauses (location_t loc, enum tree_code code,
37218 omp_clause_mask mask, tree clauses, tree *cclauses)
37219 {
37220 int i;
37221 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
37222 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
37223 if (cclauses[i])
37224 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
37225 }
37226
37227 /* OpenMP 4.0:
37228 #pragma omp simd simd-clause[optseq] new-line
37229 for-loop */
37230
37231 #define OMP_SIMD_CLAUSE_MASK \
37232 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
37233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
37242
37243 static tree
37244 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
37245 char *p_name, omp_clause_mask mask, tree *cclauses,
37246 bool *if_p)
37247 {
37248 tree clauses, sb, ret;
37249 unsigned int save;
37250 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37251
37252 strcat (p_name, " simd");
37253 mask |= OMP_SIMD_CLAUSE_MASK;
37254
37255 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37256 cclauses == NULL);
37257 if (cclauses)
37258 {
37259 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
37260 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
37261 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
37262 OMP_CLAUSE_ORDERED);
37263 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
37264 {
37265 error_at (OMP_CLAUSE_LOCATION (c),
37266 "%<ordered%> clause with parameter may not be specified "
37267 "on %qs construct", p_name);
37268 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
37269 }
37270 }
37271
37272 keep_next_level (true);
37273 sb = begin_omp_structured_block ();
37274 save = cp_parser_begin_omp_structured_block (parser);
37275
37276 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
37277
37278 cp_parser_end_omp_structured_block (parser, save);
37279 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37280
37281 return ret;
37282 }
37283
37284 /* OpenMP 2.5:
37285 #pragma omp for for-clause[optseq] new-line
37286 for-loop
37287
37288 OpenMP 4.0:
37289 #pragma omp for simd for-simd-clause[optseq] new-line
37290 for-loop */
37291
37292 #define OMP_FOR_CLAUSE_MASK \
37293 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37302
37303 static tree
37304 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
37305 char *p_name, omp_clause_mask mask, tree *cclauses,
37306 bool *if_p)
37307 {
37308 tree clauses, sb, ret;
37309 unsigned int save;
37310 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37311
37312 strcat (p_name, " for");
37313 mask |= OMP_FOR_CLAUSE_MASK;
37314 /* parallel for{, simd} disallows nowait clause, but for
37315 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37316 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
37317 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37318 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37319 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37320 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
37321
37322 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37323 {
37324 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37325 const char *p = IDENTIFIER_POINTER (id);
37326
37327 if (strcmp (p, "simd") == 0)
37328 {
37329 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37330 if (cclauses == NULL)
37331 cclauses = cclauses_buf;
37332
37333 cp_lexer_consume_token (parser->lexer);
37334 if (!flag_openmp) /* flag_openmp_simd */
37335 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37336 cclauses, if_p);
37337 sb = begin_omp_structured_block ();
37338 save = cp_parser_begin_omp_structured_block (parser);
37339 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37340 cclauses, if_p);
37341 cp_parser_end_omp_structured_block (parser, save);
37342 tree body = finish_omp_structured_block (sb);
37343 if (ret == NULL)
37344 return ret;
37345 ret = make_node (OMP_FOR);
37346 TREE_TYPE (ret) = void_type_node;
37347 OMP_FOR_BODY (ret) = body;
37348 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37349 SET_EXPR_LOCATION (ret, loc);
37350 add_stmt (ret);
37351 return ret;
37352 }
37353 }
37354 if (!flag_openmp) /* flag_openmp_simd */
37355 {
37356 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37357 return NULL_TREE;
37358 }
37359
37360 /* Composite distribute parallel for disallows linear clause. */
37361 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37362 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
37363
37364 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37365 cclauses == NULL);
37366 if (cclauses)
37367 {
37368 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
37369 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37370 }
37371
37372 keep_next_level (true);
37373 sb = begin_omp_structured_block ();
37374 save = cp_parser_begin_omp_structured_block (parser);
37375
37376 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
37377
37378 cp_parser_end_omp_structured_block (parser, save);
37379 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37380
37381 return ret;
37382 }
37383
37384 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
37385 omp_clause_mask, tree *, bool *);
37386
37387 /* OpenMP 2.5:
37388 # pragma omp master new-line
37389 structured-block */
37390
37391 static tree
37392 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
37393 char *p_name, omp_clause_mask mask, tree *cclauses,
37394 bool *if_p)
37395 {
37396 tree clauses, sb, ret;
37397 unsigned int save;
37398 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37399
37400 strcat (p_name, " master");
37401
37402 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37403 {
37404 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37405 const char *p = IDENTIFIER_POINTER (id);
37406
37407 if (strcmp (p, "taskloop") == 0)
37408 {
37409 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37410 if (cclauses == NULL)
37411 cclauses = cclauses_buf;
37412
37413 cp_lexer_consume_token (parser->lexer);
37414 if (!flag_openmp) /* flag_openmp_simd */
37415 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37416 cclauses, if_p);
37417 sb = begin_omp_structured_block ();
37418 save = cp_parser_begin_omp_structured_block (parser);
37419 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37420 cclauses, if_p);
37421 cp_parser_end_omp_structured_block (parser, save);
37422 tree body = finish_omp_structured_block (sb);
37423 if (ret == NULL)
37424 return ret;
37425 return c_finish_omp_master (loc, body);
37426 }
37427 }
37428 if (!flag_openmp) /* flag_openmp_simd */
37429 {
37430 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37431 return NULL_TREE;
37432 }
37433
37434 if (cclauses)
37435 {
37436 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37437 false);
37438 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
37439 }
37440 else
37441 cp_parser_require_pragma_eol (parser, pragma_tok);
37442
37443 return c_finish_omp_master (loc,
37444 cp_parser_omp_structured_block (parser, if_p));
37445 }
37446
37447 /* OpenMP 2.5:
37448 # pragma omp ordered new-line
37449 structured-block
37450
37451 OpenMP 4.5:
37452 # pragma omp ordered ordered-clauses new-line
37453 structured-block */
37454
37455 #define OMP_ORDERED_CLAUSE_MASK \
37456 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37458
37459 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37460 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37461
37462 static bool
37463 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
37464 enum pragma_context context, bool *if_p)
37465 {
37466 location_t loc = pragma_tok->location;
37467
37468 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37469 {
37470 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37471 const char *p = IDENTIFIER_POINTER (id);
37472
37473 if (strcmp (p, "depend") == 0)
37474 {
37475 if (!flag_openmp) /* flag_openmp_simd */
37476 {
37477 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37478 return false;
37479 }
37480 if (context == pragma_stmt)
37481 {
37482 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
37483 "%<depend%> clause may only be used in compound "
37484 "statements");
37485 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37486 return false;
37487 }
37488 tree clauses
37489 = cp_parser_omp_all_clauses (parser,
37490 OMP_ORDERED_DEPEND_CLAUSE_MASK,
37491 "#pragma omp ordered", pragma_tok);
37492 c_finish_omp_ordered (loc, clauses, NULL_TREE);
37493 return false;
37494 }
37495 }
37496
37497 tree clauses
37498 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
37499 "#pragma omp ordered", pragma_tok);
37500
37501 if (!flag_openmp /* flag_openmp_simd */
37502 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
37503 return false;
37504
37505 c_finish_omp_ordered (loc, clauses,
37506 cp_parser_omp_structured_block (parser, if_p));
37507 return true;
37508 }
37509
37510 /* OpenMP 2.5:
37511
37512 section-scope:
37513 { section-sequence }
37514
37515 section-sequence:
37516 section-directive[opt] structured-block
37517 section-sequence section-directive structured-block */
37518
37519 static tree
37520 cp_parser_omp_sections_scope (cp_parser *parser)
37521 {
37522 tree stmt, substmt;
37523 bool error_suppress = false;
37524 cp_token *tok;
37525
37526 matching_braces braces;
37527 if (!braces.require_open (parser))
37528 return NULL_TREE;
37529
37530 stmt = push_stmt_list ();
37531
37532 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
37533 != PRAGMA_OMP_SECTION)
37534 {
37535 substmt = cp_parser_omp_structured_block (parser, NULL);
37536 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37537 add_stmt (substmt);
37538 }
37539
37540 while (1)
37541 {
37542 tok = cp_lexer_peek_token (parser->lexer);
37543 if (tok->type == CPP_CLOSE_BRACE)
37544 break;
37545 if (tok->type == CPP_EOF)
37546 break;
37547
37548 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
37549 {
37550 cp_lexer_consume_token (parser->lexer);
37551 cp_parser_require_pragma_eol (parser, tok);
37552 error_suppress = false;
37553 }
37554 else if (!error_suppress)
37555 {
37556 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
37557 error_suppress = true;
37558 }
37559
37560 substmt = cp_parser_omp_structured_block (parser, NULL);
37561 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37562 add_stmt (substmt);
37563 }
37564 braces.require_close (parser);
37565
37566 substmt = pop_stmt_list (stmt);
37567
37568 stmt = make_node (OMP_SECTIONS);
37569 TREE_TYPE (stmt) = void_type_node;
37570 OMP_SECTIONS_BODY (stmt) = substmt;
37571
37572 add_stmt (stmt);
37573 return stmt;
37574 }
37575
37576 /* OpenMP 2.5:
37577 # pragma omp sections sections-clause[optseq] newline
37578 sections-scope */
37579
37580 #define OMP_SECTIONS_CLAUSE_MASK \
37581 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37586
37587 static tree
37588 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
37589 char *p_name, omp_clause_mask mask, tree *cclauses)
37590 {
37591 tree clauses, ret;
37592 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37593
37594 strcat (p_name, " sections");
37595 mask |= OMP_SECTIONS_CLAUSE_MASK;
37596 if (cclauses)
37597 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37598
37599 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37600 cclauses == NULL);
37601 if (cclauses)
37602 {
37603 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
37604 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
37605 }
37606
37607 ret = cp_parser_omp_sections_scope (parser);
37608 if (ret)
37609 OMP_SECTIONS_CLAUSES (ret) = clauses;
37610
37611 return ret;
37612 }
37613
37614 /* OpenMP 2.5:
37615 # pragma omp parallel parallel-clause[optseq] new-line
37616 structured-block
37617 # pragma omp parallel for parallel-for-clause[optseq] new-line
37618 structured-block
37619 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37620 structured-block
37621
37622 OpenMP 4.0:
37623 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37624 structured-block */
37625
37626 #define OMP_PARALLEL_CLAUSE_MASK \
37627 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37636
37637 static tree
37638 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
37639 char *p_name, omp_clause_mask mask, tree *cclauses,
37640 bool *if_p)
37641 {
37642 tree stmt, clauses, block;
37643 unsigned int save;
37644 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37645
37646 strcat (p_name, " parallel");
37647 mask |= OMP_PARALLEL_CLAUSE_MASK;
37648 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37649 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
37650 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
37651 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37652
37653 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37654 {
37655 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37656 if (cclauses == NULL)
37657 cclauses = cclauses_buf;
37658
37659 cp_lexer_consume_token (parser->lexer);
37660 if (!flag_openmp) /* flag_openmp_simd */
37661 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37662 if_p);
37663 block = begin_omp_parallel ();
37664 save = cp_parser_begin_omp_structured_block (parser);
37665 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37666 if_p);
37667 cp_parser_end_omp_structured_block (parser, save);
37668 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37669 block);
37670 if (ret == NULL_TREE)
37671 return ret;
37672 OMP_PARALLEL_COMBINED (stmt) = 1;
37673 return stmt;
37674 }
37675 /* When combined with distribute, parallel has to be followed by for.
37676 #pragma omp target parallel is allowed though. */
37677 else if (cclauses
37678 && (mask & (OMP_CLAUSE_MASK_1
37679 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37680 {
37681 error_at (loc, "expected %<for%> after %qs", p_name);
37682 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37683 return NULL_TREE;
37684 }
37685 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37686 {
37687 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37688 const char *p = IDENTIFIER_POINTER (id);
37689 if (strcmp (p, "master") == 0)
37690 {
37691 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37692 cclauses = cclauses_buf;
37693
37694 cp_lexer_consume_token (parser->lexer);
37695 block = begin_omp_parallel ();
37696 save = cp_parser_begin_omp_structured_block (parser);
37697 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37698 cclauses, if_p);
37699 cp_parser_end_omp_structured_block (parser, save);
37700 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37701 block);
37702 OMP_PARALLEL_COMBINED (stmt) = 1;
37703 if (ret == NULL_TREE)
37704 return ret;
37705 return stmt;
37706 }
37707 else if (!flag_openmp) /* flag_openmp_simd */
37708 {
37709 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37710 return NULL_TREE;
37711 }
37712 else if (strcmp (p, "sections") == 0)
37713 {
37714 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37715 cclauses = cclauses_buf;
37716
37717 cp_lexer_consume_token (parser->lexer);
37718 block = begin_omp_parallel ();
37719 save = cp_parser_begin_omp_structured_block (parser);
37720 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37721 cp_parser_end_omp_structured_block (parser, save);
37722 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37723 block);
37724 OMP_PARALLEL_COMBINED (stmt) = 1;
37725 return stmt;
37726 }
37727 }
37728 else if (!flag_openmp) /* flag_openmp_simd */
37729 {
37730 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37731 return NULL_TREE;
37732 }
37733
37734 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37735 cclauses == NULL);
37736 if (cclauses)
37737 {
37738 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37739 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37740 }
37741
37742 block = begin_omp_parallel ();
37743 save = cp_parser_begin_omp_structured_block (parser);
37744 cp_parser_statement (parser, NULL_TREE, false, if_p);
37745 cp_parser_end_omp_structured_block (parser, save);
37746 stmt = finish_omp_parallel (clauses, block);
37747 return stmt;
37748 }
37749
37750 /* OpenMP 2.5:
37751 # pragma omp single single-clause[optseq] new-line
37752 structured-block */
37753
37754 #define OMP_SINGLE_CLAUSE_MASK \
37755 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37759
37760 static tree
37761 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37762 {
37763 tree stmt = make_node (OMP_SINGLE);
37764 TREE_TYPE (stmt) = void_type_node;
37765 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37766
37767 OMP_SINGLE_CLAUSES (stmt)
37768 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37769 "#pragma omp single", pragma_tok);
37770 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37771
37772 return add_stmt (stmt);
37773 }
37774
37775 /* OpenMP 3.0:
37776 # pragma omp task task-clause[optseq] new-line
37777 structured-block */
37778
37779 #define OMP_TASK_CLAUSE_MASK \
37780 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37791
37792 static tree
37793 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37794 {
37795 tree clauses, block;
37796 unsigned int save;
37797
37798 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37799 "#pragma omp task", pragma_tok);
37800 block = begin_omp_task ();
37801 save = cp_parser_begin_omp_structured_block (parser);
37802 cp_parser_statement (parser, NULL_TREE, false, if_p);
37803 cp_parser_end_omp_structured_block (parser, save);
37804 return finish_omp_task (clauses, block);
37805 }
37806
37807 /* OpenMP 3.0:
37808 # pragma omp taskwait new-line
37809
37810 OpenMP 5.0:
37811 # pragma omp taskwait taskwait-clause[opt] new-line */
37812
37813 #define OMP_TASKWAIT_CLAUSE_MASK \
37814 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37815
37816 static void
37817 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37818 {
37819 tree clauses
37820 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37821 "#pragma omp taskwait", pragma_tok);
37822
37823 if (clauses)
37824 {
37825 tree stmt = make_node (OMP_TASK);
37826 TREE_TYPE (stmt) = void_node;
37827 OMP_TASK_CLAUSES (stmt) = clauses;
37828 OMP_TASK_BODY (stmt) = NULL_TREE;
37829 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37830 add_stmt (stmt);
37831 }
37832 else
37833 finish_omp_taskwait ();
37834 }
37835
37836 /* OpenMP 3.1:
37837 # pragma omp taskyield new-line */
37838
37839 static void
37840 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37841 {
37842 cp_parser_require_pragma_eol (parser, pragma_tok);
37843 finish_omp_taskyield ();
37844 }
37845
37846 /* OpenMP 4.0:
37847 # pragma omp taskgroup new-line
37848 structured-block
37849
37850 OpenMP 5.0:
37851 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37852
37853 #define OMP_TASKGROUP_CLAUSE_MASK \
37854 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37855
37856 static tree
37857 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37858 {
37859 tree clauses
37860 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37861 "#pragma omp taskgroup", pragma_tok);
37862 return c_finish_omp_taskgroup (input_location,
37863 cp_parser_omp_structured_block (parser,
37864 if_p),
37865 clauses);
37866 }
37867
37868
37869 /* OpenMP 2.5:
37870 # pragma omp threadprivate (variable-list) */
37871
37872 static void
37873 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37874 {
37875 tree vars;
37876
37877 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37878 cp_parser_require_pragma_eol (parser, pragma_tok);
37879
37880 finish_omp_threadprivate (vars);
37881 }
37882
37883 /* OpenMP 4.0:
37884 # pragma omp cancel cancel-clause[optseq] new-line */
37885
37886 #define OMP_CANCEL_CLAUSE_MASK \
37887 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37892
37893 static void
37894 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37895 {
37896 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
37897 "#pragma omp cancel", pragma_tok);
37898 finish_omp_cancel (clauses);
37899 }
37900
37901 /* OpenMP 4.0:
37902 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37903
37904 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37905 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37909
37910 static void
37911 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
37912 enum pragma_context context)
37913 {
37914 tree clauses;
37915 bool point_seen = false;
37916
37917 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37918 {
37919 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37920 const char *p = IDENTIFIER_POINTER (id);
37921
37922 if (strcmp (p, "point") == 0)
37923 {
37924 cp_lexer_consume_token (parser->lexer);
37925 point_seen = true;
37926 }
37927 }
37928 if (!point_seen)
37929 {
37930 cp_parser_error (parser, "expected %<point%>");
37931 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37932 return;
37933 }
37934
37935 if (context != pragma_compound)
37936 {
37937 if (context == pragma_stmt)
37938 error_at (pragma_tok->location,
37939 "%<#pragma %s%> may only be used in compound statements",
37940 "omp cancellation point");
37941 else
37942 cp_parser_error (parser, "expected declaration specifiers");
37943 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37944 return;
37945 }
37946
37947 clauses = cp_parser_omp_all_clauses (parser,
37948 OMP_CANCELLATION_POINT_CLAUSE_MASK,
37949 "#pragma omp cancellation point",
37950 pragma_tok);
37951 finish_omp_cancellation_point (clauses);
37952 }
37953
37954 /* OpenMP 4.0:
37955 #pragma omp distribute distribute-clause[optseq] new-line
37956 for-loop */
37957
37958 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37959 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37964
37965 static tree
37966 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
37967 char *p_name, omp_clause_mask mask, tree *cclauses,
37968 bool *if_p)
37969 {
37970 tree clauses, sb, ret;
37971 unsigned int save;
37972 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37973
37974 strcat (p_name, " distribute");
37975 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
37976
37977 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37978 {
37979 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37980 const char *p = IDENTIFIER_POINTER (id);
37981 bool simd = false;
37982 bool parallel = false;
37983
37984 if (strcmp (p, "simd") == 0)
37985 simd = true;
37986 else
37987 parallel = strcmp (p, "parallel") == 0;
37988 if (parallel || simd)
37989 {
37990 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37991 if (cclauses == NULL)
37992 cclauses = cclauses_buf;
37993 cp_lexer_consume_token (parser->lexer);
37994 if (!flag_openmp) /* flag_openmp_simd */
37995 {
37996 if (simd)
37997 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37998 cclauses, if_p);
37999 else
38000 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
38001 cclauses, if_p);
38002 }
38003 sb = begin_omp_structured_block ();
38004 save = cp_parser_begin_omp_structured_block (parser);
38005 if (simd)
38006 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38007 cclauses, if_p);
38008 else
38009 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
38010 cclauses, if_p);
38011 cp_parser_end_omp_structured_block (parser, save);
38012 tree body = finish_omp_structured_block (sb);
38013 if (ret == NULL)
38014 return ret;
38015 ret = make_node (OMP_DISTRIBUTE);
38016 TREE_TYPE (ret) = void_type_node;
38017 OMP_FOR_BODY (ret) = body;
38018 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
38019 SET_EXPR_LOCATION (ret, loc);
38020 add_stmt (ret);
38021 return ret;
38022 }
38023 }
38024 if (!flag_openmp) /* flag_openmp_simd */
38025 {
38026 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38027 return NULL_TREE;
38028 }
38029
38030 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38031 cclauses == NULL);
38032 if (cclauses)
38033 {
38034 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
38035 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
38036 }
38037
38038 keep_next_level (true);
38039 sb = begin_omp_structured_block ();
38040 save = cp_parser_begin_omp_structured_block (parser);
38041
38042 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
38043
38044 cp_parser_end_omp_structured_block (parser, save);
38045 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
38046
38047 return ret;
38048 }
38049
38050 /* OpenMP 4.0:
38051 # pragma omp teams teams-clause[optseq] new-line
38052 structured-block */
38053
38054 #define OMP_TEAMS_CLAUSE_MASK \
38055 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
38060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
38061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
38062
38063 static tree
38064 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
38065 char *p_name, omp_clause_mask mask, tree *cclauses,
38066 bool *if_p)
38067 {
38068 tree clauses, sb, ret;
38069 unsigned int save;
38070 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38071
38072 strcat (p_name, " teams");
38073 mask |= OMP_TEAMS_CLAUSE_MASK;
38074
38075 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38076 {
38077 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38078 const char *p = IDENTIFIER_POINTER (id);
38079 if (strcmp (p, "distribute") == 0)
38080 {
38081 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38082 if (cclauses == NULL)
38083 cclauses = cclauses_buf;
38084
38085 cp_lexer_consume_token (parser->lexer);
38086 if (!flag_openmp) /* flag_openmp_simd */
38087 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
38088 cclauses, if_p);
38089 keep_next_level (true);
38090 sb = begin_omp_structured_block ();
38091 save = cp_parser_begin_omp_structured_block (parser);
38092 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
38093 cclauses, if_p);
38094 cp_parser_end_omp_structured_block (parser, save);
38095 tree body = finish_omp_structured_block (sb);
38096 if (ret == NULL)
38097 return ret;
38098 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38099 ret = make_node (OMP_TEAMS);
38100 TREE_TYPE (ret) = void_type_node;
38101 OMP_TEAMS_CLAUSES (ret) = clauses;
38102 OMP_TEAMS_BODY (ret) = body;
38103 OMP_TEAMS_COMBINED (ret) = 1;
38104 SET_EXPR_LOCATION (ret, loc);
38105 return add_stmt (ret);
38106 }
38107 }
38108 if (!flag_openmp) /* flag_openmp_simd */
38109 {
38110 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38111 return NULL_TREE;
38112 }
38113
38114 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38115 cclauses == NULL);
38116 if (cclauses)
38117 {
38118 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
38119 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38120 }
38121
38122 tree stmt = make_node (OMP_TEAMS);
38123 TREE_TYPE (stmt) = void_type_node;
38124 OMP_TEAMS_CLAUSES (stmt) = clauses;
38125 keep_next_level (true);
38126 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38127 SET_EXPR_LOCATION (stmt, loc);
38128
38129 return add_stmt (stmt);
38130 }
38131
38132 /* OpenMP 4.0:
38133 # pragma omp target data target-data-clause[optseq] new-line
38134 structured-block */
38135
38136 #define OMP_TARGET_DATA_CLAUSE_MASK \
38137 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
38141
38142 static tree
38143 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38144 {
38145 tree clauses
38146 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
38147 "#pragma omp target data", pragma_tok);
38148 int map_seen = 0;
38149 for (tree *pc = &clauses; *pc;)
38150 {
38151 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38152 switch (OMP_CLAUSE_MAP_KIND (*pc))
38153 {
38154 case GOMP_MAP_TO:
38155 case GOMP_MAP_ALWAYS_TO:
38156 case GOMP_MAP_FROM:
38157 case GOMP_MAP_ALWAYS_FROM:
38158 case GOMP_MAP_TOFROM:
38159 case GOMP_MAP_ALWAYS_TOFROM:
38160 case GOMP_MAP_ALLOC:
38161 map_seen = 3;
38162 break;
38163 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38164 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38165 case GOMP_MAP_ALWAYS_POINTER:
38166 break;
38167 default:
38168 map_seen |= 1;
38169 error_at (OMP_CLAUSE_LOCATION (*pc),
38170 "%<#pragma omp target data%> with map-type other "
38171 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38172 "on %<map%> clause");
38173 *pc = OMP_CLAUSE_CHAIN (*pc);
38174 continue;
38175 }
38176 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
38177 map_seen = 3;
38178 pc = &OMP_CLAUSE_CHAIN (*pc);
38179 }
38180
38181 if (map_seen != 3)
38182 {
38183 if (map_seen == 0)
38184 error_at (pragma_tok->location,
38185 "%<#pragma omp target data%> must contain at least "
38186 "one %<map%> or %<use_device_ptr%> clause");
38187 return NULL_TREE;
38188 }
38189
38190 tree stmt = make_node (OMP_TARGET_DATA);
38191 TREE_TYPE (stmt) = void_type_node;
38192 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
38193
38194 keep_next_level (true);
38195 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38196
38197 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38198 return add_stmt (stmt);
38199 }
38200
38201 /* OpenMP 4.5:
38202 # pragma omp target enter data target-enter-data-clause[optseq] new-line
38203 structured-block */
38204
38205 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
38206 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38211
38212 static tree
38213 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
38214 enum pragma_context context)
38215 {
38216 bool data_seen = false;
38217 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38218 {
38219 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38220 const char *p = IDENTIFIER_POINTER (id);
38221
38222 if (strcmp (p, "data") == 0)
38223 {
38224 cp_lexer_consume_token (parser->lexer);
38225 data_seen = true;
38226 }
38227 }
38228 if (!data_seen)
38229 {
38230 cp_parser_error (parser, "expected %<data%>");
38231 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38232 return NULL_TREE;
38233 }
38234
38235 if (context == pragma_stmt)
38236 {
38237 error_at (pragma_tok->location,
38238 "%<#pragma %s%> may only be used in compound statements",
38239 "omp target enter data");
38240 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38241 return NULL_TREE;
38242 }
38243
38244 tree clauses
38245 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
38246 "#pragma omp target enter data", pragma_tok);
38247 int map_seen = 0;
38248 for (tree *pc = &clauses; *pc;)
38249 {
38250 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38251 switch (OMP_CLAUSE_MAP_KIND (*pc))
38252 {
38253 case GOMP_MAP_TO:
38254 case GOMP_MAP_ALWAYS_TO:
38255 case GOMP_MAP_ALLOC:
38256 map_seen = 3;
38257 break;
38258 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38259 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38260 case GOMP_MAP_ALWAYS_POINTER:
38261 break;
38262 default:
38263 map_seen |= 1;
38264 error_at (OMP_CLAUSE_LOCATION (*pc),
38265 "%<#pragma omp target enter data%> with map-type other "
38266 "than %<to%> or %<alloc%> on %<map%> clause");
38267 *pc = OMP_CLAUSE_CHAIN (*pc);
38268 continue;
38269 }
38270 pc = &OMP_CLAUSE_CHAIN (*pc);
38271 }
38272
38273 if (map_seen != 3)
38274 {
38275 if (map_seen == 0)
38276 error_at (pragma_tok->location,
38277 "%<#pragma omp target enter data%> must contain at least "
38278 "one %<map%> clause");
38279 return NULL_TREE;
38280 }
38281
38282 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
38283 TREE_TYPE (stmt) = void_type_node;
38284 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
38285 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38286 return add_stmt (stmt);
38287 }
38288
38289 /* OpenMP 4.5:
38290 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38291 structured-block */
38292
38293 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38294 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38299
38300 static tree
38301 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
38302 enum pragma_context context)
38303 {
38304 bool data_seen = false;
38305 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38306 {
38307 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38308 const char *p = IDENTIFIER_POINTER (id);
38309
38310 if (strcmp (p, "data") == 0)
38311 {
38312 cp_lexer_consume_token (parser->lexer);
38313 data_seen = true;
38314 }
38315 }
38316 if (!data_seen)
38317 {
38318 cp_parser_error (parser, "expected %<data%>");
38319 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38320 return NULL_TREE;
38321 }
38322
38323 if (context == pragma_stmt)
38324 {
38325 error_at (pragma_tok->location,
38326 "%<#pragma %s%> may only be used in compound statements",
38327 "omp target exit data");
38328 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38329 return NULL_TREE;
38330 }
38331
38332 tree clauses
38333 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
38334 "#pragma omp target exit data", pragma_tok);
38335 int map_seen = 0;
38336 for (tree *pc = &clauses; *pc;)
38337 {
38338 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38339 switch (OMP_CLAUSE_MAP_KIND (*pc))
38340 {
38341 case GOMP_MAP_FROM:
38342 case GOMP_MAP_ALWAYS_FROM:
38343 case GOMP_MAP_RELEASE:
38344 case GOMP_MAP_DELETE:
38345 map_seen = 3;
38346 break;
38347 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38348 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38349 case GOMP_MAP_ALWAYS_POINTER:
38350 break;
38351 default:
38352 map_seen |= 1;
38353 error_at (OMP_CLAUSE_LOCATION (*pc),
38354 "%<#pragma omp target exit data%> with map-type other "
38355 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38356 " clause");
38357 *pc = OMP_CLAUSE_CHAIN (*pc);
38358 continue;
38359 }
38360 pc = &OMP_CLAUSE_CHAIN (*pc);
38361 }
38362
38363 if (map_seen != 3)
38364 {
38365 if (map_seen == 0)
38366 error_at (pragma_tok->location,
38367 "%<#pragma omp target exit data%> must contain at least "
38368 "one %<map%> clause");
38369 return NULL_TREE;
38370 }
38371
38372 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
38373 TREE_TYPE (stmt) = void_type_node;
38374 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
38375 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38376 return add_stmt (stmt);
38377 }
38378
38379 /* OpenMP 4.0:
38380 # pragma omp target update target-update-clause[optseq] new-line */
38381
38382 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38383 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38389
38390 static bool
38391 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
38392 enum pragma_context context)
38393 {
38394 if (context == pragma_stmt)
38395 {
38396 error_at (pragma_tok->location,
38397 "%<#pragma %s%> may only be used in compound statements",
38398 "omp target update");
38399 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38400 return false;
38401 }
38402
38403 tree clauses
38404 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
38405 "#pragma omp target update", pragma_tok);
38406 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
38407 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
38408 {
38409 error_at (pragma_tok->location,
38410 "%<#pragma omp target update%> must contain at least one "
38411 "%<from%> or %<to%> clauses");
38412 return false;
38413 }
38414
38415 tree stmt = make_node (OMP_TARGET_UPDATE);
38416 TREE_TYPE (stmt) = void_type_node;
38417 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
38418 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38419 add_stmt (stmt);
38420 return false;
38421 }
38422
38423 /* OpenMP 4.0:
38424 # pragma omp target target-clause[optseq] new-line
38425 structured-block */
38426
38427 #define OMP_TARGET_CLAUSE_MASK \
38428 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38437
38438 static bool
38439 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
38440 enum pragma_context context, bool *if_p)
38441 {
38442 tree *pc = NULL, stmt;
38443
38444 if (flag_openmp)
38445 omp_requires_mask
38446 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
38447
38448 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38449 {
38450 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38451 const char *p = IDENTIFIER_POINTER (id);
38452 enum tree_code ccode = ERROR_MARK;
38453
38454 if (strcmp (p, "teams") == 0)
38455 ccode = OMP_TEAMS;
38456 else if (strcmp (p, "parallel") == 0)
38457 ccode = OMP_PARALLEL;
38458 else if (strcmp (p, "simd") == 0)
38459 ccode = OMP_SIMD;
38460 if (ccode != ERROR_MARK)
38461 {
38462 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
38463 char p_name[sizeof ("#pragma omp target teams distribute "
38464 "parallel for simd")];
38465
38466 cp_lexer_consume_token (parser->lexer);
38467 strcpy (p_name, "#pragma omp target");
38468 if (!flag_openmp) /* flag_openmp_simd */
38469 {
38470 tree stmt;
38471 switch (ccode)
38472 {
38473 case OMP_TEAMS:
38474 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
38475 OMP_TARGET_CLAUSE_MASK,
38476 cclauses, if_p);
38477 break;
38478 case OMP_PARALLEL:
38479 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38480 OMP_TARGET_CLAUSE_MASK,
38481 cclauses, if_p);
38482 break;
38483 case OMP_SIMD:
38484 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
38485 OMP_TARGET_CLAUSE_MASK,
38486 cclauses, if_p);
38487 break;
38488 default:
38489 gcc_unreachable ();
38490 }
38491 return stmt != NULL_TREE;
38492 }
38493 keep_next_level (true);
38494 tree sb = begin_omp_structured_block (), ret;
38495 unsigned save = cp_parser_begin_omp_structured_block (parser);
38496 switch (ccode)
38497 {
38498 case OMP_TEAMS:
38499 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
38500 OMP_TARGET_CLAUSE_MASK, cclauses,
38501 if_p);
38502 break;
38503 case OMP_PARALLEL:
38504 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38505 OMP_TARGET_CLAUSE_MASK, cclauses,
38506 if_p);
38507 break;
38508 case OMP_SIMD:
38509 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
38510 OMP_TARGET_CLAUSE_MASK, cclauses,
38511 if_p);
38512 break;
38513 default:
38514 gcc_unreachable ();
38515 }
38516 cp_parser_end_omp_structured_block (parser, save);
38517 tree body = finish_omp_structured_block (sb);
38518 if (ret == NULL_TREE)
38519 return false;
38520 if (ccode == OMP_TEAMS && !processing_template_decl)
38521 {
38522 /* For combined target teams, ensure the num_teams and
38523 thread_limit clause expressions are evaluated on the host,
38524 before entering the target construct. */
38525 tree c;
38526 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38527 c; c = OMP_CLAUSE_CHAIN (c))
38528 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
38529 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
38530 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
38531 {
38532 tree expr = OMP_CLAUSE_OPERAND (c, 0);
38533 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
38534 if (expr == error_mark_node)
38535 continue;
38536 tree tmp = TARGET_EXPR_SLOT (expr);
38537 add_stmt (expr);
38538 OMP_CLAUSE_OPERAND (c, 0) = expr;
38539 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
38540 OMP_CLAUSE_FIRSTPRIVATE);
38541 OMP_CLAUSE_DECL (tc) = tmp;
38542 OMP_CLAUSE_CHAIN (tc)
38543 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38544 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
38545 }
38546 }
38547 tree stmt = make_node (OMP_TARGET);
38548 TREE_TYPE (stmt) = void_type_node;
38549 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38550 OMP_TARGET_BODY (stmt) = body;
38551 OMP_TARGET_COMBINED (stmt) = 1;
38552 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38553 add_stmt (stmt);
38554 pc = &OMP_TARGET_CLAUSES (stmt);
38555 goto check_clauses;
38556 }
38557 else if (!flag_openmp) /* flag_openmp_simd */
38558 {
38559 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38560 return false;
38561 }
38562 else if (strcmp (p, "data") == 0)
38563 {
38564 cp_lexer_consume_token (parser->lexer);
38565 cp_parser_omp_target_data (parser, pragma_tok, if_p);
38566 return true;
38567 }
38568 else if (strcmp (p, "enter") == 0)
38569 {
38570 cp_lexer_consume_token (parser->lexer);
38571 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
38572 return false;
38573 }
38574 else if (strcmp (p, "exit") == 0)
38575 {
38576 cp_lexer_consume_token (parser->lexer);
38577 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
38578 return false;
38579 }
38580 else if (strcmp (p, "update") == 0)
38581 {
38582 cp_lexer_consume_token (parser->lexer);
38583 return cp_parser_omp_target_update (parser, pragma_tok, context);
38584 }
38585 }
38586 if (!flag_openmp) /* flag_openmp_simd */
38587 {
38588 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38589 return false;
38590 }
38591
38592 stmt = make_node (OMP_TARGET);
38593 TREE_TYPE (stmt) = void_type_node;
38594
38595 OMP_TARGET_CLAUSES (stmt)
38596 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
38597 "#pragma omp target", pragma_tok);
38598 pc = &OMP_TARGET_CLAUSES (stmt);
38599 keep_next_level (true);
38600 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38601
38602 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38603 add_stmt (stmt);
38604
38605 check_clauses:
38606 while (*pc)
38607 {
38608 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38609 switch (OMP_CLAUSE_MAP_KIND (*pc))
38610 {
38611 case GOMP_MAP_TO:
38612 case GOMP_MAP_ALWAYS_TO:
38613 case GOMP_MAP_FROM:
38614 case GOMP_MAP_ALWAYS_FROM:
38615 case GOMP_MAP_TOFROM:
38616 case GOMP_MAP_ALWAYS_TOFROM:
38617 case GOMP_MAP_ALLOC:
38618 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38619 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38620 case GOMP_MAP_ALWAYS_POINTER:
38621 break;
38622 default:
38623 error_at (OMP_CLAUSE_LOCATION (*pc),
38624 "%<#pragma omp target%> with map-type other "
38625 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38626 "on %<map%> clause");
38627 *pc = OMP_CLAUSE_CHAIN (*pc);
38628 continue;
38629 }
38630 pc = &OMP_CLAUSE_CHAIN (*pc);
38631 }
38632 return true;
38633 }
38634
38635 /* OpenACC 2.0:
38636 # pragma acc cache (variable-list) new-line
38637 */
38638
38639 static tree
38640 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
38641 {
38642 tree stmt, clauses;
38643
38644 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
38645 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38646
38647 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
38648
38649 stmt = make_node (OACC_CACHE);
38650 TREE_TYPE (stmt) = void_type_node;
38651 OACC_CACHE_CLAUSES (stmt) = clauses;
38652 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38653 add_stmt (stmt);
38654
38655 return stmt;
38656 }
38657
38658 /* OpenACC 2.0:
38659 # pragma acc data oacc-data-clause[optseq] new-line
38660 structured-block */
38661
38662 #define OACC_DATA_CLAUSE_MASK \
38663 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38670
38671 static tree
38672 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38673 {
38674 tree stmt, clauses, block;
38675 unsigned int save;
38676
38677 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38678 "#pragma acc data", pragma_tok);
38679
38680 block = begin_omp_parallel ();
38681 save = cp_parser_begin_omp_structured_block (parser);
38682 cp_parser_statement (parser, NULL_TREE, false, if_p);
38683 cp_parser_end_omp_structured_block (parser, save);
38684 stmt = finish_oacc_data (clauses, block);
38685 return stmt;
38686 }
38687
38688 /* OpenACC 2.0:
38689 # pragma acc host_data <clauses> new-line
38690 structured-block */
38691
38692 #define OACC_HOST_DATA_CLAUSE_MASK \
38693 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38694
38695 static tree
38696 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38697 {
38698 tree stmt, clauses, block;
38699 unsigned int save;
38700
38701 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38702 "#pragma acc host_data", pragma_tok);
38703
38704 block = begin_omp_parallel ();
38705 save = cp_parser_begin_omp_structured_block (parser);
38706 cp_parser_statement (parser, NULL_TREE, false, if_p);
38707 cp_parser_end_omp_structured_block (parser, save);
38708 stmt = finish_oacc_host_data (clauses, block);
38709 return stmt;
38710 }
38711
38712 /* OpenACC 2.0:
38713 # pragma acc declare oacc-data-clause[optseq] new-line
38714 */
38715
38716 #define OACC_DECLARE_CLAUSE_MASK \
38717 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38725
38726 static tree
38727 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38728 {
38729 tree clauses, stmt;
38730 bool error = false;
38731
38732 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38733 "#pragma acc declare", pragma_tok, true);
38734
38735
38736 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38737 {
38738 error_at (pragma_tok->location,
38739 "no valid clauses specified in %<#pragma acc declare%>");
38740 return NULL_TREE;
38741 }
38742
38743 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38744 {
38745 location_t loc = OMP_CLAUSE_LOCATION (t);
38746 tree decl = OMP_CLAUSE_DECL (t);
38747 if (!DECL_P (decl))
38748 {
38749 error_at (loc, "array section in %<#pragma acc declare%>");
38750 error = true;
38751 continue;
38752 }
38753 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38754 switch (OMP_CLAUSE_MAP_KIND (t))
38755 {
38756 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38757 case GOMP_MAP_ALLOC:
38758 case GOMP_MAP_TO:
38759 case GOMP_MAP_FORCE_DEVICEPTR:
38760 case GOMP_MAP_DEVICE_RESIDENT:
38761 break;
38762
38763 case GOMP_MAP_LINK:
38764 if (!global_bindings_p ()
38765 && (TREE_STATIC (decl)
38766 || !DECL_EXTERNAL (decl)))
38767 {
38768 error_at (loc,
38769 "%qD must be a global variable in "
38770 "%<#pragma acc declare link%>",
38771 decl);
38772 error = true;
38773 continue;
38774 }
38775 break;
38776
38777 default:
38778 if (global_bindings_p ())
38779 {
38780 error_at (loc, "invalid OpenACC clause at file scope");
38781 error = true;
38782 continue;
38783 }
38784 if (DECL_EXTERNAL (decl))
38785 {
38786 error_at (loc,
38787 "invalid use of %<extern%> variable %qD "
38788 "in %<#pragma acc declare%>", decl);
38789 error = true;
38790 continue;
38791 }
38792 else if (TREE_PUBLIC (decl))
38793 {
38794 error_at (loc,
38795 "invalid use of %<global%> variable %qD "
38796 "in %<#pragma acc declare%>", decl);
38797 error = true;
38798 continue;
38799 }
38800 break;
38801 }
38802
38803 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38804 || lookup_attribute ("omp declare target link",
38805 DECL_ATTRIBUTES (decl)))
38806 {
38807 error_at (loc, "variable %qD used more than once with "
38808 "%<#pragma acc declare%>", decl);
38809 error = true;
38810 continue;
38811 }
38812
38813 if (!error)
38814 {
38815 tree id;
38816
38817 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38818 id = get_identifier ("omp declare target link");
38819 else
38820 id = get_identifier ("omp declare target");
38821
38822 DECL_ATTRIBUTES (decl)
38823 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38824 if (global_bindings_p ())
38825 {
38826 symtab_node *node = symtab_node::get (decl);
38827 if (node != NULL)
38828 {
38829 node->offloadable = 1;
38830 if (ENABLE_OFFLOADING)
38831 {
38832 g->have_offload = true;
38833 if (is_a <varpool_node *> (node))
38834 vec_safe_push (offload_vars, decl);
38835 }
38836 }
38837 }
38838 }
38839 }
38840
38841 if (error || global_bindings_p ())
38842 return NULL_TREE;
38843
38844 stmt = make_node (OACC_DECLARE);
38845 TREE_TYPE (stmt) = void_type_node;
38846 OACC_DECLARE_CLAUSES (stmt) = clauses;
38847 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38848
38849 add_stmt (stmt);
38850
38851 return NULL_TREE;
38852 }
38853
38854 /* OpenACC 2.0:
38855 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38856
38857 or
38858
38859 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38860
38861 LOC is the location of the #pragma token.
38862 */
38863
38864 #define OACC_ENTER_DATA_CLAUSE_MASK \
38865 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38870
38871 #define OACC_EXIT_DATA_CLAUSE_MASK \
38872 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38878
38879 static tree
38880 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38881 bool enter)
38882 {
38883 location_t loc = pragma_tok->location;
38884 tree stmt, clauses;
38885 const char *p = "";
38886
38887 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38888 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38889
38890 if (strcmp (p, "data") != 0)
38891 {
38892 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38893 enter ? "enter" : "exit");
38894 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38895 return NULL_TREE;
38896 }
38897
38898 cp_lexer_consume_token (parser->lexer);
38899
38900 if (enter)
38901 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
38902 "#pragma acc enter data", pragma_tok);
38903 else
38904 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
38905 "#pragma acc exit data", pragma_tok);
38906
38907 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38908 {
38909 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
38910 enter ? "enter" : "exit");
38911 return NULL_TREE;
38912 }
38913
38914 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
38915 TREE_TYPE (stmt) = void_type_node;
38916 OMP_STANDALONE_CLAUSES (stmt) = clauses;
38917 SET_EXPR_LOCATION (stmt, loc);
38918 add_stmt (stmt);
38919 return stmt;
38920 }
38921
38922 /* OpenACC 2.0:
38923 # pragma acc loop oacc-loop-clause[optseq] new-line
38924 structured-block */
38925
38926 #define OACC_LOOP_CLAUSE_MASK \
38927 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38937
38938 static tree
38939 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
38940 omp_clause_mask mask, tree *cclauses, bool *if_p)
38941 {
38942 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
38943
38944 strcat (p_name, " loop");
38945 mask |= OACC_LOOP_CLAUSE_MASK;
38946
38947 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
38948 cclauses == NULL);
38949 if (cclauses)
38950 {
38951 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
38952 if (*cclauses)
38953 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
38954 if (clauses)
38955 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38956 }
38957
38958 tree block = begin_omp_structured_block ();
38959 int save = cp_parser_begin_omp_structured_block (parser);
38960 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
38961 cp_parser_end_omp_structured_block (parser, save);
38962 add_stmt (finish_omp_structured_block (block));
38963
38964 return stmt;
38965 }
38966
38967 /* OpenACC 2.0:
38968 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38969 structured-block
38970
38971 or
38972
38973 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38974 structured-block
38975 */
38976
38977 #define OACC_KERNELS_CLAUSE_MASK \
38978 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38991
38992 #define OACC_PARALLEL_CLAUSE_MASK \
38993 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
39001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
39003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
39004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
39005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
39006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
39007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
39008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39009
39010 static tree
39011 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
39012 char *p_name, bool *if_p)
39013 {
39014 omp_clause_mask mask;
39015 enum tree_code code;
39016 switch (cp_parser_pragma_kind (pragma_tok))
39017 {
39018 case PRAGMA_OACC_KERNELS:
39019 strcat (p_name, " kernels");
39020 mask = OACC_KERNELS_CLAUSE_MASK;
39021 code = OACC_KERNELS;
39022 break;
39023 case PRAGMA_OACC_PARALLEL:
39024 strcat (p_name, " parallel");
39025 mask = OACC_PARALLEL_CLAUSE_MASK;
39026 code = OACC_PARALLEL;
39027 break;
39028 default:
39029 gcc_unreachable ();
39030 }
39031
39032 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39033 {
39034 const char *p
39035 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
39036 if (strcmp (p, "loop") == 0)
39037 {
39038 cp_lexer_consume_token (parser->lexer);
39039 tree block = begin_omp_parallel ();
39040 tree clauses;
39041 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
39042 &clauses, if_p);
39043 protected_set_expr_location (stmt, pragma_tok->location);
39044 return finish_omp_construct (code, block, clauses);
39045 }
39046 }
39047
39048 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
39049
39050 tree block = begin_omp_parallel ();
39051 unsigned int save = cp_parser_begin_omp_structured_block (parser);
39052 cp_parser_statement (parser, NULL_TREE, false, if_p);
39053 cp_parser_end_omp_structured_block (parser, save);
39054 return finish_omp_construct (code, block, clauses);
39055 }
39056
39057 /* OpenACC 2.0:
39058 # pragma acc update oacc-update-clause[optseq] new-line
39059 */
39060
39061 #define OACC_UPDATE_CLAUSE_MASK \
39062 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
39064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
39065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
39067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
39068
39069 static tree
39070 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
39071 {
39072 tree stmt, clauses;
39073
39074 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
39075 "#pragma acc update", pragma_tok);
39076
39077 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
39078 {
39079 error_at (pragma_tok->location,
39080 "%<#pragma acc update%> must contain at least one "
39081 "%<device%> or %<host%> or %<self%> clause");
39082 return NULL_TREE;
39083 }
39084
39085 stmt = make_node (OACC_UPDATE);
39086 TREE_TYPE (stmt) = void_type_node;
39087 OACC_UPDATE_CLAUSES (stmt) = clauses;
39088 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39089 add_stmt (stmt);
39090 return stmt;
39091 }
39092
39093 /* OpenACC 2.0:
39094 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
39095
39096 LOC is the location of the #pragma token.
39097 */
39098
39099 #define OACC_WAIT_CLAUSE_MASK \
39100 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
39101
39102 static tree
39103 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
39104 {
39105 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
39106 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39107
39108 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39109 list = cp_parser_oacc_wait_list (parser, loc, list);
39110
39111 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
39112 "#pragma acc wait", pragma_tok);
39113
39114 stmt = c_finish_oacc_wait (loc, list, clauses);
39115 stmt = finish_expr_stmt (stmt);
39116
39117 return stmt;
39118 }
39119
39120 /* OpenMP 4.0:
39121 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
39122
39123 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
39124 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
39128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
39129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
39130
39131 static void
39132 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
39133 enum pragma_context context)
39134 {
39135 bool first_p = parser->omp_declare_simd == NULL;
39136 cp_omp_declare_simd_data data;
39137 if (first_p)
39138 {
39139 data.error_seen = false;
39140 data.fndecl_seen = false;
39141 data.tokens = vNULL;
39142 data.clauses = NULL_TREE;
39143 /* It is safe to take the address of a local variable; it will only be
39144 used while this scope is live. */
39145 parser->omp_declare_simd = &data;
39146 }
39147
39148 /* Store away all pragma tokens. */
39149 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39150 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39151 cp_lexer_consume_token (parser->lexer);
39152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39153 parser->omp_declare_simd->error_seen = true;
39154 cp_parser_require_pragma_eol (parser, pragma_tok);
39155 struct cp_token_cache *cp
39156 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39157 parser->omp_declare_simd->tokens.safe_push (cp);
39158
39159 if (first_p)
39160 {
39161 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39162 cp_parser_pragma (parser, context, NULL);
39163 switch (context)
39164 {
39165 case pragma_external:
39166 cp_parser_declaration (parser);
39167 break;
39168 case pragma_member:
39169 cp_parser_member_declaration (parser);
39170 break;
39171 case pragma_objc_icode:
39172 cp_parser_block_declaration (parser, /*statement_p=*/false);
39173 break;
39174 default:
39175 cp_parser_declaration_statement (parser);
39176 break;
39177 }
39178 if (parser->omp_declare_simd
39179 && !parser->omp_declare_simd->error_seen
39180 && !parser->omp_declare_simd->fndecl_seen)
39181 error_at (pragma_tok->location,
39182 "%<#pragma omp declare simd%> not immediately followed by "
39183 "function declaration or definition");
39184 data.tokens.release ();
39185 parser->omp_declare_simd = NULL;
39186 }
39187 }
39188
39189 /* Finalize #pragma omp declare simd clauses after direct declarator has
39190 been parsed, and put that into "omp declare simd" attribute. */
39191
39192 static tree
39193 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
39194 {
39195 struct cp_token_cache *ce;
39196 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
39197 int i;
39198
39199 if (!data->error_seen && data->fndecl_seen)
39200 {
39201 error ("%<#pragma omp declare simd%> not immediately followed by "
39202 "a single function declaration or definition");
39203 data->error_seen = true;
39204 }
39205 if (data->error_seen)
39206 return attrs;
39207
39208 FOR_EACH_VEC_ELT (data->tokens, i, ce)
39209 {
39210 tree c, cl;
39211
39212 cp_parser_push_lexer_for_tokens (parser, ce);
39213 parser->lexer->in_pragma = true;
39214 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39215 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39216 cp_lexer_consume_token (parser->lexer);
39217 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
39218 "#pragma omp declare simd", pragma_tok);
39219 cp_parser_pop_lexer (parser);
39220 if (cl)
39221 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
39222 c = build_tree_list (get_identifier ("omp declare simd"), cl);
39223 TREE_CHAIN (c) = attrs;
39224 if (processing_template_decl)
39225 ATTR_IS_DEPENDENT (c) = 1;
39226 attrs = c;
39227 }
39228
39229 data->fndecl_seen = true;
39230 return attrs;
39231 }
39232
39233
39234 /* OpenMP 4.0:
39235 # pragma omp declare target new-line
39236 declarations and definitions
39237 # pragma omp end declare target new-line
39238
39239 OpenMP 4.5:
39240 # pragma omp declare target ( extended-list ) new-line
39241
39242 # pragma omp declare target declare-target-clauses[seq] new-line */
39243
39244 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
39245 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
39247
39248 static void
39249 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
39250 {
39251 tree clauses = NULL_TREE;
39252 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39253 clauses
39254 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
39255 "#pragma omp declare target", pragma_tok);
39256 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39257 {
39258 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
39259 clauses);
39260 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39261 cp_parser_require_pragma_eol (parser, pragma_tok);
39262 }
39263 else
39264 {
39265 cp_parser_require_pragma_eol (parser, pragma_tok);
39266 scope_chain->omp_declare_target_attribute++;
39267 return;
39268 }
39269 if (scope_chain->omp_declare_target_attribute)
39270 error_at (pragma_tok->location,
39271 "%<#pragma omp declare target%> with clauses in between "
39272 "%<#pragma omp declare target%> without clauses and "
39273 "%<#pragma omp end declare target%>");
39274 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
39275 {
39276 tree t = OMP_CLAUSE_DECL (c), id;
39277 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
39278 tree at2 = lookup_attribute ("omp declare target link",
39279 DECL_ATTRIBUTES (t));
39280 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
39281 {
39282 id = get_identifier ("omp declare target link");
39283 std::swap (at1, at2);
39284 }
39285 else
39286 id = get_identifier ("omp declare target");
39287 if (at2)
39288 {
39289 error_at (OMP_CLAUSE_LOCATION (c),
39290 "%qD specified both in declare target %<link%> and %<to%>"
39291 " clauses", t);
39292 continue;
39293 }
39294 if (!at1)
39295 {
39296 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
39297 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
39298 continue;
39299
39300 symtab_node *node = symtab_node::get (t);
39301 if (node != NULL)
39302 {
39303 node->offloadable = 1;
39304 if (ENABLE_OFFLOADING)
39305 {
39306 g->have_offload = true;
39307 if (is_a <varpool_node *> (node))
39308 vec_safe_push (offload_vars, t);
39309 }
39310 }
39311 }
39312 }
39313 }
39314
39315 static void
39316 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
39317 {
39318 const char *p = "";
39319 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39320 {
39321 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39322 p = IDENTIFIER_POINTER (id);
39323 }
39324 if (strcmp (p, "declare") == 0)
39325 {
39326 cp_lexer_consume_token (parser->lexer);
39327 p = "";
39328 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39329 {
39330 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39331 p = IDENTIFIER_POINTER (id);
39332 }
39333 if (strcmp (p, "target") == 0)
39334 cp_lexer_consume_token (parser->lexer);
39335 else
39336 {
39337 cp_parser_error (parser, "expected %<target%>");
39338 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39339 return;
39340 }
39341 }
39342 else
39343 {
39344 cp_parser_error (parser, "expected %<declare%>");
39345 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39346 return;
39347 }
39348 cp_parser_require_pragma_eol (parser, pragma_tok);
39349 if (!scope_chain->omp_declare_target_attribute)
39350 error_at (pragma_tok->location,
39351 "%<#pragma omp end declare target%> without corresponding "
39352 "%<#pragma omp declare target%>");
39353 else
39354 scope_chain->omp_declare_target_attribute--;
39355 }
39356
39357 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39358 expression and optional initializer clause of
39359 #pragma omp declare reduction. We store the expression(s) as
39360 either 3, 6 or 7 special statements inside of the artificial function's
39361 body. The first two statements are DECL_EXPRs for the artificial
39362 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39363 expression that uses those variables.
39364 If there was any INITIALIZER clause, this is followed by further statements,
39365 the fourth and fifth statements are DECL_EXPRs for the artificial
39366 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39367 constructor variant (first token after open paren is not omp_priv),
39368 then the sixth statement is a statement with the function call expression
39369 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39370 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39371 to initialize the OMP_PRIV artificial variable and there is seventh
39372 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39373
39374 static bool
39375 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
39376 {
39377 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
39378 gcc_assert (TYPE_REF_P (type));
39379 type = TREE_TYPE (type);
39380 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
39381 DECL_ARTIFICIAL (omp_out) = 1;
39382 pushdecl (omp_out);
39383 add_decl_expr (omp_out);
39384 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
39385 DECL_ARTIFICIAL (omp_in) = 1;
39386 pushdecl (omp_in);
39387 add_decl_expr (omp_in);
39388 tree combiner;
39389 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
39390
39391 keep_next_level (true);
39392 tree block = begin_omp_structured_block ();
39393 combiner = cp_parser_expression (parser);
39394 finish_expr_stmt (combiner);
39395 block = finish_omp_structured_block (block);
39396 add_stmt (block);
39397
39398 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
39399 return false;
39400
39401 const char *p = "";
39402 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39403 {
39404 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39405 p = IDENTIFIER_POINTER (id);
39406 }
39407
39408 if (strcmp (p, "initializer") == 0)
39409 {
39410 cp_lexer_consume_token (parser->lexer);
39411 matching_parens parens;
39412 if (!parens.require_open (parser))
39413 return false;
39414
39415 p = "";
39416 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39417 {
39418 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39419 p = IDENTIFIER_POINTER (id);
39420 }
39421
39422 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
39423 DECL_ARTIFICIAL (omp_priv) = 1;
39424 pushdecl (omp_priv);
39425 add_decl_expr (omp_priv);
39426 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
39427 DECL_ARTIFICIAL (omp_orig) = 1;
39428 pushdecl (omp_orig);
39429 add_decl_expr (omp_orig);
39430
39431 keep_next_level (true);
39432 block = begin_omp_structured_block ();
39433
39434 bool ctor = false;
39435 if (strcmp (p, "omp_priv") == 0)
39436 {
39437 bool is_direct_init, is_non_constant_init;
39438 ctor = true;
39439 cp_lexer_consume_token (parser->lexer);
39440 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39441 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
39442 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39443 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
39444 == CPP_CLOSE_PAREN
39445 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
39446 == CPP_CLOSE_PAREN))
39447 {
39448 finish_omp_structured_block (block);
39449 error ("invalid initializer clause");
39450 return false;
39451 }
39452 initializer = cp_parser_initializer (parser, &is_direct_init,
39453 &is_non_constant_init);
39454 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
39455 NULL_TREE, LOOKUP_ONLYCONVERTING);
39456 }
39457 else
39458 {
39459 cp_parser_parse_tentatively (parser);
39460 /* Don't create location wrapper nodes here. */
39461 auto_suppress_location_wrappers sentinel;
39462 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
39463 /*check_dependency_p=*/true,
39464 /*template_p=*/NULL,
39465 /*declarator_p=*/false,
39466 /*optional_p=*/false);
39467 vec<tree, va_gc> *args;
39468 if (fn_name == error_mark_node
39469 || cp_parser_error_occurred (parser)
39470 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39471 || ((args = cp_parser_parenthesized_expression_list
39472 (parser, non_attr, /*cast_p=*/false,
39473 /*allow_expansion_p=*/true,
39474 /*non_constant_p=*/NULL)),
39475 cp_parser_error_occurred (parser)))
39476 {
39477 finish_omp_structured_block (block);
39478 cp_parser_abort_tentative_parse (parser);
39479 cp_parser_error (parser, "expected id-expression (arguments)");
39480 return false;
39481 }
39482 unsigned int i;
39483 tree arg;
39484 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
39485 if (arg == omp_priv
39486 || (TREE_CODE (arg) == ADDR_EXPR
39487 && TREE_OPERAND (arg, 0) == omp_priv))
39488 break;
39489 cp_parser_abort_tentative_parse (parser);
39490 if (arg == NULL_TREE)
39491 error ("one of the initializer call arguments should be %<omp_priv%>"
39492 " or %<&omp_priv%>");
39493 initializer = cp_parser_postfix_expression (parser, false, false, false,
39494 false, NULL);
39495 finish_expr_stmt (initializer);
39496 }
39497
39498 block = finish_omp_structured_block (block);
39499 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
39500 add_stmt (block);
39501
39502 if (ctor)
39503 add_decl_expr (omp_orig);
39504
39505 if (!parens.require_close (parser))
39506 return false;
39507 }
39508
39509 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
39510 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
39511 UNKNOWN_LOCATION);
39512
39513 return true;
39514 }
39515
39516 /* OpenMP 4.0
39517 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39518 initializer-clause[opt] new-line
39519
39520 initializer-clause:
39521 initializer (omp_priv initializer)
39522 initializer (function-name (argument-list)) */
39523
39524 static void
39525 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
39526 enum pragma_context)
39527 {
39528 auto_vec<tree> types;
39529 enum tree_code reduc_code = ERROR_MARK;
39530 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
39531 unsigned int i;
39532 cp_token *first_token;
39533 cp_token_cache *cp;
39534 int errs;
39535 void *p;
39536
39537 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39538 p = obstack_alloc (&declarator_obstack, 0);
39539
39540 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39541 goto fail;
39542
39543 switch (cp_lexer_peek_token (parser->lexer)->type)
39544 {
39545 case CPP_PLUS:
39546 reduc_code = PLUS_EXPR;
39547 break;
39548 case CPP_MULT:
39549 reduc_code = MULT_EXPR;
39550 break;
39551 case CPP_MINUS:
39552 reduc_code = MINUS_EXPR;
39553 break;
39554 case CPP_AND:
39555 reduc_code = BIT_AND_EXPR;
39556 break;
39557 case CPP_XOR:
39558 reduc_code = BIT_XOR_EXPR;
39559 break;
39560 case CPP_OR:
39561 reduc_code = BIT_IOR_EXPR;
39562 break;
39563 case CPP_AND_AND:
39564 reduc_code = TRUTH_ANDIF_EXPR;
39565 break;
39566 case CPP_OR_OR:
39567 reduc_code = TRUTH_ORIF_EXPR;
39568 break;
39569 case CPP_NAME:
39570 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
39571 break;
39572 default:
39573 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39574 "%<|%>, %<&&%>, %<||%> or identifier");
39575 goto fail;
39576 }
39577
39578 if (reduc_code != ERROR_MARK)
39579 cp_lexer_consume_token (parser->lexer);
39580
39581 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
39582 if (reduc_id == error_mark_node)
39583 goto fail;
39584
39585 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39586 goto fail;
39587
39588 /* Types may not be defined in declare reduction type list. */
39589 const char *saved_message;
39590 saved_message = parser->type_definition_forbidden_message;
39591 parser->type_definition_forbidden_message
39592 = G_("types may not be defined in declare reduction type list");
39593 bool saved_colon_corrects_to_scope_p;
39594 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39595 parser->colon_corrects_to_scope_p = false;
39596 bool saved_colon_doesnt_start_class_def_p;
39597 saved_colon_doesnt_start_class_def_p
39598 = parser->colon_doesnt_start_class_def_p;
39599 parser->colon_doesnt_start_class_def_p = true;
39600
39601 while (true)
39602 {
39603 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39604 type = cp_parser_type_id (parser);
39605 if (type == error_mark_node)
39606 ;
39607 else if (ARITHMETIC_TYPE_P (type)
39608 && (orig_reduc_id == NULL_TREE
39609 || (TREE_CODE (type) != COMPLEX_TYPE
39610 && (id_equal (orig_reduc_id, "min")
39611 || id_equal (orig_reduc_id, "max")))))
39612 error_at (loc, "predeclared arithmetic type %qT in "
39613 "%<#pragma omp declare reduction%>", type);
39614 else if (FUNC_OR_METHOD_TYPE_P (type)
39615 || TREE_CODE (type) == ARRAY_TYPE)
39616 error_at (loc, "function or array type %qT in "
39617 "%<#pragma omp declare reduction%>", type);
39618 else if (TYPE_REF_P (type))
39619 error_at (loc, "reference type %qT in "
39620 "%<#pragma omp declare reduction%>", type);
39621 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
39622 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
39623 "type %qT in %<#pragma omp declare reduction%>", type);
39624 else
39625 types.safe_push (type);
39626
39627 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39628 cp_lexer_consume_token (parser->lexer);
39629 else
39630 break;
39631 }
39632
39633 /* Restore the saved message. */
39634 parser->type_definition_forbidden_message = saved_message;
39635 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39636 parser->colon_doesnt_start_class_def_p
39637 = saved_colon_doesnt_start_class_def_p;
39638
39639 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
39640 || types.is_empty ())
39641 {
39642 fail:
39643 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39644 goto done;
39645 }
39646
39647 first_token = cp_lexer_peek_token (parser->lexer);
39648 cp = NULL;
39649 errs = errorcount;
39650 FOR_EACH_VEC_ELT (types, i, type)
39651 {
39652 tree fntype
39653 = build_function_type_list (void_type_node,
39654 cp_build_reference_type (type, false),
39655 NULL_TREE);
39656 tree this_reduc_id = reduc_id;
39657 if (!dependent_type_p (type))
39658 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39659 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39660 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39661 DECL_ARTIFICIAL (fndecl) = 1;
39662 DECL_EXTERNAL (fndecl) = 1;
39663 DECL_DECLARED_INLINE_P (fndecl) = 1;
39664 DECL_IGNORED_P (fndecl) = 1;
39665 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39666 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39667 DECL_ATTRIBUTES (fndecl)
39668 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39669 DECL_ATTRIBUTES (fndecl));
39670 if (processing_template_decl)
39671 fndecl = push_template_decl (fndecl);
39672 bool block_scope = false;
39673 tree block = NULL_TREE;
39674 if (current_function_decl)
39675 {
39676 block_scope = true;
39677 DECL_CONTEXT (fndecl) = global_namespace;
39678 if (!processing_template_decl)
39679 pushdecl (fndecl);
39680 }
39681 else if (current_class_type)
39682 {
39683 if (cp == NULL)
39684 {
39685 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39686 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39687 cp_lexer_consume_token (parser->lexer);
39688 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39689 goto fail;
39690 cp = cp_token_cache_new (first_token,
39691 cp_lexer_peek_nth_token (parser->lexer,
39692 2));
39693 }
39694 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39695 finish_member_declaration (fndecl);
39696 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39697 DECL_PENDING_INLINE_P (fndecl) = 1;
39698 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39699 continue;
39700 }
39701 else
39702 {
39703 DECL_CONTEXT (fndecl) = current_namespace;
39704 pushdecl (fndecl);
39705 }
39706 if (!block_scope)
39707 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39708 else
39709 block = begin_omp_structured_block ();
39710 if (cp)
39711 {
39712 cp_parser_push_lexer_for_tokens (parser, cp);
39713 parser->lexer->in_pragma = true;
39714 }
39715 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39716 {
39717 if (!block_scope)
39718 finish_function (/*inline_p=*/false);
39719 else
39720 DECL_CONTEXT (fndecl) = current_function_decl;
39721 if (cp)
39722 cp_parser_pop_lexer (parser);
39723 goto fail;
39724 }
39725 if (cp)
39726 cp_parser_pop_lexer (parser);
39727 if (!block_scope)
39728 finish_function (/*inline_p=*/false);
39729 else
39730 {
39731 DECL_CONTEXT (fndecl) = current_function_decl;
39732 block = finish_omp_structured_block (block);
39733 if (TREE_CODE (block) == BIND_EXPR)
39734 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39735 else if (TREE_CODE (block) == STATEMENT_LIST)
39736 DECL_SAVED_TREE (fndecl) = block;
39737 if (processing_template_decl)
39738 add_decl_expr (fndecl);
39739 }
39740 cp_check_omp_declare_reduction (fndecl);
39741 if (cp == NULL && types.length () > 1)
39742 cp = cp_token_cache_new (first_token,
39743 cp_lexer_peek_nth_token (parser->lexer, 2));
39744 if (errs != errorcount)
39745 break;
39746 }
39747
39748 cp_parser_require_pragma_eol (parser, pragma_tok);
39749
39750 done:
39751 /* Free any declarators allocated. */
39752 obstack_free (&declarator_obstack, p);
39753 }
39754
39755 /* OpenMP 4.0
39756 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39757 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39758 initializer-clause[opt] new-line
39759 #pragma omp declare target new-line */
39760
39761 static bool
39762 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39763 enum pragma_context context)
39764 {
39765 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39766 {
39767 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39768 const char *p = IDENTIFIER_POINTER (id);
39769
39770 if (strcmp (p, "simd") == 0)
39771 {
39772 cp_lexer_consume_token (parser->lexer);
39773 cp_parser_omp_declare_simd (parser, pragma_tok,
39774 context);
39775 return true;
39776 }
39777 cp_ensure_no_omp_declare_simd (parser);
39778 if (strcmp (p, "reduction") == 0)
39779 {
39780 cp_lexer_consume_token (parser->lexer);
39781 cp_parser_omp_declare_reduction (parser, pragma_tok,
39782 context);
39783 return false;
39784 }
39785 if (!flag_openmp) /* flag_openmp_simd */
39786 {
39787 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39788 return false;
39789 }
39790 if (strcmp (p, "target") == 0)
39791 {
39792 cp_lexer_consume_token (parser->lexer);
39793 cp_parser_omp_declare_target (parser, pragma_tok);
39794 return false;
39795 }
39796 }
39797 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39798 "or %<target%>");
39799 cp_parser_require_pragma_eol (parser, pragma_tok);
39800 return false;
39801 }
39802
39803 /* OpenMP 5.0
39804 #pragma omp requires clauses[optseq] new-line */
39805
39806 static bool
39807 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39808 {
39809 bool first = true;
39810 enum omp_requires new_req = (enum omp_requires) 0;
39811
39812 location_t loc = pragma_tok->location;
39813 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39814 {
39815 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39816 cp_lexer_consume_token (parser->lexer);
39817
39818 first = false;
39819
39820 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39821 {
39822 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39823 const char *p = IDENTIFIER_POINTER (id);
39824 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39825 enum omp_requires this_req = (enum omp_requires) 0;
39826
39827 if (!strcmp (p, "unified_address"))
39828 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39829 else if (!strcmp (p, "unified_shared_memory"))
39830 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39831 else if (!strcmp (p, "dynamic_allocators"))
39832 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39833 else if (!strcmp (p, "reverse_offload"))
39834 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39835 else if (!strcmp (p, "atomic_default_mem_order"))
39836 {
39837 cp_lexer_consume_token (parser->lexer);
39838
39839 matching_parens parens;
39840 if (parens.require_open (parser))
39841 {
39842 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39843 {
39844 id = cp_lexer_peek_token (parser->lexer)->u.value;
39845 p = IDENTIFIER_POINTER (id);
39846
39847 if (!strcmp (p, "seq_cst"))
39848 this_req
39849 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39850 else if (!strcmp (p, "relaxed"))
39851 this_req
39852 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39853 else if (!strcmp (p, "acq_rel"))
39854 this_req
39855 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39856 }
39857 if (this_req == 0)
39858 {
39859 error_at (cp_lexer_peek_token (parser->lexer)->location,
39860 "expected %<seq_cst%>, %<relaxed%> or "
39861 "%<acq_rel%>");
39862 if (cp_lexer_nth_token_is (parser->lexer, 2,
39863 CPP_CLOSE_PAREN))
39864 cp_lexer_consume_token (parser->lexer);
39865 }
39866 else
39867 cp_lexer_consume_token (parser->lexer);
39868
39869 if (!parens.require_close (parser))
39870 cp_parser_skip_to_closing_parenthesis (parser,
39871 /*recovering=*/true,
39872 /*or_comma=*/false,
39873 /*consume_paren=*/
39874 true);
39875
39876 if (this_req == 0)
39877 {
39878 cp_parser_require_pragma_eol (parser, pragma_tok);
39879 return false;
39880 }
39881 }
39882 p = NULL;
39883 }
39884 else
39885 {
39886 error_at (cloc, "expected %<unified_address%>, "
39887 "%<unified_shared_memory%>, "
39888 "%<dynamic_allocators%>, "
39889 "%<reverse_offload%> "
39890 "or %<atomic_default_mem_order%> clause");
39891 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39892 return false;
39893 }
39894 if (p)
39895 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39896 "supported yet", p);
39897 if (p)
39898 cp_lexer_consume_token (parser->lexer);
39899 if (this_req)
39900 {
39901 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39902 {
39903 if ((this_req & new_req) != 0)
39904 error_at (cloc, "too many %qs clauses", p);
39905 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
39906 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
39907 error_at (cloc, "%qs clause used lexically after first "
39908 "target construct or offloading API", p);
39909 }
39910 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39911 {
39912 error_at (cloc, "too many %qs clauses",
39913 "atomic_default_mem_order");
39914 this_req = (enum omp_requires) 0;
39915 }
39916 else if ((omp_requires_mask
39917 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39918 {
39919 error_at (cloc, "more than one %<atomic_default_mem_order%>"
39920 " clause in a single compilation unit");
39921 this_req
39922 = (enum omp_requires)
39923 (omp_requires_mask
39924 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
39925 }
39926 else if ((omp_requires_mask
39927 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
39928 error_at (cloc, "%<atomic_default_mem_order%> clause used "
39929 "lexically after first %<atomic%> construct "
39930 "without memory order clause");
39931 new_req = (enum omp_requires) (new_req | this_req);
39932 omp_requires_mask
39933 = (enum omp_requires) (omp_requires_mask | this_req);
39934 continue;
39935 }
39936 }
39937 break;
39938 }
39939 cp_parser_require_pragma_eol (parser, pragma_tok);
39940
39941 if (new_req == 0)
39942 error_at (loc, "%<pragma omp requires%> requires at least one clause");
39943 return false;
39944 }
39945
39946
39947 /* OpenMP 4.5:
39948 #pragma omp taskloop taskloop-clause[optseq] new-line
39949 for-loop
39950
39951 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39952 for-loop */
39953
39954 #define OMP_TASKLOOP_CLAUSE_MASK \
39955 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39971
39972 static tree
39973 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
39974 char *p_name, omp_clause_mask mask, tree *cclauses,
39975 bool *if_p)
39976 {
39977 tree clauses, sb, ret;
39978 unsigned int save;
39979 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39980
39981 strcat (p_name, " taskloop");
39982 mask |= OMP_TASKLOOP_CLAUSE_MASK;
39983 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39984 clause. */
39985 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
39986 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
39987
39988 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39989 {
39990 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39991 const char *p = IDENTIFIER_POINTER (id);
39992
39993 if (strcmp (p, "simd") == 0)
39994 {
39995 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39996 if (cclauses == NULL)
39997 cclauses = cclauses_buf;
39998
39999 cp_lexer_consume_token (parser->lexer);
40000 if (!flag_openmp) /* flag_openmp_simd */
40001 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40002 cclauses, if_p);
40003 sb = begin_omp_structured_block ();
40004 save = cp_parser_begin_omp_structured_block (parser);
40005 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40006 cclauses, if_p);
40007 cp_parser_end_omp_structured_block (parser, save);
40008 tree body = finish_omp_structured_block (sb);
40009 if (ret == NULL)
40010 return ret;
40011 ret = make_node (OMP_TASKLOOP);
40012 TREE_TYPE (ret) = void_type_node;
40013 OMP_FOR_BODY (ret) = body;
40014 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
40015 SET_EXPR_LOCATION (ret, loc);
40016 add_stmt (ret);
40017 return ret;
40018 }
40019 }
40020 if (!flag_openmp) /* flag_openmp_simd */
40021 {
40022 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40023 return NULL_TREE;
40024 }
40025
40026 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40027 cclauses == NULL);
40028 if (cclauses)
40029 {
40030 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
40031 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
40032 }
40033
40034 keep_next_level (true);
40035 sb = begin_omp_structured_block ();
40036 save = cp_parser_begin_omp_structured_block (parser);
40037
40038 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
40039 if_p);
40040
40041 cp_parser_end_omp_structured_block (parser, save);
40042 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40043
40044 return ret;
40045 }
40046
40047
40048 /* OpenACC 2.0:
40049 # pragma acc routine oacc-routine-clause[optseq] new-line
40050 function-definition
40051
40052 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
40053 */
40054
40055 #define OACC_ROUTINE_CLAUSE_MASK \
40056 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
40057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
40058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
40059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
40060
40061
40062 /* Parse the OpenACC routine pragma. This has an optional '( name )'
40063 component, which must resolve to a declared namespace-scope
40064 function. The clauses are either processed directly (for a named
40065 function), or defered until the immediatley following declaration
40066 is parsed. */
40067
40068 static void
40069 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
40070 enum pragma_context context)
40071 {
40072 gcc_checking_assert (context == pragma_external);
40073 /* The checking for "another pragma following this one" in the "no optional
40074 '( name )'" case makes sure that we dont re-enter. */
40075 gcc_checking_assert (parser->oacc_routine == NULL);
40076
40077 cp_oacc_routine_data data;
40078 data.error_seen = false;
40079 data.fndecl_seen = false;
40080 data.tokens = vNULL;
40081 data.clauses = NULL_TREE;
40082 data.loc = pragma_tok->location;
40083 /* It is safe to take the address of a local variable; it will only be
40084 used while this scope is live. */
40085 parser->oacc_routine = &data;
40086
40087 /* Look for optional '( name )'. */
40088 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
40089 {
40090 matching_parens parens;
40091 parens.consume_open (parser); /* '(' */
40092
40093 /* We parse the name as an id-expression. If it resolves to
40094 anything other than a non-overloaded function at namespace
40095 scope, it's an error. */
40096 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
40097 tree name = cp_parser_id_expression (parser,
40098 /*template_keyword_p=*/false,
40099 /*check_dependency_p=*/false,
40100 /*template_p=*/NULL,
40101 /*declarator_p=*/false,
40102 /*optional_p=*/false);
40103 tree decl = (identifier_p (name)
40104 ? cp_parser_lookup_name_simple (parser, name, name_loc)
40105 : name);
40106 if (name != error_mark_node && decl == error_mark_node)
40107 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
40108
40109 if (decl == error_mark_node
40110 || !parens.require_close (parser))
40111 {
40112 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40113 parser->oacc_routine = NULL;
40114 return;
40115 }
40116
40117 data.clauses
40118 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40119 "#pragma acc routine",
40120 cp_lexer_peek_token (parser->lexer));
40121 /* The clauses are in reverse order; fix that to make later diagnostic
40122 emission easier. */
40123 data.clauses = nreverse (data.clauses);
40124
40125 if (decl && is_overloaded_fn (decl)
40126 && (TREE_CODE (decl) != FUNCTION_DECL
40127 || DECL_FUNCTION_TEMPLATE_P (decl)))
40128 {
40129 error_at (name_loc,
40130 "%<#pragma acc routine%> names a set of overloads");
40131 parser->oacc_routine = NULL;
40132 return;
40133 }
40134
40135 /* Perhaps we should use the same rule as declarations in different
40136 namespaces? */
40137 if (!DECL_NAMESPACE_SCOPE_P (decl))
40138 {
40139 error_at (name_loc,
40140 "%qD does not refer to a namespace scope function", decl);
40141 parser->oacc_routine = NULL;
40142 return;
40143 }
40144
40145 if (TREE_CODE (decl) != FUNCTION_DECL)
40146 {
40147 error_at (name_loc, "%qD does not refer to a function", decl);
40148 parser->oacc_routine = NULL;
40149 return;
40150 }
40151
40152 cp_finalize_oacc_routine (parser, decl, false);
40153 parser->oacc_routine = NULL;
40154 }
40155 else /* No optional '( name )'. */
40156 {
40157 /* Store away all pragma tokens. */
40158 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
40159 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
40160 cp_lexer_consume_token (parser->lexer);
40161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40162 parser->oacc_routine->error_seen = true;
40163 cp_parser_require_pragma_eol (parser, pragma_tok);
40164 struct cp_token_cache *cp
40165 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
40166 parser->oacc_routine->tokens.safe_push (cp);
40167
40168 /* Emit a helpful diagnostic if there's another pragma following this
40169 one. */
40170 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
40171 {
40172 cp_ensure_no_oacc_routine (parser);
40173 data.tokens.release ();
40174 /* ..., and then just keep going. */
40175 return;
40176 }
40177
40178 /* We only have to consider the pragma_external case here. */
40179 cp_parser_declaration (parser);
40180 if (parser->oacc_routine
40181 && !parser->oacc_routine->fndecl_seen)
40182 cp_ensure_no_oacc_routine (parser);
40183 else
40184 parser->oacc_routine = NULL;
40185 data.tokens.release ();
40186 }
40187 }
40188
40189 /* Finalize #pragma acc routine clauses after direct declarator has
40190 been parsed. */
40191
40192 static tree
40193 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
40194 {
40195 struct cp_token_cache *ce;
40196 cp_oacc_routine_data *data = parser->oacc_routine;
40197
40198 if (!data->error_seen && data->fndecl_seen)
40199 {
40200 error_at (data->loc,
40201 "%<#pragma acc routine%> not immediately followed by "
40202 "a single function declaration or definition");
40203 data->error_seen = true;
40204 }
40205 if (data->error_seen)
40206 return attrs;
40207
40208 gcc_checking_assert (data->tokens.length () == 1);
40209 ce = data->tokens[0];
40210
40211 cp_parser_push_lexer_for_tokens (parser, ce);
40212 parser->lexer->in_pragma = true;
40213 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
40214
40215 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
40216 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
40217 parser->oacc_routine->clauses
40218 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40219 "#pragma acc routine", pragma_tok);
40220 /* The clauses are in reverse order; fix that to make later diagnostic
40221 emission easier. */
40222 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
40223 cp_parser_pop_lexer (parser);
40224 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40225 fndecl_seen. */
40226
40227 return attrs;
40228 }
40229
40230 /* Apply any saved OpenACC routine clauses to a just-parsed
40231 declaration. */
40232
40233 static void
40234 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
40235 {
40236 if (__builtin_expect (parser->oacc_routine != NULL, 0))
40237 {
40238 /* Keep going if we're in error reporting mode. */
40239 if (parser->oacc_routine->error_seen
40240 || fndecl == error_mark_node)
40241 return;
40242
40243 if (parser->oacc_routine->fndecl_seen)
40244 {
40245 error_at (parser->oacc_routine->loc,
40246 "%<#pragma acc routine%> not immediately followed by"
40247 " a single function declaration or definition");
40248 parser->oacc_routine = NULL;
40249 return;
40250 }
40251 if (TREE_CODE (fndecl) != FUNCTION_DECL)
40252 {
40253 cp_ensure_no_oacc_routine (parser);
40254 return;
40255 }
40256
40257 int compatible
40258 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
40259 parser->oacc_routine->loc,
40260 "#pragma acc routine");
40261 if (compatible < 0)
40262 {
40263 parser->oacc_routine = NULL;
40264 return;
40265 }
40266 if (compatible > 0)
40267 {
40268 }
40269 else
40270 {
40271 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
40272 {
40273 error_at (parser->oacc_routine->loc,
40274 TREE_USED (fndecl)
40275 ? G_("%<#pragma acc routine%> must be applied before"
40276 " use")
40277 : G_("%<#pragma acc routine%> must be applied before"
40278 " definition"));
40279 parser->oacc_routine = NULL;
40280 return;
40281 }
40282
40283 /* Set the routine's level of parallelism. */
40284 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
40285 oacc_replace_fn_attrib (fndecl, dims);
40286
40287 /* Add an "omp declare target" attribute. */
40288 DECL_ATTRIBUTES (fndecl)
40289 = tree_cons (get_identifier ("omp declare target"),
40290 parser->oacc_routine->clauses,
40291 DECL_ATTRIBUTES (fndecl));
40292 }
40293
40294 /* Don't unset parser->oacc_routine here: we may still need it to
40295 diagnose wrong usage. But, remember that we've used this "#pragma acc
40296 routine". */
40297 parser->oacc_routine->fndecl_seen = true;
40298 }
40299 }
40300
40301 /* Main entry point to OpenMP statement pragmas. */
40302
40303 static void
40304 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40305 {
40306 tree stmt;
40307 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
40308 omp_clause_mask mask (0);
40309
40310 switch (cp_parser_pragma_kind (pragma_tok))
40311 {
40312 case PRAGMA_OACC_ATOMIC:
40313 cp_parser_omp_atomic (parser, pragma_tok);
40314 return;
40315 case PRAGMA_OACC_CACHE:
40316 stmt = cp_parser_oacc_cache (parser, pragma_tok);
40317 break;
40318 case PRAGMA_OACC_DATA:
40319 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
40320 break;
40321 case PRAGMA_OACC_ENTER_DATA:
40322 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
40323 break;
40324 case PRAGMA_OACC_EXIT_DATA:
40325 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
40326 break;
40327 case PRAGMA_OACC_HOST_DATA:
40328 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
40329 break;
40330 case PRAGMA_OACC_KERNELS:
40331 case PRAGMA_OACC_PARALLEL:
40332 strcpy (p_name, "#pragma acc");
40333 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
40334 if_p);
40335 break;
40336 case PRAGMA_OACC_LOOP:
40337 strcpy (p_name, "#pragma acc");
40338 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
40339 if_p);
40340 break;
40341 case PRAGMA_OACC_UPDATE:
40342 stmt = cp_parser_oacc_update (parser, pragma_tok);
40343 break;
40344 case PRAGMA_OACC_WAIT:
40345 stmt = cp_parser_oacc_wait (parser, pragma_tok);
40346 break;
40347 case PRAGMA_OMP_ATOMIC:
40348 cp_parser_omp_atomic (parser, pragma_tok);
40349 return;
40350 case PRAGMA_OMP_CRITICAL:
40351 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
40352 break;
40353 case PRAGMA_OMP_DISTRIBUTE:
40354 strcpy (p_name, "#pragma omp");
40355 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
40356 if_p);
40357 break;
40358 case PRAGMA_OMP_FOR:
40359 strcpy (p_name, "#pragma omp");
40360 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
40361 if_p);
40362 break;
40363 case PRAGMA_OMP_MASTER:
40364 strcpy (p_name, "#pragma omp");
40365 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
40366 if_p);
40367 break;
40368 case PRAGMA_OMP_PARALLEL:
40369 strcpy (p_name, "#pragma omp");
40370 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
40371 if_p);
40372 break;
40373 case PRAGMA_OMP_SECTIONS:
40374 strcpy (p_name, "#pragma omp");
40375 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
40376 break;
40377 case PRAGMA_OMP_SIMD:
40378 strcpy (p_name, "#pragma omp");
40379 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
40380 if_p);
40381 break;
40382 case PRAGMA_OMP_SINGLE:
40383 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
40384 break;
40385 case PRAGMA_OMP_TASK:
40386 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
40387 break;
40388 case PRAGMA_OMP_TASKGROUP:
40389 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
40390 break;
40391 case PRAGMA_OMP_TASKLOOP:
40392 strcpy (p_name, "#pragma omp");
40393 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
40394 if_p);
40395 break;
40396 case PRAGMA_OMP_TEAMS:
40397 strcpy (p_name, "#pragma omp");
40398 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
40399 if_p);
40400 break;
40401 default:
40402 gcc_unreachable ();
40403 }
40404
40405 protected_set_expr_location (stmt, pragma_tok->location);
40406 }
40407 \f
40408 /* Transactional Memory parsing routines. */
40409
40410 /* Parse a transaction attribute.
40411
40412 txn-attribute:
40413 attribute
40414 [ [ identifier ] ]
40415
40416 We use this instead of cp_parser_attributes_opt for transactions to avoid
40417 the pedwarn in C++98 mode. */
40418
40419 static tree
40420 cp_parser_txn_attribute_opt (cp_parser *parser)
40421 {
40422 cp_token *token;
40423 tree attr_name, attr = NULL;
40424
40425 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
40426 return cp_parser_attributes_opt (parser);
40427
40428 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
40429 return NULL_TREE;
40430 cp_lexer_consume_token (parser->lexer);
40431 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
40432 goto error1;
40433
40434 token = cp_lexer_peek_token (parser->lexer);
40435 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
40436 {
40437 token = cp_lexer_consume_token (parser->lexer);
40438
40439 attr_name = (token->type == CPP_KEYWORD
40440 /* For keywords, use the canonical spelling,
40441 not the parsed identifier. */
40442 ? ridpointers[(int) token->keyword]
40443 : token->u.value);
40444 attr = build_tree_list (attr_name, NULL_TREE);
40445 }
40446 else
40447 cp_parser_error (parser, "expected identifier");
40448
40449 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40450 error1:
40451 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40452 return attr;
40453 }
40454
40455 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40456
40457 transaction-statement:
40458 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40459 compound-statement
40460 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40461 */
40462
40463 static tree
40464 cp_parser_transaction (cp_parser *parser, cp_token *token)
40465 {
40466 unsigned char old_in = parser->in_transaction;
40467 unsigned char this_in = 1, new_in;
40468 enum rid keyword = token->keyword;
40469 tree stmt, attrs, noex;
40470
40471 cp_lexer_consume_token (parser->lexer);
40472
40473 if (keyword == RID_TRANSACTION_RELAXED
40474 || keyword == RID_SYNCHRONIZED)
40475 this_in |= TM_STMT_ATTR_RELAXED;
40476 else
40477 {
40478 attrs = cp_parser_txn_attribute_opt (parser);
40479 if (attrs)
40480 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40481 }
40482
40483 /* Parse a noexcept specification. */
40484 if (keyword == RID_ATOMIC_NOEXCEPT)
40485 noex = boolean_true_node;
40486 else if (keyword == RID_ATOMIC_CANCEL)
40487 {
40488 /* cancel-and-throw is unimplemented. */
40489 sorry ("%<atomic_cancel%>");
40490 noex = NULL_TREE;
40491 }
40492 else
40493 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
40494
40495 /* Keep track if we're in the lexical scope of an outer transaction. */
40496 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
40497
40498 stmt = begin_transaction_stmt (token->location, NULL, this_in);
40499
40500 parser->in_transaction = new_in;
40501 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
40502 parser->in_transaction = old_in;
40503
40504 finish_transaction_stmt (stmt, NULL, this_in, noex);
40505
40506 return stmt;
40507 }
40508
40509 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40510
40511 transaction-expression:
40512 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40513 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40514 */
40515
40516 static tree
40517 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
40518 {
40519 unsigned char old_in = parser->in_transaction;
40520 unsigned char this_in = 1;
40521 cp_token *token;
40522 tree expr, noex;
40523 bool noex_expr;
40524 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40525
40526 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40527 || keyword == RID_TRANSACTION_RELAXED);
40528
40529 if (!flag_tm)
40530 error_at (loc,
40531 keyword == RID_TRANSACTION_RELAXED
40532 ? G_("%<__transaction_relaxed%> without transactional memory "
40533 "support enabled")
40534 : G_("%<__transaction_atomic%> without transactional memory "
40535 "support enabled"));
40536
40537 token = cp_parser_require_keyword (parser, keyword,
40538 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40539 : RT_TRANSACTION_RELAXED));
40540 gcc_assert (token != NULL);
40541
40542 if (keyword == RID_TRANSACTION_RELAXED)
40543 this_in |= TM_STMT_ATTR_RELAXED;
40544
40545 /* Set this early. This might mean that we allow transaction_cancel in
40546 an expression that we find out later actually has to be a constexpr.
40547 However, we expect that cxx_constant_value will be able to deal with
40548 this; also, if the noexcept has no constexpr, then what we parse next
40549 really is a transaction's body. */
40550 parser->in_transaction = this_in;
40551
40552 /* Parse a noexcept specification. */
40553 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
40554 true);
40555
40556 if (!noex || !noex_expr
40557 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40558 {
40559 matching_parens parens;
40560 parens.require_open (parser);
40561
40562 expr = cp_parser_expression (parser);
40563 expr = finish_parenthesized_expr (expr);
40564
40565 parens.require_close (parser);
40566 }
40567 else
40568 {
40569 /* The only expression that is available got parsed for the noexcept
40570 already. noexcept is true then. */
40571 expr = noex;
40572 noex = boolean_true_node;
40573 }
40574
40575 expr = build_transaction_expr (token->location, expr, this_in, noex);
40576 parser->in_transaction = old_in;
40577
40578 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
40579 return error_mark_node;
40580
40581 return (flag_tm ? expr : error_mark_node);
40582 }
40583
40584 /* Parse a function-transaction-block.
40585
40586 function-transaction-block:
40587 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40588 function-body
40589 __transaction_atomic txn-attribute[opt] function-try-block
40590 __transaction_relaxed ctor-initializer[opt] function-body
40591 __transaction_relaxed function-try-block
40592 */
40593
40594 static void
40595 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
40596 {
40597 unsigned char old_in = parser->in_transaction;
40598 unsigned char new_in = 1;
40599 tree compound_stmt, stmt, attrs;
40600 cp_token *token;
40601
40602 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40603 || keyword == RID_TRANSACTION_RELAXED);
40604 token = cp_parser_require_keyword (parser, keyword,
40605 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40606 : RT_TRANSACTION_RELAXED));
40607 gcc_assert (token != NULL);
40608
40609 if (keyword == RID_TRANSACTION_RELAXED)
40610 new_in |= TM_STMT_ATTR_RELAXED;
40611 else
40612 {
40613 attrs = cp_parser_txn_attribute_opt (parser);
40614 if (attrs)
40615 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40616 }
40617
40618 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
40619
40620 parser->in_transaction = new_in;
40621
40622 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
40623 cp_parser_function_try_block (parser);
40624 else
40625 cp_parser_ctor_initializer_opt_and_function_body
40626 (parser, /*in_function_try_block=*/false);
40627
40628 parser->in_transaction = old_in;
40629
40630 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
40631 }
40632
40633 /* Parse a __transaction_cancel statement.
40634
40635 cancel-statement:
40636 __transaction_cancel txn-attribute[opt] ;
40637 __transaction_cancel txn-attribute[opt] throw-expression ;
40638
40639 ??? Cancel and throw is not yet implemented. */
40640
40641 static tree
40642 cp_parser_transaction_cancel (cp_parser *parser)
40643 {
40644 cp_token *token;
40645 bool is_outer = false;
40646 tree stmt, attrs;
40647
40648 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
40649 RT_TRANSACTION_CANCEL);
40650 gcc_assert (token != NULL);
40651
40652 attrs = cp_parser_txn_attribute_opt (parser);
40653 if (attrs)
40654 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
40655
40656 /* ??? Parse cancel-and-throw here. */
40657
40658 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40659
40660 if (!flag_tm)
40661 {
40662 error_at (token->location, "%<__transaction_cancel%> without "
40663 "transactional memory support enabled");
40664 return error_mark_node;
40665 }
40666 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
40667 {
40668 error_at (token->location, "%<__transaction_cancel%> within a "
40669 "%<__transaction_relaxed%>");
40670 return error_mark_node;
40671 }
40672 else if (is_outer)
40673 {
40674 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40675 && !is_tm_may_cancel_outer (current_function_decl))
40676 {
40677 error_at (token->location, "outer %<__transaction_cancel%> not "
40678 "within outer %<__transaction_atomic%>");
40679 error_at (token->location,
40680 " or a %<transaction_may_cancel_outer%> function");
40681 return error_mark_node;
40682 }
40683 }
40684 else if (parser->in_transaction == 0)
40685 {
40686 error_at (token->location, "%<__transaction_cancel%> not within "
40687 "%<__transaction_atomic%>");
40688 return error_mark_node;
40689 }
40690
40691 stmt = build_tm_abort_call (token->location, is_outer);
40692 add_stmt (stmt);
40693
40694 return stmt;
40695 }
40696 \f
40697 /* The parser. */
40698
40699 static GTY (()) cp_parser *the_parser;
40700
40701 \f
40702 /* Special handling for the first token or line in the file. The first
40703 thing in the file might be #pragma GCC pch_preprocess, which loads a
40704 PCH file, which is a GC collection point. So we need to handle this
40705 first pragma without benefit of an existing lexer structure.
40706
40707 Always returns one token to the caller in *FIRST_TOKEN. This is
40708 either the true first token of the file, or the first token after
40709 the initial pragma. */
40710
40711 static void
40712 cp_parser_initial_pragma (cp_token *first_token)
40713 {
40714 tree name = NULL;
40715
40716 cp_lexer_get_preprocessor_token (NULL, first_token);
40717 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40718 return;
40719
40720 cp_lexer_get_preprocessor_token (NULL, first_token);
40721 if (first_token->type == CPP_STRING)
40722 {
40723 name = first_token->u.value;
40724
40725 cp_lexer_get_preprocessor_token (NULL, first_token);
40726 if (first_token->type != CPP_PRAGMA_EOL)
40727 error_at (first_token->location,
40728 "junk at end of %<#pragma GCC pch_preprocess%>");
40729 }
40730 else
40731 error_at (first_token->location, "expected string literal");
40732
40733 /* Skip to the end of the pragma. */
40734 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40735 cp_lexer_get_preprocessor_token (NULL, first_token);
40736
40737 /* Now actually load the PCH file. */
40738 if (name)
40739 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40740
40741 /* Read one more token to return to our caller. We have to do this
40742 after reading the PCH file in, since its pointers have to be
40743 live. */
40744 cp_lexer_get_preprocessor_token (NULL, first_token);
40745 }
40746
40747 /* Parse a pragma GCC ivdep. */
40748
40749 static bool
40750 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40751 {
40752 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40753 return true;
40754 }
40755
40756 /* Parse a pragma GCC unroll. */
40757
40758 static unsigned short
40759 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40760 {
40761 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40762 tree expr = cp_parser_constant_expression (parser);
40763 unsigned short unroll;
40764 expr = maybe_constant_value (expr);
40765 HOST_WIDE_INT lunroll = 0;
40766 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40767 || TREE_CODE (expr) != INTEGER_CST
40768 || (lunroll = tree_to_shwi (expr)) < 0
40769 || lunroll >= USHRT_MAX)
40770 {
40771 error_at (location, "%<#pragma GCC unroll%> requires an"
40772 " assignment-expression that evaluates to a non-negative"
40773 " integral constant less than %u", USHRT_MAX);
40774 unroll = 0;
40775 }
40776 else
40777 {
40778 unroll = (unsigned short)lunroll;
40779 if (unroll == 0)
40780 unroll = 1;
40781 }
40782 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40783 return unroll;
40784 }
40785
40786 /* Normal parsing of a pragma token. Here we can (and must) use the
40787 regular lexer. */
40788
40789 static bool
40790 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40791 {
40792 cp_token *pragma_tok;
40793 unsigned int id;
40794 tree stmt;
40795 bool ret;
40796
40797 pragma_tok = cp_lexer_consume_token (parser->lexer);
40798 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40799 parser->lexer->in_pragma = true;
40800
40801 id = cp_parser_pragma_kind (pragma_tok);
40802 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40803 cp_ensure_no_omp_declare_simd (parser);
40804 switch (id)
40805 {
40806 case PRAGMA_GCC_PCH_PREPROCESS:
40807 error_at (pragma_tok->location,
40808 "%<#pragma GCC pch_preprocess%> must be first");
40809 break;
40810
40811 case PRAGMA_OMP_BARRIER:
40812 switch (context)
40813 {
40814 case pragma_compound:
40815 cp_parser_omp_barrier (parser, pragma_tok);
40816 return false;
40817 case pragma_stmt:
40818 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40819 "used in compound statements", "omp barrier");
40820 break;
40821 default:
40822 goto bad_stmt;
40823 }
40824 break;
40825
40826 case PRAGMA_OMP_DEPOBJ:
40827 switch (context)
40828 {
40829 case pragma_compound:
40830 cp_parser_omp_depobj (parser, pragma_tok);
40831 return false;
40832 case pragma_stmt:
40833 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40834 "used in compound statements", "omp depobj");
40835 break;
40836 default:
40837 goto bad_stmt;
40838 }
40839 break;
40840
40841 case PRAGMA_OMP_FLUSH:
40842 switch (context)
40843 {
40844 case pragma_compound:
40845 cp_parser_omp_flush (parser, pragma_tok);
40846 return false;
40847 case pragma_stmt:
40848 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40849 "used in compound statements", "omp flush");
40850 break;
40851 default:
40852 goto bad_stmt;
40853 }
40854 break;
40855
40856 case PRAGMA_OMP_TASKWAIT:
40857 switch (context)
40858 {
40859 case pragma_compound:
40860 cp_parser_omp_taskwait (parser, pragma_tok);
40861 return false;
40862 case pragma_stmt:
40863 error_at (pragma_tok->location,
40864 "%<#pragma %s%> may only be used in compound statements",
40865 "omp taskwait");
40866 break;
40867 default:
40868 goto bad_stmt;
40869 }
40870 break;
40871
40872 case PRAGMA_OMP_TASKYIELD:
40873 switch (context)
40874 {
40875 case pragma_compound:
40876 cp_parser_omp_taskyield (parser, pragma_tok);
40877 return false;
40878 case pragma_stmt:
40879 error_at (pragma_tok->location,
40880 "%<#pragma %s%> may only be used in compound statements",
40881 "omp taskyield");
40882 break;
40883 default:
40884 goto bad_stmt;
40885 }
40886 break;
40887
40888 case PRAGMA_OMP_CANCEL:
40889 switch (context)
40890 {
40891 case pragma_compound:
40892 cp_parser_omp_cancel (parser, pragma_tok);
40893 return false;
40894 case pragma_stmt:
40895 error_at (pragma_tok->location,
40896 "%<#pragma %s%> may only be used in compound statements",
40897 "omp cancel");
40898 break;
40899 default:
40900 goto bad_stmt;
40901 }
40902 break;
40903
40904 case PRAGMA_OMP_CANCELLATION_POINT:
40905 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
40906 return false;
40907
40908 case PRAGMA_OMP_THREADPRIVATE:
40909 cp_parser_omp_threadprivate (parser, pragma_tok);
40910 return false;
40911
40912 case PRAGMA_OMP_DECLARE:
40913 return cp_parser_omp_declare (parser, pragma_tok, context);
40914
40915 case PRAGMA_OACC_DECLARE:
40916 cp_parser_oacc_declare (parser, pragma_tok);
40917 return false;
40918
40919 case PRAGMA_OACC_ENTER_DATA:
40920 if (context == pragma_stmt)
40921 {
40922 error_at (pragma_tok->location,
40923 "%<#pragma %s%> may only be used in compound statements",
40924 "acc enter data");
40925 break;
40926 }
40927 else if (context != pragma_compound)
40928 goto bad_stmt;
40929 cp_parser_omp_construct (parser, pragma_tok, if_p);
40930 return true;
40931
40932 case PRAGMA_OACC_EXIT_DATA:
40933 if (context == pragma_stmt)
40934 {
40935 error_at (pragma_tok->location,
40936 "%<#pragma %s%> may only be used in compound statements",
40937 "acc exit data");
40938 break;
40939 }
40940 else if (context != pragma_compound)
40941 goto bad_stmt;
40942 cp_parser_omp_construct (parser, pragma_tok, if_p);
40943 return true;
40944
40945 case PRAGMA_OACC_ROUTINE:
40946 if (context != pragma_external)
40947 {
40948 error_at (pragma_tok->location,
40949 "%<#pragma acc routine%> must be at file scope");
40950 break;
40951 }
40952 cp_parser_oacc_routine (parser, pragma_tok, context);
40953 return false;
40954
40955 case PRAGMA_OACC_UPDATE:
40956 if (context == pragma_stmt)
40957 {
40958 error_at (pragma_tok->location,
40959 "%<#pragma %s%> may only be used in compound statements",
40960 "acc update");
40961 break;
40962 }
40963 else if (context != pragma_compound)
40964 goto bad_stmt;
40965 cp_parser_omp_construct (parser, pragma_tok, if_p);
40966 return true;
40967
40968 case PRAGMA_OACC_WAIT:
40969 if (context == pragma_stmt)
40970 {
40971 error_at (pragma_tok->location,
40972 "%<#pragma %s%> may only be used in compound statements",
40973 "acc wait");
40974 break;
40975 }
40976 else if (context != pragma_compound)
40977 goto bad_stmt;
40978 cp_parser_omp_construct (parser, pragma_tok, if_p);
40979 return true;
40980
40981 case PRAGMA_OACC_ATOMIC:
40982 case PRAGMA_OACC_CACHE:
40983 case PRAGMA_OACC_DATA:
40984 case PRAGMA_OACC_HOST_DATA:
40985 case PRAGMA_OACC_KERNELS:
40986 case PRAGMA_OACC_PARALLEL:
40987 case PRAGMA_OACC_LOOP:
40988 case PRAGMA_OMP_ATOMIC:
40989 case PRAGMA_OMP_CRITICAL:
40990 case PRAGMA_OMP_DISTRIBUTE:
40991 case PRAGMA_OMP_FOR:
40992 case PRAGMA_OMP_MASTER:
40993 case PRAGMA_OMP_PARALLEL:
40994 case PRAGMA_OMP_SECTIONS:
40995 case PRAGMA_OMP_SIMD:
40996 case PRAGMA_OMP_SINGLE:
40997 case PRAGMA_OMP_TASK:
40998 case PRAGMA_OMP_TASKGROUP:
40999 case PRAGMA_OMP_TASKLOOP:
41000 case PRAGMA_OMP_TEAMS:
41001 if (context != pragma_stmt && context != pragma_compound)
41002 goto bad_stmt;
41003 stmt = push_omp_privatization_clauses (false);
41004 cp_parser_omp_construct (parser, pragma_tok, if_p);
41005 pop_omp_privatization_clauses (stmt);
41006 return true;
41007
41008 case PRAGMA_OMP_REQUIRES:
41009 return cp_parser_omp_requires (parser, pragma_tok);
41010
41011 case PRAGMA_OMP_ORDERED:
41012 if (context != pragma_stmt && context != pragma_compound)
41013 goto bad_stmt;
41014 stmt = push_omp_privatization_clauses (false);
41015 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
41016 pop_omp_privatization_clauses (stmt);
41017 return ret;
41018
41019 case PRAGMA_OMP_TARGET:
41020 if (context != pragma_stmt && context != pragma_compound)
41021 goto bad_stmt;
41022 stmt = push_omp_privatization_clauses (false);
41023 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
41024 pop_omp_privatization_clauses (stmt);
41025 return ret;
41026
41027 case PRAGMA_OMP_END_DECLARE_TARGET:
41028 cp_parser_omp_end_declare_target (parser, pragma_tok);
41029 return false;
41030
41031 case PRAGMA_OMP_SECTION:
41032 error_at (pragma_tok->location,
41033 "%<#pragma omp section%> may only be used in "
41034 "%<#pragma omp sections%> construct");
41035 break;
41036
41037 case PRAGMA_IVDEP:
41038 {
41039 if (context == pragma_external)
41040 {
41041 error_at (pragma_tok->location,
41042 "%<#pragma GCC ivdep%> must be inside a function");
41043 break;
41044 }
41045 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
41046 unsigned short unroll;
41047 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41048 if (tok->type == CPP_PRAGMA
41049 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
41050 {
41051 tok = cp_lexer_consume_token (parser->lexer);
41052 unroll = cp_parser_pragma_unroll (parser, tok);
41053 tok = cp_lexer_peek_token (the_parser->lexer);
41054 }
41055 else
41056 unroll = 0;
41057 if (tok->type != CPP_KEYWORD
41058 || (tok->keyword != RID_FOR
41059 && tok->keyword != RID_WHILE
41060 && tok->keyword != RID_DO))
41061 {
41062 cp_parser_error (parser, "for, while or do statement expected");
41063 return false;
41064 }
41065 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
41066 return true;
41067 }
41068
41069 case PRAGMA_UNROLL:
41070 {
41071 if (context == pragma_external)
41072 {
41073 error_at (pragma_tok->location,
41074 "%<#pragma GCC unroll%> must be inside a function");
41075 break;
41076 }
41077 const unsigned short unroll
41078 = cp_parser_pragma_unroll (parser, pragma_tok);
41079 bool ivdep;
41080 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41081 if (tok->type == CPP_PRAGMA
41082 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
41083 {
41084 tok = cp_lexer_consume_token (parser->lexer);
41085 ivdep = cp_parser_pragma_ivdep (parser, tok);
41086 tok = cp_lexer_peek_token (the_parser->lexer);
41087 }
41088 else
41089 ivdep = false;
41090 if (tok->type != CPP_KEYWORD
41091 || (tok->keyword != RID_FOR
41092 && tok->keyword != RID_WHILE
41093 && tok->keyword != RID_DO))
41094 {
41095 cp_parser_error (parser, "for, while or do statement expected");
41096 return false;
41097 }
41098 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
41099 return true;
41100 }
41101
41102 default:
41103 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
41104 c_invoke_pragma_handler (id);
41105 break;
41106
41107 bad_stmt:
41108 cp_parser_error (parser, "expected declaration specifiers");
41109 break;
41110 }
41111
41112 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41113 return false;
41114 }
41115
41116 /* The interface the pragma parsers have to the lexer. */
41117
41118 enum cpp_ttype
41119 pragma_lex (tree *value, location_t *loc)
41120 {
41121 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41122 enum cpp_ttype ret = tok->type;
41123
41124 *value = tok->u.value;
41125 if (loc)
41126 *loc = tok->location;
41127
41128 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
41129 ret = CPP_EOF;
41130 else if (ret == CPP_STRING)
41131 *value = cp_parser_string_literal (the_parser, false, false);
41132 else
41133 {
41134 if (ret == CPP_KEYWORD)
41135 ret = CPP_NAME;
41136 cp_lexer_consume_token (the_parser->lexer);
41137 }
41138
41139 return ret;
41140 }
41141
41142 \f
41143 /* External interface. */
41144
41145 /* Parse one entire translation unit. */
41146
41147 void
41148 c_parse_file (void)
41149 {
41150 static bool already_called = false;
41151
41152 if (already_called)
41153 fatal_error (input_location,
41154 "inter-module optimizations not implemented for C++");
41155 already_called = true;
41156
41157 the_parser = cp_parser_new ();
41158 push_deferring_access_checks (flag_access_control
41159 ? dk_no_deferred : dk_no_check);
41160 cp_parser_translation_unit (the_parser);
41161 the_parser = NULL;
41162
41163 finish_translation_unit ();
41164 }
41165
41166 /* Create an identifier for a generic parameter type (a synthesized
41167 template parameter implied by `auto' or a concept identifier). */
41168
41169 static GTY(()) int generic_parm_count;
41170 static tree
41171 make_generic_type_name ()
41172 {
41173 char buf[32];
41174 sprintf (buf, "auto:%d", ++generic_parm_count);
41175 return get_identifier (buf);
41176 }
41177
41178 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41179 (creating a new template parameter list if necessary). Returns the newly
41180 created template type parm. */
41181
41182 static tree
41183 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
41184 {
41185 gcc_assert (current_binding_level->kind == sk_function_parms);
41186
41187 /* Before committing to modifying any scope, if we're in an
41188 implicit template scope, and we're trying to synthesize a
41189 constrained parameter, try to find a previous parameter with
41190 the same name. This is the same-type rule for abbreviated
41191 function templates.
41192
41193 NOTE: We can generate implicit parameters when tentatively
41194 parsing a nested name specifier, only to reject that parse
41195 later. However, matching the same template-id as part of a
41196 direct-declarator should generate an identical template
41197 parameter, so this rule will merge them. */
41198 if (parser->implicit_template_scope && constr)
41199 {
41200 tree t = parser->implicit_template_parms;
41201 while (t)
41202 {
41203 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
41204 {
41205 tree d = TREE_VALUE (t);
41206 if (TREE_CODE (d) == PARM_DECL)
41207 /* Return the TEMPLATE_PARM_INDEX. */
41208 d = DECL_INITIAL (d);
41209 return d;
41210 }
41211 t = TREE_CHAIN (t);
41212 }
41213 }
41214
41215 /* We are either continuing a function template that already contains implicit
41216 template parameters, creating a new fully-implicit function template, or
41217 extending an existing explicit function template with implicit template
41218 parameters. */
41219
41220 cp_binding_level *const entry_scope = current_binding_level;
41221
41222 bool become_template = false;
41223 cp_binding_level *parent_scope = 0;
41224
41225 if (parser->implicit_template_scope)
41226 {
41227 gcc_assert (parser->implicit_template_parms);
41228
41229 current_binding_level = parser->implicit_template_scope;
41230 }
41231 else
41232 {
41233 /* Roll back to the existing template parameter scope (in the case of
41234 extending an explicit function template) or introduce a new template
41235 parameter scope ahead of the function parameter scope (or class scope
41236 in the case of out-of-line member definitions). The function scope is
41237 added back after template parameter synthesis below. */
41238
41239 cp_binding_level *scope = entry_scope;
41240
41241 while (scope->kind == sk_function_parms)
41242 {
41243 parent_scope = scope;
41244 scope = scope->level_chain;
41245 }
41246 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
41247 {
41248 /* If not defining a class, then any class scope is a scope level in
41249 an out-of-line member definition. In this case simply wind back
41250 beyond the first such scope to inject the template parameter list.
41251 Otherwise wind back to the class being defined. The latter can
41252 occur in class member friend declarations such as:
41253
41254 class A {
41255 void foo (auto);
41256 };
41257 class B {
41258 friend void A::foo (auto);
41259 };
41260
41261 The template parameter list synthesized for the friend declaration
41262 must be injected in the scope of 'B'. This can also occur in
41263 erroneous cases such as:
41264
41265 struct A {
41266 struct B {
41267 void foo (auto);
41268 };
41269 void B::foo (auto) {}
41270 };
41271
41272 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41273 but, nevertheless, the template parameter list synthesized for the
41274 declarator should be injected into the scope of 'A' as if the
41275 ill-formed template was specified explicitly. */
41276
41277 while (scope->kind == sk_class && !scope->defining_class_p)
41278 {
41279 parent_scope = scope;
41280 scope = scope->level_chain;
41281 }
41282 }
41283
41284 current_binding_level = scope;
41285
41286 if (scope->kind != sk_template_parms
41287 || !function_being_declared_is_template_p (parser))
41288 {
41289 /* Introduce a new template parameter list for implicit template
41290 parameters. */
41291
41292 become_template = true;
41293
41294 parser->implicit_template_scope
41295 = begin_scope (sk_template_parms, NULL);
41296
41297 ++processing_template_decl;
41298
41299 parser->fully_implicit_function_template_p = true;
41300 ++parser->num_template_parameter_lists;
41301 }
41302 else
41303 {
41304 /* Synthesize implicit template parameters at the end of the explicit
41305 template parameter list. */
41306
41307 gcc_assert (current_template_parms);
41308
41309 parser->implicit_template_scope = scope;
41310
41311 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41312 parser->implicit_template_parms
41313 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
41314 }
41315 }
41316
41317 /* Synthesize a new template parameter and track the current template
41318 parameter chain with implicit_template_parms. */
41319
41320 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
41321 tree synth_id = make_generic_type_name ();
41322 tree synth_tmpl_parm;
41323 bool non_type = false;
41324
41325 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
41326 synth_tmpl_parm
41327 = finish_template_type_parm (class_type_node, synth_id);
41328 else if (TREE_CODE (proto) == TEMPLATE_DECL)
41329 synth_tmpl_parm
41330 = finish_constrained_template_template_parm (proto, synth_id);
41331 else
41332 {
41333 synth_tmpl_parm = copy_decl (proto);
41334 DECL_NAME (synth_tmpl_parm) = synth_id;
41335 non_type = true;
41336 }
41337
41338 // Attach the constraint to the parm before processing.
41339 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
41340 TREE_TYPE (node) = constr;
41341 tree new_parm
41342 = process_template_parm (parser->implicit_template_parms,
41343 input_location,
41344 node,
41345 /*non_type=*/non_type,
41346 /*param_pack=*/false);
41347
41348 // Chain the new parameter to the list of implicit parameters.
41349 if (parser->implicit_template_parms)
41350 parser->implicit_template_parms
41351 = TREE_CHAIN (parser->implicit_template_parms);
41352 else
41353 parser->implicit_template_parms = new_parm;
41354
41355 tree new_decl = get_local_decls ();
41356 if (non_type)
41357 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41358 new_decl = DECL_INITIAL (new_decl);
41359
41360 /* If creating a fully implicit function template, start the new implicit
41361 template parameter list with this synthesized type, otherwise grow the
41362 current template parameter list. */
41363
41364 if (become_template)
41365 {
41366 parent_scope->level_chain = current_binding_level;
41367
41368 tree new_parms = make_tree_vec (1);
41369 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
41370 current_template_parms = tree_cons (size_int (processing_template_decl),
41371 new_parms, current_template_parms);
41372 }
41373 else
41374 {
41375 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41376 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
41377 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
41378 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
41379 }
41380
41381 // If the new parameter was constrained, we need to add that to the
41382 // constraints in the template parameter list.
41383 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
41384 {
41385 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
41386 reqs = conjoin_constraints (reqs, req);
41387 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
41388 }
41389
41390 current_binding_level = entry_scope;
41391
41392 return new_decl;
41393 }
41394
41395 /* Finish the declaration of a fully implicit function template. Such a
41396 template has no explicit template parameter list so has not been through the
41397 normal template head and tail processing. synthesize_implicit_template_parm
41398 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41399 provided if the declaration is a class member such that its template
41400 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41401 form is returned. Otherwise NULL_TREE is returned. */
41402
41403 static tree
41404 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
41405 {
41406 gcc_assert (parser->fully_implicit_function_template_p);
41407
41408 if (member_decl_opt && member_decl_opt != error_mark_node
41409 && DECL_VIRTUAL_P (member_decl_opt))
41410 {
41411 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
41412 "implicit templates may not be %<virtual%>");
41413 DECL_VIRTUAL_P (member_decl_opt) = false;
41414 }
41415
41416 if (member_decl_opt)
41417 member_decl_opt = finish_member_template_decl (member_decl_opt);
41418 end_template_decl ();
41419
41420 parser->fully_implicit_function_template_p = false;
41421 parser->implicit_template_parms = 0;
41422 parser->implicit_template_scope = 0;
41423 --parser->num_template_parameter_lists;
41424
41425 return member_decl_opt;
41426 }
41427
41428 /* Like finish_fully_implicit_template, but to be used in error
41429 recovery, rearranging scopes so that we restore the state we had
41430 before synthesize_implicit_template_parm inserted the implement
41431 template parms scope. */
41432
41433 static void
41434 abort_fully_implicit_template (cp_parser *parser)
41435 {
41436 cp_binding_level *return_to_scope = current_binding_level;
41437
41438 if (parser->implicit_template_scope
41439 && return_to_scope != parser->implicit_template_scope)
41440 {
41441 cp_binding_level *child = return_to_scope;
41442 for (cp_binding_level *scope = child->level_chain;
41443 scope != parser->implicit_template_scope;
41444 scope = child->level_chain)
41445 child = scope;
41446 child->level_chain = parser->implicit_template_scope->level_chain;
41447 parser->implicit_template_scope->level_chain = return_to_scope;
41448 current_binding_level = parser->implicit_template_scope;
41449 }
41450 else
41451 return_to_scope = return_to_scope->level_chain;
41452
41453 finish_fully_implicit_template (parser, NULL);
41454
41455 gcc_assert (current_binding_level == return_to_scope);
41456 }
41457
41458 /* Helper function for diagnostics that have complained about things
41459 being used with 'extern "C"' linkage.
41460
41461 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41462
41463 void
41464 maybe_show_extern_c_location (void)
41465 {
41466 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
41467 inform (the_parser->innermost_linkage_specification_location,
41468 "%<extern \"C\"%> linkage started here");
41469 }
41470
41471 #include "gt-cp-parser.h"