]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
PR c++/89767
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
48
49 \f
50 /* The lexer. */
51
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
54
55 static cp_token eof_token =
56 {
57 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
58 };
59
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant {
62 NIC_NONE,
63 /* floating-point literal */
64 NIC_FLOAT,
65 /* %<this%> */
66 NIC_THIS,
67 /* %<__FUNCTION__%> */
68 NIC_FUNC_NAME,
69 /* %<__PRETTY_FUNCTION__%> */
70 NIC_PRETTY_FUNC,
71 /* %<__func__%> */
72 NIC_C99_FUNC,
73 /* "%<va_arg%> */
74 NIC_VA_ARG,
75 /* a cast */
76 NIC_CAST,
77 /* %<typeid%> operator */
78 NIC_TYPEID,
79 /* non-constant compound literals */
80 NIC_NCC,
81 /* a function call */
82 NIC_FUNC_CALL,
83 /* an increment */
84 NIC_INC,
85 /* an decrement */
86 NIC_DEC,
87 /* an array reference */
88 NIC_ARRAY_REF,
89 /* %<->%> */
90 NIC_ARROW,
91 /* %<.%> */
92 NIC_POINT,
93 /* the address of a label */
94 NIC_ADDR_LABEL,
95 /* %<*%> */
96 NIC_STAR,
97 /* %<&%> */
98 NIC_ADDR,
99 /* %<++%> */
100 NIC_PREINCREMENT,
101 /* %<--%> */
102 NIC_PREDECREMENT,
103 /* %<new%> */
104 NIC_NEW,
105 /* %<delete%> */
106 NIC_DEL,
107 /* calls to overloaded operators */
108 NIC_OVERLOADED,
109 /* an assignment */
110 NIC_ASSIGNMENT,
111 /* a comma operator */
112 NIC_COMMA,
113 /* a call to a constructor */
114 NIC_CONSTRUCTOR,
115 /* a transaction expression */
116 NIC_TRANSACTION
117 };
118
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error {
121 /* NULL */
122 NLE_NULL,
123 /* is not a type */
124 NLE_TYPE,
125 /* is not a class or namespace */
126 NLE_CXX98,
127 /* is not a class, namespace, or enumeration */
128 NLE_NOT_CXX98
129 };
130
131 /* The various kinds of required token */
132 enum required_token {
133 RT_NONE,
134 RT_SEMICOLON, /* ';' */
135 RT_OPEN_PAREN, /* '(' */
136 RT_CLOSE_BRACE, /* '}' */
137 RT_OPEN_BRACE, /* '{' */
138 RT_CLOSE_SQUARE, /* ']' */
139 RT_OPEN_SQUARE, /* '[' */
140 RT_COMMA, /* ',' */
141 RT_SCOPE, /* '::' */
142 RT_LESS, /* '<' */
143 RT_GREATER, /* '>' */
144 RT_EQ, /* '=' */
145 RT_ELLIPSIS, /* '...' */
146 RT_MULT, /* '*' */
147 RT_COMPL, /* '~' */
148 RT_COLON, /* ':' */
149 RT_COLON_SCOPE, /* ':' or '::' */
150 RT_CLOSE_PAREN, /* ')' */
151 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
152 RT_PRAGMA_EOL, /* end of line */
153 RT_NAME, /* identifier */
154
155 /* The type is CPP_KEYWORD */
156 RT_NEW, /* new */
157 RT_DELETE, /* delete */
158 RT_RETURN, /* return */
159 RT_WHILE, /* while */
160 RT_EXTERN, /* extern */
161 RT_STATIC_ASSERT, /* static_assert */
162 RT_DECLTYPE, /* decltype */
163 RT_OPERATOR, /* operator */
164 RT_CLASS, /* class */
165 RT_TEMPLATE, /* template */
166 RT_NAMESPACE, /* namespace */
167 RT_USING, /* using */
168 RT_ASM, /* asm */
169 RT_TRY, /* try */
170 RT_CATCH, /* catch */
171 RT_THROW, /* throw */
172 RT_LABEL, /* __label__ */
173 RT_AT_TRY, /* @try */
174 RT_AT_SYNCHRONIZED, /* @synchronized */
175 RT_AT_THROW, /* @throw */
176
177 RT_SELECT, /* selection-statement */
178 RT_ITERATION, /* iteration-statement */
179 RT_JUMP, /* jump-statement */
180 RT_CLASS_KEY, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL /* __transaction_cancel */
185 };
186
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
189
190 class type_id_in_expr_sentinel
191 {
192 cp_parser *parser;
193 bool saved;
194 public:
195 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
196 : parser (parser),
197 saved (parser->in_type_id_in_expr_p)
198 { parser->in_type_id_in_expr_p = set; }
199 ~type_id_in_expr_sentinel ()
200 { parser->in_type_id_in_expr_p = saved; }
201 };
202
203 /* Prototypes. */
204
205 static cp_lexer *cp_lexer_new_main
206 (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208 (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210 (cp_lexer *);
211 static int cp_lexer_saving_tokens
212 (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214 (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218 (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220 (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228 (cp_lexer *);
229 static void cp_lexer_purge_token
230 (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234 (cp_lexer *);
235 static void cp_lexer_commit_tokens
236 (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238 (cp_lexer *);
239 static void cp_lexer_print_token
240 (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242 (cp_lexer *);
243 static void cp_lexer_start_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
247
248 static cp_token_cache *cp_token_cache_new
249 (cp_token *, cp_token *);
250
251 static void cp_parser_initial_pragma
252 (cp_token *);
253
254 static bool cp_parser_omp_declare_reduction_exprs
255 (tree, cp_parser *);
256 static void cp_finalize_oacc_routine
257 (cp_parser *, tree, bool);
258
259 /* Manifest constants. */
260 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
261 #define CP_SAVED_TOKEN_STACK 5
262
263 /* Variables. */
264
265 /* The stream to which debugging output should be written. */
266 static FILE *cp_lexer_debug_stream;
267
268 /* Nonzero if we are parsing an unevaluated operand: an operand to
269 sizeof, typeof, or alignof. */
270 int cp_unevaluated_operand;
271
272 /* Dump up to NUM tokens in BUFFER to FILE starting with token
273 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
274 first token in BUFFER. If NUM is 0, dump all the tokens. If
275 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
276 highlighted by surrounding it in [[ ]]. */
277
278 static void
279 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
280 cp_token *start_token, unsigned num,
281 cp_token *curr_token)
282 {
283 unsigned i, nprinted;
284 cp_token *token;
285 bool do_print;
286
287 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288
289 if (buffer == NULL)
290 return;
291
292 if (num == 0)
293 num = buffer->length ();
294
295 if (start_token == NULL)
296 start_token = buffer->address ();
297
298 if (start_token > buffer->address ())
299 {
300 cp_lexer_print_token (file, &(*buffer)[0]);
301 fprintf (file, " ... ");
302 }
303
304 do_print = false;
305 nprinted = 0;
306 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 {
308 if (token == start_token)
309 do_print = true;
310
311 if (!do_print)
312 continue;
313
314 nprinted++;
315 if (token == curr_token)
316 fprintf (file, "[[");
317
318 cp_lexer_print_token (file, token);
319
320 if (token == curr_token)
321 fprintf (file, "]]");
322
323 switch (token->type)
324 {
325 case CPP_SEMICOLON:
326 case CPP_OPEN_BRACE:
327 case CPP_CLOSE_BRACE:
328 case CPP_EOF:
329 fputc ('\n', file);
330 break;
331
332 default:
333 fputc (' ', file);
334 }
335 }
336
337 if (i == num && i < buffer->length ())
338 {
339 fprintf (file, " ... ");
340 cp_lexer_print_token (file, &buffer->last ());
341 }
342
343 fprintf (file, "\n");
344 }
345
346
347 /* Dump all tokens in BUFFER to stderr. */
348
349 void
350 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 {
352 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
353 }
354
355 DEBUG_FUNCTION void
356 debug (vec<cp_token, va_gc> &ref)
357 {
358 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
359 }
360
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> *ptr)
363 {
364 if (ptr)
365 debug (*ptr);
366 else
367 fprintf (stderr, "<nil>\n");
368 }
369
370
371 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
372 description for T. */
373
374 static void
375 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 {
377 if (t)
378 {
379 fprintf (file, "%s: ", desc);
380 print_node_brief (file, "", t, 0);
381 }
382 }
383
384
385 /* Dump parser context C to FILE. */
386
387 static void
388 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 {
390 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
391 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
392 print_node_brief (file, "", c->object_type, 0);
393 fprintf (file, "}\n");
394 }
395
396
397 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398
399 static void
400 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 {
402 unsigned i;
403 cp_parser_context *c;
404
405 fprintf (file, "Parsing context stack:\n");
406 for (i = 0, c = first; c; c = c->next, i++)
407 {
408 fprintf (file, "\t#%u: ", i);
409 cp_debug_print_context (file, c);
410 }
411 }
412
413
414 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415
416 static void
417 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 {
419 if (flag)
420 fprintf (file, "%s: true\n", desc);
421 }
422
423
424 /* Print an unparsed function entry UF to FILE. */
425
426 static void
427 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 {
429 unsigned i;
430 cp_default_arg_entry *default_arg_fn;
431 tree fn;
432
433 fprintf (file, "\tFunctions with default args:\n");
434 for (i = 0;
435 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
436 i++)
437 {
438 fprintf (file, "\t\tClass type: ");
439 print_node_brief (file, "", default_arg_fn->class_type, 0);
440 fprintf (file, "\t\tDeclaration: ");
441 print_node_brief (file, "", default_arg_fn->decl, 0);
442 fprintf (file, "\n");
443 }
444
445 fprintf (file, "\n\tFunctions with definitions that require "
446 "post-processing\n\t\t");
447 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 {
449 print_node_brief (file, "", fn, 0);
450 fprintf (file, " ");
451 }
452 fprintf (file, "\n");
453
454 fprintf (file, "\n\tNon-static data members with initializers that require "
455 "post-processing\n\t\t");
456 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 {
458 print_node_brief (file, "", fn, 0);
459 fprintf (file, " ");
460 }
461 fprintf (file, "\n");
462 }
463
464
465 /* Print the stack of unparsed member functions S to FILE. */
466
467 static void
468 cp_debug_print_unparsed_queues (FILE *file,
469 vec<cp_unparsed_functions_entry, va_gc> *s)
470 {
471 unsigned i;
472 cp_unparsed_functions_entry *uf;
473
474 fprintf (file, "Unparsed functions\n");
475 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 {
477 fprintf (file, "#%u:\n", i);
478 cp_debug_print_unparsed_function (file, uf);
479 }
480 }
481
482
483 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
484 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485
486 static void
487 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 {
489 cp_token *next_token, *first_token, *start_token;
490
491 if (file == NULL)
492 file = stderr;
493
494 next_token = parser->lexer->next_token;
495 first_token = parser->lexer->buffer->address ();
496 start_token = (next_token > first_token + window_size / 2)
497 ? next_token - window_size / 2
498 : first_token;
499 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
500 next_token);
501 }
502
503
504 /* Dump debugging information for the given PARSER. If FILE is NULL,
505 the output is printed on stderr. */
506
507 void
508 cp_debug_parser (FILE *file, cp_parser *parser)
509 {
510 const size_t window_size = 20;
511 cp_token *token;
512 expanded_location eloc;
513
514 if (file == NULL)
515 file = stderr;
516
517 fprintf (file, "Parser state\n\n");
518 fprintf (file, "Number of tokens: %u\n",
519 vec_safe_length (parser->lexer->buffer));
520 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
521 cp_debug_print_tree_if_set (file, "Object scope",
522 parser->object_scope);
523 cp_debug_print_tree_if_set (file, "Qualifying scope",
524 parser->qualifying_scope);
525 cp_debug_print_context_stack (file, parser->context);
526 cp_debug_print_flag (file, "Allow GNU extensions",
527 parser->allow_gnu_extensions_p);
528 cp_debug_print_flag (file, "'>' token is greater-than",
529 parser->greater_than_is_operator_p);
530 cp_debug_print_flag (file, "Default args allowed in current "
531 "parameter list", parser->default_arg_ok_p);
532 cp_debug_print_flag (file, "Parsing integral constant-expression",
533 parser->integral_constant_expression_p);
534 cp_debug_print_flag (file, "Allow non-constant expression in current "
535 "constant-expression",
536 parser->allow_non_integral_constant_expression_p);
537 cp_debug_print_flag (file, "Seen non-constant expression",
538 parser->non_integral_constant_expression_p);
539 cp_debug_print_flag (file, "Local names forbidden in current context",
540 (parser->local_variables_forbidden_p
541 & LOCAL_VARS_FORBIDDEN));
542 cp_debug_print_flag (file, "'this' forbidden in current context",
543 (parser->local_variables_forbidden_p
544 & THIS_FORBIDDEN));
545 cp_debug_print_flag (file, "In unbraced linkage specification",
546 parser->in_unbraced_linkage_specification_p);
547 cp_debug_print_flag (file, "Parsing a declarator",
548 parser->in_declarator_p);
549 cp_debug_print_flag (file, "In template argument list",
550 parser->in_template_argument_list_p);
551 cp_debug_print_flag (file, "Parsing an iteration statement",
552 parser->in_statement & IN_ITERATION_STMT);
553 cp_debug_print_flag (file, "Parsing a switch statement",
554 parser->in_statement & IN_SWITCH_STMT);
555 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
556 parser->in_statement & IN_OMP_BLOCK);
557 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
558 parser->in_statement & IN_OMP_FOR);
559 cp_debug_print_flag (file, "Parsing an if statement",
560 parser->in_statement & IN_IF_STMT);
561 cp_debug_print_flag (file, "Parsing a type-id in an expression "
562 "context", parser->in_type_id_in_expr_p);
563 cp_debug_print_flag (file, "String expressions should be translated "
564 "to execution character set",
565 parser->translate_strings_p);
566 cp_debug_print_flag (file, "Parsing function body outside of a "
567 "local class", parser->in_function_body);
568 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
569 parser->colon_corrects_to_scope_p);
570 cp_debug_print_flag (file, "Colon doesn't start a class definition",
571 parser->colon_doesnt_start_class_def_p);
572 if (parser->type_definition_forbidden_message)
573 fprintf (file, "Error message for forbidden type definitions: %s\n",
574 parser->type_definition_forbidden_message);
575 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
576 fprintf (file, "Number of class definitions in progress: %u\n",
577 parser->num_classes_being_defined);
578 fprintf (file, "Number of template parameter lists for the current "
579 "declaration: %u\n", parser->num_template_parameter_lists);
580 cp_debug_parser_tokens (file, parser, window_size);
581 token = parser->lexer->next_token;
582 fprintf (file, "Next token to parse:\n");
583 fprintf (file, "\tToken: ");
584 cp_lexer_print_token (file, token);
585 eloc = expand_location (token->location);
586 fprintf (file, "\n\tFile: %s\n", eloc.file);
587 fprintf (file, "\tLine: %d\n", eloc.line);
588 fprintf (file, "\tColumn: %d\n", eloc.column);
589 }
590
591 DEBUG_FUNCTION void
592 debug (cp_parser &ref)
593 {
594 cp_debug_parser (stderr, &ref);
595 }
596
597 DEBUG_FUNCTION void
598 debug (cp_parser *ptr)
599 {
600 if (ptr)
601 debug (*ptr);
602 else
603 fprintf (stderr, "<nil>\n");
604 }
605
606 /* Allocate memory for a new lexer object and return it. */
607
608 static cp_lexer *
609 cp_lexer_alloc (void)
610 {
611 cp_lexer *lexer;
612
613 c_common_no_more_pch ();
614
615 /* Allocate the memory. */
616 lexer = ggc_cleared_alloc<cp_lexer> ();
617
618 /* Initially we are not debugging. */
619 lexer->debugging_p = false;
620
621 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
622
623 /* Create the buffer. */
624 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
625
626 return lexer;
627 }
628
629
630 /* Create a new main C++ lexer, the lexer that gets tokens from the
631 preprocessor. */
632
633 static cp_lexer *
634 cp_lexer_new_main (void)
635 {
636 cp_lexer *lexer;
637 cp_token token;
638
639 /* It's possible that parsing the first pragma will load a PCH file,
640 which is a GC collection point. So we have to do that before
641 allocating any memory. */
642 cp_parser_initial_pragma (&token);
643
644 lexer = cp_lexer_alloc ();
645
646 /* Put the first token in the buffer. */
647 lexer->buffer->quick_push (token);
648
649 /* Get the remaining tokens from the preprocessor. */
650 while (token.type != CPP_EOF)
651 {
652 cp_lexer_get_preprocessor_token (lexer, &token);
653 vec_safe_push (lexer->buffer, token);
654 }
655
656 lexer->last_token = lexer->buffer->address ()
657 + lexer->buffer->length ()
658 - 1;
659 lexer->next_token = lexer->buffer->length ()
660 ? lexer->buffer->address ()
661 : &eof_token;
662
663 /* Subsequent preprocessor diagnostics should use compiler
664 diagnostic functions to get the compiler source location. */
665 done_lexing = true;
666
667 gcc_assert (!lexer->next_token->purged_p);
668 return lexer;
669 }
670
671 /* Create a new lexer whose token stream is primed with the tokens in
672 CACHE. When these tokens are exhausted, no new tokens will be read. */
673
674 static cp_lexer *
675 cp_lexer_new_from_tokens (cp_token_cache *cache)
676 {
677 cp_token *first = cache->first;
678 cp_token *last = cache->last;
679 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
680
681 /* We do not own the buffer. */
682 lexer->buffer = NULL;
683 lexer->next_token = first == last ? &eof_token : first;
684 lexer->last_token = last;
685
686 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
687
688 /* Initially we are not debugging. */
689 lexer->debugging_p = false;
690
691 gcc_assert (!lexer->next_token->purged_p);
692 return lexer;
693 }
694
695 /* Frees all resources associated with LEXER. */
696
697 static void
698 cp_lexer_destroy (cp_lexer *lexer)
699 {
700 vec_free (lexer->buffer);
701 lexer->saved_tokens.release ();
702 ggc_free (lexer);
703 }
704
705 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
706 be used. The point of this flag is to help the compiler to fold away calls
707 to cp_lexer_debugging_p within this source file at compile time, when the
708 lexer is not being debugged. */
709
710 #define LEXER_DEBUGGING_ENABLED_P false
711
712 /* Returns nonzero if debugging information should be output. */
713
714 static inline bool
715 cp_lexer_debugging_p (cp_lexer *lexer)
716 {
717 if (!LEXER_DEBUGGING_ENABLED_P)
718 return false;
719
720 return lexer->debugging_p;
721 }
722
723
724 static inline cp_token_position
725 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
726 {
727 gcc_assert (!previous_p || lexer->next_token != &eof_token);
728
729 return lexer->next_token - previous_p;
730 }
731
732 static inline cp_token *
733 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
734 {
735 return pos;
736 }
737
738 static inline void
739 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
740 {
741 lexer->next_token = cp_lexer_token_at (lexer, pos);
742 }
743
744 static inline cp_token_position
745 cp_lexer_previous_token_position (cp_lexer *lexer)
746 {
747 if (lexer->next_token == &eof_token)
748 return lexer->last_token - 1;
749 else
750 return cp_lexer_token_position (lexer, true);
751 }
752
753 static inline cp_token *
754 cp_lexer_previous_token (cp_lexer *lexer)
755 {
756 cp_token_position tp = cp_lexer_previous_token_position (lexer);
757
758 /* Skip past purged tokens. */
759 while (tp->purged_p)
760 {
761 gcc_assert (tp != vec_safe_address (lexer->buffer));
762 tp--;
763 }
764
765 return cp_lexer_token_at (lexer, tp);
766 }
767
768 /* nonzero if we are presently saving tokens. */
769
770 static inline int
771 cp_lexer_saving_tokens (const cp_lexer* lexer)
772 {
773 return lexer->saved_tokens.length () != 0;
774 }
775
776 /* Store the next token from the preprocessor in *TOKEN. Return true
777 if we reach EOF. If LEXER is NULL, assume we are handling an
778 initial #pragma pch_preprocess, and thus want the lexer to return
779 processed strings. */
780
781 static void
782 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
783 {
784 static int is_extern_c = 0;
785
786 /* Get a new token from the preprocessor. */
787 token->type
788 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
789 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
790 token->keyword = RID_MAX;
791 token->purged_p = false;
792 token->error_reported = false;
793
794 /* On some systems, some header files are surrounded by an
795 implicit extern "C" block. Set a flag in the token if it
796 comes from such a header. */
797 is_extern_c += pending_lang_change;
798 pending_lang_change = 0;
799 token->implicit_extern_c = is_extern_c > 0;
800
801 /* Check to see if this token is a keyword. */
802 if (token->type == CPP_NAME)
803 {
804 if (IDENTIFIER_KEYWORD_P (token->u.value))
805 {
806 /* Mark this token as a keyword. */
807 token->type = CPP_KEYWORD;
808 /* Record which keyword. */
809 token->keyword = C_RID_CODE (token->u.value);
810 }
811 else
812 {
813 if (warn_cxx11_compat
814 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
815 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
816 {
817 /* Warn about the C++0x keyword (but still treat it as
818 an identifier). */
819 warning (OPT_Wc__11_compat,
820 "identifier %qE is a keyword in C++11",
821 token->u.value);
822
823 /* Clear out the C_RID_CODE so we don't warn about this
824 particular identifier-turned-keyword again. */
825 C_SET_RID_CODE (token->u.value, RID_MAX);
826 }
827
828 token->keyword = RID_MAX;
829 }
830 }
831 else if (token->type == CPP_AT_NAME)
832 {
833 /* This only happens in Objective-C++; it must be a keyword. */
834 token->type = CPP_KEYWORD;
835 switch (C_RID_CODE (token->u.value))
836 {
837 /* Replace 'class' with '@class', 'private' with '@private',
838 etc. This prevents confusion with the C++ keyword
839 'class', and makes the tokens consistent with other
840 Objective-C 'AT' keywords. For example '@class' is
841 reported as RID_AT_CLASS which is consistent with
842 '@synchronized', which is reported as
843 RID_AT_SYNCHRONIZED.
844 */
845 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
846 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
847 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
848 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
849 case RID_THROW: token->keyword = RID_AT_THROW; break;
850 case RID_TRY: token->keyword = RID_AT_TRY; break;
851 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
852 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
853 default: token->keyword = C_RID_CODE (token->u.value);
854 }
855 }
856 }
857
858 /* Update the globals input_location and the input file stack from TOKEN. */
859 static inline void
860 cp_lexer_set_source_position_from_token (cp_token *token)
861 {
862 if (token->type != CPP_EOF)
863 {
864 input_location = token->location;
865 }
866 }
867
868 /* Update the globals input_location and the input file stack from LEXER. */
869 static inline void
870 cp_lexer_set_source_position (cp_lexer *lexer)
871 {
872 cp_token *token = cp_lexer_peek_token (lexer);
873 cp_lexer_set_source_position_from_token (token);
874 }
875
876 /* Return a pointer to the next token in the token stream, but do not
877 consume it. */
878
879 static inline cp_token *
880 cp_lexer_peek_token (cp_lexer *lexer)
881 {
882 if (cp_lexer_debugging_p (lexer))
883 {
884 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
885 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
886 putc ('\n', cp_lexer_debug_stream);
887 }
888 return lexer->next_token;
889 }
890
891 /* Return true if the next token has the indicated TYPE. */
892
893 static inline bool
894 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
895 {
896 return cp_lexer_peek_token (lexer)->type == type;
897 }
898
899 /* Return true if the next token does not have the indicated TYPE. */
900
901 static inline bool
902 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
903 {
904 return !cp_lexer_next_token_is (lexer, type);
905 }
906
907 /* Return true if the next token is the indicated KEYWORD. */
908
909 static inline bool
910 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
911 {
912 return cp_lexer_peek_token (lexer)->keyword == keyword;
913 }
914
915 static inline bool
916 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
917 {
918 return cp_lexer_peek_nth_token (lexer, n)->type == type;
919 }
920
921 static inline bool
922 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
923 {
924 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
925 }
926
927 /* Return true if KEYWORD can start a decl-specifier. */
928
929 bool
930 cp_keyword_starts_decl_specifier_p (enum rid keyword)
931 {
932 switch (keyword)
933 {
934 /* auto specifier: storage-class-specifier in C++,
935 simple-type-specifier in C++0x. */
936 case RID_AUTO:
937 /* Storage classes. */
938 case RID_REGISTER:
939 case RID_STATIC:
940 case RID_EXTERN:
941 case RID_MUTABLE:
942 case RID_THREAD:
943 /* Elaborated type specifiers. */
944 case RID_ENUM:
945 case RID_CLASS:
946 case RID_STRUCT:
947 case RID_UNION:
948 case RID_TYPENAME:
949 /* Simple type specifiers. */
950 case RID_CHAR:
951 case RID_CHAR8:
952 case RID_CHAR16:
953 case RID_CHAR32:
954 case RID_WCHAR:
955 case RID_BOOL:
956 case RID_SHORT:
957 case RID_INT:
958 case RID_LONG:
959 case RID_SIGNED:
960 case RID_UNSIGNED:
961 case RID_FLOAT:
962 case RID_DOUBLE:
963 case RID_VOID:
964 /* GNU extensions. */
965 case RID_ATTRIBUTE:
966 case RID_TYPEOF:
967 /* C++0x extensions. */
968 case RID_DECLTYPE:
969 case RID_UNDERLYING_TYPE:
970 case RID_CONSTEXPR:
971 return true;
972
973 default:
974 if (keyword >= RID_FIRST_INT_N
975 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
976 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
977 return true;
978 return false;
979 }
980 }
981
982 /* Return true if the next token is a keyword for a decl-specifier. */
983
984 static bool
985 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
986 {
987 cp_token *token;
988
989 token = cp_lexer_peek_token (lexer);
990 return cp_keyword_starts_decl_specifier_p (token->keyword);
991 }
992
993 /* Returns TRUE iff the token T begins a decltype type. */
994
995 static bool
996 token_is_decltype (cp_token *t)
997 {
998 return (t->keyword == RID_DECLTYPE
999 || t->type == CPP_DECLTYPE);
1000 }
1001
1002 /* Returns TRUE iff the next token begins a decltype type. */
1003
1004 static bool
1005 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1006 {
1007 cp_token *t = cp_lexer_peek_token (lexer);
1008 return token_is_decltype (t);
1009 }
1010
1011 /* Called when processing a token with tree_check_value; perform or defer the
1012 associated checks and return the value. */
1013
1014 static tree
1015 saved_checks_value (struct tree_check *check_value)
1016 {
1017 /* Perform any access checks that were deferred. */
1018 vec<deferred_access_check, va_gc> *checks;
1019 deferred_access_check *chk;
1020 checks = check_value->checks;
1021 if (checks)
1022 {
1023 int i;
1024 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1025 perform_or_defer_access_check (chk->binfo,
1026 chk->decl,
1027 chk->diag_decl, tf_warning_or_error);
1028 }
1029 /* Return the stored value. */
1030 return check_value->value;
1031 }
1032
1033 /* Return a pointer to the Nth token in the token stream. If N is 1,
1034 then this is precisely equivalent to cp_lexer_peek_token (except
1035 that it is not inline). One would like to disallow that case, but
1036 there is one case (cp_parser_nth_token_starts_template_id) where
1037 the caller passes a variable for N and it might be 1. */
1038
1039 static cp_token *
1040 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1041 {
1042 cp_token *token;
1043
1044 /* N is 1-based, not zero-based. */
1045 gcc_assert (n > 0);
1046
1047 if (cp_lexer_debugging_p (lexer))
1048 fprintf (cp_lexer_debug_stream,
1049 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1050
1051 --n;
1052 token = lexer->next_token;
1053 gcc_assert (!n || token != &eof_token);
1054 while (n != 0)
1055 {
1056 ++token;
1057 if (token == lexer->last_token)
1058 {
1059 token = &eof_token;
1060 break;
1061 }
1062
1063 if (!token->purged_p)
1064 --n;
1065 }
1066
1067 if (cp_lexer_debugging_p (lexer))
1068 {
1069 cp_lexer_print_token (cp_lexer_debug_stream, token);
1070 putc ('\n', cp_lexer_debug_stream);
1071 }
1072
1073 return token;
1074 }
1075
1076 /* Return the next token, and advance the lexer's next_token pointer
1077 to point to the next non-purged token. */
1078
1079 static cp_token *
1080 cp_lexer_consume_token (cp_lexer* lexer)
1081 {
1082 cp_token *token = lexer->next_token;
1083
1084 gcc_assert (token != &eof_token);
1085 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1086
1087 do
1088 {
1089 lexer->next_token++;
1090 if (lexer->next_token == lexer->last_token)
1091 {
1092 lexer->next_token = &eof_token;
1093 break;
1094 }
1095
1096 }
1097 while (lexer->next_token->purged_p);
1098
1099 cp_lexer_set_source_position_from_token (token);
1100
1101 /* Provide debugging output. */
1102 if (cp_lexer_debugging_p (lexer))
1103 {
1104 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1105 cp_lexer_print_token (cp_lexer_debug_stream, token);
1106 putc ('\n', cp_lexer_debug_stream);
1107 }
1108
1109 return token;
1110 }
1111
1112 /* Permanently remove the next token from the token stream, and
1113 advance the next_token pointer to refer to the next non-purged
1114 token. */
1115
1116 static void
1117 cp_lexer_purge_token (cp_lexer *lexer)
1118 {
1119 cp_token *tok = lexer->next_token;
1120
1121 gcc_assert (tok != &eof_token);
1122 tok->purged_p = true;
1123 tok->location = UNKNOWN_LOCATION;
1124 tok->u.value = NULL_TREE;
1125 tok->keyword = RID_MAX;
1126
1127 do
1128 {
1129 tok++;
1130 if (tok == lexer->last_token)
1131 {
1132 tok = &eof_token;
1133 break;
1134 }
1135 }
1136 while (tok->purged_p);
1137 lexer->next_token = tok;
1138 }
1139
1140 /* Permanently remove all tokens after TOK, up to, but not
1141 including, the token that will be returned next by
1142 cp_lexer_peek_token. */
1143
1144 static void
1145 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1146 {
1147 cp_token *peek = lexer->next_token;
1148
1149 if (peek == &eof_token)
1150 peek = lexer->last_token;
1151
1152 gcc_assert (tok < peek);
1153
1154 for ( tok += 1; tok != peek; tok += 1)
1155 {
1156 tok->purged_p = true;
1157 tok->location = UNKNOWN_LOCATION;
1158 tok->u.value = NULL_TREE;
1159 tok->keyword = RID_MAX;
1160 }
1161 }
1162
1163 /* Begin saving tokens. All tokens consumed after this point will be
1164 preserved. */
1165
1166 static void
1167 cp_lexer_save_tokens (cp_lexer* lexer)
1168 {
1169 /* Provide debugging output. */
1170 if (cp_lexer_debugging_p (lexer))
1171 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1172
1173 lexer->saved_tokens.safe_push (lexer->next_token);
1174 }
1175
1176 /* Commit to the portion of the token stream most recently saved. */
1177
1178 static void
1179 cp_lexer_commit_tokens (cp_lexer* lexer)
1180 {
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer))
1183 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1184
1185 lexer->saved_tokens.pop ();
1186 }
1187
1188 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1189 to the token stream. Stop saving tokens. */
1190
1191 static void
1192 cp_lexer_rollback_tokens (cp_lexer* lexer)
1193 {
1194 /* Provide debugging output. */
1195 if (cp_lexer_debugging_p (lexer))
1196 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1197
1198 lexer->next_token = lexer->saved_tokens.pop ();
1199 }
1200
1201 /* RAII wrapper around the above functions, with sanity checking. Creating
1202 a variable saves tokens, which are committed when the variable is
1203 destroyed unless they are explicitly rolled back by calling the rollback
1204 member function. */
1205
1206 struct saved_token_sentinel
1207 {
1208 cp_lexer *lexer;
1209 unsigned len;
1210 bool commit;
1211 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1212 {
1213 len = lexer->saved_tokens.length ();
1214 cp_lexer_save_tokens (lexer);
1215 }
1216 void rollback ()
1217 {
1218 cp_lexer_rollback_tokens (lexer);
1219 commit = false;
1220 }
1221 ~saved_token_sentinel()
1222 {
1223 if (commit)
1224 cp_lexer_commit_tokens (lexer);
1225 gcc_assert (lexer->saved_tokens.length () == len);
1226 }
1227 };
1228
1229 /* Print a representation of the TOKEN on the STREAM. */
1230
1231 static void
1232 cp_lexer_print_token (FILE * stream, cp_token *token)
1233 {
1234 /* We don't use cpp_type2name here because the parser defines
1235 a few tokens of its own. */
1236 static const char *const token_names[] = {
1237 /* cpplib-defined token types */
1238 #define OP(e, s) #e,
1239 #define TK(e, s) #e,
1240 TTYPE_TABLE
1241 #undef OP
1242 #undef TK
1243 /* C++ parser token types - see "Manifest constants", above. */
1244 "KEYWORD",
1245 "TEMPLATE_ID",
1246 "NESTED_NAME_SPECIFIER",
1247 };
1248
1249 /* For some tokens, print the associated data. */
1250 switch (token->type)
1251 {
1252 case CPP_KEYWORD:
1253 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1254 For example, `struct' is mapped to an INTEGER_CST. */
1255 if (!identifier_p (token->u.value))
1256 break;
1257 /* fall through */
1258 case CPP_NAME:
1259 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1260 break;
1261
1262 case CPP_STRING:
1263 case CPP_STRING16:
1264 case CPP_STRING32:
1265 case CPP_WSTRING:
1266 case CPP_UTF8STRING:
1267 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1268 break;
1269
1270 case CPP_NUMBER:
1271 print_generic_expr (stream, token->u.value);
1272 break;
1273
1274 default:
1275 /* If we have a name for the token, print it out. Otherwise, we
1276 simply give the numeric code. */
1277 if (token->type < ARRAY_SIZE(token_names))
1278 fputs (token_names[token->type], stream);
1279 else
1280 fprintf (stream, "[%d]", token->type);
1281 break;
1282 }
1283 }
1284
1285 DEBUG_FUNCTION void
1286 debug (cp_token &ref)
1287 {
1288 cp_lexer_print_token (stderr, &ref);
1289 fprintf (stderr, "\n");
1290 }
1291
1292 DEBUG_FUNCTION void
1293 debug (cp_token *ptr)
1294 {
1295 if (ptr)
1296 debug (*ptr);
1297 else
1298 fprintf (stderr, "<nil>\n");
1299 }
1300
1301
1302 /* Start emitting debugging information. */
1303
1304 static void
1305 cp_lexer_start_debugging (cp_lexer* lexer)
1306 {
1307 if (!LEXER_DEBUGGING_ENABLED_P)
1308 fatal_error (input_location,
1309 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1310
1311 lexer->debugging_p = true;
1312 cp_lexer_debug_stream = stderr;
1313 }
1314
1315 /* Stop emitting debugging information. */
1316
1317 static void
1318 cp_lexer_stop_debugging (cp_lexer* lexer)
1319 {
1320 if (!LEXER_DEBUGGING_ENABLED_P)
1321 fatal_error (input_location,
1322 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1323
1324 lexer->debugging_p = false;
1325 cp_lexer_debug_stream = NULL;
1326 }
1327
1328 /* Create a new cp_token_cache, representing a range of tokens. */
1329
1330 static cp_token_cache *
1331 cp_token_cache_new (cp_token *first, cp_token *last)
1332 {
1333 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1334 cache->first = first;
1335 cache->last = last;
1336 return cache;
1337 }
1338
1339 /* Diagnose if #pragma omp declare simd isn't followed immediately
1340 by function declaration or definition. */
1341
1342 static inline void
1343 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1344 {
1345 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1346 {
1347 error ("%<#pragma omp declare simd%> not immediately followed by "
1348 "function declaration or definition");
1349 parser->omp_declare_simd = NULL;
1350 }
1351 }
1352
1353 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1354 and put that into "omp declare simd" attribute. */
1355
1356 static inline void
1357 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1358 {
1359 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1360 {
1361 if (fndecl == error_mark_node)
1362 {
1363 parser->omp_declare_simd = NULL;
1364 return;
1365 }
1366 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1367 {
1368 cp_ensure_no_omp_declare_simd (parser);
1369 return;
1370 }
1371 }
1372 }
1373
1374 /* Diagnose if #pragma acc routine isn't followed immediately by function
1375 declaration or definition. */
1376
1377 static inline void
1378 cp_ensure_no_oacc_routine (cp_parser *parser)
1379 {
1380 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1381 {
1382 error_at (parser->oacc_routine->loc,
1383 "%<#pragma acc routine%> not immediately followed by "
1384 "function declaration or definition");
1385 parser->oacc_routine = NULL;
1386 }
1387 }
1388 \f
1389 /* Decl-specifiers. */
1390
1391 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1392
1393 static void
1394 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1395 {
1396 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1397 }
1398
1399 /* Declarators. */
1400
1401 /* Nothing other than the parser should be creating declarators;
1402 declarators are a semi-syntactic representation of C++ entities.
1403 Other parts of the front end that need to create entities (like
1404 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1405
1406 static cp_declarator *make_call_declarator
1407 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1408 static cp_declarator *make_array_declarator
1409 (cp_declarator *, tree);
1410 static cp_declarator *make_pointer_declarator
1411 (cp_cv_quals, cp_declarator *, tree);
1412 static cp_declarator *make_reference_declarator
1413 (cp_cv_quals, cp_declarator *, bool, tree);
1414 static cp_declarator *make_ptrmem_declarator
1415 (cp_cv_quals, tree, cp_declarator *, tree);
1416
1417 /* An erroneous declarator. */
1418 static cp_declarator *cp_error_declarator;
1419
1420 /* The obstack on which declarators and related data structures are
1421 allocated. */
1422 static struct obstack declarator_obstack;
1423
1424 /* Alloc BYTES from the declarator memory pool. */
1425
1426 static inline void *
1427 alloc_declarator (size_t bytes)
1428 {
1429 return obstack_alloc (&declarator_obstack, bytes);
1430 }
1431
1432 /* Allocate a declarator of the indicated KIND. Clear fields that are
1433 common to all declarators. */
1434
1435 static cp_declarator *
1436 make_declarator (cp_declarator_kind kind)
1437 {
1438 cp_declarator *declarator;
1439
1440 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1441 declarator->kind = kind;
1442 declarator->parenthesized = UNKNOWN_LOCATION;
1443 declarator->attributes = NULL_TREE;
1444 declarator->std_attributes = NULL_TREE;
1445 declarator->declarator = NULL;
1446 declarator->parameter_pack_p = false;
1447 declarator->id_loc = UNKNOWN_LOCATION;
1448
1449 return declarator;
1450 }
1451
1452 /* Make a declarator for a generalized identifier. If
1453 QUALIFYING_SCOPE is non-NULL, the identifier is
1454 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1455 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1456 is, if any. */
1457
1458 static cp_declarator *
1459 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1460 special_function_kind sfk, location_t id_location)
1461 {
1462 cp_declarator *declarator;
1463
1464 /* It is valid to write:
1465
1466 class C { void f(); };
1467 typedef C D;
1468 void D::f();
1469
1470 The standard is not clear about whether `typedef const C D' is
1471 legal; as of 2002-09-15 the committee is considering that
1472 question. EDG 3.0 allows that syntax. Therefore, we do as
1473 well. */
1474 if (qualifying_scope && TYPE_P (qualifying_scope))
1475 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1476
1477 gcc_assert (identifier_p (unqualified_name)
1478 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1479 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1480
1481 declarator = make_declarator (cdk_id);
1482 declarator->u.id.qualifying_scope = qualifying_scope;
1483 declarator->u.id.unqualified_name = unqualified_name;
1484 declarator->u.id.sfk = sfk;
1485 declarator->id_loc = id_location;
1486
1487 return declarator;
1488 }
1489
1490 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1491 of modifiers such as const or volatile to apply to the pointer
1492 type, represented as identifiers. ATTRIBUTES represent the attributes that
1493 appertain to the pointer or reference. */
1494
1495 cp_declarator *
1496 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1497 tree attributes)
1498 {
1499 cp_declarator *declarator;
1500
1501 declarator = make_declarator (cdk_pointer);
1502 declarator->declarator = target;
1503 declarator->u.pointer.qualifiers = cv_qualifiers;
1504 declarator->u.pointer.class_type = NULL_TREE;
1505 if (target)
1506 {
1507 declarator->id_loc = target->id_loc;
1508 declarator->parameter_pack_p = target->parameter_pack_p;
1509 target->parameter_pack_p = false;
1510 }
1511 else
1512 declarator->parameter_pack_p = false;
1513
1514 declarator->std_attributes = attributes;
1515
1516 return declarator;
1517 }
1518
1519 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1520 represent the attributes that appertain to the pointer or
1521 reference. */
1522
1523 cp_declarator *
1524 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1525 bool rvalue_ref, tree attributes)
1526 {
1527 cp_declarator *declarator;
1528
1529 declarator = make_declarator (cdk_reference);
1530 declarator->declarator = target;
1531 declarator->u.reference.qualifiers = cv_qualifiers;
1532 declarator->u.reference.rvalue_ref = rvalue_ref;
1533 if (target)
1534 {
1535 declarator->id_loc = target->id_loc;
1536 declarator->parameter_pack_p = target->parameter_pack_p;
1537 target->parameter_pack_p = false;
1538 }
1539 else
1540 declarator->parameter_pack_p = false;
1541
1542 declarator->std_attributes = attributes;
1543
1544 return declarator;
1545 }
1546
1547 /* Like make_pointer_declarator -- but for a pointer to a non-static
1548 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1549 appertain to the pointer or reference. */
1550
1551 cp_declarator *
1552 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1553 cp_declarator *pointee,
1554 tree attributes)
1555 {
1556 cp_declarator *declarator;
1557
1558 declarator = make_declarator (cdk_ptrmem);
1559 declarator->declarator = pointee;
1560 declarator->u.pointer.qualifiers = cv_qualifiers;
1561 declarator->u.pointer.class_type = class_type;
1562
1563 if (pointee)
1564 {
1565 declarator->parameter_pack_p = pointee->parameter_pack_p;
1566 pointee->parameter_pack_p = false;
1567 }
1568 else
1569 declarator->parameter_pack_p = false;
1570
1571 declarator->std_attributes = attributes;
1572
1573 return declarator;
1574 }
1575
1576 /* Make a declarator for the function given by TARGET, with the
1577 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1578 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1579 indicates what exceptions can be thrown. */
1580
1581 cp_declarator *
1582 make_call_declarator (cp_declarator *target,
1583 tree parms,
1584 cp_cv_quals cv_qualifiers,
1585 cp_virt_specifiers virt_specifiers,
1586 cp_ref_qualifier ref_qualifier,
1587 tree tx_qualifier,
1588 tree exception_specification,
1589 tree late_return_type,
1590 tree requires_clause)
1591 {
1592 cp_declarator *declarator;
1593
1594 declarator = make_declarator (cdk_function);
1595 declarator->declarator = target;
1596 declarator->u.function.parameters = parms;
1597 declarator->u.function.qualifiers = cv_qualifiers;
1598 declarator->u.function.virt_specifiers = virt_specifiers;
1599 declarator->u.function.ref_qualifier = ref_qualifier;
1600 declarator->u.function.tx_qualifier = tx_qualifier;
1601 declarator->u.function.exception_specification = exception_specification;
1602 declarator->u.function.late_return_type = late_return_type;
1603 declarator->u.function.requires_clause = requires_clause;
1604 if (target)
1605 {
1606 declarator->id_loc = target->id_loc;
1607 declarator->parameter_pack_p = target->parameter_pack_p;
1608 target->parameter_pack_p = false;
1609 }
1610 else
1611 declarator->parameter_pack_p = false;
1612
1613 return declarator;
1614 }
1615
1616 /* Make a declarator for an array of BOUNDS elements, each of which is
1617 defined by ELEMENT. */
1618
1619 cp_declarator *
1620 make_array_declarator (cp_declarator *element, tree bounds)
1621 {
1622 cp_declarator *declarator;
1623
1624 declarator = make_declarator (cdk_array);
1625 declarator->declarator = element;
1626 declarator->u.array.bounds = bounds;
1627 if (element)
1628 {
1629 declarator->id_loc = element->id_loc;
1630 declarator->parameter_pack_p = element->parameter_pack_p;
1631 element->parameter_pack_p = false;
1632 }
1633 else
1634 declarator->parameter_pack_p = false;
1635
1636 return declarator;
1637 }
1638
1639 /* Determine whether the declarator we've seen so far can be a
1640 parameter pack, when followed by an ellipsis. */
1641 static bool
1642 declarator_can_be_parameter_pack (cp_declarator *declarator)
1643 {
1644 if (declarator && declarator->parameter_pack_p)
1645 /* We already saw an ellipsis. */
1646 return false;
1647
1648 /* Search for a declarator name, or any other declarator that goes
1649 after the point where the ellipsis could appear in a parameter
1650 pack. If we find any of these, then this declarator cannot be
1651 made into a parameter pack. */
1652 bool found = false;
1653 while (declarator && !found)
1654 {
1655 switch ((int)declarator->kind)
1656 {
1657 case cdk_id:
1658 case cdk_array:
1659 case cdk_decomp:
1660 found = true;
1661 break;
1662
1663 case cdk_error:
1664 return true;
1665
1666 default:
1667 declarator = declarator->declarator;
1668 break;
1669 }
1670 }
1671
1672 return !found;
1673 }
1674
1675 cp_parameter_declarator *no_parameters;
1676
1677 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1678 DECLARATOR and DEFAULT_ARGUMENT. */
1679
1680 cp_parameter_declarator *
1681 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1682 cp_declarator *declarator,
1683 tree default_argument,
1684 location_t loc,
1685 bool template_parameter_pack_p = false)
1686 {
1687 cp_parameter_declarator *parameter;
1688
1689 parameter = ((cp_parameter_declarator *)
1690 alloc_declarator (sizeof (cp_parameter_declarator)));
1691 parameter->next = NULL;
1692 if (decl_specifiers)
1693 parameter->decl_specifiers = *decl_specifiers;
1694 else
1695 clear_decl_specs (&parameter->decl_specifiers);
1696 parameter->declarator = declarator;
1697 parameter->default_argument = default_argument;
1698 parameter->template_parameter_pack_p = template_parameter_pack_p;
1699 parameter->loc = loc;
1700
1701 return parameter;
1702 }
1703
1704 /* Returns true iff DECLARATOR is a declaration for a function. */
1705
1706 static bool
1707 function_declarator_p (const cp_declarator *declarator)
1708 {
1709 while (declarator)
1710 {
1711 if (declarator->kind == cdk_function
1712 && declarator->declarator->kind == cdk_id)
1713 return true;
1714 if (declarator->kind == cdk_id
1715 || declarator->kind == cdk_decomp
1716 || declarator->kind == cdk_error)
1717 return false;
1718 declarator = declarator->declarator;
1719 }
1720 return false;
1721 }
1722
1723 /* The parser. */
1724
1725 /* Overview
1726 --------
1727
1728 A cp_parser parses the token stream as specified by the C++
1729 grammar. Its job is purely parsing, not semantic analysis. For
1730 example, the parser breaks the token stream into declarators,
1731 expressions, statements, and other similar syntactic constructs.
1732 It does not check that the types of the expressions on either side
1733 of an assignment-statement are compatible, or that a function is
1734 not declared with a parameter of type `void'.
1735
1736 The parser invokes routines elsewhere in the compiler to perform
1737 semantic analysis and to build up the abstract syntax tree for the
1738 code processed.
1739
1740 The parser (and the template instantiation code, which is, in a
1741 way, a close relative of parsing) are the only parts of the
1742 compiler that should be calling push_scope and pop_scope, or
1743 related functions. The parser (and template instantiation code)
1744 keeps track of what scope is presently active; everything else
1745 should simply honor that. (The code that generates static
1746 initializers may also need to set the scope, in order to check
1747 access control correctly when emitting the initializers.)
1748
1749 Methodology
1750 -----------
1751
1752 The parser is of the standard recursive-descent variety. Upcoming
1753 tokens in the token stream are examined in order to determine which
1754 production to use when parsing a non-terminal. Some C++ constructs
1755 require arbitrary look ahead to disambiguate. For example, it is
1756 impossible, in the general case, to tell whether a statement is an
1757 expression or declaration without scanning the entire statement.
1758 Therefore, the parser is capable of "parsing tentatively." When the
1759 parser is not sure what construct comes next, it enters this mode.
1760 Then, while we attempt to parse the construct, the parser queues up
1761 error messages, rather than issuing them immediately, and saves the
1762 tokens it consumes. If the construct is parsed successfully, the
1763 parser "commits", i.e., it issues any queued error messages and
1764 the tokens that were being preserved are permanently discarded.
1765 If, however, the construct is not parsed successfully, the parser
1766 rolls back its state completely so that it can resume parsing using
1767 a different alternative.
1768
1769 Future Improvements
1770 -------------------
1771
1772 The performance of the parser could probably be improved substantially.
1773 We could often eliminate the need to parse tentatively by looking ahead
1774 a little bit. In some places, this approach might not entirely eliminate
1775 the need to parse tentatively, but it might still speed up the average
1776 case. */
1777
1778 /* Flags that are passed to some parsing functions. These values can
1779 be bitwise-ored together. */
1780
1781 enum
1782 {
1783 /* No flags. */
1784 CP_PARSER_FLAGS_NONE = 0x0,
1785 /* The construct is optional. If it is not present, then no error
1786 should be issued. */
1787 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1788 /* When parsing a type-specifier, treat user-defined type-names
1789 as non-type identifiers. */
1790 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1791 /* When parsing a type-specifier, do not try to parse a class-specifier
1792 or enum-specifier. */
1793 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1794 /* When parsing a decl-specifier-seq, only allow type-specifier or
1795 constexpr. */
1796 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1797 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1798 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1799 /* When parsing a decl-specifier-seq, allow missing typename. */
1800 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20
1801 };
1802
1803 /* This type is used for parameters and variables which hold
1804 combinations of the above flags. */
1805 typedef int cp_parser_flags;
1806
1807 /* The different kinds of declarators we want to parse. */
1808
1809 enum cp_parser_declarator_kind
1810 {
1811 /* We want an abstract declarator. */
1812 CP_PARSER_DECLARATOR_ABSTRACT,
1813 /* We want a named declarator. */
1814 CP_PARSER_DECLARATOR_NAMED,
1815 /* We don't mind, but the name must be an unqualified-id. */
1816 CP_PARSER_DECLARATOR_EITHER
1817 };
1818
1819 /* The precedence values used to parse binary expressions. The minimum value
1820 of PREC must be 1, because zero is reserved to quickly discriminate
1821 binary operators from other tokens. */
1822
1823 enum cp_parser_prec
1824 {
1825 PREC_NOT_OPERATOR,
1826 PREC_LOGICAL_OR_EXPRESSION,
1827 PREC_LOGICAL_AND_EXPRESSION,
1828 PREC_INCLUSIVE_OR_EXPRESSION,
1829 PREC_EXCLUSIVE_OR_EXPRESSION,
1830 PREC_AND_EXPRESSION,
1831 PREC_EQUALITY_EXPRESSION,
1832 PREC_RELATIONAL_EXPRESSION,
1833 PREC_SHIFT_EXPRESSION,
1834 PREC_ADDITIVE_EXPRESSION,
1835 PREC_MULTIPLICATIVE_EXPRESSION,
1836 PREC_PM_EXPRESSION,
1837 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1838 };
1839
1840 /* A mapping from a token type to a corresponding tree node type, with a
1841 precedence value. */
1842
1843 struct cp_parser_binary_operations_map_node
1844 {
1845 /* The token type. */
1846 enum cpp_ttype token_type;
1847 /* The corresponding tree code. */
1848 enum tree_code tree_type;
1849 /* The precedence of this operator. */
1850 enum cp_parser_prec prec;
1851 };
1852
1853 struct cp_parser_expression_stack_entry
1854 {
1855 /* Left hand side of the binary operation we are currently
1856 parsing. */
1857 cp_expr lhs;
1858 /* Original tree code for left hand side, if it was a binary
1859 expression itself (used for -Wparentheses). */
1860 enum tree_code lhs_type;
1861 /* Tree code for the binary operation we are parsing. */
1862 enum tree_code tree_type;
1863 /* Precedence of the binary operation we are parsing. */
1864 enum cp_parser_prec prec;
1865 /* Location of the binary operation we are parsing. */
1866 location_t loc;
1867 };
1868
1869 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1870 entries because precedence levels on the stack are monotonically
1871 increasing. */
1872 typedef struct cp_parser_expression_stack_entry
1873 cp_parser_expression_stack[NUM_PREC_VALUES];
1874
1875 /* Prototypes. */
1876
1877 /* Constructors and destructors. */
1878
1879 static cp_parser_context *cp_parser_context_new
1880 (cp_parser_context *);
1881
1882 /* Class variables. */
1883
1884 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1885
1886 /* The operator-precedence table used by cp_parser_binary_expression.
1887 Transformed into an associative array (binops_by_token) by
1888 cp_parser_new. */
1889
1890 static const cp_parser_binary_operations_map_node binops[] = {
1891 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1892 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1893
1894 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1895 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1896 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897
1898 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1899 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1900
1901 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1902 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1903
1904 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1905 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1906 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1908
1909 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1910 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1911
1912 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1913
1914 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1915
1916 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1917
1918 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1919
1920 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1921 };
1922
1923 /* The same as binops, but initialized by cp_parser_new so that
1924 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1925 for speed. */
1926 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1927
1928 /* Constructors and destructors. */
1929
1930 /* Construct a new context. The context below this one on the stack
1931 is given by NEXT. */
1932
1933 static cp_parser_context *
1934 cp_parser_context_new (cp_parser_context* next)
1935 {
1936 cp_parser_context *context;
1937
1938 /* Allocate the storage. */
1939 if (cp_parser_context_free_list != NULL)
1940 {
1941 /* Pull the first entry from the free list. */
1942 context = cp_parser_context_free_list;
1943 cp_parser_context_free_list = context->next;
1944 memset (context, 0, sizeof (*context));
1945 }
1946 else
1947 context = ggc_cleared_alloc<cp_parser_context> ();
1948
1949 /* No errors have occurred yet in this context. */
1950 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1951 /* If this is not the bottommost context, copy information that we
1952 need from the previous context. */
1953 if (next)
1954 {
1955 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1956 expression, then we are parsing one in this context, too. */
1957 context->object_type = next->object_type;
1958 /* Thread the stack. */
1959 context->next = next;
1960 }
1961
1962 return context;
1963 }
1964
1965 /* Managing the unparsed function queues. */
1966
1967 #define unparsed_funs_with_default_args \
1968 parser->unparsed_queues->last ().funs_with_default_args
1969 #define unparsed_funs_with_definitions \
1970 parser->unparsed_queues->last ().funs_with_definitions
1971 #define unparsed_nsdmis \
1972 parser->unparsed_queues->last ().nsdmis
1973 #define unparsed_classes \
1974 parser->unparsed_queues->last ().classes
1975
1976 static void
1977 push_unparsed_function_queues (cp_parser *parser)
1978 {
1979 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1980 vec_safe_push (parser->unparsed_queues, e);
1981 }
1982
1983 static void
1984 pop_unparsed_function_queues (cp_parser *parser)
1985 {
1986 release_tree_vector (unparsed_funs_with_definitions);
1987 parser->unparsed_queues->pop ();
1988 }
1989
1990 /* Prototypes. */
1991
1992 /* Constructors and destructors. */
1993
1994 static cp_parser *cp_parser_new
1995 (void);
1996
1997 /* Routines to parse various constructs.
1998
1999 Those that return `tree' will return the error_mark_node (rather
2000 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2001 Sometimes, they will return an ordinary node if error-recovery was
2002 attempted, even though a parse error occurred. So, to check
2003 whether or not a parse error occurred, you should always use
2004 cp_parser_error_occurred. If the construct is optional (indicated
2005 either by an `_opt' in the name of the function that does the
2006 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2007 the construct is not present. */
2008
2009 /* Lexical conventions [gram.lex] */
2010
2011 static cp_expr cp_parser_identifier
2012 (cp_parser *);
2013 static cp_expr cp_parser_string_literal
2014 (cp_parser *, bool, bool, bool);
2015 static cp_expr cp_parser_userdef_char_literal
2016 (cp_parser *);
2017 static tree cp_parser_userdef_string_literal
2018 (tree);
2019 static cp_expr cp_parser_userdef_numeric_literal
2020 (cp_parser *);
2021
2022 /* Basic concepts [gram.basic] */
2023
2024 static void cp_parser_translation_unit (cp_parser *);
2025
2026 /* Expressions [gram.expr] */
2027
2028 static cp_expr cp_parser_primary_expression
2029 (cp_parser *, bool, bool, bool, cp_id_kind *);
2030 static cp_expr cp_parser_id_expression
2031 (cp_parser *, bool, bool, bool *, bool, bool);
2032 static cp_expr cp_parser_unqualified_id
2033 (cp_parser *, bool, bool, bool, bool);
2034 static tree cp_parser_nested_name_specifier_opt
2035 (cp_parser *, bool, bool, bool, bool, bool = false);
2036 static tree cp_parser_nested_name_specifier
2037 (cp_parser *, bool, bool, bool, bool);
2038 static tree cp_parser_qualifying_entity
2039 (cp_parser *, bool, bool, bool, bool, bool);
2040 static cp_expr cp_parser_postfix_expression
2041 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2042 static tree cp_parser_postfix_open_square_expression
2043 (cp_parser *, tree, bool, bool);
2044 static tree cp_parser_postfix_dot_deref_expression
2045 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2046 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2047 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2048 bool = false);
2049 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2050 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2051 static void cp_parser_pseudo_destructor_name
2052 (cp_parser *, tree, tree *, tree *);
2053 static cp_expr cp_parser_unary_expression
2054 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2055 static enum tree_code cp_parser_unary_operator
2056 (cp_token *);
2057 static tree cp_parser_has_attribute_expression
2058 (cp_parser *);
2059 static tree cp_parser_new_expression
2060 (cp_parser *);
2061 static vec<tree, va_gc> *cp_parser_new_placement
2062 (cp_parser *);
2063 static tree cp_parser_new_type_id
2064 (cp_parser *, tree *);
2065 static cp_declarator *cp_parser_new_declarator_opt
2066 (cp_parser *);
2067 static cp_declarator *cp_parser_direct_new_declarator
2068 (cp_parser *);
2069 static vec<tree, va_gc> *cp_parser_new_initializer
2070 (cp_parser *);
2071 static tree cp_parser_delete_expression
2072 (cp_parser *);
2073 static cp_expr cp_parser_cast_expression
2074 (cp_parser *, bool, bool, bool, cp_id_kind *);
2075 static cp_expr cp_parser_binary_expression
2076 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2077 static tree cp_parser_question_colon_clause
2078 (cp_parser *, cp_expr);
2079 static cp_expr cp_parser_assignment_expression
2080 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2081 static enum tree_code cp_parser_assignment_operator_opt
2082 (cp_parser *);
2083 static cp_expr cp_parser_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static cp_expr cp_parser_constant_expression
2086 (cp_parser *, bool = false, bool * = NULL, bool = false);
2087 static cp_expr cp_parser_builtin_offsetof
2088 (cp_parser *);
2089 static cp_expr cp_parser_lambda_expression
2090 (cp_parser *);
2091 static void cp_parser_lambda_introducer
2092 (cp_parser *, tree);
2093 static bool cp_parser_lambda_declarator_opt
2094 (cp_parser *, tree);
2095 static void cp_parser_lambda_body
2096 (cp_parser *, tree);
2097
2098 /* Statements [gram.stmt.stmt] */
2099
2100 static void cp_parser_statement
2101 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2102 static void cp_parser_label_for_labeled_statement
2103 (cp_parser *, tree);
2104 static tree cp_parser_expression_statement
2105 (cp_parser *, tree);
2106 static tree cp_parser_compound_statement
2107 (cp_parser *, tree, int, bool);
2108 static void cp_parser_statement_seq_opt
2109 (cp_parser *, tree);
2110 static tree cp_parser_selection_statement
2111 (cp_parser *, bool *, vec<tree> *);
2112 static tree cp_parser_condition
2113 (cp_parser *);
2114 static tree cp_parser_iteration_statement
2115 (cp_parser *, bool *, bool, unsigned short);
2116 static bool cp_parser_init_statement
2117 (cp_parser *, tree *decl);
2118 static tree cp_parser_for
2119 (cp_parser *, bool, unsigned short);
2120 static tree cp_parser_c_for
2121 (cp_parser *, tree, tree, bool, unsigned short);
2122 static tree cp_parser_range_for
2123 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2124 static void do_range_for_auto_deduction
2125 (tree, tree);
2126 static tree cp_parser_perform_range_for_lookup
2127 (tree, tree *, tree *);
2128 static tree cp_parser_range_for_member_function
2129 (tree, tree);
2130 static tree cp_parser_jump_statement
2131 (cp_parser *);
2132 static void cp_parser_declaration_statement
2133 (cp_parser *);
2134
2135 static tree cp_parser_implicitly_scoped_statement
2136 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2137 static void cp_parser_already_scoped_statement
2138 (cp_parser *, bool *, const token_indent_info &);
2139
2140 /* Declarations [gram.dcl.dcl] */
2141
2142 static void cp_parser_declaration_seq_opt
2143 (cp_parser *);
2144 static void cp_parser_declaration
2145 (cp_parser *);
2146 static void cp_parser_toplevel_declaration
2147 (cp_parser *);
2148 static void cp_parser_block_declaration
2149 (cp_parser *, bool);
2150 static void cp_parser_simple_declaration
2151 (cp_parser *, bool, tree *);
2152 static void cp_parser_decl_specifier_seq
2153 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2154 static tree cp_parser_storage_class_specifier_opt
2155 (cp_parser *);
2156 static tree cp_parser_function_specifier_opt
2157 (cp_parser *, cp_decl_specifier_seq *);
2158 static tree cp_parser_type_specifier
2159 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2160 int *, bool *);
2161 static tree cp_parser_simple_type_specifier
2162 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2163 static tree cp_parser_type_name
2164 (cp_parser *, bool);
2165 static tree cp_parser_nonclass_name
2166 (cp_parser* parser);
2167 static tree cp_parser_elaborated_type_specifier
2168 (cp_parser *, bool, bool);
2169 static tree cp_parser_enum_specifier
2170 (cp_parser *);
2171 static void cp_parser_enumerator_list
2172 (cp_parser *, tree);
2173 static void cp_parser_enumerator_definition
2174 (cp_parser *, tree);
2175 static tree cp_parser_namespace_name
2176 (cp_parser *);
2177 static void cp_parser_namespace_definition
2178 (cp_parser *);
2179 static void cp_parser_namespace_body
2180 (cp_parser *);
2181 static tree cp_parser_qualified_namespace_specifier
2182 (cp_parser *);
2183 static void cp_parser_namespace_alias_definition
2184 (cp_parser *);
2185 static bool cp_parser_using_declaration
2186 (cp_parser *, bool);
2187 static void cp_parser_using_directive
2188 (cp_parser *);
2189 static tree cp_parser_alias_declaration
2190 (cp_parser *);
2191 static void cp_parser_asm_definition
2192 (cp_parser *);
2193 static void cp_parser_linkage_specification
2194 (cp_parser *);
2195 static void cp_parser_static_assert
2196 (cp_parser *, bool);
2197 static tree cp_parser_decltype
2198 (cp_parser *);
2199 static tree cp_parser_decomposition_declaration
2200 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2201
2202 /* Declarators [gram.dcl.decl] */
2203
2204 static tree cp_parser_init_declarator
2205 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2206 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2207 location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2210 bool, bool, bool);
2211 static cp_declarator *cp_parser_direct_declarator
2212 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2213 bool);
2214 static enum tree_code cp_parser_ptr_operator
2215 (cp_parser *, tree *, cp_cv_quals *, tree *);
2216 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2217 (cp_parser *);
2218 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2219 (cp_parser *);
2220 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2221 (cp_parser *);
2222 static tree cp_parser_tx_qualifier_opt
2223 (cp_parser *);
2224 static tree cp_parser_late_return_type_opt
2225 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2226 static tree cp_parser_declarator_id
2227 (cp_parser *, bool);
2228 static tree cp_parser_type_id
2229 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2230 static tree cp_parser_template_type_arg
2231 (cp_parser *);
2232 static tree cp_parser_trailing_type_id (cp_parser *);
2233 static tree cp_parser_type_id_1
2234 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2235 static void cp_parser_type_specifier_seq
2236 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2237 static tree cp_parser_parameter_declaration_clause
2238 (cp_parser *, cp_parser_flags);
2239 static tree cp_parser_parameter_declaration_list
2240 (cp_parser *, cp_parser_flags);
2241 static cp_parameter_declarator *cp_parser_parameter_declaration
2242 (cp_parser *, cp_parser_flags, bool, bool *);
2243 static tree cp_parser_default_argument
2244 (cp_parser *, bool);
2245 static void cp_parser_function_body
2246 (cp_parser *, bool);
2247 static tree cp_parser_initializer
2248 (cp_parser *, bool *, bool *, bool = false);
2249 static cp_expr cp_parser_initializer_clause
2250 (cp_parser *, bool *);
2251 static cp_expr cp_parser_braced_list
2252 (cp_parser*, bool*);
2253 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2254 (cp_parser *, bool *, bool *);
2255
2256 static void cp_parser_ctor_initializer_opt_and_function_body
2257 (cp_parser *, bool);
2258
2259 static tree cp_parser_late_parsing_omp_declare_simd
2260 (cp_parser *, tree);
2261
2262 static tree cp_parser_late_parsing_oacc_routine
2263 (cp_parser *, tree);
2264
2265 static tree synthesize_implicit_template_parm
2266 (cp_parser *, tree);
2267 static tree finish_fully_implicit_template
2268 (cp_parser *, tree);
2269 static void abort_fully_implicit_template
2270 (cp_parser *);
2271
2272 /* Classes [gram.class] */
2273
2274 static tree cp_parser_class_name
2275 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2276 static tree cp_parser_class_specifier
2277 (cp_parser *);
2278 static tree cp_parser_class_head
2279 (cp_parser *, bool *);
2280 static enum tag_types cp_parser_class_key
2281 (cp_parser *);
2282 static void cp_parser_type_parameter_key
2283 (cp_parser* parser);
2284 static void cp_parser_member_specification_opt
2285 (cp_parser *);
2286 static void cp_parser_member_declaration
2287 (cp_parser *);
2288 static tree cp_parser_pure_specifier
2289 (cp_parser *);
2290 static tree cp_parser_constant_initializer
2291 (cp_parser *);
2292
2293 /* Derived classes [gram.class.derived] */
2294
2295 static tree cp_parser_base_clause
2296 (cp_parser *);
2297 static tree cp_parser_base_specifier
2298 (cp_parser *);
2299
2300 /* Special member functions [gram.special] */
2301
2302 static tree cp_parser_conversion_function_id
2303 (cp_parser *);
2304 static tree cp_parser_conversion_type_id
2305 (cp_parser *);
2306 static cp_declarator *cp_parser_conversion_declarator_opt
2307 (cp_parser *);
2308 static void cp_parser_ctor_initializer_opt
2309 (cp_parser *);
2310 static void cp_parser_mem_initializer_list
2311 (cp_parser *);
2312 static tree cp_parser_mem_initializer
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer_id
2315 (cp_parser *);
2316
2317 /* Overloading [gram.over] */
2318
2319 static cp_expr cp_parser_operator_function_id
2320 (cp_parser *);
2321 static cp_expr cp_parser_operator
2322 (cp_parser *, location_t);
2323
2324 /* Templates [gram.temp] */
2325
2326 static void cp_parser_template_declaration
2327 (cp_parser *, bool);
2328 static tree cp_parser_template_parameter_list
2329 (cp_parser *);
2330 static tree cp_parser_template_parameter
2331 (cp_parser *, bool *, bool *);
2332 static tree cp_parser_type_parameter
2333 (cp_parser *, bool *);
2334 static tree cp_parser_template_id
2335 (cp_parser *, bool, bool, enum tag_types, bool);
2336 static tree cp_parser_template_name
2337 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2338 static tree cp_parser_template_argument_list
2339 (cp_parser *);
2340 static tree cp_parser_template_argument
2341 (cp_parser *);
2342 static void cp_parser_explicit_instantiation
2343 (cp_parser *);
2344 static void cp_parser_explicit_specialization
2345 (cp_parser *);
2346
2347 /* Exception handling [gram.exception] */
2348
2349 static tree cp_parser_try_block
2350 (cp_parser *);
2351 static void cp_parser_function_try_block
2352 (cp_parser *);
2353 static void cp_parser_handler_seq
2354 (cp_parser *);
2355 static void cp_parser_handler
2356 (cp_parser *);
2357 static tree cp_parser_exception_declaration
2358 (cp_parser *);
2359 static tree cp_parser_throw_expression
2360 (cp_parser *);
2361 static tree cp_parser_exception_specification_opt
2362 (cp_parser *);
2363 static tree cp_parser_type_id_list
2364 (cp_parser *);
2365
2366 /* GNU Extensions */
2367
2368 static tree cp_parser_asm_specification_opt
2369 (cp_parser *);
2370 static tree cp_parser_asm_operand_list
2371 (cp_parser *);
2372 static tree cp_parser_asm_clobber_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_label_list
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_attribute_p
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_gnu_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_std_attribute_p
2381 (cp_parser *);
2382 static bool cp_nth_tokens_can_be_std_attribute_p
2383 (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_gnu_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_attribute_p
2387 (cp_parser *, size_t);
2388 static tree cp_parser_attributes_opt
2389 (cp_parser *);
2390 static tree cp_parser_gnu_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attribute_list
2393 (cp_parser *, bool = false);
2394 static tree cp_parser_std_attribute
2395 (cp_parser *, tree);
2396 static tree cp_parser_std_attribute_spec
2397 (cp_parser *);
2398 static tree cp_parser_std_attribute_spec_seq
2399 (cp_parser *);
2400 static size_t cp_parser_skip_attributes_opt
2401 (cp_parser *, size_t);
2402 static bool cp_parser_extension_opt
2403 (cp_parser *, int *);
2404 static void cp_parser_label_declaration
2405 (cp_parser *);
2406
2407 /* Concept Extensions */
2408
2409 static tree cp_parser_requires_clause
2410 (cp_parser *);
2411 static tree cp_parser_requires_clause_opt
2412 (cp_parser *);
2413 static tree cp_parser_requires_expression
2414 (cp_parser *);
2415 static tree cp_parser_requirement_parameter_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement_body
2418 (cp_parser *);
2419 static tree cp_parser_requirement_list
2420 (cp_parser *);
2421 static tree cp_parser_requirement
2422 (cp_parser *);
2423 static tree cp_parser_simple_requirement
2424 (cp_parser *);
2425 static tree cp_parser_compound_requirement
2426 (cp_parser *);
2427 static tree cp_parser_type_requirement
2428 (cp_parser *);
2429 static tree cp_parser_nested_requirement
2430 (cp_parser *);
2431
2432 /* Transactional Memory Extensions */
2433
2434 static tree cp_parser_transaction
2435 (cp_parser *, cp_token *);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser *, enum rid);
2438 static void cp_parser_function_transaction
2439 (cp_parser *, enum rid);
2440 static tree cp_parser_transaction_cancel
2441 (cp_parser *);
2442
2443 enum pragma_context {
2444 pragma_external,
2445 pragma_member,
2446 pragma_objc_icode,
2447 pragma_stmt,
2448 pragma_compound
2449 };
2450 static bool cp_parser_pragma
2451 (cp_parser *, enum pragma_context, bool *);
2452
2453 /* Objective-C++ Productions */
2454
2455 static tree cp_parser_objc_message_receiver
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_args
2458 (cp_parser *);
2459 static tree cp_parser_objc_message_expression
2460 (cp_parser *);
2461 static cp_expr cp_parser_objc_encode_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_defs_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_expression
2466 (cp_parser *);
2467 static tree cp_parser_objc_selector_expression
2468 (cp_parser *);
2469 static cp_expr cp_parser_objc_expression
2470 (cp_parser *);
2471 static bool cp_parser_objc_selector_p
2472 (enum cpp_ttype);
2473 static tree cp_parser_objc_selector
2474 (cp_parser *);
2475 static tree cp_parser_objc_protocol_refs_opt
2476 (cp_parser *);
2477 static void cp_parser_objc_declaration
2478 (cp_parser *, tree);
2479 static tree cp_parser_objc_statement
2480 (cp_parser *);
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser *, tree *);
2483 static void cp_parser_objc_at_property_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_synthesize_declaration
2486 (cp_parser *) ;
2487 static void cp_parser_objc_at_dynamic_declaration
2488 (cp_parser *) ;
2489 static tree cp_parser_objc_struct_declaration
2490 (cp_parser *) ;
2491
2492 /* Utility Routines */
2493
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser *, tree, location_t);
2498 static tree cp_parser_maybe_treat_template_as_class
2499 (tree, bool);
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser *, cp_declarator *, location_t);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2504 static cp_expr cp_parser_simple_cast_expression
2505 (cp_parser *);
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser *, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser *, cp_parser_flags, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser *, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser *, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec<deferred_access_check, va_gc> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser *, tree);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2524 static tree cp_parser_save_nsdmi
2525 (cp_parser *);
2526 static tree cp_parser_enclosed_template_argument_list
2527 (cp_parser *);
2528 static void cp_parser_save_default_args
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser *, tree);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser *, tree, tree, tree);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser *, tree);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser *, tree);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser *, enum rid);
2540 static cp_expr cp_parser_trait_expr
2541 (cp_parser *, enum rid);
2542 static bool cp_parser_declares_only_class_p
2543 (cp_parser *);
2544 static void cp_parser_set_storage_class
2545 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq *decl_specs,
2550 cp_decl_spec ds, cp_token *);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq *);
2553 static void cp_parser_required_error
2554 (cp_parser *, required_token, bool, location_t);
2555 static cp_token *cp_parser_require
2556 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2557 static cp_token *cp_parser_require_keyword
2558 (cp_parser *, enum rid, required_token);
2559 static bool cp_parser_token_starts_function_definition_p
2560 (cp_token *);
2561 static bool cp_parser_next_token_starts_class_definition_p
2562 (cp_parser *);
2563 static bool cp_parser_next_token_ends_template_argument_p
2564 (cp_parser *);
2565 static bool cp_parser_nth_token_starts_template_argument_list_p
2566 (cp_parser *, size_t);
2567 static enum tag_types cp_parser_token_is_class_key
2568 (cp_token *);
2569 static enum tag_types cp_parser_token_is_type_parameter_key
2570 (cp_token *);
2571 static void cp_parser_check_class_key
2572 (enum tag_types, tree type);
2573 static void cp_parser_check_access_in_redeclaration
2574 (tree type, location_t location);
2575 static bool cp_parser_optional_template_keyword
2576 (cp_parser *);
2577 static void cp_parser_pre_parsed_nested_name_specifier
2578 (cp_parser *);
2579 static bool cp_parser_cache_group
2580 (cp_parser *, enum cpp_ttype, unsigned);
2581 static tree cp_parser_cache_defarg
2582 (cp_parser *parser, bool nsdmi);
2583 static void cp_parser_parse_tentatively
2584 (cp_parser *);
2585 static void cp_parser_commit_to_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_commit_to_topmost_tentative_parse
2588 (cp_parser *);
2589 static void cp_parser_abort_tentative_parse
2590 (cp_parser *);
2591 static bool cp_parser_parse_definitely
2592 (cp_parser *);
2593 static inline bool cp_parser_parsing_tentatively
2594 (cp_parser *);
2595 static bool cp_parser_uncommitted_to_tentative_parse_p
2596 (cp_parser *);
2597 static void cp_parser_error
2598 (cp_parser *, const char *);
2599 static void cp_parser_name_lookup_error
2600 (cp_parser *, tree, tree, name_lookup_error, location_t);
2601 static bool cp_parser_simulate_error
2602 (cp_parser *);
2603 static bool cp_parser_check_type_definition
2604 (cp_parser *);
2605 static void cp_parser_check_for_definition_in_return_type
2606 (cp_declarator *, tree, location_t type_location);
2607 static void cp_parser_check_for_invalid_template_id
2608 (cp_parser *, tree, enum tag_types, location_t location);
2609 static bool cp_parser_non_integral_constant_expression
2610 (cp_parser *, non_integral_constant);
2611 static void cp_parser_diagnose_invalid_type_name
2612 (cp_parser *, tree, location_t);
2613 static bool cp_parser_parse_and_diagnose_invalid_type_name
2614 (cp_parser *);
2615 static int cp_parser_skip_to_closing_parenthesis
2616 (cp_parser *, bool, bool, bool);
2617 static void cp_parser_skip_to_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_consume_semicolon_at_end_of_statement
2620 (cp_parser *);
2621 static void cp_parser_skip_to_end_of_block_or_statement
2622 (cp_parser *);
2623 static bool cp_parser_skip_to_closing_brace
2624 (cp_parser *);
2625 static void cp_parser_skip_to_end_of_template_parameter_list
2626 (cp_parser *);
2627 static void cp_parser_skip_to_pragma_eol
2628 (cp_parser*, cp_token *);
2629 static bool cp_parser_error_occurred
2630 (cp_parser *);
2631 static bool cp_parser_allow_gnu_extensions_p
2632 (cp_parser *);
2633 static bool cp_parser_is_pure_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_string_literal
2636 (cp_token *);
2637 static bool cp_parser_is_keyword
2638 (cp_token *, enum rid);
2639 static tree cp_parser_make_typename_type
2640 (cp_parser *, tree, location_t location);
2641 static cp_declarator * cp_parser_make_indirect_declarator
2642 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2643 static bool cp_parser_compound_literal_p
2644 (cp_parser *);
2645 static bool cp_parser_array_designator_p
2646 (cp_parser *);
2647 static bool cp_parser_init_statement_p
2648 (cp_parser *);
2649 static bool cp_parser_skip_to_closing_square_bracket
2650 (cp_parser *);
2651
2652 /* Concept-related syntactic transformations */
2653
2654 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2655 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2656
2657 // -------------------------------------------------------------------------- //
2658 // Unevaluated Operand Guard
2659 //
2660 // Implementation of an RAII helper for unevaluated operand parsing.
2661 cp_unevaluated::cp_unevaluated ()
2662 {
2663 ++cp_unevaluated_operand;
2664 ++c_inhibit_evaluation_warnings;
2665 }
2666
2667 cp_unevaluated::~cp_unevaluated ()
2668 {
2669 --c_inhibit_evaluation_warnings;
2670 --cp_unevaluated_operand;
2671 }
2672
2673 // -------------------------------------------------------------------------- //
2674 // Tentative Parsing
2675
2676 /* Returns nonzero if we are parsing tentatively. */
2677
2678 static inline bool
2679 cp_parser_parsing_tentatively (cp_parser* parser)
2680 {
2681 return parser->context->next != NULL;
2682 }
2683
2684 /* Returns nonzero if TOKEN is a string literal. */
2685
2686 static bool
2687 cp_parser_is_pure_string_literal (cp_token* token)
2688 {
2689 return (token->type == CPP_STRING ||
2690 token->type == CPP_STRING16 ||
2691 token->type == CPP_STRING32 ||
2692 token->type == CPP_WSTRING ||
2693 token->type == CPP_UTF8STRING);
2694 }
2695
2696 /* Returns nonzero if TOKEN is a string literal
2697 of a user-defined string literal. */
2698
2699 static bool
2700 cp_parser_is_string_literal (cp_token* token)
2701 {
2702 return (cp_parser_is_pure_string_literal (token) ||
2703 token->type == CPP_STRING_USERDEF ||
2704 token->type == CPP_STRING16_USERDEF ||
2705 token->type == CPP_STRING32_USERDEF ||
2706 token->type == CPP_WSTRING_USERDEF ||
2707 token->type == CPP_UTF8STRING_USERDEF);
2708 }
2709
2710 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2711
2712 static bool
2713 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2714 {
2715 return token->keyword == keyword;
2716 }
2717
2718 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 PRAGMA_NONE. */
2720
2721 static enum pragma_kind
2722 cp_parser_pragma_kind (cp_token *token)
2723 {
2724 if (token->type != CPP_PRAGMA)
2725 return PRAGMA_NONE;
2726 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2727 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2728 }
2729
2730 /* Helper function for cp_parser_error.
2731 Having peeked a token of kind TOK1_KIND that might signify
2732 a conflict marker, peek successor tokens to determine
2733 if we actually do have a conflict marker.
2734 Specifically, we consider a run of 7 '<', '=' or '>' characters
2735 at the start of a line as a conflict marker.
2736 These come through the lexer as three pairs and a single,
2737 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2738 If it returns true, *OUT_LOC is written to with the location/range
2739 of the marker. */
2740
2741 static bool
2742 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2743 location_t *out_loc)
2744 {
2745 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2746 if (token2->type != tok1_kind)
2747 return false;
2748 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2749 if (token3->type != tok1_kind)
2750 return false;
2751 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2752 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2753 return false;
2754
2755 /* It must be at the start of the line. */
2756 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2757 if (LOCATION_COLUMN (start_loc) != 1)
2758 return false;
2759
2760 /* We have a conflict marker. Construct a location of the form:
2761 <<<<<<<
2762 ^~~~~~~
2763 with start == caret, finishing at the end of the marker. */
2764 location_t finish_loc = get_finish (token4->location);
2765 *out_loc = make_location (start_loc, start_loc, finish_loc);
2766
2767 return true;
2768 }
2769
2770 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2771 RT_CLOSE_PAREN. */
2772
2773 static const char *
2774 get_matching_symbol (required_token token_desc)
2775 {
2776 switch (token_desc)
2777 {
2778 default:
2779 gcc_unreachable ();
2780 return "";
2781 case RT_CLOSE_BRACE:
2782 return "{";
2783 case RT_CLOSE_PAREN:
2784 return "(";
2785 }
2786 }
2787
2788 /* Attempt to convert TOKEN_DESC from a required_token to an
2789 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2790
2791 static enum cpp_ttype
2792 get_required_cpp_ttype (required_token token_desc)
2793 {
2794 switch (token_desc)
2795 {
2796 case RT_SEMICOLON:
2797 return CPP_SEMICOLON;
2798 case RT_OPEN_PAREN:
2799 return CPP_OPEN_PAREN;
2800 case RT_CLOSE_BRACE:
2801 return CPP_CLOSE_BRACE;
2802 case RT_OPEN_BRACE:
2803 return CPP_OPEN_BRACE;
2804 case RT_CLOSE_SQUARE:
2805 return CPP_CLOSE_SQUARE;
2806 case RT_OPEN_SQUARE:
2807 return CPP_OPEN_SQUARE;
2808 case RT_COMMA:
2809 return CPP_COMMA;
2810 case RT_COLON:
2811 return CPP_COLON;
2812 case RT_CLOSE_PAREN:
2813 return CPP_CLOSE_PAREN;
2814
2815 default:
2816 /* Use CPP_EOF as a "no completions possible" code. */
2817 return CPP_EOF;
2818 }
2819 }
2820
2821
2822 /* Subroutine of cp_parser_error and cp_parser_required_error.
2823
2824 Issue a diagnostic of the form
2825 FILE:LINE: MESSAGE before TOKEN
2826 where TOKEN is the next token in the input stream. MESSAGE
2827 (specified by the caller) is usually of the form "expected
2828 OTHER-TOKEN".
2829
2830 This bypasses the check for tentative passing, and potentially
2831 adds material needed by cp_parser_required_error.
2832
2833 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2834 suggesting insertion of the missing token.
2835
2836 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2837 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2838 location. */
2839
2840 static void
2841 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2842 required_token missing_token_desc,
2843 location_t matching_location)
2844 {
2845 cp_token *token = cp_lexer_peek_token (parser->lexer);
2846 /* This diagnostic makes more sense if it is tagged to the line
2847 of the token we just peeked at. */
2848 cp_lexer_set_source_position_from_token (token);
2849
2850 if (token->type == CPP_PRAGMA)
2851 {
2852 error_at (token->location,
2853 "%<#pragma%> is not allowed here");
2854 cp_parser_skip_to_pragma_eol (parser, token);
2855 return;
2856 }
2857
2858 /* If this is actually a conflict marker, report it as such. */
2859 if (token->type == CPP_LSHIFT
2860 || token->type == CPP_RSHIFT
2861 || token->type == CPP_EQ_EQ)
2862 {
2863 location_t loc;
2864 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2865 {
2866 error_at (loc, "version control conflict marker in file");
2867 expanded_location token_exploc = expand_location (token->location);
2868 /* Consume tokens until the end of the source line. */
2869 while (1)
2870 {
2871 cp_lexer_consume_token (parser->lexer);
2872 cp_token *next = cp_lexer_peek_token (parser->lexer);
2873 if (next == NULL)
2874 break;
2875 expanded_location next_exploc = expand_location (next->location);
2876 if (next_exploc.file != token_exploc.file)
2877 break;
2878 if (next_exploc.line != token_exploc.line)
2879 break;
2880 }
2881 return;
2882 }
2883 }
2884
2885 gcc_rich_location richloc (input_location);
2886
2887 bool added_matching_location = false;
2888
2889 if (missing_token_desc != RT_NONE)
2890 {
2891 /* Potentially supply a fix-it hint, suggesting to add the
2892 missing token immediately after the *previous* token.
2893 This may move the primary location within richloc. */
2894 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2895 location_t prev_token_loc
2896 = cp_lexer_previous_token (parser->lexer)->location;
2897 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2898
2899 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2900 Attempt to consolidate diagnostics by printing it as a
2901 secondary range within the main diagnostic. */
2902 if (matching_location != UNKNOWN_LOCATION)
2903 added_matching_location
2904 = richloc.add_location_if_nearby (matching_location);
2905 }
2906
2907 /* Actually emit the error. */
2908 c_parse_error (gmsgid,
2909 /* Because c_parser_error does not understand
2910 CPP_KEYWORD, keywords are treated like
2911 identifiers. */
2912 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2913 token->u.value, token->flags, &richloc);
2914
2915 if (missing_token_desc != RT_NONE)
2916 {
2917 /* If we weren't able to consolidate matching_location, then
2918 print it as a secondary diagnostic. */
2919 if (matching_location != UNKNOWN_LOCATION
2920 && !added_matching_location)
2921 inform (matching_location, "to match this %qs",
2922 get_matching_symbol (missing_token_desc));
2923 }
2924 }
2925
2926 /* If not parsing tentatively, issue a diagnostic of the form
2927 FILE:LINE: MESSAGE before TOKEN
2928 where TOKEN is the next token in the input stream. MESSAGE
2929 (specified by the caller) is usually of the form "expected
2930 OTHER-TOKEN". */
2931
2932 static void
2933 cp_parser_error (cp_parser* parser, const char* gmsgid)
2934 {
2935 if (!cp_parser_simulate_error (parser))
2936 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2937 }
2938
2939 /* Issue an error about name-lookup failing. NAME is the
2940 IDENTIFIER_NODE DECL is the result of
2941 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2942 the thing that we hoped to find. */
2943
2944 static void
2945 cp_parser_name_lookup_error (cp_parser* parser,
2946 tree name,
2947 tree decl,
2948 name_lookup_error desired,
2949 location_t location)
2950 {
2951 /* If name lookup completely failed, tell the user that NAME was not
2952 declared. */
2953 if (decl == error_mark_node)
2954 {
2955 if (parser->scope && parser->scope != global_namespace)
2956 error_at (location, "%<%E::%E%> has not been declared",
2957 parser->scope, name);
2958 else if (parser->scope == global_namespace)
2959 error_at (location, "%<::%E%> has not been declared", name);
2960 else if (parser->object_scope
2961 && !CLASS_TYPE_P (parser->object_scope))
2962 error_at (location, "request for member %qE in non-class type %qT",
2963 name, parser->object_scope);
2964 else if (parser->object_scope)
2965 error_at (location, "%<%T::%E%> has not been declared",
2966 parser->object_scope, name);
2967 else
2968 error_at (location, "%qE has not been declared", name);
2969 }
2970 else if (parser->scope && parser->scope != global_namespace)
2971 {
2972 switch (desired)
2973 {
2974 case NLE_TYPE:
2975 error_at (location, "%<%E::%E%> is not a type",
2976 parser->scope, name);
2977 break;
2978 case NLE_CXX98:
2979 error_at (location, "%<%E::%E%> is not a class or namespace",
2980 parser->scope, name);
2981 break;
2982 case NLE_NOT_CXX98:
2983 error_at (location,
2984 "%<%E::%E%> is not a class, namespace, or enumeration",
2985 parser->scope, name);
2986 break;
2987 default:
2988 gcc_unreachable ();
2989
2990 }
2991 }
2992 else if (parser->scope == global_namespace)
2993 {
2994 switch (desired)
2995 {
2996 case NLE_TYPE:
2997 error_at (location, "%<::%E%> is not a type", name);
2998 break;
2999 case NLE_CXX98:
3000 error_at (location, "%<::%E%> is not a class or namespace", name);
3001 break;
3002 case NLE_NOT_CXX98:
3003 error_at (location,
3004 "%<::%E%> is not a class, namespace, or enumeration",
3005 name);
3006 break;
3007 default:
3008 gcc_unreachable ();
3009 }
3010 }
3011 else
3012 {
3013 switch (desired)
3014 {
3015 case NLE_TYPE:
3016 error_at (location, "%qE is not a type", name);
3017 break;
3018 case NLE_CXX98:
3019 error_at (location, "%qE is not a class or namespace", name);
3020 break;
3021 case NLE_NOT_CXX98:
3022 error_at (location,
3023 "%qE is not a class, namespace, or enumeration", name);
3024 break;
3025 default:
3026 gcc_unreachable ();
3027 }
3028 }
3029 }
3030
3031 /* If we are parsing tentatively, remember that an error has occurred
3032 during this tentative parse. Returns true if the error was
3033 simulated; false if a message should be issued by the caller. */
3034
3035 static bool
3036 cp_parser_simulate_error (cp_parser* parser)
3037 {
3038 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3039 {
3040 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3041 return true;
3042 }
3043 return false;
3044 }
3045
3046 /* This function is called when a type is defined. If type
3047 definitions are forbidden at this point, an error message is
3048 issued. */
3049
3050 static bool
3051 cp_parser_check_type_definition (cp_parser* parser)
3052 {
3053 /* If types are forbidden here, issue a message. */
3054 if (parser->type_definition_forbidden_message)
3055 {
3056 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3057 in the message need to be interpreted. */
3058 error (parser->type_definition_forbidden_message);
3059 return false;
3060 }
3061 return true;
3062 }
3063
3064 /* This function is called when the DECLARATOR is processed. The TYPE
3065 was a type defined in the decl-specifiers. If it is invalid to
3066 define a type in the decl-specifiers for DECLARATOR, an error is
3067 issued. TYPE_LOCATION is the location of TYPE and is used
3068 for error reporting. */
3069
3070 static void
3071 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3072 tree type, location_t type_location)
3073 {
3074 /* [dcl.fct] forbids type definitions in return types.
3075 Unfortunately, it's not easy to know whether or not we are
3076 processing a return type until after the fact. */
3077 while (declarator
3078 && (declarator->kind == cdk_pointer
3079 || declarator->kind == cdk_reference
3080 || declarator->kind == cdk_ptrmem))
3081 declarator = declarator->declarator;
3082 if (declarator
3083 && declarator->kind == cdk_function)
3084 {
3085 error_at (type_location,
3086 "new types may not be defined in a return type");
3087 inform (type_location,
3088 "(perhaps a semicolon is missing after the definition of %qT)",
3089 type);
3090 }
3091 }
3092
3093 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3094 "<" in any valid C++ program. If the next token is indeed "<",
3095 issue a message warning the user about what appears to be an
3096 invalid attempt to form a template-id. LOCATION is the location
3097 of the type-specifier (TYPE) */
3098
3099 static void
3100 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3101 tree type,
3102 enum tag_types tag_type,
3103 location_t location)
3104 {
3105 cp_token_position start = 0;
3106
3107 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3108 {
3109 if (TREE_CODE (type) == TYPE_DECL)
3110 type = TREE_TYPE (type);
3111 if (TYPE_P (type) && !template_placeholder_p (type))
3112 error_at (location, "%qT is not a template", type);
3113 else if (identifier_p (type))
3114 {
3115 if (tag_type != none_type)
3116 error_at (location, "%qE is not a class template", type);
3117 else
3118 error_at (location, "%qE is not a template", type);
3119 }
3120 else
3121 error_at (location, "invalid template-id");
3122 /* Remember the location of the invalid "<". */
3123 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3124 start = cp_lexer_token_position (parser->lexer, true);
3125 /* Consume the "<". */
3126 cp_lexer_consume_token (parser->lexer);
3127 /* Parse the template arguments. */
3128 cp_parser_enclosed_template_argument_list (parser);
3129 /* Permanently remove the invalid template arguments so that
3130 this error message is not issued again. */
3131 if (start)
3132 cp_lexer_purge_tokens_after (parser->lexer, start);
3133 }
3134 }
3135
3136 /* If parsing an integral constant-expression, issue an error message
3137 about the fact that THING appeared and return true. Otherwise,
3138 return false. In either case, set
3139 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3140
3141 static bool
3142 cp_parser_non_integral_constant_expression (cp_parser *parser,
3143 non_integral_constant thing)
3144 {
3145 parser->non_integral_constant_expression_p = true;
3146 if (parser->integral_constant_expression_p)
3147 {
3148 if (!parser->allow_non_integral_constant_expression_p)
3149 {
3150 const char *msg = NULL;
3151 switch (thing)
3152 {
3153 case NIC_FLOAT:
3154 pedwarn (input_location, OPT_Wpedantic,
3155 "ISO C++ forbids using a floating-point literal "
3156 "in a constant-expression");
3157 return true;
3158 case NIC_CAST:
3159 error ("a cast to a type other than an integral or "
3160 "enumeration type cannot appear in a "
3161 "constant-expression");
3162 return true;
3163 case NIC_TYPEID:
3164 error ("%<typeid%> operator "
3165 "cannot appear in a constant-expression");
3166 return true;
3167 case NIC_NCC:
3168 error ("non-constant compound literals "
3169 "cannot appear in a constant-expression");
3170 return true;
3171 case NIC_FUNC_CALL:
3172 error ("a function call "
3173 "cannot appear in a constant-expression");
3174 return true;
3175 case NIC_INC:
3176 error ("an increment "
3177 "cannot appear in a constant-expression");
3178 return true;
3179 case NIC_DEC:
3180 error ("an decrement "
3181 "cannot appear in a constant-expression");
3182 return true;
3183 case NIC_ARRAY_REF:
3184 error ("an array reference "
3185 "cannot appear in a constant-expression");
3186 return true;
3187 case NIC_ADDR_LABEL:
3188 error ("the address of a label "
3189 "cannot appear in a constant-expression");
3190 return true;
3191 case NIC_OVERLOADED:
3192 error ("calls to overloaded operators "
3193 "cannot appear in a constant-expression");
3194 return true;
3195 case NIC_ASSIGNMENT:
3196 error ("an assignment cannot appear in a constant-expression");
3197 return true;
3198 case NIC_COMMA:
3199 error ("a comma operator "
3200 "cannot appear in a constant-expression");
3201 return true;
3202 case NIC_CONSTRUCTOR:
3203 error ("a call to a constructor "
3204 "cannot appear in a constant-expression");
3205 return true;
3206 case NIC_TRANSACTION:
3207 error ("a transaction expression "
3208 "cannot appear in a constant-expression");
3209 return true;
3210 case NIC_THIS:
3211 msg = "this";
3212 break;
3213 case NIC_FUNC_NAME:
3214 msg = "__FUNCTION__";
3215 break;
3216 case NIC_PRETTY_FUNC:
3217 msg = "__PRETTY_FUNCTION__";
3218 break;
3219 case NIC_C99_FUNC:
3220 msg = "__func__";
3221 break;
3222 case NIC_VA_ARG:
3223 msg = "va_arg";
3224 break;
3225 case NIC_ARROW:
3226 msg = "->";
3227 break;
3228 case NIC_POINT:
3229 msg = ".";
3230 break;
3231 case NIC_STAR:
3232 msg = "*";
3233 break;
3234 case NIC_ADDR:
3235 msg = "&";
3236 break;
3237 case NIC_PREINCREMENT:
3238 msg = "++";
3239 break;
3240 case NIC_PREDECREMENT:
3241 msg = "--";
3242 break;
3243 case NIC_NEW:
3244 msg = "new";
3245 break;
3246 case NIC_DEL:
3247 msg = "delete";
3248 break;
3249 default:
3250 gcc_unreachable ();
3251 }
3252 if (msg)
3253 error ("%qs cannot appear in a constant-expression", msg);
3254 return true;
3255 }
3256 }
3257 return false;
3258 }
3259
3260 /* Emit a diagnostic for an invalid type name. This function commits
3261 to the current active tentative parse, if any. (Otherwise, the
3262 problematic construct might be encountered again later, resulting
3263 in duplicate error messages.) LOCATION is the location of ID. */
3264
3265 static void
3266 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3267 location_t location)
3268 {
3269 tree decl, ambiguous_decls;
3270 cp_parser_commit_to_tentative_parse (parser);
3271 /* Try to lookup the identifier. */
3272 decl = cp_parser_lookup_name (parser, id, none_type,
3273 /*is_template=*/false,
3274 /*is_namespace=*/false,
3275 /*check_dependency=*/true,
3276 &ambiguous_decls, location);
3277 if (ambiguous_decls)
3278 /* If the lookup was ambiguous, an error will already have
3279 been issued. */
3280 return;
3281 /* If the lookup found a template-name, it means that the user forgot
3282 to specify an argument list. Emit a useful error message. */
3283 if (DECL_TYPE_TEMPLATE_P (decl))
3284 {
3285 auto_diagnostic_group d;
3286 error_at (location,
3287 "invalid use of template-name %qE without an argument list",
3288 decl);
3289 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3290 inform (location, "class template argument deduction is only available "
3291 "with %<-std=c++17%> or %<-std=gnu++17%>");
3292 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3293 }
3294 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3295 error_at (location, "invalid use of destructor %qD as a type", id);
3296 else if (TREE_CODE (decl) == TYPE_DECL)
3297 /* Something like 'unsigned A a;' */
3298 error_at (location, "invalid combination of multiple type-specifiers");
3299 else if (!parser->scope)
3300 {
3301 /* Issue an error message. */
3302 auto_diagnostic_group d;
3303 name_hint hint;
3304 if (TREE_CODE (id) == IDENTIFIER_NODE)
3305 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3306 if (const char *suggestion = hint.suggestion ())
3307 {
3308 gcc_rich_location richloc (location);
3309 richloc.add_fixit_replace (suggestion);
3310 error_at (&richloc,
3311 "%qE does not name a type; did you mean %qs?",
3312 id, suggestion);
3313 }
3314 else
3315 error_at (location, "%qE does not name a type", id);
3316 /* If we're in a template class, it's possible that the user was
3317 referring to a type from a base class. For example:
3318
3319 template <typename T> struct A { typedef T X; };
3320 template <typename T> struct B : public A<T> { X x; };
3321
3322 The user should have said "typename A<T>::X". */
3323 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3324 inform (location, "C++11 %<constexpr%> only available with "
3325 "%<-std=c++11%> or %<-std=gnu++11%>");
3326 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3327 inform (location, "C++11 %<noexcept%> only available with "
3328 "%<-std=c++11%> or %<-std=gnu++11%>");
3329 else if (cxx_dialect < cxx11
3330 && TREE_CODE (id) == IDENTIFIER_NODE
3331 && id_equal (id, "thread_local"))
3332 inform (location, "C++11 %<thread_local%> only available with "
3333 "%<-std=c++11%> or %<-std=gnu++11%>");
3334 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3335 inform (location, "%<concept%> only available with %<-fconcepts%>");
3336 else if (processing_template_decl && current_class_type
3337 && TYPE_BINFO (current_class_type))
3338 {
3339 tree b;
3340
3341 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3342 b;
3343 b = TREE_CHAIN (b))
3344 {
3345 tree base_type = BINFO_TYPE (b);
3346 if (CLASS_TYPE_P (base_type)
3347 && dependent_type_p (base_type))
3348 {
3349 tree field;
3350 /* Go from a particular instantiation of the
3351 template (which will have an empty TYPE_FIELDs),
3352 to the main version. */
3353 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3354 for (field = TYPE_FIELDS (base_type);
3355 field;
3356 field = DECL_CHAIN (field))
3357 if (TREE_CODE (field) == TYPE_DECL
3358 && DECL_NAME (field) == id)
3359 {
3360 inform (location,
3361 "(perhaps %<typename %T::%E%> was intended)",
3362 BINFO_TYPE (b), id);
3363 break;
3364 }
3365 if (field)
3366 break;
3367 }
3368 }
3369 }
3370 }
3371 /* Here we diagnose qualified-ids where the scope is actually correct,
3372 but the identifier does not resolve to a valid type name. */
3373 else if (parser->scope != error_mark_node)
3374 {
3375 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3376 {
3377 auto_diagnostic_group d;
3378 name_hint hint;
3379 if (decl == error_mark_node)
3380 hint = suggest_alternative_in_explicit_scope (location, id,
3381 parser->scope);
3382 const char *suggestion = hint.suggestion ();
3383 gcc_rich_location richloc (location_of (id));
3384 if (suggestion)
3385 richloc.add_fixit_replace (suggestion);
3386 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3387 {
3388 if (suggestion)
3389 error_at (&richloc,
3390 "%qE in namespace %qE does not name a template"
3391 " type; did you mean %qs?",
3392 id, parser->scope, suggestion);
3393 else
3394 error_at (&richloc,
3395 "%qE in namespace %qE does not name a template type",
3396 id, parser->scope);
3397 }
3398 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3399 {
3400 if (suggestion)
3401 error_at (&richloc,
3402 "%qE in namespace %qE does not name a template"
3403 " type; did you mean %qs?",
3404 TREE_OPERAND (id, 0), parser->scope, suggestion);
3405 else
3406 error_at (&richloc,
3407 "%qE in namespace %qE does not name a template"
3408 " type",
3409 TREE_OPERAND (id, 0), parser->scope);
3410 }
3411 else
3412 {
3413 if (suggestion)
3414 error_at (&richloc,
3415 "%qE in namespace %qE does not name a type"
3416 "; did you mean %qs?",
3417 id, parser->scope, suggestion);
3418 else
3419 error_at (&richloc,
3420 "%qE in namespace %qE does not name a type",
3421 id, parser->scope);
3422 }
3423 if (DECL_P (decl))
3424 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3425 }
3426 else if (CLASS_TYPE_P (parser->scope)
3427 && constructor_name_p (id, parser->scope))
3428 {
3429 /* A<T>::A<T>() */
3430 auto_diagnostic_group d;
3431 error_at (location, "%<%T::%E%> names the constructor, not"
3432 " the type", parser->scope, id);
3433 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3434 error_at (location, "and %qT has no template constructors",
3435 parser->scope);
3436 }
3437 else if (TYPE_P (parser->scope)
3438 && dependent_scope_p (parser->scope))
3439 {
3440 gcc_rich_location richloc (location);
3441 richloc.add_fixit_insert_before ("typename ");
3442 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3443 error_at (&richloc,
3444 "need %<typename%> before %<%T::%D::%E%> because "
3445 "%<%T::%D%> is a dependent scope",
3446 TYPE_CONTEXT (parser->scope),
3447 TYPENAME_TYPE_FULLNAME (parser->scope),
3448 id,
3449 TYPE_CONTEXT (parser->scope),
3450 TYPENAME_TYPE_FULLNAME (parser->scope));
3451 else
3452 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3453 "%qT is a dependent scope",
3454 parser->scope, id, parser->scope);
3455 }
3456 else if (TYPE_P (parser->scope))
3457 {
3458 auto_diagnostic_group d;
3459 if (!COMPLETE_TYPE_P (parser->scope))
3460 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3461 parser->scope);
3462 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3463 error_at (location_of (id),
3464 "%qE in %q#T does not name a template type",
3465 id, parser->scope);
3466 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3467 error_at (location_of (id),
3468 "%qE in %q#T does not name a template type",
3469 TREE_OPERAND (id, 0), parser->scope);
3470 else
3471 error_at (location_of (id),
3472 "%qE in %q#T does not name a type",
3473 id, parser->scope);
3474 if (DECL_P (decl))
3475 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3476 }
3477 else
3478 gcc_unreachable ();
3479 }
3480 }
3481
3482 /* Check for a common situation where a type-name should be present,
3483 but is not, and issue a sensible error message. Returns true if an
3484 invalid type-name was detected.
3485
3486 The situation handled by this function are variable declarations of the
3487 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3488 Usually, `ID' should name a type, but if we got here it means that it
3489 does not. We try to emit the best possible error message depending on
3490 how exactly the id-expression looks like. */
3491
3492 static bool
3493 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3494 {
3495 tree id;
3496 cp_token *token = cp_lexer_peek_token (parser->lexer);
3497
3498 /* Avoid duplicate error about ambiguous lookup. */
3499 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3500 {
3501 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3502 if (next->type == CPP_NAME && next->error_reported)
3503 goto out;
3504 }
3505
3506 cp_parser_parse_tentatively (parser);
3507 id = cp_parser_id_expression (parser,
3508 /*template_keyword_p=*/false,
3509 /*check_dependency_p=*/true,
3510 /*template_p=*/NULL,
3511 /*declarator_p=*/false,
3512 /*optional_p=*/false);
3513 /* If the next token is a (, this is a function with no explicit return
3514 type, i.e. constructor, destructor or conversion op. */
3515 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3516 || TREE_CODE (id) == TYPE_DECL)
3517 {
3518 cp_parser_abort_tentative_parse (parser);
3519 return false;
3520 }
3521 if (!cp_parser_parse_definitely (parser))
3522 return false;
3523
3524 /* Emit a diagnostic for the invalid type. */
3525 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3526 out:
3527 /* If we aren't in the middle of a declarator (i.e. in a
3528 parameter-declaration-clause), skip to the end of the declaration;
3529 there's no point in trying to process it. */
3530 if (!parser->in_declarator_p)
3531 cp_parser_skip_to_end_of_block_or_statement (parser);
3532 return true;
3533 }
3534
3535 /* Consume tokens up to, and including, the next non-nested closing `)'.
3536 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3537 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3538 found an unnested token of that type. */
3539
3540 static int
3541 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3542 bool recovering,
3543 cpp_ttype or_ttype,
3544 bool consume_paren)
3545 {
3546 unsigned paren_depth = 0;
3547 unsigned brace_depth = 0;
3548 unsigned square_depth = 0;
3549 unsigned condop_depth = 0;
3550
3551 if (recovering && or_ttype == CPP_EOF
3552 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3553 return 0;
3554
3555 while (true)
3556 {
3557 cp_token * token = cp_lexer_peek_token (parser->lexer);
3558
3559 /* Have we found what we're looking for before the closing paren? */
3560 if (token->type == or_ttype && or_ttype != CPP_EOF
3561 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3562 return -1;
3563
3564 switch (token->type)
3565 {
3566 case CPP_PRAGMA_EOL:
3567 if (!parser->lexer->in_pragma)
3568 break;
3569 /* FALLTHRU */
3570 case CPP_EOF:
3571 /* If we've run out of tokens, then there is no closing `)'. */
3572 return 0;
3573
3574 /* This is good for lambda expression capture-lists. */
3575 case CPP_OPEN_SQUARE:
3576 ++square_depth;
3577 break;
3578 case CPP_CLOSE_SQUARE:
3579 if (!square_depth--)
3580 return 0;
3581 break;
3582
3583 case CPP_SEMICOLON:
3584 /* This matches the processing in skip_to_end_of_statement. */
3585 if (!brace_depth)
3586 return 0;
3587 break;
3588
3589 case CPP_OPEN_BRACE:
3590 ++brace_depth;
3591 break;
3592 case CPP_CLOSE_BRACE:
3593 if (!brace_depth--)
3594 return 0;
3595 break;
3596
3597 case CPP_OPEN_PAREN:
3598 if (!brace_depth)
3599 ++paren_depth;
3600 break;
3601
3602 case CPP_CLOSE_PAREN:
3603 if (!brace_depth && !paren_depth--)
3604 {
3605 if (consume_paren)
3606 cp_lexer_consume_token (parser->lexer);
3607 return 1;
3608 }
3609 break;
3610
3611 case CPP_QUERY:
3612 if (!brace_depth && !paren_depth && !square_depth)
3613 ++condop_depth;
3614 break;
3615
3616 case CPP_COLON:
3617 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3618 condop_depth--;
3619 break;
3620
3621 default:
3622 break;
3623 }
3624
3625 /* Consume the token. */
3626 cp_lexer_consume_token (parser->lexer);
3627 }
3628 }
3629
3630 /* Consume tokens up to, and including, the next non-nested closing `)'.
3631 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3632 are doing error recovery. Returns -1 if OR_COMMA is true and we
3633 found an unnested token of that type. */
3634
3635 static int
3636 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3637 bool recovering,
3638 bool or_comma,
3639 bool consume_paren)
3640 {
3641 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3642 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3643 ttype, consume_paren);
3644 }
3645
3646 /* Consume tokens until we reach the end of the current statement.
3647 Normally, that will be just before consuming a `;'. However, if a
3648 non-nested `}' comes first, then we stop before consuming that. */
3649
3650 static void
3651 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3652 {
3653 unsigned nesting_depth = 0;
3654
3655 /* Unwind generic function template scope if necessary. */
3656 if (parser->fully_implicit_function_template_p)
3657 abort_fully_implicit_template (parser);
3658
3659 while (true)
3660 {
3661 cp_token *token = cp_lexer_peek_token (parser->lexer);
3662
3663 switch (token->type)
3664 {
3665 case CPP_PRAGMA_EOL:
3666 if (!parser->lexer->in_pragma)
3667 break;
3668 /* FALLTHRU */
3669 case CPP_EOF:
3670 /* If we've run out of tokens, stop. */
3671 return;
3672
3673 case CPP_SEMICOLON:
3674 /* If the next token is a `;', we have reached the end of the
3675 statement. */
3676 if (!nesting_depth)
3677 return;
3678 break;
3679
3680 case CPP_CLOSE_BRACE:
3681 /* If this is a non-nested '}', stop before consuming it.
3682 That way, when confronted with something like:
3683
3684 { 3 + }
3685
3686 we stop before consuming the closing '}', even though we
3687 have not yet reached a `;'. */
3688 if (nesting_depth == 0)
3689 return;
3690
3691 /* If it is the closing '}' for a block that we have
3692 scanned, stop -- but only after consuming the token.
3693 That way given:
3694
3695 void f g () { ... }
3696 typedef int I;
3697
3698 we will stop after the body of the erroneously declared
3699 function, but before consuming the following `typedef'
3700 declaration. */
3701 if (--nesting_depth == 0)
3702 {
3703 cp_lexer_consume_token (parser->lexer);
3704 return;
3705 }
3706 break;
3707
3708 case CPP_OPEN_BRACE:
3709 ++nesting_depth;
3710 break;
3711
3712 default:
3713 break;
3714 }
3715
3716 /* Consume the token. */
3717 cp_lexer_consume_token (parser->lexer);
3718 }
3719 }
3720
3721 /* This function is called at the end of a statement or declaration.
3722 If the next token is a semicolon, it is consumed; otherwise, error
3723 recovery is attempted. */
3724
3725 static void
3726 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3727 {
3728 /* Look for the trailing `;'. */
3729 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3730 {
3731 /* If there is additional (erroneous) input, skip to the end of
3732 the statement. */
3733 cp_parser_skip_to_end_of_statement (parser);
3734 /* If the next token is now a `;', consume it. */
3735 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3736 cp_lexer_consume_token (parser->lexer);
3737 }
3738 }
3739
3740 /* Skip tokens until we have consumed an entire block, or until we
3741 have consumed a non-nested `;'. */
3742
3743 static void
3744 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3745 {
3746 int nesting_depth = 0;
3747
3748 /* Unwind generic function template scope if necessary. */
3749 if (parser->fully_implicit_function_template_p)
3750 abort_fully_implicit_template (parser);
3751
3752 while (nesting_depth >= 0)
3753 {
3754 cp_token *token = cp_lexer_peek_token (parser->lexer);
3755
3756 switch (token->type)
3757 {
3758 case CPP_PRAGMA_EOL:
3759 if (!parser->lexer->in_pragma)
3760 break;
3761 /* FALLTHRU */
3762 case CPP_EOF:
3763 /* If we've run out of tokens, stop. */
3764 return;
3765
3766 case CPP_SEMICOLON:
3767 /* Stop if this is an unnested ';'. */
3768 if (!nesting_depth)
3769 nesting_depth = -1;
3770 break;
3771
3772 case CPP_CLOSE_BRACE:
3773 /* Stop if this is an unnested '}', or closes the outermost
3774 nesting level. */
3775 nesting_depth--;
3776 if (nesting_depth < 0)
3777 return;
3778 if (!nesting_depth)
3779 nesting_depth = -1;
3780 break;
3781
3782 case CPP_OPEN_BRACE:
3783 /* Nest. */
3784 nesting_depth++;
3785 break;
3786
3787 default:
3788 break;
3789 }
3790
3791 /* Consume the token. */
3792 cp_lexer_consume_token (parser->lexer);
3793 }
3794 }
3795
3796 /* Skip tokens until a non-nested closing curly brace is the next
3797 token, or there are no more tokens. Return true in the first case,
3798 false otherwise. */
3799
3800 static bool
3801 cp_parser_skip_to_closing_brace (cp_parser *parser)
3802 {
3803 unsigned nesting_depth = 0;
3804
3805 while (true)
3806 {
3807 cp_token *token = cp_lexer_peek_token (parser->lexer);
3808
3809 switch (token->type)
3810 {
3811 case CPP_PRAGMA_EOL:
3812 if (!parser->lexer->in_pragma)
3813 break;
3814 /* FALLTHRU */
3815 case CPP_EOF:
3816 /* If we've run out of tokens, stop. */
3817 return false;
3818
3819 case CPP_CLOSE_BRACE:
3820 /* If the next token is a non-nested `}', then we have reached
3821 the end of the current block. */
3822 if (nesting_depth-- == 0)
3823 return true;
3824 break;
3825
3826 case CPP_OPEN_BRACE:
3827 /* If it the next token is a `{', then we are entering a new
3828 block. Consume the entire block. */
3829 ++nesting_depth;
3830 break;
3831
3832 default:
3833 break;
3834 }
3835
3836 /* Consume the token. */
3837 cp_lexer_consume_token (parser->lexer);
3838 }
3839 }
3840
3841 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3842 parameter is the PRAGMA token, allowing us to purge the entire pragma
3843 sequence. */
3844
3845 static void
3846 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3847 {
3848 cp_token *token;
3849
3850 parser->lexer->in_pragma = false;
3851
3852 do
3853 token = cp_lexer_consume_token (parser->lexer);
3854 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3855
3856 /* Ensure that the pragma is not parsed again. */
3857 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3858 }
3859
3860 /* Require pragma end of line, resyncing with it as necessary. The
3861 arguments are as for cp_parser_skip_to_pragma_eol. */
3862
3863 static void
3864 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3865 {
3866 parser->lexer->in_pragma = false;
3867 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3868 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3869 }
3870
3871 /* This is a simple wrapper around make_typename_type. When the id is
3872 an unresolved identifier node, we can provide a superior diagnostic
3873 using cp_parser_diagnose_invalid_type_name. */
3874
3875 static tree
3876 cp_parser_make_typename_type (cp_parser *parser, tree id,
3877 location_t id_location)
3878 {
3879 tree result;
3880 if (identifier_p (id))
3881 {
3882 result = make_typename_type (parser->scope, id, typename_type,
3883 /*complain=*/tf_none);
3884 if (result == error_mark_node)
3885 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3886 return result;
3887 }
3888 return make_typename_type (parser->scope, id, typename_type, tf_error);
3889 }
3890
3891 /* This is a wrapper around the
3892 make_{pointer,ptrmem,reference}_declarator functions that decides
3893 which one to call based on the CODE and CLASS_TYPE arguments. The
3894 CODE argument should be one of the values returned by
3895 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3896 appertain to the pointer or reference. */
3897
3898 static cp_declarator *
3899 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3900 cp_cv_quals cv_qualifiers,
3901 cp_declarator *target,
3902 tree attributes)
3903 {
3904 if (code == ERROR_MARK || target == cp_error_declarator)
3905 return cp_error_declarator;
3906
3907 if (code == INDIRECT_REF)
3908 if (class_type == NULL_TREE)
3909 return make_pointer_declarator (cv_qualifiers, target, attributes);
3910 else
3911 return make_ptrmem_declarator (cv_qualifiers, class_type,
3912 target, attributes);
3913 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3914 return make_reference_declarator (cv_qualifiers, target,
3915 false, attributes);
3916 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3917 return make_reference_declarator (cv_qualifiers, target,
3918 true, attributes);
3919 gcc_unreachable ();
3920 }
3921
3922 /* Create a new C++ parser. */
3923
3924 static cp_parser *
3925 cp_parser_new (void)
3926 {
3927 cp_parser *parser;
3928 cp_lexer *lexer;
3929 unsigned i;
3930
3931 /* cp_lexer_new_main is called before doing GC allocation because
3932 cp_lexer_new_main might load a PCH file. */
3933 lexer = cp_lexer_new_main ();
3934
3935 /* Initialize the binops_by_token so that we can get the tree
3936 directly from the token. */
3937 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3938 binops_by_token[binops[i].token_type] = binops[i];
3939
3940 parser = ggc_cleared_alloc<cp_parser> ();
3941 parser->lexer = lexer;
3942 parser->context = cp_parser_context_new (NULL);
3943
3944 /* For now, we always accept GNU extensions. */
3945 parser->allow_gnu_extensions_p = 1;
3946
3947 /* The `>' token is a greater-than operator, not the end of a
3948 template-id. */
3949 parser->greater_than_is_operator_p = true;
3950
3951 parser->default_arg_ok_p = true;
3952
3953 /* We are not parsing a constant-expression. */
3954 parser->integral_constant_expression_p = false;
3955 parser->allow_non_integral_constant_expression_p = false;
3956 parser->non_integral_constant_expression_p = false;
3957
3958 /* Local variable names are not forbidden. */
3959 parser->local_variables_forbidden_p = 0;
3960
3961 /* We are not processing an `extern "C"' declaration. */
3962 parser->in_unbraced_linkage_specification_p = false;
3963
3964 /* We are not processing a declarator. */
3965 parser->in_declarator_p = false;
3966
3967 /* We are not processing a template-argument-list. */
3968 parser->in_template_argument_list_p = false;
3969
3970 /* We are not in an iteration statement. */
3971 parser->in_statement = 0;
3972
3973 /* We are not in a switch statement. */
3974 parser->in_switch_statement_p = false;
3975
3976 /* We are not parsing a type-id inside an expression. */
3977 parser->in_type_id_in_expr_p = false;
3978
3979 /* String literals should be translated to the execution character set. */
3980 parser->translate_strings_p = true;
3981
3982 /* We are not parsing a function body. */
3983 parser->in_function_body = false;
3984
3985 /* We can correct until told otherwise. */
3986 parser->colon_corrects_to_scope_p = true;
3987
3988 /* The unparsed function queue is empty. */
3989 push_unparsed_function_queues (parser);
3990
3991 /* There are no classes being defined. */
3992 parser->num_classes_being_defined = 0;
3993
3994 /* No template parameters apply. */
3995 parser->num_template_parameter_lists = 0;
3996
3997 /* Special parsing data structures. */
3998 parser->omp_declare_simd = NULL;
3999 parser->oacc_routine = NULL;
4000
4001 /* Not declaring an implicit function template. */
4002 parser->auto_is_implicit_function_template_parm_p = false;
4003 parser->fully_implicit_function_template_p = false;
4004 parser->implicit_template_parms = 0;
4005 parser->implicit_template_scope = 0;
4006
4007 /* Allow constrained-type-specifiers. */
4008 parser->prevent_constrained_type_specifiers = 0;
4009
4010 /* We haven't yet seen an 'extern "C"'. */
4011 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4012
4013 return parser;
4014 }
4015
4016 /* Create a cp_lexer structure which will emit the tokens in CACHE
4017 and push it onto the parser's lexer stack. This is used for delayed
4018 parsing of in-class method bodies and default arguments, and should
4019 not be confused with tentative parsing. */
4020 static void
4021 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4022 {
4023 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4024 lexer->next = parser->lexer;
4025 parser->lexer = lexer;
4026
4027 /* Move the current source position to that of the first token in the
4028 new lexer. */
4029 cp_lexer_set_source_position_from_token (lexer->next_token);
4030 }
4031
4032 /* Pop the top lexer off the parser stack. This is never used for the
4033 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4034 static void
4035 cp_parser_pop_lexer (cp_parser *parser)
4036 {
4037 cp_lexer *lexer = parser->lexer;
4038 parser->lexer = lexer->next;
4039 cp_lexer_destroy (lexer);
4040
4041 /* Put the current source position back where it was before this
4042 lexer was pushed. */
4043 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4044 }
4045
4046 /* Lexical conventions [gram.lex] */
4047
4048 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4049 identifier. */
4050
4051 static cp_expr
4052 cp_parser_identifier (cp_parser* parser)
4053 {
4054 cp_token *token;
4055
4056 /* Look for the identifier. */
4057 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4058 /* Return the value. */
4059 if (token)
4060 return cp_expr (token->u.value, token->location);
4061 else
4062 return error_mark_node;
4063 }
4064
4065 /* Parse a sequence of adjacent string constants. Returns a
4066 TREE_STRING representing the combined, nul-terminated string
4067 constant. If TRANSLATE is true, translate the string to the
4068 execution character set. If WIDE_OK is true, a wide string is
4069 invalid here.
4070
4071 C++98 [lex.string] says that if a narrow string literal token is
4072 adjacent to a wide string literal token, the behavior is undefined.
4073 However, C99 6.4.5p4 says that this results in a wide string literal.
4074 We follow C99 here, for consistency with the C front end.
4075
4076 This code is largely lifted from lex_string() in c-lex.c.
4077
4078 FUTURE: ObjC++ will need to handle @-strings here. */
4079 static cp_expr
4080 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4081 bool lookup_udlit = true)
4082 {
4083 tree value;
4084 size_t count;
4085 struct obstack str_ob;
4086 struct obstack loc_ob;
4087 cpp_string str, istr, *strs;
4088 cp_token *tok;
4089 enum cpp_ttype type, curr_type;
4090 int have_suffix_p = 0;
4091 tree string_tree;
4092 tree suffix_id = NULL_TREE;
4093 bool curr_tok_is_userdef_p = false;
4094
4095 tok = cp_lexer_peek_token (parser->lexer);
4096 if (!cp_parser_is_string_literal (tok))
4097 {
4098 cp_parser_error (parser, "expected string-literal");
4099 return error_mark_node;
4100 }
4101
4102 location_t loc = tok->location;
4103
4104 if (cpp_userdef_string_p (tok->type))
4105 {
4106 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4107 curr_type = cpp_userdef_string_remove_type (tok->type);
4108 curr_tok_is_userdef_p = true;
4109 }
4110 else
4111 {
4112 string_tree = tok->u.value;
4113 curr_type = tok->type;
4114 }
4115 type = curr_type;
4116
4117 /* Try to avoid the overhead of creating and destroying an obstack
4118 for the common case of just one string. */
4119 if (!cp_parser_is_string_literal
4120 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4121 {
4122 cp_lexer_consume_token (parser->lexer);
4123
4124 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4125 str.len = TREE_STRING_LENGTH (string_tree);
4126 count = 1;
4127
4128 if (curr_tok_is_userdef_p)
4129 {
4130 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4131 have_suffix_p = 1;
4132 curr_type = cpp_userdef_string_remove_type (tok->type);
4133 }
4134 else
4135 curr_type = tok->type;
4136
4137 strs = &str;
4138 }
4139 else
4140 {
4141 location_t last_tok_loc = tok->location;
4142 gcc_obstack_init (&str_ob);
4143 gcc_obstack_init (&loc_ob);
4144 count = 0;
4145
4146 do
4147 {
4148 cp_lexer_consume_token (parser->lexer);
4149 count++;
4150 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4151 str.len = TREE_STRING_LENGTH (string_tree);
4152
4153 if (curr_tok_is_userdef_p)
4154 {
4155 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4156 if (have_suffix_p == 0)
4157 {
4158 suffix_id = curr_suffix_id;
4159 have_suffix_p = 1;
4160 }
4161 else if (have_suffix_p == 1
4162 && curr_suffix_id != suffix_id)
4163 {
4164 error ("inconsistent user-defined literal suffixes"
4165 " %qD and %qD in string literal",
4166 suffix_id, curr_suffix_id);
4167 have_suffix_p = -1;
4168 }
4169 curr_type = cpp_userdef_string_remove_type (tok->type);
4170 }
4171 else
4172 curr_type = tok->type;
4173
4174 if (type != curr_type)
4175 {
4176 if (type == CPP_STRING)
4177 type = curr_type;
4178 else if (curr_type != CPP_STRING)
4179 {
4180 rich_location rich_loc (line_table, tok->location);
4181 rich_loc.add_range (last_tok_loc);
4182 error_at (&rich_loc,
4183 "unsupported non-standard concatenation "
4184 "of string literals");
4185 }
4186 }
4187
4188 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4189 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4190
4191 last_tok_loc = tok->location;
4192
4193 tok = cp_lexer_peek_token (parser->lexer);
4194 if (cpp_userdef_string_p (tok->type))
4195 {
4196 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4197 curr_type = cpp_userdef_string_remove_type (tok->type);
4198 curr_tok_is_userdef_p = true;
4199 }
4200 else
4201 {
4202 string_tree = tok->u.value;
4203 curr_type = tok->type;
4204 curr_tok_is_userdef_p = false;
4205 }
4206 }
4207 while (cp_parser_is_string_literal (tok));
4208
4209 /* A string literal built by concatenation has its caret=start at
4210 the start of the initial string, and its finish at the finish of
4211 the final string literal. */
4212 loc = make_location (loc, loc, get_finish (last_tok_loc));
4213
4214 strs = (cpp_string *) obstack_finish (&str_ob);
4215 }
4216
4217 if (type != CPP_STRING && !wide_ok)
4218 {
4219 cp_parser_error (parser, "a wide string is invalid in this context");
4220 type = CPP_STRING;
4221 }
4222
4223 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4224 (parse_in, strs, count, &istr, type))
4225 {
4226 value = build_string (istr.len, (const char *)istr.text);
4227 free (CONST_CAST (unsigned char *, istr.text));
4228 if (count > 1)
4229 {
4230 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4231 gcc_assert (g_string_concat_db);
4232 g_string_concat_db->record_string_concatenation (count, locs);
4233 }
4234
4235 switch (type)
4236 {
4237 default:
4238 case CPP_STRING:
4239 TREE_TYPE (value) = char_array_type_node;
4240 break;
4241 case CPP_UTF8STRING:
4242 if (flag_char8_t)
4243 TREE_TYPE (value) = char8_array_type_node;
4244 else
4245 TREE_TYPE (value) = char_array_type_node;
4246 break;
4247 case CPP_STRING16:
4248 TREE_TYPE (value) = char16_array_type_node;
4249 break;
4250 case CPP_STRING32:
4251 TREE_TYPE (value) = char32_array_type_node;
4252 break;
4253 case CPP_WSTRING:
4254 TREE_TYPE (value) = wchar_array_type_node;
4255 break;
4256 }
4257
4258 value = fix_string_type (value);
4259
4260 if (have_suffix_p)
4261 {
4262 tree literal = build_userdef_literal (suffix_id, value,
4263 OT_NONE, NULL_TREE);
4264 if (lookup_udlit)
4265 value = cp_parser_userdef_string_literal (literal);
4266 else
4267 value = literal;
4268 }
4269 }
4270 else
4271 /* cpp_interpret_string has issued an error. */
4272 value = error_mark_node;
4273
4274 if (count > 1)
4275 {
4276 obstack_free (&str_ob, 0);
4277 obstack_free (&loc_ob, 0);
4278 }
4279
4280 return cp_expr (value, loc);
4281 }
4282
4283 /* Look up a literal operator with the name and the exact arguments. */
4284
4285 static tree
4286 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4287 {
4288 tree decl = lookup_name (name);
4289 if (!decl || !is_overloaded_fn (decl))
4290 return error_mark_node;
4291
4292 for (lkp_iterator iter (decl); iter; ++iter)
4293 {
4294 tree fn = *iter;
4295
4296 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4297 {
4298 unsigned int ix;
4299 bool found = true;
4300
4301 for (ix = 0;
4302 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4303 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4304 {
4305 tree tparm = TREE_VALUE (parmtypes);
4306 tree targ = TREE_TYPE ((*args)[ix]);
4307 bool ptr = TYPE_PTR_P (tparm);
4308 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4309 if ((ptr || arr || !same_type_p (tparm, targ))
4310 && (!ptr || !arr
4311 || !same_type_p (TREE_TYPE (tparm),
4312 TREE_TYPE (targ))))
4313 found = false;
4314 }
4315
4316 if (found
4317 && ix == vec_safe_length (args)
4318 /* May be this should be sufficient_parms_p instead,
4319 depending on how exactly should user-defined literals
4320 work in presence of default arguments on the literal
4321 operator parameters. */
4322 && parmtypes == void_list_node)
4323 return decl;
4324 }
4325 }
4326
4327 return error_mark_node;
4328 }
4329
4330 /* Parse a user-defined char constant. Returns a call to a user-defined
4331 literal operator taking the character as an argument. */
4332
4333 static cp_expr
4334 cp_parser_userdef_char_literal (cp_parser *parser)
4335 {
4336 cp_token *token = cp_lexer_consume_token (parser->lexer);
4337 tree literal = token->u.value;
4338 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4339 tree value = USERDEF_LITERAL_VALUE (literal);
4340 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4341 tree decl, result;
4342
4343 /* Build up a call to the user-defined operator */
4344 /* Lookup the name we got back from the id-expression. */
4345 vec<tree, va_gc> *args = make_tree_vector ();
4346 vec_safe_push (args, value);
4347 decl = lookup_literal_operator (name, args);
4348 if (!decl || decl == error_mark_node)
4349 {
4350 error ("unable to find character literal operator %qD with %qT argument",
4351 name, TREE_TYPE (value));
4352 release_tree_vector (args);
4353 return error_mark_node;
4354 }
4355 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4356 release_tree_vector (args);
4357 return result;
4358 }
4359
4360 /* A subroutine of cp_parser_userdef_numeric_literal to
4361 create a char... template parameter pack from a string node. */
4362
4363 static tree
4364 make_char_string_pack (tree value)
4365 {
4366 tree charvec;
4367 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4368 const char *str = TREE_STRING_POINTER (value);
4369 int i, len = TREE_STRING_LENGTH (value) - 1;
4370 tree argvec = make_tree_vec (1);
4371
4372 /* Fill in CHARVEC with all of the parameters. */
4373 charvec = make_tree_vec (len);
4374 for (i = 0; i < len; ++i)
4375 {
4376 unsigned char s[3] = { '\'', str[i], '\'' };
4377 cpp_string in = { 3, s };
4378 cpp_string out = { 0, 0 };
4379 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4380 return NULL_TREE;
4381 gcc_assert (out.len == 2);
4382 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4383 out.text[0]);
4384 }
4385
4386 /* Build the argument packs. */
4387 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4388
4389 TREE_VEC_ELT (argvec, 0) = argpack;
4390
4391 return argvec;
4392 }
4393
4394 /* A subroutine of cp_parser_userdef_numeric_literal to
4395 create a char... template parameter pack from a string node. */
4396
4397 static tree
4398 make_string_pack (tree value)
4399 {
4400 tree charvec;
4401 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4402 const unsigned char *str
4403 = (const unsigned char *) TREE_STRING_POINTER (value);
4404 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4405 int len = TREE_STRING_LENGTH (value) / sz - 1;
4406 tree argvec = make_tree_vec (2);
4407
4408 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4409 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4410
4411 /* First template parm is character type. */
4412 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4413
4414 /* Fill in CHARVEC with all of the parameters. */
4415 charvec = make_tree_vec (len);
4416 for (int i = 0; i < len; ++i)
4417 TREE_VEC_ELT (charvec, i)
4418 = double_int_to_tree (str_char_type_node,
4419 double_int::from_buffer (str + i * sz, sz));
4420
4421 /* Build the argument packs. */
4422 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4423
4424 TREE_VEC_ELT (argvec, 1) = argpack;
4425
4426 return argvec;
4427 }
4428
4429 /* Parse a user-defined numeric constant. returns a call to a user-defined
4430 literal operator. */
4431
4432 static cp_expr
4433 cp_parser_userdef_numeric_literal (cp_parser *parser)
4434 {
4435 cp_token *token = cp_lexer_consume_token (parser->lexer);
4436 tree literal = token->u.value;
4437 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4438 tree value = USERDEF_LITERAL_VALUE (literal);
4439 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4440 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4441 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4442 tree decl, result;
4443 vec<tree, va_gc> *args;
4444
4445 /* Look for a literal operator taking the exact type of numeric argument
4446 as the literal value. */
4447 args = make_tree_vector ();
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 release_tree_vector (args);
4473 return result;
4474 }
4475 release_tree_vector (args);
4476
4477 /* If the numeric argument didn't work, look for a raw literal
4478 operator taking a const char* argument consisting of the number
4479 in string format. */
4480 args = make_tree_vector ();
4481 vec_safe_push (args, num_string);
4482 decl = lookup_literal_operator (name, args);
4483 if (decl && decl != error_mark_node)
4484 {
4485 result = finish_call_expr (decl, &args, false, true,
4486 tf_warning_or_error);
4487 release_tree_vector (args);
4488 return result;
4489 }
4490 release_tree_vector (args);
4491
4492 /* If the raw literal didn't work, look for a non-type template
4493 function with parameter pack char.... Call the function with
4494 template parameter characters representing the number. */
4495 args = make_tree_vector ();
4496 decl = lookup_literal_operator (name, args);
4497 if (decl && decl != error_mark_node)
4498 {
4499 tree tmpl_args = make_char_string_pack (num_string);
4500 if (tmpl_args == NULL_TREE)
4501 {
4502 error ("failed to translate literal to execution character set %qT",
4503 num_string);
4504 return error_mark_node;
4505 }
4506 decl = lookup_template_function (decl, tmpl_args);
4507 result = finish_call_expr (decl, &args, false, true,
4508 tf_warning_or_error);
4509 release_tree_vector (args);
4510 return result;
4511 }
4512
4513 release_tree_vector (args);
4514
4515 /* In C++14 the standard library defines complex number suffixes that
4516 conflict with GNU extensions. Prefer them if <complex> is #included. */
4517 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4518 bool i14 = (cxx_dialect > cxx11
4519 && (id_equal (suffix_id, "i")
4520 || id_equal (suffix_id, "if")
4521 || id_equal (suffix_id, "il")));
4522 diagnostic_t kind = DK_ERROR;
4523 int opt = 0;
4524
4525 if (i14 && ext)
4526 {
4527 tree cxlit = lookup_qualified_name (std_node,
4528 get_identifier ("complex_literals"),
4529 0, false, false);
4530 if (cxlit == error_mark_node)
4531 {
4532 /* No <complex>, so pedwarn and use GNU semantics. */
4533 kind = DK_PEDWARN;
4534 opt = OPT_Wpedantic;
4535 }
4536 }
4537
4538 bool complained
4539 = emit_diagnostic (kind, input_location, opt,
4540 "unable to find numeric literal operator %qD", name);
4541
4542 if (!complained)
4543 /* Don't inform either. */;
4544 else if (i14)
4545 {
4546 inform (token->location, "add %<using namespace std::complex_literals%> "
4547 "(from <complex>) to enable the C++14 user-defined literal "
4548 "suffixes");
4549 if (ext)
4550 inform (token->location, "or use %<j%> instead of %<i%> for the "
4551 "GNU built-in suffix");
4552 }
4553 else if (!ext)
4554 inform (token->location, "use %<-fext-numeric-literals%> "
4555 "to enable more built-in suffixes");
4556
4557 if (kind == DK_ERROR)
4558 value = error_mark_node;
4559 else
4560 {
4561 /* Use the built-in semantics. */
4562 tree type;
4563 if (id_equal (suffix_id, "i"))
4564 {
4565 if (TREE_CODE (value) == INTEGER_CST)
4566 type = integer_type_node;
4567 else
4568 type = double_type_node;
4569 }
4570 else if (id_equal (suffix_id, "if"))
4571 type = float_type_node;
4572 else /* if (id_equal (suffix_id, "il")) */
4573 type = long_double_type_node;
4574
4575 value = build_complex (build_complex_type (type),
4576 fold_convert (type, integer_zero_node),
4577 fold_convert (type, value));
4578 }
4579
4580 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4581 /* Avoid repeated diagnostics. */
4582 token->u.value = value;
4583 return value;
4584 }
4585
4586 /* Parse a user-defined string constant. Returns a call to a user-defined
4587 literal operator taking a character pointer and the length of the string
4588 as arguments. */
4589
4590 static tree
4591 cp_parser_userdef_string_literal (tree literal)
4592 {
4593 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4594 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4595 tree value = USERDEF_LITERAL_VALUE (literal);
4596 int len = TREE_STRING_LENGTH (value)
4597 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4598 tree decl;
4599
4600 /* Build up a call to the user-defined operator. */
4601 /* Lookup the name we got back from the id-expression. */
4602 releasing_vec rargs;
4603 vec<tree, va_gc> *&args = rargs.get_ref();
4604 vec_safe_push (args, value);
4605 vec_safe_push (args, build_int_cst (size_type_node, len));
4606 decl = lookup_literal_operator (name, args);
4607
4608 if (decl && decl != error_mark_node)
4609 return finish_call_expr (decl, &args, false, true,
4610 tf_warning_or_error);
4611
4612 /* Look for a suitable template function, either (C++20) with a single
4613 parameter of class type, or (N3599) with typename parameter CharT and
4614 parameter pack CharT... */
4615 args->truncate (0);
4616 decl = lookup_literal_operator (name, args);
4617 if (decl && decl != error_mark_node)
4618 {
4619 /* Use resolve_nondeduced_context to try to choose one form of template
4620 or the other. */
4621 tree tmpl_args = make_tree_vec (1);
4622 TREE_VEC_ELT (tmpl_args, 0) = value;
4623 decl = lookup_template_function (decl, tmpl_args);
4624 tree res = resolve_nondeduced_context (decl, tf_none);
4625 if (DECL_P (res))
4626 decl = res;
4627 else
4628 {
4629 TREE_OPERAND (decl, 1) = make_string_pack (value);
4630 res = resolve_nondeduced_context (decl, tf_none);
4631 if (DECL_P (res))
4632 decl = res;
4633 }
4634 if (!DECL_P (decl) && cxx_dialect > cxx17)
4635 TREE_OPERAND (decl, 1) = tmpl_args;
4636 return finish_call_expr (decl, &args, false, true,
4637 tf_warning_or_error);
4638 }
4639
4640 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4641 name, TREE_TYPE (value), size_type_node);
4642 return error_mark_node;
4643 }
4644
4645
4646 /* Basic concepts [gram.basic] */
4647
4648 /* Parse a translation-unit.
4649
4650 translation-unit:
4651 declaration-seq [opt] */
4652
4653 static void
4654 cp_parser_translation_unit (cp_parser* parser)
4655 {
4656 gcc_checking_assert (!cp_error_declarator);
4657
4658 /* Create the declarator obstack. */
4659 gcc_obstack_init (&declarator_obstack);
4660 /* Create the error declarator. */
4661 cp_error_declarator = make_declarator (cdk_error);
4662 /* Create the empty parameter list. */
4663 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4664 UNKNOWN_LOCATION);
4665 /* Remember where the base of the declarator obstack lies. */
4666 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4667
4668 bool implicit_extern_c = false;
4669
4670 for (;;)
4671 {
4672 cp_token *token = cp_lexer_peek_token (parser->lexer);
4673
4674 /* If we're entering or exiting a region that's implicitly
4675 extern "C", modify the lang context appropriately. */
4676 if (implicit_extern_c
4677 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4678 {
4679 implicit_extern_c = !implicit_extern_c;
4680 if (implicit_extern_c)
4681 push_lang_context (lang_name_c);
4682 else
4683 pop_lang_context ();
4684 }
4685
4686 if (token->type == CPP_EOF)
4687 break;
4688
4689 if (token->type == CPP_CLOSE_BRACE)
4690 {
4691 cp_parser_error (parser, "expected declaration");
4692 cp_lexer_consume_token (parser->lexer);
4693 /* If the next token is now a `;', consume it. */
4694 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4695 cp_lexer_consume_token (parser->lexer);
4696 }
4697 else
4698 cp_parser_toplevel_declaration (parser);
4699 }
4700
4701 /* Get rid of the token array; we don't need it any more. */
4702 cp_lexer_destroy (parser->lexer);
4703 parser->lexer = NULL;
4704
4705 /* The EOF should have reset this. */
4706 gcc_checking_assert (!implicit_extern_c);
4707
4708 /* Make sure the declarator obstack was fully cleaned up. */
4709 gcc_assert (obstack_next_free (&declarator_obstack)
4710 == declarator_obstack_base);
4711 }
4712
4713 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4714 decltype context. */
4715
4716 static inline tsubst_flags_t
4717 complain_flags (bool decltype_p)
4718 {
4719 tsubst_flags_t complain = tf_warning_or_error;
4720 if (decltype_p)
4721 complain |= tf_decltype;
4722 return complain;
4723 }
4724
4725 /* We're about to parse a collection of statements. If we're currently
4726 parsing tentatively, set up a firewall so that any nested
4727 cp_parser_commit_to_tentative_parse won't affect the current context. */
4728
4729 static cp_token_position
4730 cp_parser_start_tentative_firewall (cp_parser *parser)
4731 {
4732 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4733 return 0;
4734
4735 cp_parser_parse_tentatively (parser);
4736 cp_parser_commit_to_topmost_tentative_parse (parser);
4737 return cp_lexer_token_position (parser->lexer, false);
4738 }
4739
4740 /* We've finished parsing the collection of statements. Wrap up the
4741 firewall and replace the relevant tokens with the parsed form. */
4742
4743 static void
4744 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4745 tree expr)
4746 {
4747 if (!start)
4748 return;
4749
4750 /* Finish the firewall level. */
4751 cp_parser_parse_definitely (parser);
4752 /* And remember the result of the parse for when we try again. */
4753 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4754 token->type = CPP_PREPARSED_EXPR;
4755 token->u.value = expr;
4756 token->keyword = RID_MAX;
4757 cp_lexer_purge_tokens_after (parser->lexer, start);
4758 }
4759
4760 /* Like the above functions, but let the user modify the tokens. Used by
4761 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4762 later parses, so it makes sense to localize the effects of
4763 cp_parser_commit_to_tentative_parse. */
4764
4765 struct tentative_firewall
4766 {
4767 cp_parser *parser;
4768 bool set;
4769
4770 tentative_firewall (cp_parser *p): parser(p)
4771 {
4772 /* If we're currently parsing tentatively, start a committed level as a
4773 firewall and then an inner tentative parse. */
4774 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4775 {
4776 cp_parser_parse_tentatively (parser);
4777 cp_parser_commit_to_topmost_tentative_parse (parser);
4778 cp_parser_parse_tentatively (parser);
4779 }
4780 }
4781
4782 ~tentative_firewall()
4783 {
4784 if (set)
4785 {
4786 /* Finish the inner tentative parse and the firewall, propagating any
4787 uncommitted error state to the outer tentative parse. */
4788 bool err = cp_parser_error_occurred (parser);
4789 cp_parser_parse_definitely (parser);
4790 cp_parser_parse_definitely (parser);
4791 if (err)
4792 cp_parser_simulate_error (parser);
4793 }
4794 }
4795 };
4796
4797 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4798 This class is for tracking such a matching pair of symbols.
4799 In particular, it tracks the location of the first token,
4800 so that if the second token is missing, we can highlight the
4801 location of the first token when notifying the user about the
4802 problem. */
4803
4804 template <typename traits_t>
4805 class token_pair
4806 {
4807 public:
4808 /* token_pair's ctor. */
4809 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4810
4811 /* If the next token is the opening symbol for this pair, consume it and
4812 return true.
4813 Otherwise, issue an error and return false.
4814 In either case, record the location of the opening token. */
4815
4816 bool require_open (cp_parser *parser)
4817 {
4818 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4819 return cp_parser_require (parser, traits_t::open_token_type,
4820 traits_t::required_token_open);
4821 }
4822
4823 /* Consume the next token from PARSER, recording its location as
4824 that of the opening token within the pair. */
4825
4826 cp_token * consume_open (cp_parser *parser)
4827 {
4828 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4829 gcc_assert (tok->type == traits_t::open_token_type);
4830 m_open_loc = tok->location;
4831 return tok;
4832 }
4833
4834 /* If the next token is the closing symbol for this pair, consume it
4835 and return it.
4836 Otherwise, issue an error, highlighting the location of the
4837 corresponding opening token, and return NULL. */
4838
4839 cp_token *require_close (cp_parser *parser) const
4840 {
4841 return cp_parser_require (parser, traits_t::close_token_type,
4842 traits_t::required_token_close,
4843 m_open_loc);
4844 }
4845
4846 private:
4847 location_t m_open_loc;
4848 };
4849
4850 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4851
4852 struct matching_paren_traits
4853 {
4854 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4855 static const enum required_token required_token_open = RT_OPEN_PAREN;
4856 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4857 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4858 };
4859
4860 /* "matching_parens" is a token_pair<T> class for tracking matching
4861 pairs of parentheses. */
4862
4863 typedef token_pair<matching_paren_traits> matching_parens;
4864
4865 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4866
4867 struct matching_brace_traits
4868 {
4869 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4870 static const enum required_token required_token_open = RT_OPEN_BRACE;
4871 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4872 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4873 };
4874
4875 /* "matching_braces" is a token_pair<T> class for tracking matching
4876 pairs of braces. */
4877
4878 typedef token_pair<matching_brace_traits> matching_braces;
4879
4880
4881 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4882 enclosing parentheses. */
4883
4884 static cp_expr
4885 cp_parser_statement_expr (cp_parser *parser)
4886 {
4887 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4888
4889 /* Consume the '('. */
4890 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4891 matching_parens parens;
4892 parens.consume_open (parser);
4893 /* Start the statement-expression. */
4894 tree expr = begin_stmt_expr ();
4895 /* Parse the compound-statement. */
4896 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4897 /* Finish up. */
4898 expr = finish_stmt_expr (expr, false);
4899 /* Consume the ')'. */
4900 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4901 if (!parens.require_close (parser))
4902 cp_parser_skip_to_end_of_statement (parser);
4903
4904 cp_parser_end_tentative_firewall (parser, start, expr);
4905 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4906 return cp_expr (expr, combined_loc);
4907 }
4908
4909 /* Expressions [gram.expr] */
4910
4911 /* Parse a fold-operator.
4912
4913 fold-operator:
4914 - * / % ^ & | = < > << >>
4915 = -= *= /= %= ^= &= |= <<= >>=
4916 == != <= >= && || , .* ->*
4917
4918 This returns the tree code corresponding to the matched operator
4919 as an int. When the current token matches a compound assignment
4920 opertor, the resulting tree code is the negative value of the
4921 non-assignment operator. */
4922
4923 static int
4924 cp_parser_fold_operator (cp_token *token)
4925 {
4926 switch (token->type)
4927 {
4928 case CPP_PLUS: return PLUS_EXPR;
4929 case CPP_MINUS: return MINUS_EXPR;
4930 case CPP_MULT: return MULT_EXPR;
4931 case CPP_DIV: return TRUNC_DIV_EXPR;
4932 case CPP_MOD: return TRUNC_MOD_EXPR;
4933 case CPP_XOR: return BIT_XOR_EXPR;
4934 case CPP_AND: return BIT_AND_EXPR;
4935 case CPP_OR: return BIT_IOR_EXPR;
4936 case CPP_LSHIFT: return LSHIFT_EXPR;
4937 case CPP_RSHIFT: return RSHIFT_EXPR;
4938
4939 case CPP_EQ: return -NOP_EXPR;
4940 case CPP_PLUS_EQ: return -PLUS_EXPR;
4941 case CPP_MINUS_EQ: return -MINUS_EXPR;
4942 case CPP_MULT_EQ: return -MULT_EXPR;
4943 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4944 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4945 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4946 case CPP_AND_EQ: return -BIT_AND_EXPR;
4947 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4948 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4949 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4950
4951 case CPP_EQ_EQ: return EQ_EXPR;
4952 case CPP_NOT_EQ: return NE_EXPR;
4953 case CPP_LESS: return LT_EXPR;
4954 case CPP_GREATER: return GT_EXPR;
4955 case CPP_LESS_EQ: return LE_EXPR;
4956 case CPP_GREATER_EQ: return GE_EXPR;
4957
4958 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4959 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4960
4961 case CPP_COMMA: return COMPOUND_EXPR;
4962
4963 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4964 case CPP_DEREF_STAR: return MEMBER_REF;
4965
4966 default: return ERROR_MARK;
4967 }
4968 }
4969
4970 /* Returns true if CODE indicates a binary expression, which is not allowed in
4971 the LHS of a fold-expression. More codes will need to be added to use this
4972 function in other contexts. */
4973
4974 static bool
4975 is_binary_op (tree_code code)
4976 {
4977 switch (code)
4978 {
4979 case PLUS_EXPR:
4980 case POINTER_PLUS_EXPR:
4981 case MINUS_EXPR:
4982 case MULT_EXPR:
4983 case TRUNC_DIV_EXPR:
4984 case TRUNC_MOD_EXPR:
4985 case BIT_XOR_EXPR:
4986 case BIT_AND_EXPR:
4987 case BIT_IOR_EXPR:
4988 case LSHIFT_EXPR:
4989 case RSHIFT_EXPR:
4990
4991 case MODOP_EXPR:
4992
4993 case EQ_EXPR:
4994 case NE_EXPR:
4995 case LE_EXPR:
4996 case GE_EXPR:
4997 case LT_EXPR:
4998 case GT_EXPR:
4999
5000 case TRUTH_ANDIF_EXPR:
5001 case TRUTH_ORIF_EXPR:
5002
5003 case COMPOUND_EXPR:
5004
5005 case DOTSTAR_EXPR:
5006 case MEMBER_REF:
5007 return true;
5008
5009 default:
5010 return false;
5011 }
5012 }
5013
5014 /* If the next token is a suitable fold operator, consume it and return as
5015 the function above. */
5016
5017 static int
5018 cp_parser_fold_operator (cp_parser *parser)
5019 {
5020 cp_token* token = cp_lexer_peek_token (parser->lexer);
5021 int code = cp_parser_fold_operator (token);
5022 if (code != ERROR_MARK)
5023 cp_lexer_consume_token (parser->lexer);
5024 return code;
5025 }
5026
5027 /* Parse a fold-expression.
5028
5029 fold-expression:
5030 ( ... folding-operator cast-expression)
5031 ( cast-expression folding-operator ... )
5032 ( cast-expression folding operator ... folding-operator cast-expression)
5033
5034 Note that the '(' and ')' are matched in primary expression. */
5035
5036 static cp_expr
5037 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5038 {
5039 cp_id_kind pidk;
5040
5041 // Left fold.
5042 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5043 {
5044 cp_lexer_consume_token (parser->lexer);
5045 int op = cp_parser_fold_operator (parser);
5046 if (op == ERROR_MARK)
5047 {
5048 cp_parser_error (parser, "expected binary operator");
5049 return error_mark_node;
5050 }
5051
5052 tree expr = cp_parser_cast_expression (parser, false, false,
5053 false, &pidk);
5054 if (expr == error_mark_node)
5055 return error_mark_node;
5056 return finish_left_unary_fold_expr (expr, op);
5057 }
5058
5059 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5060 int op = cp_parser_fold_operator (parser);
5061 if (op == ERROR_MARK)
5062 {
5063 cp_parser_error (parser, "expected binary operator");
5064 return error_mark_node;
5065 }
5066
5067 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5068 {
5069 cp_parser_error (parser, "expected ...");
5070 return error_mark_node;
5071 }
5072 cp_lexer_consume_token (parser->lexer);
5073
5074 /* The operands of a fold-expression are cast-expressions, so binary or
5075 conditional expressions are not allowed. We check this here to avoid
5076 tentative parsing. */
5077 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5078 /* OK, the expression was parenthesized. */;
5079 else if (is_binary_op (TREE_CODE (expr1)))
5080 error_at (location_of (expr1),
5081 "binary expression in operand of fold-expression");
5082 else if (TREE_CODE (expr1) == COND_EXPR
5083 || (REFERENCE_REF_P (expr1)
5084 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5085 error_at (location_of (expr1),
5086 "conditional expression in operand of fold-expression");
5087
5088 // Right fold.
5089 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5090 return finish_right_unary_fold_expr (expr1, op);
5091
5092 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5093 {
5094 cp_parser_error (parser, "mismatched operator in fold-expression");
5095 return error_mark_node;
5096 }
5097 cp_lexer_consume_token (parser->lexer);
5098
5099 // Binary left or right fold.
5100 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5101 if (expr2 == error_mark_node)
5102 return error_mark_node;
5103 return finish_binary_fold_expr (expr1, expr2, op);
5104 }
5105
5106 /* Parse a primary-expression.
5107
5108 primary-expression:
5109 literal
5110 this
5111 ( expression )
5112 id-expression
5113 lambda-expression (C++11)
5114
5115 GNU Extensions:
5116
5117 primary-expression:
5118 ( compound-statement )
5119 __builtin_va_arg ( assignment-expression , type-id )
5120 __builtin_offsetof ( type-id , offsetof-expression )
5121
5122 C++ Extensions:
5123 __has_nothrow_assign ( type-id )
5124 __has_nothrow_constructor ( type-id )
5125 __has_nothrow_copy ( type-id )
5126 __has_trivial_assign ( type-id )
5127 __has_trivial_constructor ( type-id )
5128 __has_trivial_copy ( type-id )
5129 __has_trivial_destructor ( type-id )
5130 __has_virtual_destructor ( type-id )
5131 __is_abstract ( type-id )
5132 __is_base_of ( type-id , type-id )
5133 __is_class ( type-id )
5134 __is_empty ( type-id )
5135 __is_enum ( type-id )
5136 __is_final ( type-id )
5137 __is_literal_type ( type-id )
5138 __is_pod ( type-id )
5139 __is_polymorphic ( type-id )
5140 __is_std_layout ( type-id )
5141 __is_trivial ( type-id )
5142 __is_union ( type-id )
5143
5144 Objective-C++ Extension:
5145
5146 primary-expression:
5147 objc-expression
5148
5149 literal:
5150 __null
5151
5152 ADDRESS_P is true iff this expression was immediately preceded by
5153 "&" and therefore might denote a pointer-to-member. CAST_P is true
5154 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5155 true iff this expression is a template argument.
5156
5157 Returns a representation of the expression. Upon return, *IDK
5158 indicates what kind of id-expression (if any) was present. */
5159
5160 static cp_expr
5161 cp_parser_primary_expression (cp_parser *parser,
5162 bool address_p,
5163 bool cast_p,
5164 bool template_arg_p,
5165 bool decltype_p,
5166 cp_id_kind *idk)
5167 {
5168 cp_token *token = NULL;
5169
5170 /* Assume the primary expression is not an id-expression. */
5171 *idk = CP_ID_KIND_NONE;
5172
5173 /* Peek at the next token. */
5174 token = cp_lexer_peek_token (parser->lexer);
5175 switch ((int) token->type)
5176 {
5177 /* literal:
5178 integer-literal
5179 character-literal
5180 floating-literal
5181 string-literal
5182 boolean-literal
5183 pointer-literal
5184 user-defined-literal */
5185 case CPP_CHAR:
5186 case CPP_CHAR16:
5187 case CPP_CHAR32:
5188 case CPP_WCHAR:
5189 case CPP_UTF8CHAR:
5190 case CPP_NUMBER:
5191 case CPP_PREPARSED_EXPR:
5192 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5193 return cp_parser_userdef_numeric_literal (parser);
5194 token = cp_lexer_consume_token (parser->lexer);
5195 if (TREE_CODE (token->u.value) == FIXED_CST)
5196 {
5197 error_at (token->location,
5198 "fixed-point types not supported in C++");
5199 return error_mark_node;
5200 }
5201 /* Floating-point literals are only allowed in an integral
5202 constant expression if they are cast to an integral or
5203 enumeration type. */
5204 if (TREE_CODE (token->u.value) == REAL_CST
5205 && parser->integral_constant_expression_p
5206 && pedantic)
5207 {
5208 /* CAST_P will be set even in invalid code like "int(2.7 +
5209 ...)". Therefore, we have to check that the next token
5210 is sure to end the cast. */
5211 if (cast_p)
5212 {
5213 cp_token *next_token;
5214
5215 next_token = cp_lexer_peek_token (parser->lexer);
5216 if (/* The comma at the end of an
5217 enumerator-definition. */
5218 next_token->type != CPP_COMMA
5219 /* The curly brace at the end of an enum-specifier. */
5220 && next_token->type != CPP_CLOSE_BRACE
5221 /* The end of a statement. */
5222 && next_token->type != CPP_SEMICOLON
5223 /* The end of the cast-expression. */
5224 && next_token->type != CPP_CLOSE_PAREN
5225 /* The end of an array bound. */
5226 && next_token->type != CPP_CLOSE_SQUARE
5227 /* The closing ">" in a template-argument-list. */
5228 && (next_token->type != CPP_GREATER
5229 || parser->greater_than_is_operator_p)
5230 /* C++0x only: A ">>" treated like two ">" tokens,
5231 in a template-argument-list. */
5232 && (next_token->type != CPP_RSHIFT
5233 || (cxx_dialect == cxx98)
5234 || parser->greater_than_is_operator_p))
5235 cast_p = false;
5236 }
5237
5238 /* If we are within a cast, then the constraint that the
5239 cast is to an integral or enumeration type will be
5240 checked at that point. If we are not within a cast, then
5241 this code is invalid. */
5242 if (!cast_p)
5243 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5244 }
5245 return (cp_expr (token->u.value, token->location)
5246 .maybe_add_location_wrapper ());
5247
5248 case CPP_CHAR_USERDEF:
5249 case CPP_CHAR16_USERDEF:
5250 case CPP_CHAR32_USERDEF:
5251 case CPP_WCHAR_USERDEF:
5252 case CPP_UTF8CHAR_USERDEF:
5253 return cp_parser_userdef_char_literal (parser);
5254
5255 case CPP_STRING:
5256 case CPP_STRING16:
5257 case CPP_STRING32:
5258 case CPP_WSTRING:
5259 case CPP_UTF8STRING:
5260 case CPP_STRING_USERDEF:
5261 case CPP_STRING16_USERDEF:
5262 case CPP_STRING32_USERDEF:
5263 case CPP_WSTRING_USERDEF:
5264 case CPP_UTF8STRING_USERDEF:
5265 /* ??? Should wide strings be allowed when parser->translate_strings_p
5266 is false (i.e. in attributes)? If not, we can kill the third
5267 argument to cp_parser_string_literal. */
5268 return (cp_parser_string_literal (parser,
5269 parser->translate_strings_p,
5270 true)
5271 .maybe_add_location_wrapper ());
5272
5273 case CPP_OPEN_PAREN:
5274 /* If we see `( { ' then we are looking at the beginning of
5275 a GNU statement-expression. */
5276 if (cp_parser_allow_gnu_extensions_p (parser)
5277 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5278 {
5279 /* Statement-expressions are not allowed by the standard. */
5280 pedwarn (token->location, OPT_Wpedantic,
5281 "ISO C++ forbids braced-groups within expressions");
5282
5283 /* And they're not allowed outside of a function-body; you
5284 cannot, for example, write:
5285
5286 int i = ({ int j = 3; j + 1; });
5287
5288 at class or namespace scope. */
5289 if (!parser->in_function_body
5290 || parser->in_template_argument_list_p)
5291 {
5292 error_at (token->location,
5293 "statement-expressions are not allowed outside "
5294 "functions nor in template-argument lists");
5295 cp_parser_skip_to_end_of_block_or_statement (parser);
5296 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5297 cp_lexer_consume_token (parser->lexer);
5298 return error_mark_node;
5299 }
5300 else
5301 return cp_parser_statement_expr (parser);
5302 }
5303 /* Otherwise it's a normal parenthesized expression. */
5304 {
5305 cp_expr expr;
5306 bool saved_greater_than_is_operator_p;
5307
5308 location_t open_paren_loc = token->location;
5309
5310 /* Consume the `('. */
5311 matching_parens parens;
5312 parens.consume_open (parser);
5313 /* Within a parenthesized expression, a `>' token is always
5314 the greater-than operator. */
5315 saved_greater_than_is_operator_p
5316 = parser->greater_than_is_operator_p;
5317 parser->greater_than_is_operator_p = true;
5318
5319 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5320 /* Left fold expression. */
5321 expr = NULL_TREE;
5322 else
5323 /* Parse the parenthesized expression. */
5324 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5325
5326 token = cp_lexer_peek_token (parser->lexer);
5327 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5328 {
5329 expr = cp_parser_fold_expression (parser, expr);
5330 if (expr != error_mark_node
5331 && cxx_dialect < cxx17
5332 && !in_system_header_at (input_location))
5333 pedwarn (input_location, 0, "fold-expressions only available "
5334 "with %<-std=c++17%> or %<-std=gnu++17%>");
5335 }
5336 else
5337 /* Let the front end know that this expression was
5338 enclosed in parentheses. This matters in case, for
5339 example, the expression is of the form `A::B', since
5340 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5341 not. */
5342 expr = finish_parenthesized_expr (expr);
5343
5344 /* DR 705: Wrapping an unqualified name in parentheses
5345 suppresses arg-dependent lookup. We want to pass back
5346 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5347 (c++/37862), but none of the others. */
5348 if (*idk != CP_ID_KIND_QUALIFIED)
5349 *idk = CP_ID_KIND_NONE;
5350
5351 /* The `>' token might be the end of a template-id or
5352 template-parameter-list now. */
5353 parser->greater_than_is_operator_p
5354 = saved_greater_than_is_operator_p;
5355
5356 /* Consume the `)'. */
5357 token = cp_lexer_peek_token (parser->lexer);
5358 location_t close_paren_loc = token->location;
5359 expr.set_range (open_paren_loc, close_paren_loc);
5360 if (!parens.require_close (parser)
5361 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5362 cp_parser_skip_to_end_of_statement (parser);
5363
5364 return expr;
5365 }
5366
5367 case CPP_OPEN_SQUARE:
5368 {
5369 if (c_dialect_objc ())
5370 {
5371 /* We might have an Objective-C++ message. */
5372 cp_parser_parse_tentatively (parser);
5373 tree msg = cp_parser_objc_message_expression (parser);
5374 /* If that works out, we're done ... */
5375 if (cp_parser_parse_definitely (parser))
5376 return msg;
5377 /* ... else, fall though to see if it's a lambda. */
5378 }
5379 cp_expr lam = cp_parser_lambda_expression (parser);
5380 /* Don't warn about a failed tentative parse. */
5381 if (cp_parser_error_occurred (parser))
5382 return error_mark_node;
5383 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5384 return lam;
5385 }
5386
5387 case CPP_OBJC_STRING:
5388 if (c_dialect_objc ())
5389 /* We have an Objective-C++ string literal. */
5390 return cp_parser_objc_expression (parser);
5391 cp_parser_error (parser, "expected primary-expression");
5392 return error_mark_node;
5393
5394 case CPP_KEYWORD:
5395 switch (token->keyword)
5396 {
5397 /* These two are the boolean literals. */
5398 case RID_TRUE:
5399 cp_lexer_consume_token (parser->lexer);
5400 return cp_expr (boolean_true_node, token->location);
5401 case RID_FALSE:
5402 cp_lexer_consume_token (parser->lexer);
5403 return cp_expr (boolean_false_node, token->location);
5404
5405 /* The `__null' literal. */
5406 case RID_NULL:
5407 cp_lexer_consume_token (parser->lexer);
5408 return cp_expr (null_node, token->location);
5409
5410 /* The `nullptr' literal. */
5411 case RID_NULLPTR:
5412 cp_lexer_consume_token (parser->lexer);
5413 return cp_expr (nullptr_node, token->location);
5414
5415 /* Recognize the `this' keyword. */
5416 case RID_THIS:
5417 cp_lexer_consume_token (parser->lexer);
5418 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5419 {
5420 error_at (token->location,
5421 "%<this%> may not be used in this context");
5422 return error_mark_node;
5423 }
5424 /* Pointers cannot appear in constant-expressions. */
5425 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5426 return error_mark_node;
5427 return cp_expr (finish_this_expr (), token->location);
5428
5429 /* The `operator' keyword can be the beginning of an
5430 id-expression. */
5431 case RID_OPERATOR:
5432 goto id_expression;
5433
5434 case RID_FUNCTION_NAME:
5435 case RID_PRETTY_FUNCTION_NAME:
5436 case RID_C99_FUNCTION_NAME:
5437 {
5438 non_integral_constant name;
5439
5440 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5441 __func__ are the names of variables -- but they are
5442 treated specially. Therefore, they are handled here,
5443 rather than relying on the generic id-expression logic
5444 below. Grammatically, these names are id-expressions.
5445
5446 Consume the token. */
5447 token = cp_lexer_consume_token (parser->lexer);
5448
5449 switch (token->keyword)
5450 {
5451 case RID_FUNCTION_NAME:
5452 name = NIC_FUNC_NAME;
5453 break;
5454 case RID_PRETTY_FUNCTION_NAME:
5455 name = NIC_PRETTY_FUNC;
5456 break;
5457 case RID_C99_FUNCTION_NAME:
5458 name = NIC_C99_FUNC;
5459 break;
5460 default:
5461 gcc_unreachable ();
5462 }
5463
5464 if (cp_parser_non_integral_constant_expression (parser, name))
5465 return error_mark_node;
5466
5467 /* Look up the name. */
5468 return finish_fname (token->u.value);
5469 }
5470
5471 case RID_VA_ARG:
5472 {
5473 tree expression;
5474 tree type;
5475 location_t type_location;
5476 location_t start_loc
5477 = cp_lexer_peek_token (parser->lexer)->location;
5478 /* The `__builtin_va_arg' construct is used to handle
5479 `va_arg'. Consume the `__builtin_va_arg' token. */
5480 cp_lexer_consume_token (parser->lexer);
5481 /* Look for the opening `('. */
5482 matching_parens parens;
5483 parens.require_open (parser);
5484 /* Now, parse the assignment-expression. */
5485 expression = cp_parser_assignment_expression (parser);
5486 /* Look for the `,'. */
5487 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5488 type_location = cp_lexer_peek_token (parser->lexer)->location;
5489 /* Parse the type-id. */
5490 {
5491 type_id_in_expr_sentinel s (parser);
5492 type = cp_parser_type_id (parser);
5493 }
5494 /* Look for the closing `)'. */
5495 location_t finish_loc
5496 = cp_lexer_peek_token (parser->lexer)->location;
5497 parens.require_close (parser);
5498 /* Using `va_arg' in a constant-expression is not
5499 allowed. */
5500 if (cp_parser_non_integral_constant_expression (parser,
5501 NIC_VA_ARG))
5502 return error_mark_node;
5503 /* Construct a location of the form:
5504 __builtin_va_arg (v, int)
5505 ~~~~~~~~~~~~~~~~~~~~~^~~~
5506 with the caret at the type, ranging from the start of the
5507 "__builtin_va_arg" token to the close paren. */
5508 location_t combined_loc
5509 = make_location (type_location, start_loc, finish_loc);
5510 return build_x_va_arg (combined_loc, expression, type);
5511 }
5512
5513 case RID_OFFSETOF:
5514 return cp_parser_builtin_offsetof (parser);
5515
5516 case RID_HAS_NOTHROW_ASSIGN:
5517 case RID_HAS_NOTHROW_CONSTRUCTOR:
5518 case RID_HAS_NOTHROW_COPY:
5519 case RID_HAS_TRIVIAL_ASSIGN:
5520 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5521 case RID_HAS_TRIVIAL_COPY:
5522 case RID_HAS_TRIVIAL_DESTRUCTOR:
5523 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5524 case RID_HAS_VIRTUAL_DESTRUCTOR:
5525 case RID_IS_ABSTRACT:
5526 case RID_IS_AGGREGATE:
5527 case RID_IS_BASE_OF:
5528 case RID_IS_CLASS:
5529 case RID_IS_EMPTY:
5530 case RID_IS_ENUM:
5531 case RID_IS_FINAL:
5532 case RID_IS_LITERAL_TYPE:
5533 case RID_IS_POD:
5534 case RID_IS_POLYMORPHIC:
5535 case RID_IS_SAME_AS:
5536 case RID_IS_STD_LAYOUT:
5537 case RID_IS_TRIVIAL:
5538 case RID_IS_TRIVIALLY_ASSIGNABLE:
5539 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5540 case RID_IS_TRIVIALLY_COPYABLE:
5541 case RID_IS_UNION:
5542 case RID_IS_ASSIGNABLE:
5543 case RID_IS_CONSTRUCTIBLE:
5544 return cp_parser_trait_expr (parser, token->keyword);
5545
5546 // C++ concepts
5547 case RID_REQUIRES:
5548 return cp_parser_requires_expression (parser);
5549
5550 /* Objective-C++ expressions. */
5551 case RID_AT_ENCODE:
5552 case RID_AT_PROTOCOL:
5553 case RID_AT_SELECTOR:
5554 return cp_parser_objc_expression (parser);
5555
5556 case RID_TEMPLATE:
5557 if (parser->in_function_body
5558 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5559 == CPP_LESS))
5560 {
5561 error_at (token->location,
5562 "a template declaration cannot appear at block scope");
5563 cp_parser_skip_to_end_of_block_or_statement (parser);
5564 return error_mark_node;
5565 }
5566 /* FALLTHRU */
5567 default:
5568 cp_parser_error (parser, "expected primary-expression");
5569 return error_mark_node;
5570 }
5571
5572 /* An id-expression can start with either an identifier, a
5573 `::' as the beginning of a qualified-id, or the "operator"
5574 keyword. */
5575 case CPP_NAME:
5576 case CPP_SCOPE:
5577 case CPP_TEMPLATE_ID:
5578 case CPP_NESTED_NAME_SPECIFIER:
5579 {
5580 id_expression:
5581 cp_expr id_expression;
5582 cp_expr decl;
5583 const char *error_msg;
5584 bool template_p;
5585 bool done;
5586 cp_token *id_expr_token;
5587
5588 /* Parse the id-expression. */
5589 id_expression
5590 = cp_parser_id_expression (parser,
5591 /*template_keyword_p=*/false,
5592 /*check_dependency_p=*/true,
5593 &template_p,
5594 /*declarator_p=*/false,
5595 /*optional_p=*/false);
5596 if (id_expression == error_mark_node)
5597 return error_mark_node;
5598 id_expr_token = token;
5599 token = cp_lexer_peek_token (parser->lexer);
5600 done = (token->type != CPP_OPEN_SQUARE
5601 && token->type != CPP_OPEN_PAREN
5602 && token->type != CPP_DOT
5603 && token->type != CPP_DEREF
5604 && token->type != CPP_PLUS_PLUS
5605 && token->type != CPP_MINUS_MINUS);
5606 /* If we have a template-id, then no further lookup is
5607 required. If the template-id was for a template-class, we
5608 will sometimes have a TYPE_DECL at this point. */
5609 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5610 || TREE_CODE (id_expression) == TYPE_DECL)
5611 decl = id_expression;
5612 /* Look up the name. */
5613 else
5614 {
5615 tree ambiguous_decls;
5616
5617 /* If we already know that this lookup is ambiguous, then
5618 we've already issued an error message; there's no reason
5619 to check again. */
5620 if (id_expr_token->type == CPP_NAME
5621 && id_expr_token->error_reported)
5622 {
5623 cp_parser_simulate_error (parser);
5624 return error_mark_node;
5625 }
5626
5627 decl = cp_parser_lookup_name (parser, id_expression,
5628 none_type,
5629 template_p,
5630 /*is_namespace=*/false,
5631 /*check_dependency=*/true,
5632 &ambiguous_decls,
5633 id_expression.get_location ());
5634 /* If the lookup was ambiguous, an error will already have
5635 been issued. */
5636 if (ambiguous_decls)
5637 return error_mark_node;
5638
5639 /* In Objective-C++, we may have an Objective-C 2.0
5640 dot-syntax for classes here. */
5641 if (c_dialect_objc ()
5642 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5643 && TREE_CODE (decl) == TYPE_DECL
5644 && objc_is_class_name (decl))
5645 {
5646 tree component;
5647 cp_lexer_consume_token (parser->lexer);
5648 component = cp_parser_identifier (parser);
5649 if (component == error_mark_node)
5650 return error_mark_node;
5651
5652 tree result = objc_build_class_component_ref (id_expression,
5653 component);
5654 /* Build a location of the form:
5655 expr.component
5656 ~~~~~^~~~~~~~~
5657 with caret at the start of the component name (at
5658 input_location), ranging from the start of the id_expression
5659 to the end of the component name. */
5660 location_t combined_loc
5661 = make_location (input_location, id_expression.get_start (),
5662 get_finish (input_location));
5663 protected_set_expr_location (result, combined_loc);
5664 return result;
5665 }
5666
5667 /* In Objective-C++, an instance variable (ivar) may be preferred
5668 to whatever cp_parser_lookup_name() found.
5669 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5670 rest of c-family, we have to do a little extra work to preserve
5671 any location information in cp_expr "decl". Given that
5672 objc_lookup_ivar is implemented in "c-family" and "objc", we
5673 have a trip through the pure "tree" type, rather than cp_expr.
5674 Naively copying it back to "decl" would implicitly give the
5675 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5676 store an EXPR_LOCATION. Hence we only update "decl" (and
5677 hence its location_t) if we get back a different tree node. */
5678 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5679 id_expression);
5680 if (decl_tree != decl.get_value ())
5681 decl = cp_expr (decl_tree);
5682
5683 /* If name lookup gives us a SCOPE_REF, then the
5684 qualifying scope was dependent. */
5685 if (TREE_CODE (decl) == SCOPE_REF)
5686 {
5687 /* At this point, we do not know if DECL is a valid
5688 integral constant expression. We assume that it is
5689 in fact such an expression, so that code like:
5690
5691 template <int N> struct A {
5692 int a[B<N>::i];
5693 };
5694
5695 is accepted. At template-instantiation time, we
5696 will check that B<N>::i is actually a constant. */
5697 return decl;
5698 }
5699 /* Check to see if DECL is a local variable in a context
5700 where that is forbidden. */
5701 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
5702 && local_variable_p (decl))
5703 {
5704 error_at (id_expression.get_location (),
5705 "local variable %qD may not appear in this context",
5706 decl.get_value ());
5707 return error_mark_node;
5708 }
5709 }
5710
5711 decl = (finish_id_expression
5712 (id_expression, decl, parser->scope,
5713 idk,
5714 parser->integral_constant_expression_p,
5715 parser->allow_non_integral_constant_expression_p,
5716 &parser->non_integral_constant_expression_p,
5717 template_p, done, address_p,
5718 template_arg_p,
5719 &error_msg,
5720 id_expression.get_location ()));
5721 if (error_msg)
5722 cp_parser_error (parser, error_msg);
5723 /* Build a location for an id-expression of the form:
5724 ::ns::id
5725 ~~~~~~^~
5726 or:
5727 id
5728 ^~
5729 i.e. from the start of the first token to the end of the final
5730 token, with the caret at the start of the unqualified-id. */
5731 location_t caret_loc = get_pure_location (id_expression.get_location ());
5732 location_t start_loc = get_start (id_expr_token->location);
5733 location_t finish_loc = get_finish (id_expression.get_location ());
5734 location_t combined_loc
5735 = make_location (caret_loc, start_loc, finish_loc);
5736
5737 decl.set_location (combined_loc);
5738 return decl;
5739 }
5740
5741 /* Anything else is an error. */
5742 default:
5743 cp_parser_error (parser, "expected primary-expression");
5744 return error_mark_node;
5745 }
5746 }
5747
5748 static inline cp_expr
5749 cp_parser_primary_expression (cp_parser *parser,
5750 bool address_p,
5751 bool cast_p,
5752 bool template_arg_p,
5753 cp_id_kind *idk)
5754 {
5755 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5756 /*decltype*/false, idk);
5757 }
5758
5759 /* Parse an id-expression.
5760
5761 id-expression:
5762 unqualified-id
5763 qualified-id
5764
5765 qualified-id:
5766 :: [opt] nested-name-specifier template [opt] unqualified-id
5767 :: identifier
5768 :: operator-function-id
5769 :: template-id
5770
5771 Return a representation of the unqualified portion of the
5772 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5773 a `::' or nested-name-specifier.
5774
5775 Often, if the id-expression was a qualified-id, the caller will
5776 want to make a SCOPE_REF to represent the qualified-id. This
5777 function does not do this in order to avoid wastefully creating
5778 SCOPE_REFs when they are not required.
5779
5780 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5781 `template' keyword.
5782
5783 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5784 uninstantiated templates.
5785
5786 If *TEMPLATE_P is non-NULL, it is set to true iff the
5787 `template' keyword is used to explicitly indicate that the entity
5788 named is a template.
5789
5790 If DECLARATOR_P is true, the id-expression is appearing as part of
5791 a declarator, rather than as part of an expression. */
5792
5793 static cp_expr
5794 cp_parser_id_expression (cp_parser *parser,
5795 bool template_keyword_p,
5796 bool check_dependency_p,
5797 bool *template_p,
5798 bool declarator_p,
5799 bool optional_p)
5800 {
5801 bool global_scope_p;
5802 bool nested_name_specifier_p;
5803
5804 /* Assume the `template' keyword was not used. */
5805 if (template_p)
5806 *template_p = template_keyword_p;
5807
5808 /* Look for the optional `::' operator. */
5809 global_scope_p
5810 = (!template_keyword_p
5811 && (cp_parser_global_scope_opt (parser,
5812 /*current_scope_valid_p=*/false)
5813 != NULL_TREE));
5814
5815 /* Look for the optional nested-name-specifier. */
5816 nested_name_specifier_p
5817 = (cp_parser_nested_name_specifier_opt (parser,
5818 /*typename_keyword_p=*/false,
5819 check_dependency_p,
5820 /*type_p=*/false,
5821 declarator_p,
5822 template_keyword_p)
5823 != NULL_TREE);
5824
5825 /* If there is a nested-name-specifier, then we are looking at
5826 the first qualified-id production. */
5827 if (nested_name_specifier_p)
5828 {
5829 tree saved_scope;
5830 tree saved_object_scope;
5831 tree saved_qualifying_scope;
5832 cp_expr unqualified_id;
5833 bool is_template;
5834
5835 /* See if the next token is the `template' keyword. */
5836 if (!template_p)
5837 template_p = &is_template;
5838 *template_p = cp_parser_optional_template_keyword (parser);
5839 /* Name lookup we do during the processing of the
5840 unqualified-id might obliterate SCOPE. */
5841 saved_scope = parser->scope;
5842 saved_object_scope = parser->object_scope;
5843 saved_qualifying_scope = parser->qualifying_scope;
5844 /* Process the final unqualified-id. */
5845 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5846 check_dependency_p,
5847 declarator_p,
5848 /*optional_p=*/false);
5849 /* Restore the SAVED_SCOPE for our caller. */
5850 parser->scope = saved_scope;
5851 parser->object_scope = saved_object_scope;
5852 parser->qualifying_scope = saved_qualifying_scope;
5853
5854 return unqualified_id;
5855 }
5856 /* Otherwise, if we are in global scope, then we are looking at one
5857 of the other qualified-id productions. */
5858 else if (global_scope_p)
5859 {
5860 cp_token *token;
5861 tree id;
5862
5863 /* Peek at the next token. */
5864 token = cp_lexer_peek_token (parser->lexer);
5865
5866 /* If it's an identifier, and the next token is not a "<", then
5867 we can avoid the template-id case. This is an optimization
5868 for this common case. */
5869 if (token->type == CPP_NAME
5870 && !cp_parser_nth_token_starts_template_argument_list_p
5871 (parser, 2))
5872 return cp_parser_identifier (parser);
5873
5874 cp_parser_parse_tentatively (parser);
5875 /* Try a template-id. */
5876 id = cp_parser_template_id (parser,
5877 /*template_keyword_p=*/false,
5878 /*check_dependency_p=*/true,
5879 none_type,
5880 declarator_p);
5881 /* If that worked, we're done. */
5882 if (cp_parser_parse_definitely (parser))
5883 return id;
5884
5885 /* Peek at the next token. (Changes in the token buffer may
5886 have invalidated the pointer obtained above.) */
5887 token = cp_lexer_peek_token (parser->lexer);
5888
5889 switch (token->type)
5890 {
5891 case CPP_NAME:
5892 return cp_parser_identifier (parser);
5893
5894 case CPP_KEYWORD:
5895 if (token->keyword == RID_OPERATOR)
5896 return cp_parser_operator_function_id (parser);
5897 /* Fall through. */
5898
5899 default:
5900 cp_parser_error (parser, "expected id-expression");
5901 return error_mark_node;
5902 }
5903 }
5904 else
5905 return cp_parser_unqualified_id (parser, template_keyword_p,
5906 /*check_dependency_p=*/true,
5907 declarator_p,
5908 optional_p);
5909 }
5910
5911 /* Parse an unqualified-id.
5912
5913 unqualified-id:
5914 identifier
5915 operator-function-id
5916 conversion-function-id
5917 ~ class-name
5918 template-id
5919
5920 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5921 keyword, in a construct like `A::template ...'.
5922
5923 Returns a representation of unqualified-id. For the `identifier'
5924 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5925 production a BIT_NOT_EXPR is returned; the operand of the
5926 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5927 other productions, see the documentation accompanying the
5928 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5929 names are looked up in uninstantiated templates. If DECLARATOR_P
5930 is true, the unqualified-id is appearing as part of a declarator,
5931 rather than as part of an expression. */
5932
5933 static cp_expr
5934 cp_parser_unqualified_id (cp_parser* parser,
5935 bool template_keyword_p,
5936 bool check_dependency_p,
5937 bool declarator_p,
5938 bool optional_p)
5939 {
5940 cp_token *token;
5941
5942 /* Peek at the next token. */
5943 token = cp_lexer_peek_token (parser->lexer);
5944
5945 switch ((int) token->type)
5946 {
5947 case CPP_NAME:
5948 {
5949 tree id;
5950
5951 /* We don't know yet whether or not this will be a
5952 template-id. */
5953 cp_parser_parse_tentatively (parser);
5954 /* Try a template-id. */
5955 id = cp_parser_template_id (parser, template_keyword_p,
5956 check_dependency_p,
5957 none_type,
5958 declarator_p);
5959 /* If it worked, we're done. */
5960 if (cp_parser_parse_definitely (parser))
5961 return id;
5962 /* Otherwise, it's an ordinary identifier. */
5963 return cp_parser_identifier (parser);
5964 }
5965
5966 case CPP_TEMPLATE_ID:
5967 return cp_parser_template_id (parser, template_keyword_p,
5968 check_dependency_p,
5969 none_type,
5970 declarator_p);
5971
5972 case CPP_COMPL:
5973 {
5974 tree type_decl;
5975 tree qualifying_scope;
5976 tree object_scope;
5977 tree scope;
5978 bool done;
5979 location_t tilde_loc = token->location;
5980
5981 /* Consume the `~' token. */
5982 cp_lexer_consume_token (parser->lexer);
5983 /* Parse the class-name. The standard, as written, seems to
5984 say that:
5985
5986 template <typename T> struct S { ~S (); };
5987 template <typename T> S<T>::~S() {}
5988
5989 is invalid, since `~' must be followed by a class-name, but
5990 `S<T>' is dependent, and so not known to be a class.
5991 That's not right; we need to look in uninstantiated
5992 templates. A further complication arises from:
5993
5994 template <typename T> void f(T t) {
5995 t.T::~T();
5996 }
5997
5998 Here, it is not possible to look up `T' in the scope of `T'
5999 itself. We must look in both the current scope, and the
6000 scope of the containing complete expression.
6001
6002 Yet another issue is:
6003
6004 struct S {
6005 int S;
6006 ~S();
6007 };
6008
6009 S::~S() {}
6010
6011 The standard does not seem to say that the `S' in `~S'
6012 should refer to the type `S' and not the data member
6013 `S::S'. */
6014
6015 /* DR 244 says that we look up the name after the "~" in the
6016 same scope as we looked up the qualifying name. That idea
6017 isn't fully worked out; it's more complicated than that. */
6018 scope = parser->scope;
6019 object_scope = parser->object_scope;
6020 qualifying_scope = parser->qualifying_scope;
6021
6022 /* Check for invalid scopes. */
6023 if (scope == error_mark_node)
6024 {
6025 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6026 cp_lexer_consume_token (parser->lexer);
6027 return error_mark_node;
6028 }
6029 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6030 {
6031 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6032 error_at (token->location,
6033 "scope %qT before %<~%> is not a class-name",
6034 scope);
6035 cp_parser_simulate_error (parser);
6036 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6037 cp_lexer_consume_token (parser->lexer);
6038 return error_mark_node;
6039 }
6040 gcc_assert (!scope || TYPE_P (scope));
6041
6042 token = cp_lexer_peek_token (parser->lexer);
6043
6044 /* Create a location with caret == start at the tilde,
6045 finishing at the end of the peeked token, e.g:
6046 ~token
6047 ^~~~~~. */
6048 location_t loc
6049 = make_location (tilde_loc, tilde_loc, token->location);
6050
6051 /* If the name is of the form "X::~X" it's OK even if X is a
6052 typedef. */
6053
6054 if (scope
6055 && token->type == CPP_NAME
6056 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6057 != CPP_LESS)
6058 && (token->u.value == TYPE_IDENTIFIER (scope)
6059 || (CLASS_TYPE_P (scope)
6060 && constructor_name_p (token->u.value, scope))))
6061 {
6062 cp_lexer_consume_token (parser->lexer);
6063 return cp_expr (build_nt (BIT_NOT_EXPR, scope), loc);
6064 }
6065
6066 /* ~auto means the destructor of whatever the object is. */
6067 if (cp_parser_is_keyword (token, RID_AUTO))
6068 {
6069 if (cxx_dialect < cxx14)
6070 pedwarn (loc, 0,
6071 "%<~auto%> only available with "
6072 "%<-std=c++14%> or %<-std=gnu++14%>");
6073 cp_lexer_consume_token (parser->lexer);
6074 return cp_expr (build_nt (BIT_NOT_EXPR, make_auto (), loc));
6075 }
6076
6077 /* If there was an explicit qualification (S::~T), first look
6078 in the scope given by the qualification (i.e., S).
6079
6080 Note: in the calls to cp_parser_class_name below we pass
6081 typename_type so that lookup finds the injected-class-name
6082 rather than the constructor. */
6083 done = false;
6084 type_decl = NULL_TREE;
6085 if (scope)
6086 {
6087 cp_parser_parse_tentatively (parser);
6088 type_decl = cp_parser_class_name (parser,
6089 /*typename_keyword_p=*/false,
6090 /*template_keyword_p=*/false,
6091 typename_type,
6092 /*check_dependency=*/false,
6093 /*class_head_p=*/false,
6094 declarator_p);
6095 if (cp_parser_parse_definitely (parser))
6096 done = true;
6097 }
6098 /* In "N::S::~S", look in "N" as well. */
6099 if (!done && scope && qualifying_scope)
6100 {
6101 cp_parser_parse_tentatively (parser);
6102 parser->scope = qualifying_scope;
6103 parser->object_scope = NULL_TREE;
6104 parser->qualifying_scope = NULL_TREE;
6105 type_decl
6106 = cp_parser_class_name (parser,
6107 /*typename_keyword_p=*/false,
6108 /*template_keyword_p=*/false,
6109 typename_type,
6110 /*check_dependency=*/false,
6111 /*class_head_p=*/false,
6112 declarator_p);
6113 if (cp_parser_parse_definitely (parser))
6114 done = true;
6115 }
6116 /* In "p->S::~T", look in the scope given by "*p" as well. */
6117 else if (!done && object_scope)
6118 {
6119 cp_parser_parse_tentatively (parser);
6120 parser->scope = object_scope;
6121 parser->object_scope = NULL_TREE;
6122 parser->qualifying_scope = NULL_TREE;
6123 type_decl
6124 = cp_parser_class_name (parser,
6125 /*typename_keyword_p=*/false,
6126 /*template_keyword_p=*/false,
6127 typename_type,
6128 /*check_dependency=*/false,
6129 /*class_head_p=*/false,
6130 declarator_p);
6131 if (cp_parser_parse_definitely (parser))
6132 done = true;
6133 }
6134 /* Look in the surrounding context. */
6135 if (!done)
6136 {
6137 parser->scope = NULL_TREE;
6138 parser->object_scope = NULL_TREE;
6139 parser->qualifying_scope = NULL_TREE;
6140 if (processing_template_decl)
6141 cp_parser_parse_tentatively (parser);
6142 type_decl
6143 = cp_parser_class_name (parser,
6144 /*typename_keyword_p=*/false,
6145 /*template_keyword_p=*/false,
6146 typename_type,
6147 /*check_dependency=*/false,
6148 /*class_head_p=*/false,
6149 declarator_p);
6150 if (processing_template_decl
6151 && ! cp_parser_parse_definitely (parser))
6152 {
6153 /* We couldn't find a type with this name. If we're parsing
6154 tentatively, fail and try something else. */
6155 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6156 {
6157 cp_parser_simulate_error (parser);
6158 return error_mark_node;
6159 }
6160 /* Otherwise, accept it and check for a match at instantiation
6161 time. */
6162 type_decl = cp_parser_identifier (parser);
6163 if (type_decl != error_mark_node)
6164 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6165 return cp_expr (type_decl, loc);
6166 }
6167 }
6168 /* If an error occurred, assume that the name of the
6169 destructor is the same as the name of the qualifying
6170 class. That allows us to keep parsing after running
6171 into ill-formed destructor names. */
6172 if (type_decl == error_mark_node && scope)
6173 return build_nt (BIT_NOT_EXPR, scope);
6174 else if (type_decl == error_mark_node)
6175 return error_mark_node;
6176
6177 /* Check that destructor name and scope match. */
6178 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6179 {
6180 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6181 error_at (loc,
6182 "declaration of %<~%T%> as member of %qT",
6183 type_decl, scope);
6184 cp_parser_simulate_error (parser);
6185 return error_mark_node;
6186 }
6187
6188 /* [class.dtor]
6189
6190 A typedef-name that names a class shall not be used as the
6191 identifier in the declarator for a destructor declaration. */
6192 if (declarator_p
6193 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6194 && !DECL_SELF_REFERENCE_P (type_decl)
6195 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6196 error_at (loc,
6197 "typedef-name %qD used as destructor declarator",
6198 type_decl);
6199
6200 return cp_expr (build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl), loc));
6201 }
6202
6203 case CPP_KEYWORD:
6204 if (token->keyword == RID_OPERATOR)
6205 {
6206 cp_expr id;
6207
6208 /* This could be a template-id, so we try that first. */
6209 cp_parser_parse_tentatively (parser);
6210 /* Try a template-id. */
6211 id = cp_parser_template_id (parser, template_keyword_p,
6212 /*check_dependency_p=*/true,
6213 none_type,
6214 declarator_p);
6215 /* If that worked, we're done. */
6216 if (cp_parser_parse_definitely (parser))
6217 return id;
6218 /* We still don't know whether we're looking at an
6219 operator-function-id or a conversion-function-id. */
6220 cp_parser_parse_tentatively (parser);
6221 /* Try an operator-function-id. */
6222 id = cp_parser_operator_function_id (parser);
6223 /* If that didn't work, try a conversion-function-id. */
6224 if (!cp_parser_parse_definitely (parser))
6225 id = cp_parser_conversion_function_id (parser);
6226
6227 return id;
6228 }
6229 /* Fall through. */
6230
6231 default:
6232 if (optional_p)
6233 return NULL_TREE;
6234 cp_parser_error (parser, "expected unqualified-id");
6235 return error_mark_node;
6236 }
6237 }
6238
6239 /* Parse an (optional) nested-name-specifier.
6240
6241 nested-name-specifier: [C++98]
6242 class-or-namespace-name :: nested-name-specifier [opt]
6243 class-or-namespace-name :: template nested-name-specifier [opt]
6244
6245 nested-name-specifier: [C++0x]
6246 type-name ::
6247 namespace-name ::
6248 nested-name-specifier identifier ::
6249 nested-name-specifier template [opt] simple-template-id ::
6250
6251 PARSER->SCOPE should be set appropriately before this function is
6252 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6253 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6254 in name lookups.
6255
6256 Sets PARSER->SCOPE to the class (TYPE) or namespace
6257 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6258 it unchanged if there is no nested-name-specifier. Returns the new
6259 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6260
6261 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6262 part of a declaration and/or decl-specifier. */
6263
6264 static tree
6265 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6266 bool typename_keyword_p,
6267 bool check_dependency_p,
6268 bool type_p,
6269 bool is_declaration,
6270 bool template_keyword_p /* = false */)
6271 {
6272 bool success = false;
6273 cp_token_position start = 0;
6274 cp_token *token;
6275
6276 /* Remember where the nested-name-specifier starts. */
6277 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6278 {
6279 start = cp_lexer_token_position (parser->lexer, false);
6280 push_deferring_access_checks (dk_deferred);
6281 }
6282
6283 while (true)
6284 {
6285 tree new_scope;
6286 tree old_scope;
6287 tree saved_qualifying_scope;
6288
6289 /* Spot cases that cannot be the beginning of a
6290 nested-name-specifier. */
6291 token = cp_lexer_peek_token (parser->lexer);
6292
6293 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6294 the already parsed nested-name-specifier. */
6295 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6296 {
6297 /* Grab the nested-name-specifier and continue the loop. */
6298 cp_parser_pre_parsed_nested_name_specifier (parser);
6299 /* If we originally encountered this nested-name-specifier
6300 with IS_DECLARATION set to false, we will not have
6301 resolved TYPENAME_TYPEs, so we must do so here. */
6302 if (is_declaration
6303 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6304 {
6305 new_scope = resolve_typename_type (parser->scope,
6306 /*only_current_p=*/false);
6307 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6308 parser->scope = new_scope;
6309 }
6310 success = true;
6311 continue;
6312 }
6313
6314 /* Spot cases that cannot be the beginning of a
6315 nested-name-specifier. On the second and subsequent times
6316 through the loop, we look for the `template' keyword. */
6317 if (success && token->keyword == RID_TEMPLATE)
6318 ;
6319 /* A template-id can start a nested-name-specifier. */
6320 else if (token->type == CPP_TEMPLATE_ID)
6321 ;
6322 /* DR 743: decltype can be used in a nested-name-specifier. */
6323 else if (token_is_decltype (token))
6324 ;
6325 else
6326 {
6327 /* If the next token is not an identifier, then it is
6328 definitely not a type-name or namespace-name. */
6329 if (token->type != CPP_NAME)
6330 break;
6331 /* If the following token is neither a `<' (to begin a
6332 template-id), nor a `::', then we are not looking at a
6333 nested-name-specifier. */
6334 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6335
6336 if (token->type == CPP_COLON
6337 && parser->colon_corrects_to_scope_p
6338 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6339 {
6340 gcc_rich_location richloc (token->location);
6341 richloc.add_fixit_replace ("::");
6342 error_at (&richloc,
6343 "found %<:%> in nested-name-specifier, "
6344 "expected %<::%>");
6345 token->type = CPP_SCOPE;
6346 }
6347
6348 if (token->type != CPP_SCOPE
6349 && !cp_parser_nth_token_starts_template_argument_list_p
6350 (parser, 2))
6351 break;
6352 }
6353
6354 /* The nested-name-specifier is optional, so we parse
6355 tentatively. */
6356 cp_parser_parse_tentatively (parser);
6357
6358 /* Look for the optional `template' keyword, if this isn't the
6359 first time through the loop. */
6360 if (success)
6361 template_keyword_p = cp_parser_optional_template_keyword (parser);
6362
6363 /* Save the old scope since the name lookup we are about to do
6364 might destroy it. */
6365 old_scope = parser->scope;
6366 saved_qualifying_scope = parser->qualifying_scope;
6367 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6368 look up names in "X<T>::I" in order to determine that "Y" is
6369 a template. So, if we have a typename at this point, we make
6370 an effort to look through it. */
6371 if (is_declaration
6372 && !typename_keyword_p
6373 && parser->scope
6374 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6375 parser->scope = resolve_typename_type (parser->scope,
6376 /*only_current_p=*/false);
6377 /* Parse the qualifying entity. */
6378 new_scope
6379 = cp_parser_qualifying_entity (parser,
6380 typename_keyword_p,
6381 template_keyword_p,
6382 check_dependency_p,
6383 type_p,
6384 is_declaration);
6385 /* Look for the `::' token. */
6386 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6387
6388 /* If we found what we wanted, we keep going; otherwise, we're
6389 done. */
6390 if (!cp_parser_parse_definitely (parser))
6391 {
6392 bool error_p = false;
6393
6394 /* Restore the OLD_SCOPE since it was valid before the
6395 failed attempt at finding the last
6396 class-or-namespace-name. */
6397 parser->scope = old_scope;
6398 parser->qualifying_scope = saved_qualifying_scope;
6399
6400 /* If the next token is a decltype, and the one after that is a
6401 `::', then the decltype has failed to resolve to a class or
6402 enumeration type. Give this error even when parsing
6403 tentatively since it can't possibly be valid--and we're going
6404 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6405 won't get another chance.*/
6406 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6407 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6408 == CPP_SCOPE))
6409 {
6410 token = cp_lexer_consume_token (parser->lexer);
6411 error_at (token->location, "decltype evaluates to %qT, "
6412 "which is not a class or enumeration type",
6413 token->u.tree_check_value->value);
6414 parser->scope = error_mark_node;
6415 error_p = true;
6416 /* As below. */
6417 success = true;
6418 cp_lexer_consume_token (parser->lexer);
6419 }
6420
6421 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6422 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6423 {
6424 /* If we have a non-type template-id followed by ::, it can't
6425 possibly be valid. */
6426 token = cp_lexer_peek_token (parser->lexer);
6427 tree tid = token->u.tree_check_value->value;
6428 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6429 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6430 {
6431 tree tmpl = NULL_TREE;
6432 if (is_overloaded_fn (tid))
6433 {
6434 tree fns = get_fns (tid);
6435 if (OVL_SINGLE_P (fns))
6436 tmpl = OVL_FIRST (fns);
6437 error_at (token->location, "function template-id %qD "
6438 "in nested-name-specifier", tid);
6439 }
6440 else
6441 {
6442 /* Variable template. */
6443 tmpl = TREE_OPERAND (tid, 0);
6444 gcc_assert (variable_template_p (tmpl));
6445 error_at (token->location, "variable template-id %qD "
6446 "in nested-name-specifier", tid);
6447 }
6448 if (tmpl)
6449 inform (DECL_SOURCE_LOCATION (tmpl),
6450 "%qD declared here", tmpl);
6451
6452 parser->scope = error_mark_node;
6453 error_p = true;
6454 /* As below. */
6455 success = true;
6456 cp_lexer_consume_token (parser->lexer);
6457 cp_lexer_consume_token (parser->lexer);
6458 }
6459 }
6460
6461 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6462 break;
6463 /* If the next token is an identifier, and the one after
6464 that is a `::', then any valid interpretation would have
6465 found a class-or-namespace-name. */
6466 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6467 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6468 == CPP_SCOPE)
6469 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6470 != CPP_COMPL))
6471 {
6472 token = cp_lexer_consume_token (parser->lexer);
6473 if (!error_p)
6474 {
6475 if (!token->error_reported)
6476 {
6477 tree decl;
6478 tree ambiguous_decls;
6479
6480 decl = cp_parser_lookup_name (parser, token->u.value,
6481 none_type,
6482 /*is_template=*/false,
6483 /*is_namespace=*/false,
6484 /*check_dependency=*/true,
6485 &ambiguous_decls,
6486 token->location);
6487 if (TREE_CODE (decl) == TEMPLATE_DECL)
6488 error_at (token->location,
6489 "%qD used without template arguments",
6490 decl);
6491 else if (ambiguous_decls)
6492 {
6493 // cp_parser_lookup_name has the same diagnostic,
6494 // thus make sure to emit it at most once.
6495 if (cp_parser_uncommitted_to_tentative_parse_p
6496 (parser))
6497 {
6498 error_at (token->location,
6499 "reference to %qD is ambiguous",
6500 token->u.value);
6501 print_candidates (ambiguous_decls);
6502 }
6503 decl = error_mark_node;
6504 }
6505 else
6506 {
6507 if (cxx_dialect != cxx98)
6508 cp_parser_name_lookup_error
6509 (parser, token->u.value, decl, NLE_NOT_CXX98,
6510 token->location);
6511 else
6512 cp_parser_name_lookup_error
6513 (parser, token->u.value, decl, NLE_CXX98,
6514 token->location);
6515 }
6516 }
6517 parser->scope = error_mark_node;
6518 error_p = true;
6519 /* Treat this as a successful nested-name-specifier
6520 due to:
6521
6522 [basic.lookup.qual]
6523
6524 If the name found is not a class-name (clause
6525 _class_) or namespace-name (_namespace.def_), the
6526 program is ill-formed. */
6527 success = true;
6528 }
6529 cp_lexer_consume_token (parser->lexer);
6530 }
6531 break;
6532 }
6533 /* We've found one valid nested-name-specifier. */
6534 success = true;
6535 /* Name lookup always gives us a DECL. */
6536 if (TREE_CODE (new_scope) == TYPE_DECL)
6537 new_scope = TREE_TYPE (new_scope);
6538 /* Uses of "template" must be followed by actual templates. */
6539 if (template_keyword_p
6540 && !(CLASS_TYPE_P (new_scope)
6541 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6542 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6543 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6544 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6545 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6546 == TEMPLATE_ID_EXPR)))
6547 permerror (input_location, TYPE_P (new_scope)
6548 ? G_("%qT is not a template")
6549 : G_("%qD is not a template"),
6550 new_scope);
6551 /* If it is a class scope, try to complete it; we are about to
6552 be looking up names inside the class. */
6553 if (TYPE_P (new_scope)
6554 /* Since checking types for dependency can be expensive,
6555 avoid doing it if the type is already complete. */
6556 && !COMPLETE_TYPE_P (new_scope)
6557 /* Do not try to complete dependent types. */
6558 && !dependent_type_p (new_scope))
6559 {
6560 new_scope = complete_type (new_scope);
6561 /* If it is a typedef to current class, use the current
6562 class instead, as the typedef won't have any names inside
6563 it yet. */
6564 if (!COMPLETE_TYPE_P (new_scope)
6565 && currently_open_class (new_scope))
6566 new_scope = TYPE_MAIN_VARIANT (new_scope);
6567 }
6568 /* Make sure we look in the right scope the next time through
6569 the loop. */
6570 parser->scope = new_scope;
6571 }
6572
6573 /* If parsing tentatively, replace the sequence of tokens that makes
6574 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6575 token. That way, should we re-parse the token stream, we will
6576 not have to repeat the effort required to do the parse, nor will
6577 we issue duplicate error messages. */
6578 if (success && start)
6579 {
6580 cp_token *token;
6581
6582 token = cp_lexer_token_at (parser->lexer, start);
6583 /* Reset the contents of the START token. */
6584 token->type = CPP_NESTED_NAME_SPECIFIER;
6585 /* Retrieve any deferred checks. Do not pop this access checks yet
6586 so the memory will not be reclaimed during token replacing below. */
6587 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6588 token->u.tree_check_value->value = parser->scope;
6589 token->u.tree_check_value->checks = get_deferred_access_checks ();
6590 token->u.tree_check_value->qualifying_scope =
6591 parser->qualifying_scope;
6592 token->keyword = RID_MAX;
6593
6594 /* Purge all subsequent tokens. */
6595 cp_lexer_purge_tokens_after (parser->lexer, start);
6596 }
6597
6598 if (start)
6599 pop_to_parent_deferring_access_checks ();
6600
6601 return success ? parser->scope : NULL_TREE;
6602 }
6603
6604 /* Parse a nested-name-specifier. See
6605 cp_parser_nested_name_specifier_opt for details. This function
6606 behaves identically, except that it will an issue an error if no
6607 nested-name-specifier is present. */
6608
6609 static tree
6610 cp_parser_nested_name_specifier (cp_parser *parser,
6611 bool typename_keyword_p,
6612 bool check_dependency_p,
6613 bool type_p,
6614 bool is_declaration)
6615 {
6616 tree scope;
6617
6618 /* Look for the nested-name-specifier. */
6619 scope = cp_parser_nested_name_specifier_opt (parser,
6620 typename_keyword_p,
6621 check_dependency_p,
6622 type_p,
6623 is_declaration);
6624 /* If it was not present, issue an error message. */
6625 if (!scope)
6626 {
6627 cp_parser_error (parser, "expected nested-name-specifier");
6628 parser->scope = NULL_TREE;
6629 }
6630
6631 return scope;
6632 }
6633
6634 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6635 this is either a class-name or a namespace-name (which corresponds
6636 to the class-or-namespace-name production in the grammar). For
6637 C++0x, it can also be a type-name that refers to an enumeration
6638 type or a simple-template-id.
6639
6640 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6641 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6642 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6643 TYPE_P is TRUE iff the next name should be taken as a class-name,
6644 even the same name is declared to be another entity in the same
6645 scope.
6646
6647 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6648 specified by the class-or-namespace-name. If neither is found the
6649 ERROR_MARK_NODE is returned. */
6650
6651 static tree
6652 cp_parser_qualifying_entity (cp_parser *parser,
6653 bool typename_keyword_p,
6654 bool template_keyword_p,
6655 bool check_dependency_p,
6656 bool type_p,
6657 bool is_declaration)
6658 {
6659 tree saved_scope;
6660 tree saved_qualifying_scope;
6661 tree saved_object_scope;
6662 tree scope;
6663 bool only_class_p;
6664 bool successful_parse_p;
6665
6666 /* DR 743: decltype can appear in a nested-name-specifier. */
6667 if (cp_lexer_next_token_is_decltype (parser->lexer))
6668 {
6669 scope = cp_parser_decltype (parser);
6670 if (TREE_CODE (scope) != ENUMERAL_TYPE
6671 && !MAYBE_CLASS_TYPE_P (scope))
6672 {
6673 cp_parser_simulate_error (parser);
6674 return error_mark_node;
6675 }
6676 if (TYPE_NAME (scope))
6677 scope = TYPE_NAME (scope);
6678 return scope;
6679 }
6680
6681 /* Before we try to parse the class-name, we must save away the
6682 current PARSER->SCOPE since cp_parser_class_name will destroy
6683 it. */
6684 saved_scope = parser->scope;
6685 saved_qualifying_scope = parser->qualifying_scope;
6686 saved_object_scope = parser->object_scope;
6687 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6688 there is no need to look for a namespace-name. */
6689 only_class_p = template_keyword_p
6690 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6691 if (!only_class_p)
6692 cp_parser_parse_tentatively (parser);
6693 scope = cp_parser_class_name (parser,
6694 typename_keyword_p,
6695 template_keyword_p,
6696 type_p ? class_type : none_type,
6697 check_dependency_p,
6698 /*class_head_p=*/false,
6699 is_declaration,
6700 /*enum_ok=*/cxx_dialect > cxx98);
6701 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6702 /* If that didn't work, try for a namespace-name. */
6703 if (!only_class_p && !successful_parse_p)
6704 {
6705 /* Restore the saved scope. */
6706 parser->scope = saved_scope;
6707 parser->qualifying_scope = saved_qualifying_scope;
6708 parser->object_scope = saved_object_scope;
6709 /* If we are not looking at an identifier followed by the scope
6710 resolution operator, then this is not part of a
6711 nested-name-specifier. (Note that this function is only used
6712 to parse the components of a nested-name-specifier.) */
6713 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6714 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6715 return error_mark_node;
6716 scope = cp_parser_namespace_name (parser);
6717 }
6718
6719 return scope;
6720 }
6721
6722 /* Return true if we are looking at a compound-literal, false otherwise. */
6723
6724 static bool
6725 cp_parser_compound_literal_p (cp_parser *parser)
6726 {
6727 cp_lexer_save_tokens (parser->lexer);
6728
6729 /* Skip tokens until the next token is a closing parenthesis.
6730 If we find the closing `)', and the next token is a `{', then
6731 we are looking at a compound-literal. */
6732 bool compound_literal_p
6733 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6734 /*consume_paren=*/true)
6735 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6736
6737 /* Roll back the tokens we skipped. */
6738 cp_lexer_rollback_tokens (parser->lexer);
6739
6740 return compound_literal_p;
6741 }
6742
6743 /* Return true if EXPR is the integer constant zero or a complex constant
6744 of zero, without any folding, but ignoring location wrappers. */
6745
6746 bool
6747 literal_integer_zerop (const_tree expr)
6748 {
6749 return (location_wrapper_p (expr)
6750 && integer_zerop (TREE_OPERAND (expr, 0)));
6751 }
6752
6753 /* Parse a postfix-expression.
6754
6755 postfix-expression:
6756 primary-expression
6757 postfix-expression [ expression ]
6758 postfix-expression ( expression-list [opt] )
6759 simple-type-specifier ( expression-list [opt] )
6760 typename :: [opt] nested-name-specifier identifier
6761 ( expression-list [opt] )
6762 typename :: [opt] nested-name-specifier template [opt] template-id
6763 ( expression-list [opt] )
6764 postfix-expression . template [opt] id-expression
6765 postfix-expression -> template [opt] id-expression
6766 postfix-expression . pseudo-destructor-name
6767 postfix-expression -> pseudo-destructor-name
6768 postfix-expression ++
6769 postfix-expression --
6770 dynamic_cast < type-id > ( expression )
6771 static_cast < type-id > ( expression )
6772 reinterpret_cast < type-id > ( expression )
6773 const_cast < type-id > ( expression )
6774 typeid ( expression )
6775 typeid ( type-id )
6776
6777 GNU Extension:
6778
6779 postfix-expression:
6780 ( type-id ) { initializer-list , [opt] }
6781
6782 This extension is a GNU version of the C99 compound-literal
6783 construct. (The C99 grammar uses `type-name' instead of `type-id',
6784 but they are essentially the same concept.)
6785
6786 If ADDRESS_P is true, the postfix expression is the operand of the
6787 `&' operator. CAST_P is true if this expression is the target of a
6788 cast.
6789
6790 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6791 class member access expressions [expr.ref].
6792
6793 Returns a representation of the expression. */
6794
6795 static cp_expr
6796 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6797 bool member_access_only_p, bool decltype_p,
6798 cp_id_kind * pidk_return)
6799 {
6800 cp_token *token;
6801 location_t loc;
6802 enum rid keyword;
6803 cp_id_kind idk = CP_ID_KIND_NONE;
6804 cp_expr postfix_expression = NULL_TREE;
6805 bool is_member_access = false;
6806
6807 /* Peek at the next token. */
6808 token = cp_lexer_peek_token (parser->lexer);
6809 loc = token->location;
6810 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6811
6812 /* Some of the productions are determined by keywords. */
6813 keyword = token->keyword;
6814 switch (keyword)
6815 {
6816 case RID_DYNCAST:
6817 case RID_STATCAST:
6818 case RID_REINTCAST:
6819 case RID_CONSTCAST:
6820 {
6821 tree type;
6822 cp_expr expression;
6823 const char *saved_message;
6824 bool saved_in_type_id_in_expr_p;
6825
6826 /* All of these can be handled in the same way from the point
6827 of view of parsing. Begin by consuming the token
6828 identifying the cast. */
6829 cp_lexer_consume_token (parser->lexer);
6830
6831 /* New types cannot be defined in the cast. */
6832 saved_message = parser->type_definition_forbidden_message;
6833 parser->type_definition_forbidden_message
6834 = G_("types may not be defined in casts");
6835
6836 /* Look for the opening `<'. */
6837 cp_parser_require (parser, CPP_LESS, RT_LESS);
6838 /* Parse the type to which we are casting. */
6839 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6840 parser->in_type_id_in_expr_p = true;
6841 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
6842 NULL);
6843 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6844 /* Look for the closing `>'. */
6845 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6846 /* Restore the old message. */
6847 parser->type_definition_forbidden_message = saved_message;
6848
6849 bool saved_greater_than_is_operator_p
6850 = parser->greater_than_is_operator_p;
6851 parser->greater_than_is_operator_p = true;
6852
6853 /* And the expression which is being cast. */
6854 matching_parens parens;
6855 parens.require_open (parser);
6856 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6857 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6858 RT_CLOSE_PAREN);
6859 location_t end_loc = close_paren ?
6860 close_paren->location : UNKNOWN_LOCATION;
6861
6862 parser->greater_than_is_operator_p
6863 = saved_greater_than_is_operator_p;
6864
6865 /* Only type conversions to integral or enumeration types
6866 can be used in constant-expressions. */
6867 if (!cast_valid_in_integral_constant_expression_p (type)
6868 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6869 {
6870 postfix_expression = error_mark_node;
6871 break;
6872 }
6873
6874 switch (keyword)
6875 {
6876 case RID_DYNCAST:
6877 postfix_expression
6878 = build_dynamic_cast (type, expression, tf_warning_or_error);
6879 break;
6880 case RID_STATCAST:
6881 postfix_expression
6882 = build_static_cast (type, expression, tf_warning_or_error);
6883 break;
6884 case RID_REINTCAST:
6885 postfix_expression
6886 = build_reinterpret_cast (type, expression,
6887 tf_warning_or_error);
6888 break;
6889 case RID_CONSTCAST:
6890 postfix_expression
6891 = build_const_cast (type, expression, tf_warning_or_error);
6892 break;
6893 default:
6894 gcc_unreachable ();
6895 }
6896
6897 /* Construct a location e.g. :
6898 reinterpret_cast <int *> (expr)
6899 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6900 ranging from the start of the "*_cast" token to the final closing
6901 paren, with the caret at the start. */
6902 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6903 postfix_expression.set_location (cp_cast_loc);
6904 }
6905 break;
6906
6907 case RID_TYPEID:
6908 {
6909 tree type;
6910 const char *saved_message;
6911 bool saved_in_type_id_in_expr_p;
6912
6913 /* Consume the `typeid' token. */
6914 cp_lexer_consume_token (parser->lexer);
6915 /* Look for the `(' token. */
6916 matching_parens parens;
6917 parens.require_open (parser);
6918 /* Types cannot be defined in a `typeid' expression. */
6919 saved_message = parser->type_definition_forbidden_message;
6920 parser->type_definition_forbidden_message
6921 = G_("types may not be defined in a %<typeid%> expression");
6922 /* We can't be sure yet whether we're looking at a type-id or an
6923 expression. */
6924 cp_parser_parse_tentatively (parser);
6925 /* Try a type-id first. */
6926 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6927 parser->in_type_id_in_expr_p = true;
6928 type = cp_parser_type_id (parser);
6929 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6930 /* Look for the `)' token. Otherwise, we can't be sure that
6931 we're not looking at an expression: consider `typeid (int
6932 (3))', for example. */
6933 cp_token *close_paren = parens.require_close (parser);
6934 /* If all went well, simply lookup the type-id. */
6935 if (cp_parser_parse_definitely (parser))
6936 postfix_expression = get_typeid (type, tf_warning_or_error);
6937 /* Otherwise, fall back to the expression variant. */
6938 else
6939 {
6940 tree expression;
6941
6942 /* Look for an expression. */
6943 expression = cp_parser_expression (parser, & idk);
6944 /* Compute its typeid. */
6945 postfix_expression = build_typeid (expression, tf_warning_or_error);
6946 /* Look for the `)' token. */
6947 close_paren = parens.require_close (parser);
6948 }
6949 /* Restore the saved message. */
6950 parser->type_definition_forbidden_message = saved_message;
6951 /* `typeid' may not appear in an integral constant expression. */
6952 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6953 postfix_expression = error_mark_node;
6954
6955 /* Construct a location e.g. :
6956 typeid (expr)
6957 ^~~~~~~~~~~~~
6958 ranging from the start of the "typeid" token to the final closing
6959 paren, with the caret at the start. */
6960 if (close_paren)
6961 {
6962 location_t typeid_loc
6963 = make_location (start_loc, start_loc, close_paren->location);
6964 postfix_expression.set_location (typeid_loc);
6965 postfix_expression.maybe_add_location_wrapper ();
6966 }
6967 }
6968 break;
6969
6970 case RID_TYPENAME:
6971 {
6972 tree type;
6973 /* The syntax permitted here is the same permitted for an
6974 elaborated-type-specifier. */
6975 ++parser->prevent_constrained_type_specifiers;
6976 type = cp_parser_elaborated_type_specifier (parser,
6977 /*is_friend=*/false,
6978 /*is_declaration=*/false);
6979 --parser->prevent_constrained_type_specifiers;
6980 postfix_expression = cp_parser_functional_cast (parser, type);
6981 }
6982 break;
6983
6984 case RID_ADDRESSOF:
6985 case RID_BUILTIN_SHUFFLE:
6986 case RID_BUILTIN_LAUNDER:
6987 {
6988 vec<tree, va_gc> *vec;
6989 unsigned int i;
6990 tree p;
6991
6992 cp_lexer_consume_token (parser->lexer);
6993 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6994 /*cast_p=*/false, /*allow_expansion_p=*/true,
6995 /*non_constant_p=*/NULL);
6996 if (vec == NULL)
6997 {
6998 postfix_expression = error_mark_node;
6999 break;
7000 }
7001
7002 FOR_EACH_VEC_ELT (*vec, i, p)
7003 mark_exp_read (p);
7004
7005 switch (keyword)
7006 {
7007 case RID_ADDRESSOF:
7008 if (vec->length () == 1)
7009 postfix_expression
7010 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7011 else
7012 {
7013 error_at (loc, "wrong number of arguments to "
7014 "%<__builtin_addressof%>");
7015 postfix_expression = error_mark_node;
7016 }
7017 break;
7018
7019 case RID_BUILTIN_LAUNDER:
7020 if (vec->length () == 1)
7021 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7022 tf_warning_or_error);
7023 else
7024 {
7025 error_at (loc, "wrong number of arguments to "
7026 "%<__builtin_launder%>");
7027 postfix_expression = error_mark_node;
7028 }
7029 break;
7030
7031 case RID_BUILTIN_SHUFFLE:
7032 if (vec->length () == 2)
7033 postfix_expression
7034 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7035 (*vec)[1], tf_warning_or_error);
7036 else if (vec->length () == 3)
7037 postfix_expression
7038 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7039 (*vec)[2], tf_warning_or_error);
7040 else
7041 {
7042 error_at (loc, "wrong number of arguments to "
7043 "%<__builtin_shuffle%>");
7044 postfix_expression = error_mark_node;
7045 }
7046 break;
7047
7048 default:
7049 gcc_unreachable ();
7050 }
7051 break;
7052 }
7053
7054 case RID_BUILTIN_CONVERTVECTOR:
7055 {
7056 tree expression;
7057 tree type;
7058 /* Consume the `__builtin_convertvector' token. */
7059 cp_lexer_consume_token (parser->lexer);
7060 /* Look for the opening `('. */
7061 matching_parens parens;
7062 parens.require_open (parser);
7063 /* Now, parse the assignment-expression. */
7064 expression = cp_parser_assignment_expression (parser);
7065 /* Look for the `,'. */
7066 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7067 location_t type_location
7068 = cp_lexer_peek_token (parser->lexer)->location;
7069 /* Parse the type-id. */
7070 {
7071 type_id_in_expr_sentinel s (parser);
7072 type = cp_parser_type_id (parser);
7073 }
7074 /* Look for the closing `)'. */
7075 parens.require_close (parser);
7076 return cp_build_vec_convert (expression, type_location, type,
7077 tf_warning_or_error);
7078 }
7079
7080 default:
7081 {
7082 tree type;
7083
7084 /* If the next thing is a simple-type-specifier, we may be
7085 looking at a functional cast. We could also be looking at
7086 an id-expression. So, we try the functional cast, and if
7087 that doesn't work we fall back to the primary-expression. */
7088 cp_parser_parse_tentatively (parser);
7089 /* Look for the simple-type-specifier. */
7090 ++parser->prevent_constrained_type_specifiers;
7091 type = cp_parser_simple_type_specifier (parser,
7092 /*decl_specs=*/NULL,
7093 CP_PARSER_FLAGS_NONE);
7094 --parser->prevent_constrained_type_specifiers;
7095 /* Parse the cast itself. */
7096 if (!cp_parser_error_occurred (parser))
7097 postfix_expression
7098 = cp_parser_functional_cast (parser, type);
7099 /* If that worked, we're done. */
7100 if (cp_parser_parse_definitely (parser))
7101 break;
7102
7103 /* If the functional-cast didn't work out, try a
7104 compound-literal. */
7105 if (cp_parser_allow_gnu_extensions_p (parser)
7106 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7107 {
7108 cp_expr initializer = NULL_TREE;
7109
7110 cp_parser_parse_tentatively (parser);
7111
7112 matching_parens parens;
7113 parens.consume_open (parser);
7114
7115 /* Avoid calling cp_parser_type_id pointlessly, see comment
7116 in cp_parser_cast_expression about c++/29234. */
7117 if (!cp_parser_compound_literal_p (parser))
7118 cp_parser_simulate_error (parser);
7119 else
7120 {
7121 /* Parse the type. */
7122 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7123 parser->in_type_id_in_expr_p = true;
7124 type = cp_parser_type_id (parser);
7125 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7126 parens.require_close (parser);
7127 }
7128
7129 /* If things aren't going well, there's no need to
7130 keep going. */
7131 if (!cp_parser_error_occurred (parser))
7132 {
7133 bool non_constant_p;
7134 /* Parse the brace-enclosed initializer list. */
7135 initializer = cp_parser_braced_list (parser,
7136 &non_constant_p);
7137 }
7138 /* If that worked, we're definitely looking at a
7139 compound-literal expression. */
7140 if (cp_parser_parse_definitely (parser))
7141 {
7142 /* Warn the user that a compound literal is not
7143 allowed in standard C++. */
7144 pedwarn (input_location, OPT_Wpedantic,
7145 "ISO C++ forbids compound-literals");
7146 /* For simplicity, we disallow compound literals in
7147 constant-expressions. We could
7148 allow compound literals of integer type, whose
7149 initializer was a constant, in constant
7150 expressions. Permitting that usage, as a further
7151 extension, would not change the meaning of any
7152 currently accepted programs. (Of course, as
7153 compound literals are not part of ISO C++, the
7154 standard has nothing to say.) */
7155 if (cp_parser_non_integral_constant_expression (parser,
7156 NIC_NCC))
7157 {
7158 postfix_expression = error_mark_node;
7159 break;
7160 }
7161 /* Form the representation of the compound-literal. */
7162 postfix_expression
7163 = finish_compound_literal (type, initializer,
7164 tf_warning_or_error, fcl_c99);
7165 postfix_expression.set_location (initializer.get_location ());
7166 break;
7167 }
7168 }
7169
7170 /* It must be a primary-expression. */
7171 postfix_expression
7172 = cp_parser_primary_expression (parser, address_p, cast_p,
7173 /*template_arg_p=*/false,
7174 decltype_p,
7175 &idk);
7176 }
7177 break;
7178 }
7179
7180 /* Note that we don't need to worry about calling build_cplus_new on a
7181 class-valued CALL_EXPR in decltype when it isn't the end of the
7182 postfix-expression; unary_complex_lvalue will take care of that for
7183 all these cases. */
7184
7185 /* Keep looping until the postfix-expression is complete. */
7186 while (true)
7187 {
7188 if (idk == CP_ID_KIND_UNQUALIFIED
7189 && identifier_p (postfix_expression)
7190 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7191 /* It is not a Koenig lookup function call. */
7192 postfix_expression
7193 = unqualified_name_lookup_error (postfix_expression);
7194
7195 /* Peek at the next token. */
7196 token = cp_lexer_peek_token (parser->lexer);
7197
7198 switch (token->type)
7199 {
7200 case CPP_OPEN_SQUARE:
7201 if (cp_next_tokens_can_be_std_attribute_p (parser))
7202 {
7203 cp_parser_error (parser,
7204 "two consecutive %<[%> shall "
7205 "only introduce an attribute");
7206 return error_mark_node;
7207 }
7208 postfix_expression
7209 = cp_parser_postfix_open_square_expression (parser,
7210 postfix_expression,
7211 false,
7212 decltype_p);
7213 postfix_expression.set_range (start_loc,
7214 postfix_expression.get_location ());
7215
7216 idk = CP_ID_KIND_NONE;
7217 is_member_access = false;
7218 break;
7219
7220 case CPP_OPEN_PAREN:
7221 /* postfix-expression ( expression-list [opt] ) */
7222 {
7223 bool koenig_p;
7224 bool is_builtin_constant_p;
7225 bool saved_integral_constant_expression_p = false;
7226 bool saved_non_integral_constant_expression_p = false;
7227 tsubst_flags_t complain = complain_flags (decltype_p);
7228 vec<tree, va_gc> *args;
7229 location_t close_paren_loc = UNKNOWN_LOCATION;
7230
7231 is_member_access = false;
7232
7233 tree stripped_expression
7234 = tree_strip_any_location_wrapper (postfix_expression);
7235 is_builtin_constant_p
7236 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7237 if (is_builtin_constant_p)
7238 {
7239 /* The whole point of __builtin_constant_p is to allow
7240 non-constant expressions to appear as arguments. */
7241 saved_integral_constant_expression_p
7242 = parser->integral_constant_expression_p;
7243 saved_non_integral_constant_expression_p
7244 = parser->non_integral_constant_expression_p;
7245 parser->integral_constant_expression_p = false;
7246 }
7247 args = (cp_parser_parenthesized_expression_list
7248 (parser, non_attr,
7249 /*cast_p=*/false, /*allow_expansion_p=*/true,
7250 /*non_constant_p=*/NULL,
7251 /*close_paren_loc=*/&close_paren_loc,
7252 /*wrap_locations_p=*/true));
7253 if (is_builtin_constant_p)
7254 {
7255 parser->integral_constant_expression_p
7256 = saved_integral_constant_expression_p;
7257 parser->non_integral_constant_expression_p
7258 = saved_non_integral_constant_expression_p;
7259 }
7260
7261 if (args == NULL)
7262 {
7263 postfix_expression = error_mark_node;
7264 break;
7265 }
7266
7267 /* Function calls are not permitted in
7268 constant-expressions. */
7269 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7270 && cp_parser_non_integral_constant_expression (parser,
7271 NIC_FUNC_CALL))
7272 {
7273 postfix_expression = error_mark_node;
7274 release_tree_vector (args);
7275 break;
7276 }
7277
7278 koenig_p = false;
7279 if (idk == CP_ID_KIND_UNQUALIFIED
7280 || idk == CP_ID_KIND_TEMPLATE_ID)
7281 {
7282 if (identifier_p (postfix_expression)
7283 /* In C++2A, we may need to perform ADL for a template
7284 name. */
7285 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7286 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7287 {
7288 if (!args->is_empty ())
7289 {
7290 koenig_p = true;
7291 if (!any_type_dependent_arguments_p (args))
7292 postfix_expression
7293 = perform_koenig_lookup (postfix_expression, args,
7294 complain);
7295 }
7296 else
7297 postfix_expression
7298 = unqualified_fn_lookup_error (postfix_expression);
7299 }
7300 /* We do not perform argument-dependent lookup if
7301 normal lookup finds a non-function, in accordance
7302 with the expected resolution of DR 218. */
7303 else if (!args->is_empty ()
7304 && is_overloaded_fn (postfix_expression))
7305 {
7306 /* We only need to look at the first function,
7307 because all the fns share the attribute we're
7308 concerned with (all member fns or all local
7309 fns). */
7310 tree fn = get_first_fn (postfix_expression);
7311 fn = STRIP_TEMPLATE (fn);
7312
7313 /* Do not do argument dependent lookup if regular
7314 lookup finds a member function or a block-scope
7315 function declaration. [basic.lookup.argdep]/3 */
7316 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7317 || DECL_FUNCTION_MEMBER_P (fn)
7318 || DECL_LOCAL_FUNCTION_P (fn)))
7319 {
7320 koenig_p = true;
7321 if (!any_type_dependent_arguments_p (args))
7322 postfix_expression
7323 = perform_koenig_lookup (postfix_expression, args,
7324 complain);
7325 }
7326 }
7327 }
7328
7329 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7330 {
7331 tree instance = TREE_OPERAND (postfix_expression, 0);
7332 tree fn = TREE_OPERAND (postfix_expression, 1);
7333
7334 if (processing_template_decl
7335 && (type_dependent_object_expression_p (instance)
7336 || (!BASELINK_P (fn)
7337 && TREE_CODE (fn) != FIELD_DECL)
7338 || type_dependent_expression_p (fn)
7339 || any_type_dependent_arguments_p (args)))
7340 {
7341 maybe_generic_this_capture (instance, fn);
7342 postfix_expression
7343 = build_min_nt_call_vec (postfix_expression, args);
7344 release_tree_vector (args);
7345 break;
7346 }
7347
7348 if (BASELINK_P (fn))
7349 {
7350 postfix_expression
7351 = (build_new_method_call
7352 (instance, fn, &args, NULL_TREE,
7353 (idk == CP_ID_KIND_QUALIFIED
7354 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7355 : LOOKUP_NORMAL),
7356 /*fn_p=*/NULL,
7357 complain));
7358 }
7359 else
7360 postfix_expression
7361 = finish_call_expr (postfix_expression, &args,
7362 /*disallow_virtual=*/false,
7363 /*koenig_p=*/false,
7364 complain);
7365 }
7366 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7367 || TREE_CODE (postfix_expression) == MEMBER_REF
7368 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7369 postfix_expression = (build_offset_ref_call_from_tree
7370 (postfix_expression, &args,
7371 complain));
7372 else if (idk == CP_ID_KIND_QUALIFIED)
7373 /* A call to a static class member, or a namespace-scope
7374 function. */
7375 postfix_expression
7376 = finish_call_expr (postfix_expression, &args,
7377 /*disallow_virtual=*/true,
7378 koenig_p,
7379 complain);
7380 else
7381 /* All other function calls. */
7382 postfix_expression
7383 = finish_call_expr (postfix_expression, &args,
7384 /*disallow_virtual=*/false,
7385 koenig_p,
7386 complain);
7387
7388 if (close_paren_loc != UNKNOWN_LOCATION)
7389 {
7390 location_t combined_loc = make_location (token->location,
7391 start_loc,
7392 close_paren_loc);
7393 postfix_expression.set_location (combined_loc);
7394 }
7395
7396 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7397 idk = CP_ID_KIND_NONE;
7398
7399 release_tree_vector (args);
7400 }
7401 break;
7402
7403 case CPP_DOT:
7404 case CPP_DEREF:
7405 /* postfix-expression . template [opt] id-expression
7406 postfix-expression . pseudo-destructor-name
7407 postfix-expression -> template [opt] id-expression
7408 postfix-expression -> pseudo-destructor-name */
7409
7410 /* Consume the `.' or `->' operator. */
7411 cp_lexer_consume_token (parser->lexer);
7412
7413 postfix_expression
7414 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7415 postfix_expression,
7416 false, &idk, loc);
7417
7418 is_member_access = true;
7419 break;
7420
7421 case CPP_PLUS_PLUS:
7422 /* postfix-expression ++ */
7423 /* Consume the `++' token. */
7424 cp_lexer_consume_token (parser->lexer);
7425 /* Generate a representation for the complete expression. */
7426 postfix_expression
7427 = finish_increment_expr (postfix_expression,
7428 POSTINCREMENT_EXPR);
7429 /* Increments may not appear in constant-expressions. */
7430 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7431 postfix_expression = error_mark_node;
7432 idk = CP_ID_KIND_NONE;
7433 is_member_access = false;
7434 break;
7435
7436 case CPP_MINUS_MINUS:
7437 /* postfix-expression -- */
7438 /* Consume the `--' token. */
7439 cp_lexer_consume_token (parser->lexer);
7440 /* Generate a representation for the complete expression. */
7441 postfix_expression
7442 = finish_increment_expr (postfix_expression,
7443 POSTDECREMENT_EXPR);
7444 /* Decrements may not appear in constant-expressions. */
7445 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7446 postfix_expression = error_mark_node;
7447 idk = CP_ID_KIND_NONE;
7448 is_member_access = false;
7449 break;
7450
7451 default:
7452 if (pidk_return != NULL)
7453 * pidk_return = idk;
7454 if (member_access_only_p)
7455 return is_member_access
7456 ? postfix_expression
7457 : cp_expr (error_mark_node);
7458 else
7459 return postfix_expression;
7460 }
7461 }
7462
7463 /* We should never get here. */
7464 gcc_unreachable ();
7465 return error_mark_node;
7466 }
7467
7468 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7469 by cp_parser_builtin_offsetof. We're looking for
7470
7471 postfix-expression [ expression ]
7472 postfix-expression [ braced-init-list ] (C++11)
7473
7474 FOR_OFFSETOF is set if we're being called in that context, which
7475 changes how we deal with integer constant expressions. */
7476
7477 static tree
7478 cp_parser_postfix_open_square_expression (cp_parser *parser,
7479 tree postfix_expression,
7480 bool for_offsetof,
7481 bool decltype_p)
7482 {
7483 tree index = NULL_TREE;
7484 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7485 bool saved_greater_than_is_operator_p;
7486
7487 /* Consume the `[' token. */
7488 cp_lexer_consume_token (parser->lexer);
7489
7490 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7491 parser->greater_than_is_operator_p = true;
7492
7493 /* Parse the index expression. */
7494 /* ??? For offsetof, there is a question of what to allow here. If
7495 offsetof is not being used in an integral constant expression context,
7496 then we *could* get the right answer by computing the value at runtime.
7497 If we are in an integral constant expression context, then we might
7498 could accept any constant expression; hard to say without analysis.
7499 Rather than open the barn door too wide right away, allow only integer
7500 constant expressions here. */
7501 if (for_offsetof)
7502 index = cp_parser_constant_expression (parser);
7503 else
7504 {
7505 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7506 {
7507 bool expr_nonconst_p;
7508 cp_lexer_set_source_position (parser->lexer);
7509 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7510 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7511 }
7512 else
7513 index = cp_parser_expression (parser);
7514 }
7515
7516 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7517
7518 /* Look for the closing `]'. */
7519 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7520
7521 /* Build the ARRAY_REF. */
7522 postfix_expression = grok_array_decl (loc, postfix_expression,
7523 index, decltype_p);
7524
7525 /* When not doing offsetof, array references are not permitted in
7526 constant-expressions. */
7527 if (!for_offsetof
7528 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7529 postfix_expression = error_mark_node;
7530
7531 return postfix_expression;
7532 }
7533
7534 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7535 dereference of incomplete type, returns true if error_mark_node should
7536 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7537 and *DEPENDENT_P. */
7538
7539 bool
7540 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7541 bool *dependent_p)
7542 {
7543 /* In a template, be permissive by treating an object expression
7544 of incomplete type as dependent (after a pedwarn). */
7545 diagnostic_t kind = (processing_template_decl
7546 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7547
7548 switch (TREE_CODE (*postfix_expression))
7549 {
7550 case CAST_EXPR:
7551 case REINTERPRET_CAST_EXPR:
7552 case CONST_CAST_EXPR:
7553 case STATIC_CAST_EXPR:
7554 case DYNAMIC_CAST_EXPR:
7555 case IMPLICIT_CONV_EXPR:
7556 case VIEW_CONVERT_EXPR:
7557 case NON_LVALUE_EXPR:
7558 kind = DK_ERROR;
7559 break;
7560 case OVERLOAD:
7561 /* Don't emit any diagnostic for OVERLOADs. */
7562 kind = DK_IGNORED;
7563 break;
7564 default:
7565 /* Avoid clobbering e.g. DECLs. */
7566 if (!EXPR_P (*postfix_expression))
7567 kind = DK_ERROR;
7568 break;
7569 }
7570
7571 if (kind == DK_IGNORED)
7572 return false;
7573
7574 location_t exploc = location_of (*postfix_expression);
7575 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7576 if (!MAYBE_CLASS_TYPE_P (*scope))
7577 return true;
7578 if (kind == DK_ERROR)
7579 *scope = *postfix_expression = error_mark_node;
7580 else if (processing_template_decl)
7581 {
7582 *dependent_p = true;
7583 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7584 }
7585 return false;
7586 }
7587
7588 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7589 by cp_parser_builtin_offsetof. We're looking for
7590
7591 postfix-expression . template [opt] id-expression
7592 postfix-expression . pseudo-destructor-name
7593 postfix-expression -> template [opt] id-expression
7594 postfix-expression -> pseudo-destructor-name
7595
7596 FOR_OFFSETOF is set if we're being called in that context. That sorta
7597 limits what of the above we'll actually accept, but nevermind.
7598 TOKEN_TYPE is the "." or "->" token, which will already have been
7599 removed from the stream. */
7600
7601 static tree
7602 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7603 enum cpp_ttype token_type,
7604 cp_expr postfix_expression,
7605 bool for_offsetof, cp_id_kind *idk,
7606 location_t location)
7607 {
7608 tree name;
7609 bool dependent_p;
7610 bool pseudo_destructor_p;
7611 tree scope = NULL_TREE;
7612 location_t start_loc = postfix_expression.get_start ();
7613
7614 /* If this is a `->' operator, dereference the pointer. */
7615 if (token_type == CPP_DEREF)
7616 postfix_expression = build_x_arrow (location, postfix_expression,
7617 tf_warning_or_error);
7618 /* Check to see whether or not the expression is type-dependent and
7619 not the current instantiation. */
7620 dependent_p = type_dependent_object_expression_p (postfix_expression);
7621 /* The identifier following the `->' or `.' is not qualified. */
7622 parser->scope = NULL_TREE;
7623 parser->qualifying_scope = NULL_TREE;
7624 parser->object_scope = NULL_TREE;
7625 *idk = CP_ID_KIND_NONE;
7626
7627 /* Enter the scope corresponding to the type of the object
7628 given by the POSTFIX_EXPRESSION. */
7629 if (!dependent_p)
7630 {
7631 scope = TREE_TYPE (postfix_expression);
7632 /* According to the standard, no expression should ever have
7633 reference type. Unfortunately, we do not currently match
7634 the standard in this respect in that our internal representation
7635 of an expression may have reference type even when the standard
7636 says it does not. Therefore, we have to manually obtain the
7637 underlying type here. */
7638 scope = non_reference (scope);
7639 /* The type of the POSTFIX_EXPRESSION must be complete. */
7640 /* Unlike the object expression in other contexts, *this is not
7641 required to be of complete type for purposes of class member
7642 access (5.2.5) outside the member function body. */
7643 if (postfix_expression != current_class_ref
7644 && scope != error_mark_node
7645 && !currently_open_class (scope))
7646 {
7647 scope = complete_type (scope);
7648 if (!COMPLETE_TYPE_P (scope)
7649 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7650 &dependent_p))
7651 return error_mark_node;
7652 }
7653
7654 if (!dependent_p)
7655 {
7656 /* Let the name lookup machinery know that we are processing a
7657 class member access expression. */
7658 parser->context->object_type = scope;
7659 /* If something went wrong, we want to be able to discern that case,
7660 as opposed to the case where there was no SCOPE due to the type
7661 of expression being dependent. */
7662 if (!scope)
7663 scope = error_mark_node;
7664 /* If the SCOPE was erroneous, make the various semantic analysis
7665 functions exit quickly -- and without issuing additional error
7666 messages. */
7667 if (scope == error_mark_node)
7668 postfix_expression = error_mark_node;
7669 }
7670 }
7671
7672 if (dependent_p)
7673 /* Tell cp_parser_lookup_name that there was an object, even though it's
7674 type-dependent. */
7675 parser->context->object_type = unknown_type_node;
7676
7677 /* Assume this expression is not a pseudo-destructor access. */
7678 pseudo_destructor_p = false;
7679
7680 /* If the SCOPE is a scalar type, then, if this is a valid program,
7681 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7682 is type dependent, it can be pseudo-destructor-name or something else.
7683 Try to parse it as pseudo-destructor-name first. */
7684 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7685 {
7686 tree s;
7687 tree type;
7688
7689 cp_parser_parse_tentatively (parser);
7690 /* Parse the pseudo-destructor-name. */
7691 s = NULL_TREE;
7692 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7693 &s, &type);
7694 if (dependent_p
7695 && (cp_parser_error_occurred (parser)
7696 || !SCALAR_TYPE_P (type)))
7697 cp_parser_abort_tentative_parse (parser);
7698 else if (cp_parser_parse_definitely (parser))
7699 {
7700 pseudo_destructor_p = true;
7701 postfix_expression
7702 = finish_pseudo_destructor_expr (postfix_expression,
7703 s, type, location);
7704 }
7705 }
7706
7707 if (!pseudo_destructor_p)
7708 {
7709 /* If the SCOPE is not a scalar type, we are looking at an
7710 ordinary class member access expression, rather than a
7711 pseudo-destructor-name. */
7712 bool template_p;
7713 cp_token *token = cp_lexer_peek_token (parser->lexer);
7714 /* Parse the id-expression. */
7715 name = (cp_parser_id_expression
7716 (parser,
7717 cp_parser_optional_template_keyword (parser),
7718 /*check_dependency_p=*/true,
7719 &template_p,
7720 /*declarator_p=*/false,
7721 /*optional_p=*/false));
7722 /* In general, build a SCOPE_REF if the member name is qualified.
7723 However, if the name was not dependent and has already been
7724 resolved; there is no need to build the SCOPE_REF. For example;
7725
7726 struct X { void f(); };
7727 template <typename T> void f(T* t) { t->X::f(); }
7728
7729 Even though "t" is dependent, "X::f" is not and has been resolved
7730 to a BASELINK; there is no need to include scope information. */
7731
7732 /* But we do need to remember that there was an explicit scope for
7733 virtual function calls. */
7734 if (parser->scope)
7735 *idk = CP_ID_KIND_QUALIFIED;
7736
7737 /* If the name is a template-id that names a type, we will get a
7738 TYPE_DECL here. That is invalid code. */
7739 if (TREE_CODE (name) == TYPE_DECL)
7740 {
7741 error_at (token->location, "invalid use of %qD", name);
7742 postfix_expression = error_mark_node;
7743 }
7744 else
7745 {
7746 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7747 {
7748 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7749 {
7750 error_at (token->location, "%<%D::%D%> is not a class member",
7751 parser->scope, name);
7752 postfix_expression = error_mark_node;
7753 }
7754 else
7755 name = build_qualified_name (/*type=*/NULL_TREE,
7756 parser->scope,
7757 name,
7758 template_p);
7759 parser->scope = NULL_TREE;
7760 parser->qualifying_scope = NULL_TREE;
7761 parser->object_scope = NULL_TREE;
7762 }
7763 if (parser->scope && name && BASELINK_P (name))
7764 adjust_result_of_qualified_name_lookup
7765 (name, parser->scope, scope);
7766 postfix_expression
7767 = finish_class_member_access_expr (postfix_expression, name,
7768 template_p,
7769 tf_warning_or_error);
7770 /* Build a location e.g.:
7771 ptr->access_expr
7772 ~~~^~~~~~~~~~~~~
7773 where the caret is at the deref token, ranging from
7774 the start of postfix_expression to the end of the access expr. */
7775 location_t end_loc
7776 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7777 location_t combined_loc
7778 = make_location (input_location, start_loc, end_loc);
7779 protected_set_expr_location (postfix_expression, combined_loc);
7780 }
7781 }
7782
7783 /* We no longer need to look up names in the scope of the object on
7784 the left-hand side of the `.' or `->' operator. */
7785 parser->context->object_type = NULL_TREE;
7786
7787 /* Outside of offsetof, these operators may not appear in
7788 constant-expressions. */
7789 if (!for_offsetof
7790 && (cp_parser_non_integral_constant_expression
7791 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7792 postfix_expression = error_mark_node;
7793
7794 return postfix_expression;
7795 }
7796
7797 /* Parse a parenthesized expression-list.
7798
7799 expression-list:
7800 assignment-expression
7801 expression-list, assignment-expression
7802
7803 attribute-list:
7804 expression-list
7805 identifier
7806 identifier, expression-list
7807
7808 CAST_P is true if this expression is the target of a cast.
7809
7810 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7811 argument pack.
7812
7813 WRAP_LOCATIONS_P is true if expressions within this list for which
7814 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7815 their source locations.
7816
7817 Returns a vector of trees. Each element is a representation of an
7818 assignment-expression. NULL is returned if the ( and or ) are
7819 missing. An empty, but allocated, vector is returned on no
7820 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7821 if we are parsing an attribute list for an attribute that wants a
7822 plain identifier argument, normal_attr for an attribute that wants
7823 an expression, or non_attr if we aren't parsing an attribute list. If
7824 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7825 not all of the expressions in the list were constant.
7826 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7827 will be written to with the location of the closing parenthesis. If
7828 an error occurs, it may or may not be written to. */
7829
7830 static vec<tree, va_gc> *
7831 cp_parser_parenthesized_expression_list (cp_parser* parser,
7832 int is_attribute_list,
7833 bool cast_p,
7834 bool allow_expansion_p,
7835 bool *non_constant_p,
7836 location_t *close_paren_loc,
7837 bool wrap_locations_p)
7838 {
7839 vec<tree, va_gc> *expression_list;
7840 bool fold_expr_p = is_attribute_list != non_attr;
7841 tree identifier = NULL_TREE;
7842 bool saved_greater_than_is_operator_p;
7843
7844 /* Assume all the expressions will be constant. */
7845 if (non_constant_p)
7846 *non_constant_p = false;
7847
7848 matching_parens parens;
7849 if (!parens.require_open (parser))
7850 return NULL;
7851
7852 expression_list = make_tree_vector ();
7853
7854 /* Within a parenthesized expression, a `>' token is always
7855 the greater-than operator. */
7856 saved_greater_than_is_operator_p
7857 = parser->greater_than_is_operator_p;
7858 parser->greater_than_is_operator_p = true;
7859
7860 cp_expr expr (NULL_TREE);
7861
7862 /* Consume expressions until there are no more. */
7863 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7864 while (true)
7865 {
7866 /* At the beginning of attribute lists, check to see if the
7867 next token is an identifier. */
7868 if (is_attribute_list == id_attr
7869 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7870 {
7871 cp_token *token;
7872
7873 /* Consume the identifier. */
7874 token = cp_lexer_consume_token (parser->lexer);
7875 /* Save the identifier. */
7876 identifier = token->u.value;
7877 }
7878 else
7879 {
7880 bool expr_non_constant_p;
7881
7882 /* Parse the next assignment-expression. */
7883 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7884 {
7885 /* A braced-init-list. */
7886 cp_lexer_set_source_position (parser->lexer);
7887 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7888 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7889 if (non_constant_p && expr_non_constant_p)
7890 *non_constant_p = true;
7891 }
7892 else if (non_constant_p)
7893 {
7894 expr = (cp_parser_constant_expression
7895 (parser, /*allow_non_constant_p=*/true,
7896 &expr_non_constant_p));
7897 if (expr_non_constant_p)
7898 *non_constant_p = true;
7899 }
7900 else
7901 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7902 cast_p);
7903
7904 if (fold_expr_p)
7905 expr = instantiate_non_dependent_expr (expr);
7906
7907 /* If we have an ellipsis, then this is an expression
7908 expansion. */
7909 if (allow_expansion_p
7910 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7911 {
7912 /* Consume the `...'. */
7913 cp_lexer_consume_token (parser->lexer);
7914
7915 /* Build the argument pack. */
7916 expr = make_pack_expansion (expr);
7917 }
7918
7919 if (wrap_locations_p)
7920 expr.maybe_add_location_wrapper ();
7921
7922 /* Add it to the list. We add error_mark_node
7923 expressions to the list, so that we can still tell if
7924 the correct form for a parenthesized expression-list
7925 is found. That gives better errors. */
7926 vec_safe_push (expression_list, expr.get_value ());
7927
7928 if (expr == error_mark_node)
7929 goto skip_comma;
7930 }
7931
7932 /* After the first item, attribute lists look the same as
7933 expression lists. */
7934 is_attribute_list = non_attr;
7935
7936 get_comma:;
7937 /* If the next token isn't a `,', then we are done. */
7938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7939 break;
7940
7941 /* Otherwise, consume the `,' and keep going. */
7942 cp_lexer_consume_token (parser->lexer);
7943 }
7944
7945 if (close_paren_loc)
7946 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7947
7948 if (!parens.require_close (parser))
7949 {
7950 int ending;
7951
7952 skip_comma:;
7953 /* We try and resync to an unnested comma, as that will give the
7954 user better diagnostics. */
7955 ending = cp_parser_skip_to_closing_parenthesis (parser,
7956 /*recovering=*/true,
7957 /*or_comma=*/true,
7958 /*consume_paren=*/true);
7959 if (ending < 0)
7960 goto get_comma;
7961 if (!ending)
7962 {
7963 parser->greater_than_is_operator_p
7964 = saved_greater_than_is_operator_p;
7965 return NULL;
7966 }
7967 }
7968
7969 parser->greater_than_is_operator_p
7970 = saved_greater_than_is_operator_p;
7971
7972 if (identifier)
7973 vec_safe_insert (expression_list, 0, identifier);
7974
7975 return expression_list;
7976 }
7977
7978 /* Parse a pseudo-destructor-name.
7979
7980 pseudo-destructor-name:
7981 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7982 :: [opt] nested-name-specifier template template-id :: ~ type-name
7983 :: [opt] nested-name-specifier [opt] ~ type-name
7984
7985 If either of the first two productions is used, sets *SCOPE to the
7986 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7987 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7988 or ERROR_MARK_NODE if the parse fails. */
7989
7990 static void
7991 cp_parser_pseudo_destructor_name (cp_parser* parser,
7992 tree object,
7993 tree* scope,
7994 tree* type)
7995 {
7996 bool nested_name_specifier_p;
7997
7998 /* Handle ~auto. */
7999 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8000 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8001 && !type_dependent_expression_p (object))
8002 {
8003 if (cxx_dialect < cxx14)
8004 pedwarn (input_location, 0,
8005 "%<~auto%> only available with "
8006 "%<-std=c++14%> or %<-std=gnu++14%>");
8007 cp_lexer_consume_token (parser->lexer);
8008 cp_lexer_consume_token (parser->lexer);
8009 *scope = NULL_TREE;
8010 *type = TREE_TYPE (object);
8011 return;
8012 }
8013
8014 /* Assume that things will not work out. */
8015 *type = error_mark_node;
8016
8017 /* Look for the optional `::' operator. */
8018 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8019 /* Look for the optional nested-name-specifier. */
8020 nested_name_specifier_p
8021 = (cp_parser_nested_name_specifier_opt (parser,
8022 /*typename_keyword_p=*/false,
8023 /*check_dependency_p=*/true,
8024 /*type_p=*/false,
8025 /*is_declaration=*/false)
8026 != NULL_TREE);
8027 /* Now, if we saw a nested-name-specifier, we might be doing the
8028 second production. */
8029 if (nested_name_specifier_p
8030 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8031 {
8032 /* Consume the `template' keyword. */
8033 cp_lexer_consume_token (parser->lexer);
8034 /* Parse the template-id. */
8035 cp_parser_template_id (parser,
8036 /*template_keyword_p=*/true,
8037 /*check_dependency_p=*/false,
8038 class_type,
8039 /*is_declaration=*/true);
8040 /* Look for the `::' token. */
8041 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8042 }
8043 /* If the next token is not a `~', then there might be some
8044 additional qualification. */
8045 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8046 {
8047 /* At this point, we're looking for "type-name :: ~". The type-name
8048 must not be a class-name, since this is a pseudo-destructor. So,
8049 it must be either an enum-name, or a typedef-name -- both of which
8050 are just identifiers. So, we peek ahead to check that the "::"
8051 and "~" tokens are present; if they are not, then we can avoid
8052 calling type_name. */
8053 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8054 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8055 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8056 {
8057 cp_parser_error (parser, "non-scalar type");
8058 return;
8059 }
8060
8061 /* Look for the type-name. */
8062 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8063 if (*scope == error_mark_node)
8064 return;
8065
8066 /* Look for the `::' token. */
8067 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8068 }
8069 else
8070 *scope = NULL_TREE;
8071
8072 /* Look for the `~'. */
8073 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8074
8075 /* Once we see the ~, this has to be a pseudo-destructor. */
8076 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8077 cp_parser_commit_to_topmost_tentative_parse (parser);
8078
8079 /* Look for the type-name again. We are not responsible for
8080 checking that it matches the first type-name. */
8081 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8082 }
8083
8084 /* Parse a unary-expression.
8085
8086 unary-expression:
8087 postfix-expression
8088 ++ cast-expression
8089 -- cast-expression
8090 unary-operator cast-expression
8091 sizeof unary-expression
8092 sizeof ( type-id )
8093 alignof ( type-id ) [C++0x]
8094 new-expression
8095 delete-expression
8096
8097 GNU Extensions:
8098
8099 unary-expression:
8100 __extension__ cast-expression
8101 __alignof__ unary-expression
8102 __alignof__ ( type-id )
8103 alignof unary-expression [C++0x]
8104 __real__ cast-expression
8105 __imag__ cast-expression
8106 && identifier
8107 sizeof ( type-id ) { initializer-list , [opt] }
8108 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8109 __alignof__ ( type-id ) { initializer-list , [opt] }
8110
8111 ADDRESS_P is true iff the unary-expression is appearing as the
8112 operand of the `&' operator. CAST_P is true if this expression is
8113 the target of a cast.
8114
8115 Returns a representation of the expression. */
8116
8117 static cp_expr
8118 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8119 bool address_p, bool cast_p, bool decltype_p)
8120 {
8121 cp_token *token;
8122 enum tree_code unary_operator;
8123
8124 /* Peek at the next token. */
8125 token = cp_lexer_peek_token (parser->lexer);
8126 /* Some keywords give away the kind of expression. */
8127 if (token->type == CPP_KEYWORD)
8128 {
8129 enum rid keyword = token->keyword;
8130
8131 switch (keyword)
8132 {
8133 case RID_ALIGNOF:
8134 case RID_SIZEOF:
8135 {
8136 tree operand, ret;
8137 enum tree_code op;
8138 location_t start_loc = token->location;
8139
8140 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8141 bool std_alignof = id_equal (token->u.value, "alignof");
8142
8143 /* Consume the token. */
8144 cp_lexer_consume_token (parser->lexer);
8145 /* Parse the operand. */
8146 operand = cp_parser_sizeof_operand (parser, keyword);
8147
8148 if (TYPE_P (operand))
8149 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8150 true);
8151 else
8152 {
8153 /* ISO C++ defines alignof only with types, not with
8154 expressions. So pedwarn if alignof is used with a non-
8155 type expression. However, __alignof__ is ok. */
8156 if (std_alignof)
8157 pedwarn (token->location, OPT_Wpedantic,
8158 "ISO C++ does not allow %<alignof%> "
8159 "with a non-type");
8160
8161 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8162 }
8163 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8164 SIZEOF_EXPR with the original operand. */
8165 if (op == SIZEOF_EXPR && ret != error_mark_node)
8166 {
8167 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8168 {
8169 if (!processing_template_decl && TYPE_P (operand))
8170 {
8171 ret = build_min (SIZEOF_EXPR, size_type_node,
8172 build1 (NOP_EXPR, operand,
8173 error_mark_node));
8174 SIZEOF_EXPR_TYPE_P (ret) = 1;
8175 }
8176 else
8177 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8178 TREE_SIDE_EFFECTS (ret) = 0;
8179 TREE_READONLY (ret) = 1;
8180 }
8181 }
8182
8183 /* Construct a location e.g. :
8184 alignof (expr)
8185 ^~~~~~~~~~~~~~
8186 with start == caret at the start of the "alignof"/"sizeof"
8187 token, with the endpoint at the final closing paren. */
8188 location_t finish_loc
8189 = cp_lexer_previous_token (parser->lexer)->location;
8190 location_t compound_loc
8191 = make_location (start_loc, start_loc, finish_loc);
8192
8193 cp_expr ret_expr (ret);
8194 ret_expr.set_location (compound_loc);
8195 ret_expr = ret_expr.maybe_add_location_wrapper ();
8196 return ret_expr;
8197 }
8198
8199 case RID_BUILTIN_HAS_ATTRIBUTE:
8200 return cp_parser_has_attribute_expression (parser);
8201
8202 case RID_NEW:
8203 return cp_parser_new_expression (parser);
8204
8205 case RID_DELETE:
8206 return cp_parser_delete_expression (parser);
8207
8208 case RID_EXTENSION:
8209 {
8210 /* The saved value of the PEDANTIC flag. */
8211 int saved_pedantic;
8212 tree expr;
8213
8214 /* Save away the PEDANTIC flag. */
8215 cp_parser_extension_opt (parser, &saved_pedantic);
8216 /* Parse the cast-expression. */
8217 expr = cp_parser_simple_cast_expression (parser);
8218 /* Restore the PEDANTIC flag. */
8219 pedantic = saved_pedantic;
8220
8221 return expr;
8222 }
8223
8224 case RID_REALPART:
8225 case RID_IMAGPART:
8226 {
8227 tree expression;
8228
8229 /* Consume the `__real__' or `__imag__' token. */
8230 cp_lexer_consume_token (parser->lexer);
8231 /* Parse the cast-expression. */
8232 expression = cp_parser_simple_cast_expression (parser);
8233 /* Create the complete representation. */
8234 return build_x_unary_op (token->location,
8235 (keyword == RID_REALPART
8236 ? REALPART_EXPR : IMAGPART_EXPR),
8237 expression,
8238 tf_warning_or_error);
8239 }
8240 break;
8241
8242 case RID_TRANSACTION_ATOMIC:
8243 case RID_TRANSACTION_RELAXED:
8244 return cp_parser_transaction_expression (parser, keyword);
8245
8246 case RID_NOEXCEPT:
8247 {
8248 tree expr;
8249 const char *saved_message;
8250 bool saved_integral_constant_expression_p;
8251 bool saved_non_integral_constant_expression_p;
8252 bool saved_greater_than_is_operator_p;
8253
8254 location_t start_loc = token->location;
8255
8256 cp_lexer_consume_token (parser->lexer);
8257 matching_parens parens;
8258 parens.require_open (parser);
8259
8260 saved_message = parser->type_definition_forbidden_message;
8261 parser->type_definition_forbidden_message
8262 = G_("types may not be defined in %<noexcept%> expressions");
8263
8264 saved_integral_constant_expression_p
8265 = parser->integral_constant_expression_p;
8266 saved_non_integral_constant_expression_p
8267 = parser->non_integral_constant_expression_p;
8268 parser->integral_constant_expression_p = false;
8269
8270 saved_greater_than_is_operator_p
8271 = parser->greater_than_is_operator_p;
8272 parser->greater_than_is_operator_p = true;
8273
8274 ++cp_unevaluated_operand;
8275 ++c_inhibit_evaluation_warnings;
8276 ++cp_noexcept_operand;
8277 expr = cp_parser_expression (parser);
8278 --cp_noexcept_operand;
8279 --c_inhibit_evaluation_warnings;
8280 --cp_unevaluated_operand;
8281
8282 parser->greater_than_is_operator_p
8283 = saved_greater_than_is_operator_p;
8284
8285 parser->integral_constant_expression_p
8286 = saved_integral_constant_expression_p;
8287 parser->non_integral_constant_expression_p
8288 = saved_non_integral_constant_expression_p;
8289
8290 parser->type_definition_forbidden_message = saved_message;
8291
8292 location_t finish_loc
8293 = cp_lexer_peek_token (parser->lexer)->location;
8294 parens.require_close (parser);
8295
8296 /* Construct a location of the form:
8297 noexcept (expr)
8298 ^~~~~~~~~~~~~~~
8299 with start == caret, finishing at the close-paren. */
8300 location_t noexcept_loc
8301 = make_location (start_loc, start_loc, finish_loc);
8302
8303 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8304 noexcept_loc);
8305 }
8306
8307 default:
8308 break;
8309 }
8310 }
8311
8312 /* Look for the `:: new' and `:: delete', which also signal the
8313 beginning of a new-expression, or delete-expression,
8314 respectively. If the next token is `::', then it might be one of
8315 these. */
8316 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8317 {
8318 enum rid keyword;
8319
8320 /* See if the token after the `::' is one of the keywords in
8321 which we're interested. */
8322 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8323 /* If it's `new', we have a new-expression. */
8324 if (keyword == RID_NEW)
8325 return cp_parser_new_expression (parser);
8326 /* Similarly, for `delete'. */
8327 else if (keyword == RID_DELETE)
8328 return cp_parser_delete_expression (parser);
8329 }
8330
8331 /* Look for a unary operator. */
8332 unary_operator = cp_parser_unary_operator (token);
8333 /* The `++' and `--' operators can be handled similarly, even though
8334 they are not technically unary-operators in the grammar. */
8335 if (unary_operator == ERROR_MARK)
8336 {
8337 if (token->type == CPP_PLUS_PLUS)
8338 unary_operator = PREINCREMENT_EXPR;
8339 else if (token->type == CPP_MINUS_MINUS)
8340 unary_operator = PREDECREMENT_EXPR;
8341 /* Handle the GNU address-of-label extension. */
8342 else if (cp_parser_allow_gnu_extensions_p (parser)
8343 && token->type == CPP_AND_AND)
8344 {
8345 tree identifier;
8346 tree expression;
8347 location_t start_loc = token->location;
8348
8349 /* Consume the '&&' token. */
8350 cp_lexer_consume_token (parser->lexer);
8351 /* Look for the identifier. */
8352 location_t finish_loc
8353 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8354 identifier = cp_parser_identifier (parser);
8355 /* Construct a location of the form:
8356 &&label
8357 ^~~~~~~
8358 with caret==start at the "&&", finish at the end of the label. */
8359 location_t combined_loc
8360 = make_location (start_loc, start_loc, finish_loc);
8361 /* Create an expression representing the address. */
8362 expression = finish_label_address_expr (identifier, combined_loc);
8363 if (cp_parser_non_integral_constant_expression (parser,
8364 NIC_ADDR_LABEL))
8365 expression = error_mark_node;
8366 return expression;
8367 }
8368 }
8369 if (unary_operator != ERROR_MARK)
8370 {
8371 cp_expr cast_expression;
8372 cp_expr expression = error_mark_node;
8373 non_integral_constant non_constant_p = NIC_NONE;
8374 location_t loc = token->location;
8375 tsubst_flags_t complain = complain_flags (decltype_p);
8376
8377 /* Consume the operator token. */
8378 token = cp_lexer_consume_token (parser->lexer);
8379 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8380
8381 /* Parse the cast-expression. */
8382 cast_expression
8383 = cp_parser_cast_expression (parser,
8384 unary_operator == ADDR_EXPR,
8385 /*cast_p=*/false,
8386 /*decltype*/false,
8387 pidk);
8388
8389 /* Make a location:
8390 OP_TOKEN CAST_EXPRESSION
8391 ^~~~~~~~~~~~~~~~~~~~~~~~~
8392 with start==caret at the operator token, and
8393 extending to the end of the cast_expression. */
8394 loc = make_location (loc, loc, cast_expression.get_finish ());
8395
8396 /* Now, build an appropriate representation. */
8397 switch (unary_operator)
8398 {
8399 case INDIRECT_REF:
8400 non_constant_p = NIC_STAR;
8401 expression = build_x_indirect_ref (loc, cast_expression,
8402 RO_UNARY_STAR,
8403 complain);
8404 /* TODO: build_x_indirect_ref does not always honor the
8405 location, so ensure it is set. */
8406 expression.set_location (loc);
8407 break;
8408
8409 case ADDR_EXPR:
8410 non_constant_p = NIC_ADDR;
8411 /* Fall through. */
8412 case BIT_NOT_EXPR:
8413 expression = build_x_unary_op (loc, unary_operator,
8414 cast_expression,
8415 complain);
8416 /* TODO: build_x_unary_op does not always honor the location,
8417 so ensure it is set. */
8418 expression.set_location (loc);
8419 break;
8420
8421 case PREINCREMENT_EXPR:
8422 case PREDECREMENT_EXPR:
8423 non_constant_p = unary_operator == PREINCREMENT_EXPR
8424 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8425 /* Fall through. */
8426 case NEGATE_EXPR:
8427 /* Immediately fold negation of a constant, unless the constant is 0
8428 (since -0 == 0) or it would overflow. */
8429 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8430 {
8431 tree stripped_expr
8432 = tree_strip_any_location_wrapper (cast_expression);
8433 if (CONSTANT_CLASS_P (stripped_expr)
8434 && !integer_zerop (stripped_expr)
8435 && !TREE_OVERFLOW (stripped_expr))
8436 {
8437 tree folded = fold_build1 (unary_operator,
8438 TREE_TYPE (stripped_expr),
8439 stripped_expr);
8440 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8441 {
8442 expression = maybe_wrap_with_location (folded, loc);
8443 break;
8444 }
8445 }
8446 }
8447 /* Fall through. */
8448 case UNARY_PLUS_EXPR:
8449 case TRUTH_NOT_EXPR:
8450 expression = finish_unary_op_expr (loc, unary_operator,
8451 cast_expression, complain);
8452 break;
8453
8454 default:
8455 gcc_unreachable ();
8456 }
8457
8458 if (non_constant_p != NIC_NONE
8459 && cp_parser_non_integral_constant_expression (parser,
8460 non_constant_p))
8461 expression = error_mark_node;
8462
8463 return expression;
8464 }
8465
8466 return cp_parser_postfix_expression (parser, address_p, cast_p,
8467 /*member_access_only_p=*/false,
8468 decltype_p,
8469 pidk);
8470 }
8471
8472 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8473 unary-operator, the corresponding tree code is returned. */
8474
8475 static enum tree_code
8476 cp_parser_unary_operator (cp_token* token)
8477 {
8478 switch (token->type)
8479 {
8480 case CPP_MULT:
8481 return INDIRECT_REF;
8482
8483 case CPP_AND:
8484 return ADDR_EXPR;
8485
8486 case CPP_PLUS:
8487 return UNARY_PLUS_EXPR;
8488
8489 case CPP_MINUS:
8490 return NEGATE_EXPR;
8491
8492 case CPP_NOT:
8493 return TRUTH_NOT_EXPR;
8494
8495 case CPP_COMPL:
8496 return BIT_NOT_EXPR;
8497
8498 default:
8499 return ERROR_MARK;
8500 }
8501 }
8502
8503 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8504 Returns a representation of the expression. */
8505
8506 static tree
8507 cp_parser_has_attribute_expression (cp_parser *parser)
8508 {
8509 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8510
8511 /* Consume the __builtin_has_attribute token. */
8512 cp_lexer_consume_token (parser->lexer);
8513
8514 matching_parens parens;
8515 if (!parens.require_open (parser))
8516 return error_mark_node;
8517
8518 /* Types cannot be defined in a `sizeof' expression. Save away the
8519 old message. */
8520 const char *saved_message = parser->type_definition_forbidden_message;
8521 /* And create the new one. */
8522 const int kwd = RID_BUILTIN_HAS_ATTRIBUTE;
8523 char *tmp = concat ("types may not be defined in %<",
8524 IDENTIFIER_POINTER (ridpointers[kwd]),
8525 "%> expressions", NULL);
8526 parser->type_definition_forbidden_message = tmp;
8527
8528 /* The restrictions on constant-expressions do not apply inside
8529 sizeof expressions. */
8530 bool saved_integral_constant_expression_p
8531 = parser->integral_constant_expression_p;
8532 bool saved_non_integral_constant_expression_p
8533 = parser->non_integral_constant_expression_p;
8534 parser->integral_constant_expression_p = false;
8535
8536 /* Do not actually evaluate the expression. */
8537 ++cp_unevaluated_operand;
8538 ++c_inhibit_evaluation_warnings;
8539
8540 tree oper = NULL_TREE;
8541
8542 /* We can't be sure yet whether we're looking at a type-id or an
8543 expression. */
8544 cp_parser_parse_tentatively (parser);
8545
8546 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8547 parser->in_type_id_in_expr_p = true;
8548 /* Look for the type-id. */
8549 oper = cp_parser_type_id (parser);
8550 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8551
8552 cp_parser_parse_definitely (parser);
8553
8554 /* If the type-id production did not work out, then we must be
8555 looking at the unary-expression production. */
8556 if (!oper || oper == error_mark_node)
8557 oper = cp_parser_unary_expression (parser);
8558
8559 STRIP_ANY_LOCATION_WRAPPER (oper);
8560
8561 /* Go back to evaluating expressions. */
8562 --cp_unevaluated_operand;
8563 --c_inhibit_evaluation_warnings;
8564
8565 /* Free the message we created. */
8566 free (tmp);
8567 /* And restore the old one. */
8568 parser->type_definition_forbidden_message = saved_message;
8569 parser->integral_constant_expression_p
8570 = saved_integral_constant_expression_p;
8571 parser->non_integral_constant_expression_p
8572 = saved_non_integral_constant_expression_p;
8573
8574 /* Consume the comma if it's there. */
8575 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8576 {
8577 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8578 /*consume_paren=*/true);
8579 return error_mark_node;
8580 }
8581
8582 /* Parse the attribute specification. */
8583 bool ret = false;
8584 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8585 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8586 {
8587 if (oper != error_mark_node)
8588 {
8589 /* Fold constant expressions used in attributes first. */
8590 cp_check_const_attributes (attr);
8591
8592 /* Finally, see if OPER has been declared with ATTR. */
8593 ret = has_attribute (atloc, oper, attr, default_conversion);
8594 }
8595
8596 parens.require_close (parser);
8597 }
8598 else
8599 {
8600 error_at (atloc, "expected identifier");
8601 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8602 }
8603
8604 /* Construct a location e.g. :
8605 __builtin_has_attribute (oper, attr)
8606 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8607 with start == caret at the start of the built-in token,
8608 and with the endpoint at the final closing paren. */
8609 location_t finish_loc
8610 = cp_lexer_previous_token (parser->lexer)->location;
8611 location_t compound_loc
8612 = make_location (start_loc, start_loc, finish_loc);
8613
8614 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8615 ret_expr.set_location (compound_loc);
8616 ret_expr = ret_expr.maybe_add_location_wrapper ();
8617 return ret_expr;
8618 }
8619
8620 /* Parse a new-expression.
8621
8622 new-expression:
8623 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8624 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8625
8626 Returns a representation of the expression. */
8627
8628 static tree
8629 cp_parser_new_expression (cp_parser* parser)
8630 {
8631 bool global_scope_p;
8632 vec<tree, va_gc> *placement;
8633 tree type;
8634 vec<tree, va_gc> *initializer;
8635 tree nelts = NULL_TREE;
8636 tree ret;
8637
8638 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8639
8640 /* Look for the optional `::' operator. */
8641 global_scope_p
8642 = (cp_parser_global_scope_opt (parser,
8643 /*current_scope_valid_p=*/false)
8644 != NULL_TREE);
8645 /* Look for the `new' operator. */
8646 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8647 /* There's no easy way to tell a new-placement from the
8648 `( type-id )' construct. */
8649 cp_parser_parse_tentatively (parser);
8650 /* Look for a new-placement. */
8651 placement = cp_parser_new_placement (parser);
8652 /* If that didn't work out, there's no new-placement. */
8653 if (!cp_parser_parse_definitely (parser))
8654 {
8655 if (placement != NULL)
8656 release_tree_vector (placement);
8657 placement = NULL;
8658 }
8659
8660 /* If the next token is a `(', then we have a parenthesized
8661 type-id. */
8662 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8663 {
8664 cp_token *token;
8665 const char *saved_message = parser->type_definition_forbidden_message;
8666
8667 /* Consume the `('. */
8668 matching_parens parens;
8669 parens.consume_open (parser);
8670
8671 /* Parse the type-id. */
8672 parser->type_definition_forbidden_message
8673 = G_("types may not be defined in a new-expression");
8674 {
8675 type_id_in_expr_sentinel s (parser);
8676 type = cp_parser_type_id (parser);
8677 }
8678 parser->type_definition_forbidden_message = saved_message;
8679
8680 /* Look for the closing `)'. */
8681 parens.require_close (parser);
8682 token = cp_lexer_peek_token (parser->lexer);
8683 /* There should not be a direct-new-declarator in this production,
8684 but GCC used to allowed this, so we check and emit a sensible error
8685 message for this case. */
8686 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8687 {
8688 error_at (token->location,
8689 "array bound forbidden after parenthesized type-id");
8690 inform (token->location,
8691 "try removing the parentheses around the type-id");
8692 cp_parser_direct_new_declarator (parser);
8693 }
8694 }
8695 /* Otherwise, there must be a new-type-id. */
8696 else
8697 type = cp_parser_new_type_id (parser, &nelts);
8698
8699 /* If the next token is a `(' or '{', then we have a new-initializer. */
8700 cp_token *token = cp_lexer_peek_token (parser->lexer);
8701 if (token->type == CPP_OPEN_PAREN
8702 || token->type == CPP_OPEN_BRACE)
8703 initializer = cp_parser_new_initializer (parser);
8704 else
8705 initializer = NULL;
8706
8707 /* A new-expression may not appear in an integral constant
8708 expression. */
8709 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8710 ret = error_mark_node;
8711 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8712 of a new-type-id or type-id of a new-expression, the new-expression shall
8713 contain a new-initializer of the form ( assignment-expression )".
8714 Additionally, consistently with the spirit of DR 1467, we want to accept
8715 'new auto { 2 }' too. */
8716 else if ((ret = type_uses_auto (type))
8717 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8718 && (vec_safe_length (initializer) != 1
8719 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8720 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8721 {
8722 error_at (token->location,
8723 "initialization of new-expression for type %<auto%> "
8724 "requires exactly one element");
8725 ret = error_mark_node;
8726 }
8727 else
8728 {
8729 /* Construct a location e.g.:
8730 ptr = new int[100]
8731 ^~~~~~~~~~~~
8732 with caret == start at the start of the "new" token, and the end
8733 at the end of the final token we consumed. */
8734 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8735 location_t end_loc = get_finish (end_tok->location);
8736 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8737
8738 /* Create a representation of the new-expression. */
8739 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8740 tf_warning_or_error);
8741 protected_set_expr_location (ret, combined_loc);
8742 }
8743
8744 if (placement != NULL)
8745 release_tree_vector (placement);
8746 if (initializer != NULL)
8747 release_tree_vector (initializer);
8748
8749 return ret;
8750 }
8751
8752 /* Parse a new-placement.
8753
8754 new-placement:
8755 ( expression-list )
8756
8757 Returns the same representation as for an expression-list. */
8758
8759 static vec<tree, va_gc> *
8760 cp_parser_new_placement (cp_parser* parser)
8761 {
8762 vec<tree, va_gc> *expression_list;
8763
8764 /* Parse the expression-list. */
8765 expression_list = (cp_parser_parenthesized_expression_list
8766 (parser, non_attr, /*cast_p=*/false,
8767 /*allow_expansion_p=*/true,
8768 /*non_constant_p=*/NULL));
8769
8770 if (expression_list && expression_list->is_empty ())
8771 error ("expected expression-list or type-id");
8772
8773 return expression_list;
8774 }
8775
8776 /* Parse a new-type-id.
8777
8778 new-type-id:
8779 type-specifier-seq new-declarator [opt]
8780
8781 Returns the TYPE allocated. If the new-type-id indicates an array
8782 type, *NELTS is set to the number of elements in the last array
8783 bound; the TYPE will not include the last array bound. */
8784
8785 static tree
8786 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8787 {
8788 cp_decl_specifier_seq type_specifier_seq;
8789 cp_declarator *new_declarator;
8790 cp_declarator *declarator;
8791 cp_declarator *outer_declarator;
8792 const char *saved_message;
8793
8794 /* The type-specifier sequence must not contain type definitions.
8795 (It cannot contain declarations of new types either, but if they
8796 are not definitions we will catch that because they are not
8797 complete.) */
8798 saved_message = parser->type_definition_forbidden_message;
8799 parser->type_definition_forbidden_message
8800 = G_("types may not be defined in a new-type-id");
8801 /* Parse the type-specifier-seq. */
8802 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
8803 /*is_declaration=*/false,
8804 /*is_trailing_return=*/false,
8805 &type_specifier_seq);
8806 /* Restore the old message. */
8807 parser->type_definition_forbidden_message = saved_message;
8808
8809 if (type_specifier_seq.type == error_mark_node)
8810 return error_mark_node;
8811
8812 /* Parse the new-declarator. */
8813 new_declarator = cp_parser_new_declarator_opt (parser);
8814
8815 /* Determine the number of elements in the last array dimension, if
8816 any. */
8817 *nelts = NULL_TREE;
8818 /* Skip down to the last array dimension. */
8819 declarator = new_declarator;
8820 outer_declarator = NULL;
8821 while (declarator && (declarator->kind == cdk_pointer
8822 || declarator->kind == cdk_ptrmem))
8823 {
8824 outer_declarator = declarator;
8825 declarator = declarator->declarator;
8826 }
8827 while (declarator
8828 && declarator->kind == cdk_array
8829 && declarator->declarator
8830 && declarator->declarator->kind == cdk_array)
8831 {
8832 outer_declarator = declarator;
8833 declarator = declarator->declarator;
8834 }
8835
8836 if (declarator && declarator->kind == cdk_array)
8837 {
8838 *nelts = declarator->u.array.bounds;
8839 if (*nelts == error_mark_node)
8840 *nelts = integer_one_node;
8841
8842 if (outer_declarator)
8843 outer_declarator->declarator = declarator->declarator;
8844 else
8845 new_declarator = NULL;
8846 }
8847
8848 return groktypename (&type_specifier_seq, new_declarator, false);
8849 }
8850
8851 /* Parse an (optional) new-declarator.
8852
8853 new-declarator:
8854 ptr-operator new-declarator [opt]
8855 direct-new-declarator
8856
8857 Returns the declarator. */
8858
8859 static cp_declarator *
8860 cp_parser_new_declarator_opt (cp_parser* parser)
8861 {
8862 enum tree_code code;
8863 tree type, std_attributes = NULL_TREE;
8864 cp_cv_quals cv_quals;
8865
8866 /* We don't know if there's a ptr-operator next, or not. */
8867 cp_parser_parse_tentatively (parser);
8868 /* Look for a ptr-operator. */
8869 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8870 /* If that worked, look for more new-declarators. */
8871 if (cp_parser_parse_definitely (parser))
8872 {
8873 cp_declarator *declarator;
8874
8875 /* Parse another optional declarator. */
8876 declarator = cp_parser_new_declarator_opt (parser);
8877
8878 declarator = cp_parser_make_indirect_declarator
8879 (code, type, cv_quals, declarator, std_attributes);
8880
8881 return declarator;
8882 }
8883
8884 /* If the next token is a `[', there is a direct-new-declarator. */
8885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8886 return cp_parser_direct_new_declarator (parser);
8887
8888 return NULL;
8889 }
8890
8891 /* Parse a direct-new-declarator.
8892
8893 direct-new-declarator:
8894 [ expression ]
8895 direct-new-declarator [constant-expression]
8896
8897 */
8898
8899 static cp_declarator *
8900 cp_parser_direct_new_declarator (cp_parser* parser)
8901 {
8902 cp_declarator *declarator = NULL;
8903
8904 while (true)
8905 {
8906 tree expression;
8907 cp_token *token;
8908
8909 /* Look for the opening `['. */
8910 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8911
8912 token = cp_lexer_peek_token (parser->lexer);
8913 expression = cp_parser_expression (parser);
8914 /* The standard requires that the expression have integral
8915 type. DR 74 adds enumeration types. We believe that the
8916 real intent is that these expressions be handled like the
8917 expression in a `switch' condition, which also allows
8918 classes with a single conversion to integral or
8919 enumeration type. */
8920 if (!processing_template_decl)
8921 {
8922 expression
8923 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8924 expression,
8925 /*complain=*/true);
8926 if (!expression)
8927 {
8928 error_at (token->location,
8929 "expression in new-declarator must have integral "
8930 "or enumeration type");
8931 expression = error_mark_node;
8932 }
8933 }
8934
8935 /* Look for the closing `]'. */
8936 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8937
8938 /* Add this bound to the declarator. */
8939 declarator = make_array_declarator (declarator, expression);
8940
8941 /* If the next token is not a `[', then there are no more
8942 bounds. */
8943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8944 break;
8945 }
8946
8947 return declarator;
8948 }
8949
8950 /* Parse a new-initializer.
8951
8952 new-initializer:
8953 ( expression-list [opt] )
8954 braced-init-list
8955
8956 Returns a representation of the expression-list. */
8957
8958 static vec<tree, va_gc> *
8959 cp_parser_new_initializer (cp_parser* parser)
8960 {
8961 vec<tree, va_gc> *expression_list;
8962
8963 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8964 {
8965 tree t;
8966 bool expr_non_constant_p;
8967 cp_lexer_set_source_position (parser->lexer);
8968 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8969 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8970 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8971 expression_list = make_tree_vector_single (t);
8972 }
8973 else
8974 expression_list = (cp_parser_parenthesized_expression_list
8975 (parser, non_attr, /*cast_p=*/false,
8976 /*allow_expansion_p=*/true,
8977 /*non_constant_p=*/NULL));
8978
8979 return expression_list;
8980 }
8981
8982 /* Parse a delete-expression.
8983
8984 delete-expression:
8985 :: [opt] delete cast-expression
8986 :: [opt] delete [ ] cast-expression
8987
8988 Returns a representation of the expression. */
8989
8990 static tree
8991 cp_parser_delete_expression (cp_parser* parser)
8992 {
8993 bool global_scope_p;
8994 bool array_p;
8995 tree expression;
8996
8997 /* Look for the optional `::' operator. */
8998 global_scope_p
8999 = (cp_parser_global_scope_opt (parser,
9000 /*current_scope_valid_p=*/false)
9001 != NULL_TREE);
9002 /* Look for the `delete' keyword. */
9003 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9004 /* See if the array syntax is in use. */
9005 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9006 {
9007 /* Consume the `[' token. */
9008 cp_lexer_consume_token (parser->lexer);
9009 /* Look for the `]' token. */
9010 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9011 /* Remember that this is the `[]' construct. */
9012 array_p = true;
9013 }
9014 else
9015 array_p = false;
9016
9017 /* Parse the cast-expression. */
9018 expression = cp_parser_simple_cast_expression (parser);
9019
9020 /* A delete-expression may not appear in an integral constant
9021 expression. */
9022 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9023 return error_mark_node;
9024
9025 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
9026 tf_warning_or_error);
9027 }
9028
9029 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9030 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9031 0 otherwise. */
9032
9033 static int
9034 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9035 {
9036 cp_token *token = cp_lexer_peek_token (parser->lexer);
9037 switch (token->type)
9038 {
9039 case CPP_COMMA:
9040 case CPP_SEMICOLON:
9041 case CPP_QUERY:
9042 case CPP_COLON:
9043 case CPP_CLOSE_SQUARE:
9044 case CPP_CLOSE_PAREN:
9045 case CPP_CLOSE_BRACE:
9046 case CPP_OPEN_BRACE:
9047 case CPP_DOT:
9048 case CPP_DOT_STAR:
9049 case CPP_DEREF:
9050 case CPP_DEREF_STAR:
9051 case CPP_DIV:
9052 case CPP_MOD:
9053 case CPP_LSHIFT:
9054 case CPP_RSHIFT:
9055 case CPP_LESS:
9056 case CPP_GREATER:
9057 case CPP_LESS_EQ:
9058 case CPP_GREATER_EQ:
9059 case CPP_EQ_EQ:
9060 case CPP_NOT_EQ:
9061 case CPP_EQ:
9062 case CPP_MULT_EQ:
9063 case CPP_DIV_EQ:
9064 case CPP_MOD_EQ:
9065 case CPP_PLUS_EQ:
9066 case CPP_MINUS_EQ:
9067 case CPP_RSHIFT_EQ:
9068 case CPP_LSHIFT_EQ:
9069 case CPP_AND_EQ:
9070 case CPP_XOR_EQ:
9071 case CPP_OR_EQ:
9072 case CPP_XOR:
9073 case CPP_OR:
9074 case CPP_OR_OR:
9075 case CPP_EOF:
9076 case CPP_ELLIPSIS:
9077 return 0;
9078
9079 case CPP_OPEN_PAREN:
9080 /* In ((type ()) () the last () isn't a valid cast-expression,
9081 so the whole must be parsed as postfix-expression. */
9082 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9083 != CPP_CLOSE_PAREN;
9084
9085 case CPP_OPEN_SQUARE:
9086 /* '[' may start a primary-expression in obj-c++ and in C++11,
9087 as a lambda-expression, eg, '(void)[]{}'. */
9088 if (cxx_dialect >= cxx11)
9089 return -1;
9090 return c_dialect_objc ();
9091
9092 case CPP_PLUS_PLUS:
9093 case CPP_MINUS_MINUS:
9094 /* '++' and '--' may or may not start a cast-expression:
9095
9096 struct T { void operator++(int); };
9097 void f() { (T())++; }
9098
9099 vs
9100
9101 int a;
9102 (int)++a; */
9103 return -1;
9104
9105 default:
9106 return 1;
9107 }
9108 }
9109
9110 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9111 in the order: const_cast, static_cast, reinterpret_cast.
9112
9113 Don't suggest dynamic_cast.
9114
9115 Return the first legal cast kind found, or NULL otherwise. */
9116
9117 static const char *
9118 get_cast_suggestion (tree dst_type, tree orig_expr)
9119 {
9120 tree trial;
9121
9122 /* Reuse the parser logic by attempting to build the various kinds of
9123 cast, with "complain" disabled.
9124 Identify the first such cast that is valid. */
9125
9126 /* Don't attempt to run such logic within template processing. */
9127 if (processing_template_decl)
9128 return NULL;
9129
9130 /* First try const_cast. */
9131 trial = build_const_cast (dst_type, orig_expr, tf_none);
9132 if (trial != error_mark_node)
9133 return "const_cast";
9134
9135 /* If that fails, try static_cast. */
9136 trial = build_static_cast (dst_type, orig_expr, tf_none);
9137 if (trial != error_mark_node)
9138 return "static_cast";
9139
9140 /* Finally, try reinterpret_cast. */
9141 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
9142 if (trial != error_mark_node)
9143 return "reinterpret_cast";
9144
9145 /* No such cast possible. */
9146 return NULL;
9147 }
9148
9149 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9150 suggesting how to convert a C-style cast of the form:
9151
9152 (DST_TYPE)ORIG_EXPR
9153
9154 to a C++-style cast.
9155
9156 The primary range of RICHLOC is asssumed to be that of the original
9157 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9158 of the parens in the C-style cast. */
9159
9160 static void
9161 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9162 location_t close_paren_loc, tree orig_expr,
9163 tree dst_type)
9164 {
9165 /* This function is non-trivial, so bail out now if the warning isn't
9166 going to be emitted. */
9167 if (!warn_old_style_cast)
9168 return;
9169
9170 /* Try to find a legal C++ cast, trying them in order:
9171 const_cast, static_cast, reinterpret_cast. */
9172 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9173 if (!cast_suggestion)
9174 return;
9175
9176 /* Replace the open paren with "CAST_SUGGESTION<". */
9177 pretty_printer pp;
9178 pp_printf (&pp, "%s<", cast_suggestion);
9179 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9180
9181 /* Replace the close paren with "> (". */
9182 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9183
9184 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9185 rich_loc->add_fixit_insert_after (")");
9186 }
9187
9188
9189 /* Parse a cast-expression.
9190
9191 cast-expression:
9192 unary-expression
9193 ( type-id ) cast-expression
9194
9195 ADDRESS_P is true iff the unary-expression is appearing as the
9196 operand of the `&' operator. CAST_P is true if this expression is
9197 the target of a cast.
9198
9199 Returns a representation of the expression. */
9200
9201 static cp_expr
9202 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9203 bool decltype_p, cp_id_kind * pidk)
9204 {
9205 /* If it's a `(', then we might be looking at a cast. */
9206 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9207 {
9208 tree type = NULL_TREE;
9209 cp_expr expr (NULL_TREE);
9210 int cast_expression = 0;
9211 const char *saved_message;
9212
9213 /* There's no way to know yet whether or not this is a cast.
9214 For example, `(int (3))' is a unary-expression, while `(int)
9215 3' is a cast. So, we resort to parsing tentatively. */
9216 cp_parser_parse_tentatively (parser);
9217 /* Types may not be defined in a cast. */
9218 saved_message = parser->type_definition_forbidden_message;
9219 parser->type_definition_forbidden_message
9220 = G_("types may not be defined in casts");
9221 /* Consume the `('. */
9222 matching_parens parens;
9223 cp_token *open_paren = parens.consume_open (parser);
9224 location_t open_paren_loc = open_paren->location;
9225 location_t close_paren_loc = UNKNOWN_LOCATION;
9226
9227 /* A very tricky bit is that `(struct S) { 3 }' is a
9228 compound-literal (which we permit in C++ as an extension).
9229 But, that construct is not a cast-expression -- it is a
9230 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9231 is legal; if the compound-literal were a cast-expression,
9232 you'd need an extra set of parentheses.) But, if we parse
9233 the type-id, and it happens to be a class-specifier, then we
9234 will commit to the parse at that point, because we cannot
9235 undo the action that is done when creating a new class. So,
9236 then we cannot back up and do a postfix-expression.
9237
9238 Another tricky case is the following (c++/29234):
9239
9240 struct S { void operator () (); };
9241
9242 void foo ()
9243 {
9244 ( S()() );
9245 }
9246
9247 As a type-id we parse the parenthesized S()() as a function
9248 returning a function, groktypename complains and we cannot
9249 back up in this case either.
9250
9251 Therefore, we scan ahead to the closing `)', and check to see
9252 if the tokens after the `)' can start a cast-expression. Otherwise
9253 we are dealing with an unary-expression, a postfix-expression
9254 or something else.
9255
9256 Yet another tricky case, in C++11, is the following (c++/54891):
9257
9258 (void)[]{};
9259
9260 The issue is that usually, besides the case of lambda-expressions,
9261 the parenthesized type-id cannot be followed by '[', and, eg, we
9262 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9263 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9264 we don't commit, we try a cast-expression, then an unary-expression.
9265
9266 Save tokens so that we can put them back. */
9267 cp_lexer_save_tokens (parser->lexer);
9268
9269 /* We may be looking at a cast-expression. */
9270 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9271 /*consume_paren=*/true))
9272 cast_expression
9273 = cp_parser_tokens_start_cast_expression (parser);
9274
9275 /* Roll back the tokens we skipped. */
9276 cp_lexer_rollback_tokens (parser->lexer);
9277 /* If we aren't looking at a cast-expression, simulate an error so
9278 that the call to cp_parser_error_occurred below returns true. */
9279 if (!cast_expression)
9280 cp_parser_simulate_error (parser);
9281 else
9282 {
9283 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9284 parser->in_type_id_in_expr_p = true;
9285 /* Look for the type-id. */
9286 type = cp_parser_type_id (parser);
9287 /* Look for the closing `)'. */
9288 cp_token *close_paren = parens.require_close (parser);
9289 if (close_paren)
9290 close_paren_loc = close_paren->location;
9291 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9292 }
9293
9294 /* Restore the saved message. */
9295 parser->type_definition_forbidden_message = saved_message;
9296
9297 /* At this point this can only be either a cast or a
9298 parenthesized ctor such as `(T ())' that looks like a cast to
9299 function returning T. */
9300 if (!cp_parser_error_occurred (parser))
9301 {
9302 /* Only commit if the cast-expression doesn't start with
9303 '++', '--', or '[' in C++11. */
9304 if (cast_expression > 0)
9305 cp_parser_commit_to_topmost_tentative_parse (parser);
9306
9307 expr = cp_parser_cast_expression (parser,
9308 /*address_p=*/false,
9309 /*cast_p=*/true,
9310 /*decltype_p=*/false,
9311 pidk);
9312
9313 if (cp_parser_parse_definitely (parser))
9314 {
9315 /* Warn about old-style casts, if so requested. */
9316 if (warn_old_style_cast
9317 && !in_system_header_at (input_location)
9318 && !VOID_TYPE_P (type)
9319 && current_lang_name != lang_name_c)
9320 {
9321 gcc_rich_location rich_loc (input_location);
9322 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9323 expr, type);
9324 warning_at (&rich_loc, OPT_Wold_style_cast,
9325 "use of old-style cast to %q#T", type);
9326 }
9327
9328 /* Only type conversions to integral or enumeration types
9329 can be used in constant-expressions. */
9330 if (!cast_valid_in_integral_constant_expression_p (type)
9331 && cp_parser_non_integral_constant_expression (parser,
9332 NIC_CAST))
9333 return error_mark_node;
9334
9335 /* Perform the cast. */
9336 /* Make a location:
9337 (TYPE) EXPR
9338 ^~~~~~~~~~~
9339 with start==caret at the open paren, extending to the
9340 end of "expr". */
9341 location_t cast_loc = make_location (open_paren_loc,
9342 open_paren_loc,
9343 expr.get_finish ());
9344 expr = build_c_cast (cast_loc, type, expr);
9345 return expr;
9346 }
9347 }
9348 else
9349 cp_parser_abort_tentative_parse (parser);
9350 }
9351
9352 /* If we get here, then it's not a cast, so it must be a
9353 unary-expression. */
9354 return cp_parser_unary_expression (parser, pidk, address_p,
9355 cast_p, decltype_p);
9356 }
9357
9358 /* Parse a binary expression of the general form:
9359
9360 pm-expression:
9361 cast-expression
9362 pm-expression .* cast-expression
9363 pm-expression ->* cast-expression
9364
9365 multiplicative-expression:
9366 pm-expression
9367 multiplicative-expression * pm-expression
9368 multiplicative-expression / pm-expression
9369 multiplicative-expression % pm-expression
9370
9371 additive-expression:
9372 multiplicative-expression
9373 additive-expression + multiplicative-expression
9374 additive-expression - multiplicative-expression
9375
9376 shift-expression:
9377 additive-expression
9378 shift-expression << additive-expression
9379 shift-expression >> additive-expression
9380
9381 relational-expression:
9382 shift-expression
9383 relational-expression < shift-expression
9384 relational-expression > shift-expression
9385 relational-expression <= shift-expression
9386 relational-expression >= shift-expression
9387
9388 GNU Extension:
9389
9390 relational-expression:
9391 relational-expression <? shift-expression
9392 relational-expression >? shift-expression
9393
9394 equality-expression:
9395 relational-expression
9396 equality-expression == relational-expression
9397 equality-expression != relational-expression
9398
9399 and-expression:
9400 equality-expression
9401 and-expression & equality-expression
9402
9403 exclusive-or-expression:
9404 and-expression
9405 exclusive-or-expression ^ and-expression
9406
9407 inclusive-or-expression:
9408 exclusive-or-expression
9409 inclusive-or-expression | exclusive-or-expression
9410
9411 logical-and-expression:
9412 inclusive-or-expression
9413 logical-and-expression && inclusive-or-expression
9414
9415 logical-or-expression:
9416 logical-and-expression
9417 logical-or-expression || logical-and-expression
9418
9419 All these are implemented with a single function like:
9420
9421 binary-expression:
9422 simple-cast-expression
9423 binary-expression <token> binary-expression
9424
9425 CAST_P is true if this expression is the target of a cast.
9426
9427 The binops_by_token map is used to get the tree codes for each <token> type.
9428 binary-expressions are associated according to a precedence table. */
9429
9430 #define TOKEN_PRECEDENCE(token) \
9431 (((token->type == CPP_GREATER \
9432 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9433 && !parser->greater_than_is_operator_p) \
9434 ? PREC_NOT_OPERATOR \
9435 : binops_by_token[token->type].prec)
9436
9437 static cp_expr
9438 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9439 bool no_toplevel_fold_p,
9440 bool decltype_p,
9441 enum cp_parser_prec prec,
9442 cp_id_kind * pidk)
9443 {
9444 cp_parser_expression_stack stack;
9445 cp_parser_expression_stack_entry *sp = &stack[0];
9446 cp_parser_expression_stack_entry current;
9447 cp_expr rhs;
9448 cp_token *token;
9449 enum tree_code rhs_type;
9450 enum cp_parser_prec new_prec, lookahead_prec;
9451 tree overload;
9452
9453 /* Parse the first expression. */
9454 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9455 ? TRUTH_NOT_EXPR : ERROR_MARK);
9456 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9457 cast_p, decltype_p, pidk);
9458 current.prec = prec;
9459
9460 if (cp_parser_error_occurred (parser))
9461 return error_mark_node;
9462
9463 for (;;)
9464 {
9465 /* Get an operator token. */
9466 token = cp_lexer_peek_token (parser->lexer);
9467
9468 if (warn_cxx11_compat
9469 && token->type == CPP_RSHIFT
9470 && !parser->greater_than_is_operator_p)
9471 {
9472 if (warning_at (token->location, OPT_Wc__11_compat,
9473 "%<>>%> operator is treated"
9474 " as two right angle brackets in C++11"))
9475 inform (token->location,
9476 "suggest parentheses around %<>>%> expression");
9477 }
9478
9479 new_prec = TOKEN_PRECEDENCE (token);
9480 if (new_prec != PREC_NOT_OPERATOR
9481 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9482 /* This is a fold-expression; handle it later. */
9483 new_prec = PREC_NOT_OPERATOR;
9484
9485 /* Popping an entry off the stack means we completed a subexpression:
9486 - either we found a token which is not an operator (`>' where it is not
9487 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9488 will happen repeatedly;
9489 - or, we found an operator which has lower priority. This is the case
9490 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9491 parsing `3 * 4'. */
9492 if (new_prec <= current.prec)
9493 {
9494 if (sp == stack)
9495 break;
9496 else
9497 goto pop;
9498 }
9499
9500 get_rhs:
9501 current.tree_type = binops_by_token[token->type].tree_type;
9502 current.loc = token->location;
9503
9504 /* We used the operator token. */
9505 cp_lexer_consume_token (parser->lexer);
9506
9507 /* For "false && x" or "true || x", x will never be executed;
9508 disable warnings while evaluating it. */
9509 if (current.tree_type == TRUTH_ANDIF_EXPR)
9510 c_inhibit_evaluation_warnings +=
9511 cp_fully_fold (current.lhs) == truthvalue_false_node;
9512 else if (current.tree_type == TRUTH_ORIF_EXPR)
9513 c_inhibit_evaluation_warnings +=
9514 cp_fully_fold (current.lhs) == truthvalue_true_node;
9515
9516 /* Extract another operand. It may be the RHS of this expression
9517 or the LHS of a new, higher priority expression. */
9518 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9519 ? TRUTH_NOT_EXPR : ERROR_MARK);
9520 rhs = cp_parser_simple_cast_expression (parser);
9521
9522 /* Get another operator token. Look up its precedence to avoid
9523 building a useless (immediately popped) stack entry for common
9524 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9525 token = cp_lexer_peek_token (parser->lexer);
9526 lookahead_prec = TOKEN_PRECEDENCE (token);
9527 if (lookahead_prec != PREC_NOT_OPERATOR
9528 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9529 lookahead_prec = PREC_NOT_OPERATOR;
9530 if (lookahead_prec > new_prec)
9531 {
9532 /* ... and prepare to parse the RHS of the new, higher priority
9533 expression. Since precedence levels on the stack are
9534 monotonically increasing, we do not have to care about
9535 stack overflows. */
9536 *sp = current;
9537 ++sp;
9538 current.lhs = rhs;
9539 current.lhs_type = rhs_type;
9540 current.prec = new_prec;
9541 new_prec = lookahead_prec;
9542 goto get_rhs;
9543
9544 pop:
9545 lookahead_prec = new_prec;
9546 /* If the stack is not empty, we have parsed into LHS the right side
9547 (`4' in the example above) of an expression we had suspended.
9548 We can use the information on the stack to recover the LHS (`3')
9549 from the stack together with the tree code (`MULT_EXPR'), and
9550 the precedence of the higher level subexpression
9551 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9552 which will be used to actually build the additive expression. */
9553 rhs = current.lhs;
9554 rhs_type = current.lhs_type;
9555 --sp;
9556 current = *sp;
9557 }
9558
9559 /* Undo the disabling of warnings done above. */
9560 if (current.tree_type == TRUTH_ANDIF_EXPR)
9561 c_inhibit_evaluation_warnings -=
9562 cp_fully_fold (current.lhs) == truthvalue_false_node;
9563 else if (current.tree_type == TRUTH_ORIF_EXPR)
9564 c_inhibit_evaluation_warnings -=
9565 cp_fully_fold (current.lhs) == truthvalue_true_node;
9566
9567 if (warn_logical_not_paren
9568 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9569 && current.lhs_type == TRUTH_NOT_EXPR
9570 /* Avoid warning for !!x == y. */
9571 && (TREE_CODE (current.lhs) != NE_EXPR
9572 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9573 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9574 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9575 /* Avoid warning for !b == y where b is boolean. */
9576 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9577 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9578 != BOOLEAN_TYPE))))
9579 /* Avoid warning for !!b == y where b is boolean. */
9580 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
9581 || TREE_TYPE (current.lhs) == NULL_TREE
9582 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9583 warn_logical_not_parentheses (current.loc, current.tree_type,
9584 current.lhs, maybe_constant_value (rhs));
9585
9586 overload = NULL;
9587
9588 location_t combined_loc = make_location (current.loc,
9589 current.lhs.get_start (),
9590 rhs.get_finish ());
9591
9592 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9593 ERROR_MARK for everything that is not a binary expression.
9594 This makes warn_about_parentheses miss some warnings that
9595 involve unary operators. For unary expressions we should
9596 pass the correct tree_code unless the unary expression was
9597 surrounded by parentheses.
9598 */
9599 if (no_toplevel_fold_p
9600 && lookahead_prec <= current.prec
9601 && sp == stack)
9602 {
9603 if (current.lhs == error_mark_node || rhs == error_mark_node)
9604 current.lhs = error_mark_node;
9605 else
9606 {
9607 current.lhs
9608 = build_min (current.tree_type,
9609 TREE_CODE_CLASS (current.tree_type)
9610 == tcc_comparison
9611 ? boolean_type_node : TREE_TYPE (current.lhs),
9612 current.lhs.get_value (), rhs.get_value ());
9613 SET_EXPR_LOCATION (current.lhs, combined_loc);
9614 }
9615 }
9616 else
9617 {
9618 op_location_t op_loc (current.loc, combined_loc);
9619 current.lhs = build_x_binary_op (op_loc, current.tree_type,
9620 current.lhs, current.lhs_type,
9621 rhs, rhs_type, &overload,
9622 complain_flags (decltype_p));
9623 /* TODO: build_x_binary_op doesn't always honor the location. */
9624 current.lhs.set_location (combined_loc);
9625 }
9626 current.lhs_type = current.tree_type;
9627
9628 /* If the binary operator required the use of an overloaded operator,
9629 then this expression cannot be an integral constant-expression.
9630 An overloaded operator can be used even if both operands are
9631 otherwise permissible in an integral constant-expression if at
9632 least one of the operands is of enumeration type. */
9633
9634 if (overload
9635 && cp_parser_non_integral_constant_expression (parser,
9636 NIC_OVERLOADED))
9637 return error_mark_node;
9638 }
9639
9640 return current.lhs;
9641 }
9642
9643 static cp_expr
9644 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9645 bool no_toplevel_fold_p,
9646 enum cp_parser_prec prec,
9647 cp_id_kind * pidk)
9648 {
9649 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9650 /*decltype*/false, prec, pidk);
9651 }
9652
9653 /* Parse the `? expression : assignment-expression' part of a
9654 conditional-expression. The LOGICAL_OR_EXPR is the
9655 logical-or-expression that started the conditional-expression.
9656 Returns a representation of the entire conditional-expression.
9657
9658 This routine is used by cp_parser_assignment_expression.
9659
9660 ? expression : assignment-expression
9661
9662 GNU Extensions:
9663
9664 ? : assignment-expression */
9665
9666 static tree
9667 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9668 {
9669 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9670 cp_expr assignment_expr;
9671 struct cp_token *token;
9672 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9673
9674 /* Consume the `?' token. */
9675 cp_lexer_consume_token (parser->lexer);
9676 token = cp_lexer_peek_token (parser->lexer);
9677 if (cp_parser_allow_gnu_extensions_p (parser)
9678 && token->type == CPP_COLON)
9679 {
9680 pedwarn (token->location, OPT_Wpedantic,
9681 "ISO C++ does not allow ?: with omitted middle operand");
9682 /* Implicit true clause. */
9683 expr = NULL_TREE;
9684 c_inhibit_evaluation_warnings +=
9685 folded_logical_or_expr == truthvalue_true_node;
9686 warn_for_omitted_condop (token->location, logical_or_expr);
9687 }
9688 else
9689 {
9690 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9691 parser->colon_corrects_to_scope_p = false;
9692 /* Parse the expression. */
9693 c_inhibit_evaluation_warnings +=
9694 folded_logical_or_expr == truthvalue_false_node;
9695 expr = cp_parser_expression (parser);
9696 c_inhibit_evaluation_warnings +=
9697 ((folded_logical_or_expr == truthvalue_true_node)
9698 - (folded_logical_or_expr == truthvalue_false_node));
9699 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9700 }
9701
9702 /* The next token should be a `:'. */
9703 cp_parser_require (parser, CPP_COLON, RT_COLON);
9704 /* Parse the assignment-expression. */
9705 assignment_expr = cp_parser_assignment_expression (parser);
9706 c_inhibit_evaluation_warnings -=
9707 folded_logical_or_expr == truthvalue_true_node;
9708
9709 /* Make a location:
9710 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9711 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9712 with the caret at the "?", ranging from the start of
9713 the logical_or_expr to the end of the assignment_expr. */
9714 loc = make_location (loc,
9715 logical_or_expr.get_start (),
9716 assignment_expr.get_finish ());
9717
9718 /* Build the conditional-expression. */
9719 return build_x_conditional_expr (loc, logical_or_expr,
9720 expr,
9721 assignment_expr,
9722 tf_warning_or_error);
9723 }
9724
9725 /* Parse an assignment-expression.
9726
9727 assignment-expression:
9728 conditional-expression
9729 logical-or-expression assignment-operator assignment_expression
9730 throw-expression
9731
9732 CAST_P is true if this expression is the target of a cast.
9733 DECLTYPE_P is true if this expression is the operand of decltype.
9734
9735 Returns a representation for the expression. */
9736
9737 static cp_expr
9738 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9739 bool cast_p, bool decltype_p)
9740 {
9741 cp_expr expr;
9742
9743 /* If the next token is the `throw' keyword, then we're looking at
9744 a throw-expression. */
9745 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9746 expr = cp_parser_throw_expression (parser);
9747 /* Otherwise, it must be that we are looking at a
9748 logical-or-expression. */
9749 else
9750 {
9751 /* Parse the binary expressions (logical-or-expression). */
9752 expr = cp_parser_binary_expression (parser, cast_p, false,
9753 decltype_p,
9754 PREC_NOT_OPERATOR, pidk);
9755 /* If the next token is a `?' then we're actually looking at a
9756 conditional-expression. */
9757 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9758 return cp_parser_question_colon_clause (parser, expr);
9759 else
9760 {
9761 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9762
9763 /* If it's an assignment-operator, we're using the second
9764 production. */
9765 enum tree_code assignment_operator
9766 = cp_parser_assignment_operator_opt (parser);
9767 if (assignment_operator != ERROR_MARK)
9768 {
9769 bool non_constant_p;
9770
9771 /* Parse the right-hand side of the assignment. */
9772 cp_expr rhs = cp_parser_initializer_clause (parser,
9773 &non_constant_p);
9774
9775 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9776 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9777
9778 /* An assignment may not appear in a
9779 constant-expression. */
9780 if (cp_parser_non_integral_constant_expression (parser,
9781 NIC_ASSIGNMENT))
9782 return error_mark_node;
9783 /* Build the assignment expression. Its default
9784 location:
9785 LHS = RHS
9786 ~~~~^~~~~
9787 is the location of the '=' token as the
9788 caret, ranging from the start of the lhs to the
9789 end of the rhs. */
9790 loc = make_location (loc,
9791 expr.get_start (),
9792 rhs.get_finish ());
9793 expr = build_x_modify_expr (loc, expr,
9794 assignment_operator,
9795 rhs,
9796 complain_flags (decltype_p));
9797 /* TODO: build_x_modify_expr doesn't honor the location,
9798 so we must set it here. */
9799 expr.set_location (loc);
9800 }
9801 }
9802 }
9803
9804 return expr;
9805 }
9806
9807 /* Parse an (optional) assignment-operator.
9808
9809 assignment-operator: one of
9810 = *= /= %= += -= >>= <<= &= ^= |=
9811
9812 GNU Extension:
9813
9814 assignment-operator: one of
9815 <?= >?=
9816
9817 If the next token is an assignment operator, the corresponding tree
9818 code is returned, and the token is consumed. For example, for
9819 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9820 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9821 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9822 operator, ERROR_MARK is returned. */
9823
9824 static enum tree_code
9825 cp_parser_assignment_operator_opt (cp_parser* parser)
9826 {
9827 enum tree_code op;
9828 cp_token *token;
9829
9830 /* Peek at the next token. */
9831 token = cp_lexer_peek_token (parser->lexer);
9832
9833 switch (token->type)
9834 {
9835 case CPP_EQ:
9836 op = NOP_EXPR;
9837 break;
9838
9839 case CPP_MULT_EQ:
9840 op = MULT_EXPR;
9841 break;
9842
9843 case CPP_DIV_EQ:
9844 op = TRUNC_DIV_EXPR;
9845 break;
9846
9847 case CPP_MOD_EQ:
9848 op = TRUNC_MOD_EXPR;
9849 break;
9850
9851 case CPP_PLUS_EQ:
9852 op = PLUS_EXPR;
9853 break;
9854
9855 case CPP_MINUS_EQ:
9856 op = MINUS_EXPR;
9857 break;
9858
9859 case CPP_RSHIFT_EQ:
9860 op = RSHIFT_EXPR;
9861 break;
9862
9863 case CPP_LSHIFT_EQ:
9864 op = LSHIFT_EXPR;
9865 break;
9866
9867 case CPP_AND_EQ:
9868 op = BIT_AND_EXPR;
9869 break;
9870
9871 case CPP_XOR_EQ:
9872 op = BIT_XOR_EXPR;
9873 break;
9874
9875 case CPP_OR_EQ:
9876 op = BIT_IOR_EXPR;
9877 break;
9878
9879 default:
9880 /* Nothing else is an assignment operator. */
9881 op = ERROR_MARK;
9882 }
9883
9884 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9885 if (op != ERROR_MARK
9886 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9887 op = ERROR_MARK;
9888
9889 /* If it was an assignment operator, consume it. */
9890 if (op != ERROR_MARK)
9891 cp_lexer_consume_token (parser->lexer);
9892
9893 return op;
9894 }
9895
9896 /* Parse an expression.
9897
9898 expression:
9899 assignment-expression
9900 expression , assignment-expression
9901
9902 CAST_P is true if this expression is the target of a cast.
9903 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9904 except possibly parenthesized or on the RHS of a comma (N3276).
9905
9906 Returns a representation of the expression. */
9907
9908 static cp_expr
9909 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9910 bool cast_p, bool decltype_p)
9911 {
9912 cp_expr expression = NULL_TREE;
9913 location_t loc = UNKNOWN_LOCATION;
9914
9915 while (true)
9916 {
9917 cp_expr assignment_expression;
9918
9919 /* Parse the next assignment-expression. */
9920 assignment_expression
9921 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9922
9923 /* We don't create a temporary for a call that is the immediate operand
9924 of decltype or on the RHS of a comma. But when we see a comma, we
9925 need to create a temporary for a call on the LHS. */
9926 if (decltype_p && !processing_template_decl
9927 && TREE_CODE (assignment_expression) == CALL_EXPR
9928 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9929 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9930 assignment_expression
9931 = build_cplus_new (TREE_TYPE (assignment_expression),
9932 assignment_expression, tf_warning_or_error);
9933
9934 /* If this is the first assignment-expression, we can just
9935 save it away. */
9936 if (!expression)
9937 expression = assignment_expression;
9938 else
9939 {
9940 /* Create a location with caret at the comma, ranging
9941 from the start of the LHS to the end of the RHS. */
9942 loc = make_location (loc,
9943 expression.get_start (),
9944 assignment_expression.get_finish ());
9945 expression = build_x_compound_expr (loc, expression,
9946 assignment_expression,
9947 complain_flags (decltype_p));
9948 expression.set_location (loc);
9949 }
9950 /* If the next token is not a comma, or we're in a fold-expression, then
9951 we are done with the expression. */
9952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9953 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9954 break;
9955 /* Consume the `,'. */
9956 loc = cp_lexer_peek_token (parser->lexer)->location;
9957 cp_lexer_consume_token (parser->lexer);
9958 /* A comma operator cannot appear in a constant-expression. */
9959 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9960 expression = error_mark_node;
9961 }
9962
9963 return expression;
9964 }
9965
9966 /* Parse a constant-expression.
9967
9968 constant-expression:
9969 conditional-expression
9970
9971 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9972 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9973 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9974 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9975 only parse a conditional-expression, otherwise parse an
9976 assignment-expression. See below for rationale. */
9977
9978 static cp_expr
9979 cp_parser_constant_expression (cp_parser* parser,
9980 bool allow_non_constant_p,
9981 bool *non_constant_p,
9982 bool strict_p)
9983 {
9984 bool saved_integral_constant_expression_p;
9985 bool saved_allow_non_integral_constant_expression_p;
9986 bool saved_non_integral_constant_expression_p;
9987 cp_expr expression;
9988
9989 /* It might seem that we could simply parse the
9990 conditional-expression, and then check to see if it were
9991 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9992 one that the compiler can figure out is constant, possibly after
9993 doing some simplifications or optimizations. The standard has a
9994 precise definition of constant-expression, and we must honor
9995 that, even though it is somewhat more restrictive.
9996
9997 For example:
9998
9999 int i[(2, 3)];
10000
10001 is not a legal declaration, because `(2, 3)' is not a
10002 constant-expression. The `,' operator is forbidden in a
10003 constant-expression. However, GCC's constant-folding machinery
10004 will fold this operation to an INTEGER_CST for `3'. */
10005
10006 /* Save the old settings. */
10007 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10008 saved_allow_non_integral_constant_expression_p
10009 = parser->allow_non_integral_constant_expression_p;
10010 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10011 /* We are now parsing a constant-expression. */
10012 parser->integral_constant_expression_p = true;
10013 parser->allow_non_integral_constant_expression_p
10014 = (allow_non_constant_p || cxx_dialect >= cxx11);
10015 parser->non_integral_constant_expression_p = false;
10016 /* Although the grammar says "conditional-expression", when not STRICT_P,
10017 we parse an "assignment-expression", which also permits
10018 "throw-expression" and the use of assignment operators. In the case
10019 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10020 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10021 actually essential that we look for an assignment-expression.
10022 For example, cp_parser_initializer_clauses uses this function to
10023 determine whether a particular assignment-expression is in fact
10024 constant. */
10025 if (strict_p)
10026 {
10027 /* Parse the binary expressions (logical-or-expression). */
10028 expression = cp_parser_binary_expression (parser, false, false, false,
10029 PREC_NOT_OPERATOR, NULL);
10030 /* If the next token is a `?' then we're actually looking at
10031 a conditional-expression; otherwise we're done. */
10032 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10033 expression = cp_parser_question_colon_clause (parser, expression);
10034 }
10035 else
10036 expression = cp_parser_assignment_expression (parser);
10037 /* Restore the old settings. */
10038 parser->integral_constant_expression_p
10039 = saved_integral_constant_expression_p;
10040 parser->allow_non_integral_constant_expression_p
10041 = saved_allow_non_integral_constant_expression_p;
10042 if (cxx_dialect >= cxx11)
10043 {
10044 /* Require an rvalue constant expression here; that's what our
10045 callers expect. Reference constant expressions are handled
10046 separately in e.g. cp_parser_template_argument. */
10047 tree decay = expression;
10048 if (TREE_TYPE (expression)
10049 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10050 decay = build_address (expression);
10051 bool is_const = potential_rvalue_constant_expression (decay);
10052 parser->non_integral_constant_expression_p = !is_const;
10053 if (!is_const && !allow_non_constant_p)
10054 require_potential_rvalue_constant_expression (decay);
10055 }
10056 if (allow_non_constant_p)
10057 *non_constant_p = parser->non_integral_constant_expression_p;
10058 parser->non_integral_constant_expression_p
10059 = saved_non_integral_constant_expression_p;
10060
10061 return expression;
10062 }
10063
10064 /* Parse __builtin_offsetof.
10065
10066 offsetof-expression:
10067 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10068
10069 offsetof-member-designator:
10070 id-expression
10071 | offsetof-member-designator "." id-expression
10072 | offsetof-member-designator "[" expression "]"
10073 | offsetof-member-designator "->" id-expression */
10074
10075 static cp_expr
10076 cp_parser_builtin_offsetof (cp_parser *parser)
10077 {
10078 int save_ice_p, save_non_ice_p;
10079 tree type;
10080 cp_expr expr;
10081 cp_id_kind dummy;
10082 cp_token *token;
10083 location_t finish_loc;
10084
10085 /* We're about to accept non-integral-constant things, but will
10086 definitely yield an integral constant expression. Save and
10087 restore these values around our local parsing. */
10088 save_ice_p = parser->integral_constant_expression_p;
10089 save_non_ice_p = parser->non_integral_constant_expression_p;
10090
10091 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10092
10093 /* Consume the "__builtin_offsetof" token. */
10094 cp_lexer_consume_token (parser->lexer);
10095 /* Consume the opening `('. */
10096 matching_parens parens;
10097 parens.require_open (parser);
10098 /* Parse the type-id. */
10099 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10100 {
10101 const char *saved_message = parser->type_definition_forbidden_message;
10102 parser->type_definition_forbidden_message
10103 = G_("types may not be defined within %<__builtin_offsetof%>");
10104 type = cp_parser_type_id (parser);
10105 parser->type_definition_forbidden_message = saved_message;
10106 }
10107 /* Look for the `,'. */
10108 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10109 token = cp_lexer_peek_token (parser->lexer);
10110
10111 /* Build the (type *)null that begins the traditional offsetof macro. */
10112 tree object_ptr
10113 = build_static_cast (build_pointer_type (type), null_pointer_node,
10114 tf_warning_or_error);
10115
10116 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10117 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10118 true, &dummy, token->location);
10119 while (true)
10120 {
10121 token = cp_lexer_peek_token (parser->lexer);
10122 switch (token->type)
10123 {
10124 case CPP_OPEN_SQUARE:
10125 /* offsetof-member-designator "[" expression "]" */
10126 expr = cp_parser_postfix_open_square_expression (parser, expr,
10127 true, false);
10128 break;
10129
10130 case CPP_DEREF:
10131 /* offsetof-member-designator "->" identifier */
10132 expr = grok_array_decl (token->location, expr,
10133 integer_zero_node, false);
10134 /* FALLTHRU */
10135
10136 case CPP_DOT:
10137 /* offsetof-member-designator "." identifier */
10138 cp_lexer_consume_token (parser->lexer);
10139 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10140 expr, true, &dummy,
10141 token->location);
10142 break;
10143
10144 case CPP_CLOSE_PAREN:
10145 /* Consume the ")" token. */
10146 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10147 cp_lexer_consume_token (parser->lexer);
10148 goto success;
10149
10150 default:
10151 /* Error. We know the following require will fail, but
10152 that gives the proper error message. */
10153 parens.require_close (parser);
10154 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10155 expr = error_mark_node;
10156 goto failure;
10157 }
10158 }
10159
10160 success:
10161 /* Make a location of the form:
10162 __builtin_offsetof (struct s, f)
10163 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10164 with caret at the type-id, ranging from the start of the
10165 "_builtin_offsetof" token to the close paren. */
10166 loc = make_location (loc, start_loc, finish_loc);
10167 /* The result will be an INTEGER_CST, so we need to explicitly
10168 preserve the location. */
10169 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10170
10171 failure:
10172 parser->integral_constant_expression_p = save_ice_p;
10173 parser->non_integral_constant_expression_p = save_non_ice_p;
10174
10175 expr = expr.maybe_add_location_wrapper ();
10176 return expr;
10177 }
10178
10179 /* Parse a trait expression.
10180
10181 Returns a representation of the expression, the underlying type
10182 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10183
10184 static cp_expr
10185 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10186 {
10187 cp_trait_kind kind;
10188 tree type1, type2 = NULL_TREE;
10189 bool binary = false;
10190 bool variadic = false;
10191
10192 switch (keyword)
10193 {
10194 case RID_HAS_NOTHROW_ASSIGN:
10195 kind = CPTK_HAS_NOTHROW_ASSIGN;
10196 break;
10197 case RID_HAS_NOTHROW_CONSTRUCTOR:
10198 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10199 break;
10200 case RID_HAS_NOTHROW_COPY:
10201 kind = CPTK_HAS_NOTHROW_COPY;
10202 break;
10203 case RID_HAS_TRIVIAL_ASSIGN:
10204 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10205 break;
10206 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10207 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10208 break;
10209 case RID_HAS_TRIVIAL_COPY:
10210 kind = CPTK_HAS_TRIVIAL_COPY;
10211 break;
10212 case RID_HAS_TRIVIAL_DESTRUCTOR:
10213 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10214 break;
10215 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10216 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10217 break;
10218 case RID_HAS_VIRTUAL_DESTRUCTOR:
10219 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10220 break;
10221 case RID_IS_ABSTRACT:
10222 kind = CPTK_IS_ABSTRACT;
10223 break;
10224 case RID_IS_AGGREGATE:
10225 kind = CPTK_IS_AGGREGATE;
10226 break;
10227 case RID_IS_BASE_OF:
10228 kind = CPTK_IS_BASE_OF;
10229 binary = true;
10230 break;
10231 case RID_IS_CLASS:
10232 kind = CPTK_IS_CLASS;
10233 break;
10234 case RID_IS_EMPTY:
10235 kind = CPTK_IS_EMPTY;
10236 break;
10237 case RID_IS_ENUM:
10238 kind = CPTK_IS_ENUM;
10239 break;
10240 case RID_IS_FINAL:
10241 kind = CPTK_IS_FINAL;
10242 break;
10243 case RID_IS_LITERAL_TYPE:
10244 kind = CPTK_IS_LITERAL_TYPE;
10245 break;
10246 case RID_IS_POD:
10247 kind = CPTK_IS_POD;
10248 break;
10249 case RID_IS_POLYMORPHIC:
10250 kind = CPTK_IS_POLYMORPHIC;
10251 break;
10252 case RID_IS_SAME_AS:
10253 kind = CPTK_IS_SAME_AS;
10254 binary = true;
10255 break;
10256 case RID_IS_STD_LAYOUT:
10257 kind = CPTK_IS_STD_LAYOUT;
10258 break;
10259 case RID_IS_TRIVIAL:
10260 kind = CPTK_IS_TRIVIAL;
10261 break;
10262 case RID_IS_TRIVIALLY_ASSIGNABLE:
10263 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10264 binary = true;
10265 break;
10266 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10267 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10268 variadic = true;
10269 break;
10270 case RID_IS_TRIVIALLY_COPYABLE:
10271 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10272 break;
10273 case RID_IS_UNION:
10274 kind = CPTK_IS_UNION;
10275 break;
10276 case RID_UNDERLYING_TYPE:
10277 kind = CPTK_UNDERLYING_TYPE;
10278 break;
10279 case RID_BASES:
10280 kind = CPTK_BASES;
10281 break;
10282 case RID_DIRECT_BASES:
10283 kind = CPTK_DIRECT_BASES;
10284 break;
10285 case RID_IS_ASSIGNABLE:
10286 kind = CPTK_IS_ASSIGNABLE;
10287 binary = true;
10288 break;
10289 case RID_IS_CONSTRUCTIBLE:
10290 kind = CPTK_IS_CONSTRUCTIBLE;
10291 variadic = true;
10292 break;
10293 default:
10294 gcc_unreachable ();
10295 }
10296
10297 /* Get location of initial token. */
10298 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10299
10300 /* Consume the token. */
10301 cp_lexer_consume_token (parser->lexer);
10302
10303 matching_parens parens;
10304 parens.require_open (parser);
10305
10306 {
10307 type_id_in_expr_sentinel s (parser);
10308 type1 = cp_parser_type_id (parser);
10309 }
10310
10311 if (type1 == error_mark_node)
10312 return error_mark_node;
10313
10314 if (binary)
10315 {
10316 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10317
10318 {
10319 type_id_in_expr_sentinel s (parser);
10320 type2 = cp_parser_type_id (parser);
10321 }
10322
10323 if (type2 == error_mark_node)
10324 return error_mark_node;
10325 }
10326 else if (variadic)
10327 {
10328 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10329 {
10330 cp_lexer_consume_token (parser->lexer);
10331 tree elt = cp_parser_type_id (parser);
10332 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10333 {
10334 cp_lexer_consume_token (parser->lexer);
10335 elt = make_pack_expansion (elt);
10336 }
10337 if (elt == error_mark_node)
10338 return error_mark_node;
10339 type2 = tree_cons (NULL_TREE, elt, type2);
10340 }
10341 }
10342
10343 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10344 parens.require_close (parser);
10345
10346 /* Construct a location of the form:
10347 __is_trivially_copyable(_Tp)
10348 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10349 with start == caret, finishing at the close-paren. */
10350 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10351
10352 /* Complete the trait expression, which may mean either processing
10353 the trait expr now or saving it for template instantiation. */
10354 switch (kind)
10355 {
10356 case CPTK_UNDERLYING_TYPE:
10357 return cp_expr (finish_underlying_type (type1), trait_loc);
10358 case CPTK_BASES:
10359 return cp_expr (finish_bases (type1, false), trait_loc);
10360 case CPTK_DIRECT_BASES:
10361 return cp_expr (finish_bases (type1, true), trait_loc);
10362 default:
10363 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10364 }
10365 }
10366
10367 /* Parse a lambda expression.
10368
10369 lambda-expression:
10370 lambda-introducer lambda-declarator [opt] compound-statement
10371
10372 Returns a representation of the expression. */
10373
10374 static cp_expr
10375 cp_parser_lambda_expression (cp_parser* parser)
10376 {
10377 tree lambda_expr = build_lambda_expr ();
10378 tree type;
10379 bool ok = true;
10380 cp_token *token = cp_lexer_peek_token (parser->lexer);
10381 cp_token_position start = 0;
10382
10383 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10384
10385 if (cxx_dialect >= cxx2a)
10386 /* C++20 allows lambdas in unevaluated context. */;
10387 else if (cp_unevaluated_operand)
10388 {
10389 if (!token->error_reported)
10390 {
10391 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10392 "lambda-expression in unevaluated context"
10393 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10394 token->error_reported = true;
10395 }
10396 ok = false;
10397 }
10398 else if (parser->in_template_argument_list_p)
10399 {
10400 if (!token->error_reported)
10401 {
10402 error_at (token->location, "lambda-expression in template-argument"
10403 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10404 token->error_reported = true;
10405 }
10406 ok = false;
10407 }
10408
10409 /* We may be in the middle of deferred access check. Disable
10410 it now. */
10411 push_deferring_access_checks (dk_no_deferred);
10412
10413 cp_parser_lambda_introducer (parser, lambda_expr);
10414 if (cp_parser_error_occurred (parser))
10415 return error_mark_node;
10416
10417 type = begin_lambda_type (lambda_expr);
10418 if (type == error_mark_node)
10419 return error_mark_node;
10420
10421 record_lambda_scope (lambda_expr);
10422
10423 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10424 determine_visibility (TYPE_NAME (type));
10425
10426 /* Now that we've started the type, add the capture fields for any
10427 explicit captures. */
10428 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10429
10430 {
10431 /* Inside the class, surrounding template-parameter-lists do not apply. */
10432 unsigned int saved_num_template_parameter_lists
10433 = parser->num_template_parameter_lists;
10434 unsigned char in_statement = parser->in_statement;
10435 bool in_switch_statement_p = parser->in_switch_statement_p;
10436 bool fully_implicit_function_template_p
10437 = parser->fully_implicit_function_template_p;
10438 tree implicit_template_parms = parser->implicit_template_parms;
10439 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10440 bool auto_is_implicit_function_template_parm_p
10441 = parser->auto_is_implicit_function_template_parm_p;
10442
10443 parser->num_template_parameter_lists = 0;
10444 parser->in_statement = 0;
10445 parser->in_switch_statement_p = false;
10446 parser->fully_implicit_function_template_p = false;
10447 parser->implicit_template_parms = 0;
10448 parser->implicit_template_scope = 0;
10449 parser->auto_is_implicit_function_template_parm_p = false;
10450
10451 /* By virtue of defining a local class, a lambda expression has access to
10452 the private variables of enclosing classes. */
10453
10454 if (cp_parser_start_tentative_firewall (parser))
10455 start = token;
10456
10457 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10458
10459 if (ok && cp_parser_error_occurred (parser))
10460 ok = false;
10461
10462 if (ok)
10463 {
10464 cp_parser_lambda_body (parser, lambda_expr);
10465 }
10466 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10467 {
10468 if (cp_parser_skip_to_closing_brace (parser))
10469 cp_lexer_consume_token (parser->lexer);
10470 }
10471
10472 /* The capture list was built up in reverse order; fix that now. */
10473 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10474 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10475
10476 if (ok)
10477 maybe_add_lambda_conv_op (type);
10478
10479 type = finish_struct (type, /*attributes=*/NULL_TREE);
10480
10481 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10482 parser->in_statement = in_statement;
10483 parser->in_switch_statement_p = in_switch_statement_p;
10484 parser->fully_implicit_function_template_p
10485 = fully_implicit_function_template_p;
10486 parser->implicit_template_parms = implicit_template_parms;
10487 parser->implicit_template_scope = implicit_template_scope;
10488 parser->auto_is_implicit_function_template_parm_p
10489 = auto_is_implicit_function_template_parm_p;
10490 }
10491
10492 /* This field is only used during parsing of the lambda. */
10493 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10494
10495 /* This lambda shouldn't have any proxies left at this point. */
10496 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10497 /* And now that we're done, push proxies for an enclosing lambda. */
10498 insert_pending_capture_proxies ();
10499
10500 /* Update the lambda expression to a range. */
10501 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10502 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10503 token->location,
10504 end_tok->location);
10505
10506 if (ok)
10507 lambda_expr = build_lambda_object (lambda_expr);
10508 else
10509 lambda_expr = error_mark_node;
10510
10511 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10512
10513 pop_deferring_access_checks ();
10514
10515 return lambda_expr;
10516 }
10517
10518 /* Parse the beginning of a lambda expression.
10519
10520 lambda-introducer:
10521 [ lambda-capture [opt] ]
10522
10523 LAMBDA_EXPR is the current representation of the lambda expression. */
10524
10525 static void
10526 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10527 {
10528 /* Need commas after the first capture. */
10529 bool first = true;
10530
10531 /* Eat the leading `['. */
10532 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10533
10534 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10535 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10536 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10537 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10538 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10539 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10540
10541 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10542 {
10543 cp_lexer_consume_token (parser->lexer);
10544 first = false;
10545
10546 if (!(at_function_scope_p () || parsing_nsdmi ()))
10547 error ("non-local lambda expression cannot have a capture-default");
10548 }
10549
10550 hash_set<tree, true> ids;
10551 tree first_capture_id = NULL_TREE;
10552 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10553 {
10554 cp_token* capture_token;
10555 tree capture_id;
10556 tree capture_init_expr;
10557 cp_id_kind idk = CP_ID_KIND_NONE;
10558 bool explicit_init_p = false;
10559
10560 enum capture_kind_type
10561 {
10562 BY_COPY,
10563 BY_REFERENCE
10564 };
10565 enum capture_kind_type capture_kind = BY_COPY;
10566
10567 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10568 {
10569 error ("expected end of capture-list");
10570 return;
10571 }
10572
10573 if (first)
10574 first = false;
10575 else
10576 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10577
10578 /* Possibly capture `this'. */
10579 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10580 {
10581 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10582 if (cxx_dialect < cxx2a
10583 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10584 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10585 "with by-copy capture default");
10586 cp_lexer_consume_token (parser->lexer);
10587 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10588 pedwarn (input_location, 0,
10589 "already captured %qD in lambda expression",
10590 this_identifier);
10591 else
10592 add_capture (lambda_expr, /*id=*/this_identifier,
10593 /*initializer=*/finish_this_expr (),
10594 /*by_reference_p=*/true, explicit_init_p);
10595 continue;
10596 }
10597
10598 /* Possibly capture `*this'. */
10599 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10600 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10601 {
10602 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10603 if (cxx_dialect < cxx17)
10604 pedwarn (loc, 0, "%<*this%> capture only available with "
10605 "%<-std=c++17%> or %<-std=gnu++17%>");
10606 cp_lexer_consume_token (parser->lexer);
10607 cp_lexer_consume_token (parser->lexer);
10608 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10609 pedwarn (input_location, 0,
10610 "already captured %qD in lambda expression",
10611 this_identifier);
10612 else
10613 add_capture (lambda_expr, /*id=*/this_identifier,
10614 /*initializer=*/finish_this_expr (),
10615 /*by_reference_p=*/false, explicit_init_p);
10616 continue;
10617 }
10618
10619 bool init_pack_expansion = false;
10620 location_t ellipsis_loc = UNKNOWN_LOCATION;
10621 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10622 {
10623 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
10624 if (cxx_dialect < cxx2a)
10625 pedwarn (ellipsis_loc, 0, "pack init-capture only available with "
10626 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10627 cp_lexer_consume_token (parser->lexer);
10628 init_pack_expansion = true;
10629 }
10630
10631 /* Remember whether we want to capture as a reference or not. */
10632 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10633 {
10634 capture_kind = BY_REFERENCE;
10635 cp_lexer_consume_token (parser->lexer);
10636 }
10637
10638 /* Get the identifier. */
10639 capture_token = cp_lexer_peek_token (parser->lexer);
10640 capture_id = cp_parser_identifier (parser);
10641
10642 if (capture_id == error_mark_node)
10643 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10644 delimiters, but I modified this to stop on unnested ']' as well. It
10645 was already changed to stop on unnested '}', so the
10646 "closing_parenthesis" name is no more misleading with my change. */
10647 {
10648 cp_parser_skip_to_closing_parenthesis (parser,
10649 /*recovering=*/true,
10650 /*or_comma=*/true,
10651 /*consume_paren=*/true);
10652 break;
10653 }
10654
10655 /* Find the initializer for this capture. */
10656 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10657 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10658 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10659 {
10660 bool direct, non_constant;
10661 /* An explicit initializer exists. */
10662 if (cxx_dialect < cxx14)
10663 pedwarn (input_location, 0,
10664 "lambda capture initializers "
10665 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10666 capture_init_expr = cp_parser_initializer (parser, &direct,
10667 &non_constant, true);
10668 explicit_init_p = true;
10669 if (capture_init_expr == NULL_TREE)
10670 {
10671 error ("empty initializer for lambda init-capture");
10672 capture_init_expr = error_mark_node;
10673 }
10674 if (init_pack_expansion)
10675 capture_init_expr = make_pack_expansion (capture_init_expr);
10676 }
10677 else
10678 {
10679 const char* error_msg;
10680
10681 /* Turn the identifier into an id-expression. */
10682 capture_init_expr
10683 = cp_parser_lookup_name_simple (parser, capture_id,
10684 capture_token->location);
10685
10686 if (capture_init_expr == error_mark_node)
10687 {
10688 unqualified_name_lookup_error (capture_id);
10689 continue;
10690 }
10691 else if (!VAR_P (capture_init_expr)
10692 && TREE_CODE (capture_init_expr) != PARM_DECL)
10693 {
10694 error_at (capture_token->location,
10695 "capture of non-variable %qE",
10696 capture_init_expr);
10697 if (DECL_P (capture_init_expr))
10698 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10699 "%q#D declared here", capture_init_expr);
10700 continue;
10701 }
10702 if (VAR_P (capture_init_expr)
10703 && decl_storage_duration (capture_init_expr) != dk_auto)
10704 {
10705 if (pedwarn (capture_token->location, 0, "capture of variable "
10706 "%qD with non-automatic storage duration",
10707 capture_init_expr))
10708 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10709 "%q#D declared here", capture_init_expr);
10710 continue;
10711 }
10712
10713 capture_init_expr
10714 = finish_id_expression
10715 (capture_id,
10716 capture_init_expr,
10717 parser->scope,
10718 &idk,
10719 /*integral_constant_expression_p=*/false,
10720 /*allow_non_integral_constant_expression_p=*/false,
10721 /*non_integral_constant_expression_p=*/NULL,
10722 /*template_p=*/false,
10723 /*done=*/true,
10724 /*address_p=*/false,
10725 /*template_arg_p=*/false,
10726 &error_msg,
10727 capture_token->location);
10728
10729 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10730 {
10731 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10732 cp_lexer_consume_token (parser->lexer);
10733 capture_init_expr = make_pack_expansion (capture_init_expr);
10734 if (init_pack_expansion)
10735 {
10736 /* If what follows is an initializer, the second '...' is
10737 invalid. But for cases like [...xs...], the first one
10738 is invalid. */
10739 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10740 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10741 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10742 ellipsis_loc = loc;
10743 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
10744 continue;
10745 }
10746 }
10747 }
10748
10749 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10750 && !explicit_init_p)
10751 {
10752 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10753 && capture_kind == BY_COPY)
10754 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10755 "of %qD redundant with by-copy capture default",
10756 capture_id);
10757 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10758 && capture_kind == BY_REFERENCE)
10759 pedwarn (capture_token->location, 0, "explicit by-reference "
10760 "capture of %qD redundant with by-reference capture "
10761 "default", capture_id);
10762 }
10763
10764 /* Check for duplicates.
10765 Optimize for the zero or one explicit captures cases and only create
10766 the hash_set after adding second capture. */
10767 bool found = false;
10768 if (ids.elements ())
10769 found = ids.add (capture_id);
10770 else if (first_capture_id == NULL_TREE)
10771 first_capture_id = capture_id;
10772 else if (capture_id == first_capture_id)
10773 found = true;
10774 else
10775 {
10776 ids.add (first_capture_id);
10777 ids.add (capture_id);
10778 }
10779 if (found)
10780 pedwarn (input_location, 0,
10781 "already captured %qD in lambda expression", capture_id);
10782 else
10783 add_capture (lambda_expr, capture_id, capture_init_expr,
10784 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10785 explicit_init_p);
10786
10787 /* If there is any qualification still in effect, clear it
10788 now; we will be starting fresh with the next capture. */
10789 parser->scope = NULL_TREE;
10790 parser->qualifying_scope = NULL_TREE;
10791 parser->object_scope = NULL_TREE;
10792 }
10793
10794 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10795 }
10796
10797 /* Parse the (optional) middle of a lambda expression.
10798
10799 lambda-declarator:
10800 < template-parameter-list [opt] >
10801 ( parameter-declaration-clause [opt] )
10802 attribute-specifier [opt]
10803 decl-specifier-seq [opt]
10804 exception-specification [opt]
10805 lambda-return-type-clause [opt]
10806
10807 LAMBDA_EXPR is the current representation of the lambda expression. */
10808
10809 static bool
10810 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10811 {
10812 /* 5.1.1.4 of the standard says:
10813 If a lambda-expression does not include a lambda-declarator, it is as if
10814 the lambda-declarator were ().
10815 This means an empty parameter list, no attributes, and no exception
10816 specification. */
10817 tree param_list = void_list_node;
10818 tree std_attrs = NULL_TREE;
10819 tree gnu_attrs = NULL_TREE;
10820 tree exception_spec = NULL_TREE;
10821 tree template_param_list = NULL_TREE;
10822 tree tx_qual = NULL_TREE;
10823 tree return_type = NULL_TREE;
10824 cp_decl_specifier_seq lambda_specs;
10825 clear_decl_specs (&lambda_specs);
10826
10827 /* The template-parameter-list is optional, but must begin with
10828 an opening angle if present. */
10829 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10830 {
10831 if (cxx_dialect < cxx14)
10832 pedwarn (parser->lexer->next_token->location, 0,
10833 "lambda templates are only available with "
10834 "%<-std=c++14%> or %<-std=gnu++14%>");
10835 else if (cxx_dialect < cxx2a)
10836 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10837 "lambda templates are only available with "
10838 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10839
10840 cp_lexer_consume_token (parser->lexer);
10841
10842 template_param_list = cp_parser_template_parameter_list (parser);
10843
10844 cp_parser_skip_to_end_of_template_parameter_list (parser);
10845
10846 /* We just processed one more parameter list. */
10847 ++parser->num_template_parameter_lists;
10848 }
10849
10850 /* The parameter-declaration-clause is optional (unless
10851 template-parameter-list was given), but must begin with an
10852 opening parenthesis if present. */
10853 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10854 {
10855 matching_parens parens;
10856 parens.consume_open (parser);
10857
10858 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10859
10860 /* Parse parameters. */
10861 param_list
10862 = cp_parser_parameter_declaration_clause
10863 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
10864
10865 /* Default arguments shall not be specified in the
10866 parameter-declaration-clause of a lambda-declarator. */
10867 if (cxx_dialect < cxx14)
10868 for (tree t = param_list; t; t = TREE_CHAIN (t))
10869 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10870 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10871 "default argument specified for lambda parameter");
10872
10873 parens.require_close (parser);
10874
10875 /* In the decl-specifier-seq of the lambda-declarator, each
10876 decl-specifier shall either be mutable or constexpr. */
10877 int declares_class_or_enum;
10878 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
10879 && !cp_next_tokens_can_be_gnu_attribute_p (parser))
10880 cp_parser_decl_specifier_seq (parser,
10881 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10882 &lambda_specs, &declares_class_or_enum);
10883 if (lambda_specs.storage_class == sc_mutable)
10884 {
10885 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10886 if (lambda_specs.conflicting_specifiers_p)
10887 error_at (lambda_specs.locations[ds_storage_class],
10888 "duplicate %<mutable%>");
10889 }
10890
10891 tx_qual = cp_parser_tx_qualifier_opt (parser);
10892
10893 /* Parse optional exception specification. */
10894 exception_spec = cp_parser_exception_specification_opt (parser);
10895
10896 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10897
10898 /* Parse optional trailing return type. */
10899 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10900 {
10901 cp_lexer_consume_token (parser->lexer);
10902 return_type = cp_parser_trailing_type_id (parser);
10903 }
10904
10905 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
10906 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
10907
10908 /* The function parameters must be in scope all the way until after the
10909 trailing-return-type in case of decltype. */
10910 pop_bindings_and_leave_scope ();
10911 }
10912 else if (template_param_list != NULL_TREE) // generate diagnostic
10913 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10914
10915 /* Create the function call operator.
10916
10917 Messing with declarators like this is no uglier than building up the
10918 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10919 other code. */
10920 {
10921 cp_decl_specifier_seq return_type_specs;
10922 cp_declarator* declarator;
10923 tree fco;
10924 int quals;
10925 void *p;
10926
10927 clear_decl_specs (&return_type_specs);
10928 return_type_specs.type = make_auto ();
10929
10930 if (lambda_specs.locations[ds_constexpr])
10931 {
10932 if (cxx_dialect >= cxx17)
10933 return_type_specs.locations[ds_constexpr]
10934 = lambda_specs.locations[ds_constexpr];
10935 else
10936 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10937 "lambda only available with %<-std=c++17%> or "
10938 "%<-std=gnu++17%>");
10939 }
10940
10941 p = obstack_alloc (&declarator_obstack, 0);
10942
10943 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
10944 LAMBDA_EXPR_LOCATION (lambda_expr));
10945
10946 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10947 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10948 declarator = make_call_declarator (declarator, param_list, quals,
10949 VIRT_SPEC_UNSPECIFIED,
10950 REF_QUAL_NONE,
10951 tx_qual,
10952 exception_spec,
10953 return_type,
10954 /*requires_clause*/NULL_TREE);
10955 declarator->std_attributes = std_attrs;
10956
10957 fco = grokmethod (&return_type_specs,
10958 declarator,
10959 gnu_attrs);
10960 if (fco != error_mark_node)
10961 {
10962 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10963 DECL_ARTIFICIAL (fco) = 1;
10964 /* Give the object parameter a different name. */
10965 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10966 DECL_LAMBDA_FUNCTION (fco) = 1;
10967 }
10968 if (template_param_list)
10969 {
10970 fco = finish_member_template_decl (fco);
10971 finish_template_decl (template_param_list);
10972 --parser->num_template_parameter_lists;
10973 }
10974 else if (parser->fully_implicit_function_template_p)
10975 fco = finish_fully_implicit_template (parser, fco);
10976
10977 finish_member_declaration (fco);
10978
10979 obstack_free (&declarator_obstack, p);
10980
10981 return (fco != error_mark_node);
10982 }
10983 }
10984
10985 /* Parse the body of a lambda expression, which is simply
10986
10987 compound-statement
10988
10989 but which requires special handling.
10990 LAMBDA_EXPR is the current representation of the lambda expression. */
10991
10992 static void
10993 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10994 {
10995 bool nested = (current_function_decl != NULL_TREE);
10996 unsigned char local_variables_forbidden_p
10997 = parser->local_variables_forbidden_p;
10998 bool in_function_body = parser->in_function_body;
10999
11000 /* The body of a lambda-expression is not a subexpression of the enclosing
11001 expression. */
11002 cp_evaluated ev;
11003
11004 if (nested)
11005 push_function_context ();
11006 else
11007 /* Still increment function_depth so that we don't GC in the
11008 middle of an expression. */
11009 ++function_depth;
11010
11011 vec<tree> omp_privatization_save;
11012 save_omp_privatization_clauses (omp_privatization_save);
11013 /* Clear this in case we're in the middle of a default argument. */
11014 parser->local_variables_forbidden_p = 0;
11015 parser->in_function_body = true;
11016
11017 {
11018 local_specialization_stack s (lss_copy);
11019 tree fco = lambda_function (lambda_expr);
11020 tree body = start_lambda_function (fco, lambda_expr);
11021 matching_braces braces;
11022
11023 if (braces.require_open (parser))
11024 {
11025 tree compound_stmt = begin_compound_stmt (0);
11026
11027 /* Originally C++11 required us to peek for 'return expr'; and
11028 process it specially here to deduce the return type. N3638
11029 removed the need for that. */
11030
11031 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11032 cp_parser_label_declaration (parser);
11033 cp_parser_statement_seq_opt (parser, NULL_TREE);
11034 braces.require_close (parser);
11035
11036 finish_compound_stmt (compound_stmt);
11037 }
11038
11039 finish_lambda_function (body);
11040 }
11041
11042 restore_omp_privatization_clauses (omp_privatization_save);
11043 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11044 parser->in_function_body = in_function_body;
11045 if (nested)
11046 pop_function_context();
11047 else
11048 --function_depth;
11049 }
11050
11051 /* Statements [gram.stmt.stmt] */
11052
11053 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11054
11055 static void
11056 add_debug_begin_stmt (location_t loc)
11057 {
11058 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11059 return;
11060 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11061 /* A concept is never expanded normally. */
11062 return;
11063
11064 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11065 SET_EXPR_LOCATION (stmt, loc);
11066 add_stmt (stmt);
11067 }
11068
11069 /* Parse a statement.
11070
11071 statement:
11072 labeled-statement
11073 expression-statement
11074 compound-statement
11075 selection-statement
11076 iteration-statement
11077 jump-statement
11078 declaration-statement
11079 try-block
11080
11081 C++11:
11082
11083 statement:
11084 labeled-statement
11085 attribute-specifier-seq (opt) expression-statement
11086 attribute-specifier-seq (opt) compound-statement
11087 attribute-specifier-seq (opt) selection-statement
11088 attribute-specifier-seq (opt) iteration-statement
11089 attribute-specifier-seq (opt) jump-statement
11090 declaration-statement
11091 attribute-specifier-seq (opt) try-block
11092
11093 init-statement:
11094 expression-statement
11095 simple-declaration
11096
11097 TM Extension:
11098
11099 statement:
11100 atomic-statement
11101
11102 IN_COMPOUND is true when the statement is nested inside a
11103 cp_parser_compound_statement; this matters for certain pragmas.
11104
11105 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11106 is a (possibly labeled) if statement which is not enclosed in braces
11107 and has an else clause. This is used to implement -Wparentheses.
11108
11109 CHAIN is a vector of if-else-if conditions. */
11110
11111 static void
11112 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11113 bool in_compound, bool *if_p, vec<tree> *chain,
11114 location_t *loc_after_labels)
11115 {
11116 tree statement, std_attrs = NULL_TREE;
11117 cp_token *token;
11118 location_t statement_location, attrs_loc;
11119
11120 restart:
11121 if (if_p != NULL)
11122 *if_p = false;
11123 /* There is no statement yet. */
11124 statement = NULL_TREE;
11125
11126 saved_token_sentinel saved_tokens (parser->lexer);
11127 attrs_loc = cp_lexer_peek_token (parser->lexer)->location;
11128 if (c_dialect_objc ())
11129 /* In obj-c++, seeing '[[' might be the either the beginning of
11130 c++11 attributes, or a nested objc-message-expression. So
11131 let's parse the c++11 attributes tentatively. */
11132 cp_parser_parse_tentatively (parser);
11133 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11134 if (std_attrs)
11135 {
11136 location_t end_loc
11137 = cp_lexer_previous_token (parser->lexer)->location;
11138 attrs_loc = make_location (attrs_loc, attrs_loc, end_loc);
11139 }
11140 if (c_dialect_objc ())
11141 {
11142 if (!cp_parser_parse_definitely (parser))
11143 std_attrs = NULL_TREE;
11144 }
11145
11146 /* Peek at the next token. */
11147 token = cp_lexer_peek_token (parser->lexer);
11148 /* Remember the location of the first token in the statement. */
11149 cp_token *statement_token = token;
11150 statement_location = token->location;
11151 add_debug_begin_stmt (statement_location);
11152 /* If this is a keyword, then that will often determine what kind of
11153 statement we have. */
11154 if (token->type == CPP_KEYWORD)
11155 {
11156 enum rid keyword = token->keyword;
11157
11158 switch (keyword)
11159 {
11160 case RID_CASE:
11161 case RID_DEFAULT:
11162 /* Looks like a labeled-statement with a case label.
11163 Parse the label, and then use tail recursion to parse
11164 the statement. */
11165 cp_parser_label_for_labeled_statement (parser, std_attrs);
11166 in_compound = false;
11167 goto restart;
11168
11169 case RID_IF:
11170 case RID_SWITCH:
11171 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11172 statement = cp_parser_selection_statement (parser, if_p, chain);
11173 break;
11174
11175 case RID_WHILE:
11176 case RID_DO:
11177 case RID_FOR:
11178 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11179 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11180 break;
11181
11182 case RID_BREAK:
11183 case RID_CONTINUE:
11184 case RID_RETURN:
11185 case RID_GOTO:
11186 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11187 statement = cp_parser_jump_statement (parser);
11188 break;
11189
11190 /* Objective-C++ exception-handling constructs. */
11191 case RID_AT_TRY:
11192 case RID_AT_CATCH:
11193 case RID_AT_FINALLY:
11194 case RID_AT_SYNCHRONIZED:
11195 case RID_AT_THROW:
11196 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11197 statement = cp_parser_objc_statement (parser);
11198 break;
11199
11200 case RID_TRY:
11201 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11202 statement = cp_parser_try_block (parser);
11203 break;
11204
11205 case RID_NAMESPACE:
11206 /* This must be a namespace alias definition. */
11207 if (std_attrs != NULL_TREE)
11208 {
11209 /* Attributes should be parsed as part of the the
11210 declaration, so let's un-parse them. */
11211 saved_tokens.rollback();
11212 std_attrs = NULL_TREE;
11213 }
11214 cp_parser_declaration_statement (parser);
11215 return;
11216
11217 case RID_TRANSACTION_ATOMIC:
11218 case RID_TRANSACTION_RELAXED:
11219 case RID_SYNCHRONIZED:
11220 case RID_ATOMIC_NOEXCEPT:
11221 case RID_ATOMIC_CANCEL:
11222 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11223 statement = cp_parser_transaction (parser, token);
11224 break;
11225 case RID_TRANSACTION_CANCEL:
11226 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11227 statement = cp_parser_transaction_cancel (parser);
11228 break;
11229
11230 default:
11231 /* It might be a keyword like `int' that can start a
11232 declaration-statement. */
11233 break;
11234 }
11235 }
11236 else if (token->type == CPP_NAME)
11237 {
11238 /* If the next token is a `:', then we are looking at a
11239 labeled-statement. */
11240 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11241 if (token->type == CPP_COLON)
11242 {
11243 /* Looks like a labeled-statement with an ordinary label.
11244 Parse the label, and then use tail recursion to parse
11245 the statement. */
11246
11247 cp_parser_label_for_labeled_statement (parser, std_attrs);
11248 in_compound = false;
11249 goto restart;
11250 }
11251 }
11252 /* Anything that starts with a `{' must be a compound-statement. */
11253 else if (token->type == CPP_OPEN_BRACE)
11254 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11255 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11256 a statement all its own. */
11257 else if (token->type == CPP_PRAGMA)
11258 {
11259 /* Only certain OpenMP pragmas are attached to statements, and thus
11260 are considered statements themselves. All others are not. In
11261 the context of a compound, accept the pragma as a "statement" and
11262 return so that we can check for a close brace. Otherwise we
11263 require a real statement and must go back and read one. */
11264 if (in_compound)
11265 cp_parser_pragma (parser, pragma_compound, if_p);
11266 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11267 goto restart;
11268 return;
11269 }
11270 else if (token->type == CPP_EOF)
11271 {
11272 cp_parser_error (parser, "expected statement");
11273 return;
11274 }
11275
11276 /* Everything else must be a declaration-statement or an
11277 expression-statement. Try for the declaration-statement
11278 first, unless we are looking at a `;', in which case we know that
11279 we have an expression-statement. */
11280 if (!statement)
11281 {
11282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11283 {
11284 if (std_attrs != NULL_TREE)
11285 /* Attributes should be parsed as part of the declaration,
11286 so let's un-parse them. */
11287 saved_tokens.rollback();
11288
11289 cp_parser_parse_tentatively (parser);
11290 /* Try to parse the declaration-statement. */
11291 cp_parser_declaration_statement (parser);
11292 /* If that worked, we're done. */
11293 if (cp_parser_parse_definitely (parser))
11294 return;
11295 /* It didn't work, restore the post-attribute position. */
11296 if (std_attrs)
11297 cp_lexer_set_token_position (parser->lexer, statement_token);
11298 }
11299 /* All preceding labels have been parsed at this point. */
11300 if (loc_after_labels != NULL)
11301 *loc_after_labels = statement_location;
11302
11303 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11304
11305 /* Look for an expression-statement instead. */
11306 statement = cp_parser_expression_statement (parser, in_statement_expr);
11307
11308 /* Handle [[fallthrough]];. */
11309 if (attribute_fallthrough_p (std_attrs))
11310 {
11311 /* The next token after the fallthrough attribute is ';'. */
11312 if (statement == NULL_TREE)
11313 {
11314 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11315 statement = build_call_expr_internal_loc (statement_location,
11316 IFN_FALLTHROUGH,
11317 void_type_node, 0);
11318 finish_expr_stmt (statement);
11319 }
11320 else
11321 warning_at (statement_location, OPT_Wattributes,
11322 "%<fallthrough%> attribute not followed by %<;%>");
11323 std_attrs = NULL_TREE;
11324 }
11325 }
11326
11327 /* Set the line number for the statement. */
11328 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11329 SET_EXPR_LOCATION (statement, statement_location);
11330
11331 /* Allow "[[fallthrough]];", but warn otherwise. */
11332 if (std_attrs != NULL_TREE)
11333 warning_at (attrs_loc,
11334 OPT_Wattributes,
11335 "attributes at the beginning of statement are ignored");
11336 }
11337
11338 /* Append ATTR to attribute list ATTRS. */
11339
11340 static tree
11341 attr_chainon (tree attrs, tree attr)
11342 {
11343 if (attrs == error_mark_node)
11344 return error_mark_node;
11345 if (attr == error_mark_node)
11346 return error_mark_node;
11347 return chainon (attrs, attr);
11348 }
11349
11350 /* Parse the label for a labeled-statement, i.e.
11351
11352 identifier :
11353 case constant-expression :
11354 default :
11355
11356 GNU Extension:
11357 case constant-expression ... constant-expression : statement
11358
11359 When a label is parsed without errors, the label is added to the
11360 parse tree by the finish_* functions, so this function doesn't
11361 have to return the label. */
11362
11363 static void
11364 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11365 {
11366 cp_token *token;
11367 tree label = NULL_TREE;
11368 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11369
11370 /* The next token should be an identifier. */
11371 token = cp_lexer_peek_token (parser->lexer);
11372 if (token->type != CPP_NAME
11373 && token->type != CPP_KEYWORD)
11374 {
11375 cp_parser_error (parser, "expected labeled-statement");
11376 return;
11377 }
11378
11379 /* Remember whether this case or a user-defined label is allowed to fall
11380 through to. */
11381 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11382
11383 parser->colon_corrects_to_scope_p = false;
11384 switch (token->keyword)
11385 {
11386 case RID_CASE:
11387 {
11388 tree expr, expr_hi;
11389 cp_token *ellipsis;
11390
11391 /* Consume the `case' token. */
11392 cp_lexer_consume_token (parser->lexer);
11393 /* Parse the constant-expression. */
11394 expr = cp_parser_constant_expression (parser);
11395 if (check_for_bare_parameter_packs (expr))
11396 expr = error_mark_node;
11397
11398 ellipsis = cp_lexer_peek_token (parser->lexer);
11399 if (ellipsis->type == CPP_ELLIPSIS)
11400 {
11401 /* Consume the `...' token. */
11402 cp_lexer_consume_token (parser->lexer);
11403 expr_hi = cp_parser_constant_expression (parser);
11404 if (check_for_bare_parameter_packs (expr_hi))
11405 expr_hi = error_mark_node;
11406
11407 /* We don't need to emit warnings here, as the common code
11408 will do this for us. */
11409 }
11410 else
11411 expr_hi = NULL_TREE;
11412
11413 if (parser->in_switch_statement_p)
11414 {
11415 tree l = finish_case_label (token->location, expr, expr_hi);
11416 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11417 {
11418 label = CASE_LABEL (l);
11419 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11420 }
11421 }
11422 else
11423 error_at (token->location,
11424 "case label %qE not within a switch statement",
11425 expr);
11426 }
11427 break;
11428
11429 case RID_DEFAULT:
11430 /* Consume the `default' token. */
11431 cp_lexer_consume_token (parser->lexer);
11432
11433 if (parser->in_switch_statement_p)
11434 {
11435 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11436 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11437 {
11438 label = CASE_LABEL (l);
11439 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11440 }
11441 }
11442 else
11443 error_at (token->location, "case label not within a switch statement");
11444 break;
11445
11446 default:
11447 /* Anything else must be an ordinary label. */
11448 label = finish_label_stmt (cp_parser_identifier (parser));
11449 if (label && TREE_CODE (label) == LABEL_DECL)
11450 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11451 break;
11452 }
11453
11454 /* Require the `:' token. */
11455 cp_parser_require (parser, CPP_COLON, RT_COLON);
11456
11457 /* An ordinary label may optionally be followed by attributes.
11458 However, this is only permitted if the attributes are then
11459 followed by a semicolon. This is because, for backward
11460 compatibility, when parsing
11461 lab: __attribute__ ((unused)) int i;
11462 we want the attribute to attach to "i", not "lab". */
11463 if (label != NULL_TREE
11464 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11465 {
11466 tree attrs;
11467 cp_parser_parse_tentatively (parser);
11468 attrs = cp_parser_gnu_attributes_opt (parser);
11469 if (attrs == NULL_TREE
11470 /* And fallthrough always binds to the expression-statement. */
11471 || attribute_fallthrough_p (attrs)
11472 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11473 cp_parser_abort_tentative_parse (parser);
11474 else if (!cp_parser_parse_definitely (parser))
11475 ;
11476 else
11477 attributes = attr_chainon (attributes, attrs);
11478 }
11479
11480 if (attributes != NULL_TREE)
11481 cplus_decl_attributes (&label, attributes, 0);
11482
11483 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11484 }
11485
11486 /* Parse an expression-statement.
11487
11488 expression-statement:
11489 expression [opt] ;
11490
11491 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11492 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11493 indicates whether this expression-statement is part of an
11494 expression statement. */
11495
11496 static tree
11497 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11498 {
11499 tree statement = NULL_TREE;
11500 cp_token *token = cp_lexer_peek_token (parser->lexer);
11501 location_t loc = token->location;
11502
11503 /* There might be attribute fallthrough. */
11504 tree attr = cp_parser_gnu_attributes_opt (parser);
11505
11506 /* If the next token is a ';', then there is no expression
11507 statement. */
11508 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11509 {
11510 statement = cp_parser_expression (parser);
11511 if (statement == error_mark_node
11512 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11513 {
11514 cp_parser_skip_to_end_of_block_or_statement (parser);
11515 return error_mark_node;
11516 }
11517 }
11518
11519 /* Handle [[fallthrough]];. */
11520 if (attribute_fallthrough_p (attr))
11521 {
11522 /* The next token after the fallthrough attribute is ';'. */
11523 if (statement == NULL_TREE)
11524 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11525 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11526 void_type_node, 0);
11527 else
11528 warning_at (loc, OPT_Wattributes,
11529 "%<fallthrough%> attribute not followed by %<;%>");
11530 attr = NULL_TREE;
11531 }
11532
11533 /* Allow "[[fallthrough]];", but warn otherwise. */
11534 if (attr != NULL_TREE)
11535 warning_at (loc, OPT_Wattributes,
11536 "attributes at the beginning of statement are ignored");
11537
11538 /* Give a helpful message for "A<T>::type t;" and the like. */
11539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11540 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11541 {
11542 if (TREE_CODE (statement) == SCOPE_REF)
11543 error_at (token->location, "need %<typename%> before %qE because "
11544 "%qT is a dependent scope",
11545 statement, TREE_OPERAND (statement, 0));
11546 else if (is_overloaded_fn (statement)
11547 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11548 {
11549 /* A::A a; */
11550 tree fn = get_first_fn (statement);
11551 error_at (token->location,
11552 "%<%T::%D%> names the constructor, not the type",
11553 DECL_CONTEXT (fn), DECL_NAME (fn));
11554 }
11555 }
11556
11557 /* Consume the final `;'. */
11558 cp_parser_consume_semicolon_at_end_of_statement (parser);
11559
11560 if (in_statement_expr
11561 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11562 /* This is the final expression statement of a statement
11563 expression. */
11564 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11565 else if (statement)
11566 statement = finish_expr_stmt (statement);
11567
11568 return statement;
11569 }
11570
11571 /* Parse a compound-statement.
11572
11573 compound-statement:
11574 { statement-seq [opt] }
11575
11576 GNU extension:
11577
11578 compound-statement:
11579 { label-declaration-seq [opt] statement-seq [opt] }
11580
11581 label-declaration-seq:
11582 label-declaration
11583 label-declaration-seq label-declaration
11584
11585 Returns a tree representing the statement. */
11586
11587 static tree
11588 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11589 int bcs_flags, bool function_body)
11590 {
11591 tree compound_stmt;
11592 matching_braces braces;
11593
11594 /* Consume the `{'. */
11595 if (!braces.require_open (parser))
11596 return error_mark_node;
11597 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11598 && !function_body && cxx_dialect < cxx14)
11599 pedwarn (input_location, OPT_Wpedantic,
11600 "compound-statement in %<constexpr%> function");
11601 /* Begin the compound-statement. */
11602 compound_stmt = begin_compound_stmt (bcs_flags);
11603 /* If the next keyword is `__label__' we have a label declaration. */
11604 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11605 cp_parser_label_declaration (parser);
11606 /* Parse an (optional) statement-seq. */
11607 cp_parser_statement_seq_opt (parser, in_statement_expr);
11608 /* Finish the compound-statement. */
11609 finish_compound_stmt (compound_stmt);
11610 /* Consume the `}'. */
11611 braces.require_close (parser);
11612
11613 return compound_stmt;
11614 }
11615
11616 /* Parse an (optional) statement-seq.
11617
11618 statement-seq:
11619 statement
11620 statement-seq [opt] statement */
11621
11622 static void
11623 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11624 {
11625 /* Scan statements until there aren't any more. */
11626 while (true)
11627 {
11628 cp_token *token = cp_lexer_peek_token (parser->lexer);
11629
11630 /* If we are looking at a `}', then we have run out of
11631 statements; the same is true if we have reached the end
11632 of file, or have stumbled upon a stray '@end'. */
11633 if (token->type == CPP_CLOSE_BRACE
11634 || token->type == CPP_EOF
11635 || token->type == CPP_PRAGMA_EOL
11636 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11637 break;
11638
11639 /* If we are in a compound statement and find 'else' then
11640 something went wrong. */
11641 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11642 {
11643 if (parser->in_statement & IN_IF_STMT)
11644 break;
11645 else
11646 {
11647 token = cp_lexer_consume_token (parser->lexer);
11648 error_at (token->location, "%<else%> without a previous %<if%>");
11649 }
11650 }
11651
11652 /* Parse the statement. */
11653 cp_parser_statement (parser, in_statement_expr, true, NULL);
11654 }
11655 }
11656
11657 /* Return true if this is the C++20 version of range-based-for with
11658 init-statement. */
11659
11660 static bool
11661 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11662 {
11663 bool r = false;
11664
11665 /* Save tokens so that we can put them back. */
11666 cp_lexer_save_tokens (parser->lexer);
11667
11668 /* There has to be an unnested ; followed by an unnested :. */
11669 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11670 /*recovering=*/false,
11671 CPP_SEMICOLON,
11672 /*consume_paren=*/false) != -1)
11673 goto out;
11674
11675 /* We found the semicolon, eat it now. */
11676 cp_lexer_consume_token (parser->lexer);
11677
11678 /* Now look for ':' that is not nested in () or {}. */
11679 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11680 /*recovering=*/false,
11681 CPP_COLON,
11682 /*consume_paren=*/false) == -1);
11683
11684 out:
11685 /* Roll back the tokens we skipped. */
11686 cp_lexer_rollback_tokens (parser->lexer);
11687
11688 return r;
11689 }
11690
11691 /* Return true if we're looking at (init; cond), false otherwise. */
11692
11693 static bool
11694 cp_parser_init_statement_p (cp_parser *parser)
11695 {
11696 /* Save tokens so that we can put them back. */
11697 cp_lexer_save_tokens (parser->lexer);
11698
11699 /* Look for ';' that is not nested in () or {}. */
11700 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11701 /*recovering=*/false,
11702 CPP_SEMICOLON,
11703 /*consume_paren=*/false);
11704
11705 /* Roll back the tokens we skipped. */
11706 cp_lexer_rollback_tokens (parser->lexer);
11707
11708 return ret == -1;
11709 }
11710
11711 /* Parse a selection-statement.
11712
11713 selection-statement:
11714 if ( init-statement [opt] condition ) statement
11715 if ( init-statement [opt] condition ) statement else statement
11716 switch ( init-statement [opt] condition ) statement
11717
11718 Returns the new IF_STMT or SWITCH_STMT.
11719
11720 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11721 is a (possibly labeled) if statement which is not enclosed in
11722 braces and has an else clause. This is used to implement
11723 -Wparentheses.
11724
11725 CHAIN is a vector of if-else-if conditions. This is used to implement
11726 -Wduplicated-cond. */
11727
11728 static tree
11729 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11730 vec<tree> *chain)
11731 {
11732 cp_token *token;
11733 enum rid keyword;
11734 token_indent_info guard_tinfo;
11735
11736 if (if_p != NULL)
11737 *if_p = false;
11738
11739 /* Peek at the next token. */
11740 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11741 guard_tinfo = get_token_indent_info (token);
11742
11743 /* See what kind of keyword it is. */
11744 keyword = token->keyword;
11745 switch (keyword)
11746 {
11747 case RID_IF:
11748 case RID_SWITCH:
11749 {
11750 tree statement;
11751 tree condition;
11752
11753 bool cx = false;
11754 if (keyword == RID_IF
11755 && cp_lexer_next_token_is_keyword (parser->lexer,
11756 RID_CONSTEXPR))
11757 {
11758 cx = true;
11759 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11760 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11761 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11762 "with %<-std=c++17%> or %<-std=gnu++17%>");
11763 }
11764
11765 /* Look for the `('. */
11766 matching_parens parens;
11767 if (!parens.require_open (parser))
11768 {
11769 cp_parser_skip_to_end_of_statement (parser);
11770 return error_mark_node;
11771 }
11772
11773 /* Begin the selection-statement. */
11774 if (keyword == RID_IF)
11775 {
11776 statement = begin_if_stmt ();
11777 IF_STMT_CONSTEXPR_P (statement) = cx;
11778 }
11779 else
11780 statement = begin_switch_stmt ();
11781
11782 /* Parse the optional init-statement. */
11783 if (cp_parser_init_statement_p (parser))
11784 {
11785 tree decl;
11786 if (cxx_dialect < cxx17)
11787 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11788 "init-statement in selection statements only available "
11789 "with %<-std=c++17%> or %<-std=gnu++17%>");
11790 cp_parser_init_statement (parser, &decl);
11791 }
11792
11793 /* Parse the condition. */
11794 condition = cp_parser_condition (parser);
11795 /* Look for the `)'. */
11796 if (!parens.require_close (parser))
11797 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11798 /*consume_paren=*/true);
11799
11800 if (keyword == RID_IF)
11801 {
11802 bool nested_if;
11803 unsigned char in_statement;
11804
11805 /* Add the condition. */
11806 condition = finish_if_stmt_cond (condition, statement);
11807
11808 if (warn_duplicated_cond)
11809 warn_duplicated_cond_add_or_warn (token->location, condition,
11810 &chain);
11811
11812 /* Parse the then-clause. */
11813 in_statement = parser->in_statement;
11814 parser->in_statement |= IN_IF_STMT;
11815
11816 /* Outside a template, the non-selected branch of a constexpr
11817 if is a 'discarded statement', i.e. unevaluated. */
11818 bool was_discarded = in_discarded_stmt;
11819 bool discard_then = (cx && !processing_template_decl
11820 && integer_zerop (condition));
11821 if (discard_then)
11822 {
11823 in_discarded_stmt = true;
11824 ++c_inhibit_evaluation_warnings;
11825 }
11826
11827 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11828 guard_tinfo);
11829
11830 parser->in_statement = in_statement;
11831
11832 finish_then_clause (statement);
11833
11834 if (discard_then)
11835 {
11836 THEN_CLAUSE (statement) = NULL_TREE;
11837 in_discarded_stmt = was_discarded;
11838 --c_inhibit_evaluation_warnings;
11839 }
11840
11841 /* If the next token is `else', parse the else-clause. */
11842 if (cp_lexer_next_token_is_keyword (parser->lexer,
11843 RID_ELSE))
11844 {
11845 bool discard_else = (cx && !processing_template_decl
11846 && integer_nonzerop (condition));
11847 if (discard_else)
11848 {
11849 in_discarded_stmt = true;
11850 ++c_inhibit_evaluation_warnings;
11851 }
11852
11853 guard_tinfo
11854 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11855 /* Consume the `else' keyword. */
11856 cp_lexer_consume_token (parser->lexer);
11857 if (warn_duplicated_cond)
11858 {
11859 if (cp_lexer_next_token_is_keyword (parser->lexer,
11860 RID_IF)
11861 && chain == NULL)
11862 {
11863 /* We've got "if (COND) else if (COND2)". Start
11864 the condition chain and add COND as the first
11865 element. */
11866 chain = new vec<tree> ();
11867 if (!CONSTANT_CLASS_P (condition)
11868 && !TREE_SIDE_EFFECTS (condition))
11869 {
11870 /* Wrap it in a NOP_EXPR so that we can set the
11871 location of the condition. */
11872 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11873 condition);
11874 SET_EXPR_LOCATION (e, token->location);
11875 chain->safe_push (e);
11876 }
11877 }
11878 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11879 RID_IF))
11880 {
11881 /* This is if-else without subsequent if. Zap the
11882 condition chain; we would have already warned at
11883 this point. */
11884 delete chain;
11885 chain = NULL;
11886 }
11887 }
11888 begin_else_clause (statement);
11889 /* Parse the else-clause. */
11890 cp_parser_implicitly_scoped_statement (parser, NULL,
11891 guard_tinfo, chain);
11892
11893 finish_else_clause (statement);
11894
11895 /* If we are currently parsing a then-clause, then
11896 IF_P will not be NULL. We set it to true to
11897 indicate that this if statement has an else clause.
11898 This may trigger the Wparentheses warning below
11899 when we get back up to the parent if statement. */
11900 if (if_p != NULL)
11901 *if_p = true;
11902
11903 if (discard_else)
11904 {
11905 ELSE_CLAUSE (statement) = NULL_TREE;
11906 in_discarded_stmt = was_discarded;
11907 --c_inhibit_evaluation_warnings;
11908 }
11909 }
11910 else
11911 {
11912 /* This if statement does not have an else clause. If
11913 NESTED_IF is true, then the then-clause has an if
11914 statement which does have an else clause. We warn
11915 about the potential ambiguity. */
11916 if (nested_if)
11917 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11918 "suggest explicit braces to avoid ambiguous"
11919 " %<else%>");
11920 if (warn_duplicated_cond)
11921 {
11922 /* We don't need the condition chain anymore. */
11923 delete chain;
11924 chain = NULL;
11925 }
11926 }
11927
11928 /* Now we're all done with the if-statement. */
11929 finish_if_stmt (statement);
11930 }
11931 else
11932 {
11933 bool in_switch_statement_p;
11934 unsigned char in_statement;
11935
11936 /* Add the condition. */
11937 finish_switch_cond (condition, statement);
11938
11939 /* Parse the body of the switch-statement. */
11940 in_switch_statement_p = parser->in_switch_statement_p;
11941 in_statement = parser->in_statement;
11942 parser->in_switch_statement_p = true;
11943 parser->in_statement |= IN_SWITCH_STMT;
11944 cp_parser_implicitly_scoped_statement (parser, if_p,
11945 guard_tinfo);
11946 parser->in_switch_statement_p = in_switch_statement_p;
11947 parser->in_statement = in_statement;
11948
11949 /* Now we're all done with the switch-statement. */
11950 finish_switch_stmt (statement);
11951 }
11952
11953 return statement;
11954 }
11955 break;
11956
11957 default:
11958 cp_parser_error (parser, "expected selection-statement");
11959 return error_mark_node;
11960 }
11961 }
11962
11963 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11964 If we have seen at least one decl-specifier, and the next token
11965 is not a parenthesis, then we must be looking at a declaration.
11966 (After "int (" we might be looking at a functional cast.) */
11967
11968 static void
11969 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11970 bool any_specifiers_p)
11971 {
11972 if (any_specifiers_p
11973 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11974 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11975 && !cp_parser_error_occurred (parser))
11976 cp_parser_commit_to_tentative_parse (parser);
11977 }
11978
11979 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11980 The declarator shall not specify a function or an array. Returns
11981 TRUE if the declarator is valid, FALSE otherwise. */
11982
11983 static bool
11984 cp_parser_check_condition_declarator (cp_parser* parser,
11985 cp_declarator *declarator,
11986 location_t loc)
11987 {
11988 if (declarator == cp_error_declarator
11989 || function_declarator_p (declarator)
11990 || declarator->kind == cdk_array)
11991 {
11992 if (declarator == cp_error_declarator)
11993 /* Already complained. */;
11994 else if (declarator->kind == cdk_array)
11995 error_at (loc, "condition declares an array");
11996 else
11997 error_at (loc, "condition declares a function");
11998 if (parser->fully_implicit_function_template_p)
11999 abort_fully_implicit_template (parser);
12000 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
12001 /*or_comma=*/false,
12002 /*consume_paren=*/false);
12003 return false;
12004 }
12005 else
12006 return true;
12007 }
12008
12009 /* Parse a condition.
12010
12011 condition:
12012 expression
12013 type-specifier-seq declarator = initializer-clause
12014 type-specifier-seq declarator braced-init-list
12015
12016 GNU Extension:
12017
12018 condition:
12019 type-specifier-seq declarator asm-specification [opt]
12020 attributes [opt] = assignment-expression
12021
12022 Returns the expression that should be tested. */
12023
12024 static tree
12025 cp_parser_condition (cp_parser* parser)
12026 {
12027 cp_decl_specifier_seq type_specifiers;
12028 const char *saved_message;
12029 int declares_class_or_enum;
12030
12031 /* Try the declaration first. */
12032 cp_parser_parse_tentatively (parser);
12033 /* New types are not allowed in the type-specifier-seq for a
12034 condition. */
12035 saved_message = parser->type_definition_forbidden_message;
12036 parser->type_definition_forbidden_message
12037 = G_("types may not be defined in conditions");
12038 /* Parse the type-specifier-seq. */
12039 cp_parser_decl_specifier_seq (parser,
12040 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
12041 &type_specifiers,
12042 &declares_class_or_enum);
12043 /* Restore the saved message. */
12044 parser->type_definition_forbidden_message = saved_message;
12045
12046 cp_parser_maybe_commit_to_declaration (parser,
12047 type_specifiers.any_specifiers_p);
12048
12049 /* If all is well, we might be looking at a declaration. */
12050 if (!cp_parser_error_occurred (parser))
12051 {
12052 tree decl;
12053 tree asm_specification;
12054 tree attributes;
12055 cp_declarator *declarator;
12056 tree initializer = NULL_TREE;
12057 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12058
12059 /* Parse the declarator. */
12060 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12061 CP_PARSER_FLAGS_NONE,
12062 /*ctor_dtor_or_conv_p=*/NULL,
12063 /*parenthesized_p=*/NULL,
12064 /*member_p=*/false,
12065 /*friend_p=*/false,
12066 /*static_p=*/false);
12067 /* Parse the attributes. */
12068 attributes = cp_parser_attributes_opt (parser);
12069 /* Parse the asm-specification. */
12070 asm_specification = cp_parser_asm_specification_opt (parser);
12071 /* If the next token is not an `=' or '{', then we might still be
12072 looking at an expression. For example:
12073
12074 if (A(a).x)
12075
12076 looks like a decl-specifier-seq and a declarator -- but then
12077 there is no `=', so this is an expression. */
12078 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12079 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12080 cp_parser_simulate_error (parser);
12081
12082 /* If we did see an `=' or '{', then we are looking at a declaration
12083 for sure. */
12084 if (cp_parser_parse_definitely (parser))
12085 {
12086 tree pushed_scope;
12087 bool non_constant_p = false;
12088 int flags = LOOKUP_ONLYCONVERTING;
12089
12090 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12091 return error_mark_node;
12092
12093 /* Create the declaration. */
12094 decl = start_decl (declarator, &type_specifiers,
12095 /*initialized_p=*/true,
12096 attributes, /*prefix_attributes=*/NULL_TREE,
12097 &pushed_scope);
12098
12099 /* Parse the initializer. */
12100 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12101 {
12102 initializer = cp_parser_braced_list (parser, &non_constant_p);
12103 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12104 flags = 0;
12105 }
12106 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12107 {
12108 /* Consume the `='. */
12109 cp_lexer_consume_token (parser->lexer);
12110 initializer = cp_parser_initializer_clause (parser,
12111 &non_constant_p);
12112 }
12113 else
12114 {
12115 cp_parser_error (parser, "expected initializer");
12116 initializer = error_mark_node;
12117 }
12118 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12119 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12120
12121 /* Process the initializer. */
12122 cp_finish_decl (decl,
12123 initializer, !non_constant_p,
12124 asm_specification,
12125 flags);
12126
12127 if (pushed_scope)
12128 pop_scope (pushed_scope);
12129
12130 return convert_from_reference (decl);
12131 }
12132 }
12133 /* If we didn't even get past the declarator successfully, we are
12134 definitely not looking at a declaration. */
12135 else
12136 cp_parser_abort_tentative_parse (parser);
12137
12138 /* Otherwise, we are looking at an expression. */
12139 return cp_parser_expression (parser);
12140 }
12141
12142 /* Parses a for-statement or range-for-statement until the closing ')',
12143 not included. */
12144
12145 static tree
12146 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12147 {
12148 tree init, scope, decl;
12149 bool is_range_for;
12150
12151 /* Begin the for-statement. */
12152 scope = begin_for_scope (&init);
12153
12154 /* Parse the initialization. */
12155 is_range_for = cp_parser_init_statement (parser, &decl);
12156
12157 if (is_range_for)
12158 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12159 false);
12160 else
12161 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12162 }
12163
12164 static tree
12165 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12166 unsigned short unroll)
12167 {
12168 /* Normal for loop */
12169 tree condition = NULL_TREE;
12170 tree expression = NULL_TREE;
12171 tree stmt;
12172
12173 stmt = begin_for_stmt (scope, init);
12174 /* The init-statement has already been parsed in
12175 cp_parser_init_statement, so no work is needed here. */
12176 finish_init_stmt (stmt);
12177
12178 /* If there's a condition, process it. */
12179 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12180 condition = cp_parser_condition (parser);
12181 else if (ivdep)
12182 {
12183 cp_parser_error (parser, "missing loop condition in loop with "
12184 "%<GCC ivdep%> pragma");
12185 condition = error_mark_node;
12186 }
12187 else if (unroll)
12188 {
12189 cp_parser_error (parser, "missing loop condition in loop with "
12190 "%<GCC unroll%> pragma");
12191 condition = error_mark_node;
12192 }
12193 finish_for_cond (condition, stmt, ivdep, unroll);
12194 /* Look for the `;'. */
12195 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12196
12197 /* If there's an expression, process it. */
12198 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12199 expression = cp_parser_expression (parser);
12200 finish_for_expr (expression, stmt);
12201
12202 return stmt;
12203 }
12204
12205 /* Tries to parse a range-based for-statement:
12206
12207 range-based-for:
12208 decl-specifier-seq declarator : expression
12209
12210 The decl-specifier-seq declarator and the `:' are already parsed by
12211 cp_parser_init_statement. If processing_template_decl it returns a
12212 newly created RANGE_FOR_STMT; if not, it is converted to a
12213 regular FOR_STMT. */
12214
12215 static tree
12216 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12217 bool ivdep, unsigned short unroll, bool is_omp)
12218 {
12219 tree stmt, range_expr;
12220 auto_vec <cxx_binding *, 16> bindings;
12221 auto_vec <tree, 16> names;
12222 tree decomp_first_name = NULL_TREE;
12223 unsigned int decomp_cnt = 0;
12224
12225 /* Get the range declaration momentarily out of the way so that
12226 the range expression doesn't clash with it. */
12227 if (range_decl != error_mark_node)
12228 {
12229 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12230 {
12231 tree v = DECL_VALUE_EXPR (range_decl);
12232 /* For decomposition declaration get all of the corresponding
12233 declarations out of the way. */
12234 if (TREE_CODE (v) == ARRAY_REF
12235 && VAR_P (TREE_OPERAND (v, 0))
12236 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12237 {
12238 tree d = range_decl;
12239 range_decl = TREE_OPERAND (v, 0);
12240 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12241 decomp_first_name = d;
12242 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12243 {
12244 tree name = DECL_NAME (d);
12245 names.safe_push (name);
12246 bindings.safe_push (IDENTIFIER_BINDING (name));
12247 IDENTIFIER_BINDING (name)
12248 = IDENTIFIER_BINDING (name)->previous;
12249 }
12250 }
12251 }
12252 if (names.is_empty ())
12253 {
12254 tree name = DECL_NAME (range_decl);
12255 names.safe_push (name);
12256 bindings.safe_push (IDENTIFIER_BINDING (name));
12257 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12258 }
12259 }
12260
12261 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12262 {
12263 bool expr_non_constant_p;
12264 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12265 }
12266 else
12267 range_expr = cp_parser_expression (parser);
12268
12269 /* Put the range declaration(s) back into scope. */
12270 for (unsigned int i = 0; i < names.length (); i++)
12271 {
12272 cxx_binding *binding = bindings[i];
12273 binding->previous = IDENTIFIER_BINDING (names[i]);
12274 IDENTIFIER_BINDING (names[i]) = binding;
12275 }
12276
12277 /* finish_omp_for has its own code for the following, so just
12278 return the range_expr instead. */
12279 if (is_omp)
12280 return range_expr;
12281
12282 /* If in template, STMT is converted to a normal for-statement
12283 at instantiation. If not, it is done just ahead. */
12284 if (processing_template_decl)
12285 {
12286 if (check_for_bare_parameter_packs (range_expr))
12287 range_expr = error_mark_node;
12288 stmt = begin_range_for_stmt (scope, init);
12289 if (ivdep)
12290 RANGE_FOR_IVDEP (stmt) = 1;
12291 if (unroll)
12292 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12293 finish_range_for_decl (stmt, range_decl, range_expr);
12294 if (!type_dependent_expression_p (range_expr)
12295 /* do_auto_deduction doesn't mess with template init-lists. */
12296 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12297 do_range_for_auto_deduction (range_decl, range_expr);
12298 }
12299 else
12300 {
12301 stmt = begin_for_stmt (scope, init);
12302 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12303 decomp_first_name, decomp_cnt, ivdep,
12304 unroll);
12305 }
12306 return stmt;
12307 }
12308
12309 /* Subroutine of cp_convert_range_for: given the initializer expression,
12310 builds up the range temporary. */
12311
12312 static tree
12313 build_range_temp (tree range_expr)
12314 {
12315 tree range_type, range_temp;
12316
12317 /* Find out the type deduced by the declaration
12318 `auto &&__range = range_expr'. */
12319 range_type = cp_build_reference_type (make_auto (), true);
12320 range_type = do_auto_deduction (range_type, range_expr,
12321 type_uses_auto (range_type));
12322
12323 /* Create the __range variable. */
12324 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12325 range_type);
12326 TREE_USED (range_temp) = 1;
12327 DECL_ARTIFICIAL (range_temp) = 1;
12328
12329 return range_temp;
12330 }
12331
12332 /* Used by cp_parser_range_for in template context: we aren't going to
12333 do a full conversion yet, but we still need to resolve auto in the
12334 type of the for-range-declaration if present. This is basically
12335 a shortcut version of cp_convert_range_for. */
12336
12337 static void
12338 do_range_for_auto_deduction (tree decl, tree range_expr)
12339 {
12340 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12341 if (auto_node)
12342 {
12343 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12344 range_temp = convert_from_reference (build_range_temp (range_expr));
12345 iter_type = (cp_parser_perform_range_for_lookup
12346 (range_temp, &begin_dummy, &end_dummy));
12347 if (iter_type)
12348 {
12349 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12350 iter_type);
12351 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12352 RO_UNARY_STAR,
12353 tf_warning_or_error);
12354 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12355 iter_decl, auto_node);
12356 }
12357 }
12358 }
12359
12360 /* Converts a range-based for-statement into a normal
12361 for-statement, as per the definition.
12362
12363 for (RANGE_DECL : RANGE_EXPR)
12364 BLOCK
12365
12366 should be equivalent to:
12367
12368 {
12369 auto &&__range = RANGE_EXPR;
12370 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12371 __begin != __end;
12372 ++__begin)
12373 {
12374 RANGE_DECL = *__begin;
12375 BLOCK
12376 }
12377 }
12378
12379 If RANGE_EXPR is an array:
12380 BEGIN_EXPR = __range
12381 END_EXPR = __range + ARRAY_SIZE(__range)
12382 Else if RANGE_EXPR has a member 'begin' or 'end':
12383 BEGIN_EXPR = __range.begin()
12384 END_EXPR = __range.end()
12385 Else:
12386 BEGIN_EXPR = begin(__range)
12387 END_EXPR = end(__range);
12388
12389 If __range has a member 'begin' but not 'end', or vice versa, we must
12390 still use the second alternative (it will surely fail, however).
12391 When calling begin()/end() in the third alternative we must use
12392 argument dependent lookup, but always considering 'std' as an associated
12393 namespace. */
12394
12395 tree
12396 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12397 tree decomp_first_name, unsigned int decomp_cnt,
12398 bool ivdep, unsigned short unroll)
12399 {
12400 tree begin, end;
12401 tree iter_type, begin_expr, end_expr;
12402 tree condition, expression;
12403
12404 range_expr = mark_lvalue_use (range_expr);
12405
12406 if (range_decl == error_mark_node || range_expr == error_mark_node)
12407 /* If an error happened previously do nothing or else a lot of
12408 unhelpful errors would be issued. */
12409 begin_expr = end_expr = iter_type = error_mark_node;
12410 else
12411 {
12412 tree range_temp;
12413
12414 if (VAR_P (range_expr)
12415 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12416 /* Can't bind a reference to an array of runtime bound. */
12417 range_temp = range_expr;
12418 else
12419 {
12420 range_temp = build_range_temp (range_expr);
12421 pushdecl (range_temp);
12422 cp_finish_decl (range_temp, range_expr,
12423 /*is_constant_init*/false, NULL_TREE,
12424 LOOKUP_ONLYCONVERTING);
12425 range_temp = convert_from_reference (range_temp);
12426 }
12427 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12428 &begin_expr, &end_expr);
12429 }
12430
12431 /* The new for initialization statement. */
12432 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12433 iter_type);
12434 TREE_USED (begin) = 1;
12435 DECL_ARTIFICIAL (begin) = 1;
12436 pushdecl (begin);
12437 cp_finish_decl (begin, begin_expr,
12438 /*is_constant_init*/false, NULL_TREE,
12439 LOOKUP_ONLYCONVERTING);
12440
12441 if (cxx_dialect >= cxx17)
12442 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12443 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12444 TREE_USED (end) = 1;
12445 DECL_ARTIFICIAL (end) = 1;
12446 pushdecl (end);
12447 cp_finish_decl (end, end_expr,
12448 /*is_constant_init*/false, NULL_TREE,
12449 LOOKUP_ONLYCONVERTING);
12450
12451 finish_init_stmt (statement);
12452
12453 /* The new for condition. */
12454 condition = build_x_binary_op (input_location, NE_EXPR,
12455 begin, ERROR_MARK,
12456 end, ERROR_MARK,
12457 NULL, tf_warning_or_error);
12458 finish_for_cond (condition, statement, ivdep, unroll);
12459
12460 /* The new increment expression. */
12461 expression = finish_unary_op_expr (input_location,
12462 PREINCREMENT_EXPR, begin,
12463 tf_warning_or_error);
12464 finish_for_expr (expression, statement);
12465
12466 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12467 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12468
12469 /* The declaration is initialized with *__begin inside the loop body. */
12470 cp_finish_decl (range_decl,
12471 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12472 tf_warning_or_error),
12473 /*is_constant_init*/false, NULL_TREE,
12474 LOOKUP_ONLYCONVERTING);
12475 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12476 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12477
12478 return statement;
12479 }
12480
12481 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12482 We need to solve both at the same time because the method used
12483 depends on the existence of members begin or end.
12484 Returns the type deduced for the iterator expression. */
12485
12486 static tree
12487 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12488 {
12489 if (error_operand_p (range))
12490 {
12491 *begin = *end = error_mark_node;
12492 return error_mark_node;
12493 }
12494
12495 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12496 {
12497 error ("range-based %<for%> expression of type %qT "
12498 "has incomplete type", TREE_TYPE (range));
12499 *begin = *end = error_mark_node;
12500 return error_mark_node;
12501 }
12502 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12503 {
12504 /* If RANGE is an array, we will use pointer arithmetic. */
12505 *begin = decay_conversion (range, tf_warning_or_error);
12506 *end = build_binary_op (input_location, PLUS_EXPR,
12507 range,
12508 array_type_nelts_top (TREE_TYPE (range)),
12509 false);
12510 return TREE_TYPE (*begin);
12511 }
12512 else
12513 {
12514 /* If it is not an array, we must do a bit of magic. */
12515 tree id_begin, id_end;
12516 tree member_begin, member_end;
12517
12518 *begin = *end = error_mark_node;
12519
12520 id_begin = get_identifier ("begin");
12521 id_end = get_identifier ("end");
12522 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12523 /*protect=*/2, /*want_type=*/false,
12524 tf_warning_or_error);
12525 member_end = lookup_member (TREE_TYPE (range), id_end,
12526 /*protect=*/2, /*want_type=*/false,
12527 tf_warning_or_error);
12528
12529 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12530 {
12531 /* Use the member functions. */
12532 *begin = cp_parser_range_for_member_function (range, id_begin);
12533 *end = cp_parser_range_for_member_function (range, id_end);
12534 }
12535 else
12536 {
12537 /* Use global functions with ADL. */
12538 vec<tree, va_gc> *vec;
12539 vec = make_tree_vector ();
12540
12541 vec_safe_push (vec, range);
12542
12543 member_begin = perform_koenig_lookup (id_begin, vec,
12544 tf_warning_or_error);
12545 *begin = finish_call_expr (member_begin, &vec, false, true,
12546 tf_warning_or_error);
12547 member_end = perform_koenig_lookup (id_end, vec,
12548 tf_warning_or_error);
12549 *end = finish_call_expr (member_end, &vec, false, true,
12550 tf_warning_or_error);
12551
12552 release_tree_vector (vec);
12553 }
12554
12555 /* Last common checks. */
12556 if (*begin == error_mark_node || *end == error_mark_node)
12557 {
12558 /* If one of the expressions is an error do no more checks. */
12559 *begin = *end = error_mark_node;
12560 return error_mark_node;
12561 }
12562 else if (type_dependent_expression_p (*begin)
12563 || type_dependent_expression_p (*end))
12564 /* Can happen, when, eg, in a template context, Koenig lookup
12565 can't resolve begin/end (c++/58503). */
12566 return NULL_TREE;
12567 else
12568 {
12569 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12570 /* The unqualified type of the __begin and __end temporaries should
12571 be the same, as required by the multiple auto declaration. */
12572 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12573 {
12574 if (cxx_dialect >= cxx17
12575 && (build_x_binary_op (input_location, NE_EXPR,
12576 *begin, ERROR_MARK,
12577 *end, ERROR_MARK,
12578 NULL, tf_none)
12579 != error_mark_node))
12580 /* P0184R0 allows __begin and __end to have different types,
12581 but make sure they are comparable so we can give a better
12582 diagnostic. */;
12583 else
12584 error ("inconsistent begin/end types in range-based %<for%> "
12585 "statement: %qT and %qT",
12586 TREE_TYPE (*begin), TREE_TYPE (*end));
12587 }
12588 return iter_type;
12589 }
12590 }
12591 }
12592
12593 /* Helper function for cp_parser_perform_range_for_lookup.
12594 Builds a tree for RANGE.IDENTIFIER(). */
12595
12596 static tree
12597 cp_parser_range_for_member_function (tree range, tree identifier)
12598 {
12599 tree member, res;
12600 vec<tree, va_gc> *vec;
12601
12602 member = finish_class_member_access_expr (range, identifier,
12603 false, tf_warning_or_error);
12604 if (member == error_mark_node)
12605 return error_mark_node;
12606
12607 vec = make_tree_vector ();
12608 res = finish_call_expr (member, &vec,
12609 /*disallow_virtual=*/false,
12610 /*koenig_p=*/false,
12611 tf_warning_or_error);
12612 release_tree_vector (vec);
12613 return res;
12614 }
12615
12616 /* Parse an iteration-statement.
12617
12618 iteration-statement:
12619 while ( condition ) statement
12620 do statement while ( expression ) ;
12621 for ( init-statement condition [opt] ; expression [opt] )
12622 statement
12623
12624 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12625
12626 static tree
12627 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12628 unsigned short unroll)
12629 {
12630 cp_token *token;
12631 enum rid keyword;
12632 tree statement;
12633 unsigned char in_statement;
12634 token_indent_info guard_tinfo;
12635
12636 /* Peek at the next token. */
12637 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12638 if (!token)
12639 return error_mark_node;
12640
12641 guard_tinfo = get_token_indent_info (token);
12642
12643 /* Remember whether or not we are already within an iteration
12644 statement. */
12645 in_statement = parser->in_statement;
12646
12647 /* See what kind of keyword it is. */
12648 keyword = token->keyword;
12649 switch (keyword)
12650 {
12651 case RID_WHILE:
12652 {
12653 tree condition;
12654
12655 /* Begin the while-statement. */
12656 statement = begin_while_stmt ();
12657 /* Look for the `('. */
12658 matching_parens parens;
12659 parens.require_open (parser);
12660 /* Parse the condition. */
12661 condition = cp_parser_condition (parser);
12662 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12663 /* Look for the `)'. */
12664 parens.require_close (parser);
12665 /* Parse the dependent statement. */
12666 parser->in_statement = IN_ITERATION_STMT;
12667 bool prev = note_iteration_stmt_body_start ();
12668 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12669 note_iteration_stmt_body_end (prev);
12670 parser->in_statement = in_statement;
12671 /* We're done with the while-statement. */
12672 finish_while_stmt (statement);
12673 }
12674 break;
12675
12676 case RID_DO:
12677 {
12678 tree expression;
12679
12680 /* Begin the do-statement. */
12681 statement = begin_do_stmt ();
12682 /* Parse the body of the do-statement. */
12683 parser->in_statement = IN_ITERATION_STMT;
12684 bool prev = note_iteration_stmt_body_start ();
12685 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12686 note_iteration_stmt_body_end (prev);
12687 parser->in_statement = in_statement;
12688 finish_do_body (statement);
12689 /* Look for the `while' keyword. */
12690 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12691 /* Look for the `('. */
12692 matching_parens parens;
12693 parens.require_open (parser);
12694 /* Parse the expression. */
12695 expression = cp_parser_expression (parser);
12696 /* We're done with the do-statement. */
12697 finish_do_stmt (expression, statement, ivdep, unroll);
12698 /* Look for the `)'. */
12699 parens.require_close (parser);
12700 /* Look for the `;'. */
12701 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12702 }
12703 break;
12704
12705 case RID_FOR:
12706 {
12707 /* Look for the `('. */
12708 matching_parens parens;
12709 parens.require_open (parser);
12710
12711 statement = cp_parser_for (parser, ivdep, unroll);
12712
12713 /* Look for the `)'. */
12714 parens.require_close (parser);
12715
12716 /* Parse the body of the for-statement. */
12717 parser->in_statement = IN_ITERATION_STMT;
12718 bool prev = note_iteration_stmt_body_start ();
12719 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12720 note_iteration_stmt_body_end (prev);
12721 parser->in_statement = in_statement;
12722
12723 /* We're done with the for-statement. */
12724 finish_for_stmt (statement);
12725 }
12726 break;
12727
12728 default:
12729 cp_parser_error (parser, "expected iteration-statement");
12730 statement = error_mark_node;
12731 break;
12732 }
12733
12734 return statement;
12735 }
12736
12737 /* Parse a init-statement or the declarator of a range-based-for.
12738 Returns true if a range-based-for declaration is seen.
12739
12740 init-statement:
12741 expression-statement
12742 simple-declaration */
12743
12744 static bool
12745 cp_parser_init_statement (cp_parser *parser, tree *decl)
12746 {
12747 /* If the next token is a `;', then we have an empty
12748 expression-statement. Grammatically, this is also a
12749 simple-declaration, but an invalid one, because it does not
12750 declare anything. Therefore, if we did not handle this case
12751 specially, we would issue an error message about an invalid
12752 declaration. */
12753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12754 {
12755 bool is_range_for = false;
12756 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12757
12758 /* Try to parse the init-statement. */
12759 if (cp_parser_range_based_for_with_init_p (parser))
12760 {
12761 tree dummy;
12762 cp_parser_parse_tentatively (parser);
12763 /* Parse the declaration. */
12764 cp_parser_simple_declaration (parser,
12765 /*function_definition_allowed_p=*/false,
12766 &dummy);
12767 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12768 if (!cp_parser_parse_definitely (parser))
12769 /* That didn't work, try to parse it as an expression-statement. */
12770 cp_parser_expression_statement (parser, NULL_TREE);
12771
12772 if (cxx_dialect < cxx2a)
12773 {
12774 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12775 "range-based %<for%> loops with initializer only "
12776 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
12777 *decl = error_mark_node;
12778 }
12779 }
12780
12781 /* A colon is used in range-based for. */
12782 parser->colon_corrects_to_scope_p = false;
12783
12784 /* We're going to speculatively look for a declaration, falling back
12785 to an expression, if necessary. */
12786 cp_parser_parse_tentatively (parser);
12787 /* Parse the declaration. */
12788 cp_parser_simple_declaration (parser,
12789 /*function_definition_allowed_p=*/false,
12790 decl);
12791 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12792 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12793 {
12794 /* It is a range-for, consume the ':'. */
12795 cp_lexer_consume_token (parser->lexer);
12796 is_range_for = true;
12797 if (cxx_dialect < cxx11)
12798 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12799 "range-based %<for%> loops only available with "
12800 "%<-std=c++11%> or %<-std=gnu++11%>");
12801 }
12802 else
12803 /* The ';' is not consumed yet because we told
12804 cp_parser_simple_declaration not to. */
12805 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12806
12807 if (cp_parser_parse_definitely (parser))
12808 return is_range_for;
12809 /* If the tentative parse failed, then we shall need to look for an
12810 expression-statement. */
12811 }
12812 /* If we are here, it is an expression-statement. */
12813 cp_parser_expression_statement (parser, NULL_TREE);
12814 return false;
12815 }
12816
12817 /* Parse a jump-statement.
12818
12819 jump-statement:
12820 break ;
12821 continue ;
12822 return expression [opt] ;
12823 return braced-init-list ;
12824 goto identifier ;
12825
12826 GNU extension:
12827
12828 jump-statement:
12829 goto * expression ;
12830
12831 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12832
12833 static tree
12834 cp_parser_jump_statement (cp_parser* parser)
12835 {
12836 tree statement = error_mark_node;
12837 cp_token *token;
12838 enum rid keyword;
12839 unsigned char in_statement;
12840
12841 /* Peek at the next token. */
12842 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12843 if (!token)
12844 return error_mark_node;
12845
12846 /* See what kind of keyword it is. */
12847 keyword = token->keyword;
12848 switch (keyword)
12849 {
12850 case RID_BREAK:
12851 in_statement = parser->in_statement & ~IN_IF_STMT;
12852 switch (in_statement)
12853 {
12854 case 0:
12855 error_at (token->location, "break statement not within loop or switch");
12856 break;
12857 default:
12858 gcc_assert ((in_statement & IN_SWITCH_STMT)
12859 || in_statement == IN_ITERATION_STMT);
12860 statement = finish_break_stmt ();
12861 if (in_statement == IN_ITERATION_STMT)
12862 break_maybe_infinite_loop ();
12863 break;
12864 case IN_OMP_BLOCK:
12865 error_at (token->location, "invalid exit from OpenMP structured block");
12866 break;
12867 case IN_OMP_FOR:
12868 error_at (token->location, "break statement used with OpenMP for loop");
12869 break;
12870 }
12871 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12872 break;
12873
12874 case RID_CONTINUE:
12875 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12876 {
12877 case 0:
12878 error_at (token->location, "continue statement not within a loop");
12879 break;
12880 /* Fall through. */
12881 case IN_ITERATION_STMT:
12882 case IN_OMP_FOR:
12883 statement = finish_continue_stmt ();
12884 break;
12885 case IN_OMP_BLOCK:
12886 error_at (token->location, "invalid exit from OpenMP structured block");
12887 break;
12888 default:
12889 gcc_unreachable ();
12890 }
12891 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12892 break;
12893
12894 case RID_RETURN:
12895 {
12896 tree expr;
12897 bool expr_non_constant_p;
12898
12899 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12900 {
12901 cp_lexer_set_source_position (parser->lexer);
12902 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12903 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12904 }
12905 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12906 expr = cp_parser_expression (parser);
12907 else
12908 /* If the next token is a `;', then there is no
12909 expression. */
12910 expr = NULL_TREE;
12911 /* Build the return-statement. */
12912 if (current_function_auto_return_pattern && in_discarded_stmt)
12913 /* Don't deduce from a discarded return statement. */;
12914 else
12915 statement = finish_return_stmt (expr);
12916 /* Look for the final `;'. */
12917 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12918 }
12919 break;
12920
12921 case RID_GOTO:
12922 if (parser->in_function_body
12923 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12924 {
12925 error ("%<goto%> in %<constexpr%> function");
12926 cp_function_chain->invalid_constexpr = true;
12927 }
12928
12929 /* Create the goto-statement. */
12930 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12931 {
12932 /* Issue a warning about this use of a GNU extension. */
12933 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12934 /* Consume the '*' token. */
12935 cp_lexer_consume_token (parser->lexer);
12936 /* Parse the dependent expression. */
12937 finish_goto_stmt (cp_parser_expression (parser));
12938 }
12939 else
12940 finish_goto_stmt (cp_parser_identifier (parser));
12941 /* Look for the final `;'. */
12942 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12943 break;
12944
12945 default:
12946 cp_parser_error (parser, "expected jump-statement");
12947 break;
12948 }
12949
12950 return statement;
12951 }
12952
12953 /* Parse a declaration-statement.
12954
12955 declaration-statement:
12956 block-declaration */
12957
12958 static void
12959 cp_parser_declaration_statement (cp_parser* parser)
12960 {
12961 void *p;
12962
12963 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12964 p = obstack_alloc (&declarator_obstack, 0);
12965
12966 /* Parse the block-declaration. */
12967 cp_parser_block_declaration (parser, /*statement_p=*/true);
12968
12969 /* Free any declarators allocated. */
12970 obstack_free (&declarator_obstack, p);
12971 }
12972
12973 /* Some dependent statements (like `if (cond) statement'), are
12974 implicitly in their own scope. In other words, if the statement is
12975 a single statement (as opposed to a compound-statement), it is
12976 none-the-less treated as if it were enclosed in braces. Any
12977 declarations appearing in the dependent statement are out of scope
12978 after control passes that point. This function parses a statement,
12979 but ensures that is in its own scope, even if it is not a
12980 compound-statement.
12981
12982 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12983 is a (possibly labeled) if statement which is not enclosed in
12984 braces and has an else clause. This is used to implement
12985 -Wparentheses.
12986
12987 CHAIN is a vector of if-else-if conditions. This is used to implement
12988 -Wduplicated-cond.
12989
12990 Returns the new statement. */
12991
12992 static tree
12993 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12994 const token_indent_info &guard_tinfo,
12995 vec<tree> *chain)
12996 {
12997 tree statement;
12998 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12999 location_t body_loc_after_labels = UNKNOWN_LOCATION;
13000 token_indent_info body_tinfo
13001 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13002
13003 if (if_p != NULL)
13004 *if_p = false;
13005
13006 /* Mark if () ; with a special NOP_EXPR. */
13007 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13008 {
13009 cp_lexer_consume_token (parser->lexer);
13010 statement = add_stmt (build_empty_stmt (body_loc));
13011
13012 if (guard_tinfo.keyword == RID_IF
13013 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
13014 warning_at (body_loc, OPT_Wempty_body,
13015 "suggest braces around empty body in an %<if%> statement");
13016 else if (guard_tinfo.keyword == RID_ELSE)
13017 warning_at (body_loc, OPT_Wempty_body,
13018 "suggest braces around empty body in an %<else%> statement");
13019 }
13020 /* if a compound is opened, we simply parse the statement directly. */
13021 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13022 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
13023 /* If the token is not a `{', then we must take special action. */
13024 else
13025 {
13026 /* Create a compound-statement. */
13027 statement = begin_compound_stmt (0);
13028 /* Parse the dependent-statement. */
13029 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
13030 &body_loc_after_labels);
13031 /* Finish the dummy compound-statement. */
13032 finish_compound_stmt (statement);
13033 }
13034
13035 token_indent_info next_tinfo
13036 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13037 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13038
13039 if (body_loc_after_labels != UNKNOWN_LOCATION
13040 && next_tinfo.type != CPP_SEMICOLON)
13041 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
13042 guard_tinfo.location, guard_tinfo.keyword);
13043
13044 /* Return the statement. */
13045 return statement;
13046 }
13047
13048 /* For some dependent statements (like `while (cond) statement'), we
13049 have already created a scope. Therefore, even if the dependent
13050 statement is a compound-statement, we do not want to create another
13051 scope. */
13052
13053 static void
13054 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
13055 const token_indent_info &guard_tinfo)
13056 {
13057 /* If the token is a `{', then we must take special action. */
13058 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13059 {
13060 token_indent_info body_tinfo
13061 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13062 location_t loc_after_labels = UNKNOWN_LOCATION;
13063
13064 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
13065 &loc_after_labels);
13066 token_indent_info next_tinfo
13067 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13068 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13069
13070 if (loc_after_labels != UNKNOWN_LOCATION
13071 && next_tinfo.type != CPP_SEMICOLON)
13072 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13073 guard_tinfo.location,
13074 guard_tinfo.keyword);
13075 }
13076 else
13077 {
13078 /* Avoid calling cp_parser_compound_statement, so that we
13079 don't create a new scope. Do everything else by hand. */
13080 matching_braces braces;
13081 braces.require_open (parser);
13082 /* If the next keyword is `__label__' we have a label declaration. */
13083 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13084 cp_parser_label_declaration (parser);
13085 /* Parse an (optional) statement-seq. */
13086 cp_parser_statement_seq_opt (parser, NULL_TREE);
13087 braces.require_close (parser);
13088 }
13089 }
13090
13091 /* Declarations [gram.dcl.dcl] */
13092
13093 /* Parse an optional declaration-sequence.
13094
13095 declaration-seq:
13096 declaration
13097 declaration-seq declaration */
13098
13099 static void
13100 cp_parser_declaration_seq_opt (cp_parser* parser)
13101 {
13102 while (true)
13103 {
13104 cp_token *token = cp_lexer_peek_token (parser->lexer);
13105
13106 if (token->type == CPP_CLOSE_BRACE
13107 || token->type == CPP_EOF)
13108 break;
13109 else
13110 cp_parser_toplevel_declaration (parser);
13111 }
13112 }
13113
13114 /* Parse a declaration.
13115
13116 declaration:
13117 block-declaration
13118 function-definition
13119 template-declaration
13120 explicit-instantiation
13121 explicit-specialization
13122 linkage-specification
13123 namespace-definition
13124
13125 C++17:
13126 deduction-guide
13127
13128 GNU extension:
13129
13130 declaration:
13131 __extension__ declaration */
13132
13133 static void
13134 cp_parser_declaration (cp_parser* parser)
13135 {
13136 cp_token token1;
13137 cp_token token2;
13138 int saved_pedantic;
13139 void *p;
13140 tree attributes = NULL_TREE;
13141
13142 /* Check for the `__extension__' keyword. */
13143 if (cp_parser_extension_opt (parser, &saved_pedantic))
13144 {
13145 /* Parse the qualified declaration. */
13146 cp_parser_declaration (parser);
13147 /* Restore the PEDANTIC flag. */
13148 pedantic = saved_pedantic;
13149
13150 return;
13151 }
13152
13153 /* Try to figure out what kind of declaration is present. */
13154 token1 = *cp_lexer_peek_token (parser->lexer);
13155
13156 if (token1.type != CPP_EOF)
13157 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13158 else
13159 {
13160 token2.type = CPP_EOF;
13161 token2.keyword = RID_MAX;
13162 }
13163
13164 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13165 p = obstack_alloc (&declarator_obstack, 0);
13166
13167 /* If the next token is `extern' and the following token is a string
13168 literal, then we have a linkage specification. */
13169 if (token1.keyword == RID_EXTERN
13170 && cp_parser_is_pure_string_literal (&token2))
13171 cp_parser_linkage_specification (parser);
13172 /* If the next token is `template', then we have either a template
13173 declaration, an explicit instantiation, or an explicit
13174 specialization. */
13175 else if (token1.keyword == RID_TEMPLATE)
13176 {
13177 /* `template <>' indicates a template specialization. */
13178 if (token2.type == CPP_LESS
13179 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13180 cp_parser_explicit_specialization (parser);
13181 /* `template <' indicates a template declaration. */
13182 else if (token2.type == CPP_LESS)
13183 cp_parser_template_declaration (parser, /*member_p=*/false);
13184 /* Anything else must be an explicit instantiation. */
13185 else
13186 cp_parser_explicit_instantiation (parser);
13187 }
13188 /* If the next token is `export', then we have a template
13189 declaration. */
13190 else if (token1.keyword == RID_EXPORT)
13191 cp_parser_template_declaration (parser, /*member_p=*/false);
13192 /* If the next token is `extern', 'static' or 'inline' and the one
13193 after that is `template', we have a GNU extended explicit
13194 instantiation directive. */
13195 else if (cp_parser_allow_gnu_extensions_p (parser)
13196 && (token1.keyword == RID_EXTERN
13197 || token1.keyword == RID_STATIC
13198 || token1.keyword == RID_INLINE)
13199 && token2.keyword == RID_TEMPLATE)
13200 cp_parser_explicit_instantiation (parser);
13201 /* If the next token is `namespace', check for a named or unnamed
13202 namespace definition. */
13203 else if (token1.keyword == RID_NAMESPACE
13204 && (/* A named namespace definition. */
13205 (token2.type == CPP_NAME
13206 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13207 != CPP_EQ))
13208 || (token2.type == CPP_OPEN_SQUARE
13209 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13210 == CPP_OPEN_SQUARE)
13211 /* An unnamed namespace definition. */
13212 || token2.type == CPP_OPEN_BRACE
13213 || token2.keyword == RID_ATTRIBUTE))
13214 cp_parser_namespace_definition (parser);
13215 /* An inline (associated) namespace definition. */
13216 else if (token1.keyword == RID_INLINE
13217 && token2.keyword == RID_NAMESPACE)
13218 cp_parser_namespace_definition (parser);
13219 /* Objective-C++ declaration/definition. */
13220 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13221 cp_parser_objc_declaration (parser, NULL_TREE);
13222 else if (c_dialect_objc ()
13223 && token1.keyword == RID_ATTRIBUTE
13224 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13225 cp_parser_objc_declaration (parser, attributes);
13226 /* At this point we may have a template declared by a concept
13227 introduction. */
13228 else if (flag_concepts
13229 && cp_parser_template_declaration_after_export (parser,
13230 /*member_p=*/false))
13231 /* We did. */;
13232 else
13233 /* Try to parse a block-declaration, or a function-definition. */
13234 cp_parser_block_declaration (parser, /*statement_p=*/false);
13235
13236 /* Free any declarators allocated. */
13237 obstack_free (&declarator_obstack, p);
13238 }
13239
13240 /* Parse a namespace-scope declaration. */
13241
13242 static void
13243 cp_parser_toplevel_declaration (cp_parser* parser)
13244 {
13245 cp_token *token = cp_lexer_peek_token (parser->lexer);
13246
13247 if (token->type == CPP_PRAGMA)
13248 /* A top-level declaration can consist solely of a #pragma. A
13249 nested declaration cannot, so this is done here and not in
13250 cp_parser_declaration. (A #pragma at block scope is
13251 handled in cp_parser_statement.) */
13252 cp_parser_pragma (parser, pragma_external, NULL);
13253 else if (token->type == CPP_SEMICOLON)
13254 {
13255 /* A declaration consisting of a single semicolon is
13256 invalid. Allow it unless we're being pedantic. */
13257 cp_lexer_consume_token (parser->lexer);
13258 if (!in_system_header_at (input_location))
13259 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13260 }
13261 else
13262 /* Parse the declaration itself. */
13263 cp_parser_declaration (parser);
13264 }
13265
13266 /* Parse a block-declaration.
13267
13268 block-declaration:
13269 simple-declaration
13270 asm-definition
13271 namespace-alias-definition
13272 using-declaration
13273 using-directive
13274
13275 GNU Extension:
13276
13277 block-declaration:
13278 __extension__ block-declaration
13279
13280 C++0x Extension:
13281
13282 block-declaration:
13283 static_assert-declaration
13284
13285 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13286 part of a declaration-statement. */
13287
13288 static void
13289 cp_parser_block_declaration (cp_parser *parser,
13290 bool statement_p)
13291 {
13292 cp_token *token1;
13293 int saved_pedantic;
13294
13295 /* Check for the `__extension__' keyword. */
13296 if (cp_parser_extension_opt (parser, &saved_pedantic))
13297 {
13298 /* Parse the qualified declaration. */
13299 cp_parser_block_declaration (parser, statement_p);
13300 /* Restore the PEDANTIC flag. */
13301 pedantic = saved_pedantic;
13302
13303 return;
13304 }
13305
13306 /* Peek at the next token to figure out which kind of declaration is
13307 present. */
13308 token1 = cp_lexer_peek_token (parser->lexer);
13309
13310 /* If the next keyword is `asm', we have an asm-definition. */
13311 if (token1->keyword == RID_ASM)
13312 {
13313 if (statement_p)
13314 cp_parser_commit_to_tentative_parse (parser);
13315 cp_parser_asm_definition (parser);
13316 }
13317 /* If the next keyword is `namespace', we have a
13318 namespace-alias-definition. */
13319 else if (token1->keyword == RID_NAMESPACE)
13320 cp_parser_namespace_alias_definition (parser);
13321 /* If the next keyword is `using', we have a
13322 using-declaration, a using-directive, or an alias-declaration. */
13323 else if (token1->keyword == RID_USING)
13324 {
13325 cp_token *token2;
13326
13327 if (statement_p)
13328 cp_parser_commit_to_tentative_parse (parser);
13329 /* If the token after `using' is `namespace', then we have a
13330 using-directive. */
13331 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13332 if (token2->keyword == RID_NAMESPACE)
13333 cp_parser_using_directive (parser);
13334 /* If the second token after 'using' is '=', then we have an
13335 alias-declaration. */
13336 else if (cxx_dialect >= cxx11
13337 && token2->type == CPP_NAME
13338 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13339 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13340 cp_parser_alias_declaration (parser);
13341 /* Otherwise, it's a using-declaration. */
13342 else
13343 cp_parser_using_declaration (parser,
13344 /*access_declaration_p=*/false);
13345 }
13346 /* If the next keyword is `__label__' we have a misplaced label
13347 declaration. */
13348 else if (token1->keyword == RID_LABEL)
13349 {
13350 cp_lexer_consume_token (parser->lexer);
13351 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13352 cp_parser_skip_to_end_of_statement (parser);
13353 /* If the next token is now a `;', consume it. */
13354 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13355 cp_lexer_consume_token (parser->lexer);
13356 }
13357 /* If the next token is `static_assert' we have a static assertion. */
13358 else if (token1->keyword == RID_STATIC_ASSERT)
13359 cp_parser_static_assert (parser, /*member_p=*/false);
13360 /* Anything else must be a simple-declaration. */
13361 else
13362 cp_parser_simple_declaration (parser, !statement_p,
13363 /*maybe_range_for_decl*/NULL);
13364 }
13365
13366 /* Parse a simple-declaration.
13367
13368 simple-declaration:
13369 decl-specifier-seq [opt] init-declarator-list [opt] ;
13370 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13371 brace-or-equal-initializer ;
13372
13373 init-declarator-list:
13374 init-declarator
13375 init-declarator-list , init-declarator
13376
13377 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13378 function-definition as a simple-declaration.
13379
13380 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13381 parsed declaration if it is an uninitialized single declarator not followed
13382 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13383 if present, will not be consumed. */
13384
13385 static void
13386 cp_parser_simple_declaration (cp_parser* parser,
13387 bool function_definition_allowed_p,
13388 tree *maybe_range_for_decl)
13389 {
13390 cp_decl_specifier_seq decl_specifiers;
13391 int declares_class_or_enum;
13392 bool saw_declarator;
13393 location_t comma_loc = UNKNOWN_LOCATION;
13394 location_t init_loc = UNKNOWN_LOCATION;
13395
13396 if (maybe_range_for_decl)
13397 *maybe_range_for_decl = NULL_TREE;
13398
13399 /* Defer access checks until we know what is being declared; the
13400 checks for names appearing in the decl-specifier-seq should be
13401 done as if we were in the scope of the thing being declared. */
13402 push_deferring_access_checks (dk_deferred);
13403
13404 /* Parse the decl-specifier-seq. We have to keep track of whether
13405 or not the decl-specifier-seq declares a named class or
13406 enumeration type, since that is the only case in which the
13407 init-declarator-list is allowed to be empty.
13408
13409 [dcl.dcl]
13410
13411 In a simple-declaration, the optional init-declarator-list can be
13412 omitted only when declaring a class or enumeration, that is when
13413 the decl-specifier-seq contains either a class-specifier, an
13414 elaborated-type-specifier, or an enum-specifier. */
13415 cp_parser_decl_specifier_seq (parser,
13416 CP_PARSER_FLAGS_OPTIONAL,
13417 &decl_specifiers,
13418 &declares_class_or_enum);
13419 /* We no longer need to defer access checks. */
13420 stop_deferring_access_checks ();
13421
13422 /* In a block scope, a valid declaration must always have a
13423 decl-specifier-seq. By not trying to parse declarators, we can
13424 resolve the declaration/expression ambiguity more quickly. */
13425 if (!function_definition_allowed_p
13426 && !decl_specifiers.any_specifiers_p)
13427 {
13428 cp_parser_error (parser, "expected declaration");
13429 goto done;
13430 }
13431
13432 /* If the next two tokens are both identifiers, the code is
13433 erroneous. The usual cause of this situation is code like:
13434
13435 T t;
13436
13437 where "T" should name a type -- but does not. */
13438 if (!decl_specifiers.any_type_specifiers_p
13439 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13440 {
13441 /* If parsing tentatively, we should commit; we really are
13442 looking at a declaration. */
13443 cp_parser_commit_to_tentative_parse (parser);
13444 /* Give up. */
13445 goto done;
13446 }
13447
13448 cp_parser_maybe_commit_to_declaration (parser,
13449 decl_specifiers.any_specifiers_p);
13450
13451 /* Look for C++17 decomposition declaration. */
13452 for (size_t n = 1; ; n++)
13453 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13454 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13455 continue;
13456 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13457 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13458 && decl_specifiers.any_specifiers_p)
13459 {
13460 tree decl
13461 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13462 maybe_range_for_decl,
13463 &init_loc);
13464
13465 /* The next token should be either a `,' or a `;'. */
13466 cp_token *token = cp_lexer_peek_token (parser->lexer);
13467 /* If it's a `;', we are done. */
13468 if (token->type == CPP_SEMICOLON)
13469 goto finish;
13470 else if (maybe_range_for_decl)
13471 {
13472 if (*maybe_range_for_decl == NULL_TREE)
13473 *maybe_range_for_decl = error_mark_node;
13474 goto finish;
13475 }
13476 /* Anything else is an error. */
13477 else
13478 {
13479 /* If we have already issued an error message we don't need
13480 to issue another one. */
13481 if ((decl != error_mark_node
13482 && DECL_INITIAL (decl) != error_mark_node)
13483 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13484 cp_parser_error (parser, "expected %<,%> or %<;%>");
13485 /* Skip tokens until we reach the end of the statement. */
13486 cp_parser_skip_to_end_of_statement (parser);
13487 /* If the next token is now a `;', consume it. */
13488 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13489 cp_lexer_consume_token (parser->lexer);
13490 goto done;
13491 }
13492 }
13493 else
13494 break;
13495
13496 tree last_type;
13497 bool auto_specifier_p;
13498 /* NULL_TREE if both variable and function declaration are allowed,
13499 error_mark_node if function declaration are not allowed and
13500 a FUNCTION_DECL that should be diagnosed if it is followed by
13501 variable declarations. */
13502 tree auto_function_declaration;
13503
13504 last_type = NULL_TREE;
13505 auto_specifier_p
13506 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13507 auto_function_declaration = NULL_TREE;
13508
13509 /* Keep going until we hit the `;' at the end of the simple
13510 declaration. */
13511 saw_declarator = false;
13512 while (cp_lexer_next_token_is_not (parser->lexer,
13513 CPP_SEMICOLON))
13514 {
13515 cp_token *token;
13516 bool function_definition_p;
13517 tree decl;
13518 tree auto_result = NULL_TREE;
13519
13520 if (saw_declarator)
13521 {
13522 /* If we are processing next declarator, comma is expected */
13523 token = cp_lexer_peek_token (parser->lexer);
13524 gcc_assert (token->type == CPP_COMMA);
13525 cp_lexer_consume_token (parser->lexer);
13526 if (maybe_range_for_decl)
13527 {
13528 *maybe_range_for_decl = error_mark_node;
13529 if (comma_loc == UNKNOWN_LOCATION)
13530 comma_loc = token->location;
13531 }
13532 }
13533 else
13534 saw_declarator = true;
13535
13536 /* Parse the init-declarator. */
13537 decl = cp_parser_init_declarator (parser,
13538 CP_PARSER_FLAGS_NONE,
13539 &decl_specifiers,
13540 /*checks=*/NULL,
13541 function_definition_allowed_p,
13542 /*member_p=*/false,
13543 declares_class_or_enum,
13544 &function_definition_p,
13545 maybe_range_for_decl,
13546 &init_loc,
13547 &auto_result);
13548 /* If an error occurred while parsing tentatively, exit quickly.
13549 (That usually happens when in the body of a function; each
13550 statement is treated as a declaration-statement until proven
13551 otherwise.) */
13552 if (cp_parser_error_occurred (parser))
13553 goto done;
13554
13555 if (auto_specifier_p && cxx_dialect >= cxx14)
13556 {
13557 /* If the init-declarator-list contains more than one
13558 init-declarator, they shall all form declarations of
13559 variables. */
13560 if (auto_function_declaration == NULL_TREE)
13561 auto_function_declaration
13562 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13563 else if (TREE_CODE (decl) == FUNCTION_DECL
13564 || auto_function_declaration != error_mark_node)
13565 {
13566 error_at (decl_specifiers.locations[ds_type_spec],
13567 "non-variable %qD in declaration with more than one "
13568 "declarator with placeholder type",
13569 TREE_CODE (decl) == FUNCTION_DECL
13570 ? decl : auto_function_declaration);
13571 auto_function_declaration = error_mark_node;
13572 }
13573 }
13574
13575 if (auto_result
13576 && (!processing_template_decl || !type_uses_auto (auto_result)))
13577 {
13578 if (last_type
13579 && last_type != error_mark_node
13580 && !same_type_p (auto_result, last_type))
13581 {
13582 /* If the list of declarators contains more than one declarator,
13583 the type of each declared variable is determined as described
13584 above. If the type deduced for the template parameter U is not
13585 the same in each deduction, the program is ill-formed. */
13586 error_at (decl_specifiers.locations[ds_type_spec],
13587 "inconsistent deduction for %qT: %qT and then %qT",
13588 decl_specifiers.type, last_type, auto_result);
13589 last_type = error_mark_node;
13590 }
13591 else
13592 last_type = auto_result;
13593 }
13594
13595 /* Handle function definitions specially. */
13596 if (function_definition_p)
13597 {
13598 /* If the next token is a `,', then we are probably
13599 processing something like:
13600
13601 void f() {}, *p;
13602
13603 which is erroneous. */
13604 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13605 {
13606 cp_token *token = cp_lexer_peek_token (parser->lexer);
13607 error_at (token->location,
13608 "mixing"
13609 " declarations and function-definitions is forbidden");
13610 }
13611 /* Otherwise, we're done with the list of declarators. */
13612 else
13613 {
13614 pop_deferring_access_checks ();
13615 return;
13616 }
13617 }
13618 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13619 *maybe_range_for_decl = decl;
13620 /* The next token should be either a `,' or a `;'. */
13621 token = cp_lexer_peek_token (parser->lexer);
13622 /* If it's a `,', there are more declarators to come. */
13623 if (token->type == CPP_COMMA)
13624 /* will be consumed next time around */;
13625 /* If it's a `;', we are done. */
13626 else if (token->type == CPP_SEMICOLON)
13627 break;
13628 else if (maybe_range_for_decl)
13629 {
13630 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13631 permerror (decl_specifiers.locations[ds_type_spec],
13632 "types may not be defined in a for-range-declaration");
13633 break;
13634 }
13635 /* Anything else is an error. */
13636 else
13637 {
13638 /* If we have already issued an error message we don't need
13639 to issue another one. */
13640 if ((decl != error_mark_node
13641 && DECL_INITIAL (decl) != error_mark_node)
13642 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13643 cp_parser_error (parser, "expected %<,%> or %<;%>");
13644 /* Skip tokens until we reach the end of the statement. */
13645 cp_parser_skip_to_end_of_statement (parser);
13646 /* If the next token is now a `;', consume it. */
13647 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13648 cp_lexer_consume_token (parser->lexer);
13649 goto done;
13650 }
13651 /* After the first time around, a function-definition is not
13652 allowed -- even if it was OK at first. For example:
13653
13654 int i, f() {}
13655
13656 is not valid. */
13657 function_definition_allowed_p = false;
13658 }
13659
13660 /* Issue an error message if no declarators are present, and the
13661 decl-specifier-seq does not itself declare a class or
13662 enumeration: [dcl.dcl]/3. */
13663 if (!saw_declarator)
13664 {
13665 if (cp_parser_declares_only_class_p (parser))
13666 {
13667 if (!declares_class_or_enum
13668 && decl_specifiers.type
13669 && OVERLOAD_TYPE_P (decl_specifiers.type))
13670 /* Ensure an error is issued anyway when finish_decltype_type,
13671 called via cp_parser_decl_specifier_seq, returns a class or
13672 an enumeration (c++/51786). */
13673 decl_specifiers.type = NULL_TREE;
13674 shadow_tag (&decl_specifiers);
13675 }
13676 /* Perform any deferred access checks. */
13677 perform_deferred_access_checks (tf_warning_or_error);
13678 }
13679
13680 /* Consume the `;'. */
13681 finish:
13682 if (!maybe_range_for_decl)
13683 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13684 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13685 {
13686 if (init_loc != UNKNOWN_LOCATION)
13687 error_at (init_loc, "initializer in range-based %<for%> loop");
13688 if (comma_loc != UNKNOWN_LOCATION)
13689 error_at (comma_loc,
13690 "multiple declarations in range-based %<for%> loop");
13691 }
13692
13693 done:
13694 pop_deferring_access_checks ();
13695 }
13696
13697 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13698 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13699 initializer ; */
13700
13701 static tree
13702 cp_parser_decomposition_declaration (cp_parser *parser,
13703 cp_decl_specifier_seq *decl_specifiers,
13704 tree *maybe_range_for_decl,
13705 location_t *init_loc)
13706 {
13707 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13708 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13709 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13710
13711 /* Parse the identifier-list. */
13712 auto_vec<cp_expr, 10> v;
13713 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13714 while (true)
13715 {
13716 cp_expr e = cp_parser_identifier (parser);
13717 if (e.get_value () == error_mark_node)
13718 break;
13719 v.safe_push (e);
13720 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13721 break;
13722 cp_lexer_consume_token (parser->lexer);
13723 }
13724
13725 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13726 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13727 {
13728 end_loc = UNKNOWN_LOCATION;
13729 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13730 false);
13731 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13732 cp_lexer_consume_token (parser->lexer);
13733 else
13734 {
13735 cp_parser_skip_to_end_of_statement (parser);
13736 return error_mark_node;
13737 }
13738 }
13739
13740 if (cxx_dialect < cxx17)
13741 pedwarn (loc, 0, "structured bindings only available with "
13742 "%<-std=c++17%> or %<-std=gnu++17%>");
13743
13744 tree pushed_scope;
13745 cp_declarator *declarator = make_declarator (cdk_decomp);
13746 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13747 declarator->id_loc = loc;
13748 if (ref_qual != REF_QUAL_NONE)
13749 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13750 ref_qual == REF_QUAL_RVALUE,
13751 NULL_TREE);
13752 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13753 NULL_TREE, decl_specifiers->attributes,
13754 &pushed_scope);
13755 tree orig_decl = decl;
13756
13757 unsigned int i;
13758 cp_expr e;
13759 cp_decl_specifier_seq decl_specs;
13760 clear_decl_specs (&decl_specs);
13761 decl_specs.type = make_auto ();
13762 tree prev = decl;
13763 FOR_EACH_VEC_ELT (v, i, e)
13764 {
13765 if (i == 0)
13766 declarator = make_id_declarator (NULL_TREE, e.get_value (),
13767 sfk_none, e.get_location ());
13768 else
13769 {
13770 declarator->u.id.unqualified_name = e.get_value ();
13771 declarator->id_loc = e.get_location ();
13772 }
13773 tree elt_pushed_scope;
13774 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13775 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13776 if (decl2 == error_mark_node)
13777 decl = error_mark_node;
13778 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13779 {
13780 /* Ensure we've diagnosed redeclaration if we aren't creating
13781 a new VAR_DECL. */
13782 gcc_assert (errorcount);
13783 decl = error_mark_node;
13784 }
13785 else
13786 prev = decl2;
13787 if (elt_pushed_scope)
13788 pop_scope (elt_pushed_scope);
13789 }
13790
13791 if (v.is_empty ())
13792 {
13793 error_at (loc, "empty structured binding declaration");
13794 decl = error_mark_node;
13795 }
13796
13797 if (maybe_range_for_decl == NULL
13798 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13799 {
13800 bool non_constant_p = false, is_direct_init = false;
13801 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13802 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13803 &non_constant_p);
13804 if (initializer == NULL_TREE
13805 || (TREE_CODE (initializer) == TREE_LIST
13806 && TREE_CHAIN (initializer))
13807 || (is_direct_init
13808 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13809 && CONSTRUCTOR_NELTS (initializer) != 1))
13810 {
13811 error_at (loc, "invalid initializer for structured binding "
13812 "declaration");
13813 initializer = error_mark_node;
13814 }
13815
13816 if (decl != error_mark_node)
13817 {
13818 cp_maybe_mangle_decomp (decl, prev, v.length ());
13819 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13820 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13821 cp_finish_decomp (decl, prev, v.length ());
13822 }
13823 }
13824 else if (decl != error_mark_node)
13825 {
13826 *maybe_range_for_decl = prev;
13827 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13828 the underlying DECL. */
13829 cp_finish_decomp (decl, prev, v.length ());
13830 }
13831
13832 if (pushed_scope)
13833 pop_scope (pushed_scope);
13834
13835 if (decl == error_mark_node && DECL_P (orig_decl))
13836 {
13837 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13838 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13839 }
13840
13841 return decl;
13842 }
13843
13844 /* Parse a decl-specifier-seq.
13845
13846 decl-specifier-seq:
13847 decl-specifier-seq [opt] decl-specifier
13848 decl-specifier attribute-specifier-seq [opt] (C++11)
13849
13850 decl-specifier:
13851 storage-class-specifier
13852 type-specifier
13853 function-specifier
13854 friend
13855 typedef
13856
13857 GNU Extension:
13858
13859 decl-specifier:
13860 attributes
13861
13862 Concepts Extension:
13863
13864 decl-specifier:
13865 concept
13866
13867 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13868
13869 The parser flags FLAGS is used to control type-specifier parsing.
13870
13871 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13872 flags:
13873
13874 1: one of the decl-specifiers is an elaborated-type-specifier
13875 (i.e., a type declaration)
13876 2: one of the decl-specifiers is an enum-specifier or a
13877 class-specifier (i.e., a type definition)
13878
13879 */
13880
13881 static void
13882 cp_parser_decl_specifier_seq (cp_parser* parser,
13883 cp_parser_flags flags,
13884 cp_decl_specifier_seq *decl_specs,
13885 int* declares_class_or_enum)
13886 {
13887 bool constructor_possible_p = !parser->in_declarator_p;
13888 bool found_decl_spec = false;
13889 cp_token *start_token = NULL;
13890 cp_decl_spec ds;
13891
13892 /* Clear DECL_SPECS. */
13893 clear_decl_specs (decl_specs);
13894
13895 /* Assume no class or enumeration type is declared. */
13896 *declares_class_or_enum = 0;
13897
13898 /* Keep reading specifiers until there are no more to read. */
13899 while (true)
13900 {
13901 bool constructor_p;
13902 cp_token *token;
13903 ds = ds_last;
13904
13905 /* Peek at the next token. */
13906 token = cp_lexer_peek_token (parser->lexer);
13907
13908 /* Save the first token of the decl spec list for error
13909 reporting. */
13910 if (!start_token)
13911 start_token = token;
13912 /* Handle attributes. */
13913 if (cp_next_tokens_can_be_attribute_p (parser))
13914 {
13915 /* Parse the attributes. */
13916 tree attrs = cp_parser_attributes_opt (parser);
13917
13918 /* In a sequence of declaration specifiers, c++11 attributes
13919 appertain to the type that precede them. In that case
13920 [dcl.spec]/1 says:
13921
13922 The attribute-specifier-seq affects the type only for
13923 the declaration it appears in, not other declarations
13924 involving the same type.
13925
13926 But for now let's force the user to position the
13927 attribute either at the beginning of the declaration or
13928 after the declarator-id, which would clearly mean that it
13929 applies to the declarator. */
13930 if (cxx11_attribute_p (attrs))
13931 {
13932 if (!found_decl_spec)
13933 /* The c++11 attribute is at the beginning of the
13934 declaration. It appertains to the entity being
13935 declared. */;
13936 else
13937 {
13938 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13939 {
13940 /* This is an attribute following a
13941 class-specifier. */
13942 if (decl_specs->type_definition_p)
13943 warn_misplaced_attr_for_class_type (token->location,
13944 decl_specs->type);
13945 attrs = NULL_TREE;
13946 }
13947 else
13948 {
13949 decl_specs->std_attributes
13950 = attr_chainon (decl_specs->std_attributes, attrs);
13951 if (decl_specs->locations[ds_std_attribute] == 0)
13952 decl_specs->locations[ds_std_attribute] = token->location;
13953 }
13954 continue;
13955 }
13956 }
13957
13958 decl_specs->attributes
13959 = attr_chainon (decl_specs->attributes, attrs);
13960 if (decl_specs->locations[ds_attribute] == 0)
13961 decl_specs->locations[ds_attribute] = token->location;
13962 continue;
13963 }
13964 /* Assume we will find a decl-specifier keyword. */
13965 found_decl_spec = true;
13966 /* If the next token is an appropriate keyword, we can simply
13967 add it to the list. */
13968 switch (token->keyword)
13969 {
13970 /* decl-specifier:
13971 friend
13972 constexpr */
13973 case RID_FRIEND:
13974 if (!at_class_scope_p ())
13975 {
13976 gcc_rich_location richloc (token->location);
13977 richloc.add_fixit_remove ();
13978 error_at (&richloc, "%<friend%> used outside of class");
13979 cp_lexer_purge_token (parser->lexer);
13980 }
13981 else
13982 {
13983 ds = ds_friend;
13984 /* Consume the token. */
13985 cp_lexer_consume_token (parser->lexer);
13986 }
13987 break;
13988
13989 case RID_CONSTEXPR:
13990 ds = ds_constexpr;
13991 cp_lexer_consume_token (parser->lexer);
13992 break;
13993
13994 case RID_CONCEPT:
13995 ds = ds_concept;
13996 cp_lexer_consume_token (parser->lexer);
13997 /* In C++20 a concept definition is just 'concept name = expr;'
13998 Support that syntax by pretending we've seen 'bool'. */
13999 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14000 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
14001 {
14002 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
14003 token, /*type_definition*/false);
14004 decl_specs->any_type_specifiers_p = true;
14005 }
14006 break;
14007
14008 /* function-specifier:
14009 inline
14010 virtual
14011 explicit */
14012 case RID_INLINE:
14013 case RID_VIRTUAL:
14014 case RID_EXPLICIT:
14015 cp_parser_function_specifier_opt (parser, decl_specs);
14016 break;
14017
14018 /* decl-specifier:
14019 typedef */
14020 case RID_TYPEDEF:
14021 ds = ds_typedef;
14022 /* Consume the token. */
14023 cp_lexer_consume_token (parser->lexer);
14024 /* A constructor declarator cannot appear in a typedef. */
14025 constructor_possible_p = false;
14026 /* The "typedef" keyword can only occur in a declaration; we
14027 may as well commit at this point. */
14028 cp_parser_commit_to_tentative_parse (parser);
14029
14030 if (decl_specs->storage_class != sc_none)
14031 decl_specs->conflicting_specifiers_p = true;
14032 break;
14033
14034 /* storage-class-specifier:
14035 auto
14036 register
14037 static
14038 extern
14039 mutable
14040
14041 GNU Extension:
14042 thread */
14043 case RID_AUTO:
14044 if (cxx_dialect == cxx98)
14045 {
14046 /* Consume the token. */
14047 cp_lexer_consume_token (parser->lexer);
14048
14049 /* Complain about `auto' as a storage specifier, if
14050 we're complaining about C++0x compatibility. */
14051 gcc_rich_location richloc (token->location);
14052 richloc.add_fixit_remove ();
14053 warning_at (&richloc, OPT_Wc__11_compat,
14054 "%<auto%> changes meaning in C++11; "
14055 "please remove it");
14056
14057 /* Set the storage class anyway. */
14058 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
14059 token);
14060 }
14061 else
14062 /* C++0x auto type-specifier. */
14063 found_decl_spec = false;
14064 break;
14065
14066 case RID_REGISTER:
14067 case RID_STATIC:
14068 case RID_EXTERN:
14069 case RID_MUTABLE:
14070 /* Consume the token. */
14071 cp_lexer_consume_token (parser->lexer);
14072 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
14073 token);
14074 break;
14075 case RID_THREAD:
14076 /* Consume the token. */
14077 ds = ds_thread;
14078 cp_lexer_consume_token (parser->lexer);
14079 break;
14080
14081 default:
14082 /* We did not yet find a decl-specifier yet. */
14083 found_decl_spec = false;
14084 break;
14085 }
14086
14087 if (found_decl_spec
14088 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14089 && token->keyword != RID_CONSTEXPR)
14090 error ("decl-specifier invalid in condition");
14091
14092 if (found_decl_spec
14093 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14094 && token->keyword != RID_MUTABLE
14095 && token->keyword != RID_CONSTEXPR)
14096 error_at (token->location, "%qD invalid in lambda",
14097 ridpointers[token->keyword]);
14098
14099 if (ds != ds_last)
14100 set_and_check_decl_spec_loc (decl_specs, ds, token);
14101
14102 /* Constructors are a special case. The `S' in `S()' is not a
14103 decl-specifier; it is the beginning of the declarator. */
14104 constructor_p
14105 = (!found_decl_spec
14106 && constructor_possible_p
14107 && (cp_parser_constructor_declarator_p
14108 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
14109 ds_friend))));
14110
14111 /* If we don't have a DECL_SPEC yet, then we must be looking at
14112 a type-specifier. */
14113 if (!found_decl_spec && !constructor_p)
14114 {
14115 int decl_spec_declares_class_or_enum;
14116 bool is_cv_qualifier;
14117 tree type_spec;
14118
14119 type_spec
14120 = cp_parser_type_specifier (parser, flags,
14121 decl_specs,
14122 /*is_declaration=*/true,
14123 &decl_spec_declares_class_or_enum,
14124 &is_cv_qualifier);
14125 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14126
14127 /* If this type-specifier referenced a user-defined type
14128 (a typedef, class-name, etc.), then we can't allow any
14129 more such type-specifiers henceforth.
14130
14131 [dcl.spec]
14132
14133 The longest sequence of decl-specifiers that could
14134 possibly be a type name is taken as the
14135 decl-specifier-seq of a declaration. The sequence shall
14136 be self-consistent as described below.
14137
14138 [dcl.type]
14139
14140 As a general rule, at most one type-specifier is allowed
14141 in the complete decl-specifier-seq of a declaration. The
14142 only exceptions are the following:
14143
14144 -- const or volatile can be combined with any other
14145 type-specifier.
14146
14147 -- signed or unsigned can be combined with char, long,
14148 short, or int.
14149
14150 -- ..
14151
14152 Example:
14153
14154 typedef char* Pc;
14155 void g (const int Pc);
14156
14157 Here, Pc is *not* part of the decl-specifier seq; it's
14158 the declarator. Therefore, once we see a type-specifier
14159 (other than a cv-qualifier), we forbid any additional
14160 user-defined types. We *do* still allow things like `int
14161 int' to be considered a decl-specifier-seq, and issue the
14162 error message later. */
14163 if (type_spec && !is_cv_qualifier)
14164 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14165 /* A constructor declarator cannot follow a type-specifier. */
14166 if (type_spec)
14167 {
14168 constructor_possible_p = false;
14169 found_decl_spec = true;
14170 if (!is_cv_qualifier)
14171 decl_specs->any_type_specifiers_p = true;
14172
14173 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14174 error_at (token->location, "type-specifier invalid in lambda");
14175 }
14176 }
14177
14178 /* If we still do not have a DECL_SPEC, then there are no more
14179 decl-specifiers. */
14180 if (!found_decl_spec)
14181 break;
14182
14183 decl_specs->any_specifiers_p = true;
14184 /* After we see one decl-specifier, further decl-specifiers are
14185 always optional. */
14186 flags |= CP_PARSER_FLAGS_OPTIONAL;
14187 }
14188
14189 /* Don't allow a friend specifier with a class definition. */
14190 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14191 && (*declares_class_or_enum & 2))
14192 error_at (decl_specs->locations[ds_friend],
14193 "class definition may not be declared a friend");
14194 }
14195
14196 /* Parse an (optional) storage-class-specifier.
14197
14198 storage-class-specifier:
14199 auto
14200 register
14201 static
14202 extern
14203 mutable
14204
14205 GNU Extension:
14206
14207 storage-class-specifier:
14208 thread
14209
14210 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14211
14212 static tree
14213 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14214 {
14215 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14216 {
14217 case RID_AUTO:
14218 if (cxx_dialect != cxx98)
14219 return NULL_TREE;
14220 /* Fall through for C++98. */
14221 gcc_fallthrough ();
14222
14223 case RID_REGISTER:
14224 case RID_STATIC:
14225 case RID_EXTERN:
14226 case RID_MUTABLE:
14227 case RID_THREAD:
14228 /* Consume the token. */
14229 return cp_lexer_consume_token (parser->lexer)->u.value;
14230
14231 default:
14232 return NULL_TREE;
14233 }
14234 }
14235
14236 /* Parse an (optional) function-specifier.
14237
14238 function-specifier:
14239 inline
14240 virtual
14241 explicit
14242
14243 C++2A Extension:
14244 explicit(constant-expression)
14245
14246 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14247 Updates DECL_SPECS, if it is non-NULL. */
14248
14249 static tree
14250 cp_parser_function_specifier_opt (cp_parser* parser,
14251 cp_decl_specifier_seq *decl_specs)
14252 {
14253 cp_token *token = cp_lexer_peek_token (parser->lexer);
14254 switch (token->keyword)
14255 {
14256 case RID_INLINE:
14257 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14258 break;
14259
14260 case RID_VIRTUAL:
14261 /* 14.5.2.3 [temp.mem]
14262
14263 A member function template shall not be virtual. */
14264 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14265 && current_class_type)
14266 error_at (token->location, "templates may not be %<virtual%>");
14267 else
14268 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14269 break;
14270
14271 case RID_EXPLICIT:
14272 {
14273 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14274 /* If we see '(', it's C++20 explicit(bool). */
14275 tree expr;
14276 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14277 {
14278 matching_parens parens;
14279 parens.consume_open (parser);
14280
14281 /* New types are not allowed in an explicit-specifier. */
14282 const char *saved_message
14283 = parser->type_definition_forbidden_message;
14284 parser->type_definition_forbidden_message
14285 = G_("types may not be defined in explicit-specifier");
14286
14287 if (cxx_dialect < cxx2a)
14288 pedwarn (token->location, 0,
14289 "%<explicit(bool)%> only available with %<-std=c++2a%> "
14290 "or %<-std=gnu++2a%>");
14291
14292 /* Parse the constant-expression. */
14293 expr = cp_parser_constant_expression (parser);
14294
14295 /* Restore the saved message. */
14296 parser->type_definition_forbidden_message = saved_message;
14297 parens.require_close (parser);
14298 }
14299 else
14300 /* The explicit-specifier explicit without a constant-expression is
14301 equivalent to the explicit-specifier explicit(true). */
14302 expr = boolean_true_node;
14303
14304 /* [dcl.fct.spec]
14305 "the constant-expression, if supplied, shall be a contextually
14306 converted constant expression of type bool." */
14307 expr = build_explicit_specifier (expr, tf_warning_or_error);
14308 /* We could evaluate it -- mark the decl as appropriate. */
14309 if (expr == boolean_true_node)
14310 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14311 else if (expr == boolean_false_node)
14312 /* Don't mark the decl as explicit. */;
14313 else if (decl_specs)
14314 /* The expression was value-dependent. Remember it so that we can
14315 substitute it later. */
14316 decl_specs->explicit_specifier = expr;
14317 return id;
14318 }
14319
14320 default:
14321 return NULL_TREE;
14322 }
14323
14324 /* Consume the token. */
14325 return cp_lexer_consume_token (parser->lexer)->u.value;
14326 }
14327
14328 /* Parse a linkage-specification.
14329
14330 linkage-specification:
14331 extern string-literal { declaration-seq [opt] }
14332 extern string-literal declaration */
14333
14334 static void
14335 cp_parser_linkage_specification (cp_parser* parser)
14336 {
14337 tree linkage;
14338
14339 /* Look for the `extern' keyword. */
14340 cp_token *extern_token
14341 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14342
14343 /* Look for the string-literal. */
14344 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14345 linkage = cp_parser_string_literal (parser, false, false);
14346
14347 /* Transform the literal into an identifier. If the literal is a
14348 wide-character string, or contains embedded NULs, then we can't
14349 handle it as the user wants. */
14350 if (strlen (TREE_STRING_POINTER (linkage))
14351 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14352 {
14353 cp_parser_error (parser, "invalid linkage-specification");
14354 /* Assume C++ linkage. */
14355 linkage = lang_name_cplusplus;
14356 }
14357 else
14358 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14359
14360 /* We're now using the new linkage. */
14361 push_lang_context (linkage);
14362
14363 /* Preserve the location of the the innermost linkage specification,
14364 tracking the locations of nested specifications via a local. */
14365 location_t saved_location
14366 = parser->innermost_linkage_specification_location;
14367 /* Construct a location ranging from the start of the "extern" to
14368 the end of the string-literal, with the caret at the start, e.g.:
14369 extern "C" {
14370 ^~~~~~~~~~
14371 */
14372 parser->innermost_linkage_specification_location
14373 = make_location (extern_token->location,
14374 extern_token->location,
14375 get_finish (string_token->location));
14376
14377 /* If the next token is a `{', then we're using the first
14378 production. */
14379 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14380 {
14381 cp_ensure_no_omp_declare_simd (parser);
14382 cp_ensure_no_oacc_routine (parser);
14383
14384 /* Consume the `{' token. */
14385 matching_braces braces;
14386 braces.consume_open (parser);
14387 /* Parse the declarations. */
14388 cp_parser_declaration_seq_opt (parser);
14389 /* Look for the closing `}'. */
14390 braces.require_close (parser);
14391 }
14392 /* Otherwise, there's just one declaration. */
14393 else
14394 {
14395 bool saved_in_unbraced_linkage_specification_p;
14396
14397 saved_in_unbraced_linkage_specification_p
14398 = parser->in_unbraced_linkage_specification_p;
14399 parser->in_unbraced_linkage_specification_p = true;
14400 cp_parser_declaration (parser);
14401 parser->in_unbraced_linkage_specification_p
14402 = saved_in_unbraced_linkage_specification_p;
14403 }
14404
14405 /* We're done with the linkage-specification. */
14406 pop_lang_context ();
14407
14408 /* Restore location of parent linkage specification, if any. */
14409 parser->innermost_linkage_specification_location = saved_location;
14410 }
14411
14412 /* Parse a static_assert-declaration.
14413
14414 static_assert-declaration:
14415 static_assert ( constant-expression , string-literal ) ;
14416 static_assert ( constant-expression ) ; (C++17)
14417
14418 If MEMBER_P, this static_assert is a class member. */
14419
14420 static void
14421 cp_parser_static_assert(cp_parser *parser, bool member_p)
14422 {
14423 cp_expr condition;
14424 location_t token_loc;
14425 tree message;
14426 bool dummy;
14427
14428 /* Peek at the `static_assert' token so we can keep track of exactly
14429 where the static assertion started. */
14430 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14431
14432 /* Look for the `static_assert' keyword. */
14433 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14434 RT_STATIC_ASSERT))
14435 return;
14436
14437 /* We know we are in a static assertion; commit to any tentative
14438 parse. */
14439 if (cp_parser_parsing_tentatively (parser))
14440 cp_parser_commit_to_tentative_parse (parser);
14441
14442 /* Parse the `(' starting the static assertion condition. */
14443 matching_parens parens;
14444 parens.require_open (parser);
14445
14446 /* Parse the constant-expression. Allow a non-constant expression
14447 here in order to give better diagnostics in finish_static_assert. */
14448 condition =
14449 cp_parser_constant_expression (parser,
14450 /*allow_non_constant_p=*/true,
14451 /*non_constant_p=*/&dummy);
14452
14453 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14454 {
14455 if (cxx_dialect < cxx17)
14456 pedwarn (input_location, OPT_Wpedantic,
14457 "static_assert without a message "
14458 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14459 /* Eat the ')' */
14460 cp_lexer_consume_token (parser->lexer);
14461 message = build_string (1, "");
14462 TREE_TYPE (message) = char_array_type_node;
14463 fix_string_type (message);
14464 }
14465 else
14466 {
14467 /* Parse the separating `,'. */
14468 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14469
14470 /* Parse the string-literal message. */
14471 message = cp_parser_string_literal (parser,
14472 /*translate=*/false,
14473 /*wide_ok=*/true);
14474
14475 /* A `)' completes the static assertion. */
14476 if (!parens.require_close (parser))
14477 cp_parser_skip_to_closing_parenthesis (parser,
14478 /*recovering=*/true,
14479 /*or_comma=*/false,
14480 /*consume_paren=*/true);
14481 }
14482
14483 /* A semicolon terminates the declaration. */
14484 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14485
14486 /* Get the location for the static assertion. Use that of the
14487 condition if available, otherwise, use that of the "static_assert"
14488 token. */
14489 location_t assert_loc = condition.get_location ();
14490 if (assert_loc == UNKNOWN_LOCATION)
14491 assert_loc = token_loc;
14492
14493 /* Complete the static assertion, which may mean either processing
14494 the static assert now or saving it for template instantiation. */
14495 finish_static_assert (condition, message, assert_loc, member_p);
14496 }
14497
14498 /* Parse the expression in decltype ( expression ). */
14499
14500 static tree
14501 cp_parser_decltype_expr (cp_parser *parser,
14502 bool &id_expression_or_member_access_p)
14503 {
14504 cp_token *id_expr_start_token;
14505 tree expr;
14506
14507 /* Since we're going to preserve any side-effects from this parse, set up a
14508 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14509 in the expression. */
14510 tentative_firewall firewall (parser);
14511
14512 /* First, try parsing an id-expression. */
14513 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14514 cp_parser_parse_tentatively (parser);
14515 expr = cp_parser_id_expression (parser,
14516 /*template_keyword_p=*/false,
14517 /*check_dependency_p=*/true,
14518 /*template_p=*/NULL,
14519 /*declarator_p=*/false,
14520 /*optional_p=*/false);
14521
14522 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14523 {
14524 bool non_integral_constant_expression_p = false;
14525 tree id_expression = expr;
14526 cp_id_kind idk;
14527 const char *error_msg;
14528
14529 if (identifier_p (expr))
14530 /* Lookup the name we got back from the id-expression. */
14531 expr = cp_parser_lookup_name_simple (parser, expr,
14532 id_expr_start_token->location);
14533
14534 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14535 /* A template without args is not a complete id-expression. */
14536 expr = error_mark_node;
14537
14538 if (expr
14539 && expr != error_mark_node
14540 && TREE_CODE (expr) != TYPE_DECL
14541 && (TREE_CODE (expr) != BIT_NOT_EXPR
14542 || !TYPE_P (TREE_OPERAND (expr, 0)))
14543 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14544 {
14545 /* Complete lookup of the id-expression. */
14546 expr = (finish_id_expression
14547 (id_expression, expr, parser->scope, &idk,
14548 /*integral_constant_expression_p=*/false,
14549 /*allow_non_integral_constant_expression_p=*/true,
14550 &non_integral_constant_expression_p,
14551 /*template_p=*/false,
14552 /*done=*/true,
14553 /*address_p=*/false,
14554 /*template_arg_p=*/false,
14555 &error_msg,
14556 id_expr_start_token->location));
14557
14558 if (expr == error_mark_node)
14559 /* We found an id-expression, but it was something that we
14560 should not have found. This is an error, not something
14561 we can recover from, so note that we found an
14562 id-expression and we'll recover as gracefully as
14563 possible. */
14564 id_expression_or_member_access_p = true;
14565 }
14566
14567 if (expr
14568 && expr != error_mark_node
14569 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14570 /* We have an id-expression. */
14571 id_expression_or_member_access_p = true;
14572 }
14573
14574 if (!id_expression_or_member_access_p)
14575 {
14576 /* Abort the id-expression parse. */
14577 cp_parser_abort_tentative_parse (parser);
14578
14579 /* Parsing tentatively, again. */
14580 cp_parser_parse_tentatively (parser);
14581
14582 /* Parse a class member access. */
14583 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14584 /*cast_p=*/false, /*decltype*/true,
14585 /*member_access_only_p=*/true, NULL);
14586
14587 if (expr
14588 && expr != error_mark_node
14589 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14590 /* We have an id-expression. */
14591 id_expression_or_member_access_p = true;
14592 }
14593
14594 if (id_expression_or_member_access_p)
14595 /* We have parsed the complete id-expression or member access. */
14596 cp_parser_parse_definitely (parser);
14597 else
14598 {
14599 /* Abort our attempt to parse an id-expression or member access
14600 expression. */
14601 cp_parser_abort_tentative_parse (parser);
14602
14603 /* Commit to the tentative_firewall so we get syntax errors. */
14604 cp_parser_commit_to_tentative_parse (parser);
14605
14606 /* Parse a full expression. */
14607 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14608 /*decltype_p=*/true);
14609 }
14610
14611 return expr;
14612 }
14613
14614 /* Parse a `decltype' type. Returns the type.
14615
14616 simple-type-specifier:
14617 decltype ( expression )
14618 C++14 proposal:
14619 decltype ( auto ) */
14620
14621 static tree
14622 cp_parser_decltype (cp_parser *parser)
14623 {
14624 bool id_expression_or_member_access_p = false;
14625 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14626
14627 if (start_token->type == CPP_DECLTYPE)
14628 {
14629 /* Already parsed. */
14630 cp_lexer_consume_token (parser->lexer);
14631 return saved_checks_value (start_token->u.tree_check_value);
14632 }
14633
14634 /* Look for the `decltype' token. */
14635 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14636 return error_mark_node;
14637
14638 /* Parse the opening `('. */
14639 matching_parens parens;
14640 if (!parens.require_open (parser))
14641 return error_mark_node;
14642
14643 push_deferring_access_checks (dk_deferred);
14644
14645 tree expr = NULL_TREE;
14646
14647 if (cxx_dialect >= cxx14
14648 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14649 /* decltype (auto) */
14650 cp_lexer_consume_token (parser->lexer);
14651 else
14652 {
14653 /* decltype (expression) */
14654
14655 /* Types cannot be defined in a `decltype' expression. Save away the
14656 old message and set the new one. */
14657 const char *saved_message = parser->type_definition_forbidden_message;
14658 parser->type_definition_forbidden_message
14659 = G_("types may not be defined in %<decltype%> expressions");
14660
14661 /* The restrictions on constant-expressions do not apply inside
14662 decltype expressions. */
14663 bool saved_integral_constant_expression_p
14664 = parser->integral_constant_expression_p;
14665 bool saved_non_integral_constant_expression_p
14666 = parser->non_integral_constant_expression_p;
14667 parser->integral_constant_expression_p = false;
14668
14669 /* Within a parenthesized expression, a `>' token is always
14670 the greater-than operator. */
14671 bool saved_greater_than_is_operator_p
14672 = parser->greater_than_is_operator_p;
14673 parser->greater_than_is_operator_p = true;
14674
14675 /* Do not actually evaluate the expression. */
14676 ++cp_unevaluated_operand;
14677
14678 /* Do not warn about problems with the expression. */
14679 ++c_inhibit_evaluation_warnings;
14680
14681 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14682 STRIP_ANY_LOCATION_WRAPPER (expr);
14683
14684 /* Go back to evaluating expressions. */
14685 --cp_unevaluated_operand;
14686 --c_inhibit_evaluation_warnings;
14687
14688 /* The `>' token might be the end of a template-id or
14689 template-parameter-list now. */
14690 parser->greater_than_is_operator_p
14691 = saved_greater_than_is_operator_p;
14692
14693 /* Restore the old message and the integral constant expression
14694 flags. */
14695 parser->type_definition_forbidden_message = saved_message;
14696 parser->integral_constant_expression_p
14697 = saved_integral_constant_expression_p;
14698 parser->non_integral_constant_expression_p
14699 = saved_non_integral_constant_expression_p;
14700 }
14701
14702 /* Parse to the closing `)'. */
14703 if (!parens.require_close (parser))
14704 {
14705 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14706 /*consume_paren=*/true);
14707 pop_deferring_access_checks ();
14708 return error_mark_node;
14709 }
14710
14711 if (!expr)
14712 {
14713 /* Build auto. */
14714 expr = make_decltype_auto ();
14715 AUTO_IS_DECLTYPE (expr) = true;
14716 }
14717 else
14718 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14719 tf_warning_or_error);
14720
14721 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14722 it again. */
14723 start_token->type = CPP_DECLTYPE;
14724 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14725 start_token->u.tree_check_value->value = expr;
14726 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14727 start_token->keyword = RID_MAX;
14728 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14729
14730 pop_to_parent_deferring_access_checks ();
14731
14732 return expr;
14733 }
14734
14735 /* Special member functions [gram.special] */
14736
14737 /* Parse a conversion-function-id.
14738
14739 conversion-function-id:
14740 operator conversion-type-id
14741
14742 Returns an IDENTIFIER_NODE representing the operator. */
14743
14744 static tree
14745 cp_parser_conversion_function_id (cp_parser* parser)
14746 {
14747 tree type;
14748 tree saved_scope;
14749 tree saved_qualifying_scope;
14750 tree saved_object_scope;
14751 tree pushed_scope = NULL_TREE;
14752
14753 /* Look for the `operator' token. */
14754 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14755 return error_mark_node;
14756 /* When we parse the conversion-type-id, the current scope will be
14757 reset. However, we need that information in able to look up the
14758 conversion function later, so we save it here. */
14759 saved_scope = parser->scope;
14760 saved_qualifying_scope = parser->qualifying_scope;
14761 saved_object_scope = parser->object_scope;
14762 /* We must enter the scope of the class so that the names of
14763 entities declared within the class are available in the
14764 conversion-type-id. For example, consider:
14765
14766 struct S {
14767 typedef int I;
14768 operator I();
14769 };
14770
14771 S::operator I() { ... }
14772
14773 In order to see that `I' is a type-name in the definition, we
14774 must be in the scope of `S'. */
14775 if (saved_scope)
14776 pushed_scope = push_scope (saved_scope);
14777 /* Parse the conversion-type-id. */
14778 type = cp_parser_conversion_type_id (parser);
14779 /* Leave the scope of the class, if any. */
14780 if (pushed_scope)
14781 pop_scope (pushed_scope);
14782 /* Restore the saved scope. */
14783 parser->scope = saved_scope;
14784 parser->qualifying_scope = saved_qualifying_scope;
14785 parser->object_scope = saved_object_scope;
14786 /* If the TYPE is invalid, indicate failure. */
14787 if (type == error_mark_node)
14788 return error_mark_node;
14789 return make_conv_op_name (type);
14790 }
14791
14792 /* Parse a conversion-type-id:
14793
14794 conversion-type-id:
14795 type-specifier-seq conversion-declarator [opt]
14796
14797 Returns the TYPE specified. */
14798
14799 static tree
14800 cp_parser_conversion_type_id (cp_parser* parser)
14801 {
14802 tree attributes;
14803 cp_decl_specifier_seq type_specifiers;
14804 cp_declarator *declarator;
14805 tree type_specified;
14806 const char *saved_message;
14807
14808 /* Parse the attributes. */
14809 attributes = cp_parser_attributes_opt (parser);
14810
14811 saved_message = parser->type_definition_forbidden_message;
14812 parser->type_definition_forbidden_message
14813 = G_("types may not be defined in a conversion-type-id");
14814
14815 /* Parse the type-specifiers. */
14816 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
14817 /*is_declaration=*/false,
14818 /*is_trailing_return=*/false,
14819 &type_specifiers);
14820
14821 parser->type_definition_forbidden_message = saved_message;
14822
14823 /* If that didn't work, stop. */
14824 if (type_specifiers.type == error_mark_node)
14825 return error_mark_node;
14826 /* Parse the conversion-declarator. */
14827 declarator = cp_parser_conversion_declarator_opt (parser);
14828
14829 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14830 /*initialized=*/0, &attributes);
14831 if (attributes)
14832 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14833
14834 /* Don't give this error when parsing tentatively. This happens to
14835 work because we always parse this definitively once. */
14836 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14837 && type_uses_auto (type_specified))
14838 {
14839 if (cxx_dialect < cxx14)
14840 {
14841 error ("invalid use of %<auto%> in conversion operator");
14842 return error_mark_node;
14843 }
14844 else if (template_parm_scope_p ())
14845 warning (0, "use of %<auto%> in member template "
14846 "conversion operator can never be deduced");
14847 }
14848
14849 return type_specified;
14850 }
14851
14852 /* Parse an (optional) conversion-declarator.
14853
14854 conversion-declarator:
14855 ptr-operator conversion-declarator [opt]
14856
14857 */
14858
14859 static cp_declarator *
14860 cp_parser_conversion_declarator_opt (cp_parser* parser)
14861 {
14862 enum tree_code code;
14863 tree class_type, std_attributes = NULL_TREE;
14864 cp_cv_quals cv_quals;
14865
14866 /* We don't know if there's a ptr-operator next, or not. */
14867 cp_parser_parse_tentatively (parser);
14868 /* Try the ptr-operator. */
14869 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14870 &std_attributes);
14871 /* If it worked, look for more conversion-declarators. */
14872 if (cp_parser_parse_definitely (parser))
14873 {
14874 cp_declarator *declarator;
14875
14876 /* Parse another optional declarator. */
14877 declarator = cp_parser_conversion_declarator_opt (parser);
14878
14879 declarator = cp_parser_make_indirect_declarator
14880 (code, class_type, cv_quals, declarator, std_attributes);
14881
14882 return declarator;
14883 }
14884
14885 return NULL;
14886 }
14887
14888 /* Parse an (optional) ctor-initializer.
14889
14890 ctor-initializer:
14891 : mem-initializer-list */
14892
14893 static void
14894 cp_parser_ctor_initializer_opt (cp_parser* parser)
14895 {
14896 /* If the next token is not a `:', then there is no
14897 ctor-initializer. */
14898 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14899 {
14900 /* Do default initialization of any bases and members. */
14901 if (DECL_CONSTRUCTOR_P (current_function_decl))
14902 finish_mem_initializers (NULL_TREE);
14903 return;
14904 }
14905
14906 /* Consume the `:' token. */
14907 cp_lexer_consume_token (parser->lexer);
14908 /* And the mem-initializer-list. */
14909 cp_parser_mem_initializer_list (parser);
14910 }
14911
14912 /* Parse a mem-initializer-list.
14913
14914 mem-initializer-list:
14915 mem-initializer ... [opt]
14916 mem-initializer ... [opt] , mem-initializer-list */
14917
14918 static void
14919 cp_parser_mem_initializer_list (cp_parser* parser)
14920 {
14921 tree mem_initializer_list = NULL_TREE;
14922 tree target_ctor = error_mark_node;
14923 cp_token *token = cp_lexer_peek_token (parser->lexer);
14924
14925 /* Let the semantic analysis code know that we are starting the
14926 mem-initializer-list. */
14927 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14928 error_at (token->location,
14929 "only constructors take member initializers");
14930
14931 /* Loop through the list. */
14932 while (true)
14933 {
14934 tree mem_initializer;
14935
14936 token = cp_lexer_peek_token (parser->lexer);
14937 /* Parse the mem-initializer. */
14938 mem_initializer = cp_parser_mem_initializer (parser);
14939 /* If the next token is a `...', we're expanding member initializers. */
14940 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14941 if (ellipsis
14942 || (mem_initializer != error_mark_node
14943 && check_for_bare_parameter_packs (TREE_PURPOSE
14944 (mem_initializer))))
14945 {
14946 /* Consume the `...'. */
14947 if (ellipsis)
14948 cp_lexer_consume_token (parser->lexer);
14949
14950 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14951 can be expanded but members cannot. */
14952 if (mem_initializer != error_mark_node
14953 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14954 {
14955 error_at (token->location,
14956 "cannot expand initializer for member %qD",
14957 TREE_PURPOSE (mem_initializer));
14958 mem_initializer = error_mark_node;
14959 }
14960
14961 /* Construct the pack expansion type. */
14962 if (mem_initializer != error_mark_node)
14963 mem_initializer = make_pack_expansion (mem_initializer);
14964 }
14965 if (target_ctor != error_mark_node
14966 && mem_initializer != error_mark_node)
14967 {
14968 error ("mem-initializer for %qD follows constructor delegation",
14969 TREE_PURPOSE (mem_initializer));
14970 mem_initializer = error_mark_node;
14971 }
14972 /* Look for a target constructor. */
14973 if (mem_initializer != error_mark_node
14974 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14975 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14976 {
14977 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14978 if (mem_initializer_list)
14979 {
14980 error ("constructor delegation follows mem-initializer for %qD",
14981 TREE_PURPOSE (mem_initializer_list));
14982 mem_initializer = error_mark_node;
14983 }
14984 target_ctor = mem_initializer;
14985 }
14986 /* Add it to the list, unless it was erroneous. */
14987 if (mem_initializer != error_mark_node)
14988 {
14989 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14990 mem_initializer_list = mem_initializer;
14991 }
14992 /* If the next token is not a `,', we're done. */
14993 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14994 break;
14995 /* Consume the `,' token. */
14996 cp_lexer_consume_token (parser->lexer);
14997 }
14998
14999 /* Perform semantic analysis. */
15000 if (DECL_CONSTRUCTOR_P (current_function_decl))
15001 finish_mem_initializers (mem_initializer_list);
15002 }
15003
15004 /* Parse a mem-initializer.
15005
15006 mem-initializer:
15007 mem-initializer-id ( expression-list [opt] )
15008 mem-initializer-id braced-init-list
15009
15010 GNU extension:
15011
15012 mem-initializer:
15013 ( expression-list [opt] )
15014
15015 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15016 class) or FIELD_DECL (for a non-static data member) to initialize;
15017 the TREE_VALUE is the expression-list. An empty initialization
15018 list is represented by void_list_node. */
15019
15020 static tree
15021 cp_parser_mem_initializer (cp_parser* parser)
15022 {
15023 tree mem_initializer_id;
15024 tree expression_list;
15025 tree member;
15026 cp_token *token = cp_lexer_peek_token (parser->lexer);
15027
15028 /* Find out what is being initialized. */
15029 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15030 {
15031 permerror (token->location,
15032 "anachronistic old-style base class initializer");
15033 mem_initializer_id = NULL_TREE;
15034 }
15035 else
15036 {
15037 mem_initializer_id = cp_parser_mem_initializer_id (parser);
15038 if (mem_initializer_id == error_mark_node)
15039 return mem_initializer_id;
15040 }
15041 member = expand_member_init (mem_initializer_id);
15042 if (member && !DECL_P (member))
15043 in_base_initializer = 1;
15044
15045 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15046 {
15047 bool expr_non_constant_p;
15048 cp_lexer_set_source_position (parser->lexer);
15049 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15050 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
15051 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
15052 expression_list = build_tree_list (NULL_TREE, expression_list);
15053 }
15054 else
15055 {
15056 vec<tree, va_gc> *vec;
15057 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15058 /*cast_p=*/false,
15059 /*allow_expansion_p=*/true,
15060 /*non_constant_p=*/NULL,
15061 /*close_paren_loc=*/NULL,
15062 /*wrap_locations_p=*/true);
15063 if (vec == NULL)
15064 return error_mark_node;
15065 expression_list = build_tree_list_vec (vec);
15066 release_tree_vector (vec);
15067 }
15068
15069 if (expression_list == error_mark_node)
15070 return error_mark_node;
15071 if (!expression_list)
15072 expression_list = void_type_node;
15073
15074 in_base_initializer = 0;
15075
15076 return member ? build_tree_list (member, expression_list) : error_mark_node;
15077 }
15078
15079 /* Parse a mem-initializer-id.
15080
15081 mem-initializer-id:
15082 :: [opt] nested-name-specifier [opt] class-name
15083 decltype-specifier (C++11)
15084 identifier
15085
15086 Returns a TYPE indicating the class to be initialized for the first
15087 production (and the second in C++11). Returns an IDENTIFIER_NODE
15088 indicating the data member to be initialized for the last production. */
15089
15090 static tree
15091 cp_parser_mem_initializer_id (cp_parser* parser)
15092 {
15093 bool global_scope_p;
15094 bool nested_name_specifier_p;
15095 bool template_p = false;
15096 tree id;
15097
15098 cp_token *token = cp_lexer_peek_token (parser->lexer);
15099
15100 /* `typename' is not allowed in this context ([temp.res]). */
15101 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15102 {
15103 error_at (token->location,
15104 "keyword %<typename%> not allowed in this context (a qualified "
15105 "member initializer is implicitly a type)");
15106 cp_lexer_consume_token (parser->lexer);
15107 }
15108 /* Look for the optional `::' operator. */
15109 global_scope_p
15110 = (cp_parser_global_scope_opt (parser,
15111 /*current_scope_valid_p=*/false)
15112 != NULL_TREE);
15113 /* Look for the optional nested-name-specifier. The simplest way to
15114 implement:
15115
15116 [temp.res]
15117
15118 The keyword `typename' is not permitted in a base-specifier or
15119 mem-initializer; in these contexts a qualified name that
15120 depends on a template-parameter is implicitly assumed to be a
15121 type name.
15122
15123 is to assume that we have seen the `typename' keyword at this
15124 point. */
15125 nested_name_specifier_p
15126 = (cp_parser_nested_name_specifier_opt (parser,
15127 /*typename_keyword_p=*/true,
15128 /*check_dependency_p=*/true,
15129 /*type_p=*/true,
15130 /*is_declaration=*/true)
15131 != NULL_TREE);
15132 if (nested_name_specifier_p)
15133 template_p = cp_parser_optional_template_keyword (parser);
15134 /* If there is a `::' operator or a nested-name-specifier, then we
15135 are definitely looking for a class-name. */
15136 if (global_scope_p || nested_name_specifier_p)
15137 return cp_parser_class_name (parser,
15138 /*typename_keyword_p=*/true,
15139 /*template_keyword_p=*/template_p,
15140 typename_type,
15141 /*check_dependency_p=*/true,
15142 /*class_head_p=*/false,
15143 /*is_declaration=*/true);
15144 /* Otherwise, we could also be looking for an ordinary identifier. */
15145 cp_parser_parse_tentatively (parser);
15146 if (cp_lexer_next_token_is_decltype (parser->lexer))
15147 /* Try a decltype-specifier. */
15148 id = cp_parser_decltype (parser);
15149 else
15150 /* Otherwise, try a class-name. */
15151 id = cp_parser_class_name (parser,
15152 /*typename_keyword_p=*/true,
15153 /*template_keyword_p=*/false,
15154 none_type,
15155 /*check_dependency_p=*/true,
15156 /*class_head_p=*/false,
15157 /*is_declaration=*/true);
15158 /* If we found one, we're done. */
15159 if (cp_parser_parse_definitely (parser))
15160 return id;
15161 /* Otherwise, look for an ordinary identifier. */
15162 return cp_parser_identifier (parser);
15163 }
15164
15165 /* Overloading [gram.over] */
15166
15167 /* Parse an operator-function-id.
15168
15169 operator-function-id:
15170 operator operator
15171
15172 Returns an IDENTIFIER_NODE for the operator which is a
15173 human-readable spelling of the identifier, e.g., `operator +'. */
15174
15175 static cp_expr
15176 cp_parser_operator_function_id (cp_parser* parser)
15177 {
15178 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15179 /* Look for the `operator' keyword. */
15180 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15181 return error_mark_node;
15182 /* And then the name of the operator itself. */
15183 return cp_parser_operator (parser, start_loc);
15184 }
15185
15186 /* Return an identifier node for a user-defined literal operator.
15187 The suffix identifier is chained to the operator name identifier. */
15188
15189 tree
15190 cp_literal_operator_id (const char* name)
15191 {
15192 tree identifier;
15193 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15194 + strlen (name) + 10);
15195 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15196 identifier = get_identifier (buffer);
15197
15198 return identifier;
15199 }
15200
15201 /* Parse an operator.
15202
15203 operator:
15204 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15205 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15206 || ++ -- , ->* -> () []
15207
15208 GNU Extensions:
15209
15210 operator:
15211 <? >? <?= >?=
15212
15213 Returns an IDENTIFIER_NODE for the operator which is a
15214 human-readable spelling of the identifier, e.g., `operator +'. */
15215
15216 static cp_expr
15217 cp_parser_operator (cp_parser* parser, location_t start_loc)
15218 {
15219 tree id = NULL_TREE;
15220 cp_token *token;
15221 bool utf8 = false;
15222
15223 /* Peek at the next token. */
15224 token = cp_lexer_peek_token (parser->lexer);
15225
15226 location_t end_loc = token->location;
15227
15228 /* Figure out which operator we have. */
15229 enum tree_code op = ERROR_MARK;
15230 bool assop = false;
15231 bool consumed = false;
15232 switch (token->type)
15233 {
15234 case CPP_KEYWORD:
15235 {
15236 /* The keyword should be either `new' or `delete'. */
15237 if (token->keyword == RID_NEW)
15238 op = NEW_EXPR;
15239 else if (token->keyword == RID_DELETE)
15240 op = DELETE_EXPR;
15241 else
15242 break;
15243
15244 /* Consume the `new' or `delete' token. */
15245 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15246
15247 /* Peek at the next token. */
15248 token = cp_lexer_peek_token (parser->lexer);
15249 /* If it's a `[' token then this is the array variant of the
15250 operator. */
15251 if (token->type == CPP_OPEN_SQUARE)
15252 {
15253 /* Consume the `[' token. */
15254 cp_lexer_consume_token (parser->lexer);
15255 /* Look for the `]' token. */
15256 if (cp_token *close_token
15257 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15258 end_loc = close_token->location;
15259 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15260 }
15261 consumed = true;
15262 break;
15263 }
15264
15265 case CPP_PLUS:
15266 op = PLUS_EXPR;
15267 break;
15268
15269 case CPP_MINUS:
15270 op = MINUS_EXPR;
15271 break;
15272
15273 case CPP_MULT:
15274 op = MULT_EXPR;
15275 break;
15276
15277 case CPP_DIV:
15278 op = TRUNC_DIV_EXPR;
15279 break;
15280
15281 case CPP_MOD:
15282 op = TRUNC_MOD_EXPR;
15283 break;
15284
15285 case CPP_XOR:
15286 op = BIT_XOR_EXPR;
15287 break;
15288
15289 case CPP_AND:
15290 op = BIT_AND_EXPR;
15291 break;
15292
15293 case CPP_OR:
15294 op = BIT_IOR_EXPR;
15295 break;
15296
15297 case CPP_COMPL:
15298 op = BIT_NOT_EXPR;
15299 break;
15300
15301 case CPP_NOT:
15302 op = TRUTH_NOT_EXPR;
15303 break;
15304
15305 case CPP_EQ:
15306 assop = true;
15307 op = NOP_EXPR;
15308 break;
15309
15310 case CPP_LESS:
15311 op = LT_EXPR;
15312 break;
15313
15314 case CPP_GREATER:
15315 op = GT_EXPR;
15316 break;
15317
15318 case CPP_PLUS_EQ:
15319 assop = true;
15320 op = PLUS_EXPR;
15321 break;
15322
15323 case CPP_MINUS_EQ:
15324 assop = true;
15325 op = MINUS_EXPR;
15326 break;
15327
15328 case CPP_MULT_EQ:
15329 assop = true;
15330 op = MULT_EXPR;
15331 break;
15332
15333 case CPP_DIV_EQ:
15334 assop = true;
15335 op = TRUNC_DIV_EXPR;
15336 break;
15337
15338 case CPP_MOD_EQ:
15339 assop = true;
15340 op = TRUNC_MOD_EXPR;
15341 break;
15342
15343 case CPP_XOR_EQ:
15344 assop = true;
15345 op = BIT_XOR_EXPR;
15346 break;
15347
15348 case CPP_AND_EQ:
15349 assop = true;
15350 op = BIT_AND_EXPR;
15351 break;
15352
15353 case CPP_OR_EQ:
15354 assop = true;
15355 op = BIT_IOR_EXPR;
15356 break;
15357
15358 case CPP_LSHIFT:
15359 op = LSHIFT_EXPR;
15360 break;
15361
15362 case CPP_RSHIFT:
15363 op = RSHIFT_EXPR;
15364 break;
15365
15366 case CPP_LSHIFT_EQ:
15367 assop = true;
15368 op = LSHIFT_EXPR;
15369 break;
15370
15371 case CPP_RSHIFT_EQ:
15372 assop = true;
15373 op = RSHIFT_EXPR;
15374 break;
15375
15376 case CPP_EQ_EQ:
15377 op = EQ_EXPR;
15378 break;
15379
15380 case CPP_NOT_EQ:
15381 op = NE_EXPR;
15382 break;
15383
15384 case CPP_LESS_EQ:
15385 op = LE_EXPR;
15386 break;
15387
15388 case CPP_GREATER_EQ:
15389 op = GE_EXPR;
15390 break;
15391
15392 case CPP_AND_AND:
15393 op = TRUTH_ANDIF_EXPR;
15394 break;
15395
15396 case CPP_OR_OR:
15397 op = TRUTH_ORIF_EXPR;
15398 break;
15399
15400 case CPP_PLUS_PLUS:
15401 op = POSTINCREMENT_EXPR;
15402 break;
15403
15404 case CPP_MINUS_MINUS:
15405 op = PREDECREMENT_EXPR;
15406 break;
15407
15408 case CPP_COMMA:
15409 op = COMPOUND_EXPR;
15410 break;
15411
15412 case CPP_DEREF_STAR:
15413 op = MEMBER_REF;
15414 break;
15415
15416 case CPP_DEREF:
15417 op = COMPONENT_REF;
15418 break;
15419
15420 case CPP_OPEN_PAREN:
15421 {
15422 /* Consume the `('. */
15423 matching_parens parens;
15424 parens.consume_open (parser);
15425 /* Look for the matching `)'. */
15426 token = parens.require_close (parser);
15427 if (token)
15428 end_loc = token->location;
15429 op = CALL_EXPR;
15430 consumed = true;
15431 break;
15432 }
15433
15434 case CPP_OPEN_SQUARE:
15435 /* Consume the `['. */
15436 cp_lexer_consume_token (parser->lexer);
15437 /* Look for the matching `]'. */
15438 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15439 if (token)
15440 end_loc = token->location;
15441 op = ARRAY_REF;
15442 consumed = true;
15443 break;
15444
15445 case CPP_UTF8STRING:
15446 case CPP_UTF8STRING_USERDEF:
15447 utf8 = true;
15448 /* FALLTHRU */
15449 case CPP_STRING:
15450 case CPP_WSTRING:
15451 case CPP_STRING16:
15452 case CPP_STRING32:
15453 case CPP_STRING_USERDEF:
15454 case CPP_WSTRING_USERDEF:
15455 case CPP_STRING16_USERDEF:
15456 case CPP_STRING32_USERDEF:
15457 {
15458 cp_expr str;
15459 tree string_tree;
15460 int sz, len;
15461
15462 if (cxx_dialect == cxx98)
15463 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15464
15465 /* Consume the string. */
15466 str = cp_parser_string_literal (parser, /*translate=*/true,
15467 /*wide_ok=*/true, /*lookup_udlit=*/false);
15468 if (str == error_mark_node)
15469 return error_mark_node;
15470 else if (TREE_CODE (str) == USERDEF_LITERAL)
15471 {
15472 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15473 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15474 end_loc = str.get_location ();
15475 }
15476 else
15477 {
15478 string_tree = str;
15479 /* Look for the suffix identifier. */
15480 token = cp_lexer_peek_token (parser->lexer);
15481 if (token->type == CPP_NAME)
15482 {
15483 id = cp_parser_identifier (parser);
15484 end_loc = token->location;
15485 }
15486 else if (token->type == CPP_KEYWORD)
15487 {
15488 error ("unexpected keyword;"
15489 " remove space between quotes and suffix identifier");
15490 return error_mark_node;
15491 }
15492 else
15493 {
15494 error ("expected suffix identifier");
15495 return error_mark_node;
15496 }
15497 }
15498 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15499 (TREE_TYPE (TREE_TYPE (string_tree))));
15500 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15501 if (len != 0)
15502 {
15503 error ("expected empty string after %<operator%> keyword");
15504 return error_mark_node;
15505 }
15506 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15507 != char_type_node)
15508 {
15509 error ("invalid encoding prefix in literal operator");
15510 return error_mark_node;
15511 }
15512 if (id != error_mark_node)
15513 {
15514 const char *name = IDENTIFIER_POINTER (id);
15515 id = cp_literal_operator_id (name);
15516 }
15517 /* Generate a location of the form:
15518 "" _suffix_identifier
15519 ^~~~~~~~~~~~~~~~~~~~~
15520 with caret == start at the start token, finish at the end of the
15521 suffix identifier. */
15522 location_t finish_loc
15523 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15524 location_t combined_loc
15525 = make_location (start_loc, start_loc, finish_loc);
15526 return cp_expr (id, combined_loc);
15527 }
15528
15529 default:
15530 /* Anything else is an error. */
15531 break;
15532 }
15533
15534 /* If we have selected an identifier, we need to consume the
15535 operator token. */
15536 if (op != ERROR_MARK)
15537 {
15538 id = ovl_op_identifier (assop, op);
15539 if (!consumed)
15540 cp_lexer_consume_token (parser->lexer);
15541 }
15542 /* Otherwise, no valid operator name was present. */
15543 else
15544 {
15545 cp_parser_error (parser, "expected operator");
15546 id = error_mark_node;
15547 }
15548
15549 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15550 return cp_expr (id, start_loc);
15551 }
15552
15553 /* Parse a template-declaration.
15554
15555 template-declaration:
15556 export [opt] template < template-parameter-list > declaration
15557
15558 If MEMBER_P is TRUE, this template-declaration occurs within a
15559 class-specifier.
15560
15561 The grammar rule given by the standard isn't correct. What
15562 is really meant is:
15563
15564 template-declaration:
15565 export [opt] template-parameter-list-seq
15566 decl-specifier-seq [opt] init-declarator [opt] ;
15567 export [opt] template-parameter-list-seq
15568 function-definition
15569
15570 template-parameter-list-seq:
15571 template-parameter-list-seq [opt]
15572 template < template-parameter-list >
15573
15574 Concept Extensions:
15575
15576 template-parameter-list-seq:
15577 template < template-parameter-list > requires-clause [opt]
15578
15579 requires-clause:
15580 requires logical-or-expression */
15581
15582 static void
15583 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15584 {
15585 /* Check for `export'. */
15586 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15587 {
15588 /* Consume the `export' token. */
15589 cp_lexer_consume_token (parser->lexer);
15590 /* Warn that we do not support `export'. */
15591 warning (0, "keyword %<export%> not implemented, and will be ignored");
15592 }
15593
15594 cp_parser_template_declaration_after_export (parser, member_p);
15595 }
15596
15597 /* Parse a template-parameter-list.
15598
15599 template-parameter-list:
15600 template-parameter
15601 template-parameter-list , template-parameter
15602
15603 Returns a TREE_LIST. Each node represents a template parameter.
15604 The nodes are connected via their TREE_CHAINs. */
15605
15606 static tree
15607 cp_parser_template_parameter_list (cp_parser* parser)
15608 {
15609 tree parameter_list = NULL_TREE;
15610
15611 /* Don't create wrapper nodes within a template-parameter-list,
15612 since we don't want to have different types based on the
15613 spelling location of constants and decls within them. */
15614 auto_suppress_location_wrappers sentinel;
15615
15616 begin_template_parm_list ();
15617
15618 /* The loop below parses the template parms. We first need to know
15619 the total number of template parms to be able to compute proper
15620 canonical types of each dependent type. So after the loop, when
15621 we know the total number of template parms,
15622 end_template_parm_list computes the proper canonical types and
15623 fixes up the dependent types accordingly. */
15624 while (true)
15625 {
15626 tree parameter;
15627 bool is_non_type;
15628 bool is_parameter_pack;
15629 location_t parm_loc;
15630
15631 /* Parse the template-parameter. */
15632 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15633 parameter = cp_parser_template_parameter (parser,
15634 &is_non_type,
15635 &is_parameter_pack);
15636 /* Add it to the list. */
15637 if (parameter != error_mark_node)
15638 parameter_list = process_template_parm (parameter_list,
15639 parm_loc,
15640 parameter,
15641 is_non_type,
15642 is_parameter_pack);
15643 else
15644 {
15645 tree err_parm = build_tree_list (parameter, parameter);
15646 parameter_list = chainon (parameter_list, err_parm);
15647 }
15648
15649 /* If the next token is not a `,', we're done. */
15650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15651 break;
15652 /* Otherwise, consume the `,' token. */
15653 cp_lexer_consume_token (parser->lexer);
15654 }
15655
15656 return end_template_parm_list (parameter_list);
15657 }
15658
15659 /* Parse a introduction-list.
15660
15661 introduction-list:
15662 introduced-parameter
15663 introduction-list , introduced-parameter
15664
15665 introduced-parameter:
15666 ...[opt] identifier
15667
15668 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15669 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15670 WILDCARD_DECL will also have DECL_NAME set and token location in
15671 DECL_SOURCE_LOCATION. */
15672
15673 static tree
15674 cp_parser_introduction_list (cp_parser *parser)
15675 {
15676 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15677
15678 while (true)
15679 {
15680 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15681 if (is_pack)
15682 cp_lexer_consume_token (parser->lexer);
15683
15684 tree identifier = cp_parser_identifier (parser);
15685 if (identifier == error_mark_node)
15686 break;
15687
15688 /* Build placeholder. */
15689 tree parm = build_nt (WILDCARD_DECL);
15690 DECL_SOURCE_LOCATION (parm)
15691 = cp_lexer_peek_token (parser->lexer)->location;
15692 DECL_NAME (parm) = identifier;
15693 WILDCARD_PACK_P (parm) = is_pack;
15694 vec_safe_push (introduction_vec, parm);
15695
15696 /* If the next token is not a `,', we're done. */
15697 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15698 break;
15699 /* Otherwise, consume the `,' token. */
15700 cp_lexer_consume_token (parser->lexer);
15701 }
15702
15703 /* Convert the vec into a TREE_VEC. */
15704 tree introduction_list = make_tree_vec (introduction_vec->length ());
15705 unsigned int n;
15706 tree parm;
15707 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15708 TREE_VEC_ELT (introduction_list, n) = parm;
15709
15710 release_tree_vector (introduction_vec);
15711 return introduction_list;
15712 }
15713
15714 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15715 is an abstract declarator. */
15716
15717 static inline cp_declarator*
15718 get_id_declarator (cp_declarator *declarator)
15719 {
15720 cp_declarator *d = declarator;
15721 while (d && d->kind != cdk_id)
15722 d = d->declarator;
15723 return d;
15724 }
15725
15726 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15727 is an abstract declarator. */
15728
15729 static inline tree
15730 get_unqualified_id (cp_declarator *declarator)
15731 {
15732 declarator = get_id_declarator (declarator);
15733 if (declarator)
15734 return declarator->u.id.unqualified_name;
15735 else
15736 return NULL_TREE;
15737 }
15738
15739 /* Returns true if DECL represents a constrained-parameter. */
15740
15741 static inline bool
15742 is_constrained_parameter (tree decl)
15743 {
15744 return (decl
15745 && TREE_CODE (decl) == TYPE_DECL
15746 && CONSTRAINED_PARM_CONCEPT (decl)
15747 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15748 }
15749
15750 /* Returns true if PARM declares a constrained-parameter. */
15751
15752 static inline bool
15753 is_constrained_parameter (cp_parameter_declarator *parm)
15754 {
15755 return is_constrained_parameter (parm->decl_specifiers.type);
15756 }
15757
15758 /* Check that the type parameter is only a declarator-id, and that its
15759 type is not cv-qualified. */
15760
15761 bool
15762 cp_parser_check_constrained_type_parm (cp_parser *parser,
15763 cp_parameter_declarator *parm)
15764 {
15765 if (!parm->declarator)
15766 return true;
15767
15768 if (parm->declarator->kind != cdk_id)
15769 {
15770 cp_parser_error (parser, "invalid constrained type parameter");
15771 return false;
15772 }
15773
15774 /* Don't allow cv-qualified type parameters. */
15775 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15776 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15777 {
15778 cp_parser_error (parser, "cv-qualified type parameter");
15779 return false;
15780 }
15781
15782 return true;
15783 }
15784
15785 /* Finish parsing/processing a template type parameter and checking
15786 various restrictions. */
15787
15788 static inline tree
15789 cp_parser_constrained_type_template_parm (cp_parser *parser,
15790 tree id,
15791 cp_parameter_declarator* parmdecl)
15792 {
15793 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15794 return finish_template_type_parm (class_type_node, id);
15795 else
15796 return error_mark_node;
15797 }
15798
15799 static tree
15800 finish_constrained_template_template_parm (tree proto, tree id)
15801 {
15802 /* FIXME: This should probably be copied, and we may need to adjust
15803 the template parameter depths. */
15804 tree saved_parms = current_template_parms;
15805 begin_template_parm_list ();
15806 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15807 end_template_parm_list ();
15808
15809 tree parm = finish_template_template_parm (class_type_node, id);
15810 current_template_parms = saved_parms;
15811
15812 return parm;
15813 }
15814
15815 /* Finish parsing/processing a template template parameter by borrowing
15816 the template parameter list from the prototype parameter. */
15817
15818 static tree
15819 cp_parser_constrained_template_template_parm (cp_parser *parser,
15820 tree proto,
15821 tree id,
15822 cp_parameter_declarator *parmdecl)
15823 {
15824 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15825 return error_mark_node;
15826 return finish_constrained_template_template_parm (proto, id);
15827 }
15828
15829 /* Create a new non-type template parameter from the given PARM
15830 declarator. */
15831
15832 static tree
15833 constrained_non_type_template_parm (bool *is_non_type,
15834 cp_parameter_declarator *parm)
15835 {
15836 *is_non_type = true;
15837 cp_declarator *decl = parm->declarator;
15838 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15839 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15840 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15841 }
15842
15843 /* Build a constrained template parameter based on the PARMDECL
15844 declarator. The type of PARMDECL is the constrained type, which
15845 refers to the prototype template parameter that ultimately
15846 specifies the type of the declared parameter. */
15847
15848 static tree
15849 finish_constrained_parameter (cp_parser *parser,
15850 cp_parameter_declarator *parmdecl,
15851 bool *is_non_type,
15852 bool *is_parameter_pack)
15853 {
15854 tree decl = parmdecl->decl_specifiers.type;
15855 tree id = get_unqualified_id (parmdecl->declarator);
15856 tree def = parmdecl->default_argument;
15857 tree proto = DECL_INITIAL (decl);
15858
15859 /* A template parameter constrained by a variadic concept shall also
15860 be declared as a template parameter pack. */
15861 bool is_variadic = template_parameter_pack_p (proto);
15862 if (is_variadic && !*is_parameter_pack)
15863 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15864
15865 /* Build the parameter. Return an error if the declarator was invalid. */
15866 tree parm;
15867 if (TREE_CODE (proto) == TYPE_DECL)
15868 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15869 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15870 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15871 parmdecl);
15872 else
15873 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15874 if (parm == error_mark_node)
15875 return error_mark_node;
15876
15877 /* Finish the parameter decl and create a node attaching the
15878 default argument and constraint. */
15879 parm = build_tree_list (def, parm);
15880 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15881
15882 return parm;
15883 }
15884
15885 /* Returns true if the parsed type actually represents the declaration
15886 of a type template-parameter. */
15887
15888 static inline bool
15889 declares_constrained_type_template_parameter (tree type)
15890 {
15891 return (is_constrained_parameter (type)
15892 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15893 }
15894
15895
15896 /* Returns true if the parsed type actually represents the declaration of
15897 a template template-parameter. */
15898
15899 static bool
15900 declares_constrained_template_template_parameter (tree type)
15901 {
15902 return (is_constrained_parameter (type)
15903 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15904 }
15905
15906 /* Parse a default argument for a type template-parameter.
15907 Note that diagnostics are handled in cp_parser_template_parameter. */
15908
15909 static tree
15910 cp_parser_default_type_template_argument (cp_parser *parser)
15911 {
15912 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15913
15914 /* Consume the `=' token. */
15915 cp_lexer_consume_token (parser->lexer);
15916
15917 cp_token *token = cp_lexer_peek_token (parser->lexer);
15918
15919 /* Parse the default-argument. */
15920 push_deferring_access_checks (dk_no_deferred);
15921 tree default_argument = cp_parser_type_id (parser,
15922 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15923 NULL);
15924 pop_deferring_access_checks ();
15925
15926 if (flag_concepts && type_uses_auto (default_argument))
15927 {
15928 error_at (token->location,
15929 "invalid use of %<auto%> in default template argument");
15930 return error_mark_node;
15931 }
15932
15933 return default_argument;
15934 }
15935
15936 /* Parse a default argument for a template template-parameter. */
15937
15938 static tree
15939 cp_parser_default_template_template_argument (cp_parser *parser)
15940 {
15941 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15942
15943 bool is_template;
15944
15945 /* Consume the `='. */
15946 cp_lexer_consume_token (parser->lexer);
15947 /* Parse the id-expression. */
15948 push_deferring_access_checks (dk_no_deferred);
15949 /* save token before parsing the id-expression, for error
15950 reporting */
15951 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15952 tree default_argument
15953 = cp_parser_id_expression (parser,
15954 /*template_keyword_p=*/false,
15955 /*check_dependency_p=*/true,
15956 /*template_p=*/&is_template,
15957 /*declarator_p=*/false,
15958 /*optional_p=*/false);
15959 if (TREE_CODE (default_argument) == TYPE_DECL)
15960 /* If the id-expression was a template-id that refers to
15961 a template-class, we already have the declaration here,
15962 so no further lookup is needed. */
15963 ;
15964 else
15965 /* Look up the name. */
15966 default_argument
15967 = cp_parser_lookup_name (parser, default_argument,
15968 none_type,
15969 /*is_template=*/is_template,
15970 /*is_namespace=*/false,
15971 /*check_dependency=*/true,
15972 /*ambiguous_decls=*/NULL,
15973 token->location);
15974 /* See if the default argument is valid. */
15975 default_argument = check_template_template_default_arg (default_argument);
15976 pop_deferring_access_checks ();
15977 return default_argument;
15978 }
15979
15980 /* Parse a template-parameter.
15981
15982 template-parameter:
15983 type-parameter
15984 parameter-declaration
15985
15986 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15987 the parameter. The TREE_PURPOSE is the default value, if any.
15988 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15989 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15990 set to true iff this parameter is a parameter pack. */
15991
15992 static tree
15993 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15994 bool *is_parameter_pack)
15995 {
15996 cp_token *token;
15997 cp_parameter_declarator *parameter_declarator;
15998 tree parm;
15999
16000 /* Assume it is a type parameter or a template parameter. */
16001 *is_non_type = false;
16002 /* Assume it not a parameter pack. */
16003 *is_parameter_pack = false;
16004 /* Peek at the next token. */
16005 token = cp_lexer_peek_token (parser->lexer);
16006 /* If it is `template', we have a type-parameter. */
16007 if (token->keyword == RID_TEMPLATE)
16008 return cp_parser_type_parameter (parser, is_parameter_pack);
16009 /* If it is `class' or `typename' we do not know yet whether it is a
16010 type parameter or a non-type parameter. Consider:
16011
16012 template <typename T, typename T::X X> ...
16013
16014 or:
16015
16016 template <class C, class D*> ...
16017
16018 Here, the first parameter is a type parameter, and the second is
16019 a non-type parameter. We can tell by looking at the token after
16020 the identifier -- if it is a `,', `=', or `>' then we have a type
16021 parameter. */
16022 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
16023 {
16024 /* Peek at the token after `class' or `typename'. */
16025 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16026 /* If it's an ellipsis, we have a template type parameter
16027 pack. */
16028 if (token->type == CPP_ELLIPSIS)
16029 return cp_parser_type_parameter (parser, is_parameter_pack);
16030 /* If it's an identifier, skip it. */
16031 if (token->type == CPP_NAME)
16032 token = cp_lexer_peek_nth_token (parser->lexer, 3);
16033 /* Now, see if the token looks like the end of a template
16034 parameter. */
16035 if (token->type == CPP_COMMA
16036 || token->type == CPP_EQ
16037 || token->type == CPP_GREATER)
16038 return cp_parser_type_parameter (parser, is_parameter_pack);
16039 }
16040
16041 /* Otherwise, it is a non-type parameter or a constrained parameter.
16042
16043 [temp.param]
16044
16045 When parsing a default template-argument for a non-type
16046 template-parameter, the first non-nested `>' is taken as the end
16047 of the template parameter-list rather than a greater-than
16048 operator. */
16049 parameter_declarator
16050 = cp_parser_parameter_declaration (parser,
16051 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16052 /*template_parm_p=*/true,
16053 /*parenthesized_p=*/NULL);
16054
16055 if (!parameter_declarator)
16056 return error_mark_node;
16057
16058 /* If the parameter declaration is marked as a parameter pack, set
16059 *IS_PARAMETER_PACK to notify the caller. */
16060 if (parameter_declarator->template_parameter_pack_p)
16061 *is_parameter_pack = true;
16062
16063 if (parameter_declarator->default_argument)
16064 {
16065 /* Can happen in some cases of erroneous input (c++/34892). */
16066 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16067 /* Consume the `...' for better error recovery. */
16068 cp_lexer_consume_token (parser->lexer);
16069 }
16070
16071 // The parameter may have been constrained.
16072 if (is_constrained_parameter (parameter_declarator))
16073 return finish_constrained_parameter (parser,
16074 parameter_declarator,
16075 is_non_type,
16076 is_parameter_pack);
16077
16078 // Now we're sure that the parameter is a non-type parameter.
16079 *is_non_type = true;
16080
16081 parm = grokdeclarator (parameter_declarator->declarator,
16082 &parameter_declarator->decl_specifiers,
16083 TPARM, /*initialized=*/0,
16084 /*attrlist=*/NULL);
16085 if (parm == error_mark_node)
16086 return error_mark_node;
16087
16088 return build_tree_list (parameter_declarator->default_argument, parm);
16089 }
16090
16091 /* Parse a type-parameter.
16092
16093 type-parameter:
16094 class identifier [opt]
16095 class identifier [opt] = type-id
16096 typename identifier [opt]
16097 typename identifier [opt] = type-id
16098 template < template-parameter-list > class identifier [opt]
16099 template < template-parameter-list > class identifier [opt]
16100 = id-expression
16101
16102 GNU Extension (variadic templates):
16103
16104 type-parameter:
16105 class ... identifier [opt]
16106 typename ... identifier [opt]
16107
16108 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16109 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16110 the declaration of the parameter.
16111
16112 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16113
16114 static tree
16115 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
16116 {
16117 cp_token *token;
16118 tree parameter;
16119
16120 /* Look for a keyword to tell us what kind of parameter this is. */
16121 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
16122 if (!token)
16123 return error_mark_node;
16124
16125 switch (token->keyword)
16126 {
16127 case RID_CLASS:
16128 case RID_TYPENAME:
16129 {
16130 tree identifier;
16131 tree default_argument;
16132
16133 /* If the next token is an ellipsis, we have a template
16134 argument pack. */
16135 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16136 {
16137 /* Consume the `...' token. */
16138 cp_lexer_consume_token (parser->lexer);
16139 maybe_warn_variadic_templates ();
16140
16141 *is_parameter_pack = true;
16142 }
16143
16144 /* If the next token is an identifier, then it names the
16145 parameter. */
16146 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16147 identifier = cp_parser_identifier (parser);
16148 else
16149 identifier = NULL_TREE;
16150
16151 /* Create the parameter. */
16152 parameter = finish_template_type_parm (class_type_node, identifier);
16153
16154 /* If the next token is an `=', we have a default argument. */
16155 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16156 {
16157 default_argument
16158 = cp_parser_default_type_template_argument (parser);
16159
16160 /* Template parameter packs cannot have default
16161 arguments. */
16162 if (*is_parameter_pack)
16163 {
16164 if (identifier)
16165 error_at (token->location,
16166 "template parameter pack %qD cannot have a "
16167 "default argument", identifier);
16168 else
16169 error_at (token->location,
16170 "template parameter packs cannot have "
16171 "default arguments");
16172 default_argument = NULL_TREE;
16173 }
16174 else if (check_for_bare_parameter_packs (default_argument))
16175 default_argument = error_mark_node;
16176 }
16177 else
16178 default_argument = NULL_TREE;
16179
16180 /* Create the combined representation of the parameter and the
16181 default argument. */
16182 parameter = build_tree_list (default_argument, parameter);
16183 }
16184 break;
16185
16186 case RID_TEMPLATE:
16187 {
16188 tree identifier;
16189 tree default_argument;
16190
16191 /* Look for the `<'. */
16192 cp_parser_require (parser, CPP_LESS, RT_LESS);
16193 /* Parse the template-parameter-list. */
16194 cp_parser_template_parameter_list (parser);
16195 /* Look for the `>'. */
16196 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16197
16198 // If template requirements are present, parse them.
16199 if (flag_concepts)
16200 {
16201 tree reqs = get_shorthand_constraints (current_template_parms);
16202 if (tree r = cp_parser_requires_clause_opt (parser))
16203 reqs = conjoin_constraints (reqs, normalize_expression (r));
16204 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16205 }
16206
16207 /* Look for the `class' or 'typename' keywords. */
16208 cp_parser_type_parameter_key (parser);
16209 /* If the next token is an ellipsis, we have a template
16210 argument pack. */
16211 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16212 {
16213 /* Consume the `...' token. */
16214 cp_lexer_consume_token (parser->lexer);
16215 maybe_warn_variadic_templates ();
16216
16217 *is_parameter_pack = true;
16218 }
16219 /* If the next token is an `=', then there is a
16220 default-argument. If the next token is a `>', we are at
16221 the end of the parameter-list. If the next token is a `,',
16222 then we are at the end of this parameter. */
16223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16224 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16225 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16226 {
16227 identifier = cp_parser_identifier (parser);
16228 /* Treat invalid names as if the parameter were nameless. */
16229 if (identifier == error_mark_node)
16230 identifier = NULL_TREE;
16231 }
16232 else
16233 identifier = NULL_TREE;
16234
16235 /* Create the template parameter. */
16236 parameter = finish_template_template_parm (class_type_node,
16237 identifier);
16238
16239 /* If the next token is an `=', then there is a
16240 default-argument. */
16241 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16242 {
16243 default_argument
16244 = cp_parser_default_template_template_argument (parser);
16245
16246 /* Template parameter packs cannot have default
16247 arguments. */
16248 if (*is_parameter_pack)
16249 {
16250 if (identifier)
16251 error_at (token->location,
16252 "template parameter pack %qD cannot "
16253 "have a default argument",
16254 identifier);
16255 else
16256 error_at (token->location, "template parameter packs cannot "
16257 "have default arguments");
16258 default_argument = NULL_TREE;
16259 }
16260 }
16261 else
16262 default_argument = NULL_TREE;
16263
16264 /* Create the combined representation of the parameter and the
16265 default argument. */
16266 parameter = build_tree_list (default_argument, parameter);
16267 }
16268 break;
16269
16270 default:
16271 gcc_unreachable ();
16272 break;
16273 }
16274
16275 return parameter;
16276 }
16277
16278 /* Parse a template-id.
16279
16280 template-id:
16281 template-name < template-argument-list [opt] >
16282
16283 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16284 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16285 returned. Otherwise, if the template-name names a function, or set
16286 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16287 names a class, returns a TYPE_DECL for the specialization.
16288
16289 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16290 uninstantiated templates. */
16291
16292 static tree
16293 cp_parser_template_id (cp_parser *parser,
16294 bool template_keyword_p,
16295 bool check_dependency_p,
16296 enum tag_types tag_type,
16297 bool is_declaration)
16298 {
16299 tree templ;
16300 tree arguments;
16301 tree template_id;
16302 cp_token_position start_of_id = 0;
16303 cp_token *next_token = NULL, *next_token_2 = NULL;
16304 bool is_identifier;
16305
16306 /* If the next token corresponds to a template-id, there is no need
16307 to reparse it. */
16308 cp_token *token = cp_lexer_peek_token (parser->lexer);
16309 if (token->type == CPP_TEMPLATE_ID)
16310 {
16311 cp_lexer_consume_token (parser->lexer);
16312 return saved_checks_value (token->u.tree_check_value);
16313 }
16314
16315 /* Avoid performing name lookup if there is no possibility of
16316 finding a template-id. */
16317 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16318 || (token->type == CPP_NAME
16319 && !cp_parser_nth_token_starts_template_argument_list_p
16320 (parser, 2)))
16321 {
16322 cp_parser_error (parser, "expected template-id");
16323 return error_mark_node;
16324 }
16325
16326 /* Remember where the template-id starts. */
16327 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16328 start_of_id = cp_lexer_token_position (parser->lexer, false);
16329
16330 push_deferring_access_checks (dk_deferred);
16331
16332 /* Parse the template-name. */
16333 is_identifier = false;
16334 templ = cp_parser_template_name (parser, template_keyword_p,
16335 check_dependency_p,
16336 is_declaration,
16337 tag_type,
16338 &is_identifier);
16339
16340 /* Push any access checks inside the firewall we're about to create. */
16341 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16342 pop_deferring_access_checks ();
16343 if (templ == error_mark_node || is_identifier)
16344 return templ;
16345
16346 /* Since we're going to preserve any side-effects from this parse, set up a
16347 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16348 in the template arguments. */
16349 tentative_firewall firewall (parser);
16350 reopen_deferring_access_checks (checks);
16351
16352 /* If we find the sequence `[:' after a template-name, it's probably
16353 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16354 parse correctly the argument list. */
16355 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16356 == CPP_OPEN_SQUARE)
16357 && next_token->flags & DIGRAPH
16358 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16359 == CPP_COLON)
16360 && !(next_token_2->flags & PREV_WHITE))
16361 {
16362 cp_parser_parse_tentatively (parser);
16363 /* Change `:' into `::'. */
16364 next_token_2->type = CPP_SCOPE;
16365 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16366 CPP_LESS. */
16367 cp_lexer_consume_token (parser->lexer);
16368
16369 /* Parse the arguments. */
16370 arguments = cp_parser_enclosed_template_argument_list (parser);
16371 if (!cp_parser_parse_definitely (parser))
16372 {
16373 /* If we couldn't parse an argument list, then we revert our changes
16374 and return simply an error. Maybe this is not a template-id
16375 after all. */
16376 next_token_2->type = CPP_COLON;
16377 cp_parser_error (parser, "expected %<<%>");
16378 pop_deferring_access_checks ();
16379 return error_mark_node;
16380 }
16381 /* Otherwise, emit an error about the invalid digraph, but continue
16382 parsing because we got our argument list. */
16383 if (permerror (next_token->location,
16384 "%<<::%> cannot begin a template-argument list"))
16385 {
16386 static bool hint = false;
16387 inform (next_token->location,
16388 "%<<:%> is an alternate spelling for %<[%>."
16389 " Insert whitespace between %<<%> and %<::%>");
16390 if (!hint && !flag_permissive)
16391 {
16392 inform (next_token->location, "(if you use %<-fpermissive%> "
16393 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16394 "accept your code)");
16395 hint = true;
16396 }
16397 }
16398 }
16399 else
16400 {
16401 /* Look for the `<' that starts the template-argument-list. */
16402 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16403 {
16404 pop_deferring_access_checks ();
16405 return error_mark_node;
16406 }
16407 /* Parse the arguments. */
16408 arguments = cp_parser_enclosed_template_argument_list (parser);
16409
16410 if ((cxx_dialect > cxx17)
16411 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16412 && !template_keyword_p
16413 && (cp_parser_error_occurred (parser)
16414 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16415 {
16416 /* This didn't go well. */
16417 if (TREE_CODE (templ) == FUNCTION_DECL)
16418 {
16419 /* C++2A says that "function-name < a;" is now ill-formed. */
16420 if (cp_parser_error_occurred (parser))
16421 {
16422 error_at (token->location, "invalid template-argument-list");
16423 inform (token->location, "function name as the left hand "
16424 "operand of %<<%> is ill-formed in C++2a; wrap the "
16425 "function name in %<()%>");
16426 }
16427 else
16428 /* We expect "f<targs>" to be followed by "(args)". */
16429 error_at (cp_lexer_peek_token (parser->lexer)->location,
16430 "expected %<(%> after template-argument-list");
16431 if (start_of_id)
16432 /* Purge all subsequent tokens. */
16433 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16434 }
16435 else
16436 cp_parser_simulate_error (parser);
16437 pop_deferring_access_checks ();
16438 return error_mark_node;
16439 }
16440 }
16441
16442 /* Set the location to be of the form:
16443 template-name < template-argument-list [opt] >
16444 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16445 with caret == start at the start of the template-name,
16446 ranging until the closing '>'. */
16447 location_t finish_loc
16448 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16449 location_t combined_loc
16450 = make_location (token->location, token->location, finish_loc);
16451
16452 /* Check for concepts autos where they don't belong. We could
16453 identify types in some cases of idnetifier TEMPL, looking ahead
16454 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16455 types. We reject them in functions, but if what we have is an
16456 identifier, even with none_type we can't conclude it's NOT a
16457 type, we have to wait for template substitution. */
16458 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16459 template_id = error_mark_node;
16460 /* Build a representation of the specialization. */
16461 else if (identifier_p (templ))
16462 template_id = build_min_nt_loc (combined_loc,
16463 TEMPLATE_ID_EXPR,
16464 templ, arguments);
16465 else if (DECL_TYPE_TEMPLATE_P (templ)
16466 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16467 {
16468 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16469 template (rather than some instantiation thereof) only if
16470 is not nested within some other construct. For example, in
16471 "template <typename T> void f(T) { A<T>::", A<T> is just an
16472 instantiation of A. */
16473 bool entering_scope
16474 = (template_parm_scope_p ()
16475 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16476 template_id
16477 = finish_template_type (templ, arguments, entering_scope);
16478 }
16479 /* A template-like identifier may be a partial concept id. */
16480 else if (flag_concepts
16481 && (template_id = (cp_parser_maybe_partial_concept_id
16482 (parser, templ, arguments))))
16483 return template_id;
16484 else if (variable_template_p (templ))
16485 {
16486 template_id = lookup_template_variable (templ, arguments);
16487 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16488 SET_EXPR_LOCATION (template_id, combined_loc);
16489 }
16490 else
16491 {
16492 /* If it's not a class-template or a template-template, it should be
16493 a function-template. */
16494 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16495 || TREE_CODE (templ) == OVERLOAD
16496 || TREE_CODE (templ) == FUNCTION_DECL
16497 || BASELINK_P (templ)));
16498
16499 template_id = lookup_template_function (templ, arguments);
16500 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16501 SET_EXPR_LOCATION (template_id, combined_loc);
16502 }
16503
16504 /* If parsing tentatively, replace the sequence of tokens that makes
16505 up the template-id with a CPP_TEMPLATE_ID token. That way,
16506 should we re-parse the token stream, we will not have to repeat
16507 the effort required to do the parse, nor will we issue duplicate
16508 error messages about problems during instantiation of the
16509 template. */
16510 if (start_of_id
16511 /* Don't do this if we had a parse error in a declarator; re-parsing
16512 might succeed if a name changes meaning (60361). */
16513 && !(cp_parser_error_occurred (parser)
16514 && cp_parser_parsing_tentatively (parser)
16515 && parser->in_declarator_p))
16516 {
16517 /* Reset the contents of the START_OF_ID token. */
16518 token->type = CPP_TEMPLATE_ID;
16519 token->location = combined_loc;
16520
16521 /* Retrieve any deferred checks. Do not pop this access checks yet
16522 so the memory will not be reclaimed during token replacing below. */
16523 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16524 token->u.tree_check_value->value = template_id;
16525 token->u.tree_check_value->checks = get_deferred_access_checks ();
16526 token->keyword = RID_MAX;
16527
16528 /* Purge all subsequent tokens. */
16529 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16530
16531 /* ??? Can we actually assume that, if template_id ==
16532 error_mark_node, we will have issued a diagnostic to the
16533 user, as opposed to simply marking the tentative parse as
16534 failed? */
16535 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16536 error_at (token->location, "parse error in template argument list");
16537 }
16538
16539 pop_to_parent_deferring_access_checks ();
16540 return template_id;
16541 }
16542
16543 /* Parse a template-name.
16544
16545 template-name:
16546 identifier
16547
16548 The standard should actually say:
16549
16550 template-name:
16551 identifier
16552 operator-function-id
16553
16554 A defect report has been filed about this issue.
16555
16556 A conversion-function-id cannot be a template name because they cannot
16557 be part of a template-id. In fact, looking at this code:
16558
16559 a.operator K<int>()
16560
16561 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16562 It is impossible to call a templated conversion-function-id with an
16563 explicit argument list, since the only allowed template parameter is
16564 the type to which it is converting.
16565
16566 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16567 `template' keyword, in a construction like:
16568
16569 T::template f<3>()
16570
16571 In that case `f' is taken to be a template-name, even though there
16572 is no way of knowing for sure.
16573
16574 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16575 name refers to a set of overloaded functions, at least one of which
16576 is a template, or an IDENTIFIER_NODE with the name of the template,
16577 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16578 names are looked up inside uninstantiated templates. */
16579
16580 static tree
16581 cp_parser_template_name (cp_parser* parser,
16582 bool template_keyword_p,
16583 bool check_dependency_p,
16584 bool is_declaration,
16585 enum tag_types tag_type,
16586 bool *is_identifier)
16587 {
16588 tree identifier;
16589 tree decl;
16590 cp_token *token = cp_lexer_peek_token (parser->lexer);
16591
16592 /* If the next token is `operator', then we have either an
16593 operator-function-id or a conversion-function-id. */
16594 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16595 {
16596 /* We don't know whether we're looking at an
16597 operator-function-id or a conversion-function-id. */
16598 cp_parser_parse_tentatively (parser);
16599 /* Try an operator-function-id. */
16600 identifier = cp_parser_operator_function_id (parser);
16601 /* If that didn't work, try a conversion-function-id. */
16602 if (!cp_parser_parse_definitely (parser))
16603 {
16604 cp_parser_error (parser, "expected template-name");
16605 return error_mark_node;
16606 }
16607 }
16608 /* Look for the identifier. */
16609 else
16610 identifier = cp_parser_identifier (parser);
16611
16612 /* If we didn't find an identifier, we don't have a template-id. */
16613 if (identifier == error_mark_node)
16614 return error_mark_node;
16615
16616 /* If the name immediately followed the `template' keyword, then it
16617 is a template-name. However, if the next token is not `<', then
16618 we do not treat it as a template-name, since it is not being used
16619 as part of a template-id. This enables us to handle constructs
16620 like:
16621
16622 template <typename T> struct S { S(); };
16623 template <typename T> S<T>::S();
16624
16625 correctly. We would treat `S' as a template -- if it were `S<T>'
16626 -- but we do not if there is no `<'. */
16627
16628 if (processing_template_decl
16629 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16630 {
16631 /* In a declaration, in a dependent context, we pretend that the
16632 "template" keyword was present in order to improve error
16633 recovery. For example, given:
16634
16635 template <typename T> void f(T::X<int>);
16636
16637 we want to treat "X<int>" as a template-id. */
16638 if (is_declaration
16639 && !template_keyword_p
16640 && parser->scope && TYPE_P (parser->scope)
16641 && check_dependency_p
16642 && dependent_scope_p (parser->scope)
16643 /* Do not do this for dtors (or ctors), since they never
16644 need the template keyword before their name. */
16645 && !constructor_name_p (identifier, parser->scope))
16646 {
16647 cp_token_position start = 0;
16648
16649 /* Explain what went wrong. */
16650 error_at (token->location, "non-template %qD used as template",
16651 identifier);
16652 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16653 parser->scope, identifier);
16654 /* If parsing tentatively, find the location of the "<" token. */
16655 if (cp_parser_simulate_error (parser))
16656 start = cp_lexer_token_position (parser->lexer, true);
16657 /* Parse the template arguments so that we can issue error
16658 messages about them. */
16659 cp_lexer_consume_token (parser->lexer);
16660 cp_parser_enclosed_template_argument_list (parser);
16661 /* Skip tokens until we find a good place from which to
16662 continue parsing. */
16663 cp_parser_skip_to_closing_parenthesis (parser,
16664 /*recovering=*/true,
16665 /*or_comma=*/true,
16666 /*consume_paren=*/false);
16667 /* If parsing tentatively, permanently remove the
16668 template argument list. That will prevent duplicate
16669 error messages from being issued about the missing
16670 "template" keyword. */
16671 if (start)
16672 cp_lexer_purge_tokens_after (parser->lexer, start);
16673 if (is_identifier)
16674 *is_identifier = true;
16675 parser->context->object_type = NULL_TREE;
16676 return identifier;
16677 }
16678
16679 /* If the "template" keyword is present, then there is generally
16680 no point in doing name-lookup, so we just return IDENTIFIER.
16681 But, if the qualifying scope is non-dependent then we can
16682 (and must) do name-lookup normally. */
16683 if (template_keyword_p)
16684 {
16685 tree scope = (parser->scope ? parser->scope
16686 : parser->context->object_type);
16687 if (scope && TYPE_P (scope)
16688 && (!CLASS_TYPE_P (scope)
16689 || (check_dependency_p && dependent_type_p (scope))))
16690 {
16691 /* We're optimizing away the call to cp_parser_lookup_name, but
16692 we still need to do this. */
16693 parser->context->object_type = NULL_TREE;
16694 return identifier;
16695 }
16696 }
16697 }
16698
16699 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16700 const bool scoped_p = ((parser->scope ? parser->scope
16701 : parser->context->object_type) != NULL_TREE);
16702
16703 /* Look up the name. */
16704 decl = cp_parser_lookup_name (parser, identifier,
16705 tag_type,
16706 /*is_template=*/true,
16707 /*is_namespace=*/false,
16708 check_dependency_p,
16709 /*ambiguous_decls=*/NULL,
16710 token->location);
16711
16712 decl = strip_using_decl (decl);
16713
16714 /* If DECL is a template, then the name was a template-name. */
16715 if (TREE_CODE (decl) == TEMPLATE_DECL)
16716 {
16717 if (TREE_DEPRECATED (decl)
16718 && deprecated_state != DEPRECATED_SUPPRESS)
16719 warn_deprecated_use (decl, NULL_TREE);
16720 }
16721 else
16722 {
16723 /* The standard does not explicitly indicate whether a name that
16724 names a set of overloaded declarations, some of which are
16725 templates, is a template-name. However, such a name should
16726 be a template-name; otherwise, there is no way to form a
16727 template-id for the overloaded templates. */
16728 bool found = false;
16729
16730 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16731 !found && iter; ++iter)
16732 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16733 found = true;
16734
16735 if (!found
16736 && (cxx_dialect > cxx17)
16737 && !scoped_p
16738 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
16739 && tag_type == none_type)
16740 {
16741 /* [temp.names] says "A name is also considered to refer to a template
16742 if it is an unqualified-id followed by a < and name lookup finds
16743 either one or more functions or finds nothing." */
16744
16745 /* The "more functions" case. Just use the OVERLOAD as normally.
16746 We don't use is_overloaded_fn here to avoid considering
16747 BASELINKs. */
16748 if (TREE_CODE (decl) == OVERLOAD
16749 /* Name lookup found one function. */
16750 || TREE_CODE (decl) == FUNCTION_DECL)
16751 found = true;
16752 /* Name lookup found nothing. */
16753 else if (decl == error_mark_node)
16754 return identifier;
16755 }
16756
16757 if (!found)
16758 {
16759 /* The name does not name a template. */
16760 cp_parser_error (parser, "expected template-name");
16761 return error_mark_node;
16762 }
16763 }
16764
16765 return decl;
16766 }
16767
16768 /* Parse a template-argument-list.
16769
16770 template-argument-list:
16771 template-argument ... [opt]
16772 template-argument-list , template-argument ... [opt]
16773
16774 Returns a TREE_VEC containing the arguments. */
16775
16776 static tree
16777 cp_parser_template_argument_list (cp_parser* parser)
16778 {
16779 tree fixed_args[10];
16780 unsigned n_args = 0;
16781 unsigned alloced = 10;
16782 tree *arg_ary = fixed_args;
16783 tree vec;
16784 bool saved_in_template_argument_list_p;
16785 bool saved_ice_p;
16786 bool saved_non_ice_p;
16787
16788 /* Don't create location wrapper nodes within a template-argument-list. */
16789 auto_suppress_location_wrappers sentinel;
16790
16791 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16792 parser->in_template_argument_list_p = true;
16793 /* Even if the template-id appears in an integral
16794 constant-expression, the contents of the argument list do
16795 not. */
16796 saved_ice_p = parser->integral_constant_expression_p;
16797 parser->integral_constant_expression_p = false;
16798 saved_non_ice_p = parser->non_integral_constant_expression_p;
16799 parser->non_integral_constant_expression_p = false;
16800
16801 /* Parse the arguments. */
16802 do
16803 {
16804 tree argument;
16805
16806 if (n_args)
16807 /* Consume the comma. */
16808 cp_lexer_consume_token (parser->lexer);
16809
16810 /* Parse the template-argument. */
16811 argument = cp_parser_template_argument (parser);
16812
16813 /* If the next token is an ellipsis, we're expanding a template
16814 argument pack. */
16815 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16816 {
16817 if (argument == error_mark_node)
16818 {
16819 cp_token *token = cp_lexer_peek_token (parser->lexer);
16820 error_at (token->location,
16821 "expected parameter pack before %<...%>");
16822 }
16823 /* Consume the `...' token. */
16824 cp_lexer_consume_token (parser->lexer);
16825
16826 /* Make the argument into a TYPE_PACK_EXPANSION or
16827 EXPR_PACK_EXPANSION. */
16828 argument = make_pack_expansion (argument);
16829 }
16830
16831 if (n_args == alloced)
16832 {
16833 alloced *= 2;
16834
16835 if (arg_ary == fixed_args)
16836 {
16837 arg_ary = XNEWVEC (tree, alloced);
16838 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16839 }
16840 else
16841 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16842 }
16843 arg_ary[n_args++] = argument;
16844 }
16845 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16846
16847 vec = make_tree_vec (n_args);
16848
16849 while (n_args--)
16850 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16851
16852 if (arg_ary != fixed_args)
16853 free (arg_ary);
16854 parser->non_integral_constant_expression_p = saved_non_ice_p;
16855 parser->integral_constant_expression_p = saved_ice_p;
16856 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16857 if (CHECKING_P)
16858 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16859 return vec;
16860 }
16861
16862 /* Parse a template-argument.
16863
16864 template-argument:
16865 assignment-expression
16866 type-id
16867 id-expression
16868
16869 The representation is that of an assignment-expression, type-id, or
16870 id-expression -- except that the qualified id-expression is
16871 evaluated, so that the value returned is either a DECL or an
16872 OVERLOAD.
16873
16874 Although the standard says "assignment-expression", it forbids
16875 throw-expressions or assignments in the template argument.
16876 Therefore, we use "conditional-expression" instead. */
16877
16878 static tree
16879 cp_parser_template_argument (cp_parser* parser)
16880 {
16881 tree argument;
16882 bool template_p;
16883 bool address_p;
16884 bool maybe_type_id = false;
16885 cp_token *token = NULL, *argument_start_token = NULL;
16886 location_t loc = 0;
16887 cp_id_kind idk;
16888
16889 /* There's really no way to know what we're looking at, so we just
16890 try each alternative in order.
16891
16892 [temp.arg]
16893
16894 In a template-argument, an ambiguity between a type-id and an
16895 expression is resolved to a type-id, regardless of the form of
16896 the corresponding template-parameter.
16897
16898 Therefore, we try a type-id first. */
16899 cp_parser_parse_tentatively (parser);
16900 argument = cp_parser_template_type_arg (parser);
16901 /* If there was no error parsing the type-id but the next token is a
16902 '>>', our behavior depends on which dialect of C++ we're
16903 parsing. In C++98, we probably found a typo for '> >'. But there
16904 are type-id which are also valid expressions. For instance:
16905
16906 struct X { int operator >> (int); };
16907 template <int V> struct Foo {};
16908 Foo<X () >> 5> r;
16909
16910 Here 'X()' is a valid type-id of a function type, but the user just
16911 wanted to write the expression "X() >> 5". Thus, we remember that we
16912 found a valid type-id, but we still try to parse the argument as an
16913 expression to see what happens.
16914
16915 In C++0x, the '>>' will be considered two separate '>'
16916 tokens. */
16917 if (!cp_parser_error_occurred (parser)
16918 && cxx_dialect == cxx98
16919 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16920 {
16921 maybe_type_id = true;
16922 cp_parser_abort_tentative_parse (parser);
16923 }
16924 else
16925 {
16926 /* If the next token isn't a `,' or a `>', then this argument wasn't
16927 really finished. This means that the argument is not a valid
16928 type-id. */
16929 if (!cp_parser_next_token_ends_template_argument_p (parser))
16930 cp_parser_error (parser, "expected template-argument");
16931 /* If that worked, we're done. */
16932 if (cp_parser_parse_definitely (parser))
16933 return argument;
16934 }
16935 /* We're still not sure what the argument will be. */
16936 cp_parser_parse_tentatively (parser);
16937 /* Try a template. */
16938 argument_start_token = cp_lexer_peek_token (parser->lexer);
16939 argument = cp_parser_id_expression (parser,
16940 /*template_keyword_p=*/false,
16941 /*check_dependency_p=*/true,
16942 &template_p,
16943 /*declarator_p=*/false,
16944 /*optional_p=*/false);
16945 /* If the next token isn't a `,' or a `>', then this argument wasn't
16946 really finished. */
16947 if (!cp_parser_next_token_ends_template_argument_p (parser))
16948 cp_parser_error (parser, "expected template-argument");
16949 if (!cp_parser_error_occurred (parser))
16950 {
16951 /* Figure out what is being referred to. If the id-expression
16952 was for a class template specialization, then we will have a
16953 TYPE_DECL at this point. There is no need to do name lookup
16954 at this point in that case. */
16955 if (TREE_CODE (argument) != TYPE_DECL)
16956 argument = cp_parser_lookup_name (parser, argument,
16957 none_type,
16958 /*is_template=*/template_p,
16959 /*is_namespace=*/false,
16960 /*check_dependency=*/true,
16961 /*ambiguous_decls=*/NULL,
16962 argument_start_token->location);
16963 /* Handle a constrained-type-specifier for a non-type template
16964 parameter. */
16965 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16966 argument = decl;
16967 else if (TREE_CODE (argument) != TEMPLATE_DECL
16968 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16969 cp_parser_error (parser, "expected template-name");
16970 }
16971 if (cp_parser_parse_definitely (parser))
16972 {
16973 if (TREE_DEPRECATED (argument))
16974 warn_deprecated_use (argument, NULL_TREE);
16975 return argument;
16976 }
16977 /* It must be a non-type argument. In C++17 any constant-expression is
16978 allowed. */
16979 if (cxx_dialect > cxx14)
16980 goto general_expr;
16981
16982 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16983
16984 -- an integral constant-expression of integral or enumeration
16985 type; or
16986
16987 -- the name of a non-type template-parameter; or
16988
16989 -- the name of an object or function with external linkage...
16990
16991 -- the address of an object or function with external linkage...
16992
16993 -- a pointer to member... */
16994 /* Look for a non-type template parameter. */
16995 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16996 {
16997 cp_parser_parse_tentatively (parser);
16998 argument = cp_parser_primary_expression (parser,
16999 /*address_p=*/false,
17000 /*cast_p=*/false,
17001 /*template_arg_p=*/true,
17002 &idk);
17003 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
17004 || !cp_parser_next_token_ends_template_argument_p (parser))
17005 cp_parser_simulate_error (parser);
17006 if (cp_parser_parse_definitely (parser))
17007 return argument;
17008 }
17009
17010 /* If the next token is "&", the argument must be the address of an
17011 object or function with external linkage. */
17012 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
17013 if (address_p)
17014 {
17015 loc = cp_lexer_peek_token (parser->lexer)->location;
17016 cp_lexer_consume_token (parser->lexer);
17017 }
17018 /* See if we might have an id-expression. */
17019 token = cp_lexer_peek_token (parser->lexer);
17020 if (token->type == CPP_NAME
17021 || token->keyword == RID_OPERATOR
17022 || token->type == CPP_SCOPE
17023 || token->type == CPP_TEMPLATE_ID
17024 || token->type == CPP_NESTED_NAME_SPECIFIER)
17025 {
17026 cp_parser_parse_tentatively (parser);
17027 argument = cp_parser_primary_expression (parser,
17028 address_p,
17029 /*cast_p=*/false,
17030 /*template_arg_p=*/true,
17031 &idk);
17032 if (cp_parser_error_occurred (parser)
17033 || !cp_parser_next_token_ends_template_argument_p (parser))
17034 cp_parser_abort_tentative_parse (parser);
17035 else
17036 {
17037 tree probe;
17038
17039 if (INDIRECT_REF_P (argument))
17040 {
17041 /* Strip the dereference temporarily. */
17042 gcc_assert (REFERENCE_REF_P (argument));
17043 argument = TREE_OPERAND (argument, 0);
17044 }
17045
17046 /* If we're in a template, we represent a qualified-id referring
17047 to a static data member as a SCOPE_REF even if the scope isn't
17048 dependent so that we can check access control later. */
17049 probe = argument;
17050 if (TREE_CODE (probe) == SCOPE_REF)
17051 probe = TREE_OPERAND (probe, 1);
17052 if (VAR_P (probe))
17053 {
17054 /* A variable without external linkage might still be a
17055 valid constant-expression, so no error is issued here
17056 if the external-linkage check fails. */
17057 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
17058 cp_parser_simulate_error (parser);
17059 }
17060 else if (is_overloaded_fn (argument))
17061 /* All overloaded functions are allowed; if the external
17062 linkage test does not pass, an error will be issued
17063 later. */
17064 ;
17065 else if (address_p
17066 && (TREE_CODE (argument) == OFFSET_REF
17067 || TREE_CODE (argument) == SCOPE_REF))
17068 /* A pointer-to-member. */
17069 ;
17070 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
17071 ;
17072 else
17073 cp_parser_simulate_error (parser);
17074
17075 if (cp_parser_parse_definitely (parser))
17076 {
17077 if (address_p)
17078 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
17079 tf_warning_or_error);
17080 else
17081 argument = convert_from_reference (argument);
17082 return argument;
17083 }
17084 }
17085 }
17086 /* If the argument started with "&", there are no other valid
17087 alternatives at this point. */
17088 if (address_p)
17089 {
17090 cp_parser_error (parser, "invalid non-type template argument");
17091 return error_mark_node;
17092 }
17093
17094 general_expr:
17095 /* If the argument wasn't successfully parsed as a type-id followed
17096 by '>>', the argument can only be a constant expression now.
17097 Otherwise, we try parsing the constant-expression tentatively,
17098 because the argument could really be a type-id. */
17099 if (maybe_type_id)
17100 cp_parser_parse_tentatively (parser);
17101
17102 if (cxx_dialect <= cxx14)
17103 argument = cp_parser_constant_expression (parser);
17104 else
17105 {
17106 /* In C++20, we can encounter a braced-init-list. */
17107 if (cxx_dialect >= cxx2a
17108 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17109 {
17110 bool expr_non_constant_p;
17111 return cp_parser_braced_list (parser, &expr_non_constant_p);
17112 }
17113
17114 /* With C++17 generalized non-type template arguments we need to handle
17115 lvalue constant expressions, too. */
17116 argument = cp_parser_assignment_expression (parser);
17117 require_potential_constant_expression (argument);
17118 }
17119
17120 if (!maybe_type_id)
17121 return argument;
17122 if (!cp_parser_next_token_ends_template_argument_p (parser))
17123 cp_parser_error (parser, "expected template-argument");
17124 if (cp_parser_parse_definitely (parser))
17125 return argument;
17126 /* We did our best to parse the argument as a non type-id, but that
17127 was the only alternative that matched (albeit with a '>' after
17128 it). We can assume it's just a typo from the user, and a
17129 diagnostic will then be issued. */
17130 return cp_parser_template_type_arg (parser);
17131 }
17132
17133 /* Parse an explicit-instantiation.
17134
17135 explicit-instantiation:
17136 template declaration
17137
17138 Although the standard says `declaration', what it really means is:
17139
17140 explicit-instantiation:
17141 template decl-specifier-seq [opt] declarator [opt] ;
17142
17143 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17144 supposed to be allowed. A defect report has been filed about this
17145 issue.
17146
17147 GNU Extension:
17148
17149 explicit-instantiation:
17150 storage-class-specifier template
17151 decl-specifier-seq [opt] declarator [opt] ;
17152 function-specifier template
17153 decl-specifier-seq [opt] declarator [opt] ; */
17154
17155 static void
17156 cp_parser_explicit_instantiation (cp_parser* parser)
17157 {
17158 int declares_class_or_enum;
17159 cp_decl_specifier_seq decl_specifiers;
17160 tree extension_specifier = NULL_TREE;
17161
17162 timevar_push (TV_TEMPLATE_INST);
17163
17164 /* Look for an (optional) storage-class-specifier or
17165 function-specifier. */
17166 if (cp_parser_allow_gnu_extensions_p (parser))
17167 {
17168 extension_specifier
17169 = cp_parser_storage_class_specifier_opt (parser);
17170 if (!extension_specifier)
17171 extension_specifier
17172 = cp_parser_function_specifier_opt (parser,
17173 /*decl_specs=*/NULL);
17174 }
17175
17176 /* Look for the `template' keyword. */
17177 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17178 /* Let the front end know that we are processing an explicit
17179 instantiation. */
17180 begin_explicit_instantiation ();
17181 /* [temp.explicit] says that we are supposed to ignore access
17182 control while processing explicit instantiation directives. */
17183 push_deferring_access_checks (dk_no_check);
17184 /* Parse a decl-specifier-seq. */
17185 cp_parser_decl_specifier_seq (parser,
17186 CP_PARSER_FLAGS_OPTIONAL,
17187 &decl_specifiers,
17188 &declares_class_or_enum);
17189 /* If there was exactly one decl-specifier, and it declared a class,
17190 and there's no declarator, then we have an explicit type
17191 instantiation. */
17192 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17193 {
17194 tree type;
17195
17196 type = check_tag_decl (&decl_specifiers,
17197 /*explicit_type_instantiation_p=*/true);
17198 /* Turn access control back on for names used during
17199 template instantiation. */
17200 pop_deferring_access_checks ();
17201 if (type)
17202 do_type_instantiation (type, extension_specifier,
17203 /*complain=*/tf_error);
17204 }
17205 else
17206 {
17207 cp_declarator *declarator;
17208 tree decl;
17209
17210 /* Parse the declarator. */
17211 declarator
17212 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17213 CP_PARSER_FLAGS_NONE,
17214 /*ctor_dtor_or_conv_p=*/NULL,
17215 /*parenthesized_p=*/NULL,
17216 /*member_p=*/false,
17217 /*friend_p=*/false,
17218 /*static_p=*/false);
17219 if (declares_class_or_enum & 2)
17220 cp_parser_check_for_definition_in_return_type (declarator,
17221 decl_specifiers.type,
17222 decl_specifiers.locations[ds_type_spec]);
17223 if (declarator != cp_error_declarator)
17224 {
17225 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17226 permerror (decl_specifiers.locations[ds_inline],
17227 "explicit instantiation shall not use"
17228 " %<inline%> specifier");
17229 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17230 permerror (decl_specifiers.locations[ds_constexpr],
17231 "explicit instantiation shall not use"
17232 " %<constexpr%> specifier");
17233
17234 decl = grokdeclarator (declarator, &decl_specifiers,
17235 NORMAL, 0, &decl_specifiers.attributes);
17236 /* Turn access control back on for names used during
17237 template instantiation. */
17238 pop_deferring_access_checks ();
17239 /* Do the explicit instantiation. */
17240 do_decl_instantiation (decl, extension_specifier);
17241 }
17242 else
17243 {
17244 pop_deferring_access_checks ();
17245 /* Skip the body of the explicit instantiation. */
17246 cp_parser_skip_to_end_of_statement (parser);
17247 }
17248 }
17249 /* We're done with the instantiation. */
17250 end_explicit_instantiation ();
17251
17252 cp_parser_consume_semicolon_at_end_of_statement (parser);
17253
17254 timevar_pop (TV_TEMPLATE_INST);
17255 }
17256
17257 /* Parse an explicit-specialization.
17258
17259 explicit-specialization:
17260 template < > declaration
17261
17262 Although the standard says `declaration', what it really means is:
17263
17264 explicit-specialization:
17265 template <> decl-specifier [opt] init-declarator [opt] ;
17266 template <> function-definition
17267 template <> explicit-specialization
17268 template <> template-declaration */
17269
17270 static void
17271 cp_parser_explicit_specialization (cp_parser* parser)
17272 {
17273 bool need_lang_pop;
17274 cp_token *token = cp_lexer_peek_token (parser->lexer);
17275
17276 /* Look for the `template' keyword. */
17277 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17278 /* Look for the `<'. */
17279 cp_parser_require (parser, CPP_LESS, RT_LESS);
17280 /* Look for the `>'. */
17281 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17282 /* We have processed another parameter list. */
17283 ++parser->num_template_parameter_lists;
17284 /* [temp]
17285
17286 A template ... explicit specialization ... shall not have C
17287 linkage. */
17288 if (current_lang_name == lang_name_c)
17289 {
17290 error_at (token->location, "template specialization with C linkage");
17291 maybe_show_extern_c_location ();
17292 /* Give it C++ linkage to avoid confusing other parts of the
17293 front end. */
17294 push_lang_context (lang_name_cplusplus);
17295 need_lang_pop = true;
17296 }
17297 else
17298 need_lang_pop = false;
17299 /* Let the front end know that we are beginning a specialization. */
17300 if (!begin_specialization ())
17301 {
17302 end_specialization ();
17303 return;
17304 }
17305
17306 /* If the next keyword is `template', we need to figure out whether
17307 or not we're looking a template-declaration. */
17308 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17309 {
17310 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17311 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17312 cp_parser_template_declaration_after_export (parser,
17313 /*member_p=*/false);
17314 else
17315 cp_parser_explicit_specialization (parser);
17316 }
17317 else
17318 /* Parse the dependent declaration. */
17319 cp_parser_single_declaration (parser,
17320 /*checks=*/NULL,
17321 /*member_p=*/false,
17322 /*explicit_specialization_p=*/true,
17323 /*friend_p=*/NULL);
17324 /* We're done with the specialization. */
17325 end_specialization ();
17326 /* For the erroneous case of a template with C linkage, we pushed an
17327 implicit C++ linkage scope; exit that scope now. */
17328 if (need_lang_pop)
17329 pop_lang_context ();
17330 /* We're done with this parameter list. */
17331 --parser->num_template_parameter_lists;
17332 }
17333
17334 /* Parse a type-specifier.
17335
17336 type-specifier:
17337 simple-type-specifier
17338 class-specifier
17339 enum-specifier
17340 elaborated-type-specifier
17341 cv-qualifier
17342
17343 GNU Extension:
17344
17345 type-specifier:
17346 __complex__
17347
17348 Returns a representation of the type-specifier. For a
17349 class-specifier, enum-specifier, or elaborated-type-specifier, a
17350 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17351
17352 The parser flags FLAGS is used to control type-specifier parsing.
17353
17354 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17355 in a decl-specifier-seq.
17356
17357 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17358 class-specifier, enum-specifier, or elaborated-type-specifier, then
17359 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17360 if a type is declared; 2 if it is defined. Otherwise, it is set to
17361 zero.
17362
17363 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17364 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17365 is set to FALSE. */
17366
17367 static tree
17368 cp_parser_type_specifier (cp_parser* parser,
17369 cp_parser_flags flags,
17370 cp_decl_specifier_seq *decl_specs,
17371 bool is_declaration,
17372 int* declares_class_or_enum,
17373 bool* is_cv_qualifier)
17374 {
17375 tree type_spec = NULL_TREE;
17376 cp_token *token;
17377 enum rid keyword;
17378 cp_decl_spec ds = ds_last;
17379
17380 /* Assume this type-specifier does not declare a new type. */
17381 if (declares_class_or_enum)
17382 *declares_class_or_enum = 0;
17383 /* And that it does not specify a cv-qualifier. */
17384 if (is_cv_qualifier)
17385 *is_cv_qualifier = false;
17386 /* Peek at the next token. */
17387 token = cp_lexer_peek_token (parser->lexer);
17388
17389 /* If we're looking at a keyword, we can use that to guide the
17390 production we choose. */
17391 keyword = token->keyword;
17392 switch (keyword)
17393 {
17394 case RID_ENUM:
17395 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17396 goto elaborated_type_specifier;
17397
17398 /* Look for the enum-specifier. */
17399 type_spec = cp_parser_enum_specifier (parser);
17400 /* If that worked, we're done. */
17401 if (type_spec)
17402 {
17403 if (declares_class_or_enum)
17404 *declares_class_or_enum = 2;
17405 if (decl_specs)
17406 cp_parser_set_decl_spec_type (decl_specs,
17407 type_spec,
17408 token,
17409 /*type_definition_p=*/true);
17410 return type_spec;
17411 }
17412 else
17413 goto elaborated_type_specifier;
17414
17415 /* Any of these indicate either a class-specifier, or an
17416 elaborated-type-specifier. */
17417 case RID_CLASS:
17418 case RID_STRUCT:
17419 case RID_UNION:
17420 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17421 goto elaborated_type_specifier;
17422
17423 /* Parse tentatively so that we can back up if we don't find a
17424 class-specifier. */
17425 cp_parser_parse_tentatively (parser);
17426 /* Look for the class-specifier. */
17427 type_spec = cp_parser_class_specifier (parser);
17428 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17429 /* If that worked, we're done. */
17430 if (cp_parser_parse_definitely (parser))
17431 {
17432 if (declares_class_or_enum)
17433 *declares_class_or_enum = 2;
17434 if (decl_specs)
17435 cp_parser_set_decl_spec_type (decl_specs,
17436 type_spec,
17437 token,
17438 /*type_definition_p=*/true);
17439 return type_spec;
17440 }
17441
17442 /* Fall through. */
17443 elaborated_type_specifier:
17444 /* We're declaring (not defining) a class or enum. */
17445 if (declares_class_or_enum)
17446 *declares_class_or_enum = 1;
17447
17448 /* Fall through. */
17449 case RID_TYPENAME:
17450 /* Look for an elaborated-type-specifier. */
17451 type_spec
17452 = (cp_parser_elaborated_type_specifier
17453 (parser,
17454 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17455 is_declaration));
17456 if (decl_specs)
17457 cp_parser_set_decl_spec_type (decl_specs,
17458 type_spec,
17459 token,
17460 /*type_definition_p=*/false);
17461 return type_spec;
17462
17463 case RID_CONST:
17464 ds = ds_const;
17465 if (is_cv_qualifier)
17466 *is_cv_qualifier = true;
17467 break;
17468
17469 case RID_VOLATILE:
17470 ds = ds_volatile;
17471 if (is_cv_qualifier)
17472 *is_cv_qualifier = true;
17473 break;
17474
17475 case RID_RESTRICT:
17476 ds = ds_restrict;
17477 if (is_cv_qualifier)
17478 *is_cv_qualifier = true;
17479 break;
17480
17481 case RID_COMPLEX:
17482 /* The `__complex__' keyword is a GNU extension. */
17483 ds = ds_complex;
17484 break;
17485
17486 default:
17487 break;
17488 }
17489
17490 /* Handle simple keywords. */
17491 if (ds != ds_last)
17492 {
17493 if (decl_specs)
17494 {
17495 set_and_check_decl_spec_loc (decl_specs, ds, token);
17496 decl_specs->any_specifiers_p = true;
17497 }
17498 return cp_lexer_consume_token (parser->lexer)->u.value;
17499 }
17500
17501 /* If we do not already have a type-specifier, assume we are looking
17502 at a simple-type-specifier. */
17503 type_spec = cp_parser_simple_type_specifier (parser,
17504 decl_specs,
17505 flags);
17506
17507 /* If we didn't find a type-specifier, and a type-specifier was not
17508 optional in this context, issue an error message. */
17509 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17510 {
17511 cp_parser_error (parser, "expected type specifier");
17512 return error_mark_node;
17513 }
17514
17515 return type_spec;
17516 }
17517
17518 /* Parse a simple-type-specifier.
17519
17520 simple-type-specifier:
17521 :: [opt] nested-name-specifier [opt] type-name
17522 :: [opt] nested-name-specifier template template-id
17523 char
17524 wchar_t
17525 bool
17526 short
17527 int
17528 long
17529 signed
17530 unsigned
17531 float
17532 double
17533 void
17534
17535 C++11 Extension:
17536
17537 simple-type-specifier:
17538 auto
17539 decltype ( expression )
17540 char16_t
17541 char32_t
17542 __underlying_type ( type-id )
17543
17544 C++17 extension:
17545
17546 nested-name-specifier(opt) template-name
17547
17548 GNU Extension:
17549
17550 simple-type-specifier:
17551 __int128
17552 __typeof__ unary-expression
17553 __typeof__ ( type-id )
17554 __typeof__ ( type-id ) { initializer-list , [opt] }
17555
17556 Concepts Extension:
17557
17558 simple-type-specifier:
17559 constrained-type-specifier
17560
17561 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17562 appropriately updated. */
17563
17564 static tree
17565 cp_parser_simple_type_specifier (cp_parser* parser,
17566 cp_decl_specifier_seq *decl_specs,
17567 cp_parser_flags flags)
17568 {
17569 tree type = NULL_TREE;
17570 cp_token *token;
17571 int idx;
17572
17573 /* Peek at the next token. */
17574 token = cp_lexer_peek_token (parser->lexer);
17575
17576 /* If we're looking at a keyword, things are easy. */
17577 switch (token->keyword)
17578 {
17579 case RID_CHAR:
17580 if (decl_specs)
17581 decl_specs->explicit_char_p = true;
17582 type = char_type_node;
17583 break;
17584 case RID_CHAR8:
17585 type = char8_type_node;
17586 break;
17587 case RID_CHAR16:
17588 type = char16_type_node;
17589 break;
17590 case RID_CHAR32:
17591 type = char32_type_node;
17592 break;
17593 case RID_WCHAR:
17594 type = wchar_type_node;
17595 break;
17596 case RID_BOOL:
17597 type = boolean_type_node;
17598 break;
17599 case RID_SHORT:
17600 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17601 type = short_integer_type_node;
17602 break;
17603 case RID_INT:
17604 if (decl_specs)
17605 decl_specs->explicit_int_p = true;
17606 type = integer_type_node;
17607 break;
17608 case RID_INT_N_0:
17609 case RID_INT_N_1:
17610 case RID_INT_N_2:
17611 case RID_INT_N_3:
17612 idx = token->keyword - RID_INT_N_0;
17613 if (! int_n_enabled_p [idx])
17614 break;
17615 if (decl_specs)
17616 {
17617 decl_specs->explicit_intN_p = true;
17618 decl_specs->int_n_idx = idx;
17619 }
17620 type = int_n_trees [idx].signed_type;
17621 break;
17622 case RID_LONG:
17623 if (decl_specs)
17624 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17625 type = long_integer_type_node;
17626 break;
17627 case RID_SIGNED:
17628 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17629 type = integer_type_node;
17630 break;
17631 case RID_UNSIGNED:
17632 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17633 type = unsigned_type_node;
17634 break;
17635 case RID_FLOAT:
17636 type = float_type_node;
17637 break;
17638 case RID_DOUBLE:
17639 type = double_type_node;
17640 break;
17641 case RID_VOID:
17642 type = void_type_node;
17643 break;
17644
17645 case RID_AUTO:
17646 maybe_warn_cpp0x (CPP0X_AUTO);
17647 if (parser->auto_is_implicit_function_template_parm_p)
17648 {
17649 /* The 'auto' might be the placeholder return type for a function decl
17650 with trailing return type. */
17651 bool have_trailing_return_fn_decl = false;
17652
17653 cp_parser_parse_tentatively (parser);
17654 cp_lexer_consume_token (parser->lexer);
17655 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17656 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17657 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17658 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17659 {
17660 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17661 {
17662 cp_lexer_consume_token (parser->lexer);
17663 cp_parser_skip_to_closing_parenthesis (parser,
17664 /*recovering*/false,
17665 /*or_comma*/false,
17666 /*consume_paren*/true);
17667 continue;
17668 }
17669
17670 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17671 {
17672 have_trailing_return_fn_decl = true;
17673 break;
17674 }
17675
17676 cp_lexer_consume_token (parser->lexer);
17677 }
17678 cp_parser_abort_tentative_parse (parser);
17679
17680 if (have_trailing_return_fn_decl)
17681 {
17682 type = make_auto ();
17683 break;
17684 }
17685
17686 if (cxx_dialect >= cxx14)
17687 {
17688 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17689 type = TREE_TYPE (type);
17690 }
17691 else
17692 type = error_mark_node;
17693
17694 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17695 {
17696 if (cxx_dialect < cxx14)
17697 error_at (token->location,
17698 "use of %<auto%> in lambda parameter declaration "
17699 "only available with "
17700 "%<-std=c++14%> or %<-std=gnu++14%>");
17701 }
17702 else if (cxx_dialect < cxx14)
17703 error_at (token->location,
17704 "use of %<auto%> in parameter declaration "
17705 "only available with "
17706 "%<-std=c++14%> or %<-std=gnu++14%>");
17707 else if (!flag_concepts)
17708 pedwarn (token->location, 0,
17709 "use of %<auto%> in parameter declaration "
17710 "only available with %<-fconcepts%>");
17711 }
17712 else
17713 type = make_auto ();
17714 break;
17715
17716 case RID_DECLTYPE:
17717 /* Since DR 743, decltype can either be a simple-type-specifier by
17718 itself or begin a nested-name-specifier. Parsing it will replace
17719 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17720 handling below decide what to do. */
17721 cp_parser_decltype (parser);
17722 cp_lexer_set_token_position (parser->lexer, token);
17723 break;
17724
17725 case RID_TYPEOF:
17726 /* Consume the `typeof' token. */
17727 cp_lexer_consume_token (parser->lexer);
17728 /* Parse the operand to `typeof'. */
17729 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17730 /* If it is not already a TYPE, take its type. */
17731 if (!TYPE_P (type))
17732 type = finish_typeof (type);
17733
17734 if (decl_specs)
17735 cp_parser_set_decl_spec_type (decl_specs, type,
17736 token,
17737 /*type_definition_p=*/false);
17738
17739 return type;
17740
17741 case RID_UNDERLYING_TYPE:
17742 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17743 if (decl_specs)
17744 cp_parser_set_decl_spec_type (decl_specs, type,
17745 token,
17746 /*type_definition_p=*/false);
17747
17748 return type;
17749
17750 case RID_BASES:
17751 case RID_DIRECT_BASES:
17752 type = cp_parser_trait_expr (parser, token->keyword);
17753 if (decl_specs)
17754 cp_parser_set_decl_spec_type (decl_specs, type,
17755 token,
17756 /*type_definition_p=*/false);
17757 return type;
17758 default:
17759 break;
17760 }
17761
17762 /* If token is an already-parsed decltype not followed by ::,
17763 it's a simple-type-specifier. */
17764 if (token->type == CPP_DECLTYPE
17765 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17766 {
17767 type = saved_checks_value (token->u.tree_check_value);
17768 if (decl_specs)
17769 {
17770 cp_parser_set_decl_spec_type (decl_specs, type,
17771 token,
17772 /*type_definition_p=*/false);
17773 /* Remember that we are handling a decltype in order to
17774 implement the resolution of DR 1510 when the argument
17775 isn't instantiation dependent. */
17776 decl_specs->decltype_p = true;
17777 }
17778 cp_lexer_consume_token (parser->lexer);
17779 return type;
17780 }
17781
17782 /* If the type-specifier was for a built-in type, we're done. */
17783 if (type)
17784 {
17785 /* Record the type. */
17786 if (decl_specs
17787 && (token->keyword != RID_SIGNED
17788 && token->keyword != RID_UNSIGNED
17789 && token->keyword != RID_SHORT
17790 && token->keyword != RID_LONG))
17791 cp_parser_set_decl_spec_type (decl_specs,
17792 type,
17793 token,
17794 /*type_definition_p=*/false);
17795 if (decl_specs)
17796 decl_specs->any_specifiers_p = true;
17797
17798 /* Consume the token. */
17799 cp_lexer_consume_token (parser->lexer);
17800
17801 if (type == error_mark_node)
17802 return error_mark_node;
17803
17804 /* There is no valid C++ program where a non-template type is
17805 followed by a "<". That usually indicates that the user thought
17806 that the type was a template. */
17807 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17808 token->location);
17809
17810 return TYPE_NAME (type);
17811 }
17812
17813 /* The type-specifier must be a user-defined type. */
17814 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17815 {
17816 bool qualified_p;
17817 bool global_p;
17818 const bool typename_p = (cxx_dialect >= cxx2a
17819 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
17820
17821 /* Don't gobble tokens or issue error messages if this is an
17822 optional type-specifier. */
17823 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17824 cp_parser_parse_tentatively (parser);
17825
17826 token = cp_lexer_peek_token (parser->lexer);
17827
17828 /* Look for the optional `::' operator. */
17829 global_p
17830 = (cp_parser_global_scope_opt (parser,
17831 /*current_scope_valid_p=*/false)
17832 != NULL_TREE);
17833 /* Look for the nested-name specifier. */
17834 qualified_p
17835 = (cp_parser_nested_name_specifier_opt (parser,
17836 /*typename_keyword_p=*/false,
17837 /*check_dependency_p=*/true,
17838 /*type_p=*/false,
17839 /*is_declaration=*/false)
17840 != NULL_TREE);
17841 /* If we have seen a nested-name-specifier, and the next token
17842 is `template', then we are using the template-id production. */
17843 if (parser->scope
17844 && cp_parser_optional_template_keyword (parser))
17845 {
17846 /* Look for the template-id. */
17847 type = cp_parser_template_id (parser,
17848 /*template_keyword_p=*/true,
17849 /*check_dependency_p=*/true,
17850 none_type,
17851 /*is_declaration=*/false);
17852 /* If the template-id did not name a type, we are out of
17853 luck. */
17854 if (TREE_CODE (type) != TYPE_DECL)
17855 {
17856 /* ...unless we pretend we have seen 'typename'. */
17857 if (typename_p)
17858 type = cp_parser_make_typename_type (parser, type,
17859 token->location);
17860 else
17861 {
17862 cp_parser_error (parser, "expected template-id for type");
17863 type = NULL_TREE;
17864 }
17865 }
17866 }
17867 /* Otherwise, look for a type-name. */
17868 else
17869 type = cp_parser_type_name (parser, (qualified_p && typename_p));
17870
17871 /* Keep track of all name-lookups performed in class scopes. */
17872 if (type
17873 && !global_p
17874 && !qualified_p
17875 && TREE_CODE (type) == TYPE_DECL
17876 && identifier_p (DECL_NAME (type)))
17877 maybe_note_name_used_in_class (DECL_NAME (type), type);
17878 /* If it didn't work out, we don't have a TYPE. */
17879 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17880 && !cp_parser_parse_definitely (parser))
17881 type = NULL_TREE;
17882 if (!type && cxx_dialect >= cxx17)
17883 {
17884 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17885 cp_parser_parse_tentatively (parser);
17886
17887 cp_parser_global_scope_opt (parser,
17888 /*current_scope_valid_p=*/false);
17889 cp_parser_nested_name_specifier_opt (parser,
17890 /*typename_keyword_p=*/false,
17891 /*check_dependency_p=*/true,
17892 /*type_p=*/false,
17893 /*is_declaration=*/false);
17894 tree name = cp_parser_identifier (parser);
17895 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17896 && parser->scope != error_mark_node)
17897 {
17898 tree tmpl = cp_parser_lookup_name (parser, name,
17899 none_type,
17900 /*is_template=*/false,
17901 /*is_namespace=*/false,
17902 /*check_dependency=*/true,
17903 /*ambiguous_decls=*/NULL,
17904 token->location);
17905 if (tmpl && tmpl != error_mark_node
17906 && (DECL_CLASS_TEMPLATE_P (tmpl)
17907 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17908 type = make_template_placeholder (tmpl);
17909 else
17910 {
17911 type = error_mark_node;
17912 if (!cp_parser_simulate_error (parser))
17913 cp_parser_name_lookup_error (parser, name, tmpl,
17914 NLE_TYPE, token->location);
17915 }
17916 }
17917 else
17918 type = error_mark_node;
17919
17920 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17921 && !cp_parser_parse_definitely (parser))
17922 type = NULL_TREE;
17923 }
17924 if (type && decl_specs)
17925 cp_parser_set_decl_spec_type (decl_specs, type,
17926 token,
17927 /*type_definition_p=*/false);
17928 }
17929
17930 /* If we didn't get a type-name, issue an error message. */
17931 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17932 {
17933 cp_parser_error (parser, "expected type-name");
17934 return error_mark_node;
17935 }
17936
17937 if (type && type != error_mark_node)
17938 {
17939 /* See if TYPE is an Objective-C type, and if so, parse and
17940 accept any protocol references following it. Do this before
17941 the cp_parser_check_for_invalid_template_id() call, because
17942 Objective-C types can be followed by '<...>' which would
17943 enclose protocol names rather than template arguments, and so
17944 everything is fine. */
17945 if (c_dialect_objc () && !parser->scope
17946 && (objc_is_id (type) || objc_is_class_name (type)))
17947 {
17948 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17949 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17950
17951 /* Clobber the "unqualified" type previously entered into
17952 DECL_SPECS with the new, improved protocol-qualified version. */
17953 if (decl_specs)
17954 decl_specs->type = qual_type;
17955
17956 return qual_type;
17957 }
17958
17959 /* There is no valid C++ program where a non-template type is
17960 followed by a "<". That usually indicates that the user
17961 thought that the type was a template. */
17962 cp_parser_check_for_invalid_template_id (parser, type,
17963 none_type,
17964 token->location);
17965 }
17966
17967 return type;
17968 }
17969
17970 /* Parse a type-name.
17971
17972 type-name:
17973 class-name
17974 enum-name
17975 typedef-name
17976 simple-template-id [in c++0x]
17977
17978 enum-name:
17979 identifier
17980
17981 typedef-name:
17982 identifier
17983
17984 Concepts:
17985
17986 type-name:
17987 concept-name
17988 partial-concept-id
17989
17990 concept-name:
17991 identifier
17992
17993 Returns a TYPE_DECL for the type. */
17994
17995 static tree
17996 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17997 {
17998 tree type_decl;
17999
18000 /* We can't know yet whether it is a class-name or not. */
18001 cp_parser_parse_tentatively (parser);
18002 /* Try a class-name. */
18003 type_decl = cp_parser_class_name (parser,
18004 typename_keyword_p,
18005 /*template_keyword_p=*/false,
18006 none_type,
18007 /*check_dependency_p=*/true,
18008 /*class_head_p=*/false,
18009 /*is_declaration=*/false);
18010 /* If it's not a class-name, keep looking. */
18011 if (!cp_parser_parse_definitely (parser))
18012 {
18013 if (cxx_dialect < cxx11)
18014 /* It must be a typedef-name or an enum-name. */
18015 return cp_parser_nonclass_name (parser);
18016
18017 cp_parser_parse_tentatively (parser);
18018 /* It is either a simple-template-id representing an
18019 instantiation of an alias template... */
18020 type_decl = cp_parser_template_id (parser,
18021 /*template_keyword_p=*/false,
18022 /*check_dependency_p=*/true,
18023 none_type,
18024 /*is_declaration=*/false);
18025 /* Note that this must be an instantiation of an alias template
18026 because [temp.names]/6 says:
18027
18028 A template-id that names an alias template specialization
18029 is a type-name.
18030
18031 Whereas [temp.names]/7 says:
18032
18033 A simple-template-id that names a class template
18034 specialization is a class-name.
18035
18036 With concepts, this could also be a partial-concept-id that
18037 declares a non-type template parameter. */
18038 if (type_decl != NULL_TREE
18039 && TREE_CODE (type_decl) == TYPE_DECL
18040 && TYPE_DECL_ALIAS_P (type_decl))
18041 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
18042 else if (is_constrained_parameter (type_decl))
18043 /* Don't do anything. */ ;
18044 else
18045 cp_parser_simulate_error (parser);
18046
18047 if (!cp_parser_parse_definitely (parser))
18048 /* ... Or a typedef-name or an enum-name. */
18049 return cp_parser_nonclass_name (parser);
18050 }
18051
18052 return type_decl;
18053 }
18054
18055 /* Check if DECL and ARGS can form a constrained-type-specifier.
18056 If ARGS is non-null, we try to form a concept check of the
18057 form DECL<?, ARGS> where ? is a wildcard that matches any
18058 kind of template argument. If ARGS is NULL, then we try to
18059 form a concept check of the form DECL<?>. */
18060
18061 static tree
18062 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
18063 tree decl, tree args)
18064 {
18065 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
18066
18067 /* If we a constrained-type-specifier cannot be deduced. */
18068 if (parser->prevent_constrained_type_specifiers)
18069 return NULL_TREE;
18070
18071 /* A constrained type specifier can only be found in an
18072 overload set or as a reference to a template declaration.
18073
18074 FIXME: This might be masking a bug. It's possible that
18075 that the deduction below is causing template specializations
18076 to be formed with the wildcard as an argument. */
18077 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
18078 return NULL_TREE;
18079
18080 /* Try to build a call expression that evaluates the
18081 concept. This can fail if the overload set refers
18082 only to non-templates. */
18083 tree placeholder = build_nt (WILDCARD_DECL);
18084 tree check = build_concept_check (decl, placeholder, args);
18085 if (check == error_mark_node)
18086 return NULL_TREE;
18087
18088 /* Deduce the checked constraint and the prototype parameter.
18089
18090 FIXME: In certain cases, failure to deduce should be a
18091 diagnosable error. */
18092 tree conc;
18093 tree proto;
18094 if (!deduce_constrained_parameter (check, conc, proto))
18095 return NULL_TREE;
18096
18097 /* In template parameter scope, this results in a constrained
18098 parameter. Return a descriptor of that parm. */
18099 if (processing_template_parmlist)
18100 return build_constrained_parameter (conc, proto, args);
18101
18102 /* In a parameter-declaration-clause, constrained-type
18103 specifiers result in invented template parameters. */
18104 if (parser->auto_is_implicit_function_template_parm_p)
18105 {
18106 tree x = build_constrained_parameter (conc, proto, args);
18107 return synthesize_implicit_template_parm (parser, x);
18108 }
18109 else
18110 {
18111 /* Otherwise, we're in a context where the constrained
18112 type name is deduced and the constraint applies
18113 after deduction. */
18114 return make_constrained_auto (conc, args);
18115 }
18116
18117 return NULL_TREE;
18118 }
18119
18120 /* If DECL refers to a concept, return a TYPE_DECL representing
18121 the result of using the constrained type specifier in the
18122 current context. DECL refers to a concept if
18123
18124 - it is an overload set containing a function concept taking a single
18125 type argument, or
18126
18127 - it is a variable concept taking a single type argument. */
18128
18129 static tree
18130 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
18131 {
18132 if (flag_concepts
18133 && (TREE_CODE (decl) == OVERLOAD
18134 || BASELINK_P (decl)
18135 || variable_concept_p (decl)))
18136 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
18137 else
18138 return NULL_TREE;
18139 }
18140
18141 /* Check if DECL and ARGS form a partial-concept-id. If so,
18142 assign ID to the resulting constrained placeholder.
18143
18144 Returns true if the partial-concept-id designates a placeholder
18145 and false otherwise. Note that *id is set to NULL_TREE in
18146 this case. */
18147
18148 static tree
18149 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
18150 {
18151 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
18152 }
18153
18154 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18155 or a concept-name.
18156
18157 enum-name:
18158 identifier
18159
18160 typedef-name:
18161 identifier
18162
18163 concept-name:
18164 identifier
18165
18166 Returns a TYPE_DECL for the type. */
18167
18168 static tree
18169 cp_parser_nonclass_name (cp_parser* parser)
18170 {
18171 tree type_decl;
18172 tree identifier;
18173
18174 cp_token *token = cp_lexer_peek_token (parser->lexer);
18175 identifier = cp_parser_identifier (parser);
18176 if (identifier == error_mark_node)
18177 return error_mark_node;
18178
18179 /* Look up the type-name. */
18180 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18181
18182 type_decl = strip_using_decl (type_decl);
18183
18184 /* If we found an overload set, then it may refer to a concept-name. */
18185 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
18186 type_decl = decl;
18187
18188 if (TREE_CODE (type_decl) != TYPE_DECL
18189 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18190 {
18191 /* See if this is an Objective-C type. */
18192 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18193 tree type = objc_get_protocol_qualified_type (identifier, protos);
18194 if (type)
18195 type_decl = TYPE_NAME (type);
18196 }
18197
18198 /* Issue an error if we did not find a type-name. */
18199 if (TREE_CODE (type_decl) != TYPE_DECL
18200 /* In Objective-C, we have the complication that class names are
18201 normally type names and start declarations (eg, the
18202 "NSObject" in "NSObject *object;"), but can be used in an
18203 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18204 is an expression. So, a classname followed by a dot is not a
18205 valid type-name. */
18206 || (objc_is_class_name (TREE_TYPE (type_decl))
18207 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18208 {
18209 if (!cp_parser_simulate_error (parser))
18210 cp_parser_name_lookup_error (parser, identifier, type_decl,
18211 NLE_TYPE, token->location);
18212 return error_mark_node;
18213 }
18214 /* Remember that the name was used in the definition of the
18215 current class so that we can check later to see if the
18216 meaning would have been different after the class was
18217 entirely defined. */
18218 else if (type_decl != error_mark_node
18219 && !parser->scope)
18220 maybe_note_name_used_in_class (identifier, type_decl);
18221
18222 return type_decl;
18223 }
18224
18225 /* Parse an elaborated-type-specifier. Note that the grammar given
18226 here incorporates the resolution to DR68.
18227
18228 elaborated-type-specifier:
18229 class-key :: [opt] nested-name-specifier [opt] identifier
18230 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18231 enum-key :: [opt] nested-name-specifier [opt] identifier
18232 typename :: [opt] nested-name-specifier identifier
18233 typename :: [opt] nested-name-specifier template [opt]
18234 template-id
18235
18236 GNU extension:
18237
18238 elaborated-type-specifier:
18239 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18240 class-key attributes :: [opt] nested-name-specifier [opt]
18241 template [opt] template-id
18242 enum attributes :: [opt] nested-name-specifier [opt] identifier
18243
18244 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18245 declared `friend'. If IS_DECLARATION is TRUE, then this
18246 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18247 something is being declared.
18248
18249 Returns the TYPE specified. */
18250
18251 static tree
18252 cp_parser_elaborated_type_specifier (cp_parser* parser,
18253 bool is_friend,
18254 bool is_declaration)
18255 {
18256 enum tag_types tag_type;
18257 tree identifier;
18258 tree type = NULL_TREE;
18259 tree attributes = NULL_TREE;
18260 tree globalscope;
18261 cp_token *token = NULL;
18262
18263 /* See if we're looking at the `enum' keyword. */
18264 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18265 {
18266 /* Consume the `enum' token. */
18267 cp_lexer_consume_token (parser->lexer);
18268 /* Remember that it's an enumeration type. */
18269 tag_type = enum_type;
18270 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18271 enums) is used here. */
18272 cp_token *token = cp_lexer_peek_token (parser->lexer);
18273 if (cp_parser_is_keyword (token, RID_CLASS)
18274 || cp_parser_is_keyword (token, RID_STRUCT))
18275 {
18276 gcc_rich_location richloc (token->location);
18277 richloc.add_range (input_location);
18278 richloc.add_fixit_remove ();
18279 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18280 "a scoped enum must not use the %qD keyword",
18281 token->u.value);
18282 /* Consume the `struct' or `class' and parse it anyway. */
18283 cp_lexer_consume_token (parser->lexer);
18284 }
18285 /* Parse the attributes. */
18286 attributes = cp_parser_attributes_opt (parser);
18287 }
18288 /* Or, it might be `typename'. */
18289 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18290 RID_TYPENAME))
18291 {
18292 /* Consume the `typename' token. */
18293 cp_lexer_consume_token (parser->lexer);
18294 /* Remember that it's a `typename' type. */
18295 tag_type = typename_type;
18296 }
18297 /* Otherwise it must be a class-key. */
18298 else
18299 {
18300 tag_type = cp_parser_class_key (parser);
18301 if (tag_type == none_type)
18302 return error_mark_node;
18303 /* Parse the attributes. */
18304 attributes = cp_parser_attributes_opt (parser);
18305 }
18306
18307 /* Look for the `::' operator. */
18308 globalscope = cp_parser_global_scope_opt (parser,
18309 /*current_scope_valid_p=*/false);
18310 /* Look for the nested-name-specifier. */
18311 tree nested_name_specifier;
18312 if (tag_type == typename_type && !globalscope)
18313 {
18314 nested_name_specifier
18315 = cp_parser_nested_name_specifier (parser,
18316 /*typename_keyword_p=*/true,
18317 /*check_dependency_p=*/true,
18318 /*type_p=*/true,
18319 is_declaration);
18320 if (!nested_name_specifier)
18321 return error_mark_node;
18322 }
18323 else
18324 /* Even though `typename' is not present, the proposed resolution
18325 to Core Issue 180 says that in `class A<T>::B', `B' should be
18326 considered a type-name, even if `A<T>' is dependent. */
18327 nested_name_specifier
18328 = cp_parser_nested_name_specifier_opt (parser,
18329 /*typename_keyword_p=*/true,
18330 /*check_dependency_p=*/true,
18331 /*type_p=*/true,
18332 is_declaration);
18333 /* For everything but enumeration types, consider a template-id.
18334 For an enumeration type, consider only a plain identifier. */
18335 if (tag_type != enum_type)
18336 {
18337 bool template_p = false;
18338 tree decl;
18339
18340 /* Allow the `template' keyword. */
18341 template_p = cp_parser_optional_template_keyword (parser);
18342 /* If we didn't see `template', we don't know if there's a
18343 template-id or not. */
18344 if (!template_p)
18345 cp_parser_parse_tentatively (parser);
18346 /* The `template' keyword must follow a nested-name-specifier. */
18347 else if (!nested_name_specifier)
18348 {
18349 cp_parser_error (parser, "%<template%> must follow a nested-"
18350 "name-specifier");
18351 return error_mark_node;
18352 }
18353
18354 /* Parse the template-id. */
18355 token = cp_lexer_peek_token (parser->lexer);
18356 decl = cp_parser_template_id (parser, template_p,
18357 /*check_dependency_p=*/true,
18358 tag_type,
18359 is_declaration);
18360 /* If we didn't find a template-id, look for an ordinary
18361 identifier. */
18362 if (!template_p && !cp_parser_parse_definitely (parser))
18363 ;
18364 /* We can get here when cp_parser_template_id, called by
18365 cp_parser_class_name with tag_type == none_type, succeeds
18366 and caches a BASELINK. Then, when called again here,
18367 instead of failing and returning an error_mark_node
18368 returns it (see template/typename17.C in C++11).
18369 ??? Could we diagnose this earlier? */
18370 else if (tag_type == typename_type && BASELINK_P (decl))
18371 {
18372 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18373 type = error_mark_node;
18374 }
18375 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18376 in effect, then we must assume that, upon instantiation, the
18377 template will correspond to a class. */
18378 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18379 && tag_type == typename_type)
18380 type = make_typename_type (parser->scope, decl,
18381 typename_type,
18382 /*complain=*/tf_error);
18383 /* If the `typename' keyword is in effect and DECL is not a type
18384 decl, then type is non existent. */
18385 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18386 ;
18387 else if (TREE_CODE (decl) == TYPE_DECL)
18388 {
18389 type = check_elaborated_type_specifier (tag_type, decl,
18390 /*allow_template_p=*/true);
18391
18392 /* If the next token is a semicolon, this must be a specialization,
18393 instantiation, or friend declaration. Check the scope while we
18394 still know whether or not we had a nested-name-specifier. */
18395 if (type != error_mark_node
18396 && !nested_name_specifier && !is_friend
18397 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18398 check_unqualified_spec_or_inst (type, token->location);
18399 }
18400 else if (decl == error_mark_node)
18401 type = error_mark_node;
18402 }
18403
18404 if (!type)
18405 {
18406 token = cp_lexer_peek_token (parser->lexer);
18407 identifier = cp_parser_identifier (parser);
18408
18409 if (identifier == error_mark_node)
18410 {
18411 parser->scope = NULL_TREE;
18412 return error_mark_node;
18413 }
18414
18415 /* For a `typename', we needn't call xref_tag. */
18416 if (tag_type == typename_type
18417 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18418 return cp_parser_make_typename_type (parser, identifier,
18419 token->location);
18420
18421 /* Template parameter lists apply only if we are not within a
18422 function parameter list. */
18423 bool template_parm_lists_apply
18424 = parser->num_template_parameter_lists;
18425 if (template_parm_lists_apply)
18426 for (cp_binding_level *s = current_binding_level;
18427 s && s->kind != sk_template_parms;
18428 s = s->level_chain)
18429 if (s->kind == sk_function_parms)
18430 template_parm_lists_apply = false;
18431
18432 /* Look up a qualified name in the usual way. */
18433 if (parser->scope)
18434 {
18435 tree decl;
18436 tree ambiguous_decls;
18437
18438 decl = cp_parser_lookup_name (parser, identifier,
18439 tag_type,
18440 /*is_template=*/false,
18441 /*is_namespace=*/false,
18442 /*check_dependency=*/true,
18443 &ambiguous_decls,
18444 token->location);
18445
18446 /* If the lookup was ambiguous, an error will already have been
18447 issued. */
18448 if (ambiguous_decls)
18449 return error_mark_node;
18450
18451 /* If we are parsing friend declaration, DECL may be a
18452 TEMPLATE_DECL tree node here. However, we need to check
18453 whether this TEMPLATE_DECL results in valid code. Consider
18454 the following example:
18455
18456 namespace N {
18457 template <class T> class C {};
18458 }
18459 class X {
18460 template <class T> friend class N::C; // #1, valid code
18461 };
18462 template <class T> class Y {
18463 friend class N::C; // #2, invalid code
18464 };
18465
18466 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18467 name lookup of `N::C'. We see that friend declaration must
18468 be template for the code to be valid. Note that
18469 processing_template_decl does not work here since it is
18470 always 1 for the above two cases. */
18471
18472 decl = (cp_parser_maybe_treat_template_as_class
18473 (decl, /*tag_name_p=*/is_friend
18474 && template_parm_lists_apply));
18475
18476 if (TREE_CODE (decl) != TYPE_DECL)
18477 {
18478 cp_parser_diagnose_invalid_type_name (parser,
18479 identifier,
18480 token->location);
18481 return error_mark_node;
18482 }
18483
18484 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18485 {
18486 bool allow_template = (template_parm_lists_apply
18487 || DECL_SELF_REFERENCE_P (decl));
18488 type = check_elaborated_type_specifier (tag_type, decl,
18489 allow_template);
18490
18491 if (type == error_mark_node)
18492 return error_mark_node;
18493 }
18494
18495 /* Forward declarations of nested types, such as
18496
18497 class C1::C2;
18498 class C1::C2::C3;
18499
18500 are invalid unless all components preceding the final '::'
18501 are complete. If all enclosing types are complete, these
18502 declarations become merely pointless.
18503
18504 Invalid forward declarations of nested types are errors
18505 caught elsewhere in parsing. Those that are pointless arrive
18506 here. */
18507
18508 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18509 && !is_friend && !processing_explicit_instantiation)
18510 warning (0, "declaration %qD does not declare anything", decl);
18511
18512 type = TREE_TYPE (decl);
18513 }
18514 else
18515 {
18516 /* An elaborated-type-specifier sometimes introduces a new type and
18517 sometimes names an existing type. Normally, the rule is that it
18518 introduces a new type only if there is not an existing type of
18519 the same name already in scope. For example, given:
18520
18521 struct S {};
18522 void f() { struct S s; }
18523
18524 the `struct S' in the body of `f' is the same `struct S' as in
18525 the global scope; the existing definition is used. However, if
18526 there were no global declaration, this would introduce a new
18527 local class named `S'.
18528
18529 An exception to this rule applies to the following code:
18530
18531 namespace N { struct S; }
18532
18533 Here, the elaborated-type-specifier names a new type
18534 unconditionally; even if there is already an `S' in the
18535 containing scope this declaration names a new type.
18536 This exception only applies if the elaborated-type-specifier
18537 forms the complete declaration:
18538
18539 [class.name]
18540
18541 A declaration consisting solely of `class-key identifier ;' is
18542 either a redeclaration of the name in the current scope or a
18543 forward declaration of the identifier as a class name. It
18544 introduces the name into the current scope.
18545
18546 We are in this situation precisely when the next token is a `;'.
18547
18548 An exception to the exception is that a `friend' declaration does
18549 *not* name a new type; i.e., given:
18550
18551 struct S { friend struct T; };
18552
18553 `T' is not a new type in the scope of `S'.
18554
18555 Also, `new struct S' or `sizeof (struct S)' never results in the
18556 definition of a new type; a new type can only be declared in a
18557 declaration context. */
18558
18559 tag_scope ts;
18560 bool template_p;
18561
18562 if (is_friend)
18563 /* Friends have special name lookup rules. */
18564 ts = ts_within_enclosing_non_class;
18565 else if (is_declaration
18566 && cp_lexer_next_token_is (parser->lexer,
18567 CPP_SEMICOLON))
18568 /* This is a `class-key identifier ;' */
18569 ts = ts_current;
18570 else
18571 ts = ts_global;
18572
18573 template_p =
18574 (template_parm_lists_apply
18575 && (cp_parser_next_token_starts_class_definition_p (parser)
18576 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18577 /* An unqualified name was used to reference this type, so
18578 there were no qualifying templates. */
18579 if (template_parm_lists_apply
18580 && !cp_parser_check_template_parameters (parser,
18581 /*num_templates=*/0,
18582 /*template_id*/false,
18583 token->location,
18584 /*declarator=*/NULL))
18585 return error_mark_node;
18586 type = xref_tag (tag_type, identifier, ts, template_p);
18587 }
18588 }
18589
18590 if (type == error_mark_node)
18591 return error_mark_node;
18592
18593 /* Allow attributes on forward declarations of classes. */
18594 if (attributes)
18595 {
18596 if (TREE_CODE (type) == TYPENAME_TYPE)
18597 warning (OPT_Wattributes,
18598 "attributes ignored on uninstantiated type");
18599 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18600 && ! processing_explicit_instantiation)
18601 warning (OPT_Wattributes,
18602 "attributes ignored on template instantiation");
18603 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18604 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18605 else
18606 warning (OPT_Wattributes,
18607 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18608 }
18609
18610 if (tag_type != enum_type)
18611 {
18612 /* Indicate whether this class was declared as a `class' or as a
18613 `struct'. */
18614 if (CLASS_TYPE_P (type))
18615 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18616 cp_parser_check_class_key (tag_type, type);
18617 }
18618
18619 /* A "<" cannot follow an elaborated type specifier. If that
18620 happens, the user was probably trying to form a template-id. */
18621 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18622 token->location);
18623
18624 return type;
18625 }
18626
18627 /* Parse an enum-specifier.
18628
18629 enum-specifier:
18630 enum-head { enumerator-list [opt] }
18631 enum-head { enumerator-list , } [C++0x]
18632
18633 enum-head:
18634 enum-key identifier [opt] enum-base [opt]
18635 enum-key nested-name-specifier identifier enum-base [opt]
18636
18637 enum-key:
18638 enum
18639 enum class [C++0x]
18640 enum struct [C++0x]
18641
18642 enum-base: [C++0x]
18643 : type-specifier-seq
18644
18645 opaque-enum-specifier:
18646 enum-key identifier enum-base [opt] ;
18647
18648 GNU Extensions:
18649 enum-key attributes[opt] identifier [opt] enum-base [opt]
18650 { enumerator-list [opt] }attributes[opt]
18651 enum-key attributes[opt] identifier [opt] enum-base [opt]
18652 { enumerator-list, }attributes[opt] [C++0x]
18653
18654 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18655 if the token stream isn't an enum-specifier after all. */
18656
18657 static tree
18658 cp_parser_enum_specifier (cp_parser* parser)
18659 {
18660 tree identifier;
18661 tree type = NULL_TREE;
18662 tree prev_scope;
18663 tree nested_name_specifier = NULL_TREE;
18664 tree attributes;
18665 bool scoped_enum_p = false;
18666 bool has_underlying_type = false;
18667 bool nested_being_defined = false;
18668 bool new_value_list = false;
18669 bool is_new_type = false;
18670 bool is_unnamed = false;
18671 tree underlying_type = NULL_TREE;
18672 cp_token *type_start_token = NULL;
18673 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18674
18675 parser->colon_corrects_to_scope_p = false;
18676
18677 /* Parse tentatively so that we can back up if we don't find a
18678 enum-specifier. */
18679 cp_parser_parse_tentatively (parser);
18680
18681 /* Caller guarantees that the current token is 'enum', an identifier
18682 possibly follows, and the token after that is an opening brace.
18683 If we don't have an identifier, fabricate an anonymous name for
18684 the enumeration being defined. */
18685 cp_lexer_consume_token (parser->lexer);
18686
18687 /* Parse the "class" or "struct", which indicates a scoped
18688 enumeration type in C++0x. */
18689 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18690 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18691 {
18692 if (cxx_dialect < cxx11)
18693 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18694
18695 /* Consume the `struct' or `class' token. */
18696 cp_lexer_consume_token (parser->lexer);
18697
18698 scoped_enum_p = true;
18699 }
18700
18701 attributes = cp_parser_attributes_opt (parser);
18702
18703 /* Clear the qualification. */
18704 parser->scope = NULL_TREE;
18705 parser->qualifying_scope = NULL_TREE;
18706 parser->object_scope = NULL_TREE;
18707
18708 /* Figure out in what scope the declaration is being placed. */
18709 prev_scope = current_scope ();
18710
18711 type_start_token = cp_lexer_peek_token (parser->lexer);
18712
18713 push_deferring_access_checks (dk_no_check);
18714 nested_name_specifier
18715 = cp_parser_nested_name_specifier_opt (parser,
18716 /*typename_keyword_p=*/true,
18717 /*check_dependency_p=*/false,
18718 /*type_p=*/false,
18719 /*is_declaration=*/false);
18720
18721 if (nested_name_specifier)
18722 {
18723 tree name;
18724
18725 identifier = cp_parser_identifier (parser);
18726 name = cp_parser_lookup_name (parser, identifier,
18727 enum_type,
18728 /*is_template=*/false,
18729 /*is_namespace=*/false,
18730 /*check_dependency=*/true,
18731 /*ambiguous_decls=*/NULL,
18732 input_location);
18733 if (name && name != error_mark_node)
18734 {
18735 type = TREE_TYPE (name);
18736 if (TREE_CODE (type) == TYPENAME_TYPE)
18737 {
18738 /* Are template enums allowed in ISO? */
18739 if (template_parm_scope_p ())
18740 pedwarn (type_start_token->location, OPT_Wpedantic,
18741 "%qD is an enumeration template", name);
18742 /* ignore a typename reference, for it will be solved by name
18743 in start_enum. */
18744 type = NULL_TREE;
18745 }
18746 }
18747 else if (nested_name_specifier == error_mark_node)
18748 /* We already issued an error. */;
18749 else
18750 {
18751 error_at (type_start_token->location,
18752 "%qD does not name an enumeration in %qT",
18753 identifier, nested_name_specifier);
18754 nested_name_specifier = error_mark_node;
18755 }
18756 }
18757 else
18758 {
18759 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18760 identifier = cp_parser_identifier (parser);
18761 else
18762 {
18763 identifier = make_anon_name ();
18764 is_unnamed = true;
18765 if (scoped_enum_p)
18766 error_at (type_start_token->location,
18767 "unnamed scoped enum is not allowed");
18768 }
18769 }
18770 pop_deferring_access_checks ();
18771
18772 /* Check for the `:' that denotes a specified underlying type in C++0x.
18773 Note that a ':' could also indicate a bitfield width, however. */
18774 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18775 {
18776 cp_decl_specifier_seq type_specifiers;
18777
18778 /* Consume the `:'. */
18779 cp_lexer_consume_token (parser->lexer);
18780
18781 /* Parse the type-specifier-seq. */
18782 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
18783 /*is_declaration=*/false,
18784 /*is_trailing_return=*/false,
18785 &type_specifiers);
18786
18787 /* At this point this is surely not elaborated type specifier. */
18788 if (!cp_parser_parse_definitely (parser))
18789 return NULL_TREE;
18790
18791 if (cxx_dialect < cxx11)
18792 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18793
18794 has_underlying_type = true;
18795
18796 /* If that didn't work, stop. */
18797 if (type_specifiers.type != error_mark_node)
18798 {
18799 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18800 /*initialized=*/0, NULL);
18801 if (underlying_type == error_mark_node
18802 || check_for_bare_parameter_packs (underlying_type))
18803 underlying_type = NULL_TREE;
18804 }
18805 }
18806
18807 /* Look for the `{' but don't consume it yet. */
18808 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18809 {
18810 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18811 {
18812 cp_parser_error (parser, "expected %<{%>");
18813 if (has_underlying_type)
18814 {
18815 type = NULL_TREE;
18816 goto out;
18817 }
18818 }
18819 /* An opaque-enum-specifier must have a ';' here. */
18820 if ((scoped_enum_p || underlying_type)
18821 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18822 {
18823 cp_parser_error (parser, "expected %<;%> or %<{%>");
18824 if (has_underlying_type)
18825 {
18826 type = NULL_TREE;
18827 goto out;
18828 }
18829 }
18830 }
18831
18832 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18833 return NULL_TREE;
18834
18835 if (nested_name_specifier)
18836 {
18837 if (CLASS_TYPE_P (nested_name_specifier))
18838 {
18839 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18840 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18841 push_scope (nested_name_specifier);
18842 }
18843 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18844 {
18845 push_nested_namespace (nested_name_specifier);
18846 }
18847 }
18848
18849 /* Issue an error message if type-definitions are forbidden here. */
18850 if (!cp_parser_check_type_definition (parser))
18851 type = error_mark_node;
18852 else
18853 /* Create the new type. We do this before consuming the opening
18854 brace so the enum will be recorded as being on the line of its
18855 tag (or the 'enum' keyword, if there is no tag). */
18856 type = start_enum (identifier, type, underlying_type,
18857 attributes, scoped_enum_p, &is_new_type);
18858
18859 /* If the next token is not '{' it is an opaque-enum-specifier or an
18860 elaborated-type-specifier. */
18861 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18862 {
18863 timevar_push (TV_PARSE_ENUM);
18864 if (nested_name_specifier
18865 && nested_name_specifier != error_mark_node)
18866 {
18867 /* The following catches invalid code such as:
18868 enum class S<int>::E { A, B, C }; */
18869 if (!processing_specialization
18870 && CLASS_TYPE_P (nested_name_specifier)
18871 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18872 error_at (type_start_token->location, "cannot add an enumerator "
18873 "list to a template instantiation");
18874
18875 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18876 {
18877 error_at (type_start_token->location,
18878 "%<%T::%E%> has not been declared",
18879 TYPE_CONTEXT (nested_name_specifier),
18880 nested_name_specifier);
18881 type = error_mark_node;
18882 }
18883 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18884 && !CLASS_TYPE_P (nested_name_specifier))
18885 {
18886 error_at (type_start_token->location, "nested name specifier "
18887 "%qT for enum declaration does not name a class "
18888 "or namespace", nested_name_specifier);
18889 type = error_mark_node;
18890 }
18891 /* If that scope does not contain the scope in which the
18892 class was originally declared, the program is invalid. */
18893 else if (prev_scope && !is_ancestor (prev_scope,
18894 nested_name_specifier))
18895 {
18896 if (at_namespace_scope_p ())
18897 error_at (type_start_token->location,
18898 "declaration of %qD in namespace %qD which does not "
18899 "enclose %qD",
18900 type, prev_scope, nested_name_specifier);
18901 else
18902 error_at (type_start_token->location,
18903 "declaration of %qD in %qD which does not "
18904 "enclose %qD",
18905 type, prev_scope, nested_name_specifier);
18906 type = error_mark_node;
18907 }
18908 /* If that scope is the scope where the declaration is being placed
18909 the program is invalid. */
18910 else if (CLASS_TYPE_P (nested_name_specifier)
18911 && CLASS_TYPE_P (prev_scope)
18912 && same_type_p (nested_name_specifier, prev_scope))
18913 {
18914 permerror (type_start_token->location,
18915 "extra qualification not allowed");
18916 nested_name_specifier = NULL_TREE;
18917 }
18918 }
18919
18920 if (scoped_enum_p)
18921 begin_scope (sk_scoped_enum, type);
18922
18923 /* Consume the opening brace. */
18924 matching_braces braces;
18925 braces.consume_open (parser);
18926
18927 if (type == error_mark_node)
18928 ; /* Nothing to add */
18929 else if (OPAQUE_ENUM_P (type)
18930 || (cxx_dialect > cxx98 && processing_specialization))
18931 {
18932 new_value_list = true;
18933 SET_OPAQUE_ENUM_P (type, false);
18934 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18935 }
18936 else
18937 {
18938 error_at (type_start_token->location,
18939 "multiple definition of %q#T", type);
18940 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18941 "previous definition here");
18942 type = error_mark_node;
18943 }
18944
18945 if (type == error_mark_node)
18946 cp_parser_skip_to_end_of_block_or_statement (parser);
18947 /* If the next token is not '}', then there are some enumerators. */
18948 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18949 {
18950 if (is_unnamed && !scoped_enum_p)
18951 pedwarn (type_start_token->location, OPT_Wpedantic,
18952 "ISO C++ forbids empty unnamed enum");
18953 }
18954 else
18955 cp_parser_enumerator_list (parser, type);
18956
18957 /* Consume the final '}'. */
18958 braces.require_close (parser);
18959
18960 if (scoped_enum_p)
18961 finish_scope ();
18962 timevar_pop (TV_PARSE_ENUM);
18963 }
18964 else
18965 {
18966 /* If a ';' follows, then it is an opaque-enum-specifier
18967 and additional restrictions apply. */
18968 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18969 {
18970 if (is_unnamed)
18971 error_at (type_start_token->location,
18972 "opaque-enum-specifier without name");
18973 else if (nested_name_specifier)
18974 error_at (type_start_token->location,
18975 "opaque-enum-specifier must use a simple identifier");
18976 }
18977 }
18978
18979 /* Look for trailing attributes to apply to this enumeration, and
18980 apply them if appropriate. */
18981 if (cp_parser_allow_gnu_extensions_p (parser))
18982 {
18983 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18984 cplus_decl_attributes (&type,
18985 trailing_attr,
18986 (int) ATTR_FLAG_TYPE_IN_PLACE);
18987 }
18988
18989 /* Finish up the enumeration. */
18990 if (type != error_mark_node)
18991 {
18992 if (new_value_list)
18993 finish_enum_value_list (type);
18994 if (is_new_type)
18995 finish_enum (type);
18996 }
18997
18998 if (nested_name_specifier)
18999 {
19000 if (CLASS_TYPE_P (nested_name_specifier))
19001 {
19002 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
19003 pop_scope (nested_name_specifier);
19004 }
19005 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19006 {
19007 pop_nested_namespace (nested_name_specifier);
19008 }
19009 }
19010 out:
19011 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19012 return type;
19013 }
19014
19015 /* Parse an enumerator-list. The enumerators all have the indicated
19016 TYPE.
19017
19018 enumerator-list:
19019 enumerator-definition
19020 enumerator-list , enumerator-definition */
19021
19022 static void
19023 cp_parser_enumerator_list (cp_parser* parser, tree type)
19024 {
19025 while (true)
19026 {
19027 /* Parse an enumerator-definition. */
19028 cp_parser_enumerator_definition (parser, type);
19029
19030 /* If the next token is not a ',', we've reached the end of
19031 the list. */
19032 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19033 break;
19034 /* Otherwise, consume the `,' and keep going. */
19035 cp_lexer_consume_token (parser->lexer);
19036 /* If the next token is a `}', there is a trailing comma. */
19037 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19038 {
19039 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
19040 pedwarn (input_location, OPT_Wpedantic,
19041 "comma at end of enumerator list");
19042 break;
19043 }
19044 }
19045 }
19046
19047 /* Parse an enumerator-definition. The enumerator has the indicated
19048 TYPE.
19049
19050 enumerator-definition:
19051 enumerator
19052 enumerator = constant-expression
19053
19054 enumerator:
19055 identifier
19056
19057 GNU Extensions:
19058
19059 enumerator-definition:
19060 enumerator attributes [opt]
19061 enumerator attributes [opt] = constant-expression */
19062
19063 static void
19064 cp_parser_enumerator_definition (cp_parser* parser, tree type)
19065 {
19066 tree identifier;
19067 tree value;
19068 location_t loc;
19069
19070 /* Save the input location because we are interested in the location
19071 of the identifier and not the location of the explicit value. */
19072 loc = cp_lexer_peek_token (parser->lexer)->location;
19073
19074 /* Look for the identifier. */
19075 identifier = cp_parser_identifier (parser);
19076 if (identifier == error_mark_node)
19077 return;
19078
19079 /* Parse any specified attributes. */
19080 tree attrs = cp_parser_attributes_opt (parser);
19081
19082 /* If the next token is an '=', then there is an explicit value. */
19083 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19084 {
19085 /* Consume the `=' token. */
19086 cp_lexer_consume_token (parser->lexer);
19087 /* Parse the value. */
19088 value = cp_parser_constant_expression (parser);
19089 }
19090 else
19091 value = NULL_TREE;
19092
19093 /* If we are processing a template, make sure the initializer of the
19094 enumerator doesn't contain any bare template parameter pack. */
19095 if (check_for_bare_parameter_packs (value))
19096 value = error_mark_node;
19097
19098 /* Create the enumerator. */
19099 build_enumerator (identifier, value, type, attrs, loc);
19100 }
19101
19102 /* Parse a namespace-name.
19103
19104 namespace-name:
19105 original-namespace-name
19106 namespace-alias
19107
19108 Returns the NAMESPACE_DECL for the namespace. */
19109
19110 static tree
19111 cp_parser_namespace_name (cp_parser* parser)
19112 {
19113 tree identifier;
19114 tree namespace_decl;
19115
19116 cp_token *token = cp_lexer_peek_token (parser->lexer);
19117
19118 /* Get the name of the namespace. */
19119 identifier = cp_parser_identifier (parser);
19120 if (identifier == error_mark_node)
19121 return error_mark_node;
19122
19123 /* Look up the identifier in the currently active scope. Look only
19124 for namespaces, due to:
19125
19126 [basic.lookup.udir]
19127
19128 When looking up a namespace-name in a using-directive or alias
19129 definition, only namespace names are considered.
19130
19131 And:
19132
19133 [basic.lookup.qual]
19134
19135 During the lookup of a name preceding the :: scope resolution
19136 operator, object, function, and enumerator names are ignored.
19137
19138 (Note that cp_parser_qualifying_entity only calls this
19139 function if the token after the name is the scope resolution
19140 operator.) */
19141 namespace_decl = cp_parser_lookup_name (parser, identifier,
19142 none_type,
19143 /*is_template=*/false,
19144 /*is_namespace=*/true,
19145 /*check_dependency=*/true,
19146 /*ambiguous_decls=*/NULL,
19147 token->location);
19148 /* If it's not a namespace, issue an error. */
19149 if (namespace_decl == error_mark_node
19150 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19151 {
19152 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19153 {
19154 auto_diagnostic_group d;
19155 name_hint hint;
19156 if (namespace_decl == error_mark_node
19157 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19158 hint = suggest_alternative_in_explicit_scope (token->location,
19159 identifier,
19160 parser->scope);
19161 if (const char *suggestion = hint.suggestion ())
19162 {
19163 gcc_rich_location richloc (token->location);
19164 richloc.add_fixit_replace (suggestion);
19165 error_at (&richloc,
19166 "%qD is not a namespace-name; did you mean %qs?",
19167 identifier, suggestion);
19168 }
19169 else
19170 error_at (token->location, "%qD is not a namespace-name",
19171 identifier);
19172 }
19173 else
19174 cp_parser_error (parser, "expected namespace-name");
19175 namespace_decl = error_mark_node;
19176 }
19177
19178 return namespace_decl;
19179 }
19180
19181 /* Parse a namespace-definition.
19182
19183 namespace-definition:
19184 named-namespace-definition
19185 unnamed-namespace-definition
19186
19187 named-namespace-definition:
19188 original-namespace-definition
19189 extension-namespace-definition
19190
19191 original-namespace-definition:
19192 namespace identifier { namespace-body }
19193
19194 extension-namespace-definition:
19195 namespace original-namespace-name { namespace-body }
19196
19197 unnamed-namespace-definition:
19198 namespace { namespace-body } */
19199
19200 static void
19201 cp_parser_namespace_definition (cp_parser* parser)
19202 {
19203 tree identifier;
19204 int nested_definition_count = 0;
19205
19206 cp_ensure_no_omp_declare_simd (parser);
19207 cp_ensure_no_oacc_routine (parser);
19208
19209 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19210 const bool topmost_inline_p = is_inline;
19211
19212 if (is_inline)
19213 {
19214 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19215 cp_lexer_consume_token (parser->lexer);
19216 }
19217
19218 /* Look for the `namespace' keyword. */
19219 cp_token* token
19220 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19221
19222 /* Parse any specified attributes before the identifier. */
19223 tree attribs = cp_parser_attributes_opt (parser);
19224
19225 for (;;)
19226 {
19227 identifier = NULL_TREE;
19228
19229 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19230 RID_INLINE);
19231 if (nested_inline_p && nested_definition_count != 0)
19232 {
19233 if (cxx_dialect < cxx2a)
19234 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19235 OPT_Wpedantic, "nested inline namespace definitions only "
19236 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
19237 cp_lexer_consume_token (parser->lexer);
19238 }
19239
19240 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19241 {
19242 identifier = cp_parser_identifier (parser);
19243
19244 if (cp_next_tokens_can_be_std_attribute_p (parser))
19245 pedwarn (input_location, OPT_Wpedantic,
19246 "standard attributes on namespaces must precede "
19247 "the namespace name");
19248
19249 /* Parse any attributes specified after the identifier. */
19250 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19251 }
19252
19253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19254 {
19255 /* Don't forget that the innermost namespace might have been
19256 marked as inline. Use |= because we cannot overwrite
19257 IS_INLINE in case the outermost namespace is inline, but
19258 there are no nested inlines. */
19259 is_inline |= nested_inline_p;
19260 break;
19261 }
19262
19263 if (!nested_definition_count && cxx_dialect < cxx17)
19264 pedwarn (input_location, OPT_Wpedantic,
19265 "nested namespace definitions only available with "
19266 "%<-std=c++17%> or %<-std=gnu++17%>");
19267
19268 /* Nested namespace names can create new namespaces (unlike
19269 other qualified-ids). */
19270 if (int count = (identifier
19271 ? push_namespace (identifier, nested_inline_p)
19272 : 0))
19273 nested_definition_count += count;
19274 else
19275 cp_parser_error (parser, "nested namespace name required");
19276 cp_lexer_consume_token (parser->lexer);
19277 }
19278
19279 if (nested_definition_count && !identifier)
19280 cp_parser_error (parser, "namespace name required");
19281
19282 if (nested_definition_count && attribs)
19283 error_at (token->location,
19284 "a nested namespace definition cannot have attributes");
19285 if (nested_definition_count && topmost_inline_p)
19286 error_at (token->location,
19287 "a nested namespace definition cannot be inline");
19288
19289 /* Start the namespace. */
19290 nested_definition_count += push_namespace (identifier, is_inline);
19291
19292 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19293
19294 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19295
19296 /* Look for the `{' to validate starting the namespace. */
19297 matching_braces braces;
19298 if (braces.require_open (parser))
19299 {
19300 /* Parse the body of the namespace. */
19301 cp_parser_namespace_body (parser);
19302
19303 /* Look for the final `}'. */
19304 braces.require_close (parser);
19305 }
19306
19307 if (has_visibility)
19308 pop_visibility (1);
19309
19310 /* Pop the nested namespace definitions. */
19311 while (nested_definition_count--)
19312 pop_namespace ();
19313 }
19314
19315 /* Parse a namespace-body.
19316
19317 namespace-body:
19318 declaration-seq [opt] */
19319
19320 static void
19321 cp_parser_namespace_body (cp_parser* parser)
19322 {
19323 cp_parser_declaration_seq_opt (parser);
19324 }
19325
19326 /* Parse a namespace-alias-definition.
19327
19328 namespace-alias-definition:
19329 namespace identifier = qualified-namespace-specifier ; */
19330
19331 static void
19332 cp_parser_namespace_alias_definition (cp_parser* parser)
19333 {
19334 tree identifier;
19335 tree namespace_specifier;
19336
19337 cp_token *token = cp_lexer_peek_token (parser->lexer);
19338
19339 /* Look for the `namespace' keyword. */
19340 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19341 /* Look for the identifier. */
19342 identifier = cp_parser_identifier (parser);
19343 if (identifier == error_mark_node)
19344 return;
19345 /* Look for the `=' token. */
19346 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19347 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19348 {
19349 error_at (token->location, "%<namespace%> definition is not allowed here");
19350 /* Skip the definition. */
19351 cp_lexer_consume_token (parser->lexer);
19352 if (cp_parser_skip_to_closing_brace (parser))
19353 cp_lexer_consume_token (parser->lexer);
19354 return;
19355 }
19356 cp_parser_require (parser, CPP_EQ, RT_EQ);
19357 /* Look for the qualified-namespace-specifier. */
19358 namespace_specifier
19359 = cp_parser_qualified_namespace_specifier (parser);
19360 /* Look for the `;' token. */
19361 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19362
19363 /* Register the alias in the symbol table. */
19364 do_namespace_alias (identifier, namespace_specifier);
19365 }
19366
19367 /* Parse a qualified-namespace-specifier.
19368
19369 qualified-namespace-specifier:
19370 :: [opt] nested-name-specifier [opt] namespace-name
19371
19372 Returns a NAMESPACE_DECL corresponding to the specified
19373 namespace. */
19374
19375 static tree
19376 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19377 {
19378 /* Look for the optional `::'. */
19379 cp_parser_global_scope_opt (parser,
19380 /*current_scope_valid_p=*/false);
19381
19382 /* Look for the optional nested-name-specifier. */
19383 cp_parser_nested_name_specifier_opt (parser,
19384 /*typename_keyword_p=*/false,
19385 /*check_dependency_p=*/true,
19386 /*type_p=*/false,
19387 /*is_declaration=*/true);
19388
19389 return cp_parser_namespace_name (parser);
19390 }
19391
19392 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19393 access declaration.
19394
19395 using-declaration:
19396 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19397 using :: unqualified-id ;
19398
19399 access-declaration:
19400 qualified-id ;
19401
19402 */
19403
19404 static bool
19405 cp_parser_using_declaration (cp_parser* parser,
19406 bool access_declaration_p)
19407 {
19408 cp_token *token;
19409 bool typename_p = false;
19410 bool global_scope_p;
19411 tree decl;
19412 tree identifier;
19413 tree qscope;
19414 int oldcount = errorcount;
19415 cp_token *diag_token = NULL;
19416
19417 if (access_declaration_p)
19418 {
19419 diag_token = cp_lexer_peek_token (parser->lexer);
19420 cp_parser_parse_tentatively (parser);
19421 }
19422 else
19423 {
19424 /* Look for the `using' keyword. */
19425 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19426
19427 again:
19428 /* Peek at the next token. */
19429 token = cp_lexer_peek_token (parser->lexer);
19430 /* See if it's `typename'. */
19431 if (token->keyword == RID_TYPENAME)
19432 {
19433 /* Remember that we've seen it. */
19434 typename_p = true;
19435 /* Consume the `typename' token. */
19436 cp_lexer_consume_token (parser->lexer);
19437 }
19438 }
19439
19440 /* Look for the optional global scope qualification. */
19441 global_scope_p
19442 = (cp_parser_global_scope_opt (parser,
19443 /*current_scope_valid_p=*/false)
19444 != NULL_TREE);
19445
19446 /* If we saw `typename', or didn't see `::', then there must be a
19447 nested-name-specifier present. */
19448 if (typename_p || !global_scope_p)
19449 {
19450 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19451 /*check_dependency_p=*/true,
19452 /*type_p=*/false,
19453 /*is_declaration=*/true);
19454 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19455 {
19456 cp_parser_skip_to_end_of_block_or_statement (parser);
19457 return false;
19458 }
19459 }
19460 /* Otherwise, we could be in either of the two productions. In that
19461 case, treat the nested-name-specifier as optional. */
19462 else
19463 qscope = cp_parser_nested_name_specifier_opt (parser,
19464 /*typename_keyword_p=*/false,
19465 /*check_dependency_p=*/true,
19466 /*type_p=*/false,
19467 /*is_declaration=*/true);
19468 if (!qscope)
19469 qscope = global_namespace;
19470 else if (UNSCOPED_ENUM_P (qscope)
19471 && !TYPE_FUNCTION_SCOPE_P (qscope))
19472 qscope = CP_TYPE_CONTEXT (qscope);
19473
19474 if (access_declaration_p && cp_parser_error_occurred (parser))
19475 /* Something has already gone wrong; there's no need to parse
19476 further. Since an error has occurred, the return value of
19477 cp_parser_parse_definitely will be false, as required. */
19478 return cp_parser_parse_definitely (parser);
19479
19480 token = cp_lexer_peek_token (parser->lexer);
19481 /* Parse the unqualified-id. */
19482 identifier = cp_parser_unqualified_id (parser,
19483 /*template_keyword_p=*/false,
19484 /*check_dependency_p=*/true,
19485 /*declarator_p=*/true,
19486 /*optional_p=*/false);
19487
19488 if (access_declaration_p)
19489 {
19490 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19491 cp_parser_simulate_error (parser);
19492 if (!cp_parser_parse_definitely (parser))
19493 return false;
19494 }
19495 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19496 {
19497 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19498 if (cxx_dialect < cxx17
19499 && !in_system_header_at (ell->location))
19500 pedwarn (ell->location, 0,
19501 "pack expansion in using-declaration only available "
19502 "with %<-std=c++17%> or %<-std=gnu++17%>");
19503 qscope = make_pack_expansion (qscope);
19504 }
19505
19506 /* The function we call to handle a using-declaration is different
19507 depending on what scope we are in. */
19508 if (qscope == error_mark_node || identifier == error_mark_node)
19509 ;
19510 else if (!identifier_p (identifier)
19511 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19512 /* [namespace.udecl]
19513
19514 A using declaration shall not name a template-id. */
19515 error_at (token->location,
19516 "a template-id may not appear in a using-declaration");
19517 else
19518 {
19519 if (at_class_scope_p ())
19520 {
19521 /* Create the USING_DECL. */
19522 decl = do_class_using_decl (qscope, identifier);
19523
19524 if (decl && typename_p)
19525 USING_DECL_TYPENAME_P (decl) = 1;
19526
19527 if (check_for_bare_parameter_packs (decl))
19528 {
19529 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19530 return false;
19531 }
19532 else
19533 /* Add it to the list of members in this class. */
19534 finish_member_declaration (decl);
19535 }
19536 else
19537 {
19538 decl = cp_parser_lookup_name_simple (parser,
19539 identifier,
19540 token->location);
19541 if (decl == error_mark_node)
19542 cp_parser_name_lookup_error (parser, identifier,
19543 decl, NLE_NULL,
19544 token->location);
19545 else if (check_for_bare_parameter_packs (decl))
19546 {
19547 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19548 return false;
19549 }
19550 else if (!at_namespace_scope_p ())
19551 finish_local_using_decl (decl, qscope, identifier);
19552 else
19553 finish_namespace_using_decl (decl, qscope, identifier);
19554 }
19555 }
19556
19557 if (!access_declaration_p
19558 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19559 {
19560 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19561 if (cxx_dialect < cxx17)
19562 pedwarn (comma->location, 0,
19563 "comma-separated list in using-declaration only available "
19564 "with %<-std=c++17%> or %<-std=gnu++17%>");
19565 goto again;
19566 }
19567
19568 /* Look for the final `;'. */
19569 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19570
19571 if (access_declaration_p && errorcount == oldcount)
19572 warning_at (diag_token->location, OPT_Wdeprecated,
19573 "access declarations are deprecated "
19574 "in favour of using-declarations; "
19575 "suggestion: add the %<using%> keyword");
19576
19577 return true;
19578 }
19579
19580 /* Parse an alias-declaration.
19581
19582 alias-declaration:
19583 using identifier attribute-specifier-seq [opt] = type-id */
19584
19585 static tree
19586 cp_parser_alias_declaration (cp_parser* parser)
19587 {
19588 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19589 location_t id_location, type_location;
19590 cp_declarator *declarator;
19591 cp_decl_specifier_seq decl_specs;
19592 bool member_p;
19593 const char *saved_message = NULL;
19594
19595 /* Look for the `using' keyword. */
19596 cp_token *using_token
19597 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19598 if (using_token == NULL)
19599 return error_mark_node;
19600
19601 id_location = cp_lexer_peek_token (parser->lexer)->location;
19602 id = cp_parser_identifier (parser);
19603 if (id == error_mark_node)
19604 return error_mark_node;
19605
19606 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19607 attributes = cp_parser_attributes_opt (parser);
19608 if (attributes == error_mark_node)
19609 return error_mark_node;
19610
19611 cp_parser_require (parser, CPP_EQ, RT_EQ);
19612
19613 if (cp_parser_error_occurred (parser))
19614 return error_mark_node;
19615
19616 cp_parser_commit_to_tentative_parse (parser);
19617
19618 /* Now we are going to parse the type-id of the declaration. */
19619
19620 /*
19621 [dcl.type]/3 says:
19622
19623 "A type-specifier-seq shall not define a class or enumeration
19624 unless it appears in the type-id of an alias-declaration (7.1.3) that
19625 is not the declaration of a template-declaration."
19626
19627 In other words, if we currently are in an alias template, the
19628 type-id should not define a type.
19629
19630 So let's set parser->type_definition_forbidden_message in that
19631 case; cp_parser_check_type_definition (called by
19632 cp_parser_class_specifier) will then emit an error if a type is
19633 defined in the type-id. */
19634 if (parser->num_template_parameter_lists)
19635 {
19636 saved_message = parser->type_definition_forbidden_message;
19637 parser->type_definition_forbidden_message =
19638 G_("types may not be defined in alias template declarations");
19639 }
19640
19641 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
19642 &type_location);
19643
19644 /* Restore the error message if need be. */
19645 if (parser->num_template_parameter_lists)
19646 parser->type_definition_forbidden_message = saved_message;
19647
19648 if (type == error_mark_node
19649 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19650 {
19651 cp_parser_skip_to_end_of_block_or_statement (parser);
19652 return error_mark_node;
19653 }
19654
19655 /* A typedef-name can also be introduced by an alias-declaration. The
19656 identifier following the using keyword becomes a typedef-name. It has
19657 the same semantics as if it were introduced by the typedef
19658 specifier. In particular, it does not define a new type and it shall
19659 not appear in the type-id. */
19660
19661 clear_decl_specs (&decl_specs);
19662 decl_specs.type = type;
19663 if (attributes != NULL_TREE)
19664 {
19665 decl_specs.attributes = attributes;
19666 set_and_check_decl_spec_loc (&decl_specs,
19667 ds_attribute,
19668 attrs_token);
19669 }
19670 set_and_check_decl_spec_loc (&decl_specs,
19671 ds_typedef,
19672 using_token);
19673 set_and_check_decl_spec_loc (&decl_specs,
19674 ds_alias,
19675 using_token);
19676 decl_specs.locations[ds_type_spec] = type_location;
19677
19678 if (parser->num_template_parameter_lists
19679 && !cp_parser_check_template_parameters (parser,
19680 /*num_templates=*/0,
19681 /*template_id*/false,
19682 id_location,
19683 /*declarator=*/NULL))
19684 return error_mark_node;
19685
19686 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19687
19688 member_p = at_class_scope_p ();
19689 if (member_p)
19690 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19691 NULL_TREE, attributes);
19692 else
19693 decl = start_decl (declarator, &decl_specs, 0,
19694 attributes, NULL_TREE, &pushed_scope);
19695 if (decl == error_mark_node)
19696 return decl;
19697
19698 // Attach constraints to the alias declaration.
19699 if (flag_concepts && current_template_parms)
19700 {
19701 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19702 tree constr = build_constraints (reqs, NULL_TREE);
19703 set_constraints (decl, constr);
19704 }
19705
19706 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19707
19708 if (pushed_scope)
19709 pop_scope (pushed_scope);
19710
19711 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19712 added into the symbol table; otherwise, return the TYPE_DECL. */
19713 if (DECL_LANG_SPECIFIC (decl)
19714 && DECL_TEMPLATE_INFO (decl)
19715 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19716 {
19717 decl = DECL_TI_TEMPLATE (decl);
19718 if (member_p)
19719 check_member_template (decl);
19720 }
19721
19722 return decl;
19723 }
19724
19725 /* Parse a using-directive.
19726
19727 using-directive:
19728 using namespace :: [opt] nested-name-specifier [opt]
19729 namespace-name ; */
19730
19731 static void
19732 cp_parser_using_directive (cp_parser* parser)
19733 {
19734 tree namespace_decl;
19735 tree attribs;
19736
19737 /* Look for the `using' keyword. */
19738 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19739 /* And the `namespace' keyword. */
19740 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19741 /* Look for the optional `::' operator. */
19742 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19743 /* And the optional nested-name-specifier. */
19744 cp_parser_nested_name_specifier_opt (parser,
19745 /*typename_keyword_p=*/false,
19746 /*check_dependency_p=*/true,
19747 /*type_p=*/false,
19748 /*is_declaration=*/true);
19749 /* Get the namespace being used. */
19750 namespace_decl = cp_parser_namespace_name (parser);
19751 /* And any specified attributes. */
19752 attribs = cp_parser_attributes_opt (parser);
19753
19754 /* Update the symbol table. */
19755 if (namespace_bindings_p ())
19756 finish_namespace_using_directive (namespace_decl, attribs);
19757 else
19758 finish_local_using_directive (namespace_decl, attribs);
19759
19760 /* Look for the final `;'. */
19761 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19762 }
19763
19764 /* Parse an asm-definition.
19765
19766 asm-qualifier:
19767 volatile
19768 inline
19769 goto
19770
19771 asm-qualifier-list:
19772 asm-qualifier
19773 asm-qualifier-list asm-qualifier
19774
19775 asm-definition:
19776 asm ( string-literal ) ;
19777
19778 GNU Extension:
19779
19780 asm-definition:
19781 asm asm-qualifier-list [opt] ( string-literal ) ;
19782 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19783 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19784 : asm-operand-list [opt] ) ;
19785 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19786 : asm-operand-list [opt]
19787 : asm-clobber-list [opt] ) ;
19788 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19789 : asm-clobber-list [opt]
19790 : asm-goto-list ) ;
19791
19792 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19793 contains goto, and is the only allowed form in that case. No duplicates are
19794 allowed in an asm-qualifier-list. */
19795
19796 static void
19797 cp_parser_asm_definition (cp_parser* parser)
19798 {
19799 tree string;
19800 tree outputs = NULL_TREE;
19801 tree inputs = NULL_TREE;
19802 tree clobbers = NULL_TREE;
19803 tree labels = NULL_TREE;
19804 tree asm_stmt;
19805 bool extended_p = false;
19806 bool invalid_inputs_p = false;
19807 bool invalid_outputs_p = false;
19808 required_token missing = RT_NONE;
19809
19810 /* Look for the `asm' keyword. */
19811 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19812
19813 if (parser->in_function_body
19814 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19815 {
19816 error ("%<asm%> in %<constexpr%> function");
19817 cp_function_chain->invalid_constexpr = true;
19818 }
19819
19820 /* Handle the asm-qualifier-list. */
19821 location_t volatile_loc = UNKNOWN_LOCATION;
19822 location_t inline_loc = UNKNOWN_LOCATION;
19823 location_t goto_loc = UNKNOWN_LOCATION;
19824 location_t first_loc = UNKNOWN_LOCATION;
19825
19826 if (cp_parser_allow_gnu_extensions_p (parser))
19827 for (;;)
19828 {
19829 cp_token *token = cp_lexer_peek_token (parser->lexer);
19830 location_t loc = token->location;
19831 switch (cp_lexer_peek_token (parser->lexer)->keyword)
19832 {
19833 case RID_VOLATILE:
19834 if (volatile_loc)
19835 {
19836 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19837 inform (volatile_loc, "first seen here");
19838 }
19839 else
19840 {
19841 if (!parser->in_function_body)
19842 warning_at (loc, 0, "asm qualifier %qT ignored outside of "
19843 "function body", token->u.value);
19844 volatile_loc = loc;
19845 }
19846 cp_lexer_consume_token (parser->lexer);
19847 continue;
19848
19849 case RID_INLINE:
19850 if (inline_loc)
19851 {
19852 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19853 inform (inline_loc, "first seen here");
19854 }
19855 else
19856 inline_loc = loc;
19857 if (!first_loc)
19858 first_loc = loc;
19859 cp_lexer_consume_token (parser->lexer);
19860 continue;
19861
19862 case RID_GOTO:
19863 if (goto_loc)
19864 {
19865 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19866 inform (goto_loc, "first seen here");
19867 }
19868 else
19869 goto_loc = loc;
19870 if (!first_loc)
19871 first_loc = loc;
19872 cp_lexer_consume_token (parser->lexer);
19873 continue;
19874
19875 case RID_CONST:
19876 case RID_RESTRICT:
19877 error_at (loc, "%qT is not an asm qualifier", token->u.value);
19878 cp_lexer_consume_token (parser->lexer);
19879 continue;
19880
19881 default:
19882 break;
19883 }
19884 break;
19885 }
19886
19887 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
19888 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
19889 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
19890
19891 if (!parser->in_function_body && (inline_p || goto_p))
19892 {
19893 error_at (first_loc, "asm qualifier outside of function body");
19894 inline_p = goto_p = false;
19895 }
19896
19897 /* Look for the opening `('. */
19898 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19899 return;
19900 /* Look for the string. */
19901 string = cp_parser_string_literal (parser, false, false);
19902 if (string == error_mark_node)
19903 {
19904 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19905 /*consume_paren=*/true);
19906 return;
19907 }
19908
19909 /* If we're allowing GNU extensions, check for the extended assembly
19910 syntax. Unfortunately, the `:' tokens need not be separated by
19911 a space in C, and so, for compatibility, we tolerate that here
19912 too. Doing that means that we have to treat the `::' operator as
19913 two `:' tokens. */
19914 if (cp_parser_allow_gnu_extensions_p (parser)
19915 && parser->in_function_body
19916 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19917 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19918 {
19919 bool inputs_p = false;
19920 bool clobbers_p = false;
19921 bool labels_p = false;
19922
19923 /* The extended syntax was used. */
19924 extended_p = true;
19925
19926 /* Look for outputs. */
19927 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19928 {
19929 /* Consume the `:'. */
19930 cp_lexer_consume_token (parser->lexer);
19931 /* Parse the output-operands. */
19932 if (cp_lexer_next_token_is_not (parser->lexer,
19933 CPP_COLON)
19934 && cp_lexer_next_token_is_not (parser->lexer,
19935 CPP_SCOPE)
19936 && cp_lexer_next_token_is_not (parser->lexer,
19937 CPP_CLOSE_PAREN)
19938 && !goto_p)
19939 {
19940 outputs = cp_parser_asm_operand_list (parser);
19941 if (outputs == error_mark_node)
19942 invalid_outputs_p = true;
19943 }
19944 }
19945 /* If the next token is `::', there are no outputs, and the
19946 next token is the beginning of the inputs. */
19947 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19948 /* The inputs are coming next. */
19949 inputs_p = true;
19950
19951 /* Look for inputs. */
19952 if (inputs_p
19953 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19954 {
19955 /* Consume the `:' or `::'. */
19956 cp_lexer_consume_token (parser->lexer);
19957 /* Parse the output-operands. */
19958 if (cp_lexer_next_token_is_not (parser->lexer,
19959 CPP_COLON)
19960 && cp_lexer_next_token_is_not (parser->lexer,
19961 CPP_SCOPE)
19962 && cp_lexer_next_token_is_not (parser->lexer,
19963 CPP_CLOSE_PAREN))
19964 {
19965 inputs = cp_parser_asm_operand_list (parser);
19966 if (inputs == error_mark_node)
19967 invalid_inputs_p = true;
19968 }
19969 }
19970 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19971 /* The clobbers are coming next. */
19972 clobbers_p = true;
19973
19974 /* Look for clobbers. */
19975 if (clobbers_p
19976 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19977 {
19978 clobbers_p = true;
19979 /* Consume the `:' or `::'. */
19980 cp_lexer_consume_token (parser->lexer);
19981 /* Parse the clobbers. */
19982 if (cp_lexer_next_token_is_not (parser->lexer,
19983 CPP_COLON)
19984 && cp_lexer_next_token_is_not (parser->lexer,
19985 CPP_CLOSE_PAREN))
19986 clobbers = cp_parser_asm_clobber_list (parser);
19987 }
19988 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19989 /* The labels are coming next. */
19990 labels_p = true;
19991
19992 /* Look for labels. */
19993 if (labels_p
19994 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19995 {
19996 labels_p = true;
19997 /* Consume the `:' or `::'. */
19998 cp_lexer_consume_token (parser->lexer);
19999 /* Parse the labels. */
20000 labels = cp_parser_asm_label_list (parser);
20001 }
20002
20003 if (goto_p && !labels_p)
20004 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
20005 }
20006 else if (goto_p)
20007 missing = RT_COLON_SCOPE;
20008
20009 /* Look for the closing `)'. */
20010 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
20011 missing ? missing : RT_CLOSE_PAREN))
20012 cp_parser_skip_to_closing_parenthesis (parser, true, false,
20013 /*consume_paren=*/true);
20014 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20015
20016 if (!invalid_inputs_p && !invalid_outputs_p)
20017 {
20018 /* Create the ASM_EXPR. */
20019 if (parser->in_function_body)
20020 {
20021 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
20022 inputs, clobbers, labels, inline_p);
20023 /* If the extended syntax was not used, mark the ASM_EXPR. */
20024 if (!extended_p)
20025 {
20026 tree temp = asm_stmt;
20027 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
20028 temp = TREE_OPERAND (temp, 0);
20029
20030 ASM_INPUT_P (temp) = 1;
20031 }
20032 }
20033 else
20034 symtab->finalize_toplevel_asm (string);
20035 }
20036 }
20037
20038 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20039 type that comes from the decl-specifier-seq. */
20040
20041 static tree
20042 strip_declarator_types (tree type, cp_declarator *declarator)
20043 {
20044 for (cp_declarator *d = declarator; d;)
20045 switch (d->kind)
20046 {
20047 case cdk_id:
20048 case cdk_decomp:
20049 case cdk_error:
20050 d = NULL;
20051 break;
20052
20053 default:
20054 if (TYPE_PTRMEMFUNC_P (type))
20055 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
20056 type = TREE_TYPE (type);
20057 d = d->declarator;
20058 break;
20059 }
20060
20061 return type;
20062 }
20063
20064 /* Declarators [gram.dcl.decl] */
20065
20066 /* Parse an init-declarator.
20067
20068 init-declarator:
20069 declarator initializer [opt]
20070
20071 GNU Extension:
20072
20073 init-declarator:
20074 declarator asm-specification [opt] attributes [opt] initializer [opt]
20075
20076 function-definition:
20077 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20078 function-body
20079 decl-specifier-seq [opt] declarator function-try-block
20080
20081 GNU Extension:
20082
20083 function-definition:
20084 __extension__ function-definition
20085
20086 TM Extension:
20087
20088 function-definition:
20089 decl-specifier-seq [opt] declarator function-transaction-block
20090
20091 The parser flags FLAGS is used to control type-specifier parsing.
20092
20093 The DECL_SPECIFIERS apply to this declarator. Returns a
20094 representation of the entity declared. If MEMBER_P is TRUE, then
20095 this declarator appears in a class scope. The new DECL created by
20096 this declarator is returned.
20097
20098 The CHECKS are access checks that should be performed once we know
20099 what entity is being declared (and, therefore, what classes have
20100 befriended it).
20101
20102 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20103 for a function-definition here as well. If the declarator is a
20104 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20105 be TRUE upon return. By that point, the function-definition will
20106 have been completely parsed.
20107
20108 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20109 is FALSE.
20110
20111 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20112 parsed declaration if it is an uninitialized single declarator not followed
20113 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20114 if present, will not be consumed. If returned, this declarator will be
20115 created with SD_INITIALIZED but will not call cp_finish_decl.
20116
20117 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20118 and there is an initializer, the pointed location_t is set to the
20119 location of the '=' or `(', or '{' in C++11 token introducing the
20120 initializer. */
20121
20122 static tree
20123 cp_parser_init_declarator (cp_parser* parser,
20124 cp_parser_flags flags,
20125 cp_decl_specifier_seq *decl_specifiers,
20126 vec<deferred_access_check, va_gc> *checks,
20127 bool function_definition_allowed_p,
20128 bool member_p,
20129 int declares_class_or_enum,
20130 bool* function_definition_p,
20131 tree* maybe_range_for_decl,
20132 location_t* init_loc,
20133 tree* auto_result)
20134 {
20135 cp_token *token = NULL, *asm_spec_start_token = NULL,
20136 *attributes_start_token = NULL;
20137 cp_declarator *declarator;
20138 tree prefix_attributes;
20139 tree attributes = NULL;
20140 tree asm_specification;
20141 tree initializer;
20142 tree decl = NULL_TREE;
20143 tree scope;
20144 int is_initialized;
20145 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20146 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20147 "(...)". */
20148 enum cpp_ttype initialization_kind;
20149 bool is_direct_init = false;
20150 bool is_non_constant_init;
20151 int ctor_dtor_or_conv_p;
20152 bool friend_p = cp_parser_friend_p (decl_specifiers);
20153 tree pushed_scope = NULL_TREE;
20154 bool range_for_decl_p = false;
20155 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20156 location_t tmp_init_loc = UNKNOWN_LOCATION;
20157
20158 /* Gather the attributes that were provided with the
20159 decl-specifiers. */
20160 prefix_attributes = decl_specifiers->attributes;
20161
20162 /* Assume that this is not the declarator for a function
20163 definition. */
20164 if (function_definition_p)
20165 *function_definition_p = false;
20166
20167 /* Default arguments are only permitted for function parameters. */
20168 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20169 parser->default_arg_ok_p = false;
20170
20171 /* Defer access checks while parsing the declarator; we cannot know
20172 what names are accessible until we know what is being
20173 declared. */
20174 resume_deferring_access_checks ();
20175
20176 token = cp_lexer_peek_token (parser->lexer);
20177
20178 /* Parse the declarator. */
20179 declarator
20180 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20181 flags, &ctor_dtor_or_conv_p,
20182 /*parenthesized_p=*/NULL,
20183 member_p, friend_p, /*static_p=*/false);
20184 /* Gather up the deferred checks. */
20185 stop_deferring_access_checks ();
20186
20187 parser->default_arg_ok_p = saved_default_arg_ok_p;
20188
20189 /* If the DECLARATOR was erroneous, there's no need to go
20190 further. */
20191 if (declarator == cp_error_declarator)
20192 return error_mark_node;
20193
20194 /* Check that the number of template-parameter-lists is OK. */
20195 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20196 token->location))
20197 return error_mark_node;
20198
20199 if (declares_class_or_enum & 2)
20200 cp_parser_check_for_definition_in_return_type (declarator,
20201 decl_specifiers->type,
20202 decl_specifiers->locations[ds_type_spec]);
20203
20204 /* Figure out what scope the entity declared by the DECLARATOR is
20205 located in. `grokdeclarator' sometimes changes the scope, so
20206 we compute it now. */
20207 scope = get_scope_of_declarator (declarator);
20208
20209 /* Perform any lookups in the declared type which were thought to be
20210 dependent, but are not in the scope of the declarator. */
20211 decl_specifiers->type
20212 = maybe_update_decl_type (decl_specifiers->type, scope);
20213
20214 /* If we're allowing GNU extensions, look for an
20215 asm-specification. */
20216 if (cp_parser_allow_gnu_extensions_p (parser))
20217 {
20218 /* Look for an asm-specification. */
20219 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20220 asm_specification = cp_parser_asm_specification_opt (parser);
20221 }
20222 else
20223 asm_specification = NULL_TREE;
20224
20225 /* Look for attributes. */
20226 attributes_start_token = cp_lexer_peek_token (parser->lexer);
20227 attributes = cp_parser_attributes_opt (parser);
20228
20229 /* Peek at the next token. */
20230 token = cp_lexer_peek_token (parser->lexer);
20231
20232 bool bogus_implicit_tmpl = false;
20233
20234 if (function_declarator_p (declarator))
20235 {
20236 /* Handle C++17 deduction guides. */
20237 if (!decl_specifiers->type
20238 && ctor_dtor_or_conv_p <= 0
20239 && cxx_dialect >= cxx17)
20240 {
20241 cp_declarator *id = get_id_declarator (declarator);
20242 tree name = id->u.id.unqualified_name;
20243 parser->scope = id->u.id.qualifying_scope;
20244 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20245 if (tmpl
20246 && (DECL_CLASS_TEMPLATE_P (tmpl)
20247 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20248 {
20249 id->u.id.unqualified_name = dguide_name (tmpl);
20250 id->u.id.sfk = sfk_deduction_guide;
20251 ctor_dtor_or_conv_p = 1;
20252 }
20253 }
20254
20255 /* Check to see if the token indicates the start of a
20256 function-definition. */
20257 if (cp_parser_token_starts_function_definition_p (token))
20258 {
20259 if (!function_definition_allowed_p)
20260 {
20261 /* If a function-definition should not appear here, issue an
20262 error message. */
20263 cp_parser_error (parser,
20264 "a function-definition is not allowed here");
20265 return error_mark_node;
20266 }
20267
20268 location_t func_brace_location
20269 = cp_lexer_peek_token (parser->lexer)->location;
20270
20271 /* Neither attributes nor an asm-specification are allowed
20272 on a function-definition. */
20273 if (asm_specification)
20274 error_at (asm_spec_start_token->location,
20275 "an asm-specification is not allowed "
20276 "on a function-definition");
20277 if (attributes)
20278 error_at (attributes_start_token->location,
20279 "attributes are not allowed "
20280 "on a function-definition");
20281 /* This is a function-definition. */
20282 *function_definition_p = true;
20283
20284 /* Parse the function definition. */
20285 if (member_p)
20286 decl = cp_parser_save_member_function_body (parser,
20287 decl_specifiers,
20288 declarator,
20289 prefix_attributes);
20290 else
20291 decl =
20292 (cp_parser_function_definition_from_specifiers_and_declarator
20293 (parser, decl_specifiers, prefix_attributes, declarator));
20294
20295 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20296 {
20297 /* This is where the prologue starts... */
20298 DECL_STRUCT_FUNCTION (decl)->function_start_locus
20299 = func_brace_location;
20300 }
20301
20302 return decl;
20303 }
20304 }
20305 else if (parser->fully_implicit_function_template_p)
20306 {
20307 /* A non-template declaration involving a function parameter list
20308 containing an implicit template parameter will be made into a
20309 template. If the resulting declaration is not going to be an
20310 actual function then finish the template scope here to prevent it.
20311 An error message will be issued once we have a decl to talk about.
20312
20313 FIXME probably we should do type deduction rather than create an
20314 implicit template, but the standard currently doesn't allow it. */
20315 bogus_implicit_tmpl = true;
20316 finish_fully_implicit_template (parser, NULL_TREE);
20317 }
20318
20319 /* [dcl.dcl]
20320
20321 Only in function declarations for constructors, destructors, type
20322 conversions, and deduction guides can the decl-specifier-seq be omitted.
20323
20324 We explicitly postpone this check past the point where we handle
20325 function-definitions because we tolerate function-definitions
20326 that are missing their return types in some modes. */
20327 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20328 {
20329 cp_parser_error (parser,
20330 "expected constructor, destructor, or type conversion");
20331 return error_mark_node;
20332 }
20333
20334 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20335 if (token->type == CPP_EQ
20336 || token->type == CPP_OPEN_PAREN
20337 || token->type == CPP_OPEN_BRACE)
20338 {
20339 is_initialized = SD_INITIALIZED;
20340 initialization_kind = token->type;
20341 if (maybe_range_for_decl)
20342 *maybe_range_for_decl = error_mark_node;
20343 tmp_init_loc = token->location;
20344 if (init_loc && *init_loc == UNKNOWN_LOCATION)
20345 *init_loc = tmp_init_loc;
20346
20347 if (token->type == CPP_EQ
20348 && function_declarator_p (declarator))
20349 {
20350 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20351 if (t2->keyword == RID_DEFAULT)
20352 is_initialized = SD_DEFAULTED;
20353 else if (t2->keyword == RID_DELETE)
20354 is_initialized = SD_DELETED;
20355 }
20356 }
20357 else
20358 {
20359 /* If the init-declarator isn't initialized and isn't followed by a
20360 `,' or `;', it's not a valid init-declarator. */
20361 if (token->type != CPP_COMMA
20362 && token->type != CPP_SEMICOLON)
20363 {
20364 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20365 range_for_decl_p = true;
20366 else
20367 {
20368 if (!maybe_range_for_decl)
20369 cp_parser_error (parser, "expected initializer");
20370 return error_mark_node;
20371 }
20372 }
20373 is_initialized = SD_UNINITIALIZED;
20374 initialization_kind = CPP_EOF;
20375 }
20376
20377 /* Because start_decl has side-effects, we should only call it if we
20378 know we're going ahead. By this point, we know that we cannot
20379 possibly be looking at any other construct. */
20380 cp_parser_commit_to_tentative_parse (parser);
20381
20382 /* Enter the newly declared entry in the symbol table. If we're
20383 processing a declaration in a class-specifier, we wait until
20384 after processing the initializer. */
20385 if (!member_p)
20386 {
20387 if (parser->in_unbraced_linkage_specification_p)
20388 decl_specifiers->storage_class = sc_extern;
20389 decl = start_decl (declarator, decl_specifiers,
20390 range_for_decl_p? SD_INITIALIZED : is_initialized,
20391 attributes, prefix_attributes, &pushed_scope);
20392 cp_finalize_omp_declare_simd (parser, decl);
20393 cp_finalize_oacc_routine (parser, decl, false);
20394 /* Adjust location of decl if declarator->id_loc is more appropriate:
20395 set, and decl wasn't merged with another decl, in which case its
20396 location would be different from input_location, and more accurate. */
20397 if (DECL_P (decl)
20398 && declarator->id_loc != UNKNOWN_LOCATION
20399 && DECL_SOURCE_LOCATION (decl) == input_location)
20400 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20401 }
20402 else if (scope)
20403 /* Enter the SCOPE. That way unqualified names appearing in the
20404 initializer will be looked up in SCOPE. */
20405 pushed_scope = push_scope (scope);
20406
20407 /* Perform deferred access control checks, now that we know in which
20408 SCOPE the declared entity resides. */
20409 if (!member_p && decl)
20410 {
20411 tree saved_current_function_decl = NULL_TREE;
20412
20413 /* If the entity being declared is a function, pretend that we
20414 are in its scope. If it is a `friend', it may have access to
20415 things that would not otherwise be accessible. */
20416 if (TREE_CODE (decl) == FUNCTION_DECL)
20417 {
20418 saved_current_function_decl = current_function_decl;
20419 current_function_decl = decl;
20420 }
20421
20422 /* Perform access checks for template parameters. */
20423 cp_parser_perform_template_parameter_access_checks (checks);
20424
20425 /* Perform the access control checks for the declarator and the
20426 decl-specifiers. */
20427 perform_deferred_access_checks (tf_warning_or_error);
20428
20429 /* Restore the saved value. */
20430 if (TREE_CODE (decl) == FUNCTION_DECL)
20431 current_function_decl = saved_current_function_decl;
20432 }
20433
20434 /* Parse the initializer. */
20435 initializer = NULL_TREE;
20436 is_direct_init = false;
20437 is_non_constant_init = true;
20438 if (is_initialized)
20439 {
20440 if (function_declarator_p (declarator))
20441 {
20442 if (initialization_kind == CPP_EQ)
20443 initializer = cp_parser_pure_specifier (parser);
20444 else
20445 {
20446 /* If the declaration was erroneous, we don't really
20447 know what the user intended, so just silently
20448 consume the initializer. */
20449 if (decl != error_mark_node)
20450 error_at (tmp_init_loc, "initializer provided for function");
20451 cp_parser_skip_to_closing_parenthesis (parser,
20452 /*recovering=*/true,
20453 /*or_comma=*/false,
20454 /*consume_paren=*/true);
20455 }
20456 }
20457 else
20458 {
20459 /* We want to record the extra mangling scope for in-class
20460 initializers of class members and initializers of static data
20461 member templates. The former involves deferring
20462 parsing of the initializer until end of class as with default
20463 arguments. So right here we only handle the latter. */
20464 if (!member_p && processing_template_decl && decl != error_mark_node)
20465 start_lambda_scope (decl);
20466 initializer = cp_parser_initializer (parser,
20467 &is_direct_init,
20468 &is_non_constant_init);
20469 if (!member_p && processing_template_decl && decl != error_mark_node)
20470 finish_lambda_scope ();
20471 if (initializer == error_mark_node)
20472 cp_parser_skip_to_end_of_statement (parser);
20473 }
20474 }
20475
20476 /* The old parser allows attributes to appear after a parenthesized
20477 initializer. Mark Mitchell proposed removing this functionality
20478 on the GCC mailing lists on 2002-08-13. This parser accepts the
20479 attributes -- but ignores them. Made a permerror in GCC 8. */
20480 if (cp_parser_allow_gnu_extensions_p (parser)
20481 && initialization_kind == CPP_OPEN_PAREN
20482 && cp_parser_attributes_opt (parser)
20483 && permerror (input_location,
20484 "attributes after parenthesized initializer ignored"))
20485 {
20486 static bool hint;
20487 if (flag_permissive && !hint)
20488 {
20489 hint = true;
20490 inform (input_location,
20491 "this flexibility is deprecated and will be removed");
20492 }
20493 }
20494
20495 /* And now complain about a non-function implicit template. */
20496 if (bogus_implicit_tmpl && decl != error_mark_node)
20497 error_at (DECL_SOURCE_LOCATION (decl),
20498 "non-function %qD declared as implicit template", decl);
20499
20500 /* For an in-class declaration, use `grokfield' to create the
20501 declaration. */
20502 if (member_p)
20503 {
20504 if (pushed_scope)
20505 {
20506 pop_scope (pushed_scope);
20507 pushed_scope = NULL_TREE;
20508 }
20509 decl = grokfield (declarator, decl_specifiers,
20510 initializer, !is_non_constant_init,
20511 /*asmspec=*/NULL_TREE,
20512 attr_chainon (attributes, prefix_attributes));
20513 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20514 cp_parser_save_default_args (parser, decl);
20515 cp_finalize_omp_declare_simd (parser, decl);
20516 cp_finalize_oacc_routine (parser, decl, false);
20517 }
20518
20519 /* Finish processing the declaration. But, skip member
20520 declarations. */
20521 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20522 {
20523 cp_finish_decl (decl,
20524 initializer, !is_non_constant_init,
20525 asm_specification,
20526 /* If the initializer is in parentheses, then this is
20527 a direct-initialization, which means that an
20528 `explicit' constructor is OK. Otherwise, an
20529 `explicit' constructor cannot be used. */
20530 ((is_direct_init || !is_initialized)
20531 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20532 }
20533 else if ((cxx_dialect != cxx98) && friend_p
20534 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20535 /* Core issue #226 (C++0x only): A default template-argument
20536 shall not be specified in a friend class template
20537 declaration. */
20538 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20539 /*is_partial=*/false, /*is_friend_decl=*/1);
20540
20541 if (!friend_p && pushed_scope)
20542 pop_scope (pushed_scope);
20543
20544 if (function_declarator_p (declarator)
20545 && parser->fully_implicit_function_template_p)
20546 {
20547 if (member_p)
20548 decl = finish_fully_implicit_template (parser, decl);
20549 else
20550 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20551 }
20552
20553 if (auto_result && is_initialized && decl_specifiers->type
20554 && type_uses_auto (decl_specifiers->type))
20555 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20556
20557 return decl;
20558 }
20559
20560 /* Parse a declarator.
20561
20562 declarator:
20563 direct-declarator
20564 ptr-operator declarator
20565
20566 abstract-declarator:
20567 ptr-operator abstract-declarator [opt]
20568 direct-abstract-declarator
20569
20570 GNU Extensions:
20571
20572 declarator:
20573 attributes [opt] direct-declarator
20574 attributes [opt] ptr-operator declarator
20575
20576 abstract-declarator:
20577 attributes [opt] ptr-operator abstract-declarator [opt]
20578 attributes [opt] direct-abstract-declarator
20579
20580 The parser flags FLAGS is used to control type-specifier parsing.
20581
20582 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20583 detect constructors, destructors, deduction guides, or conversion operators.
20584 It is set to -1 if the declarator is a name, and +1 if it is a
20585 function. Otherwise it is set to zero. Usually you just want to
20586 test for >0, but internally the negative value is used.
20587
20588 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20589 a decl-specifier-seq unless it declares a constructor, destructor,
20590 or conversion. It might seem that we could check this condition in
20591 semantic analysis, rather than parsing, but that makes it difficult
20592 to handle something like `f()'. We want to notice that there are
20593 no decl-specifiers, and therefore realize that this is an
20594 expression, not a declaration.)
20595
20596 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20597 the declarator is a direct-declarator of the form "(...)".
20598
20599 MEMBER_P is true iff this declarator is a member-declarator.
20600
20601 FRIEND_P is true iff this declarator is a friend.
20602
20603 STATIC_P is true iff the keyword static was seen. */
20604
20605 static cp_declarator *
20606 cp_parser_declarator (cp_parser* parser,
20607 cp_parser_declarator_kind dcl_kind,
20608 cp_parser_flags flags,
20609 int* ctor_dtor_or_conv_p,
20610 bool* parenthesized_p,
20611 bool member_p, bool friend_p, bool static_p)
20612 {
20613 cp_declarator *declarator;
20614 enum tree_code code;
20615 cp_cv_quals cv_quals;
20616 tree class_type;
20617 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20618
20619 /* Assume this is not a constructor, destructor, or type-conversion
20620 operator. */
20621 if (ctor_dtor_or_conv_p)
20622 *ctor_dtor_or_conv_p = 0;
20623
20624 if (cp_parser_allow_gnu_extensions_p (parser))
20625 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20626
20627 /* Check for the ptr-operator production. */
20628 cp_parser_parse_tentatively (parser);
20629 /* Parse the ptr-operator. */
20630 code = cp_parser_ptr_operator (parser,
20631 &class_type,
20632 &cv_quals,
20633 &std_attributes);
20634
20635 /* If that worked, then we have a ptr-operator. */
20636 if (cp_parser_parse_definitely (parser))
20637 {
20638 /* If a ptr-operator was found, then this declarator was not
20639 parenthesized. */
20640 if (parenthesized_p)
20641 *parenthesized_p = true;
20642 /* The dependent declarator is optional if we are parsing an
20643 abstract-declarator. */
20644 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20645 cp_parser_parse_tentatively (parser);
20646
20647 /* Parse the dependent declarator. */
20648 declarator = cp_parser_declarator (parser, dcl_kind,
20649 CP_PARSER_FLAGS_NONE,
20650 /*ctor_dtor_or_conv_p=*/NULL,
20651 /*parenthesized_p=*/NULL,
20652 /*member_p=*/false,
20653 friend_p, /*static_p=*/false);
20654
20655 /* If we are parsing an abstract-declarator, we must handle the
20656 case where the dependent declarator is absent. */
20657 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20658 && !cp_parser_parse_definitely (parser))
20659 declarator = NULL;
20660
20661 declarator = cp_parser_make_indirect_declarator
20662 (code, class_type, cv_quals, declarator, std_attributes);
20663 }
20664 /* Everything else is a direct-declarator. */
20665 else
20666 {
20667 if (parenthesized_p)
20668 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20669 CPP_OPEN_PAREN);
20670 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20671 flags, ctor_dtor_or_conv_p,
20672 member_p, friend_p, static_p);
20673 }
20674
20675 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20676 declarator->attributes = gnu_attributes;
20677 return declarator;
20678 }
20679
20680 /* Parse a direct-declarator or direct-abstract-declarator.
20681
20682 direct-declarator:
20683 declarator-id
20684 direct-declarator ( parameter-declaration-clause )
20685 cv-qualifier-seq [opt]
20686 ref-qualifier [opt]
20687 exception-specification [opt]
20688 direct-declarator [ constant-expression [opt] ]
20689 ( declarator )
20690
20691 direct-abstract-declarator:
20692 direct-abstract-declarator [opt]
20693 ( parameter-declaration-clause )
20694 cv-qualifier-seq [opt]
20695 ref-qualifier [opt]
20696 exception-specification [opt]
20697 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20698 ( abstract-declarator )
20699
20700 Returns a representation of the declarator. DCL_KIND is
20701 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20702 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20703 we are parsing a direct-declarator. It is
20704 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20705 of ambiguity we prefer an abstract declarator, as per
20706 [dcl.ambig.res].
20707 The parser flags FLAGS is used to control type-specifier parsing.
20708 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20709 as for cp_parser_declarator. */
20710
20711 static cp_declarator *
20712 cp_parser_direct_declarator (cp_parser* parser,
20713 cp_parser_declarator_kind dcl_kind,
20714 cp_parser_flags flags,
20715 int* ctor_dtor_or_conv_p,
20716 bool member_p, bool friend_p, bool static_p)
20717 {
20718 cp_token *token;
20719 cp_declarator *declarator = NULL;
20720 tree scope = NULL_TREE;
20721 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20722 bool saved_in_declarator_p = parser->in_declarator_p;
20723 bool first = true;
20724 tree pushed_scope = NULL_TREE;
20725 cp_token *open_paren = NULL, *close_paren = NULL;
20726
20727 while (true)
20728 {
20729 /* Peek at the next token. */
20730 token = cp_lexer_peek_token (parser->lexer);
20731 if (token->type == CPP_OPEN_PAREN)
20732 {
20733 /* This is either a parameter-declaration-clause, or a
20734 parenthesized declarator. When we know we are parsing a
20735 named declarator, it must be a parenthesized declarator
20736 if FIRST is true. For instance, `(int)' is a
20737 parameter-declaration-clause, with an omitted
20738 direct-abstract-declarator. But `((*))', is a
20739 parenthesized abstract declarator. Finally, when T is a
20740 template parameter `(T)' is a
20741 parameter-declaration-clause, and not a parenthesized
20742 named declarator.
20743
20744 We first try and parse a parameter-declaration-clause,
20745 and then try a nested declarator (if FIRST is true).
20746
20747 It is not an error for it not to be a
20748 parameter-declaration-clause, even when FIRST is
20749 false. Consider,
20750
20751 int i (int);
20752 int i (3);
20753
20754 The first is the declaration of a function while the
20755 second is the definition of a variable, including its
20756 initializer.
20757
20758 Having seen only the parenthesis, we cannot know which of
20759 these two alternatives should be selected. Even more
20760 complex are examples like:
20761
20762 int i (int (a));
20763 int i (int (3));
20764
20765 The former is a function-declaration; the latter is a
20766 variable initialization.
20767
20768 Thus again, we try a parameter-declaration-clause, and if
20769 that fails, we back out and return. */
20770
20771 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20772 {
20773 tree params;
20774 bool is_declarator = false;
20775
20776 open_paren = NULL;
20777
20778 /* In a member-declarator, the only valid interpretation
20779 of a parenthesis is the start of a
20780 parameter-declaration-clause. (It is invalid to
20781 initialize a static data member with a parenthesized
20782 initializer; only the "=" form of initialization is
20783 permitted.) */
20784 if (!member_p)
20785 cp_parser_parse_tentatively (parser);
20786
20787 /* Consume the `('. */
20788 matching_parens parens;
20789 parens.consume_open (parser);
20790 if (first)
20791 {
20792 /* If this is going to be an abstract declarator, we're
20793 in a declarator and we can't have default args. */
20794 parser->default_arg_ok_p = false;
20795 parser->in_declarator_p = true;
20796 }
20797
20798 begin_scope (sk_function_parms, NULL_TREE);
20799
20800 /* Parse the parameter-declaration-clause. */
20801 params
20802 = cp_parser_parameter_declaration_clause (parser, flags);
20803
20804 /* Consume the `)'. */
20805 parens.require_close (parser);
20806
20807 /* If all went well, parse the cv-qualifier-seq,
20808 ref-qualifier and the exception-specification. */
20809 if (member_p || cp_parser_parse_definitely (parser))
20810 {
20811 cp_cv_quals cv_quals;
20812 cp_virt_specifiers virt_specifiers;
20813 cp_ref_qualifier ref_qual;
20814 tree exception_specification;
20815 tree late_return;
20816 tree attrs;
20817 bool memfn = (member_p || (pushed_scope
20818 && CLASS_TYPE_P (pushed_scope)));
20819 unsigned char local_variables_forbidden_p
20820 = parser->local_variables_forbidden_p;
20821 /* 'this' is not allowed in static member functions. */
20822 if (static_p || friend_p)
20823 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
20824
20825 is_declarator = true;
20826
20827 if (ctor_dtor_or_conv_p)
20828 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20829 first = false;
20830
20831 /* Parse the cv-qualifier-seq. */
20832 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20833 /* Parse the ref-qualifier. */
20834 ref_qual = cp_parser_ref_qualifier_opt (parser);
20835 /* Parse the tx-qualifier. */
20836 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20837 /* And the exception-specification. */
20838 exception_specification
20839 = cp_parser_exception_specification_opt (parser);
20840
20841 attrs = cp_parser_std_attribute_spec_seq (parser);
20842
20843 /* In here, we handle cases where attribute is used after
20844 the function declaration. For example:
20845 void func (int x) __attribute__((vector(..))); */
20846 tree gnu_attrs = NULL_TREE;
20847 tree requires_clause = NULL_TREE;
20848 late_return = (cp_parser_late_return_type_opt
20849 (parser, declarator, requires_clause,
20850 memfn ? cv_quals : -1));
20851
20852 /* Parse the virt-specifier-seq. */
20853 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20854
20855 /* Create the function-declarator. */
20856 declarator = make_call_declarator (declarator,
20857 params,
20858 cv_quals,
20859 virt_specifiers,
20860 ref_qual,
20861 tx_qual,
20862 exception_specification,
20863 late_return,
20864 requires_clause);
20865 declarator->std_attributes = attrs;
20866 declarator->attributes = gnu_attrs;
20867 /* Any subsequent parameter lists are to do with
20868 return type, so are not those of the declared
20869 function. */
20870 parser->default_arg_ok_p = false;
20871
20872 /* Restore the state of local_variables_forbidden_p. */
20873 parser->local_variables_forbidden_p
20874 = local_variables_forbidden_p;
20875 }
20876
20877 /* Remove the function parms from scope. */
20878 pop_bindings_and_leave_scope ();
20879
20880 if (is_declarator)
20881 /* Repeat the main loop. */
20882 continue;
20883 }
20884
20885 /* If this is the first, we can try a parenthesized
20886 declarator. */
20887 if (first)
20888 {
20889 bool saved_in_type_id_in_expr_p;
20890
20891 parser->default_arg_ok_p = saved_default_arg_ok_p;
20892 parser->in_declarator_p = saved_in_declarator_p;
20893
20894 open_paren = token;
20895 /* Consume the `('. */
20896 matching_parens parens;
20897 parens.consume_open (parser);
20898 /* Parse the nested declarator. */
20899 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20900 parser->in_type_id_in_expr_p = true;
20901 declarator
20902 = cp_parser_declarator (parser, dcl_kind, flags,
20903 ctor_dtor_or_conv_p,
20904 /*parenthesized_p=*/NULL,
20905 member_p, friend_p,
20906 /*static_p=*/false);
20907 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20908 first = false;
20909 /* Expect a `)'. */
20910 close_paren = cp_lexer_peek_token (parser->lexer);
20911 if (!parens.require_close (parser))
20912 declarator = cp_error_declarator;
20913 if (declarator == cp_error_declarator)
20914 break;
20915
20916 goto handle_declarator;
20917 }
20918 /* Otherwise, we must be done. */
20919 else
20920 break;
20921 }
20922 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20923 && token->type == CPP_OPEN_SQUARE
20924 && !cp_next_tokens_can_be_attribute_p (parser))
20925 {
20926 /* Parse an array-declarator. */
20927 tree bounds, attrs;
20928
20929 if (ctor_dtor_or_conv_p)
20930 *ctor_dtor_or_conv_p = 0;
20931
20932 open_paren = NULL;
20933 first = false;
20934 parser->default_arg_ok_p = false;
20935 parser->in_declarator_p = true;
20936 /* Consume the `['. */
20937 cp_lexer_consume_token (parser->lexer);
20938 /* Peek at the next token. */
20939 token = cp_lexer_peek_token (parser->lexer);
20940 /* If the next token is `]', then there is no
20941 constant-expression. */
20942 if (token->type != CPP_CLOSE_SQUARE)
20943 {
20944 bool non_constant_p;
20945 bounds
20946 = cp_parser_constant_expression (parser,
20947 /*allow_non_constant=*/true,
20948 &non_constant_p);
20949 if (!non_constant_p)
20950 /* OK */;
20951 else if (error_operand_p (bounds))
20952 /* Already gave an error. */;
20953 else if (!parser->in_function_body
20954 || current_binding_level->kind == sk_function_parms)
20955 {
20956 /* Normally, the array bound must be an integral constant
20957 expression. However, as an extension, we allow VLAs
20958 in function scopes as long as they aren't part of a
20959 parameter declaration. */
20960 cp_parser_error (parser,
20961 "array bound is not an integer constant");
20962 bounds = error_mark_node;
20963 }
20964 else if (processing_template_decl
20965 && !type_dependent_expression_p (bounds))
20966 {
20967 /* Remember this wasn't a constant-expression. */
20968 bounds = build_nop (TREE_TYPE (bounds), bounds);
20969 TREE_SIDE_EFFECTS (bounds) = 1;
20970 }
20971 }
20972 else
20973 bounds = NULL_TREE;
20974 /* Look for the closing `]'. */
20975 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20976 {
20977 declarator = cp_error_declarator;
20978 break;
20979 }
20980
20981 attrs = cp_parser_std_attribute_spec_seq (parser);
20982 declarator = make_array_declarator (declarator, bounds);
20983 declarator->std_attributes = attrs;
20984 }
20985 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20986 {
20987 {
20988 tree qualifying_scope;
20989 tree unqualified_name;
20990 tree attrs;
20991 special_function_kind sfk;
20992 bool abstract_ok;
20993 bool pack_expansion_p = false;
20994 cp_token *declarator_id_start_token;
20995
20996 /* Parse a declarator-id */
20997 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20998 if (abstract_ok)
20999 {
21000 cp_parser_parse_tentatively (parser);
21001
21002 /* If we see an ellipsis, we should be looking at a
21003 parameter pack. */
21004 if (token->type == CPP_ELLIPSIS)
21005 {
21006 /* Consume the `...' */
21007 cp_lexer_consume_token (parser->lexer);
21008
21009 pack_expansion_p = true;
21010 }
21011 }
21012
21013 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
21014 unqualified_name
21015 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
21016 qualifying_scope = parser->scope;
21017 if (abstract_ok)
21018 {
21019 bool okay = false;
21020
21021 if (!unqualified_name && pack_expansion_p)
21022 {
21023 /* Check whether an error occurred. */
21024 okay = !cp_parser_error_occurred (parser);
21025
21026 /* We already consumed the ellipsis to mark a
21027 parameter pack, but we have no way to report it,
21028 so abort the tentative parse. We will be exiting
21029 immediately anyway. */
21030 cp_parser_abort_tentative_parse (parser);
21031 }
21032 else
21033 okay = cp_parser_parse_definitely (parser);
21034
21035 if (!okay)
21036 unqualified_name = error_mark_node;
21037 else if (unqualified_name
21038 && (qualifying_scope
21039 || (!identifier_p (unqualified_name))))
21040 {
21041 cp_parser_error (parser, "expected unqualified-id");
21042 unqualified_name = error_mark_node;
21043 }
21044 }
21045
21046 if (!unqualified_name)
21047 return NULL;
21048 if (unqualified_name == error_mark_node)
21049 {
21050 declarator = cp_error_declarator;
21051 pack_expansion_p = false;
21052 declarator->parameter_pack_p = false;
21053 break;
21054 }
21055
21056 attrs = cp_parser_std_attribute_spec_seq (parser);
21057
21058 if (qualifying_scope && at_namespace_scope_p ()
21059 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
21060 {
21061 /* In the declaration of a member of a template class
21062 outside of the class itself, the SCOPE will sometimes
21063 be a TYPENAME_TYPE. For example, given:
21064
21065 template <typename T>
21066 int S<T>::R::i = 3;
21067
21068 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
21069 this context, we must resolve S<T>::R to an ordinary
21070 type, rather than a typename type.
21071
21072 The reason we normally avoid resolving TYPENAME_TYPEs
21073 is that a specialization of `S' might render
21074 `S<T>::R' not a type. However, if `S' is
21075 specialized, then this `i' will not be used, so there
21076 is no harm in resolving the types here. */
21077 tree type;
21078
21079 /* Resolve the TYPENAME_TYPE. */
21080 type = resolve_typename_type (qualifying_scope,
21081 /*only_current_p=*/false);
21082 /* If that failed, the declarator is invalid. */
21083 if (TREE_CODE (type) == TYPENAME_TYPE)
21084 {
21085 if (typedef_variant_p (type))
21086 error_at (declarator_id_start_token->location,
21087 "cannot define member of dependent typedef "
21088 "%qT", type);
21089 else
21090 error_at (declarator_id_start_token->location,
21091 "%<%T::%E%> is not a type",
21092 TYPE_CONTEXT (qualifying_scope),
21093 TYPE_IDENTIFIER (qualifying_scope));
21094 }
21095 qualifying_scope = type;
21096 }
21097
21098 sfk = sfk_none;
21099
21100 if (unqualified_name)
21101 {
21102 tree class_type;
21103
21104 if (qualifying_scope
21105 && CLASS_TYPE_P (qualifying_scope))
21106 class_type = qualifying_scope;
21107 else
21108 class_type = current_class_type;
21109
21110 if (TREE_CODE (unqualified_name) == TYPE_DECL)
21111 {
21112 tree name_type = TREE_TYPE (unqualified_name);
21113
21114 if (!class_type || !same_type_p (name_type, class_type))
21115 {
21116 /* We do not attempt to print the declarator
21117 here because we do not have enough
21118 information about its original syntactic
21119 form. */
21120 cp_parser_error (parser, "invalid declarator");
21121 declarator = cp_error_declarator;
21122 break;
21123 }
21124 else if (qualifying_scope
21125 && CLASSTYPE_USE_TEMPLATE (name_type))
21126 {
21127 error_at (declarator_id_start_token->location,
21128 "invalid use of constructor as a template");
21129 inform (declarator_id_start_token->location,
21130 "use %<%T::%D%> instead of %<%T::%D%> to "
21131 "name the constructor in a qualified name",
21132 class_type,
21133 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
21134 class_type, name_type);
21135 declarator = cp_error_declarator;
21136 break;
21137 }
21138 unqualified_name = constructor_name (class_type);
21139 }
21140
21141 if (class_type)
21142 {
21143 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
21144 sfk = sfk_destructor;
21145 else if (identifier_p (unqualified_name)
21146 && IDENTIFIER_CONV_OP_P (unqualified_name))
21147 sfk = sfk_conversion;
21148 else if (/* There's no way to declare a constructor
21149 for an unnamed type, even if the type
21150 got a name for linkage purposes. */
21151 !TYPE_WAS_UNNAMED (class_type)
21152 /* Handle correctly (c++/19200):
21153
21154 struct S {
21155 struct T{};
21156 friend void S(T);
21157 };
21158
21159 and also:
21160
21161 namespace N {
21162 void S();
21163 }
21164
21165 struct S {
21166 friend void N::S();
21167 }; */
21168 && (!friend_p || class_type == qualifying_scope)
21169 && constructor_name_p (unqualified_name,
21170 class_type))
21171 sfk = sfk_constructor;
21172 else if (is_overloaded_fn (unqualified_name)
21173 && DECL_CONSTRUCTOR_P (get_first_fn
21174 (unqualified_name)))
21175 sfk = sfk_constructor;
21176
21177 if (ctor_dtor_or_conv_p && sfk != sfk_none)
21178 *ctor_dtor_or_conv_p = -1;
21179 }
21180 }
21181 declarator = make_id_declarator (qualifying_scope,
21182 unqualified_name,
21183 sfk, token->location);
21184 declarator->std_attributes = attrs;
21185 declarator->parameter_pack_p = pack_expansion_p;
21186
21187 if (pack_expansion_p)
21188 maybe_warn_variadic_templates ();
21189
21190 /* We're looking for this case in [temp.res]:
21191 A qualified-id is assumed to name a type if [...]
21192 - it is a decl-specifier of the decl-specifier-seq of a
21193 parameter-declaration in a declarator of a function or
21194 function template declaration, ... */
21195 if (cxx_dialect >= cxx2a
21196 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
21197 && declarator->kind == cdk_id
21198 && !at_class_scope_p ()
21199 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21200 {
21201 /* ...whose declarator-id is qualified. If it isn't, never
21202 assume the parameters to refer to types. */
21203 if (qualifying_scope == NULL_TREE)
21204 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21205 else
21206 {
21207 /* Now we have something like
21208 template <typename T> int C::x(S::p);
21209 which can be a function template declaration or a
21210 variable template definition. If name lookup for
21211 the declarator-id C::x finds one or more function
21212 templates, assume S::p to name a type. Otherwise,
21213 don't. */
21214 tree decl
21215 = cp_parser_lookup_name_simple (parser, unqualified_name,
21216 token->location);
21217 if (!is_overloaded_fn (decl)
21218 /* Allow
21219 template<typename T>
21220 A<T>::A(T::type) { } */
21221 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
21222 && constructor_name_p (unqualified_name,
21223 qualifying_scope)))
21224 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21225 }
21226 }
21227 }
21228
21229 handle_declarator:;
21230 scope = get_scope_of_declarator (declarator);
21231 if (scope)
21232 {
21233 /* Any names that appear after the declarator-id for a
21234 member are looked up in the containing scope. */
21235 if (at_function_scope_p ())
21236 {
21237 /* But declarations with qualified-ids can't appear in a
21238 function. */
21239 cp_parser_error (parser, "qualified-id in declaration");
21240 declarator = cp_error_declarator;
21241 break;
21242 }
21243 pushed_scope = push_scope (scope);
21244 }
21245 parser->in_declarator_p = true;
21246 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21247 || (declarator && declarator->kind == cdk_id))
21248 /* Default args are only allowed on function
21249 declarations. */
21250 parser->default_arg_ok_p = saved_default_arg_ok_p;
21251 else
21252 parser->default_arg_ok_p = false;
21253
21254 first = false;
21255 }
21256 /* We're done. */
21257 else
21258 break;
21259 }
21260
21261 /* For an abstract declarator, we might wind up with nothing at this
21262 point. That's an error; the declarator is not optional. */
21263 if (!declarator)
21264 cp_parser_error (parser, "expected declarator");
21265 else if (open_paren)
21266 {
21267 /* Record overly parenthesized declarator so we can give a
21268 diagnostic about confusing decl/expr disambiguation. */
21269 if (declarator->kind == cdk_array)
21270 {
21271 /* If the open and close parens are on different lines, this
21272 is probably a formatting thing, so ignore. */
21273 expanded_location open = expand_location (open_paren->location);
21274 expanded_location close = expand_location (close_paren->location);
21275 if (open.line != close.line || open.file != close.file)
21276 open_paren = NULL;
21277 }
21278 if (open_paren)
21279 declarator->parenthesized = open_paren->location;
21280 }
21281
21282 /* If we entered a scope, we must exit it now. */
21283 if (pushed_scope)
21284 pop_scope (pushed_scope);
21285
21286 parser->default_arg_ok_p = saved_default_arg_ok_p;
21287 parser->in_declarator_p = saved_in_declarator_p;
21288
21289 return declarator;
21290 }
21291
21292 /* Parse a ptr-operator.
21293
21294 ptr-operator:
21295 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21296 * cv-qualifier-seq [opt]
21297 &
21298 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21299 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21300
21301 GNU Extension:
21302
21303 ptr-operator:
21304 & cv-qualifier-seq [opt]
21305
21306 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21307 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21308 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21309 filled in with the TYPE containing the member. *CV_QUALS is
21310 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21311 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21312 Note that the tree codes returned by this function have nothing
21313 to do with the types of trees that will be eventually be created
21314 to represent the pointer or reference type being parsed. They are
21315 just constants with suggestive names. */
21316 static enum tree_code
21317 cp_parser_ptr_operator (cp_parser* parser,
21318 tree* type,
21319 cp_cv_quals *cv_quals,
21320 tree *attributes)
21321 {
21322 enum tree_code code = ERROR_MARK;
21323 cp_token *token;
21324 tree attrs = NULL_TREE;
21325
21326 /* Assume that it's not a pointer-to-member. */
21327 *type = NULL_TREE;
21328 /* And that there are no cv-qualifiers. */
21329 *cv_quals = TYPE_UNQUALIFIED;
21330
21331 /* Peek at the next token. */
21332 token = cp_lexer_peek_token (parser->lexer);
21333
21334 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21335 if (token->type == CPP_MULT)
21336 code = INDIRECT_REF;
21337 else if (token->type == CPP_AND)
21338 code = ADDR_EXPR;
21339 else if ((cxx_dialect != cxx98) &&
21340 token->type == CPP_AND_AND) /* C++0x only */
21341 code = NON_LVALUE_EXPR;
21342
21343 if (code != ERROR_MARK)
21344 {
21345 /* Consume the `*', `&' or `&&'. */
21346 cp_lexer_consume_token (parser->lexer);
21347
21348 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21349 `&', if we are allowing GNU extensions. (The only qualifier
21350 that can legally appear after `&' is `restrict', but that is
21351 enforced during semantic analysis. */
21352 if (code == INDIRECT_REF
21353 || cp_parser_allow_gnu_extensions_p (parser))
21354 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21355
21356 attrs = cp_parser_std_attribute_spec_seq (parser);
21357 if (attributes != NULL)
21358 *attributes = attrs;
21359 }
21360 else
21361 {
21362 /* Try the pointer-to-member case. */
21363 cp_parser_parse_tentatively (parser);
21364 /* Look for the optional `::' operator. */
21365 cp_parser_global_scope_opt (parser,
21366 /*current_scope_valid_p=*/false);
21367 /* Look for the nested-name specifier. */
21368 token = cp_lexer_peek_token (parser->lexer);
21369 cp_parser_nested_name_specifier (parser,
21370 /*typename_keyword_p=*/false,
21371 /*check_dependency_p=*/true,
21372 /*type_p=*/false,
21373 /*is_declaration=*/false);
21374 /* If we found it, and the next token is a `*', then we are
21375 indeed looking at a pointer-to-member operator. */
21376 if (!cp_parser_error_occurred (parser)
21377 && cp_parser_require (parser, CPP_MULT, RT_MULT))
21378 {
21379 /* Indicate that the `*' operator was used. */
21380 code = INDIRECT_REF;
21381
21382 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21383 error_at (token->location, "%qD is a namespace", parser->scope);
21384 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21385 error_at (token->location, "cannot form pointer to member of "
21386 "non-class %q#T", parser->scope);
21387 else
21388 {
21389 /* The type of which the member is a member is given by the
21390 current SCOPE. */
21391 *type = parser->scope;
21392 /* The next name will not be qualified. */
21393 parser->scope = NULL_TREE;
21394 parser->qualifying_scope = NULL_TREE;
21395 parser->object_scope = NULL_TREE;
21396 /* Look for optional c++11 attributes. */
21397 attrs = cp_parser_std_attribute_spec_seq (parser);
21398 if (attributes != NULL)
21399 *attributes = attrs;
21400 /* Look for the optional cv-qualifier-seq. */
21401 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21402 }
21403 }
21404 /* If that didn't work we don't have a ptr-operator. */
21405 if (!cp_parser_parse_definitely (parser))
21406 cp_parser_error (parser, "expected ptr-operator");
21407 }
21408
21409 return code;
21410 }
21411
21412 /* Parse an (optional) cv-qualifier-seq.
21413
21414 cv-qualifier-seq:
21415 cv-qualifier cv-qualifier-seq [opt]
21416
21417 cv-qualifier:
21418 const
21419 volatile
21420
21421 GNU Extension:
21422
21423 cv-qualifier:
21424 __restrict__
21425
21426 Returns a bitmask representing the cv-qualifiers. */
21427
21428 static cp_cv_quals
21429 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21430 {
21431 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21432
21433 while (true)
21434 {
21435 cp_token *token;
21436 cp_cv_quals cv_qualifier;
21437
21438 /* Peek at the next token. */
21439 token = cp_lexer_peek_token (parser->lexer);
21440 /* See if it's a cv-qualifier. */
21441 switch (token->keyword)
21442 {
21443 case RID_CONST:
21444 cv_qualifier = TYPE_QUAL_CONST;
21445 break;
21446
21447 case RID_VOLATILE:
21448 cv_qualifier = TYPE_QUAL_VOLATILE;
21449 break;
21450
21451 case RID_RESTRICT:
21452 cv_qualifier = TYPE_QUAL_RESTRICT;
21453 break;
21454
21455 default:
21456 cv_qualifier = TYPE_UNQUALIFIED;
21457 break;
21458 }
21459
21460 if (!cv_qualifier)
21461 break;
21462
21463 if (cv_quals & cv_qualifier)
21464 {
21465 gcc_rich_location richloc (token->location);
21466 richloc.add_fixit_remove ();
21467 error_at (&richloc, "duplicate cv-qualifier");
21468 cp_lexer_purge_token (parser->lexer);
21469 }
21470 else
21471 {
21472 cp_lexer_consume_token (parser->lexer);
21473 cv_quals |= cv_qualifier;
21474 }
21475 }
21476
21477 return cv_quals;
21478 }
21479
21480 /* Parse an (optional) ref-qualifier
21481
21482 ref-qualifier:
21483 &
21484 &&
21485
21486 Returns cp_ref_qualifier representing ref-qualifier. */
21487
21488 static cp_ref_qualifier
21489 cp_parser_ref_qualifier_opt (cp_parser* parser)
21490 {
21491 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21492
21493 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21494 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21495 return ref_qual;
21496
21497 while (true)
21498 {
21499 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21500 cp_token *token = cp_lexer_peek_token (parser->lexer);
21501
21502 switch (token->type)
21503 {
21504 case CPP_AND:
21505 curr_ref_qual = REF_QUAL_LVALUE;
21506 break;
21507
21508 case CPP_AND_AND:
21509 curr_ref_qual = REF_QUAL_RVALUE;
21510 break;
21511
21512 default:
21513 curr_ref_qual = REF_QUAL_NONE;
21514 break;
21515 }
21516
21517 if (!curr_ref_qual)
21518 break;
21519 else if (ref_qual)
21520 {
21521 error_at (token->location, "multiple ref-qualifiers");
21522 cp_lexer_purge_token (parser->lexer);
21523 }
21524 else
21525 {
21526 ref_qual = curr_ref_qual;
21527 cp_lexer_consume_token (parser->lexer);
21528 }
21529 }
21530
21531 return ref_qual;
21532 }
21533
21534 /* Parse an optional tx-qualifier.
21535
21536 tx-qualifier:
21537 transaction_safe
21538 transaction_safe_dynamic */
21539
21540 static tree
21541 cp_parser_tx_qualifier_opt (cp_parser *parser)
21542 {
21543 cp_token *token = cp_lexer_peek_token (parser->lexer);
21544 if (token->type == CPP_NAME)
21545 {
21546 tree name = token->u.value;
21547 const char *p = IDENTIFIER_POINTER (name);
21548 const int len = strlen ("transaction_safe");
21549 if (!strncmp (p, "transaction_safe", len))
21550 {
21551 p += len;
21552 if (*p == '\0'
21553 || !strcmp (p, "_dynamic"))
21554 {
21555 cp_lexer_consume_token (parser->lexer);
21556 if (!flag_tm)
21557 {
21558 error ("%qE requires %<-fgnu-tm%>", name);
21559 return NULL_TREE;
21560 }
21561 else
21562 return name;
21563 }
21564 }
21565 }
21566 return NULL_TREE;
21567 }
21568
21569 /* Parse an (optional) virt-specifier-seq.
21570
21571 virt-specifier-seq:
21572 virt-specifier virt-specifier-seq [opt]
21573
21574 virt-specifier:
21575 override
21576 final
21577
21578 Returns a bitmask representing the virt-specifiers. */
21579
21580 static cp_virt_specifiers
21581 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21582 {
21583 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21584
21585 while (true)
21586 {
21587 cp_token *token;
21588 cp_virt_specifiers virt_specifier;
21589
21590 /* Peek at the next token. */
21591 token = cp_lexer_peek_token (parser->lexer);
21592 /* See if it's a virt-specifier-qualifier. */
21593 if (token->type != CPP_NAME)
21594 break;
21595 if (id_equal (token->u.value, "override"))
21596 {
21597 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21598 virt_specifier = VIRT_SPEC_OVERRIDE;
21599 }
21600 else if (id_equal (token->u.value, "final"))
21601 {
21602 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21603 virt_specifier = VIRT_SPEC_FINAL;
21604 }
21605 else if (id_equal (token->u.value, "__final"))
21606 {
21607 virt_specifier = VIRT_SPEC_FINAL;
21608 }
21609 else
21610 break;
21611
21612 if (virt_specifiers & virt_specifier)
21613 {
21614 gcc_rich_location richloc (token->location);
21615 richloc.add_fixit_remove ();
21616 error_at (&richloc, "duplicate virt-specifier");
21617 cp_lexer_purge_token (parser->lexer);
21618 }
21619 else
21620 {
21621 cp_lexer_consume_token (parser->lexer);
21622 virt_specifiers |= virt_specifier;
21623 }
21624 }
21625 return virt_specifiers;
21626 }
21627
21628 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21629 is in scope even though it isn't real. */
21630
21631 void
21632 inject_this_parameter (tree ctype, cp_cv_quals quals)
21633 {
21634 tree this_parm;
21635
21636 if (current_class_ptr)
21637 {
21638 /* We don't clear this between NSDMIs. Is it already what we want? */
21639 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21640 if (DECL_P (current_class_ptr)
21641 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21642 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21643 && cp_type_quals (type) == quals)
21644 return;
21645 }
21646
21647 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21648 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21649 current_class_ptr = NULL_TREE;
21650 current_class_ref
21651 = cp_build_fold_indirect_ref (this_parm);
21652 current_class_ptr = this_parm;
21653 }
21654
21655 /* Return true iff our current scope is a non-static data member
21656 initializer. */
21657
21658 bool
21659 parsing_nsdmi (void)
21660 {
21661 /* We recognize NSDMI context by the context-less 'this' pointer set up
21662 by the function above. */
21663 if (current_class_ptr
21664 && TREE_CODE (current_class_ptr) == PARM_DECL
21665 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21666 return true;
21667 return false;
21668 }
21669
21670 /* Parse a late-specified return type, if any. This is not a separate
21671 non-terminal, but part of a function declarator, which looks like
21672
21673 -> trailing-type-specifier-seq abstract-declarator(opt)
21674
21675 Returns the type indicated by the type-id.
21676
21677 In addition to this, parse any queued up #pragma omp declare simd
21678 clauses, and #pragma acc routine clauses.
21679
21680 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21681 function. */
21682
21683 static tree
21684 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21685 tree& requires_clause, cp_cv_quals quals)
21686 {
21687 cp_token *token;
21688 tree type = NULL_TREE;
21689 bool declare_simd_p = (parser->omp_declare_simd
21690 && declarator
21691 && declarator->kind == cdk_id);
21692
21693 bool oacc_routine_p = (parser->oacc_routine
21694 && declarator
21695 && declarator->kind == cdk_id);
21696
21697 /* Peek at the next token. */
21698 token = cp_lexer_peek_token (parser->lexer);
21699 /* A late-specified return type is indicated by an initial '->'. */
21700 if (token->type != CPP_DEREF
21701 && token->keyword != RID_REQUIRES
21702 && !(token->type == CPP_NAME
21703 && token->u.value == ridpointers[RID_REQUIRES])
21704 && !(declare_simd_p || oacc_routine_p))
21705 return NULL_TREE;
21706
21707 tree save_ccp = current_class_ptr;
21708 tree save_ccr = current_class_ref;
21709 if (quals >= 0)
21710 {
21711 /* DR 1207: 'this' is in scope in the trailing return type. */
21712 inject_this_parameter (current_class_type, quals);
21713 }
21714
21715 if (token->type == CPP_DEREF)
21716 {
21717 /* Consume the ->. */
21718 cp_lexer_consume_token (parser->lexer);
21719
21720 type = cp_parser_trailing_type_id (parser);
21721 }
21722
21723 /* Function declarations may be followed by a trailing
21724 requires-clause. */
21725 requires_clause = cp_parser_requires_clause_opt (parser);
21726
21727 if (declare_simd_p)
21728 declarator->attributes
21729 = cp_parser_late_parsing_omp_declare_simd (parser,
21730 declarator->attributes);
21731 if (oacc_routine_p)
21732 declarator->attributes
21733 = cp_parser_late_parsing_oacc_routine (parser,
21734 declarator->attributes);
21735
21736 if (quals >= 0)
21737 {
21738 current_class_ptr = save_ccp;
21739 current_class_ref = save_ccr;
21740 }
21741
21742 return type;
21743 }
21744
21745 /* Parse a declarator-id.
21746
21747 declarator-id:
21748 id-expression
21749 :: [opt] nested-name-specifier [opt] type-name
21750
21751 In the `id-expression' case, the value returned is as for
21752 cp_parser_id_expression if the id-expression was an unqualified-id.
21753 If the id-expression was a qualified-id, then a SCOPE_REF is
21754 returned. The first operand is the scope (either a NAMESPACE_DECL
21755 or TREE_TYPE), but the second is still just a representation of an
21756 unqualified-id. */
21757
21758 static tree
21759 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21760 {
21761 tree id;
21762 /* The expression must be an id-expression. Assume that qualified
21763 names are the names of types so that:
21764
21765 template <class T>
21766 int S<T>::R::i = 3;
21767
21768 will work; we must treat `S<T>::R' as the name of a type.
21769 Similarly, assume that qualified names are templates, where
21770 required, so that:
21771
21772 template <class T>
21773 int S<T>::R<T>::i = 3;
21774
21775 will work, too. */
21776 id = cp_parser_id_expression (parser,
21777 /*template_keyword_p=*/false,
21778 /*check_dependency_p=*/false,
21779 /*template_p=*/NULL,
21780 /*declarator_p=*/true,
21781 optional_p);
21782 if (id && BASELINK_P (id))
21783 id = BASELINK_FUNCTIONS (id);
21784 return id;
21785 }
21786
21787 /* Parse a type-id.
21788
21789 type-id:
21790 type-specifier-seq abstract-declarator [opt]
21791
21792 The parser flags FLAGS is used to control type-specifier parsing.
21793
21794 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21795
21796 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21797 i.e. we've just seen "->".
21798
21799 Returns the TYPE specified. */
21800
21801 static tree
21802 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
21803 bool is_template_arg, bool is_trailing_return,
21804 location_t *type_location)
21805 {
21806 cp_decl_specifier_seq type_specifier_seq;
21807 cp_declarator *abstract_declarator;
21808
21809 /* Parse the type-specifier-seq. */
21810 cp_parser_type_specifier_seq (parser, flags,
21811 /*is_declaration=*/false,
21812 is_trailing_return,
21813 &type_specifier_seq);
21814 if (type_location)
21815 *type_location = type_specifier_seq.locations[ds_type_spec];
21816
21817 if (is_template_arg && type_specifier_seq.type
21818 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21819 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21820 /* A bare template name as a template argument is a template template
21821 argument, not a placeholder, so fail parsing it as a type argument. */
21822 {
21823 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21824 cp_parser_simulate_error (parser);
21825 return error_mark_node;
21826 }
21827 if (type_specifier_seq.type == error_mark_node)
21828 return error_mark_node;
21829
21830 /* There might or might not be an abstract declarator. */
21831 cp_parser_parse_tentatively (parser);
21832 /* Look for the declarator. */
21833 abstract_declarator
21834 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
21835 CP_PARSER_FLAGS_NONE, NULL,
21836 /*parenthesized_p=*/NULL,
21837 /*member_p=*/false,
21838 /*friend_p=*/false,
21839 /*static_p=*/false);
21840 /* Check to see if there really was a declarator. */
21841 if (!cp_parser_parse_definitely (parser))
21842 abstract_declarator = NULL;
21843
21844 if (type_specifier_seq.type
21845 /* The concepts TS allows 'auto' as a type-id. */
21846 && (!flag_concepts || parser->in_type_id_in_expr_p)
21847 /* None of the valid uses of 'auto' in C++14 involve the type-id
21848 nonterminal, but it is valid in a trailing-return-type. */
21849 && !(cxx_dialect >= cxx14 && is_trailing_return))
21850 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21851 {
21852 /* A type-id with type 'auto' is only ok if the abstract declarator
21853 is a function declarator with a late-specified return type.
21854
21855 A type-id with 'auto' is also valid in a trailing-return-type
21856 in a compound-requirement. */
21857 if (abstract_declarator
21858 && abstract_declarator->kind == cdk_function
21859 && abstract_declarator->u.function.late_return_type)
21860 /* OK */;
21861 else if (parser->in_result_type_constraint_p)
21862 /* OK */;
21863 else
21864 {
21865 location_t loc = type_specifier_seq.locations[ds_type_spec];
21866 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21867 {
21868 error_at (loc, "missing template arguments after %qT",
21869 auto_node);
21870 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21871 tmpl);
21872 }
21873 else
21874 error_at (loc, "invalid use of %qT", auto_node);
21875 return error_mark_node;
21876 }
21877 }
21878
21879 return groktypename (&type_specifier_seq, abstract_declarator,
21880 is_template_arg);
21881 }
21882
21883 /* Wrapper for cp_parser_type_id_1. */
21884
21885 static tree
21886 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
21887 location_t *type_location)
21888 {
21889 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
21890 }
21891
21892 /* Wrapper for cp_parser_type_id_1. */
21893
21894 static tree
21895 cp_parser_template_type_arg (cp_parser *parser)
21896 {
21897 tree r;
21898 const char *saved_message = parser->type_definition_forbidden_message;
21899 parser->type_definition_forbidden_message
21900 = G_("types may not be defined in template arguments");
21901 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
21902 parser->type_definition_forbidden_message = saved_message;
21903 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21904 {
21905 error ("invalid use of %<auto%> in template argument");
21906 r = error_mark_node;
21907 }
21908 return r;
21909 }
21910
21911 /* Wrapper for cp_parser_type_id_1. */
21912
21913 static tree
21914 cp_parser_trailing_type_id (cp_parser *parser)
21915 {
21916 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21917 false, true, NULL);
21918 }
21919
21920 /* Parse a type-specifier-seq.
21921
21922 type-specifier-seq:
21923 type-specifier type-specifier-seq [opt]
21924
21925 GNU extension:
21926
21927 type-specifier-seq:
21928 attributes type-specifier-seq [opt]
21929
21930 The parser flags FLAGS is used to control type-specifier parsing.
21931
21932 If IS_DECLARATION is true, we are at the start of a "condition" or
21933 exception-declaration, so we might be followed by a declarator-id.
21934
21935 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21936 i.e. we've just seen "->".
21937
21938 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21939
21940 static void
21941 cp_parser_type_specifier_seq (cp_parser* parser,
21942 cp_parser_flags flags,
21943 bool is_declaration,
21944 bool is_trailing_return,
21945 cp_decl_specifier_seq *type_specifier_seq)
21946 {
21947 bool seen_type_specifier = false;
21948 cp_token *start_token = NULL;
21949
21950 /* Clear the TYPE_SPECIFIER_SEQ. */
21951 clear_decl_specs (type_specifier_seq);
21952
21953 flags |= CP_PARSER_FLAGS_OPTIONAL;
21954 /* In the context of a trailing return type, enum E { } is an
21955 elaborated-type-specifier followed by a function-body, not an
21956 enum-specifier. */
21957 if (is_trailing_return)
21958 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21959
21960 /* Parse the type-specifiers and attributes. */
21961 while (true)
21962 {
21963 tree type_specifier;
21964 bool is_cv_qualifier;
21965
21966 /* Check for attributes first. */
21967 if (cp_next_tokens_can_be_attribute_p (parser))
21968 {
21969 type_specifier_seq->attributes
21970 = attr_chainon (type_specifier_seq->attributes,
21971 cp_parser_attributes_opt (parser));
21972 continue;
21973 }
21974
21975 /* record the token of the beginning of the type specifier seq,
21976 for error reporting purposes*/
21977 if (!start_token)
21978 start_token = cp_lexer_peek_token (parser->lexer);
21979
21980 /* Look for the type-specifier. */
21981 type_specifier = cp_parser_type_specifier (parser,
21982 flags,
21983 type_specifier_seq,
21984 /*is_declaration=*/false,
21985 NULL,
21986 &is_cv_qualifier);
21987 if (!type_specifier)
21988 {
21989 /* If the first type-specifier could not be found, this is not a
21990 type-specifier-seq at all. */
21991 if (!seen_type_specifier)
21992 {
21993 /* Set in_declarator_p to avoid skipping to the semicolon. */
21994 int in_decl = parser->in_declarator_p;
21995 parser->in_declarator_p = true;
21996
21997 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21998 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21999 cp_parser_error (parser, "expected type-specifier");
22000
22001 parser->in_declarator_p = in_decl;
22002
22003 type_specifier_seq->type = error_mark_node;
22004 return;
22005 }
22006 /* If subsequent type-specifiers could not be found, the
22007 type-specifier-seq is complete. */
22008 break;
22009 }
22010
22011 seen_type_specifier = true;
22012 /* The standard says that a condition can be:
22013
22014 type-specifier-seq declarator = assignment-expression
22015
22016 However, given:
22017
22018 struct S {};
22019 if (int S = ...)
22020
22021 we should treat the "S" as a declarator, not as a
22022 type-specifier. The standard doesn't say that explicitly for
22023 type-specifier-seq, but it does say that for
22024 decl-specifier-seq in an ordinary declaration. Perhaps it
22025 would be clearer just to allow a decl-specifier-seq here, and
22026 then add a semantic restriction that if any decl-specifiers
22027 that are not type-specifiers appear, the program is invalid. */
22028 if (is_declaration && !is_cv_qualifier)
22029 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
22030 }
22031 }
22032
22033 /* Return whether the function currently being declared has an associated
22034 template parameter list. */
22035
22036 static bool
22037 function_being_declared_is_template_p (cp_parser* parser)
22038 {
22039 if (!current_template_parms || processing_template_parmlist)
22040 return false;
22041
22042 if (parser->implicit_template_scope)
22043 return true;
22044
22045 if (at_class_scope_p ()
22046 && TYPE_BEING_DEFINED (current_class_type))
22047 return parser->num_template_parameter_lists != 0;
22048
22049 return ((int) parser->num_template_parameter_lists > template_class_depth
22050 (current_class_type));
22051 }
22052
22053 /* Parse a parameter-declaration-clause.
22054
22055 parameter-declaration-clause:
22056 parameter-declaration-list [opt] ... [opt]
22057 parameter-declaration-list , ...
22058
22059 The parser flags FLAGS is used to control type-specifier parsing.
22060
22061 Returns a representation for the parameter declarations. A return
22062 value of NULL indicates a parameter-declaration-clause consisting
22063 only of an ellipsis. */
22064
22065 static tree
22066 cp_parser_parameter_declaration_clause (cp_parser* parser,
22067 cp_parser_flags flags)
22068 {
22069 tree parameters;
22070 cp_token *token;
22071 bool ellipsis_p;
22072
22073 temp_override<bool> cleanup
22074 (parser->auto_is_implicit_function_template_parm_p);
22075
22076 if (!processing_specialization
22077 && !processing_template_parmlist
22078 && !processing_explicit_instantiation
22079 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22080 actual function or a random abstract declarator. */
22081 && parser->default_arg_ok_p)
22082 if (!current_function_decl
22083 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
22084 parser->auto_is_implicit_function_template_parm_p = true;
22085
22086 /* Peek at the next token. */
22087 token = cp_lexer_peek_token (parser->lexer);
22088 /* Check for trivial parameter-declaration-clauses. */
22089 if (token->type == CPP_ELLIPSIS)
22090 {
22091 /* Consume the `...' token. */
22092 cp_lexer_consume_token (parser->lexer);
22093 return NULL_TREE;
22094 }
22095 else if (token->type == CPP_CLOSE_PAREN)
22096 /* There are no parameters. */
22097 return void_list_node;
22098 /* Check for `(void)', too, which is a special case. */
22099 else if (token->keyword == RID_VOID
22100 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22101 == CPP_CLOSE_PAREN))
22102 {
22103 /* Consume the `void' token. */
22104 cp_lexer_consume_token (parser->lexer);
22105 /* There are no parameters. */
22106 return void_list_node;
22107 }
22108
22109 /* Parse the parameter-declaration-list. */
22110 parameters = cp_parser_parameter_declaration_list (parser, flags);
22111 /* If a parse error occurred while parsing the
22112 parameter-declaration-list, then the entire
22113 parameter-declaration-clause is erroneous. */
22114 if (parameters == error_mark_node)
22115 return NULL_TREE;
22116
22117 /* Peek at the next token. */
22118 token = cp_lexer_peek_token (parser->lexer);
22119 /* If it's a `,', the clause should terminate with an ellipsis. */
22120 if (token->type == CPP_COMMA)
22121 {
22122 /* Consume the `,'. */
22123 cp_lexer_consume_token (parser->lexer);
22124 /* Expect an ellipsis. */
22125 ellipsis_p
22126 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
22127 }
22128 /* It might also be `...' if the optional trailing `,' was
22129 omitted. */
22130 else if (token->type == CPP_ELLIPSIS)
22131 {
22132 /* Consume the `...' token. */
22133 cp_lexer_consume_token (parser->lexer);
22134 /* And remember that we saw it. */
22135 ellipsis_p = true;
22136 }
22137 else
22138 ellipsis_p = false;
22139
22140 /* Finish the parameter list. */
22141 if (!ellipsis_p)
22142 parameters = chainon (parameters, void_list_node);
22143
22144 return parameters;
22145 }
22146
22147 /* Parse a parameter-declaration-list.
22148
22149 parameter-declaration-list:
22150 parameter-declaration
22151 parameter-declaration-list , parameter-declaration
22152
22153 The parser flags FLAGS is used to control type-specifier parsing.
22154
22155 Returns a representation of the parameter-declaration-list, as for
22156 cp_parser_parameter_declaration_clause. However, the
22157 `void_list_node' is never appended to the list. */
22158
22159 static tree
22160 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
22161 {
22162 tree parameters = NULL_TREE;
22163 tree *tail = &parameters;
22164 bool saved_in_unbraced_linkage_specification_p;
22165 int index = 0;
22166
22167 /* The special considerations that apply to a function within an
22168 unbraced linkage specifications do not apply to the parameters
22169 to the function. */
22170 saved_in_unbraced_linkage_specification_p
22171 = parser->in_unbraced_linkage_specification_p;
22172 parser->in_unbraced_linkage_specification_p = false;
22173
22174 /* Look for more parameters. */
22175 while (true)
22176 {
22177 cp_parameter_declarator *parameter;
22178 tree decl = error_mark_node;
22179 bool parenthesized_p = false;
22180
22181 /* Parse the parameter. */
22182 parameter
22183 = cp_parser_parameter_declaration (parser, flags,
22184 /*template_parm_p=*/false,
22185 &parenthesized_p);
22186
22187 /* We don't know yet if the enclosing context is deprecated, so wait
22188 and warn in grokparms if appropriate. */
22189 deprecated_state = DEPRECATED_SUPPRESS;
22190
22191 if (parameter)
22192 {
22193 decl = grokdeclarator (parameter->declarator,
22194 &parameter->decl_specifiers,
22195 PARM,
22196 parameter->default_argument != NULL_TREE,
22197 &parameter->decl_specifiers.attributes);
22198 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
22199 DECL_SOURCE_LOCATION (decl) = parameter->loc;
22200 }
22201
22202 deprecated_state = DEPRECATED_NORMAL;
22203
22204 /* If a parse error occurred parsing the parameter declaration,
22205 then the entire parameter-declaration-list is erroneous. */
22206 if (decl == error_mark_node)
22207 {
22208 parameters = error_mark_node;
22209 break;
22210 }
22211
22212 if (parameter->decl_specifiers.attributes)
22213 cplus_decl_attributes (&decl,
22214 parameter->decl_specifiers.attributes,
22215 0);
22216 if (DECL_NAME (decl))
22217 decl = pushdecl (decl);
22218
22219 if (decl != error_mark_node)
22220 {
22221 retrofit_lang_decl (decl);
22222 DECL_PARM_INDEX (decl) = ++index;
22223 DECL_PARM_LEVEL (decl) = function_parm_depth ();
22224 }
22225
22226 /* Add the new parameter to the list. */
22227 *tail = build_tree_list (parameter->default_argument, decl);
22228 tail = &TREE_CHAIN (*tail);
22229
22230 /* Peek at the next token. */
22231 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22232 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22233 /* These are for Objective-C++ */
22234 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22235 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22236 /* The parameter-declaration-list is complete. */
22237 break;
22238 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22239 {
22240 cp_token *token;
22241
22242 /* Peek at the next token. */
22243 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22244 /* If it's an ellipsis, then the list is complete. */
22245 if (token->type == CPP_ELLIPSIS)
22246 break;
22247 /* Otherwise, there must be more parameters. Consume the
22248 `,'. */
22249 cp_lexer_consume_token (parser->lexer);
22250 /* When parsing something like:
22251
22252 int i(float f, double d)
22253
22254 we can tell after seeing the declaration for "f" that we
22255 are not looking at an initialization of a variable "i",
22256 but rather at the declaration of a function "i".
22257
22258 Due to the fact that the parsing of template arguments
22259 (as specified to a template-id) requires backtracking we
22260 cannot use this technique when inside a template argument
22261 list. */
22262 if (!parser->in_template_argument_list_p
22263 && !parser->in_type_id_in_expr_p
22264 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22265 /* However, a parameter-declaration of the form
22266 "float(f)" (which is a valid declaration of a
22267 parameter "f") can also be interpreted as an
22268 expression (the conversion of "f" to "float"). */
22269 && !parenthesized_p)
22270 cp_parser_commit_to_tentative_parse (parser);
22271 }
22272 else
22273 {
22274 cp_parser_error (parser, "expected %<,%> or %<...%>");
22275 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22276 cp_parser_skip_to_closing_parenthesis (parser,
22277 /*recovering=*/true,
22278 /*or_comma=*/false,
22279 /*consume_paren=*/false);
22280 break;
22281 }
22282 }
22283
22284 parser->in_unbraced_linkage_specification_p
22285 = saved_in_unbraced_linkage_specification_p;
22286
22287 /* Reset implicit_template_scope if we are about to leave the function
22288 parameter list that introduced it. Note that for out-of-line member
22289 definitions, there will be one or more class scopes before we get to
22290 the template parameter scope. */
22291
22292 if (cp_binding_level *its = parser->implicit_template_scope)
22293 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
22294 {
22295 while (maybe_its->kind == sk_class)
22296 maybe_its = maybe_its->level_chain;
22297 if (maybe_its == its)
22298 {
22299 parser->implicit_template_parms = 0;
22300 parser->implicit_template_scope = 0;
22301 }
22302 }
22303
22304 return parameters;
22305 }
22306
22307 /* Parse a parameter declaration.
22308
22309 parameter-declaration:
22310 decl-specifier-seq ... [opt] declarator
22311 decl-specifier-seq declarator = assignment-expression
22312 decl-specifier-seq ... [opt] abstract-declarator [opt]
22313 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22314
22315 The parser flags FLAGS is used to control type-specifier parsing.
22316
22317 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22318 declares a template parameter. (In that case, a non-nested `>'
22319 token encountered during the parsing of the assignment-expression
22320 is not interpreted as a greater-than operator.)
22321
22322 Returns a representation of the parameter, or NULL if an error
22323 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22324 true iff the declarator is of the form "(p)". */
22325
22326 static cp_parameter_declarator *
22327 cp_parser_parameter_declaration (cp_parser *parser,
22328 cp_parser_flags flags,
22329 bool template_parm_p,
22330 bool *parenthesized_p)
22331 {
22332 int declares_class_or_enum;
22333 cp_decl_specifier_seq decl_specifiers;
22334 cp_declarator *declarator;
22335 tree default_argument;
22336 cp_token *token = NULL, *declarator_token_start = NULL;
22337 const char *saved_message;
22338 bool template_parameter_pack_p = false;
22339
22340 /* In a template parameter, `>' is not an operator.
22341
22342 [temp.param]
22343
22344 When parsing a default template-argument for a non-type
22345 template-parameter, the first non-nested `>' is taken as the end
22346 of the template parameter-list rather than a greater-than
22347 operator. */
22348
22349 /* Type definitions may not appear in parameter types. */
22350 saved_message = parser->type_definition_forbidden_message;
22351 parser->type_definition_forbidden_message
22352 = G_("types may not be defined in parameter types");
22353
22354 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
22355 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22356 (current_template_parms)) : 0);
22357
22358 /* Parse the declaration-specifiers. */
22359 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22360 cp_parser_decl_specifier_seq (parser,
22361 flags,
22362 &decl_specifiers,
22363 &declares_class_or_enum);
22364
22365 /* Complain about missing 'typename' or other invalid type names. */
22366 if (!decl_specifiers.any_type_specifiers_p
22367 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22368 decl_specifiers.type = error_mark_node;
22369
22370 /* If an error occurred, there's no reason to attempt to parse the
22371 rest of the declaration. */
22372 if (cp_parser_error_occurred (parser))
22373 {
22374 parser->type_definition_forbidden_message = saved_message;
22375 return NULL;
22376 }
22377
22378 /* Peek at the next token. */
22379 token = cp_lexer_peek_token (parser->lexer);
22380
22381 /* If the next token is a `)', `,', `=', `>', or `...', then there
22382 is no declarator. However, when variadic templates are enabled,
22383 there may be a declarator following `...'. */
22384 if (token->type == CPP_CLOSE_PAREN
22385 || token->type == CPP_COMMA
22386 || token->type == CPP_EQ
22387 || token->type == CPP_GREATER)
22388 {
22389 declarator = NULL;
22390 if (parenthesized_p)
22391 *parenthesized_p = false;
22392 }
22393 /* Otherwise, there should be a declarator. */
22394 else
22395 {
22396 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22397 parser->default_arg_ok_p = false;
22398
22399 /* After seeing a decl-specifier-seq, if the next token is not a
22400 "(", there is no possibility that the code is a valid
22401 expression. Therefore, if parsing tentatively, we commit at
22402 this point. */
22403 if (!parser->in_template_argument_list_p
22404 /* In an expression context, having seen:
22405
22406 (int((char ...
22407
22408 we cannot be sure whether we are looking at a
22409 function-type (taking a "char" as a parameter) or a cast
22410 of some object of type "char" to "int". */
22411 && !parser->in_type_id_in_expr_p
22412 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22413 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22414 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22415 cp_parser_commit_to_tentative_parse (parser);
22416 /* Parse the declarator. */
22417 declarator_token_start = token;
22418 declarator = cp_parser_declarator (parser,
22419 CP_PARSER_DECLARATOR_EITHER,
22420 CP_PARSER_FLAGS_NONE,
22421 /*ctor_dtor_or_conv_p=*/NULL,
22422 parenthesized_p,
22423 /*member_p=*/false,
22424 /*friend_p=*/false,
22425 /*static_p=*/false);
22426 parser->default_arg_ok_p = saved_default_arg_ok_p;
22427 /* After the declarator, allow more attributes. */
22428 decl_specifiers.attributes
22429 = attr_chainon (decl_specifiers.attributes,
22430 cp_parser_attributes_opt (parser));
22431
22432 /* If the declarator is a template parameter pack, remember that and
22433 clear the flag in the declarator itself so we don't get errors
22434 from grokdeclarator. */
22435 if (template_parm_p && declarator && declarator->parameter_pack_p)
22436 {
22437 declarator->parameter_pack_p = false;
22438 template_parameter_pack_p = true;
22439 }
22440 }
22441
22442 /* If the next token is an ellipsis, and we have not seen a declarator
22443 name, and if either the type of the declarator contains parameter
22444 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22445 for, eg, abbreviated integral type names), then we actually have a
22446 parameter pack expansion expression. Otherwise, leave the ellipsis
22447 for a C-style variadic function. */
22448 token = cp_lexer_peek_token (parser->lexer);
22449
22450 /* If a function parameter pack was specified and an implicit template
22451 parameter was introduced during cp_parser_parameter_declaration,
22452 change any implicit parameters introduced into packs. */
22453 if (parser->implicit_template_parms
22454 && ((token->type == CPP_ELLIPSIS
22455 && declarator_can_be_parameter_pack (declarator))
22456 || (declarator && declarator->parameter_pack_p)))
22457 {
22458 int latest_template_parm_idx = TREE_VEC_LENGTH
22459 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
22460
22461 if (latest_template_parm_idx != template_parm_idx)
22462 decl_specifiers.type = convert_generic_types_to_packs
22463 (decl_specifiers.type,
22464 template_parm_idx, latest_template_parm_idx);
22465 }
22466
22467 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22468 {
22469 tree type = decl_specifiers.type;
22470
22471 if (type && DECL_P (type))
22472 type = TREE_TYPE (type);
22473
22474 if (((type
22475 && TREE_CODE (type) != TYPE_PACK_EXPANSION
22476 && (template_parm_p || uses_parameter_packs (type)))
22477 || (!type && template_parm_p))
22478 && declarator_can_be_parameter_pack (declarator))
22479 {
22480 /* Consume the `...'. */
22481 cp_lexer_consume_token (parser->lexer);
22482 maybe_warn_variadic_templates ();
22483
22484 /* Build a pack expansion type */
22485 if (template_parm_p)
22486 template_parameter_pack_p = true;
22487 else if (declarator)
22488 declarator->parameter_pack_p = true;
22489 else
22490 decl_specifiers.type = make_pack_expansion (type);
22491 }
22492 }
22493
22494 /* The restriction on defining new types applies only to the type
22495 of the parameter, not to the default argument. */
22496 parser->type_definition_forbidden_message = saved_message;
22497
22498 /* If the next token is `=', then process a default argument. */
22499 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22500 {
22501 tree type = decl_specifiers.type;
22502 token = cp_lexer_peek_token (parser->lexer);
22503 /* If we are defining a class, then the tokens that make up the
22504 default argument must be saved and processed later. */
22505 if (!template_parm_p && at_class_scope_p ()
22506 && TYPE_BEING_DEFINED (current_class_type)
22507 && !LAMBDA_TYPE_P (current_class_type))
22508 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22509
22510 // A constrained-type-specifier may declare a type template-parameter.
22511 else if (declares_constrained_type_template_parameter (type))
22512 default_argument
22513 = cp_parser_default_type_template_argument (parser);
22514
22515 // A constrained-type-specifier may declare a template-template-parameter.
22516 else if (declares_constrained_template_template_parameter (type))
22517 default_argument
22518 = cp_parser_default_template_template_argument (parser);
22519
22520 /* Outside of a class definition, we can just parse the
22521 assignment-expression. */
22522 else
22523 default_argument
22524 = cp_parser_default_argument (parser, template_parm_p);
22525
22526 if (!parser->default_arg_ok_p)
22527 {
22528 permerror (token->location,
22529 "default arguments are only "
22530 "permitted for function parameters");
22531 }
22532 else if ((declarator && declarator->parameter_pack_p)
22533 || template_parameter_pack_p
22534 || (decl_specifiers.type
22535 && PACK_EXPANSION_P (decl_specifiers.type)))
22536 {
22537 /* Find the name of the parameter pack. */
22538 cp_declarator *id_declarator = declarator;
22539 while (id_declarator && id_declarator->kind != cdk_id)
22540 id_declarator = id_declarator->declarator;
22541
22542 if (id_declarator && id_declarator->kind == cdk_id)
22543 error_at (declarator_token_start->location,
22544 template_parm_p
22545 ? G_("template parameter pack %qD "
22546 "cannot have a default argument")
22547 : G_("parameter pack %qD cannot have "
22548 "a default argument"),
22549 id_declarator->u.id.unqualified_name);
22550 else
22551 error_at (declarator_token_start->location,
22552 template_parm_p
22553 ? G_("template parameter pack cannot have "
22554 "a default argument")
22555 : G_("parameter pack cannot have a "
22556 "default argument"));
22557
22558 default_argument = NULL_TREE;
22559 }
22560 }
22561 else
22562 default_argument = NULL_TREE;
22563
22564 if (default_argument)
22565 STRIP_ANY_LOCATION_WRAPPER (default_argument);
22566
22567 /* Generate a location for the parameter, ranging from the start of the
22568 initial token to the end of the final token (using input_location for
22569 the latter, set up by cp_lexer_set_source_position_from_token when
22570 consuming tokens).
22571
22572 If we have a identifier, then use it for the caret location, e.g.
22573
22574 extern int callee (int one, int (*two)(int, int), float three);
22575 ~~~~~~^~~~~~~~~~~~~~
22576
22577 otherwise, reuse the start location for the caret location e.g.:
22578
22579 extern int callee (int one, int (*)(int, int), float three);
22580 ^~~~~~~~~~~~~~~~~
22581
22582 */
22583 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22584 ? declarator->id_loc
22585 : decl_spec_token_start->location);
22586 location_t param_loc = make_location (caret_loc,
22587 decl_spec_token_start->location,
22588 input_location);
22589
22590 return make_parameter_declarator (&decl_specifiers,
22591 declarator,
22592 default_argument,
22593 param_loc,
22594 template_parameter_pack_p);
22595 }
22596
22597 /* Parse a default argument and return it.
22598
22599 TEMPLATE_PARM_P is true if this is a default argument for a
22600 non-type template parameter. */
22601 static tree
22602 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22603 {
22604 tree default_argument = NULL_TREE;
22605 bool saved_greater_than_is_operator_p;
22606 unsigned char saved_local_variables_forbidden_p;
22607 bool non_constant_p, is_direct_init;
22608
22609 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22610 set correctly. */
22611 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22612 parser->greater_than_is_operator_p = !template_parm_p;
22613 /* Local variable names (and the `this' keyword) may not
22614 appear in a default argument. */
22615 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22616 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
22617 /* Parse the assignment-expression. */
22618 if (template_parm_p)
22619 push_deferring_access_checks (dk_no_deferred);
22620 tree saved_class_ptr = NULL_TREE;
22621 tree saved_class_ref = NULL_TREE;
22622 /* The "this" pointer is not valid in a default argument. */
22623 if (cfun)
22624 {
22625 saved_class_ptr = current_class_ptr;
22626 cp_function_chain->x_current_class_ptr = NULL_TREE;
22627 saved_class_ref = current_class_ref;
22628 cp_function_chain->x_current_class_ref = NULL_TREE;
22629 }
22630 default_argument
22631 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22632 /* Restore the "this" pointer. */
22633 if (cfun)
22634 {
22635 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22636 cp_function_chain->x_current_class_ref = saved_class_ref;
22637 }
22638 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22639 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22640 if (template_parm_p)
22641 pop_deferring_access_checks ();
22642 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22643 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22644
22645 return default_argument;
22646 }
22647
22648 /* Parse a function-body.
22649
22650 function-body:
22651 compound_statement */
22652
22653 static void
22654 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22655 {
22656 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22657 ? BCS_TRY_BLOCK : BCS_NORMAL),
22658 true);
22659 }
22660
22661 /* Parse a ctor-initializer-opt followed by a function-body. Return
22662 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22663 is true we are parsing a function-try-block. */
22664
22665 static void
22666 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22667 bool in_function_try_block)
22668 {
22669 tree body, list;
22670 const bool check_body_p
22671 = (DECL_CONSTRUCTOR_P (current_function_decl)
22672 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
22673 tree last = NULL;
22674
22675 if (in_function_try_block
22676 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22677 && cxx_dialect < cxx2a)
22678 {
22679 if (DECL_CONSTRUCTOR_P (current_function_decl))
22680 pedwarn (input_location, 0,
22681 "function-try-block body of %<constexpr%> constructor only "
22682 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22683 else
22684 pedwarn (input_location, 0,
22685 "function-try-block body of %<constexpr%> function only "
22686 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22687 }
22688
22689 /* Begin the function body. */
22690 body = begin_function_body ();
22691 /* Parse the optional ctor-initializer. */
22692 cp_parser_ctor_initializer_opt (parser);
22693
22694 /* If we're parsing a constexpr constructor definition, we need
22695 to check that the constructor body is indeed empty. However,
22696 before we get to cp_parser_function_body lot of junk has been
22697 generated, so we can't just check that we have an empty block.
22698 Rather we take a snapshot of the outermost block, and check whether
22699 cp_parser_function_body changed its state. */
22700 if (check_body_p)
22701 {
22702 list = cur_stmt_list;
22703 if (STATEMENT_LIST_TAIL (list))
22704 last = STATEMENT_LIST_TAIL (list)->stmt;
22705 }
22706 /* Parse the function-body. */
22707 cp_parser_function_body (parser, in_function_try_block);
22708 if (check_body_p)
22709 check_constexpr_ctor_body (last, list, /*complain=*/true);
22710 /* Finish the function body. */
22711 finish_function_body (body);
22712 }
22713
22714 /* Parse an initializer.
22715
22716 initializer:
22717 = initializer-clause
22718 ( expression-list )
22719
22720 Returns an expression representing the initializer. If no
22721 initializer is present, NULL_TREE is returned.
22722
22723 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22724 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22725 set to TRUE if there is no initializer present. If there is an
22726 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22727 is set to true; otherwise it is set to false. */
22728
22729 static tree
22730 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22731 bool* non_constant_p, bool subexpression_p)
22732 {
22733 cp_token *token;
22734 tree init;
22735
22736 /* Peek at the next token. */
22737 token = cp_lexer_peek_token (parser->lexer);
22738
22739 /* Let our caller know whether or not this initializer was
22740 parenthesized. */
22741 *is_direct_init = (token->type != CPP_EQ);
22742 /* Assume that the initializer is constant. */
22743 *non_constant_p = false;
22744
22745 if (token->type == CPP_EQ)
22746 {
22747 /* Consume the `='. */
22748 cp_lexer_consume_token (parser->lexer);
22749 /* Parse the initializer-clause. */
22750 init = cp_parser_initializer_clause (parser, non_constant_p);
22751 }
22752 else if (token->type == CPP_OPEN_PAREN)
22753 {
22754 vec<tree, va_gc> *vec;
22755 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22756 /*cast_p=*/false,
22757 /*allow_expansion_p=*/true,
22758 non_constant_p);
22759 if (vec == NULL)
22760 return error_mark_node;
22761 init = build_tree_list_vec (vec);
22762 release_tree_vector (vec);
22763 }
22764 else if (token->type == CPP_OPEN_BRACE)
22765 {
22766 cp_lexer_set_source_position (parser->lexer);
22767 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22768 init = cp_parser_braced_list (parser, non_constant_p);
22769 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22770 }
22771 else
22772 {
22773 /* Anything else is an error. */
22774 cp_parser_error (parser, "expected initializer");
22775 init = error_mark_node;
22776 }
22777
22778 if (!subexpression_p && check_for_bare_parameter_packs (init))
22779 init = error_mark_node;
22780
22781 return init;
22782 }
22783
22784 /* Parse an initializer-clause.
22785
22786 initializer-clause:
22787 assignment-expression
22788 braced-init-list
22789
22790 Returns an expression representing the initializer.
22791
22792 If the `assignment-expression' production is used the value
22793 returned is simply a representation for the expression.
22794
22795 Otherwise, calls cp_parser_braced_list. */
22796
22797 static cp_expr
22798 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22799 {
22800 cp_expr initializer;
22801
22802 /* Assume the expression is constant. */
22803 *non_constant_p = false;
22804
22805 /* If it is not a `{', then we are looking at an
22806 assignment-expression. */
22807 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22808 {
22809 initializer
22810 = cp_parser_constant_expression (parser,
22811 /*allow_non_constant_p=*/true,
22812 non_constant_p);
22813 }
22814 else
22815 initializer = cp_parser_braced_list (parser, non_constant_p);
22816
22817 return initializer;
22818 }
22819
22820 /* Parse a brace-enclosed initializer list.
22821
22822 braced-init-list:
22823 { initializer-list , [opt] }
22824 { designated-initializer-list , [opt] }
22825 { }
22826
22827 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22828 the elements of the initializer-list (or NULL, if the last
22829 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22830 NULL_TREE. There is no way to detect whether or not the optional
22831 trailing `,' was provided. NON_CONSTANT_P is as for
22832 cp_parser_initializer. */
22833
22834 static cp_expr
22835 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22836 {
22837 tree initializer;
22838 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22839
22840 /* Consume the `{' token. */
22841 matching_braces braces;
22842 braces.require_open (parser);
22843 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22844 initializer = make_node (CONSTRUCTOR);
22845 /* If it's not a `}', then there is a non-trivial initializer. */
22846 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22847 {
22848 bool designated;
22849 /* Parse the initializer list. */
22850 CONSTRUCTOR_ELTS (initializer)
22851 = cp_parser_initializer_list (parser, non_constant_p, &designated);
22852 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
22853 /* A trailing `,' token is allowed. */
22854 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22855 cp_lexer_consume_token (parser->lexer);
22856 }
22857 else
22858 *non_constant_p = false;
22859 /* Now, there should be a trailing `}'. */
22860 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22861 braces.require_close (parser);
22862 TREE_TYPE (initializer) = init_list_type_node;
22863
22864 cp_expr result (initializer);
22865 /* Build a location of the form:
22866 { ... }
22867 ^~~~~~~
22868 with caret==start at the open brace, finish at the close brace. */
22869 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22870 result.set_location (combined_loc);
22871 return result;
22872 }
22873
22874 /* Consume tokens up to, and including, the next non-nested closing `]'.
22875 Returns true iff we found a closing `]'. */
22876
22877 static bool
22878 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22879 {
22880 unsigned square_depth = 0;
22881
22882 while (true)
22883 {
22884 cp_token * token = cp_lexer_peek_token (parser->lexer);
22885
22886 switch (token->type)
22887 {
22888 case CPP_PRAGMA_EOL:
22889 if (!parser->lexer->in_pragma)
22890 break;
22891 /* FALLTHRU */
22892 case CPP_EOF:
22893 /* If we've run out of tokens, then there is no closing `]'. */
22894 return false;
22895
22896 case CPP_OPEN_SQUARE:
22897 ++square_depth;
22898 break;
22899
22900 case CPP_CLOSE_SQUARE:
22901 if (!square_depth--)
22902 {
22903 cp_lexer_consume_token (parser->lexer);
22904 return true;
22905 }
22906 break;
22907
22908 default:
22909 break;
22910 }
22911
22912 /* Consume the token. */
22913 cp_lexer_consume_token (parser->lexer);
22914 }
22915 }
22916
22917 /* Return true if we are looking at an array-designator, false otherwise. */
22918
22919 static bool
22920 cp_parser_array_designator_p (cp_parser *parser)
22921 {
22922 /* Consume the `['. */
22923 cp_lexer_consume_token (parser->lexer);
22924
22925 cp_lexer_save_tokens (parser->lexer);
22926
22927 /* Skip tokens until the next token is a closing square bracket.
22928 If we find the closing `]', and the next token is a `=', then
22929 we are looking at an array designator. */
22930 bool array_designator_p
22931 = (cp_parser_skip_to_closing_square_bracket (parser)
22932 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22933
22934 /* Roll back the tokens we skipped. */
22935 cp_lexer_rollback_tokens (parser->lexer);
22936
22937 return array_designator_p;
22938 }
22939
22940 /* Parse an initializer-list.
22941
22942 initializer-list:
22943 initializer-clause ... [opt]
22944 initializer-list , initializer-clause ... [opt]
22945
22946 C++2A Extension:
22947
22948 designated-initializer-list:
22949 designated-initializer-clause
22950 designated-initializer-list , designated-initializer-clause
22951
22952 designated-initializer-clause:
22953 designator brace-or-equal-initializer
22954
22955 designator:
22956 . identifier
22957
22958 GNU Extension:
22959
22960 initializer-list:
22961 designation initializer-clause ...[opt]
22962 initializer-list , designation initializer-clause ...[opt]
22963
22964 designation:
22965 . identifier =
22966 identifier :
22967 [ constant-expression ] =
22968
22969 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22970 for the initializer. If the INDEX of the elt is non-NULL, it is the
22971 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22972 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
22973 are any designators. */
22974
22975 static vec<constructor_elt, va_gc> *
22976 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
22977 bool *designated)
22978 {
22979 vec<constructor_elt, va_gc> *v = NULL;
22980 bool first_p = true;
22981 tree first_designator = NULL_TREE;
22982
22983 /* Assume all of the expressions are constant. */
22984 *non_constant_p = false;
22985
22986 /* Parse the rest of the list. */
22987 while (true)
22988 {
22989 cp_token *token;
22990 tree designator;
22991 tree initializer;
22992 bool clause_non_constant_p;
22993 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22994
22995 /* Handle the C++2A syntax, '. id ='. */
22996 if ((cxx_dialect >= cxx2a
22997 || cp_parser_allow_gnu_extensions_p (parser))
22998 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22999 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
23000 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
23001 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
23002 == CPP_OPEN_BRACE)))
23003 {
23004 if (cxx_dialect < cxx2a)
23005 pedwarn (loc, OPT_Wpedantic,
23006 "C++ designated initializers only available with "
23007 "%<-std=c++2a%> or %<-std=gnu++2a%>");
23008 /* Consume the `.'. */
23009 cp_lexer_consume_token (parser->lexer);
23010 /* Consume the identifier. */
23011 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23012 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23013 /* Consume the `='. */
23014 cp_lexer_consume_token (parser->lexer);
23015 }
23016 /* Also, if the next token is an identifier and the following one is a
23017 colon, we are looking at the GNU designated-initializer
23018 syntax. */
23019 else if (cp_parser_allow_gnu_extensions_p (parser)
23020 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
23021 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23022 == CPP_COLON))
23023 {
23024 /* Warn the user that they are using an extension. */
23025 pedwarn (loc, OPT_Wpedantic,
23026 "ISO C++ does not allow GNU designated initializers");
23027 /* Consume the identifier. */
23028 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23029 /* Consume the `:'. */
23030 cp_lexer_consume_token (parser->lexer);
23031 }
23032 /* Also handle C99 array designators, '[ const ] ='. */
23033 else if (cp_parser_allow_gnu_extensions_p (parser)
23034 && !c_dialect_objc ()
23035 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23036 {
23037 /* In C++11, [ could start a lambda-introducer. */
23038 bool non_const = false;
23039
23040 cp_parser_parse_tentatively (parser);
23041
23042 if (!cp_parser_array_designator_p (parser))
23043 {
23044 cp_parser_simulate_error (parser);
23045 designator = NULL_TREE;
23046 }
23047 else
23048 {
23049 designator = cp_parser_constant_expression (parser, true,
23050 &non_const);
23051 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23052 cp_parser_require (parser, CPP_EQ, RT_EQ);
23053 }
23054
23055 if (!cp_parser_parse_definitely (parser))
23056 designator = NULL_TREE;
23057 else if (non_const
23058 && (!require_potential_rvalue_constant_expression
23059 (designator)))
23060 designator = NULL_TREE;
23061 if (designator)
23062 /* Warn the user that they are using an extension. */
23063 pedwarn (loc, OPT_Wpedantic,
23064 "ISO C++ does not allow C99 designated initializers");
23065 }
23066 else
23067 designator = NULL_TREE;
23068
23069 if (first_p)
23070 {
23071 first_designator = designator;
23072 first_p = false;
23073 }
23074 else if (cxx_dialect >= cxx2a
23075 && first_designator != error_mark_node
23076 && (!first_designator != !designator))
23077 {
23078 error_at (loc, "either all initializer clauses should be designated "
23079 "or none of them should be");
23080 first_designator = error_mark_node;
23081 }
23082 else if (cxx_dialect < cxx2a && !first_designator)
23083 first_designator = designator;
23084
23085 /* Parse the initializer. */
23086 initializer = cp_parser_initializer_clause (parser,
23087 &clause_non_constant_p);
23088 /* If any clause is non-constant, so is the entire initializer. */
23089 if (clause_non_constant_p)
23090 *non_constant_p = true;
23091
23092 /* If we have an ellipsis, this is an initializer pack
23093 expansion. */
23094 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23095 {
23096 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23097
23098 /* Consume the `...'. */
23099 cp_lexer_consume_token (parser->lexer);
23100
23101 if (designator && cxx_dialect >= cxx2a)
23102 error_at (loc,
23103 "%<...%> not allowed in designated initializer list");
23104
23105 /* Turn the initializer into an initializer expansion. */
23106 initializer = make_pack_expansion (initializer);
23107 }
23108
23109 /* Add it to the vector. */
23110 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
23111
23112 /* If the next token is not a comma, we have reached the end of
23113 the list. */
23114 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23115 break;
23116
23117 /* Peek at the next token. */
23118 token = cp_lexer_peek_nth_token (parser->lexer, 2);
23119 /* If the next token is a `}', then we're still done. An
23120 initializer-clause can have a trailing `,' after the
23121 initializer-list and before the closing `}'. */
23122 if (token->type == CPP_CLOSE_BRACE)
23123 break;
23124
23125 /* Consume the `,' token. */
23126 cp_lexer_consume_token (parser->lexer);
23127 }
23128
23129 /* The same identifier shall not appear in multiple designators
23130 of a designated-initializer-list. */
23131 if (first_designator)
23132 {
23133 unsigned int i;
23134 tree designator, val;
23135 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23136 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23137 {
23138 if (IDENTIFIER_MARKED (designator))
23139 {
23140 error_at (cp_expr_loc_or_loc (val, input_location),
23141 "%<.%s%> designator used multiple times in "
23142 "the same initializer list",
23143 IDENTIFIER_POINTER (designator));
23144 (*v)[i].index = error_mark_node;
23145 }
23146 else
23147 IDENTIFIER_MARKED (designator) = 1;
23148 }
23149 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23150 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23151 IDENTIFIER_MARKED (designator) = 0;
23152 }
23153
23154 *designated = first_designator != NULL_TREE;
23155 return v;
23156 }
23157
23158 /* Classes [gram.class] */
23159
23160 /* Parse a class-name.
23161
23162 class-name:
23163 identifier
23164 template-id
23165
23166 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23167 to indicate that names looked up in dependent types should be
23168 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23169 keyword has been used to indicate that the name that appears next
23170 is a template. TAG_TYPE indicates the explicit tag given before
23171 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23172 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23173 is the class being defined in a class-head. If ENUM_OK is TRUE,
23174 enum-names are also accepted.
23175
23176 Returns the TYPE_DECL representing the class. */
23177
23178 static tree
23179 cp_parser_class_name (cp_parser *parser,
23180 bool typename_keyword_p,
23181 bool template_keyword_p,
23182 enum tag_types tag_type,
23183 bool check_dependency_p,
23184 bool class_head_p,
23185 bool is_declaration,
23186 bool enum_ok)
23187 {
23188 tree decl;
23189 tree scope;
23190 bool typename_p;
23191 cp_token *token;
23192 tree identifier = NULL_TREE;
23193
23194 /* All class-names start with an identifier. */
23195 token = cp_lexer_peek_token (parser->lexer);
23196 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
23197 {
23198 cp_parser_error (parser, "expected class-name");
23199 return error_mark_node;
23200 }
23201
23202 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23203 to a template-id, so we save it here. */
23204 scope = parser->scope;
23205 if (scope == error_mark_node)
23206 return error_mark_node;
23207
23208 /* Any name names a type if we're following the `typename' keyword
23209 in a qualified name where the enclosing scope is type-dependent. */
23210 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
23211 && dependent_type_p (scope));
23212 /* Handle the common case (an identifier, but not a template-id)
23213 efficiently. */
23214 if (token->type == CPP_NAME
23215 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
23216 {
23217 cp_token *identifier_token;
23218 bool ambiguous_p;
23219
23220 /* Look for the identifier. */
23221 identifier_token = cp_lexer_peek_token (parser->lexer);
23222 ambiguous_p = identifier_token->error_reported;
23223 identifier = cp_parser_identifier (parser);
23224 /* If the next token isn't an identifier, we are certainly not
23225 looking at a class-name. */
23226 if (identifier == error_mark_node)
23227 decl = error_mark_node;
23228 /* If we know this is a type-name, there's no need to look it
23229 up. */
23230 else if (typename_p)
23231 decl = identifier;
23232 else
23233 {
23234 tree ambiguous_decls;
23235 /* If we already know that this lookup is ambiguous, then
23236 we've already issued an error message; there's no reason
23237 to check again. */
23238 if (ambiguous_p)
23239 {
23240 cp_parser_simulate_error (parser);
23241 return error_mark_node;
23242 }
23243 /* If the next token is a `::', then the name must be a type
23244 name.
23245
23246 [basic.lookup.qual]
23247
23248 During the lookup for a name preceding the :: scope
23249 resolution operator, object, function, and enumerator
23250 names are ignored. */
23251 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23252 tag_type = scope_type;
23253 /* Look up the name. */
23254 decl = cp_parser_lookup_name (parser, identifier,
23255 tag_type,
23256 /*is_template=*/false,
23257 /*is_namespace=*/false,
23258 check_dependency_p,
23259 &ambiguous_decls,
23260 identifier_token->location);
23261 if (ambiguous_decls)
23262 {
23263 if (cp_parser_parsing_tentatively (parser))
23264 cp_parser_simulate_error (parser);
23265 return error_mark_node;
23266 }
23267 }
23268 }
23269 else
23270 {
23271 /* Try a template-id. */
23272 decl = cp_parser_template_id (parser, template_keyword_p,
23273 check_dependency_p,
23274 tag_type,
23275 is_declaration);
23276 if (decl == error_mark_node)
23277 return error_mark_node;
23278 }
23279
23280 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
23281
23282 /* If this is a typename, create a TYPENAME_TYPE. */
23283 if (typename_p
23284 && decl != error_mark_node
23285 && !is_overloaded_fn (decl))
23286 {
23287 decl = make_typename_type (scope, decl, typename_type,
23288 /*complain=*/tf_error);
23289 if (decl != error_mark_node)
23290 decl = TYPE_NAME (decl);
23291 }
23292
23293 decl = strip_using_decl (decl);
23294
23295 /* Check to see that it is really the name of a class. */
23296 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
23297 && identifier_p (TREE_OPERAND (decl, 0))
23298 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23299 /* Situations like this:
23300
23301 template <typename T> struct A {
23302 typename T::template X<int>::I i;
23303 };
23304
23305 are problematic. Is `T::template X<int>' a class-name? The
23306 standard does not seem to be definitive, but there is no other
23307 valid interpretation of the following `::'. Therefore, those
23308 names are considered class-names. */
23309 {
23310 decl = make_typename_type (scope, decl, tag_type, tf_error);
23311 if (decl != error_mark_node)
23312 decl = TYPE_NAME (decl);
23313 }
23314 else if (TREE_CODE (decl) != TYPE_DECL
23315 || TREE_TYPE (decl) == error_mark_node
23316 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
23317 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
23318 /* In Objective-C 2.0, a classname followed by '.' starts a
23319 dot-syntax expression, and it's not a type-name. */
23320 || (c_dialect_objc ()
23321 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
23322 && objc_is_class_name (decl)))
23323 decl = error_mark_node;
23324
23325 if (decl == error_mark_node)
23326 cp_parser_error (parser, "expected class-name");
23327 else if (identifier && !parser->scope)
23328 maybe_note_name_used_in_class (identifier, decl);
23329
23330 return decl;
23331 }
23332
23333 /* Parse a class-specifier.
23334
23335 class-specifier:
23336 class-head { member-specification [opt] }
23337
23338 Returns the TREE_TYPE representing the class. */
23339
23340 static tree
23341 cp_parser_class_specifier_1 (cp_parser* parser)
23342 {
23343 tree type;
23344 tree attributes = NULL_TREE;
23345 bool nested_name_specifier_p;
23346 unsigned saved_num_template_parameter_lists;
23347 bool saved_in_function_body;
23348 unsigned char in_statement;
23349 bool in_switch_statement_p;
23350 bool saved_in_unbraced_linkage_specification_p;
23351 tree old_scope = NULL_TREE;
23352 tree scope = NULL_TREE;
23353 cp_token *closing_brace;
23354
23355 push_deferring_access_checks (dk_no_deferred);
23356
23357 /* Parse the class-head. */
23358 type = cp_parser_class_head (parser,
23359 &nested_name_specifier_p);
23360 /* If the class-head was a semantic disaster, skip the entire body
23361 of the class. */
23362 if (!type)
23363 {
23364 cp_parser_skip_to_end_of_block_or_statement (parser);
23365 pop_deferring_access_checks ();
23366 return error_mark_node;
23367 }
23368
23369 /* Look for the `{'. */
23370 matching_braces braces;
23371 if (!braces.require_open (parser))
23372 {
23373 pop_deferring_access_checks ();
23374 return error_mark_node;
23375 }
23376
23377 cp_ensure_no_omp_declare_simd (parser);
23378 cp_ensure_no_oacc_routine (parser);
23379
23380 /* Issue an error message if type-definitions are forbidden here. */
23381 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
23382 /* Remember that we are defining one more class. */
23383 ++parser->num_classes_being_defined;
23384 /* Inside the class, surrounding template-parameter-lists do not
23385 apply. */
23386 saved_num_template_parameter_lists
23387 = parser->num_template_parameter_lists;
23388 parser->num_template_parameter_lists = 0;
23389 /* We are not in a function body. */
23390 saved_in_function_body = parser->in_function_body;
23391 parser->in_function_body = false;
23392 /* Or in a loop. */
23393 in_statement = parser->in_statement;
23394 parser->in_statement = 0;
23395 /* Or in a switch. */
23396 in_switch_statement_p = parser->in_switch_statement_p;
23397 parser->in_switch_statement_p = false;
23398 /* We are not immediately inside an extern "lang" block. */
23399 saved_in_unbraced_linkage_specification_p
23400 = parser->in_unbraced_linkage_specification_p;
23401 parser->in_unbraced_linkage_specification_p = false;
23402
23403 // Associate constraints with the type.
23404 if (flag_concepts)
23405 type = associate_classtype_constraints (type);
23406
23407 /* Start the class. */
23408 if (nested_name_specifier_p)
23409 {
23410 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23411 old_scope = push_inner_scope (scope);
23412 }
23413 type = begin_class_definition (type);
23414
23415 if (type == error_mark_node)
23416 /* If the type is erroneous, skip the entire body of the class. */
23417 cp_parser_skip_to_closing_brace (parser);
23418 else
23419 /* Parse the member-specification. */
23420 cp_parser_member_specification_opt (parser);
23421
23422 /* Look for the trailing `}'. */
23423 closing_brace = braces.require_close (parser);
23424 /* Look for trailing attributes to apply to this class. */
23425 if (cp_parser_allow_gnu_extensions_p (parser))
23426 attributes = cp_parser_gnu_attributes_opt (parser);
23427 if (type != error_mark_node)
23428 type = finish_struct (type, attributes);
23429 if (nested_name_specifier_p)
23430 pop_inner_scope (old_scope, scope);
23431
23432 /* We've finished a type definition. Check for the common syntax
23433 error of forgetting a semicolon after the definition. We need to
23434 be careful, as we can't just check for not-a-semicolon and be done
23435 with it; the user might have typed:
23436
23437 class X { } c = ...;
23438 class X { } *p = ...;
23439
23440 and so forth. Instead, enumerate all the possible tokens that
23441 might follow this production; if we don't see one of them, then
23442 complain and silently insert the semicolon. */
23443 {
23444 cp_token *token = cp_lexer_peek_token (parser->lexer);
23445 bool want_semicolon = true;
23446
23447 if (cp_next_tokens_can_be_std_attribute_p (parser))
23448 /* Don't try to parse c++11 attributes here. As per the
23449 grammar, that should be a task for
23450 cp_parser_decl_specifier_seq. */
23451 want_semicolon = false;
23452
23453 switch (token->type)
23454 {
23455 case CPP_NAME:
23456 case CPP_SEMICOLON:
23457 case CPP_MULT:
23458 case CPP_AND:
23459 case CPP_OPEN_PAREN:
23460 case CPP_CLOSE_PAREN:
23461 case CPP_COMMA:
23462 want_semicolon = false;
23463 break;
23464
23465 /* While it's legal for type qualifiers and storage class
23466 specifiers to follow type definitions in the grammar, only
23467 compiler testsuites contain code like that. Assume that if
23468 we see such code, then what we're really seeing is a case
23469 like:
23470
23471 class X { }
23472 const <type> var = ...;
23473
23474 or
23475
23476 class Y { }
23477 static <type> func (...) ...
23478
23479 i.e. the qualifier or specifier applies to the next
23480 declaration. To do so, however, we need to look ahead one
23481 more token to see if *that* token is a type specifier.
23482
23483 This code could be improved to handle:
23484
23485 class Z { }
23486 static const <type> var = ...; */
23487 case CPP_KEYWORD:
23488 if (keyword_is_decl_specifier (token->keyword))
23489 {
23490 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
23491
23492 /* Handling user-defined types here would be nice, but very
23493 tricky. */
23494 want_semicolon
23495 = (lookahead->type == CPP_KEYWORD
23496 && keyword_begins_type_specifier (lookahead->keyword));
23497 }
23498 break;
23499 default:
23500 break;
23501 }
23502
23503 /* If we don't have a type, then something is very wrong and we
23504 shouldn't try to do anything clever. Likewise for not seeing the
23505 closing brace. */
23506 if (closing_brace && TYPE_P (type) && want_semicolon)
23507 {
23508 /* Locate the closing brace. */
23509 cp_token_position prev
23510 = cp_lexer_previous_token_position (parser->lexer);
23511 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
23512 location_t loc = prev_token->location;
23513
23514 /* We want to suggest insertion of a ';' immediately *after* the
23515 closing brace, so, if we can, offset the location by 1 column. */
23516 location_t next_loc = loc;
23517 if (!linemap_location_from_macro_expansion_p (line_table, loc))
23518 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
23519
23520 rich_location richloc (line_table, next_loc);
23521
23522 /* If we successfully offset the location, suggest the fix-it. */
23523 if (next_loc != loc)
23524 richloc.add_fixit_insert_before (next_loc, ";");
23525
23526 if (CLASSTYPE_DECLARED_CLASS (type))
23527 error_at (&richloc,
23528 "expected %<;%> after class definition");
23529 else if (TREE_CODE (type) == RECORD_TYPE)
23530 error_at (&richloc,
23531 "expected %<;%> after struct definition");
23532 else if (TREE_CODE (type) == UNION_TYPE)
23533 error_at (&richloc,
23534 "expected %<;%> after union definition");
23535 else
23536 gcc_unreachable ();
23537
23538 /* Unget one token and smash it to look as though we encountered
23539 a semicolon in the input stream. */
23540 cp_lexer_set_token_position (parser->lexer, prev);
23541 token = cp_lexer_peek_token (parser->lexer);
23542 token->type = CPP_SEMICOLON;
23543 token->keyword = RID_MAX;
23544 }
23545 }
23546
23547 /* If this class is not itself within the scope of another class,
23548 then we need to parse the bodies of all of the queued function
23549 definitions. Note that the queued functions defined in a class
23550 are not always processed immediately following the
23551 class-specifier for that class. Consider:
23552
23553 struct A {
23554 struct B { void f() { sizeof (A); } };
23555 };
23556
23557 If `f' were processed before the processing of `A' were
23558 completed, there would be no way to compute the size of `A'.
23559 Note that the nesting we are interested in here is lexical --
23560 not the semantic nesting given by TYPE_CONTEXT. In particular,
23561 for:
23562
23563 struct A { struct B; };
23564 struct A::B { void f() { } };
23565
23566 there is no need to delay the parsing of `A::B::f'. */
23567 if (--parser->num_classes_being_defined == 0)
23568 {
23569 tree decl;
23570 tree class_type = NULL_TREE;
23571 tree pushed_scope = NULL_TREE;
23572 unsigned ix;
23573 cp_default_arg_entry *e;
23574 tree save_ccp, save_ccr;
23575
23576 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
23577 {
23578 /* Skip default arguments, NSDMIs, etc, in order to improve
23579 error recovery (c++/71169, c++/71832). */
23580 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23581 vec_safe_truncate (unparsed_nsdmis, 0);
23582 vec_safe_truncate (unparsed_classes, 0);
23583 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23584 }
23585
23586 /* In a first pass, parse default arguments to the functions.
23587 Then, in a second pass, parse the bodies of the functions.
23588 This two-phased approach handles cases like:
23589
23590 struct S {
23591 void f() { g(); }
23592 void g(int i = 3);
23593 };
23594
23595 */
23596 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23597 {
23598 decl = e->decl;
23599 /* If there are default arguments that have not yet been processed,
23600 take care of them now. */
23601 if (class_type != e->class_type)
23602 {
23603 if (pushed_scope)
23604 pop_scope (pushed_scope);
23605 class_type = e->class_type;
23606 pushed_scope = push_scope (class_type);
23607 }
23608 /* Make sure that any template parameters are in scope. */
23609 maybe_begin_member_template_processing (decl);
23610 /* Parse the default argument expressions. */
23611 cp_parser_late_parsing_default_args (parser, decl);
23612 /* Remove any template parameters from the symbol table. */
23613 maybe_end_member_template_processing ();
23614 }
23615 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23616 /* Now parse any NSDMIs. */
23617 save_ccp = current_class_ptr;
23618 save_ccr = current_class_ref;
23619 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23620 {
23621 if (class_type != DECL_CONTEXT (decl))
23622 {
23623 if (pushed_scope)
23624 pop_scope (pushed_scope);
23625 class_type = DECL_CONTEXT (decl);
23626 pushed_scope = push_scope (class_type);
23627 }
23628 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23629 cp_parser_late_parsing_nsdmi (parser, decl);
23630 }
23631 vec_safe_truncate (unparsed_nsdmis, 0);
23632 current_class_ptr = save_ccp;
23633 current_class_ref = save_ccr;
23634 if (pushed_scope)
23635 pop_scope (pushed_scope);
23636
23637 /* Now do some post-NSDMI bookkeeping. */
23638 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23639 after_nsdmi_defaulted_late_checks (class_type);
23640 vec_safe_truncate (unparsed_classes, 0);
23641 after_nsdmi_defaulted_late_checks (type);
23642
23643 /* Now parse the body of the functions. */
23644 if (flag_openmp)
23645 {
23646 /* OpenMP UDRs need to be parsed before all other functions. */
23647 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23648 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23649 cp_parser_late_parsing_for_member (parser, decl);
23650 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23651 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23652 cp_parser_late_parsing_for_member (parser, decl);
23653 }
23654 else
23655 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23656 cp_parser_late_parsing_for_member (parser, decl);
23657 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23658 }
23659 else
23660 vec_safe_push (unparsed_classes, type);
23661
23662 /* Put back any saved access checks. */
23663 pop_deferring_access_checks ();
23664
23665 /* Restore saved state. */
23666 parser->in_switch_statement_p = in_switch_statement_p;
23667 parser->in_statement = in_statement;
23668 parser->in_function_body = saved_in_function_body;
23669 parser->num_template_parameter_lists
23670 = saved_num_template_parameter_lists;
23671 parser->in_unbraced_linkage_specification_p
23672 = saved_in_unbraced_linkage_specification_p;
23673
23674 return type;
23675 }
23676
23677 static tree
23678 cp_parser_class_specifier (cp_parser* parser)
23679 {
23680 tree ret;
23681 timevar_push (TV_PARSE_STRUCT);
23682 ret = cp_parser_class_specifier_1 (parser);
23683 timevar_pop (TV_PARSE_STRUCT);
23684 return ret;
23685 }
23686
23687 /* Parse a class-head.
23688
23689 class-head:
23690 class-key identifier [opt] base-clause [opt]
23691 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23692 class-key nested-name-specifier [opt] template-id
23693 base-clause [opt]
23694
23695 class-virt-specifier:
23696 final
23697
23698 GNU Extensions:
23699 class-key attributes identifier [opt] base-clause [opt]
23700 class-key attributes nested-name-specifier identifier base-clause [opt]
23701 class-key attributes nested-name-specifier [opt] template-id
23702 base-clause [opt]
23703
23704 Upon return BASES is initialized to the list of base classes (or
23705 NULL, if there are none) in the same form returned by
23706 cp_parser_base_clause.
23707
23708 Returns the TYPE of the indicated class. Sets
23709 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23710 involving a nested-name-specifier was used, and FALSE otherwise.
23711
23712 Returns error_mark_node if this is not a class-head.
23713
23714 Returns NULL_TREE if the class-head is syntactically valid, but
23715 semantically invalid in a way that means we should skip the entire
23716 body of the class. */
23717
23718 static tree
23719 cp_parser_class_head (cp_parser* parser,
23720 bool* nested_name_specifier_p)
23721 {
23722 tree nested_name_specifier;
23723 enum tag_types class_key;
23724 tree id = NULL_TREE;
23725 tree type = NULL_TREE;
23726 tree attributes;
23727 tree bases;
23728 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23729 bool template_id_p = false;
23730 bool qualified_p = false;
23731 bool invalid_nested_name_p = false;
23732 bool invalid_explicit_specialization_p = false;
23733 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23734 tree pushed_scope = NULL_TREE;
23735 unsigned num_templates;
23736 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23737 /* Assume no nested-name-specifier will be present. */
23738 *nested_name_specifier_p = false;
23739 /* Assume no template parameter lists will be used in defining the
23740 type. */
23741 num_templates = 0;
23742 parser->colon_corrects_to_scope_p = false;
23743
23744 /* Look for the class-key. */
23745 class_key = cp_parser_class_key (parser);
23746 if (class_key == none_type)
23747 return error_mark_node;
23748
23749 location_t class_head_start_location = input_location;
23750
23751 /* Parse the attributes. */
23752 attributes = cp_parser_attributes_opt (parser);
23753
23754 /* If the next token is `::', that is invalid -- but sometimes
23755 people do try to write:
23756
23757 struct ::S {};
23758
23759 Handle this gracefully by accepting the extra qualifier, and then
23760 issuing an error about it later if this really is a
23761 class-head. If it turns out just to be an elaborated type
23762 specifier, remain silent. */
23763 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23764 qualified_p = true;
23765
23766 push_deferring_access_checks (dk_no_check);
23767
23768 /* Determine the name of the class. Begin by looking for an
23769 optional nested-name-specifier. */
23770 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23771 nested_name_specifier
23772 = cp_parser_nested_name_specifier_opt (parser,
23773 /*typename_keyword_p=*/false,
23774 /*check_dependency_p=*/false,
23775 /*type_p=*/true,
23776 /*is_declaration=*/false);
23777 /* If there was a nested-name-specifier, then there *must* be an
23778 identifier. */
23779
23780 cp_token *bad_template_keyword = NULL;
23781
23782 if (nested_name_specifier)
23783 {
23784 type_start_token = cp_lexer_peek_token (parser->lexer);
23785 /* Although the grammar says `identifier', it really means
23786 `class-name' or `template-name'. You are only allowed to
23787 define a class that has already been declared with this
23788 syntax.
23789
23790 The proposed resolution for Core Issue 180 says that wherever
23791 you see `class T::X' you should treat `X' as a type-name.
23792
23793 It is OK to define an inaccessible class; for example:
23794
23795 class A { class B; };
23796 class A::B {};
23797
23798 We do not know if we will see a class-name, or a
23799 template-name. We look for a class-name first, in case the
23800 class-name is a template-id; if we looked for the
23801 template-name first we would stop after the template-name. */
23802 cp_parser_parse_tentatively (parser);
23803 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23804 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23805 type = cp_parser_class_name (parser,
23806 /*typename_keyword_p=*/false,
23807 /*template_keyword_p=*/false,
23808 class_type,
23809 /*check_dependency_p=*/false,
23810 /*class_head_p=*/true,
23811 /*is_declaration=*/false);
23812 /* If that didn't work, ignore the nested-name-specifier. */
23813 if (!cp_parser_parse_definitely (parser))
23814 {
23815 invalid_nested_name_p = true;
23816 type_start_token = cp_lexer_peek_token (parser->lexer);
23817 id = cp_parser_identifier (parser);
23818 if (id == error_mark_node)
23819 id = NULL_TREE;
23820 }
23821 /* If we could not find a corresponding TYPE, treat this
23822 declaration like an unqualified declaration. */
23823 if (type == error_mark_node)
23824 nested_name_specifier = NULL_TREE;
23825 /* Otherwise, count the number of templates used in TYPE and its
23826 containing scopes. */
23827 else
23828 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23829 }
23830 /* Otherwise, the identifier is optional. */
23831 else
23832 {
23833 /* We don't know whether what comes next is a template-id,
23834 an identifier, or nothing at all. */
23835 cp_parser_parse_tentatively (parser);
23836 /* Check for a template-id. */
23837 type_start_token = cp_lexer_peek_token (parser->lexer);
23838 id = cp_parser_template_id (parser,
23839 /*template_keyword_p=*/false,
23840 /*check_dependency_p=*/true,
23841 class_key,
23842 /*is_declaration=*/true);
23843 /* If that didn't work, it could still be an identifier. */
23844 if (!cp_parser_parse_definitely (parser))
23845 {
23846 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23847 {
23848 type_start_token = cp_lexer_peek_token (parser->lexer);
23849 id = cp_parser_identifier (parser);
23850 }
23851 else
23852 id = NULL_TREE;
23853 }
23854 else
23855 {
23856 template_id_p = true;
23857 ++num_templates;
23858 }
23859 }
23860
23861 pop_deferring_access_checks ();
23862
23863 if (id)
23864 {
23865 cp_parser_check_for_invalid_template_id (parser, id,
23866 class_key,
23867 type_start_token->location);
23868 }
23869 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23870
23871 /* If it's not a `:' or a `{' then we can't really be looking at a
23872 class-head, since a class-head only appears as part of a
23873 class-specifier. We have to detect this situation before calling
23874 xref_tag, since that has irreversible side-effects. */
23875 if (!cp_parser_next_token_starts_class_definition_p (parser))
23876 {
23877 cp_parser_error (parser, "expected %<{%> or %<:%>");
23878 type = error_mark_node;
23879 goto out;
23880 }
23881
23882 /* At this point, we're going ahead with the class-specifier, even
23883 if some other problem occurs. */
23884 cp_parser_commit_to_tentative_parse (parser);
23885 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23886 {
23887 cp_parser_error (parser,
23888 "cannot specify %<override%> for a class");
23889 type = error_mark_node;
23890 goto out;
23891 }
23892 /* Issue the error about the overly-qualified name now. */
23893 if (qualified_p)
23894 {
23895 cp_parser_error (parser,
23896 "global qualification of class name is invalid");
23897 type = error_mark_node;
23898 goto out;
23899 }
23900 else if (invalid_nested_name_p)
23901 {
23902 cp_parser_error (parser,
23903 "qualified name does not name a class");
23904 type = error_mark_node;
23905 goto out;
23906 }
23907 else if (nested_name_specifier)
23908 {
23909 tree scope;
23910
23911 if (bad_template_keyword)
23912 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23913 keyword template shall not appear at the top level. */
23914 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23915 "keyword %<template%> not allowed in class-head-name");
23916
23917 /* Reject typedef-names in class heads. */
23918 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23919 {
23920 error_at (type_start_token->location,
23921 "invalid class name in declaration of %qD",
23922 type);
23923 type = NULL_TREE;
23924 goto done;
23925 }
23926
23927 /* Figure out in what scope the declaration is being placed. */
23928 scope = current_scope ();
23929 /* If that scope does not contain the scope in which the
23930 class was originally declared, the program is invalid. */
23931 if (scope && !is_ancestor (scope, nested_name_specifier))
23932 {
23933 if (at_namespace_scope_p ())
23934 error_at (type_start_token->location,
23935 "declaration of %qD in namespace %qD which does not "
23936 "enclose %qD",
23937 type, scope, nested_name_specifier);
23938 else
23939 error_at (type_start_token->location,
23940 "declaration of %qD in %qD which does not enclose %qD",
23941 type, scope, nested_name_specifier);
23942 type = NULL_TREE;
23943 goto done;
23944 }
23945 /* [dcl.meaning]
23946
23947 A declarator-id shall not be qualified except for the
23948 definition of a ... nested class outside of its class
23949 ... [or] the definition or explicit instantiation of a
23950 class member of a namespace outside of its namespace. */
23951 if (scope == nested_name_specifier)
23952 {
23953 permerror (nested_name_specifier_token_start->location,
23954 "extra qualification not allowed");
23955 nested_name_specifier = NULL_TREE;
23956 num_templates = 0;
23957 }
23958 }
23959 /* An explicit-specialization must be preceded by "template <>". If
23960 it is not, try to recover gracefully. */
23961 if (at_namespace_scope_p ()
23962 && parser->num_template_parameter_lists == 0
23963 && !processing_template_parmlist
23964 && template_id_p)
23965 {
23966 /* Build a location of this form:
23967 struct typename <ARGS>
23968 ^~~~~~~~~~~~~~~~~~~~~~
23969 with caret==start at the start token, and
23970 finishing at the end of the type. */
23971 location_t reported_loc
23972 = make_location (class_head_start_location,
23973 class_head_start_location,
23974 get_finish (type_start_token->location));
23975 rich_location richloc (line_table, reported_loc);
23976 richloc.add_fixit_insert_before (class_head_start_location,
23977 "template <> ");
23978 error_at (&richloc,
23979 "an explicit specialization must be preceded by"
23980 " %<template <>%>");
23981 invalid_explicit_specialization_p = true;
23982 /* Take the same action that would have been taken by
23983 cp_parser_explicit_specialization. */
23984 ++parser->num_template_parameter_lists;
23985 begin_specialization ();
23986 }
23987 /* There must be no "return" statements between this point and the
23988 end of this function; set "type "to the correct return value and
23989 use "goto done;" to return. */
23990 /* Make sure that the right number of template parameters were
23991 present. */
23992 if (!cp_parser_check_template_parameters (parser, num_templates,
23993 template_id_p,
23994 type_start_token->location,
23995 /*declarator=*/NULL))
23996 {
23997 /* If something went wrong, there is no point in even trying to
23998 process the class-definition. */
23999 type = NULL_TREE;
24000 goto done;
24001 }
24002
24003 /* Look up the type. */
24004 if (template_id_p)
24005 {
24006 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
24007 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
24008 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
24009 {
24010 error_at (type_start_token->location,
24011 "function template %qD redeclared as a class template", id);
24012 type = error_mark_node;
24013 }
24014 else
24015 {
24016 type = TREE_TYPE (id);
24017 type = maybe_process_partial_specialization (type);
24018
24019 /* Check the scope while we still know whether or not we had a
24020 nested-name-specifier. */
24021 if (type != error_mark_node)
24022 check_unqualified_spec_or_inst (type, type_start_token->location);
24023 }
24024 if (nested_name_specifier)
24025 pushed_scope = push_scope (nested_name_specifier);
24026 }
24027 else if (nested_name_specifier)
24028 {
24029 tree class_type;
24030
24031 /* Given:
24032
24033 template <typename T> struct S { struct T };
24034 template <typename T> struct S<T>::T { };
24035
24036 we will get a TYPENAME_TYPE when processing the definition of
24037 `S::T'. We need to resolve it to the actual type before we
24038 try to define it. */
24039 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
24040 {
24041 class_type = resolve_typename_type (TREE_TYPE (type),
24042 /*only_current_p=*/false);
24043 if (TREE_CODE (class_type) != TYPENAME_TYPE)
24044 type = TYPE_NAME (class_type);
24045 else
24046 {
24047 cp_parser_error (parser, "could not resolve typename type");
24048 type = error_mark_node;
24049 }
24050 }
24051
24052 if (maybe_process_partial_specialization (TREE_TYPE (type))
24053 == error_mark_node)
24054 {
24055 type = NULL_TREE;
24056 goto done;
24057 }
24058
24059 class_type = current_class_type;
24060 /* Enter the scope indicated by the nested-name-specifier. */
24061 pushed_scope = push_scope (nested_name_specifier);
24062 /* Get the canonical version of this type. */
24063 type = TYPE_MAIN_DECL (TREE_TYPE (type));
24064 /* Call push_template_decl if it seems like we should be defining a
24065 template either from the template headers or the type we're
24066 defining, so that we diagnose both extra and missing headers. */
24067 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24068 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
24069 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
24070 {
24071 type = push_template_decl (type);
24072 if (type == error_mark_node)
24073 {
24074 type = NULL_TREE;
24075 goto done;
24076 }
24077 }
24078
24079 type = TREE_TYPE (type);
24080 *nested_name_specifier_p = true;
24081 }
24082 else /* The name is not a nested name. */
24083 {
24084 /* If the class was unnamed, create a dummy name. */
24085 if (!id)
24086 id = make_anon_name ();
24087 tag_scope tag_scope = (parser->in_type_id_in_expr_p
24088 ? ts_within_enclosing_non_class
24089 : ts_current);
24090 type = xref_tag (class_key, id, tag_scope,
24091 parser->num_template_parameter_lists);
24092 }
24093
24094 /* Indicate whether this class was declared as a `class' or as a
24095 `struct'. */
24096 if (TREE_CODE (type) == RECORD_TYPE)
24097 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
24098 cp_parser_check_class_key (class_key, type);
24099
24100 /* If this type was already complete, and we see another definition,
24101 that's an error. Likewise if the type is already being defined:
24102 this can happen, eg, when it's defined from within an expression
24103 (c++/84605). */
24104 if (type != error_mark_node
24105 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
24106 {
24107 error_at (type_start_token->location, "redefinition of %q#T",
24108 type);
24109 inform (location_of (type), "previous definition of %q#T",
24110 type);
24111 type = NULL_TREE;
24112 goto done;
24113 }
24114 else if (type == error_mark_node)
24115 type = NULL_TREE;
24116
24117 if (type)
24118 {
24119 /* Apply attributes now, before any use of the class as a template
24120 argument in its base list. */
24121 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
24122 fixup_attribute_variants (type);
24123 }
24124
24125 /* We will have entered the scope containing the class; the names of
24126 base classes should be looked up in that context. For example:
24127
24128 struct A { struct B {}; struct C; };
24129 struct A::C : B {};
24130
24131 is valid. */
24132
24133 /* Get the list of base-classes, if there is one. */
24134 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24135 {
24136 /* PR59482: enter the class scope so that base-specifiers are looked
24137 up correctly. */
24138 if (type)
24139 pushclass (type);
24140 bases = cp_parser_base_clause (parser);
24141 /* PR59482: get out of the previously pushed class scope so that the
24142 subsequent pops pop the right thing. */
24143 if (type)
24144 popclass ();
24145 }
24146 else
24147 bases = NULL_TREE;
24148
24149 /* If we're really defining a class, process the base classes.
24150 If they're invalid, fail. */
24151 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24152 xref_basetypes (type, bases);
24153
24154 done:
24155 /* Leave the scope given by the nested-name-specifier. We will
24156 enter the class scope itself while processing the members. */
24157 if (pushed_scope)
24158 pop_scope (pushed_scope);
24159
24160 if (invalid_explicit_specialization_p)
24161 {
24162 end_specialization ();
24163 --parser->num_template_parameter_lists;
24164 }
24165
24166 if (type)
24167 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
24168 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
24169 CLASSTYPE_FINAL (type) = 1;
24170 out:
24171 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24172 return type;
24173 }
24174
24175 /* Parse a class-key.
24176
24177 class-key:
24178 class
24179 struct
24180 union
24181
24182 Returns the kind of class-key specified, or none_type to indicate
24183 error. */
24184
24185 static enum tag_types
24186 cp_parser_class_key (cp_parser* parser)
24187 {
24188 cp_token *token;
24189 enum tag_types tag_type;
24190
24191 /* Look for the class-key. */
24192 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
24193 if (!token)
24194 return none_type;
24195
24196 /* Check to see if the TOKEN is a class-key. */
24197 tag_type = cp_parser_token_is_class_key (token);
24198 if (!tag_type)
24199 cp_parser_error (parser, "expected class-key");
24200 return tag_type;
24201 }
24202
24203 /* Parse a type-parameter-key.
24204
24205 type-parameter-key:
24206 class
24207 typename
24208 */
24209
24210 static void
24211 cp_parser_type_parameter_key (cp_parser* parser)
24212 {
24213 /* Look for the type-parameter-key. */
24214 enum tag_types tag_type = none_type;
24215 cp_token *token = cp_lexer_peek_token (parser->lexer);
24216 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
24217 {
24218 cp_lexer_consume_token (parser->lexer);
24219 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
24220 /* typename is not allowed in a template template parameter
24221 by the standard until C++17. */
24222 pedwarn (token->location, OPT_Wpedantic,
24223 "ISO C++ forbids typename key in template template parameter;"
24224 " use %<-std=c++17%> or %<-std=gnu++17%>");
24225 }
24226 else
24227 cp_parser_error (parser, "expected %<class%> or %<typename%>");
24228
24229 return;
24230 }
24231
24232 /* Parse an (optional) member-specification.
24233
24234 member-specification:
24235 member-declaration member-specification [opt]
24236 access-specifier : member-specification [opt] */
24237
24238 static void
24239 cp_parser_member_specification_opt (cp_parser* parser)
24240 {
24241 while (true)
24242 {
24243 cp_token *token;
24244 enum rid keyword;
24245
24246 /* Peek at the next token. */
24247 token = cp_lexer_peek_token (parser->lexer);
24248 /* If it's a `}', or EOF then we've seen all the members. */
24249 if (token->type == CPP_CLOSE_BRACE
24250 || token->type == CPP_EOF
24251 || token->type == CPP_PRAGMA_EOL)
24252 break;
24253
24254 /* See if this token is a keyword. */
24255 keyword = token->keyword;
24256 switch (keyword)
24257 {
24258 case RID_PUBLIC:
24259 case RID_PROTECTED:
24260 case RID_PRIVATE:
24261 /* Consume the access-specifier. */
24262 cp_lexer_consume_token (parser->lexer);
24263 /* Remember which access-specifier is active. */
24264 current_access_specifier = token->u.value;
24265 /* Look for the `:'. */
24266 cp_parser_require (parser, CPP_COLON, RT_COLON);
24267 break;
24268
24269 default:
24270 /* Accept #pragmas at class scope. */
24271 if (token->type == CPP_PRAGMA)
24272 {
24273 cp_parser_pragma (parser, pragma_member, NULL);
24274 break;
24275 }
24276
24277 /* Otherwise, the next construction must be a
24278 member-declaration. */
24279 cp_parser_member_declaration (parser);
24280 }
24281 }
24282 }
24283
24284 /* Parse a member-declaration.
24285
24286 member-declaration:
24287 decl-specifier-seq [opt] member-declarator-list [opt] ;
24288 function-definition ; [opt]
24289 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24290 using-declaration
24291 template-declaration
24292 alias-declaration
24293
24294 member-declarator-list:
24295 member-declarator
24296 member-declarator-list , member-declarator
24297
24298 member-declarator:
24299 declarator pure-specifier [opt]
24300 declarator constant-initializer [opt]
24301 identifier [opt] : constant-expression
24302
24303 GNU Extensions:
24304
24305 member-declaration:
24306 __extension__ member-declaration
24307
24308 member-declarator:
24309 declarator attributes [opt] pure-specifier [opt]
24310 declarator attributes [opt] constant-initializer [opt]
24311 identifier [opt] attributes [opt] : constant-expression
24312
24313 C++0x Extensions:
24314
24315 member-declaration:
24316 static_assert-declaration */
24317
24318 static void
24319 cp_parser_member_declaration (cp_parser* parser)
24320 {
24321 cp_decl_specifier_seq decl_specifiers;
24322 tree prefix_attributes;
24323 tree decl;
24324 int declares_class_or_enum;
24325 bool friend_p;
24326 cp_token *token = NULL;
24327 cp_token *decl_spec_token_start = NULL;
24328 cp_token *initializer_token_start = NULL;
24329 int saved_pedantic;
24330 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24331
24332 /* Check for the `__extension__' keyword. */
24333 if (cp_parser_extension_opt (parser, &saved_pedantic))
24334 {
24335 /* Recurse. */
24336 cp_parser_member_declaration (parser);
24337 /* Restore the old value of the PEDANTIC flag. */
24338 pedantic = saved_pedantic;
24339
24340 return;
24341 }
24342
24343 /* Check for a template-declaration. */
24344 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24345 {
24346 /* An explicit specialization here is an error condition, and we
24347 expect the specialization handler to detect and report this. */
24348 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
24349 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
24350 cp_parser_explicit_specialization (parser);
24351 else
24352 cp_parser_template_declaration (parser, /*member_p=*/true);
24353
24354 return;
24355 }
24356 /* Check for a template introduction. */
24357 else if (cp_parser_template_declaration_after_export (parser, true))
24358 return;
24359
24360 /* Check for a using-declaration. */
24361 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24362 {
24363 if (cxx_dialect < cxx11)
24364 {
24365 /* Parse the using-declaration. */
24366 cp_parser_using_declaration (parser,
24367 /*access_declaration_p=*/false);
24368 return;
24369 }
24370 else
24371 {
24372 tree decl;
24373 bool alias_decl_expected;
24374 cp_parser_parse_tentatively (parser);
24375 decl = cp_parser_alias_declaration (parser);
24376 /* Note that if we actually see the '=' token after the
24377 identifier, cp_parser_alias_declaration commits the
24378 tentative parse. In that case, we really expect an
24379 alias-declaration. Otherwise, we expect a using
24380 declaration. */
24381 alias_decl_expected =
24382 !cp_parser_uncommitted_to_tentative_parse_p (parser);
24383 cp_parser_parse_definitely (parser);
24384
24385 if (alias_decl_expected)
24386 finish_member_declaration (decl);
24387 else
24388 cp_parser_using_declaration (parser,
24389 /*access_declaration_p=*/false);
24390 return;
24391 }
24392 }
24393
24394 /* Check for @defs. */
24395 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
24396 {
24397 tree ivar, member;
24398 tree ivar_chains = cp_parser_objc_defs_expression (parser);
24399 ivar = ivar_chains;
24400 while (ivar)
24401 {
24402 member = ivar;
24403 ivar = TREE_CHAIN (member);
24404 TREE_CHAIN (member) = NULL_TREE;
24405 finish_member_declaration (member);
24406 }
24407 return;
24408 }
24409
24410 /* If the next token is `static_assert' we have a static assertion. */
24411 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
24412 {
24413 cp_parser_static_assert (parser, /*member_p=*/true);
24414 return;
24415 }
24416
24417 parser->colon_corrects_to_scope_p = false;
24418
24419 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
24420 goto out;
24421
24422 /* Parse the decl-specifier-seq. */
24423 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24424 cp_parser_decl_specifier_seq (parser,
24425 (CP_PARSER_FLAGS_OPTIONAL
24426 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
24427 &decl_specifiers,
24428 &declares_class_or_enum);
24429 /* Check for an invalid type-name. */
24430 if (!decl_specifiers.any_type_specifiers_p
24431 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24432 goto out;
24433 /* If there is no declarator, then the decl-specifier-seq should
24434 specify a type. */
24435 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24436 {
24437 /* If there was no decl-specifier-seq, and the next token is a
24438 `;', then we have something like:
24439
24440 struct S { ; };
24441
24442 [class.mem]
24443
24444 Each member-declaration shall declare at least one member
24445 name of the class. */
24446 if (!decl_specifiers.any_specifiers_p)
24447 {
24448 cp_token *token = cp_lexer_peek_token (parser->lexer);
24449 if (!in_system_header_at (token->location))
24450 {
24451 gcc_rich_location richloc (token->location);
24452 richloc.add_fixit_remove ();
24453 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
24454 }
24455 }
24456 else
24457 {
24458 tree type;
24459
24460 /* See if this declaration is a friend. */
24461 friend_p = cp_parser_friend_p (&decl_specifiers);
24462 /* If there were decl-specifiers, check to see if there was
24463 a class-declaration. */
24464 type = check_tag_decl (&decl_specifiers,
24465 /*explicit_type_instantiation_p=*/false);
24466 /* Nested classes have already been added to the class, but
24467 a `friend' needs to be explicitly registered. */
24468 if (friend_p)
24469 {
24470 /* If the `friend' keyword was present, the friend must
24471 be introduced with a class-key. */
24472 if (!declares_class_or_enum && cxx_dialect < cxx11)
24473 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
24474 "in C++03 a class-key must be used "
24475 "when declaring a friend");
24476 /* In this case:
24477
24478 template <typename T> struct A {
24479 friend struct A<T>::B;
24480 };
24481
24482 A<T>::B will be represented by a TYPENAME_TYPE, and
24483 therefore not recognized by check_tag_decl. */
24484 if (!type)
24485 {
24486 type = decl_specifiers.type;
24487 if (type && TREE_CODE (type) == TYPE_DECL)
24488 type = TREE_TYPE (type);
24489 }
24490 if (!type || !TYPE_P (type))
24491 error_at (decl_spec_token_start->location,
24492 "friend declaration does not name a class or "
24493 "function");
24494 else
24495 make_friend_class (current_class_type, type,
24496 /*complain=*/true);
24497 }
24498 /* If there is no TYPE, an error message will already have
24499 been issued. */
24500 else if (!type || type == error_mark_node)
24501 ;
24502 /* An anonymous aggregate has to be handled specially; such
24503 a declaration really declares a data member (with a
24504 particular type), as opposed to a nested class. */
24505 else if (ANON_AGGR_TYPE_P (type))
24506 {
24507 /* C++11 9.5/6. */
24508 if (decl_specifiers.storage_class != sc_none)
24509 error_at (decl_spec_token_start->location,
24510 "a storage class on an anonymous aggregate "
24511 "in class scope is not allowed");
24512
24513 /* Remove constructors and such from TYPE, now that we
24514 know it is an anonymous aggregate. */
24515 fixup_anonymous_aggr (type);
24516 /* And make the corresponding data member. */
24517 decl = build_decl (decl_spec_token_start->location,
24518 FIELD_DECL, NULL_TREE, type);
24519 /* Add it to the class. */
24520 finish_member_declaration (decl);
24521 }
24522 else
24523 cp_parser_check_access_in_redeclaration
24524 (TYPE_NAME (type),
24525 decl_spec_token_start->location);
24526 }
24527 }
24528 else
24529 {
24530 bool assume_semicolon = false;
24531
24532 /* Clear attributes from the decl_specifiers but keep them
24533 around as prefix attributes that apply them to the entity
24534 being declared. */
24535 prefix_attributes = decl_specifiers.attributes;
24536 decl_specifiers.attributes = NULL_TREE;
24537
24538 /* See if these declarations will be friends. */
24539 friend_p = cp_parser_friend_p (&decl_specifiers);
24540
24541 /* Keep going until we hit the `;' at the end of the
24542 declaration. */
24543 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24544 {
24545 tree attributes = NULL_TREE;
24546 tree first_attribute;
24547 tree initializer;
24548 bool named_bitfld = false;
24549
24550 /* Peek at the next token. */
24551 token = cp_lexer_peek_token (parser->lexer);
24552
24553 /* The following code wants to know early if it is a bit-field
24554 or some other declaration. Attributes can appear before
24555 the `:' token. Skip over them without consuming any tokens
24556 to peek if they are followed by `:'. */
24557 if (cp_next_tokens_can_be_attribute_p (parser)
24558 || (token->type == CPP_NAME
24559 && cp_nth_tokens_can_be_attribute_p (parser, 2)
24560 && (named_bitfld = true)))
24561 {
24562 size_t n
24563 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24564 token = cp_lexer_peek_nth_token (parser->lexer, n);
24565 }
24566
24567 /* Check for a bitfield declaration. */
24568 if (token->type == CPP_COLON
24569 || (token->type == CPP_NAME
24570 && token == cp_lexer_peek_token (parser->lexer)
24571 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24572 && (named_bitfld = true)))
24573 {
24574 tree identifier;
24575 tree width;
24576 tree late_attributes = NULL_TREE;
24577 location_t id_location
24578 = cp_lexer_peek_token (parser->lexer)->location;
24579
24580 if (named_bitfld)
24581 identifier = cp_parser_identifier (parser);
24582 else
24583 identifier = NULL_TREE;
24584
24585 /* Look for attributes that apply to the bitfield. */
24586 attributes = cp_parser_attributes_opt (parser);
24587
24588 /* Consume the `:' token. */
24589 cp_lexer_consume_token (parser->lexer);
24590
24591 /* Get the width of the bitfield. */
24592 width = cp_parser_constant_expression (parser, false, NULL,
24593 cxx_dialect >= cxx11);
24594
24595 /* In C++2A and as extension for C++11 and above we allow
24596 default member initializers for bit-fields. */
24597 initializer = NULL_TREE;
24598 if (cxx_dialect >= cxx11
24599 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24600 || cp_lexer_next_token_is (parser->lexer,
24601 CPP_OPEN_BRACE)))
24602 {
24603 location_t loc
24604 = cp_lexer_peek_token (parser->lexer)->location;
24605 if (cxx_dialect < cxx2a
24606 && !in_system_header_at (loc)
24607 && identifier != NULL_TREE)
24608 pedwarn (loc, 0,
24609 "default member initializers for bit-fields "
24610 "only available with %<-std=c++2a%> or "
24611 "%<-std=gnu++2a%>");
24612
24613 initializer = cp_parser_save_nsdmi (parser);
24614 if (identifier == NULL_TREE)
24615 {
24616 error_at (loc, "default member initializer for "
24617 "unnamed bit-field");
24618 initializer = NULL_TREE;
24619 }
24620 }
24621 else
24622 {
24623 /* Look for attributes that apply to the bitfield after
24624 the `:' token and width. This is where GCC used to
24625 parse attributes in the past, pedwarn if there is
24626 a std attribute. */
24627 if (cp_next_tokens_can_be_std_attribute_p (parser))
24628 pedwarn (input_location, OPT_Wpedantic,
24629 "ISO C++ allows bit-field attributes only "
24630 "before the %<:%> token");
24631
24632 late_attributes = cp_parser_attributes_opt (parser);
24633 }
24634
24635 attributes = attr_chainon (attributes, late_attributes);
24636
24637 /* Remember which attributes are prefix attributes and
24638 which are not. */
24639 first_attribute = attributes;
24640 /* Combine the attributes. */
24641 attributes = attr_chainon (prefix_attributes, attributes);
24642
24643 /* Create the bitfield declaration. */
24644 decl = grokbitfield (identifier
24645 ? make_id_declarator (NULL_TREE,
24646 identifier,
24647 sfk_none,
24648 id_location)
24649 : NULL,
24650 &decl_specifiers,
24651 width, initializer,
24652 attributes);
24653 }
24654 else
24655 {
24656 cp_declarator *declarator;
24657 tree asm_specification;
24658 int ctor_dtor_or_conv_p;
24659 bool static_p = (decl_specifiers.storage_class == sc_static);
24660
24661 /* Parse the declarator. */
24662 declarator
24663 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24664 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24665 &ctor_dtor_or_conv_p,
24666 /*parenthesized_p=*/NULL,
24667 /*member_p=*/true,
24668 friend_p, static_p);
24669
24670 /* If something went wrong parsing the declarator, make sure
24671 that we at least consume some tokens. */
24672 if (declarator == cp_error_declarator)
24673 {
24674 /* Skip to the end of the statement. */
24675 cp_parser_skip_to_end_of_statement (parser);
24676 /* If the next token is not a semicolon, that is
24677 probably because we just skipped over the body of
24678 a function. So, we consume a semicolon if
24679 present, but do not issue an error message if it
24680 is not present. */
24681 if (cp_lexer_next_token_is (parser->lexer,
24682 CPP_SEMICOLON))
24683 cp_lexer_consume_token (parser->lexer);
24684 goto out;
24685 }
24686
24687 if (declares_class_or_enum & 2)
24688 cp_parser_check_for_definition_in_return_type
24689 (declarator, decl_specifiers.type,
24690 decl_specifiers.locations[ds_type_spec]);
24691
24692 /* Look for an asm-specification. */
24693 asm_specification = cp_parser_asm_specification_opt (parser);
24694 /* Look for attributes that apply to the declaration. */
24695 attributes = cp_parser_attributes_opt (parser);
24696 /* Remember which attributes are prefix attributes and
24697 which are not. */
24698 first_attribute = attributes;
24699 /* Combine the attributes. */
24700 attributes = attr_chainon (prefix_attributes, attributes);
24701
24702 /* If it's an `=', then we have a constant-initializer or a
24703 pure-specifier. It is not correct to parse the
24704 initializer before registering the member declaration
24705 since the member declaration should be in scope while
24706 its initializer is processed. However, the rest of the
24707 front end does not yet provide an interface that allows
24708 us to handle this correctly. */
24709 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24710 {
24711 /* In [class.mem]:
24712
24713 A pure-specifier shall be used only in the declaration of
24714 a virtual function.
24715
24716 A member-declarator can contain a constant-initializer
24717 only if it declares a static member of integral or
24718 enumeration type.
24719
24720 Therefore, if the DECLARATOR is for a function, we look
24721 for a pure-specifier; otherwise, we look for a
24722 constant-initializer. When we call `grokfield', it will
24723 perform more stringent semantics checks. */
24724 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24725 if (function_declarator_p (declarator)
24726 || (decl_specifiers.type
24727 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24728 && declarator->kind == cdk_id
24729 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24730 == FUNCTION_TYPE)))
24731 initializer = cp_parser_pure_specifier (parser);
24732 else if (decl_specifiers.storage_class != sc_static)
24733 initializer = cp_parser_save_nsdmi (parser);
24734 else if (cxx_dialect >= cxx11)
24735 {
24736 bool nonconst;
24737 /* Don't require a constant rvalue in C++11, since we
24738 might want a reference constant. We'll enforce
24739 constancy later. */
24740 cp_lexer_consume_token (parser->lexer);
24741 /* Parse the initializer. */
24742 initializer = cp_parser_initializer_clause (parser,
24743 &nonconst);
24744 }
24745 else
24746 /* Parse the initializer. */
24747 initializer = cp_parser_constant_initializer (parser);
24748 }
24749 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24750 && !function_declarator_p (declarator))
24751 {
24752 bool x;
24753 if (decl_specifiers.storage_class != sc_static)
24754 initializer = cp_parser_save_nsdmi (parser);
24755 else
24756 initializer = cp_parser_initializer (parser, &x, &x);
24757 }
24758 /* Otherwise, there is no initializer. */
24759 else
24760 initializer = NULL_TREE;
24761
24762 /* See if we are probably looking at a function
24763 definition. We are certainly not looking at a
24764 member-declarator. Calling `grokfield' has
24765 side-effects, so we must not do it unless we are sure
24766 that we are looking at a member-declarator. */
24767 if (cp_parser_token_starts_function_definition_p
24768 (cp_lexer_peek_token (parser->lexer)))
24769 {
24770 /* The grammar does not allow a pure-specifier to be
24771 used when a member function is defined. (It is
24772 possible that this fact is an oversight in the
24773 standard, since a pure function may be defined
24774 outside of the class-specifier. */
24775 if (initializer && initializer_token_start)
24776 error_at (initializer_token_start->location,
24777 "pure-specifier on function-definition");
24778 decl = cp_parser_save_member_function_body (parser,
24779 &decl_specifiers,
24780 declarator,
24781 attributes);
24782 if (parser->fully_implicit_function_template_p)
24783 decl = finish_fully_implicit_template (parser, decl);
24784 /* If the member was not a friend, declare it here. */
24785 if (!friend_p)
24786 finish_member_declaration (decl);
24787 /* Peek at the next token. */
24788 token = cp_lexer_peek_token (parser->lexer);
24789 /* If the next token is a semicolon, consume it. */
24790 if (token->type == CPP_SEMICOLON)
24791 {
24792 location_t semicolon_loc
24793 = cp_lexer_consume_token (parser->lexer)->location;
24794 gcc_rich_location richloc (semicolon_loc);
24795 richloc.add_fixit_remove ();
24796 warning_at (&richloc, OPT_Wextra_semi,
24797 "extra %<;%> after in-class "
24798 "function definition");
24799 }
24800 goto out;
24801 }
24802 else
24803 if (declarator->kind == cdk_function)
24804 declarator->id_loc = token->location;
24805 /* Create the declaration. */
24806 decl = grokfield (declarator, &decl_specifiers,
24807 initializer, /*init_const_expr_p=*/true,
24808 asm_specification, attributes);
24809 if (parser->fully_implicit_function_template_p)
24810 {
24811 if (friend_p)
24812 finish_fully_implicit_template (parser, 0);
24813 else
24814 decl = finish_fully_implicit_template (parser, decl);
24815 }
24816 }
24817
24818 cp_finalize_omp_declare_simd (parser, decl);
24819 cp_finalize_oacc_routine (parser, decl, false);
24820
24821 /* Reset PREFIX_ATTRIBUTES. */
24822 if (attributes != error_mark_node)
24823 {
24824 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24825 attributes = TREE_CHAIN (attributes);
24826 if (attributes)
24827 TREE_CHAIN (attributes) = NULL_TREE;
24828 }
24829
24830 /* If there is any qualification still in effect, clear it
24831 now; we will be starting fresh with the next declarator. */
24832 parser->scope = NULL_TREE;
24833 parser->qualifying_scope = NULL_TREE;
24834 parser->object_scope = NULL_TREE;
24835 /* If it's a `,', then there are more declarators. */
24836 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24837 {
24838 cp_lexer_consume_token (parser->lexer);
24839 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24840 {
24841 cp_token *token = cp_lexer_previous_token (parser->lexer);
24842 gcc_rich_location richloc (token->location);
24843 richloc.add_fixit_remove ();
24844 error_at (&richloc, "stray %<,%> at end of "
24845 "member declaration");
24846 }
24847 }
24848 /* If the next token isn't a `;', then we have a parse error. */
24849 else if (cp_lexer_next_token_is_not (parser->lexer,
24850 CPP_SEMICOLON))
24851 {
24852 /* The next token might be a ways away from where the
24853 actual semicolon is missing. Find the previous token
24854 and use that for our error position. */
24855 cp_token *token = cp_lexer_previous_token (parser->lexer);
24856 gcc_rich_location richloc (token->location);
24857 richloc.add_fixit_insert_after (";");
24858 error_at (&richloc, "expected %<;%> at end of "
24859 "member declaration");
24860
24861 /* Assume that the user meant to provide a semicolon. If
24862 we were to cp_parser_skip_to_end_of_statement, we might
24863 skip to a semicolon inside a member function definition
24864 and issue nonsensical error messages. */
24865 assume_semicolon = true;
24866 }
24867
24868 if (decl)
24869 {
24870 /* Add DECL to the list of members. */
24871 if (!friend_p
24872 /* Explicitly include, eg, NSDMIs, for better error
24873 recovery (c++/58650). */
24874 || !DECL_DECLARES_FUNCTION_P (decl))
24875 finish_member_declaration (decl);
24876
24877 if (TREE_CODE (decl) == FUNCTION_DECL)
24878 cp_parser_save_default_args (parser, decl);
24879 else if (TREE_CODE (decl) == FIELD_DECL
24880 && DECL_INITIAL (decl))
24881 /* Add DECL to the queue of NSDMI to be parsed later. */
24882 vec_safe_push (unparsed_nsdmis, decl);
24883 }
24884
24885 if (assume_semicolon)
24886 goto out;
24887 }
24888 }
24889
24890 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24891 out:
24892 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24893 }
24894
24895 /* Parse a pure-specifier.
24896
24897 pure-specifier:
24898 = 0
24899
24900 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24901 Otherwise, ERROR_MARK_NODE is returned. */
24902
24903 static tree
24904 cp_parser_pure_specifier (cp_parser* parser)
24905 {
24906 cp_token *token;
24907
24908 /* Look for the `=' token. */
24909 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24910 return error_mark_node;
24911 /* Look for the `0' token. */
24912 token = cp_lexer_peek_token (parser->lexer);
24913
24914 if (token->type == CPP_EOF
24915 || token->type == CPP_PRAGMA_EOL)
24916 return error_mark_node;
24917
24918 cp_lexer_consume_token (parser->lexer);
24919
24920 /* Accept = default or = delete in c++0x mode. */
24921 if (token->keyword == RID_DEFAULT
24922 || token->keyword == RID_DELETE)
24923 {
24924 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24925 return token->u.value;
24926 }
24927
24928 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24929 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24930 {
24931 cp_parser_error (parser,
24932 "invalid pure specifier (only %<= 0%> is allowed)");
24933 cp_parser_skip_to_end_of_statement (parser);
24934 return error_mark_node;
24935 }
24936 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24937 {
24938 error_at (token->location, "templates may not be %<virtual%>");
24939 return error_mark_node;
24940 }
24941
24942 return integer_zero_node;
24943 }
24944
24945 /* Parse a constant-initializer.
24946
24947 constant-initializer:
24948 = constant-expression
24949
24950 Returns a representation of the constant-expression. */
24951
24952 static tree
24953 cp_parser_constant_initializer (cp_parser* parser)
24954 {
24955 /* Look for the `=' token. */
24956 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24957 return error_mark_node;
24958
24959 /* It is invalid to write:
24960
24961 struct S { static const int i = { 7 }; };
24962
24963 */
24964 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24965 {
24966 cp_parser_error (parser,
24967 "a brace-enclosed initializer is not allowed here");
24968 /* Consume the opening brace. */
24969 matching_braces braces;
24970 braces.consume_open (parser);
24971 /* Skip the initializer. */
24972 cp_parser_skip_to_closing_brace (parser);
24973 /* Look for the trailing `}'. */
24974 braces.require_close (parser);
24975
24976 return error_mark_node;
24977 }
24978
24979 return cp_parser_constant_expression (parser);
24980 }
24981
24982 /* Derived classes [gram.class.derived] */
24983
24984 /* Parse a base-clause.
24985
24986 base-clause:
24987 : base-specifier-list
24988
24989 base-specifier-list:
24990 base-specifier ... [opt]
24991 base-specifier-list , base-specifier ... [opt]
24992
24993 Returns a TREE_LIST representing the base-classes, in the order in
24994 which they were declared. The representation of each node is as
24995 described by cp_parser_base_specifier.
24996
24997 In the case that no bases are specified, this function will return
24998 NULL_TREE, not ERROR_MARK_NODE. */
24999
25000 static tree
25001 cp_parser_base_clause (cp_parser* parser)
25002 {
25003 tree bases = NULL_TREE;
25004
25005 /* Look for the `:' that begins the list. */
25006 cp_parser_require (parser, CPP_COLON, RT_COLON);
25007
25008 /* Scan the base-specifier-list. */
25009 while (true)
25010 {
25011 cp_token *token;
25012 tree base;
25013 bool pack_expansion_p = false;
25014
25015 /* Look for the base-specifier. */
25016 base = cp_parser_base_specifier (parser);
25017 /* Look for the (optional) ellipsis. */
25018 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25019 {
25020 /* Consume the `...'. */
25021 cp_lexer_consume_token (parser->lexer);
25022
25023 pack_expansion_p = true;
25024 }
25025
25026 /* Add BASE to the front of the list. */
25027 if (base && base != error_mark_node)
25028 {
25029 if (pack_expansion_p)
25030 /* Make this a pack expansion type. */
25031 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
25032
25033 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
25034 {
25035 TREE_CHAIN (base) = bases;
25036 bases = base;
25037 }
25038 }
25039 /* Peek at the next token. */
25040 token = cp_lexer_peek_token (parser->lexer);
25041 /* If it's not a comma, then the list is complete. */
25042 if (token->type != CPP_COMMA)
25043 break;
25044 /* Consume the `,'. */
25045 cp_lexer_consume_token (parser->lexer);
25046 }
25047
25048 /* PARSER->SCOPE may still be non-NULL at this point, if the last
25049 base class had a qualified name. However, the next name that
25050 appears is certainly not qualified. */
25051 parser->scope = NULL_TREE;
25052 parser->qualifying_scope = NULL_TREE;
25053 parser->object_scope = NULL_TREE;
25054
25055 return nreverse (bases);
25056 }
25057
25058 /* Parse a base-specifier.
25059
25060 base-specifier:
25061 :: [opt] nested-name-specifier [opt] class-name
25062 virtual access-specifier [opt] :: [opt] nested-name-specifier
25063 [opt] class-name
25064 access-specifier virtual [opt] :: [opt] nested-name-specifier
25065 [opt] class-name
25066
25067 Returns a TREE_LIST. The TREE_PURPOSE will be one of
25068 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25069 indicate the specifiers provided. The TREE_VALUE will be a TYPE
25070 (or the ERROR_MARK_NODE) indicating the type that was specified. */
25071
25072 static tree
25073 cp_parser_base_specifier (cp_parser* parser)
25074 {
25075 cp_token *token;
25076 bool done = false;
25077 bool virtual_p = false;
25078 bool duplicate_virtual_error_issued_p = false;
25079 bool duplicate_access_error_issued_p = false;
25080 bool class_scope_p, template_p;
25081 tree access = access_default_node;
25082 tree type;
25083
25084 /* Process the optional `virtual' and `access-specifier'. */
25085 while (!done)
25086 {
25087 /* Peek at the next token. */
25088 token = cp_lexer_peek_token (parser->lexer);
25089 /* Process `virtual'. */
25090 switch (token->keyword)
25091 {
25092 case RID_VIRTUAL:
25093 /* If `virtual' appears more than once, issue an error. */
25094 if (virtual_p && !duplicate_virtual_error_issued_p)
25095 {
25096 cp_parser_error (parser,
25097 "%<virtual%> specified more than once in base-specifier");
25098 duplicate_virtual_error_issued_p = true;
25099 }
25100
25101 virtual_p = true;
25102
25103 /* Consume the `virtual' token. */
25104 cp_lexer_consume_token (parser->lexer);
25105
25106 break;
25107
25108 case RID_PUBLIC:
25109 case RID_PROTECTED:
25110 case RID_PRIVATE:
25111 /* If more than one access specifier appears, issue an
25112 error. */
25113 if (access != access_default_node
25114 && !duplicate_access_error_issued_p)
25115 {
25116 cp_parser_error (parser,
25117 "more than one access specifier in base-specifier");
25118 duplicate_access_error_issued_p = true;
25119 }
25120
25121 access = ridpointers[(int) token->keyword];
25122
25123 /* Consume the access-specifier. */
25124 cp_lexer_consume_token (parser->lexer);
25125
25126 break;
25127
25128 default:
25129 done = true;
25130 break;
25131 }
25132 }
25133 /* It is not uncommon to see programs mechanically, erroneously, use
25134 the 'typename' keyword to denote (dependent) qualified types
25135 as base classes. */
25136 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25137 {
25138 token = cp_lexer_peek_token (parser->lexer);
25139 if (!processing_template_decl)
25140 error_at (token->location,
25141 "keyword %<typename%> not allowed outside of templates");
25142 else
25143 error_at (token->location,
25144 "keyword %<typename%> not allowed in this context "
25145 "(the base class is implicitly a type)");
25146 cp_lexer_consume_token (parser->lexer);
25147 }
25148
25149 /* Look for the optional `::' operator. */
25150 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
25151 /* Look for the nested-name-specifier. The simplest way to
25152 implement:
25153
25154 [temp.res]
25155
25156 The keyword `typename' is not permitted in a base-specifier or
25157 mem-initializer; in these contexts a qualified name that
25158 depends on a template-parameter is implicitly assumed to be a
25159 type name.
25160
25161 is to pretend that we have seen the `typename' keyword at this
25162 point. */
25163 cp_parser_nested_name_specifier_opt (parser,
25164 /*typename_keyword_p=*/true,
25165 /*check_dependency_p=*/true,
25166 /*type_p=*/true,
25167 /*is_declaration=*/true);
25168 /* If the base class is given by a qualified name, assume that names
25169 we see are type names or templates, as appropriate. */
25170 class_scope_p = (parser->scope && TYPE_P (parser->scope));
25171 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
25172
25173 if (!parser->scope
25174 && cp_lexer_next_token_is_decltype (parser->lexer))
25175 /* DR 950 allows decltype as a base-specifier. */
25176 type = cp_parser_decltype (parser);
25177 else
25178 {
25179 /* Otherwise, look for the class-name. */
25180 type = cp_parser_class_name (parser,
25181 class_scope_p,
25182 template_p,
25183 typename_type,
25184 /*check_dependency_p=*/true,
25185 /*class_head_p=*/false,
25186 /*is_declaration=*/true);
25187 type = TREE_TYPE (type);
25188 }
25189
25190 if (type == error_mark_node)
25191 return error_mark_node;
25192
25193 return finish_base_specifier (type, access, virtual_p);
25194 }
25195
25196 /* Exception handling [gram.exception] */
25197
25198 /* Parse an (optional) noexcept-specification.
25199
25200 noexcept-specification:
25201 noexcept ( constant-expression ) [opt]
25202
25203 If no noexcept-specification is present, returns NULL_TREE.
25204 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25205 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25206 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25207 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25208 in which case a boolean condition is returned instead. */
25209
25210 static tree
25211 cp_parser_noexcept_specification_opt (cp_parser* parser,
25212 bool require_constexpr,
25213 bool* consumed_expr,
25214 bool return_cond)
25215 {
25216 cp_token *token;
25217 const char *saved_message;
25218
25219 /* Peek at the next token. */
25220 token = cp_lexer_peek_token (parser->lexer);
25221
25222 /* Is it a noexcept-specification? */
25223 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
25224 {
25225 tree expr;
25226 cp_lexer_consume_token (parser->lexer);
25227
25228 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
25229 {
25230 matching_parens parens;
25231 parens.consume_open (parser);
25232
25233 tree save_ccp = current_class_ptr;
25234 tree save_ccr = current_class_ref;
25235
25236 if (current_class_type)
25237 inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
25238
25239 if (require_constexpr)
25240 {
25241 /* Types may not be defined in an exception-specification. */
25242 saved_message = parser->type_definition_forbidden_message;
25243 parser->type_definition_forbidden_message
25244 = G_("types may not be defined in an exception-specification");
25245
25246 bool non_constant_p;
25247 expr
25248 = cp_parser_constant_expression (parser,
25249 /*allow_non_constant=*/true,
25250 &non_constant_p);
25251 if (non_constant_p
25252 && !require_potential_rvalue_constant_expression (expr))
25253 {
25254 expr = NULL_TREE;
25255 return_cond = true;
25256 }
25257
25258 /* Restore the saved message. */
25259 parser->type_definition_forbidden_message = saved_message;
25260 }
25261 else
25262 {
25263 expr = cp_parser_expression (parser);
25264 *consumed_expr = true;
25265 }
25266
25267 parens.require_close (parser);
25268
25269 current_class_ptr = save_ccp;
25270 current_class_ref = save_ccr;
25271 }
25272 else
25273 {
25274 expr = boolean_true_node;
25275 if (!require_constexpr)
25276 *consumed_expr = false;
25277 }
25278
25279 /* We cannot build a noexcept-spec right away because this will check
25280 that expr is a constexpr. */
25281 if (!return_cond)
25282 return build_noexcept_spec (expr, tf_warning_or_error);
25283 else
25284 return expr;
25285 }
25286 else
25287 return NULL_TREE;
25288 }
25289
25290 /* Parse an (optional) exception-specification.
25291
25292 exception-specification:
25293 throw ( type-id-list [opt] )
25294
25295 Returns a TREE_LIST representing the exception-specification. The
25296 TREE_VALUE of each node is a type. */
25297
25298 static tree
25299 cp_parser_exception_specification_opt (cp_parser* parser)
25300 {
25301 cp_token *token;
25302 tree type_id_list;
25303 const char *saved_message;
25304
25305 /* Peek at the next token. */
25306 token = cp_lexer_peek_token (parser->lexer);
25307
25308 /* Is it a noexcept-specification? */
25309 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
25310 false);
25311 if (type_id_list != NULL_TREE)
25312 return type_id_list;
25313
25314 /* If it's not `throw', then there's no exception-specification. */
25315 if (!cp_parser_is_keyword (token, RID_THROW))
25316 return NULL_TREE;
25317
25318 location_t loc = token->location;
25319
25320 /* Consume the `throw'. */
25321 cp_lexer_consume_token (parser->lexer);
25322
25323 /* Look for the `('. */
25324 matching_parens parens;
25325 parens.require_open (parser);
25326
25327 /* Peek at the next token. */
25328 token = cp_lexer_peek_token (parser->lexer);
25329 /* If it's not a `)', then there is a type-id-list. */
25330 if (token->type != CPP_CLOSE_PAREN)
25331 {
25332 /* Types may not be defined in an exception-specification. */
25333 saved_message = parser->type_definition_forbidden_message;
25334 parser->type_definition_forbidden_message
25335 = G_("types may not be defined in an exception-specification");
25336 /* Parse the type-id-list. */
25337 type_id_list = cp_parser_type_id_list (parser);
25338 /* Restore the saved message. */
25339 parser->type_definition_forbidden_message = saved_message;
25340
25341 if (cxx_dialect >= cxx17)
25342 {
25343 error_at (loc, "ISO C++17 does not allow dynamic exception "
25344 "specifications");
25345 type_id_list = NULL_TREE;
25346 }
25347 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
25348 warning_at (loc, OPT_Wdeprecated,
25349 "dynamic exception specifications are deprecated in "
25350 "C++11");
25351 }
25352 /* In C++17, throw() is equivalent to noexcept (true). throw()
25353 is deprecated in C++11 and above as well, but is still widely used,
25354 so don't warn about it yet. */
25355 else if (cxx_dialect >= cxx17)
25356 type_id_list = noexcept_true_spec;
25357 else
25358 type_id_list = empty_except_spec;
25359
25360 /* Look for the `)'. */
25361 parens.require_close (parser);
25362
25363 return type_id_list;
25364 }
25365
25366 /* Parse an (optional) type-id-list.
25367
25368 type-id-list:
25369 type-id ... [opt]
25370 type-id-list , type-id ... [opt]
25371
25372 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25373 in the order that the types were presented. */
25374
25375 static tree
25376 cp_parser_type_id_list (cp_parser* parser)
25377 {
25378 tree types = NULL_TREE;
25379
25380 while (true)
25381 {
25382 cp_token *token;
25383 tree type;
25384
25385 token = cp_lexer_peek_token (parser->lexer);
25386
25387 /* Get the next type-id. */
25388 type = cp_parser_type_id (parser);
25389 /* Check for invalid 'auto'. */
25390 if (flag_concepts && type_uses_auto (type))
25391 {
25392 error_at (token->location,
25393 "invalid use of %<auto%> in exception-specification");
25394 type = error_mark_node;
25395 }
25396 /* Parse the optional ellipsis. */
25397 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25398 {
25399 /* Consume the `...'. */
25400 cp_lexer_consume_token (parser->lexer);
25401
25402 /* Turn the type into a pack expansion expression. */
25403 type = make_pack_expansion (type);
25404 }
25405 /* Add it to the list. */
25406 types = add_exception_specifier (types, type, /*complain=*/1);
25407 /* Peek at the next token. */
25408 token = cp_lexer_peek_token (parser->lexer);
25409 /* If it is not a `,', we are done. */
25410 if (token->type != CPP_COMMA)
25411 break;
25412 /* Consume the `,'. */
25413 cp_lexer_consume_token (parser->lexer);
25414 }
25415
25416 return nreverse (types);
25417 }
25418
25419 /* Parse a try-block.
25420
25421 try-block:
25422 try compound-statement handler-seq */
25423
25424 static tree
25425 cp_parser_try_block (cp_parser* parser)
25426 {
25427 tree try_block;
25428
25429 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
25430 if (parser->in_function_body
25431 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25432 && cxx_dialect < cxx2a)
25433 pedwarn (input_location, 0,
25434 "%<try%> in %<constexpr%> function only "
25435 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
25436
25437 try_block = begin_try_block ();
25438 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
25439 finish_try_block (try_block);
25440 cp_parser_handler_seq (parser);
25441 finish_handler_sequence (try_block);
25442
25443 return try_block;
25444 }
25445
25446 /* Parse a function-try-block.
25447
25448 function-try-block:
25449 try ctor-initializer [opt] function-body handler-seq */
25450
25451 static void
25452 cp_parser_function_try_block (cp_parser* parser)
25453 {
25454 tree compound_stmt;
25455 tree try_block;
25456
25457 /* Look for the `try' keyword. */
25458 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
25459 return;
25460 /* Let the rest of the front end know where we are. */
25461 try_block = begin_function_try_block (&compound_stmt);
25462 /* Parse the function-body. */
25463 cp_parser_ctor_initializer_opt_and_function_body
25464 (parser, /*in_function_try_block=*/true);
25465 /* We're done with the `try' part. */
25466 finish_function_try_block (try_block);
25467 /* Parse the handlers. */
25468 cp_parser_handler_seq (parser);
25469 /* We're done with the handlers. */
25470 finish_function_handler_sequence (try_block, compound_stmt);
25471 }
25472
25473 /* Parse a handler-seq.
25474
25475 handler-seq:
25476 handler handler-seq [opt] */
25477
25478 static void
25479 cp_parser_handler_seq (cp_parser* parser)
25480 {
25481 while (true)
25482 {
25483 cp_token *token;
25484
25485 /* Parse the handler. */
25486 cp_parser_handler (parser);
25487 /* Peek at the next token. */
25488 token = cp_lexer_peek_token (parser->lexer);
25489 /* If it's not `catch' then there are no more handlers. */
25490 if (!cp_parser_is_keyword (token, RID_CATCH))
25491 break;
25492 }
25493 }
25494
25495 /* Parse a handler.
25496
25497 handler:
25498 catch ( exception-declaration ) compound-statement */
25499
25500 static void
25501 cp_parser_handler (cp_parser* parser)
25502 {
25503 tree handler;
25504 tree declaration;
25505
25506 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
25507 handler = begin_handler ();
25508 matching_parens parens;
25509 parens.require_open (parser);
25510 declaration = cp_parser_exception_declaration (parser);
25511 finish_handler_parms (declaration, handler);
25512 parens.require_close (parser);
25513 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
25514 finish_handler (handler);
25515 }
25516
25517 /* Parse an exception-declaration.
25518
25519 exception-declaration:
25520 type-specifier-seq declarator
25521 type-specifier-seq abstract-declarator
25522 type-specifier-seq
25523 ...
25524
25525 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25526 ellipsis variant is used. */
25527
25528 static tree
25529 cp_parser_exception_declaration (cp_parser* parser)
25530 {
25531 cp_decl_specifier_seq type_specifiers;
25532 cp_declarator *declarator;
25533 const char *saved_message;
25534
25535 /* If it's an ellipsis, it's easy to handle. */
25536 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25537 {
25538 /* Consume the `...' token. */
25539 cp_lexer_consume_token (parser->lexer);
25540 return NULL_TREE;
25541 }
25542
25543 /* Types may not be defined in exception-declarations. */
25544 saved_message = parser->type_definition_forbidden_message;
25545 parser->type_definition_forbidden_message
25546 = G_("types may not be defined in exception-declarations");
25547
25548 /* Parse the type-specifier-seq. */
25549 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
25550 /*is_declaration=*/true,
25551 /*is_trailing_return=*/false,
25552 &type_specifiers);
25553 /* If it's a `)', then there is no declarator. */
25554 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25555 declarator = NULL;
25556 else
25557 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
25558 CP_PARSER_FLAGS_NONE,
25559 /*ctor_dtor_or_conv_p=*/NULL,
25560 /*parenthesized_p=*/NULL,
25561 /*member_p=*/false,
25562 /*friend_p=*/false,
25563 /*static_p=*/false);
25564
25565 /* Restore the saved message. */
25566 parser->type_definition_forbidden_message = saved_message;
25567
25568 if (!type_specifiers.any_specifiers_p)
25569 return error_mark_node;
25570
25571 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
25572 }
25573
25574 /* Parse a throw-expression.
25575
25576 throw-expression:
25577 throw assignment-expression [opt]
25578
25579 Returns a THROW_EXPR representing the throw-expression. */
25580
25581 static tree
25582 cp_parser_throw_expression (cp_parser* parser)
25583 {
25584 tree expression;
25585 cp_token* token;
25586
25587 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
25588 token = cp_lexer_peek_token (parser->lexer);
25589 /* Figure out whether or not there is an assignment-expression
25590 following the "throw" keyword. */
25591 if (token->type == CPP_COMMA
25592 || token->type == CPP_SEMICOLON
25593 || token->type == CPP_CLOSE_PAREN
25594 || token->type == CPP_CLOSE_SQUARE
25595 || token->type == CPP_CLOSE_BRACE
25596 || token->type == CPP_COLON)
25597 expression = NULL_TREE;
25598 else
25599 expression = cp_parser_assignment_expression (parser);
25600
25601 return build_throw (expression);
25602 }
25603
25604 /* GNU Extensions */
25605
25606 /* Parse an (optional) asm-specification.
25607
25608 asm-specification:
25609 asm ( string-literal )
25610
25611 If the asm-specification is present, returns a STRING_CST
25612 corresponding to the string-literal. Otherwise, returns
25613 NULL_TREE. */
25614
25615 static tree
25616 cp_parser_asm_specification_opt (cp_parser* parser)
25617 {
25618 cp_token *token;
25619 tree asm_specification;
25620
25621 /* Peek at the next token. */
25622 token = cp_lexer_peek_token (parser->lexer);
25623 /* If the next token isn't the `asm' keyword, then there's no
25624 asm-specification. */
25625 if (!cp_parser_is_keyword (token, RID_ASM))
25626 return NULL_TREE;
25627
25628 /* Consume the `asm' token. */
25629 cp_lexer_consume_token (parser->lexer);
25630 /* Look for the `('. */
25631 matching_parens parens;
25632 parens.require_open (parser);
25633
25634 /* Look for the string-literal. */
25635 asm_specification = cp_parser_string_literal (parser, false, false);
25636
25637 /* Look for the `)'. */
25638 parens.require_close (parser);
25639
25640 return asm_specification;
25641 }
25642
25643 /* Parse an asm-operand-list.
25644
25645 asm-operand-list:
25646 asm-operand
25647 asm-operand-list , asm-operand
25648
25649 asm-operand:
25650 string-literal ( expression )
25651 [ string-literal ] string-literal ( expression )
25652
25653 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25654 each node is the expression. The TREE_PURPOSE is itself a
25655 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25656 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25657 is a STRING_CST for the string literal before the parenthesis. Returns
25658 ERROR_MARK_NODE if any of the operands are invalid. */
25659
25660 static tree
25661 cp_parser_asm_operand_list (cp_parser* parser)
25662 {
25663 tree asm_operands = NULL_TREE;
25664 bool invalid_operands = false;
25665
25666 while (true)
25667 {
25668 tree string_literal;
25669 tree expression;
25670 tree name;
25671
25672 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25673 {
25674 /* Consume the `[' token. */
25675 cp_lexer_consume_token (parser->lexer);
25676 /* Read the operand name. */
25677 name = cp_parser_identifier (parser);
25678 if (name != error_mark_node)
25679 name = build_string (IDENTIFIER_LENGTH (name),
25680 IDENTIFIER_POINTER (name));
25681 /* Look for the closing `]'. */
25682 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25683 }
25684 else
25685 name = NULL_TREE;
25686 /* Look for the string-literal. */
25687 string_literal = cp_parser_string_literal (parser, false, false);
25688
25689 /* Look for the `('. */
25690 matching_parens parens;
25691 parens.require_open (parser);
25692 /* Parse the expression. */
25693 expression = cp_parser_expression (parser);
25694 /* Look for the `)'. */
25695 parens.require_close (parser);
25696
25697 if (name == error_mark_node
25698 || string_literal == error_mark_node
25699 || expression == error_mark_node)
25700 invalid_operands = true;
25701
25702 /* Add this operand to the list. */
25703 asm_operands = tree_cons (build_tree_list (name, string_literal),
25704 expression,
25705 asm_operands);
25706 /* If the next token is not a `,', there are no more
25707 operands. */
25708 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25709 break;
25710 /* Consume the `,'. */
25711 cp_lexer_consume_token (parser->lexer);
25712 }
25713
25714 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25715 }
25716
25717 /* Parse an asm-clobber-list.
25718
25719 asm-clobber-list:
25720 string-literal
25721 asm-clobber-list , string-literal
25722
25723 Returns a TREE_LIST, indicating the clobbers in the order that they
25724 appeared. The TREE_VALUE of each node is a STRING_CST. */
25725
25726 static tree
25727 cp_parser_asm_clobber_list (cp_parser* parser)
25728 {
25729 tree clobbers = NULL_TREE;
25730
25731 while (true)
25732 {
25733 tree string_literal;
25734
25735 /* Look for the string literal. */
25736 string_literal = cp_parser_string_literal (parser, false, false);
25737 /* Add it to the list. */
25738 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25739 /* If the next token is not a `,', then the list is
25740 complete. */
25741 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25742 break;
25743 /* Consume the `,' token. */
25744 cp_lexer_consume_token (parser->lexer);
25745 }
25746
25747 return clobbers;
25748 }
25749
25750 /* Parse an asm-label-list.
25751
25752 asm-label-list:
25753 identifier
25754 asm-label-list , identifier
25755
25756 Returns a TREE_LIST, indicating the labels in the order that they
25757 appeared. The TREE_VALUE of each node is a label. */
25758
25759 static tree
25760 cp_parser_asm_label_list (cp_parser* parser)
25761 {
25762 tree labels = NULL_TREE;
25763
25764 while (true)
25765 {
25766 tree identifier, label, name;
25767
25768 /* Look for the identifier. */
25769 identifier = cp_parser_identifier (parser);
25770 if (!error_operand_p (identifier))
25771 {
25772 label = lookup_label (identifier);
25773 if (TREE_CODE (label) == LABEL_DECL)
25774 {
25775 TREE_USED (label) = 1;
25776 check_goto (label);
25777 name = build_string (IDENTIFIER_LENGTH (identifier),
25778 IDENTIFIER_POINTER (identifier));
25779 labels = tree_cons (name, label, labels);
25780 }
25781 }
25782 /* If the next token is not a `,', then the list is
25783 complete. */
25784 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25785 break;
25786 /* Consume the `,' token. */
25787 cp_lexer_consume_token (parser->lexer);
25788 }
25789
25790 return nreverse (labels);
25791 }
25792
25793 /* Return TRUE iff the next tokens in the stream are possibly the
25794 beginning of a GNU extension attribute. */
25795
25796 static bool
25797 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25798 {
25799 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25800 }
25801
25802 /* Return TRUE iff the next tokens in the stream are possibly the
25803 beginning of a standard C++-11 attribute specifier. */
25804
25805 static bool
25806 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25807 {
25808 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25809 }
25810
25811 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25812 beginning of a standard C++-11 attribute specifier. */
25813
25814 static bool
25815 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25816 {
25817 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25818
25819 return (cxx_dialect >= cxx11
25820 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25821 || (token->type == CPP_OPEN_SQUARE
25822 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25823 && token->type == CPP_OPEN_SQUARE)));
25824 }
25825
25826 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25827 beginning of a GNU extension attribute. */
25828
25829 static bool
25830 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25831 {
25832 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25833
25834 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25835 }
25836
25837 /* Return true iff the next tokens can be the beginning of either a
25838 GNU attribute list, or a standard C++11 attribute sequence. */
25839
25840 static bool
25841 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25842 {
25843 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25844 || cp_next_tokens_can_be_std_attribute_p (parser));
25845 }
25846
25847 /* Return true iff the next Nth tokens can be the beginning of either
25848 a GNU attribute list, or a standard C++11 attribute sequence. */
25849
25850 static bool
25851 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25852 {
25853 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25854 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25855 }
25856
25857 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25858 of GNU attributes, or return NULL. */
25859
25860 static tree
25861 cp_parser_attributes_opt (cp_parser *parser)
25862 {
25863 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25864 return cp_parser_gnu_attributes_opt (parser);
25865 return cp_parser_std_attribute_spec_seq (parser);
25866 }
25867
25868 /* Parse an (optional) series of attributes.
25869
25870 attributes:
25871 attributes attribute
25872
25873 attribute:
25874 __attribute__ (( attribute-list [opt] ))
25875
25876 The return value is as for cp_parser_gnu_attribute_list. */
25877
25878 static tree
25879 cp_parser_gnu_attributes_opt (cp_parser* parser)
25880 {
25881 tree attributes = NULL_TREE;
25882
25883 temp_override<bool> cleanup
25884 (parser->auto_is_implicit_function_template_parm_p, false);
25885
25886 while (true)
25887 {
25888 cp_token *token;
25889 tree attribute_list;
25890 bool ok = true;
25891
25892 /* Peek at the next token. */
25893 token = cp_lexer_peek_token (parser->lexer);
25894 /* If it's not `__attribute__', then we're done. */
25895 if (token->keyword != RID_ATTRIBUTE)
25896 break;
25897
25898 /* Consume the `__attribute__' keyword. */
25899 cp_lexer_consume_token (parser->lexer);
25900 /* Look for the two `(' tokens. */
25901 matching_parens outer_parens;
25902 if (!outer_parens.require_open (parser))
25903 ok = false;
25904 matching_parens inner_parens;
25905 if (!inner_parens.require_open (parser))
25906 ok = false;
25907
25908 /* Peek at the next token. */
25909 token = cp_lexer_peek_token (parser->lexer);
25910 if (token->type != CPP_CLOSE_PAREN)
25911 /* Parse the attribute-list. */
25912 attribute_list = cp_parser_gnu_attribute_list (parser);
25913 else
25914 /* If the next token is a `)', then there is no attribute
25915 list. */
25916 attribute_list = NULL;
25917
25918 /* Look for the two `)' tokens. */
25919 if (!inner_parens.require_close (parser))
25920 ok = false;
25921 if (!outer_parens.require_close (parser))
25922 ok = false;
25923 if (!ok)
25924 cp_parser_skip_to_end_of_statement (parser);
25925
25926 /* Add these new attributes to the list. */
25927 attributes = attr_chainon (attributes, attribute_list);
25928 }
25929
25930 return attributes;
25931 }
25932
25933 /* Parse a GNU attribute-list.
25934
25935 attribute-list:
25936 attribute
25937 attribute-list , attribute
25938
25939 attribute:
25940 identifier
25941 identifier ( identifier )
25942 identifier ( identifier , expression-list )
25943 identifier ( expression-list )
25944
25945 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25946 to an attribute. The TREE_PURPOSE of each node is the identifier
25947 indicating which attribute is in use. The TREE_VALUE represents
25948 the arguments, if any. */
25949
25950 static tree
25951 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
25952 {
25953 tree attribute_list = NULL_TREE;
25954 bool save_translate_strings_p = parser->translate_strings_p;
25955
25956 /* Don't create wrapper nodes within attributes: the
25957 handlers don't know how to handle them. */
25958 auto_suppress_location_wrappers sentinel;
25959
25960 parser->translate_strings_p = false;
25961 while (true)
25962 {
25963 cp_token *token;
25964 tree identifier;
25965 tree attribute;
25966
25967 /* Look for the identifier. We also allow keywords here; for
25968 example `__attribute__ ((const))' is legal. */
25969 token = cp_lexer_peek_token (parser->lexer);
25970 if (token->type == CPP_NAME
25971 || token->type == CPP_KEYWORD)
25972 {
25973 tree arguments = NULL_TREE;
25974
25975 /* Consume the token, but save it since we need it for the
25976 SIMD enabled function parsing. */
25977 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25978
25979 /* Save away the identifier that indicates which attribute
25980 this is. */
25981 identifier = (token->type == CPP_KEYWORD)
25982 /* For keywords, use the canonical spelling, not the
25983 parsed identifier. */
25984 ? ridpointers[(int) token->keyword]
25985 : id_token->u.value;
25986
25987 identifier = canonicalize_attr_name (identifier);
25988 attribute = build_tree_list (identifier, NULL_TREE);
25989
25990 /* Peek at the next token. */
25991 token = cp_lexer_peek_token (parser->lexer);
25992 /* If it's an `(', then parse the attribute arguments. */
25993 if (token->type == CPP_OPEN_PAREN)
25994 {
25995 vec<tree, va_gc> *vec;
25996 int attr_flag = (attribute_takes_identifier_p (identifier)
25997 ? id_attr : normal_attr);
25998 vec = cp_parser_parenthesized_expression_list
25999 (parser, attr_flag, /*cast_p=*/false,
26000 /*allow_expansion_p=*/false,
26001 /*non_constant_p=*/NULL);
26002 if (vec == NULL)
26003 arguments = error_mark_node;
26004 else
26005 {
26006 arguments = build_tree_list_vec (vec);
26007 release_tree_vector (vec);
26008 }
26009 /* Save the arguments away. */
26010 TREE_VALUE (attribute) = arguments;
26011 }
26012
26013 if (arguments != error_mark_node)
26014 {
26015 /* Add this attribute to the list. */
26016 TREE_CHAIN (attribute) = attribute_list;
26017 attribute_list = attribute;
26018 }
26019
26020 token = cp_lexer_peek_token (parser->lexer);
26021 }
26022 /* Unless EXACTLY_ONE is set look for more attributes.
26023 If the next token isn't a `,', we're done. */
26024 if (exactly_one || token->type != CPP_COMMA)
26025 break;
26026
26027 /* Consume the comma and keep going. */
26028 cp_lexer_consume_token (parser->lexer);
26029 }
26030 parser->translate_strings_p = save_translate_strings_p;
26031
26032 /* We built up the list in reverse order. */
26033 return nreverse (attribute_list);
26034 }
26035
26036 /* Parse a standard C++11 attribute.
26037
26038 The returned representation is a TREE_LIST which TREE_PURPOSE is
26039 the scoped name of the attribute, and the TREE_VALUE is its
26040 arguments list.
26041
26042 Note that the scoped name of the attribute is itself a TREE_LIST
26043 which TREE_PURPOSE is the namespace of the attribute, and
26044 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
26045 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
26046 and which TREE_PURPOSE is directly the attribute name.
26047
26048 Clients of the attribute code should use get_attribute_namespace
26049 and get_attribute_name to get the actual namespace and name of
26050 attributes, regardless of their being GNU or C++11 attributes.
26051
26052 attribute:
26053 attribute-token attribute-argument-clause [opt]
26054
26055 attribute-token:
26056 identifier
26057 attribute-scoped-token
26058
26059 attribute-scoped-token:
26060 attribute-namespace :: identifier
26061
26062 attribute-namespace:
26063 identifier
26064
26065 attribute-argument-clause:
26066 ( balanced-token-seq )
26067
26068 balanced-token-seq:
26069 balanced-token [opt]
26070 balanced-token-seq balanced-token
26071
26072 balanced-token:
26073 ( balanced-token-seq )
26074 [ balanced-token-seq ]
26075 { balanced-token-seq }. */
26076
26077 static tree
26078 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
26079 {
26080 tree attribute, attr_id = NULL_TREE, arguments;
26081 cp_token *token;
26082
26083 temp_override<bool> cleanup
26084 (parser->auto_is_implicit_function_template_parm_p, false);
26085
26086 /* First, parse name of the attribute, a.k.a attribute-token. */
26087
26088 token = cp_lexer_peek_token (parser->lexer);
26089 if (token->type == CPP_NAME)
26090 attr_id = token->u.value;
26091 else if (token->type == CPP_KEYWORD)
26092 attr_id = ridpointers[(int) token->keyword];
26093 else if (token->flags & NAMED_OP)
26094 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26095
26096 if (attr_id == NULL_TREE)
26097 return NULL_TREE;
26098
26099 cp_lexer_consume_token (parser->lexer);
26100
26101 token = cp_lexer_peek_token (parser->lexer);
26102 if (token->type == CPP_SCOPE)
26103 {
26104 /* We are seeing a scoped attribute token. */
26105
26106 cp_lexer_consume_token (parser->lexer);
26107 if (attr_ns)
26108 error_at (token->location, "attribute using prefix used together "
26109 "with scoped attribute token");
26110 attr_ns = attr_id;
26111
26112 token = cp_lexer_consume_token (parser->lexer);
26113 if (token->type == CPP_NAME)
26114 attr_id = token->u.value;
26115 else if (token->type == CPP_KEYWORD)
26116 attr_id = ridpointers[(int) token->keyword];
26117 else if (token->flags & NAMED_OP)
26118 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26119 else
26120 {
26121 error_at (token->location,
26122 "expected an identifier for the attribute name");
26123 return error_mark_node;
26124 }
26125
26126 attr_ns = canonicalize_attr_name (attr_ns);
26127 attr_id = canonicalize_attr_name (attr_id);
26128 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26129 NULL_TREE);
26130 token = cp_lexer_peek_token (parser->lexer);
26131 }
26132 else if (attr_ns)
26133 {
26134 attr_ns = canonicalize_attr_name (attr_ns);
26135 attr_id = canonicalize_attr_name (attr_id);
26136 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26137 NULL_TREE);
26138 }
26139 else
26140 {
26141 attr_id = canonicalize_attr_name (attr_id);
26142 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
26143 NULL_TREE);
26144 /* C++11 noreturn attribute is equivalent to GNU's. */
26145 if (is_attribute_p ("noreturn", attr_id))
26146 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26147 /* C++14 deprecated attribute is equivalent to GNU's. */
26148 else if (is_attribute_p ("deprecated", attr_id))
26149 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26150 /* C++17 fallthrough attribute is equivalent to GNU's. */
26151 else if (is_attribute_p ("fallthrough", attr_id))
26152 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26153 /* Transactional Memory TS optimize_for_synchronized attribute is
26154 equivalent to GNU transaction_callable. */
26155 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
26156 TREE_PURPOSE (attribute)
26157 = get_identifier ("transaction_callable");
26158 /* Transactional Memory attributes are GNU attributes. */
26159 else if (tm_attr_to_mask (attr_id))
26160 TREE_PURPOSE (attribute) = attr_id;
26161 }
26162
26163 /* Now parse the optional argument clause of the attribute. */
26164
26165 if (token->type != CPP_OPEN_PAREN)
26166 return attribute;
26167
26168 {
26169 vec<tree, va_gc> *vec;
26170 int attr_flag = normal_attr;
26171
26172 if (attr_ns == gnu_identifier
26173 && attribute_takes_identifier_p (attr_id))
26174 /* A GNU attribute that takes an identifier in parameter. */
26175 attr_flag = id_attr;
26176
26177 vec = cp_parser_parenthesized_expression_list
26178 (parser, attr_flag, /*cast_p=*/false,
26179 /*allow_expansion_p=*/true,
26180 /*non_constant_p=*/NULL);
26181 if (vec == NULL)
26182 arguments = error_mark_node;
26183 else
26184 {
26185 arguments = build_tree_list_vec (vec);
26186 release_tree_vector (vec);
26187 }
26188
26189 if (arguments == error_mark_node)
26190 attribute = error_mark_node;
26191 else
26192 TREE_VALUE (attribute) = arguments;
26193 }
26194
26195 return attribute;
26196 }
26197
26198 /* Check that the attribute ATTRIBUTE appears at most once in the
26199 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
26200 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
26201 isn't implemented yet in GCC. */
26202
26203 static void
26204 cp_parser_check_std_attribute (tree attributes, tree attribute)
26205 {
26206 if (attributes)
26207 {
26208 tree name = get_attribute_name (attribute);
26209 if (is_attribute_p ("noreturn", name)
26210 && lookup_attribute ("noreturn", attributes))
26211 error ("attribute %<noreturn%> can appear at most once "
26212 "in an attribute-list");
26213 else if (is_attribute_p ("deprecated", name)
26214 && lookup_attribute ("deprecated", attributes))
26215 error ("attribute %<deprecated%> can appear at most once "
26216 "in an attribute-list");
26217 }
26218 }
26219
26220 /* Parse a list of standard C++-11 attributes.
26221
26222 attribute-list:
26223 attribute [opt]
26224 attribute-list , attribute[opt]
26225 attribute ...
26226 attribute-list , attribute ...
26227 */
26228
26229 static tree
26230 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
26231 {
26232 tree attributes = NULL_TREE, attribute = NULL_TREE;
26233 cp_token *token = NULL;
26234
26235 while (true)
26236 {
26237 attribute = cp_parser_std_attribute (parser, attr_ns);
26238 if (attribute == error_mark_node)
26239 break;
26240 if (attribute != NULL_TREE)
26241 {
26242 cp_parser_check_std_attribute (attributes, attribute);
26243 TREE_CHAIN (attribute) = attributes;
26244 attributes = attribute;
26245 }
26246 token = cp_lexer_peek_token (parser->lexer);
26247 if (token->type == CPP_ELLIPSIS)
26248 {
26249 cp_lexer_consume_token (parser->lexer);
26250 if (attribute == NULL_TREE)
26251 error_at (token->location,
26252 "expected attribute before %<...%>");
26253 else
26254 {
26255 tree pack = make_pack_expansion (TREE_VALUE (attribute));
26256 if (pack == error_mark_node)
26257 return error_mark_node;
26258 TREE_VALUE (attribute) = pack;
26259 }
26260 token = cp_lexer_peek_token (parser->lexer);
26261 }
26262 if (token->type != CPP_COMMA)
26263 break;
26264 cp_lexer_consume_token (parser->lexer);
26265 }
26266 attributes = nreverse (attributes);
26267 return attributes;
26268 }
26269
26270 /* Parse a standard C++-11 attribute specifier.
26271
26272 attribute-specifier:
26273 [ [ attribute-using-prefix [opt] attribute-list ] ]
26274 alignment-specifier
26275
26276 attribute-using-prefix:
26277 using attribute-namespace :
26278
26279 alignment-specifier:
26280 alignas ( type-id ... [opt] )
26281 alignas ( alignment-expression ... [opt] ). */
26282
26283 static tree
26284 cp_parser_std_attribute_spec (cp_parser *parser)
26285 {
26286 tree attributes = NULL_TREE;
26287 cp_token *token = cp_lexer_peek_token (parser->lexer);
26288
26289 if (token->type == CPP_OPEN_SQUARE
26290 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
26291 {
26292 tree attr_ns = NULL_TREE;
26293
26294 cp_lexer_consume_token (parser->lexer);
26295 cp_lexer_consume_token (parser->lexer);
26296
26297 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26298 {
26299 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26300 if (token->type == CPP_NAME)
26301 attr_ns = token->u.value;
26302 else if (token->type == CPP_KEYWORD)
26303 attr_ns = ridpointers[(int) token->keyword];
26304 else if (token->flags & NAMED_OP)
26305 attr_ns = get_identifier (cpp_type2name (token->type,
26306 token->flags));
26307 if (attr_ns
26308 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
26309 {
26310 if (cxx_dialect < cxx17
26311 && !in_system_header_at (input_location))
26312 pedwarn (input_location, 0,
26313 "attribute using prefix only available "
26314 "with %<-std=c++17%> or %<-std=gnu++17%>");
26315
26316 cp_lexer_consume_token (parser->lexer);
26317 cp_lexer_consume_token (parser->lexer);
26318 cp_lexer_consume_token (parser->lexer);
26319 }
26320 else
26321 attr_ns = NULL_TREE;
26322 }
26323
26324 attributes = cp_parser_std_attribute_list (parser, attr_ns);
26325
26326 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
26327 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
26328 cp_parser_skip_to_end_of_statement (parser);
26329 else
26330 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26331 when we are sure that we have actually parsed them. */
26332 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26333 }
26334 else
26335 {
26336 tree alignas_expr;
26337
26338 /* Look for an alignment-specifier. */
26339
26340 token = cp_lexer_peek_token (parser->lexer);
26341
26342 if (token->type != CPP_KEYWORD
26343 || token->keyword != RID_ALIGNAS)
26344 return NULL_TREE;
26345
26346 cp_lexer_consume_token (parser->lexer);
26347 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26348
26349 matching_parens parens;
26350 if (!parens.require_open (parser))
26351 return error_mark_node;
26352
26353 cp_parser_parse_tentatively (parser);
26354 alignas_expr = cp_parser_type_id (parser);
26355
26356 if (!cp_parser_parse_definitely (parser))
26357 {
26358 alignas_expr = cp_parser_assignment_expression (parser);
26359 if (alignas_expr == error_mark_node)
26360 cp_parser_skip_to_end_of_statement (parser);
26361 if (alignas_expr == NULL_TREE
26362 || alignas_expr == error_mark_node)
26363 return alignas_expr;
26364 }
26365
26366 alignas_expr = cxx_alignas_expr (alignas_expr);
26367 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
26368
26369 /* Handle alignas (pack...). */
26370 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26371 {
26372 cp_lexer_consume_token (parser->lexer);
26373 alignas_expr = make_pack_expansion (alignas_expr);
26374 }
26375
26376 /* Something went wrong, so don't build the attribute. */
26377 if (alignas_expr == error_mark_node)
26378 return error_mark_node;
26379
26380 if (!parens.require_close (parser))
26381 return error_mark_node;
26382
26383 /* Build the C++-11 representation of an 'aligned'
26384 attribute. */
26385 attributes
26386 = build_tree_list (build_tree_list (gnu_identifier,
26387 aligned_identifier), alignas_expr);
26388 }
26389
26390 return attributes;
26391 }
26392
26393 /* Parse a standard C++-11 attribute-specifier-seq.
26394
26395 attribute-specifier-seq:
26396 attribute-specifier-seq [opt] attribute-specifier
26397 */
26398
26399 static tree
26400 cp_parser_std_attribute_spec_seq (cp_parser *parser)
26401 {
26402 tree attr_specs = NULL_TREE;
26403 tree attr_last = NULL_TREE;
26404
26405 /* Don't create wrapper nodes within attributes: the
26406 handlers don't know how to handle them. */
26407 auto_suppress_location_wrappers sentinel;
26408
26409 while (true)
26410 {
26411 tree attr_spec = cp_parser_std_attribute_spec (parser);
26412 if (attr_spec == NULL_TREE)
26413 break;
26414 if (attr_spec == error_mark_node)
26415 return error_mark_node;
26416
26417 if (attr_last)
26418 TREE_CHAIN (attr_last) = attr_spec;
26419 else
26420 attr_specs = attr_last = attr_spec;
26421 attr_last = tree_last (attr_last);
26422 }
26423
26424 return attr_specs;
26425 }
26426
26427 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26428 return index of the first token after balanced-token, or N on failure. */
26429
26430 static size_t
26431 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
26432 {
26433 size_t orig_n = n;
26434 int nparens = 0, nbraces = 0, nsquares = 0;
26435 do
26436 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
26437 {
26438 case CPP_PRAGMA_EOL:
26439 if (!parser->lexer->in_pragma)
26440 break;
26441 /* FALLTHRU */
26442 case CPP_EOF:
26443 /* Ran out of tokens. */
26444 return orig_n;
26445 case CPP_OPEN_PAREN:
26446 ++nparens;
26447 break;
26448 case CPP_OPEN_BRACE:
26449 ++nbraces;
26450 break;
26451 case CPP_OPEN_SQUARE:
26452 ++nsquares;
26453 break;
26454 case CPP_CLOSE_PAREN:
26455 --nparens;
26456 break;
26457 case CPP_CLOSE_BRACE:
26458 --nbraces;
26459 break;
26460 case CPP_CLOSE_SQUARE:
26461 --nsquares;
26462 break;
26463 default:
26464 break;
26465 }
26466 while (nparens || nbraces || nsquares);
26467 return n;
26468 }
26469
26470 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26471 return index of the first token after the GNU attribute tokens, or N on
26472 failure. */
26473
26474 static size_t
26475 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
26476 {
26477 while (true)
26478 {
26479 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
26480 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
26481 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
26482 break;
26483
26484 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
26485 if (n2 == n + 2)
26486 break;
26487 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
26488 break;
26489 n = n2 + 1;
26490 }
26491 return n;
26492 }
26493
26494 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26495 next token), return index of the first token after the standard C++11
26496 attribute tokens, or N on failure. */
26497
26498 static size_t
26499 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
26500 {
26501 while (true)
26502 {
26503 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
26504 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
26505 {
26506 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26507 if (n2 == n + 1)
26508 break;
26509 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
26510 break;
26511 n = n2 + 1;
26512 }
26513 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
26514 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
26515 {
26516 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26517 if (n2 == n + 1)
26518 break;
26519 n = n2;
26520 }
26521 else
26522 break;
26523 }
26524 return n;
26525 }
26526
26527 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26528 as the next token), return index of the first token after the attribute
26529 tokens, or N on failure. */
26530
26531 static size_t
26532 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
26533 {
26534 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
26535 return cp_parser_skip_gnu_attributes_opt (parser, n);
26536 return cp_parser_skip_std_attribute_spec_seq (parser, n);
26537 }
26538
26539 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26540 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26541 current value of the PEDANTIC flag, regardless of whether or not
26542 the `__extension__' keyword is present. The caller is responsible
26543 for restoring the value of the PEDANTIC flag. */
26544
26545 static bool
26546 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
26547 {
26548 /* Save the old value of the PEDANTIC flag. */
26549 *saved_pedantic = pedantic;
26550
26551 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
26552 {
26553 /* Consume the `__extension__' token. */
26554 cp_lexer_consume_token (parser->lexer);
26555 /* We're not being pedantic while the `__extension__' keyword is
26556 in effect. */
26557 pedantic = 0;
26558
26559 return true;
26560 }
26561
26562 return false;
26563 }
26564
26565 /* Parse a label declaration.
26566
26567 label-declaration:
26568 __label__ label-declarator-seq ;
26569
26570 label-declarator-seq:
26571 identifier , label-declarator-seq
26572 identifier */
26573
26574 static void
26575 cp_parser_label_declaration (cp_parser* parser)
26576 {
26577 /* Look for the `__label__' keyword. */
26578 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
26579
26580 while (true)
26581 {
26582 tree identifier;
26583
26584 /* Look for an identifier. */
26585 identifier = cp_parser_identifier (parser);
26586 /* If we failed, stop. */
26587 if (identifier == error_mark_node)
26588 break;
26589 /* Declare it as a label. */
26590 finish_label_decl (identifier);
26591 /* If the next token is a `;', stop. */
26592 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26593 break;
26594 /* Look for the `,' separating the label declarations. */
26595 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
26596 }
26597
26598 /* Look for the final `;'. */
26599 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26600 }
26601
26602 // -------------------------------------------------------------------------- //
26603 // Requires Clause
26604
26605 // Parse a requires clause.
26606 //
26607 // requires-clause:
26608 // 'requires' logical-or-expression
26609 //
26610 // The required logical-or-expression must be a constant expression. Note
26611 // that we don't check that the expression is constepxr here. We defer until
26612 // we analyze constraints and then, we only check atomic constraints.
26613 static tree
26614 cp_parser_requires_clause (cp_parser *parser)
26615 {
26616 // Parse the requires clause so that it is not automatically folded.
26617 ++processing_template_decl;
26618 tree expr = cp_parser_binary_expression (parser, false, false,
26619 PREC_NOT_OPERATOR, NULL);
26620 if (check_for_bare_parameter_packs (expr))
26621 expr = error_mark_node;
26622 --processing_template_decl;
26623 return expr;
26624 }
26625
26626 // Optionally parse a requires clause:
26627 static tree
26628 cp_parser_requires_clause_opt (cp_parser *parser)
26629 {
26630 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26631 if (tok->keyword != RID_REQUIRES)
26632 {
26633 if (!flag_concepts && tok->type == CPP_NAME
26634 && tok->u.value == ridpointers[RID_REQUIRES])
26635 {
26636 error_at (cp_lexer_peek_token (parser->lexer)->location,
26637 "%<requires%> only available with %<-fconcepts%>");
26638 /* Parse and discard the requires-clause. */
26639 cp_lexer_consume_token (parser->lexer);
26640 cp_parser_requires_clause (parser);
26641 }
26642 return NULL_TREE;
26643 }
26644 cp_lexer_consume_token (parser->lexer);
26645 return cp_parser_requires_clause (parser);
26646 }
26647
26648
26649 /*---------------------------------------------------------------------------
26650 Requires expressions
26651 ---------------------------------------------------------------------------*/
26652
26653 /* Parse a requires expression
26654
26655 requirement-expression:
26656 'requires' requirement-parameter-list [opt] requirement-body */
26657 static tree
26658 cp_parser_requires_expression (cp_parser *parser)
26659 {
26660 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26661 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26662
26663 /* A requires-expression shall appear only within a concept
26664 definition or a requires-clause.
26665
26666 TODO: Implement this diagnostic correctly. */
26667 if (!processing_template_decl)
26668 {
26669 error_at (loc, "a requires expression cannot appear outside a template");
26670 cp_parser_skip_to_end_of_statement (parser);
26671 return error_mark_node;
26672 }
26673
26674 tree parms, reqs;
26675 {
26676 /* Local parameters are delared as variables within the scope
26677 of the expression. They are not visible past the end of
26678 the expression. Expressions within the requires-expression
26679 are unevaluated. */
26680 struct scope_sentinel
26681 {
26682 scope_sentinel ()
26683 {
26684 ++cp_unevaluated_operand;
26685 begin_scope (sk_block, NULL_TREE);
26686 }
26687
26688 ~scope_sentinel ()
26689 {
26690 pop_bindings_and_leave_scope ();
26691 --cp_unevaluated_operand;
26692 }
26693 } s;
26694
26695 /* Parse the optional parameter list. */
26696 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26697 {
26698 parms = cp_parser_requirement_parameter_list (parser);
26699 if (parms == error_mark_node)
26700 return error_mark_node;
26701 }
26702 else
26703 parms = NULL_TREE;
26704
26705 /* Parse the requirement body. */
26706 reqs = cp_parser_requirement_body (parser);
26707 if (reqs == error_mark_node)
26708 return error_mark_node;
26709 }
26710
26711 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26712 the parm chain. */
26713 grokparms (parms, &parms);
26714 return finish_requires_expr (parms, reqs);
26715 }
26716
26717 /* Parse a parameterized requirement.
26718
26719 requirement-parameter-list:
26720 '(' parameter-declaration-clause ')' */
26721 static tree
26722 cp_parser_requirement_parameter_list (cp_parser *parser)
26723 {
26724 matching_parens parens;
26725 if (!parens.require_open (parser))
26726 return error_mark_node;
26727
26728 tree parms
26729 = cp_parser_parameter_declaration_clause (parser, CP_PARSER_FLAGS_NONE);
26730
26731 if (!parens.require_close (parser))
26732 return error_mark_node;
26733
26734 return parms;
26735 }
26736
26737 /* Parse the body of a requirement.
26738
26739 requirement-body:
26740 '{' requirement-list '}' */
26741 static tree
26742 cp_parser_requirement_body (cp_parser *parser)
26743 {
26744 matching_braces braces;
26745 if (!braces.require_open (parser))
26746 return error_mark_node;
26747
26748 tree reqs = cp_parser_requirement_list (parser);
26749
26750 if (!braces.require_close (parser))
26751 return error_mark_node;
26752
26753 return reqs;
26754 }
26755
26756 /* Parse a list of requirements.
26757
26758 requirement-list:
26759 requirement
26760 requirement-list ';' requirement[opt] */
26761 static tree
26762 cp_parser_requirement_list (cp_parser *parser)
26763 {
26764 tree result = NULL_TREE;
26765 while (true)
26766 {
26767 tree req = cp_parser_requirement (parser);
26768 if (req == error_mark_node)
26769 return error_mark_node;
26770
26771 result = tree_cons (NULL_TREE, req, result);
26772
26773 /* If we see a semi-colon, consume it. */
26774 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26775 cp_lexer_consume_token (parser->lexer);
26776
26777 /* Stop processing at the end of the list. */
26778 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26779 break;
26780 }
26781
26782 /* Reverse the order of requirements so they are analyzed in
26783 declaration order. */
26784 return nreverse (result);
26785 }
26786
26787 /* Parse a syntactic requirement or type requirement.
26788
26789 requirement:
26790 simple-requirement
26791 compound-requirement
26792 type-requirement
26793 nested-requirement */
26794 static tree
26795 cp_parser_requirement (cp_parser *parser)
26796 {
26797 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26798 return cp_parser_compound_requirement (parser);
26799 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26800 return cp_parser_type_requirement (parser);
26801 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26802 return cp_parser_nested_requirement (parser);
26803 else
26804 return cp_parser_simple_requirement (parser);
26805 }
26806
26807 /* Parse a simple requirement.
26808
26809 simple-requirement:
26810 expression ';' */
26811 static tree
26812 cp_parser_simple_requirement (cp_parser *parser)
26813 {
26814 tree expr = cp_parser_expression (parser, NULL, false, false);
26815 if (!expr || expr == error_mark_node)
26816 return error_mark_node;
26817
26818 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26819 return error_mark_node;
26820
26821 return finish_simple_requirement (expr);
26822 }
26823
26824 /* Parse a type requirement
26825
26826 type-requirement
26827 nested-name-specifier [opt] required-type-name ';'
26828
26829 required-type-name:
26830 type-name
26831 'template' [opt] simple-template-id */
26832 static tree
26833 cp_parser_type_requirement (cp_parser *parser)
26834 {
26835 cp_lexer_consume_token (parser->lexer);
26836
26837 // Save the scope before parsing name specifiers.
26838 tree saved_scope = parser->scope;
26839 tree saved_object_scope = parser->object_scope;
26840 tree saved_qualifying_scope = parser->qualifying_scope;
26841 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26842 cp_parser_nested_name_specifier_opt (parser,
26843 /*typename_keyword_p=*/true,
26844 /*check_dependency_p=*/false,
26845 /*type_p=*/true,
26846 /*is_declaration=*/false);
26847
26848 tree type;
26849 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26850 {
26851 cp_lexer_consume_token (parser->lexer);
26852 type = cp_parser_template_id (parser,
26853 /*template_keyword_p=*/true,
26854 /*check_dependency=*/false,
26855 /*tag_type=*/none_type,
26856 /*is_declaration=*/false);
26857 type = make_typename_type (parser->scope, type, typename_type,
26858 /*complain=*/tf_error);
26859 }
26860 else
26861 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26862
26863 if (TREE_CODE (type) == TYPE_DECL)
26864 type = TREE_TYPE (type);
26865
26866 parser->scope = saved_scope;
26867 parser->object_scope = saved_object_scope;
26868 parser->qualifying_scope = saved_qualifying_scope;
26869
26870 if (type == error_mark_node)
26871 cp_parser_skip_to_end_of_statement (parser);
26872
26873 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26874 return error_mark_node;
26875 if (type == error_mark_node)
26876 return error_mark_node;
26877
26878 return finish_type_requirement (type);
26879 }
26880
26881 /* Parse a compound requirement
26882
26883 compound-requirement:
26884 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26885 static tree
26886 cp_parser_compound_requirement (cp_parser *parser)
26887 {
26888 /* Parse an expression enclosed in '{ }'s. */
26889 matching_braces braces;
26890 if (!braces.require_open (parser))
26891 return error_mark_node;
26892
26893 tree expr = cp_parser_expression (parser, NULL, false, false);
26894 if (!expr || expr == error_mark_node)
26895 return error_mark_node;
26896
26897 if (!braces.require_close (parser))
26898 return error_mark_node;
26899
26900 /* Parse the optional noexcept. */
26901 bool noexcept_p = false;
26902 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26903 {
26904 cp_lexer_consume_token (parser->lexer);
26905 noexcept_p = true;
26906 }
26907
26908 /* Parse the optional trailing return type. */
26909 tree type = NULL_TREE;
26910 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26911 {
26912 cp_lexer_consume_token (parser->lexer);
26913 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26914 parser->in_result_type_constraint_p = true;
26915 type = cp_parser_trailing_type_id (parser);
26916 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26917 if (type == error_mark_node)
26918 return error_mark_node;
26919 }
26920
26921 return finish_compound_requirement (expr, type, noexcept_p);
26922 }
26923
26924 /* Parse a nested requirement. This is the same as a requires clause.
26925
26926 nested-requirement:
26927 requires-clause */
26928 static tree
26929 cp_parser_nested_requirement (cp_parser *parser)
26930 {
26931 cp_lexer_consume_token (parser->lexer);
26932 tree req = cp_parser_requires_clause (parser);
26933 if (req == error_mark_node)
26934 return error_mark_node;
26935 return finish_nested_requirement (req);
26936 }
26937
26938 /* Support Functions */
26939
26940 /* Return the appropriate prefer_type argument for lookup_name_real based on
26941 tag_type and template_mem_access. */
26942
26943 static inline int
26944 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26945 {
26946 /* DR 141: When looking in the current enclosing context for a template-name
26947 after -> or ., only consider class templates. */
26948 if (template_mem_access)
26949 return 2;
26950 switch (tag_type)
26951 {
26952 case none_type: return 0; // No preference.
26953 case scope_type: return 1; // Type or namespace.
26954 default: return 2; // Type only.
26955 }
26956 }
26957
26958 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26959 NAME should have one of the representations used for an
26960 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26961 is returned. If PARSER->SCOPE is a dependent type, then a
26962 SCOPE_REF is returned.
26963
26964 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26965 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26966 was formed. Abstractly, such entities should not be passed to this
26967 function, because they do not need to be looked up, but it is
26968 simpler to check for this special case here, rather than at the
26969 call-sites.
26970
26971 In cases not explicitly covered above, this function returns a
26972 DECL, OVERLOAD, or baselink representing the result of the lookup.
26973 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26974 is returned.
26975
26976 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26977 (e.g., "struct") that was used. In that case bindings that do not
26978 refer to types are ignored.
26979
26980 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26981 ignored.
26982
26983 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26984 are ignored.
26985
26986 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26987 types.
26988
26989 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26990 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26991 NULL_TREE otherwise. */
26992
26993 static cp_expr
26994 cp_parser_lookup_name (cp_parser *parser, tree name,
26995 enum tag_types tag_type,
26996 bool is_template,
26997 bool is_namespace,
26998 bool check_dependency,
26999 tree *ambiguous_decls,
27000 location_t name_location)
27001 {
27002 tree decl;
27003 tree object_type = parser->context->object_type;
27004
27005 /* Assume that the lookup will be unambiguous. */
27006 if (ambiguous_decls)
27007 *ambiguous_decls = NULL_TREE;
27008
27009 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
27010 no longer valid. Note that if we are parsing tentatively, and
27011 the parse fails, OBJECT_TYPE will be automatically restored. */
27012 parser->context->object_type = NULL_TREE;
27013
27014 if (name == error_mark_node)
27015 return error_mark_node;
27016
27017 /* A template-id has already been resolved; there is no lookup to
27018 do. */
27019 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
27020 return name;
27021 if (BASELINK_P (name))
27022 {
27023 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
27024 == TEMPLATE_ID_EXPR);
27025 return name;
27026 }
27027
27028 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
27029 it should already have been checked to make sure that the name
27030 used matches the type being destroyed. */
27031 if (TREE_CODE (name) == BIT_NOT_EXPR)
27032 {
27033 tree type;
27034
27035 /* Figure out to which type this destructor applies. */
27036 if (parser->scope)
27037 type = parser->scope;
27038 else if (object_type)
27039 type = object_type;
27040 else
27041 type = current_class_type;
27042 /* If that's not a class type, there is no destructor. */
27043 if (!type || !CLASS_TYPE_P (type))
27044 return error_mark_node;
27045
27046 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
27047 lazily_declare_fn (sfk_destructor, type);
27048
27049 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
27050 return dtor;
27051
27052 return error_mark_node;
27053 }
27054
27055 /* By this point, the NAME should be an ordinary identifier. If
27056 the id-expression was a qualified name, the qualifying scope is
27057 stored in PARSER->SCOPE at this point. */
27058 gcc_assert (identifier_p (name));
27059
27060 /* Perform the lookup. */
27061 if (parser->scope)
27062 {
27063 bool dependent_p;
27064
27065 if (parser->scope == error_mark_node)
27066 return error_mark_node;
27067
27068 /* If the SCOPE is dependent, the lookup must be deferred until
27069 the template is instantiated -- unless we are explicitly
27070 looking up names in uninstantiated templates. Even then, we
27071 cannot look up the name if the scope is not a class type; it
27072 might, for example, be a template type parameter. */
27073 dependent_p = (TYPE_P (parser->scope)
27074 && dependent_scope_p (parser->scope));
27075 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
27076 && dependent_p)
27077 /* Defer lookup. */
27078 decl = error_mark_node;
27079 else
27080 {
27081 tree pushed_scope = NULL_TREE;
27082
27083 /* If PARSER->SCOPE is a dependent type, then it must be a
27084 class type, and we must not be checking dependencies;
27085 otherwise, we would have processed this lookup above. So
27086 that PARSER->SCOPE is not considered a dependent base by
27087 lookup_member, we must enter the scope here. */
27088 if (dependent_p)
27089 pushed_scope = push_scope (parser->scope);
27090
27091 /* If the PARSER->SCOPE is a template specialization, it
27092 may be instantiated during name lookup. In that case,
27093 errors may be issued. Even if we rollback the current
27094 tentative parse, those errors are valid. */
27095 decl = lookup_qualified_name (parser->scope, name,
27096 prefer_type_arg (tag_type),
27097 /*complain=*/true);
27098
27099 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
27100 lookup result and the nested-name-specifier nominates a class C:
27101 * if the name specified after the nested-name-specifier, when
27102 looked up in C, is the injected-class-name of C (Clause 9), or
27103 * if the name specified after the nested-name-specifier is the
27104 same as the identifier or the simple-template-id's template-
27105 name in the last component of the nested-name-specifier,
27106 the name is instead considered to name the constructor of
27107 class C. [ Note: for example, the constructor is not an
27108 acceptable lookup result in an elaborated-type-specifier so
27109 the constructor would not be used in place of the
27110 injected-class-name. --end note ] Such a constructor name
27111 shall be used only in the declarator-id of a declaration that
27112 names a constructor or in a using-declaration. */
27113 if (tag_type == none_type
27114 && DECL_SELF_REFERENCE_P (decl)
27115 && same_type_p (DECL_CONTEXT (decl), parser->scope))
27116 decl = lookup_qualified_name (parser->scope, ctor_identifier,
27117 prefer_type_arg (tag_type),
27118 /*complain=*/true);
27119
27120 /* If we have a single function from a using decl, pull it out. */
27121 if (TREE_CODE (decl) == OVERLOAD
27122 && !really_overloaded_fn (decl))
27123 decl = OVL_FUNCTION (decl);
27124
27125 if (pushed_scope)
27126 pop_scope (pushed_scope);
27127 }
27128
27129 /* If the scope is a dependent type and either we deferred lookup or
27130 we did lookup but didn't find the name, rememeber the name. */
27131 if (decl == error_mark_node && TYPE_P (parser->scope)
27132 && dependent_type_p (parser->scope))
27133 {
27134 if (tag_type)
27135 {
27136 tree type;
27137
27138 /* The resolution to Core Issue 180 says that `struct
27139 A::B' should be considered a type-name, even if `A'
27140 is dependent. */
27141 type = make_typename_type (parser->scope, name, tag_type,
27142 /*complain=*/tf_error);
27143 if (type != error_mark_node)
27144 decl = TYPE_NAME (type);
27145 }
27146 else if (is_template
27147 && (cp_parser_next_token_ends_template_argument_p (parser)
27148 || cp_lexer_next_token_is (parser->lexer,
27149 CPP_CLOSE_PAREN)))
27150 decl = make_unbound_class_template (parser->scope,
27151 name, NULL_TREE,
27152 /*complain=*/tf_error);
27153 else
27154 decl = build_qualified_name (/*type=*/NULL_TREE,
27155 parser->scope, name,
27156 is_template);
27157 }
27158 parser->qualifying_scope = parser->scope;
27159 parser->object_scope = NULL_TREE;
27160 }
27161 else if (object_type)
27162 {
27163 /* Look up the name in the scope of the OBJECT_TYPE, unless the
27164 OBJECT_TYPE is not a class. */
27165 if (CLASS_TYPE_P (object_type))
27166 /* If the OBJECT_TYPE is a template specialization, it may
27167 be instantiated during name lookup. In that case, errors
27168 may be issued. Even if we rollback the current tentative
27169 parse, those errors are valid. */
27170 decl = lookup_member (object_type,
27171 name,
27172 /*protect=*/0,
27173 prefer_type_arg (tag_type),
27174 tf_warning_or_error);
27175 else
27176 decl = NULL_TREE;
27177
27178 if (!decl)
27179 /* Look it up in the enclosing context. DR 141: When looking for a
27180 template-name after -> or ., only consider class templates. */
27181 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
27182 /*nonclass=*/0,
27183 /*block_p=*/true, is_namespace, 0);
27184 if (object_type == unknown_type_node)
27185 /* The object is type-dependent, so we can't look anything up; we used
27186 this to get the DR 141 behavior. */
27187 object_type = NULL_TREE;
27188 parser->object_scope = object_type;
27189 parser->qualifying_scope = NULL_TREE;
27190 }
27191 else
27192 {
27193 decl = lookup_name_real (name, prefer_type_arg (tag_type),
27194 /*nonclass=*/0,
27195 /*block_p=*/true, is_namespace, 0);
27196 parser->qualifying_scope = NULL_TREE;
27197 parser->object_scope = NULL_TREE;
27198 }
27199
27200 /* If the lookup failed, let our caller know. */
27201 if (!decl || decl == error_mark_node)
27202 return error_mark_node;
27203
27204 /* Pull out the template from an injected-class-name (or multiple). */
27205 if (is_template)
27206 decl = maybe_get_template_decl_from_type_decl (decl);
27207
27208 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
27209 if (TREE_CODE (decl) == TREE_LIST)
27210 {
27211 if (ambiguous_decls)
27212 *ambiguous_decls = decl;
27213 /* The error message we have to print is too complicated for
27214 cp_parser_error, so we incorporate its actions directly. */
27215 if (!cp_parser_simulate_error (parser))
27216 {
27217 error_at (name_location, "reference to %qD is ambiguous",
27218 name);
27219 print_candidates (decl);
27220 }
27221 return error_mark_node;
27222 }
27223
27224 gcc_assert (DECL_P (decl)
27225 || TREE_CODE (decl) == OVERLOAD
27226 || TREE_CODE (decl) == SCOPE_REF
27227 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
27228 || BASELINK_P (decl));
27229
27230 /* If we have resolved the name of a member declaration, check to
27231 see if the declaration is accessible. When the name resolves to
27232 set of overloaded functions, accessibility is checked when
27233 overload resolution is done.
27234
27235 During an explicit instantiation, access is not checked at all,
27236 as per [temp.explicit]. */
27237 if (DECL_P (decl))
27238 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
27239
27240 maybe_record_typedef_use (decl);
27241
27242 return cp_expr (decl, name_location);
27243 }
27244
27245 /* Like cp_parser_lookup_name, but for use in the typical case where
27246 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27247 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
27248
27249 static tree
27250 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
27251 {
27252 return cp_parser_lookup_name (parser, name,
27253 none_type,
27254 /*is_template=*/false,
27255 /*is_namespace=*/false,
27256 /*check_dependency=*/true,
27257 /*ambiguous_decls=*/NULL,
27258 location);
27259 }
27260
27261 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27262 the current context, return the TYPE_DECL. If TAG_NAME_P is
27263 true, the DECL indicates the class being defined in a class-head,
27264 or declared in an elaborated-type-specifier.
27265
27266 Otherwise, return DECL. */
27267
27268 static tree
27269 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
27270 {
27271 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27272 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27273
27274 struct A {
27275 template <typename T> struct B;
27276 };
27277
27278 template <typename T> struct A::B {};
27279
27280 Similarly, in an elaborated-type-specifier:
27281
27282 namespace N { struct X{}; }
27283
27284 struct A {
27285 template <typename T> friend struct N::X;
27286 };
27287
27288 However, if the DECL refers to a class type, and we are in
27289 the scope of the class, then the name lookup automatically
27290 finds the TYPE_DECL created by build_self_reference rather
27291 than a TEMPLATE_DECL. For example, in:
27292
27293 template <class T> struct S {
27294 S s;
27295 };
27296
27297 there is no need to handle such case. */
27298
27299 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
27300 return DECL_TEMPLATE_RESULT (decl);
27301
27302 return decl;
27303 }
27304
27305 /* If too many, or too few, template-parameter lists apply to the
27306 declarator, issue an error message. Returns TRUE if all went well,
27307 and FALSE otherwise. */
27308
27309 static bool
27310 cp_parser_check_declarator_template_parameters (cp_parser* parser,
27311 cp_declarator *declarator,
27312 location_t declarator_location)
27313 {
27314 switch (declarator->kind)
27315 {
27316 case cdk_id:
27317 {
27318 unsigned num_templates = 0;
27319 tree scope = declarator->u.id.qualifying_scope;
27320 bool template_id_p = false;
27321
27322 if (scope)
27323 num_templates = num_template_headers_for_class (scope);
27324 else if (TREE_CODE (declarator->u.id.unqualified_name)
27325 == TEMPLATE_ID_EXPR)
27326 {
27327 /* If the DECLARATOR has the form `X<y>' then it uses one
27328 additional level of template parameters. */
27329 ++num_templates;
27330 template_id_p = true;
27331 }
27332
27333 return cp_parser_check_template_parameters
27334 (parser, num_templates, template_id_p, declarator_location,
27335 declarator);
27336 }
27337
27338 case cdk_function:
27339 case cdk_array:
27340 case cdk_pointer:
27341 case cdk_reference:
27342 case cdk_ptrmem:
27343 return (cp_parser_check_declarator_template_parameters
27344 (parser, declarator->declarator, declarator_location));
27345
27346 case cdk_decomp:
27347 case cdk_error:
27348 return true;
27349
27350 default:
27351 gcc_unreachable ();
27352 }
27353 return false;
27354 }
27355
27356 /* NUM_TEMPLATES were used in the current declaration. If that is
27357 invalid, return FALSE and issue an error messages. Otherwise,
27358 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27359 declarator and we can print more accurate diagnostics. */
27360
27361 static bool
27362 cp_parser_check_template_parameters (cp_parser* parser,
27363 unsigned num_templates,
27364 bool template_id_p,
27365 location_t location,
27366 cp_declarator *declarator)
27367 {
27368 /* If there are the same number of template classes and parameter
27369 lists, that's OK. */
27370 if (parser->num_template_parameter_lists == num_templates)
27371 return true;
27372 /* If there are more, but only one more, and the name ends in an identifier,
27373 then we are declaring a primary template. That's OK too. */
27374 if (!template_id_p
27375 && parser->num_template_parameter_lists == num_templates + 1)
27376 return true;
27377 /* If there are more template classes than parameter lists, we have
27378 something like:
27379
27380 template <class T> void S<T>::R<T>::f (); */
27381 if (parser->num_template_parameter_lists < num_templates)
27382 {
27383 if (declarator && !current_function_decl)
27384 error_at (location, "specializing member %<%T::%E%> "
27385 "requires %<template<>%> syntax",
27386 declarator->u.id.qualifying_scope,
27387 declarator->u.id.unqualified_name);
27388 else if (declarator)
27389 error_at (location, "invalid declaration of %<%T::%E%>",
27390 declarator->u.id.qualifying_scope,
27391 declarator->u.id.unqualified_name);
27392 else
27393 error_at (location, "too few template-parameter-lists");
27394 return false;
27395 }
27396 /* Otherwise, there are too many template parameter lists. We have
27397 something like:
27398
27399 template <class T> template <class U> void S::f(); */
27400 error_at (location, "too many template-parameter-lists");
27401 return false;
27402 }
27403
27404 /* Parse an optional `::' token indicating that the following name is
27405 from the global namespace. If so, PARSER->SCOPE is set to the
27406 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27407 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27408 Returns the new value of PARSER->SCOPE, if the `::' token is
27409 present, and NULL_TREE otherwise. */
27410
27411 static tree
27412 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
27413 {
27414 cp_token *token;
27415
27416 /* Peek at the next token. */
27417 token = cp_lexer_peek_token (parser->lexer);
27418 /* If we're looking at a `::' token then we're starting from the
27419 global namespace, not our current location. */
27420 if (token->type == CPP_SCOPE)
27421 {
27422 /* Consume the `::' token. */
27423 cp_lexer_consume_token (parser->lexer);
27424 /* Set the SCOPE so that we know where to start the lookup. */
27425 parser->scope = global_namespace;
27426 parser->qualifying_scope = global_namespace;
27427 parser->object_scope = NULL_TREE;
27428
27429 return parser->scope;
27430 }
27431 else if (!current_scope_valid_p)
27432 {
27433 parser->scope = NULL_TREE;
27434 parser->qualifying_scope = NULL_TREE;
27435 parser->object_scope = NULL_TREE;
27436 }
27437
27438 return NULL_TREE;
27439 }
27440
27441 /* Returns TRUE if the upcoming token sequence is the start of a
27442 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27443 declarator is preceded by the `friend' specifier. The parser flags FLAGS
27444 is used to control type-specifier parsing. */
27445
27446 static bool
27447 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
27448 bool friend_p)
27449 {
27450 bool constructor_p;
27451 bool outside_class_specifier_p;
27452 tree nested_name_specifier;
27453 cp_token *next_token;
27454
27455 /* The common case is that this is not a constructor declarator, so
27456 try to avoid doing lots of work if at all possible. It's not
27457 valid declare a constructor at function scope. */
27458 if (parser->in_function_body)
27459 return false;
27460 /* And only certain tokens can begin a constructor declarator. */
27461 next_token = cp_lexer_peek_token (parser->lexer);
27462 if (next_token->type != CPP_NAME
27463 && next_token->type != CPP_SCOPE
27464 && next_token->type != CPP_NESTED_NAME_SPECIFIER
27465 && next_token->type != CPP_TEMPLATE_ID)
27466 return false;
27467
27468 /* Parse tentatively; we are going to roll back all of the tokens
27469 consumed here. */
27470 cp_parser_parse_tentatively (parser);
27471 /* Assume that we are looking at a constructor declarator. */
27472 constructor_p = true;
27473
27474 /* Look for the optional `::' operator. */
27475 cp_parser_global_scope_opt (parser,
27476 /*current_scope_valid_p=*/false);
27477 /* Look for the nested-name-specifier. */
27478 nested_name_specifier
27479 = (cp_parser_nested_name_specifier_opt (parser,
27480 /*typename_keyword_p=*/false,
27481 /*check_dependency_p=*/false,
27482 /*type_p=*/false,
27483 /*is_declaration=*/false));
27484
27485 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27486 if (nested_name_specifier
27487 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
27488 {
27489 tree s = resolve_typename_type (nested_name_specifier,
27490 /*only_current_p=*/false);
27491 if (TREE_CODE (s) != TYPENAME_TYPE)
27492 nested_name_specifier = s;
27493 }
27494
27495 outside_class_specifier_p = (!at_class_scope_p ()
27496 || !TYPE_BEING_DEFINED (current_class_type)
27497 || friend_p);
27498
27499 /* Outside of a class-specifier, there must be a
27500 nested-name-specifier. Except in C++17 mode, where we
27501 might be declaring a guiding declaration. */
27502 if (!nested_name_specifier && outside_class_specifier_p
27503 && cxx_dialect < cxx17)
27504 constructor_p = false;
27505 else if (nested_name_specifier == error_mark_node)
27506 constructor_p = false;
27507
27508 /* If we have a class scope, this is easy; DR 147 says that S::S always
27509 names the constructor, and no other qualified name could. */
27510 if (constructor_p && nested_name_specifier
27511 && CLASS_TYPE_P (nested_name_specifier))
27512 {
27513 tree id = cp_parser_unqualified_id (parser,
27514 /*template_keyword_p=*/false,
27515 /*check_dependency_p=*/false,
27516 /*declarator_p=*/true,
27517 /*optional_p=*/false);
27518 if (is_overloaded_fn (id))
27519 id = DECL_NAME (get_first_fn (id));
27520 if (!constructor_name_p (id, nested_name_specifier))
27521 constructor_p = false;
27522 }
27523 /* If we still think that this might be a constructor-declarator,
27524 look for a class-name. */
27525 else if (constructor_p)
27526 {
27527 /* If we have:
27528
27529 template <typename T> struct S {
27530 S();
27531 };
27532
27533 we must recognize that the nested `S' names a class. */
27534 if (cxx_dialect >= cxx17)
27535 cp_parser_parse_tentatively (parser);
27536
27537 tree type_decl;
27538 type_decl = cp_parser_class_name (parser,
27539 /*typename_keyword_p=*/false,
27540 /*template_keyword_p=*/false,
27541 none_type,
27542 /*check_dependency_p=*/false,
27543 /*class_head_p=*/false,
27544 /*is_declaration=*/false);
27545
27546 if (cxx_dialect >= cxx17
27547 && !cp_parser_parse_definitely (parser))
27548 {
27549 type_decl = NULL_TREE;
27550 tree tmpl = cp_parser_template_name (parser,
27551 /*template_keyword*/false,
27552 /*check_dependency_p*/false,
27553 /*is_declaration*/false,
27554 none_type,
27555 /*is_identifier*/NULL);
27556 if (DECL_CLASS_TEMPLATE_P (tmpl)
27557 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27558 /* It's a deduction guide, return true. */;
27559 else
27560 cp_parser_simulate_error (parser);
27561 }
27562
27563 /* If there was no class-name, then this is not a constructor.
27564 Otherwise, if we are in a class-specifier and we aren't
27565 handling a friend declaration, check that its type matches
27566 current_class_type (c++/38313). Note: error_mark_node
27567 is left alone for error recovery purposes. */
27568 constructor_p = (!cp_parser_error_occurred (parser)
27569 && (outside_class_specifier_p
27570 || type_decl == NULL_TREE
27571 || type_decl == error_mark_node
27572 || same_type_p (current_class_type,
27573 TREE_TYPE (type_decl))));
27574
27575 /* If we're still considering a constructor, we have to see a `(',
27576 to begin the parameter-declaration-clause, followed by either a
27577 `)', an `...', or a decl-specifier. We need to check for a
27578 type-specifier to avoid being fooled into thinking that:
27579
27580 S (f) (int);
27581
27582 is a constructor. (It is actually a function named `f' that
27583 takes one parameter (of type `int') and returns a value of type
27584 `S'. */
27585 if (constructor_p
27586 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27587 constructor_p = false;
27588
27589 if (constructor_p
27590 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
27591 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
27592 /* A parameter declaration begins with a decl-specifier,
27593 which is either the "attribute" keyword, a storage class
27594 specifier, or (usually) a type-specifier. */
27595 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
27596 {
27597 tree type;
27598 tree pushed_scope = NULL_TREE;
27599 unsigned saved_num_template_parameter_lists;
27600
27601 /* Names appearing in the type-specifier should be looked up
27602 in the scope of the class. */
27603 if (current_class_type)
27604 type = NULL_TREE;
27605 else if (type_decl)
27606 {
27607 type = TREE_TYPE (type_decl);
27608 if (TREE_CODE (type) == TYPENAME_TYPE)
27609 {
27610 type = resolve_typename_type (type,
27611 /*only_current_p=*/false);
27612 if (TREE_CODE (type) == TYPENAME_TYPE)
27613 {
27614 cp_parser_abort_tentative_parse (parser);
27615 return false;
27616 }
27617 }
27618 pushed_scope = push_scope (type);
27619 }
27620
27621 /* Inside the constructor parameter list, surrounding
27622 template-parameter-lists do not apply. */
27623 saved_num_template_parameter_lists
27624 = parser->num_template_parameter_lists;
27625 parser->num_template_parameter_lists = 0;
27626
27627 /* Look for the type-specifier. It's not optional, but its typename
27628 might be. */
27629 cp_parser_type_specifier (parser,
27630 (flags & ~CP_PARSER_FLAGS_OPTIONAL),
27631 /*decl_specs=*/NULL,
27632 /*is_declarator=*/true,
27633 /*declares_class_or_enum=*/NULL,
27634 /*is_cv_qualifier=*/NULL);
27635
27636 parser->num_template_parameter_lists
27637 = saved_num_template_parameter_lists;
27638
27639 /* Leave the scope of the class. */
27640 if (pushed_scope)
27641 pop_scope (pushed_scope);
27642
27643 constructor_p = !cp_parser_error_occurred (parser);
27644 }
27645 }
27646
27647 /* We did not really want to consume any tokens. */
27648 cp_parser_abort_tentative_parse (parser);
27649
27650 return constructor_p;
27651 }
27652
27653 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27654 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27655 they must be performed once we are in the scope of the function.
27656
27657 Returns the function defined. */
27658
27659 static tree
27660 cp_parser_function_definition_from_specifiers_and_declarator
27661 (cp_parser* parser,
27662 cp_decl_specifier_seq *decl_specifiers,
27663 tree attributes,
27664 const cp_declarator *declarator)
27665 {
27666 tree fn;
27667 bool success_p;
27668
27669 /* Begin the function-definition. */
27670 success_p = start_function (decl_specifiers, declarator, attributes);
27671
27672 /* The things we're about to see are not directly qualified by any
27673 template headers we've seen thus far. */
27674 reset_specialization ();
27675
27676 /* If there were names looked up in the decl-specifier-seq that we
27677 did not check, check them now. We must wait until we are in the
27678 scope of the function to perform the checks, since the function
27679 might be a friend. */
27680 perform_deferred_access_checks (tf_warning_or_error);
27681
27682 if (success_p)
27683 {
27684 cp_finalize_omp_declare_simd (parser, current_function_decl);
27685 parser->omp_declare_simd = NULL;
27686 cp_finalize_oacc_routine (parser, current_function_decl, true);
27687 parser->oacc_routine = NULL;
27688 }
27689
27690 if (!success_p)
27691 {
27692 /* Skip the entire function. */
27693 cp_parser_skip_to_end_of_block_or_statement (parser);
27694 fn = error_mark_node;
27695 }
27696 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27697 {
27698 /* Seen already, skip it. An error message has already been output. */
27699 cp_parser_skip_to_end_of_block_or_statement (parser);
27700 fn = current_function_decl;
27701 current_function_decl = NULL_TREE;
27702 /* If this is a function from a class, pop the nested class. */
27703 if (current_class_name)
27704 pop_nested_class ();
27705 }
27706 else
27707 {
27708 timevar_id_t tv;
27709 if (DECL_DECLARED_INLINE_P (current_function_decl))
27710 tv = TV_PARSE_INLINE;
27711 else
27712 tv = TV_PARSE_FUNC;
27713 timevar_push (tv);
27714 fn = cp_parser_function_definition_after_declarator (parser,
27715 /*inline_p=*/false);
27716 timevar_pop (tv);
27717 }
27718
27719 return fn;
27720 }
27721
27722 /* Parse the part of a function-definition that follows the
27723 declarator. INLINE_P is TRUE iff this function is an inline
27724 function defined within a class-specifier.
27725
27726 Returns the function defined. */
27727
27728 static tree
27729 cp_parser_function_definition_after_declarator (cp_parser* parser,
27730 bool inline_p)
27731 {
27732 tree fn;
27733 bool saved_in_unbraced_linkage_specification_p;
27734 bool saved_in_function_body;
27735 unsigned saved_num_template_parameter_lists;
27736 cp_token *token;
27737 bool fully_implicit_function_template_p
27738 = parser->fully_implicit_function_template_p;
27739 parser->fully_implicit_function_template_p = false;
27740 tree implicit_template_parms
27741 = parser->implicit_template_parms;
27742 parser->implicit_template_parms = 0;
27743 cp_binding_level* implicit_template_scope
27744 = parser->implicit_template_scope;
27745 parser->implicit_template_scope = 0;
27746
27747 saved_in_function_body = parser->in_function_body;
27748 parser->in_function_body = true;
27749 /* If the next token is `return', then the code may be trying to
27750 make use of the "named return value" extension that G++ used to
27751 support. */
27752 token = cp_lexer_peek_token (parser->lexer);
27753 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27754 {
27755 /* Consume the `return' keyword. */
27756 cp_lexer_consume_token (parser->lexer);
27757 /* Look for the identifier that indicates what value is to be
27758 returned. */
27759 cp_parser_identifier (parser);
27760 /* Issue an error message. */
27761 error_at (token->location,
27762 "named return values are no longer supported");
27763 /* Skip tokens until we reach the start of the function body. */
27764 while (true)
27765 {
27766 cp_token *token = cp_lexer_peek_token (parser->lexer);
27767 if (token->type == CPP_OPEN_BRACE
27768 || token->type == CPP_EOF
27769 || token->type == CPP_PRAGMA_EOL)
27770 break;
27771 cp_lexer_consume_token (parser->lexer);
27772 }
27773 }
27774 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27775 anything declared inside `f'. */
27776 saved_in_unbraced_linkage_specification_p
27777 = parser->in_unbraced_linkage_specification_p;
27778 parser->in_unbraced_linkage_specification_p = false;
27779 /* Inside the function, surrounding template-parameter-lists do not
27780 apply. */
27781 saved_num_template_parameter_lists
27782 = parser->num_template_parameter_lists;
27783 parser->num_template_parameter_lists = 0;
27784
27785 /* If the next token is `try', `__transaction_atomic', or
27786 `__transaction_relaxed`, then we are looking at either function-try-block
27787 or function-transaction-block. Note that all of these include the
27788 function-body. */
27789 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27790 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27791 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27792 RID_TRANSACTION_RELAXED))
27793 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27794 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27795 cp_parser_function_try_block (parser);
27796 else
27797 cp_parser_ctor_initializer_opt_and_function_body
27798 (parser, /*in_function_try_block=*/false);
27799
27800 /* Finish the function. */
27801 fn = finish_function (inline_p);
27802 /* Generate code for it, if necessary. */
27803 expand_or_defer_fn (fn);
27804 /* Restore the saved values. */
27805 parser->in_unbraced_linkage_specification_p
27806 = saved_in_unbraced_linkage_specification_p;
27807 parser->num_template_parameter_lists
27808 = saved_num_template_parameter_lists;
27809 parser->in_function_body = saved_in_function_body;
27810
27811 parser->fully_implicit_function_template_p
27812 = fully_implicit_function_template_p;
27813 parser->implicit_template_parms
27814 = implicit_template_parms;
27815 parser->implicit_template_scope
27816 = implicit_template_scope;
27817
27818 if (parser->fully_implicit_function_template_p)
27819 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27820
27821 return fn;
27822 }
27823
27824 /* Parse a template-declaration body (following argument list). */
27825
27826 static void
27827 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27828 tree parameter_list,
27829 bool member_p)
27830 {
27831 tree decl = NULL_TREE;
27832 bool friend_p = false;
27833
27834 /* We just processed one more parameter list. */
27835 ++parser->num_template_parameter_lists;
27836
27837 /* Get the deferred access checks from the parameter list. These
27838 will be checked once we know what is being declared, as for a
27839 member template the checks must be performed in the scope of the
27840 class containing the member. */
27841 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27842
27843 /* Tentatively parse for a new template parameter list, which can either be
27844 the template keyword or a template introduction. */
27845 if (cp_parser_template_declaration_after_export (parser, member_p))
27846 /* OK */;
27847 else if (cxx_dialect >= cxx11
27848 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27849 decl = cp_parser_alias_declaration (parser);
27850 else
27851 {
27852 /* There are no access checks when parsing a template, as we do not
27853 know if a specialization will be a friend. */
27854 push_deferring_access_checks (dk_no_check);
27855 cp_token *token = cp_lexer_peek_token (parser->lexer);
27856 decl = cp_parser_single_declaration (parser,
27857 checks,
27858 member_p,
27859 /*explicit_specialization_p=*/false,
27860 &friend_p);
27861 pop_deferring_access_checks ();
27862
27863 /* If this is a member template declaration, let the front
27864 end know. */
27865 if (member_p && !friend_p && decl)
27866 {
27867 if (TREE_CODE (decl) == TYPE_DECL)
27868 cp_parser_check_access_in_redeclaration (decl, token->location);
27869
27870 decl = finish_member_template_decl (decl);
27871 }
27872 else if (friend_p && decl
27873 && DECL_DECLARES_TYPE_P (decl))
27874 make_friend_class (current_class_type, TREE_TYPE (decl),
27875 /*complain=*/true);
27876 }
27877 /* We are done with the current parameter list. */
27878 --parser->num_template_parameter_lists;
27879
27880 pop_deferring_access_checks ();
27881
27882 /* Finish up. */
27883 finish_template_decl (parameter_list);
27884
27885 /* Check the template arguments for a literal operator template. */
27886 if (decl
27887 && DECL_DECLARES_FUNCTION_P (decl)
27888 && UDLIT_OPER_P (DECL_NAME (decl)))
27889 {
27890 bool ok = true;
27891 if (parameter_list == NULL_TREE)
27892 ok = false;
27893 else
27894 {
27895 int num_parms = TREE_VEC_LENGTH (parameter_list);
27896 if (num_parms == 1)
27897 {
27898 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27899 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27900 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27901 /* OK, C++20 string literal operator template. We don't need
27902 to warn in lower dialects here because we will have already
27903 warned about the template parameter. */;
27904 else if (TREE_TYPE (parm) != char_type_node
27905 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27906 ok = false;
27907 }
27908 else if (num_parms == 2 && cxx_dialect >= cxx14)
27909 {
27910 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27911 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27912 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27913 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27914 if (parm == error_mark_node
27915 || TREE_TYPE (parm) != TREE_TYPE (type)
27916 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27917 ok = false;
27918 else
27919 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27920 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27921 "ISO C++ did not adopt string literal operator templa"
27922 "tes taking an argument pack of characters");
27923 }
27924 else
27925 ok = false;
27926 }
27927 if (!ok)
27928 {
27929 if (cxx_dialect > cxx17)
27930 error ("literal operator template %qD has invalid parameter list;"
27931 " Expected non-type template parameter pack <char...> "
27932 "or single non-type parameter of class type",
27933 decl);
27934 else
27935 error ("literal operator template %qD has invalid parameter list."
27936 " Expected non-type template parameter pack <char...>",
27937 decl);
27938 }
27939 }
27940
27941 /* Register member declarations. */
27942 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27943 finish_member_declaration (decl);
27944 /* If DECL is a function template, we must return to parse it later.
27945 (Even though there is no definition, there might be default
27946 arguments that need handling.) */
27947 if (member_p && decl
27948 && DECL_DECLARES_FUNCTION_P (decl))
27949 vec_safe_push (unparsed_funs_with_definitions, decl);
27950 }
27951
27952 /* Parse a template introduction header for a template-declaration. Returns
27953 false if tentative parse fails. */
27954
27955 static bool
27956 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27957 {
27958 cp_parser_parse_tentatively (parser);
27959
27960 tree saved_scope = parser->scope;
27961 tree saved_object_scope = parser->object_scope;
27962 tree saved_qualifying_scope = parser->qualifying_scope;
27963
27964 /* Look for the optional `::' operator. */
27965 cp_parser_global_scope_opt (parser,
27966 /*current_scope_valid_p=*/false);
27967 /* Look for the nested-name-specifier. */
27968 cp_parser_nested_name_specifier_opt (parser,
27969 /*typename_keyword_p=*/false,
27970 /*check_dependency_p=*/true,
27971 /*type_p=*/false,
27972 /*is_declaration=*/false);
27973
27974 cp_token *token = cp_lexer_peek_token (parser->lexer);
27975 tree concept_name = cp_parser_identifier (parser);
27976
27977 /* Look up the concept for which we will be matching
27978 template parameters. */
27979 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27980 token->location);
27981 parser->scope = saved_scope;
27982 parser->object_scope = saved_object_scope;
27983 parser->qualifying_scope = saved_qualifying_scope;
27984
27985 if (concept_name == error_mark_node)
27986 cp_parser_simulate_error (parser);
27987
27988 /* Look for opening brace for introduction. */
27989 matching_braces braces;
27990 braces.require_open (parser);
27991
27992 if (!cp_parser_parse_definitely (parser))
27993 return false;
27994
27995 push_deferring_access_checks (dk_deferred);
27996
27997 /* Build vector of placeholder parameters and grab
27998 matching identifiers. */
27999 tree introduction_list = cp_parser_introduction_list (parser);
28000
28001 /* Look for closing brace for introduction. */
28002 if (!braces.require_close (parser))
28003 return true;
28004
28005 /* The introduction-list shall not be empty. */
28006 int nargs = TREE_VEC_LENGTH (introduction_list);
28007 if (nargs == 0)
28008 {
28009 /* In cp_parser_introduction_list we have already issued an error. */
28010 return true;
28011 }
28012
28013 if (tmpl_decl == error_mark_node)
28014 {
28015 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
28016 token->location);
28017 return true;
28018 }
28019
28020 /* Build and associate the constraint. */
28021 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
28022 if (parms && parms != error_mark_node)
28023 {
28024 cp_parser_template_declaration_after_parameters (parser, parms,
28025 member_p);
28026 return true;
28027 }
28028
28029 error_at (token->location, "no matching concept for template-introduction");
28030 return true;
28031 }
28032
28033 /* Parse a normal template-declaration following the template keyword. */
28034
28035 static void
28036 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
28037 {
28038 tree parameter_list;
28039 bool need_lang_pop;
28040 location_t location = input_location;
28041
28042 /* Look for the `<' token. */
28043 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
28044 return;
28045 if (at_class_scope_p () && current_function_decl)
28046 {
28047 /* 14.5.2.2 [temp.mem]
28048
28049 A local class shall not have member templates. */
28050 error_at (location,
28051 "invalid declaration of member template in local class");
28052 cp_parser_skip_to_end_of_block_or_statement (parser);
28053 return;
28054 }
28055 /* [temp]
28056
28057 A template ... shall not have C linkage. */
28058 if (current_lang_name == lang_name_c)
28059 {
28060 error_at (location, "template with C linkage");
28061 maybe_show_extern_c_location ();
28062 /* Give it C++ linkage to avoid confusing other parts of the
28063 front end. */
28064 push_lang_context (lang_name_cplusplus);
28065 need_lang_pop = true;
28066 }
28067 else
28068 need_lang_pop = false;
28069
28070 /* We cannot perform access checks on the template parameter
28071 declarations until we know what is being declared, just as we
28072 cannot check the decl-specifier list. */
28073 push_deferring_access_checks (dk_deferred);
28074
28075 /* If the next token is `>', then we have an invalid
28076 specialization. Rather than complain about an invalid template
28077 parameter, issue an error message here. */
28078 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
28079 {
28080 cp_parser_error (parser, "invalid explicit specialization");
28081 begin_specialization ();
28082 parameter_list = NULL_TREE;
28083 }
28084 else
28085 {
28086 /* Parse the template parameters. */
28087 parameter_list = cp_parser_template_parameter_list (parser);
28088 }
28089
28090 /* Look for the `>'. */
28091 cp_parser_skip_to_end_of_template_parameter_list (parser);
28092
28093 /* Manage template requirements */
28094 if (flag_concepts)
28095 {
28096 tree reqs = get_shorthand_constraints (current_template_parms);
28097 if (tree r = cp_parser_requires_clause_opt (parser))
28098 reqs = conjoin_constraints (reqs, normalize_expression (r));
28099 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
28100 }
28101
28102 cp_parser_template_declaration_after_parameters (parser, parameter_list,
28103 member_p);
28104
28105 /* For the erroneous case of a template with C linkage, we pushed an
28106 implicit C++ linkage scope; exit that scope now. */
28107 if (need_lang_pop)
28108 pop_lang_context ();
28109 }
28110
28111 /* Parse a template-declaration, assuming that the `export' (and
28112 `extern') keywords, if present, has already been scanned. MEMBER_P
28113 is as for cp_parser_template_declaration. */
28114
28115 static bool
28116 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
28117 {
28118 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28119 {
28120 cp_lexer_consume_token (parser->lexer);
28121 cp_parser_explicit_template_declaration (parser, member_p);
28122 return true;
28123 }
28124 else if (flag_concepts)
28125 return cp_parser_template_introduction (parser, member_p);
28126
28127 return false;
28128 }
28129
28130 /* Perform the deferred access checks from a template-parameter-list.
28131 CHECKS is a TREE_LIST of access checks, as returned by
28132 get_deferred_access_checks. */
28133
28134 static void
28135 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
28136 {
28137 ++processing_template_parmlist;
28138 perform_access_checks (checks, tf_warning_or_error);
28139 --processing_template_parmlist;
28140 }
28141
28142 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
28143 `function-definition' sequence that follows a template header.
28144 If MEMBER_P is true, this declaration appears in a class scope.
28145
28146 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
28147 *FRIEND_P is set to TRUE iff the declaration is a friend. */
28148
28149 static tree
28150 cp_parser_single_declaration (cp_parser* parser,
28151 vec<deferred_access_check, va_gc> *checks,
28152 bool member_p,
28153 bool explicit_specialization_p,
28154 bool* friend_p)
28155 {
28156 int declares_class_or_enum;
28157 tree decl = NULL_TREE;
28158 cp_decl_specifier_seq decl_specifiers;
28159 bool function_definition_p = false;
28160 cp_token *decl_spec_token_start;
28161
28162 /* This function is only used when processing a template
28163 declaration. */
28164 gcc_assert (innermost_scope_kind () == sk_template_parms
28165 || innermost_scope_kind () == sk_template_spec);
28166
28167 /* Defer access checks until we know what is being declared. */
28168 push_deferring_access_checks (dk_deferred);
28169
28170 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
28171 alternative. */
28172 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
28173 cp_parser_decl_specifier_seq (parser,
28174 (CP_PARSER_FLAGS_OPTIONAL
28175 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
28176 &decl_specifiers,
28177 &declares_class_or_enum);
28178 if (friend_p)
28179 *friend_p = cp_parser_friend_p (&decl_specifiers);
28180
28181 /* There are no template typedefs. */
28182 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
28183 {
28184 error_at (decl_spec_token_start->location,
28185 "template declaration of %<typedef%>");
28186 decl = error_mark_node;
28187 }
28188
28189 /* Gather up the access checks that occurred the
28190 decl-specifier-seq. */
28191 stop_deferring_access_checks ();
28192
28193 /* Check for the declaration of a template class. */
28194 if (declares_class_or_enum)
28195 {
28196 if (cp_parser_declares_only_class_p (parser)
28197 || (declares_class_or_enum & 2))
28198 {
28199 // If this is a declaration, but not a definition, associate
28200 // any constraints with the type declaration. Constraints
28201 // are associated with definitions in cp_parser_class_specifier.
28202 if (declares_class_or_enum == 1)
28203 associate_classtype_constraints (decl_specifiers.type);
28204
28205 decl = shadow_tag (&decl_specifiers);
28206
28207 /* In this case:
28208
28209 struct C {
28210 friend template <typename T> struct A<T>::B;
28211 };
28212
28213 A<T>::B will be represented by a TYPENAME_TYPE, and
28214 therefore not recognized by shadow_tag. */
28215 if (friend_p && *friend_p
28216 && !decl
28217 && decl_specifiers.type
28218 && TYPE_P (decl_specifiers.type))
28219 decl = decl_specifiers.type;
28220
28221 if (decl && decl != error_mark_node)
28222 decl = TYPE_NAME (decl);
28223 else
28224 decl = error_mark_node;
28225
28226 /* Perform access checks for template parameters. */
28227 cp_parser_perform_template_parameter_access_checks (checks);
28228
28229 /* Give a helpful diagnostic for
28230 template <class T> struct A { } a;
28231 if we aren't already recovering from an error. */
28232 if (!cp_parser_declares_only_class_p (parser)
28233 && !seen_error ())
28234 {
28235 error_at (cp_lexer_peek_token (parser->lexer)->location,
28236 "a class template declaration must not declare "
28237 "anything else");
28238 cp_parser_skip_to_end_of_block_or_statement (parser);
28239 goto out;
28240 }
28241 }
28242 }
28243
28244 /* Complain about missing 'typename' or other invalid type names. */
28245 if (!decl_specifiers.any_type_specifiers_p
28246 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
28247 {
28248 /* cp_parser_parse_and_diagnose_invalid_type_name calls
28249 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28250 the rest of this declaration. */
28251 decl = error_mark_node;
28252 goto out;
28253 }
28254
28255 /* If it's not a template class, try for a template function. If
28256 the next token is a `;', then this declaration does not declare
28257 anything. But, if there were errors in the decl-specifiers, then
28258 the error might well have come from an attempted class-specifier.
28259 In that case, there's no need to warn about a missing declarator. */
28260 if (!decl
28261 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
28262 || decl_specifiers.type != error_mark_node))
28263 {
28264 decl = cp_parser_init_declarator (parser,
28265 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
28266 &decl_specifiers,
28267 checks,
28268 /*function_definition_allowed_p=*/true,
28269 member_p,
28270 declares_class_or_enum,
28271 &function_definition_p,
28272 NULL, NULL, NULL);
28273
28274 /* 7.1.1-1 [dcl.stc]
28275
28276 A storage-class-specifier shall not be specified in an explicit
28277 specialization... */
28278 if (decl
28279 && explicit_specialization_p
28280 && decl_specifiers.storage_class != sc_none)
28281 {
28282 error_at (decl_spec_token_start->location,
28283 "explicit template specialization cannot have a storage class");
28284 decl = error_mark_node;
28285 }
28286
28287 if (decl && VAR_P (decl))
28288 check_template_variable (decl);
28289 }
28290
28291 /* Look for a trailing `;' after the declaration. */
28292 if (!function_definition_p
28293 && (decl == error_mark_node
28294 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
28295 cp_parser_skip_to_end_of_block_or_statement (parser);
28296
28297 out:
28298 pop_deferring_access_checks ();
28299
28300 /* Clear any current qualification; whatever comes next is the start
28301 of something new. */
28302 parser->scope = NULL_TREE;
28303 parser->qualifying_scope = NULL_TREE;
28304 parser->object_scope = NULL_TREE;
28305
28306 return decl;
28307 }
28308
28309 /* Parse a cast-expression that is not the operand of a unary "&". */
28310
28311 static cp_expr
28312 cp_parser_simple_cast_expression (cp_parser *parser)
28313 {
28314 return cp_parser_cast_expression (parser, /*address_p=*/false,
28315 /*cast_p=*/false, /*decltype*/false, NULL);
28316 }
28317
28318 /* Parse a functional cast to TYPE. Returns an expression
28319 representing the cast. */
28320
28321 static cp_expr
28322 cp_parser_functional_cast (cp_parser* parser, tree type)
28323 {
28324 vec<tree, va_gc> *vec;
28325 tree expression_list;
28326 cp_expr cast;
28327 bool nonconst_p;
28328
28329 location_t start_loc = input_location;
28330
28331 if (!type)
28332 type = error_mark_node;
28333
28334 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28335 {
28336 cp_lexer_set_source_position (parser->lexer);
28337 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28338 expression_list = cp_parser_braced_list (parser, &nonconst_p);
28339 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
28340 if (TREE_CODE (type) == TYPE_DECL)
28341 type = TREE_TYPE (type);
28342
28343 cast = finish_compound_literal (type, expression_list,
28344 tf_warning_or_error, fcl_functional);
28345 /* Create a location of the form:
28346 type_name{i, f}
28347 ^~~~~~~~~~~~~~~
28348 with caret == start at the start of the type name,
28349 finishing at the closing brace. */
28350 location_t finish_loc
28351 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28352 location_t combined_loc = make_location (start_loc, start_loc,
28353 finish_loc);
28354 cast.set_location (combined_loc);
28355 return cast;
28356 }
28357
28358
28359 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
28360 /*cast_p=*/true,
28361 /*allow_expansion_p=*/true,
28362 /*non_constant_p=*/NULL);
28363 if (vec == NULL)
28364 expression_list = error_mark_node;
28365 else
28366 {
28367 expression_list = build_tree_list_vec (vec);
28368 release_tree_vector (vec);
28369 }
28370
28371 cast = build_functional_cast (type, expression_list,
28372 tf_warning_or_error);
28373 /* [expr.const]/1: In an integral constant expression "only type
28374 conversions to integral or enumeration type can be used". */
28375 if (TREE_CODE (type) == TYPE_DECL)
28376 type = TREE_TYPE (type);
28377 if (cast != error_mark_node
28378 && !cast_valid_in_integral_constant_expression_p (type)
28379 && cp_parser_non_integral_constant_expression (parser,
28380 NIC_CONSTRUCTOR))
28381 return error_mark_node;
28382
28383 /* Create a location of the form:
28384 float(i)
28385 ^~~~~~~~
28386 with caret == start at the start of the type name,
28387 finishing at the closing paren. */
28388 location_t finish_loc
28389 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28390 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
28391 cast.set_location (combined_loc);
28392 return cast;
28393 }
28394
28395 /* Save the tokens that make up the body of a member function defined
28396 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28397 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28398 specifiers applied to the declaration. Returns the FUNCTION_DECL
28399 for the member function. */
28400
28401 static tree
28402 cp_parser_save_member_function_body (cp_parser* parser,
28403 cp_decl_specifier_seq *decl_specifiers,
28404 cp_declarator *declarator,
28405 tree attributes)
28406 {
28407 cp_token *first;
28408 cp_token *last;
28409 tree fn;
28410 bool function_try_block = false;
28411
28412 /* Create the FUNCTION_DECL. */
28413 fn = grokmethod (decl_specifiers, declarator, attributes);
28414 cp_finalize_omp_declare_simd (parser, fn);
28415 cp_finalize_oacc_routine (parser, fn, true);
28416 /* If something went badly wrong, bail out now. */
28417 if (fn == error_mark_node)
28418 {
28419 /* If there's a function-body, skip it. */
28420 if (cp_parser_token_starts_function_definition_p
28421 (cp_lexer_peek_token (parser->lexer)))
28422 cp_parser_skip_to_end_of_block_or_statement (parser);
28423 return error_mark_node;
28424 }
28425
28426 /* Remember it, if there default args to post process. */
28427 cp_parser_save_default_args (parser, fn);
28428
28429 /* Save away the tokens that make up the body of the
28430 function. */
28431 first = parser->lexer->next_token;
28432
28433 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
28434 cp_lexer_consume_token (parser->lexer);
28435 else if (cp_lexer_next_token_is_keyword (parser->lexer,
28436 RID_TRANSACTION_ATOMIC))
28437 {
28438 cp_lexer_consume_token (parser->lexer);
28439 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28440 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
28441 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
28442 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
28443 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28444 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
28445 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
28446 {
28447 cp_lexer_consume_token (parser->lexer);
28448 cp_lexer_consume_token (parser->lexer);
28449 cp_lexer_consume_token (parser->lexer);
28450 cp_lexer_consume_token (parser->lexer);
28451 cp_lexer_consume_token (parser->lexer);
28452 }
28453 else
28454 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
28455 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
28456 {
28457 cp_lexer_consume_token (parser->lexer);
28458 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28459 break;
28460 }
28461 }
28462
28463 /* Handle function try blocks. */
28464 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28465 {
28466 cp_lexer_consume_token (parser->lexer);
28467 function_try_block = true;
28468 }
28469 /* We can have braced-init-list mem-initializers before the fn body. */
28470 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28471 {
28472 cp_lexer_consume_token (parser->lexer);
28473 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
28474 {
28475 /* cache_group will stop after an un-nested { } pair, too. */
28476 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28477 break;
28478
28479 /* variadic mem-inits have ... after the ')'. */
28480 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28481 cp_lexer_consume_token (parser->lexer);
28482 }
28483 }
28484 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28485 /* Handle function try blocks. */
28486 if (function_try_block)
28487 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
28488 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28489 last = parser->lexer->next_token;
28490
28491 /* Save away the inline definition; we will process it when the
28492 class is complete. */
28493 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
28494 DECL_PENDING_INLINE_P (fn) = 1;
28495
28496 /* We need to know that this was defined in the class, so that
28497 friend templates are handled correctly. */
28498 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
28499
28500 /* Add FN to the queue of functions to be parsed later. */
28501 vec_safe_push (unparsed_funs_with_definitions, fn);
28502
28503 return fn;
28504 }
28505
28506 /* Save the tokens that make up the in-class initializer for a non-static
28507 data member. Returns a DEFAULT_ARG. */
28508
28509 static tree
28510 cp_parser_save_nsdmi (cp_parser* parser)
28511 {
28512 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
28513 }
28514
28515 /* Parse a template-argument-list, as well as the trailing ">" (but
28516 not the opening "<"). See cp_parser_template_argument_list for the
28517 return value. */
28518
28519 static tree
28520 cp_parser_enclosed_template_argument_list (cp_parser* parser)
28521 {
28522 tree arguments;
28523 tree saved_scope;
28524 tree saved_qualifying_scope;
28525 tree saved_object_scope;
28526 bool saved_greater_than_is_operator_p;
28527
28528 /* [temp.names]
28529
28530 When parsing a template-id, the first non-nested `>' is taken as
28531 the end of the template-argument-list rather than a greater-than
28532 operator. */
28533 saved_greater_than_is_operator_p
28534 = parser->greater_than_is_operator_p;
28535 parser->greater_than_is_operator_p = false;
28536 /* Parsing the argument list may modify SCOPE, so we save it
28537 here. */
28538 saved_scope = parser->scope;
28539 saved_qualifying_scope = parser->qualifying_scope;
28540 saved_object_scope = parser->object_scope;
28541 /* We need to evaluate the template arguments, even though this
28542 template-id may be nested within a "sizeof". */
28543 cp_evaluated ev;
28544 /* Parse the template-argument-list itself. */
28545 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
28546 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28547 arguments = NULL_TREE;
28548 else
28549 arguments = cp_parser_template_argument_list (parser);
28550 /* Look for the `>' that ends the template-argument-list. If we find
28551 a '>>' instead, it's probably just a typo. */
28552 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28553 {
28554 if (cxx_dialect != cxx98)
28555 {
28556 /* In C++0x, a `>>' in a template argument list or cast
28557 expression is considered to be two separate `>'
28558 tokens. So, change the current token to a `>', but don't
28559 consume it: it will be consumed later when the outer
28560 template argument list (or cast expression) is parsed.
28561 Note that this replacement of `>' for `>>' is necessary
28562 even if we are parsing tentatively: in the tentative
28563 case, after calling
28564 cp_parser_enclosed_template_argument_list we will always
28565 throw away all of the template arguments and the first
28566 closing `>', either because the template argument list
28567 was erroneous or because we are replacing those tokens
28568 with a CPP_TEMPLATE_ID token. The second `>' (which will
28569 not have been thrown away) is needed either to close an
28570 outer template argument list or to complete a new-style
28571 cast. */
28572 cp_token *token = cp_lexer_peek_token (parser->lexer);
28573 token->type = CPP_GREATER;
28574 }
28575 else if (!saved_greater_than_is_operator_p)
28576 {
28577 /* If we're in a nested template argument list, the '>>' has
28578 to be a typo for '> >'. We emit the error message, but we
28579 continue parsing and we push a '>' as next token, so that
28580 the argument list will be parsed correctly. Note that the
28581 global source location is still on the token before the
28582 '>>', so we need to say explicitly where we want it. */
28583 cp_token *token = cp_lexer_peek_token (parser->lexer);
28584 gcc_rich_location richloc (token->location);
28585 richloc.add_fixit_replace ("> >");
28586 error_at (&richloc, "%<>>%> should be %<> >%> "
28587 "within a nested template argument list");
28588
28589 token->type = CPP_GREATER;
28590 }
28591 else
28592 {
28593 /* If this is not a nested template argument list, the '>>'
28594 is a typo for '>'. Emit an error message and continue.
28595 Same deal about the token location, but here we can get it
28596 right by consuming the '>>' before issuing the diagnostic. */
28597 cp_token *token = cp_lexer_consume_token (parser->lexer);
28598 error_at (token->location,
28599 "spurious %<>>%>, use %<>%> to terminate "
28600 "a template argument list");
28601 }
28602 }
28603 else
28604 cp_parser_skip_to_end_of_template_parameter_list (parser);
28605 /* The `>' token might be a greater-than operator again now. */
28606 parser->greater_than_is_operator_p
28607 = saved_greater_than_is_operator_p;
28608 /* Restore the SAVED_SCOPE. */
28609 parser->scope = saved_scope;
28610 parser->qualifying_scope = saved_qualifying_scope;
28611 parser->object_scope = saved_object_scope;
28612
28613 return arguments;
28614 }
28615
28616 /* MEMBER_FUNCTION is a member function, or a friend. If default
28617 arguments, or the body of the function have not yet been parsed,
28618 parse them now. */
28619
28620 static void
28621 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
28622 {
28623 timevar_push (TV_PARSE_INMETH);
28624 /* If this member is a template, get the underlying
28625 FUNCTION_DECL. */
28626 if (DECL_FUNCTION_TEMPLATE_P (member_function))
28627 member_function = DECL_TEMPLATE_RESULT (member_function);
28628
28629 /* There should not be any class definitions in progress at this
28630 point; the bodies of members are only parsed outside of all class
28631 definitions. */
28632 gcc_assert (parser->num_classes_being_defined == 0);
28633 /* While we're parsing the member functions we might encounter more
28634 classes. We want to handle them right away, but we don't want
28635 them getting mixed up with functions that are currently in the
28636 queue. */
28637 push_unparsed_function_queues (parser);
28638
28639 /* Make sure that any template parameters are in scope. */
28640 maybe_begin_member_template_processing (member_function);
28641
28642 /* If the body of the function has not yet been parsed, parse it
28643 now. */
28644 if (DECL_PENDING_INLINE_P (member_function))
28645 {
28646 tree function_scope;
28647 cp_token_cache *tokens;
28648
28649 /* The function is no longer pending; we are processing it. */
28650 tokens = DECL_PENDING_INLINE_INFO (member_function);
28651 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28652 DECL_PENDING_INLINE_P (member_function) = 0;
28653
28654 /* If this is a local class, enter the scope of the containing
28655 function. */
28656 function_scope = current_function_decl;
28657 if (function_scope)
28658 push_function_context ();
28659
28660 /* Push the body of the function onto the lexer stack. */
28661 cp_parser_push_lexer_for_tokens (parser, tokens);
28662
28663 /* Let the front end know that we going to be defining this
28664 function. */
28665 start_preparsed_function (member_function, NULL_TREE,
28666 SF_PRE_PARSED | SF_INCLASS_INLINE);
28667
28668 /* Don't do access checking if it is a templated function. */
28669 if (processing_template_decl)
28670 push_deferring_access_checks (dk_no_check);
28671
28672 /* #pragma omp declare reduction needs special parsing. */
28673 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28674 {
28675 parser->lexer->in_pragma = true;
28676 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28677 finish_function (/*inline_p=*/true);
28678 cp_check_omp_declare_reduction (member_function);
28679 }
28680 else
28681 /* Now, parse the body of the function. */
28682 cp_parser_function_definition_after_declarator (parser,
28683 /*inline_p=*/true);
28684
28685 if (processing_template_decl)
28686 pop_deferring_access_checks ();
28687
28688 /* Leave the scope of the containing function. */
28689 if (function_scope)
28690 pop_function_context ();
28691 cp_parser_pop_lexer (parser);
28692 }
28693
28694 /* Remove any template parameters from the symbol table. */
28695 maybe_end_member_template_processing ();
28696
28697 /* Restore the queue. */
28698 pop_unparsed_function_queues (parser);
28699 timevar_pop (TV_PARSE_INMETH);
28700 }
28701
28702 /* If DECL contains any default args, remember it on the unparsed
28703 functions queue. */
28704
28705 static void
28706 cp_parser_save_default_args (cp_parser* parser, tree decl)
28707 {
28708 tree probe;
28709
28710 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28711 probe;
28712 probe = TREE_CHAIN (probe))
28713 if (TREE_PURPOSE (probe))
28714 {
28715 cp_default_arg_entry entry = {current_class_type, decl};
28716 vec_safe_push (unparsed_funs_with_default_args, entry);
28717 break;
28718 }
28719 }
28720
28721 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28722 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28723 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28724 from the parameter-type-list. */
28725
28726 static tree
28727 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28728 tree default_arg, tree parmtype)
28729 {
28730 cp_token_cache *tokens;
28731 tree parsed_arg;
28732 bool dummy;
28733
28734 if (default_arg == error_mark_node)
28735 return error_mark_node;
28736
28737 /* Push the saved tokens for the default argument onto the parser's
28738 lexer stack. */
28739 tokens = DEFARG_TOKENS (default_arg);
28740 cp_parser_push_lexer_for_tokens (parser, tokens);
28741
28742 start_lambda_scope (decl);
28743
28744 /* Parse the default argument. */
28745 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28746 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28747 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28748
28749 finish_lambda_scope ();
28750
28751 if (parsed_arg == error_mark_node)
28752 cp_parser_skip_to_end_of_statement (parser);
28753
28754 if (!processing_template_decl)
28755 {
28756 /* In a non-template class, check conversions now. In a template,
28757 we'll wait and instantiate these as needed. */
28758 if (TREE_CODE (decl) == PARM_DECL)
28759 parsed_arg = check_default_argument (parmtype, parsed_arg,
28760 tf_warning_or_error);
28761 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28762 parsed_arg = error_mark_node;
28763 else
28764 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28765 }
28766
28767 /* If the token stream has not been completely used up, then
28768 there was extra junk after the end of the default
28769 argument. */
28770 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28771 {
28772 if (TREE_CODE (decl) == PARM_DECL)
28773 cp_parser_error (parser, "expected %<,%>");
28774 else
28775 cp_parser_error (parser, "expected %<;%>");
28776 }
28777
28778 /* Revert to the main lexer. */
28779 cp_parser_pop_lexer (parser);
28780
28781 return parsed_arg;
28782 }
28783
28784 /* FIELD is a non-static data member with an initializer which we saved for
28785 later; parse it now. */
28786
28787 static void
28788 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28789 {
28790 tree def;
28791
28792 maybe_begin_member_template_processing (field);
28793
28794 push_unparsed_function_queues (parser);
28795 def = cp_parser_late_parse_one_default_arg (parser, field,
28796 DECL_INITIAL (field),
28797 NULL_TREE);
28798 pop_unparsed_function_queues (parser);
28799
28800 maybe_end_member_template_processing ();
28801
28802 DECL_INITIAL (field) = def;
28803 }
28804
28805 /* FN is a FUNCTION_DECL which may contains a parameter with an
28806 unparsed DEFAULT_ARG. Parse the default args now. This function
28807 assumes that the current scope is the scope in which the default
28808 argument should be processed. */
28809
28810 static void
28811 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28812 {
28813 unsigned char saved_local_variables_forbidden_p;
28814 tree parm, parmdecl;
28815
28816 /* While we're parsing the default args, we might (due to the
28817 statement expression extension) encounter more classes. We want
28818 to handle them right away, but we don't want them getting mixed
28819 up with default args that are currently in the queue. */
28820 push_unparsed_function_queues (parser);
28821
28822 /* Local variable names (and the `this' keyword) may not appear
28823 in a default argument. */
28824 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28825 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
28826
28827 push_defarg_context (fn);
28828
28829 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28830 parmdecl = DECL_ARGUMENTS (fn);
28831 parm && parm != void_list_node;
28832 parm = TREE_CHAIN (parm),
28833 parmdecl = DECL_CHAIN (parmdecl))
28834 {
28835 tree default_arg = TREE_PURPOSE (parm);
28836 tree parsed_arg;
28837 vec<tree, va_gc> *insts;
28838 tree copy;
28839 unsigned ix;
28840
28841 if (!default_arg)
28842 continue;
28843
28844 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28845 /* This can happen for a friend declaration for a function
28846 already declared with default arguments. */
28847 continue;
28848
28849 parsed_arg
28850 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28851 default_arg,
28852 TREE_VALUE (parm));
28853 TREE_PURPOSE (parm) = parsed_arg;
28854
28855 /* Update any instantiations we've already created. */
28856 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28857 vec_safe_iterate (insts, ix, &copy); ix++)
28858 TREE_PURPOSE (copy) = parsed_arg;
28859 }
28860
28861 pop_defarg_context ();
28862
28863 /* Make sure no default arg is missing. */
28864 check_default_args (fn);
28865
28866 /* Restore the state of local_variables_forbidden_p. */
28867 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28868
28869 /* Restore the queue. */
28870 pop_unparsed_function_queues (parser);
28871 }
28872
28873 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28874
28875 sizeof ... ( identifier )
28876
28877 where the 'sizeof' token has already been consumed. */
28878
28879 static tree
28880 cp_parser_sizeof_pack (cp_parser *parser)
28881 {
28882 /* Consume the `...'. */
28883 cp_lexer_consume_token (parser->lexer);
28884 maybe_warn_variadic_templates ();
28885
28886 matching_parens parens;
28887 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28888 if (paren)
28889 parens.consume_open (parser);
28890 else
28891 permerror (cp_lexer_peek_token (parser->lexer)->location,
28892 "%<sizeof...%> argument must be surrounded by parentheses");
28893
28894 cp_token *token = cp_lexer_peek_token (parser->lexer);
28895 tree name = cp_parser_identifier (parser);
28896 if (name == error_mark_node)
28897 return error_mark_node;
28898 /* The name is not qualified. */
28899 parser->scope = NULL_TREE;
28900 parser->qualifying_scope = NULL_TREE;
28901 parser->object_scope = NULL_TREE;
28902 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28903 if (expr == error_mark_node)
28904 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28905 token->location);
28906 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28907 expr = TREE_TYPE (expr);
28908 else if (TREE_CODE (expr) == CONST_DECL)
28909 expr = DECL_INITIAL (expr);
28910 expr = make_pack_expansion (expr);
28911 PACK_EXPANSION_SIZEOF_P (expr) = true;
28912
28913 if (paren)
28914 parens.require_close (parser);
28915
28916 return expr;
28917 }
28918
28919 /* Parse the operand of `sizeof' (or a similar operator). Returns
28920 either a TYPE or an expression, depending on the form of the
28921 input. The KEYWORD indicates which kind of expression we have
28922 encountered. */
28923
28924 static tree
28925 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28926 {
28927 tree expr = NULL_TREE;
28928 const char *saved_message;
28929 char *tmp;
28930 bool saved_integral_constant_expression_p;
28931 bool saved_non_integral_constant_expression_p;
28932
28933 /* If it's a `...', then we are computing the length of a parameter
28934 pack. */
28935 if (keyword == RID_SIZEOF
28936 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28937 return cp_parser_sizeof_pack (parser);
28938
28939 /* Types cannot be defined in a `sizeof' expression. Save away the
28940 old message. */
28941 saved_message = parser->type_definition_forbidden_message;
28942 /* And create the new one. */
28943 tmp = concat ("types may not be defined in %<",
28944 IDENTIFIER_POINTER (ridpointers[keyword]),
28945 "%> expressions", NULL);
28946 parser->type_definition_forbidden_message = tmp;
28947
28948 /* The restrictions on constant-expressions do not apply inside
28949 sizeof expressions. */
28950 saved_integral_constant_expression_p
28951 = parser->integral_constant_expression_p;
28952 saved_non_integral_constant_expression_p
28953 = parser->non_integral_constant_expression_p;
28954 parser->integral_constant_expression_p = false;
28955
28956 /* Do not actually evaluate the expression. */
28957 ++cp_unevaluated_operand;
28958 ++c_inhibit_evaluation_warnings;
28959 /* If it's a `(', then we might be looking at the type-id
28960 construction. */
28961 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28962 {
28963 tree type = NULL_TREE;
28964
28965 /* We can't be sure yet whether we're looking at a type-id or an
28966 expression. */
28967 cp_parser_parse_tentatively (parser);
28968
28969 matching_parens parens;
28970 parens.consume_open (parser);
28971
28972 /* Note: as a GNU Extension, compound literals are considered
28973 postfix-expressions as they are in C99, so they are valid
28974 arguments to sizeof. See comment in cp_parser_cast_expression
28975 for details. */
28976 if (cp_parser_compound_literal_p (parser))
28977 cp_parser_simulate_error (parser);
28978 else
28979 {
28980 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28981 parser->in_type_id_in_expr_p = true;
28982 /* Look for the type-id. */
28983 type = cp_parser_type_id (parser);
28984 /* Look for the closing `)'. */
28985 parens.require_close (parser);
28986 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28987 }
28988
28989 /* If all went well, then we're done. */
28990 if (cp_parser_parse_definitely (parser))
28991 expr = type;
28992 }
28993
28994 /* If the type-id production did not work out, then we must be
28995 looking at the unary-expression production. */
28996 if (!expr)
28997 expr = cp_parser_unary_expression (parser);
28998
28999 /* Go back to evaluating expressions. */
29000 --cp_unevaluated_operand;
29001 --c_inhibit_evaluation_warnings;
29002
29003 /* Free the message we created. */
29004 free (tmp);
29005 /* And restore the old one. */
29006 parser->type_definition_forbidden_message = saved_message;
29007 parser->integral_constant_expression_p
29008 = saved_integral_constant_expression_p;
29009 parser->non_integral_constant_expression_p
29010 = saved_non_integral_constant_expression_p;
29011
29012 return expr;
29013 }
29014
29015 /* If the current declaration has no declarator, return true. */
29016
29017 static bool
29018 cp_parser_declares_only_class_p (cp_parser *parser)
29019 {
29020 /* If the next token is a `;' or a `,' then there is no
29021 declarator. */
29022 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29023 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
29024 }
29025
29026 /* Update the DECL_SPECS to reflect the storage class indicated by
29027 KEYWORD. */
29028
29029 static void
29030 cp_parser_set_storage_class (cp_parser *parser,
29031 cp_decl_specifier_seq *decl_specs,
29032 enum rid keyword,
29033 cp_token *token)
29034 {
29035 cp_storage_class storage_class;
29036
29037 if (parser->in_unbraced_linkage_specification_p)
29038 {
29039 error_at (token->location, "invalid use of %qD in linkage specification",
29040 ridpointers[keyword]);
29041 return;
29042 }
29043 else if (decl_specs->storage_class != sc_none)
29044 {
29045 decl_specs->conflicting_specifiers_p = true;
29046 return;
29047 }
29048
29049 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
29050 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
29051 && decl_specs->gnu_thread_keyword_p)
29052 {
29053 pedwarn (decl_specs->locations[ds_thread], 0,
29054 "%<__thread%> before %qD", ridpointers[keyword]);
29055 }
29056
29057 switch (keyword)
29058 {
29059 case RID_AUTO:
29060 storage_class = sc_auto;
29061 break;
29062 case RID_REGISTER:
29063 storage_class = sc_register;
29064 break;
29065 case RID_STATIC:
29066 storage_class = sc_static;
29067 break;
29068 case RID_EXTERN:
29069 storage_class = sc_extern;
29070 break;
29071 case RID_MUTABLE:
29072 storage_class = sc_mutable;
29073 break;
29074 default:
29075 gcc_unreachable ();
29076 }
29077 decl_specs->storage_class = storage_class;
29078 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
29079
29080 /* A storage class specifier cannot be applied alongside a typedef
29081 specifier. If there is a typedef specifier present then set
29082 conflicting_specifiers_p which will trigger an error later
29083 on in grokdeclarator. */
29084 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
29085 decl_specs->conflicting_specifiers_p = true;
29086 }
29087
29088 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
29089 is true, the type is a class or enum definition. */
29090
29091 static void
29092 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
29093 tree type_spec,
29094 cp_token *token,
29095 bool type_definition_p)
29096 {
29097 decl_specs->any_specifiers_p = true;
29098
29099 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
29100 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
29101 this is what happened. In system headers, we ignore these
29102 declarations so that G++ can work with system headers that are not
29103 C++-safe. */
29104 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
29105 && !type_definition_p
29106 && (type_spec == boolean_type_node
29107 || type_spec == char8_type_node
29108 || type_spec == char16_type_node
29109 || type_spec == char32_type_node
29110 || type_spec == wchar_type_node)
29111 && (decl_specs->type
29112 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
29113 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
29114 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
29115 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
29116 {
29117 decl_specs->redefined_builtin_type = type_spec;
29118 set_and_check_decl_spec_loc (decl_specs,
29119 ds_redefined_builtin_type_spec,
29120 token);
29121 if (!decl_specs->type)
29122 {
29123 decl_specs->type = type_spec;
29124 decl_specs->type_definition_p = false;
29125 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
29126 }
29127 }
29128 else if (decl_specs->type)
29129 decl_specs->multiple_types_p = true;
29130 else
29131 {
29132 decl_specs->type = type_spec;
29133 decl_specs->type_definition_p = type_definition_p;
29134 decl_specs->redefined_builtin_type = NULL_TREE;
29135 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
29136 }
29137 }
29138
29139 /* True iff TOKEN is the GNU keyword __thread. */
29140
29141 static bool
29142 token_is__thread (cp_token *token)
29143 {
29144 gcc_assert (token->keyword == RID_THREAD);
29145 return id_equal (token->u.value, "__thread");
29146 }
29147
29148 /* Set the location for a declarator specifier and check if it is
29149 duplicated.
29150
29151 DECL_SPECS is the sequence of declarator specifiers onto which to
29152 set the location.
29153
29154 DS is the single declarator specifier to set which location is to
29155 be set onto the existing sequence of declarators.
29156
29157 LOCATION is the location for the declarator specifier to
29158 consider. */
29159
29160 static void
29161 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
29162 cp_decl_spec ds, cp_token *token)
29163 {
29164 gcc_assert (ds < ds_last);
29165
29166 if (decl_specs == NULL)
29167 return;
29168
29169 location_t location = token->location;
29170
29171 if (decl_specs->locations[ds] == 0)
29172 {
29173 decl_specs->locations[ds] = location;
29174 if (ds == ds_thread)
29175 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
29176 }
29177 else
29178 {
29179 if (ds == ds_long)
29180 {
29181 if (decl_specs->locations[ds_long_long] != 0)
29182 error_at (location,
29183 "%<long long long%> is too long for GCC");
29184 else
29185 {
29186 decl_specs->locations[ds_long_long] = location;
29187 pedwarn_cxx98 (location,
29188 OPT_Wlong_long,
29189 "ISO C++ 1998 does not support %<long long%>");
29190 }
29191 }
29192 else if (ds == ds_thread)
29193 {
29194 bool gnu = token_is__thread (token);
29195 gcc_rich_location richloc (location);
29196 if (gnu != decl_specs->gnu_thread_keyword_p)
29197 {
29198 richloc.add_range (decl_specs->locations[ds_thread]);
29199 error_at (&richloc,
29200 "both %<__thread%> and %<thread_local%> specified");
29201 }
29202 else
29203 {
29204 richloc.add_fixit_remove ();
29205 error_at (&richloc, "duplicate %qD", token->u.value);
29206 }
29207 }
29208 else
29209 {
29210 static const char *const decl_spec_names[] = {
29211 "signed",
29212 "unsigned",
29213 "short",
29214 "long",
29215 "const",
29216 "volatile",
29217 "restrict",
29218 "inline",
29219 "virtual",
29220 "explicit",
29221 "friend",
29222 "typedef",
29223 "using",
29224 "constexpr",
29225 "__complex"
29226 };
29227 gcc_rich_location richloc (location);
29228 richloc.add_fixit_remove ();
29229 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
29230 }
29231 }
29232 }
29233
29234 /* Return true iff the declarator specifier DS is present in the
29235 sequence of declarator specifiers DECL_SPECS. */
29236
29237 bool
29238 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
29239 cp_decl_spec ds)
29240 {
29241 gcc_assert (ds < ds_last);
29242
29243 if (decl_specs == NULL)
29244 return false;
29245
29246 return decl_specs->locations[ds] != 0;
29247 }
29248
29249 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29250 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
29251
29252 static bool
29253 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
29254 {
29255 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
29256 }
29257
29258 /* Issue an error message indicating that TOKEN_DESC was expected.
29259 If KEYWORD is true, it indicated this function is called by
29260 cp_parser_require_keword and the required token can only be
29261 a indicated keyword.
29262
29263 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29264 within any error as the location of an "opening" token matching
29265 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29266 RT_CLOSE_PAREN). */
29267
29268 static void
29269 cp_parser_required_error (cp_parser *parser,
29270 required_token token_desc,
29271 bool keyword,
29272 location_t matching_location)
29273 {
29274 if (cp_parser_simulate_error (parser))
29275 return;
29276
29277 const char *gmsgid = NULL;
29278 switch (token_desc)
29279 {
29280 case RT_NEW:
29281 gmsgid = G_("expected %<new%>");
29282 break;
29283 case RT_DELETE:
29284 gmsgid = G_("expected %<delete%>");
29285 break;
29286 case RT_RETURN:
29287 gmsgid = G_("expected %<return%>");
29288 break;
29289 case RT_WHILE:
29290 gmsgid = G_("expected %<while%>");
29291 break;
29292 case RT_EXTERN:
29293 gmsgid = G_("expected %<extern%>");
29294 break;
29295 case RT_STATIC_ASSERT:
29296 gmsgid = G_("expected %<static_assert%>");
29297 break;
29298 case RT_DECLTYPE:
29299 gmsgid = G_("expected %<decltype%>");
29300 break;
29301 case RT_OPERATOR:
29302 gmsgid = G_("expected %<operator%>");
29303 break;
29304 case RT_CLASS:
29305 gmsgid = G_("expected %<class%>");
29306 break;
29307 case RT_TEMPLATE:
29308 gmsgid = G_("expected %<template%>");
29309 break;
29310 case RT_NAMESPACE:
29311 gmsgid = G_("expected %<namespace%>");
29312 break;
29313 case RT_USING:
29314 gmsgid = G_("expected %<using%>");
29315 break;
29316 case RT_ASM:
29317 gmsgid = G_("expected %<asm%>");
29318 break;
29319 case RT_TRY:
29320 gmsgid = G_("expected %<try%>");
29321 break;
29322 case RT_CATCH:
29323 gmsgid = G_("expected %<catch%>");
29324 break;
29325 case RT_THROW:
29326 gmsgid = G_("expected %<throw%>");
29327 break;
29328 case RT_LABEL:
29329 gmsgid = G_("expected %<__label__%>");
29330 break;
29331 case RT_AT_TRY:
29332 gmsgid = G_("expected %<@try%>");
29333 break;
29334 case RT_AT_SYNCHRONIZED:
29335 gmsgid = G_("expected %<@synchronized%>");
29336 break;
29337 case RT_AT_THROW:
29338 gmsgid = G_("expected %<@throw%>");
29339 break;
29340 case RT_TRANSACTION_ATOMIC:
29341 gmsgid = G_("expected %<__transaction_atomic%>");
29342 break;
29343 case RT_TRANSACTION_RELAXED:
29344 gmsgid = G_("expected %<__transaction_relaxed%>");
29345 break;
29346 default:
29347 break;
29348 }
29349
29350 if (!gmsgid && !keyword)
29351 {
29352 switch (token_desc)
29353 {
29354 case RT_SEMICOLON:
29355 gmsgid = G_("expected %<;%>");
29356 break;
29357 case RT_OPEN_PAREN:
29358 gmsgid = G_("expected %<(%>");
29359 break;
29360 case RT_CLOSE_BRACE:
29361 gmsgid = G_("expected %<}%>");
29362 break;
29363 case RT_OPEN_BRACE:
29364 gmsgid = G_("expected %<{%>");
29365 break;
29366 case RT_CLOSE_SQUARE:
29367 gmsgid = G_("expected %<]%>");
29368 break;
29369 case RT_OPEN_SQUARE:
29370 gmsgid = G_("expected %<[%>");
29371 break;
29372 case RT_COMMA:
29373 gmsgid = G_("expected %<,%>");
29374 break;
29375 case RT_SCOPE:
29376 gmsgid = G_("expected %<::%>");
29377 break;
29378 case RT_LESS:
29379 gmsgid = G_("expected %<<%>");
29380 break;
29381 case RT_GREATER:
29382 gmsgid = G_("expected %<>%>");
29383 break;
29384 case RT_EQ:
29385 gmsgid = G_("expected %<=%>");
29386 break;
29387 case RT_ELLIPSIS:
29388 gmsgid = G_("expected %<...%>");
29389 break;
29390 case RT_MULT:
29391 gmsgid = G_("expected %<*%>");
29392 break;
29393 case RT_COMPL:
29394 gmsgid = G_("expected %<~%>");
29395 break;
29396 case RT_COLON:
29397 gmsgid = G_("expected %<:%>");
29398 break;
29399 case RT_COLON_SCOPE:
29400 gmsgid = G_("expected %<:%> or %<::%>");
29401 break;
29402 case RT_CLOSE_PAREN:
29403 gmsgid = G_("expected %<)%>");
29404 break;
29405 case RT_COMMA_CLOSE_PAREN:
29406 gmsgid = G_("expected %<,%> or %<)%>");
29407 break;
29408 case RT_PRAGMA_EOL:
29409 gmsgid = G_("expected end of line");
29410 break;
29411 case RT_NAME:
29412 gmsgid = G_("expected identifier");
29413 break;
29414 case RT_SELECT:
29415 gmsgid = G_("expected selection-statement");
29416 break;
29417 case RT_ITERATION:
29418 gmsgid = G_("expected iteration-statement");
29419 break;
29420 case RT_JUMP:
29421 gmsgid = G_("expected jump-statement");
29422 break;
29423 case RT_CLASS_KEY:
29424 gmsgid = G_("expected class-key");
29425 break;
29426 case RT_CLASS_TYPENAME_TEMPLATE:
29427 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
29428 break;
29429 default:
29430 gcc_unreachable ();
29431 }
29432 }
29433
29434 if (gmsgid)
29435 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
29436 }
29437
29438
29439 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29440 issue an error message indicating that TOKEN_DESC was expected.
29441
29442 Returns the token consumed, if the token had the appropriate type.
29443 Otherwise, returns NULL.
29444
29445 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29446 within any error as the location of an "opening" token matching
29447 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29448 RT_CLOSE_PAREN). */
29449
29450 static cp_token *
29451 cp_parser_require (cp_parser* parser,
29452 enum cpp_ttype type,
29453 required_token token_desc,
29454 location_t matching_location)
29455 {
29456 if (cp_lexer_next_token_is (parser->lexer, type))
29457 return cp_lexer_consume_token (parser->lexer);
29458 else
29459 {
29460 /* Output the MESSAGE -- unless we're parsing tentatively. */
29461 if (!cp_parser_simulate_error (parser))
29462 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
29463 matching_location);
29464 return NULL;
29465 }
29466 }
29467
29468 /* An error message is produced if the next token is not '>'.
29469 All further tokens are skipped until the desired token is
29470 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29471
29472 static void
29473 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
29474 {
29475 /* Current level of '< ... >'. */
29476 unsigned level = 0;
29477 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29478 unsigned nesting_depth = 0;
29479
29480 /* Are we ready, yet? If not, issue error message. */
29481 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
29482 return;
29483
29484 /* Skip tokens until the desired token is found. */
29485 while (true)
29486 {
29487 /* Peek at the next token. */
29488 switch (cp_lexer_peek_token (parser->lexer)->type)
29489 {
29490 case CPP_LESS:
29491 if (!nesting_depth)
29492 ++level;
29493 break;
29494
29495 case CPP_RSHIFT:
29496 if (cxx_dialect == cxx98)
29497 /* C++0x views the `>>' operator as two `>' tokens, but
29498 C++98 does not. */
29499 break;
29500 else if (!nesting_depth && level-- == 0)
29501 {
29502 /* We've hit a `>>' where the first `>' closes the
29503 template argument list, and the second `>' is
29504 spurious. Just consume the `>>' and stop; we've
29505 already produced at least one error. */
29506 cp_lexer_consume_token (parser->lexer);
29507 return;
29508 }
29509 /* Fall through for C++0x, so we handle the second `>' in
29510 the `>>'. */
29511 gcc_fallthrough ();
29512
29513 case CPP_GREATER:
29514 if (!nesting_depth && level-- == 0)
29515 {
29516 /* We've reached the token we want, consume it and stop. */
29517 cp_lexer_consume_token (parser->lexer);
29518 return;
29519 }
29520 break;
29521
29522 case CPP_OPEN_PAREN:
29523 case CPP_OPEN_SQUARE:
29524 ++nesting_depth;
29525 break;
29526
29527 case CPP_CLOSE_PAREN:
29528 case CPP_CLOSE_SQUARE:
29529 if (nesting_depth-- == 0)
29530 return;
29531 break;
29532
29533 case CPP_EOF:
29534 case CPP_PRAGMA_EOL:
29535 case CPP_SEMICOLON:
29536 case CPP_OPEN_BRACE:
29537 case CPP_CLOSE_BRACE:
29538 /* The '>' was probably forgotten, don't look further. */
29539 return;
29540
29541 default:
29542 break;
29543 }
29544
29545 /* Consume this token. */
29546 cp_lexer_consume_token (parser->lexer);
29547 }
29548 }
29549
29550 /* If the next token is the indicated keyword, consume it. Otherwise,
29551 issue an error message indicating that TOKEN_DESC was expected.
29552
29553 Returns the token consumed, if the token had the appropriate type.
29554 Otherwise, returns NULL. */
29555
29556 static cp_token *
29557 cp_parser_require_keyword (cp_parser* parser,
29558 enum rid keyword,
29559 required_token token_desc)
29560 {
29561 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
29562
29563 if (token && token->keyword != keyword)
29564 {
29565 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
29566 UNKNOWN_LOCATION);
29567 return NULL;
29568 }
29569
29570 return token;
29571 }
29572
29573 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29574 function-definition. */
29575
29576 static bool
29577 cp_parser_token_starts_function_definition_p (cp_token* token)
29578 {
29579 return (/* An ordinary function-body begins with an `{'. */
29580 token->type == CPP_OPEN_BRACE
29581 /* A ctor-initializer begins with a `:'. */
29582 || token->type == CPP_COLON
29583 /* A function-try-block begins with `try'. */
29584 || token->keyword == RID_TRY
29585 /* A function-transaction-block begins with `__transaction_atomic'
29586 or `__transaction_relaxed'. */
29587 || token->keyword == RID_TRANSACTION_ATOMIC
29588 || token->keyword == RID_TRANSACTION_RELAXED
29589 /* The named return value extension begins with `return'. */
29590 || token->keyword == RID_RETURN);
29591 }
29592
29593 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29594 definition. */
29595
29596 static bool
29597 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
29598 {
29599 cp_token *token;
29600
29601 token = cp_lexer_peek_token (parser->lexer);
29602 return (token->type == CPP_OPEN_BRACE
29603 || (token->type == CPP_COLON
29604 && !parser->colon_doesnt_start_class_def_p));
29605 }
29606
29607 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29608 C++0x) ending a template-argument. */
29609
29610 static bool
29611 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
29612 {
29613 cp_token *token;
29614
29615 token = cp_lexer_peek_token (parser->lexer);
29616 return (token->type == CPP_COMMA
29617 || token->type == CPP_GREATER
29618 || token->type == CPP_ELLIPSIS
29619 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
29620 }
29621
29622 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29623 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29624
29625 static bool
29626 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
29627 size_t n)
29628 {
29629 cp_token *token;
29630
29631 token = cp_lexer_peek_nth_token (parser->lexer, n);
29632 if (token->type == CPP_LESS)
29633 return true;
29634 /* Check for the sequence `<::' in the original code. It would be lexed as
29635 `[:', where `[' is a digraph, and there is no whitespace before
29636 `:'. */
29637 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
29638 {
29639 cp_token *token2;
29640 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29641 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29642 return true;
29643 }
29644 return false;
29645 }
29646
29647 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29648 or none_type otherwise. */
29649
29650 static enum tag_types
29651 cp_parser_token_is_class_key (cp_token* token)
29652 {
29653 switch (token->keyword)
29654 {
29655 case RID_CLASS:
29656 return class_type;
29657 case RID_STRUCT:
29658 return record_type;
29659 case RID_UNION:
29660 return union_type;
29661
29662 default:
29663 return none_type;
29664 }
29665 }
29666
29667 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29668 or none_type otherwise or if the token is null. */
29669
29670 static enum tag_types
29671 cp_parser_token_is_type_parameter_key (cp_token* token)
29672 {
29673 if (!token)
29674 return none_type;
29675
29676 switch (token->keyword)
29677 {
29678 case RID_CLASS:
29679 return class_type;
29680 case RID_TYPENAME:
29681 return typename_type;
29682
29683 default:
29684 return none_type;
29685 }
29686 }
29687
29688 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29689
29690 static void
29691 cp_parser_check_class_key (enum tag_types class_key, tree type)
29692 {
29693 if (type == error_mark_node)
29694 return;
29695 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29696 {
29697 if (permerror (input_location, "%qs tag used in naming %q#T",
29698 class_key == union_type ? "union"
29699 : class_key == record_type ? "struct" : "class",
29700 type))
29701 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29702 "%q#T was previously declared here", type);
29703 }
29704 }
29705
29706 /* Issue an error message if DECL is redeclared with different
29707 access than its original declaration [class.access.spec/3].
29708 This applies to nested classes, nested class templates and
29709 enumerations [class.mem/1]. */
29710
29711 static void
29712 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29713 {
29714 if (!decl
29715 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29716 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29717 return;
29718
29719 if ((TREE_PRIVATE (decl)
29720 != (current_access_specifier == access_private_node))
29721 || (TREE_PROTECTED (decl)
29722 != (current_access_specifier == access_protected_node)))
29723 error_at (location, "%qD redeclared with different access", decl);
29724 }
29725
29726 /* Look for the `template' keyword, as a syntactic disambiguator.
29727 Return TRUE iff it is present, in which case it will be
29728 consumed. */
29729
29730 static bool
29731 cp_parser_optional_template_keyword (cp_parser *parser)
29732 {
29733 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29734 {
29735 /* In C++98 the `template' keyword can only be used within templates;
29736 outside templates the parser can always figure out what is a
29737 template and what is not. In C++11, per the resolution of DR 468,
29738 `template' is allowed in cases where it is not strictly necessary. */
29739 if (!processing_template_decl
29740 && pedantic && cxx_dialect == cxx98)
29741 {
29742 cp_token *token = cp_lexer_peek_token (parser->lexer);
29743 pedwarn (token->location, OPT_Wpedantic,
29744 "in C++98 %<template%> (as a disambiguator) is only "
29745 "allowed within templates");
29746 /* If this part of the token stream is rescanned, the same
29747 error message would be generated. So, we purge the token
29748 from the stream. */
29749 cp_lexer_purge_token (parser->lexer);
29750 return false;
29751 }
29752 else
29753 {
29754 /* Consume the `template' keyword. */
29755 cp_lexer_consume_token (parser->lexer);
29756 return true;
29757 }
29758 }
29759 return false;
29760 }
29761
29762 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29763 set PARSER->SCOPE, and perform other related actions. */
29764
29765 static void
29766 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29767 {
29768 struct tree_check *check_value;
29769
29770 /* Get the stored value. */
29771 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29772 /* Set the scope from the stored value. */
29773 parser->scope = saved_checks_value (check_value);
29774 parser->qualifying_scope = check_value->qualifying_scope;
29775 parser->object_scope = NULL_TREE;
29776 }
29777
29778 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29779 encounter the end of a block before what we were looking for. */
29780
29781 static bool
29782 cp_parser_cache_group (cp_parser *parser,
29783 enum cpp_ttype end,
29784 unsigned depth)
29785 {
29786 while (true)
29787 {
29788 cp_token *token = cp_lexer_peek_token (parser->lexer);
29789
29790 /* Abort a parenthesized expression if we encounter a semicolon. */
29791 if ((end == CPP_CLOSE_PAREN || depth == 0)
29792 && token->type == CPP_SEMICOLON)
29793 return true;
29794 /* If we've reached the end of the file, stop. */
29795 if (token->type == CPP_EOF
29796 || (end != CPP_PRAGMA_EOL
29797 && token->type == CPP_PRAGMA_EOL))
29798 return true;
29799 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29800 /* We've hit the end of an enclosing block, so there's been some
29801 kind of syntax error. */
29802 return true;
29803
29804 /* Consume the token. */
29805 cp_lexer_consume_token (parser->lexer);
29806 /* See if it starts a new group. */
29807 if (token->type == CPP_OPEN_BRACE)
29808 {
29809 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29810 /* In theory this should probably check end == '}', but
29811 cp_parser_save_member_function_body needs it to exit
29812 after either '}' or ')' when called with ')'. */
29813 if (depth == 0)
29814 return false;
29815 }
29816 else if (token->type == CPP_OPEN_PAREN)
29817 {
29818 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29819 if (depth == 0 && end == CPP_CLOSE_PAREN)
29820 return false;
29821 }
29822 else if (token->type == CPP_PRAGMA)
29823 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29824 else if (token->type == end)
29825 return false;
29826 }
29827 }
29828
29829 /* Like above, for caching a default argument or NSDMI. Both of these are
29830 terminated by a non-nested comma, but it can be unclear whether or not a
29831 comma is nested in a template argument list unless we do more parsing.
29832 In order to handle this ambiguity, when we encounter a ',' after a '<'
29833 we try to parse what follows as a parameter-declaration-list (in the
29834 case of a default argument) or a member-declarator (in the case of an
29835 NSDMI). If that succeeds, then we stop caching. */
29836
29837 static tree
29838 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29839 {
29840 unsigned depth = 0;
29841 int maybe_template_id = 0;
29842 cp_token *first_token;
29843 cp_token *token;
29844 tree default_argument;
29845
29846 /* Add tokens until we have processed the entire default
29847 argument. We add the range [first_token, token). */
29848 first_token = cp_lexer_peek_token (parser->lexer);
29849 if (first_token->type == CPP_OPEN_BRACE)
29850 {
29851 /* For list-initialization, this is straightforward. */
29852 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29853 token = cp_lexer_peek_token (parser->lexer);
29854 }
29855 else while (true)
29856 {
29857 bool done = false;
29858
29859 /* Peek at the next token. */
29860 token = cp_lexer_peek_token (parser->lexer);
29861 /* What we do depends on what token we have. */
29862 switch (token->type)
29863 {
29864 /* In valid code, a default argument must be
29865 immediately followed by a `,' `)', or `...'. */
29866 case CPP_COMMA:
29867 if (depth == 0 && maybe_template_id)
29868 {
29869 /* If we've seen a '<', we might be in a
29870 template-argument-list. Until Core issue 325 is
29871 resolved, we don't know how this situation ought
29872 to be handled, so try to DTRT. We check whether
29873 what comes after the comma is a valid parameter
29874 declaration list. If it is, then the comma ends
29875 the default argument; otherwise the default
29876 argument continues. */
29877 bool error = false;
29878 cp_token *peek;
29879
29880 /* Set ITALP so cp_parser_parameter_declaration_list
29881 doesn't decide to commit to this parse. */
29882 bool saved_italp = parser->in_template_argument_list_p;
29883 parser->in_template_argument_list_p = true;
29884
29885 cp_parser_parse_tentatively (parser);
29886
29887 if (nsdmi)
29888 {
29889 /* Parse declarators until we reach a non-comma or
29890 somthing that cannot be an initializer.
29891 Just checking whether we're looking at a single
29892 declarator is insufficient. Consider:
29893 int var = tuple<T,U>::x;
29894 The template parameter 'U' looks exactly like a
29895 declarator. */
29896 do
29897 {
29898 int ctor_dtor_or_conv_p;
29899 cp_lexer_consume_token (parser->lexer);
29900 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29901 CP_PARSER_FLAGS_NONE,
29902 &ctor_dtor_or_conv_p,
29903 /*parenthesized_p=*/NULL,
29904 /*member_p=*/true,
29905 /*friend_p=*/false,
29906 /*static_p=*/false);
29907 peek = cp_lexer_peek_token (parser->lexer);
29908 if (cp_parser_error_occurred (parser))
29909 break;
29910 }
29911 while (peek->type == CPP_COMMA);
29912 /* If we met an '=' or ';' then the original comma
29913 was the end of the NSDMI. Otherwise assume
29914 we're still in the NSDMI. */
29915 error = (peek->type != CPP_EQ
29916 && peek->type != CPP_SEMICOLON);
29917 }
29918 else
29919 {
29920 cp_lexer_consume_token (parser->lexer);
29921 begin_scope (sk_function_parms, NULL_TREE);
29922 tree t = cp_parser_parameter_declaration_list
29923 (parser, CP_PARSER_FLAGS_NONE);
29924 if (t == error_mark_node)
29925 error = true;
29926 pop_bindings_and_leave_scope ();
29927 }
29928 if (!cp_parser_error_occurred (parser) && !error)
29929 done = true;
29930 cp_parser_abort_tentative_parse (parser);
29931
29932 parser->in_template_argument_list_p = saved_italp;
29933 break;
29934 }
29935 /* FALLTHRU */
29936 case CPP_CLOSE_PAREN:
29937 case CPP_ELLIPSIS:
29938 /* If we run into a non-nested `;', `}', or `]',
29939 then the code is invalid -- but the default
29940 argument is certainly over. */
29941 case CPP_SEMICOLON:
29942 case CPP_CLOSE_BRACE:
29943 case CPP_CLOSE_SQUARE:
29944 if (depth == 0
29945 /* Handle correctly int n = sizeof ... ( p ); */
29946 && token->type != CPP_ELLIPSIS)
29947 done = true;
29948 /* Update DEPTH, if necessary. */
29949 else if (token->type == CPP_CLOSE_PAREN
29950 || token->type == CPP_CLOSE_BRACE
29951 || token->type == CPP_CLOSE_SQUARE)
29952 --depth;
29953 break;
29954
29955 case CPP_OPEN_PAREN:
29956 case CPP_OPEN_SQUARE:
29957 case CPP_OPEN_BRACE:
29958 ++depth;
29959 break;
29960
29961 case CPP_LESS:
29962 if (depth == 0)
29963 /* This might be the comparison operator, or it might
29964 start a template argument list. */
29965 ++maybe_template_id;
29966 break;
29967
29968 case CPP_RSHIFT:
29969 if (cxx_dialect == cxx98)
29970 break;
29971 /* Fall through for C++0x, which treats the `>>'
29972 operator like two `>' tokens in certain
29973 cases. */
29974 gcc_fallthrough ();
29975
29976 case CPP_GREATER:
29977 if (depth == 0)
29978 {
29979 /* This might be an operator, or it might close a
29980 template argument list. But if a previous '<'
29981 started a template argument list, this will have
29982 closed it, so we can't be in one anymore. */
29983 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29984 if (maybe_template_id < 0)
29985 maybe_template_id = 0;
29986 }
29987 break;
29988
29989 /* If we run out of tokens, issue an error message. */
29990 case CPP_EOF:
29991 case CPP_PRAGMA_EOL:
29992 error_at (token->location, "file ends in default argument");
29993 return error_mark_node;
29994
29995 case CPP_NAME:
29996 case CPP_SCOPE:
29997 /* In these cases, we should look for template-ids.
29998 For example, if the default argument is
29999 `X<int, double>()', we need to do name lookup to
30000 figure out whether or not `X' is a template; if
30001 so, the `,' does not end the default argument.
30002
30003 That is not yet done. */
30004 break;
30005
30006 default:
30007 break;
30008 }
30009
30010 /* If we've reached the end, stop. */
30011 if (done)
30012 break;
30013
30014 /* Add the token to the token block. */
30015 token = cp_lexer_consume_token (parser->lexer);
30016 }
30017
30018 /* Create a DEFAULT_ARG to represent the unparsed default
30019 argument. */
30020 default_argument = make_node (DEFAULT_ARG);
30021 DEFARG_TOKENS (default_argument)
30022 = cp_token_cache_new (first_token, token);
30023 DEFARG_INSTANTIATIONS (default_argument) = NULL;
30024
30025 return default_argument;
30026 }
30027
30028 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
30029
30030 location_t
30031 defarg_location (tree default_argument)
30032 {
30033 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
30034 location_t start = tokens->first->location;
30035 location_t end = tokens->last->location;
30036 return make_location (start, start, end);
30037 }
30038
30039 /* Begin parsing tentatively. We always save tokens while parsing
30040 tentatively so that if the tentative parsing fails we can restore the
30041 tokens. */
30042
30043 static void
30044 cp_parser_parse_tentatively (cp_parser* parser)
30045 {
30046 /* Enter a new parsing context. */
30047 parser->context = cp_parser_context_new (parser->context);
30048 /* Begin saving tokens. */
30049 cp_lexer_save_tokens (parser->lexer);
30050 /* In order to avoid repetitive access control error messages,
30051 access checks are queued up until we are no longer parsing
30052 tentatively. */
30053 push_deferring_access_checks (dk_deferred);
30054 }
30055
30056 /* Commit to the currently active tentative parse. */
30057
30058 static void
30059 cp_parser_commit_to_tentative_parse (cp_parser* parser)
30060 {
30061 cp_parser_context *context;
30062 cp_lexer *lexer;
30063
30064 /* Mark all of the levels as committed. */
30065 lexer = parser->lexer;
30066 for (context = parser->context; context->next; context = context->next)
30067 {
30068 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
30069 break;
30070 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
30071 while (!cp_lexer_saving_tokens (lexer))
30072 lexer = lexer->next;
30073 cp_lexer_commit_tokens (lexer);
30074 }
30075 }
30076
30077 /* Commit to the topmost currently active tentative parse.
30078
30079 Note that this function shouldn't be called when there are
30080 irreversible side-effects while in a tentative state. For
30081 example, we shouldn't create a permanent entry in the symbol
30082 table, or issue an error message that might not apply if the
30083 tentative parse is aborted. */
30084
30085 static void
30086 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
30087 {
30088 cp_parser_context *context = parser->context;
30089 cp_lexer *lexer = parser->lexer;
30090
30091 if (context)
30092 {
30093 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
30094 return;
30095 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
30096
30097 while (!cp_lexer_saving_tokens (lexer))
30098 lexer = lexer->next;
30099 cp_lexer_commit_tokens (lexer);
30100 }
30101 }
30102
30103 /* Abort the currently active tentative parse. All consumed tokens
30104 will be rolled back, and no diagnostics will be issued. */
30105
30106 static void
30107 cp_parser_abort_tentative_parse (cp_parser* parser)
30108 {
30109 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
30110 || errorcount > 0);
30111 cp_parser_simulate_error (parser);
30112 /* Now, pretend that we want to see if the construct was
30113 successfully parsed. */
30114 cp_parser_parse_definitely (parser);
30115 }
30116
30117 /* Stop parsing tentatively. If a parse error has occurred, restore the
30118 token stream. Otherwise, commit to the tokens we have consumed.
30119 Returns true if no error occurred; false otherwise. */
30120
30121 static bool
30122 cp_parser_parse_definitely (cp_parser* parser)
30123 {
30124 bool error_occurred;
30125 cp_parser_context *context;
30126
30127 /* Remember whether or not an error occurred, since we are about to
30128 destroy that information. */
30129 error_occurred = cp_parser_error_occurred (parser);
30130 /* Remove the topmost context from the stack. */
30131 context = parser->context;
30132 parser->context = context->next;
30133 /* If no parse errors occurred, commit to the tentative parse. */
30134 if (!error_occurred)
30135 {
30136 /* Commit to the tokens read tentatively, unless that was
30137 already done. */
30138 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
30139 cp_lexer_commit_tokens (parser->lexer);
30140
30141 pop_to_parent_deferring_access_checks ();
30142 }
30143 /* Otherwise, if errors occurred, roll back our state so that things
30144 are just as they were before we began the tentative parse. */
30145 else
30146 {
30147 cp_lexer_rollback_tokens (parser->lexer);
30148 pop_deferring_access_checks ();
30149 }
30150 /* Add the context to the front of the free list. */
30151 context->next = cp_parser_context_free_list;
30152 cp_parser_context_free_list = context;
30153
30154 return !error_occurred;
30155 }
30156
30157 /* Returns true if we are parsing tentatively and are not committed to
30158 this tentative parse. */
30159
30160 static bool
30161 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
30162 {
30163 return (cp_parser_parsing_tentatively (parser)
30164 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
30165 }
30166
30167 /* Returns nonzero iff an error has occurred during the most recent
30168 tentative parse. */
30169
30170 static bool
30171 cp_parser_error_occurred (cp_parser* parser)
30172 {
30173 return (cp_parser_parsing_tentatively (parser)
30174 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
30175 }
30176
30177 /* Returns nonzero if GNU extensions are allowed. */
30178
30179 static bool
30180 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
30181 {
30182 return parser->allow_gnu_extensions_p;
30183 }
30184 \f
30185 /* Objective-C++ Productions */
30186
30187
30188 /* Parse an Objective-C expression, which feeds into a primary-expression
30189 above.
30190
30191 objc-expression:
30192 objc-message-expression
30193 objc-string-literal
30194 objc-encode-expression
30195 objc-protocol-expression
30196 objc-selector-expression
30197
30198 Returns a tree representation of the expression. */
30199
30200 static cp_expr
30201 cp_parser_objc_expression (cp_parser* parser)
30202 {
30203 /* Try to figure out what kind of declaration is present. */
30204 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30205
30206 switch (kwd->type)
30207 {
30208 case CPP_OPEN_SQUARE:
30209 return cp_parser_objc_message_expression (parser);
30210
30211 case CPP_OBJC_STRING:
30212 kwd = cp_lexer_consume_token (parser->lexer);
30213 return objc_build_string_object (kwd->u.value);
30214
30215 case CPP_KEYWORD:
30216 switch (kwd->keyword)
30217 {
30218 case RID_AT_ENCODE:
30219 return cp_parser_objc_encode_expression (parser);
30220
30221 case RID_AT_PROTOCOL:
30222 return cp_parser_objc_protocol_expression (parser);
30223
30224 case RID_AT_SELECTOR:
30225 return cp_parser_objc_selector_expression (parser);
30226
30227 default:
30228 break;
30229 }
30230 /* FALLTHRU */
30231 default:
30232 error_at (kwd->location,
30233 "misplaced %<@%D%> Objective-C++ construct",
30234 kwd->u.value);
30235 cp_parser_skip_to_end_of_block_or_statement (parser);
30236 }
30237
30238 return error_mark_node;
30239 }
30240
30241 /* Parse an Objective-C message expression.
30242
30243 objc-message-expression:
30244 [ objc-message-receiver objc-message-args ]
30245
30246 Returns a representation of an Objective-C message. */
30247
30248 static tree
30249 cp_parser_objc_message_expression (cp_parser* parser)
30250 {
30251 tree receiver, messageargs;
30252
30253 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30254 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
30255 receiver = cp_parser_objc_message_receiver (parser);
30256 messageargs = cp_parser_objc_message_args (parser);
30257 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
30258 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30259
30260 tree result = objc_build_message_expr (receiver, messageargs);
30261
30262 /* Construct a location e.g.
30263 [self func1:5]
30264 ^~~~~~~~~~~~~~
30265 ranging from the '[' to the ']', with the caret at the start. */
30266 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
30267 protected_set_expr_location (result, combined_loc);
30268
30269 return result;
30270 }
30271
30272 /* Parse an objc-message-receiver.
30273
30274 objc-message-receiver:
30275 expression
30276 simple-type-specifier
30277
30278 Returns a representation of the type or expression. */
30279
30280 static tree
30281 cp_parser_objc_message_receiver (cp_parser* parser)
30282 {
30283 tree rcv;
30284
30285 /* An Objective-C message receiver may be either (1) a type
30286 or (2) an expression. */
30287 cp_parser_parse_tentatively (parser);
30288 rcv = cp_parser_expression (parser);
30289
30290 /* If that worked out, fine. */
30291 if (cp_parser_parse_definitely (parser))
30292 return rcv;
30293
30294 cp_parser_parse_tentatively (parser);
30295 rcv = cp_parser_simple_type_specifier (parser,
30296 /*decl_specs=*/NULL,
30297 CP_PARSER_FLAGS_NONE);
30298
30299 if (cp_parser_parse_definitely (parser))
30300 return objc_get_class_reference (rcv);
30301
30302 cp_parser_error (parser, "objective-c++ message receiver expected");
30303 return error_mark_node;
30304 }
30305
30306 /* Parse the arguments and selectors comprising an Objective-C message.
30307
30308 objc-message-args:
30309 objc-selector
30310 objc-selector-args
30311 objc-selector-args , objc-comma-args
30312
30313 objc-selector-args:
30314 objc-selector [opt] : assignment-expression
30315 objc-selector-args objc-selector [opt] : assignment-expression
30316
30317 objc-comma-args:
30318 assignment-expression
30319 objc-comma-args , assignment-expression
30320
30321 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30322 selector arguments and TREE_VALUE containing a list of comma
30323 arguments. */
30324
30325 static tree
30326 cp_parser_objc_message_args (cp_parser* parser)
30327 {
30328 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
30329 bool maybe_unary_selector_p = true;
30330 cp_token *token = cp_lexer_peek_token (parser->lexer);
30331
30332 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30333 {
30334 tree selector = NULL_TREE, arg;
30335
30336 if (token->type != CPP_COLON)
30337 selector = cp_parser_objc_selector (parser);
30338
30339 /* Detect if we have a unary selector. */
30340 if (maybe_unary_selector_p
30341 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30342 return build_tree_list (selector, NULL_TREE);
30343
30344 maybe_unary_selector_p = false;
30345 cp_parser_require (parser, CPP_COLON, RT_COLON);
30346 arg = cp_parser_assignment_expression (parser);
30347
30348 sel_args
30349 = chainon (sel_args,
30350 build_tree_list (selector, arg));
30351
30352 token = cp_lexer_peek_token (parser->lexer);
30353 }
30354
30355 /* Handle non-selector arguments, if any. */
30356 while (token->type == CPP_COMMA)
30357 {
30358 tree arg;
30359
30360 cp_lexer_consume_token (parser->lexer);
30361 arg = cp_parser_assignment_expression (parser);
30362
30363 addl_args
30364 = chainon (addl_args,
30365 build_tree_list (NULL_TREE, arg));
30366
30367 token = cp_lexer_peek_token (parser->lexer);
30368 }
30369
30370 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
30371 {
30372 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
30373 return build_tree_list (error_mark_node, error_mark_node);
30374 }
30375
30376 return build_tree_list (sel_args, addl_args);
30377 }
30378
30379 /* Parse an Objective-C encode expression.
30380
30381 objc-encode-expression:
30382 @encode objc-typename
30383
30384 Returns an encoded representation of the type argument. */
30385
30386 static cp_expr
30387 cp_parser_objc_encode_expression (cp_parser* parser)
30388 {
30389 tree type;
30390 cp_token *token;
30391 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30392
30393 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
30394 matching_parens parens;
30395 parens.require_open (parser);
30396 token = cp_lexer_peek_token (parser->lexer);
30397 type = complete_type (cp_parser_type_id (parser));
30398 parens.require_close (parser);
30399
30400 if (!type)
30401 {
30402 error_at (token->location,
30403 "%<@encode%> must specify a type as an argument");
30404 return error_mark_node;
30405 }
30406
30407 /* This happens if we find @encode(T) (where T is a template
30408 typename or something dependent on a template typename) when
30409 parsing a template. In that case, we can't compile it
30410 immediately, but we rather create an AT_ENCODE_EXPR which will
30411 need to be instantiated when the template is used.
30412 */
30413 if (dependent_type_p (type))
30414 {
30415 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
30416 TREE_READONLY (value) = 1;
30417 return value;
30418 }
30419
30420
30421 /* Build a location of the form:
30422 @encode(int)
30423 ^~~~~~~~~~~~
30424 with caret==start at the @ token, finishing at the close paren. */
30425 location_t combined_loc
30426 = make_location (start_loc, start_loc,
30427 cp_lexer_previous_token (parser->lexer)->location);
30428
30429 return cp_expr (objc_build_encode_expr (type), combined_loc);
30430 }
30431
30432 /* Parse an Objective-C @defs expression. */
30433
30434 static tree
30435 cp_parser_objc_defs_expression (cp_parser *parser)
30436 {
30437 tree name;
30438
30439 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
30440 matching_parens parens;
30441 parens.require_open (parser);
30442 name = cp_parser_identifier (parser);
30443 parens.require_close (parser);
30444
30445 return objc_get_class_ivars (name);
30446 }
30447
30448 /* Parse an Objective-C protocol expression.
30449
30450 objc-protocol-expression:
30451 @protocol ( identifier )
30452
30453 Returns a representation of the protocol expression. */
30454
30455 static tree
30456 cp_parser_objc_protocol_expression (cp_parser* parser)
30457 {
30458 tree proto;
30459 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30460
30461 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30462 matching_parens parens;
30463 parens.require_open (parser);
30464 proto = cp_parser_identifier (parser);
30465 parens.require_close (parser);
30466
30467 /* Build a location of the form:
30468 @protocol(prot)
30469 ^~~~~~~~~~~~~~~
30470 with caret==start at the @ token, finishing at the close paren. */
30471 location_t combined_loc
30472 = make_location (start_loc, start_loc,
30473 cp_lexer_previous_token (parser->lexer)->location);
30474 tree result = objc_build_protocol_expr (proto);
30475 protected_set_expr_location (result, combined_loc);
30476 return result;
30477 }
30478
30479 /* Parse an Objective-C selector expression.
30480
30481 objc-selector-expression:
30482 @selector ( objc-method-signature )
30483
30484 objc-method-signature:
30485 objc-selector
30486 objc-selector-seq
30487
30488 objc-selector-seq:
30489 objc-selector :
30490 objc-selector-seq objc-selector :
30491
30492 Returns a representation of the method selector. */
30493
30494 static tree
30495 cp_parser_objc_selector_expression (cp_parser* parser)
30496 {
30497 tree sel_seq = NULL_TREE;
30498 bool maybe_unary_selector_p = true;
30499 cp_token *token;
30500 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30501
30502 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
30503 matching_parens parens;
30504 parens.require_open (parser);
30505 token = cp_lexer_peek_token (parser->lexer);
30506
30507 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
30508 || token->type == CPP_SCOPE)
30509 {
30510 tree selector = NULL_TREE;
30511
30512 if (token->type != CPP_COLON
30513 || token->type == CPP_SCOPE)
30514 selector = cp_parser_objc_selector (parser);
30515
30516 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
30517 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
30518 {
30519 /* Detect if we have a unary selector. */
30520 if (maybe_unary_selector_p)
30521 {
30522 sel_seq = selector;
30523 goto finish_selector;
30524 }
30525 else
30526 {
30527 cp_parser_error (parser, "expected %<:%>");
30528 }
30529 }
30530 maybe_unary_selector_p = false;
30531 token = cp_lexer_consume_token (parser->lexer);
30532
30533 if (token->type == CPP_SCOPE)
30534 {
30535 sel_seq
30536 = chainon (sel_seq,
30537 build_tree_list (selector, NULL_TREE));
30538 sel_seq
30539 = chainon (sel_seq,
30540 build_tree_list (NULL_TREE, NULL_TREE));
30541 }
30542 else
30543 sel_seq
30544 = chainon (sel_seq,
30545 build_tree_list (selector, NULL_TREE));
30546
30547 token = cp_lexer_peek_token (parser->lexer);
30548 }
30549
30550 finish_selector:
30551 parens.require_close (parser);
30552
30553
30554 /* Build a location of the form:
30555 @selector(func)
30556 ^~~~~~~~~~~~~~~
30557 with caret==start at the @ token, finishing at the close paren. */
30558 location_t combined_loc
30559 = make_location (loc, loc,
30560 cp_lexer_previous_token (parser->lexer)->location);
30561 tree result = objc_build_selector_expr (combined_loc, sel_seq);
30562 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30563 protected_set_expr_location (result, combined_loc);
30564 return result;
30565 }
30566
30567 /* Parse a list of identifiers.
30568
30569 objc-identifier-list:
30570 identifier
30571 objc-identifier-list , identifier
30572
30573 Returns a TREE_LIST of identifier nodes. */
30574
30575 static tree
30576 cp_parser_objc_identifier_list (cp_parser* parser)
30577 {
30578 tree identifier;
30579 tree list;
30580 cp_token *sep;
30581
30582 identifier = cp_parser_identifier (parser);
30583 if (identifier == error_mark_node)
30584 return error_mark_node;
30585
30586 list = build_tree_list (NULL_TREE, identifier);
30587 sep = cp_lexer_peek_token (parser->lexer);
30588
30589 while (sep->type == CPP_COMMA)
30590 {
30591 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30592 identifier = cp_parser_identifier (parser);
30593 if (identifier == error_mark_node)
30594 return list;
30595
30596 list = chainon (list, build_tree_list (NULL_TREE,
30597 identifier));
30598 sep = cp_lexer_peek_token (parser->lexer);
30599 }
30600
30601 return list;
30602 }
30603
30604 /* Parse an Objective-C alias declaration.
30605
30606 objc-alias-declaration:
30607 @compatibility_alias identifier identifier ;
30608
30609 This function registers the alias mapping with the Objective-C front end.
30610 It returns nothing. */
30611
30612 static void
30613 cp_parser_objc_alias_declaration (cp_parser* parser)
30614 {
30615 tree alias, orig;
30616
30617 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
30618 alias = cp_parser_identifier (parser);
30619 orig = cp_parser_identifier (parser);
30620 objc_declare_alias (alias, orig);
30621 cp_parser_consume_semicolon_at_end_of_statement (parser);
30622 }
30623
30624 /* Parse an Objective-C class forward-declaration.
30625
30626 objc-class-declaration:
30627 @class objc-identifier-list ;
30628
30629 The function registers the forward declarations with the Objective-C
30630 front end. It returns nothing. */
30631
30632 static void
30633 cp_parser_objc_class_declaration (cp_parser* parser)
30634 {
30635 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
30636 while (true)
30637 {
30638 tree id;
30639
30640 id = cp_parser_identifier (parser);
30641 if (id == error_mark_node)
30642 break;
30643
30644 objc_declare_class (id);
30645
30646 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30647 cp_lexer_consume_token (parser->lexer);
30648 else
30649 break;
30650 }
30651 cp_parser_consume_semicolon_at_end_of_statement (parser);
30652 }
30653
30654 /* Parse a list of Objective-C protocol references.
30655
30656 objc-protocol-refs-opt:
30657 objc-protocol-refs [opt]
30658
30659 objc-protocol-refs:
30660 < objc-identifier-list >
30661
30662 Returns a TREE_LIST of identifiers, if any. */
30663
30664 static tree
30665 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30666 {
30667 tree protorefs = NULL_TREE;
30668
30669 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30670 {
30671 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30672 protorefs = cp_parser_objc_identifier_list (parser);
30673 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30674 }
30675
30676 return protorefs;
30677 }
30678
30679 /* Parse a Objective-C visibility specification. */
30680
30681 static void
30682 cp_parser_objc_visibility_spec (cp_parser* parser)
30683 {
30684 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30685
30686 switch (vis->keyword)
30687 {
30688 case RID_AT_PRIVATE:
30689 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30690 break;
30691 case RID_AT_PROTECTED:
30692 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30693 break;
30694 case RID_AT_PUBLIC:
30695 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30696 break;
30697 case RID_AT_PACKAGE:
30698 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30699 break;
30700 default:
30701 return;
30702 }
30703
30704 /* Eat '@private'/'@protected'/'@public'. */
30705 cp_lexer_consume_token (parser->lexer);
30706 }
30707
30708 /* Parse an Objective-C method type. Return 'true' if it is a class
30709 (+) method, and 'false' if it is an instance (-) method. */
30710
30711 static inline bool
30712 cp_parser_objc_method_type (cp_parser* parser)
30713 {
30714 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30715 return true;
30716 else
30717 return false;
30718 }
30719
30720 /* Parse an Objective-C protocol qualifier. */
30721
30722 static tree
30723 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30724 {
30725 tree quals = NULL_TREE, node;
30726 cp_token *token = cp_lexer_peek_token (parser->lexer);
30727
30728 node = token->u.value;
30729
30730 while (node && identifier_p (node)
30731 && (node == ridpointers [(int) RID_IN]
30732 || node == ridpointers [(int) RID_OUT]
30733 || node == ridpointers [(int) RID_INOUT]
30734 || node == ridpointers [(int) RID_BYCOPY]
30735 || node == ridpointers [(int) RID_BYREF]
30736 || node == ridpointers [(int) RID_ONEWAY]))
30737 {
30738 quals = tree_cons (NULL_TREE, node, quals);
30739 cp_lexer_consume_token (parser->lexer);
30740 token = cp_lexer_peek_token (parser->lexer);
30741 node = token->u.value;
30742 }
30743
30744 return quals;
30745 }
30746
30747 /* Parse an Objective-C typename. */
30748
30749 static tree
30750 cp_parser_objc_typename (cp_parser* parser)
30751 {
30752 tree type_name = NULL_TREE;
30753
30754 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30755 {
30756 tree proto_quals, cp_type = NULL_TREE;
30757
30758 matching_parens parens;
30759 parens.consume_open (parser); /* Eat '('. */
30760 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30761
30762 /* An ObjC type name may consist of just protocol qualifiers, in which
30763 case the type shall default to 'id'. */
30764 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30765 {
30766 cp_type = cp_parser_type_id (parser);
30767
30768 /* If the type could not be parsed, an error has already
30769 been produced. For error recovery, behave as if it had
30770 not been specified, which will use the default type
30771 'id'. */
30772 if (cp_type == error_mark_node)
30773 {
30774 cp_type = NULL_TREE;
30775 /* We need to skip to the closing parenthesis as
30776 cp_parser_type_id() does not seem to do it for
30777 us. */
30778 cp_parser_skip_to_closing_parenthesis (parser,
30779 /*recovering=*/true,
30780 /*or_comma=*/false,
30781 /*consume_paren=*/false);
30782 }
30783 }
30784
30785 parens.require_close (parser);
30786 type_name = build_tree_list (proto_quals, cp_type);
30787 }
30788
30789 return type_name;
30790 }
30791
30792 /* Check to see if TYPE refers to an Objective-C selector name. */
30793
30794 static bool
30795 cp_parser_objc_selector_p (enum cpp_ttype type)
30796 {
30797 return (type == CPP_NAME || type == CPP_KEYWORD
30798 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30799 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30800 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30801 || type == CPP_XOR || type == CPP_XOR_EQ);
30802 }
30803
30804 /* Parse an Objective-C selector. */
30805
30806 static tree
30807 cp_parser_objc_selector (cp_parser* parser)
30808 {
30809 cp_token *token = cp_lexer_consume_token (parser->lexer);
30810
30811 if (!cp_parser_objc_selector_p (token->type))
30812 {
30813 error_at (token->location, "invalid Objective-C++ selector name");
30814 return error_mark_node;
30815 }
30816
30817 /* C++ operator names are allowed to appear in ObjC selectors. */
30818 switch (token->type)
30819 {
30820 case CPP_AND_AND: return get_identifier ("and");
30821 case CPP_AND_EQ: return get_identifier ("and_eq");
30822 case CPP_AND: return get_identifier ("bitand");
30823 case CPP_OR: return get_identifier ("bitor");
30824 case CPP_COMPL: return get_identifier ("compl");
30825 case CPP_NOT: return get_identifier ("not");
30826 case CPP_NOT_EQ: return get_identifier ("not_eq");
30827 case CPP_OR_OR: return get_identifier ("or");
30828 case CPP_OR_EQ: return get_identifier ("or_eq");
30829 case CPP_XOR: return get_identifier ("xor");
30830 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30831 default: return token->u.value;
30832 }
30833 }
30834
30835 /* Parse an Objective-C params list. */
30836
30837 static tree
30838 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30839 {
30840 tree params = NULL_TREE;
30841 bool maybe_unary_selector_p = true;
30842 cp_token *token = cp_lexer_peek_token (parser->lexer);
30843
30844 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30845 {
30846 tree selector = NULL_TREE, type_name, identifier;
30847 tree parm_attr = NULL_TREE;
30848
30849 if (token->keyword == RID_ATTRIBUTE)
30850 break;
30851
30852 if (token->type != CPP_COLON)
30853 selector = cp_parser_objc_selector (parser);
30854
30855 /* Detect if we have a unary selector. */
30856 if (maybe_unary_selector_p
30857 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30858 {
30859 params = selector; /* Might be followed by attributes. */
30860 break;
30861 }
30862
30863 maybe_unary_selector_p = false;
30864 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30865 {
30866 /* Something went quite wrong. There should be a colon
30867 here, but there is not. Stop parsing parameters. */
30868 break;
30869 }
30870 type_name = cp_parser_objc_typename (parser);
30871 /* New ObjC allows attributes on parameters too. */
30872 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30873 parm_attr = cp_parser_attributes_opt (parser);
30874 identifier = cp_parser_identifier (parser);
30875
30876 params
30877 = chainon (params,
30878 objc_build_keyword_decl (selector,
30879 type_name,
30880 identifier,
30881 parm_attr));
30882
30883 token = cp_lexer_peek_token (parser->lexer);
30884 }
30885
30886 if (params == NULL_TREE)
30887 {
30888 cp_parser_error (parser, "objective-c++ method declaration is expected");
30889 return error_mark_node;
30890 }
30891
30892 /* We allow tail attributes for the method. */
30893 if (token->keyword == RID_ATTRIBUTE)
30894 {
30895 *attributes = cp_parser_attributes_opt (parser);
30896 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30897 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30898 return params;
30899 cp_parser_error (parser,
30900 "method attributes must be specified at the end");
30901 return error_mark_node;
30902 }
30903
30904 if (params == NULL_TREE)
30905 {
30906 cp_parser_error (parser, "objective-c++ method declaration is expected");
30907 return error_mark_node;
30908 }
30909 return params;
30910 }
30911
30912 /* Parse the non-keyword Objective-C params. */
30913
30914 static tree
30915 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30916 tree* attributes)
30917 {
30918 tree params = make_node (TREE_LIST);
30919 cp_token *token = cp_lexer_peek_token (parser->lexer);
30920 *ellipsisp = false; /* Initially, assume no ellipsis. */
30921
30922 while (token->type == CPP_COMMA)
30923 {
30924 cp_parameter_declarator *parmdecl;
30925 tree parm;
30926
30927 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30928 token = cp_lexer_peek_token (parser->lexer);
30929
30930 if (token->type == CPP_ELLIPSIS)
30931 {
30932 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30933 *ellipsisp = true;
30934 token = cp_lexer_peek_token (parser->lexer);
30935 break;
30936 }
30937
30938 /* TODO: parse attributes for tail parameters. */
30939 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
30940 false, NULL);
30941 parm = grokdeclarator (parmdecl->declarator,
30942 &parmdecl->decl_specifiers,
30943 PARM, /*initialized=*/0,
30944 /*attrlist=*/NULL);
30945
30946 chainon (params, build_tree_list (NULL_TREE, parm));
30947 token = cp_lexer_peek_token (parser->lexer);
30948 }
30949
30950 /* We allow tail attributes for the method. */
30951 if (token->keyword == RID_ATTRIBUTE)
30952 {
30953 if (*attributes == NULL_TREE)
30954 {
30955 *attributes = cp_parser_attributes_opt (parser);
30956 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30957 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30958 return params;
30959 }
30960 else
30961 /* We have an error, but parse the attributes, so that we can
30962 carry on. */
30963 *attributes = cp_parser_attributes_opt (parser);
30964
30965 cp_parser_error (parser,
30966 "method attributes must be specified at the end");
30967 return error_mark_node;
30968 }
30969
30970 return params;
30971 }
30972
30973 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30974
30975 static void
30976 cp_parser_objc_interstitial_code (cp_parser* parser)
30977 {
30978 cp_token *token = cp_lexer_peek_token (parser->lexer);
30979
30980 /* If the next token is `extern' and the following token is a string
30981 literal, then we have a linkage specification. */
30982 if (token->keyword == RID_EXTERN
30983 && cp_parser_is_pure_string_literal
30984 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30985 cp_parser_linkage_specification (parser);
30986 /* Handle #pragma, if any. */
30987 else if (token->type == CPP_PRAGMA)
30988 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30989 /* Allow stray semicolons. */
30990 else if (token->type == CPP_SEMICOLON)
30991 cp_lexer_consume_token (parser->lexer);
30992 /* Mark methods as optional or required, when building protocols. */
30993 else if (token->keyword == RID_AT_OPTIONAL)
30994 {
30995 cp_lexer_consume_token (parser->lexer);
30996 objc_set_method_opt (true);
30997 }
30998 else if (token->keyword == RID_AT_REQUIRED)
30999 {
31000 cp_lexer_consume_token (parser->lexer);
31001 objc_set_method_opt (false);
31002 }
31003 else if (token->keyword == RID_NAMESPACE)
31004 cp_parser_namespace_definition (parser);
31005 /* Other stray characters must generate errors. */
31006 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
31007 {
31008 cp_lexer_consume_token (parser->lexer);
31009 error ("stray %qs between Objective-C++ methods",
31010 token->type == CPP_OPEN_BRACE ? "{" : "}");
31011 }
31012 /* Finally, try to parse a block-declaration, or a function-definition. */
31013 else
31014 cp_parser_block_declaration (parser, /*statement_p=*/false);
31015 }
31016
31017 /* Parse a method signature. */
31018
31019 static tree
31020 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
31021 {
31022 tree rettype, kwdparms, optparms;
31023 bool ellipsis = false;
31024 bool is_class_method;
31025
31026 is_class_method = cp_parser_objc_method_type (parser);
31027 rettype = cp_parser_objc_typename (parser);
31028 *attributes = NULL_TREE;
31029 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
31030 if (kwdparms == error_mark_node)
31031 return error_mark_node;
31032 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
31033 if (optparms == error_mark_node)
31034 return error_mark_node;
31035
31036 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
31037 }
31038
31039 static bool
31040 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
31041 {
31042 tree tattr;
31043 cp_lexer_save_tokens (parser->lexer);
31044 tattr = cp_parser_attributes_opt (parser);
31045 gcc_assert (tattr) ;
31046
31047 /* If the attributes are followed by a method introducer, this is not allowed.
31048 Dump the attributes and flag the situation. */
31049 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
31050 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31051 return true;
31052
31053 /* Otherwise, the attributes introduce some interstitial code, possibly so
31054 rewind to allow that check. */
31055 cp_lexer_rollback_tokens (parser->lexer);
31056 return false;
31057 }
31058
31059 /* Parse an Objective-C method prototype list. */
31060
31061 static void
31062 cp_parser_objc_method_prototype_list (cp_parser* parser)
31063 {
31064 cp_token *token = cp_lexer_peek_token (parser->lexer);
31065
31066 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31067 {
31068 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31069 {
31070 tree attributes, sig;
31071 bool is_class_method;
31072 if (token->type == CPP_PLUS)
31073 is_class_method = true;
31074 else
31075 is_class_method = false;
31076 sig = cp_parser_objc_method_signature (parser, &attributes);
31077 if (sig == error_mark_node)
31078 {
31079 cp_parser_skip_to_end_of_block_or_statement (parser);
31080 token = cp_lexer_peek_token (parser->lexer);
31081 continue;
31082 }
31083 objc_add_method_declaration (is_class_method, sig, attributes);
31084 cp_parser_consume_semicolon_at_end_of_statement (parser);
31085 }
31086 else if (token->keyword == RID_AT_PROPERTY)
31087 cp_parser_objc_at_property_declaration (parser);
31088 else if (token->keyword == RID_ATTRIBUTE
31089 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31090 warning_at (cp_lexer_peek_token (parser->lexer)->location,
31091 OPT_Wattributes,
31092 "prefix attributes are ignored for methods");
31093 else
31094 /* Allow for interspersed non-ObjC++ code. */
31095 cp_parser_objc_interstitial_code (parser);
31096
31097 token = cp_lexer_peek_token (parser->lexer);
31098 }
31099
31100 if (token->type != CPP_EOF)
31101 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31102 else
31103 cp_parser_error (parser, "expected %<@end%>");
31104
31105 objc_finish_interface ();
31106 }
31107
31108 /* Parse an Objective-C method definition list. */
31109
31110 static void
31111 cp_parser_objc_method_definition_list (cp_parser* parser)
31112 {
31113 cp_token *token = cp_lexer_peek_token (parser->lexer);
31114
31115 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31116 {
31117 tree meth;
31118
31119 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31120 {
31121 cp_token *ptk;
31122 tree sig, attribute;
31123 bool is_class_method;
31124 if (token->type == CPP_PLUS)
31125 is_class_method = true;
31126 else
31127 is_class_method = false;
31128 push_deferring_access_checks (dk_deferred);
31129 sig = cp_parser_objc_method_signature (parser, &attribute);
31130 if (sig == error_mark_node)
31131 {
31132 cp_parser_skip_to_end_of_block_or_statement (parser);
31133 token = cp_lexer_peek_token (parser->lexer);
31134 continue;
31135 }
31136 objc_start_method_definition (is_class_method, sig, attribute,
31137 NULL_TREE);
31138
31139 /* For historical reasons, we accept an optional semicolon. */
31140 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31141 cp_lexer_consume_token (parser->lexer);
31142
31143 ptk = cp_lexer_peek_token (parser->lexer);
31144 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
31145 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
31146 {
31147 perform_deferred_access_checks (tf_warning_or_error);
31148 stop_deferring_access_checks ();
31149 meth = cp_parser_function_definition_after_declarator (parser,
31150 false);
31151 pop_deferring_access_checks ();
31152 objc_finish_method_definition (meth);
31153 }
31154 }
31155 /* The following case will be removed once @synthesize is
31156 completely implemented. */
31157 else if (token->keyword == RID_AT_PROPERTY)
31158 cp_parser_objc_at_property_declaration (parser);
31159 else if (token->keyword == RID_AT_SYNTHESIZE)
31160 cp_parser_objc_at_synthesize_declaration (parser);
31161 else if (token->keyword == RID_AT_DYNAMIC)
31162 cp_parser_objc_at_dynamic_declaration (parser);
31163 else if (token->keyword == RID_ATTRIBUTE
31164 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31165 warning_at (token->location, OPT_Wattributes,
31166 "prefix attributes are ignored for methods");
31167 else
31168 /* Allow for interspersed non-ObjC++ code. */
31169 cp_parser_objc_interstitial_code (parser);
31170
31171 token = cp_lexer_peek_token (parser->lexer);
31172 }
31173
31174 if (token->type != CPP_EOF)
31175 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31176 else
31177 cp_parser_error (parser, "expected %<@end%>");
31178
31179 objc_finish_implementation ();
31180 }
31181
31182 /* Parse Objective-C ivars. */
31183
31184 static void
31185 cp_parser_objc_class_ivars (cp_parser* parser)
31186 {
31187 cp_token *token = cp_lexer_peek_token (parser->lexer);
31188
31189 if (token->type != CPP_OPEN_BRACE)
31190 return; /* No ivars specified. */
31191
31192 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
31193 token = cp_lexer_peek_token (parser->lexer);
31194
31195 while (token->type != CPP_CLOSE_BRACE
31196 && token->keyword != RID_AT_END && token->type != CPP_EOF)
31197 {
31198 cp_decl_specifier_seq declspecs;
31199 int decl_class_or_enum_p;
31200 tree prefix_attributes;
31201
31202 cp_parser_objc_visibility_spec (parser);
31203
31204 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31205 break;
31206
31207 cp_parser_decl_specifier_seq (parser,
31208 CP_PARSER_FLAGS_OPTIONAL,
31209 &declspecs,
31210 &decl_class_or_enum_p);
31211
31212 /* auto, register, static, extern, mutable. */
31213 if (declspecs.storage_class != sc_none)
31214 {
31215 cp_parser_error (parser, "invalid type for instance variable");
31216 declspecs.storage_class = sc_none;
31217 }
31218
31219 /* thread_local. */
31220 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31221 {
31222 cp_parser_error (parser, "invalid type for instance variable");
31223 declspecs.locations[ds_thread] = 0;
31224 }
31225
31226 /* typedef. */
31227 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31228 {
31229 cp_parser_error (parser, "invalid type for instance variable");
31230 declspecs.locations[ds_typedef] = 0;
31231 }
31232
31233 prefix_attributes = declspecs.attributes;
31234 declspecs.attributes = NULL_TREE;
31235
31236 /* Keep going until we hit the `;' at the end of the
31237 declaration. */
31238 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31239 {
31240 tree width = NULL_TREE, attributes, first_attribute, decl;
31241 cp_declarator *declarator = NULL;
31242 int ctor_dtor_or_conv_p;
31243
31244 /* Check for a (possibly unnamed) bitfield declaration. */
31245 token = cp_lexer_peek_token (parser->lexer);
31246 if (token->type == CPP_COLON)
31247 goto eat_colon;
31248
31249 if (token->type == CPP_NAME
31250 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
31251 == CPP_COLON))
31252 {
31253 /* Get the name of the bitfield. */
31254 declarator = make_id_declarator (NULL_TREE,
31255 cp_parser_identifier (parser),
31256 sfk_none, token->location);
31257
31258 eat_colon:
31259 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31260 /* Get the width of the bitfield. */
31261 width
31262 = cp_parser_constant_expression (parser);
31263 }
31264 else
31265 {
31266 /* Parse the declarator. */
31267 declarator
31268 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31269 CP_PARSER_FLAGS_NONE,
31270 &ctor_dtor_or_conv_p,
31271 /*parenthesized_p=*/NULL,
31272 /*member_p=*/false,
31273 /*friend_p=*/false,
31274 /*static_p=*/false);
31275 }
31276
31277 /* Look for attributes that apply to the ivar. */
31278 attributes = cp_parser_attributes_opt (parser);
31279 /* Remember which attributes are prefix attributes and
31280 which are not. */
31281 first_attribute = attributes;
31282 /* Combine the attributes. */
31283 attributes = attr_chainon (prefix_attributes, attributes);
31284
31285 if (width)
31286 /* Create the bitfield declaration. */
31287 decl = grokbitfield (declarator, &declspecs,
31288 width, NULL_TREE, attributes);
31289 else
31290 decl = grokfield (declarator, &declspecs,
31291 NULL_TREE, /*init_const_expr_p=*/false,
31292 NULL_TREE, attributes);
31293
31294 /* Add the instance variable. */
31295 if (decl != error_mark_node && decl != NULL_TREE)
31296 objc_add_instance_variable (decl);
31297
31298 /* Reset PREFIX_ATTRIBUTES. */
31299 if (attributes != error_mark_node)
31300 {
31301 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31302 attributes = TREE_CHAIN (attributes);
31303 if (attributes)
31304 TREE_CHAIN (attributes) = NULL_TREE;
31305 }
31306
31307 token = cp_lexer_peek_token (parser->lexer);
31308
31309 if (token->type == CPP_COMMA)
31310 {
31311 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31312 continue;
31313 }
31314 break;
31315 }
31316
31317 cp_parser_consume_semicolon_at_end_of_statement (parser);
31318 token = cp_lexer_peek_token (parser->lexer);
31319 }
31320
31321 if (token->keyword == RID_AT_END)
31322 cp_parser_error (parser, "expected %<}%>");
31323
31324 /* Do not consume the RID_AT_END, so it will be read again as terminating
31325 the @interface of @implementation. */
31326 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
31327 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
31328
31329 /* For historical reasons, we accept an optional semicolon. */
31330 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31331 cp_lexer_consume_token (parser->lexer);
31332 }
31333
31334 /* Parse an Objective-C protocol declaration. */
31335
31336 static void
31337 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
31338 {
31339 tree proto, protorefs;
31340 cp_token *tok;
31341
31342 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
31343 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31344 {
31345 tok = cp_lexer_peek_token (parser->lexer);
31346 error_at (tok->location, "identifier expected after %<@protocol%>");
31347 cp_parser_consume_semicolon_at_end_of_statement (parser);
31348 return;
31349 }
31350
31351 /* See if we have a forward declaration or a definition. */
31352 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
31353
31354 /* Try a forward declaration first. */
31355 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
31356 {
31357 while (true)
31358 {
31359 tree id;
31360
31361 id = cp_parser_identifier (parser);
31362 if (id == error_mark_node)
31363 break;
31364
31365 objc_declare_protocol (id, attributes);
31366
31367 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31368 cp_lexer_consume_token (parser->lexer);
31369 else
31370 break;
31371 }
31372 cp_parser_consume_semicolon_at_end_of_statement (parser);
31373 }
31374
31375 /* Ok, we got a full-fledged definition (or at least should). */
31376 else
31377 {
31378 proto = cp_parser_identifier (parser);
31379 protorefs = cp_parser_objc_protocol_refs_opt (parser);
31380 objc_start_protocol (proto, protorefs, attributes);
31381 cp_parser_objc_method_prototype_list (parser);
31382 }
31383 }
31384
31385 /* Parse an Objective-C superclass or category. */
31386
31387 static void
31388 cp_parser_objc_superclass_or_category (cp_parser *parser,
31389 bool iface_p,
31390 tree *super,
31391 tree *categ, bool *is_class_extension)
31392 {
31393 cp_token *next = cp_lexer_peek_token (parser->lexer);
31394
31395 *super = *categ = NULL_TREE;
31396 *is_class_extension = false;
31397 if (next->type == CPP_COLON)
31398 {
31399 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31400 *super = cp_parser_identifier (parser);
31401 }
31402 else if (next->type == CPP_OPEN_PAREN)
31403 {
31404 matching_parens parens;
31405 parens.consume_open (parser); /* Eat '('. */
31406
31407 /* If there is no category name, and this is an @interface, we
31408 have a class extension. */
31409 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31410 {
31411 *categ = NULL_TREE;
31412 *is_class_extension = true;
31413 }
31414 else
31415 *categ = cp_parser_identifier (parser);
31416
31417 parens.require_close (parser);
31418 }
31419 }
31420
31421 /* Parse an Objective-C class interface. */
31422
31423 static void
31424 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
31425 {
31426 tree name, super, categ, protos;
31427 bool is_class_extension;
31428
31429 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
31430 name = cp_parser_identifier (parser);
31431 if (name == error_mark_node)
31432 {
31433 /* It's hard to recover because even if valid @interface stuff
31434 is to follow, we can't compile it (or validate it) if we
31435 don't even know which class it refers to. Let's assume this
31436 was a stray '@interface' token in the stream and skip it.
31437 */
31438 return;
31439 }
31440 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
31441 &is_class_extension);
31442 protos = cp_parser_objc_protocol_refs_opt (parser);
31443
31444 /* We have either a class or a category on our hands. */
31445 if (categ || is_class_extension)
31446 objc_start_category_interface (name, categ, protos, attributes);
31447 else
31448 {
31449 objc_start_class_interface (name, super, protos, attributes);
31450 /* Handle instance variable declarations, if any. */
31451 cp_parser_objc_class_ivars (parser);
31452 objc_continue_interface ();
31453 }
31454
31455 cp_parser_objc_method_prototype_list (parser);
31456 }
31457
31458 /* Parse an Objective-C class implementation. */
31459
31460 static void
31461 cp_parser_objc_class_implementation (cp_parser* parser)
31462 {
31463 tree name, super, categ;
31464 bool is_class_extension;
31465
31466 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
31467 name = cp_parser_identifier (parser);
31468 if (name == error_mark_node)
31469 {
31470 /* It's hard to recover because even if valid @implementation
31471 stuff is to follow, we can't compile it (or validate it) if
31472 we don't even know which class it refers to. Let's assume
31473 this was a stray '@implementation' token in the stream and
31474 skip it.
31475 */
31476 return;
31477 }
31478 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
31479 &is_class_extension);
31480
31481 /* We have either a class or a category on our hands. */
31482 if (categ)
31483 objc_start_category_implementation (name, categ);
31484 else
31485 {
31486 objc_start_class_implementation (name, super);
31487 /* Handle instance variable declarations, if any. */
31488 cp_parser_objc_class_ivars (parser);
31489 objc_continue_implementation ();
31490 }
31491
31492 cp_parser_objc_method_definition_list (parser);
31493 }
31494
31495 /* Consume the @end token and finish off the implementation. */
31496
31497 static void
31498 cp_parser_objc_end_implementation (cp_parser* parser)
31499 {
31500 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31501 objc_finish_implementation ();
31502 }
31503
31504 /* Parse an Objective-C declaration. */
31505
31506 static void
31507 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
31508 {
31509 /* Try to figure out what kind of declaration is present. */
31510 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31511
31512 if (attributes)
31513 switch (kwd->keyword)
31514 {
31515 case RID_AT_ALIAS:
31516 case RID_AT_CLASS:
31517 case RID_AT_END:
31518 error_at (kwd->location, "attributes may not be specified before"
31519 " the %<@%D%> Objective-C++ keyword",
31520 kwd->u.value);
31521 attributes = NULL;
31522 break;
31523 case RID_AT_IMPLEMENTATION:
31524 warning_at (kwd->location, OPT_Wattributes,
31525 "prefix attributes are ignored before %<@%D%>",
31526 kwd->u.value);
31527 attributes = NULL;
31528 default:
31529 break;
31530 }
31531
31532 switch (kwd->keyword)
31533 {
31534 case RID_AT_ALIAS:
31535 cp_parser_objc_alias_declaration (parser);
31536 break;
31537 case RID_AT_CLASS:
31538 cp_parser_objc_class_declaration (parser);
31539 break;
31540 case RID_AT_PROTOCOL:
31541 cp_parser_objc_protocol_declaration (parser, attributes);
31542 break;
31543 case RID_AT_INTERFACE:
31544 cp_parser_objc_class_interface (parser, attributes);
31545 break;
31546 case RID_AT_IMPLEMENTATION:
31547 cp_parser_objc_class_implementation (parser);
31548 break;
31549 case RID_AT_END:
31550 cp_parser_objc_end_implementation (parser);
31551 break;
31552 default:
31553 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31554 kwd->u.value);
31555 cp_parser_skip_to_end_of_block_or_statement (parser);
31556 }
31557 }
31558
31559 /* Parse an Objective-C try-catch-finally statement.
31560
31561 objc-try-catch-finally-stmt:
31562 @try compound-statement objc-catch-clause-seq [opt]
31563 objc-finally-clause [opt]
31564
31565 objc-catch-clause-seq:
31566 objc-catch-clause objc-catch-clause-seq [opt]
31567
31568 objc-catch-clause:
31569 @catch ( objc-exception-declaration ) compound-statement
31570
31571 objc-finally-clause:
31572 @finally compound-statement
31573
31574 objc-exception-declaration:
31575 parameter-declaration
31576 '...'
31577
31578 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31579
31580 Returns NULL_TREE.
31581
31582 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31583 for C. Keep them in sync. */
31584
31585 static tree
31586 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
31587 {
31588 location_t location;
31589 tree stmt;
31590
31591 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
31592 location = cp_lexer_peek_token (parser->lexer)->location;
31593 objc_maybe_warn_exceptions (location);
31594 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31595 node, lest it get absorbed into the surrounding block. */
31596 stmt = push_stmt_list ();
31597 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31598 objc_begin_try_stmt (location, pop_stmt_list (stmt));
31599
31600 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
31601 {
31602 cp_parameter_declarator *parm;
31603 tree parameter_declaration = error_mark_node;
31604 bool seen_open_paren = false;
31605 matching_parens parens;
31606
31607 cp_lexer_consume_token (parser->lexer);
31608 if (parens.require_open (parser))
31609 seen_open_paren = true;
31610 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31611 {
31612 /* We have "@catch (...)" (where the '...' are literally
31613 what is in the code). Skip the '...'.
31614 parameter_declaration is set to NULL_TREE, and
31615 objc_being_catch_clauses() knows that that means
31616 '...'. */
31617 cp_lexer_consume_token (parser->lexer);
31618 parameter_declaration = NULL_TREE;
31619 }
31620 else
31621 {
31622 /* We have "@catch (NSException *exception)" or something
31623 like that. Parse the parameter declaration. */
31624 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31625 false, NULL);
31626 if (parm == NULL)
31627 parameter_declaration = error_mark_node;
31628 else
31629 parameter_declaration = grokdeclarator (parm->declarator,
31630 &parm->decl_specifiers,
31631 PARM, /*initialized=*/0,
31632 /*attrlist=*/NULL);
31633 }
31634 if (seen_open_paren)
31635 parens.require_close (parser);
31636 else
31637 {
31638 /* If there was no open parenthesis, we are recovering from
31639 an error, and we are trying to figure out what mistake
31640 the user has made. */
31641
31642 /* If there is an immediate closing parenthesis, the user
31643 probably forgot the opening one (ie, they typed "@catch
31644 NSException *e)". Parse the closing parenthesis and keep
31645 going. */
31646 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31647 cp_lexer_consume_token (parser->lexer);
31648
31649 /* If these is no immediate closing parenthesis, the user
31650 probably doesn't know that parenthesis are required at
31651 all (ie, they typed "@catch NSException *e"). So, just
31652 forget about the closing parenthesis and keep going. */
31653 }
31654 objc_begin_catch_clause (parameter_declaration);
31655 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31656 objc_finish_catch_clause ();
31657 }
31658 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31659 {
31660 cp_lexer_consume_token (parser->lexer);
31661 location = cp_lexer_peek_token (parser->lexer)->location;
31662 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31663 node, lest it get absorbed into the surrounding block. */
31664 stmt = push_stmt_list ();
31665 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31666 objc_build_finally_clause (location, pop_stmt_list (stmt));
31667 }
31668
31669 return objc_finish_try_stmt ();
31670 }
31671
31672 /* Parse an Objective-C synchronized statement.
31673
31674 objc-synchronized-stmt:
31675 @synchronized ( expression ) compound-statement
31676
31677 Returns NULL_TREE. */
31678
31679 static tree
31680 cp_parser_objc_synchronized_statement (cp_parser *parser)
31681 {
31682 location_t location;
31683 tree lock, stmt;
31684
31685 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31686
31687 location = cp_lexer_peek_token (parser->lexer)->location;
31688 objc_maybe_warn_exceptions (location);
31689 matching_parens parens;
31690 parens.require_open (parser);
31691 lock = cp_parser_expression (parser);
31692 parens.require_close (parser);
31693
31694 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31695 node, lest it get absorbed into the surrounding block. */
31696 stmt = push_stmt_list ();
31697 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31698
31699 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31700 }
31701
31702 /* Parse an Objective-C throw statement.
31703
31704 objc-throw-stmt:
31705 @throw assignment-expression [opt] ;
31706
31707 Returns a constructed '@throw' statement. */
31708
31709 static tree
31710 cp_parser_objc_throw_statement (cp_parser *parser)
31711 {
31712 tree expr = NULL_TREE;
31713 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31714
31715 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31716
31717 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31718 expr = cp_parser_expression (parser);
31719
31720 cp_parser_consume_semicolon_at_end_of_statement (parser);
31721
31722 return objc_build_throw_stmt (loc, expr);
31723 }
31724
31725 /* Parse an Objective-C statement. */
31726
31727 static tree
31728 cp_parser_objc_statement (cp_parser * parser)
31729 {
31730 /* Try to figure out what kind of declaration is present. */
31731 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31732
31733 switch (kwd->keyword)
31734 {
31735 case RID_AT_TRY:
31736 return cp_parser_objc_try_catch_finally_statement (parser);
31737 case RID_AT_SYNCHRONIZED:
31738 return cp_parser_objc_synchronized_statement (parser);
31739 case RID_AT_THROW:
31740 return cp_parser_objc_throw_statement (parser);
31741 default:
31742 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31743 kwd->u.value);
31744 cp_parser_skip_to_end_of_block_or_statement (parser);
31745 }
31746
31747 return error_mark_node;
31748 }
31749
31750 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31751 look ahead to see if an objc keyword follows the attributes. This
31752 is to detect the use of prefix attributes on ObjC @interface and
31753 @protocol. */
31754
31755 static bool
31756 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31757 {
31758 cp_lexer_save_tokens (parser->lexer);
31759 *attrib = cp_parser_attributes_opt (parser);
31760 gcc_assert (*attrib);
31761 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31762 {
31763 cp_lexer_commit_tokens (parser->lexer);
31764 return true;
31765 }
31766 cp_lexer_rollback_tokens (parser->lexer);
31767 return false;
31768 }
31769
31770 /* This routine is a minimal replacement for
31771 c_parser_struct_declaration () used when parsing the list of
31772 types/names or ObjC++ properties. For example, when parsing the
31773 code
31774
31775 @property (readonly) int a, b, c;
31776
31777 this function is responsible for parsing "int a, int b, int c" and
31778 returning the declarations as CHAIN of DECLs.
31779
31780 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31781 similar parsing. */
31782 static tree
31783 cp_parser_objc_struct_declaration (cp_parser *parser)
31784 {
31785 tree decls = NULL_TREE;
31786 cp_decl_specifier_seq declspecs;
31787 int decl_class_or_enum_p;
31788 tree prefix_attributes;
31789
31790 cp_parser_decl_specifier_seq (parser,
31791 CP_PARSER_FLAGS_NONE,
31792 &declspecs,
31793 &decl_class_or_enum_p);
31794
31795 if (declspecs.type == error_mark_node)
31796 return error_mark_node;
31797
31798 /* auto, register, static, extern, mutable. */
31799 if (declspecs.storage_class != sc_none)
31800 {
31801 cp_parser_error (parser, "invalid type for property");
31802 declspecs.storage_class = sc_none;
31803 }
31804
31805 /* thread_local. */
31806 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31807 {
31808 cp_parser_error (parser, "invalid type for property");
31809 declspecs.locations[ds_thread] = 0;
31810 }
31811
31812 /* typedef. */
31813 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31814 {
31815 cp_parser_error (parser, "invalid type for property");
31816 declspecs.locations[ds_typedef] = 0;
31817 }
31818
31819 prefix_attributes = declspecs.attributes;
31820 declspecs.attributes = NULL_TREE;
31821
31822 /* Keep going until we hit the `;' at the end of the declaration. */
31823 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31824 {
31825 tree attributes, first_attribute, decl;
31826 cp_declarator *declarator;
31827 cp_token *token;
31828
31829 /* Parse the declarator. */
31830 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31831 CP_PARSER_FLAGS_NONE,
31832 NULL, NULL, false, false, false);
31833
31834 /* Look for attributes that apply to the ivar. */
31835 attributes = cp_parser_attributes_opt (parser);
31836 /* Remember which attributes are prefix attributes and
31837 which are not. */
31838 first_attribute = attributes;
31839 /* Combine the attributes. */
31840 attributes = attr_chainon (prefix_attributes, attributes);
31841
31842 decl = grokfield (declarator, &declspecs,
31843 NULL_TREE, /*init_const_expr_p=*/false,
31844 NULL_TREE, attributes);
31845
31846 if (decl == error_mark_node || decl == NULL_TREE)
31847 return error_mark_node;
31848
31849 /* Reset PREFIX_ATTRIBUTES. */
31850 if (attributes != error_mark_node)
31851 {
31852 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31853 attributes = TREE_CHAIN (attributes);
31854 if (attributes)
31855 TREE_CHAIN (attributes) = NULL_TREE;
31856 }
31857
31858 DECL_CHAIN (decl) = decls;
31859 decls = decl;
31860
31861 token = cp_lexer_peek_token (parser->lexer);
31862 if (token->type == CPP_COMMA)
31863 {
31864 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31865 continue;
31866 }
31867 else
31868 break;
31869 }
31870 return decls;
31871 }
31872
31873 /* Parse an Objective-C @property declaration. The syntax is:
31874
31875 objc-property-declaration:
31876 '@property' objc-property-attributes[opt] struct-declaration ;
31877
31878 objc-property-attributes:
31879 '(' objc-property-attribute-list ')'
31880
31881 objc-property-attribute-list:
31882 objc-property-attribute
31883 objc-property-attribute-list, objc-property-attribute
31884
31885 objc-property-attribute
31886 'getter' = identifier
31887 'setter' = identifier
31888 'readonly'
31889 'readwrite'
31890 'assign'
31891 'retain'
31892 'copy'
31893 'nonatomic'
31894
31895 For example:
31896 @property NSString *name;
31897 @property (readonly) id object;
31898 @property (retain, nonatomic, getter=getTheName) id name;
31899 @property int a, b, c;
31900
31901 PS: This function is identical to
31902 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31903 static void
31904 cp_parser_objc_at_property_declaration (cp_parser *parser)
31905 {
31906 /* The following variables hold the attributes of the properties as
31907 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31908 seen. When we see an attribute, we set them to 'true' (if they
31909 are boolean properties) or to the identifier (if they have an
31910 argument, ie, for getter and setter). Note that here we only
31911 parse the list of attributes, check the syntax and accumulate the
31912 attributes that we find. objc_add_property_declaration() will
31913 then process the information. */
31914 bool property_assign = false;
31915 bool property_copy = false;
31916 tree property_getter_ident = NULL_TREE;
31917 bool property_nonatomic = false;
31918 bool property_readonly = false;
31919 bool property_readwrite = false;
31920 bool property_retain = false;
31921 tree property_setter_ident = NULL_TREE;
31922
31923 /* 'properties' is the list of properties that we read. Usually a
31924 single one, but maybe more (eg, in "@property int a, b, c;" there
31925 are three). */
31926 tree properties;
31927 location_t loc;
31928
31929 loc = cp_lexer_peek_token (parser->lexer)->location;
31930
31931 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31932
31933 /* Parse the optional attribute list... */
31934 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31935 {
31936 /* Eat the '('. */
31937 matching_parens parens;
31938 parens.consume_open (parser);
31939
31940 while (true)
31941 {
31942 bool syntax_error = false;
31943 cp_token *token = cp_lexer_peek_token (parser->lexer);
31944 enum rid keyword;
31945
31946 if (token->type != CPP_NAME)
31947 {
31948 cp_parser_error (parser, "expected identifier");
31949 break;
31950 }
31951 keyword = C_RID_CODE (token->u.value);
31952 cp_lexer_consume_token (parser->lexer);
31953 switch (keyword)
31954 {
31955 case RID_ASSIGN: property_assign = true; break;
31956 case RID_COPY: property_copy = true; break;
31957 case RID_NONATOMIC: property_nonatomic = true; break;
31958 case RID_READONLY: property_readonly = true; break;
31959 case RID_READWRITE: property_readwrite = true; break;
31960 case RID_RETAIN: property_retain = true; break;
31961
31962 case RID_GETTER:
31963 case RID_SETTER:
31964 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31965 {
31966 if (keyword == RID_GETTER)
31967 cp_parser_error (parser,
31968 "missing %<=%> (after %<getter%> attribute)");
31969 else
31970 cp_parser_error (parser,
31971 "missing %<=%> (after %<setter%> attribute)");
31972 syntax_error = true;
31973 break;
31974 }
31975 cp_lexer_consume_token (parser->lexer); /* eat the = */
31976 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31977 {
31978 cp_parser_error (parser, "expected identifier");
31979 syntax_error = true;
31980 break;
31981 }
31982 if (keyword == RID_SETTER)
31983 {
31984 if (property_setter_ident != NULL_TREE)
31985 {
31986 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31987 cp_lexer_consume_token (parser->lexer);
31988 }
31989 else
31990 property_setter_ident = cp_parser_objc_selector (parser);
31991 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31992 cp_parser_error (parser, "setter name must terminate with %<:%>");
31993 else
31994 cp_lexer_consume_token (parser->lexer);
31995 }
31996 else
31997 {
31998 if (property_getter_ident != NULL_TREE)
31999 {
32000 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
32001 cp_lexer_consume_token (parser->lexer);
32002 }
32003 else
32004 property_getter_ident = cp_parser_objc_selector (parser);
32005 }
32006 break;
32007 default:
32008 cp_parser_error (parser, "unknown property attribute");
32009 syntax_error = true;
32010 break;
32011 }
32012
32013 if (syntax_error)
32014 break;
32015
32016 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32017 cp_lexer_consume_token (parser->lexer);
32018 else
32019 break;
32020 }
32021
32022 /* FIXME: "@property (setter, assign);" will generate a spurious
32023 "error: expected ‘)’ before ‘,’ token". This is because
32024 cp_parser_require, unlike the C counterpart, will produce an
32025 error even if we are in error recovery. */
32026 if (!parens.require_close (parser))
32027 {
32028 cp_parser_skip_to_closing_parenthesis (parser,
32029 /*recovering=*/true,
32030 /*or_comma=*/false,
32031 /*consume_paren=*/true);
32032 }
32033 }
32034
32035 /* ... and the property declaration(s). */
32036 properties = cp_parser_objc_struct_declaration (parser);
32037
32038 if (properties == error_mark_node)
32039 {
32040 cp_parser_skip_to_end_of_statement (parser);
32041 /* If the next token is now a `;', consume it. */
32042 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32043 cp_lexer_consume_token (parser->lexer);
32044 return;
32045 }
32046
32047 if (properties == NULL_TREE)
32048 cp_parser_error (parser, "expected identifier");
32049 else
32050 {
32051 /* Comma-separated properties are chained together in
32052 reverse order; add them one by one. */
32053 properties = nreverse (properties);
32054
32055 for (; properties; properties = TREE_CHAIN (properties))
32056 objc_add_property_declaration (loc, copy_node (properties),
32057 property_readonly, property_readwrite,
32058 property_assign, property_retain,
32059 property_copy, property_nonatomic,
32060 property_getter_ident, property_setter_ident);
32061 }
32062
32063 cp_parser_consume_semicolon_at_end_of_statement (parser);
32064 }
32065
32066 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
32067
32068 objc-synthesize-declaration:
32069 @synthesize objc-synthesize-identifier-list ;
32070
32071 objc-synthesize-identifier-list:
32072 objc-synthesize-identifier
32073 objc-synthesize-identifier-list, objc-synthesize-identifier
32074
32075 objc-synthesize-identifier
32076 identifier
32077 identifier = identifier
32078
32079 For example:
32080 @synthesize MyProperty;
32081 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
32082
32083 PS: This function is identical to c_parser_objc_at_synthesize_declaration
32084 for C. Keep them in sync.
32085 */
32086 static void
32087 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
32088 {
32089 tree list = NULL_TREE;
32090 location_t loc;
32091 loc = cp_lexer_peek_token (parser->lexer)->location;
32092
32093 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
32094 while (true)
32095 {
32096 tree property, ivar;
32097 property = cp_parser_identifier (parser);
32098 if (property == error_mark_node)
32099 {
32100 cp_parser_consume_semicolon_at_end_of_statement (parser);
32101 return;
32102 }
32103 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
32104 {
32105 cp_lexer_consume_token (parser->lexer);
32106 ivar = cp_parser_identifier (parser);
32107 if (ivar == error_mark_node)
32108 {
32109 cp_parser_consume_semicolon_at_end_of_statement (parser);
32110 return;
32111 }
32112 }
32113 else
32114 ivar = NULL_TREE;
32115 list = chainon (list, build_tree_list (ivar, property));
32116 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32117 cp_lexer_consume_token (parser->lexer);
32118 else
32119 break;
32120 }
32121 cp_parser_consume_semicolon_at_end_of_statement (parser);
32122 objc_add_synthesize_declaration (loc, list);
32123 }
32124
32125 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
32126
32127 objc-dynamic-declaration:
32128 @dynamic identifier-list ;
32129
32130 For example:
32131 @dynamic MyProperty;
32132 @dynamic MyProperty, AnotherProperty;
32133
32134 PS: This function is identical to c_parser_objc_at_dynamic_declaration
32135 for C. Keep them in sync.
32136 */
32137 static void
32138 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
32139 {
32140 tree list = NULL_TREE;
32141 location_t loc;
32142 loc = cp_lexer_peek_token (parser->lexer)->location;
32143
32144 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
32145 while (true)
32146 {
32147 tree property;
32148 property = cp_parser_identifier (parser);
32149 if (property == error_mark_node)
32150 {
32151 cp_parser_consume_semicolon_at_end_of_statement (parser);
32152 return;
32153 }
32154 list = chainon (list, build_tree_list (NULL, property));
32155 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32156 cp_lexer_consume_token (parser->lexer);
32157 else
32158 break;
32159 }
32160 cp_parser_consume_semicolon_at_end_of_statement (parser);
32161 objc_add_dynamic_declaration (loc, list);
32162 }
32163
32164 \f
32165 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
32166
32167 /* Returns name of the next clause.
32168 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
32169 the token is not consumed. Otherwise appropriate pragma_omp_clause is
32170 returned and the token is consumed. */
32171
32172 static pragma_omp_clause
32173 cp_parser_omp_clause_name (cp_parser *parser)
32174 {
32175 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
32176
32177 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32178 result = PRAGMA_OACC_CLAUSE_AUTO;
32179 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
32180 result = PRAGMA_OMP_CLAUSE_IF;
32181 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32182 result = PRAGMA_OMP_CLAUSE_DEFAULT;
32183 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
32184 result = PRAGMA_OACC_CLAUSE_DELETE;
32185 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
32186 result = PRAGMA_OMP_CLAUSE_PRIVATE;
32187 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32188 result = PRAGMA_OMP_CLAUSE_FOR;
32189 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32190 {
32191 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32192 const char *p = IDENTIFIER_POINTER (id);
32193
32194 switch (p[0])
32195 {
32196 case 'a':
32197 if (!strcmp ("aligned", p))
32198 result = PRAGMA_OMP_CLAUSE_ALIGNED;
32199 else if (!strcmp ("async", p))
32200 result = PRAGMA_OACC_CLAUSE_ASYNC;
32201 break;
32202 case 'c':
32203 if (!strcmp ("collapse", p))
32204 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
32205 else if (!strcmp ("copy", p))
32206 result = PRAGMA_OACC_CLAUSE_COPY;
32207 else if (!strcmp ("copyin", p))
32208 result = PRAGMA_OMP_CLAUSE_COPYIN;
32209 else if (!strcmp ("copyout", p))
32210 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32211 else if (!strcmp ("copyprivate", p))
32212 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
32213 else if (!strcmp ("create", p))
32214 result = PRAGMA_OACC_CLAUSE_CREATE;
32215 break;
32216 case 'd':
32217 if (!strcmp ("defaultmap", p))
32218 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
32219 else if (!strcmp ("depend", p))
32220 result = PRAGMA_OMP_CLAUSE_DEPEND;
32221 else if (!strcmp ("device", p))
32222 result = PRAGMA_OMP_CLAUSE_DEVICE;
32223 else if (!strcmp ("deviceptr", p))
32224 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
32225 else if (!strcmp ("device_resident", p))
32226 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
32227 else if (!strcmp ("dist_schedule", p))
32228 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
32229 break;
32230 case 'f':
32231 if (!strcmp ("final", p))
32232 result = PRAGMA_OMP_CLAUSE_FINAL;
32233 else if (!strcmp ("finalize", p))
32234 result = PRAGMA_OACC_CLAUSE_FINALIZE;
32235 else if (!strcmp ("firstprivate", p))
32236 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
32237 else if (!strcmp ("from", p))
32238 result = PRAGMA_OMP_CLAUSE_FROM;
32239 break;
32240 case 'g':
32241 if (!strcmp ("gang", p))
32242 result = PRAGMA_OACC_CLAUSE_GANG;
32243 else if (!strcmp ("grainsize", p))
32244 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
32245 break;
32246 case 'h':
32247 if (!strcmp ("hint", p))
32248 result = PRAGMA_OMP_CLAUSE_HINT;
32249 else if (!strcmp ("host", p))
32250 result = PRAGMA_OACC_CLAUSE_HOST;
32251 break;
32252 case 'i':
32253 if (!strcmp ("if_present", p))
32254 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
32255 else if (!strcmp ("in_reduction", p))
32256 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
32257 else if (!strcmp ("inbranch", p))
32258 result = PRAGMA_OMP_CLAUSE_INBRANCH;
32259 else if (!strcmp ("independent", p))
32260 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
32261 else if (!strcmp ("is_device_ptr", p))
32262 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
32263 break;
32264 case 'l':
32265 if (!strcmp ("lastprivate", p))
32266 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
32267 else if (!strcmp ("linear", p))
32268 result = PRAGMA_OMP_CLAUSE_LINEAR;
32269 else if (!strcmp ("link", p))
32270 result = PRAGMA_OMP_CLAUSE_LINK;
32271 break;
32272 case 'm':
32273 if (!strcmp ("map", p))
32274 result = PRAGMA_OMP_CLAUSE_MAP;
32275 else if (!strcmp ("mergeable", p))
32276 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
32277 break;
32278 case 'n':
32279 if (!strcmp ("nogroup", p))
32280 result = PRAGMA_OMP_CLAUSE_NOGROUP;
32281 else if (!strcmp ("nontemporal", p))
32282 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
32283 else if (!strcmp ("notinbranch", p))
32284 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
32285 else if (!strcmp ("nowait", p))
32286 result = PRAGMA_OMP_CLAUSE_NOWAIT;
32287 else if (!strcmp ("num_gangs", p))
32288 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
32289 else if (!strcmp ("num_tasks", p))
32290 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
32291 else if (!strcmp ("num_teams", p))
32292 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
32293 else if (!strcmp ("num_threads", p))
32294 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
32295 else if (!strcmp ("num_workers", p))
32296 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
32297 break;
32298 case 'o':
32299 if (!strcmp ("ordered", p))
32300 result = PRAGMA_OMP_CLAUSE_ORDERED;
32301 break;
32302 case 'p':
32303 if (!strcmp ("parallel", p))
32304 result = PRAGMA_OMP_CLAUSE_PARALLEL;
32305 else if (!strcmp ("present", p))
32306 result = PRAGMA_OACC_CLAUSE_PRESENT;
32307 else if (!strcmp ("present_or_copy", p)
32308 || !strcmp ("pcopy", p))
32309 result = PRAGMA_OACC_CLAUSE_COPY;
32310 else if (!strcmp ("present_or_copyin", p)
32311 || !strcmp ("pcopyin", p))
32312 result = PRAGMA_OACC_CLAUSE_COPYIN;
32313 else if (!strcmp ("present_or_copyout", p)
32314 || !strcmp ("pcopyout", p))
32315 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32316 else if (!strcmp ("present_or_create", p)
32317 || !strcmp ("pcreate", p))
32318 result = PRAGMA_OACC_CLAUSE_CREATE;
32319 else if (!strcmp ("priority", p))
32320 result = PRAGMA_OMP_CLAUSE_PRIORITY;
32321 else if (!strcmp ("proc_bind", p))
32322 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
32323 break;
32324 case 'r':
32325 if (!strcmp ("reduction", p))
32326 result = PRAGMA_OMP_CLAUSE_REDUCTION;
32327 break;
32328 case 's':
32329 if (!strcmp ("safelen", p))
32330 result = PRAGMA_OMP_CLAUSE_SAFELEN;
32331 else if (!strcmp ("schedule", p))
32332 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
32333 else if (!strcmp ("sections", p))
32334 result = PRAGMA_OMP_CLAUSE_SECTIONS;
32335 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
32336 result = PRAGMA_OACC_CLAUSE_HOST;
32337 else if (!strcmp ("seq", p))
32338 result = PRAGMA_OACC_CLAUSE_SEQ;
32339 else if (!strcmp ("shared", p))
32340 result = PRAGMA_OMP_CLAUSE_SHARED;
32341 else if (!strcmp ("simd", p))
32342 result = PRAGMA_OMP_CLAUSE_SIMD;
32343 else if (!strcmp ("simdlen", p))
32344 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
32345 break;
32346 case 't':
32347 if (!strcmp ("task_reduction", p))
32348 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
32349 else if (!strcmp ("taskgroup", p))
32350 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
32351 else if (!strcmp ("thread_limit", p))
32352 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
32353 else if (!strcmp ("threads", p))
32354 result = PRAGMA_OMP_CLAUSE_THREADS;
32355 else if (!strcmp ("tile", p))
32356 result = PRAGMA_OACC_CLAUSE_TILE;
32357 else if (!strcmp ("to", p))
32358 result = PRAGMA_OMP_CLAUSE_TO;
32359 break;
32360 case 'u':
32361 if (!strcmp ("uniform", p))
32362 result = PRAGMA_OMP_CLAUSE_UNIFORM;
32363 else if (!strcmp ("untied", p))
32364 result = PRAGMA_OMP_CLAUSE_UNTIED;
32365 else if (!strcmp ("use_device", p))
32366 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
32367 else if (!strcmp ("use_device_ptr", p))
32368 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
32369 break;
32370 case 'v':
32371 if (!strcmp ("vector", p))
32372 result = PRAGMA_OACC_CLAUSE_VECTOR;
32373 else if (!strcmp ("vector_length", p))
32374 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
32375 break;
32376 case 'w':
32377 if (!strcmp ("wait", p))
32378 result = PRAGMA_OACC_CLAUSE_WAIT;
32379 else if (!strcmp ("worker", p))
32380 result = PRAGMA_OACC_CLAUSE_WORKER;
32381 break;
32382 }
32383 }
32384
32385 if (result != PRAGMA_OMP_CLAUSE_NONE)
32386 cp_lexer_consume_token (parser->lexer);
32387
32388 return result;
32389 }
32390
32391 /* Validate that a clause of the given type does not already exist. */
32392
32393 static void
32394 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
32395 const char *name, location_t location)
32396 {
32397 tree c;
32398
32399 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
32400 if (OMP_CLAUSE_CODE (c) == code)
32401 {
32402 error_at (location, "too many %qs clauses", name);
32403 break;
32404 }
32405 }
32406
32407 /* OpenMP 2.5:
32408 variable-list:
32409 identifier
32410 variable-list , identifier
32411
32412 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32413 colon). An opening parenthesis will have been consumed by the caller.
32414
32415 If KIND is nonzero, create the appropriate node and install the decl
32416 in OMP_CLAUSE_DECL and add the node to the head of the list.
32417
32418 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32419 return the list created.
32420
32421 COLON can be NULL if only closing parenthesis should end the list,
32422 or pointer to bool which will receive false if the list is terminated
32423 by closing parenthesis or true if the list is terminated by colon. */
32424
32425 static tree
32426 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
32427 tree list, bool *colon)
32428 {
32429 cp_token *token;
32430 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32431 if (colon)
32432 {
32433 parser->colon_corrects_to_scope_p = false;
32434 *colon = false;
32435 }
32436 while (1)
32437 {
32438 tree name, decl;
32439
32440 if (kind == OMP_CLAUSE_DEPEND)
32441 cp_parser_parse_tentatively (parser);
32442 token = cp_lexer_peek_token (parser->lexer);
32443 if (kind != 0
32444 && current_class_ptr
32445 && cp_parser_is_keyword (token, RID_THIS))
32446 {
32447 decl = finish_this_expr ();
32448 if (TREE_CODE (decl) == NON_LVALUE_EXPR
32449 || CONVERT_EXPR_P (decl))
32450 decl = TREE_OPERAND (decl, 0);
32451 cp_lexer_consume_token (parser->lexer);
32452 }
32453 else
32454 {
32455 name = cp_parser_id_expression (parser, /*template_p=*/false,
32456 /*check_dependency_p=*/true,
32457 /*template_p=*/NULL,
32458 /*declarator_p=*/false,
32459 /*optional_p=*/false);
32460 if (name == error_mark_node)
32461 {
32462 if (kind == OMP_CLAUSE_DEPEND
32463 && cp_parser_simulate_error (parser))
32464 goto depend_lvalue;
32465 goto skip_comma;
32466 }
32467
32468 if (identifier_p (name))
32469 decl = cp_parser_lookup_name_simple (parser, name, token->location);
32470 else
32471 decl = name;
32472 if (decl == error_mark_node)
32473 {
32474 if (kind == OMP_CLAUSE_DEPEND
32475 && cp_parser_simulate_error (parser))
32476 goto depend_lvalue;
32477 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
32478 token->location);
32479 }
32480 }
32481 if (decl == error_mark_node)
32482 ;
32483 else if (kind != 0)
32484 {
32485 switch (kind)
32486 {
32487 case OMP_CLAUSE__CACHE_:
32488 /* The OpenACC cache directive explicitly only allows "array
32489 elements or subarrays". */
32490 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
32491 {
32492 error_at (token->location, "expected %<[%>");
32493 decl = error_mark_node;
32494 break;
32495 }
32496 /* FALLTHROUGH. */
32497 case OMP_CLAUSE_MAP:
32498 case OMP_CLAUSE_FROM:
32499 case OMP_CLAUSE_TO:
32500 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
32501 {
32502 location_t loc
32503 = cp_lexer_peek_token (parser->lexer)->location;
32504 cp_id_kind idk = CP_ID_KIND_NONE;
32505 cp_lexer_consume_token (parser->lexer);
32506 decl = convert_from_reference (decl);
32507 decl
32508 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
32509 decl, false,
32510 &idk, loc);
32511 }
32512 /* FALLTHROUGH. */
32513 case OMP_CLAUSE_DEPEND:
32514 case OMP_CLAUSE_REDUCTION:
32515 case OMP_CLAUSE_IN_REDUCTION:
32516 case OMP_CLAUSE_TASK_REDUCTION:
32517 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
32518 {
32519 tree low_bound = NULL_TREE, length = NULL_TREE;
32520
32521 parser->colon_corrects_to_scope_p = false;
32522 cp_lexer_consume_token (parser->lexer);
32523 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32524 low_bound = cp_parser_expression (parser);
32525 if (!colon)
32526 parser->colon_corrects_to_scope_p
32527 = saved_colon_corrects_to_scope_p;
32528 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
32529 length = integer_one_node;
32530 else
32531 {
32532 /* Look for `:'. */
32533 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32534 {
32535 if (kind == OMP_CLAUSE_DEPEND
32536 && cp_parser_simulate_error (parser))
32537 goto depend_lvalue;
32538 goto skip_comma;
32539 }
32540 if (kind == OMP_CLAUSE_DEPEND)
32541 cp_parser_commit_to_tentative_parse (parser);
32542 if (!cp_lexer_next_token_is (parser->lexer,
32543 CPP_CLOSE_SQUARE))
32544 length = cp_parser_expression (parser);
32545 }
32546 /* Look for the closing `]'. */
32547 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
32548 RT_CLOSE_SQUARE))
32549 {
32550 if (kind == OMP_CLAUSE_DEPEND
32551 && cp_parser_simulate_error (parser))
32552 goto depend_lvalue;
32553 goto skip_comma;
32554 }
32555
32556 decl = tree_cons (low_bound, length, decl);
32557 }
32558 break;
32559 default:
32560 break;
32561 }
32562
32563 if (kind == OMP_CLAUSE_DEPEND)
32564 {
32565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
32566 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32567 && cp_parser_simulate_error (parser))
32568 {
32569 depend_lvalue:
32570 cp_parser_abort_tentative_parse (parser);
32571 decl = cp_parser_assignment_expression (parser, NULL,
32572 false, false);
32573 }
32574 else
32575 cp_parser_parse_definitely (parser);
32576 }
32577
32578 tree u = build_omp_clause (token->location, kind);
32579 OMP_CLAUSE_DECL (u) = decl;
32580 OMP_CLAUSE_CHAIN (u) = list;
32581 list = u;
32582 }
32583 else
32584 list = tree_cons (decl, NULL_TREE, list);
32585
32586 get_comma:
32587 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32588 break;
32589 cp_lexer_consume_token (parser->lexer);
32590 }
32591
32592 if (colon)
32593 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32594
32595 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32596 {
32597 *colon = true;
32598 cp_parser_require (parser, CPP_COLON, RT_COLON);
32599 return list;
32600 }
32601
32602 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32603 {
32604 int ending;
32605
32606 /* Try to resync to an unnested comma. Copied from
32607 cp_parser_parenthesized_expression_list. */
32608 skip_comma:
32609 if (colon)
32610 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32611 ending = cp_parser_skip_to_closing_parenthesis (parser,
32612 /*recovering=*/true,
32613 /*or_comma=*/true,
32614 /*consume_paren=*/true);
32615 if (ending < 0)
32616 goto get_comma;
32617 }
32618
32619 return list;
32620 }
32621
32622 /* Similarly, but expect leading and trailing parenthesis. This is a very
32623 common case for omp clauses. */
32624
32625 static tree
32626 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
32627 {
32628 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32629 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
32630 return list;
32631 }
32632
32633 /* OpenACC 2.0:
32634 copy ( variable-list )
32635 copyin ( variable-list )
32636 copyout ( variable-list )
32637 create ( variable-list )
32638 delete ( variable-list )
32639 present ( variable-list ) */
32640
32641 static tree
32642 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
32643 tree list)
32644 {
32645 enum gomp_map_kind kind;
32646 switch (c_kind)
32647 {
32648 case PRAGMA_OACC_CLAUSE_COPY:
32649 kind = GOMP_MAP_TOFROM;
32650 break;
32651 case PRAGMA_OACC_CLAUSE_COPYIN:
32652 kind = GOMP_MAP_TO;
32653 break;
32654 case PRAGMA_OACC_CLAUSE_COPYOUT:
32655 kind = GOMP_MAP_FROM;
32656 break;
32657 case PRAGMA_OACC_CLAUSE_CREATE:
32658 kind = GOMP_MAP_ALLOC;
32659 break;
32660 case PRAGMA_OACC_CLAUSE_DELETE:
32661 kind = GOMP_MAP_RELEASE;
32662 break;
32663 case PRAGMA_OACC_CLAUSE_DEVICE:
32664 kind = GOMP_MAP_FORCE_TO;
32665 break;
32666 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32667 kind = GOMP_MAP_DEVICE_RESIDENT;
32668 break;
32669 case PRAGMA_OACC_CLAUSE_HOST:
32670 kind = GOMP_MAP_FORCE_FROM;
32671 break;
32672 case PRAGMA_OACC_CLAUSE_LINK:
32673 kind = GOMP_MAP_LINK;
32674 break;
32675 case PRAGMA_OACC_CLAUSE_PRESENT:
32676 kind = GOMP_MAP_FORCE_PRESENT;
32677 break;
32678 default:
32679 gcc_unreachable ();
32680 }
32681 tree nl, c;
32682 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32683
32684 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32685 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32686
32687 return nl;
32688 }
32689
32690 /* OpenACC 2.0:
32691 deviceptr ( variable-list ) */
32692
32693 static tree
32694 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32695 {
32696 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32697 tree vars, t;
32698
32699 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32700 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32701 variable-list must only allow for pointer variables. */
32702 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32703 for (t = vars; t; t = TREE_CHAIN (t))
32704 {
32705 tree v = TREE_PURPOSE (t);
32706 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32707 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32708 OMP_CLAUSE_DECL (u) = v;
32709 OMP_CLAUSE_CHAIN (u) = list;
32710 list = u;
32711 }
32712
32713 return list;
32714 }
32715
32716 /* OpenACC 2.5:
32717 auto
32718 finalize
32719 independent
32720 nohost
32721 seq */
32722
32723 static tree
32724 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
32725 tree list)
32726 {
32727 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
32728
32729 tree c = build_omp_clause (loc, code);
32730 OMP_CLAUSE_CHAIN (c) = list;
32731
32732 return c;
32733 }
32734
32735 /* OpenACC:
32736 num_gangs ( expression )
32737 num_workers ( expression )
32738 vector_length ( expression ) */
32739
32740 static tree
32741 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32742 const char *str, tree list)
32743 {
32744 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32745
32746 matching_parens parens;
32747 if (!parens.require_open (parser))
32748 return list;
32749
32750 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32751
32752 if (t == error_mark_node
32753 || !parens.require_close (parser))
32754 {
32755 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32756 /*or_comma=*/false,
32757 /*consume_paren=*/true);
32758 return list;
32759 }
32760
32761 check_no_duplicate_clause (list, code, str, loc);
32762
32763 tree c = build_omp_clause (loc, code);
32764 OMP_CLAUSE_OPERAND (c, 0) = t;
32765 OMP_CLAUSE_CHAIN (c) = list;
32766 return c;
32767 }
32768
32769 /* OpenACC:
32770
32771 gang [( gang-arg-list )]
32772 worker [( [num:] int-expr )]
32773 vector [( [length:] int-expr )]
32774
32775 where gang-arg is one of:
32776
32777 [num:] int-expr
32778 static: size-expr
32779
32780 and size-expr may be:
32781
32782 *
32783 int-expr
32784 */
32785
32786 static tree
32787 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
32788 omp_clause_code kind,
32789 const char *str, tree list)
32790 {
32791 const char *id = "num";
32792 cp_lexer *lexer = parser->lexer;
32793 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32794
32795 if (kind == OMP_CLAUSE_VECTOR)
32796 id = "length";
32797
32798 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32799 {
32800 matching_parens parens;
32801 parens.consume_open (parser);
32802
32803 do
32804 {
32805 cp_token *next = cp_lexer_peek_token (lexer);
32806 int idx = 0;
32807
32808 /* Gang static argument. */
32809 if (kind == OMP_CLAUSE_GANG
32810 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32811 {
32812 cp_lexer_consume_token (lexer);
32813
32814 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32815 goto cleanup_error;
32816
32817 idx = 1;
32818 if (ops[idx] != NULL)
32819 {
32820 cp_parser_error (parser, "too many %<static%> arguments");
32821 goto cleanup_error;
32822 }
32823
32824 /* Check for the '*' argument. */
32825 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32826 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32827 || cp_lexer_nth_token_is (parser->lexer, 2,
32828 CPP_CLOSE_PAREN)))
32829 {
32830 cp_lexer_consume_token (lexer);
32831 ops[idx] = integer_minus_one_node;
32832
32833 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32834 {
32835 cp_lexer_consume_token (lexer);
32836 continue;
32837 }
32838 else break;
32839 }
32840 }
32841 /* Worker num: argument and vector length: arguments. */
32842 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32843 && id_equal (next->u.value, id)
32844 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32845 {
32846 cp_lexer_consume_token (lexer); /* id */
32847 cp_lexer_consume_token (lexer); /* ':' */
32848 }
32849
32850 /* Now collect the actual argument. */
32851 if (ops[idx] != NULL_TREE)
32852 {
32853 cp_parser_error (parser, "unexpected argument");
32854 goto cleanup_error;
32855 }
32856
32857 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32858 false);
32859 if (expr == error_mark_node)
32860 goto cleanup_error;
32861
32862 mark_exp_read (expr);
32863 ops[idx] = expr;
32864
32865 if (kind == OMP_CLAUSE_GANG
32866 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32867 {
32868 cp_lexer_consume_token (lexer);
32869 continue;
32870 }
32871 break;
32872 }
32873 while (1);
32874
32875 if (!parens.require_close (parser))
32876 goto cleanup_error;
32877 }
32878
32879 check_no_duplicate_clause (list, kind, str, loc);
32880
32881 c = build_omp_clause (loc, kind);
32882
32883 if (ops[1])
32884 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32885
32886 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32887 OMP_CLAUSE_CHAIN (c) = list;
32888
32889 return c;
32890
32891 cleanup_error:
32892 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32893 return list;
32894 }
32895
32896 /* OpenACC 2.0:
32897 tile ( size-expr-list ) */
32898
32899 static tree
32900 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32901 {
32902 tree c, expr = error_mark_node;
32903 tree tile = NULL_TREE;
32904
32905 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32906 so, but the spec authors never considered such a case and have
32907 differing opinions on what it might mean, including 'not
32908 allowed'.) */
32909 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32910 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32911 clause_loc);
32912
32913 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32914 return list;
32915
32916 do
32917 {
32918 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32919 return list;
32920
32921 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32922 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32923 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32924 {
32925 cp_lexer_consume_token (parser->lexer);
32926 expr = integer_zero_node;
32927 }
32928 else
32929 expr = cp_parser_constant_expression (parser);
32930
32931 tile = tree_cons (NULL_TREE, expr, tile);
32932 }
32933 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32934
32935 /* Consume the trailing ')'. */
32936 cp_lexer_consume_token (parser->lexer);
32937
32938 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32939 tile = nreverse (tile);
32940 OMP_CLAUSE_TILE_LIST (c) = tile;
32941 OMP_CLAUSE_CHAIN (c) = list;
32942 return c;
32943 }
32944
32945 /* OpenACC 2.0
32946 Parse wait clause or directive parameters. */
32947
32948 static tree
32949 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32950 {
32951 vec<tree, va_gc> *args;
32952 tree t, args_tree;
32953
32954 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32955 /*cast_p=*/false,
32956 /*allow_expansion_p=*/true,
32957 /*non_constant_p=*/NULL);
32958
32959 if (args == NULL || args->length () == 0)
32960 {
32961 if (args != NULL)
32962 {
32963 cp_parser_error (parser, "expected integer expression list");
32964 release_tree_vector (args);
32965 }
32966 return list;
32967 }
32968
32969 args_tree = build_tree_list_vec (args);
32970
32971 release_tree_vector (args);
32972
32973 for (t = args_tree; t; t = TREE_CHAIN (t))
32974 {
32975 tree targ = TREE_VALUE (t);
32976
32977 if (targ != error_mark_node)
32978 {
32979 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32980 error ("%<wait%> expression must be integral");
32981 else
32982 {
32983 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32984
32985 targ = mark_rvalue_use (targ);
32986 OMP_CLAUSE_DECL (c) = targ;
32987 OMP_CLAUSE_CHAIN (c) = list;
32988 list = c;
32989 }
32990 }
32991 }
32992
32993 return list;
32994 }
32995
32996 /* OpenACC:
32997 wait [( int-expr-list )] */
32998
32999 static tree
33000 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
33001 {
33002 location_t location = cp_lexer_peek_token (parser->lexer)->location;
33003
33004 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33005 list = cp_parser_oacc_wait_list (parser, location, list);
33006 else
33007 {
33008 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
33009
33010 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33011 OMP_CLAUSE_CHAIN (c) = list;
33012 list = c;
33013 }
33014
33015 return list;
33016 }
33017
33018 /* OpenMP 3.0:
33019 collapse ( constant-expression ) */
33020
33021 static tree
33022 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
33023 {
33024 tree c, num;
33025 location_t loc;
33026 HOST_WIDE_INT n;
33027
33028 loc = cp_lexer_peek_token (parser->lexer)->location;
33029 matching_parens parens;
33030 if (!parens.require_open (parser))
33031 return list;
33032
33033 num = cp_parser_constant_expression (parser);
33034
33035 if (!parens.require_close (parser))
33036 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33037 /*or_comma=*/false,
33038 /*consume_paren=*/true);
33039
33040 if (num == error_mark_node)
33041 return list;
33042 num = fold_non_dependent_expr (num);
33043 if (!tree_fits_shwi_p (num)
33044 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33045 || (n = tree_to_shwi (num)) <= 0
33046 || (int) n != n)
33047 {
33048 error_at (loc, "collapse argument needs positive constant integer expression");
33049 return list;
33050 }
33051
33052 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
33053 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
33054 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
33055 OMP_CLAUSE_CHAIN (c) = list;
33056 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
33057
33058 return c;
33059 }
33060
33061 /* OpenMP 2.5:
33062 default ( none | shared )
33063
33064 OpenACC:
33065 default ( none | present ) */
33066
33067 static tree
33068 cp_parser_omp_clause_default (cp_parser *parser, tree list,
33069 location_t location, bool is_oacc)
33070 {
33071 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
33072 tree c;
33073
33074 matching_parens parens;
33075 if (!parens.require_open (parser))
33076 return list;
33077 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33078 {
33079 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33080 const char *p = IDENTIFIER_POINTER (id);
33081
33082 switch (p[0])
33083 {
33084 case 'n':
33085 if (strcmp ("none", p) != 0)
33086 goto invalid_kind;
33087 kind = OMP_CLAUSE_DEFAULT_NONE;
33088 break;
33089
33090 case 'p':
33091 if (strcmp ("present", p) != 0 || !is_oacc)
33092 goto invalid_kind;
33093 kind = OMP_CLAUSE_DEFAULT_PRESENT;
33094 break;
33095
33096 case 's':
33097 if (strcmp ("shared", p) != 0 || is_oacc)
33098 goto invalid_kind;
33099 kind = OMP_CLAUSE_DEFAULT_SHARED;
33100 break;
33101
33102 default:
33103 goto invalid_kind;
33104 }
33105
33106 cp_lexer_consume_token (parser->lexer);
33107 }
33108 else
33109 {
33110 invalid_kind:
33111 if (is_oacc)
33112 cp_parser_error (parser, "expected %<none%> or %<present%>");
33113 else
33114 cp_parser_error (parser, "expected %<none%> or %<shared%>");
33115 }
33116
33117 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
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 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
33124 return list;
33125
33126 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
33127 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
33128 OMP_CLAUSE_CHAIN (c) = list;
33129 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
33130
33131 return c;
33132 }
33133
33134 /* OpenMP 3.1:
33135 final ( expression ) */
33136
33137 static tree
33138 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
33139 {
33140 tree t, c;
33141
33142 matching_parens parens;
33143 if (!parens.require_open (parser))
33144 return list;
33145
33146 t = cp_parser_assignment_expression (parser);
33147
33148 if (t == error_mark_node
33149 || !parens.require_close (parser))
33150 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33151 /*or_comma=*/false,
33152 /*consume_paren=*/true);
33153
33154 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
33155
33156 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
33157 OMP_CLAUSE_FINAL_EXPR (c) = t;
33158 OMP_CLAUSE_CHAIN (c) = list;
33159
33160 return c;
33161 }
33162
33163 /* OpenMP 2.5:
33164 if ( expression )
33165
33166 OpenMP 4.5:
33167 if ( directive-name-modifier : expression )
33168
33169 directive-name-modifier:
33170 parallel | task | taskloop | target data | target | target update
33171 | target enter data | target exit data
33172
33173 OpenMP 5.0:
33174 directive-name-modifier:
33175 ... | simd | cancel */
33176
33177 static tree
33178 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
33179 bool is_omp)
33180 {
33181 tree t, c;
33182 enum tree_code if_modifier = ERROR_MARK;
33183
33184 matching_parens parens;
33185 if (!parens.require_open (parser))
33186 return list;
33187
33188 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33189 {
33190 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33191 const char *p = IDENTIFIER_POINTER (id);
33192 int n = 2;
33193
33194 if (strcmp ("cancel", p) == 0)
33195 if_modifier = VOID_CST;
33196 else if (strcmp ("parallel", p) == 0)
33197 if_modifier = OMP_PARALLEL;
33198 else if (strcmp ("simd", p) == 0)
33199 if_modifier = OMP_SIMD;
33200 else if (strcmp ("task", p) == 0)
33201 if_modifier = OMP_TASK;
33202 else if (strcmp ("taskloop", p) == 0)
33203 if_modifier = OMP_TASKLOOP;
33204 else if (strcmp ("target", p) == 0)
33205 {
33206 if_modifier = OMP_TARGET;
33207 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
33208 {
33209 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
33210 p = IDENTIFIER_POINTER (id);
33211 if (strcmp ("data", p) == 0)
33212 if_modifier = OMP_TARGET_DATA;
33213 else if (strcmp ("update", p) == 0)
33214 if_modifier = OMP_TARGET_UPDATE;
33215 else if (strcmp ("enter", p) == 0)
33216 if_modifier = OMP_TARGET_ENTER_DATA;
33217 else if (strcmp ("exit", p) == 0)
33218 if_modifier = OMP_TARGET_EXIT_DATA;
33219 if (if_modifier != OMP_TARGET)
33220 n = 3;
33221 else
33222 {
33223 location_t loc
33224 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
33225 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
33226 "or %<exit%>");
33227 if_modifier = ERROR_MARK;
33228 }
33229 if (if_modifier == OMP_TARGET_ENTER_DATA
33230 || if_modifier == OMP_TARGET_EXIT_DATA)
33231 {
33232 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
33233 {
33234 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
33235 p = IDENTIFIER_POINTER (id);
33236 if (strcmp ("data", p) == 0)
33237 n = 4;
33238 }
33239 if (n != 4)
33240 {
33241 location_t loc
33242 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
33243 error_at (loc, "expected %<data%>");
33244 if_modifier = ERROR_MARK;
33245 }
33246 }
33247 }
33248 }
33249 if (if_modifier != ERROR_MARK)
33250 {
33251 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
33252 {
33253 while (n-- > 0)
33254 cp_lexer_consume_token (parser->lexer);
33255 }
33256 else
33257 {
33258 if (n > 2)
33259 {
33260 location_t loc
33261 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
33262 error_at (loc, "expected %<:%>");
33263 }
33264 if_modifier = ERROR_MARK;
33265 }
33266 }
33267 }
33268
33269 t = cp_parser_assignment_expression (parser);
33270
33271 if (t == error_mark_node
33272 || !parens.require_close (parser))
33273 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33274 /*or_comma=*/false,
33275 /*consume_paren=*/true);
33276
33277 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33278 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
33279 {
33280 if (if_modifier != ERROR_MARK
33281 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33282 {
33283 const char *p = NULL;
33284 switch (if_modifier)
33285 {
33286 case VOID_CST: p = "cancel"; break;
33287 case OMP_PARALLEL: p = "parallel"; break;
33288 case OMP_SIMD: p = "simd"; break;
33289 case OMP_TASK: p = "task"; break;
33290 case OMP_TASKLOOP: p = "taskloop"; break;
33291 case OMP_TARGET_DATA: p = "target data"; break;
33292 case OMP_TARGET: p = "target"; break;
33293 case OMP_TARGET_UPDATE: p = "target update"; break;
33294 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
33295 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
33296 default: gcc_unreachable ();
33297 }
33298 error_at (location, "too many %<if%> clauses with %qs modifier",
33299 p);
33300 return list;
33301 }
33302 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33303 {
33304 if (!is_omp)
33305 error_at (location, "too many %<if%> clauses");
33306 else
33307 error_at (location, "too many %<if%> clauses without modifier");
33308 return list;
33309 }
33310 else if (if_modifier == ERROR_MARK
33311 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
33312 {
33313 error_at (location, "if any %<if%> clause has modifier, then all "
33314 "%<if%> clauses have to use modifier");
33315 return list;
33316 }
33317 }
33318
33319 c = build_omp_clause (location, OMP_CLAUSE_IF);
33320 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
33321 OMP_CLAUSE_IF_EXPR (c) = t;
33322 OMP_CLAUSE_CHAIN (c) = list;
33323
33324 return c;
33325 }
33326
33327 /* OpenMP 3.1:
33328 mergeable */
33329
33330 static tree
33331 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
33332 tree list, location_t location)
33333 {
33334 tree c;
33335
33336 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
33337 location);
33338
33339 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
33340 OMP_CLAUSE_CHAIN (c) = list;
33341 return c;
33342 }
33343
33344 /* OpenMP 2.5:
33345 nowait */
33346
33347 static tree
33348 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
33349 tree list, location_t location)
33350 {
33351 tree c;
33352
33353 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
33354
33355 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
33356 OMP_CLAUSE_CHAIN (c) = list;
33357 return c;
33358 }
33359
33360 /* OpenMP 2.5:
33361 num_threads ( expression ) */
33362
33363 static tree
33364 cp_parser_omp_clause_num_threads (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_THREADS,
33382 "num_threads", location);
33383
33384 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
33385 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
33386 OMP_CLAUSE_CHAIN (c) = list;
33387
33388 return c;
33389 }
33390
33391 /* OpenMP 4.5:
33392 num_tasks ( expression ) */
33393
33394 static tree
33395 cp_parser_omp_clause_num_tasks (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_NUM_TASKS,
33413 "num_tasks", location);
33414
33415 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
33416 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
33417 OMP_CLAUSE_CHAIN (c) = list;
33418
33419 return c;
33420 }
33421
33422 /* OpenMP 4.5:
33423 grainsize ( expression ) */
33424
33425 static tree
33426 cp_parser_omp_clause_grainsize (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_GRAINSIZE,
33444 "grainsize", location);
33445
33446 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
33447 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
33448 OMP_CLAUSE_CHAIN (c) = list;
33449
33450 return c;
33451 }
33452
33453 /* OpenMP 4.5:
33454 priority ( expression ) */
33455
33456 static tree
33457 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
33458 location_t location)
33459 {
33460 tree t, c;
33461
33462 matching_parens parens;
33463 if (!parens.require_open (parser))
33464 return list;
33465
33466 t = cp_parser_assignment_expression (parser);
33467
33468 if (t == error_mark_node
33469 || !parens.require_close (parser))
33470 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33471 /*or_comma=*/false,
33472 /*consume_paren=*/true);
33473
33474 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
33475 "priority", location);
33476
33477 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
33478 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
33479 OMP_CLAUSE_CHAIN (c) = list;
33480
33481 return c;
33482 }
33483
33484 /* OpenMP 4.5:
33485 hint ( expression ) */
33486
33487 static tree
33488 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
33489 {
33490 tree t, c;
33491
33492 matching_parens parens;
33493 if (!parens.require_open (parser))
33494 return list;
33495
33496 t = cp_parser_assignment_expression (parser);
33497
33498 if (t == error_mark_node
33499 || !parens.require_close (parser))
33500 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33501 /*or_comma=*/false,
33502 /*consume_paren=*/true);
33503
33504 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
33505
33506 c = build_omp_clause (location, OMP_CLAUSE_HINT);
33507 OMP_CLAUSE_HINT_EXPR (c) = t;
33508 OMP_CLAUSE_CHAIN (c) = list;
33509
33510 return c;
33511 }
33512
33513 /* OpenMP 4.5:
33514 defaultmap ( tofrom : scalar )
33515
33516 OpenMP 5.0:
33517 defaultmap ( implicit-behavior [ : variable-category ] ) */
33518
33519 static tree
33520 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
33521 location_t location)
33522 {
33523 tree c, id;
33524 const char *p;
33525 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33526 enum omp_clause_defaultmap_kind category
33527 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
33528
33529 matching_parens parens;
33530 if (!parens.require_open (parser))
33531 return list;
33532
33533 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
33534 p = "default";
33535 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33536 {
33537 invalid_behavior:
33538 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
33539 "%<tofrom%>, %<firstprivate%>, %<none%> "
33540 "or %<default%>");
33541 goto out_err;
33542 }
33543 else
33544 {
33545 id = cp_lexer_peek_token (parser->lexer)->u.value;
33546 p = IDENTIFIER_POINTER (id);
33547 }
33548
33549 switch (p[0])
33550 {
33551 case 'a':
33552 if (strcmp ("alloc", p) == 0)
33553 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
33554 else
33555 goto invalid_behavior;
33556 break;
33557
33558 case 'd':
33559 if (strcmp ("default", p) == 0)
33560 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33561 else
33562 goto invalid_behavior;
33563 break;
33564
33565 case 'f':
33566 if (strcmp ("firstprivate", p) == 0)
33567 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
33568 else if (strcmp ("from", p) == 0)
33569 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
33570 else
33571 goto invalid_behavior;
33572 break;
33573
33574 case 'n':
33575 if (strcmp ("none", p) == 0)
33576 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
33577 else
33578 goto invalid_behavior;
33579 break;
33580
33581 case 't':
33582 if (strcmp ("tofrom", p) == 0)
33583 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
33584 else if (strcmp ("to", p) == 0)
33585 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
33586 else
33587 goto invalid_behavior;
33588 break;
33589
33590 default:
33591 goto invalid_behavior;
33592 }
33593 cp_lexer_consume_token (parser->lexer);
33594
33595 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33596 {
33597 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33598 goto out_err;
33599
33600 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33601 {
33602 invalid_category:
33603 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
33604 "%<pointer%>");
33605 goto out_err;
33606 }
33607 id = cp_lexer_peek_token (parser->lexer)->u.value;
33608 p = IDENTIFIER_POINTER (id);
33609
33610 switch (p[0])
33611 {
33612 case 'a':
33613 if (strcmp ("aggregate", p) == 0)
33614 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
33615 else
33616 goto invalid_category;
33617 break;
33618
33619 case 'p':
33620 if (strcmp ("pointer", p) == 0)
33621 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
33622 else
33623 goto invalid_category;
33624 break;
33625
33626 case 's':
33627 if (strcmp ("scalar", p) == 0)
33628 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
33629 else
33630 goto invalid_category;
33631 break;
33632
33633 default:
33634 goto invalid_category;
33635 }
33636
33637 cp_lexer_consume_token (parser->lexer);
33638 }
33639 if (!parens.require_close (parser))
33640 goto out_err;
33641
33642 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33643 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
33644 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33645 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
33646 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
33647 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
33648 {
33649 enum omp_clause_defaultmap_kind cat = category;
33650 location_t loc = OMP_CLAUSE_LOCATION (c);
33651 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
33652 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
33653 p = NULL;
33654 switch (cat)
33655 {
33656 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
33657 p = NULL;
33658 break;
33659 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33660 p = "aggregate";
33661 break;
33662 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33663 p = "pointer";
33664 break;
33665 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33666 p = "scalar";
33667 break;
33668 default:
33669 gcc_unreachable ();
33670 }
33671 if (p)
33672 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33673 p);
33674 else
33675 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33676 "category");
33677 break;
33678 }
33679
33680 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33681 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33682 OMP_CLAUSE_CHAIN (c) = list;
33683 return c;
33684
33685 out_err:
33686 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33687 /*or_comma=*/false,
33688 /*consume_paren=*/true);
33689 return list;
33690 }
33691
33692 /* OpenMP 2.5:
33693 ordered
33694
33695 OpenMP 4.5:
33696 ordered ( constant-expression ) */
33697
33698 static tree
33699 cp_parser_omp_clause_ordered (cp_parser *parser,
33700 tree list, location_t location)
33701 {
33702 tree c, num = NULL_TREE;
33703 HOST_WIDE_INT n;
33704
33705 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33706 "ordered", location);
33707
33708 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33709 {
33710 matching_parens parens;
33711 parens.consume_open (parser);
33712
33713 num = cp_parser_constant_expression (parser);
33714
33715 if (!parens.require_close (parser))
33716 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33717 /*or_comma=*/false,
33718 /*consume_paren=*/true);
33719
33720 if (num == error_mark_node)
33721 return list;
33722 num = fold_non_dependent_expr (num);
33723 if (!tree_fits_shwi_p (num)
33724 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33725 || (n = tree_to_shwi (num)) <= 0
33726 || (int) n != n)
33727 {
33728 error_at (location,
33729 "ordered argument needs positive constant integer "
33730 "expression");
33731 return list;
33732 }
33733 }
33734
33735 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33736 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33737 OMP_CLAUSE_CHAIN (c) = list;
33738 return c;
33739 }
33740
33741 /* OpenMP 2.5:
33742 reduction ( reduction-operator : variable-list )
33743
33744 reduction-operator:
33745 One of: + * - & ^ | && ||
33746
33747 OpenMP 3.1:
33748
33749 reduction-operator:
33750 One of: + * - & ^ | && || min max
33751
33752 OpenMP 4.0:
33753
33754 reduction-operator:
33755 One of: + * - & ^ | && ||
33756 id-expression
33757
33758 OpenMP 5.0:
33759 reduction ( reduction-modifier, reduction-operator : variable-list )
33760 in_reduction ( reduction-operator : variable-list )
33761 task_reduction ( reduction-operator : variable-list ) */
33762
33763 static tree
33764 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33765 bool is_omp, tree list)
33766 {
33767 enum tree_code code = ERROR_MARK;
33768 tree nlist, c, id = NULL_TREE;
33769 bool task = false;
33770 bool inscan = false;
33771
33772 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33773 return list;
33774
33775 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33776 {
33777 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33778 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33779 {
33780 cp_lexer_consume_token (parser->lexer);
33781 cp_lexer_consume_token (parser->lexer);
33782 }
33783 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33784 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33785 {
33786 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33787 const char *p = IDENTIFIER_POINTER (id);
33788 if (strcmp (p, "task") == 0)
33789 task = true;
33790 else if (strcmp (p, "inscan") == 0)
33791 {
33792 inscan = true;
33793 sorry ("%<inscan%> modifier on %<reduction%> clause "
33794 "not supported yet");
33795 }
33796 if (task || inscan)
33797 {
33798 cp_lexer_consume_token (parser->lexer);
33799 cp_lexer_consume_token (parser->lexer);
33800 }
33801 }
33802 }
33803
33804 switch (cp_lexer_peek_token (parser->lexer)->type)
33805 {
33806 case CPP_PLUS: code = PLUS_EXPR; break;
33807 case CPP_MULT: code = MULT_EXPR; break;
33808 case CPP_MINUS: code = MINUS_EXPR; break;
33809 case CPP_AND: code = BIT_AND_EXPR; break;
33810 case CPP_XOR: code = BIT_XOR_EXPR; break;
33811 case CPP_OR: code = BIT_IOR_EXPR; break;
33812 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33813 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33814 default: break;
33815 }
33816
33817 if (code != ERROR_MARK)
33818 cp_lexer_consume_token (parser->lexer);
33819 else
33820 {
33821 bool saved_colon_corrects_to_scope_p;
33822 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33823 parser->colon_corrects_to_scope_p = false;
33824 id = cp_parser_id_expression (parser, /*template_p=*/false,
33825 /*check_dependency_p=*/true,
33826 /*template_p=*/NULL,
33827 /*declarator_p=*/false,
33828 /*optional_p=*/false);
33829 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33830 if (identifier_p (id))
33831 {
33832 const char *p = IDENTIFIER_POINTER (id);
33833
33834 if (strcmp (p, "min") == 0)
33835 code = MIN_EXPR;
33836 else if (strcmp (p, "max") == 0)
33837 code = MAX_EXPR;
33838 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33839 code = PLUS_EXPR;
33840 else if (id == ovl_op_identifier (false, MULT_EXPR))
33841 code = MULT_EXPR;
33842 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33843 code = MINUS_EXPR;
33844 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33845 code = BIT_AND_EXPR;
33846 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33847 code = BIT_IOR_EXPR;
33848 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33849 code = BIT_XOR_EXPR;
33850 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33851 code = TRUTH_ANDIF_EXPR;
33852 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33853 code = TRUTH_ORIF_EXPR;
33854 id = omp_reduction_id (code, id, NULL_TREE);
33855 tree scope = parser->scope;
33856 if (scope)
33857 id = build_qualified_name (NULL_TREE, scope, id, false);
33858 parser->scope = NULL_TREE;
33859 parser->qualifying_scope = NULL_TREE;
33860 parser->object_scope = NULL_TREE;
33861 }
33862 else
33863 {
33864 error ("invalid reduction-identifier");
33865 resync_fail:
33866 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33867 /*or_comma=*/false,
33868 /*consume_paren=*/true);
33869 return list;
33870 }
33871 }
33872
33873 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33874 goto resync_fail;
33875
33876 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33877 NULL);
33878 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33879 {
33880 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33881 if (task)
33882 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33883 else if (inscan)
33884 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33885 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33886 }
33887
33888 return nlist;
33889 }
33890
33891 /* OpenMP 2.5:
33892 schedule ( schedule-kind )
33893 schedule ( schedule-kind , expression )
33894
33895 schedule-kind:
33896 static | dynamic | guided | runtime | auto
33897
33898 OpenMP 4.5:
33899 schedule ( schedule-modifier : schedule-kind )
33900 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33901
33902 schedule-modifier:
33903 simd
33904 monotonic
33905 nonmonotonic */
33906
33907 static tree
33908 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33909 {
33910 tree c, t;
33911 int modifiers = 0, nmodifiers = 0;
33912
33913 matching_parens parens;
33914 if (!parens.require_open (parser))
33915 return list;
33916
33917 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33918
33919 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33920 {
33921 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33922 const char *p = IDENTIFIER_POINTER (id);
33923 if (strcmp ("simd", p) == 0)
33924 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33925 else if (strcmp ("monotonic", p) == 0)
33926 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33927 else if (strcmp ("nonmonotonic", p) == 0)
33928 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33929 else
33930 break;
33931 cp_lexer_consume_token (parser->lexer);
33932 if (nmodifiers++ == 0
33933 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33934 cp_lexer_consume_token (parser->lexer);
33935 else
33936 {
33937 cp_parser_require (parser, CPP_COLON, RT_COLON);
33938 break;
33939 }
33940 }
33941
33942 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33943 {
33944 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33945 const char *p = IDENTIFIER_POINTER (id);
33946
33947 switch (p[0])
33948 {
33949 case 'd':
33950 if (strcmp ("dynamic", p) != 0)
33951 goto invalid_kind;
33952 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33953 break;
33954
33955 case 'g':
33956 if (strcmp ("guided", p) != 0)
33957 goto invalid_kind;
33958 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33959 break;
33960
33961 case 'r':
33962 if (strcmp ("runtime", p) != 0)
33963 goto invalid_kind;
33964 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33965 break;
33966
33967 default:
33968 goto invalid_kind;
33969 }
33970 }
33971 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33972 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33973 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33974 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33975 else
33976 goto invalid_kind;
33977 cp_lexer_consume_token (parser->lexer);
33978
33979 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33980 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33981 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33982 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33983 {
33984 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33985 "specified");
33986 modifiers = 0;
33987 }
33988
33989 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33990 {
33991 cp_token *token;
33992 cp_lexer_consume_token (parser->lexer);
33993
33994 token = cp_lexer_peek_token (parser->lexer);
33995 t = cp_parser_assignment_expression (parser);
33996
33997 if (t == error_mark_node)
33998 goto resync_fail;
33999 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
34000 error_at (token->location, "schedule %<runtime%> does not take "
34001 "a %<chunk_size%> parameter");
34002 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
34003 error_at (token->location, "schedule %<auto%> does not take "
34004 "a %<chunk_size%> parameter");
34005 else
34006 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
34007
34008 if (!parens.require_close (parser))
34009 goto resync_fail;
34010 }
34011 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34012 goto resync_fail;
34013
34014 OMP_CLAUSE_SCHEDULE_KIND (c)
34015 = (enum omp_clause_schedule_kind)
34016 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
34017
34018 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
34019 OMP_CLAUSE_CHAIN (c) = list;
34020 return c;
34021
34022 invalid_kind:
34023 cp_parser_error (parser, "invalid schedule kind");
34024 resync_fail:
34025 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34026 /*or_comma=*/false,
34027 /*consume_paren=*/true);
34028 return list;
34029 }
34030
34031 /* OpenMP 3.0:
34032 untied */
34033
34034 static tree
34035 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
34036 tree list, location_t location)
34037 {
34038 tree c;
34039
34040 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
34041
34042 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
34043 OMP_CLAUSE_CHAIN (c) = list;
34044 return c;
34045 }
34046
34047 /* OpenMP 4.0:
34048 inbranch
34049 notinbranch */
34050
34051 static tree
34052 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
34053 tree list, location_t location)
34054 {
34055 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
34056 tree c = build_omp_clause (location, code);
34057 OMP_CLAUSE_CHAIN (c) = list;
34058 return c;
34059 }
34060
34061 /* OpenMP 4.0:
34062 parallel
34063 for
34064 sections
34065 taskgroup */
34066
34067 static tree
34068 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
34069 enum omp_clause_code code,
34070 tree list, location_t location)
34071 {
34072 tree c = build_omp_clause (location, code);
34073 OMP_CLAUSE_CHAIN (c) = list;
34074 return c;
34075 }
34076
34077 /* OpenMP 4.5:
34078 nogroup */
34079
34080 static tree
34081 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
34082 tree list, location_t location)
34083 {
34084 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
34085 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
34086 OMP_CLAUSE_CHAIN (c) = list;
34087 return c;
34088 }
34089
34090 /* OpenMP 4.5:
34091 simd
34092 threads */
34093
34094 static tree
34095 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
34096 enum omp_clause_code code,
34097 tree list, location_t location)
34098 {
34099 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
34100 tree c = build_omp_clause (location, code);
34101 OMP_CLAUSE_CHAIN (c) = list;
34102 return c;
34103 }
34104
34105 /* OpenMP 4.0:
34106 num_teams ( expression ) */
34107
34108 static tree
34109 cp_parser_omp_clause_num_teams (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_NUM_TEAMS,
34127 "num_teams", location);
34128
34129 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
34130 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
34131 OMP_CLAUSE_CHAIN (c) = list;
34132
34133 return c;
34134 }
34135
34136 /* OpenMP 4.0:
34137 thread_limit ( expression ) */
34138
34139 static tree
34140 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
34141 location_t location)
34142 {
34143 tree t, c;
34144
34145 matching_parens parens;
34146 if (!parens.require_open (parser))
34147 return list;
34148
34149 t = cp_parser_assignment_expression (parser);
34150
34151 if (t == error_mark_node
34152 || !parens.require_close (parser))
34153 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34154 /*or_comma=*/false,
34155 /*consume_paren=*/true);
34156
34157 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
34158 "thread_limit", location);
34159
34160 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
34161 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
34162 OMP_CLAUSE_CHAIN (c) = list;
34163
34164 return c;
34165 }
34166
34167 /* OpenMP 4.0:
34168 aligned ( variable-list )
34169 aligned ( variable-list : constant-expression ) */
34170
34171 static tree
34172 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
34173 {
34174 tree nlist, c, alignment = NULL_TREE;
34175 bool colon;
34176
34177 matching_parens parens;
34178 if (!parens.require_open (parser))
34179 return list;
34180
34181 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
34182 &colon);
34183
34184 if (colon)
34185 {
34186 alignment = cp_parser_constant_expression (parser);
34187
34188 if (!parens.require_close (parser))
34189 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34190 /*or_comma=*/false,
34191 /*consume_paren=*/true);
34192
34193 if (alignment == error_mark_node)
34194 alignment = NULL_TREE;
34195 }
34196
34197 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34198 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
34199
34200 return nlist;
34201 }
34202
34203 /* OpenMP 2.5:
34204 lastprivate ( variable-list )
34205
34206 OpenMP 5.0:
34207 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
34208
34209 static tree
34210 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
34211 {
34212 bool conditional = false;
34213
34214 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34215 return list;
34216
34217 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34218 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
34219 {
34220 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34221 const char *p = IDENTIFIER_POINTER (id);
34222
34223 if (strcmp ("conditional", p) == 0)
34224 {
34225 conditional = true;
34226 cp_lexer_consume_token (parser->lexer);
34227 cp_lexer_consume_token (parser->lexer);
34228 }
34229 }
34230
34231 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
34232 list, NULL);
34233
34234 if (conditional)
34235 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34236 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
34237 return nlist;
34238 }
34239
34240 /* OpenMP 4.0:
34241 linear ( variable-list )
34242 linear ( variable-list : expression )
34243
34244 OpenMP 4.5:
34245 linear ( modifier ( variable-list ) )
34246 linear ( modifier ( variable-list ) : expression ) */
34247
34248 static tree
34249 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
34250 bool declare_simd)
34251 {
34252 tree nlist, c, step = integer_one_node;
34253 bool colon;
34254 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
34255
34256 matching_parens parens;
34257 if (!parens.require_open (parser))
34258 return list;
34259
34260 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34261 {
34262 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34263 const char *p = IDENTIFIER_POINTER (id);
34264
34265 if (strcmp ("ref", p) == 0)
34266 kind = OMP_CLAUSE_LINEAR_REF;
34267 else if (strcmp ("val", p) == 0)
34268 kind = OMP_CLAUSE_LINEAR_VAL;
34269 else if (strcmp ("uval", p) == 0)
34270 kind = OMP_CLAUSE_LINEAR_UVAL;
34271 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
34272 cp_lexer_consume_token (parser->lexer);
34273 else
34274 kind = OMP_CLAUSE_LINEAR_DEFAULT;
34275 }
34276
34277 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
34278 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
34279 &colon);
34280 else
34281 {
34282 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
34283 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
34284 if (colon)
34285 cp_parser_require (parser, CPP_COLON, RT_COLON);
34286 else if (!parens.require_close (parser))
34287 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34288 /*or_comma=*/false,
34289 /*consume_paren=*/true);
34290 }
34291
34292 if (colon)
34293 {
34294 step = NULL_TREE;
34295 if (declare_simd
34296 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34297 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
34298 {
34299 cp_token *token = cp_lexer_peek_token (parser->lexer);
34300 cp_parser_parse_tentatively (parser);
34301 step = cp_parser_id_expression (parser, /*template_p=*/false,
34302 /*check_dependency_p=*/true,
34303 /*template_p=*/NULL,
34304 /*declarator_p=*/false,
34305 /*optional_p=*/false);
34306 if (step != error_mark_node)
34307 step = cp_parser_lookup_name_simple (parser, step, token->location);
34308 if (step == error_mark_node)
34309 {
34310 step = NULL_TREE;
34311 cp_parser_abort_tentative_parse (parser);
34312 }
34313 else if (!cp_parser_parse_definitely (parser))
34314 step = NULL_TREE;
34315 }
34316 if (!step)
34317 step = cp_parser_assignment_expression (parser);
34318
34319 if (!parens.require_close (parser))
34320 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34321 /*or_comma=*/false,
34322 /*consume_paren=*/true);
34323
34324 if (step == error_mark_node)
34325 return list;
34326 }
34327
34328 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34329 {
34330 OMP_CLAUSE_LINEAR_STEP (c) = step;
34331 OMP_CLAUSE_LINEAR_KIND (c) = kind;
34332 }
34333
34334 return nlist;
34335 }
34336
34337 /* OpenMP 4.0:
34338 safelen ( constant-expression ) */
34339
34340 static tree
34341 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
34342 location_t location)
34343 {
34344 tree t, c;
34345
34346 matching_parens parens;
34347 if (!parens.require_open (parser))
34348 return list;
34349
34350 t = cp_parser_constant_expression (parser);
34351
34352 if (t == error_mark_node
34353 || !parens.require_close (parser))
34354 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34355 /*or_comma=*/false,
34356 /*consume_paren=*/true);
34357
34358 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
34359
34360 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
34361 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
34362 OMP_CLAUSE_CHAIN (c) = list;
34363
34364 return c;
34365 }
34366
34367 /* OpenMP 4.0:
34368 simdlen ( constant-expression ) */
34369
34370 static tree
34371 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
34372 location_t location)
34373 {
34374 tree t, c;
34375
34376 matching_parens parens;
34377 if (!parens.require_open (parser))
34378 return list;
34379
34380 t = cp_parser_constant_expression (parser);
34381
34382 if (t == error_mark_node
34383 || !parens.require_close (parser))
34384 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34385 /*or_comma=*/false,
34386 /*consume_paren=*/true);
34387
34388 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
34389
34390 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
34391 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
34392 OMP_CLAUSE_CHAIN (c) = list;
34393
34394 return c;
34395 }
34396
34397 /* OpenMP 4.5:
34398 vec:
34399 identifier [+/- integer]
34400 vec , identifier [+/- integer]
34401 */
34402
34403 static tree
34404 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
34405 tree list)
34406 {
34407 tree vec = NULL;
34408
34409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34410 {
34411 cp_parser_error (parser, "expected identifier");
34412 return list;
34413 }
34414
34415 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34416 {
34417 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
34418 tree t, identifier = cp_parser_identifier (parser);
34419 tree addend = NULL;
34420
34421 if (identifier == error_mark_node)
34422 t = error_mark_node;
34423 else
34424 {
34425 t = cp_parser_lookup_name_simple
34426 (parser, identifier,
34427 cp_lexer_peek_token (parser->lexer)->location);
34428 if (t == error_mark_node)
34429 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
34430 id_loc);
34431 }
34432
34433 bool neg = false;
34434 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
34435 neg = true;
34436 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
34437 {
34438 addend = integer_zero_node;
34439 goto add_to_vector;
34440 }
34441 cp_lexer_consume_token (parser->lexer);
34442
34443 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
34444 {
34445 cp_parser_error (parser, "expected integer");
34446 return list;
34447 }
34448
34449 addend = cp_lexer_peek_token (parser->lexer)->u.value;
34450 if (TREE_CODE (addend) != INTEGER_CST)
34451 {
34452 cp_parser_error (parser, "expected integer");
34453 return list;
34454 }
34455 cp_lexer_consume_token (parser->lexer);
34456
34457 add_to_vector:
34458 if (t != error_mark_node)
34459 {
34460 vec = tree_cons (addend, t, vec);
34461 if (neg)
34462 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
34463 }
34464
34465 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34466 break;
34467
34468 cp_lexer_consume_token (parser->lexer);
34469 }
34470
34471 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
34472 {
34473 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
34474 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
34475 OMP_CLAUSE_DECL (u) = nreverse (vec);
34476 OMP_CLAUSE_CHAIN (u) = list;
34477 return u;
34478 }
34479 return list;
34480 }
34481
34482 /* OpenMP 5.0:
34483 iterators ( iterators-definition )
34484
34485 iterators-definition:
34486 iterator-specifier
34487 iterator-specifier , iterators-definition
34488
34489 iterator-specifier:
34490 identifier = range-specification
34491 iterator-type identifier = range-specification
34492
34493 range-specification:
34494 begin : end
34495 begin : end : step */
34496
34497 static tree
34498 cp_parser_omp_iterators (cp_parser *parser)
34499 {
34500 tree ret = NULL_TREE, *last = &ret;
34501 cp_lexer_consume_token (parser->lexer);
34502
34503 matching_parens parens;
34504 if (!parens.require_open (parser))
34505 return error_mark_node;
34506
34507 bool saved_colon_corrects_to_scope_p
34508 = parser->colon_corrects_to_scope_p;
34509 bool saved_colon_doesnt_start_class_def_p
34510 = parser->colon_doesnt_start_class_def_p;
34511
34512 do
34513 {
34514 tree iter_type;
34515 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34516 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
34517 iter_type = integer_type_node;
34518 else
34519 {
34520 const char *saved_message
34521 = parser->type_definition_forbidden_message;
34522 parser->type_definition_forbidden_message
34523 = G_("types may not be defined in iterator type");
34524
34525 iter_type = cp_parser_type_id (parser);
34526
34527 parser->type_definition_forbidden_message = saved_message;
34528 }
34529
34530 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34531 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34532 {
34533 cp_parser_error (parser, "expected identifier");
34534 break;
34535 }
34536
34537 tree id = cp_parser_identifier (parser);
34538 if (id == error_mark_node)
34539 break;
34540
34541 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34542 break;
34543
34544 parser->colon_corrects_to_scope_p = false;
34545 parser->colon_doesnt_start_class_def_p = true;
34546 tree begin = cp_parser_assignment_expression (parser);
34547
34548 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34549 break;
34550
34551 tree end = cp_parser_assignment_expression (parser);
34552
34553 tree step = integer_one_node;
34554 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34555 {
34556 cp_lexer_consume_token (parser->lexer);
34557 step = cp_parser_assignment_expression (parser);
34558 }
34559
34560 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
34561 DECL_ARTIFICIAL (iter_var) = 1;
34562 DECL_CONTEXT (iter_var) = current_function_decl;
34563 pushdecl (iter_var);
34564
34565 *last = make_tree_vec (6);
34566 TREE_VEC_ELT (*last, 0) = iter_var;
34567 TREE_VEC_ELT (*last, 1) = begin;
34568 TREE_VEC_ELT (*last, 2) = end;
34569 TREE_VEC_ELT (*last, 3) = step;
34570 last = &TREE_CHAIN (*last);
34571
34572 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34573 {
34574 cp_lexer_consume_token (parser->lexer);
34575 continue;
34576 }
34577 break;
34578 }
34579 while (1);
34580
34581 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34582 parser->colon_doesnt_start_class_def_p
34583 = saved_colon_doesnt_start_class_def_p;
34584
34585 if (!parens.require_close (parser))
34586 cp_parser_skip_to_closing_parenthesis (parser,
34587 /*recovering=*/true,
34588 /*or_comma=*/false,
34589 /*consume_paren=*/true);
34590
34591 return ret ? ret : error_mark_node;
34592 }
34593
34594 /* OpenMP 4.0:
34595 depend ( depend-kind : variable-list )
34596
34597 depend-kind:
34598 in | out | inout
34599
34600 OpenMP 4.5:
34601 depend ( source )
34602
34603 depend ( sink : vec )
34604
34605 OpenMP 5.0:
34606 depend ( depend-modifier , depend-kind: variable-list )
34607
34608 depend-kind:
34609 in | out | inout | mutexinoutset | depobj
34610
34611 depend-modifier:
34612 iterator ( iterators-definition ) */
34613
34614 static tree
34615 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
34616 {
34617 tree nlist, c, iterators = NULL_TREE;
34618 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
34619
34620 matching_parens parens;
34621 if (!parens.require_open (parser))
34622 return list;
34623
34624 do
34625 {
34626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34627 goto invalid_kind;
34628
34629 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34630 const char *p = IDENTIFIER_POINTER (id);
34631
34632 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
34633 {
34634 begin_scope (sk_omp, NULL);
34635 iterators = cp_parser_omp_iterators (parser);
34636 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
34637 continue;
34638 }
34639 if (strcmp ("in", p) == 0)
34640 kind = OMP_CLAUSE_DEPEND_IN;
34641 else if (strcmp ("inout", p) == 0)
34642 kind = OMP_CLAUSE_DEPEND_INOUT;
34643 else if (strcmp ("mutexinoutset", p) == 0)
34644 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
34645 else if (strcmp ("out", p) == 0)
34646 kind = OMP_CLAUSE_DEPEND_OUT;
34647 else if (strcmp ("depobj", p) == 0)
34648 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
34649 else if (strcmp ("sink", p) == 0)
34650 kind = OMP_CLAUSE_DEPEND_SINK;
34651 else if (strcmp ("source", p) == 0)
34652 kind = OMP_CLAUSE_DEPEND_SOURCE;
34653 else
34654 goto invalid_kind;
34655 break;
34656 }
34657 while (1);
34658
34659 cp_lexer_consume_token (parser->lexer);
34660
34661 if (iterators
34662 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34663 {
34664 poplevel (0, 1, 0);
34665 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34666 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34667 iterators = NULL_TREE;
34668 }
34669
34670 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34671 {
34672 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34673 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34674 OMP_CLAUSE_DECL (c) = NULL_TREE;
34675 OMP_CLAUSE_CHAIN (c) = list;
34676 if (!parens.require_close (parser))
34677 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34678 /*or_comma=*/false,
34679 /*consume_paren=*/true);
34680 return c;
34681 }
34682
34683 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34684 goto resync_fail;
34685
34686 if (kind == OMP_CLAUSE_DEPEND_SINK)
34687 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34688 else
34689 {
34690 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34691 list, NULL);
34692
34693 if (iterators)
34694 {
34695 tree block = poplevel (1, 1, 0);
34696 if (iterators == error_mark_node)
34697 iterators = NULL_TREE;
34698 else
34699 TREE_VEC_ELT (iterators, 5) = block;
34700 }
34701
34702 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34703 {
34704 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34705 if (iterators)
34706 OMP_CLAUSE_DECL (c)
34707 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34708 }
34709 }
34710 return nlist;
34711
34712 invalid_kind:
34713 cp_parser_error (parser, "invalid depend kind");
34714 resync_fail:
34715 if (iterators)
34716 poplevel (0, 1, 0);
34717 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34718 /*or_comma=*/false,
34719 /*consume_paren=*/true);
34720 return list;
34721 }
34722
34723 /* OpenMP 4.0:
34724 map ( map-kind : variable-list )
34725 map ( variable-list )
34726
34727 map-kind:
34728 alloc | to | from | tofrom
34729
34730 OpenMP 4.5:
34731 map-kind:
34732 alloc | to | from | tofrom | release | delete
34733
34734 map ( always [,] map-kind: variable-list ) */
34735
34736 static tree
34737 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34738 {
34739 tree nlist, c;
34740 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34741 bool always = false;
34742
34743 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34744 return list;
34745
34746 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34747 {
34748 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34749 const char *p = IDENTIFIER_POINTER (id);
34750
34751 if (strcmp ("always", p) == 0)
34752 {
34753 int nth = 2;
34754 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34755 nth++;
34756 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34757 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34758 == RID_DELETE))
34759 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34760 == CPP_COLON))
34761 {
34762 always = true;
34763 cp_lexer_consume_token (parser->lexer);
34764 if (nth == 3)
34765 cp_lexer_consume_token (parser->lexer);
34766 }
34767 }
34768 }
34769
34770 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34771 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34772 {
34773 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34774 const char *p = IDENTIFIER_POINTER (id);
34775
34776 if (strcmp ("alloc", p) == 0)
34777 kind = GOMP_MAP_ALLOC;
34778 else if (strcmp ("to", p) == 0)
34779 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34780 else if (strcmp ("from", p) == 0)
34781 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34782 else if (strcmp ("tofrom", p) == 0)
34783 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34784 else if (strcmp ("release", p) == 0)
34785 kind = GOMP_MAP_RELEASE;
34786 else
34787 {
34788 cp_parser_error (parser, "invalid map kind");
34789 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34790 /*or_comma=*/false,
34791 /*consume_paren=*/true);
34792 return list;
34793 }
34794 cp_lexer_consume_token (parser->lexer);
34795 cp_lexer_consume_token (parser->lexer);
34796 }
34797 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34798 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34799 {
34800 kind = GOMP_MAP_DELETE;
34801 cp_lexer_consume_token (parser->lexer);
34802 cp_lexer_consume_token (parser->lexer);
34803 }
34804
34805 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34806 NULL);
34807
34808 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34809 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34810
34811 return nlist;
34812 }
34813
34814 /* OpenMP 4.0:
34815 device ( expression ) */
34816
34817 static tree
34818 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34819 location_t location)
34820 {
34821 tree t, c;
34822
34823 matching_parens parens;
34824 if (!parens.require_open (parser))
34825 return list;
34826
34827 t = cp_parser_assignment_expression (parser);
34828
34829 if (t == error_mark_node
34830 || !parens.require_close (parser))
34831 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34832 /*or_comma=*/false,
34833 /*consume_paren=*/true);
34834
34835 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34836 "device", location);
34837
34838 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34839 OMP_CLAUSE_DEVICE_ID (c) = t;
34840 OMP_CLAUSE_CHAIN (c) = list;
34841
34842 return c;
34843 }
34844
34845 /* OpenMP 4.0:
34846 dist_schedule ( static )
34847 dist_schedule ( static , expression ) */
34848
34849 static tree
34850 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34851 location_t location)
34852 {
34853 tree c, t;
34854
34855 matching_parens parens;
34856 if (!parens.require_open (parser))
34857 return list;
34858
34859 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34860
34861 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34862 goto invalid_kind;
34863 cp_lexer_consume_token (parser->lexer);
34864
34865 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34866 {
34867 cp_lexer_consume_token (parser->lexer);
34868
34869 t = cp_parser_assignment_expression (parser);
34870
34871 if (t == error_mark_node)
34872 goto resync_fail;
34873 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34874
34875 if (!parens.require_close (parser))
34876 goto resync_fail;
34877 }
34878 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34879 goto resync_fail;
34880
34881 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34882 location);
34883 OMP_CLAUSE_CHAIN (c) = list;
34884 return c;
34885
34886 invalid_kind:
34887 cp_parser_error (parser, "invalid dist_schedule kind");
34888 resync_fail:
34889 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34890 /*or_comma=*/false,
34891 /*consume_paren=*/true);
34892 return list;
34893 }
34894
34895 /* OpenMP 4.0:
34896 proc_bind ( proc-bind-kind )
34897
34898 proc-bind-kind:
34899 master | close | spread */
34900
34901 static tree
34902 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34903 location_t location)
34904 {
34905 tree c;
34906 enum omp_clause_proc_bind_kind kind;
34907
34908 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34909 return list;
34910
34911 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34912 {
34913 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34914 const char *p = IDENTIFIER_POINTER (id);
34915
34916 if (strcmp ("master", p) == 0)
34917 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34918 else if (strcmp ("close", p) == 0)
34919 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34920 else if (strcmp ("spread", p) == 0)
34921 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34922 else
34923 goto invalid_kind;
34924 }
34925 else
34926 goto invalid_kind;
34927
34928 cp_lexer_consume_token (parser->lexer);
34929 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34930 goto resync_fail;
34931
34932 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34933 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34934 location);
34935 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34936 OMP_CLAUSE_CHAIN (c) = list;
34937 return c;
34938
34939 invalid_kind:
34940 cp_parser_error (parser, "invalid depend kind");
34941 resync_fail:
34942 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34943 /*or_comma=*/false,
34944 /*consume_paren=*/true);
34945 return list;
34946 }
34947
34948 /* OpenACC:
34949 async [( int-expr )] */
34950
34951 static tree
34952 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34953 {
34954 tree c, t;
34955 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34956
34957 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34958
34959 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34960 {
34961 matching_parens parens;
34962 parens.consume_open (parser);
34963
34964 t = cp_parser_expression (parser);
34965 if (t == error_mark_node
34966 || !parens.require_close (parser))
34967 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34968 /*or_comma=*/false,
34969 /*consume_paren=*/true);
34970 }
34971
34972 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34973
34974 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34975 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34976 OMP_CLAUSE_CHAIN (c) = list;
34977 list = c;
34978
34979 return list;
34980 }
34981
34982 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34983 is a bitmask in MASK. Return the list of clauses found. */
34984
34985 static tree
34986 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34987 const char *where, cp_token *pragma_tok,
34988 bool finish_p = true)
34989 {
34990 tree clauses = NULL;
34991 bool first = true;
34992
34993 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34994 {
34995 location_t here;
34996 pragma_omp_clause c_kind;
34997 omp_clause_code code;
34998 const char *c_name;
34999 tree prev = clauses;
35000
35001 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35002 cp_lexer_consume_token (parser->lexer);
35003
35004 here = cp_lexer_peek_token (parser->lexer)->location;
35005 c_kind = cp_parser_omp_clause_name (parser);
35006
35007 switch (c_kind)
35008 {
35009 case PRAGMA_OACC_CLAUSE_ASYNC:
35010 clauses = cp_parser_oacc_clause_async (parser, clauses);
35011 c_name = "async";
35012 break;
35013 case PRAGMA_OACC_CLAUSE_AUTO:
35014 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
35015 clauses);
35016 c_name = "auto";
35017 break;
35018 case PRAGMA_OACC_CLAUSE_COLLAPSE:
35019 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
35020 c_name = "collapse";
35021 break;
35022 case PRAGMA_OACC_CLAUSE_COPY:
35023 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35024 c_name = "copy";
35025 break;
35026 case PRAGMA_OACC_CLAUSE_COPYIN:
35027 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35028 c_name = "copyin";
35029 break;
35030 case PRAGMA_OACC_CLAUSE_COPYOUT:
35031 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35032 c_name = "copyout";
35033 break;
35034 case PRAGMA_OACC_CLAUSE_CREATE:
35035 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35036 c_name = "create";
35037 break;
35038 case PRAGMA_OACC_CLAUSE_DELETE:
35039 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35040 c_name = "delete";
35041 break;
35042 case PRAGMA_OMP_CLAUSE_DEFAULT:
35043 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
35044 c_name = "default";
35045 break;
35046 case PRAGMA_OACC_CLAUSE_DEVICE:
35047 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35048 c_name = "device";
35049 break;
35050 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
35051 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
35052 c_name = "deviceptr";
35053 break;
35054 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
35055 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35056 c_name = "device_resident";
35057 break;
35058 case PRAGMA_OACC_CLAUSE_FINALIZE:
35059 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
35060 clauses);
35061 c_name = "finalize";
35062 break;
35063 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
35064 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35065 clauses);
35066 c_name = "firstprivate";
35067 break;
35068 case PRAGMA_OACC_CLAUSE_GANG:
35069 c_name = "gang";
35070 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
35071 c_name, clauses);
35072 break;
35073 case PRAGMA_OACC_CLAUSE_HOST:
35074 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35075 c_name = "host";
35076 break;
35077 case PRAGMA_OACC_CLAUSE_IF:
35078 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
35079 c_name = "if";
35080 break;
35081 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
35082 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
35083 clauses);
35084 c_name = "if_present";
35085 break;
35086 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
35087 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
35088 clauses);
35089 c_name = "independent";
35090 break;
35091 case PRAGMA_OACC_CLAUSE_LINK:
35092 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35093 c_name = "link";
35094 break;
35095 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
35096 code = OMP_CLAUSE_NUM_GANGS;
35097 c_name = "num_gangs";
35098 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35099 clauses);
35100 break;
35101 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
35102 c_name = "num_workers";
35103 code = OMP_CLAUSE_NUM_WORKERS;
35104 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35105 clauses);
35106 break;
35107 case PRAGMA_OACC_CLAUSE_PRESENT:
35108 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35109 c_name = "present";
35110 break;
35111 case PRAGMA_OACC_CLAUSE_PRIVATE:
35112 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35113 clauses);
35114 c_name = "private";
35115 break;
35116 case PRAGMA_OACC_CLAUSE_REDUCTION:
35117 clauses
35118 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35119 false, clauses);
35120 c_name = "reduction";
35121 break;
35122 case PRAGMA_OACC_CLAUSE_SEQ:
35123 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
35124 clauses);
35125 c_name = "seq";
35126 break;
35127 case PRAGMA_OACC_CLAUSE_TILE:
35128 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
35129 c_name = "tile";
35130 break;
35131 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
35132 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35133 clauses);
35134 c_name = "use_device";
35135 break;
35136 case PRAGMA_OACC_CLAUSE_VECTOR:
35137 c_name = "vector";
35138 clauses = cp_parser_oacc_shape_clause (parser, here,
35139 OMP_CLAUSE_VECTOR,
35140 c_name, clauses);
35141 break;
35142 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
35143 c_name = "vector_length";
35144 code = OMP_CLAUSE_VECTOR_LENGTH;
35145 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35146 clauses);
35147 break;
35148 case PRAGMA_OACC_CLAUSE_WAIT:
35149 clauses = cp_parser_oacc_clause_wait (parser, clauses);
35150 c_name = "wait";
35151 break;
35152 case PRAGMA_OACC_CLAUSE_WORKER:
35153 c_name = "worker";
35154 clauses = cp_parser_oacc_shape_clause (parser, here,
35155 OMP_CLAUSE_WORKER,
35156 c_name, clauses);
35157 break;
35158 default:
35159 cp_parser_error (parser, "expected %<#pragma acc%> clause");
35160 goto saw_error;
35161 }
35162
35163 first = false;
35164
35165 if (((mask >> c_kind) & 1) == 0)
35166 {
35167 /* Remove the invalid clause(s) from the list to avoid
35168 confusing the rest of the compiler. */
35169 clauses = prev;
35170 error_at (here, "%qs is not valid for %qs", c_name, where);
35171 }
35172 }
35173
35174 saw_error:
35175 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35176
35177 if (finish_p)
35178 return finish_omp_clauses (clauses, C_ORT_ACC);
35179
35180 return clauses;
35181 }
35182
35183 /* Parse all OpenMP clauses. The set clauses allowed by the directive
35184 is a bitmask in MASK. Return the list of clauses found; the result
35185 of clause default goes in *pdefault. */
35186
35187 static tree
35188 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
35189 const char *where, cp_token *pragma_tok,
35190 bool finish_p = true)
35191 {
35192 tree clauses = NULL;
35193 bool first = true;
35194 cp_token *token = NULL;
35195
35196 /* Don't create location wrapper nodes within OpenMP clauses. */
35197 auto_suppress_location_wrappers sentinel;
35198
35199 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35200 {
35201 pragma_omp_clause c_kind;
35202 const char *c_name;
35203 tree prev = clauses;
35204
35205 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35206 cp_lexer_consume_token (parser->lexer);
35207
35208 token = cp_lexer_peek_token (parser->lexer);
35209 c_kind = cp_parser_omp_clause_name (parser);
35210
35211 switch (c_kind)
35212 {
35213 case PRAGMA_OMP_CLAUSE_COLLAPSE:
35214 clauses = cp_parser_omp_clause_collapse (parser, clauses,
35215 token->location);
35216 c_name = "collapse";
35217 break;
35218 case PRAGMA_OMP_CLAUSE_COPYIN:
35219 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
35220 c_name = "copyin";
35221 break;
35222 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
35223 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
35224 clauses);
35225 c_name = "copyprivate";
35226 break;
35227 case PRAGMA_OMP_CLAUSE_DEFAULT:
35228 clauses = cp_parser_omp_clause_default (parser, clauses,
35229 token->location, false);
35230 c_name = "default";
35231 break;
35232 case PRAGMA_OMP_CLAUSE_FINAL:
35233 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
35234 c_name = "final";
35235 break;
35236 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
35237 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35238 clauses);
35239 c_name = "firstprivate";
35240 break;
35241 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
35242 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
35243 token->location);
35244 c_name = "grainsize";
35245 break;
35246 case PRAGMA_OMP_CLAUSE_HINT:
35247 clauses = cp_parser_omp_clause_hint (parser, clauses,
35248 token->location);
35249 c_name = "hint";
35250 break;
35251 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
35252 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
35253 token->location);
35254 c_name = "defaultmap";
35255 break;
35256 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
35257 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35258 clauses);
35259 c_name = "use_device_ptr";
35260 break;
35261 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
35262 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
35263 clauses);
35264 c_name = "is_device_ptr";
35265 break;
35266 case PRAGMA_OMP_CLAUSE_IF:
35267 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
35268 true);
35269 c_name = "if";
35270 break;
35271 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
35272 clauses
35273 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
35274 true, clauses);
35275 c_name = "in_reduction";
35276 break;
35277 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
35278 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
35279 c_name = "lastprivate";
35280 break;
35281 case PRAGMA_OMP_CLAUSE_MERGEABLE:
35282 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
35283 token->location);
35284 c_name = "mergeable";
35285 break;
35286 case PRAGMA_OMP_CLAUSE_NOWAIT:
35287 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
35288 c_name = "nowait";
35289 break;
35290 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
35291 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
35292 token->location);
35293 c_name = "num_tasks";
35294 break;
35295 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
35296 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
35297 token->location);
35298 c_name = "num_threads";
35299 break;
35300 case PRAGMA_OMP_CLAUSE_ORDERED:
35301 clauses = cp_parser_omp_clause_ordered (parser, clauses,
35302 token->location);
35303 c_name = "ordered";
35304 break;
35305 case PRAGMA_OMP_CLAUSE_PRIORITY:
35306 clauses = cp_parser_omp_clause_priority (parser, clauses,
35307 token->location);
35308 c_name = "priority";
35309 break;
35310 case PRAGMA_OMP_CLAUSE_PRIVATE:
35311 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35312 clauses);
35313 c_name = "private";
35314 break;
35315 case PRAGMA_OMP_CLAUSE_REDUCTION:
35316 clauses
35317 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35318 true, clauses);
35319 c_name = "reduction";
35320 break;
35321 case PRAGMA_OMP_CLAUSE_SCHEDULE:
35322 clauses = cp_parser_omp_clause_schedule (parser, clauses,
35323 token->location);
35324 c_name = "schedule";
35325 break;
35326 case PRAGMA_OMP_CLAUSE_SHARED:
35327 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
35328 clauses);
35329 c_name = "shared";
35330 break;
35331 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
35332 clauses
35333 = cp_parser_omp_clause_reduction (parser,
35334 OMP_CLAUSE_TASK_REDUCTION,
35335 true, clauses);
35336 c_name = "task_reduction";
35337 break;
35338 case PRAGMA_OMP_CLAUSE_UNTIED:
35339 clauses = cp_parser_omp_clause_untied (parser, clauses,
35340 token->location);
35341 c_name = "untied";
35342 break;
35343 case PRAGMA_OMP_CLAUSE_INBRANCH:
35344 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
35345 clauses, token->location);
35346 c_name = "inbranch";
35347 break;
35348 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
35349 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
35350 clauses);
35351 c_name = "nontemporal";
35352 break;
35353 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
35354 clauses = cp_parser_omp_clause_branch (parser,
35355 OMP_CLAUSE_NOTINBRANCH,
35356 clauses, token->location);
35357 c_name = "notinbranch";
35358 break;
35359 case PRAGMA_OMP_CLAUSE_PARALLEL:
35360 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
35361 clauses, token->location);
35362 c_name = "parallel";
35363 if (!first)
35364 {
35365 clause_not_first:
35366 error_at (token->location, "%qs must be the first clause of %qs",
35367 c_name, where);
35368 clauses = prev;
35369 }
35370 break;
35371 case PRAGMA_OMP_CLAUSE_FOR:
35372 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
35373 clauses, token->location);
35374 c_name = "for";
35375 if (!first)
35376 goto clause_not_first;
35377 break;
35378 case PRAGMA_OMP_CLAUSE_SECTIONS:
35379 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
35380 clauses, token->location);
35381 c_name = "sections";
35382 if (!first)
35383 goto clause_not_first;
35384 break;
35385 case PRAGMA_OMP_CLAUSE_TASKGROUP:
35386 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
35387 clauses, token->location);
35388 c_name = "taskgroup";
35389 if (!first)
35390 goto clause_not_first;
35391 break;
35392 case PRAGMA_OMP_CLAUSE_LINK:
35393 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
35394 c_name = "to";
35395 break;
35396 case PRAGMA_OMP_CLAUSE_TO:
35397 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
35398 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35399 clauses);
35400 else
35401 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
35402 c_name = "to";
35403 break;
35404 case PRAGMA_OMP_CLAUSE_FROM:
35405 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
35406 c_name = "from";
35407 break;
35408 case PRAGMA_OMP_CLAUSE_UNIFORM:
35409 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
35410 clauses);
35411 c_name = "uniform";
35412 break;
35413 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
35414 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
35415 token->location);
35416 c_name = "num_teams";
35417 break;
35418 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
35419 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
35420 token->location);
35421 c_name = "thread_limit";
35422 break;
35423 case PRAGMA_OMP_CLAUSE_ALIGNED:
35424 clauses = cp_parser_omp_clause_aligned (parser, clauses);
35425 c_name = "aligned";
35426 break;
35427 case PRAGMA_OMP_CLAUSE_LINEAR:
35428 {
35429 bool declare_simd = false;
35430 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
35431 declare_simd = true;
35432 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
35433 }
35434 c_name = "linear";
35435 break;
35436 case PRAGMA_OMP_CLAUSE_DEPEND:
35437 clauses = cp_parser_omp_clause_depend (parser, clauses,
35438 token->location);
35439 c_name = "depend";
35440 break;
35441 case PRAGMA_OMP_CLAUSE_MAP:
35442 clauses = cp_parser_omp_clause_map (parser, clauses);
35443 c_name = "map";
35444 break;
35445 case PRAGMA_OMP_CLAUSE_DEVICE:
35446 clauses = cp_parser_omp_clause_device (parser, clauses,
35447 token->location);
35448 c_name = "device";
35449 break;
35450 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
35451 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
35452 token->location);
35453 c_name = "dist_schedule";
35454 break;
35455 case PRAGMA_OMP_CLAUSE_PROC_BIND:
35456 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
35457 token->location);
35458 c_name = "proc_bind";
35459 break;
35460 case PRAGMA_OMP_CLAUSE_SAFELEN:
35461 clauses = cp_parser_omp_clause_safelen (parser, clauses,
35462 token->location);
35463 c_name = "safelen";
35464 break;
35465 case PRAGMA_OMP_CLAUSE_SIMDLEN:
35466 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
35467 token->location);
35468 c_name = "simdlen";
35469 break;
35470 case PRAGMA_OMP_CLAUSE_NOGROUP:
35471 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
35472 token->location);
35473 c_name = "nogroup";
35474 break;
35475 case PRAGMA_OMP_CLAUSE_THREADS:
35476 clauses
35477 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
35478 clauses, token->location);
35479 c_name = "threads";
35480 break;
35481 case PRAGMA_OMP_CLAUSE_SIMD:
35482 clauses
35483 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
35484 clauses, token->location);
35485 c_name = "simd";
35486 break;
35487 default:
35488 cp_parser_error (parser, "expected %<#pragma omp%> clause");
35489 goto saw_error;
35490 }
35491
35492 first = false;
35493
35494 if (((mask >> c_kind) & 1) == 0)
35495 {
35496 /* Remove the invalid clause(s) from the list to avoid
35497 confusing the rest of the compiler. */
35498 clauses = prev;
35499 error_at (token->location, "%qs is not valid for %qs", c_name, where);
35500 }
35501 }
35502 saw_error:
35503 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35504 if (finish_p)
35505 {
35506 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
35507 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
35508 else
35509 return finish_omp_clauses (clauses, C_ORT_OMP);
35510 }
35511 return clauses;
35512 }
35513
35514 /* OpenMP 2.5:
35515 structured-block:
35516 statement
35517
35518 In practice, we're also interested in adding the statement to an
35519 outer node. So it is convenient if we work around the fact that
35520 cp_parser_statement calls add_stmt. */
35521
35522 static unsigned
35523 cp_parser_begin_omp_structured_block (cp_parser *parser)
35524 {
35525 unsigned save = parser->in_statement;
35526
35527 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35528 This preserves the "not within loop or switch" style error messages
35529 for nonsense cases like
35530 void foo() {
35531 #pragma omp single
35532 break;
35533 }
35534 */
35535 if (parser->in_statement)
35536 parser->in_statement = IN_OMP_BLOCK;
35537
35538 return save;
35539 }
35540
35541 static void
35542 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
35543 {
35544 parser->in_statement = save;
35545 }
35546
35547 static tree
35548 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
35549 {
35550 tree stmt = begin_omp_structured_block ();
35551 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35552
35553 cp_parser_statement (parser, NULL_TREE, false, if_p);
35554
35555 cp_parser_end_omp_structured_block (parser, save);
35556 return finish_omp_structured_block (stmt);
35557 }
35558
35559 /* OpenMP 2.5:
35560 # pragma omp atomic new-line
35561 expression-stmt
35562
35563 expression-stmt:
35564 x binop= expr | x++ | ++x | x-- | --x
35565 binop:
35566 +, *, -, /, &, ^, |, <<, >>
35567
35568 where x is an lvalue expression with scalar type.
35569
35570 OpenMP 3.1:
35571 # pragma omp atomic new-line
35572 update-stmt
35573
35574 # pragma omp atomic read new-line
35575 read-stmt
35576
35577 # pragma omp atomic write new-line
35578 write-stmt
35579
35580 # pragma omp atomic update new-line
35581 update-stmt
35582
35583 # pragma omp atomic capture new-line
35584 capture-stmt
35585
35586 # pragma omp atomic capture new-line
35587 capture-block
35588
35589 read-stmt:
35590 v = x
35591 write-stmt:
35592 x = expr
35593 update-stmt:
35594 expression-stmt | x = x binop expr
35595 capture-stmt:
35596 v = expression-stmt
35597 capture-block:
35598 { v = x; update-stmt; } | { update-stmt; v = x; }
35599
35600 OpenMP 4.0:
35601 update-stmt:
35602 expression-stmt | x = x binop expr | x = expr binop x
35603 capture-stmt:
35604 v = update-stmt
35605 capture-block:
35606 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35607
35608 where x and v are lvalue expressions with scalar type. */
35609
35610 static void
35611 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
35612 {
35613 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
35614 tree rhs1 = NULL_TREE, orig_lhs;
35615 location_t loc = pragma_tok->location;
35616 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
35617 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
35618 bool structured_block = false;
35619 bool first = true;
35620 tree clauses = NULL_TREE;
35621
35622 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35623 {
35624 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35625 cp_lexer_consume_token (parser->lexer);
35626
35627 first = false;
35628
35629 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35630 {
35631 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35632 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
35633 const char *p = IDENTIFIER_POINTER (id);
35634 enum tree_code new_code = ERROR_MARK;
35635 enum omp_memory_order new_memory_order
35636 = OMP_MEMORY_ORDER_UNSPECIFIED;
35637
35638 if (!strcmp (p, "read"))
35639 new_code = OMP_ATOMIC_READ;
35640 else if (!strcmp (p, "write"))
35641 new_code = NOP_EXPR;
35642 else if (!strcmp (p, "update"))
35643 new_code = OMP_ATOMIC;
35644 else if (!strcmp (p, "capture"))
35645 new_code = OMP_ATOMIC_CAPTURE_NEW;
35646 else if (!strcmp (p, "seq_cst"))
35647 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35648 else if (!strcmp (p, "acq_rel"))
35649 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35650 else if (!strcmp (p, "release"))
35651 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
35652 else if (!strcmp (p, "acquire"))
35653 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35654 else if (!strcmp (p, "relaxed"))
35655 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
35656 else if (!strcmp (p, "hint"))
35657 {
35658 cp_lexer_consume_token (parser->lexer);
35659 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
35660 continue;
35661 }
35662 else
35663 {
35664 p = NULL;
35665 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35666 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35667 "%<release%>, %<relaxed%> or %<hint%> clause");
35668 }
35669 if (p)
35670 {
35671 if (new_code != ERROR_MARK)
35672 {
35673 if (code != ERROR_MARK)
35674 error_at (cloc, "too many atomic clauses");
35675 else
35676 code = new_code;
35677 }
35678 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35679 {
35680 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35681 error_at (cloc, "too many memory order clauses");
35682 else
35683 memory_order = new_memory_order;
35684 }
35685 cp_lexer_consume_token (parser->lexer);
35686 continue;
35687 }
35688 }
35689 break;
35690 }
35691 cp_parser_require_pragma_eol (parser, pragma_tok);
35692
35693 if (code == ERROR_MARK)
35694 code = OMP_ATOMIC;
35695 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35696 {
35697 omp_requires_mask
35698 = (enum omp_requires) (omp_requires_mask
35699 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35700 switch ((enum omp_memory_order)
35701 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35702 {
35703 case OMP_MEMORY_ORDER_UNSPECIFIED:
35704 case OMP_MEMORY_ORDER_RELAXED:
35705 memory_order = OMP_MEMORY_ORDER_RELAXED;
35706 break;
35707 case OMP_MEMORY_ORDER_SEQ_CST:
35708 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35709 break;
35710 case OMP_MEMORY_ORDER_ACQ_REL:
35711 switch (code)
35712 {
35713 case OMP_ATOMIC_READ:
35714 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35715 break;
35716 case NOP_EXPR: /* atomic write */
35717 case OMP_ATOMIC:
35718 memory_order = OMP_MEMORY_ORDER_RELEASE;
35719 break;
35720 default:
35721 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35722 break;
35723 }
35724 break;
35725 default:
35726 gcc_unreachable ();
35727 }
35728 }
35729 else
35730 switch (code)
35731 {
35732 case OMP_ATOMIC_READ:
35733 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35734 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35735 {
35736 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35737 "%<acq_rel%> or %<release%> clauses");
35738 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35739 }
35740 break;
35741 case NOP_EXPR: /* atomic write */
35742 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35743 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35744 {
35745 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35746 "%<acq_rel%> or %<acquire%> clauses");
35747 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35748 }
35749 break;
35750 case OMP_ATOMIC:
35751 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35752 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35753 {
35754 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35755 "%<acq_rel%> or %<acquire%> clauses");
35756 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35757 }
35758 break;
35759 default:
35760 break;
35761 }
35762
35763 switch (code)
35764 {
35765 case OMP_ATOMIC_READ:
35766 case NOP_EXPR: /* atomic write */
35767 v = cp_parser_unary_expression (parser);
35768 if (v == error_mark_node)
35769 goto saw_error;
35770 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35771 goto saw_error;
35772 if (code == NOP_EXPR)
35773 lhs = cp_parser_expression (parser);
35774 else
35775 lhs = cp_parser_unary_expression (parser);
35776 if (lhs == error_mark_node)
35777 goto saw_error;
35778 if (code == NOP_EXPR)
35779 {
35780 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35781 opcode. */
35782 code = OMP_ATOMIC;
35783 rhs = lhs;
35784 lhs = v;
35785 v = NULL_TREE;
35786 }
35787 goto done;
35788 case OMP_ATOMIC_CAPTURE_NEW:
35789 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35790 {
35791 cp_lexer_consume_token (parser->lexer);
35792 structured_block = true;
35793 }
35794 else
35795 {
35796 v = cp_parser_unary_expression (parser);
35797 if (v == error_mark_node)
35798 goto saw_error;
35799 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35800 goto saw_error;
35801 }
35802 default:
35803 break;
35804 }
35805
35806 restart:
35807 lhs = cp_parser_unary_expression (parser);
35808 orig_lhs = lhs;
35809 switch (TREE_CODE (lhs))
35810 {
35811 case ERROR_MARK:
35812 goto saw_error;
35813
35814 case POSTINCREMENT_EXPR:
35815 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35816 code = OMP_ATOMIC_CAPTURE_OLD;
35817 /* FALLTHROUGH */
35818 case PREINCREMENT_EXPR:
35819 lhs = TREE_OPERAND (lhs, 0);
35820 opcode = PLUS_EXPR;
35821 rhs = integer_one_node;
35822 break;
35823
35824 case POSTDECREMENT_EXPR:
35825 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35826 code = OMP_ATOMIC_CAPTURE_OLD;
35827 /* FALLTHROUGH */
35828 case PREDECREMENT_EXPR:
35829 lhs = TREE_OPERAND (lhs, 0);
35830 opcode = MINUS_EXPR;
35831 rhs = integer_one_node;
35832 break;
35833
35834 case COMPOUND_EXPR:
35835 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35836 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35837 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35838 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35839 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35840 (TREE_OPERAND (lhs, 1), 0), 0)))
35841 == BOOLEAN_TYPE)
35842 /* Undo effects of boolean_increment for post {in,de}crement. */
35843 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35844 /* FALLTHRU */
35845 case MODIFY_EXPR:
35846 if (TREE_CODE (lhs) == MODIFY_EXPR
35847 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35848 {
35849 /* Undo effects of boolean_increment. */
35850 if (integer_onep (TREE_OPERAND (lhs, 1)))
35851 {
35852 /* This is pre or post increment. */
35853 rhs = TREE_OPERAND (lhs, 1);
35854 lhs = TREE_OPERAND (lhs, 0);
35855 opcode = NOP_EXPR;
35856 if (code == OMP_ATOMIC_CAPTURE_NEW
35857 && !structured_block
35858 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35859 code = OMP_ATOMIC_CAPTURE_OLD;
35860 break;
35861 }
35862 }
35863 /* FALLTHRU */
35864 default:
35865 switch (cp_lexer_peek_token (parser->lexer)->type)
35866 {
35867 case CPP_MULT_EQ:
35868 opcode = MULT_EXPR;
35869 break;
35870 case CPP_DIV_EQ:
35871 opcode = TRUNC_DIV_EXPR;
35872 break;
35873 case CPP_PLUS_EQ:
35874 opcode = PLUS_EXPR;
35875 break;
35876 case CPP_MINUS_EQ:
35877 opcode = MINUS_EXPR;
35878 break;
35879 case CPP_LSHIFT_EQ:
35880 opcode = LSHIFT_EXPR;
35881 break;
35882 case CPP_RSHIFT_EQ:
35883 opcode = RSHIFT_EXPR;
35884 break;
35885 case CPP_AND_EQ:
35886 opcode = BIT_AND_EXPR;
35887 break;
35888 case CPP_OR_EQ:
35889 opcode = BIT_IOR_EXPR;
35890 break;
35891 case CPP_XOR_EQ:
35892 opcode = BIT_XOR_EXPR;
35893 break;
35894 case CPP_EQ:
35895 enum cp_parser_prec oprec;
35896 cp_token *token;
35897 cp_lexer_consume_token (parser->lexer);
35898 cp_parser_parse_tentatively (parser);
35899 rhs1 = cp_parser_simple_cast_expression (parser);
35900 if (rhs1 == error_mark_node)
35901 {
35902 cp_parser_abort_tentative_parse (parser);
35903 cp_parser_simple_cast_expression (parser);
35904 goto saw_error;
35905 }
35906 token = cp_lexer_peek_token (parser->lexer);
35907 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35908 {
35909 cp_parser_abort_tentative_parse (parser);
35910 cp_parser_parse_tentatively (parser);
35911 rhs = cp_parser_binary_expression (parser, false, true,
35912 PREC_NOT_OPERATOR, NULL);
35913 if (rhs == error_mark_node)
35914 {
35915 cp_parser_abort_tentative_parse (parser);
35916 cp_parser_binary_expression (parser, false, true,
35917 PREC_NOT_OPERATOR, NULL);
35918 goto saw_error;
35919 }
35920 switch (TREE_CODE (rhs))
35921 {
35922 case MULT_EXPR:
35923 case TRUNC_DIV_EXPR:
35924 case RDIV_EXPR:
35925 case PLUS_EXPR:
35926 case MINUS_EXPR:
35927 case LSHIFT_EXPR:
35928 case RSHIFT_EXPR:
35929 case BIT_AND_EXPR:
35930 case BIT_IOR_EXPR:
35931 case BIT_XOR_EXPR:
35932 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35933 {
35934 if (cp_parser_parse_definitely (parser))
35935 {
35936 opcode = TREE_CODE (rhs);
35937 rhs1 = TREE_OPERAND (rhs, 0);
35938 rhs = TREE_OPERAND (rhs, 1);
35939 goto stmt_done;
35940 }
35941 else
35942 goto saw_error;
35943 }
35944 break;
35945 default:
35946 break;
35947 }
35948 cp_parser_abort_tentative_parse (parser);
35949 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35950 {
35951 rhs = cp_parser_expression (parser);
35952 if (rhs == error_mark_node)
35953 goto saw_error;
35954 opcode = NOP_EXPR;
35955 rhs1 = NULL_TREE;
35956 goto stmt_done;
35957 }
35958 cp_parser_error (parser,
35959 "invalid form of %<#pragma omp atomic%>");
35960 goto saw_error;
35961 }
35962 if (!cp_parser_parse_definitely (parser))
35963 goto saw_error;
35964 switch (token->type)
35965 {
35966 case CPP_SEMICOLON:
35967 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35968 {
35969 code = OMP_ATOMIC_CAPTURE_OLD;
35970 v = lhs;
35971 lhs = NULL_TREE;
35972 lhs1 = rhs1;
35973 rhs1 = NULL_TREE;
35974 cp_lexer_consume_token (parser->lexer);
35975 goto restart;
35976 }
35977 else if (structured_block)
35978 {
35979 opcode = NOP_EXPR;
35980 rhs = rhs1;
35981 rhs1 = NULL_TREE;
35982 goto stmt_done;
35983 }
35984 cp_parser_error (parser,
35985 "invalid form of %<#pragma omp atomic%>");
35986 goto saw_error;
35987 case CPP_MULT:
35988 opcode = MULT_EXPR;
35989 break;
35990 case CPP_DIV:
35991 opcode = TRUNC_DIV_EXPR;
35992 break;
35993 case CPP_PLUS:
35994 opcode = PLUS_EXPR;
35995 break;
35996 case CPP_MINUS:
35997 opcode = MINUS_EXPR;
35998 break;
35999 case CPP_LSHIFT:
36000 opcode = LSHIFT_EXPR;
36001 break;
36002 case CPP_RSHIFT:
36003 opcode = RSHIFT_EXPR;
36004 break;
36005 case CPP_AND:
36006 opcode = BIT_AND_EXPR;
36007 break;
36008 case CPP_OR:
36009 opcode = BIT_IOR_EXPR;
36010 break;
36011 case CPP_XOR:
36012 opcode = BIT_XOR_EXPR;
36013 break;
36014 default:
36015 cp_parser_error (parser,
36016 "invalid operator for %<#pragma omp atomic%>");
36017 goto saw_error;
36018 }
36019 oprec = TOKEN_PRECEDENCE (token);
36020 gcc_assert (oprec != PREC_NOT_OPERATOR);
36021 if (commutative_tree_code (opcode))
36022 oprec = (enum cp_parser_prec) (oprec - 1);
36023 cp_lexer_consume_token (parser->lexer);
36024 rhs = cp_parser_binary_expression (parser, false, false,
36025 oprec, NULL);
36026 if (rhs == error_mark_node)
36027 goto saw_error;
36028 goto stmt_done;
36029 /* FALLTHROUGH */
36030 default:
36031 cp_parser_error (parser,
36032 "invalid operator for %<#pragma omp atomic%>");
36033 goto saw_error;
36034 }
36035 cp_lexer_consume_token (parser->lexer);
36036
36037 rhs = cp_parser_expression (parser);
36038 if (rhs == error_mark_node)
36039 goto saw_error;
36040 break;
36041 }
36042 stmt_done:
36043 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
36044 {
36045 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
36046 goto saw_error;
36047 v = cp_parser_unary_expression (parser);
36048 if (v == error_mark_node)
36049 goto saw_error;
36050 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36051 goto saw_error;
36052 lhs1 = cp_parser_unary_expression (parser);
36053 if (lhs1 == error_mark_node)
36054 goto saw_error;
36055 }
36056 if (structured_block)
36057 {
36058 cp_parser_consume_semicolon_at_end_of_statement (parser);
36059 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
36060 }
36061 done:
36062 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36063 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
36064 rhs1, clauses, memory_order);
36065 if (!structured_block)
36066 cp_parser_consume_semicolon_at_end_of_statement (parser);
36067 return;
36068
36069 saw_error:
36070 cp_parser_skip_to_end_of_block_or_statement (parser);
36071 if (structured_block)
36072 {
36073 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36074 cp_lexer_consume_token (parser->lexer);
36075 else if (code == OMP_ATOMIC_CAPTURE_NEW)
36076 {
36077 cp_parser_skip_to_end_of_block_or_statement (parser);
36078 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36079 cp_lexer_consume_token (parser->lexer);
36080 }
36081 }
36082 }
36083
36084
36085 /* OpenMP 2.5:
36086 # pragma omp barrier new-line */
36087
36088 static void
36089 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
36090 {
36091 cp_parser_require_pragma_eol (parser, pragma_tok);
36092 finish_omp_barrier ();
36093 }
36094
36095 /* OpenMP 2.5:
36096 # pragma omp critical [(name)] new-line
36097 structured-block
36098
36099 OpenMP 4.5:
36100 # pragma omp critical [(name) [hint(expression)]] new-line
36101 structured-block */
36102
36103 #define OMP_CRITICAL_CLAUSE_MASK \
36104 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
36105
36106 static tree
36107 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36108 {
36109 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
36110
36111 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36112 {
36113 matching_parens parens;
36114 parens.consume_open (parser);
36115
36116 name = cp_parser_identifier (parser);
36117
36118 if (name == error_mark_node
36119 || !parens.require_close (parser))
36120 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36121 /*or_comma=*/false,
36122 /*consume_paren=*/true);
36123 if (name == error_mark_node)
36124 name = NULL;
36125
36126 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
36127 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
36128 cp_lexer_consume_token (parser->lexer);
36129
36130 clauses = cp_parser_omp_all_clauses (parser,
36131 OMP_CRITICAL_CLAUSE_MASK,
36132 "#pragma omp critical", pragma_tok);
36133 }
36134 else
36135 cp_parser_require_pragma_eol (parser, pragma_tok);
36136
36137 stmt = cp_parser_omp_structured_block (parser, if_p);
36138 return c_finish_omp_critical (input_location, stmt, name, clauses);
36139 }
36140
36141 /* OpenMP 5.0:
36142 # pragma omp depobj ( depobj ) depobj-clause new-line
36143
36144 depobj-clause:
36145 depend (dependence-type : locator)
36146 destroy
36147 update (dependence-type)
36148
36149 dependence-type:
36150 in
36151 out
36152 inout
36153 mutexinout */
36154
36155 static void
36156 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
36157 {
36158 location_t loc = pragma_tok->location;
36159 matching_parens parens;
36160 if (!parens.require_open (parser))
36161 {
36162 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36163 return;
36164 }
36165
36166 tree depobj = cp_parser_assignment_expression (parser);
36167
36168 if (!parens.require_close (parser))
36169 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36170 /*or_comma=*/false,
36171 /*consume_paren=*/true);
36172
36173 tree clause = NULL_TREE;
36174 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
36175 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
36176 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36177 {
36178 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36179 const char *p = IDENTIFIER_POINTER (id);
36180
36181 cp_lexer_consume_token (parser->lexer);
36182 if (!strcmp ("depend", p))
36183 {
36184 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
36185 if (clause)
36186 clause = finish_omp_clauses (clause, C_ORT_OMP);
36187 if (!clause)
36188 clause = error_mark_node;
36189 }
36190 else if (!strcmp ("destroy", p))
36191 kind = OMP_CLAUSE_DEPEND_LAST;
36192 else if (!strcmp ("update", p))
36193 {
36194 matching_parens c_parens;
36195 if (c_parens.require_open (parser))
36196 {
36197 location_t c2_loc
36198 = cp_lexer_peek_token (parser->lexer)->location;
36199 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36200 {
36201 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
36202 const char *p2 = IDENTIFIER_POINTER (id2);
36203
36204 cp_lexer_consume_token (parser->lexer);
36205 if (!strcmp ("in", p2))
36206 kind = OMP_CLAUSE_DEPEND_IN;
36207 else if (!strcmp ("out", p2))
36208 kind = OMP_CLAUSE_DEPEND_OUT;
36209 else if (!strcmp ("inout", p2))
36210 kind = OMP_CLAUSE_DEPEND_INOUT;
36211 else if (!strcmp ("mutexinoutset", p2))
36212 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
36213 }
36214 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
36215 {
36216 clause = error_mark_node;
36217 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
36218 "%<mutexinoutset%>");
36219 }
36220 if (!c_parens.require_close (parser))
36221 cp_parser_skip_to_closing_parenthesis (parser,
36222 /*recovering=*/true,
36223 /*or_comma=*/false,
36224 /*consume_paren=*/true);
36225 }
36226 else
36227 clause = error_mark_node;
36228 }
36229 }
36230 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
36231 {
36232 clause = error_mark_node;
36233 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
36234 }
36235 cp_parser_require_pragma_eol (parser, pragma_tok);
36236
36237 finish_omp_depobj (loc, depobj, kind, clause);
36238 }
36239
36240
36241 /* OpenMP 2.5:
36242 # pragma omp flush flush-vars[opt] new-line
36243
36244 flush-vars:
36245 ( variable-list )
36246
36247 OpenMP 5.0:
36248 # pragma omp flush memory-order-clause new-line */
36249
36250 static void
36251 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
36252 {
36253 enum memmodel mo = MEMMODEL_LAST;
36254 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36255 {
36256 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36257 const char *p = IDENTIFIER_POINTER (id);
36258 if (!strcmp (p, "acq_rel"))
36259 mo = MEMMODEL_ACQ_REL;
36260 else if (!strcmp (p, "release"))
36261 mo = MEMMODEL_RELEASE;
36262 else if (!strcmp (p, "acquire"))
36263 mo = MEMMODEL_ACQUIRE;
36264 else
36265 error_at (cp_lexer_peek_token (parser->lexer)->location,
36266 "expected %<acq_rel%>, %<release%> or %<acquire%>");
36267 cp_lexer_consume_token (parser->lexer);
36268 }
36269 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36270 {
36271 if (mo != MEMMODEL_LAST)
36272 error_at (cp_lexer_peek_token (parser->lexer)->location,
36273 "%<flush%> list specified together with memory order "
36274 "clause");
36275 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36276 }
36277 cp_parser_require_pragma_eol (parser, pragma_tok);
36278
36279 finish_omp_flush (mo);
36280 }
36281
36282 /* Helper function, to parse omp for increment expression. */
36283
36284 static tree
36285 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
36286 {
36287 tree cond = cp_parser_binary_expression (parser, false, true,
36288 PREC_NOT_OPERATOR, NULL);
36289 if (cond == error_mark_node
36290 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36291 {
36292 cp_parser_skip_to_end_of_statement (parser);
36293 return error_mark_node;
36294 }
36295
36296 switch (TREE_CODE (cond))
36297 {
36298 case GT_EXPR:
36299 case GE_EXPR:
36300 case LT_EXPR:
36301 case LE_EXPR:
36302 break;
36303 case NE_EXPR:
36304 if (code != OACC_LOOP)
36305 break;
36306 gcc_fallthrough ();
36307 default:
36308 return error_mark_node;
36309 }
36310
36311 /* If decl is an iterator, preserve LHS and RHS of the relational
36312 expr until finish_omp_for. */
36313 if (decl
36314 && (type_dependent_expression_p (decl)
36315 || CLASS_TYPE_P (TREE_TYPE (decl))))
36316 return cond;
36317
36318 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
36319 TREE_CODE (cond),
36320 TREE_OPERAND (cond, 0), ERROR_MARK,
36321 TREE_OPERAND (cond, 1), ERROR_MARK,
36322 /*overload=*/NULL, tf_warning_or_error);
36323 }
36324
36325 /* Helper function, to parse omp for increment expression. */
36326
36327 static tree
36328 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
36329 {
36330 cp_token *token = cp_lexer_peek_token (parser->lexer);
36331 enum tree_code op;
36332 tree lhs, rhs;
36333 cp_id_kind idk;
36334 bool decl_first;
36335
36336 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36337 {
36338 op = (token->type == CPP_PLUS_PLUS
36339 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
36340 cp_lexer_consume_token (parser->lexer);
36341 lhs = cp_parser_simple_cast_expression (parser);
36342 if (lhs != decl
36343 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36344 return error_mark_node;
36345 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36346 }
36347
36348 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
36349 if (lhs != decl
36350 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36351 return error_mark_node;
36352
36353 token = cp_lexer_peek_token (parser->lexer);
36354 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36355 {
36356 op = (token->type == CPP_PLUS_PLUS
36357 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
36358 cp_lexer_consume_token (parser->lexer);
36359 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36360 }
36361
36362 op = cp_parser_assignment_operator_opt (parser);
36363 if (op == ERROR_MARK)
36364 return error_mark_node;
36365
36366 if (op != NOP_EXPR)
36367 {
36368 rhs = cp_parser_assignment_expression (parser);
36369 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
36370 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36371 }
36372
36373 lhs = cp_parser_binary_expression (parser, false, false,
36374 PREC_ADDITIVE_EXPRESSION, NULL);
36375 token = cp_lexer_peek_token (parser->lexer);
36376 decl_first = (lhs == decl
36377 || (processing_template_decl && cp_tree_equal (lhs, decl)));
36378 if (decl_first)
36379 lhs = NULL_TREE;
36380 if (token->type != CPP_PLUS
36381 && token->type != CPP_MINUS)
36382 return error_mark_node;
36383
36384 do
36385 {
36386 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
36387 cp_lexer_consume_token (parser->lexer);
36388 rhs = cp_parser_binary_expression (parser, false, false,
36389 PREC_ADDITIVE_EXPRESSION, NULL);
36390 token = cp_lexer_peek_token (parser->lexer);
36391 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
36392 {
36393 if (lhs == NULL_TREE)
36394 {
36395 if (op == PLUS_EXPR)
36396 lhs = rhs;
36397 else
36398 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
36399 tf_warning_or_error);
36400 }
36401 else
36402 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
36403 ERROR_MARK, NULL, tf_warning_or_error);
36404 }
36405 }
36406 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
36407
36408 if (!decl_first)
36409 {
36410 if ((rhs != decl
36411 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
36412 || op == MINUS_EXPR)
36413 return error_mark_node;
36414 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
36415 }
36416 else
36417 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
36418
36419 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36420 }
36421
36422 /* Parse the initialization statement of an OpenMP for loop.
36423
36424 Return true if the resulting construct should have an
36425 OMP_CLAUSE_PRIVATE added to it. */
36426
36427 static tree
36428 cp_parser_omp_for_loop_init (cp_parser *parser,
36429 tree &this_pre_body,
36430 vec<tree, va_gc> *&for_block,
36431 tree &init,
36432 tree &orig_init,
36433 tree &decl,
36434 tree &real_decl)
36435 {
36436 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36437 return NULL_TREE;
36438
36439 tree add_private_clause = NULL_TREE;
36440
36441 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36442
36443 init-expr:
36444 var = lb
36445 integer-type var = lb
36446 random-access-iterator-type var = lb
36447 pointer-type var = lb
36448 */
36449 cp_decl_specifier_seq type_specifiers;
36450
36451 /* First, try to parse as an initialized declaration. See
36452 cp_parser_condition, from whence the bulk of this is copied. */
36453
36454 cp_parser_parse_tentatively (parser);
36455 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
36456 /*is_declaration=*/true,
36457 /*is_trailing_return=*/false,
36458 &type_specifiers);
36459 if (cp_parser_parse_definitely (parser))
36460 {
36461 /* If parsing a type specifier seq succeeded, then this
36462 MUST be a initialized declaration. */
36463 tree asm_specification, attributes;
36464 cp_declarator *declarator;
36465
36466 declarator = cp_parser_declarator (parser,
36467 CP_PARSER_DECLARATOR_NAMED,
36468 CP_PARSER_FLAGS_NONE,
36469 /*ctor_dtor_or_conv_p=*/NULL,
36470 /*parenthesized_p=*/NULL,
36471 /*member_p=*/false,
36472 /*friend_p=*/false,
36473 /*static_p=*/false);
36474 attributes = cp_parser_attributes_opt (parser);
36475 asm_specification = cp_parser_asm_specification_opt (parser);
36476
36477 if (declarator == cp_error_declarator)
36478 cp_parser_skip_to_end_of_statement (parser);
36479
36480 else
36481 {
36482 tree pushed_scope, auto_node;
36483
36484 decl = start_decl (declarator, &type_specifiers,
36485 SD_INITIALIZED, attributes,
36486 /*prefix_attributes=*/NULL_TREE,
36487 &pushed_scope);
36488
36489 auto_node = type_uses_auto (TREE_TYPE (decl));
36490 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36491 {
36492 if (cp_lexer_next_token_is (parser->lexer,
36493 CPP_OPEN_PAREN))
36494 error ("parenthesized initialization is not allowed in "
36495 "OpenMP %<for%> loop");
36496 else
36497 /* Trigger an error. */
36498 cp_parser_require (parser, CPP_EQ, RT_EQ);
36499
36500 init = error_mark_node;
36501 cp_parser_skip_to_end_of_statement (parser);
36502 }
36503 else if (CLASS_TYPE_P (TREE_TYPE (decl))
36504 || type_dependent_expression_p (decl)
36505 || auto_node)
36506 {
36507 bool is_direct_init, is_non_constant_init;
36508
36509 init = cp_parser_initializer (parser,
36510 &is_direct_init,
36511 &is_non_constant_init);
36512
36513 if (auto_node)
36514 {
36515 TREE_TYPE (decl)
36516 = do_auto_deduction (TREE_TYPE (decl), init,
36517 auto_node);
36518
36519 if (!CLASS_TYPE_P (TREE_TYPE (decl))
36520 && !type_dependent_expression_p (decl))
36521 goto non_class;
36522 }
36523
36524 cp_finish_decl (decl, init, !is_non_constant_init,
36525 asm_specification,
36526 LOOKUP_ONLYCONVERTING);
36527 orig_init = init;
36528 if (CLASS_TYPE_P (TREE_TYPE (decl)))
36529 {
36530 vec_safe_push (for_block, this_pre_body);
36531 init = NULL_TREE;
36532 }
36533 else
36534 {
36535 init = pop_stmt_list (this_pre_body);
36536 if (init && TREE_CODE (init) == STATEMENT_LIST)
36537 {
36538 tree_stmt_iterator i = tsi_start (init);
36539 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36540 while (!tsi_end_p (i))
36541 {
36542 tree t = tsi_stmt (i);
36543 if (TREE_CODE (t) == DECL_EXPR
36544 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
36545 {
36546 tsi_delink (&i);
36547 vec_safe_push (for_block, t);
36548 continue;
36549 }
36550 break;
36551 }
36552 if (tsi_one_before_end_p (i))
36553 {
36554 tree t = tsi_stmt (i);
36555 tsi_delink (&i);
36556 free_stmt_list (init);
36557 init = t;
36558 }
36559 }
36560 }
36561 this_pre_body = NULL_TREE;
36562 }
36563 else
36564 {
36565 /* Consume '='. */
36566 cp_lexer_consume_token (parser->lexer);
36567 init = cp_parser_assignment_expression (parser);
36568
36569 non_class:
36570 if (TYPE_REF_P (TREE_TYPE (decl)))
36571 init = error_mark_node;
36572 else
36573 cp_finish_decl (decl, NULL_TREE,
36574 /*init_const_expr_p=*/false,
36575 asm_specification,
36576 LOOKUP_ONLYCONVERTING);
36577 }
36578
36579 if (pushed_scope)
36580 pop_scope (pushed_scope);
36581 }
36582 }
36583 else
36584 {
36585 cp_id_kind idk;
36586 /* If parsing a type specifier sequence failed, then
36587 this MUST be a simple expression. */
36588 cp_parser_parse_tentatively (parser);
36589 decl = cp_parser_primary_expression (parser, false, false,
36590 false, &idk);
36591 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
36592 if (!cp_parser_error_occurred (parser)
36593 && decl
36594 && (TREE_CODE (decl) == COMPONENT_REF
36595 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
36596 {
36597 cp_parser_abort_tentative_parse (parser);
36598 cp_parser_parse_tentatively (parser);
36599 cp_token *token = cp_lexer_peek_token (parser->lexer);
36600 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
36601 /*check_dependency_p=*/true,
36602 /*template_p=*/NULL,
36603 /*declarator_p=*/false,
36604 /*optional_p=*/false);
36605 if (name != error_mark_node
36606 && last_tok == cp_lexer_peek_token (parser->lexer))
36607 {
36608 decl = cp_parser_lookup_name_simple (parser, name,
36609 token->location);
36610 if (TREE_CODE (decl) == FIELD_DECL)
36611 add_private_clause = omp_privatize_field (decl, false);
36612 }
36613 cp_parser_abort_tentative_parse (parser);
36614 cp_parser_parse_tentatively (parser);
36615 decl = cp_parser_primary_expression (parser, false, false,
36616 false, &idk);
36617 }
36618 if (!cp_parser_error_occurred (parser)
36619 && decl
36620 && DECL_P (decl)
36621 && CLASS_TYPE_P (TREE_TYPE (decl)))
36622 {
36623 tree rhs;
36624
36625 cp_parser_parse_definitely (parser);
36626 cp_parser_require (parser, CPP_EQ, RT_EQ);
36627 rhs = cp_parser_assignment_expression (parser);
36628 orig_init = rhs;
36629 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
36630 decl, NOP_EXPR,
36631 rhs,
36632 tf_warning_or_error));
36633 if (!add_private_clause)
36634 add_private_clause = decl;
36635 }
36636 else
36637 {
36638 decl = NULL;
36639 cp_parser_abort_tentative_parse (parser);
36640 init = cp_parser_expression (parser);
36641 if (init)
36642 {
36643 if (TREE_CODE (init) == MODIFY_EXPR
36644 || TREE_CODE (init) == MODOP_EXPR)
36645 real_decl = TREE_OPERAND (init, 0);
36646 }
36647 }
36648 }
36649 return add_private_clause;
36650 }
36651
36652 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36653
36654 void
36655 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
36656 tree &decl, tree &orig_decl, tree &init,
36657 tree &orig_init, tree &cond, tree &incr)
36658 {
36659 tree begin, end, range_temp_decl = NULL_TREE;
36660 tree iter_type, begin_expr, end_expr;
36661
36662 if (processing_template_decl)
36663 {
36664 if (check_for_bare_parameter_packs (init))
36665 init = error_mark_node;
36666 if (!type_dependent_expression_p (init)
36667 /* do_auto_deduction doesn't mess with template init-lists. */
36668 && !BRACE_ENCLOSED_INITIALIZER_P (init))
36669 {
36670 tree d = decl;
36671 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36672 {
36673 tree v = DECL_VALUE_EXPR (decl);
36674 if (TREE_CODE (v) == ARRAY_REF
36675 && VAR_P (TREE_OPERAND (v, 0))
36676 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36677 d = TREE_OPERAND (v, 0);
36678 }
36679 do_range_for_auto_deduction (d, init);
36680 }
36681 cond = global_namespace;
36682 incr = NULL_TREE;
36683 orig_init = init;
36684 if (this_pre_body)
36685 this_pre_body = pop_stmt_list (this_pre_body);
36686 return;
36687 }
36688
36689 init = mark_lvalue_use (init);
36690
36691 if (decl == error_mark_node || init == error_mark_node)
36692 /* If an error happened previously do nothing or else a lot of
36693 unhelpful errors would be issued. */
36694 begin_expr = end_expr = iter_type = error_mark_node;
36695 else
36696 {
36697 tree range_temp;
36698
36699 if (VAR_P (init)
36700 && array_of_runtime_bound_p (TREE_TYPE (init)))
36701 /* Can't bind a reference to an array of runtime bound. */
36702 range_temp = init;
36703 else
36704 {
36705 range_temp = build_range_temp (init);
36706 DECL_NAME (range_temp) = NULL_TREE;
36707 pushdecl (range_temp);
36708 cp_finish_decl (range_temp, init,
36709 /*is_constant_init*/false, NULL_TREE,
36710 LOOKUP_ONLYCONVERTING);
36711 range_temp_decl = range_temp;
36712 range_temp = convert_from_reference (range_temp);
36713 }
36714 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36715 &begin_expr, &end_expr);
36716 }
36717
36718 tree end_iter_type = iter_type;
36719 if (cxx_dialect >= cxx17)
36720 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36721 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36722 TREE_USED (end) = 1;
36723 DECL_ARTIFICIAL (end) = 1;
36724 pushdecl (end);
36725 cp_finish_decl (end, end_expr,
36726 /*is_constant_init*/false, NULL_TREE,
36727 LOOKUP_ONLYCONVERTING);
36728
36729 /* The new for initialization statement. */
36730 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36731 TREE_USED (begin) = 1;
36732 DECL_ARTIFICIAL (begin) = 1;
36733 pushdecl (begin);
36734 orig_init = init;
36735 if (CLASS_TYPE_P (iter_type))
36736 init = NULL_TREE;
36737 else
36738 {
36739 init = begin_expr;
36740 begin_expr = NULL_TREE;
36741 }
36742 cp_finish_decl (begin, begin_expr,
36743 /*is_constant_init*/false, NULL_TREE,
36744 LOOKUP_ONLYCONVERTING);
36745
36746 /* The new for condition. */
36747 if (CLASS_TYPE_P (iter_type))
36748 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36749 else
36750 cond = build_x_binary_op (input_location, NE_EXPR,
36751 begin, ERROR_MARK,
36752 end, ERROR_MARK,
36753 NULL, tf_warning_or_error);
36754
36755 /* The new increment expression. */
36756 if (CLASS_TYPE_P (iter_type))
36757 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36758 else
36759 incr = finish_unary_op_expr (input_location,
36760 PREINCREMENT_EXPR, begin,
36761 tf_warning_or_error);
36762
36763 orig_decl = decl;
36764 decl = begin;
36765 if (for_block)
36766 {
36767 vec_safe_push (for_block, this_pre_body);
36768 this_pre_body = NULL_TREE;
36769 }
36770
36771 tree decomp_first_name = NULL_TREE;
36772 unsigned decomp_cnt = 0;
36773 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36774 {
36775 tree v = DECL_VALUE_EXPR (orig_decl);
36776 if (TREE_CODE (v) == ARRAY_REF
36777 && VAR_P (TREE_OPERAND (v, 0))
36778 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36779 {
36780 tree d = orig_decl;
36781 orig_decl = TREE_OPERAND (v, 0);
36782 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36783 decomp_first_name = d;
36784 }
36785 }
36786
36787 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36788 if (auto_node)
36789 {
36790 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36791 tf_none);
36792 if (!error_operand_p (t))
36793 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36794 t, auto_node);
36795 }
36796
36797 tree v = make_tree_vec (decomp_cnt + 3);
36798 TREE_VEC_ELT (v, 0) = range_temp_decl;
36799 TREE_VEC_ELT (v, 1) = end;
36800 TREE_VEC_ELT (v, 2) = orig_decl;
36801 for (unsigned i = 0; i < decomp_cnt; i++)
36802 {
36803 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36804 decomp_first_name = DECL_CHAIN (decomp_first_name);
36805 }
36806 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36807 }
36808
36809 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36810 inside of the collapsed body. */
36811
36812 void
36813 cp_finish_omp_range_for (tree orig, tree begin)
36814 {
36815 gcc_assert (TREE_CODE (orig) == TREE_LIST
36816 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36817 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36818 tree decomp_first_name = NULL_TREE;
36819 unsigned int decomp_cnt = 0;
36820
36821 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36822 {
36823 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36824 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36825 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36826 }
36827
36828 /* The declaration is initialized with *__begin inside the loop body. */
36829 cp_finish_decl (decl,
36830 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36831 tf_warning_or_error),
36832 /*is_constant_init*/false, NULL_TREE,
36833 LOOKUP_ONLYCONVERTING);
36834 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36835 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36836 }
36837
36838 /* Parse the restricted form of the for statement allowed by OpenMP. */
36839
36840 static tree
36841 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36842 tree *cclauses, bool *if_p)
36843 {
36844 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36845 tree orig_decl;
36846 tree real_decl, initv, condv, incrv, declv, orig_declv;
36847 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36848 location_t loc_first;
36849 bool collapse_err = false;
36850 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36851 vec<tree, va_gc> *for_block = make_tree_vector ();
36852 auto_vec<tree, 4> orig_inits;
36853 bool tiling = false;
36854
36855 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36856 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36857 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36858 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36859 {
36860 tiling = true;
36861 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36862 }
36863 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36864 && OMP_CLAUSE_ORDERED_EXPR (cl))
36865 {
36866 ordered_cl = cl;
36867 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36868 }
36869
36870 if (ordered && ordered < collapse)
36871 {
36872 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36873 "%<ordered%> clause parameter is less than %<collapse%>");
36874 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36875 = build_int_cst (NULL_TREE, collapse);
36876 ordered = collapse;
36877 }
36878 if (ordered)
36879 {
36880 for (tree *pc = &clauses; *pc; )
36881 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36882 {
36883 error_at (OMP_CLAUSE_LOCATION (*pc),
36884 "%<linear%> clause may not be specified together "
36885 "with %<ordered%> clause with a parameter");
36886 *pc = OMP_CLAUSE_CHAIN (*pc);
36887 }
36888 else
36889 pc = &OMP_CLAUSE_CHAIN (*pc);
36890 }
36891
36892 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36893 count = ordered ? ordered : collapse;
36894
36895 declv = make_tree_vec (count);
36896 initv = make_tree_vec (count);
36897 condv = make_tree_vec (count);
36898 incrv = make_tree_vec (count);
36899 orig_declv = NULL_TREE;
36900
36901 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36902
36903 for (i = 0; i < count; i++)
36904 {
36905 int bracecount = 0;
36906 tree add_private_clause = NULL_TREE;
36907 location_t loc;
36908
36909 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36910 {
36911 if (!collapse_err)
36912 cp_parser_error (parser, "for statement expected");
36913 return NULL;
36914 }
36915 loc = cp_lexer_consume_token (parser->lexer)->location;
36916
36917 /* Don't create location wrapper nodes within an OpenMP "for"
36918 statement. */
36919 auto_suppress_location_wrappers sentinel;
36920
36921 matching_parens parens;
36922 if (!parens.require_open (parser))
36923 return NULL;
36924
36925 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36926 this_pre_body = push_stmt_list ();
36927
36928 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36929 {
36930 /* Save tokens so that we can put them back. */
36931 cp_lexer_save_tokens (parser->lexer);
36932
36933 /* Look for ':' that is not nested in () or {}. */
36934 bool is_range_for
36935 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
36936 /*recovering=*/false,
36937 CPP_COLON,
36938 /*consume_paren=*/
36939 false) == -1);
36940
36941 /* Roll back the tokens we skipped. */
36942 cp_lexer_rollback_tokens (parser->lexer);
36943
36944 if (is_range_for)
36945 {
36946 bool saved_colon_corrects_to_scope_p
36947 = parser->colon_corrects_to_scope_p;
36948
36949 /* A colon is used in range-based for. */
36950 parser->colon_corrects_to_scope_p = false;
36951
36952 /* Parse the declaration. */
36953 cp_parser_simple_declaration (parser,
36954 /*function_definition_allowed_p=*/
36955 false, &decl);
36956 parser->colon_corrects_to_scope_p
36957 = saved_colon_corrects_to_scope_p;
36958
36959 cp_parser_require (parser, CPP_COLON, RT_COLON);
36960
36961 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
36962 false, 0, true);
36963
36964 cp_convert_omp_range_for (this_pre_body, for_block, decl,
36965 orig_decl, init, orig_init,
36966 cond, incr);
36967 if (this_pre_body)
36968 {
36969 if (pre_body)
36970 {
36971 tree t = pre_body;
36972 pre_body = push_stmt_list ();
36973 add_stmt (t);
36974 add_stmt (this_pre_body);
36975 pre_body = pop_stmt_list (pre_body);
36976 }
36977 else
36978 pre_body = this_pre_body;
36979 }
36980
36981 if (ordered_cl)
36982 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36983 "%<ordered%> clause with parameter on "
36984 "range-based %<for%> loop");
36985
36986 goto parse_close_paren;
36987 }
36988 }
36989
36990 add_private_clause
36991 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
36992 init, orig_init, decl, real_decl);
36993
36994 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36995 if (this_pre_body)
36996 {
36997 this_pre_body = pop_stmt_list (this_pre_body);
36998 if (pre_body)
36999 {
37000 tree t = pre_body;
37001 pre_body = push_stmt_list ();
37002 add_stmt (t);
37003 add_stmt (this_pre_body);
37004 pre_body = pop_stmt_list (pre_body);
37005 }
37006 else
37007 pre_body = this_pre_body;
37008 }
37009
37010 if (decl)
37011 real_decl = decl;
37012 if (cclauses != NULL
37013 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
37014 && real_decl != NULL_TREE)
37015 {
37016 tree *c;
37017 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
37018 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
37019 && OMP_CLAUSE_DECL (*c) == real_decl)
37020 {
37021 error_at (loc, "iteration variable %qD"
37022 " should not be firstprivate", real_decl);
37023 *c = OMP_CLAUSE_CHAIN (*c);
37024 }
37025 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
37026 && OMP_CLAUSE_DECL (*c) == real_decl)
37027 {
37028 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
37029 tree l = *c;
37030 *c = OMP_CLAUSE_CHAIN (*c);
37031 if (code == OMP_SIMD)
37032 {
37033 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37034 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
37035 }
37036 else
37037 {
37038 OMP_CLAUSE_CHAIN (l) = clauses;
37039 clauses = l;
37040 }
37041 add_private_clause = NULL_TREE;
37042 }
37043 else
37044 {
37045 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
37046 && OMP_CLAUSE_DECL (*c) == real_decl)
37047 add_private_clause = NULL_TREE;
37048 c = &OMP_CLAUSE_CHAIN (*c);
37049 }
37050 }
37051
37052 if (add_private_clause)
37053 {
37054 tree c;
37055 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
37056 {
37057 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
37058 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
37059 && OMP_CLAUSE_DECL (c) == decl)
37060 break;
37061 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
37062 && OMP_CLAUSE_DECL (c) == decl)
37063 error_at (loc, "iteration variable %qD "
37064 "should not be firstprivate",
37065 decl);
37066 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
37067 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
37068 && OMP_CLAUSE_DECL (c) == decl)
37069 error_at (loc, "iteration variable %qD should not be reduction",
37070 decl);
37071 }
37072 if (c == NULL)
37073 {
37074 if (code != OMP_SIMD)
37075 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
37076 else if (collapse == 1)
37077 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37078 else
37079 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
37080 OMP_CLAUSE_DECL (c) = add_private_clause;
37081 c = finish_omp_clauses (c, C_ORT_OMP);
37082 if (c)
37083 {
37084 OMP_CLAUSE_CHAIN (c) = clauses;
37085 clauses = c;
37086 /* For linear, signal that we need to fill up
37087 the so far unknown linear step. */
37088 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
37089 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
37090 }
37091 }
37092 }
37093
37094 cond = NULL;
37095 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37096 cond = cp_parser_omp_for_cond (parser, decl, code);
37097 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37098
37099 incr = NULL;
37100 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
37101 {
37102 /* If decl is an iterator, preserve the operator on decl
37103 until finish_omp_for. */
37104 if (real_decl
37105 && ((processing_template_decl
37106 && (TREE_TYPE (real_decl) == NULL_TREE
37107 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
37108 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
37109 incr = cp_parser_omp_for_incr (parser, real_decl);
37110 else
37111 incr = cp_parser_expression (parser);
37112 if (!EXPR_HAS_LOCATION (incr))
37113 protected_set_expr_location (incr, input_location);
37114 }
37115
37116 parse_close_paren:
37117 if (!parens.require_close (parser))
37118 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37119 /*or_comma=*/false,
37120 /*consume_paren=*/true);
37121
37122 TREE_VEC_ELT (declv, i) = decl;
37123 TREE_VEC_ELT (initv, i) = init;
37124 TREE_VEC_ELT (condv, i) = cond;
37125 TREE_VEC_ELT (incrv, i) = incr;
37126 if (orig_init)
37127 {
37128 orig_inits.safe_grow_cleared (i + 1);
37129 orig_inits[i] = orig_init;
37130 }
37131 if (orig_decl)
37132 {
37133 if (!orig_declv)
37134 orig_declv = copy_node (declv);
37135 TREE_VEC_ELT (orig_declv, i) = orig_decl;
37136 }
37137 else if (orig_declv)
37138 TREE_VEC_ELT (orig_declv, i) = decl;
37139
37140 if (i == count - 1)
37141 break;
37142
37143 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
37144 in between the collapsed for loops to be still considered perfectly
37145 nested. Hopefully the final version clarifies this.
37146 For now handle (multiple) {'s and empty statements. */
37147 cp_parser_parse_tentatively (parser);
37148 for (;;)
37149 {
37150 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37151 break;
37152 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
37153 {
37154 cp_lexer_consume_token (parser->lexer);
37155 bracecount++;
37156 }
37157 else if (bracecount
37158 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37159 cp_lexer_consume_token (parser->lexer);
37160 else
37161 {
37162 loc = cp_lexer_peek_token (parser->lexer)->location;
37163 error_at (loc, "not enough for loops to collapse");
37164 collapse_err = true;
37165 cp_parser_abort_tentative_parse (parser);
37166 declv = NULL_TREE;
37167 break;
37168 }
37169 }
37170
37171 if (declv)
37172 {
37173 cp_parser_parse_definitely (parser);
37174 nbraces += bracecount;
37175 }
37176 }
37177
37178 if (nbraces)
37179 if_p = NULL;
37180
37181 /* Note that we saved the original contents of this flag when we entered
37182 the structured block, and so we don't need to re-save it here. */
37183 parser->in_statement = IN_OMP_FOR;
37184
37185 /* Note that the grammar doesn't call for a structured block here,
37186 though the loop as a whole is a structured block. */
37187 if (orig_declv)
37188 {
37189 body = begin_omp_structured_block ();
37190 for (i = 0; i < count; i++)
37191 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
37192 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
37193 TREE_VEC_ELT (declv, i));
37194 }
37195 else
37196 body = push_stmt_list ();
37197 cp_parser_statement (parser, NULL_TREE, false, if_p);
37198 if (orig_declv)
37199 body = finish_omp_structured_block (body);
37200 else
37201 body = pop_stmt_list (body);
37202
37203 if (declv == NULL_TREE)
37204 ret = NULL_TREE;
37205 else
37206 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
37207 incrv, body, pre_body, &orig_inits, clauses);
37208
37209 while (nbraces)
37210 {
37211 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
37212 {
37213 cp_lexer_consume_token (parser->lexer);
37214 nbraces--;
37215 }
37216 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37217 cp_lexer_consume_token (parser->lexer);
37218 else
37219 {
37220 if (!collapse_err)
37221 {
37222 error_at (cp_lexer_peek_token (parser->lexer)->location,
37223 "collapsed loops not perfectly nested");
37224 }
37225 collapse_err = true;
37226 cp_parser_statement_seq_opt (parser, NULL);
37227 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
37228 break;
37229 }
37230 }
37231
37232 while (!for_block->is_empty ())
37233 {
37234 tree t = for_block->pop ();
37235 if (TREE_CODE (t) == STATEMENT_LIST)
37236 add_stmt (pop_stmt_list (t));
37237 else
37238 add_stmt (t);
37239 }
37240 release_tree_vector (for_block);
37241
37242 return ret;
37243 }
37244
37245 /* Helper function for OpenMP parsing, split clauses and call
37246 finish_omp_clauses on each of the set of clauses afterwards. */
37247
37248 static void
37249 cp_omp_split_clauses (location_t loc, enum tree_code code,
37250 omp_clause_mask mask, tree clauses, tree *cclauses)
37251 {
37252 int i;
37253 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
37254 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
37255 if (cclauses[i])
37256 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
37257 }
37258
37259 /* OpenMP 4.0:
37260 #pragma omp simd simd-clause[optseq] new-line
37261 for-loop */
37262
37263 #define OMP_SIMD_CLAUSE_MASK \
37264 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
37265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
37274
37275 static tree
37276 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
37277 char *p_name, omp_clause_mask mask, tree *cclauses,
37278 bool *if_p)
37279 {
37280 tree clauses, sb, ret;
37281 unsigned int save;
37282 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37283
37284 strcat (p_name, " simd");
37285 mask |= OMP_SIMD_CLAUSE_MASK;
37286
37287 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37288 cclauses == NULL);
37289 if (cclauses)
37290 {
37291 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
37292 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
37293 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
37294 OMP_CLAUSE_ORDERED);
37295 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
37296 {
37297 error_at (OMP_CLAUSE_LOCATION (c),
37298 "%<ordered%> clause with parameter may not be specified "
37299 "on %qs construct", p_name);
37300 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
37301 }
37302 }
37303
37304 keep_next_level (true);
37305 sb = begin_omp_structured_block ();
37306 save = cp_parser_begin_omp_structured_block (parser);
37307
37308 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
37309
37310 cp_parser_end_omp_structured_block (parser, save);
37311 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37312
37313 return ret;
37314 }
37315
37316 /* OpenMP 2.5:
37317 #pragma omp for for-clause[optseq] new-line
37318 for-loop
37319
37320 OpenMP 4.0:
37321 #pragma omp for simd for-simd-clause[optseq] new-line
37322 for-loop */
37323
37324 #define OMP_FOR_CLAUSE_MASK \
37325 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37334
37335 static tree
37336 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
37337 char *p_name, omp_clause_mask mask, tree *cclauses,
37338 bool *if_p)
37339 {
37340 tree clauses, sb, ret;
37341 unsigned int save;
37342 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37343
37344 strcat (p_name, " for");
37345 mask |= OMP_FOR_CLAUSE_MASK;
37346 /* parallel for{, simd} disallows nowait clause, but for
37347 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37348 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
37349 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37350 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37351 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37352 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
37353
37354 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37355 {
37356 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37357 const char *p = IDENTIFIER_POINTER (id);
37358
37359 if (strcmp (p, "simd") == 0)
37360 {
37361 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37362 if (cclauses == NULL)
37363 cclauses = cclauses_buf;
37364
37365 cp_lexer_consume_token (parser->lexer);
37366 if (!flag_openmp) /* flag_openmp_simd */
37367 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37368 cclauses, if_p);
37369 sb = begin_omp_structured_block ();
37370 save = cp_parser_begin_omp_structured_block (parser);
37371 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37372 cclauses, if_p);
37373 cp_parser_end_omp_structured_block (parser, save);
37374 tree body = finish_omp_structured_block (sb);
37375 if (ret == NULL)
37376 return ret;
37377 ret = make_node (OMP_FOR);
37378 TREE_TYPE (ret) = void_type_node;
37379 OMP_FOR_BODY (ret) = body;
37380 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37381 SET_EXPR_LOCATION (ret, loc);
37382 add_stmt (ret);
37383 return ret;
37384 }
37385 }
37386 if (!flag_openmp) /* flag_openmp_simd */
37387 {
37388 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37389 return NULL_TREE;
37390 }
37391
37392 /* Composite distribute parallel for disallows linear clause. */
37393 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37394 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
37395
37396 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37397 cclauses == NULL);
37398 if (cclauses)
37399 {
37400 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
37401 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37402 }
37403
37404 keep_next_level (true);
37405 sb = begin_omp_structured_block ();
37406 save = cp_parser_begin_omp_structured_block (parser);
37407
37408 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
37409
37410 cp_parser_end_omp_structured_block (parser, save);
37411 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37412
37413 return ret;
37414 }
37415
37416 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
37417 omp_clause_mask, tree *, bool *);
37418
37419 /* OpenMP 2.5:
37420 # pragma omp master new-line
37421 structured-block */
37422
37423 static tree
37424 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
37425 char *p_name, omp_clause_mask mask, tree *cclauses,
37426 bool *if_p)
37427 {
37428 tree clauses, sb, ret;
37429 unsigned int save;
37430 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37431
37432 strcat (p_name, " master");
37433
37434 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37435 {
37436 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37437 const char *p = IDENTIFIER_POINTER (id);
37438
37439 if (strcmp (p, "taskloop") == 0)
37440 {
37441 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37442 if (cclauses == NULL)
37443 cclauses = cclauses_buf;
37444
37445 cp_lexer_consume_token (parser->lexer);
37446 if (!flag_openmp) /* flag_openmp_simd */
37447 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37448 cclauses, if_p);
37449 sb = begin_omp_structured_block ();
37450 save = cp_parser_begin_omp_structured_block (parser);
37451 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37452 cclauses, if_p);
37453 cp_parser_end_omp_structured_block (parser, save);
37454 tree body = finish_omp_structured_block (sb);
37455 if (ret == NULL)
37456 return ret;
37457 return c_finish_omp_master (loc, body);
37458 }
37459 }
37460 if (!flag_openmp) /* flag_openmp_simd */
37461 {
37462 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37463 return NULL_TREE;
37464 }
37465
37466 if (cclauses)
37467 {
37468 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37469 false);
37470 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
37471 }
37472 else
37473 cp_parser_require_pragma_eol (parser, pragma_tok);
37474
37475 return c_finish_omp_master (loc,
37476 cp_parser_omp_structured_block (parser, if_p));
37477 }
37478
37479 /* OpenMP 2.5:
37480 # pragma omp ordered new-line
37481 structured-block
37482
37483 OpenMP 4.5:
37484 # pragma omp ordered ordered-clauses new-line
37485 structured-block */
37486
37487 #define OMP_ORDERED_CLAUSE_MASK \
37488 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37490
37491 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37492 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37493
37494 static bool
37495 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
37496 enum pragma_context context, bool *if_p)
37497 {
37498 location_t loc = pragma_tok->location;
37499
37500 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37501 {
37502 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37503 const char *p = IDENTIFIER_POINTER (id);
37504
37505 if (strcmp (p, "depend") == 0)
37506 {
37507 if (!flag_openmp) /* flag_openmp_simd */
37508 {
37509 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37510 return false;
37511 }
37512 if (context == pragma_stmt)
37513 {
37514 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
37515 "%<depend%> clause may only be used in compound "
37516 "statements");
37517 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37518 return false;
37519 }
37520 tree clauses
37521 = cp_parser_omp_all_clauses (parser,
37522 OMP_ORDERED_DEPEND_CLAUSE_MASK,
37523 "#pragma omp ordered", pragma_tok);
37524 c_finish_omp_ordered (loc, clauses, NULL_TREE);
37525 return false;
37526 }
37527 }
37528
37529 tree clauses
37530 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
37531 "#pragma omp ordered", pragma_tok);
37532
37533 if (!flag_openmp /* flag_openmp_simd */
37534 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
37535 return false;
37536
37537 c_finish_omp_ordered (loc, clauses,
37538 cp_parser_omp_structured_block (parser, if_p));
37539 return true;
37540 }
37541
37542 /* OpenMP 2.5:
37543
37544 section-scope:
37545 { section-sequence }
37546
37547 section-sequence:
37548 section-directive[opt] structured-block
37549 section-sequence section-directive structured-block */
37550
37551 static tree
37552 cp_parser_omp_sections_scope (cp_parser *parser)
37553 {
37554 tree stmt, substmt;
37555 bool error_suppress = false;
37556 cp_token *tok;
37557
37558 matching_braces braces;
37559 if (!braces.require_open (parser))
37560 return NULL_TREE;
37561
37562 stmt = push_stmt_list ();
37563
37564 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
37565 != PRAGMA_OMP_SECTION)
37566 {
37567 substmt = cp_parser_omp_structured_block (parser, NULL);
37568 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37569 add_stmt (substmt);
37570 }
37571
37572 while (1)
37573 {
37574 tok = cp_lexer_peek_token (parser->lexer);
37575 if (tok->type == CPP_CLOSE_BRACE)
37576 break;
37577 if (tok->type == CPP_EOF)
37578 break;
37579
37580 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
37581 {
37582 cp_lexer_consume_token (parser->lexer);
37583 cp_parser_require_pragma_eol (parser, tok);
37584 error_suppress = false;
37585 }
37586 else if (!error_suppress)
37587 {
37588 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
37589 error_suppress = true;
37590 }
37591
37592 substmt = cp_parser_omp_structured_block (parser, NULL);
37593 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37594 add_stmt (substmt);
37595 }
37596 braces.require_close (parser);
37597
37598 substmt = pop_stmt_list (stmt);
37599
37600 stmt = make_node (OMP_SECTIONS);
37601 TREE_TYPE (stmt) = void_type_node;
37602 OMP_SECTIONS_BODY (stmt) = substmt;
37603
37604 add_stmt (stmt);
37605 return stmt;
37606 }
37607
37608 /* OpenMP 2.5:
37609 # pragma omp sections sections-clause[optseq] newline
37610 sections-scope */
37611
37612 #define OMP_SECTIONS_CLAUSE_MASK \
37613 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37618
37619 static tree
37620 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
37621 char *p_name, omp_clause_mask mask, tree *cclauses)
37622 {
37623 tree clauses, ret;
37624 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37625
37626 strcat (p_name, " sections");
37627 mask |= OMP_SECTIONS_CLAUSE_MASK;
37628 if (cclauses)
37629 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37630
37631 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37632 cclauses == NULL);
37633 if (cclauses)
37634 {
37635 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
37636 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
37637 }
37638
37639 ret = cp_parser_omp_sections_scope (parser);
37640 if (ret)
37641 OMP_SECTIONS_CLAUSES (ret) = clauses;
37642
37643 return ret;
37644 }
37645
37646 /* OpenMP 2.5:
37647 # pragma omp parallel parallel-clause[optseq] new-line
37648 structured-block
37649 # pragma omp parallel for parallel-for-clause[optseq] new-line
37650 structured-block
37651 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37652 structured-block
37653
37654 OpenMP 4.0:
37655 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37656 structured-block */
37657
37658 #define OMP_PARALLEL_CLAUSE_MASK \
37659 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37668
37669 static tree
37670 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
37671 char *p_name, omp_clause_mask mask, tree *cclauses,
37672 bool *if_p)
37673 {
37674 tree stmt, clauses, block;
37675 unsigned int save;
37676 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37677
37678 strcat (p_name, " parallel");
37679 mask |= OMP_PARALLEL_CLAUSE_MASK;
37680 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37681 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
37682 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
37683 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37684
37685 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37686 {
37687 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37688 if (cclauses == NULL)
37689 cclauses = cclauses_buf;
37690
37691 cp_lexer_consume_token (parser->lexer);
37692 if (!flag_openmp) /* flag_openmp_simd */
37693 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37694 if_p);
37695 block = begin_omp_parallel ();
37696 save = cp_parser_begin_omp_structured_block (parser);
37697 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37698 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 if (ret == NULL_TREE)
37703 return ret;
37704 OMP_PARALLEL_COMBINED (stmt) = 1;
37705 return stmt;
37706 }
37707 /* When combined with distribute, parallel has to be followed by for.
37708 #pragma omp target parallel is allowed though. */
37709 else if (cclauses
37710 && (mask & (OMP_CLAUSE_MASK_1
37711 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37712 {
37713 error_at (loc, "expected %<for%> after %qs", p_name);
37714 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37715 return NULL_TREE;
37716 }
37717 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37718 {
37719 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37720 const char *p = IDENTIFIER_POINTER (id);
37721 if (strcmp (p, "master") == 0)
37722 {
37723 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37724 cclauses = cclauses_buf;
37725
37726 cp_lexer_consume_token (parser->lexer);
37727 block = begin_omp_parallel ();
37728 save = cp_parser_begin_omp_structured_block (parser);
37729 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37730 cclauses, if_p);
37731 cp_parser_end_omp_structured_block (parser, save);
37732 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37733 block);
37734 OMP_PARALLEL_COMBINED (stmt) = 1;
37735 if (ret == NULL_TREE)
37736 return ret;
37737 return stmt;
37738 }
37739 else if (!flag_openmp) /* flag_openmp_simd */
37740 {
37741 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37742 return NULL_TREE;
37743 }
37744 else if (strcmp (p, "sections") == 0)
37745 {
37746 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37747 cclauses = cclauses_buf;
37748
37749 cp_lexer_consume_token (parser->lexer);
37750 block = begin_omp_parallel ();
37751 save = cp_parser_begin_omp_structured_block (parser);
37752 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37753 cp_parser_end_omp_structured_block (parser, save);
37754 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37755 block);
37756 OMP_PARALLEL_COMBINED (stmt) = 1;
37757 return stmt;
37758 }
37759 }
37760 else if (!flag_openmp) /* flag_openmp_simd */
37761 {
37762 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37763 return NULL_TREE;
37764 }
37765
37766 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37767 cclauses == NULL);
37768 if (cclauses)
37769 {
37770 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37771 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37772 }
37773
37774 block = begin_omp_parallel ();
37775 save = cp_parser_begin_omp_structured_block (parser);
37776 cp_parser_statement (parser, NULL_TREE, false, if_p);
37777 cp_parser_end_omp_structured_block (parser, save);
37778 stmt = finish_omp_parallel (clauses, block);
37779 return stmt;
37780 }
37781
37782 /* OpenMP 2.5:
37783 # pragma omp single single-clause[optseq] new-line
37784 structured-block */
37785
37786 #define OMP_SINGLE_CLAUSE_MASK \
37787 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37791
37792 static tree
37793 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37794 {
37795 tree stmt = make_node (OMP_SINGLE);
37796 TREE_TYPE (stmt) = void_type_node;
37797 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37798
37799 OMP_SINGLE_CLAUSES (stmt)
37800 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37801 "#pragma omp single", pragma_tok);
37802 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37803
37804 return add_stmt (stmt);
37805 }
37806
37807 /* OpenMP 3.0:
37808 # pragma omp task task-clause[optseq] new-line
37809 structured-block */
37810
37811 #define OMP_TASK_CLAUSE_MASK \
37812 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37823
37824 static tree
37825 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37826 {
37827 tree clauses, block;
37828 unsigned int save;
37829
37830 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37831 "#pragma omp task", pragma_tok);
37832 block = begin_omp_task ();
37833 save = cp_parser_begin_omp_structured_block (parser);
37834 cp_parser_statement (parser, NULL_TREE, false, if_p);
37835 cp_parser_end_omp_structured_block (parser, save);
37836 return finish_omp_task (clauses, block);
37837 }
37838
37839 /* OpenMP 3.0:
37840 # pragma omp taskwait new-line
37841
37842 OpenMP 5.0:
37843 # pragma omp taskwait taskwait-clause[opt] new-line */
37844
37845 #define OMP_TASKWAIT_CLAUSE_MASK \
37846 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37847
37848 static void
37849 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37850 {
37851 tree clauses
37852 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37853 "#pragma omp taskwait", pragma_tok);
37854
37855 if (clauses)
37856 {
37857 tree stmt = make_node (OMP_TASK);
37858 TREE_TYPE (stmt) = void_node;
37859 OMP_TASK_CLAUSES (stmt) = clauses;
37860 OMP_TASK_BODY (stmt) = NULL_TREE;
37861 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37862 add_stmt (stmt);
37863 }
37864 else
37865 finish_omp_taskwait ();
37866 }
37867
37868 /* OpenMP 3.1:
37869 # pragma omp taskyield new-line */
37870
37871 static void
37872 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37873 {
37874 cp_parser_require_pragma_eol (parser, pragma_tok);
37875 finish_omp_taskyield ();
37876 }
37877
37878 /* OpenMP 4.0:
37879 # pragma omp taskgroup new-line
37880 structured-block
37881
37882 OpenMP 5.0:
37883 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37884
37885 #define OMP_TASKGROUP_CLAUSE_MASK \
37886 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37887
37888 static tree
37889 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37890 {
37891 tree clauses
37892 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37893 "#pragma omp taskgroup", pragma_tok);
37894 return c_finish_omp_taskgroup (input_location,
37895 cp_parser_omp_structured_block (parser,
37896 if_p),
37897 clauses);
37898 }
37899
37900
37901 /* OpenMP 2.5:
37902 # pragma omp threadprivate (variable-list) */
37903
37904 static void
37905 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37906 {
37907 tree vars;
37908
37909 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37910 cp_parser_require_pragma_eol (parser, pragma_tok);
37911
37912 finish_omp_threadprivate (vars);
37913 }
37914
37915 /* OpenMP 4.0:
37916 # pragma omp cancel cancel-clause[optseq] new-line */
37917
37918 #define OMP_CANCEL_CLAUSE_MASK \
37919 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37924
37925 static void
37926 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37927 {
37928 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
37929 "#pragma omp cancel", pragma_tok);
37930 finish_omp_cancel (clauses);
37931 }
37932
37933 /* OpenMP 4.0:
37934 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37935
37936 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37937 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37941
37942 static void
37943 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
37944 enum pragma_context context)
37945 {
37946 tree clauses;
37947 bool point_seen = false;
37948
37949 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37950 {
37951 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37952 const char *p = IDENTIFIER_POINTER (id);
37953
37954 if (strcmp (p, "point") == 0)
37955 {
37956 cp_lexer_consume_token (parser->lexer);
37957 point_seen = true;
37958 }
37959 }
37960 if (!point_seen)
37961 {
37962 cp_parser_error (parser, "expected %<point%>");
37963 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37964 return;
37965 }
37966
37967 if (context != pragma_compound)
37968 {
37969 if (context == pragma_stmt)
37970 error_at (pragma_tok->location,
37971 "%<#pragma %s%> may only be used in compound statements",
37972 "omp cancellation point");
37973 else
37974 cp_parser_error (parser, "expected declaration specifiers");
37975 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37976 return;
37977 }
37978
37979 clauses = cp_parser_omp_all_clauses (parser,
37980 OMP_CANCELLATION_POINT_CLAUSE_MASK,
37981 "#pragma omp cancellation point",
37982 pragma_tok);
37983 finish_omp_cancellation_point (clauses);
37984 }
37985
37986 /* OpenMP 4.0:
37987 #pragma omp distribute distribute-clause[optseq] new-line
37988 for-loop */
37989
37990 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37991 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37996
37997 static tree
37998 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
37999 char *p_name, omp_clause_mask mask, tree *cclauses,
38000 bool *if_p)
38001 {
38002 tree clauses, sb, ret;
38003 unsigned int save;
38004 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38005
38006 strcat (p_name, " distribute");
38007 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
38008
38009 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38010 {
38011 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38012 const char *p = IDENTIFIER_POINTER (id);
38013 bool simd = false;
38014 bool parallel = false;
38015
38016 if (strcmp (p, "simd") == 0)
38017 simd = true;
38018 else
38019 parallel = strcmp (p, "parallel") == 0;
38020 if (parallel || simd)
38021 {
38022 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38023 if (cclauses == NULL)
38024 cclauses = cclauses_buf;
38025 cp_lexer_consume_token (parser->lexer);
38026 if (!flag_openmp) /* flag_openmp_simd */
38027 {
38028 if (simd)
38029 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38030 cclauses, if_p);
38031 else
38032 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
38033 cclauses, if_p);
38034 }
38035 sb = begin_omp_structured_block ();
38036 save = cp_parser_begin_omp_structured_block (parser);
38037 if (simd)
38038 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38039 cclauses, if_p);
38040 else
38041 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
38042 cclauses, if_p);
38043 cp_parser_end_omp_structured_block (parser, save);
38044 tree body = finish_omp_structured_block (sb);
38045 if (ret == NULL)
38046 return ret;
38047 ret = make_node (OMP_DISTRIBUTE);
38048 TREE_TYPE (ret) = void_type_node;
38049 OMP_FOR_BODY (ret) = body;
38050 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
38051 SET_EXPR_LOCATION (ret, loc);
38052 add_stmt (ret);
38053 return ret;
38054 }
38055 }
38056 if (!flag_openmp) /* flag_openmp_simd */
38057 {
38058 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38059 return NULL_TREE;
38060 }
38061
38062 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38063 cclauses == NULL);
38064 if (cclauses)
38065 {
38066 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
38067 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
38068 }
38069
38070 keep_next_level (true);
38071 sb = begin_omp_structured_block ();
38072 save = cp_parser_begin_omp_structured_block (parser);
38073
38074 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
38075
38076 cp_parser_end_omp_structured_block (parser, save);
38077 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
38078
38079 return ret;
38080 }
38081
38082 /* OpenMP 4.0:
38083 # pragma omp teams teams-clause[optseq] new-line
38084 structured-block */
38085
38086 #define OMP_TEAMS_CLAUSE_MASK \
38087 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
38092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
38093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
38094
38095 static tree
38096 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
38097 char *p_name, omp_clause_mask mask, tree *cclauses,
38098 bool *if_p)
38099 {
38100 tree clauses, sb, ret;
38101 unsigned int save;
38102 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38103
38104 strcat (p_name, " teams");
38105 mask |= OMP_TEAMS_CLAUSE_MASK;
38106
38107 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38108 {
38109 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38110 const char *p = IDENTIFIER_POINTER (id);
38111 if (strcmp (p, "distribute") == 0)
38112 {
38113 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38114 if (cclauses == NULL)
38115 cclauses = cclauses_buf;
38116
38117 cp_lexer_consume_token (parser->lexer);
38118 if (!flag_openmp) /* flag_openmp_simd */
38119 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
38120 cclauses, if_p);
38121 keep_next_level (true);
38122 sb = begin_omp_structured_block ();
38123 save = cp_parser_begin_omp_structured_block (parser);
38124 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
38125 cclauses, if_p);
38126 cp_parser_end_omp_structured_block (parser, save);
38127 tree body = finish_omp_structured_block (sb);
38128 if (ret == NULL)
38129 return ret;
38130 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38131 ret = make_node (OMP_TEAMS);
38132 TREE_TYPE (ret) = void_type_node;
38133 OMP_TEAMS_CLAUSES (ret) = clauses;
38134 OMP_TEAMS_BODY (ret) = body;
38135 OMP_TEAMS_COMBINED (ret) = 1;
38136 SET_EXPR_LOCATION (ret, loc);
38137 return add_stmt (ret);
38138 }
38139 }
38140 if (!flag_openmp) /* flag_openmp_simd */
38141 {
38142 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38143 return NULL_TREE;
38144 }
38145
38146 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38147 cclauses == NULL);
38148 if (cclauses)
38149 {
38150 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
38151 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38152 }
38153
38154 tree stmt = make_node (OMP_TEAMS);
38155 TREE_TYPE (stmt) = void_type_node;
38156 OMP_TEAMS_CLAUSES (stmt) = clauses;
38157 keep_next_level (true);
38158 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38159 SET_EXPR_LOCATION (stmt, loc);
38160
38161 return add_stmt (stmt);
38162 }
38163
38164 /* OpenMP 4.0:
38165 # pragma omp target data target-data-clause[optseq] new-line
38166 structured-block */
38167
38168 #define OMP_TARGET_DATA_CLAUSE_MASK \
38169 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
38173
38174 static tree
38175 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38176 {
38177 tree clauses
38178 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
38179 "#pragma omp target data", pragma_tok);
38180 int map_seen = 0;
38181 for (tree *pc = &clauses; *pc;)
38182 {
38183 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38184 switch (OMP_CLAUSE_MAP_KIND (*pc))
38185 {
38186 case GOMP_MAP_TO:
38187 case GOMP_MAP_ALWAYS_TO:
38188 case GOMP_MAP_FROM:
38189 case GOMP_MAP_ALWAYS_FROM:
38190 case GOMP_MAP_TOFROM:
38191 case GOMP_MAP_ALWAYS_TOFROM:
38192 case GOMP_MAP_ALLOC:
38193 map_seen = 3;
38194 break;
38195 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38196 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38197 case GOMP_MAP_ALWAYS_POINTER:
38198 break;
38199 default:
38200 map_seen |= 1;
38201 error_at (OMP_CLAUSE_LOCATION (*pc),
38202 "%<#pragma omp target data%> with map-type other "
38203 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38204 "on %<map%> clause");
38205 *pc = OMP_CLAUSE_CHAIN (*pc);
38206 continue;
38207 }
38208 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
38209 map_seen = 3;
38210 pc = &OMP_CLAUSE_CHAIN (*pc);
38211 }
38212
38213 if (map_seen != 3)
38214 {
38215 if (map_seen == 0)
38216 error_at (pragma_tok->location,
38217 "%<#pragma omp target data%> must contain at least "
38218 "one %<map%> or %<use_device_ptr%> clause");
38219 return NULL_TREE;
38220 }
38221
38222 tree stmt = make_node (OMP_TARGET_DATA);
38223 TREE_TYPE (stmt) = void_type_node;
38224 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
38225
38226 keep_next_level (true);
38227 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38228
38229 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38230 return add_stmt (stmt);
38231 }
38232
38233 /* OpenMP 4.5:
38234 # pragma omp target enter data target-enter-data-clause[optseq] new-line
38235 structured-block */
38236
38237 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
38238 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38243
38244 static tree
38245 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
38246 enum pragma_context context)
38247 {
38248 bool data_seen = false;
38249 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38250 {
38251 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38252 const char *p = IDENTIFIER_POINTER (id);
38253
38254 if (strcmp (p, "data") == 0)
38255 {
38256 cp_lexer_consume_token (parser->lexer);
38257 data_seen = true;
38258 }
38259 }
38260 if (!data_seen)
38261 {
38262 cp_parser_error (parser, "expected %<data%>");
38263 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38264 return NULL_TREE;
38265 }
38266
38267 if (context == pragma_stmt)
38268 {
38269 error_at (pragma_tok->location,
38270 "%<#pragma %s%> may only be used in compound statements",
38271 "omp target enter data");
38272 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38273 return NULL_TREE;
38274 }
38275
38276 tree clauses
38277 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
38278 "#pragma omp target enter data", pragma_tok);
38279 int map_seen = 0;
38280 for (tree *pc = &clauses; *pc;)
38281 {
38282 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38283 switch (OMP_CLAUSE_MAP_KIND (*pc))
38284 {
38285 case GOMP_MAP_TO:
38286 case GOMP_MAP_ALWAYS_TO:
38287 case GOMP_MAP_ALLOC:
38288 map_seen = 3;
38289 break;
38290 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38291 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38292 case GOMP_MAP_ALWAYS_POINTER:
38293 break;
38294 default:
38295 map_seen |= 1;
38296 error_at (OMP_CLAUSE_LOCATION (*pc),
38297 "%<#pragma omp target enter data%> with map-type other "
38298 "than %<to%> or %<alloc%> on %<map%> clause");
38299 *pc = OMP_CLAUSE_CHAIN (*pc);
38300 continue;
38301 }
38302 pc = &OMP_CLAUSE_CHAIN (*pc);
38303 }
38304
38305 if (map_seen != 3)
38306 {
38307 if (map_seen == 0)
38308 error_at (pragma_tok->location,
38309 "%<#pragma omp target enter data%> must contain at least "
38310 "one %<map%> clause");
38311 return NULL_TREE;
38312 }
38313
38314 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
38315 TREE_TYPE (stmt) = void_type_node;
38316 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
38317 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38318 return add_stmt (stmt);
38319 }
38320
38321 /* OpenMP 4.5:
38322 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38323 structured-block */
38324
38325 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38326 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38331
38332 static tree
38333 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
38334 enum pragma_context context)
38335 {
38336 bool data_seen = false;
38337 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38338 {
38339 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38340 const char *p = IDENTIFIER_POINTER (id);
38341
38342 if (strcmp (p, "data") == 0)
38343 {
38344 cp_lexer_consume_token (parser->lexer);
38345 data_seen = true;
38346 }
38347 }
38348 if (!data_seen)
38349 {
38350 cp_parser_error (parser, "expected %<data%>");
38351 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38352 return NULL_TREE;
38353 }
38354
38355 if (context == pragma_stmt)
38356 {
38357 error_at (pragma_tok->location,
38358 "%<#pragma %s%> may only be used in compound statements",
38359 "omp target exit data");
38360 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38361 return NULL_TREE;
38362 }
38363
38364 tree clauses
38365 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
38366 "#pragma omp target exit data", pragma_tok);
38367 int map_seen = 0;
38368 for (tree *pc = &clauses; *pc;)
38369 {
38370 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38371 switch (OMP_CLAUSE_MAP_KIND (*pc))
38372 {
38373 case GOMP_MAP_FROM:
38374 case GOMP_MAP_ALWAYS_FROM:
38375 case GOMP_MAP_RELEASE:
38376 case GOMP_MAP_DELETE:
38377 map_seen = 3;
38378 break;
38379 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38380 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38381 case GOMP_MAP_ALWAYS_POINTER:
38382 break;
38383 default:
38384 map_seen |= 1;
38385 error_at (OMP_CLAUSE_LOCATION (*pc),
38386 "%<#pragma omp target exit data%> with map-type other "
38387 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38388 " clause");
38389 *pc = OMP_CLAUSE_CHAIN (*pc);
38390 continue;
38391 }
38392 pc = &OMP_CLAUSE_CHAIN (*pc);
38393 }
38394
38395 if (map_seen != 3)
38396 {
38397 if (map_seen == 0)
38398 error_at (pragma_tok->location,
38399 "%<#pragma omp target exit data%> must contain at least "
38400 "one %<map%> clause");
38401 return NULL_TREE;
38402 }
38403
38404 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
38405 TREE_TYPE (stmt) = void_type_node;
38406 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
38407 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38408 return add_stmt (stmt);
38409 }
38410
38411 /* OpenMP 4.0:
38412 # pragma omp target update target-update-clause[optseq] new-line */
38413
38414 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38415 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38421
38422 static bool
38423 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
38424 enum pragma_context context)
38425 {
38426 if (context == pragma_stmt)
38427 {
38428 error_at (pragma_tok->location,
38429 "%<#pragma %s%> may only be used in compound statements",
38430 "omp target update");
38431 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38432 return false;
38433 }
38434
38435 tree clauses
38436 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
38437 "#pragma omp target update", pragma_tok);
38438 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
38439 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
38440 {
38441 error_at (pragma_tok->location,
38442 "%<#pragma omp target update%> must contain at least one "
38443 "%<from%> or %<to%> clauses");
38444 return false;
38445 }
38446
38447 tree stmt = make_node (OMP_TARGET_UPDATE);
38448 TREE_TYPE (stmt) = void_type_node;
38449 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
38450 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38451 add_stmt (stmt);
38452 return false;
38453 }
38454
38455 /* OpenMP 4.0:
38456 # pragma omp target target-clause[optseq] new-line
38457 structured-block */
38458
38459 #define OMP_TARGET_CLAUSE_MASK \
38460 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38469
38470 static bool
38471 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
38472 enum pragma_context context, bool *if_p)
38473 {
38474 tree *pc = NULL, stmt;
38475
38476 if (flag_openmp)
38477 omp_requires_mask
38478 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
38479
38480 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38481 {
38482 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38483 const char *p = IDENTIFIER_POINTER (id);
38484 enum tree_code ccode = ERROR_MARK;
38485
38486 if (strcmp (p, "teams") == 0)
38487 ccode = OMP_TEAMS;
38488 else if (strcmp (p, "parallel") == 0)
38489 ccode = OMP_PARALLEL;
38490 else if (strcmp (p, "simd") == 0)
38491 ccode = OMP_SIMD;
38492 if (ccode != ERROR_MARK)
38493 {
38494 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
38495 char p_name[sizeof ("#pragma omp target teams distribute "
38496 "parallel for simd")];
38497
38498 cp_lexer_consume_token (parser->lexer);
38499 strcpy (p_name, "#pragma omp target");
38500 if (!flag_openmp) /* flag_openmp_simd */
38501 {
38502 tree stmt;
38503 switch (ccode)
38504 {
38505 case OMP_TEAMS:
38506 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
38507 OMP_TARGET_CLAUSE_MASK,
38508 cclauses, if_p);
38509 break;
38510 case OMP_PARALLEL:
38511 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38512 OMP_TARGET_CLAUSE_MASK,
38513 cclauses, if_p);
38514 break;
38515 case OMP_SIMD:
38516 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
38517 OMP_TARGET_CLAUSE_MASK,
38518 cclauses, if_p);
38519 break;
38520 default:
38521 gcc_unreachable ();
38522 }
38523 return stmt != NULL_TREE;
38524 }
38525 keep_next_level (true);
38526 tree sb = begin_omp_structured_block (), ret;
38527 unsigned save = cp_parser_begin_omp_structured_block (parser);
38528 switch (ccode)
38529 {
38530 case OMP_TEAMS:
38531 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
38532 OMP_TARGET_CLAUSE_MASK, cclauses,
38533 if_p);
38534 break;
38535 case OMP_PARALLEL:
38536 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38537 OMP_TARGET_CLAUSE_MASK, cclauses,
38538 if_p);
38539 break;
38540 case OMP_SIMD:
38541 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
38542 OMP_TARGET_CLAUSE_MASK, cclauses,
38543 if_p);
38544 break;
38545 default:
38546 gcc_unreachable ();
38547 }
38548 cp_parser_end_omp_structured_block (parser, save);
38549 tree body = finish_omp_structured_block (sb);
38550 if (ret == NULL_TREE)
38551 return false;
38552 if (ccode == OMP_TEAMS && !processing_template_decl)
38553 {
38554 /* For combined target teams, ensure the num_teams and
38555 thread_limit clause expressions are evaluated on the host,
38556 before entering the target construct. */
38557 tree c;
38558 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38559 c; c = OMP_CLAUSE_CHAIN (c))
38560 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
38561 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
38562 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
38563 {
38564 tree expr = OMP_CLAUSE_OPERAND (c, 0);
38565 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
38566 if (expr == error_mark_node)
38567 continue;
38568 tree tmp = TARGET_EXPR_SLOT (expr);
38569 add_stmt (expr);
38570 OMP_CLAUSE_OPERAND (c, 0) = expr;
38571 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
38572 OMP_CLAUSE_FIRSTPRIVATE);
38573 OMP_CLAUSE_DECL (tc) = tmp;
38574 OMP_CLAUSE_CHAIN (tc)
38575 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38576 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
38577 }
38578 }
38579 tree stmt = make_node (OMP_TARGET);
38580 TREE_TYPE (stmt) = void_type_node;
38581 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38582 OMP_TARGET_BODY (stmt) = body;
38583 OMP_TARGET_COMBINED (stmt) = 1;
38584 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38585 add_stmt (stmt);
38586 pc = &OMP_TARGET_CLAUSES (stmt);
38587 goto check_clauses;
38588 }
38589 else if (!flag_openmp) /* flag_openmp_simd */
38590 {
38591 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38592 return false;
38593 }
38594 else if (strcmp (p, "data") == 0)
38595 {
38596 cp_lexer_consume_token (parser->lexer);
38597 cp_parser_omp_target_data (parser, pragma_tok, if_p);
38598 return true;
38599 }
38600 else if (strcmp (p, "enter") == 0)
38601 {
38602 cp_lexer_consume_token (parser->lexer);
38603 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
38604 return false;
38605 }
38606 else if (strcmp (p, "exit") == 0)
38607 {
38608 cp_lexer_consume_token (parser->lexer);
38609 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
38610 return false;
38611 }
38612 else if (strcmp (p, "update") == 0)
38613 {
38614 cp_lexer_consume_token (parser->lexer);
38615 return cp_parser_omp_target_update (parser, pragma_tok, context);
38616 }
38617 }
38618 if (!flag_openmp) /* flag_openmp_simd */
38619 {
38620 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38621 return false;
38622 }
38623
38624 stmt = make_node (OMP_TARGET);
38625 TREE_TYPE (stmt) = void_type_node;
38626
38627 OMP_TARGET_CLAUSES (stmt)
38628 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
38629 "#pragma omp target", pragma_tok);
38630 pc = &OMP_TARGET_CLAUSES (stmt);
38631 keep_next_level (true);
38632 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38633
38634 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38635 add_stmt (stmt);
38636
38637 check_clauses:
38638 while (*pc)
38639 {
38640 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38641 switch (OMP_CLAUSE_MAP_KIND (*pc))
38642 {
38643 case GOMP_MAP_TO:
38644 case GOMP_MAP_ALWAYS_TO:
38645 case GOMP_MAP_FROM:
38646 case GOMP_MAP_ALWAYS_FROM:
38647 case GOMP_MAP_TOFROM:
38648 case GOMP_MAP_ALWAYS_TOFROM:
38649 case GOMP_MAP_ALLOC:
38650 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38651 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38652 case GOMP_MAP_ALWAYS_POINTER:
38653 break;
38654 default:
38655 error_at (OMP_CLAUSE_LOCATION (*pc),
38656 "%<#pragma omp target%> with map-type other "
38657 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38658 "on %<map%> clause");
38659 *pc = OMP_CLAUSE_CHAIN (*pc);
38660 continue;
38661 }
38662 pc = &OMP_CLAUSE_CHAIN (*pc);
38663 }
38664 return true;
38665 }
38666
38667 /* OpenACC 2.0:
38668 # pragma acc cache (variable-list) new-line
38669 */
38670
38671 static tree
38672 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
38673 {
38674 tree stmt, clauses;
38675
38676 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
38677 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38678
38679 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
38680
38681 stmt = make_node (OACC_CACHE);
38682 TREE_TYPE (stmt) = void_type_node;
38683 OACC_CACHE_CLAUSES (stmt) = clauses;
38684 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38685 add_stmt (stmt);
38686
38687 return stmt;
38688 }
38689
38690 /* OpenACC 2.0:
38691 # pragma acc data oacc-data-clause[optseq] new-line
38692 structured-block */
38693
38694 #define OACC_DATA_CLAUSE_MASK \
38695 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38702
38703 static tree
38704 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38705 {
38706 tree stmt, clauses, block;
38707 unsigned int save;
38708
38709 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38710 "#pragma acc data", pragma_tok);
38711
38712 block = begin_omp_parallel ();
38713 save = cp_parser_begin_omp_structured_block (parser);
38714 cp_parser_statement (parser, NULL_TREE, false, if_p);
38715 cp_parser_end_omp_structured_block (parser, save);
38716 stmt = finish_oacc_data (clauses, block);
38717 return stmt;
38718 }
38719
38720 /* OpenACC 2.0:
38721 # pragma acc host_data <clauses> new-line
38722 structured-block */
38723
38724 #define OACC_HOST_DATA_CLAUSE_MASK \
38725 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38726
38727 static tree
38728 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38729 {
38730 tree stmt, clauses, block;
38731 unsigned int save;
38732
38733 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38734 "#pragma acc host_data", pragma_tok);
38735
38736 block = begin_omp_parallel ();
38737 save = cp_parser_begin_omp_structured_block (parser);
38738 cp_parser_statement (parser, NULL_TREE, false, if_p);
38739 cp_parser_end_omp_structured_block (parser, save);
38740 stmt = finish_oacc_host_data (clauses, block);
38741 return stmt;
38742 }
38743
38744 /* OpenACC 2.0:
38745 # pragma acc declare oacc-data-clause[optseq] new-line
38746 */
38747
38748 #define OACC_DECLARE_CLAUSE_MASK \
38749 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38757
38758 static tree
38759 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38760 {
38761 tree clauses, stmt;
38762 bool error = false;
38763
38764 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38765 "#pragma acc declare", pragma_tok, true);
38766
38767
38768 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38769 {
38770 error_at (pragma_tok->location,
38771 "no valid clauses specified in %<#pragma acc declare%>");
38772 return NULL_TREE;
38773 }
38774
38775 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38776 {
38777 location_t loc = OMP_CLAUSE_LOCATION (t);
38778 tree decl = OMP_CLAUSE_DECL (t);
38779 if (!DECL_P (decl))
38780 {
38781 error_at (loc, "array section in %<#pragma acc declare%>");
38782 error = true;
38783 continue;
38784 }
38785 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38786 switch (OMP_CLAUSE_MAP_KIND (t))
38787 {
38788 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38789 case GOMP_MAP_ALLOC:
38790 case GOMP_MAP_TO:
38791 case GOMP_MAP_FORCE_DEVICEPTR:
38792 case GOMP_MAP_DEVICE_RESIDENT:
38793 break;
38794
38795 case GOMP_MAP_LINK:
38796 if (!global_bindings_p ()
38797 && (TREE_STATIC (decl)
38798 || !DECL_EXTERNAL (decl)))
38799 {
38800 error_at (loc,
38801 "%qD must be a global variable in "
38802 "%<#pragma acc declare link%>",
38803 decl);
38804 error = true;
38805 continue;
38806 }
38807 break;
38808
38809 default:
38810 if (global_bindings_p ())
38811 {
38812 error_at (loc, "invalid OpenACC clause at file scope");
38813 error = true;
38814 continue;
38815 }
38816 if (DECL_EXTERNAL (decl))
38817 {
38818 error_at (loc,
38819 "invalid use of %<extern%> variable %qD "
38820 "in %<#pragma acc declare%>", decl);
38821 error = true;
38822 continue;
38823 }
38824 else if (TREE_PUBLIC (decl))
38825 {
38826 error_at (loc,
38827 "invalid use of %<global%> variable %qD "
38828 "in %<#pragma acc declare%>", decl);
38829 error = true;
38830 continue;
38831 }
38832 break;
38833 }
38834
38835 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38836 || lookup_attribute ("omp declare target link",
38837 DECL_ATTRIBUTES (decl)))
38838 {
38839 error_at (loc, "variable %qD used more than once with "
38840 "%<#pragma acc declare%>", decl);
38841 error = true;
38842 continue;
38843 }
38844
38845 if (!error)
38846 {
38847 tree id;
38848
38849 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38850 id = get_identifier ("omp declare target link");
38851 else
38852 id = get_identifier ("omp declare target");
38853
38854 DECL_ATTRIBUTES (decl)
38855 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38856 if (global_bindings_p ())
38857 {
38858 symtab_node *node = symtab_node::get (decl);
38859 if (node != NULL)
38860 {
38861 node->offloadable = 1;
38862 if (ENABLE_OFFLOADING)
38863 {
38864 g->have_offload = true;
38865 if (is_a <varpool_node *> (node))
38866 vec_safe_push (offload_vars, decl);
38867 }
38868 }
38869 }
38870 }
38871 }
38872
38873 if (error || global_bindings_p ())
38874 return NULL_TREE;
38875
38876 stmt = make_node (OACC_DECLARE);
38877 TREE_TYPE (stmt) = void_type_node;
38878 OACC_DECLARE_CLAUSES (stmt) = clauses;
38879 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38880
38881 add_stmt (stmt);
38882
38883 return NULL_TREE;
38884 }
38885
38886 /* OpenACC 2.0:
38887 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38888
38889 or
38890
38891 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38892
38893 LOC is the location of the #pragma token.
38894 */
38895
38896 #define OACC_ENTER_DATA_CLAUSE_MASK \
38897 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38902
38903 #define OACC_EXIT_DATA_CLAUSE_MASK \
38904 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38910
38911 static tree
38912 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38913 bool enter)
38914 {
38915 location_t loc = pragma_tok->location;
38916 tree stmt, clauses;
38917 const char *p = "";
38918
38919 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38920 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38921
38922 if (strcmp (p, "data") != 0)
38923 {
38924 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38925 enter ? "enter" : "exit");
38926 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38927 return NULL_TREE;
38928 }
38929
38930 cp_lexer_consume_token (parser->lexer);
38931
38932 if (enter)
38933 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
38934 "#pragma acc enter data", pragma_tok);
38935 else
38936 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
38937 "#pragma acc exit data", pragma_tok);
38938
38939 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38940 {
38941 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
38942 enter ? "enter" : "exit");
38943 return NULL_TREE;
38944 }
38945
38946 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
38947 TREE_TYPE (stmt) = void_type_node;
38948 OMP_STANDALONE_CLAUSES (stmt) = clauses;
38949 SET_EXPR_LOCATION (stmt, loc);
38950 add_stmt (stmt);
38951 return stmt;
38952 }
38953
38954 /* OpenACC 2.0:
38955 # pragma acc loop oacc-loop-clause[optseq] new-line
38956 structured-block */
38957
38958 #define OACC_LOOP_CLAUSE_MASK \
38959 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38969
38970 static tree
38971 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
38972 omp_clause_mask mask, tree *cclauses, bool *if_p)
38973 {
38974 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
38975
38976 strcat (p_name, " loop");
38977 mask |= OACC_LOOP_CLAUSE_MASK;
38978
38979 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
38980 cclauses == NULL);
38981 if (cclauses)
38982 {
38983 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
38984 if (*cclauses)
38985 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
38986 if (clauses)
38987 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38988 }
38989
38990 tree block = begin_omp_structured_block ();
38991 int save = cp_parser_begin_omp_structured_block (parser);
38992 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
38993 cp_parser_end_omp_structured_block (parser, save);
38994 add_stmt (finish_omp_structured_block (block));
38995
38996 return stmt;
38997 }
38998
38999 /* OpenACC 2.0:
39000 # pragma acc kernels oacc-kernels-clause[optseq] new-line
39001 structured-block
39002
39003 or
39004
39005 # pragma acc parallel oacc-parallel-clause[optseq] new-line
39006 structured-block
39007 */
39008
39009 #define OACC_KERNELS_CLAUSE_MASK \
39010 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
39016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
39019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
39020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
39021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
39022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39023
39024 #define OACC_PARALLEL_CLAUSE_MASK \
39025 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
39031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
39033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
39035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
39036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
39037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
39038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
39039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
39040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39041
39042 static tree
39043 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
39044 char *p_name, bool *if_p)
39045 {
39046 omp_clause_mask mask;
39047 enum tree_code code;
39048 switch (cp_parser_pragma_kind (pragma_tok))
39049 {
39050 case PRAGMA_OACC_KERNELS:
39051 strcat (p_name, " kernels");
39052 mask = OACC_KERNELS_CLAUSE_MASK;
39053 code = OACC_KERNELS;
39054 break;
39055 case PRAGMA_OACC_PARALLEL:
39056 strcat (p_name, " parallel");
39057 mask = OACC_PARALLEL_CLAUSE_MASK;
39058 code = OACC_PARALLEL;
39059 break;
39060 default:
39061 gcc_unreachable ();
39062 }
39063
39064 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39065 {
39066 const char *p
39067 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
39068 if (strcmp (p, "loop") == 0)
39069 {
39070 cp_lexer_consume_token (parser->lexer);
39071 tree block = begin_omp_parallel ();
39072 tree clauses;
39073 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
39074 &clauses, if_p);
39075 protected_set_expr_location (stmt, pragma_tok->location);
39076 return finish_omp_construct (code, block, clauses);
39077 }
39078 }
39079
39080 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
39081
39082 tree block = begin_omp_parallel ();
39083 unsigned int save = cp_parser_begin_omp_structured_block (parser);
39084 cp_parser_statement (parser, NULL_TREE, false, if_p);
39085 cp_parser_end_omp_structured_block (parser, save);
39086 return finish_omp_construct (code, block, clauses);
39087 }
39088
39089 /* OpenACC 2.0:
39090 # pragma acc update oacc-update-clause[optseq] new-line
39091 */
39092
39093 #define OACC_UPDATE_CLAUSE_MASK \
39094 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
39096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
39097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
39099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
39100
39101 static tree
39102 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
39103 {
39104 tree stmt, clauses;
39105
39106 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
39107 "#pragma acc update", pragma_tok);
39108
39109 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
39110 {
39111 error_at (pragma_tok->location,
39112 "%<#pragma acc update%> must contain at least one "
39113 "%<device%> or %<host%> or %<self%> clause");
39114 return NULL_TREE;
39115 }
39116
39117 stmt = make_node (OACC_UPDATE);
39118 TREE_TYPE (stmt) = void_type_node;
39119 OACC_UPDATE_CLAUSES (stmt) = clauses;
39120 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39121 add_stmt (stmt);
39122 return stmt;
39123 }
39124
39125 /* OpenACC 2.0:
39126 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
39127
39128 LOC is the location of the #pragma token.
39129 */
39130
39131 #define OACC_WAIT_CLAUSE_MASK \
39132 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
39133
39134 static tree
39135 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
39136 {
39137 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
39138 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39139
39140 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39141 list = cp_parser_oacc_wait_list (parser, loc, list);
39142
39143 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
39144 "#pragma acc wait", pragma_tok);
39145
39146 stmt = c_finish_oacc_wait (loc, list, clauses);
39147 stmt = finish_expr_stmt (stmt);
39148
39149 return stmt;
39150 }
39151
39152 /* OpenMP 4.0:
39153 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
39154
39155 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
39156 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
39160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
39161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
39162
39163 static void
39164 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
39165 enum pragma_context context)
39166 {
39167 bool first_p = parser->omp_declare_simd == NULL;
39168 cp_omp_declare_simd_data data;
39169 if (first_p)
39170 {
39171 data.error_seen = false;
39172 data.fndecl_seen = false;
39173 data.tokens = vNULL;
39174 data.clauses = NULL_TREE;
39175 /* It is safe to take the address of a local variable; it will only be
39176 used while this scope is live. */
39177 parser->omp_declare_simd = &data;
39178 }
39179
39180 /* Store away all pragma tokens. */
39181 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39182 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39183 cp_lexer_consume_token (parser->lexer);
39184 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39185 parser->omp_declare_simd->error_seen = true;
39186 cp_parser_require_pragma_eol (parser, pragma_tok);
39187 struct cp_token_cache *cp
39188 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39189 parser->omp_declare_simd->tokens.safe_push (cp);
39190
39191 if (first_p)
39192 {
39193 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39194 cp_parser_pragma (parser, context, NULL);
39195 switch (context)
39196 {
39197 case pragma_external:
39198 cp_parser_declaration (parser);
39199 break;
39200 case pragma_member:
39201 cp_parser_member_declaration (parser);
39202 break;
39203 case pragma_objc_icode:
39204 cp_parser_block_declaration (parser, /*statement_p=*/false);
39205 break;
39206 default:
39207 cp_parser_declaration_statement (parser);
39208 break;
39209 }
39210 if (parser->omp_declare_simd
39211 && !parser->omp_declare_simd->error_seen
39212 && !parser->omp_declare_simd->fndecl_seen)
39213 error_at (pragma_tok->location,
39214 "%<#pragma omp declare simd%> not immediately followed by "
39215 "function declaration or definition");
39216 data.tokens.release ();
39217 parser->omp_declare_simd = NULL;
39218 }
39219 }
39220
39221 /* Finalize #pragma omp declare simd clauses after direct declarator has
39222 been parsed, and put that into "omp declare simd" attribute. */
39223
39224 static tree
39225 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
39226 {
39227 struct cp_token_cache *ce;
39228 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
39229 int i;
39230
39231 if (!data->error_seen && data->fndecl_seen)
39232 {
39233 error ("%<#pragma omp declare simd%> not immediately followed by "
39234 "a single function declaration or definition");
39235 data->error_seen = true;
39236 }
39237 if (data->error_seen)
39238 return attrs;
39239
39240 FOR_EACH_VEC_ELT (data->tokens, i, ce)
39241 {
39242 tree c, cl;
39243
39244 cp_parser_push_lexer_for_tokens (parser, ce);
39245 parser->lexer->in_pragma = true;
39246 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39247 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39248 cp_lexer_consume_token (parser->lexer);
39249 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
39250 "#pragma omp declare simd", pragma_tok);
39251 cp_parser_pop_lexer (parser);
39252 if (cl)
39253 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
39254 c = build_tree_list (get_identifier ("omp declare simd"), cl);
39255 TREE_CHAIN (c) = attrs;
39256 if (processing_template_decl)
39257 ATTR_IS_DEPENDENT (c) = 1;
39258 attrs = c;
39259 }
39260
39261 data->fndecl_seen = true;
39262 return attrs;
39263 }
39264
39265
39266 /* OpenMP 4.0:
39267 # pragma omp declare target new-line
39268 declarations and definitions
39269 # pragma omp end declare target new-line
39270
39271 OpenMP 4.5:
39272 # pragma omp declare target ( extended-list ) new-line
39273
39274 # pragma omp declare target declare-target-clauses[seq] new-line */
39275
39276 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
39277 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
39279
39280 static void
39281 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
39282 {
39283 tree clauses = NULL_TREE;
39284 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39285 clauses
39286 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
39287 "#pragma omp declare target", pragma_tok);
39288 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39289 {
39290 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
39291 clauses);
39292 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39293 cp_parser_require_pragma_eol (parser, pragma_tok);
39294 }
39295 else
39296 {
39297 cp_parser_require_pragma_eol (parser, pragma_tok);
39298 scope_chain->omp_declare_target_attribute++;
39299 return;
39300 }
39301 if (scope_chain->omp_declare_target_attribute)
39302 error_at (pragma_tok->location,
39303 "%<#pragma omp declare target%> with clauses in between "
39304 "%<#pragma omp declare target%> without clauses and "
39305 "%<#pragma omp end declare target%>");
39306 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
39307 {
39308 tree t = OMP_CLAUSE_DECL (c), id;
39309 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
39310 tree at2 = lookup_attribute ("omp declare target link",
39311 DECL_ATTRIBUTES (t));
39312 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
39313 {
39314 id = get_identifier ("omp declare target link");
39315 std::swap (at1, at2);
39316 }
39317 else
39318 id = get_identifier ("omp declare target");
39319 if (at2)
39320 {
39321 error_at (OMP_CLAUSE_LOCATION (c),
39322 "%qD specified both in declare target %<link%> and %<to%>"
39323 " clauses", t);
39324 continue;
39325 }
39326 if (!at1)
39327 {
39328 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
39329 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
39330 continue;
39331
39332 symtab_node *node = symtab_node::get (t);
39333 if (node != NULL)
39334 {
39335 node->offloadable = 1;
39336 if (ENABLE_OFFLOADING)
39337 {
39338 g->have_offload = true;
39339 if (is_a <varpool_node *> (node))
39340 vec_safe_push (offload_vars, t);
39341 }
39342 }
39343 }
39344 }
39345 }
39346
39347 static void
39348 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
39349 {
39350 const char *p = "";
39351 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39352 {
39353 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39354 p = IDENTIFIER_POINTER (id);
39355 }
39356 if (strcmp (p, "declare") == 0)
39357 {
39358 cp_lexer_consume_token (parser->lexer);
39359 p = "";
39360 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39361 {
39362 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39363 p = IDENTIFIER_POINTER (id);
39364 }
39365 if (strcmp (p, "target") == 0)
39366 cp_lexer_consume_token (parser->lexer);
39367 else
39368 {
39369 cp_parser_error (parser, "expected %<target%>");
39370 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39371 return;
39372 }
39373 }
39374 else
39375 {
39376 cp_parser_error (parser, "expected %<declare%>");
39377 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39378 return;
39379 }
39380 cp_parser_require_pragma_eol (parser, pragma_tok);
39381 if (!scope_chain->omp_declare_target_attribute)
39382 error_at (pragma_tok->location,
39383 "%<#pragma omp end declare target%> without corresponding "
39384 "%<#pragma omp declare target%>");
39385 else
39386 scope_chain->omp_declare_target_attribute--;
39387 }
39388
39389 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39390 expression and optional initializer clause of
39391 #pragma omp declare reduction. We store the expression(s) as
39392 either 3, 6 or 7 special statements inside of the artificial function's
39393 body. The first two statements are DECL_EXPRs for the artificial
39394 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39395 expression that uses those variables.
39396 If there was any INITIALIZER clause, this is followed by further statements,
39397 the fourth and fifth statements are DECL_EXPRs for the artificial
39398 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39399 constructor variant (first token after open paren is not omp_priv),
39400 then the sixth statement is a statement with the function call expression
39401 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39402 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39403 to initialize the OMP_PRIV artificial variable and there is seventh
39404 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39405
39406 static bool
39407 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
39408 {
39409 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
39410 gcc_assert (TYPE_REF_P (type));
39411 type = TREE_TYPE (type);
39412 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
39413 DECL_ARTIFICIAL (omp_out) = 1;
39414 pushdecl (omp_out);
39415 add_decl_expr (omp_out);
39416 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
39417 DECL_ARTIFICIAL (omp_in) = 1;
39418 pushdecl (omp_in);
39419 add_decl_expr (omp_in);
39420 tree combiner;
39421 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
39422
39423 keep_next_level (true);
39424 tree block = begin_omp_structured_block ();
39425 combiner = cp_parser_expression (parser);
39426 finish_expr_stmt (combiner);
39427 block = finish_omp_structured_block (block);
39428 add_stmt (block);
39429
39430 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
39431 return false;
39432
39433 const char *p = "";
39434 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39435 {
39436 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39437 p = IDENTIFIER_POINTER (id);
39438 }
39439
39440 if (strcmp (p, "initializer") == 0)
39441 {
39442 cp_lexer_consume_token (parser->lexer);
39443 matching_parens parens;
39444 if (!parens.require_open (parser))
39445 return false;
39446
39447 p = "";
39448 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39449 {
39450 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39451 p = IDENTIFIER_POINTER (id);
39452 }
39453
39454 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
39455 DECL_ARTIFICIAL (omp_priv) = 1;
39456 pushdecl (omp_priv);
39457 add_decl_expr (omp_priv);
39458 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
39459 DECL_ARTIFICIAL (omp_orig) = 1;
39460 pushdecl (omp_orig);
39461 add_decl_expr (omp_orig);
39462
39463 keep_next_level (true);
39464 block = begin_omp_structured_block ();
39465
39466 bool ctor = false;
39467 if (strcmp (p, "omp_priv") == 0)
39468 {
39469 bool is_direct_init, is_non_constant_init;
39470 ctor = true;
39471 cp_lexer_consume_token (parser->lexer);
39472 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39473 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
39474 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39475 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
39476 == CPP_CLOSE_PAREN
39477 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
39478 == CPP_CLOSE_PAREN))
39479 {
39480 finish_omp_structured_block (block);
39481 error ("invalid initializer clause");
39482 return false;
39483 }
39484 initializer = cp_parser_initializer (parser, &is_direct_init,
39485 &is_non_constant_init);
39486 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
39487 NULL_TREE, LOOKUP_ONLYCONVERTING);
39488 }
39489 else
39490 {
39491 cp_parser_parse_tentatively (parser);
39492 /* Don't create location wrapper nodes here. */
39493 auto_suppress_location_wrappers sentinel;
39494 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
39495 /*check_dependency_p=*/true,
39496 /*template_p=*/NULL,
39497 /*declarator_p=*/false,
39498 /*optional_p=*/false);
39499 vec<tree, va_gc> *args;
39500 if (fn_name == error_mark_node
39501 || cp_parser_error_occurred (parser)
39502 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39503 || ((args = cp_parser_parenthesized_expression_list
39504 (parser, non_attr, /*cast_p=*/false,
39505 /*allow_expansion_p=*/true,
39506 /*non_constant_p=*/NULL)),
39507 cp_parser_error_occurred (parser)))
39508 {
39509 finish_omp_structured_block (block);
39510 cp_parser_abort_tentative_parse (parser);
39511 cp_parser_error (parser, "expected id-expression (arguments)");
39512 return false;
39513 }
39514 unsigned int i;
39515 tree arg;
39516 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
39517 if (arg == omp_priv
39518 || (TREE_CODE (arg) == ADDR_EXPR
39519 && TREE_OPERAND (arg, 0) == omp_priv))
39520 break;
39521 cp_parser_abort_tentative_parse (parser);
39522 if (arg == NULL_TREE)
39523 error ("one of the initializer call arguments should be %<omp_priv%>"
39524 " or %<&omp_priv%>");
39525 initializer = cp_parser_postfix_expression (parser, false, false, false,
39526 false, NULL);
39527 finish_expr_stmt (initializer);
39528 }
39529
39530 block = finish_omp_structured_block (block);
39531 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
39532 add_stmt (block);
39533
39534 if (ctor)
39535 add_decl_expr (omp_orig);
39536
39537 if (!parens.require_close (parser))
39538 return false;
39539 }
39540
39541 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
39542 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
39543 UNKNOWN_LOCATION);
39544
39545 return true;
39546 }
39547
39548 /* OpenMP 4.0
39549 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39550 initializer-clause[opt] new-line
39551
39552 initializer-clause:
39553 initializer (omp_priv initializer)
39554 initializer (function-name (argument-list)) */
39555
39556 static void
39557 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
39558 enum pragma_context)
39559 {
39560 auto_vec<tree> types;
39561 enum tree_code reduc_code = ERROR_MARK;
39562 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
39563 unsigned int i;
39564 cp_token *first_token;
39565 cp_token_cache *cp;
39566 int errs;
39567 void *p;
39568
39569 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39570 p = obstack_alloc (&declarator_obstack, 0);
39571
39572 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39573 goto fail;
39574
39575 switch (cp_lexer_peek_token (parser->lexer)->type)
39576 {
39577 case CPP_PLUS:
39578 reduc_code = PLUS_EXPR;
39579 break;
39580 case CPP_MULT:
39581 reduc_code = MULT_EXPR;
39582 break;
39583 case CPP_MINUS:
39584 reduc_code = MINUS_EXPR;
39585 break;
39586 case CPP_AND:
39587 reduc_code = BIT_AND_EXPR;
39588 break;
39589 case CPP_XOR:
39590 reduc_code = BIT_XOR_EXPR;
39591 break;
39592 case CPP_OR:
39593 reduc_code = BIT_IOR_EXPR;
39594 break;
39595 case CPP_AND_AND:
39596 reduc_code = TRUTH_ANDIF_EXPR;
39597 break;
39598 case CPP_OR_OR:
39599 reduc_code = TRUTH_ORIF_EXPR;
39600 break;
39601 case CPP_NAME:
39602 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
39603 break;
39604 default:
39605 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39606 "%<|%>, %<&&%>, %<||%> or identifier");
39607 goto fail;
39608 }
39609
39610 if (reduc_code != ERROR_MARK)
39611 cp_lexer_consume_token (parser->lexer);
39612
39613 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
39614 if (reduc_id == error_mark_node)
39615 goto fail;
39616
39617 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39618 goto fail;
39619
39620 /* Types may not be defined in declare reduction type list. */
39621 const char *saved_message;
39622 saved_message = parser->type_definition_forbidden_message;
39623 parser->type_definition_forbidden_message
39624 = G_("types may not be defined in declare reduction type list");
39625 bool saved_colon_corrects_to_scope_p;
39626 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39627 parser->colon_corrects_to_scope_p = false;
39628 bool saved_colon_doesnt_start_class_def_p;
39629 saved_colon_doesnt_start_class_def_p
39630 = parser->colon_doesnt_start_class_def_p;
39631 parser->colon_doesnt_start_class_def_p = true;
39632
39633 while (true)
39634 {
39635 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39636 type = cp_parser_type_id (parser);
39637 if (type == error_mark_node)
39638 ;
39639 else if (ARITHMETIC_TYPE_P (type)
39640 && (orig_reduc_id == NULL_TREE
39641 || (TREE_CODE (type) != COMPLEX_TYPE
39642 && (id_equal (orig_reduc_id, "min")
39643 || id_equal (orig_reduc_id, "max")))))
39644 error_at (loc, "predeclared arithmetic type %qT in "
39645 "%<#pragma omp declare reduction%>", type);
39646 else if (TREE_CODE (type) == FUNCTION_TYPE
39647 || TREE_CODE (type) == METHOD_TYPE
39648 || TREE_CODE (type) == ARRAY_TYPE)
39649 error_at (loc, "function or array type %qT in "
39650 "%<#pragma omp declare reduction%>", type);
39651 else if (TYPE_REF_P (type))
39652 error_at (loc, "reference type %qT in "
39653 "%<#pragma omp declare reduction%>", type);
39654 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
39655 error_at (loc, "const, volatile or __restrict qualified type %qT in "
39656 "%<#pragma omp declare reduction%>", type);
39657 else
39658 types.safe_push (type);
39659
39660 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39661 cp_lexer_consume_token (parser->lexer);
39662 else
39663 break;
39664 }
39665
39666 /* Restore the saved message. */
39667 parser->type_definition_forbidden_message = saved_message;
39668 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39669 parser->colon_doesnt_start_class_def_p
39670 = saved_colon_doesnt_start_class_def_p;
39671
39672 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
39673 || types.is_empty ())
39674 {
39675 fail:
39676 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39677 goto done;
39678 }
39679
39680 first_token = cp_lexer_peek_token (parser->lexer);
39681 cp = NULL;
39682 errs = errorcount;
39683 FOR_EACH_VEC_ELT (types, i, type)
39684 {
39685 tree fntype
39686 = build_function_type_list (void_type_node,
39687 cp_build_reference_type (type, false),
39688 NULL_TREE);
39689 tree this_reduc_id = reduc_id;
39690 if (!dependent_type_p (type))
39691 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39692 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39693 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39694 DECL_ARTIFICIAL (fndecl) = 1;
39695 DECL_EXTERNAL (fndecl) = 1;
39696 DECL_DECLARED_INLINE_P (fndecl) = 1;
39697 DECL_IGNORED_P (fndecl) = 1;
39698 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39699 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39700 DECL_ATTRIBUTES (fndecl)
39701 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39702 DECL_ATTRIBUTES (fndecl));
39703 if (processing_template_decl)
39704 fndecl = push_template_decl (fndecl);
39705 bool block_scope = false;
39706 tree block = NULL_TREE;
39707 if (current_function_decl)
39708 {
39709 block_scope = true;
39710 DECL_CONTEXT (fndecl) = global_namespace;
39711 if (!processing_template_decl)
39712 pushdecl (fndecl);
39713 }
39714 else if (current_class_type)
39715 {
39716 if (cp == NULL)
39717 {
39718 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39719 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39720 cp_lexer_consume_token (parser->lexer);
39721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39722 goto fail;
39723 cp = cp_token_cache_new (first_token,
39724 cp_lexer_peek_nth_token (parser->lexer,
39725 2));
39726 }
39727 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39728 finish_member_declaration (fndecl);
39729 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39730 DECL_PENDING_INLINE_P (fndecl) = 1;
39731 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39732 continue;
39733 }
39734 else
39735 {
39736 DECL_CONTEXT (fndecl) = current_namespace;
39737 pushdecl (fndecl);
39738 }
39739 if (!block_scope)
39740 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39741 else
39742 block = begin_omp_structured_block ();
39743 if (cp)
39744 {
39745 cp_parser_push_lexer_for_tokens (parser, cp);
39746 parser->lexer->in_pragma = true;
39747 }
39748 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39749 {
39750 if (!block_scope)
39751 finish_function (/*inline_p=*/false);
39752 else
39753 DECL_CONTEXT (fndecl) = current_function_decl;
39754 if (cp)
39755 cp_parser_pop_lexer (parser);
39756 goto fail;
39757 }
39758 if (cp)
39759 cp_parser_pop_lexer (parser);
39760 if (!block_scope)
39761 finish_function (/*inline_p=*/false);
39762 else
39763 {
39764 DECL_CONTEXT (fndecl) = current_function_decl;
39765 block = finish_omp_structured_block (block);
39766 if (TREE_CODE (block) == BIND_EXPR)
39767 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39768 else if (TREE_CODE (block) == STATEMENT_LIST)
39769 DECL_SAVED_TREE (fndecl) = block;
39770 if (processing_template_decl)
39771 add_decl_expr (fndecl);
39772 }
39773 cp_check_omp_declare_reduction (fndecl);
39774 if (cp == NULL && types.length () > 1)
39775 cp = cp_token_cache_new (first_token,
39776 cp_lexer_peek_nth_token (parser->lexer, 2));
39777 if (errs != errorcount)
39778 break;
39779 }
39780
39781 cp_parser_require_pragma_eol (parser, pragma_tok);
39782
39783 done:
39784 /* Free any declarators allocated. */
39785 obstack_free (&declarator_obstack, p);
39786 }
39787
39788 /* OpenMP 4.0
39789 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39790 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39791 initializer-clause[opt] new-line
39792 #pragma omp declare target new-line */
39793
39794 static bool
39795 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39796 enum pragma_context context)
39797 {
39798 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39799 {
39800 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39801 const char *p = IDENTIFIER_POINTER (id);
39802
39803 if (strcmp (p, "simd") == 0)
39804 {
39805 cp_lexer_consume_token (parser->lexer);
39806 cp_parser_omp_declare_simd (parser, pragma_tok,
39807 context);
39808 return true;
39809 }
39810 cp_ensure_no_omp_declare_simd (parser);
39811 if (strcmp (p, "reduction") == 0)
39812 {
39813 cp_lexer_consume_token (parser->lexer);
39814 cp_parser_omp_declare_reduction (parser, pragma_tok,
39815 context);
39816 return false;
39817 }
39818 if (!flag_openmp) /* flag_openmp_simd */
39819 {
39820 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39821 return false;
39822 }
39823 if (strcmp (p, "target") == 0)
39824 {
39825 cp_lexer_consume_token (parser->lexer);
39826 cp_parser_omp_declare_target (parser, pragma_tok);
39827 return false;
39828 }
39829 }
39830 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39831 "or %<target%>");
39832 cp_parser_require_pragma_eol (parser, pragma_tok);
39833 return false;
39834 }
39835
39836 /* OpenMP 5.0
39837 #pragma omp requires clauses[optseq] new-line */
39838
39839 static bool
39840 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39841 {
39842 bool first = true;
39843 enum omp_requires new_req = (enum omp_requires) 0;
39844
39845 location_t loc = pragma_tok->location;
39846 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39847 {
39848 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39849 cp_lexer_consume_token (parser->lexer);
39850
39851 first = false;
39852
39853 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39854 {
39855 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39856 const char *p = IDENTIFIER_POINTER (id);
39857 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39858 enum omp_requires this_req = (enum omp_requires) 0;
39859
39860 if (!strcmp (p, "unified_address"))
39861 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39862 else if (!strcmp (p, "unified_shared_memory"))
39863 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39864 else if (!strcmp (p, "dynamic_allocators"))
39865 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39866 else if (!strcmp (p, "reverse_offload"))
39867 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39868 else if (!strcmp (p, "atomic_default_mem_order"))
39869 {
39870 cp_lexer_consume_token (parser->lexer);
39871
39872 matching_parens parens;
39873 if (parens.require_open (parser))
39874 {
39875 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39876 {
39877 id = cp_lexer_peek_token (parser->lexer)->u.value;
39878 p = IDENTIFIER_POINTER (id);
39879
39880 if (!strcmp (p, "seq_cst"))
39881 this_req
39882 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39883 else if (!strcmp (p, "relaxed"))
39884 this_req
39885 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39886 else if (!strcmp (p, "acq_rel"))
39887 this_req
39888 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39889 }
39890 if (this_req == 0)
39891 {
39892 error_at (cp_lexer_peek_token (parser->lexer)->location,
39893 "expected %<seq_cst%>, %<relaxed%> or "
39894 "%<acq_rel%>");
39895 if (cp_lexer_nth_token_is (parser->lexer, 2,
39896 CPP_CLOSE_PAREN))
39897 cp_lexer_consume_token (parser->lexer);
39898 }
39899 else
39900 cp_lexer_consume_token (parser->lexer);
39901
39902 if (!parens.require_close (parser))
39903 cp_parser_skip_to_closing_parenthesis (parser,
39904 /*recovering=*/true,
39905 /*or_comma=*/false,
39906 /*consume_paren=*/
39907 true);
39908
39909 if (this_req == 0)
39910 {
39911 cp_parser_require_pragma_eol (parser, pragma_tok);
39912 return false;
39913 }
39914 }
39915 p = NULL;
39916 }
39917 else
39918 {
39919 error_at (cloc, "expected %<unified_address%>, "
39920 "%<unified_shared_memory%>, "
39921 "%<dynamic_allocators%>, "
39922 "%<reverse_offload%> "
39923 "or %<atomic_default_mem_order%> clause");
39924 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39925 return false;
39926 }
39927 if (p)
39928 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39929 "supported yet", p);
39930 if (p)
39931 cp_lexer_consume_token (parser->lexer);
39932 if (this_req)
39933 {
39934 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39935 {
39936 if ((this_req & new_req) != 0)
39937 error_at (cloc, "too many %qs clauses", p);
39938 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
39939 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
39940 error_at (cloc, "%qs clause used lexically after first "
39941 "target construct or offloading API", p);
39942 }
39943 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39944 {
39945 error_at (cloc, "too many %qs clauses",
39946 "atomic_default_mem_order");
39947 this_req = (enum omp_requires) 0;
39948 }
39949 else if ((omp_requires_mask
39950 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39951 {
39952 error_at (cloc, "more than one %<atomic_default_mem_order%>"
39953 " clause in a single compilation unit");
39954 this_req
39955 = (enum omp_requires)
39956 (omp_requires_mask
39957 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
39958 }
39959 else if ((omp_requires_mask
39960 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
39961 error_at (cloc, "%<atomic_default_mem_order%> clause used "
39962 "lexically after first %<atomic%> construct "
39963 "without memory order clause");
39964 new_req = (enum omp_requires) (new_req | this_req);
39965 omp_requires_mask
39966 = (enum omp_requires) (omp_requires_mask | this_req);
39967 continue;
39968 }
39969 }
39970 break;
39971 }
39972 cp_parser_require_pragma_eol (parser, pragma_tok);
39973
39974 if (new_req == 0)
39975 error_at (loc, "%<pragma omp requires%> requires at least one clause");
39976 return false;
39977 }
39978
39979
39980 /* OpenMP 4.5:
39981 #pragma omp taskloop taskloop-clause[optseq] new-line
39982 for-loop
39983
39984 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39985 for-loop */
39986
39987 #define OMP_TASKLOOP_CLAUSE_MASK \
39988 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
40000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
40001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
40002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
40004
40005 static tree
40006 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
40007 char *p_name, omp_clause_mask mask, tree *cclauses,
40008 bool *if_p)
40009 {
40010 tree clauses, sb, ret;
40011 unsigned int save;
40012 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40013
40014 strcat (p_name, " taskloop");
40015 mask |= OMP_TASKLOOP_CLAUSE_MASK;
40016 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
40017 clause. */
40018 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
40019 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
40020
40021 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40022 {
40023 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40024 const char *p = IDENTIFIER_POINTER (id);
40025
40026 if (strcmp (p, "simd") == 0)
40027 {
40028 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40029 if (cclauses == NULL)
40030 cclauses = cclauses_buf;
40031
40032 cp_lexer_consume_token (parser->lexer);
40033 if (!flag_openmp) /* flag_openmp_simd */
40034 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40035 cclauses, if_p);
40036 sb = begin_omp_structured_block ();
40037 save = cp_parser_begin_omp_structured_block (parser);
40038 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40039 cclauses, if_p);
40040 cp_parser_end_omp_structured_block (parser, save);
40041 tree body = finish_omp_structured_block (sb);
40042 if (ret == NULL)
40043 return ret;
40044 ret = make_node (OMP_TASKLOOP);
40045 TREE_TYPE (ret) = void_type_node;
40046 OMP_FOR_BODY (ret) = body;
40047 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
40048 SET_EXPR_LOCATION (ret, loc);
40049 add_stmt (ret);
40050 return ret;
40051 }
40052 }
40053 if (!flag_openmp) /* flag_openmp_simd */
40054 {
40055 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40056 return NULL_TREE;
40057 }
40058
40059 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40060 cclauses == NULL);
40061 if (cclauses)
40062 {
40063 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
40064 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
40065 }
40066
40067 keep_next_level (true);
40068 sb = begin_omp_structured_block ();
40069 save = cp_parser_begin_omp_structured_block (parser);
40070
40071 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
40072 if_p);
40073
40074 cp_parser_end_omp_structured_block (parser, save);
40075 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40076
40077 return ret;
40078 }
40079
40080
40081 /* OpenACC 2.0:
40082 # pragma acc routine oacc-routine-clause[optseq] new-line
40083 function-definition
40084
40085 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
40086 */
40087
40088 #define OACC_ROUTINE_CLAUSE_MASK \
40089 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
40090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
40091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
40092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
40093
40094
40095 /* Parse the OpenACC routine pragma. This has an optional '( name )'
40096 component, which must resolve to a declared namespace-scope
40097 function. The clauses are either processed directly (for a named
40098 function), or defered until the immediatley following declaration
40099 is parsed. */
40100
40101 static void
40102 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
40103 enum pragma_context context)
40104 {
40105 gcc_checking_assert (context == pragma_external);
40106 /* The checking for "another pragma following this one" in the "no optional
40107 '( name )'" case makes sure that we dont re-enter. */
40108 gcc_checking_assert (parser->oacc_routine == NULL);
40109
40110 cp_oacc_routine_data data;
40111 data.error_seen = false;
40112 data.fndecl_seen = false;
40113 data.tokens = vNULL;
40114 data.clauses = NULL_TREE;
40115 data.loc = pragma_tok->location;
40116 /* It is safe to take the address of a local variable; it will only be
40117 used while this scope is live. */
40118 parser->oacc_routine = &data;
40119
40120 /* Look for optional '( name )'. */
40121 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
40122 {
40123 matching_parens parens;
40124 parens.consume_open (parser); /* '(' */
40125
40126 /* We parse the name as an id-expression. If it resolves to
40127 anything other than a non-overloaded function at namespace
40128 scope, it's an error. */
40129 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
40130 tree name = cp_parser_id_expression (parser,
40131 /*template_keyword_p=*/false,
40132 /*check_dependency_p=*/false,
40133 /*template_p=*/NULL,
40134 /*declarator_p=*/false,
40135 /*optional_p=*/false);
40136 tree decl = (identifier_p (name)
40137 ? cp_parser_lookup_name_simple (parser, name, name_loc)
40138 : name);
40139 if (name != error_mark_node && decl == error_mark_node)
40140 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
40141
40142 if (decl == error_mark_node
40143 || !parens.require_close (parser))
40144 {
40145 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40146 parser->oacc_routine = NULL;
40147 return;
40148 }
40149
40150 data.clauses
40151 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40152 "#pragma acc routine",
40153 cp_lexer_peek_token (parser->lexer));
40154
40155 if (decl && is_overloaded_fn (decl)
40156 && (TREE_CODE (decl) != FUNCTION_DECL
40157 || DECL_FUNCTION_TEMPLATE_P (decl)))
40158 {
40159 error_at (name_loc,
40160 "%<#pragma acc routine%> names a set of overloads");
40161 parser->oacc_routine = NULL;
40162 return;
40163 }
40164
40165 /* Perhaps we should use the same rule as declarations in different
40166 namespaces? */
40167 if (!DECL_NAMESPACE_SCOPE_P (decl))
40168 {
40169 error_at (name_loc,
40170 "%qD does not refer to a namespace scope function", decl);
40171 parser->oacc_routine = NULL;
40172 return;
40173 }
40174
40175 if (TREE_CODE (decl) != FUNCTION_DECL)
40176 {
40177 error_at (name_loc, "%qD does not refer to a function", decl);
40178 parser->oacc_routine = NULL;
40179 return;
40180 }
40181
40182 cp_finalize_oacc_routine (parser, decl, false);
40183 parser->oacc_routine = NULL;
40184 }
40185 else /* No optional '( name )'. */
40186 {
40187 /* Store away all pragma tokens. */
40188 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
40189 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
40190 cp_lexer_consume_token (parser->lexer);
40191 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40192 parser->oacc_routine->error_seen = true;
40193 cp_parser_require_pragma_eol (parser, pragma_tok);
40194 struct cp_token_cache *cp
40195 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
40196 parser->oacc_routine->tokens.safe_push (cp);
40197
40198 /* Emit a helpful diagnostic if there's another pragma following this
40199 one. */
40200 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
40201 {
40202 cp_ensure_no_oacc_routine (parser);
40203 data.tokens.release ();
40204 /* ..., and then just keep going. */
40205 return;
40206 }
40207
40208 /* We only have to consider the pragma_external case here. */
40209 cp_parser_declaration (parser);
40210 if (parser->oacc_routine
40211 && !parser->oacc_routine->fndecl_seen)
40212 cp_ensure_no_oacc_routine (parser);
40213 else
40214 parser->oacc_routine = NULL;
40215 data.tokens.release ();
40216 }
40217 }
40218
40219 /* Finalize #pragma acc routine clauses after direct declarator has
40220 been parsed. */
40221
40222 static tree
40223 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
40224 {
40225 struct cp_token_cache *ce;
40226 cp_oacc_routine_data *data = parser->oacc_routine;
40227
40228 if (!data->error_seen && data->fndecl_seen)
40229 {
40230 error_at (data->loc,
40231 "%<#pragma acc routine%> not immediately followed by "
40232 "a single function declaration or definition");
40233 data->error_seen = true;
40234 }
40235 if (data->error_seen)
40236 return attrs;
40237
40238 gcc_checking_assert (data->tokens.length () == 1);
40239 ce = data->tokens[0];
40240
40241 cp_parser_push_lexer_for_tokens (parser, ce);
40242 parser->lexer->in_pragma = true;
40243 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
40244
40245 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
40246 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
40247 parser->oacc_routine->clauses
40248 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40249 "#pragma acc routine", pragma_tok);
40250 cp_parser_pop_lexer (parser);
40251 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40252 fndecl_seen. */
40253
40254 return attrs;
40255 }
40256
40257 /* Apply any saved OpenACC routine clauses to a just-parsed
40258 declaration. */
40259
40260 static void
40261 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
40262 {
40263 if (__builtin_expect (parser->oacc_routine != NULL, 0))
40264 {
40265 /* Keep going if we're in error reporting mode. */
40266 if (parser->oacc_routine->error_seen
40267 || fndecl == error_mark_node)
40268 return;
40269
40270 if (parser->oacc_routine->fndecl_seen)
40271 {
40272 error_at (parser->oacc_routine->loc,
40273 "%<#pragma acc routine%> not immediately followed by"
40274 " a single function declaration or definition");
40275 parser->oacc_routine = NULL;
40276 return;
40277 }
40278 if (TREE_CODE (fndecl) != FUNCTION_DECL)
40279 {
40280 cp_ensure_no_oacc_routine (parser);
40281 return;
40282 }
40283
40284 if (oacc_get_fn_attrib (fndecl))
40285 {
40286 error_at (parser->oacc_routine->loc,
40287 "%<#pragma acc routine%> already applied to %qD", fndecl);
40288 parser->oacc_routine = NULL;
40289 return;
40290 }
40291
40292 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
40293 {
40294 error_at (parser->oacc_routine->loc,
40295 TREE_USED (fndecl)
40296 ? G_("%<#pragma acc routine%> must be applied before use")
40297 : G_("%<#pragma acc routine%> must be applied before "
40298 "definition"));
40299 parser->oacc_routine = NULL;
40300 return;
40301 }
40302
40303 /* Process the routine's dimension clauses. */
40304 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
40305 oacc_replace_fn_attrib (fndecl, dims);
40306
40307 /* Add an "omp declare target" attribute. */
40308 DECL_ATTRIBUTES (fndecl)
40309 = tree_cons (get_identifier ("omp declare target"),
40310 NULL_TREE, DECL_ATTRIBUTES (fndecl));
40311
40312 /* Don't unset parser->oacc_routine here: we may still need it to
40313 diagnose wrong usage. But, remember that we've used this "#pragma acc
40314 routine". */
40315 parser->oacc_routine->fndecl_seen = true;
40316 }
40317 }
40318
40319 /* Main entry point to OpenMP statement pragmas. */
40320
40321 static void
40322 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40323 {
40324 tree stmt;
40325 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
40326 omp_clause_mask mask (0);
40327
40328 switch (cp_parser_pragma_kind (pragma_tok))
40329 {
40330 case PRAGMA_OACC_ATOMIC:
40331 cp_parser_omp_atomic (parser, pragma_tok);
40332 return;
40333 case PRAGMA_OACC_CACHE:
40334 stmt = cp_parser_oacc_cache (parser, pragma_tok);
40335 break;
40336 case PRAGMA_OACC_DATA:
40337 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
40338 break;
40339 case PRAGMA_OACC_ENTER_DATA:
40340 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
40341 break;
40342 case PRAGMA_OACC_EXIT_DATA:
40343 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
40344 break;
40345 case PRAGMA_OACC_HOST_DATA:
40346 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
40347 break;
40348 case PRAGMA_OACC_KERNELS:
40349 case PRAGMA_OACC_PARALLEL:
40350 strcpy (p_name, "#pragma acc");
40351 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
40352 if_p);
40353 break;
40354 case PRAGMA_OACC_LOOP:
40355 strcpy (p_name, "#pragma acc");
40356 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
40357 if_p);
40358 break;
40359 case PRAGMA_OACC_UPDATE:
40360 stmt = cp_parser_oacc_update (parser, pragma_tok);
40361 break;
40362 case PRAGMA_OACC_WAIT:
40363 stmt = cp_parser_oacc_wait (parser, pragma_tok);
40364 break;
40365 case PRAGMA_OMP_ATOMIC:
40366 cp_parser_omp_atomic (parser, pragma_tok);
40367 return;
40368 case PRAGMA_OMP_CRITICAL:
40369 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
40370 break;
40371 case PRAGMA_OMP_DISTRIBUTE:
40372 strcpy (p_name, "#pragma omp");
40373 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
40374 if_p);
40375 break;
40376 case PRAGMA_OMP_FOR:
40377 strcpy (p_name, "#pragma omp");
40378 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
40379 if_p);
40380 break;
40381 case PRAGMA_OMP_MASTER:
40382 strcpy (p_name, "#pragma omp");
40383 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
40384 if_p);
40385 break;
40386 case PRAGMA_OMP_PARALLEL:
40387 strcpy (p_name, "#pragma omp");
40388 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
40389 if_p);
40390 break;
40391 case PRAGMA_OMP_SECTIONS:
40392 strcpy (p_name, "#pragma omp");
40393 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
40394 break;
40395 case PRAGMA_OMP_SIMD:
40396 strcpy (p_name, "#pragma omp");
40397 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
40398 if_p);
40399 break;
40400 case PRAGMA_OMP_SINGLE:
40401 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
40402 break;
40403 case PRAGMA_OMP_TASK:
40404 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
40405 break;
40406 case PRAGMA_OMP_TASKGROUP:
40407 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
40408 break;
40409 case PRAGMA_OMP_TASKLOOP:
40410 strcpy (p_name, "#pragma omp");
40411 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
40412 if_p);
40413 break;
40414 case PRAGMA_OMP_TEAMS:
40415 strcpy (p_name, "#pragma omp");
40416 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
40417 if_p);
40418 break;
40419 default:
40420 gcc_unreachable ();
40421 }
40422
40423 protected_set_expr_location (stmt, pragma_tok->location);
40424 }
40425 \f
40426 /* Transactional Memory parsing routines. */
40427
40428 /* Parse a transaction attribute.
40429
40430 txn-attribute:
40431 attribute
40432 [ [ identifier ] ]
40433
40434 We use this instead of cp_parser_attributes_opt for transactions to avoid
40435 the pedwarn in C++98 mode. */
40436
40437 static tree
40438 cp_parser_txn_attribute_opt (cp_parser *parser)
40439 {
40440 cp_token *token;
40441 tree attr_name, attr = NULL;
40442
40443 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
40444 return cp_parser_attributes_opt (parser);
40445
40446 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
40447 return NULL_TREE;
40448 cp_lexer_consume_token (parser->lexer);
40449 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
40450 goto error1;
40451
40452 token = cp_lexer_peek_token (parser->lexer);
40453 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
40454 {
40455 token = cp_lexer_consume_token (parser->lexer);
40456
40457 attr_name = (token->type == CPP_KEYWORD
40458 /* For keywords, use the canonical spelling,
40459 not the parsed identifier. */
40460 ? ridpointers[(int) token->keyword]
40461 : token->u.value);
40462 attr = build_tree_list (attr_name, NULL_TREE);
40463 }
40464 else
40465 cp_parser_error (parser, "expected identifier");
40466
40467 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40468 error1:
40469 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40470 return attr;
40471 }
40472
40473 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40474
40475 transaction-statement:
40476 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40477 compound-statement
40478 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40479 */
40480
40481 static tree
40482 cp_parser_transaction (cp_parser *parser, cp_token *token)
40483 {
40484 unsigned char old_in = parser->in_transaction;
40485 unsigned char this_in = 1, new_in;
40486 enum rid keyword = token->keyword;
40487 tree stmt, attrs, noex;
40488
40489 cp_lexer_consume_token (parser->lexer);
40490
40491 if (keyword == RID_TRANSACTION_RELAXED
40492 || keyword == RID_SYNCHRONIZED)
40493 this_in |= TM_STMT_ATTR_RELAXED;
40494 else
40495 {
40496 attrs = cp_parser_txn_attribute_opt (parser);
40497 if (attrs)
40498 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40499 }
40500
40501 /* Parse a noexcept specification. */
40502 if (keyword == RID_ATOMIC_NOEXCEPT)
40503 noex = boolean_true_node;
40504 else if (keyword == RID_ATOMIC_CANCEL)
40505 {
40506 /* cancel-and-throw is unimplemented. */
40507 sorry ("atomic_cancel");
40508 noex = NULL_TREE;
40509 }
40510 else
40511 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
40512
40513 /* Keep track if we're in the lexical scope of an outer transaction. */
40514 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
40515
40516 stmt = begin_transaction_stmt (token->location, NULL, this_in);
40517
40518 parser->in_transaction = new_in;
40519 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
40520 parser->in_transaction = old_in;
40521
40522 finish_transaction_stmt (stmt, NULL, this_in, noex);
40523
40524 return stmt;
40525 }
40526
40527 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40528
40529 transaction-expression:
40530 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40531 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40532 */
40533
40534 static tree
40535 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
40536 {
40537 unsigned char old_in = parser->in_transaction;
40538 unsigned char this_in = 1;
40539 cp_token *token;
40540 tree expr, noex;
40541 bool noex_expr;
40542 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40543
40544 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40545 || keyword == RID_TRANSACTION_RELAXED);
40546
40547 if (!flag_tm)
40548 error_at (loc,
40549 keyword == RID_TRANSACTION_RELAXED
40550 ? G_("%<__transaction_relaxed%> without transactional memory "
40551 "support enabled")
40552 : G_("%<__transaction_atomic%> without transactional memory "
40553 "support enabled"));
40554
40555 token = cp_parser_require_keyword (parser, keyword,
40556 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40557 : RT_TRANSACTION_RELAXED));
40558 gcc_assert (token != NULL);
40559
40560 if (keyword == RID_TRANSACTION_RELAXED)
40561 this_in |= TM_STMT_ATTR_RELAXED;
40562
40563 /* Set this early. This might mean that we allow transaction_cancel in
40564 an expression that we find out later actually has to be a constexpr.
40565 However, we expect that cxx_constant_value will be able to deal with
40566 this; also, if the noexcept has no constexpr, then what we parse next
40567 really is a transaction's body. */
40568 parser->in_transaction = this_in;
40569
40570 /* Parse a noexcept specification. */
40571 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
40572 true);
40573
40574 if (!noex || !noex_expr
40575 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40576 {
40577 matching_parens parens;
40578 parens.require_open (parser);
40579
40580 expr = cp_parser_expression (parser);
40581 expr = finish_parenthesized_expr (expr);
40582
40583 parens.require_close (parser);
40584 }
40585 else
40586 {
40587 /* The only expression that is available got parsed for the noexcept
40588 already. noexcept is true then. */
40589 expr = noex;
40590 noex = boolean_true_node;
40591 }
40592
40593 expr = build_transaction_expr (token->location, expr, this_in, noex);
40594 parser->in_transaction = old_in;
40595
40596 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
40597 return error_mark_node;
40598
40599 return (flag_tm ? expr : error_mark_node);
40600 }
40601
40602 /* Parse a function-transaction-block.
40603
40604 function-transaction-block:
40605 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40606 function-body
40607 __transaction_atomic txn-attribute[opt] function-try-block
40608 __transaction_relaxed ctor-initializer[opt] function-body
40609 __transaction_relaxed function-try-block
40610 */
40611
40612 static void
40613 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
40614 {
40615 unsigned char old_in = parser->in_transaction;
40616 unsigned char new_in = 1;
40617 tree compound_stmt, stmt, attrs;
40618 cp_token *token;
40619
40620 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40621 || keyword == RID_TRANSACTION_RELAXED);
40622 token = cp_parser_require_keyword (parser, keyword,
40623 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40624 : RT_TRANSACTION_RELAXED));
40625 gcc_assert (token != NULL);
40626
40627 if (keyword == RID_TRANSACTION_RELAXED)
40628 new_in |= TM_STMT_ATTR_RELAXED;
40629 else
40630 {
40631 attrs = cp_parser_txn_attribute_opt (parser);
40632 if (attrs)
40633 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40634 }
40635
40636 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
40637
40638 parser->in_transaction = new_in;
40639
40640 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
40641 cp_parser_function_try_block (parser);
40642 else
40643 cp_parser_ctor_initializer_opt_and_function_body
40644 (parser, /*in_function_try_block=*/false);
40645
40646 parser->in_transaction = old_in;
40647
40648 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
40649 }
40650
40651 /* Parse a __transaction_cancel statement.
40652
40653 cancel-statement:
40654 __transaction_cancel txn-attribute[opt] ;
40655 __transaction_cancel txn-attribute[opt] throw-expression ;
40656
40657 ??? Cancel and throw is not yet implemented. */
40658
40659 static tree
40660 cp_parser_transaction_cancel (cp_parser *parser)
40661 {
40662 cp_token *token;
40663 bool is_outer = false;
40664 tree stmt, attrs;
40665
40666 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
40667 RT_TRANSACTION_CANCEL);
40668 gcc_assert (token != NULL);
40669
40670 attrs = cp_parser_txn_attribute_opt (parser);
40671 if (attrs)
40672 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
40673
40674 /* ??? Parse cancel-and-throw here. */
40675
40676 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40677
40678 if (!flag_tm)
40679 {
40680 error_at (token->location, "%<__transaction_cancel%> without "
40681 "transactional memory support enabled");
40682 return error_mark_node;
40683 }
40684 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
40685 {
40686 error_at (token->location, "%<__transaction_cancel%> within a "
40687 "%<__transaction_relaxed%>");
40688 return error_mark_node;
40689 }
40690 else if (is_outer)
40691 {
40692 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40693 && !is_tm_may_cancel_outer (current_function_decl))
40694 {
40695 error_at (token->location, "outer %<__transaction_cancel%> not "
40696 "within outer %<__transaction_atomic%>");
40697 error_at (token->location,
40698 " or a %<transaction_may_cancel_outer%> function");
40699 return error_mark_node;
40700 }
40701 }
40702 else if (parser->in_transaction == 0)
40703 {
40704 error_at (token->location, "%<__transaction_cancel%> not within "
40705 "%<__transaction_atomic%>");
40706 return error_mark_node;
40707 }
40708
40709 stmt = build_tm_abort_call (token->location, is_outer);
40710 add_stmt (stmt);
40711
40712 return stmt;
40713 }
40714 \f
40715 /* The parser. */
40716
40717 static GTY (()) cp_parser *the_parser;
40718
40719 \f
40720 /* Special handling for the first token or line in the file. The first
40721 thing in the file might be #pragma GCC pch_preprocess, which loads a
40722 PCH file, which is a GC collection point. So we need to handle this
40723 first pragma without benefit of an existing lexer structure.
40724
40725 Always returns one token to the caller in *FIRST_TOKEN. This is
40726 either the true first token of the file, or the first token after
40727 the initial pragma. */
40728
40729 static void
40730 cp_parser_initial_pragma (cp_token *first_token)
40731 {
40732 tree name = NULL;
40733
40734 cp_lexer_get_preprocessor_token (NULL, first_token);
40735 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40736 return;
40737
40738 cp_lexer_get_preprocessor_token (NULL, first_token);
40739 if (first_token->type == CPP_STRING)
40740 {
40741 name = first_token->u.value;
40742
40743 cp_lexer_get_preprocessor_token (NULL, first_token);
40744 if (first_token->type != CPP_PRAGMA_EOL)
40745 error_at (first_token->location,
40746 "junk at end of %<#pragma GCC pch_preprocess%>");
40747 }
40748 else
40749 error_at (first_token->location, "expected string literal");
40750
40751 /* Skip to the end of the pragma. */
40752 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40753 cp_lexer_get_preprocessor_token (NULL, first_token);
40754
40755 /* Now actually load the PCH file. */
40756 if (name)
40757 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40758
40759 /* Read one more token to return to our caller. We have to do this
40760 after reading the PCH file in, since its pointers have to be
40761 live. */
40762 cp_lexer_get_preprocessor_token (NULL, first_token);
40763 }
40764
40765 /* Parse a pragma GCC ivdep. */
40766
40767 static bool
40768 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40769 {
40770 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40771 return true;
40772 }
40773
40774 /* Parse a pragma GCC unroll. */
40775
40776 static unsigned short
40777 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40778 {
40779 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40780 tree expr = cp_parser_constant_expression (parser);
40781 unsigned short unroll;
40782 expr = maybe_constant_value (expr);
40783 HOST_WIDE_INT lunroll = 0;
40784 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40785 || TREE_CODE (expr) != INTEGER_CST
40786 || (lunroll = tree_to_shwi (expr)) < 0
40787 || lunroll >= USHRT_MAX)
40788 {
40789 error_at (location, "%<#pragma GCC unroll%> requires an"
40790 " assignment-expression that evaluates to a non-negative"
40791 " integral constant less than %u", USHRT_MAX);
40792 unroll = 0;
40793 }
40794 else
40795 {
40796 unroll = (unsigned short)lunroll;
40797 if (unroll == 0)
40798 unroll = 1;
40799 }
40800 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40801 return unroll;
40802 }
40803
40804 /* Normal parsing of a pragma token. Here we can (and must) use the
40805 regular lexer. */
40806
40807 static bool
40808 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40809 {
40810 cp_token *pragma_tok;
40811 unsigned int id;
40812 tree stmt;
40813 bool ret;
40814
40815 pragma_tok = cp_lexer_consume_token (parser->lexer);
40816 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40817 parser->lexer->in_pragma = true;
40818
40819 id = cp_parser_pragma_kind (pragma_tok);
40820 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40821 cp_ensure_no_omp_declare_simd (parser);
40822 switch (id)
40823 {
40824 case PRAGMA_GCC_PCH_PREPROCESS:
40825 error_at (pragma_tok->location,
40826 "%<#pragma GCC pch_preprocess%> must be first");
40827 break;
40828
40829 case PRAGMA_OMP_BARRIER:
40830 switch (context)
40831 {
40832 case pragma_compound:
40833 cp_parser_omp_barrier (parser, pragma_tok);
40834 return false;
40835 case pragma_stmt:
40836 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40837 "used in compound statements", "omp barrier");
40838 break;
40839 default:
40840 goto bad_stmt;
40841 }
40842 break;
40843
40844 case PRAGMA_OMP_DEPOBJ:
40845 switch (context)
40846 {
40847 case pragma_compound:
40848 cp_parser_omp_depobj (parser, pragma_tok);
40849 return false;
40850 case pragma_stmt:
40851 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40852 "used in compound statements", "omp depobj");
40853 break;
40854 default:
40855 goto bad_stmt;
40856 }
40857 break;
40858
40859 case PRAGMA_OMP_FLUSH:
40860 switch (context)
40861 {
40862 case pragma_compound:
40863 cp_parser_omp_flush (parser, pragma_tok);
40864 return false;
40865 case pragma_stmt:
40866 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40867 "used in compound statements", "omp flush");
40868 break;
40869 default:
40870 goto bad_stmt;
40871 }
40872 break;
40873
40874 case PRAGMA_OMP_TASKWAIT:
40875 switch (context)
40876 {
40877 case pragma_compound:
40878 cp_parser_omp_taskwait (parser, pragma_tok);
40879 return false;
40880 case pragma_stmt:
40881 error_at (pragma_tok->location,
40882 "%<#pragma %s%> may only be used in compound statements",
40883 "omp taskwait");
40884 break;
40885 default:
40886 goto bad_stmt;
40887 }
40888 break;
40889
40890 case PRAGMA_OMP_TASKYIELD:
40891 switch (context)
40892 {
40893 case pragma_compound:
40894 cp_parser_omp_taskyield (parser, pragma_tok);
40895 return false;
40896 case pragma_stmt:
40897 error_at (pragma_tok->location,
40898 "%<#pragma %s%> may only be used in compound statements",
40899 "omp taskyield");
40900 break;
40901 default:
40902 goto bad_stmt;
40903 }
40904 break;
40905
40906 case PRAGMA_OMP_CANCEL:
40907 switch (context)
40908 {
40909 case pragma_compound:
40910 cp_parser_omp_cancel (parser, pragma_tok);
40911 return false;
40912 case pragma_stmt:
40913 error_at (pragma_tok->location,
40914 "%<#pragma %s%> may only be used in compound statements",
40915 "omp cancel");
40916 break;
40917 default:
40918 goto bad_stmt;
40919 }
40920 break;
40921
40922 case PRAGMA_OMP_CANCELLATION_POINT:
40923 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
40924 return false;
40925
40926 case PRAGMA_OMP_THREADPRIVATE:
40927 cp_parser_omp_threadprivate (parser, pragma_tok);
40928 return false;
40929
40930 case PRAGMA_OMP_DECLARE:
40931 return cp_parser_omp_declare (parser, pragma_tok, context);
40932
40933 case PRAGMA_OACC_DECLARE:
40934 cp_parser_oacc_declare (parser, pragma_tok);
40935 return false;
40936
40937 case PRAGMA_OACC_ENTER_DATA:
40938 if (context == pragma_stmt)
40939 {
40940 error_at (pragma_tok->location,
40941 "%<#pragma %s%> may only be used in compound statements",
40942 "acc enter data");
40943 break;
40944 }
40945 else if (context != pragma_compound)
40946 goto bad_stmt;
40947 cp_parser_omp_construct (parser, pragma_tok, if_p);
40948 return true;
40949
40950 case PRAGMA_OACC_EXIT_DATA:
40951 if (context == pragma_stmt)
40952 {
40953 error_at (pragma_tok->location,
40954 "%<#pragma %s%> may only be used in compound statements",
40955 "acc exit data");
40956 break;
40957 }
40958 else if (context != pragma_compound)
40959 goto bad_stmt;
40960 cp_parser_omp_construct (parser, pragma_tok, if_p);
40961 return true;
40962
40963 case PRAGMA_OACC_ROUTINE:
40964 if (context != pragma_external)
40965 {
40966 error_at (pragma_tok->location,
40967 "%<#pragma acc routine%> must be at file scope");
40968 break;
40969 }
40970 cp_parser_oacc_routine (parser, pragma_tok, context);
40971 return false;
40972
40973 case PRAGMA_OACC_UPDATE:
40974 if (context == pragma_stmt)
40975 {
40976 error_at (pragma_tok->location,
40977 "%<#pragma %s%> may only be used in compound statements",
40978 "acc update");
40979 break;
40980 }
40981 else if (context != pragma_compound)
40982 goto bad_stmt;
40983 cp_parser_omp_construct (parser, pragma_tok, if_p);
40984 return true;
40985
40986 case PRAGMA_OACC_WAIT:
40987 if (context == pragma_stmt)
40988 {
40989 error_at (pragma_tok->location,
40990 "%<#pragma %s%> may only be used in compound statements",
40991 "acc wait");
40992 break;
40993 }
40994 else if (context != pragma_compound)
40995 goto bad_stmt;
40996 cp_parser_omp_construct (parser, pragma_tok, if_p);
40997 return true;
40998
40999 case PRAGMA_OACC_ATOMIC:
41000 case PRAGMA_OACC_CACHE:
41001 case PRAGMA_OACC_DATA:
41002 case PRAGMA_OACC_HOST_DATA:
41003 case PRAGMA_OACC_KERNELS:
41004 case PRAGMA_OACC_PARALLEL:
41005 case PRAGMA_OACC_LOOP:
41006 case PRAGMA_OMP_ATOMIC:
41007 case PRAGMA_OMP_CRITICAL:
41008 case PRAGMA_OMP_DISTRIBUTE:
41009 case PRAGMA_OMP_FOR:
41010 case PRAGMA_OMP_MASTER:
41011 case PRAGMA_OMP_PARALLEL:
41012 case PRAGMA_OMP_SECTIONS:
41013 case PRAGMA_OMP_SIMD:
41014 case PRAGMA_OMP_SINGLE:
41015 case PRAGMA_OMP_TASK:
41016 case PRAGMA_OMP_TASKGROUP:
41017 case PRAGMA_OMP_TASKLOOP:
41018 case PRAGMA_OMP_TEAMS:
41019 if (context != pragma_stmt && context != pragma_compound)
41020 goto bad_stmt;
41021 stmt = push_omp_privatization_clauses (false);
41022 cp_parser_omp_construct (parser, pragma_tok, if_p);
41023 pop_omp_privatization_clauses (stmt);
41024 return true;
41025
41026 case PRAGMA_OMP_REQUIRES:
41027 return cp_parser_omp_requires (parser, pragma_tok);
41028
41029 case PRAGMA_OMP_ORDERED:
41030 if (context != pragma_stmt && context != pragma_compound)
41031 goto bad_stmt;
41032 stmt = push_omp_privatization_clauses (false);
41033 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
41034 pop_omp_privatization_clauses (stmt);
41035 return ret;
41036
41037 case PRAGMA_OMP_TARGET:
41038 if (context != pragma_stmt && context != pragma_compound)
41039 goto bad_stmt;
41040 stmt = push_omp_privatization_clauses (false);
41041 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
41042 pop_omp_privatization_clauses (stmt);
41043 return ret;
41044
41045 case PRAGMA_OMP_END_DECLARE_TARGET:
41046 cp_parser_omp_end_declare_target (parser, pragma_tok);
41047 return false;
41048
41049 case PRAGMA_OMP_SECTION:
41050 error_at (pragma_tok->location,
41051 "%<#pragma omp section%> may only be used in "
41052 "%<#pragma omp sections%> construct");
41053 break;
41054
41055 case PRAGMA_IVDEP:
41056 {
41057 if (context == pragma_external)
41058 {
41059 error_at (pragma_tok->location,
41060 "%<#pragma GCC ivdep%> must be inside a function");
41061 break;
41062 }
41063 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
41064 unsigned short unroll;
41065 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41066 if (tok->type == CPP_PRAGMA
41067 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
41068 {
41069 tok = cp_lexer_consume_token (parser->lexer);
41070 unroll = cp_parser_pragma_unroll (parser, tok);
41071 tok = cp_lexer_peek_token (the_parser->lexer);
41072 }
41073 else
41074 unroll = 0;
41075 if (tok->type != CPP_KEYWORD
41076 || (tok->keyword != RID_FOR
41077 && tok->keyword != RID_WHILE
41078 && tok->keyword != RID_DO))
41079 {
41080 cp_parser_error (parser, "for, while or do statement expected");
41081 return false;
41082 }
41083 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
41084 return true;
41085 }
41086
41087 case PRAGMA_UNROLL:
41088 {
41089 if (context == pragma_external)
41090 {
41091 error_at (pragma_tok->location,
41092 "%<#pragma GCC unroll%> must be inside a function");
41093 break;
41094 }
41095 const unsigned short unroll
41096 = cp_parser_pragma_unroll (parser, pragma_tok);
41097 bool ivdep;
41098 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41099 if (tok->type == CPP_PRAGMA
41100 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
41101 {
41102 tok = cp_lexer_consume_token (parser->lexer);
41103 ivdep = cp_parser_pragma_ivdep (parser, tok);
41104 tok = cp_lexer_peek_token (the_parser->lexer);
41105 }
41106 else
41107 ivdep = false;
41108 if (tok->type != CPP_KEYWORD
41109 || (tok->keyword != RID_FOR
41110 && tok->keyword != RID_WHILE
41111 && tok->keyword != RID_DO))
41112 {
41113 cp_parser_error (parser, "for, while or do statement expected");
41114 return false;
41115 }
41116 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
41117 return true;
41118 }
41119
41120 default:
41121 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
41122 c_invoke_pragma_handler (id);
41123 break;
41124
41125 bad_stmt:
41126 cp_parser_error (parser, "expected declaration specifiers");
41127 break;
41128 }
41129
41130 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41131 return false;
41132 }
41133
41134 /* The interface the pragma parsers have to the lexer. */
41135
41136 enum cpp_ttype
41137 pragma_lex (tree *value, location_t *loc)
41138 {
41139 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41140 enum cpp_ttype ret = tok->type;
41141
41142 *value = tok->u.value;
41143 if (loc)
41144 *loc = tok->location;
41145
41146 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
41147 ret = CPP_EOF;
41148 else if (ret == CPP_STRING)
41149 *value = cp_parser_string_literal (the_parser, false, false);
41150 else
41151 {
41152 if (ret == CPP_KEYWORD)
41153 ret = CPP_NAME;
41154 cp_lexer_consume_token (the_parser->lexer);
41155 }
41156
41157 return ret;
41158 }
41159
41160 \f
41161 /* External interface. */
41162
41163 /* Parse one entire translation unit. */
41164
41165 void
41166 c_parse_file (void)
41167 {
41168 static bool already_called = false;
41169
41170 if (already_called)
41171 fatal_error (input_location,
41172 "inter-module optimizations not implemented for C++");
41173 already_called = true;
41174
41175 the_parser = cp_parser_new ();
41176 push_deferring_access_checks (flag_access_control
41177 ? dk_no_deferred : dk_no_check);
41178 cp_parser_translation_unit (the_parser);
41179 the_parser = NULL;
41180
41181 finish_translation_unit ();
41182 }
41183
41184 /* Create an identifier for a generic parameter type (a synthesized
41185 template parameter implied by `auto' or a concept identifier). */
41186
41187 static GTY(()) int generic_parm_count;
41188 static tree
41189 make_generic_type_name ()
41190 {
41191 char buf[32];
41192 sprintf (buf, "auto:%d", ++generic_parm_count);
41193 return get_identifier (buf);
41194 }
41195
41196 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41197 (creating a new template parameter list if necessary). Returns the newly
41198 created template type parm. */
41199
41200 static tree
41201 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
41202 {
41203 gcc_assert (current_binding_level->kind == sk_function_parms);
41204
41205 /* Before committing to modifying any scope, if we're in an
41206 implicit template scope, and we're trying to synthesize a
41207 constrained parameter, try to find a previous parameter with
41208 the same name. This is the same-type rule for abbreviated
41209 function templates.
41210
41211 NOTE: We can generate implicit parameters when tentatively
41212 parsing a nested name specifier, only to reject that parse
41213 later. However, matching the same template-id as part of a
41214 direct-declarator should generate an identical template
41215 parameter, so this rule will merge them. */
41216 if (parser->implicit_template_scope && constr)
41217 {
41218 tree t = parser->implicit_template_parms;
41219 while (t)
41220 {
41221 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
41222 {
41223 tree d = TREE_VALUE (t);
41224 if (TREE_CODE (d) == PARM_DECL)
41225 /* Return the TEMPLATE_PARM_INDEX. */
41226 d = DECL_INITIAL (d);
41227 return d;
41228 }
41229 t = TREE_CHAIN (t);
41230 }
41231 }
41232
41233 /* We are either continuing a function template that already contains implicit
41234 template parameters, creating a new fully-implicit function template, or
41235 extending an existing explicit function template with implicit template
41236 parameters. */
41237
41238 cp_binding_level *const entry_scope = current_binding_level;
41239
41240 bool become_template = false;
41241 cp_binding_level *parent_scope = 0;
41242
41243 if (parser->implicit_template_scope)
41244 {
41245 gcc_assert (parser->implicit_template_parms);
41246
41247 current_binding_level = parser->implicit_template_scope;
41248 }
41249 else
41250 {
41251 /* Roll back to the existing template parameter scope (in the case of
41252 extending an explicit function template) or introduce a new template
41253 parameter scope ahead of the function parameter scope (or class scope
41254 in the case of out-of-line member definitions). The function scope is
41255 added back after template parameter synthesis below. */
41256
41257 cp_binding_level *scope = entry_scope;
41258
41259 while (scope->kind == sk_function_parms)
41260 {
41261 parent_scope = scope;
41262 scope = scope->level_chain;
41263 }
41264 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
41265 {
41266 /* If not defining a class, then any class scope is a scope level in
41267 an out-of-line member definition. In this case simply wind back
41268 beyond the first such scope to inject the template parameter list.
41269 Otherwise wind back to the class being defined. The latter can
41270 occur in class member friend declarations such as:
41271
41272 class A {
41273 void foo (auto);
41274 };
41275 class B {
41276 friend void A::foo (auto);
41277 };
41278
41279 The template parameter list synthesized for the friend declaration
41280 must be injected in the scope of 'B'. This can also occur in
41281 erroneous cases such as:
41282
41283 struct A {
41284 struct B {
41285 void foo (auto);
41286 };
41287 void B::foo (auto) {}
41288 };
41289
41290 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41291 but, nevertheless, the template parameter list synthesized for the
41292 declarator should be injected into the scope of 'A' as if the
41293 ill-formed template was specified explicitly. */
41294
41295 while (scope->kind == sk_class && !scope->defining_class_p)
41296 {
41297 parent_scope = scope;
41298 scope = scope->level_chain;
41299 }
41300 }
41301
41302 current_binding_level = scope;
41303
41304 if (scope->kind != sk_template_parms
41305 || !function_being_declared_is_template_p (parser))
41306 {
41307 /* Introduce a new template parameter list for implicit template
41308 parameters. */
41309
41310 become_template = true;
41311
41312 parser->implicit_template_scope
41313 = begin_scope (sk_template_parms, NULL);
41314
41315 ++processing_template_decl;
41316
41317 parser->fully_implicit_function_template_p = true;
41318 ++parser->num_template_parameter_lists;
41319 }
41320 else
41321 {
41322 /* Synthesize implicit template parameters at the end of the explicit
41323 template parameter list. */
41324
41325 gcc_assert (current_template_parms);
41326
41327 parser->implicit_template_scope = scope;
41328
41329 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41330 parser->implicit_template_parms
41331 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
41332 }
41333 }
41334
41335 /* Synthesize a new template parameter and track the current template
41336 parameter chain with implicit_template_parms. */
41337
41338 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
41339 tree synth_id = make_generic_type_name ();
41340 tree synth_tmpl_parm;
41341 bool non_type = false;
41342
41343 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
41344 synth_tmpl_parm
41345 = finish_template_type_parm (class_type_node, synth_id);
41346 else if (TREE_CODE (proto) == TEMPLATE_DECL)
41347 synth_tmpl_parm
41348 = finish_constrained_template_template_parm (proto, synth_id);
41349 else
41350 {
41351 synth_tmpl_parm = copy_decl (proto);
41352 DECL_NAME (synth_tmpl_parm) = synth_id;
41353 non_type = true;
41354 }
41355
41356 // Attach the constraint to the parm before processing.
41357 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
41358 TREE_TYPE (node) = constr;
41359 tree new_parm
41360 = process_template_parm (parser->implicit_template_parms,
41361 input_location,
41362 node,
41363 /*non_type=*/non_type,
41364 /*param_pack=*/false);
41365
41366 // Chain the new parameter to the list of implicit parameters.
41367 if (parser->implicit_template_parms)
41368 parser->implicit_template_parms
41369 = TREE_CHAIN (parser->implicit_template_parms);
41370 else
41371 parser->implicit_template_parms = new_parm;
41372
41373 tree new_decl = get_local_decls ();
41374 if (non_type)
41375 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41376 new_decl = DECL_INITIAL (new_decl);
41377
41378 /* If creating a fully implicit function template, start the new implicit
41379 template parameter list with this synthesized type, otherwise grow the
41380 current template parameter list. */
41381
41382 if (become_template)
41383 {
41384 parent_scope->level_chain = current_binding_level;
41385
41386 tree new_parms = make_tree_vec (1);
41387 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
41388 current_template_parms = tree_cons (size_int (processing_template_decl),
41389 new_parms, current_template_parms);
41390 }
41391 else
41392 {
41393 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41394 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
41395 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
41396 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
41397 }
41398
41399 // If the new parameter was constrained, we need to add that to the
41400 // constraints in the template parameter list.
41401 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
41402 {
41403 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
41404 reqs = conjoin_constraints (reqs, req);
41405 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
41406 }
41407
41408 current_binding_level = entry_scope;
41409
41410 return new_decl;
41411 }
41412
41413 /* Finish the declaration of a fully implicit function template. Such a
41414 template has no explicit template parameter list so has not been through the
41415 normal template head and tail processing. synthesize_implicit_template_parm
41416 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41417 provided if the declaration is a class member such that its template
41418 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41419 form is returned. Otherwise NULL_TREE is returned. */
41420
41421 static tree
41422 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
41423 {
41424 gcc_assert (parser->fully_implicit_function_template_p);
41425
41426 if (member_decl_opt && member_decl_opt != error_mark_node
41427 && DECL_VIRTUAL_P (member_decl_opt))
41428 {
41429 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
41430 "implicit templates may not be %<virtual%>");
41431 DECL_VIRTUAL_P (member_decl_opt) = false;
41432 }
41433
41434 if (member_decl_opt)
41435 member_decl_opt = finish_member_template_decl (member_decl_opt);
41436 end_template_decl ();
41437
41438 parser->fully_implicit_function_template_p = false;
41439 parser->implicit_template_parms = 0;
41440 parser->implicit_template_scope = 0;
41441 --parser->num_template_parameter_lists;
41442
41443 return member_decl_opt;
41444 }
41445
41446 /* Like finish_fully_implicit_template, but to be used in error
41447 recovery, rearranging scopes so that we restore the state we had
41448 before synthesize_implicit_template_parm inserted the implement
41449 template parms scope. */
41450
41451 static void
41452 abort_fully_implicit_template (cp_parser *parser)
41453 {
41454 cp_binding_level *return_to_scope = current_binding_level;
41455
41456 if (parser->implicit_template_scope
41457 && return_to_scope != parser->implicit_template_scope)
41458 {
41459 cp_binding_level *child = return_to_scope;
41460 for (cp_binding_level *scope = child->level_chain;
41461 scope != parser->implicit_template_scope;
41462 scope = child->level_chain)
41463 child = scope;
41464 child->level_chain = parser->implicit_template_scope->level_chain;
41465 parser->implicit_template_scope->level_chain = return_to_scope;
41466 current_binding_level = parser->implicit_template_scope;
41467 }
41468 else
41469 return_to_scope = return_to_scope->level_chain;
41470
41471 finish_fully_implicit_template (parser, NULL);
41472
41473 gcc_assert (current_binding_level == return_to_scope);
41474 }
41475
41476 /* Helper function for diagnostics that have complained about things
41477 being used with 'extern "C"' linkage.
41478
41479 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41480
41481 void
41482 maybe_show_extern_c_location (void)
41483 {
41484 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
41485 inform (the_parser->innermost_linkage_specification_location,
41486 "%<extern \"C\"%> linkage started here");
41487 }
41488
41489 #include "gt-cp-parser.h"